trans.c 98 KB

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