trans.c 86 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078
  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. * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of version 2 of the GNU General Public License as
  14. * published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful, but
  17. * WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
  24. * USA
  25. *
  26. * The full GNU General Public License is included in this distribution
  27. * in the file called COPYING.
  28. *
  29. * Contact Information:
  30. * Intel Linux Wireless <linuxwifi@intel.com>
  31. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  32. *
  33. * BSD LICENSE
  34. *
  35. * Copyright(c) 2005 - 2015 Intel Corporation. All rights reserved.
  36. * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
  37. * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
  38. * All rights reserved.
  39. *
  40. * Redistribution and use in source and binary forms, with or without
  41. * modification, are permitted provided that the following conditions
  42. * are met:
  43. *
  44. * * Redistributions of source code must retain the above copyright
  45. * notice, this list of conditions and the following disclaimer.
  46. * * Redistributions in binary form must reproduce the above copyright
  47. * notice, this list of conditions and the following disclaimer in
  48. * the documentation and/or other materials provided with the
  49. * distribution.
  50. * * Neither the name Intel Corporation nor the names of its
  51. * contributors may be used to endorse or promote products derived
  52. * from this software without specific prior written permission.
  53. *
  54. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  55. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  56. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  57. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  58. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  59. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  60. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  61. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  62. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  63. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  64. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  65. *
  66. *****************************************************************************/
  67. #include <linux/pci.h>
  68. #include <linux/pci-aspm.h>
  69. #include <linux/interrupt.h>
  70. #include <linux/debugfs.h>
  71. #include <linux/sched.h>
  72. #include <linux/bitops.h>
  73. #include <linux/gfp.h>
  74. #include <linux/vmalloc.h>
  75. #include <linux/pm_runtime.h>
  76. #include "iwl-drv.h"
  77. #include "iwl-trans.h"
  78. #include "iwl-csr.h"
  79. #include "iwl-prph.h"
  80. #include "iwl-scd.h"
  81. #include "iwl-agn-hw.h"
  82. #include "iwl-fw-error-dump.h"
  83. #include "internal.h"
  84. #include "iwl-fh.h"
  85. /* extended range in FW SRAM */
  86. #define IWL_FW_MEM_EXTENDED_START 0x40000
  87. #define IWL_FW_MEM_EXTENDED_END 0x57FFF
  88. static void iwl_pcie_free_fw_monitor(struct iwl_trans *trans)
  89. {
  90. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  91. if (!trans_pcie->fw_mon_page)
  92. return;
  93. dma_unmap_page(trans->dev, trans_pcie->fw_mon_phys,
  94. trans_pcie->fw_mon_size, DMA_FROM_DEVICE);
  95. __free_pages(trans_pcie->fw_mon_page,
  96. get_order(trans_pcie->fw_mon_size));
  97. trans_pcie->fw_mon_page = NULL;
  98. trans_pcie->fw_mon_phys = 0;
  99. trans_pcie->fw_mon_size = 0;
  100. }
  101. static void iwl_pcie_alloc_fw_monitor(struct iwl_trans *trans, u8 max_power)
  102. {
  103. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  104. struct page *page = NULL;
  105. dma_addr_t phys;
  106. u32 size = 0;
  107. u8 power;
  108. if (!max_power) {
  109. /* default max_power is maximum */
  110. max_power = 26;
  111. } else {
  112. max_power += 11;
  113. }
  114. if (WARN(max_power > 26,
  115. "External buffer size for monitor is too big %d, check the FW TLV\n",
  116. max_power))
  117. return;
  118. if (trans_pcie->fw_mon_page) {
  119. dma_sync_single_for_device(trans->dev, trans_pcie->fw_mon_phys,
  120. trans_pcie->fw_mon_size,
  121. DMA_FROM_DEVICE);
  122. return;
  123. }
  124. phys = 0;
  125. for (power = max_power; power >= 11; power--) {
  126. int order;
  127. size = BIT(power);
  128. order = get_order(size);
  129. page = alloc_pages(__GFP_COMP | __GFP_NOWARN | __GFP_ZERO,
  130. order);
  131. if (!page)
  132. continue;
  133. phys = dma_map_page(trans->dev, page, 0, PAGE_SIZE << order,
  134. DMA_FROM_DEVICE);
  135. if (dma_mapping_error(trans->dev, phys)) {
  136. __free_pages(page, order);
  137. page = NULL;
  138. continue;
  139. }
  140. IWL_INFO(trans,
  141. "Allocated 0x%08x bytes (order %d) for firmware monitor.\n",
  142. size, order);
  143. break;
  144. }
  145. if (WARN_ON_ONCE(!page))
  146. return;
  147. if (power != max_power)
  148. IWL_ERR(trans,
  149. "Sorry - debug buffer is only %luK while you requested %luK\n",
  150. (unsigned long)BIT(power - 10),
  151. (unsigned long)BIT(max_power - 10));
  152. trans_pcie->fw_mon_page = page;
  153. trans_pcie->fw_mon_phys = phys;
  154. trans_pcie->fw_mon_size = size;
  155. }
  156. static u32 iwl_trans_pcie_read_shr(struct iwl_trans *trans, u32 reg)
  157. {
  158. iwl_write32(trans, HEEP_CTRL_WRD_PCIEX_CTRL_REG,
  159. ((reg & 0x0000ffff) | (2 << 28)));
  160. return iwl_read32(trans, HEEP_CTRL_WRD_PCIEX_DATA_REG);
  161. }
  162. static void iwl_trans_pcie_write_shr(struct iwl_trans *trans, u32 reg, u32 val)
  163. {
  164. iwl_write32(trans, HEEP_CTRL_WRD_PCIEX_DATA_REG, val);
  165. iwl_write32(trans, HEEP_CTRL_WRD_PCIEX_CTRL_REG,
  166. ((reg & 0x0000ffff) | (3 << 28)));
  167. }
  168. static void iwl_pcie_set_pwr(struct iwl_trans *trans, bool vaux)
  169. {
  170. if (trans->cfg->apmg_not_supported)
  171. return;
  172. if (vaux && pci_pme_capable(to_pci_dev(trans->dev), PCI_D3cold))
  173. iwl_set_bits_mask_prph(trans, APMG_PS_CTRL_REG,
  174. APMG_PS_CTRL_VAL_PWR_SRC_VAUX,
  175. ~APMG_PS_CTRL_MSK_PWR_SRC);
  176. else
  177. iwl_set_bits_mask_prph(trans, APMG_PS_CTRL_REG,
  178. APMG_PS_CTRL_VAL_PWR_SRC_VMAIN,
  179. ~APMG_PS_CTRL_MSK_PWR_SRC);
  180. }
  181. /* PCI registers */
  182. #define PCI_CFG_RETRY_TIMEOUT 0x041
  183. void iwl_pcie_apm_config(struct iwl_trans *trans)
  184. {
  185. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  186. u16 lctl;
  187. u16 cap;
  188. /*
  189. * HW bug W/A for instability in PCIe bus L0S->L1 transition.
  190. * Check if BIOS (or OS) enabled L1-ASPM on this device.
  191. * If so (likely), disable L0S, so device moves directly L0->L1;
  192. * costs negligible amount of power savings.
  193. * If not (unlikely), enable L0S, so there is at least some
  194. * power savings, even without L1.
  195. */
  196. pcie_capability_read_word(trans_pcie->pci_dev, PCI_EXP_LNKCTL, &lctl);
  197. if (lctl & PCI_EXP_LNKCTL_ASPM_L1)
  198. iwl_set_bit(trans, CSR_GIO_REG, CSR_GIO_REG_VAL_L0S_ENABLED);
  199. else
  200. iwl_clear_bit(trans, CSR_GIO_REG, CSR_GIO_REG_VAL_L0S_ENABLED);
  201. trans->pm_support = !(lctl & PCI_EXP_LNKCTL_ASPM_L0S);
  202. pcie_capability_read_word(trans_pcie->pci_dev, PCI_EXP_DEVCTL2, &cap);
  203. trans->ltr_enabled = cap & PCI_EXP_DEVCTL2_LTR_EN;
  204. dev_info(trans->dev, "L1 %sabled - LTR %sabled\n",
  205. (lctl & PCI_EXP_LNKCTL_ASPM_L1) ? "En" : "Dis",
  206. trans->ltr_enabled ? "En" : "Dis");
  207. }
  208. /*
  209. * Start up NIC's basic functionality after it has been reset
  210. * (e.g. after platform boot, or shutdown via iwl_pcie_apm_stop())
  211. * NOTE: This does not load uCode nor start the embedded processor
  212. */
  213. static int iwl_pcie_apm_init(struct iwl_trans *trans)
  214. {
  215. int ret = 0;
  216. IWL_DEBUG_INFO(trans, "Init card's basic functions\n");
  217. /*
  218. * Use "set_bit" below rather than "write", to preserve any hardware
  219. * bits already set by default after reset.
  220. */
  221. /* Disable L0S exit timer (platform NMI Work/Around) */
  222. if (trans->cfg->device_family != IWL_DEVICE_FAMILY_8000)
  223. iwl_set_bit(trans, CSR_GIO_CHICKEN_BITS,
  224. CSR_GIO_CHICKEN_BITS_REG_BIT_DIS_L0S_EXIT_TIMER);
  225. /*
  226. * Disable L0s without affecting L1;
  227. * don't wait for ICH L0s (ICH bug W/A)
  228. */
  229. iwl_set_bit(trans, CSR_GIO_CHICKEN_BITS,
  230. CSR_GIO_CHICKEN_BITS_REG_BIT_L1A_NO_L0S_RX);
  231. /* Set FH wait threshold to maximum (HW error during stress W/A) */
  232. iwl_set_bit(trans, CSR_DBG_HPET_MEM_REG, CSR_DBG_HPET_MEM_REG_VAL);
  233. /*
  234. * Enable HAP INTA (interrupt from management bus) to
  235. * wake device's PCI Express link L1a -> L0s
  236. */
  237. iwl_set_bit(trans, CSR_HW_IF_CONFIG_REG,
  238. CSR_HW_IF_CONFIG_REG_BIT_HAP_WAKE_L1A);
  239. iwl_pcie_apm_config(trans);
  240. /* Configure analog phase-lock-loop before activating to D0A */
  241. if (trans->cfg->base_params->pll_cfg)
  242. iwl_set_bit(trans, CSR_ANA_PLL_CFG, CSR50_ANA_PLL_CFG_VAL);
  243. /*
  244. * Set "initialization complete" bit to move adapter from
  245. * D0U* --> D0A* (powered-up active) state.
  246. */
  247. iwl_set_bit(trans, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
  248. /*
  249. * Wait for clock stabilization; once stabilized, access to
  250. * device-internal resources is supported, e.g. iwl_write_prph()
  251. * and accesses to uCode SRAM.
  252. */
  253. ret = iwl_poll_bit(trans, CSR_GP_CNTRL,
  254. CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
  255. CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY, 25000);
  256. if (ret < 0) {
  257. IWL_DEBUG_INFO(trans, "Failed to init the card\n");
  258. goto out;
  259. }
  260. if (trans->cfg->host_interrupt_operation_mode) {
  261. /*
  262. * This is a bit of an abuse - This is needed for 7260 / 3160
  263. * only check host_interrupt_operation_mode even if this is
  264. * not related to host_interrupt_operation_mode.
  265. *
  266. * Enable the oscillator to count wake up time for L1 exit. This
  267. * consumes slightly more power (100uA) - but allows to be sure
  268. * that we wake up from L1 on time.
  269. *
  270. * This looks weird: read twice the same register, discard the
  271. * value, set a bit, and yet again, read that same register
  272. * just to discard the value. But that's the way the hardware
  273. * seems to like it.
  274. */
  275. iwl_read_prph(trans, OSC_CLK);
  276. iwl_read_prph(trans, OSC_CLK);
  277. iwl_set_bits_prph(trans, OSC_CLK, OSC_CLK_FORCE_CONTROL);
  278. iwl_read_prph(trans, OSC_CLK);
  279. iwl_read_prph(trans, OSC_CLK);
  280. }
  281. /*
  282. * Enable DMA clock and wait for it to stabilize.
  283. *
  284. * Write to "CLK_EN_REG"; "1" bits enable clocks, while "0"
  285. * bits do not disable clocks. This preserves any hardware
  286. * bits already set by default in "CLK_CTRL_REG" after reset.
  287. */
  288. if (!trans->cfg->apmg_not_supported) {
  289. iwl_write_prph(trans, APMG_CLK_EN_REG,
  290. APMG_CLK_VAL_DMA_CLK_RQT);
  291. udelay(20);
  292. /* Disable L1-Active */
  293. iwl_set_bits_prph(trans, APMG_PCIDEV_STT_REG,
  294. APMG_PCIDEV_STT_VAL_L1_ACT_DIS);
  295. /* Clear the interrupt in APMG if the NIC is in RFKILL */
  296. iwl_write_prph(trans, APMG_RTC_INT_STT_REG,
  297. APMG_RTC_INT_STT_RFKILL);
  298. }
  299. set_bit(STATUS_DEVICE_ENABLED, &trans->status);
  300. out:
  301. return ret;
  302. }
  303. /*
  304. * Enable LP XTAL to avoid HW bug where device may consume much power if
  305. * FW is not loaded after device reset. LP XTAL is disabled by default
  306. * after device HW reset. Do it only if XTAL is fed by internal source.
  307. * Configure device's "persistence" mode to avoid resetting XTAL again when
  308. * SHRD_HW_RST occurs in S3.
  309. */
  310. static void iwl_pcie_apm_lp_xtal_enable(struct iwl_trans *trans)
  311. {
  312. int ret;
  313. u32 apmg_gp1_reg;
  314. u32 apmg_xtal_cfg_reg;
  315. u32 dl_cfg_reg;
  316. /* Force XTAL ON */
  317. __iwl_trans_pcie_set_bit(trans, CSR_GP_CNTRL,
  318. CSR_GP_CNTRL_REG_FLAG_XTAL_ON);
  319. /* Reset entire device - do controller reset (results in SHRD_HW_RST) */
  320. iwl_set_bit(trans, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
  321. usleep_range(1000, 2000);
  322. /*
  323. * Set "initialization complete" bit to move adapter from
  324. * D0U* --> D0A* (powered-up active) state.
  325. */
  326. iwl_set_bit(trans, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
  327. /*
  328. * Wait for clock stabilization; once stabilized, access to
  329. * device-internal resources is possible.
  330. */
  331. ret = iwl_poll_bit(trans, CSR_GP_CNTRL,
  332. CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
  333. CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
  334. 25000);
  335. if (WARN_ON(ret < 0)) {
  336. IWL_ERR(trans, "Access time out - failed to enable LP XTAL\n");
  337. /* Release XTAL ON request */
  338. __iwl_trans_pcie_clear_bit(trans, CSR_GP_CNTRL,
  339. CSR_GP_CNTRL_REG_FLAG_XTAL_ON);
  340. return;
  341. }
  342. /*
  343. * Clear "disable persistence" to avoid LP XTAL resetting when
  344. * SHRD_HW_RST is applied in S3.
  345. */
  346. iwl_clear_bits_prph(trans, APMG_PCIDEV_STT_REG,
  347. APMG_PCIDEV_STT_VAL_PERSIST_DIS);
  348. /*
  349. * Force APMG XTAL to be active to prevent its disabling by HW
  350. * caused by APMG idle state.
  351. */
  352. apmg_xtal_cfg_reg = iwl_trans_pcie_read_shr(trans,
  353. SHR_APMG_XTAL_CFG_REG);
  354. iwl_trans_pcie_write_shr(trans, SHR_APMG_XTAL_CFG_REG,
  355. apmg_xtal_cfg_reg |
  356. SHR_APMG_XTAL_CFG_XTAL_ON_REQ);
  357. /*
  358. * Reset entire device again - do controller reset (results in
  359. * SHRD_HW_RST). Turn MAC off before proceeding.
  360. */
  361. iwl_set_bit(trans, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
  362. usleep_range(1000, 2000);
  363. /* Enable LP XTAL by indirect access through CSR */
  364. apmg_gp1_reg = iwl_trans_pcie_read_shr(trans, SHR_APMG_GP1_REG);
  365. iwl_trans_pcie_write_shr(trans, SHR_APMG_GP1_REG, apmg_gp1_reg |
  366. SHR_APMG_GP1_WF_XTAL_LP_EN |
  367. SHR_APMG_GP1_CHICKEN_BIT_SELECT);
  368. /* Clear delay line clock power up */
  369. dl_cfg_reg = iwl_trans_pcie_read_shr(trans, SHR_APMG_DL_CFG_REG);
  370. iwl_trans_pcie_write_shr(trans, SHR_APMG_DL_CFG_REG, dl_cfg_reg &
  371. ~SHR_APMG_DL_CFG_DL_CLOCK_POWER_UP);
  372. /*
  373. * Enable persistence mode to avoid LP XTAL resetting when
  374. * SHRD_HW_RST is applied in S3.
  375. */
  376. iwl_set_bit(trans, CSR_HW_IF_CONFIG_REG,
  377. CSR_HW_IF_CONFIG_REG_PERSIST_MODE);
  378. /*
  379. * Clear "initialization complete" bit to move adapter from
  380. * D0A* (powered-up Active) --> D0U* (Uninitialized) state.
  381. */
  382. iwl_clear_bit(trans, CSR_GP_CNTRL,
  383. CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
  384. /* Activates XTAL resources monitor */
  385. __iwl_trans_pcie_set_bit(trans, CSR_MONITOR_CFG_REG,
  386. CSR_MONITOR_XTAL_RESOURCES);
  387. /* Release XTAL ON request */
  388. __iwl_trans_pcie_clear_bit(trans, CSR_GP_CNTRL,
  389. CSR_GP_CNTRL_REG_FLAG_XTAL_ON);
  390. udelay(10);
  391. /* Release APMG XTAL */
  392. iwl_trans_pcie_write_shr(trans, SHR_APMG_XTAL_CFG_REG,
  393. apmg_xtal_cfg_reg &
  394. ~SHR_APMG_XTAL_CFG_XTAL_ON_REQ);
  395. }
  396. int iwl_pcie_apm_stop_master(struct iwl_trans *trans)
  397. {
  398. int ret = 0;
  399. /* stop device's busmaster DMA activity */
  400. iwl_set_bit(trans, CSR_RESET, CSR_RESET_REG_FLAG_STOP_MASTER);
  401. ret = iwl_poll_bit(trans, CSR_RESET,
  402. CSR_RESET_REG_FLAG_MASTER_DISABLED,
  403. CSR_RESET_REG_FLAG_MASTER_DISABLED, 100);
  404. if (ret < 0)
  405. IWL_WARN(trans, "Master Disable Timed Out, 100 usec\n");
  406. IWL_DEBUG_INFO(trans, "stop master\n");
  407. return ret;
  408. }
  409. static void iwl_pcie_apm_stop(struct iwl_trans *trans, bool op_mode_leave)
  410. {
  411. IWL_DEBUG_INFO(trans, "Stop card, put in low power state\n");
  412. if (op_mode_leave) {
  413. if (!test_bit(STATUS_DEVICE_ENABLED, &trans->status))
  414. iwl_pcie_apm_init(trans);
  415. /* inform ME that we are leaving */
  416. if (trans->cfg->device_family == IWL_DEVICE_FAMILY_7000)
  417. iwl_set_bits_prph(trans, APMG_PCIDEV_STT_REG,
  418. APMG_PCIDEV_STT_VAL_WAKE_ME);
  419. else if (trans->cfg->device_family == IWL_DEVICE_FAMILY_8000) {
  420. iwl_set_bit(trans, CSR_DBG_LINK_PWR_MGMT_REG,
  421. CSR_RESET_LINK_PWR_MGMT_DISABLED);
  422. iwl_set_bit(trans, CSR_HW_IF_CONFIG_REG,
  423. CSR_HW_IF_CONFIG_REG_PREPARE |
  424. CSR_HW_IF_CONFIG_REG_ENABLE_PME);
  425. mdelay(1);
  426. iwl_clear_bit(trans, CSR_DBG_LINK_PWR_MGMT_REG,
  427. CSR_RESET_LINK_PWR_MGMT_DISABLED);
  428. }
  429. mdelay(5);
  430. }
  431. clear_bit(STATUS_DEVICE_ENABLED, &trans->status);
  432. /* Stop device's DMA activity */
  433. iwl_pcie_apm_stop_master(trans);
  434. if (trans->cfg->lp_xtal_workaround) {
  435. iwl_pcie_apm_lp_xtal_enable(trans);
  436. return;
  437. }
  438. /* Reset the entire device */
  439. iwl_set_bit(trans, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
  440. usleep_range(1000, 2000);
  441. /*
  442. * Clear "initialization complete" bit to move adapter from
  443. * D0A* (powered-up Active) --> D0U* (Uninitialized) state.
  444. */
  445. iwl_clear_bit(trans, CSR_GP_CNTRL,
  446. CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
  447. }
  448. static int iwl_pcie_nic_init(struct iwl_trans *trans)
  449. {
  450. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  451. /* nic_init */
  452. spin_lock(&trans_pcie->irq_lock);
  453. iwl_pcie_apm_init(trans);
  454. spin_unlock(&trans_pcie->irq_lock);
  455. iwl_pcie_set_pwr(trans, false);
  456. iwl_op_mode_nic_config(trans->op_mode);
  457. /* Allocate the RX queue, or reset if it is already allocated */
  458. iwl_pcie_rx_init(trans);
  459. /* Allocate or reset and init all Tx and Command queues */
  460. if (iwl_pcie_tx_init(trans))
  461. return -ENOMEM;
  462. if (trans->cfg->base_params->shadow_reg_enable) {
  463. /* enable shadow regs in HW */
  464. iwl_set_bit(trans, CSR_MAC_SHADOW_REG_CTRL, 0x800FFFFF);
  465. IWL_DEBUG_INFO(trans, "Enabling shadow registers in device\n");
  466. }
  467. return 0;
  468. }
  469. #define HW_READY_TIMEOUT (50)
  470. /* Note: returns poll_bit return value, which is >= 0 if success */
  471. static int iwl_pcie_set_hw_ready(struct iwl_trans *trans)
  472. {
  473. int ret;
  474. iwl_set_bit(trans, CSR_HW_IF_CONFIG_REG,
  475. CSR_HW_IF_CONFIG_REG_BIT_NIC_READY);
  476. /* See if we got it */
  477. ret = iwl_poll_bit(trans, CSR_HW_IF_CONFIG_REG,
  478. CSR_HW_IF_CONFIG_REG_BIT_NIC_READY,
  479. CSR_HW_IF_CONFIG_REG_BIT_NIC_READY,
  480. HW_READY_TIMEOUT);
  481. if (ret >= 0)
  482. iwl_set_bit(trans, CSR_MBOX_SET_REG, CSR_MBOX_SET_REG_OS_ALIVE);
  483. IWL_DEBUG_INFO(trans, "hardware%s ready\n", ret < 0 ? " not" : "");
  484. return ret;
  485. }
  486. /* Note: returns standard 0/-ERROR code */
  487. int iwl_pcie_prepare_card_hw(struct iwl_trans *trans)
  488. {
  489. int ret;
  490. int t = 0;
  491. int iter;
  492. IWL_DEBUG_INFO(trans, "iwl_trans_prepare_card_hw enter\n");
  493. ret = iwl_pcie_set_hw_ready(trans);
  494. /* If the card is ready, exit 0 */
  495. if (ret >= 0)
  496. return 0;
  497. iwl_set_bit(trans, CSR_DBG_LINK_PWR_MGMT_REG,
  498. CSR_RESET_LINK_PWR_MGMT_DISABLED);
  499. usleep_range(1000, 2000);
  500. for (iter = 0; iter < 10; iter++) {
  501. /* If HW is not ready, prepare the conditions to check again */
  502. iwl_set_bit(trans, CSR_HW_IF_CONFIG_REG,
  503. CSR_HW_IF_CONFIG_REG_PREPARE);
  504. do {
  505. ret = iwl_pcie_set_hw_ready(trans);
  506. if (ret >= 0)
  507. return 0;
  508. usleep_range(200, 1000);
  509. t += 200;
  510. } while (t < 150000);
  511. msleep(25);
  512. }
  513. IWL_ERR(trans, "Couldn't prepare the card\n");
  514. return ret;
  515. }
  516. /*
  517. * ucode
  518. */
  519. static void iwl_pcie_load_firmware_chunk_fh(struct iwl_trans *trans,
  520. u32 dst_addr, dma_addr_t phy_addr,
  521. u32 byte_cnt)
  522. {
  523. iwl_write32(trans, FH_TCSR_CHNL_TX_CONFIG_REG(FH_SRVC_CHNL),
  524. FH_TCSR_TX_CONFIG_REG_VAL_DMA_CHNL_PAUSE);
  525. iwl_write32(trans, FH_SRVC_CHNL_SRAM_ADDR_REG(FH_SRVC_CHNL),
  526. dst_addr);
  527. iwl_write32(trans, FH_TFDIB_CTRL0_REG(FH_SRVC_CHNL),
  528. phy_addr & FH_MEM_TFDIB_DRAM_ADDR_LSB_MSK);
  529. iwl_write32(trans, FH_TFDIB_CTRL1_REG(FH_SRVC_CHNL),
  530. (iwl_get_dma_hi_addr(phy_addr)
  531. << FH_MEM_TFDIB_REG1_ADDR_BITSHIFT) | byte_cnt);
  532. iwl_write32(trans, FH_TCSR_CHNL_TX_BUF_STS_REG(FH_SRVC_CHNL),
  533. BIT(FH_TCSR_CHNL_TX_BUF_STS_REG_POS_TB_NUM) |
  534. BIT(FH_TCSR_CHNL_TX_BUF_STS_REG_POS_TB_IDX) |
  535. FH_TCSR_CHNL_TX_BUF_STS_REG_VAL_TFDB_VALID);
  536. iwl_write32(trans, FH_TCSR_CHNL_TX_CONFIG_REG(FH_SRVC_CHNL),
  537. FH_TCSR_TX_CONFIG_REG_VAL_DMA_CHNL_ENABLE |
  538. FH_TCSR_TX_CONFIG_REG_VAL_DMA_CREDIT_DISABLE |
  539. FH_TCSR_TX_CONFIG_REG_VAL_CIRQ_HOST_ENDTFD);
  540. }
  541. static int iwl_pcie_load_firmware_chunk(struct iwl_trans *trans,
  542. u32 dst_addr, dma_addr_t phy_addr,
  543. u32 byte_cnt)
  544. {
  545. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  546. unsigned long flags;
  547. int ret;
  548. trans_pcie->ucode_write_complete = false;
  549. if (!iwl_trans_grab_nic_access(trans, &flags))
  550. return -EIO;
  551. iwl_pcie_load_firmware_chunk_fh(trans, dst_addr, phy_addr,
  552. byte_cnt);
  553. iwl_trans_release_nic_access(trans, &flags);
  554. ret = wait_event_timeout(trans_pcie->ucode_write_waitq,
  555. trans_pcie->ucode_write_complete, 5 * HZ);
  556. if (!ret) {
  557. IWL_ERR(trans, "Failed to load firmware chunk!\n");
  558. return -ETIMEDOUT;
  559. }
  560. return 0;
  561. }
  562. static int iwl_pcie_load_section(struct iwl_trans *trans, u8 section_num,
  563. const struct fw_desc *section)
  564. {
  565. u8 *v_addr;
  566. dma_addr_t p_addr;
  567. u32 offset, chunk_sz = min_t(u32, FH_MEM_TB_MAX_LENGTH, section->len);
  568. int ret = 0;
  569. IWL_DEBUG_FW(trans, "[%d] uCode section being loaded...\n",
  570. section_num);
  571. v_addr = dma_alloc_coherent(trans->dev, chunk_sz, &p_addr,
  572. GFP_KERNEL | __GFP_NOWARN);
  573. if (!v_addr) {
  574. IWL_DEBUG_INFO(trans, "Falling back to small chunks of DMA\n");
  575. chunk_sz = PAGE_SIZE;
  576. v_addr = dma_alloc_coherent(trans->dev, chunk_sz,
  577. &p_addr, GFP_KERNEL);
  578. if (!v_addr)
  579. return -ENOMEM;
  580. }
  581. for (offset = 0; offset < section->len; offset += chunk_sz) {
  582. u32 copy_size, dst_addr;
  583. bool extended_addr = false;
  584. copy_size = min_t(u32, chunk_sz, section->len - offset);
  585. dst_addr = section->offset + offset;
  586. if (dst_addr >= IWL_FW_MEM_EXTENDED_START &&
  587. dst_addr <= IWL_FW_MEM_EXTENDED_END)
  588. extended_addr = true;
  589. if (extended_addr)
  590. iwl_set_bits_prph(trans, LMPM_CHICK,
  591. LMPM_CHICK_EXTENDED_ADDR_SPACE);
  592. memcpy(v_addr, (u8 *)section->data + offset, copy_size);
  593. ret = iwl_pcie_load_firmware_chunk(trans, dst_addr, p_addr,
  594. copy_size);
  595. if (extended_addr)
  596. iwl_clear_bits_prph(trans, LMPM_CHICK,
  597. LMPM_CHICK_EXTENDED_ADDR_SPACE);
  598. if (ret) {
  599. IWL_ERR(trans,
  600. "Could not load the [%d] uCode section\n",
  601. section_num);
  602. break;
  603. }
  604. }
  605. dma_free_coherent(trans->dev, chunk_sz, v_addr, p_addr);
  606. return ret;
  607. }
  608. static int iwl_pcie_load_cpu_sections_8000(struct iwl_trans *trans,
  609. const struct fw_img *image,
  610. int cpu,
  611. int *first_ucode_section)
  612. {
  613. int shift_param;
  614. int i, ret = 0, sec_num = 0x1;
  615. u32 val, last_read_idx = 0;
  616. if (cpu == 1) {
  617. shift_param = 0;
  618. *first_ucode_section = 0;
  619. } else {
  620. shift_param = 16;
  621. (*first_ucode_section)++;
  622. }
  623. for (i = *first_ucode_section; i < image->num_sec; i++) {
  624. last_read_idx = i;
  625. /*
  626. * CPU1_CPU2_SEPARATOR_SECTION delimiter - separate between
  627. * CPU1 to CPU2.
  628. * PAGING_SEPARATOR_SECTION delimiter - separate between
  629. * CPU2 non paged to CPU2 paging sec.
  630. */
  631. if (!image->sec[i].data ||
  632. image->sec[i].offset == CPU1_CPU2_SEPARATOR_SECTION ||
  633. image->sec[i].offset == PAGING_SEPARATOR_SECTION) {
  634. IWL_DEBUG_FW(trans,
  635. "Break since Data not valid or Empty section, sec = %d\n",
  636. i);
  637. break;
  638. }
  639. ret = iwl_pcie_load_section(trans, i, &image->sec[i]);
  640. if (ret)
  641. return ret;
  642. /* Notify ucode of loaded section number and status */
  643. val = iwl_read_direct32(trans, FH_UCODE_LOAD_STATUS);
  644. val = val | (sec_num << shift_param);
  645. iwl_write_direct32(trans, FH_UCODE_LOAD_STATUS, val);
  646. sec_num = (sec_num << 1) | 0x1;
  647. }
  648. *first_ucode_section = last_read_idx;
  649. iwl_enable_interrupts(trans);
  650. if (trans->cfg->use_tfh) {
  651. if (cpu == 1)
  652. iwl_write_prph(trans, UREG_UCODE_LOAD_STATUS,
  653. 0xFFFF);
  654. else
  655. iwl_write_prph(trans, UREG_UCODE_LOAD_STATUS,
  656. 0xFFFFFFFF);
  657. } else {
  658. if (cpu == 1)
  659. iwl_write_direct32(trans, FH_UCODE_LOAD_STATUS,
  660. 0xFFFF);
  661. else
  662. iwl_write_direct32(trans, FH_UCODE_LOAD_STATUS,
  663. 0xFFFFFFFF);
  664. }
  665. return 0;
  666. }
  667. static int iwl_pcie_load_cpu_sections(struct iwl_trans *trans,
  668. const struct fw_img *image,
  669. int cpu,
  670. int *first_ucode_section)
  671. {
  672. int i, ret = 0;
  673. u32 last_read_idx = 0;
  674. if (cpu == 1)
  675. *first_ucode_section = 0;
  676. else
  677. (*first_ucode_section)++;
  678. for (i = *first_ucode_section; i < image->num_sec; i++) {
  679. last_read_idx = i;
  680. /*
  681. * CPU1_CPU2_SEPARATOR_SECTION delimiter - separate between
  682. * CPU1 to CPU2.
  683. * PAGING_SEPARATOR_SECTION delimiter - separate between
  684. * CPU2 non paged to CPU2 paging sec.
  685. */
  686. if (!image->sec[i].data ||
  687. image->sec[i].offset == CPU1_CPU2_SEPARATOR_SECTION ||
  688. image->sec[i].offset == PAGING_SEPARATOR_SECTION) {
  689. IWL_DEBUG_FW(trans,
  690. "Break since Data not valid or Empty section, sec = %d\n",
  691. i);
  692. break;
  693. }
  694. ret = iwl_pcie_load_section(trans, i, &image->sec[i]);
  695. if (ret)
  696. return ret;
  697. }
  698. *first_ucode_section = last_read_idx;
  699. return 0;
  700. }
  701. void iwl_pcie_apply_destination(struct iwl_trans *trans)
  702. {
  703. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  704. const struct iwl_fw_dbg_dest_tlv *dest = trans->dbg_dest_tlv;
  705. int i;
  706. if (dest->version)
  707. IWL_ERR(trans,
  708. "DBG DEST version is %d - expect issues\n",
  709. dest->version);
  710. IWL_INFO(trans, "Applying debug destination %s\n",
  711. get_fw_dbg_mode_string(dest->monitor_mode));
  712. if (dest->monitor_mode == EXTERNAL_MODE)
  713. iwl_pcie_alloc_fw_monitor(trans, dest->size_power);
  714. else
  715. IWL_WARN(trans, "PCI should have external buffer debug\n");
  716. for (i = 0; i < trans->dbg_dest_reg_num; i++) {
  717. u32 addr = le32_to_cpu(dest->reg_ops[i].addr);
  718. u32 val = le32_to_cpu(dest->reg_ops[i].val);
  719. switch (dest->reg_ops[i].op) {
  720. case CSR_ASSIGN:
  721. iwl_write32(trans, addr, val);
  722. break;
  723. case CSR_SETBIT:
  724. iwl_set_bit(trans, addr, BIT(val));
  725. break;
  726. case CSR_CLEARBIT:
  727. iwl_clear_bit(trans, addr, BIT(val));
  728. break;
  729. case PRPH_ASSIGN:
  730. iwl_write_prph(trans, addr, val);
  731. break;
  732. case PRPH_SETBIT:
  733. iwl_set_bits_prph(trans, addr, BIT(val));
  734. break;
  735. case PRPH_CLEARBIT:
  736. iwl_clear_bits_prph(trans, addr, BIT(val));
  737. break;
  738. case PRPH_BLOCKBIT:
  739. if (iwl_read_prph(trans, addr) & BIT(val)) {
  740. IWL_ERR(trans,
  741. "BIT(%u) in address 0x%x is 1, stopping FW configuration\n",
  742. val, addr);
  743. goto monitor;
  744. }
  745. break;
  746. default:
  747. IWL_ERR(trans, "FW debug - unknown OP %d\n",
  748. dest->reg_ops[i].op);
  749. break;
  750. }
  751. }
  752. monitor:
  753. if (dest->monitor_mode == EXTERNAL_MODE && trans_pcie->fw_mon_size) {
  754. iwl_write_prph(trans, le32_to_cpu(dest->base_reg),
  755. trans_pcie->fw_mon_phys >> dest->base_shift);
  756. if (trans->cfg->device_family == IWL_DEVICE_FAMILY_8000)
  757. iwl_write_prph(trans, le32_to_cpu(dest->end_reg),
  758. (trans_pcie->fw_mon_phys +
  759. trans_pcie->fw_mon_size - 256) >>
  760. dest->end_shift);
  761. else
  762. iwl_write_prph(trans, le32_to_cpu(dest->end_reg),
  763. (trans_pcie->fw_mon_phys +
  764. trans_pcie->fw_mon_size) >>
  765. dest->end_shift);
  766. }
  767. }
  768. static int iwl_pcie_load_given_ucode(struct iwl_trans *trans,
  769. const struct fw_img *image)
  770. {
  771. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  772. int ret = 0;
  773. int first_ucode_section;
  774. IWL_DEBUG_FW(trans, "working with %s CPU\n",
  775. image->is_dual_cpus ? "Dual" : "Single");
  776. /* load to FW the binary non secured sections of CPU1 */
  777. ret = iwl_pcie_load_cpu_sections(trans, image, 1, &first_ucode_section);
  778. if (ret)
  779. return ret;
  780. if (image->is_dual_cpus) {
  781. /* set CPU2 header address */
  782. iwl_write_prph(trans,
  783. LMPM_SECURE_UCODE_LOAD_CPU2_HDR_ADDR,
  784. LMPM_SECURE_CPU2_HDR_MEM_SPACE);
  785. /* load to FW the binary sections of CPU2 */
  786. ret = iwl_pcie_load_cpu_sections(trans, image, 2,
  787. &first_ucode_section);
  788. if (ret)
  789. return ret;
  790. }
  791. /* supported for 7000 only for the moment */
  792. if (iwlwifi_mod_params.fw_monitor &&
  793. trans->cfg->device_family == IWL_DEVICE_FAMILY_7000) {
  794. iwl_pcie_alloc_fw_monitor(trans, 0);
  795. if (trans_pcie->fw_mon_size) {
  796. iwl_write_prph(trans, MON_BUFF_BASE_ADDR,
  797. trans_pcie->fw_mon_phys >> 4);
  798. iwl_write_prph(trans, MON_BUFF_END_ADDR,
  799. (trans_pcie->fw_mon_phys +
  800. trans_pcie->fw_mon_size) >> 4);
  801. }
  802. } else if (trans->dbg_dest_tlv) {
  803. iwl_pcie_apply_destination(trans);
  804. }
  805. iwl_enable_interrupts(trans);
  806. /* release CPU reset */
  807. iwl_write32(trans, CSR_RESET, 0);
  808. return 0;
  809. }
  810. static int iwl_pcie_load_given_ucode_8000(struct iwl_trans *trans,
  811. const struct fw_img *image)
  812. {
  813. int ret = 0;
  814. int first_ucode_section;
  815. IWL_DEBUG_FW(trans, "working with %s CPU\n",
  816. image->is_dual_cpus ? "Dual" : "Single");
  817. if (trans->dbg_dest_tlv)
  818. iwl_pcie_apply_destination(trans);
  819. IWL_DEBUG_POWER(trans, "Original WFPM value = 0x%08X\n",
  820. iwl_read_prph(trans, WFPM_GP2));
  821. /*
  822. * Set default value. On resume reading the values that were
  823. * zeored can provide debug data on the resume flow.
  824. * This is for debugging only and has no functional impact.
  825. */
  826. iwl_write_prph(trans, WFPM_GP2, 0x01010101);
  827. /* configure the ucode to be ready to get the secured image */
  828. /* release CPU reset */
  829. iwl_write_prph(trans, RELEASE_CPU_RESET, RELEASE_CPU_RESET_BIT);
  830. /* load to FW the binary Secured sections of CPU1 */
  831. ret = iwl_pcie_load_cpu_sections_8000(trans, image, 1,
  832. &first_ucode_section);
  833. if (ret)
  834. return ret;
  835. /* load to FW the binary sections of CPU2 */
  836. return iwl_pcie_load_cpu_sections_8000(trans, image, 2,
  837. &first_ucode_section);
  838. }
  839. bool iwl_trans_check_hw_rf_kill(struct iwl_trans *trans)
  840. {
  841. bool hw_rfkill = iwl_is_rfkill_set(trans);
  842. if (hw_rfkill)
  843. set_bit(STATUS_RFKILL, &trans->status);
  844. else
  845. clear_bit(STATUS_RFKILL, &trans->status);
  846. iwl_trans_pcie_rf_kill(trans, hw_rfkill);
  847. return hw_rfkill;
  848. }
  849. struct iwl_causes_list {
  850. u32 cause_num;
  851. u32 mask_reg;
  852. u8 addr;
  853. };
  854. static struct iwl_causes_list causes_list[] = {
  855. {MSIX_FH_INT_CAUSES_D2S_CH0_NUM, CSR_MSIX_FH_INT_MASK_AD, 0},
  856. {MSIX_FH_INT_CAUSES_D2S_CH1_NUM, CSR_MSIX_FH_INT_MASK_AD, 0x1},
  857. {MSIX_FH_INT_CAUSES_S2D, CSR_MSIX_FH_INT_MASK_AD, 0x3},
  858. {MSIX_FH_INT_CAUSES_FH_ERR, CSR_MSIX_FH_INT_MASK_AD, 0x5},
  859. {MSIX_HW_INT_CAUSES_REG_ALIVE, CSR_MSIX_HW_INT_MASK_AD, 0x10},
  860. {MSIX_HW_INT_CAUSES_REG_WAKEUP, CSR_MSIX_HW_INT_MASK_AD, 0x11},
  861. {MSIX_HW_INT_CAUSES_REG_CT_KILL, CSR_MSIX_HW_INT_MASK_AD, 0x16},
  862. {MSIX_HW_INT_CAUSES_REG_RF_KILL, CSR_MSIX_HW_INT_MASK_AD, 0x17},
  863. {MSIX_HW_INT_CAUSES_REG_PERIODIC, CSR_MSIX_HW_INT_MASK_AD, 0x18},
  864. {MSIX_HW_INT_CAUSES_REG_SW_ERR, CSR_MSIX_HW_INT_MASK_AD, 0x29},
  865. {MSIX_HW_INT_CAUSES_REG_SCD, CSR_MSIX_HW_INT_MASK_AD, 0x2A},
  866. {MSIX_HW_INT_CAUSES_REG_FH_TX, CSR_MSIX_HW_INT_MASK_AD, 0x2B},
  867. {MSIX_HW_INT_CAUSES_REG_HW_ERR, CSR_MSIX_HW_INT_MASK_AD, 0x2D},
  868. {MSIX_HW_INT_CAUSES_REG_HAP, CSR_MSIX_HW_INT_MASK_AD, 0x2E},
  869. };
  870. static void iwl_pcie_map_non_rx_causes(struct iwl_trans *trans)
  871. {
  872. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  873. int val = trans_pcie->def_irq | MSIX_NON_AUTO_CLEAR_CAUSE;
  874. int i;
  875. /*
  876. * Access all non RX causes and map them to the default irq.
  877. * In case we are missing at least one interrupt vector,
  878. * the first interrupt vector will serve non-RX and FBQ causes.
  879. */
  880. for (i = 0; i < ARRAY_SIZE(causes_list); i++) {
  881. iwl_write8(trans, CSR_MSIX_IVAR(causes_list[i].addr), val);
  882. iwl_clear_bit(trans, causes_list[i].mask_reg,
  883. causes_list[i].cause_num);
  884. }
  885. }
  886. static void iwl_pcie_map_rx_causes(struct iwl_trans *trans)
  887. {
  888. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  889. u32 offset =
  890. trans_pcie->shared_vec_mask & IWL_SHARED_IRQ_FIRST_RSS ? 1 : 0;
  891. u32 val, idx;
  892. /*
  893. * The first RX queue - fallback queue, which is designated for
  894. * management frame, command responses etc, is always mapped to the
  895. * first interrupt vector. The other RX queues are mapped to
  896. * the other (N - 2) interrupt vectors.
  897. */
  898. val = BIT(MSIX_FH_INT_CAUSES_Q(0));
  899. for (idx = 1; idx < trans->num_rx_queues; idx++) {
  900. iwl_write8(trans, CSR_MSIX_RX_IVAR(idx),
  901. MSIX_FH_INT_CAUSES_Q(idx - offset));
  902. val |= BIT(MSIX_FH_INT_CAUSES_Q(idx));
  903. }
  904. iwl_write32(trans, CSR_MSIX_FH_INT_MASK_AD, ~val);
  905. val = MSIX_FH_INT_CAUSES_Q(0);
  906. if (trans_pcie->shared_vec_mask & IWL_SHARED_IRQ_NON_RX)
  907. val |= MSIX_NON_AUTO_CLEAR_CAUSE;
  908. iwl_write8(trans, CSR_MSIX_RX_IVAR(0), val);
  909. if (trans_pcie->shared_vec_mask & IWL_SHARED_IRQ_FIRST_RSS)
  910. iwl_write8(trans, CSR_MSIX_RX_IVAR(1), val);
  911. }
  912. void iwl_pcie_conf_msix_hw(struct iwl_trans_pcie *trans_pcie)
  913. {
  914. struct iwl_trans *trans = trans_pcie->trans;
  915. if (!trans_pcie->msix_enabled) {
  916. if (trans->cfg->mq_rx_supported &&
  917. test_bit(STATUS_DEVICE_ENABLED, &trans->status))
  918. iwl_write_prph(trans, UREG_CHICK,
  919. UREG_CHICK_MSI_ENABLE);
  920. return;
  921. }
  922. /*
  923. * The IVAR table needs to be configured again after reset,
  924. * but if the device is disabled, we can't write to
  925. * prph.
  926. */
  927. if (test_bit(STATUS_DEVICE_ENABLED, &trans->status))
  928. iwl_write_prph(trans, UREG_CHICK, UREG_CHICK_MSIX_ENABLE);
  929. /*
  930. * Each cause from the causes list above and the RX causes is
  931. * represented as a byte in the IVAR table. The first nibble
  932. * represents the bound interrupt vector of the cause, the second
  933. * represents no auto clear for this cause. This will be set if its
  934. * interrupt vector is bound to serve other causes.
  935. */
  936. iwl_pcie_map_rx_causes(trans);
  937. iwl_pcie_map_non_rx_causes(trans);
  938. }
  939. static void iwl_pcie_init_msix(struct iwl_trans_pcie *trans_pcie)
  940. {
  941. struct iwl_trans *trans = trans_pcie->trans;
  942. iwl_pcie_conf_msix_hw(trans_pcie);
  943. if (!trans_pcie->msix_enabled)
  944. return;
  945. trans_pcie->fh_init_mask = ~iwl_read32(trans, CSR_MSIX_FH_INT_MASK_AD);
  946. trans_pcie->fh_mask = trans_pcie->fh_init_mask;
  947. trans_pcie->hw_init_mask = ~iwl_read32(trans, CSR_MSIX_HW_INT_MASK_AD);
  948. trans_pcie->hw_mask = trans_pcie->hw_init_mask;
  949. }
  950. static void _iwl_trans_pcie_stop_device(struct iwl_trans *trans, bool low_power)
  951. {
  952. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  953. bool hw_rfkill, was_hw_rfkill;
  954. lockdep_assert_held(&trans_pcie->mutex);
  955. if (trans_pcie->is_down)
  956. return;
  957. trans_pcie->is_down = true;
  958. was_hw_rfkill = iwl_is_rfkill_set(trans);
  959. /* tell the device to stop sending interrupts */
  960. iwl_disable_interrupts(trans);
  961. /* device going down, Stop using ICT table */
  962. iwl_pcie_disable_ict(trans);
  963. /*
  964. * If a HW restart happens during firmware loading,
  965. * then the firmware loading might call this function
  966. * and later it might be called again due to the
  967. * restart. So don't process again if the device is
  968. * already dead.
  969. */
  970. if (test_and_clear_bit(STATUS_DEVICE_ENABLED, &trans->status)) {
  971. IWL_DEBUG_INFO(trans,
  972. "DEVICE_ENABLED bit was set and is now cleared\n");
  973. iwl_pcie_tx_stop(trans);
  974. iwl_pcie_rx_stop(trans);
  975. /* Power-down device's busmaster DMA clocks */
  976. if (!trans->cfg->apmg_not_supported) {
  977. iwl_write_prph(trans, APMG_CLK_DIS_REG,
  978. APMG_CLK_VAL_DMA_CLK_RQT);
  979. udelay(5);
  980. }
  981. }
  982. /* Make sure (redundant) we've released our request to stay awake */
  983. iwl_clear_bit(trans, CSR_GP_CNTRL,
  984. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  985. /* Stop the device, and put it in low power state */
  986. iwl_pcie_apm_stop(trans, false);
  987. /* stop and reset the on-board processor */
  988. iwl_write32(trans, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
  989. usleep_range(1000, 2000);
  990. /*
  991. * Upon stop, the IVAR table gets erased, so msi-x won't
  992. * work. This causes a bug in RF-KILL flows, since the interrupt
  993. * that enables radio won't fire on the correct irq, and the
  994. * driver won't be able to handle the interrupt.
  995. * Configure the IVAR table again after reset.
  996. */
  997. iwl_pcie_conf_msix_hw(trans_pcie);
  998. /*
  999. * Upon stop, the APM issues an interrupt if HW RF kill is set.
  1000. * This is a bug in certain verions of the hardware.
  1001. * Certain devices also keep sending HW RF kill interrupt all
  1002. * the time, unless the interrupt is ACKed even if the interrupt
  1003. * should be masked. Re-ACK all the interrupts here.
  1004. */
  1005. iwl_disable_interrupts(trans);
  1006. /* clear all status bits */
  1007. clear_bit(STATUS_SYNC_HCMD_ACTIVE, &trans->status);
  1008. clear_bit(STATUS_INT_ENABLED, &trans->status);
  1009. clear_bit(STATUS_TPOWER_PMI, &trans->status);
  1010. clear_bit(STATUS_RFKILL, &trans->status);
  1011. /*
  1012. * Even if we stop the HW, we still want the RF kill
  1013. * interrupt
  1014. */
  1015. iwl_enable_rfkill_int(trans);
  1016. /*
  1017. * Check again since the RF kill state may have changed while
  1018. * all the interrupts were disabled, in this case we couldn't
  1019. * receive the RF kill interrupt and update the state in the
  1020. * op_mode.
  1021. * Don't call the op_mode if the rkfill state hasn't changed.
  1022. * This allows the op_mode to call stop_device from the rfkill
  1023. * notification without endless recursion. Under very rare
  1024. * circumstances, we might have a small recursion if the rfkill
  1025. * state changed exactly now while we were called from stop_device.
  1026. * This is very unlikely but can happen and is supported.
  1027. */
  1028. hw_rfkill = iwl_is_rfkill_set(trans);
  1029. if (hw_rfkill)
  1030. set_bit(STATUS_RFKILL, &trans->status);
  1031. else
  1032. clear_bit(STATUS_RFKILL, &trans->status);
  1033. if (hw_rfkill != was_hw_rfkill)
  1034. iwl_trans_pcie_rf_kill(trans, hw_rfkill);
  1035. /* re-take ownership to prevent other users from stealing the device */
  1036. iwl_pcie_prepare_card_hw(trans);
  1037. }
  1038. void iwl_pcie_synchronize_irqs(struct iwl_trans *trans)
  1039. {
  1040. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1041. if (trans_pcie->msix_enabled) {
  1042. int i;
  1043. for (i = 0; i < trans_pcie->alloc_vecs; i++)
  1044. synchronize_irq(trans_pcie->msix_entries[i].vector);
  1045. } else {
  1046. synchronize_irq(trans_pcie->pci_dev->irq);
  1047. }
  1048. }
  1049. static int iwl_trans_pcie_start_fw(struct iwl_trans *trans,
  1050. const struct fw_img *fw, bool run_in_rfkill)
  1051. {
  1052. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1053. bool hw_rfkill;
  1054. int ret;
  1055. /* This may fail if AMT took ownership of the device */
  1056. if (iwl_pcie_prepare_card_hw(trans)) {
  1057. IWL_WARN(trans, "Exit HW not ready\n");
  1058. ret = -EIO;
  1059. goto out;
  1060. }
  1061. iwl_enable_rfkill_int(trans);
  1062. iwl_write32(trans, CSR_INT, 0xFFFFFFFF);
  1063. /*
  1064. * We enabled the RF-Kill interrupt and the handler may very
  1065. * well be running. Disable the interrupts to make sure no other
  1066. * interrupt can be fired.
  1067. */
  1068. iwl_disable_interrupts(trans);
  1069. /* Make sure it finished running */
  1070. iwl_pcie_synchronize_irqs(trans);
  1071. mutex_lock(&trans_pcie->mutex);
  1072. /* If platform's RF_KILL switch is NOT set to KILL */
  1073. hw_rfkill = iwl_trans_check_hw_rf_kill(trans);
  1074. if (hw_rfkill && !run_in_rfkill) {
  1075. ret = -ERFKILL;
  1076. goto out;
  1077. }
  1078. /* Someone called stop_device, don't try to start_fw */
  1079. if (trans_pcie->is_down) {
  1080. IWL_WARN(trans,
  1081. "Can't start_fw since the HW hasn't been started\n");
  1082. ret = -EIO;
  1083. goto out;
  1084. }
  1085. /* make sure rfkill handshake bits are cleared */
  1086. iwl_write32(trans, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  1087. iwl_write32(trans, CSR_UCODE_DRV_GP1_CLR,
  1088. CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
  1089. /* clear (again), then enable host interrupts */
  1090. iwl_write32(trans, CSR_INT, 0xFFFFFFFF);
  1091. ret = iwl_pcie_nic_init(trans);
  1092. if (ret) {
  1093. IWL_ERR(trans, "Unable to init nic\n");
  1094. goto out;
  1095. }
  1096. /*
  1097. * Now, we load the firmware and don't want to be interrupted, even
  1098. * by the RF-Kill interrupt (hence mask all the interrupt besides the
  1099. * FH_TX interrupt which is needed to load the firmware). If the
  1100. * RF-Kill switch is toggled, we will find out after having loaded
  1101. * the firmware and return the proper value to the caller.
  1102. */
  1103. iwl_enable_fw_load_int(trans);
  1104. /* really make sure rfkill handshake bits are cleared */
  1105. iwl_write32(trans, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  1106. iwl_write32(trans, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  1107. /* Load the given image to the HW */
  1108. if (trans->cfg->device_family == IWL_DEVICE_FAMILY_8000)
  1109. ret = iwl_pcie_load_given_ucode_8000(trans, fw);
  1110. else
  1111. ret = iwl_pcie_load_given_ucode(trans, fw);
  1112. /* re-check RF-Kill state since we may have missed the interrupt */
  1113. hw_rfkill = iwl_trans_check_hw_rf_kill(trans);
  1114. if (hw_rfkill && !run_in_rfkill)
  1115. ret = -ERFKILL;
  1116. out:
  1117. mutex_unlock(&trans_pcie->mutex);
  1118. return ret;
  1119. }
  1120. static void iwl_trans_pcie_fw_alive(struct iwl_trans *trans, u32 scd_addr)
  1121. {
  1122. iwl_pcie_reset_ict(trans);
  1123. iwl_pcie_tx_start(trans, scd_addr);
  1124. }
  1125. static void iwl_trans_pcie_stop_device(struct iwl_trans *trans, bool low_power)
  1126. {
  1127. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1128. mutex_lock(&trans_pcie->mutex);
  1129. _iwl_trans_pcie_stop_device(trans, low_power);
  1130. mutex_unlock(&trans_pcie->mutex);
  1131. }
  1132. void iwl_trans_pcie_rf_kill(struct iwl_trans *trans, bool state)
  1133. {
  1134. struct iwl_trans_pcie __maybe_unused *trans_pcie =
  1135. IWL_TRANS_GET_PCIE_TRANS(trans);
  1136. lockdep_assert_held(&trans_pcie->mutex);
  1137. if (iwl_op_mode_hw_rf_kill(trans->op_mode, state)) {
  1138. if (trans->cfg->gen2)
  1139. _iwl_trans_pcie_gen2_stop_device(trans, true);
  1140. else
  1141. _iwl_trans_pcie_stop_device(trans, true);
  1142. }
  1143. }
  1144. static void iwl_trans_pcie_d3_suspend(struct iwl_trans *trans, bool test,
  1145. bool reset)
  1146. {
  1147. if (!reset) {
  1148. /* Enable persistence mode to avoid reset */
  1149. iwl_set_bit(trans, CSR_HW_IF_CONFIG_REG,
  1150. CSR_HW_IF_CONFIG_REG_PERSIST_MODE);
  1151. }
  1152. iwl_disable_interrupts(trans);
  1153. /*
  1154. * in testing mode, the host stays awake and the
  1155. * hardware won't be reset (not even partially)
  1156. */
  1157. if (test)
  1158. return;
  1159. iwl_pcie_disable_ict(trans);
  1160. iwl_pcie_synchronize_irqs(trans);
  1161. iwl_clear_bit(trans, CSR_GP_CNTRL,
  1162. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  1163. iwl_clear_bit(trans, CSR_GP_CNTRL,
  1164. CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
  1165. iwl_pcie_enable_rx_wake(trans, false);
  1166. if (reset) {
  1167. /*
  1168. * reset TX queues -- some of their registers reset during S3
  1169. * so if we don't reset everything here the D3 image would try
  1170. * to execute some invalid memory upon resume
  1171. */
  1172. iwl_trans_pcie_tx_reset(trans);
  1173. }
  1174. iwl_pcie_set_pwr(trans, true);
  1175. }
  1176. static int iwl_trans_pcie_d3_resume(struct iwl_trans *trans,
  1177. enum iwl_d3_status *status,
  1178. bool test, bool reset)
  1179. {
  1180. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1181. u32 val;
  1182. int ret;
  1183. if (test) {
  1184. iwl_enable_interrupts(trans);
  1185. *status = IWL_D3_STATUS_ALIVE;
  1186. return 0;
  1187. }
  1188. iwl_pcie_enable_rx_wake(trans, true);
  1189. /*
  1190. * Reconfigure IVAR table in case of MSIX or reset ict table in
  1191. * MSI mode since HW reset erased it.
  1192. * Also enables interrupts - none will happen as
  1193. * the device doesn't know we're waking it up, only when
  1194. * the opmode actually tells it after this call.
  1195. */
  1196. iwl_pcie_conf_msix_hw(trans_pcie);
  1197. if (!trans_pcie->msix_enabled)
  1198. iwl_pcie_reset_ict(trans);
  1199. iwl_enable_interrupts(trans);
  1200. iwl_set_bit(trans, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  1201. iwl_set_bit(trans, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
  1202. if (trans->cfg->device_family == IWL_DEVICE_FAMILY_8000)
  1203. udelay(2);
  1204. ret = iwl_poll_bit(trans, CSR_GP_CNTRL,
  1205. CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
  1206. CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
  1207. 25000);
  1208. if (ret < 0) {
  1209. IWL_ERR(trans, "Failed to resume the device (mac ready)\n");
  1210. return ret;
  1211. }
  1212. iwl_pcie_set_pwr(trans, false);
  1213. if (!reset) {
  1214. iwl_clear_bit(trans, CSR_GP_CNTRL,
  1215. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  1216. } else {
  1217. iwl_trans_pcie_tx_reset(trans);
  1218. ret = iwl_pcie_rx_init(trans);
  1219. if (ret) {
  1220. IWL_ERR(trans,
  1221. "Failed to resume the device (RX reset)\n");
  1222. return ret;
  1223. }
  1224. }
  1225. IWL_DEBUG_POWER(trans, "WFPM value upon resume = 0x%08X\n",
  1226. iwl_read_prph(trans, WFPM_GP2));
  1227. val = iwl_read32(trans, CSR_RESET);
  1228. if (val & CSR_RESET_REG_FLAG_NEVO_RESET)
  1229. *status = IWL_D3_STATUS_RESET;
  1230. else
  1231. *status = IWL_D3_STATUS_ALIVE;
  1232. return 0;
  1233. }
  1234. static void iwl_pcie_set_interrupt_capa(struct pci_dev *pdev,
  1235. struct iwl_trans *trans)
  1236. {
  1237. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1238. int max_irqs, num_irqs, i, ret, nr_online_cpus;
  1239. u16 pci_cmd;
  1240. if (!trans->cfg->mq_rx_supported)
  1241. goto enable_msi;
  1242. nr_online_cpus = num_online_cpus();
  1243. max_irqs = min_t(u32, nr_online_cpus + 2, IWL_MAX_RX_HW_QUEUES);
  1244. for (i = 0; i < max_irqs; i++)
  1245. trans_pcie->msix_entries[i].entry = i;
  1246. num_irqs = pci_enable_msix_range(pdev, trans_pcie->msix_entries,
  1247. MSIX_MIN_INTERRUPT_VECTORS,
  1248. max_irqs);
  1249. if (num_irqs < 0) {
  1250. IWL_DEBUG_INFO(trans,
  1251. "Failed to enable msi-x mode (ret %d). Moving to msi mode.\n",
  1252. num_irqs);
  1253. goto enable_msi;
  1254. }
  1255. trans_pcie->def_irq = (num_irqs == max_irqs) ? num_irqs - 1 : 0;
  1256. IWL_DEBUG_INFO(trans,
  1257. "MSI-X enabled. %d interrupt vectors were allocated\n",
  1258. num_irqs);
  1259. /*
  1260. * In case the OS provides fewer interrupts than requested, different
  1261. * causes will share the same interrupt vector as follows:
  1262. * One interrupt less: non rx causes shared with FBQ.
  1263. * Two interrupts less: non rx causes shared with FBQ and RSS.
  1264. * More than two interrupts: we will use fewer RSS queues.
  1265. */
  1266. if (num_irqs <= nr_online_cpus) {
  1267. trans_pcie->trans->num_rx_queues = num_irqs + 1;
  1268. trans_pcie->shared_vec_mask = IWL_SHARED_IRQ_NON_RX |
  1269. IWL_SHARED_IRQ_FIRST_RSS;
  1270. } else if (num_irqs == nr_online_cpus + 1) {
  1271. trans_pcie->trans->num_rx_queues = num_irqs;
  1272. trans_pcie->shared_vec_mask = IWL_SHARED_IRQ_NON_RX;
  1273. } else {
  1274. trans_pcie->trans->num_rx_queues = num_irqs - 1;
  1275. }
  1276. trans_pcie->alloc_vecs = num_irqs;
  1277. trans_pcie->msix_enabled = true;
  1278. return;
  1279. enable_msi:
  1280. ret = pci_enable_msi(pdev);
  1281. if (ret) {
  1282. dev_err(&pdev->dev, "pci_enable_msi failed - %d\n", ret);
  1283. /* enable rfkill interrupt: hw bug w/a */
  1284. pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
  1285. if (pci_cmd & PCI_COMMAND_INTX_DISABLE) {
  1286. pci_cmd &= ~PCI_COMMAND_INTX_DISABLE;
  1287. pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
  1288. }
  1289. }
  1290. }
  1291. static void iwl_pcie_irq_set_affinity(struct iwl_trans *trans)
  1292. {
  1293. int iter_rx_q, i, ret, cpu, offset;
  1294. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1295. i = trans_pcie->shared_vec_mask & IWL_SHARED_IRQ_FIRST_RSS ? 0 : 1;
  1296. iter_rx_q = trans_pcie->trans->num_rx_queues - 1 + i;
  1297. offset = 1 + i;
  1298. for (; i < iter_rx_q ; i++) {
  1299. /*
  1300. * Get the cpu prior to the place to search
  1301. * (i.e. return will be > i - 1).
  1302. */
  1303. cpu = cpumask_next(i - offset, cpu_online_mask);
  1304. cpumask_set_cpu(cpu, &trans_pcie->affinity_mask[i]);
  1305. ret = irq_set_affinity_hint(trans_pcie->msix_entries[i].vector,
  1306. &trans_pcie->affinity_mask[i]);
  1307. if (ret)
  1308. IWL_ERR(trans_pcie->trans,
  1309. "Failed to set affinity mask for IRQ %d\n",
  1310. i);
  1311. }
  1312. }
  1313. static const char *queue_name(struct device *dev,
  1314. struct iwl_trans_pcie *trans_p, int i)
  1315. {
  1316. if (trans_p->shared_vec_mask) {
  1317. int vec = trans_p->shared_vec_mask &
  1318. IWL_SHARED_IRQ_FIRST_RSS ? 1 : 0;
  1319. if (i == 0)
  1320. return DRV_NAME ": shared IRQ";
  1321. return devm_kasprintf(dev, GFP_KERNEL,
  1322. DRV_NAME ": queue %d", i + vec);
  1323. }
  1324. if (i == 0)
  1325. return DRV_NAME ": default queue";
  1326. if (i == trans_p->alloc_vecs - 1)
  1327. return DRV_NAME ": exception";
  1328. return devm_kasprintf(dev, GFP_KERNEL,
  1329. DRV_NAME ": queue %d", i);
  1330. }
  1331. static int iwl_pcie_init_msix_handler(struct pci_dev *pdev,
  1332. struct iwl_trans_pcie *trans_pcie)
  1333. {
  1334. int i;
  1335. for (i = 0; i < trans_pcie->alloc_vecs; i++) {
  1336. int ret;
  1337. struct msix_entry *msix_entry;
  1338. const char *qname = queue_name(&pdev->dev, trans_pcie, i);
  1339. if (!qname)
  1340. return -ENOMEM;
  1341. msix_entry = &trans_pcie->msix_entries[i];
  1342. ret = devm_request_threaded_irq(&pdev->dev,
  1343. msix_entry->vector,
  1344. iwl_pcie_msix_isr,
  1345. (i == trans_pcie->def_irq) ?
  1346. iwl_pcie_irq_msix_handler :
  1347. iwl_pcie_irq_rx_msix_handler,
  1348. IRQF_SHARED,
  1349. qname,
  1350. msix_entry);
  1351. if (ret) {
  1352. IWL_ERR(trans_pcie->trans,
  1353. "Error allocating IRQ %d\n", i);
  1354. return ret;
  1355. }
  1356. }
  1357. iwl_pcie_irq_set_affinity(trans_pcie->trans);
  1358. return 0;
  1359. }
  1360. static int _iwl_trans_pcie_start_hw(struct iwl_trans *trans, bool low_power)
  1361. {
  1362. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1363. int err;
  1364. lockdep_assert_held(&trans_pcie->mutex);
  1365. err = iwl_pcie_prepare_card_hw(trans);
  1366. if (err) {
  1367. IWL_ERR(trans, "Error while preparing HW: %d\n", err);
  1368. return err;
  1369. }
  1370. /* Reset the entire device */
  1371. iwl_write32(trans, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
  1372. usleep_range(1000, 2000);
  1373. iwl_pcie_apm_init(trans);
  1374. iwl_pcie_init_msix(trans_pcie);
  1375. /* From now on, the op_mode will be kept updated about RF kill state */
  1376. iwl_enable_rfkill_int(trans);
  1377. /* Set is_down to false here so that...*/
  1378. trans_pcie->is_down = false;
  1379. /* ...rfkill can call stop_device and set it false if needed */
  1380. iwl_trans_check_hw_rf_kill(trans);
  1381. /* Make sure we sync here, because we'll need full access later */
  1382. if (low_power)
  1383. pm_runtime_resume(trans->dev);
  1384. return 0;
  1385. }
  1386. static int iwl_trans_pcie_start_hw(struct iwl_trans *trans, bool low_power)
  1387. {
  1388. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1389. int ret;
  1390. mutex_lock(&trans_pcie->mutex);
  1391. ret = _iwl_trans_pcie_start_hw(trans, low_power);
  1392. mutex_unlock(&trans_pcie->mutex);
  1393. return ret;
  1394. }
  1395. static void iwl_trans_pcie_op_mode_leave(struct iwl_trans *trans)
  1396. {
  1397. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1398. mutex_lock(&trans_pcie->mutex);
  1399. /* disable interrupts - don't enable HW RF kill interrupt */
  1400. iwl_disable_interrupts(trans);
  1401. iwl_pcie_apm_stop(trans, true);
  1402. iwl_disable_interrupts(trans);
  1403. iwl_pcie_disable_ict(trans);
  1404. mutex_unlock(&trans_pcie->mutex);
  1405. iwl_pcie_synchronize_irqs(trans);
  1406. }
  1407. static void iwl_trans_pcie_write8(struct iwl_trans *trans, u32 ofs, u8 val)
  1408. {
  1409. writeb(val, IWL_TRANS_GET_PCIE_TRANS(trans)->hw_base + ofs);
  1410. }
  1411. static void iwl_trans_pcie_write32(struct iwl_trans *trans, u32 ofs, u32 val)
  1412. {
  1413. writel(val, IWL_TRANS_GET_PCIE_TRANS(trans)->hw_base + ofs);
  1414. }
  1415. static u32 iwl_trans_pcie_read32(struct iwl_trans *trans, u32 ofs)
  1416. {
  1417. return readl(IWL_TRANS_GET_PCIE_TRANS(trans)->hw_base + ofs);
  1418. }
  1419. static u32 iwl_trans_pcie_read_prph(struct iwl_trans *trans, u32 reg)
  1420. {
  1421. iwl_trans_pcie_write32(trans, HBUS_TARG_PRPH_RADDR,
  1422. ((reg & 0x000FFFFF) | (3 << 24)));
  1423. return iwl_trans_pcie_read32(trans, HBUS_TARG_PRPH_RDAT);
  1424. }
  1425. static void iwl_trans_pcie_write_prph(struct iwl_trans *trans, u32 addr,
  1426. u32 val)
  1427. {
  1428. iwl_trans_pcie_write32(trans, HBUS_TARG_PRPH_WADDR,
  1429. ((addr & 0x000FFFFF) | (3 << 24)));
  1430. iwl_trans_pcie_write32(trans, HBUS_TARG_PRPH_WDAT, val);
  1431. }
  1432. static void iwl_trans_pcie_configure(struct iwl_trans *trans,
  1433. const struct iwl_trans_config *trans_cfg)
  1434. {
  1435. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1436. trans_pcie->cmd_queue = trans_cfg->cmd_queue;
  1437. trans_pcie->cmd_fifo = trans_cfg->cmd_fifo;
  1438. trans_pcie->cmd_q_wdg_timeout = trans_cfg->cmd_q_wdg_timeout;
  1439. if (WARN_ON(trans_cfg->n_no_reclaim_cmds > MAX_NO_RECLAIM_CMDS))
  1440. trans_pcie->n_no_reclaim_cmds = 0;
  1441. else
  1442. trans_pcie->n_no_reclaim_cmds = trans_cfg->n_no_reclaim_cmds;
  1443. if (trans_pcie->n_no_reclaim_cmds)
  1444. memcpy(trans_pcie->no_reclaim_cmds, trans_cfg->no_reclaim_cmds,
  1445. trans_pcie->n_no_reclaim_cmds * sizeof(u8));
  1446. trans_pcie->rx_buf_size = trans_cfg->rx_buf_size;
  1447. trans_pcie->rx_page_order =
  1448. iwl_trans_get_rb_size_order(trans_pcie->rx_buf_size);
  1449. trans_pcie->bc_table_dword = trans_cfg->bc_table_dword;
  1450. trans_pcie->scd_set_active = trans_cfg->scd_set_active;
  1451. trans_pcie->sw_csum_tx = trans_cfg->sw_csum_tx;
  1452. trans_pcie->page_offs = trans_cfg->cb_data_offs;
  1453. trans_pcie->dev_cmd_offs = trans_cfg->cb_data_offs + sizeof(void *);
  1454. trans->command_groups = trans_cfg->command_groups;
  1455. trans->command_groups_size = trans_cfg->command_groups_size;
  1456. /* Initialize NAPI here - it should be before registering to mac80211
  1457. * in the opmode but after the HW struct is allocated.
  1458. * As this function may be called again in some corner cases don't
  1459. * do anything if NAPI was already initialized.
  1460. */
  1461. if (trans_pcie->napi_dev.reg_state != NETREG_DUMMY)
  1462. init_dummy_netdev(&trans_pcie->napi_dev);
  1463. }
  1464. void iwl_trans_pcie_free(struct iwl_trans *trans)
  1465. {
  1466. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1467. int i;
  1468. iwl_pcie_synchronize_irqs(trans);
  1469. if (trans->cfg->gen2)
  1470. iwl_pcie_gen2_tx_free(trans);
  1471. else
  1472. iwl_pcie_tx_free(trans);
  1473. iwl_pcie_rx_free(trans);
  1474. if (trans_pcie->msix_enabled) {
  1475. for (i = 0; i < trans_pcie->alloc_vecs; i++) {
  1476. irq_set_affinity_hint(
  1477. trans_pcie->msix_entries[i].vector,
  1478. NULL);
  1479. }
  1480. trans_pcie->msix_enabled = false;
  1481. } else {
  1482. iwl_pcie_free_ict(trans);
  1483. }
  1484. iwl_pcie_free_fw_monitor(trans);
  1485. for_each_possible_cpu(i) {
  1486. struct iwl_tso_hdr_page *p =
  1487. per_cpu_ptr(trans_pcie->tso_hdr_page, i);
  1488. if (p->page)
  1489. __free_page(p->page);
  1490. }
  1491. free_percpu(trans_pcie->tso_hdr_page);
  1492. mutex_destroy(&trans_pcie->mutex);
  1493. iwl_trans_free(trans);
  1494. }
  1495. static void iwl_trans_pcie_set_pmi(struct iwl_trans *trans, bool state)
  1496. {
  1497. if (state)
  1498. set_bit(STATUS_TPOWER_PMI, &trans->status);
  1499. else
  1500. clear_bit(STATUS_TPOWER_PMI, &trans->status);
  1501. }
  1502. static bool iwl_trans_pcie_grab_nic_access(struct iwl_trans *trans,
  1503. unsigned long *flags)
  1504. {
  1505. int ret;
  1506. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1507. spin_lock_irqsave(&trans_pcie->reg_lock, *flags);
  1508. if (trans_pcie->cmd_hold_nic_awake)
  1509. goto out;
  1510. /* this bit wakes up the NIC */
  1511. __iwl_trans_pcie_set_bit(trans, CSR_GP_CNTRL,
  1512. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  1513. if (trans->cfg->device_family == IWL_DEVICE_FAMILY_8000)
  1514. udelay(2);
  1515. /*
  1516. * These bits say the device is running, and should keep running for
  1517. * at least a short while (at least as long as MAC_ACCESS_REQ stays 1),
  1518. * but they do not indicate that embedded SRAM is restored yet;
  1519. * 3945 and 4965 have volatile SRAM, and must save/restore contents
  1520. * to/from host DRAM when sleeping/waking for power-saving.
  1521. * Each direction takes approximately 1/4 millisecond; with this
  1522. * overhead, it's a good idea to grab and hold MAC_ACCESS_REQUEST if a
  1523. * series of register accesses are expected (e.g. reading Event Log),
  1524. * to keep device from sleeping.
  1525. *
  1526. * CSR_UCODE_DRV_GP1 register bit MAC_SLEEP == 0 indicates that
  1527. * SRAM is okay/restored. We don't check that here because this call
  1528. * is just for hardware register access; but GP1 MAC_SLEEP check is a
  1529. * good idea before accessing 3945/4965 SRAM (e.g. reading Event Log).
  1530. *
  1531. * 5000 series and later (including 1000 series) have non-volatile SRAM,
  1532. * and do not save/restore SRAM when power cycling.
  1533. */
  1534. ret = iwl_poll_bit(trans, CSR_GP_CNTRL,
  1535. CSR_GP_CNTRL_REG_VAL_MAC_ACCESS_EN,
  1536. (CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY |
  1537. CSR_GP_CNTRL_REG_FLAG_GOING_TO_SLEEP), 15000);
  1538. if (unlikely(ret < 0)) {
  1539. iwl_write32(trans, CSR_RESET, CSR_RESET_REG_FLAG_FORCE_NMI);
  1540. WARN_ONCE(1,
  1541. "Timeout waiting for hardware access (CSR_GP_CNTRL 0x%08x)\n",
  1542. iwl_read32(trans, CSR_GP_CNTRL));
  1543. spin_unlock_irqrestore(&trans_pcie->reg_lock, *flags);
  1544. return false;
  1545. }
  1546. out:
  1547. /*
  1548. * Fool sparse by faking we release the lock - sparse will
  1549. * track nic_access anyway.
  1550. */
  1551. __release(&trans_pcie->reg_lock);
  1552. return true;
  1553. }
  1554. static void iwl_trans_pcie_release_nic_access(struct iwl_trans *trans,
  1555. unsigned long *flags)
  1556. {
  1557. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1558. lockdep_assert_held(&trans_pcie->reg_lock);
  1559. /*
  1560. * Fool sparse by faking we acquiring the lock - sparse will
  1561. * track nic_access anyway.
  1562. */
  1563. __acquire(&trans_pcie->reg_lock);
  1564. if (trans_pcie->cmd_hold_nic_awake)
  1565. goto out;
  1566. __iwl_trans_pcie_clear_bit(trans, CSR_GP_CNTRL,
  1567. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  1568. /*
  1569. * Above we read the CSR_GP_CNTRL register, which will flush
  1570. * any previous writes, but we need the write that clears the
  1571. * MAC_ACCESS_REQ bit to be performed before any other writes
  1572. * scheduled on different CPUs (after we drop reg_lock).
  1573. */
  1574. mmiowb();
  1575. out:
  1576. spin_unlock_irqrestore(&trans_pcie->reg_lock, *flags);
  1577. }
  1578. static int iwl_trans_pcie_read_mem(struct iwl_trans *trans, u32 addr,
  1579. void *buf, int dwords)
  1580. {
  1581. unsigned long flags;
  1582. int offs, ret = 0;
  1583. u32 *vals = buf;
  1584. if (iwl_trans_grab_nic_access(trans, &flags)) {
  1585. iwl_write32(trans, HBUS_TARG_MEM_RADDR, addr);
  1586. for (offs = 0; offs < dwords; offs++)
  1587. vals[offs] = iwl_read32(trans, HBUS_TARG_MEM_RDAT);
  1588. iwl_trans_release_nic_access(trans, &flags);
  1589. } else {
  1590. ret = -EBUSY;
  1591. }
  1592. return ret;
  1593. }
  1594. static int iwl_trans_pcie_write_mem(struct iwl_trans *trans, u32 addr,
  1595. const void *buf, int dwords)
  1596. {
  1597. unsigned long flags;
  1598. int offs, ret = 0;
  1599. const u32 *vals = buf;
  1600. if (iwl_trans_grab_nic_access(trans, &flags)) {
  1601. iwl_write32(trans, HBUS_TARG_MEM_WADDR, addr);
  1602. for (offs = 0; offs < dwords; offs++)
  1603. iwl_write32(trans, HBUS_TARG_MEM_WDAT,
  1604. vals ? vals[offs] : 0);
  1605. iwl_trans_release_nic_access(trans, &flags);
  1606. } else {
  1607. ret = -EBUSY;
  1608. }
  1609. return ret;
  1610. }
  1611. static void iwl_trans_pcie_freeze_txq_timer(struct iwl_trans *trans,
  1612. unsigned long txqs,
  1613. bool freeze)
  1614. {
  1615. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1616. int queue;
  1617. for_each_set_bit(queue, &txqs, BITS_PER_LONG) {
  1618. struct iwl_txq *txq = trans_pcie->txq[queue];
  1619. unsigned long now;
  1620. spin_lock_bh(&txq->lock);
  1621. now = jiffies;
  1622. if (txq->frozen == freeze)
  1623. goto next_queue;
  1624. IWL_DEBUG_TX_QUEUES(trans, "%s TXQ %d\n",
  1625. freeze ? "Freezing" : "Waking", queue);
  1626. txq->frozen = freeze;
  1627. if (txq->read_ptr == txq->write_ptr)
  1628. goto next_queue;
  1629. if (freeze) {
  1630. if (unlikely(time_after(now,
  1631. txq->stuck_timer.expires))) {
  1632. /*
  1633. * The timer should have fired, maybe it is
  1634. * spinning right now on the lock.
  1635. */
  1636. goto next_queue;
  1637. }
  1638. /* remember how long until the timer fires */
  1639. txq->frozen_expiry_remainder =
  1640. txq->stuck_timer.expires - now;
  1641. del_timer(&txq->stuck_timer);
  1642. goto next_queue;
  1643. }
  1644. /*
  1645. * Wake a non-empty queue -> arm timer with the
  1646. * remainder before it froze
  1647. */
  1648. mod_timer(&txq->stuck_timer,
  1649. now + txq->frozen_expiry_remainder);
  1650. next_queue:
  1651. spin_unlock_bh(&txq->lock);
  1652. }
  1653. }
  1654. static void iwl_trans_pcie_block_txq_ptrs(struct iwl_trans *trans, bool block)
  1655. {
  1656. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1657. int i;
  1658. for (i = 0; i < trans->cfg->base_params->num_of_queues; i++) {
  1659. struct iwl_txq *txq = trans_pcie->txq[i];
  1660. if (i == trans_pcie->cmd_queue)
  1661. continue;
  1662. spin_lock_bh(&txq->lock);
  1663. if (!block && !(WARN_ON_ONCE(!txq->block))) {
  1664. txq->block--;
  1665. if (!txq->block) {
  1666. iwl_write32(trans, HBUS_TARG_WRPTR,
  1667. txq->write_ptr | (i << 8));
  1668. }
  1669. } else if (block) {
  1670. txq->block++;
  1671. }
  1672. spin_unlock_bh(&txq->lock);
  1673. }
  1674. }
  1675. #define IWL_FLUSH_WAIT_MS 2000
  1676. void iwl_trans_pcie_log_scd_error(struct iwl_trans *trans, struct iwl_txq *txq)
  1677. {
  1678. u32 txq_id = txq->id;
  1679. u32 status;
  1680. bool active;
  1681. u8 fifo;
  1682. if (trans->cfg->use_tfh) {
  1683. IWL_ERR(trans, "Queue %d is stuck %d %d\n", txq_id,
  1684. txq->read_ptr, txq->write_ptr);
  1685. /* TODO: access new SCD registers and dump them */
  1686. return;
  1687. }
  1688. status = iwl_read_prph(trans, SCD_QUEUE_STATUS_BITS(txq_id));
  1689. fifo = (status >> SCD_QUEUE_STTS_REG_POS_TXF) & 0x7;
  1690. active = !!(status & BIT(SCD_QUEUE_STTS_REG_POS_ACTIVE));
  1691. IWL_ERR(trans,
  1692. "Queue %d is %sactive on fifo %d and stuck for %u ms. SW [%d, %d] HW [%d, %d] FH TRB=0x0%x\n",
  1693. txq_id, active ? "" : "in", fifo,
  1694. jiffies_to_msecs(txq->wd_timeout),
  1695. txq->read_ptr, txq->write_ptr,
  1696. iwl_read_prph(trans, SCD_QUEUE_RDPTR(txq_id)) &
  1697. (TFD_QUEUE_SIZE_MAX - 1),
  1698. iwl_read_prph(trans, SCD_QUEUE_WRPTR(txq_id)) &
  1699. (TFD_QUEUE_SIZE_MAX - 1),
  1700. iwl_read_direct32(trans, FH_TX_TRB_REG(fifo)));
  1701. }
  1702. static int iwl_trans_pcie_wait_txq_empty(struct iwl_trans *trans, u32 txq_bm)
  1703. {
  1704. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1705. struct iwl_txq *txq;
  1706. int cnt;
  1707. unsigned long now = jiffies;
  1708. int ret = 0;
  1709. /* waiting for all the tx frames complete might take a while */
  1710. for (cnt = 0; cnt < trans->cfg->base_params->num_of_queues; cnt++) {
  1711. u8 wr_ptr;
  1712. if (cnt == trans_pcie->cmd_queue)
  1713. continue;
  1714. if (!test_bit(cnt, trans_pcie->queue_used))
  1715. continue;
  1716. if (!(BIT(cnt) & txq_bm))
  1717. continue;
  1718. IWL_DEBUG_TX_QUEUES(trans, "Emptying queue %d...\n", cnt);
  1719. txq = trans_pcie->txq[cnt];
  1720. wr_ptr = ACCESS_ONCE(txq->write_ptr);
  1721. while (txq->read_ptr != ACCESS_ONCE(txq->write_ptr) &&
  1722. !time_after(jiffies,
  1723. now + msecs_to_jiffies(IWL_FLUSH_WAIT_MS))) {
  1724. u8 write_ptr = ACCESS_ONCE(txq->write_ptr);
  1725. if (WARN_ONCE(wr_ptr != write_ptr,
  1726. "WR pointer moved while flushing %d -> %d\n",
  1727. wr_ptr, write_ptr))
  1728. return -ETIMEDOUT;
  1729. usleep_range(1000, 2000);
  1730. }
  1731. if (txq->read_ptr != txq->write_ptr) {
  1732. IWL_ERR(trans,
  1733. "fail to flush all tx fifo queues Q %d\n", cnt);
  1734. ret = -ETIMEDOUT;
  1735. break;
  1736. }
  1737. IWL_DEBUG_TX_QUEUES(trans, "Queue %d is now empty.\n", cnt);
  1738. }
  1739. if (ret)
  1740. iwl_trans_pcie_log_scd_error(trans, txq);
  1741. return ret;
  1742. }
  1743. static void iwl_trans_pcie_set_bits_mask(struct iwl_trans *trans, u32 reg,
  1744. u32 mask, u32 value)
  1745. {
  1746. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1747. unsigned long flags;
  1748. spin_lock_irqsave(&trans_pcie->reg_lock, flags);
  1749. __iwl_trans_pcie_set_bits_mask(trans, reg, mask, value);
  1750. spin_unlock_irqrestore(&trans_pcie->reg_lock, flags);
  1751. }
  1752. static void iwl_trans_pcie_ref(struct iwl_trans *trans)
  1753. {
  1754. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1755. if (iwlwifi_mod_params.d0i3_disable)
  1756. return;
  1757. pm_runtime_get(&trans_pcie->pci_dev->dev);
  1758. #ifdef CONFIG_PM
  1759. IWL_DEBUG_RPM(trans, "runtime usage count: %d\n",
  1760. atomic_read(&trans_pcie->pci_dev->dev.power.usage_count));
  1761. #endif /* CONFIG_PM */
  1762. }
  1763. static void iwl_trans_pcie_unref(struct iwl_trans *trans)
  1764. {
  1765. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1766. if (iwlwifi_mod_params.d0i3_disable)
  1767. return;
  1768. pm_runtime_mark_last_busy(&trans_pcie->pci_dev->dev);
  1769. pm_runtime_put_autosuspend(&trans_pcie->pci_dev->dev);
  1770. #ifdef CONFIG_PM
  1771. IWL_DEBUG_RPM(trans, "runtime usage count: %d\n",
  1772. atomic_read(&trans_pcie->pci_dev->dev.power.usage_count));
  1773. #endif /* CONFIG_PM */
  1774. }
  1775. static const char *get_csr_string(int cmd)
  1776. {
  1777. #define IWL_CMD(x) case x: return #x
  1778. switch (cmd) {
  1779. IWL_CMD(CSR_HW_IF_CONFIG_REG);
  1780. IWL_CMD(CSR_INT_COALESCING);
  1781. IWL_CMD(CSR_INT);
  1782. IWL_CMD(CSR_INT_MASK);
  1783. IWL_CMD(CSR_FH_INT_STATUS);
  1784. IWL_CMD(CSR_GPIO_IN);
  1785. IWL_CMD(CSR_RESET);
  1786. IWL_CMD(CSR_GP_CNTRL);
  1787. IWL_CMD(CSR_HW_REV);
  1788. IWL_CMD(CSR_EEPROM_REG);
  1789. IWL_CMD(CSR_EEPROM_GP);
  1790. IWL_CMD(CSR_OTP_GP_REG);
  1791. IWL_CMD(CSR_GIO_REG);
  1792. IWL_CMD(CSR_GP_UCODE_REG);
  1793. IWL_CMD(CSR_GP_DRIVER_REG);
  1794. IWL_CMD(CSR_UCODE_DRV_GP1);
  1795. IWL_CMD(CSR_UCODE_DRV_GP2);
  1796. IWL_CMD(CSR_LED_REG);
  1797. IWL_CMD(CSR_DRAM_INT_TBL_REG);
  1798. IWL_CMD(CSR_GIO_CHICKEN_BITS);
  1799. IWL_CMD(CSR_ANA_PLL_CFG);
  1800. IWL_CMD(CSR_HW_REV_WA_REG);
  1801. IWL_CMD(CSR_MONITOR_STATUS_REG);
  1802. IWL_CMD(CSR_DBG_HPET_MEM_REG);
  1803. default:
  1804. return "UNKNOWN";
  1805. }
  1806. #undef IWL_CMD
  1807. }
  1808. void iwl_pcie_dump_csr(struct iwl_trans *trans)
  1809. {
  1810. int i;
  1811. static const u32 csr_tbl[] = {
  1812. CSR_HW_IF_CONFIG_REG,
  1813. CSR_INT_COALESCING,
  1814. CSR_INT,
  1815. CSR_INT_MASK,
  1816. CSR_FH_INT_STATUS,
  1817. CSR_GPIO_IN,
  1818. CSR_RESET,
  1819. CSR_GP_CNTRL,
  1820. CSR_HW_REV,
  1821. CSR_EEPROM_REG,
  1822. CSR_EEPROM_GP,
  1823. CSR_OTP_GP_REG,
  1824. CSR_GIO_REG,
  1825. CSR_GP_UCODE_REG,
  1826. CSR_GP_DRIVER_REG,
  1827. CSR_UCODE_DRV_GP1,
  1828. CSR_UCODE_DRV_GP2,
  1829. CSR_LED_REG,
  1830. CSR_DRAM_INT_TBL_REG,
  1831. CSR_GIO_CHICKEN_BITS,
  1832. CSR_ANA_PLL_CFG,
  1833. CSR_MONITOR_STATUS_REG,
  1834. CSR_HW_REV_WA_REG,
  1835. CSR_DBG_HPET_MEM_REG
  1836. };
  1837. IWL_ERR(trans, "CSR values:\n");
  1838. IWL_ERR(trans, "(2nd byte of CSR_INT_COALESCING is "
  1839. "CSR_INT_PERIODIC_REG)\n");
  1840. for (i = 0; i < ARRAY_SIZE(csr_tbl); i++) {
  1841. IWL_ERR(trans, " %25s: 0X%08x\n",
  1842. get_csr_string(csr_tbl[i]),
  1843. iwl_read32(trans, csr_tbl[i]));
  1844. }
  1845. }
  1846. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1847. /* create and remove of files */
  1848. #define DEBUGFS_ADD_FILE(name, parent, mode) do { \
  1849. if (!debugfs_create_file(#name, mode, parent, trans, \
  1850. &iwl_dbgfs_##name##_ops)) \
  1851. goto err; \
  1852. } while (0)
  1853. /* file operation */
  1854. #define DEBUGFS_READ_FILE_OPS(name) \
  1855. static const struct file_operations iwl_dbgfs_##name##_ops = { \
  1856. .read = iwl_dbgfs_##name##_read, \
  1857. .open = simple_open, \
  1858. .llseek = generic_file_llseek, \
  1859. };
  1860. #define DEBUGFS_WRITE_FILE_OPS(name) \
  1861. static const struct file_operations iwl_dbgfs_##name##_ops = { \
  1862. .write = iwl_dbgfs_##name##_write, \
  1863. .open = simple_open, \
  1864. .llseek = generic_file_llseek, \
  1865. };
  1866. #define DEBUGFS_READ_WRITE_FILE_OPS(name) \
  1867. static const struct file_operations iwl_dbgfs_##name##_ops = { \
  1868. .write = iwl_dbgfs_##name##_write, \
  1869. .read = iwl_dbgfs_##name##_read, \
  1870. .open = simple_open, \
  1871. .llseek = generic_file_llseek, \
  1872. };
  1873. static ssize_t iwl_dbgfs_tx_queue_read(struct file *file,
  1874. char __user *user_buf,
  1875. size_t count, loff_t *ppos)
  1876. {
  1877. struct iwl_trans *trans = file->private_data;
  1878. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1879. struct iwl_txq *txq;
  1880. char *buf;
  1881. int pos = 0;
  1882. int cnt;
  1883. int ret;
  1884. size_t bufsz;
  1885. bufsz = sizeof(char) * 75 * trans->cfg->base_params->num_of_queues;
  1886. if (!trans_pcie->txq_memory)
  1887. return -EAGAIN;
  1888. buf = kzalloc(bufsz, GFP_KERNEL);
  1889. if (!buf)
  1890. return -ENOMEM;
  1891. for (cnt = 0; cnt < trans->cfg->base_params->num_of_queues; cnt++) {
  1892. txq = trans_pcie->txq[cnt];
  1893. pos += scnprintf(buf + pos, bufsz - pos,
  1894. "hwq %.2d: read=%u write=%u use=%d stop=%d need_update=%d frozen=%d%s\n",
  1895. cnt, txq->read_ptr, txq->write_ptr,
  1896. !!test_bit(cnt, trans_pcie->queue_used),
  1897. !!test_bit(cnt, trans_pcie->queue_stopped),
  1898. txq->need_update, txq->frozen,
  1899. (cnt == trans_pcie->cmd_queue ? " HCMD" : ""));
  1900. }
  1901. ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
  1902. kfree(buf);
  1903. return ret;
  1904. }
  1905. static ssize_t iwl_dbgfs_rx_queue_read(struct file *file,
  1906. char __user *user_buf,
  1907. size_t count, loff_t *ppos)
  1908. {
  1909. struct iwl_trans *trans = file->private_data;
  1910. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1911. char *buf;
  1912. int pos = 0, i, ret;
  1913. size_t bufsz = sizeof(buf);
  1914. bufsz = sizeof(char) * 121 * trans->num_rx_queues;
  1915. if (!trans_pcie->rxq)
  1916. return -EAGAIN;
  1917. buf = kzalloc(bufsz, GFP_KERNEL);
  1918. if (!buf)
  1919. return -ENOMEM;
  1920. for (i = 0; i < trans->num_rx_queues && pos < bufsz; i++) {
  1921. struct iwl_rxq *rxq = &trans_pcie->rxq[i];
  1922. pos += scnprintf(buf + pos, bufsz - pos, "queue#: %2d\n",
  1923. i);
  1924. pos += scnprintf(buf + pos, bufsz - pos, "\tread: %u\n",
  1925. rxq->read);
  1926. pos += scnprintf(buf + pos, bufsz - pos, "\twrite: %u\n",
  1927. rxq->write);
  1928. pos += scnprintf(buf + pos, bufsz - pos, "\twrite_actual: %u\n",
  1929. rxq->write_actual);
  1930. pos += scnprintf(buf + pos, bufsz - pos, "\tneed_update: %2d\n",
  1931. rxq->need_update);
  1932. pos += scnprintf(buf + pos, bufsz - pos, "\tfree_count: %u\n",
  1933. rxq->free_count);
  1934. if (rxq->rb_stts) {
  1935. pos += scnprintf(buf + pos, bufsz - pos,
  1936. "\tclosed_rb_num: %u\n",
  1937. le16_to_cpu(rxq->rb_stts->closed_rb_num) &
  1938. 0x0FFF);
  1939. } else {
  1940. pos += scnprintf(buf + pos, bufsz - pos,
  1941. "\tclosed_rb_num: Not Allocated\n");
  1942. }
  1943. }
  1944. ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
  1945. kfree(buf);
  1946. return ret;
  1947. }
  1948. static ssize_t iwl_dbgfs_interrupt_read(struct file *file,
  1949. char __user *user_buf,
  1950. size_t count, loff_t *ppos)
  1951. {
  1952. struct iwl_trans *trans = file->private_data;
  1953. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1954. struct isr_statistics *isr_stats = &trans_pcie->isr_stats;
  1955. int pos = 0;
  1956. char *buf;
  1957. int bufsz = 24 * 64; /* 24 items * 64 char per item */
  1958. ssize_t ret;
  1959. buf = kzalloc(bufsz, GFP_KERNEL);
  1960. if (!buf)
  1961. return -ENOMEM;
  1962. pos += scnprintf(buf + pos, bufsz - pos,
  1963. "Interrupt Statistics Report:\n");
  1964. pos += scnprintf(buf + pos, bufsz - pos, "HW Error:\t\t\t %u\n",
  1965. isr_stats->hw);
  1966. pos += scnprintf(buf + pos, bufsz - pos, "SW Error:\t\t\t %u\n",
  1967. isr_stats->sw);
  1968. if (isr_stats->sw || isr_stats->hw) {
  1969. pos += scnprintf(buf + pos, bufsz - pos,
  1970. "\tLast Restarting Code: 0x%X\n",
  1971. isr_stats->err_code);
  1972. }
  1973. #ifdef CONFIG_IWLWIFI_DEBUG
  1974. pos += scnprintf(buf + pos, bufsz - pos, "Frame transmitted:\t\t %u\n",
  1975. isr_stats->sch);
  1976. pos += scnprintf(buf + pos, bufsz - pos, "Alive interrupt:\t\t %u\n",
  1977. isr_stats->alive);
  1978. #endif
  1979. pos += scnprintf(buf + pos, bufsz - pos,
  1980. "HW RF KILL switch toggled:\t %u\n", isr_stats->rfkill);
  1981. pos += scnprintf(buf + pos, bufsz - pos, "CT KILL:\t\t\t %u\n",
  1982. isr_stats->ctkill);
  1983. pos += scnprintf(buf + pos, bufsz - pos, "Wakeup Interrupt:\t\t %u\n",
  1984. isr_stats->wakeup);
  1985. pos += scnprintf(buf + pos, bufsz - pos,
  1986. "Rx command responses:\t\t %u\n", isr_stats->rx);
  1987. pos += scnprintf(buf + pos, bufsz - pos, "Tx/FH interrupt:\t\t %u\n",
  1988. isr_stats->tx);
  1989. pos += scnprintf(buf + pos, bufsz - pos, "Unexpected INTA:\t\t %u\n",
  1990. isr_stats->unhandled);
  1991. ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
  1992. kfree(buf);
  1993. return ret;
  1994. }
  1995. static ssize_t iwl_dbgfs_interrupt_write(struct file *file,
  1996. const char __user *user_buf,
  1997. size_t count, loff_t *ppos)
  1998. {
  1999. struct iwl_trans *trans = file->private_data;
  2000. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  2001. struct isr_statistics *isr_stats = &trans_pcie->isr_stats;
  2002. char buf[8];
  2003. int buf_size;
  2004. u32 reset_flag;
  2005. memset(buf, 0, sizeof(buf));
  2006. buf_size = min(count, sizeof(buf) - 1);
  2007. if (copy_from_user(buf, user_buf, buf_size))
  2008. return -EFAULT;
  2009. if (sscanf(buf, "%x", &reset_flag) != 1)
  2010. return -EFAULT;
  2011. if (reset_flag == 0)
  2012. memset(isr_stats, 0, sizeof(*isr_stats));
  2013. return count;
  2014. }
  2015. static ssize_t iwl_dbgfs_csr_write(struct file *file,
  2016. const char __user *user_buf,
  2017. size_t count, loff_t *ppos)
  2018. {
  2019. struct iwl_trans *trans = file->private_data;
  2020. char buf[8];
  2021. int buf_size;
  2022. int csr;
  2023. memset(buf, 0, sizeof(buf));
  2024. buf_size = min(count, sizeof(buf) - 1);
  2025. if (copy_from_user(buf, user_buf, buf_size))
  2026. return -EFAULT;
  2027. if (sscanf(buf, "%d", &csr) != 1)
  2028. return -EFAULT;
  2029. iwl_pcie_dump_csr(trans);
  2030. return count;
  2031. }
  2032. static ssize_t iwl_dbgfs_fh_reg_read(struct file *file,
  2033. char __user *user_buf,
  2034. size_t count, loff_t *ppos)
  2035. {
  2036. struct iwl_trans *trans = file->private_data;
  2037. char *buf = NULL;
  2038. ssize_t ret;
  2039. ret = iwl_dump_fh(trans, &buf);
  2040. if (ret < 0)
  2041. return ret;
  2042. if (!buf)
  2043. return -EINVAL;
  2044. ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
  2045. kfree(buf);
  2046. return ret;
  2047. }
  2048. DEBUGFS_READ_WRITE_FILE_OPS(interrupt);
  2049. DEBUGFS_READ_FILE_OPS(fh_reg);
  2050. DEBUGFS_READ_FILE_OPS(rx_queue);
  2051. DEBUGFS_READ_FILE_OPS(tx_queue);
  2052. DEBUGFS_WRITE_FILE_OPS(csr);
  2053. /* Create the debugfs files and directories */
  2054. int iwl_trans_pcie_dbgfs_register(struct iwl_trans *trans)
  2055. {
  2056. struct dentry *dir = trans->dbgfs_dir;
  2057. DEBUGFS_ADD_FILE(rx_queue, dir, S_IRUSR);
  2058. DEBUGFS_ADD_FILE(tx_queue, dir, S_IRUSR);
  2059. DEBUGFS_ADD_FILE(interrupt, dir, S_IWUSR | S_IRUSR);
  2060. DEBUGFS_ADD_FILE(csr, dir, S_IWUSR);
  2061. DEBUGFS_ADD_FILE(fh_reg, dir, S_IRUSR);
  2062. return 0;
  2063. err:
  2064. IWL_ERR(trans, "failed to create the trans debugfs entry\n");
  2065. return -ENOMEM;
  2066. }
  2067. #endif /*CONFIG_IWLWIFI_DEBUGFS */
  2068. static u32 iwl_trans_pcie_get_cmdlen(struct iwl_trans *trans, void *tfd)
  2069. {
  2070. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  2071. u32 cmdlen = 0;
  2072. int i;
  2073. for (i = 0; i < trans_pcie->max_tbs; i++)
  2074. cmdlen += iwl_pcie_tfd_tb_get_len(trans, tfd, i);
  2075. return cmdlen;
  2076. }
  2077. static u32 iwl_trans_pcie_dump_rbs(struct iwl_trans *trans,
  2078. struct iwl_fw_error_dump_data **data,
  2079. int allocated_rb_nums)
  2080. {
  2081. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  2082. int max_len = PAGE_SIZE << trans_pcie->rx_page_order;
  2083. /* Dump RBs is supported only for pre-9000 devices (1 queue) */
  2084. struct iwl_rxq *rxq = &trans_pcie->rxq[0];
  2085. u32 i, r, j, rb_len = 0;
  2086. spin_lock(&rxq->lock);
  2087. r = le16_to_cpu(ACCESS_ONCE(rxq->rb_stts->closed_rb_num)) & 0x0FFF;
  2088. for (i = rxq->read, j = 0;
  2089. i != r && j < allocated_rb_nums;
  2090. i = (i + 1) & RX_QUEUE_MASK, j++) {
  2091. struct iwl_rx_mem_buffer *rxb = rxq->queue[i];
  2092. struct iwl_fw_error_dump_rb *rb;
  2093. dma_unmap_page(trans->dev, rxb->page_dma, max_len,
  2094. DMA_FROM_DEVICE);
  2095. rb_len += sizeof(**data) + sizeof(*rb) + max_len;
  2096. (*data)->type = cpu_to_le32(IWL_FW_ERROR_DUMP_RB);
  2097. (*data)->len = cpu_to_le32(sizeof(*rb) + max_len);
  2098. rb = (void *)(*data)->data;
  2099. rb->index = cpu_to_le32(i);
  2100. memcpy(rb->data, page_address(rxb->page), max_len);
  2101. /* remap the page for the free benefit */
  2102. rxb->page_dma = dma_map_page(trans->dev, rxb->page, 0,
  2103. max_len,
  2104. DMA_FROM_DEVICE);
  2105. *data = iwl_fw_error_next_data(*data);
  2106. }
  2107. spin_unlock(&rxq->lock);
  2108. return rb_len;
  2109. }
  2110. #define IWL_CSR_TO_DUMP (0x250)
  2111. static u32 iwl_trans_pcie_dump_csr(struct iwl_trans *trans,
  2112. struct iwl_fw_error_dump_data **data)
  2113. {
  2114. u32 csr_len = sizeof(**data) + IWL_CSR_TO_DUMP;
  2115. __le32 *val;
  2116. int i;
  2117. (*data)->type = cpu_to_le32(IWL_FW_ERROR_DUMP_CSR);
  2118. (*data)->len = cpu_to_le32(IWL_CSR_TO_DUMP);
  2119. val = (void *)(*data)->data;
  2120. for (i = 0; i < IWL_CSR_TO_DUMP; i += 4)
  2121. *val++ = cpu_to_le32(iwl_trans_pcie_read32(trans, i));
  2122. *data = iwl_fw_error_next_data(*data);
  2123. return csr_len;
  2124. }
  2125. static u32 iwl_trans_pcie_fh_regs_dump(struct iwl_trans *trans,
  2126. struct iwl_fw_error_dump_data **data)
  2127. {
  2128. u32 fh_regs_len = FH_MEM_UPPER_BOUND - FH_MEM_LOWER_BOUND;
  2129. unsigned long flags;
  2130. __le32 *val;
  2131. int i;
  2132. if (!iwl_trans_grab_nic_access(trans, &flags))
  2133. return 0;
  2134. (*data)->type = cpu_to_le32(IWL_FW_ERROR_DUMP_FH_REGS);
  2135. (*data)->len = cpu_to_le32(fh_regs_len);
  2136. val = (void *)(*data)->data;
  2137. for (i = FH_MEM_LOWER_BOUND; i < FH_MEM_UPPER_BOUND; i += sizeof(u32))
  2138. *val++ = cpu_to_le32(iwl_trans_pcie_read32(trans, i));
  2139. iwl_trans_release_nic_access(trans, &flags);
  2140. *data = iwl_fw_error_next_data(*data);
  2141. return sizeof(**data) + fh_regs_len;
  2142. }
  2143. static u32
  2144. iwl_trans_pci_dump_marbh_monitor(struct iwl_trans *trans,
  2145. struct iwl_fw_error_dump_fw_mon *fw_mon_data,
  2146. u32 monitor_len)
  2147. {
  2148. u32 buf_size_in_dwords = (monitor_len >> 2);
  2149. u32 *buffer = (u32 *)fw_mon_data->data;
  2150. unsigned long flags;
  2151. u32 i;
  2152. if (!iwl_trans_grab_nic_access(trans, &flags))
  2153. return 0;
  2154. iwl_write_prph_no_grab(trans, MON_DMARB_RD_CTL_ADDR, 0x1);
  2155. for (i = 0; i < buf_size_in_dwords; i++)
  2156. buffer[i] = iwl_read_prph_no_grab(trans,
  2157. MON_DMARB_RD_DATA_ADDR);
  2158. iwl_write_prph_no_grab(trans, MON_DMARB_RD_CTL_ADDR, 0x0);
  2159. iwl_trans_release_nic_access(trans, &flags);
  2160. return monitor_len;
  2161. }
  2162. static u32
  2163. iwl_trans_pcie_dump_monitor(struct iwl_trans *trans,
  2164. struct iwl_fw_error_dump_data **data,
  2165. u32 monitor_len)
  2166. {
  2167. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  2168. u32 len = 0;
  2169. if ((trans_pcie->fw_mon_page &&
  2170. trans->cfg->device_family == IWL_DEVICE_FAMILY_7000) ||
  2171. trans->dbg_dest_tlv) {
  2172. struct iwl_fw_error_dump_fw_mon *fw_mon_data;
  2173. u32 base, write_ptr, wrap_cnt;
  2174. /* If there was a dest TLV - use the values from there */
  2175. if (trans->dbg_dest_tlv) {
  2176. write_ptr =
  2177. le32_to_cpu(trans->dbg_dest_tlv->write_ptr_reg);
  2178. wrap_cnt = le32_to_cpu(trans->dbg_dest_tlv->wrap_count);
  2179. base = le32_to_cpu(trans->dbg_dest_tlv->base_reg);
  2180. } else {
  2181. base = MON_BUFF_BASE_ADDR;
  2182. write_ptr = MON_BUFF_WRPTR;
  2183. wrap_cnt = MON_BUFF_CYCLE_CNT;
  2184. }
  2185. (*data)->type = cpu_to_le32(IWL_FW_ERROR_DUMP_FW_MONITOR);
  2186. fw_mon_data = (void *)(*data)->data;
  2187. fw_mon_data->fw_mon_wr_ptr =
  2188. cpu_to_le32(iwl_read_prph(trans, write_ptr));
  2189. fw_mon_data->fw_mon_cycle_cnt =
  2190. cpu_to_le32(iwl_read_prph(trans, wrap_cnt));
  2191. fw_mon_data->fw_mon_base_ptr =
  2192. cpu_to_le32(iwl_read_prph(trans, base));
  2193. len += sizeof(**data) + sizeof(*fw_mon_data);
  2194. if (trans_pcie->fw_mon_page) {
  2195. /*
  2196. * The firmware is now asserted, it won't write anything
  2197. * to the buffer. CPU can take ownership to fetch the
  2198. * data. The buffer will be handed back to the device
  2199. * before the firmware will be restarted.
  2200. */
  2201. dma_sync_single_for_cpu(trans->dev,
  2202. trans_pcie->fw_mon_phys,
  2203. trans_pcie->fw_mon_size,
  2204. DMA_FROM_DEVICE);
  2205. memcpy(fw_mon_data->data,
  2206. page_address(trans_pcie->fw_mon_page),
  2207. trans_pcie->fw_mon_size);
  2208. monitor_len = trans_pcie->fw_mon_size;
  2209. } else if (trans->dbg_dest_tlv->monitor_mode == SMEM_MODE) {
  2210. /*
  2211. * Update pointers to reflect actual values after
  2212. * shifting
  2213. */
  2214. base = iwl_read_prph(trans, base) <<
  2215. trans->dbg_dest_tlv->base_shift;
  2216. iwl_trans_read_mem(trans, base, fw_mon_data->data,
  2217. monitor_len / sizeof(u32));
  2218. } else if (trans->dbg_dest_tlv->monitor_mode == MARBH_MODE) {
  2219. monitor_len =
  2220. iwl_trans_pci_dump_marbh_monitor(trans,
  2221. fw_mon_data,
  2222. monitor_len);
  2223. } else {
  2224. /* Didn't match anything - output no monitor data */
  2225. monitor_len = 0;
  2226. }
  2227. len += monitor_len;
  2228. (*data)->len = cpu_to_le32(monitor_len + sizeof(*fw_mon_data));
  2229. }
  2230. return len;
  2231. }
  2232. static struct iwl_trans_dump_data
  2233. *iwl_trans_pcie_dump_data(struct iwl_trans *trans,
  2234. const struct iwl_fw_dbg_trigger_tlv *trigger)
  2235. {
  2236. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  2237. struct iwl_fw_error_dump_data *data;
  2238. struct iwl_txq *cmdq = trans_pcie->txq[trans_pcie->cmd_queue];
  2239. struct iwl_fw_error_dump_txcmd *txcmd;
  2240. struct iwl_trans_dump_data *dump_data;
  2241. u32 len, num_rbs;
  2242. u32 monitor_len;
  2243. int i, ptr;
  2244. bool dump_rbs = test_bit(STATUS_FW_ERROR, &trans->status) &&
  2245. !trans->cfg->mq_rx_supported;
  2246. /* transport dump header */
  2247. len = sizeof(*dump_data);
  2248. /* host commands */
  2249. len += sizeof(*data) +
  2250. cmdq->n_window * (sizeof(*txcmd) + TFD_MAX_PAYLOAD_SIZE);
  2251. /* FW monitor */
  2252. if (trans_pcie->fw_mon_page) {
  2253. len += sizeof(*data) + sizeof(struct iwl_fw_error_dump_fw_mon) +
  2254. trans_pcie->fw_mon_size;
  2255. monitor_len = trans_pcie->fw_mon_size;
  2256. } else if (trans->dbg_dest_tlv) {
  2257. u32 base, end;
  2258. base = le32_to_cpu(trans->dbg_dest_tlv->base_reg);
  2259. end = le32_to_cpu(trans->dbg_dest_tlv->end_reg);
  2260. base = iwl_read_prph(trans, base) <<
  2261. trans->dbg_dest_tlv->base_shift;
  2262. end = iwl_read_prph(trans, end) <<
  2263. trans->dbg_dest_tlv->end_shift;
  2264. /* Make "end" point to the actual end */
  2265. if (trans->cfg->device_family == IWL_DEVICE_FAMILY_8000 ||
  2266. trans->dbg_dest_tlv->monitor_mode == MARBH_MODE)
  2267. end += (1 << trans->dbg_dest_tlv->end_shift);
  2268. monitor_len = end - base;
  2269. len += sizeof(*data) + sizeof(struct iwl_fw_error_dump_fw_mon) +
  2270. monitor_len;
  2271. } else {
  2272. monitor_len = 0;
  2273. }
  2274. if (trigger && (trigger->mode & IWL_FW_DBG_TRIGGER_MONITOR_ONLY)) {
  2275. dump_data = vzalloc(len);
  2276. if (!dump_data)
  2277. return NULL;
  2278. data = (void *)dump_data->data;
  2279. len = iwl_trans_pcie_dump_monitor(trans, &data, monitor_len);
  2280. dump_data->len = len;
  2281. return dump_data;
  2282. }
  2283. /* CSR registers */
  2284. len += sizeof(*data) + IWL_CSR_TO_DUMP;
  2285. /* FH registers */
  2286. len += sizeof(*data) + (FH_MEM_UPPER_BOUND - FH_MEM_LOWER_BOUND);
  2287. if (dump_rbs) {
  2288. /* Dump RBs is supported only for pre-9000 devices (1 queue) */
  2289. struct iwl_rxq *rxq = &trans_pcie->rxq[0];
  2290. /* RBs */
  2291. num_rbs = le16_to_cpu(ACCESS_ONCE(rxq->rb_stts->closed_rb_num))
  2292. & 0x0FFF;
  2293. num_rbs = (num_rbs - rxq->read) & RX_QUEUE_MASK;
  2294. len += num_rbs * (sizeof(*data) +
  2295. sizeof(struct iwl_fw_error_dump_rb) +
  2296. (PAGE_SIZE << trans_pcie->rx_page_order));
  2297. }
  2298. dump_data = vzalloc(len);
  2299. if (!dump_data)
  2300. return NULL;
  2301. len = 0;
  2302. data = (void *)dump_data->data;
  2303. data->type = cpu_to_le32(IWL_FW_ERROR_DUMP_TXCMD);
  2304. txcmd = (void *)data->data;
  2305. spin_lock_bh(&cmdq->lock);
  2306. ptr = cmdq->write_ptr;
  2307. for (i = 0; i < cmdq->n_window; i++) {
  2308. u8 idx = get_cmd_index(cmdq, ptr);
  2309. u32 caplen, cmdlen;
  2310. cmdlen = iwl_trans_pcie_get_cmdlen(trans, cmdq->tfds +
  2311. trans_pcie->tfd_size * ptr);
  2312. caplen = min_t(u32, TFD_MAX_PAYLOAD_SIZE, cmdlen);
  2313. if (cmdlen) {
  2314. len += sizeof(*txcmd) + caplen;
  2315. txcmd->cmdlen = cpu_to_le32(cmdlen);
  2316. txcmd->caplen = cpu_to_le32(caplen);
  2317. memcpy(txcmd->data, cmdq->entries[idx].cmd, caplen);
  2318. txcmd = (void *)((u8 *)txcmd->data + caplen);
  2319. }
  2320. ptr = iwl_queue_dec_wrap(ptr);
  2321. }
  2322. spin_unlock_bh(&cmdq->lock);
  2323. data->len = cpu_to_le32(len);
  2324. len += sizeof(*data);
  2325. data = iwl_fw_error_next_data(data);
  2326. len += iwl_trans_pcie_dump_csr(trans, &data);
  2327. len += iwl_trans_pcie_fh_regs_dump(trans, &data);
  2328. if (dump_rbs)
  2329. len += iwl_trans_pcie_dump_rbs(trans, &data, num_rbs);
  2330. len += iwl_trans_pcie_dump_monitor(trans, &data, monitor_len);
  2331. dump_data->len = len;
  2332. return dump_data;
  2333. }
  2334. #ifdef CONFIG_PM_SLEEP
  2335. static int iwl_trans_pcie_suspend(struct iwl_trans *trans)
  2336. {
  2337. if (trans->runtime_pm_mode == IWL_PLAT_PM_MODE_D0I3)
  2338. return iwl_pci_fw_enter_d0i3(trans);
  2339. return 0;
  2340. }
  2341. static void iwl_trans_pcie_resume(struct iwl_trans *trans)
  2342. {
  2343. if (trans->runtime_pm_mode == IWL_PLAT_PM_MODE_D0I3)
  2344. iwl_pci_fw_exit_d0i3(trans);
  2345. }
  2346. #endif /* CONFIG_PM_SLEEP */
  2347. #define IWL_TRANS_COMMON_OPS \
  2348. .op_mode_leave = iwl_trans_pcie_op_mode_leave, \
  2349. .write8 = iwl_trans_pcie_write8, \
  2350. .write32 = iwl_trans_pcie_write32, \
  2351. .read32 = iwl_trans_pcie_read32, \
  2352. .read_prph = iwl_trans_pcie_read_prph, \
  2353. .write_prph = iwl_trans_pcie_write_prph, \
  2354. .read_mem = iwl_trans_pcie_read_mem, \
  2355. .write_mem = iwl_trans_pcie_write_mem, \
  2356. .configure = iwl_trans_pcie_configure, \
  2357. .set_pmi = iwl_trans_pcie_set_pmi, \
  2358. .grab_nic_access = iwl_trans_pcie_grab_nic_access, \
  2359. .release_nic_access = iwl_trans_pcie_release_nic_access, \
  2360. .set_bits_mask = iwl_trans_pcie_set_bits_mask, \
  2361. .ref = iwl_trans_pcie_ref, \
  2362. .unref = iwl_trans_pcie_unref, \
  2363. .dump_data = iwl_trans_pcie_dump_data, \
  2364. .wait_tx_queues_empty = iwl_trans_pcie_wait_txq_empty, \
  2365. .d3_suspend = iwl_trans_pcie_d3_suspend, \
  2366. .d3_resume = iwl_trans_pcie_d3_resume
  2367. #ifdef CONFIG_PM_SLEEP
  2368. #define IWL_TRANS_PM_OPS \
  2369. .suspend = iwl_trans_pcie_suspend, \
  2370. .resume = iwl_trans_pcie_resume,
  2371. #else
  2372. #define IWL_TRANS_PM_OPS
  2373. #endif /* CONFIG_PM_SLEEP */
  2374. static const struct iwl_trans_ops trans_ops_pcie = {
  2375. IWL_TRANS_COMMON_OPS,
  2376. IWL_TRANS_PM_OPS
  2377. .start_hw = iwl_trans_pcie_start_hw,
  2378. .fw_alive = iwl_trans_pcie_fw_alive,
  2379. .start_fw = iwl_trans_pcie_start_fw,
  2380. .stop_device = iwl_trans_pcie_stop_device,
  2381. .send_cmd = iwl_trans_pcie_send_hcmd,
  2382. .tx = iwl_trans_pcie_tx,
  2383. .reclaim = iwl_trans_pcie_reclaim,
  2384. .txq_disable = iwl_trans_pcie_txq_disable,
  2385. .txq_enable = iwl_trans_pcie_txq_enable,
  2386. .txq_set_shared_mode = iwl_trans_pcie_txq_set_shared_mode,
  2387. .freeze_txq_timer = iwl_trans_pcie_freeze_txq_timer,
  2388. .block_txq_ptrs = iwl_trans_pcie_block_txq_ptrs,
  2389. };
  2390. static const struct iwl_trans_ops trans_ops_pcie_gen2 = {
  2391. IWL_TRANS_COMMON_OPS,
  2392. IWL_TRANS_PM_OPS
  2393. .start_hw = iwl_trans_pcie_start_hw,
  2394. .fw_alive = iwl_trans_pcie_gen2_fw_alive,
  2395. .start_fw = iwl_trans_pcie_gen2_start_fw,
  2396. .stop_device = iwl_trans_pcie_gen2_stop_device,
  2397. .send_cmd = iwl_trans_pcie_gen2_send_hcmd,
  2398. .tx = iwl_trans_pcie_gen2_tx,
  2399. .reclaim = iwl_trans_pcie_reclaim,
  2400. .txq_alloc = iwl_trans_pcie_dyn_txq_alloc,
  2401. .txq_free = iwl_trans_pcie_dyn_txq_free,
  2402. };
  2403. struct iwl_trans *iwl_trans_pcie_alloc(struct pci_dev *pdev,
  2404. const struct pci_device_id *ent,
  2405. const struct iwl_cfg *cfg)
  2406. {
  2407. struct iwl_trans_pcie *trans_pcie;
  2408. struct iwl_trans *trans;
  2409. int ret, addr_size;
  2410. ret = pcim_enable_device(pdev);
  2411. if (ret)
  2412. return ERR_PTR(ret);
  2413. if (cfg->gen2)
  2414. trans = iwl_trans_alloc(sizeof(struct iwl_trans_pcie),
  2415. &pdev->dev, cfg, &trans_ops_pcie_gen2);
  2416. else
  2417. trans = iwl_trans_alloc(sizeof(struct iwl_trans_pcie),
  2418. &pdev->dev, cfg, &trans_ops_pcie);
  2419. if (!trans)
  2420. return ERR_PTR(-ENOMEM);
  2421. trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  2422. trans_pcie->trans = trans;
  2423. spin_lock_init(&trans_pcie->irq_lock);
  2424. spin_lock_init(&trans_pcie->reg_lock);
  2425. mutex_init(&trans_pcie->mutex);
  2426. init_waitqueue_head(&trans_pcie->ucode_write_waitq);
  2427. trans_pcie->tso_hdr_page = alloc_percpu(struct iwl_tso_hdr_page);
  2428. if (!trans_pcie->tso_hdr_page) {
  2429. ret = -ENOMEM;
  2430. goto out_no_pci;
  2431. }
  2432. if (!cfg->base_params->pcie_l1_allowed) {
  2433. /*
  2434. * W/A - seems to solve weird behavior. We need to remove this
  2435. * if we don't want to stay in L1 all the time. This wastes a
  2436. * lot of power.
  2437. */
  2438. pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S |
  2439. PCIE_LINK_STATE_L1 |
  2440. PCIE_LINK_STATE_CLKPM);
  2441. }
  2442. if (cfg->use_tfh) {
  2443. addr_size = 64;
  2444. trans_pcie->max_tbs = IWL_TFH_NUM_TBS;
  2445. trans_pcie->tfd_size = sizeof(struct iwl_tfh_tfd);
  2446. } else {
  2447. addr_size = 36;
  2448. trans_pcie->max_tbs = IWL_NUM_OF_TBS;
  2449. trans_pcie->tfd_size = sizeof(struct iwl_tfd);
  2450. }
  2451. trans->max_skb_frags = IWL_PCIE_MAX_FRAGS(trans_pcie);
  2452. pci_set_master(pdev);
  2453. ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(addr_size));
  2454. if (!ret)
  2455. ret = pci_set_consistent_dma_mask(pdev,
  2456. DMA_BIT_MASK(addr_size));
  2457. if (ret) {
  2458. ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  2459. if (!ret)
  2460. ret = pci_set_consistent_dma_mask(pdev,
  2461. DMA_BIT_MASK(32));
  2462. /* both attempts failed: */
  2463. if (ret) {
  2464. dev_err(&pdev->dev, "No suitable DMA available\n");
  2465. goto out_no_pci;
  2466. }
  2467. }
  2468. ret = pcim_iomap_regions_request_all(pdev, BIT(0), DRV_NAME);
  2469. if (ret) {
  2470. dev_err(&pdev->dev, "pcim_iomap_regions_request_all failed\n");
  2471. goto out_no_pci;
  2472. }
  2473. trans_pcie->hw_base = pcim_iomap_table(pdev)[0];
  2474. if (!trans_pcie->hw_base) {
  2475. dev_err(&pdev->dev, "pcim_iomap_table failed\n");
  2476. ret = -ENODEV;
  2477. goto out_no_pci;
  2478. }
  2479. /* We disable the RETRY_TIMEOUT register (0x41) to keep
  2480. * PCI Tx retries from interfering with C3 CPU state */
  2481. pci_write_config_byte(pdev, PCI_CFG_RETRY_TIMEOUT, 0x00);
  2482. trans_pcie->pci_dev = pdev;
  2483. iwl_disable_interrupts(trans);
  2484. trans->hw_rev = iwl_read32(trans, CSR_HW_REV);
  2485. /*
  2486. * In the 8000 HW family the format of the 4 bytes of CSR_HW_REV have
  2487. * changed, and now the revision step also includes bit 0-1 (no more
  2488. * "dash" value). To keep hw_rev backwards compatible - we'll store it
  2489. * in the old format.
  2490. */
  2491. if (trans->cfg->device_family == IWL_DEVICE_FAMILY_8000) {
  2492. unsigned long flags;
  2493. trans->hw_rev = (trans->hw_rev & 0xfff0) |
  2494. (CSR_HW_REV_STEP(trans->hw_rev << 2) << 2);
  2495. ret = iwl_pcie_prepare_card_hw(trans);
  2496. if (ret) {
  2497. IWL_WARN(trans, "Exit HW not ready\n");
  2498. goto out_no_pci;
  2499. }
  2500. /*
  2501. * in-order to recognize C step driver should read chip version
  2502. * id located at the AUX bus MISC address space.
  2503. */
  2504. iwl_set_bit(trans, CSR_GP_CNTRL,
  2505. CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
  2506. udelay(2);
  2507. ret = iwl_poll_bit(trans, CSR_GP_CNTRL,
  2508. CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
  2509. CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
  2510. 25000);
  2511. if (ret < 0) {
  2512. IWL_DEBUG_INFO(trans, "Failed to wake up the nic\n");
  2513. goto out_no_pci;
  2514. }
  2515. if (iwl_trans_grab_nic_access(trans, &flags)) {
  2516. u32 hw_step;
  2517. hw_step = iwl_read_prph_no_grab(trans, WFPM_CTRL_REG);
  2518. hw_step |= ENABLE_WFPM;
  2519. iwl_write_prph_no_grab(trans, WFPM_CTRL_REG, hw_step);
  2520. hw_step = iwl_read_prph_no_grab(trans, AUX_MISC_REG);
  2521. hw_step = (hw_step >> HW_STEP_LOCATION_BITS) & 0xF;
  2522. if (hw_step == 0x3)
  2523. trans->hw_rev = (trans->hw_rev & 0xFFFFFFF3) |
  2524. (SILICON_C_STEP << 2);
  2525. iwl_trans_release_nic_access(trans, &flags);
  2526. }
  2527. }
  2528. trans->hw_rf_id = iwl_read32(trans, CSR_HW_RF_ID);
  2529. iwl_pcie_set_interrupt_capa(pdev, trans);
  2530. trans->hw_id = (pdev->device << 16) + pdev->subsystem_device;
  2531. snprintf(trans->hw_id_str, sizeof(trans->hw_id_str),
  2532. "PCI ID: 0x%04X:0x%04X", pdev->device, pdev->subsystem_device);
  2533. /* Initialize the wait queue for commands */
  2534. init_waitqueue_head(&trans_pcie->wait_command_queue);
  2535. init_waitqueue_head(&trans_pcie->d0i3_waitq);
  2536. if (trans_pcie->msix_enabled) {
  2537. if (iwl_pcie_init_msix_handler(pdev, trans_pcie))
  2538. goto out_no_pci;
  2539. } else {
  2540. ret = iwl_pcie_alloc_ict(trans);
  2541. if (ret)
  2542. goto out_no_pci;
  2543. ret = devm_request_threaded_irq(&pdev->dev, pdev->irq,
  2544. iwl_pcie_isr,
  2545. iwl_pcie_irq_handler,
  2546. IRQF_SHARED, DRV_NAME, trans);
  2547. if (ret) {
  2548. IWL_ERR(trans, "Error allocating IRQ %d\n", pdev->irq);
  2549. goto out_free_ict;
  2550. }
  2551. trans_pcie->inta_mask = CSR_INI_SET_MASK;
  2552. }
  2553. #ifdef CONFIG_IWLWIFI_PCIE_RTPM
  2554. trans->runtime_pm_mode = IWL_PLAT_PM_MODE_D0I3;
  2555. #else
  2556. trans->runtime_pm_mode = IWL_PLAT_PM_MODE_DISABLED;
  2557. #endif /* CONFIG_IWLWIFI_PCIE_RTPM */
  2558. return trans;
  2559. out_free_ict:
  2560. iwl_pcie_free_ict(trans);
  2561. out_no_pci:
  2562. free_percpu(trans_pcie->tso_hdr_page);
  2563. iwl_trans_free(trans);
  2564. return ERR_PTR(ret);
  2565. }