trans.c 80 KB

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