trans.c 90 KB

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