trans.c 87 KB

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