trans.c 93 KB

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