trans.c 86 KB

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