trans.c 82 KB

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