trans.c 96 KB

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