pci.c 87 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500
  1. /*
  2. * Copyright (c) 2005-2011 Atheros Communications Inc.
  3. * Copyright (c) 2011-2013 Qualcomm Atheros, Inc.
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  15. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #include <linux/pci.h>
  18. #include <linux/module.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/spinlock.h>
  21. #include <linux/bitops.h>
  22. #include "core.h"
  23. #include "debug.h"
  24. #include "targaddrs.h"
  25. #include "bmi.h"
  26. #include "hif.h"
  27. #include "htc.h"
  28. #include "ce.h"
  29. #include "pci.h"
  30. enum ath10k_pci_reset_mode {
  31. ATH10K_PCI_RESET_AUTO = 0,
  32. ATH10K_PCI_RESET_WARM_ONLY = 1,
  33. };
  34. static unsigned int ath10k_pci_irq_mode = ATH10K_PCI_IRQ_AUTO;
  35. static unsigned int ath10k_pci_reset_mode = ATH10K_PCI_RESET_AUTO;
  36. module_param_named(irq_mode, ath10k_pci_irq_mode, uint, 0644);
  37. MODULE_PARM_DESC(irq_mode, "0: auto, 1: legacy, 2: msi (default: 0)");
  38. module_param_named(reset_mode, ath10k_pci_reset_mode, uint, 0644);
  39. MODULE_PARM_DESC(reset_mode, "0: auto, 1: warm only (default: 0)");
  40. /* how long wait to wait for target to initialise, in ms */
  41. #define ATH10K_PCI_TARGET_WAIT 3000
  42. #define ATH10K_PCI_NUM_WARM_RESET_ATTEMPTS 3
  43. static const struct pci_device_id ath10k_pci_id_table[] = {
  44. { PCI_VDEVICE(ATHEROS, QCA988X_2_0_DEVICE_ID) }, /* PCI-E QCA988X V2 */
  45. { PCI_VDEVICE(ATHEROS, QCA6164_2_1_DEVICE_ID) }, /* PCI-E QCA6164 V2.1 */
  46. { PCI_VDEVICE(ATHEROS, QCA6174_2_1_DEVICE_ID) }, /* PCI-E QCA6174 V2.1 */
  47. { PCI_VDEVICE(ATHEROS, QCA99X0_2_0_DEVICE_ID) }, /* PCI-E QCA99X0 V2 */
  48. { PCI_VDEVICE(ATHEROS, QCA9888_2_0_DEVICE_ID) }, /* PCI-E QCA9888 V2 */
  49. { PCI_VDEVICE(ATHEROS, QCA9984_1_0_DEVICE_ID) }, /* PCI-E QCA9984 V1 */
  50. { PCI_VDEVICE(ATHEROS, QCA9377_1_0_DEVICE_ID) }, /* PCI-E QCA9377 V1 */
  51. { PCI_VDEVICE(ATHEROS, QCA9887_1_0_DEVICE_ID) }, /* PCI-E QCA9887 */
  52. {0}
  53. };
  54. static const struct ath10k_pci_supp_chip ath10k_pci_supp_chips[] = {
  55. /* QCA988X pre 2.0 chips are not supported because they need some nasty
  56. * hacks. ath10k doesn't have them and these devices crash horribly
  57. * because of that.
  58. */
  59. { QCA988X_2_0_DEVICE_ID, QCA988X_HW_2_0_CHIP_ID_REV },
  60. { QCA6164_2_1_DEVICE_ID, QCA6174_HW_2_1_CHIP_ID_REV },
  61. { QCA6164_2_1_DEVICE_ID, QCA6174_HW_2_2_CHIP_ID_REV },
  62. { QCA6164_2_1_DEVICE_ID, QCA6174_HW_3_0_CHIP_ID_REV },
  63. { QCA6164_2_1_DEVICE_ID, QCA6174_HW_3_1_CHIP_ID_REV },
  64. { QCA6164_2_1_DEVICE_ID, QCA6174_HW_3_2_CHIP_ID_REV },
  65. { QCA6174_2_1_DEVICE_ID, QCA6174_HW_2_1_CHIP_ID_REV },
  66. { QCA6174_2_1_DEVICE_ID, QCA6174_HW_2_2_CHIP_ID_REV },
  67. { QCA6174_2_1_DEVICE_ID, QCA6174_HW_3_0_CHIP_ID_REV },
  68. { QCA6174_2_1_DEVICE_ID, QCA6174_HW_3_1_CHIP_ID_REV },
  69. { QCA6174_2_1_DEVICE_ID, QCA6174_HW_3_2_CHIP_ID_REV },
  70. { QCA99X0_2_0_DEVICE_ID, QCA99X0_HW_2_0_CHIP_ID_REV },
  71. { QCA9984_1_0_DEVICE_ID, QCA9984_HW_1_0_CHIP_ID_REV },
  72. { QCA9888_2_0_DEVICE_ID, QCA9888_HW_2_0_CHIP_ID_REV },
  73. { QCA9377_1_0_DEVICE_ID, QCA9377_HW_1_0_CHIP_ID_REV },
  74. { QCA9377_1_0_DEVICE_ID, QCA9377_HW_1_1_CHIP_ID_REV },
  75. { QCA9887_1_0_DEVICE_ID, QCA9887_HW_1_0_CHIP_ID_REV },
  76. };
  77. static void ath10k_pci_buffer_cleanup(struct ath10k *ar);
  78. static int ath10k_pci_cold_reset(struct ath10k *ar);
  79. static int ath10k_pci_safe_chip_reset(struct ath10k *ar);
  80. static int ath10k_pci_init_irq(struct ath10k *ar);
  81. static int ath10k_pci_deinit_irq(struct ath10k *ar);
  82. static int ath10k_pci_request_irq(struct ath10k *ar);
  83. static void ath10k_pci_free_irq(struct ath10k *ar);
  84. static int ath10k_pci_bmi_wait(struct ath10k *ar,
  85. struct ath10k_ce_pipe *tx_pipe,
  86. struct ath10k_ce_pipe *rx_pipe,
  87. struct bmi_xfer *xfer);
  88. static int ath10k_pci_qca99x0_chip_reset(struct ath10k *ar);
  89. static void ath10k_pci_htc_tx_cb(struct ath10k_ce_pipe *ce_state);
  90. static void ath10k_pci_htc_rx_cb(struct ath10k_ce_pipe *ce_state);
  91. static void ath10k_pci_htt_tx_cb(struct ath10k_ce_pipe *ce_state);
  92. static void ath10k_pci_htt_rx_cb(struct ath10k_ce_pipe *ce_state);
  93. static void ath10k_pci_htt_htc_rx_cb(struct ath10k_ce_pipe *ce_state);
  94. static void ath10k_pci_pktlog_rx_cb(struct ath10k_ce_pipe *ce_state);
  95. static struct ce_attr host_ce_config_wlan[] = {
  96. /* CE0: host->target HTC control and raw streams */
  97. {
  98. .flags = CE_ATTR_FLAGS,
  99. .src_nentries = 16,
  100. .src_sz_max = 256,
  101. .dest_nentries = 0,
  102. .send_cb = ath10k_pci_htc_tx_cb,
  103. },
  104. /* CE1: target->host HTT + HTC control */
  105. {
  106. .flags = CE_ATTR_FLAGS,
  107. .src_nentries = 0,
  108. .src_sz_max = 2048,
  109. .dest_nentries = 512,
  110. .recv_cb = ath10k_pci_htt_htc_rx_cb,
  111. },
  112. /* CE2: target->host WMI */
  113. {
  114. .flags = CE_ATTR_FLAGS,
  115. .src_nentries = 0,
  116. .src_sz_max = 2048,
  117. .dest_nentries = 128,
  118. .recv_cb = ath10k_pci_htc_rx_cb,
  119. },
  120. /* CE3: host->target WMI */
  121. {
  122. .flags = CE_ATTR_FLAGS,
  123. .src_nentries = 32,
  124. .src_sz_max = 2048,
  125. .dest_nentries = 0,
  126. .send_cb = ath10k_pci_htc_tx_cb,
  127. },
  128. /* CE4: host->target HTT */
  129. {
  130. .flags = CE_ATTR_FLAGS | CE_ATTR_DIS_INTR,
  131. .src_nentries = CE_HTT_H2T_MSG_SRC_NENTRIES,
  132. .src_sz_max = 256,
  133. .dest_nentries = 0,
  134. .send_cb = ath10k_pci_htt_tx_cb,
  135. },
  136. /* CE5: target->host HTT (HIF->HTT) */
  137. {
  138. .flags = CE_ATTR_FLAGS,
  139. .src_nentries = 0,
  140. .src_sz_max = 512,
  141. .dest_nentries = 512,
  142. .recv_cb = ath10k_pci_htt_rx_cb,
  143. },
  144. /* CE6: target autonomous hif_memcpy */
  145. {
  146. .flags = CE_ATTR_FLAGS,
  147. .src_nentries = 0,
  148. .src_sz_max = 0,
  149. .dest_nentries = 0,
  150. },
  151. /* CE7: ce_diag, the Diagnostic Window */
  152. {
  153. .flags = CE_ATTR_FLAGS,
  154. .src_nentries = 2,
  155. .src_sz_max = DIAG_TRANSFER_LIMIT,
  156. .dest_nentries = 2,
  157. },
  158. /* CE8: target->host pktlog */
  159. {
  160. .flags = CE_ATTR_FLAGS,
  161. .src_nentries = 0,
  162. .src_sz_max = 2048,
  163. .dest_nentries = 128,
  164. .recv_cb = ath10k_pci_pktlog_rx_cb,
  165. },
  166. /* CE9 target autonomous qcache memcpy */
  167. {
  168. .flags = CE_ATTR_FLAGS,
  169. .src_nentries = 0,
  170. .src_sz_max = 0,
  171. .dest_nentries = 0,
  172. },
  173. /* CE10: target autonomous hif memcpy */
  174. {
  175. .flags = CE_ATTR_FLAGS,
  176. .src_nentries = 0,
  177. .src_sz_max = 0,
  178. .dest_nentries = 0,
  179. },
  180. /* CE11: target autonomous hif memcpy */
  181. {
  182. .flags = CE_ATTR_FLAGS,
  183. .src_nentries = 0,
  184. .src_sz_max = 0,
  185. .dest_nentries = 0,
  186. },
  187. };
  188. /* Target firmware's Copy Engine configuration. */
  189. static struct ce_pipe_config target_ce_config_wlan[] = {
  190. /* CE0: host->target HTC control and raw streams */
  191. {
  192. .pipenum = __cpu_to_le32(0),
  193. .pipedir = __cpu_to_le32(PIPEDIR_OUT),
  194. .nentries = __cpu_to_le32(32),
  195. .nbytes_max = __cpu_to_le32(256),
  196. .flags = __cpu_to_le32(CE_ATTR_FLAGS),
  197. .reserved = __cpu_to_le32(0),
  198. },
  199. /* CE1: target->host HTT + HTC control */
  200. {
  201. .pipenum = __cpu_to_le32(1),
  202. .pipedir = __cpu_to_le32(PIPEDIR_IN),
  203. .nentries = __cpu_to_le32(32),
  204. .nbytes_max = __cpu_to_le32(2048),
  205. .flags = __cpu_to_le32(CE_ATTR_FLAGS),
  206. .reserved = __cpu_to_le32(0),
  207. },
  208. /* CE2: target->host WMI */
  209. {
  210. .pipenum = __cpu_to_le32(2),
  211. .pipedir = __cpu_to_le32(PIPEDIR_IN),
  212. .nentries = __cpu_to_le32(64),
  213. .nbytes_max = __cpu_to_le32(2048),
  214. .flags = __cpu_to_le32(CE_ATTR_FLAGS),
  215. .reserved = __cpu_to_le32(0),
  216. },
  217. /* CE3: host->target WMI */
  218. {
  219. .pipenum = __cpu_to_le32(3),
  220. .pipedir = __cpu_to_le32(PIPEDIR_OUT),
  221. .nentries = __cpu_to_le32(32),
  222. .nbytes_max = __cpu_to_le32(2048),
  223. .flags = __cpu_to_le32(CE_ATTR_FLAGS),
  224. .reserved = __cpu_to_le32(0),
  225. },
  226. /* CE4: host->target HTT */
  227. {
  228. .pipenum = __cpu_to_le32(4),
  229. .pipedir = __cpu_to_le32(PIPEDIR_OUT),
  230. .nentries = __cpu_to_le32(256),
  231. .nbytes_max = __cpu_to_le32(256),
  232. .flags = __cpu_to_le32(CE_ATTR_FLAGS),
  233. .reserved = __cpu_to_le32(0),
  234. },
  235. /* NB: 50% of src nentries, since tx has 2 frags */
  236. /* CE5: target->host HTT (HIF->HTT) */
  237. {
  238. .pipenum = __cpu_to_le32(5),
  239. .pipedir = __cpu_to_le32(PIPEDIR_IN),
  240. .nentries = __cpu_to_le32(32),
  241. .nbytes_max = __cpu_to_le32(512),
  242. .flags = __cpu_to_le32(CE_ATTR_FLAGS),
  243. .reserved = __cpu_to_le32(0),
  244. },
  245. /* CE6: Reserved for target autonomous hif_memcpy */
  246. {
  247. .pipenum = __cpu_to_le32(6),
  248. .pipedir = __cpu_to_le32(PIPEDIR_INOUT),
  249. .nentries = __cpu_to_le32(32),
  250. .nbytes_max = __cpu_to_le32(4096),
  251. .flags = __cpu_to_le32(CE_ATTR_FLAGS),
  252. .reserved = __cpu_to_le32(0),
  253. },
  254. /* CE7 used only by Host */
  255. {
  256. .pipenum = __cpu_to_le32(7),
  257. .pipedir = __cpu_to_le32(PIPEDIR_INOUT),
  258. .nentries = __cpu_to_le32(0),
  259. .nbytes_max = __cpu_to_le32(0),
  260. .flags = __cpu_to_le32(0),
  261. .reserved = __cpu_to_le32(0),
  262. },
  263. /* CE8 target->host packtlog */
  264. {
  265. .pipenum = __cpu_to_le32(8),
  266. .pipedir = __cpu_to_le32(PIPEDIR_IN),
  267. .nentries = __cpu_to_le32(64),
  268. .nbytes_max = __cpu_to_le32(2048),
  269. .flags = __cpu_to_le32(CE_ATTR_FLAGS | CE_ATTR_DIS_INTR),
  270. .reserved = __cpu_to_le32(0),
  271. },
  272. /* CE9 target autonomous qcache memcpy */
  273. {
  274. .pipenum = __cpu_to_le32(9),
  275. .pipedir = __cpu_to_le32(PIPEDIR_INOUT),
  276. .nentries = __cpu_to_le32(32),
  277. .nbytes_max = __cpu_to_le32(2048),
  278. .flags = __cpu_to_le32(CE_ATTR_FLAGS | CE_ATTR_DIS_INTR),
  279. .reserved = __cpu_to_le32(0),
  280. },
  281. /* It not necessary to send target wlan configuration for CE10 & CE11
  282. * as these CEs are not actively used in target.
  283. */
  284. };
  285. /*
  286. * Map from service/endpoint to Copy Engine.
  287. * This table is derived from the CE_PCI TABLE, above.
  288. * It is passed to the Target at startup for use by firmware.
  289. */
  290. static struct service_to_pipe target_service_to_ce_map_wlan[] = {
  291. {
  292. __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_VO),
  293. __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
  294. __cpu_to_le32(3),
  295. },
  296. {
  297. __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_VO),
  298. __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
  299. __cpu_to_le32(2),
  300. },
  301. {
  302. __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_BK),
  303. __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
  304. __cpu_to_le32(3),
  305. },
  306. {
  307. __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_BK),
  308. __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
  309. __cpu_to_le32(2),
  310. },
  311. {
  312. __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_BE),
  313. __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
  314. __cpu_to_le32(3),
  315. },
  316. {
  317. __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_BE),
  318. __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
  319. __cpu_to_le32(2),
  320. },
  321. {
  322. __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_VI),
  323. __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
  324. __cpu_to_le32(3),
  325. },
  326. {
  327. __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_VI),
  328. __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
  329. __cpu_to_le32(2),
  330. },
  331. {
  332. __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_CONTROL),
  333. __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
  334. __cpu_to_le32(3),
  335. },
  336. {
  337. __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_CONTROL),
  338. __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
  339. __cpu_to_le32(2),
  340. },
  341. {
  342. __cpu_to_le32(ATH10K_HTC_SVC_ID_RSVD_CTRL),
  343. __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
  344. __cpu_to_le32(0),
  345. },
  346. {
  347. __cpu_to_le32(ATH10K_HTC_SVC_ID_RSVD_CTRL),
  348. __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
  349. __cpu_to_le32(1),
  350. },
  351. { /* not used */
  352. __cpu_to_le32(ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS),
  353. __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
  354. __cpu_to_le32(0),
  355. },
  356. { /* not used */
  357. __cpu_to_le32(ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS),
  358. __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
  359. __cpu_to_le32(1),
  360. },
  361. {
  362. __cpu_to_le32(ATH10K_HTC_SVC_ID_HTT_DATA_MSG),
  363. __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
  364. __cpu_to_le32(4),
  365. },
  366. {
  367. __cpu_to_le32(ATH10K_HTC_SVC_ID_HTT_DATA_MSG),
  368. __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
  369. __cpu_to_le32(5),
  370. },
  371. /* (Additions here) */
  372. { /* must be last */
  373. __cpu_to_le32(0),
  374. __cpu_to_le32(0),
  375. __cpu_to_le32(0),
  376. },
  377. };
  378. static bool ath10k_pci_is_awake(struct ath10k *ar)
  379. {
  380. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  381. u32 val = ioread32(ar_pci->mem + PCIE_LOCAL_BASE_ADDRESS +
  382. RTC_STATE_ADDRESS);
  383. return RTC_STATE_V_GET(val) == RTC_STATE_V_ON;
  384. }
  385. static void __ath10k_pci_wake(struct ath10k *ar)
  386. {
  387. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  388. lockdep_assert_held(&ar_pci->ps_lock);
  389. ath10k_dbg(ar, ATH10K_DBG_PCI_PS, "pci ps wake reg refcount %lu awake %d\n",
  390. ar_pci->ps_wake_refcount, ar_pci->ps_awake);
  391. iowrite32(PCIE_SOC_WAKE_V_MASK,
  392. ar_pci->mem + PCIE_LOCAL_BASE_ADDRESS +
  393. PCIE_SOC_WAKE_ADDRESS);
  394. }
  395. static void __ath10k_pci_sleep(struct ath10k *ar)
  396. {
  397. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  398. lockdep_assert_held(&ar_pci->ps_lock);
  399. ath10k_dbg(ar, ATH10K_DBG_PCI_PS, "pci ps sleep reg refcount %lu awake %d\n",
  400. ar_pci->ps_wake_refcount, ar_pci->ps_awake);
  401. iowrite32(PCIE_SOC_WAKE_RESET,
  402. ar_pci->mem + PCIE_LOCAL_BASE_ADDRESS +
  403. PCIE_SOC_WAKE_ADDRESS);
  404. ar_pci->ps_awake = false;
  405. }
  406. static int ath10k_pci_wake_wait(struct ath10k *ar)
  407. {
  408. int tot_delay = 0;
  409. int curr_delay = 5;
  410. while (tot_delay < PCIE_WAKE_TIMEOUT) {
  411. if (ath10k_pci_is_awake(ar)) {
  412. if (tot_delay > PCIE_WAKE_LATE_US)
  413. ath10k_warn(ar, "device wakeup took %d ms which is unusually long, otherwise it works normally.\n",
  414. tot_delay / 1000);
  415. return 0;
  416. }
  417. udelay(curr_delay);
  418. tot_delay += curr_delay;
  419. if (curr_delay < 50)
  420. curr_delay += 5;
  421. }
  422. return -ETIMEDOUT;
  423. }
  424. static int ath10k_pci_force_wake(struct ath10k *ar)
  425. {
  426. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  427. unsigned long flags;
  428. int ret = 0;
  429. if (ar_pci->pci_ps)
  430. return ret;
  431. spin_lock_irqsave(&ar_pci->ps_lock, flags);
  432. if (!ar_pci->ps_awake) {
  433. iowrite32(PCIE_SOC_WAKE_V_MASK,
  434. ar_pci->mem + PCIE_LOCAL_BASE_ADDRESS +
  435. PCIE_SOC_WAKE_ADDRESS);
  436. ret = ath10k_pci_wake_wait(ar);
  437. if (ret == 0)
  438. ar_pci->ps_awake = true;
  439. }
  440. spin_unlock_irqrestore(&ar_pci->ps_lock, flags);
  441. return ret;
  442. }
  443. static void ath10k_pci_force_sleep(struct ath10k *ar)
  444. {
  445. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  446. unsigned long flags;
  447. spin_lock_irqsave(&ar_pci->ps_lock, flags);
  448. iowrite32(PCIE_SOC_WAKE_RESET,
  449. ar_pci->mem + PCIE_LOCAL_BASE_ADDRESS +
  450. PCIE_SOC_WAKE_ADDRESS);
  451. ar_pci->ps_awake = false;
  452. spin_unlock_irqrestore(&ar_pci->ps_lock, flags);
  453. }
  454. static int ath10k_pci_wake(struct ath10k *ar)
  455. {
  456. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  457. unsigned long flags;
  458. int ret = 0;
  459. if (ar_pci->pci_ps == 0)
  460. return ret;
  461. spin_lock_irqsave(&ar_pci->ps_lock, flags);
  462. ath10k_dbg(ar, ATH10K_DBG_PCI_PS, "pci ps wake refcount %lu awake %d\n",
  463. ar_pci->ps_wake_refcount, ar_pci->ps_awake);
  464. /* This function can be called very frequently. To avoid excessive
  465. * CPU stalls for MMIO reads use a cache var to hold the device state.
  466. */
  467. if (!ar_pci->ps_awake) {
  468. __ath10k_pci_wake(ar);
  469. ret = ath10k_pci_wake_wait(ar);
  470. if (ret == 0)
  471. ar_pci->ps_awake = true;
  472. }
  473. if (ret == 0) {
  474. ar_pci->ps_wake_refcount++;
  475. WARN_ON(ar_pci->ps_wake_refcount == 0);
  476. }
  477. spin_unlock_irqrestore(&ar_pci->ps_lock, flags);
  478. return ret;
  479. }
  480. static void ath10k_pci_sleep(struct ath10k *ar)
  481. {
  482. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  483. unsigned long flags;
  484. if (ar_pci->pci_ps == 0)
  485. return;
  486. spin_lock_irqsave(&ar_pci->ps_lock, flags);
  487. ath10k_dbg(ar, ATH10K_DBG_PCI_PS, "pci ps sleep refcount %lu awake %d\n",
  488. ar_pci->ps_wake_refcount, ar_pci->ps_awake);
  489. if (WARN_ON(ar_pci->ps_wake_refcount == 0))
  490. goto skip;
  491. ar_pci->ps_wake_refcount--;
  492. mod_timer(&ar_pci->ps_timer, jiffies +
  493. msecs_to_jiffies(ATH10K_PCI_SLEEP_GRACE_PERIOD_MSEC));
  494. skip:
  495. spin_unlock_irqrestore(&ar_pci->ps_lock, flags);
  496. }
  497. static void ath10k_pci_ps_timer(struct timer_list *t)
  498. {
  499. struct ath10k_pci *ar_pci = from_timer(ar_pci, t, ps_timer);
  500. struct ath10k *ar = ar_pci->ar;
  501. unsigned long flags;
  502. spin_lock_irqsave(&ar_pci->ps_lock, flags);
  503. ath10k_dbg(ar, ATH10K_DBG_PCI_PS, "pci ps timer refcount %lu awake %d\n",
  504. ar_pci->ps_wake_refcount, ar_pci->ps_awake);
  505. if (ar_pci->ps_wake_refcount > 0)
  506. goto skip;
  507. __ath10k_pci_sleep(ar);
  508. skip:
  509. spin_unlock_irqrestore(&ar_pci->ps_lock, flags);
  510. }
  511. static void ath10k_pci_sleep_sync(struct ath10k *ar)
  512. {
  513. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  514. unsigned long flags;
  515. if (ar_pci->pci_ps == 0) {
  516. ath10k_pci_force_sleep(ar);
  517. return;
  518. }
  519. del_timer_sync(&ar_pci->ps_timer);
  520. spin_lock_irqsave(&ar_pci->ps_lock, flags);
  521. WARN_ON(ar_pci->ps_wake_refcount > 0);
  522. __ath10k_pci_sleep(ar);
  523. spin_unlock_irqrestore(&ar_pci->ps_lock, flags);
  524. }
  525. static void ath10k_bus_pci_write32(struct ath10k *ar, u32 offset, u32 value)
  526. {
  527. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  528. int ret;
  529. if (unlikely(offset + sizeof(value) > ar_pci->mem_len)) {
  530. ath10k_warn(ar, "refusing to write mmio out of bounds at 0x%08x - 0x%08zx (max 0x%08zx)\n",
  531. offset, offset + sizeof(value), ar_pci->mem_len);
  532. return;
  533. }
  534. ret = ath10k_pci_wake(ar);
  535. if (ret) {
  536. ath10k_warn(ar, "failed to wake target for write32 of 0x%08x at 0x%08x: %d\n",
  537. value, offset, ret);
  538. return;
  539. }
  540. iowrite32(value, ar_pci->mem + offset);
  541. ath10k_pci_sleep(ar);
  542. }
  543. static u32 ath10k_bus_pci_read32(struct ath10k *ar, u32 offset)
  544. {
  545. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  546. u32 val;
  547. int ret;
  548. if (unlikely(offset + sizeof(val) > ar_pci->mem_len)) {
  549. ath10k_warn(ar, "refusing to read mmio out of bounds at 0x%08x - 0x%08zx (max 0x%08zx)\n",
  550. offset, offset + sizeof(val), ar_pci->mem_len);
  551. return 0;
  552. }
  553. ret = ath10k_pci_wake(ar);
  554. if (ret) {
  555. ath10k_warn(ar, "failed to wake target for read32 at 0x%08x: %d\n",
  556. offset, ret);
  557. return 0xffffffff;
  558. }
  559. val = ioread32(ar_pci->mem + offset);
  560. ath10k_pci_sleep(ar);
  561. return val;
  562. }
  563. inline void ath10k_pci_write32(struct ath10k *ar, u32 offset, u32 value)
  564. {
  565. struct ath10k_ce *ce = ath10k_ce_priv(ar);
  566. ce->bus_ops->write32(ar, offset, value);
  567. }
  568. inline u32 ath10k_pci_read32(struct ath10k *ar, u32 offset)
  569. {
  570. struct ath10k_ce *ce = ath10k_ce_priv(ar);
  571. return ce->bus_ops->read32(ar, offset);
  572. }
  573. u32 ath10k_pci_soc_read32(struct ath10k *ar, u32 addr)
  574. {
  575. return ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS + addr);
  576. }
  577. void ath10k_pci_soc_write32(struct ath10k *ar, u32 addr, u32 val)
  578. {
  579. ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + addr, val);
  580. }
  581. u32 ath10k_pci_reg_read32(struct ath10k *ar, u32 addr)
  582. {
  583. return ath10k_pci_read32(ar, PCIE_LOCAL_BASE_ADDRESS + addr);
  584. }
  585. void ath10k_pci_reg_write32(struct ath10k *ar, u32 addr, u32 val)
  586. {
  587. ath10k_pci_write32(ar, PCIE_LOCAL_BASE_ADDRESS + addr, val);
  588. }
  589. bool ath10k_pci_irq_pending(struct ath10k *ar)
  590. {
  591. u32 cause;
  592. /* Check if the shared legacy irq is for us */
  593. cause = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
  594. PCIE_INTR_CAUSE_ADDRESS);
  595. if (cause & (PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL))
  596. return true;
  597. return false;
  598. }
  599. void ath10k_pci_disable_and_clear_legacy_irq(struct ath10k *ar)
  600. {
  601. /* IMPORTANT: INTR_CLR register has to be set after
  602. * INTR_ENABLE is set to 0, otherwise interrupt can not be
  603. * really cleared.
  604. */
  605. ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
  606. 0);
  607. ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_CLR_ADDRESS,
  608. PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL);
  609. /* IMPORTANT: this extra read transaction is required to
  610. * flush the posted write buffer.
  611. */
  612. (void)ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
  613. PCIE_INTR_ENABLE_ADDRESS);
  614. }
  615. void ath10k_pci_enable_legacy_irq(struct ath10k *ar)
  616. {
  617. ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS +
  618. PCIE_INTR_ENABLE_ADDRESS,
  619. PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL);
  620. /* IMPORTANT: this extra read transaction is required to
  621. * flush the posted write buffer.
  622. */
  623. (void)ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
  624. PCIE_INTR_ENABLE_ADDRESS);
  625. }
  626. static inline const char *ath10k_pci_get_irq_method(struct ath10k *ar)
  627. {
  628. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  629. if (ar_pci->oper_irq_mode == ATH10K_PCI_IRQ_MSI)
  630. return "msi";
  631. return "legacy";
  632. }
  633. static int __ath10k_pci_rx_post_buf(struct ath10k_pci_pipe *pipe)
  634. {
  635. struct ath10k *ar = pipe->hif_ce_state;
  636. struct ath10k_ce *ce = ath10k_ce_priv(ar);
  637. struct ath10k_ce_pipe *ce_pipe = pipe->ce_hdl;
  638. struct sk_buff *skb;
  639. dma_addr_t paddr;
  640. int ret;
  641. skb = dev_alloc_skb(pipe->buf_sz);
  642. if (!skb)
  643. return -ENOMEM;
  644. WARN_ONCE((unsigned long)skb->data & 3, "unaligned skb");
  645. paddr = dma_map_single(ar->dev, skb->data,
  646. skb->len + skb_tailroom(skb),
  647. DMA_FROM_DEVICE);
  648. if (unlikely(dma_mapping_error(ar->dev, paddr))) {
  649. ath10k_warn(ar, "failed to dma map pci rx buf\n");
  650. dev_kfree_skb_any(skb);
  651. return -EIO;
  652. }
  653. ATH10K_SKB_RXCB(skb)->paddr = paddr;
  654. spin_lock_bh(&ce->ce_lock);
  655. ret = __ath10k_ce_rx_post_buf(ce_pipe, skb, paddr);
  656. spin_unlock_bh(&ce->ce_lock);
  657. if (ret) {
  658. dma_unmap_single(ar->dev, paddr, skb->len + skb_tailroom(skb),
  659. DMA_FROM_DEVICE);
  660. dev_kfree_skb_any(skb);
  661. return ret;
  662. }
  663. return 0;
  664. }
  665. static void ath10k_pci_rx_post_pipe(struct ath10k_pci_pipe *pipe)
  666. {
  667. struct ath10k *ar = pipe->hif_ce_state;
  668. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  669. struct ath10k_ce *ce = ath10k_ce_priv(ar);
  670. struct ath10k_ce_pipe *ce_pipe = pipe->ce_hdl;
  671. int ret, num;
  672. if (pipe->buf_sz == 0)
  673. return;
  674. if (!ce_pipe->dest_ring)
  675. return;
  676. spin_lock_bh(&ce->ce_lock);
  677. num = __ath10k_ce_rx_num_free_bufs(ce_pipe);
  678. spin_unlock_bh(&ce->ce_lock);
  679. while (num >= 0) {
  680. ret = __ath10k_pci_rx_post_buf(pipe);
  681. if (ret) {
  682. if (ret == -ENOSPC)
  683. break;
  684. ath10k_warn(ar, "failed to post pci rx buf: %d\n", ret);
  685. mod_timer(&ar_pci->rx_post_retry, jiffies +
  686. ATH10K_PCI_RX_POST_RETRY_MS);
  687. break;
  688. }
  689. num--;
  690. }
  691. }
  692. void ath10k_pci_rx_post(struct ath10k *ar)
  693. {
  694. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  695. int i;
  696. for (i = 0; i < CE_COUNT; i++)
  697. ath10k_pci_rx_post_pipe(&ar_pci->pipe_info[i]);
  698. }
  699. void ath10k_pci_rx_replenish_retry(struct timer_list *t)
  700. {
  701. struct ath10k_pci *ar_pci = from_timer(ar_pci, t, rx_post_retry);
  702. struct ath10k *ar = ar_pci->ar;
  703. ath10k_pci_rx_post(ar);
  704. }
  705. static u32 ath10k_pci_qca988x_targ_cpu_to_ce_addr(struct ath10k *ar, u32 addr)
  706. {
  707. u32 val = 0, region = addr & 0xfffff;
  708. val = (ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS + CORE_CTRL_ADDRESS)
  709. & 0x7ff) << 21;
  710. val |= 0x100000 | region;
  711. return val;
  712. }
  713. static u32 ath10k_pci_qca99x0_targ_cpu_to_ce_addr(struct ath10k *ar, u32 addr)
  714. {
  715. u32 val = 0, region = addr & 0xfffff;
  716. val = ath10k_pci_read32(ar, PCIE_BAR_REG_ADDRESS);
  717. val |= 0x100000 | region;
  718. return val;
  719. }
  720. static u32 ath10k_pci_targ_cpu_to_ce_addr(struct ath10k *ar, u32 addr)
  721. {
  722. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  723. if (WARN_ON_ONCE(!ar_pci->targ_cpu_to_ce_addr))
  724. return -ENOTSUPP;
  725. return ar_pci->targ_cpu_to_ce_addr(ar, addr);
  726. }
  727. /*
  728. * Diagnostic read/write access is provided for startup/config/debug usage.
  729. * Caller must guarantee proper alignment, when applicable, and single user
  730. * at any moment.
  731. */
  732. static int ath10k_pci_diag_read_mem(struct ath10k *ar, u32 address, void *data,
  733. int nbytes)
  734. {
  735. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  736. struct ath10k_ce *ce = ath10k_ce_priv(ar);
  737. int ret = 0;
  738. u32 *buf;
  739. unsigned int completed_nbytes, alloc_nbytes, remaining_bytes;
  740. struct ath10k_ce_pipe *ce_diag;
  741. /* Host buffer address in CE space */
  742. u32 ce_data;
  743. dma_addr_t ce_data_base = 0;
  744. void *data_buf = NULL;
  745. int i;
  746. spin_lock_bh(&ce->ce_lock);
  747. ce_diag = ar_pci->ce_diag;
  748. /*
  749. * Allocate a temporary bounce buffer to hold caller's data
  750. * to be DMA'ed from Target. This guarantees
  751. * 1) 4-byte alignment
  752. * 2) Buffer in DMA-able space
  753. */
  754. alloc_nbytes = min_t(unsigned int, nbytes, DIAG_TRANSFER_LIMIT);
  755. data_buf = (unsigned char *)dma_zalloc_coherent(ar->dev,
  756. alloc_nbytes,
  757. &ce_data_base,
  758. GFP_ATOMIC);
  759. if (!data_buf) {
  760. ret = -ENOMEM;
  761. goto done;
  762. }
  763. remaining_bytes = nbytes;
  764. ce_data = ce_data_base;
  765. while (remaining_bytes) {
  766. nbytes = min_t(unsigned int, remaining_bytes,
  767. DIAG_TRANSFER_LIMIT);
  768. ret = __ath10k_ce_rx_post_buf(ce_diag, &ce_data, ce_data);
  769. if (ret != 0)
  770. goto done;
  771. /* Request CE to send from Target(!) address to Host buffer */
  772. /*
  773. * The address supplied by the caller is in the
  774. * Target CPU virtual address space.
  775. *
  776. * In order to use this address with the diagnostic CE,
  777. * convert it from Target CPU virtual address space
  778. * to CE address space
  779. */
  780. address = ath10k_pci_targ_cpu_to_ce_addr(ar, address);
  781. ret = ath10k_ce_send_nolock(ce_diag, NULL, (u32)address, nbytes, 0,
  782. 0);
  783. if (ret)
  784. goto done;
  785. i = 0;
  786. while (ath10k_ce_completed_send_next_nolock(ce_diag,
  787. NULL) != 0) {
  788. mdelay(1);
  789. if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
  790. ret = -EBUSY;
  791. goto done;
  792. }
  793. }
  794. i = 0;
  795. while (ath10k_ce_completed_recv_next_nolock(ce_diag,
  796. (void **)&buf,
  797. &completed_nbytes)
  798. != 0) {
  799. mdelay(1);
  800. if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
  801. ret = -EBUSY;
  802. goto done;
  803. }
  804. }
  805. if (nbytes != completed_nbytes) {
  806. ret = -EIO;
  807. goto done;
  808. }
  809. if (*buf != ce_data) {
  810. ret = -EIO;
  811. goto done;
  812. }
  813. remaining_bytes -= nbytes;
  814. memcpy(data, data_buf, nbytes);
  815. address += nbytes;
  816. data += nbytes;
  817. }
  818. done:
  819. if (data_buf)
  820. dma_free_coherent(ar->dev, alloc_nbytes, data_buf,
  821. ce_data_base);
  822. spin_unlock_bh(&ce->ce_lock);
  823. return ret;
  824. }
  825. static int ath10k_pci_diag_read32(struct ath10k *ar, u32 address, u32 *value)
  826. {
  827. __le32 val = 0;
  828. int ret;
  829. ret = ath10k_pci_diag_read_mem(ar, address, &val, sizeof(val));
  830. *value = __le32_to_cpu(val);
  831. return ret;
  832. }
  833. static int __ath10k_pci_diag_read_hi(struct ath10k *ar, void *dest,
  834. u32 src, u32 len)
  835. {
  836. u32 host_addr, addr;
  837. int ret;
  838. host_addr = host_interest_item_address(src);
  839. ret = ath10k_pci_diag_read32(ar, host_addr, &addr);
  840. if (ret != 0) {
  841. ath10k_warn(ar, "failed to get memcpy hi address for firmware address %d: %d\n",
  842. src, ret);
  843. return ret;
  844. }
  845. ret = ath10k_pci_diag_read_mem(ar, addr, dest, len);
  846. if (ret != 0) {
  847. ath10k_warn(ar, "failed to memcpy firmware memory from %d (%d B): %d\n",
  848. addr, len, ret);
  849. return ret;
  850. }
  851. return 0;
  852. }
  853. #define ath10k_pci_diag_read_hi(ar, dest, src, len) \
  854. __ath10k_pci_diag_read_hi(ar, dest, HI_ITEM(src), len)
  855. int ath10k_pci_diag_write_mem(struct ath10k *ar, u32 address,
  856. const void *data, int nbytes)
  857. {
  858. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  859. struct ath10k_ce *ce = ath10k_ce_priv(ar);
  860. int ret = 0;
  861. u32 *buf;
  862. unsigned int completed_nbytes, orig_nbytes, remaining_bytes;
  863. struct ath10k_ce_pipe *ce_diag;
  864. void *data_buf = NULL;
  865. u32 ce_data; /* Host buffer address in CE space */
  866. dma_addr_t ce_data_base = 0;
  867. int i;
  868. spin_lock_bh(&ce->ce_lock);
  869. ce_diag = ar_pci->ce_diag;
  870. /*
  871. * Allocate a temporary bounce buffer to hold caller's data
  872. * to be DMA'ed to Target. This guarantees
  873. * 1) 4-byte alignment
  874. * 2) Buffer in DMA-able space
  875. */
  876. orig_nbytes = nbytes;
  877. data_buf = (unsigned char *)dma_alloc_coherent(ar->dev,
  878. orig_nbytes,
  879. &ce_data_base,
  880. GFP_ATOMIC);
  881. if (!data_buf) {
  882. ret = -ENOMEM;
  883. goto done;
  884. }
  885. /* Copy caller's data to allocated DMA buf */
  886. memcpy(data_buf, data, orig_nbytes);
  887. /*
  888. * The address supplied by the caller is in the
  889. * Target CPU virtual address space.
  890. *
  891. * In order to use this address with the diagnostic CE,
  892. * convert it from
  893. * Target CPU virtual address space
  894. * to
  895. * CE address space
  896. */
  897. address = ath10k_pci_targ_cpu_to_ce_addr(ar, address);
  898. remaining_bytes = orig_nbytes;
  899. ce_data = ce_data_base;
  900. while (remaining_bytes) {
  901. /* FIXME: check cast */
  902. nbytes = min_t(int, remaining_bytes, DIAG_TRANSFER_LIMIT);
  903. /* Set up to receive directly into Target(!) address */
  904. ret = __ath10k_ce_rx_post_buf(ce_diag, &address, address);
  905. if (ret != 0)
  906. goto done;
  907. /*
  908. * Request CE to send caller-supplied data that
  909. * was copied to bounce buffer to Target(!) address.
  910. */
  911. ret = ath10k_ce_send_nolock(ce_diag, NULL, (u32)ce_data,
  912. nbytes, 0, 0);
  913. if (ret != 0)
  914. goto done;
  915. i = 0;
  916. while (ath10k_ce_completed_send_next_nolock(ce_diag,
  917. NULL) != 0) {
  918. mdelay(1);
  919. if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
  920. ret = -EBUSY;
  921. goto done;
  922. }
  923. }
  924. i = 0;
  925. while (ath10k_ce_completed_recv_next_nolock(ce_diag,
  926. (void **)&buf,
  927. &completed_nbytes)
  928. != 0) {
  929. mdelay(1);
  930. if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
  931. ret = -EBUSY;
  932. goto done;
  933. }
  934. }
  935. if (nbytes != completed_nbytes) {
  936. ret = -EIO;
  937. goto done;
  938. }
  939. if (*buf != address) {
  940. ret = -EIO;
  941. goto done;
  942. }
  943. remaining_bytes -= nbytes;
  944. address += nbytes;
  945. ce_data += nbytes;
  946. }
  947. done:
  948. if (data_buf) {
  949. dma_free_coherent(ar->dev, orig_nbytes, data_buf,
  950. ce_data_base);
  951. }
  952. if (ret != 0)
  953. ath10k_warn(ar, "failed to write diag value at 0x%x: %d\n",
  954. address, ret);
  955. spin_unlock_bh(&ce->ce_lock);
  956. return ret;
  957. }
  958. static int ath10k_pci_diag_write32(struct ath10k *ar, u32 address, u32 value)
  959. {
  960. __le32 val = __cpu_to_le32(value);
  961. return ath10k_pci_diag_write_mem(ar, address, &val, sizeof(val));
  962. }
  963. /* Called by lower (CE) layer when a send to Target completes. */
  964. static void ath10k_pci_htc_tx_cb(struct ath10k_ce_pipe *ce_state)
  965. {
  966. struct ath10k *ar = ce_state->ar;
  967. struct sk_buff_head list;
  968. struct sk_buff *skb;
  969. __skb_queue_head_init(&list);
  970. while (ath10k_ce_completed_send_next(ce_state, (void **)&skb) == 0) {
  971. /* no need to call tx completion for NULL pointers */
  972. if (skb == NULL)
  973. continue;
  974. __skb_queue_tail(&list, skb);
  975. }
  976. while ((skb = __skb_dequeue(&list)))
  977. ath10k_htc_tx_completion_handler(ar, skb);
  978. }
  979. static void ath10k_pci_process_rx_cb(struct ath10k_ce_pipe *ce_state,
  980. void (*callback)(struct ath10k *ar,
  981. struct sk_buff *skb))
  982. {
  983. struct ath10k *ar = ce_state->ar;
  984. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  985. struct ath10k_pci_pipe *pipe_info = &ar_pci->pipe_info[ce_state->id];
  986. struct sk_buff *skb;
  987. struct sk_buff_head list;
  988. void *transfer_context;
  989. unsigned int nbytes, max_nbytes;
  990. __skb_queue_head_init(&list);
  991. while (ath10k_ce_completed_recv_next(ce_state, &transfer_context,
  992. &nbytes) == 0) {
  993. skb = transfer_context;
  994. max_nbytes = skb->len + skb_tailroom(skb);
  995. dma_unmap_single(ar->dev, ATH10K_SKB_RXCB(skb)->paddr,
  996. max_nbytes, DMA_FROM_DEVICE);
  997. if (unlikely(max_nbytes < nbytes)) {
  998. ath10k_warn(ar, "rxed more than expected (nbytes %d, max %d)",
  999. nbytes, max_nbytes);
  1000. dev_kfree_skb_any(skb);
  1001. continue;
  1002. }
  1003. skb_put(skb, nbytes);
  1004. __skb_queue_tail(&list, skb);
  1005. }
  1006. while ((skb = __skb_dequeue(&list))) {
  1007. ath10k_dbg(ar, ATH10K_DBG_PCI, "pci rx ce pipe %d len %d\n",
  1008. ce_state->id, skb->len);
  1009. ath10k_dbg_dump(ar, ATH10K_DBG_PCI_DUMP, NULL, "pci rx: ",
  1010. skb->data, skb->len);
  1011. callback(ar, skb);
  1012. }
  1013. ath10k_pci_rx_post_pipe(pipe_info);
  1014. }
  1015. static void ath10k_pci_process_htt_rx_cb(struct ath10k_ce_pipe *ce_state,
  1016. void (*callback)(struct ath10k *ar,
  1017. struct sk_buff *skb))
  1018. {
  1019. struct ath10k *ar = ce_state->ar;
  1020. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1021. struct ath10k_pci_pipe *pipe_info = &ar_pci->pipe_info[ce_state->id];
  1022. struct ath10k_ce_pipe *ce_pipe = pipe_info->ce_hdl;
  1023. struct sk_buff *skb;
  1024. struct sk_buff_head list;
  1025. void *transfer_context;
  1026. unsigned int nbytes, max_nbytes, nentries;
  1027. int orig_len;
  1028. /* No need to aquire ce_lock for CE5, since this is the only place CE5
  1029. * is processed other than init and deinit. Before releasing CE5
  1030. * buffers, interrupts are disabled. Thus CE5 access is serialized.
  1031. */
  1032. __skb_queue_head_init(&list);
  1033. while (ath10k_ce_completed_recv_next_nolock(ce_state, &transfer_context,
  1034. &nbytes) == 0) {
  1035. skb = transfer_context;
  1036. max_nbytes = skb->len + skb_tailroom(skb);
  1037. if (unlikely(max_nbytes < nbytes)) {
  1038. ath10k_warn(ar, "rxed more than expected (nbytes %d, max %d)",
  1039. nbytes, max_nbytes);
  1040. continue;
  1041. }
  1042. dma_sync_single_for_cpu(ar->dev, ATH10K_SKB_RXCB(skb)->paddr,
  1043. max_nbytes, DMA_FROM_DEVICE);
  1044. skb_put(skb, nbytes);
  1045. __skb_queue_tail(&list, skb);
  1046. }
  1047. nentries = skb_queue_len(&list);
  1048. while ((skb = __skb_dequeue(&list))) {
  1049. ath10k_dbg(ar, ATH10K_DBG_PCI, "pci rx ce pipe %d len %d\n",
  1050. ce_state->id, skb->len);
  1051. ath10k_dbg_dump(ar, ATH10K_DBG_PCI_DUMP, NULL, "pci rx: ",
  1052. skb->data, skb->len);
  1053. orig_len = skb->len;
  1054. callback(ar, skb);
  1055. skb_push(skb, orig_len - skb->len);
  1056. skb_reset_tail_pointer(skb);
  1057. skb_trim(skb, 0);
  1058. /*let device gain the buffer again*/
  1059. dma_sync_single_for_device(ar->dev, ATH10K_SKB_RXCB(skb)->paddr,
  1060. skb->len + skb_tailroom(skb),
  1061. DMA_FROM_DEVICE);
  1062. }
  1063. ath10k_ce_rx_update_write_idx(ce_pipe, nentries);
  1064. }
  1065. /* Called by lower (CE) layer when data is received from the Target. */
  1066. static void ath10k_pci_htc_rx_cb(struct ath10k_ce_pipe *ce_state)
  1067. {
  1068. ath10k_pci_process_rx_cb(ce_state, ath10k_htc_rx_completion_handler);
  1069. }
  1070. static void ath10k_pci_htt_htc_rx_cb(struct ath10k_ce_pipe *ce_state)
  1071. {
  1072. /* CE4 polling needs to be done whenever CE pipe which transports
  1073. * HTT Rx (target->host) is processed.
  1074. */
  1075. ath10k_ce_per_engine_service(ce_state->ar, 4);
  1076. ath10k_pci_process_rx_cb(ce_state, ath10k_htc_rx_completion_handler);
  1077. }
  1078. /* Called by lower (CE) layer when data is received from the Target.
  1079. * Only 10.4 firmware uses separate CE to transfer pktlog data.
  1080. */
  1081. static void ath10k_pci_pktlog_rx_cb(struct ath10k_ce_pipe *ce_state)
  1082. {
  1083. ath10k_pci_process_rx_cb(ce_state,
  1084. ath10k_htt_rx_pktlog_completion_handler);
  1085. }
  1086. /* Called by lower (CE) layer when a send to HTT Target completes. */
  1087. static void ath10k_pci_htt_tx_cb(struct ath10k_ce_pipe *ce_state)
  1088. {
  1089. struct ath10k *ar = ce_state->ar;
  1090. struct sk_buff *skb;
  1091. while (ath10k_ce_completed_send_next(ce_state, (void **)&skb) == 0) {
  1092. /* no need to call tx completion for NULL pointers */
  1093. if (!skb)
  1094. continue;
  1095. dma_unmap_single(ar->dev, ATH10K_SKB_CB(skb)->paddr,
  1096. skb->len, DMA_TO_DEVICE);
  1097. ath10k_htt_hif_tx_complete(ar, skb);
  1098. }
  1099. }
  1100. static void ath10k_pci_htt_rx_deliver(struct ath10k *ar, struct sk_buff *skb)
  1101. {
  1102. skb_pull(skb, sizeof(struct ath10k_htc_hdr));
  1103. ath10k_htt_t2h_msg_handler(ar, skb);
  1104. }
  1105. /* Called by lower (CE) layer when HTT data is received from the Target. */
  1106. static void ath10k_pci_htt_rx_cb(struct ath10k_ce_pipe *ce_state)
  1107. {
  1108. /* CE4 polling needs to be done whenever CE pipe which transports
  1109. * HTT Rx (target->host) is processed.
  1110. */
  1111. ath10k_ce_per_engine_service(ce_state->ar, 4);
  1112. ath10k_pci_process_htt_rx_cb(ce_state, ath10k_pci_htt_rx_deliver);
  1113. }
  1114. int ath10k_pci_hif_tx_sg(struct ath10k *ar, u8 pipe_id,
  1115. struct ath10k_hif_sg_item *items, int n_items)
  1116. {
  1117. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1118. struct ath10k_ce *ce = ath10k_ce_priv(ar);
  1119. struct ath10k_pci_pipe *pci_pipe = &ar_pci->pipe_info[pipe_id];
  1120. struct ath10k_ce_pipe *ce_pipe = pci_pipe->ce_hdl;
  1121. struct ath10k_ce_ring *src_ring = ce_pipe->src_ring;
  1122. unsigned int nentries_mask;
  1123. unsigned int sw_index;
  1124. unsigned int write_index;
  1125. int err, i = 0;
  1126. spin_lock_bh(&ce->ce_lock);
  1127. nentries_mask = src_ring->nentries_mask;
  1128. sw_index = src_ring->sw_index;
  1129. write_index = src_ring->write_index;
  1130. if (unlikely(CE_RING_DELTA(nentries_mask,
  1131. write_index, sw_index - 1) < n_items)) {
  1132. err = -ENOBUFS;
  1133. goto err;
  1134. }
  1135. for (i = 0; i < n_items - 1; i++) {
  1136. ath10k_dbg(ar, ATH10K_DBG_PCI,
  1137. "pci tx item %d paddr 0x%08x len %d n_items %d\n",
  1138. i, items[i].paddr, items[i].len, n_items);
  1139. ath10k_dbg_dump(ar, ATH10K_DBG_PCI_DUMP, NULL, "pci tx data: ",
  1140. items[i].vaddr, items[i].len);
  1141. err = ath10k_ce_send_nolock(ce_pipe,
  1142. items[i].transfer_context,
  1143. items[i].paddr,
  1144. items[i].len,
  1145. items[i].transfer_id,
  1146. CE_SEND_FLAG_GATHER);
  1147. if (err)
  1148. goto err;
  1149. }
  1150. /* `i` is equal to `n_items -1` after for() */
  1151. ath10k_dbg(ar, ATH10K_DBG_PCI,
  1152. "pci tx item %d paddr 0x%08x len %d n_items %d\n",
  1153. i, items[i].paddr, items[i].len, n_items);
  1154. ath10k_dbg_dump(ar, ATH10K_DBG_PCI_DUMP, NULL, "pci tx data: ",
  1155. items[i].vaddr, items[i].len);
  1156. err = ath10k_ce_send_nolock(ce_pipe,
  1157. items[i].transfer_context,
  1158. items[i].paddr,
  1159. items[i].len,
  1160. items[i].transfer_id,
  1161. 0);
  1162. if (err)
  1163. goto err;
  1164. spin_unlock_bh(&ce->ce_lock);
  1165. return 0;
  1166. err:
  1167. for (; i > 0; i--)
  1168. __ath10k_ce_send_revert(ce_pipe);
  1169. spin_unlock_bh(&ce->ce_lock);
  1170. return err;
  1171. }
  1172. int ath10k_pci_hif_diag_read(struct ath10k *ar, u32 address, void *buf,
  1173. size_t buf_len)
  1174. {
  1175. return ath10k_pci_diag_read_mem(ar, address, buf, buf_len);
  1176. }
  1177. u16 ath10k_pci_hif_get_free_queue_number(struct ath10k *ar, u8 pipe)
  1178. {
  1179. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1180. ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif get free queue number\n");
  1181. return ath10k_ce_num_free_src_entries(ar_pci->pipe_info[pipe].ce_hdl);
  1182. }
  1183. static void ath10k_pci_dump_registers(struct ath10k *ar,
  1184. struct ath10k_fw_crash_data *crash_data)
  1185. {
  1186. __le32 reg_dump_values[REG_DUMP_COUNT_QCA988X] = {};
  1187. int i, ret;
  1188. lockdep_assert_held(&ar->data_lock);
  1189. ret = ath10k_pci_diag_read_hi(ar, &reg_dump_values[0],
  1190. hi_failure_state,
  1191. REG_DUMP_COUNT_QCA988X * sizeof(__le32));
  1192. if (ret) {
  1193. ath10k_err(ar, "failed to read firmware dump area: %d\n", ret);
  1194. return;
  1195. }
  1196. BUILD_BUG_ON(REG_DUMP_COUNT_QCA988X % 4);
  1197. ath10k_err(ar, "firmware register dump:\n");
  1198. for (i = 0; i < REG_DUMP_COUNT_QCA988X; i += 4)
  1199. ath10k_err(ar, "[%02d]: 0x%08X 0x%08X 0x%08X 0x%08X\n",
  1200. i,
  1201. __le32_to_cpu(reg_dump_values[i]),
  1202. __le32_to_cpu(reg_dump_values[i + 1]),
  1203. __le32_to_cpu(reg_dump_values[i + 2]),
  1204. __le32_to_cpu(reg_dump_values[i + 3]));
  1205. if (!crash_data)
  1206. return;
  1207. for (i = 0; i < REG_DUMP_COUNT_QCA988X; i++)
  1208. crash_data->registers[i] = reg_dump_values[i];
  1209. }
  1210. static void ath10k_pci_fw_crashed_dump(struct ath10k *ar)
  1211. {
  1212. struct ath10k_fw_crash_data *crash_data;
  1213. char guid[UUID_STRING_LEN + 1];
  1214. spin_lock_bh(&ar->data_lock);
  1215. ar->stats.fw_crash_counter++;
  1216. crash_data = ath10k_debug_get_new_fw_crash_data(ar);
  1217. if (crash_data)
  1218. scnprintf(guid, sizeof(guid), "%pUl", &crash_data->guid);
  1219. else
  1220. scnprintf(guid, sizeof(guid), "n/a");
  1221. ath10k_err(ar, "firmware crashed! (guid %s)\n", guid);
  1222. ath10k_print_driver_info(ar);
  1223. ath10k_pci_dump_registers(ar, crash_data);
  1224. ath10k_ce_dump_registers(ar, crash_data);
  1225. spin_unlock_bh(&ar->data_lock);
  1226. queue_work(ar->workqueue, &ar->restart_work);
  1227. }
  1228. void ath10k_pci_hif_send_complete_check(struct ath10k *ar, u8 pipe,
  1229. int force)
  1230. {
  1231. ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif send complete check\n");
  1232. if (!force) {
  1233. int resources;
  1234. /*
  1235. * Decide whether to actually poll for completions, or just
  1236. * wait for a later chance.
  1237. * If there seem to be plenty of resources left, then just wait
  1238. * since checking involves reading a CE register, which is a
  1239. * relatively expensive operation.
  1240. */
  1241. resources = ath10k_pci_hif_get_free_queue_number(ar, pipe);
  1242. /*
  1243. * If at least 50% of the total resources are still available,
  1244. * don't bother checking again yet.
  1245. */
  1246. if (resources > (host_ce_config_wlan[pipe].src_nentries >> 1))
  1247. return;
  1248. }
  1249. ath10k_ce_per_engine_service(ar, pipe);
  1250. }
  1251. static void ath10k_pci_rx_retry_sync(struct ath10k *ar)
  1252. {
  1253. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1254. del_timer_sync(&ar_pci->rx_post_retry);
  1255. }
  1256. int ath10k_pci_hif_map_service_to_pipe(struct ath10k *ar, u16 service_id,
  1257. u8 *ul_pipe, u8 *dl_pipe)
  1258. {
  1259. const struct service_to_pipe *entry;
  1260. bool ul_set = false, dl_set = false;
  1261. int i;
  1262. ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif map service\n");
  1263. for (i = 0; i < ARRAY_SIZE(target_service_to_ce_map_wlan); i++) {
  1264. entry = &target_service_to_ce_map_wlan[i];
  1265. if (__le32_to_cpu(entry->service_id) != service_id)
  1266. continue;
  1267. switch (__le32_to_cpu(entry->pipedir)) {
  1268. case PIPEDIR_NONE:
  1269. break;
  1270. case PIPEDIR_IN:
  1271. WARN_ON(dl_set);
  1272. *dl_pipe = __le32_to_cpu(entry->pipenum);
  1273. dl_set = true;
  1274. break;
  1275. case PIPEDIR_OUT:
  1276. WARN_ON(ul_set);
  1277. *ul_pipe = __le32_to_cpu(entry->pipenum);
  1278. ul_set = true;
  1279. break;
  1280. case PIPEDIR_INOUT:
  1281. WARN_ON(dl_set);
  1282. WARN_ON(ul_set);
  1283. *dl_pipe = __le32_to_cpu(entry->pipenum);
  1284. *ul_pipe = __le32_to_cpu(entry->pipenum);
  1285. dl_set = true;
  1286. ul_set = true;
  1287. break;
  1288. }
  1289. }
  1290. if (WARN_ON(!ul_set || !dl_set))
  1291. return -ENOENT;
  1292. return 0;
  1293. }
  1294. void ath10k_pci_hif_get_default_pipe(struct ath10k *ar,
  1295. u8 *ul_pipe, u8 *dl_pipe)
  1296. {
  1297. ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif get default pipe\n");
  1298. (void)ath10k_pci_hif_map_service_to_pipe(ar,
  1299. ATH10K_HTC_SVC_ID_RSVD_CTRL,
  1300. ul_pipe, dl_pipe);
  1301. }
  1302. void ath10k_pci_irq_msi_fw_mask(struct ath10k *ar)
  1303. {
  1304. u32 val;
  1305. switch (ar->hw_rev) {
  1306. case ATH10K_HW_QCA988X:
  1307. case ATH10K_HW_QCA9887:
  1308. case ATH10K_HW_QCA6174:
  1309. case ATH10K_HW_QCA9377:
  1310. val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
  1311. CORE_CTRL_ADDRESS);
  1312. val &= ~CORE_CTRL_PCIE_REG_31_MASK;
  1313. ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS +
  1314. CORE_CTRL_ADDRESS, val);
  1315. break;
  1316. case ATH10K_HW_QCA99X0:
  1317. case ATH10K_HW_QCA9984:
  1318. case ATH10K_HW_QCA9888:
  1319. case ATH10K_HW_QCA4019:
  1320. /* TODO: Find appropriate register configuration for QCA99X0
  1321. * to mask irq/MSI.
  1322. */
  1323. break;
  1324. case ATH10K_HW_WCN3990:
  1325. break;
  1326. }
  1327. }
  1328. static void ath10k_pci_irq_msi_fw_unmask(struct ath10k *ar)
  1329. {
  1330. u32 val;
  1331. switch (ar->hw_rev) {
  1332. case ATH10K_HW_QCA988X:
  1333. case ATH10K_HW_QCA9887:
  1334. case ATH10K_HW_QCA6174:
  1335. case ATH10K_HW_QCA9377:
  1336. val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
  1337. CORE_CTRL_ADDRESS);
  1338. val |= CORE_CTRL_PCIE_REG_31_MASK;
  1339. ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS +
  1340. CORE_CTRL_ADDRESS, val);
  1341. break;
  1342. case ATH10K_HW_QCA99X0:
  1343. case ATH10K_HW_QCA9984:
  1344. case ATH10K_HW_QCA9888:
  1345. case ATH10K_HW_QCA4019:
  1346. /* TODO: Find appropriate register configuration for QCA99X0
  1347. * to unmask irq/MSI.
  1348. */
  1349. break;
  1350. case ATH10K_HW_WCN3990:
  1351. break;
  1352. }
  1353. }
  1354. static void ath10k_pci_irq_disable(struct ath10k *ar)
  1355. {
  1356. ath10k_ce_disable_interrupts(ar);
  1357. ath10k_pci_disable_and_clear_legacy_irq(ar);
  1358. ath10k_pci_irq_msi_fw_mask(ar);
  1359. }
  1360. static void ath10k_pci_irq_sync(struct ath10k *ar)
  1361. {
  1362. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1363. synchronize_irq(ar_pci->pdev->irq);
  1364. }
  1365. static void ath10k_pci_irq_enable(struct ath10k *ar)
  1366. {
  1367. ath10k_ce_enable_interrupts(ar);
  1368. ath10k_pci_enable_legacy_irq(ar);
  1369. ath10k_pci_irq_msi_fw_unmask(ar);
  1370. }
  1371. static int ath10k_pci_hif_start(struct ath10k *ar)
  1372. {
  1373. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1374. ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif start\n");
  1375. napi_enable(&ar->napi);
  1376. ath10k_pci_irq_enable(ar);
  1377. ath10k_pci_rx_post(ar);
  1378. pcie_capability_write_word(ar_pci->pdev, PCI_EXP_LNKCTL,
  1379. ar_pci->link_ctl);
  1380. return 0;
  1381. }
  1382. static void ath10k_pci_rx_pipe_cleanup(struct ath10k_pci_pipe *pci_pipe)
  1383. {
  1384. struct ath10k *ar;
  1385. struct ath10k_ce_pipe *ce_pipe;
  1386. struct ath10k_ce_ring *ce_ring;
  1387. struct sk_buff *skb;
  1388. int i;
  1389. ar = pci_pipe->hif_ce_state;
  1390. ce_pipe = pci_pipe->ce_hdl;
  1391. ce_ring = ce_pipe->dest_ring;
  1392. if (!ce_ring)
  1393. return;
  1394. if (!pci_pipe->buf_sz)
  1395. return;
  1396. for (i = 0; i < ce_ring->nentries; i++) {
  1397. skb = ce_ring->per_transfer_context[i];
  1398. if (!skb)
  1399. continue;
  1400. ce_ring->per_transfer_context[i] = NULL;
  1401. dma_unmap_single(ar->dev, ATH10K_SKB_RXCB(skb)->paddr,
  1402. skb->len + skb_tailroom(skb),
  1403. DMA_FROM_DEVICE);
  1404. dev_kfree_skb_any(skb);
  1405. }
  1406. }
  1407. static void ath10k_pci_tx_pipe_cleanup(struct ath10k_pci_pipe *pci_pipe)
  1408. {
  1409. struct ath10k *ar;
  1410. struct ath10k_ce_pipe *ce_pipe;
  1411. struct ath10k_ce_ring *ce_ring;
  1412. struct sk_buff *skb;
  1413. int i;
  1414. ar = pci_pipe->hif_ce_state;
  1415. ce_pipe = pci_pipe->ce_hdl;
  1416. ce_ring = ce_pipe->src_ring;
  1417. if (!ce_ring)
  1418. return;
  1419. if (!pci_pipe->buf_sz)
  1420. return;
  1421. for (i = 0; i < ce_ring->nentries; i++) {
  1422. skb = ce_ring->per_transfer_context[i];
  1423. if (!skb)
  1424. continue;
  1425. ce_ring->per_transfer_context[i] = NULL;
  1426. ath10k_htc_tx_completion_handler(ar, skb);
  1427. }
  1428. }
  1429. /*
  1430. * Cleanup residual buffers for device shutdown:
  1431. * buffers that were enqueued for receive
  1432. * buffers that were to be sent
  1433. * Note: Buffers that had completed but which were
  1434. * not yet processed are on a completion queue. They
  1435. * are handled when the completion thread shuts down.
  1436. */
  1437. static void ath10k_pci_buffer_cleanup(struct ath10k *ar)
  1438. {
  1439. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1440. int pipe_num;
  1441. for (pipe_num = 0; pipe_num < CE_COUNT; pipe_num++) {
  1442. struct ath10k_pci_pipe *pipe_info;
  1443. pipe_info = &ar_pci->pipe_info[pipe_num];
  1444. ath10k_pci_rx_pipe_cleanup(pipe_info);
  1445. ath10k_pci_tx_pipe_cleanup(pipe_info);
  1446. }
  1447. }
  1448. void ath10k_pci_ce_deinit(struct ath10k *ar)
  1449. {
  1450. int i;
  1451. for (i = 0; i < CE_COUNT; i++)
  1452. ath10k_ce_deinit_pipe(ar, i);
  1453. }
  1454. void ath10k_pci_flush(struct ath10k *ar)
  1455. {
  1456. ath10k_pci_rx_retry_sync(ar);
  1457. ath10k_pci_buffer_cleanup(ar);
  1458. }
  1459. static void ath10k_pci_hif_stop(struct ath10k *ar)
  1460. {
  1461. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1462. unsigned long flags;
  1463. ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif stop\n");
  1464. /* Most likely the device has HTT Rx ring configured. The only way to
  1465. * prevent the device from accessing (and possible corrupting) host
  1466. * memory is to reset the chip now.
  1467. *
  1468. * There's also no known way of masking MSI interrupts on the device.
  1469. * For ranged MSI the CE-related interrupts can be masked. However
  1470. * regardless how many MSI interrupts are assigned the first one
  1471. * is always used for firmware indications (crashes) and cannot be
  1472. * masked. To prevent the device from asserting the interrupt reset it
  1473. * before proceeding with cleanup.
  1474. */
  1475. ath10k_pci_safe_chip_reset(ar);
  1476. ath10k_pci_irq_disable(ar);
  1477. ath10k_pci_irq_sync(ar);
  1478. ath10k_pci_flush(ar);
  1479. napi_synchronize(&ar->napi);
  1480. napi_disable(&ar->napi);
  1481. spin_lock_irqsave(&ar_pci->ps_lock, flags);
  1482. WARN_ON(ar_pci->ps_wake_refcount > 0);
  1483. spin_unlock_irqrestore(&ar_pci->ps_lock, flags);
  1484. }
  1485. int ath10k_pci_hif_exchange_bmi_msg(struct ath10k *ar,
  1486. void *req, u32 req_len,
  1487. void *resp, u32 *resp_len)
  1488. {
  1489. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1490. struct ath10k_pci_pipe *pci_tx = &ar_pci->pipe_info[BMI_CE_NUM_TO_TARG];
  1491. struct ath10k_pci_pipe *pci_rx = &ar_pci->pipe_info[BMI_CE_NUM_TO_HOST];
  1492. struct ath10k_ce_pipe *ce_tx = pci_tx->ce_hdl;
  1493. struct ath10k_ce_pipe *ce_rx = pci_rx->ce_hdl;
  1494. dma_addr_t req_paddr = 0;
  1495. dma_addr_t resp_paddr = 0;
  1496. struct bmi_xfer xfer = {};
  1497. void *treq, *tresp = NULL;
  1498. int ret = 0;
  1499. might_sleep();
  1500. if (resp && !resp_len)
  1501. return -EINVAL;
  1502. if (resp && resp_len && *resp_len == 0)
  1503. return -EINVAL;
  1504. treq = kmemdup(req, req_len, GFP_KERNEL);
  1505. if (!treq)
  1506. return -ENOMEM;
  1507. req_paddr = dma_map_single(ar->dev, treq, req_len, DMA_TO_DEVICE);
  1508. ret = dma_mapping_error(ar->dev, req_paddr);
  1509. if (ret) {
  1510. ret = -EIO;
  1511. goto err_dma;
  1512. }
  1513. if (resp && resp_len) {
  1514. tresp = kzalloc(*resp_len, GFP_KERNEL);
  1515. if (!tresp) {
  1516. ret = -ENOMEM;
  1517. goto err_req;
  1518. }
  1519. resp_paddr = dma_map_single(ar->dev, tresp, *resp_len,
  1520. DMA_FROM_DEVICE);
  1521. ret = dma_mapping_error(ar->dev, resp_paddr);
  1522. if (ret) {
  1523. ret = -EIO;
  1524. goto err_req;
  1525. }
  1526. xfer.wait_for_resp = true;
  1527. xfer.resp_len = 0;
  1528. ath10k_ce_rx_post_buf(ce_rx, &xfer, resp_paddr);
  1529. }
  1530. ret = ath10k_ce_send(ce_tx, &xfer, req_paddr, req_len, -1, 0);
  1531. if (ret)
  1532. goto err_resp;
  1533. ret = ath10k_pci_bmi_wait(ar, ce_tx, ce_rx, &xfer);
  1534. if (ret) {
  1535. u32 unused_buffer;
  1536. unsigned int unused_nbytes;
  1537. unsigned int unused_id;
  1538. ath10k_ce_cancel_send_next(ce_tx, NULL, &unused_buffer,
  1539. &unused_nbytes, &unused_id);
  1540. } else {
  1541. /* non-zero means we did not time out */
  1542. ret = 0;
  1543. }
  1544. err_resp:
  1545. if (resp) {
  1546. u32 unused_buffer;
  1547. ath10k_ce_revoke_recv_next(ce_rx, NULL, &unused_buffer);
  1548. dma_unmap_single(ar->dev, resp_paddr,
  1549. *resp_len, DMA_FROM_DEVICE);
  1550. }
  1551. err_req:
  1552. dma_unmap_single(ar->dev, req_paddr, req_len, DMA_TO_DEVICE);
  1553. if (ret == 0 && resp_len) {
  1554. *resp_len = min(*resp_len, xfer.resp_len);
  1555. memcpy(resp, tresp, xfer.resp_len);
  1556. }
  1557. err_dma:
  1558. kfree(treq);
  1559. kfree(tresp);
  1560. return ret;
  1561. }
  1562. static void ath10k_pci_bmi_send_done(struct ath10k_ce_pipe *ce_state)
  1563. {
  1564. struct bmi_xfer *xfer;
  1565. if (ath10k_ce_completed_send_next(ce_state, (void **)&xfer))
  1566. return;
  1567. xfer->tx_done = true;
  1568. }
  1569. static void ath10k_pci_bmi_recv_data(struct ath10k_ce_pipe *ce_state)
  1570. {
  1571. struct ath10k *ar = ce_state->ar;
  1572. struct bmi_xfer *xfer;
  1573. unsigned int nbytes;
  1574. if (ath10k_ce_completed_recv_next(ce_state, (void **)&xfer,
  1575. &nbytes))
  1576. return;
  1577. if (WARN_ON_ONCE(!xfer))
  1578. return;
  1579. if (!xfer->wait_for_resp) {
  1580. ath10k_warn(ar, "unexpected: BMI data received; ignoring\n");
  1581. return;
  1582. }
  1583. xfer->resp_len = nbytes;
  1584. xfer->rx_done = true;
  1585. }
  1586. static int ath10k_pci_bmi_wait(struct ath10k *ar,
  1587. struct ath10k_ce_pipe *tx_pipe,
  1588. struct ath10k_ce_pipe *rx_pipe,
  1589. struct bmi_xfer *xfer)
  1590. {
  1591. unsigned long timeout = jiffies + BMI_COMMUNICATION_TIMEOUT_HZ;
  1592. unsigned long started = jiffies;
  1593. unsigned long dur;
  1594. int ret;
  1595. while (time_before_eq(jiffies, timeout)) {
  1596. ath10k_pci_bmi_send_done(tx_pipe);
  1597. ath10k_pci_bmi_recv_data(rx_pipe);
  1598. if (xfer->tx_done && (xfer->rx_done == xfer->wait_for_resp)) {
  1599. ret = 0;
  1600. goto out;
  1601. }
  1602. schedule();
  1603. }
  1604. ret = -ETIMEDOUT;
  1605. out:
  1606. dur = jiffies - started;
  1607. if (dur > HZ)
  1608. ath10k_dbg(ar, ATH10K_DBG_BMI,
  1609. "bmi cmd took %lu jiffies hz %d ret %d\n",
  1610. dur, HZ, ret);
  1611. return ret;
  1612. }
  1613. /*
  1614. * Send an interrupt to the device to wake up the Target CPU
  1615. * so it has an opportunity to notice any changed state.
  1616. */
  1617. static int ath10k_pci_wake_target_cpu(struct ath10k *ar)
  1618. {
  1619. u32 addr, val;
  1620. addr = SOC_CORE_BASE_ADDRESS + CORE_CTRL_ADDRESS;
  1621. val = ath10k_pci_read32(ar, addr);
  1622. val |= CORE_CTRL_CPU_INTR_MASK;
  1623. ath10k_pci_write32(ar, addr, val);
  1624. return 0;
  1625. }
  1626. static int ath10k_pci_get_num_banks(struct ath10k *ar)
  1627. {
  1628. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1629. switch (ar_pci->pdev->device) {
  1630. case QCA988X_2_0_DEVICE_ID:
  1631. case QCA99X0_2_0_DEVICE_ID:
  1632. case QCA9888_2_0_DEVICE_ID:
  1633. case QCA9984_1_0_DEVICE_ID:
  1634. case QCA9887_1_0_DEVICE_ID:
  1635. return 1;
  1636. case QCA6164_2_1_DEVICE_ID:
  1637. case QCA6174_2_1_DEVICE_ID:
  1638. switch (MS(ar->chip_id, SOC_CHIP_ID_REV)) {
  1639. case QCA6174_HW_1_0_CHIP_ID_REV:
  1640. case QCA6174_HW_1_1_CHIP_ID_REV:
  1641. case QCA6174_HW_2_1_CHIP_ID_REV:
  1642. case QCA6174_HW_2_2_CHIP_ID_REV:
  1643. return 3;
  1644. case QCA6174_HW_1_3_CHIP_ID_REV:
  1645. return 2;
  1646. case QCA6174_HW_3_0_CHIP_ID_REV:
  1647. case QCA6174_HW_3_1_CHIP_ID_REV:
  1648. case QCA6174_HW_3_2_CHIP_ID_REV:
  1649. return 9;
  1650. }
  1651. break;
  1652. case QCA9377_1_0_DEVICE_ID:
  1653. return 4;
  1654. }
  1655. ath10k_warn(ar, "unknown number of banks, assuming 1\n");
  1656. return 1;
  1657. }
  1658. static int ath10k_bus_get_num_banks(struct ath10k *ar)
  1659. {
  1660. struct ath10k_ce *ce = ath10k_ce_priv(ar);
  1661. return ce->bus_ops->get_num_banks(ar);
  1662. }
  1663. int ath10k_pci_init_config(struct ath10k *ar)
  1664. {
  1665. u32 interconnect_targ_addr;
  1666. u32 pcie_state_targ_addr = 0;
  1667. u32 pipe_cfg_targ_addr = 0;
  1668. u32 svc_to_pipe_map = 0;
  1669. u32 pcie_config_flags = 0;
  1670. u32 ealloc_value;
  1671. u32 ealloc_targ_addr;
  1672. u32 flag2_value;
  1673. u32 flag2_targ_addr;
  1674. int ret = 0;
  1675. /* Download to Target the CE Config and the service-to-CE map */
  1676. interconnect_targ_addr =
  1677. host_interest_item_address(HI_ITEM(hi_interconnect_state));
  1678. /* Supply Target-side CE configuration */
  1679. ret = ath10k_pci_diag_read32(ar, interconnect_targ_addr,
  1680. &pcie_state_targ_addr);
  1681. if (ret != 0) {
  1682. ath10k_err(ar, "Failed to get pcie state addr: %d\n", ret);
  1683. return ret;
  1684. }
  1685. if (pcie_state_targ_addr == 0) {
  1686. ret = -EIO;
  1687. ath10k_err(ar, "Invalid pcie state addr\n");
  1688. return ret;
  1689. }
  1690. ret = ath10k_pci_diag_read32(ar, (pcie_state_targ_addr +
  1691. offsetof(struct pcie_state,
  1692. pipe_cfg_addr)),
  1693. &pipe_cfg_targ_addr);
  1694. if (ret != 0) {
  1695. ath10k_err(ar, "Failed to get pipe cfg addr: %d\n", ret);
  1696. return ret;
  1697. }
  1698. if (pipe_cfg_targ_addr == 0) {
  1699. ret = -EIO;
  1700. ath10k_err(ar, "Invalid pipe cfg addr\n");
  1701. return ret;
  1702. }
  1703. ret = ath10k_pci_diag_write_mem(ar, pipe_cfg_targ_addr,
  1704. target_ce_config_wlan,
  1705. sizeof(struct ce_pipe_config) *
  1706. NUM_TARGET_CE_CONFIG_WLAN);
  1707. if (ret != 0) {
  1708. ath10k_err(ar, "Failed to write pipe cfg: %d\n", ret);
  1709. return ret;
  1710. }
  1711. ret = ath10k_pci_diag_read32(ar, (pcie_state_targ_addr +
  1712. offsetof(struct pcie_state,
  1713. svc_to_pipe_map)),
  1714. &svc_to_pipe_map);
  1715. if (ret != 0) {
  1716. ath10k_err(ar, "Failed to get svc/pipe map: %d\n", ret);
  1717. return ret;
  1718. }
  1719. if (svc_to_pipe_map == 0) {
  1720. ret = -EIO;
  1721. ath10k_err(ar, "Invalid svc_to_pipe map\n");
  1722. return ret;
  1723. }
  1724. ret = ath10k_pci_diag_write_mem(ar, svc_to_pipe_map,
  1725. target_service_to_ce_map_wlan,
  1726. sizeof(target_service_to_ce_map_wlan));
  1727. if (ret != 0) {
  1728. ath10k_err(ar, "Failed to write svc/pipe map: %d\n", ret);
  1729. return ret;
  1730. }
  1731. ret = ath10k_pci_diag_read32(ar, (pcie_state_targ_addr +
  1732. offsetof(struct pcie_state,
  1733. config_flags)),
  1734. &pcie_config_flags);
  1735. if (ret != 0) {
  1736. ath10k_err(ar, "Failed to get pcie config_flags: %d\n", ret);
  1737. return ret;
  1738. }
  1739. pcie_config_flags &= ~PCIE_CONFIG_FLAG_ENABLE_L1;
  1740. ret = ath10k_pci_diag_write32(ar, (pcie_state_targ_addr +
  1741. offsetof(struct pcie_state,
  1742. config_flags)),
  1743. pcie_config_flags);
  1744. if (ret != 0) {
  1745. ath10k_err(ar, "Failed to write pcie config_flags: %d\n", ret);
  1746. return ret;
  1747. }
  1748. /* configure early allocation */
  1749. ealloc_targ_addr = host_interest_item_address(HI_ITEM(hi_early_alloc));
  1750. ret = ath10k_pci_diag_read32(ar, ealloc_targ_addr, &ealloc_value);
  1751. if (ret != 0) {
  1752. ath10k_err(ar, "Failed to get early alloc val: %d\n", ret);
  1753. return ret;
  1754. }
  1755. /* first bank is switched to IRAM */
  1756. ealloc_value |= ((HI_EARLY_ALLOC_MAGIC << HI_EARLY_ALLOC_MAGIC_SHIFT) &
  1757. HI_EARLY_ALLOC_MAGIC_MASK);
  1758. ealloc_value |= ((ath10k_bus_get_num_banks(ar) <<
  1759. HI_EARLY_ALLOC_IRAM_BANKS_SHIFT) &
  1760. HI_EARLY_ALLOC_IRAM_BANKS_MASK);
  1761. ret = ath10k_pci_diag_write32(ar, ealloc_targ_addr, ealloc_value);
  1762. if (ret != 0) {
  1763. ath10k_err(ar, "Failed to set early alloc val: %d\n", ret);
  1764. return ret;
  1765. }
  1766. /* Tell Target to proceed with initialization */
  1767. flag2_targ_addr = host_interest_item_address(HI_ITEM(hi_option_flag2));
  1768. ret = ath10k_pci_diag_read32(ar, flag2_targ_addr, &flag2_value);
  1769. if (ret != 0) {
  1770. ath10k_err(ar, "Failed to get option val: %d\n", ret);
  1771. return ret;
  1772. }
  1773. flag2_value |= HI_OPTION_EARLY_CFG_DONE;
  1774. ret = ath10k_pci_diag_write32(ar, flag2_targ_addr, flag2_value);
  1775. if (ret != 0) {
  1776. ath10k_err(ar, "Failed to set option val: %d\n", ret);
  1777. return ret;
  1778. }
  1779. return 0;
  1780. }
  1781. static void ath10k_pci_override_ce_config(struct ath10k *ar)
  1782. {
  1783. struct ce_attr *attr;
  1784. struct ce_pipe_config *config;
  1785. /* For QCA6174 we're overriding the Copy Engine 5 configuration,
  1786. * since it is currently used for other feature.
  1787. */
  1788. /* Override Host's Copy Engine 5 configuration */
  1789. attr = &host_ce_config_wlan[5];
  1790. attr->src_sz_max = 0;
  1791. attr->dest_nentries = 0;
  1792. /* Override Target firmware's Copy Engine configuration */
  1793. config = &target_ce_config_wlan[5];
  1794. config->pipedir = __cpu_to_le32(PIPEDIR_OUT);
  1795. config->nbytes_max = __cpu_to_le32(2048);
  1796. /* Map from service/endpoint to Copy Engine */
  1797. target_service_to_ce_map_wlan[15].pipenum = __cpu_to_le32(1);
  1798. }
  1799. int ath10k_pci_alloc_pipes(struct ath10k *ar)
  1800. {
  1801. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1802. struct ath10k_pci_pipe *pipe;
  1803. struct ath10k_ce *ce = ath10k_ce_priv(ar);
  1804. int i, ret;
  1805. for (i = 0; i < CE_COUNT; i++) {
  1806. pipe = &ar_pci->pipe_info[i];
  1807. pipe->ce_hdl = &ce->ce_states[i];
  1808. pipe->pipe_num = i;
  1809. pipe->hif_ce_state = ar;
  1810. ret = ath10k_ce_alloc_pipe(ar, i, &host_ce_config_wlan[i]);
  1811. if (ret) {
  1812. ath10k_err(ar, "failed to allocate copy engine pipe %d: %d\n",
  1813. i, ret);
  1814. return ret;
  1815. }
  1816. /* Last CE is Diagnostic Window */
  1817. if (i == CE_DIAG_PIPE) {
  1818. ar_pci->ce_diag = pipe->ce_hdl;
  1819. continue;
  1820. }
  1821. pipe->buf_sz = (size_t)(host_ce_config_wlan[i].src_sz_max);
  1822. }
  1823. return 0;
  1824. }
  1825. void ath10k_pci_free_pipes(struct ath10k *ar)
  1826. {
  1827. int i;
  1828. for (i = 0; i < CE_COUNT; i++)
  1829. ath10k_ce_free_pipe(ar, i);
  1830. }
  1831. int ath10k_pci_init_pipes(struct ath10k *ar)
  1832. {
  1833. int i, ret;
  1834. for (i = 0; i < CE_COUNT; i++) {
  1835. ret = ath10k_ce_init_pipe(ar, i, &host_ce_config_wlan[i]);
  1836. if (ret) {
  1837. ath10k_err(ar, "failed to initialize copy engine pipe %d: %d\n",
  1838. i, ret);
  1839. return ret;
  1840. }
  1841. }
  1842. return 0;
  1843. }
  1844. static bool ath10k_pci_has_fw_crashed(struct ath10k *ar)
  1845. {
  1846. return ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS) &
  1847. FW_IND_EVENT_PENDING;
  1848. }
  1849. static void ath10k_pci_fw_crashed_clear(struct ath10k *ar)
  1850. {
  1851. u32 val;
  1852. val = ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS);
  1853. val &= ~FW_IND_EVENT_PENDING;
  1854. ath10k_pci_write32(ar, FW_INDICATOR_ADDRESS, val);
  1855. }
  1856. static bool ath10k_pci_has_device_gone(struct ath10k *ar)
  1857. {
  1858. u32 val;
  1859. val = ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS);
  1860. return (val == 0xffffffff);
  1861. }
  1862. /* this function effectively clears target memory controller assert line */
  1863. static void ath10k_pci_warm_reset_si0(struct ath10k *ar)
  1864. {
  1865. u32 val;
  1866. val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
  1867. ath10k_pci_soc_write32(ar, SOC_RESET_CONTROL_ADDRESS,
  1868. val | SOC_RESET_CONTROL_SI0_RST_MASK);
  1869. val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
  1870. msleep(10);
  1871. val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
  1872. ath10k_pci_soc_write32(ar, SOC_RESET_CONTROL_ADDRESS,
  1873. val & ~SOC_RESET_CONTROL_SI0_RST_MASK);
  1874. val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
  1875. msleep(10);
  1876. }
  1877. static void ath10k_pci_warm_reset_cpu(struct ath10k *ar)
  1878. {
  1879. u32 val;
  1880. ath10k_pci_write32(ar, FW_INDICATOR_ADDRESS, 0);
  1881. val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
  1882. SOC_RESET_CONTROL_ADDRESS);
  1883. ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
  1884. val | SOC_RESET_CONTROL_CPU_WARM_RST_MASK);
  1885. }
  1886. static void ath10k_pci_warm_reset_ce(struct ath10k *ar)
  1887. {
  1888. u32 val;
  1889. val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
  1890. SOC_RESET_CONTROL_ADDRESS);
  1891. ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
  1892. val | SOC_RESET_CONTROL_CE_RST_MASK);
  1893. msleep(10);
  1894. ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
  1895. val & ~SOC_RESET_CONTROL_CE_RST_MASK);
  1896. }
  1897. static void ath10k_pci_warm_reset_clear_lf(struct ath10k *ar)
  1898. {
  1899. u32 val;
  1900. val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
  1901. SOC_LF_TIMER_CONTROL0_ADDRESS);
  1902. ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS +
  1903. SOC_LF_TIMER_CONTROL0_ADDRESS,
  1904. val & ~SOC_LF_TIMER_CONTROL0_ENABLE_MASK);
  1905. }
  1906. static int ath10k_pci_warm_reset(struct ath10k *ar)
  1907. {
  1908. int ret;
  1909. ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot warm reset\n");
  1910. spin_lock_bh(&ar->data_lock);
  1911. ar->stats.fw_warm_reset_counter++;
  1912. spin_unlock_bh(&ar->data_lock);
  1913. ath10k_pci_irq_disable(ar);
  1914. /* Make sure the target CPU is not doing anything dangerous, e.g. if it
  1915. * were to access copy engine while host performs copy engine reset
  1916. * then it is possible for the device to confuse pci-e controller to
  1917. * the point of bringing host system to a complete stop (i.e. hang).
  1918. */
  1919. ath10k_pci_warm_reset_si0(ar);
  1920. ath10k_pci_warm_reset_cpu(ar);
  1921. ath10k_pci_init_pipes(ar);
  1922. ath10k_pci_wait_for_target_init(ar);
  1923. ath10k_pci_warm_reset_clear_lf(ar);
  1924. ath10k_pci_warm_reset_ce(ar);
  1925. ath10k_pci_warm_reset_cpu(ar);
  1926. ath10k_pci_init_pipes(ar);
  1927. ret = ath10k_pci_wait_for_target_init(ar);
  1928. if (ret) {
  1929. ath10k_warn(ar, "failed to wait for target init: %d\n", ret);
  1930. return ret;
  1931. }
  1932. ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot warm reset complete\n");
  1933. return 0;
  1934. }
  1935. static int ath10k_pci_qca99x0_soft_chip_reset(struct ath10k *ar)
  1936. {
  1937. ath10k_pci_irq_disable(ar);
  1938. return ath10k_pci_qca99x0_chip_reset(ar);
  1939. }
  1940. static int ath10k_pci_safe_chip_reset(struct ath10k *ar)
  1941. {
  1942. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1943. if (!ar_pci->pci_soft_reset)
  1944. return -ENOTSUPP;
  1945. return ar_pci->pci_soft_reset(ar);
  1946. }
  1947. static int ath10k_pci_qca988x_chip_reset(struct ath10k *ar)
  1948. {
  1949. int i, ret;
  1950. u32 val;
  1951. ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot 988x chip reset\n");
  1952. /* Some hardware revisions (e.g. CUS223v2) has issues with cold reset.
  1953. * It is thus preferred to use warm reset which is safer but may not be
  1954. * able to recover the device from all possible fail scenarios.
  1955. *
  1956. * Warm reset doesn't always work on first try so attempt it a few
  1957. * times before giving up.
  1958. */
  1959. for (i = 0; i < ATH10K_PCI_NUM_WARM_RESET_ATTEMPTS; i++) {
  1960. ret = ath10k_pci_warm_reset(ar);
  1961. if (ret) {
  1962. ath10k_warn(ar, "failed to warm reset attempt %d of %d: %d\n",
  1963. i + 1, ATH10K_PCI_NUM_WARM_RESET_ATTEMPTS,
  1964. ret);
  1965. continue;
  1966. }
  1967. /* FIXME: Sometimes copy engine doesn't recover after warm
  1968. * reset. In most cases this needs cold reset. In some of these
  1969. * cases the device is in such a state that a cold reset may
  1970. * lock up the host.
  1971. *
  1972. * Reading any host interest register via copy engine is
  1973. * sufficient to verify if device is capable of booting
  1974. * firmware blob.
  1975. */
  1976. ret = ath10k_pci_init_pipes(ar);
  1977. if (ret) {
  1978. ath10k_warn(ar, "failed to init copy engine: %d\n",
  1979. ret);
  1980. continue;
  1981. }
  1982. ret = ath10k_pci_diag_read32(ar, QCA988X_HOST_INTEREST_ADDRESS,
  1983. &val);
  1984. if (ret) {
  1985. ath10k_warn(ar, "failed to poke copy engine: %d\n",
  1986. ret);
  1987. continue;
  1988. }
  1989. ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot chip reset complete (warm)\n");
  1990. return 0;
  1991. }
  1992. if (ath10k_pci_reset_mode == ATH10K_PCI_RESET_WARM_ONLY) {
  1993. ath10k_warn(ar, "refusing cold reset as requested\n");
  1994. return -EPERM;
  1995. }
  1996. ret = ath10k_pci_cold_reset(ar);
  1997. if (ret) {
  1998. ath10k_warn(ar, "failed to cold reset: %d\n", ret);
  1999. return ret;
  2000. }
  2001. ret = ath10k_pci_wait_for_target_init(ar);
  2002. if (ret) {
  2003. ath10k_warn(ar, "failed to wait for target after cold reset: %d\n",
  2004. ret);
  2005. return ret;
  2006. }
  2007. ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot qca988x chip reset complete (cold)\n");
  2008. return 0;
  2009. }
  2010. static int ath10k_pci_qca6174_chip_reset(struct ath10k *ar)
  2011. {
  2012. int ret;
  2013. ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot qca6174 chip reset\n");
  2014. /* FIXME: QCA6174 requires cold + warm reset to work. */
  2015. ret = ath10k_pci_cold_reset(ar);
  2016. if (ret) {
  2017. ath10k_warn(ar, "failed to cold reset: %d\n", ret);
  2018. return ret;
  2019. }
  2020. ret = ath10k_pci_wait_for_target_init(ar);
  2021. if (ret) {
  2022. ath10k_warn(ar, "failed to wait for target after cold reset: %d\n",
  2023. ret);
  2024. return ret;
  2025. }
  2026. ret = ath10k_pci_warm_reset(ar);
  2027. if (ret) {
  2028. ath10k_warn(ar, "failed to warm reset: %d\n", ret);
  2029. return ret;
  2030. }
  2031. ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot qca6174 chip reset complete (cold)\n");
  2032. return 0;
  2033. }
  2034. static int ath10k_pci_qca99x0_chip_reset(struct ath10k *ar)
  2035. {
  2036. int ret;
  2037. ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot qca99x0 chip reset\n");
  2038. ret = ath10k_pci_cold_reset(ar);
  2039. if (ret) {
  2040. ath10k_warn(ar, "failed to cold reset: %d\n", ret);
  2041. return ret;
  2042. }
  2043. ret = ath10k_pci_wait_for_target_init(ar);
  2044. if (ret) {
  2045. ath10k_warn(ar, "failed to wait for target after cold reset: %d\n",
  2046. ret);
  2047. return ret;
  2048. }
  2049. ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot qca99x0 chip reset complete (cold)\n");
  2050. return 0;
  2051. }
  2052. static int ath10k_pci_chip_reset(struct ath10k *ar)
  2053. {
  2054. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  2055. if (WARN_ON(!ar_pci->pci_hard_reset))
  2056. return -ENOTSUPP;
  2057. return ar_pci->pci_hard_reset(ar);
  2058. }
  2059. static int ath10k_pci_hif_power_up(struct ath10k *ar)
  2060. {
  2061. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  2062. int ret;
  2063. ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif power up\n");
  2064. pcie_capability_read_word(ar_pci->pdev, PCI_EXP_LNKCTL,
  2065. &ar_pci->link_ctl);
  2066. pcie_capability_write_word(ar_pci->pdev, PCI_EXP_LNKCTL,
  2067. ar_pci->link_ctl & ~PCI_EXP_LNKCTL_ASPMC);
  2068. /*
  2069. * Bring the target up cleanly.
  2070. *
  2071. * The target may be in an undefined state with an AUX-powered Target
  2072. * and a Host in WoW mode. If the Host crashes, loses power, or is
  2073. * restarted (without unloading the driver) then the Target is left
  2074. * (aux) powered and running. On a subsequent driver load, the Target
  2075. * is in an unexpected state. We try to catch that here in order to
  2076. * reset the Target and retry the probe.
  2077. */
  2078. ret = ath10k_pci_chip_reset(ar);
  2079. if (ret) {
  2080. if (ath10k_pci_has_fw_crashed(ar)) {
  2081. ath10k_warn(ar, "firmware crashed during chip reset\n");
  2082. ath10k_pci_fw_crashed_clear(ar);
  2083. ath10k_pci_fw_crashed_dump(ar);
  2084. }
  2085. ath10k_err(ar, "failed to reset chip: %d\n", ret);
  2086. goto err_sleep;
  2087. }
  2088. ret = ath10k_pci_init_pipes(ar);
  2089. if (ret) {
  2090. ath10k_err(ar, "failed to initialize CE: %d\n", ret);
  2091. goto err_sleep;
  2092. }
  2093. ret = ath10k_pci_init_config(ar);
  2094. if (ret) {
  2095. ath10k_err(ar, "failed to setup init config: %d\n", ret);
  2096. goto err_ce;
  2097. }
  2098. ret = ath10k_pci_wake_target_cpu(ar);
  2099. if (ret) {
  2100. ath10k_err(ar, "could not wake up target CPU: %d\n", ret);
  2101. goto err_ce;
  2102. }
  2103. return 0;
  2104. err_ce:
  2105. ath10k_pci_ce_deinit(ar);
  2106. err_sleep:
  2107. return ret;
  2108. }
  2109. void ath10k_pci_hif_power_down(struct ath10k *ar)
  2110. {
  2111. ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif power down\n");
  2112. /* Currently hif_power_up performs effectively a reset and hif_stop
  2113. * resets the chip as well so there's no point in resetting here.
  2114. */
  2115. }
  2116. static int ath10k_pci_hif_suspend(struct ath10k *ar)
  2117. {
  2118. /* Nothing to do; the important stuff is in the driver suspend. */
  2119. return 0;
  2120. }
  2121. static int ath10k_pci_suspend(struct ath10k *ar)
  2122. {
  2123. /* The grace timer can still be counting down and ar->ps_awake be true.
  2124. * It is known that the device may be asleep after resuming regardless
  2125. * of the SoC powersave state before suspending. Hence make sure the
  2126. * device is asleep before proceeding.
  2127. */
  2128. ath10k_pci_sleep_sync(ar);
  2129. return 0;
  2130. }
  2131. static int ath10k_pci_hif_resume(struct ath10k *ar)
  2132. {
  2133. /* Nothing to do; the important stuff is in the driver resume. */
  2134. return 0;
  2135. }
  2136. static int ath10k_pci_resume(struct ath10k *ar)
  2137. {
  2138. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  2139. struct pci_dev *pdev = ar_pci->pdev;
  2140. u32 val;
  2141. int ret = 0;
  2142. ret = ath10k_pci_force_wake(ar);
  2143. if (ret) {
  2144. ath10k_err(ar, "failed to wake up target: %d\n", ret);
  2145. return ret;
  2146. }
  2147. /* Suspend/Resume resets the PCI configuration space, so we have to
  2148. * re-disable the RETRY_TIMEOUT register (0x41) to keep PCI Tx retries
  2149. * from interfering with C3 CPU state. pci_restore_state won't help
  2150. * here since it only restores the first 64 bytes pci config header.
  2151. */
  2152. pci_read_config_dword(pdev, 0x40, &val);
  2153. if ((val & 0x0000ff00) != 0)
  2154. pci_write_config_dword(pdev, 0x40, val & 0xffff00ff);
  2155. return ret;
  2156. }
  2157. static bool ath10k_pci_validate_cal(void *data, size_t size)
  2158. {
  2159. __le16 *cal_words = data;
  2160. u16 checksum = 0;
  2161. size_t i;
  2162. if (size % 2 != 0)
  2163. return false;
  2164. for (i = 0; i < size / 2; i++)
  2165. checksum ^= le16_to_cpu(cal_words[i]);
  2166. return checksum == 0xffff;
  2167. }
  2168. static void ath10k_pci_enable_eeprom(struct ath10k *ar)
  2169. {
  2170. /* Enable SI clock */
  2171. ath10k_pci_soc_write32(ar, CLOCK_CONTROL_OFFSET, 0x0);
  2172. /* Configure GPIOs for I2C operation */
  2173. ath10k_pci_write32(ar,
  2174. GPIO_BASE_ADDRESS + GPIO_PIN0_OFFSET +
  2175. 4 * QCA9887_1_0_I2C_SDA_GPIO_PIN,
  2176. SM(QCA9887_1_0_I2C_SDA_PIN_CONFIG,
  2177. GPIO_PIN0_CONFIG) |
  2178. SM(1, GPIO_PIN0_PAD_PULL));
  2179. ath10k_pci_write32(ar,
  2180. GPIO_BASE_ADDRESS + GPIO_PIN0_OFFSET +
  2181. 4 * QCA9887_1_0_SI_CLK_GPIO_PIN,
  2182. SM(QCA9887_1_0_SI_CLK_PIN_CONFIG, GPIO_PIN0_CONFIG) |
  2183. SM(1, GPIO_PIN0_PAD_PULL));
  2184. ath10k_pci_write32(ar,
  2185. GPIO_BASE_ADDRESS +
  2186. QCA9887_1_0_GPIO_ENABLE_W1TS_LOW_ADDRESS,
  2187. 1u << QCA9887_1_0_SI_CLK_GPIO_PIN);
  2188. /* In Swift ASIC - EEPROM clock will be (110MHz/512) = 214KHz */
  2189. ath10k_pci_write32(ar,
  2190. SI_BASE_ADDRESS + SI_CONFIG_OFFSET,
  2191. SM(1, SI_CONFIG_ERR_INT) |
  2192. SM(1, SI_CONFIG_BIDIR_OD_DATA) |
  2193. SM(1, SI_CONFIG_I2C) |
  2194. SM(1, SI_CONFIG_POS_SAMPLE) |
  2195. SM(1, SI_CONFIG_INACTIVE_DATA) |
  2196. SM(1, SI_CONFIG_INACTIVE_CLK) |
  2197. SM(8, SI_CONFIG_DIVIDER));
  2198. }
  2199. static int ath10k_pci_read_eeprom(struct ath10k *ar, u16 addr, u8 *out)
  2200. {
  2201. u32 reg;
  2202. int wait_limit;
  2203. /* set device select byte and for the read operation */
  2204. reg = QCA9887_EEPROM_SELECT_READ |
  2205. SM(addr, QCA9887_EEPROM_ADDR_LO) |
  2206. SM(addr >> 8, QCA9887_EEPROM_ADDR_HI);
  2207. ath10k_pci_write32(ar, SI_BASE_ADDRESS + SI_TX_DATA0_OFFSET, reg);
  2208. /* write transmit data, transfer length, and START bit */
  2209. ath10k_pci_write32(ar, SI_BASE_ADDRESS + SI_CS_OFFSET,
  2210. SM(1, SI_CS_START) | SM(1, SI_CS_RX_CNT) |
  2211. SM(4, SI_CS_TX_CNT));
  2212. /* wait max 1 sec */
  2213. wait_limit = 100000;
  2214. /* wait for SI_CS_DONE_INT */
  2215. do {
  2216. reg = ath10k_pci_read32(ar, SI_BASE_ADDRESS + SI_CS_OFFSET);
  2217. if (MS(reg, SI_CS_DONE_INT))
  2218. break;
  2219. wait_limit--;
  2220. udelay(10);
  2221. } while (wait_limit > 0);
  2222. if (!MS(reg, SI_CS_DONE_INT)) {
  2223. ath10k_err(ar, "timeout while reading device EEPROM at %04x\n",
  2224. addr);
  2225. return -ETIMEDOUT;
  2226. }
  2227. /* clear SI_CS_DONE_INT */
  2228. ath10k_pci_write32(ar, SI_BASE_ADDRESS + SI_CS_OFFSET, reg);
  2229. if (MS(reg, SI_CS_DONE_ERR)) {
  2230. ath10k_err(ar, "failed to read device EEPROM at %04x\n", addr);
  2231. return -EIO;
  2232. }
  2233. /* extract receive data */
  2234. reg = ath10k_pci_read32(ar, SI_BASE_ADDRESS + SI_RX_DATA0_OFFSET);
  2235. *out = reg;
  2236. return 0;
  2237. }
  2238. static int ath10k_pci_hif_fetch_cal_eeprom(struct ath10k *ar, void **data,
  2239. size_t *data_len)
  2240. {
  2241. u8 *caldata = NULL;
  2242. size_t calsize, i;
  2243. int ret;
  2244. if (!QCA_REV_9887(ar))
  2245. return -EOPNOTSUPP;
  2246. calsize = ar->hw_params.cal_data_len;
  2247. caldata = kmalloc(calsize, GFP_KERNEL);
  2248. if (!caldata)
  2249. return -ENOMEM;
  2250. ath10k_pci_enable_eeprom(ar);
  2251. for (i = 0; i < calsize; i++) {
  2252. ret = ath10k_pci_read_eeprom(ar, i, &caldata[i]);
  2253. if (ret)
  2254. goto err_free;
  2255. }
  2256. if (!ath10k_pci_validate_cal(caldata, calsize))
  2257. goto err_free;
  2258. *data = caldata;
  2259. *data_len = calsize;
  2260. return 0;
  2261. err_free:
  2262. kfree(caldata);
  2263. return -EINVAL;
  2264. }
  2265. static const struct ath10k_hif_ops ath10k_pci_hif_ops = {
  2266. .tx_sg = ath10k_pci_hif_tx_sg,
  2267. .diag_read = ath10k_pci_hif_diag_read,
  2268. .diag_write = ath10k_pci_diag_write_mem,
  2269. .exchange_bmi_msg = ath10k_pci_hif_exchange_bmi_msg,
  2270. .start = ath10k_pci_hif_start,
  2271. .stop = ath10k_pci_hif_stop,
  2272. .map_service_to_pipe = ath10k_pci_hif_map_service_to_pipe,
  2273. .get_default_pipe = ath10k_pci_hif_get_default_pipe,
  2274. .send_complete_check = ath10k_pci_hif_send_complete_check,
  2275. .get_free_queue_number = ath10k_pci_hif_get_free_queue_number,
  2276. .power_up = ath10k_pci_hif_power_up,
  2277. .power_down = ath10k_pci_hif_power_down,
  2278. .read32 = ath10k_pci_read32,
  2279. .write32 = ath10k_pci_write32,
  2280. .suspend = ath10k_pci_hif_suspend,
  2281. .resume = ath10k_pci_hif_resume,
  2282. .fetch_cal_eeprom = ath10k_pci_hif_fetch_cal_eeprom,
  2283. };
  2284. /*
  2285. * Top-level interrupt handler for all PCI interrupts from a Target.
  2286. * When a block of MSI interrupts is allocated, this top-level handler
  2287. * is not used; instead, we directly call the correct sub-handler.
  2288. */
  2289. static irqreturn_t ath10k_pci_interrupt_handler(int irq, void *arg)
  2290. {
  2291. struct ath10k *ar = arg;
  2292. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  2293. int ret;
  2294. if (ath10k_pci_has_device_gone(ar))
  2295. return IRQ_NONE;
  2296. ret = ath10k_pci_force_wake(ar);
  2297. if (ret) {
  2298. ath10k_warn(ar, "failed to wake device up on irq: %d\n", ret);
  2299. return IRQ_NONE;
  2300. }
  2301. if ((ar_pci->oper_irq_mode == ATH10K_PCI_IRQ_LEGACY) &&
  2302. !ath10k_pci_irq_pending(ar))
  2303. return IRQ_NONE;
  2304. ath10k_pci_disable_and_clear_legacy_irq(ar);
  2305. ath10k_pci_irq_msi_fw_mask(ar);
  2306. napi_schedule(&ar->napi);
  2307. return IRQ_HANDLED;
  2308. }
  2309. static int ath10k_pci_napi_poll(struct napi_struct *ctx, int budget)
  2310. {
  2311. struct ath10k *ar = container_of(ctx, struct ath10k, napi);
  2312. int done = 0;
  2313. if (ath10k_pci_has_fw_crashed(ar)) {
  2314. ath10k_pci_fw_crashed_clear(ar);
  2315. ath10k_pci_fw_crashed_dump(ar);
  2316. napi_complete(ctx);
  2317. return done;
  2318. }
  2319. ath10k_ce_per_engine_service_any(ar);
  2320. done = ath10k_htt_txrx_compl_task(ar, budget);
  2321. if (done < budget) {
  2322. napi_complete_done(ctx, done);
  2323. /* In case of MSI, it is possible that interrupts are received
  2324. * while NAPI poll is inprogress. So pending interrupts that are
  2325. * received after processing all copy engine pipes by NAPI poll
  2326. * will not be handled again. This is causing failure to
  2327. * complete boot sequence in x86 platform. So before enabling
  2328. * interrupts safer to check for pending interrupts for
  2329. * immediate servicing.
  2330. */
  2331. if (ath10k_ce_interrupt_summary(ar)) {
  2332. napi_reschedule(ctx);
  2333. goto out;
  2334. }
  2335. ath10k_pci_enable_legacy_irq(ar);
  2336. ath10k_pci_irq_msi_fw_unmask(ar);
  2337. }
  2338. out:
  2339. return done;
  2340. }
  2341. static int ath10k_pci_request_irq_msi(struct ath10k *ar)
  2342. {
  2343. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  2344. int ret;
  2345. ret = request_irq(ar_pci->pdev->irq,
  2346. ath10k_pci_interrupt_handler,
  2347. IRQF_SHARED, "ath10k_pci", ar);
  2348. if (ret) {
  2349. ath10k_warn(ar, "failed to request MSI irq %d: %d\n",
  2350. ar_pci->pdev->irq, ret);
  2351. return ret;
  2352. }
  2353. return 0;
  2354. }
  2355. static int ath10k_pci_request_irq_legacy(struct ath10k *ar)
  2356. {
  2357. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  2358. int ret;
  2359. ret = request_irq(ar_pci->pdev->irq,
  2360. ath10k_pci_interrupt_handler,
  2361. IRQF_SHARED, "ath10k_pci", ar);
  2362. if (ret) {
  2363. ath10k_warn(ar, "failed to request legacy irq %d: %d\n",
  2364. ar_pci->pdev->irq, ret);
  2365. return ret;
  2366. }
  2367. return 0;
  2368. }
  2369. static int ath10k_pci_request_irq(struct ath10k *ar)
  2370. {
  2371. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  2372. switch (ar_pci->oper_irq_mode) {
  2373. case ATH10K_PCI_IRQ_LEGACY:
  2374. return ath10k_pci_request_irq_legacy(ar);
  2375. case ATH10K_PCI_IRQ_MSI:
  2376. return ath10k_pci_request_irq_msi(ar);
  2377. default:
  2378. return -EINVAL;
  2379. }
  2380. }
  2381. static void ath10k_pci_free_irq(struct ath10k *ar)
  2382. {
  2383. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  2384. free_irq(ar_pci->pdev->irq, ar);
  2385. }
  2386. void ath10k_pci_init_napi(struct ath10k *ar)
  2387. {
  2388. netif_napi_add(&ar->napi_dev, &ar->napi, ath10k_pci_napi_poll,
  2389. ATH10K_NAPI_BUDGET);
  2390. }
  2391. static int ath10k_pci_init_irq(struct ath10k *ar)
  2392. {
  2393. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  2394. int ret;
  2395. ath10k_pci_init_napi(ar);
  2396. if (ath10k_pci_irq_mode != ATH10K_PCI_IRQ_AUTO)
  2397. ath10k_info(ar, "limiting irq mode to: %d\n",
  2398. ath10k_pci_irq_mode);
  2399. /* Try MSI */
  2400. if (ath10k_pci_irq_mode != ATH10K_PCI_IRQ_LEGACY) {
  2401. ar_pci->oper_irq_mode = ATH10K_PCI_IRQ_MSI;
  2402. ret = pci_enable_msi(ar_pci->pdev);
  2403. if (ret == 0)
  2404. return 0;
  2405. /* fall-through */
  2406. }
  2407. /* Try legacy irq
  2408. *
  2409. * A potential race occurs here: The CORE_BASE write
  2410. * depends on target correctly decoding AXI address but
  2411. * host won't know when target writes BAR to CORE_CTRL.
  2412. * This write might get lost if target has NOT written BAR.
  2413. * For now, fix the race by repeating the write in below
  2414. * synchronization checking.
  2415. */
  2416. ar_pci->oper_irq_mode = ATH10K_PCI_IRQ_LEGACY;
  2417. ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
  2418. PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL);
  2419. return 0;
  2420. }
  2421. static void ath10k_pci_deinit_irq_legacy(struct ath10k *ar)
  2422. {
  2423. ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
  2424. 0);
  2425. }
  2426. static int ath10k_pci_deinit_irq(struct ath10k *ar)
  2427. {
  2428. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  2429. switch (ar_pci->oper_irq_mode) {
  2430. case ATH10K_PCI_IRQ_LEGACY:
  2431. ath10k_pci_deinit_irq_legacy(ar);
  2432. break;
  2433. default:
  2434. pci_disable_msi(ar_pci->pdev);
  2435. break;
  2436. }
  2437. return 0;
  2438. }
  2439. int ath10k_pci_wait_for_target_init(struct ath10k *ar)
  2440. {
  2441. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  2442. unsigned long timeout;
  2443. u32 val;
  2444. ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot waiting target to initialise\n");
  2445. timeout = jiffies + msecs_to_jiffies(ATH10K_PCI_TARGET_WAIT);
  2446. do {
  2447. val = ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS);
  2448. ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot target indicator %x\n",
  2449. val);
  2450. /* target should never return this */
  2451. if (val == 0xffffffff)
  2452. continue;
  2453. /* the device has crashed so don't bother trying anymore */
  2454. if (val & FW_IND_EVENT_PENDING)
  2455. break;
  2456. if (val & FW_IND_INITIALIZED)
  2457. break;
  2458. if (ar_pci->oper_irq_mode == ATH10K_PCI_IRQ_LEGACY)
  2459. /* Fix potential race by repeating CORE_BASE writes */
  2460. ath10k_pci_enable_legacy_irq(ar);
  2461. mdelay(10);
  2462. } while (time_before(jiffies, timeout));
  2463. ath10k_pci_disable_and_clear_legacy_irq(ar);
  2464. ath10k_pci_irq_msi_fw_mask(ar);
  2465. if (val == 0xffffffff) {
  2466. ath10k_err(ar, "failed to read device register, device is gone\n");
  2467. return -EIO;
  2468. }
  2469. if (val & FW_IND_EVENT_PENDING) {
  2470. ath10k_warn(ar, "device has crashed during init\n");
  2471. return -ECOMM;
  2472. }
  2473. if (!(val & FW_IND_INITIALIZED)) {
  2474. ath10k_err(ar, "failed to receive initialized event from target: %08x\n",
  2475. val);
  2476. return -ETIMEDOUT;
  2477. }
  2478. ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot target initialised\n");
  2479. return 0;
  2480. }
  2481. static int ath10k_pci_cold_reset(struct ath10k *ar)
  2482. {
  2483. u32 val;
  2484. ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot cold reset\n");
  2485. spin_lock_bh(&ar->data_lock);
  2486. ar->stats.fw_cold_reset_counter++;
  2487. spin_unlock_bh(&ar->data_lock);
  2488. /* Put Target, including PCIe, into RESET. */
  2489. val = ath10k_pci_reg_read32(ar, SOC_GLOBAL_RESET_ADDRESS);
  2490. val |= 1;
  2491. ath10k_pci_reg_write32(ar, SOC_GLOBAL_RESET_ADDRESS, val);
  2492. /* After writing into SOC_GLOBAL_RESET to put device into
  2493. * reset and pulling out of reset pcie may not be stable
  2494. * for any immediate pcie register access and cause bus error,
  2495. * add delay before any pcie access request to fix this issue.
  2496. */
  2497. msleep(20);
  2498. /* Pull Target, including PCIe, out of RESET. */
  2499. val &= ~1;
  2500. ath10k_pci_reg_write32(ar, SOC_GLOBAL_RESET_ADDRESS, val);
  2501. msleep(20);
  2502. ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot cold reset complete\n");
  2503. return 0;
  2504. }
  2505. static int ath10k_pci_claim(struct ath10k *ar)
  2506. {
  2507. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  2508. struct pci_dev *pdev = ar_pci->pdev;
  2509. int ret;
  2510. pci_set_drvdata(pdev, ar);
  2511. ret = pci_enable_device(pdev);
  2512. if (ret) {
  2513. ath10k_err(ar, "failed to enable pci device: %d\n", ret);
  2514. return ret;
  2515. }
  2516. ret = pci_request_region(pdev, BAR_NUM, "ath");
  2517. if (ret) {
  2518. ath10k_err(ar, "failed to request region BAR%d: %d\n", BAR_NUM,
  2519. ret);
  2520. goto err_device;
  2521. }
  2522. /* Target expects 32 bit DMA. Enforce it. */
  2523. ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  2524. if (ret) {
  2525. ath10k_err(ar, "failed to set dma mask to 32-bit: %d\n", ret);
  2526. goto err_region;
  2527. }
  2528. ret = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  2529. if (ret) {
  2530. ath10k_err(ar, "failed to set consistent dma mask to 32-bit: %d\n",
  2531. ret);
  2532. goto err_region;
  2533. }
  2534. pci_set_master(pdev);
  2535. /* Arrange for access to Target SoC registers. */
  2536. ar_pci->mem_len = pci_resource_len(pdev, BAR_NUM);
  2537. ar_pci->mem = pci_iomap(pdev, BAR_NUM, 0);
  2538. if (!ar_pci->mem) {
  2539. ath10k_err(ar, "failed to iomap BAR%d\n", BAR_NUM);
  2540. ret = -EIO;
  2541. goto err_master;
  2542. }
  2543. ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot pci_mem 0x%pK\n", ar_pci->mem);
  2544. return 0;
  2545. err_master:
  2546. pci_clear_master(pdev);
  2547. err_region:
  2548. pci_release_region(pdev, BAR_NUM);
  2549. err_device:
  2550. pci_disable_device(pdev);
  2551. return ret;
  2552. }
  2553. static void ath10k_pci_release(struct ath10k *ar)
  2554. {
  2555. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  2556. struct pci_dev *pdev = ar_pci->pdev;
  2557. pci_iounmap(pdev, ar_pci->mem);
  2558. pci_release_region(pdev, BAR_NUM);
  2559. pci_clear_master(pdev);
  2560. pci_disable_device(pdev);
  2561. }
  2562. static bool ath10k_pci_chip_is_supported(u32 dev_id, u32 chip_id)
  2563. {
  2564. const struct ath10k_pci_supp_chip *supp_chip;
  2565. int i;
  2566. u32 rev_id = MS(chip_id, SOC_CHIP_ID_REV);
  2567. for (i = 0; i < ARRAY_SIZE(ath10k_pci_supp_chips); i++) {
  2568. supp_chip = &ath10k_pci_supp_chips[i];
  2569. if (supp_chip->dev_id == dev_id &&
  2570. supp_chip->rev_id == rev_id)
  2571. return true;
  2572. }
  2573. return false;
  2574. }
  2575. int ath10k_pci_setup_resource(struct ath10k *ar)
  2576. {
  2577. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  2578. struct ath10k_ce *ce = ath10k_ce_priv(ar);
  2579. int ret;
  2580. spin_lock_init(&ce->ce_lock);
  2581. spin_lock_init(&ar_pci->ps_lock);
  2582. timer_setup(&ar_pci->rx_post_retry, ath10k_pci_rx_replenish_retry, 0);
  2583. if (QCA_REV_6174(ar) || QCA_REV_9377(ar))
  2584. ath10k_pci_override_ce_config(ar);
  2585. ret = ath10k_pci_alloc_pipes(ar);
  2586. if (ret) {
  2587. ath10k_err(ar, "failed to allocate copy engine pipes: %d\n",
  2588. ret);
  2589. return ret;
  2590. }
  2591. return 0;
  2592. }
  2593. void ath10k_pci_release_resource(struct ath10k *ar)
  2594. {
  2595. ath10k_pci_rx_retry_sync(ar);
  2596. netif_napi_del(&ar->napi);
  2597. ath10k_pci_ce_deinit(ar);
  2598. ath10k_pci_free_pipes(ar);
  2599. }
  2600. static const struct ath10k_bus_ops ath10k_pci_bus_ops = {
  2601. .read32 = ath10k_bus_pci_read32,
  2602. .write32 = ath10k_bus_pci_write32,
  2603. .get_num_banks = ath10k_pci_get_num_banks,
  2604. };
  2605. static int ath10k_pci_probe(struct pci_dev *pdev,
  2606. const struct pci_device_id *pci_dev)
  2607. {
  2608. int ret = 0;
  2609. struct ath10k *ar;
  2610. struct ath10k_pci *ar_pci;
  2611. enum ath10k_hw_rev hw_rev;
  2612. u32 chip_id;
  2613. bool pci_ps;
  2614. int (*pci_soft_reset)(struct ath10k *ar);
  2615. int (*pci_hard_reset)(struct ath10k *ar);
  2616. u32 (*targ_cpu_to_ce_addr)(struct ath10k *ar, u32 addr);
  2617. switch (pci_dev->device) {
  2618. case QCA988X_2_0_DEVICE_ID:
  2619. hw_rev = ATH10K_HW_QCA988X;
  2620. pci_ps = false;
  2621. pci_soft_reset = ath10k_pci_warm_reset;
  2622. pci_hard_reset = ath10k_pci_qca988x_chip_reset;
  2623. targ_cpu_to_ce_addr = ath10k_pci_qca988x_targ_cpu_to_ce_addr;
  2624. break;
  2625. case QCA9887_1_0_DEVICE_ID:
  2626. hw_rev = ATH10K_HW_QCA9887;
  2627. pci_ps = false;
  2628. pci_soft_reset = ath10k_pci_warm_reset;
  2629. pci_hard_reset = ath10k_pci_qca988x_chip_reset;
  2630. targ_cpu_to_ce_addr = ath10k_pci_qca988x_targ_cpu_to_ce_addr;
  2631. break;
  2632. case QCA6164_2_1_DEVICE_ID:
  2633. case QCA6174_2_1_DEVICE_ID:
  2634. hw_rev = ATH10K_HW_QCA6174;
  2635. pci_ps = true;
  2636. pci_soft_reset = ath10k_pci_warm_reset;
  2637. pci_hard_reset = ath10k_pci_qca6174_chip_reset;
  2638. targ_cpu_to_ce_addr = ath10k_pci_qca988x_targ_cpu_to_ce_addr;
  2639. break;
  2640. case QCA99X0_2_0_DEVICE_ID:
  2641. hw_rev = ATH10K_HW_QCA99X0;
  2642. pci_ps = false;
  2643. pci_soft_reset = ath10k_pci_qca99x0_soft_chip_reset;
  2644. pci_hard_reset = ath10k_pci_qca99x0_chip_reset;
  2645. targ_cpu_to_ce_addr = ath10k_pci_qca99x0_targ_cpu_to_ce_addr;
  2646. break;
  2647. case QCA9984_1_0_DEVICE_ID:
  2648. hw_rev = ATH10K_HW_QCA9984;
  2649. pci_ps = false;
  2650. pci_soft_reset = ath10k_pci_qca99x0_soft_chip_reset;
  2651. pci_hard_reset = ath10k_pci_qca99x0_chip_reset;
  2652. targ_cpu_to_ce_addr = ath10k_pci_qca99x0_targ_cpu_to_ce_addr;
  2653. break;
  2654. case QCA9888_2_0_DEVICE_ID:
  2655. hw_rev = ATH10K_HW_QCA9888;
  2656. pci_ps = false;
  2657. pci_soft_reset = ath10k_pci_qca99x0_soft_chip_reset;
  2658. pci_hard_reset = ath10k_pci_qca99x0_chip_reset;
  2659. targ_cpu_to_ce_addr = ath10k_pci_qca99x0_targ_cpu_to_ce_addr;
  2660. break;
  2661. case QCA9377_1_0_DEVICE_ID:
  2662. hw_rev = ATH10K_HW_QCA9377;
  2663. pci_ps = true;
  2664. pci_soft_reset = NULL;
  2665. pci_hard_reset = ath10k_pci_qca6174_chip_reset;
  2666. targ_cpu_to_ce_addr = ath10k_pci_qca988x_targ_cpu_to_ce_addr;
  2667. break;
  2668. default:
  2669. WARN_ON(1);
  2670. return -ENOTSUPP;
  2671. }
  2672. ar = ath10k_core_create(sizeof(*ar_pci), &pdev->dev, ATH10K_BUS_PCI,
  2673. hw_rev, &ath10k_pci_hif_ops);
  2674. if (!ar) {
  2675. dev_err(&pdev->dev, "failed to allocate core\n");
  2676. return -ENOMEM;
  2677. }
  2678. ath10k_dbg(ar, ATH10K_DBG_BOOT, "pci probe %04x:%04x %04x:%04x\n",
  2679. pdev->vendor, pdev->device,
  2680. pdev->subsystem_vendor, pdev->subsystem_device);
  2681. ar_pci = ath10k_pci_priv(ar);
  2682. ar_pci->pdev = pdev;
  2683. ar_pci->dev = &pdev->dev;
  2684. ar_pci->ar = ar;
  2685. ar->dev_id = pci_dev->device;
  2686. ar_pci->pci_ps = pci_ps;
  2687. ar_pci->ce.bus_ops = &ath10k_pci_bus_ops;
  2688. ar_pci->pci_soft_reset = pci_soft_reset;
  2689. ar_pci->pci_hard_reset = pci_hard_reset;
  2690. ar_pci->targ_cpu_to_ce_addr = targ_cpu_to_ce_addr;
  2691. ar->ce_priv = &ar_pci->ce;
  2692. ar->id.vendor = pdev->vendor;
  2693. ar->id.device = pdev->device;
  2694. ar->id.subsystem_vendor = pdev->subsystem_vendor;
  2695. ar->id.subsystem_device = pdev->subsystem_device;
  2696. timer_setup(&ar_pci->ps_timer, ath10k_pci_ps_timer, 0);
  2697. ret = ath10k_pci_setup_resource(ar);
  2698. if (ret) {
  2699. ath10k_err(ar, "failed to setup resource: %d\n", ret);
  2700. goto err_core_destroy;
  2701. }
  2702. ret = ath10k_pci_claim(ar);
  2703. if (ret) {
  2704. ath10k_err(ar, "failed to claim device: %d\n", ret);
  2705. goto err_free_pipes;
  2706. }
  2707. ret = ath10k_pci_force_wake(ar);
  2708. if (ret) {
  2709. ath10k_warn(ar, "failed to wake up device : %d\n", ret);
  2710. goto err_sleep;
  2711. }
  2712. ath10k_pci_ce_deinit(ar);
  2713. ath10k_pci_irq_disable(ar);
  2714. ret = ath10k_pci_init_irq(ar);
  2715. if (ret) {
  2716. ath10k_err(ar, "failed to init irqs: %d\n", ret);
  2717. goto err_sleep;
  2718. }
  2719. ath10k_info(ar, "pci irq %s oper_irq_mode %d irq_mode %d reset_mode %d\n",
  2720. ath10k_pci_get_irq_method(ar), ar_pci->oper_irq_mode,
  2721. ath10k_pci_irq_mode, ath10k_pci_reset_mode);
  2722. ret = ath10k_pci_request_irq(ar);
  2723. if (ret) {
  2724. ath10k_warn(ar, "failed to request irqs: %d\n", ret);
  2725. goto err_deinit_irq;
  2726. }
  2727. ret = ath10k_pci_chip_reset(ar);
  2728. if (ret) {
  2729. ath10k_err(ar, "failed to reset chip: %d\n", ret);
  2730. goto err_free_irq;
  2731. }
  2732. chip_id = ath10k_pci_soc_read32(ar, SOC_CHIP_ID_ADDRESS);
  2733. if (chip_id == 0xffffffff) {
  2734. ath10k_err(ar, "failed to get chip id\n");
  2735. goto err_free_irq;
  2736. }
  2737. if (!ath10k_pci_chip_is_supported(pdev->device, chip_id)) {
  2738. ath10k_err(ar, "device %04x with chip_id %08x isn't supported\n",
  2739. pdev->device, chip_id);
  2740. goto err_free_irq;
  2741. }
  2742. ret = ath10k_core_register(ar, chip_id);
  2743. if (ret) {
  2744. ath10k_err(ar, "failed to register driver core: %d\n", ret);
  2745. goto err_free_irq;
  2746. }
  2747. return 0;
  2748. err_free_irq:
  2749. ath10k_pci_free_irq(ar);
  2750. ath10k_pci_rx_retry_sync(ar);
  2751. err_deinit_irq:
  2752. ath10k_pci_deinit_irq(ar);
  2753. err_sleep:
  2754. ath10k_pci_sleep_sync(ar);
  2755. ath10k_pci_release(ar);
  2756. err_free_pipes:
  2757. ath10k_pci_free_pipes(ar);
  2758. err_core_destroy:
  2759. ath10k_core_destroy(ar);
  2760. return ret;
  2761. }
  2762. static void ath10k_pci_remove(struct pci_dev *pdev)
  2763. {
  2764. struct ath10k *ar = pci_get_drvdata(pdev);
  2765. struct ath10k_pci *ar_pci;
  2766. ath10k_dbg(ar, ATH10K_DBG_PCI, "pci remove\n");
  2767. if (!ar)
  2768. return;
  2769. ar_pci = ath10k_pci_priv(ar);
  2770. if (!ar_pci)
  2771. return;
  2772. ath10k_core_unregister(ar);
  2773. ath10k_pci_free_irq(ar);
  2774. ath10k_pci_deinit_irq(ar);
  2775. ath10k_pci_release_resource(ar);
  2776. ath10k_pci_sleep_sync(ar);
  2777. ath10k_pci_release(ar);
  2778. ath10k_core_destroy(ar);
  2779. }
  2780. MODULE_DEVICE_TABLE(pci, ath10k_pci_id_table);
  2781. static __maybe_unused int ath10k_pci_pm_suspend(struct device *dev)
  2782. {
  2783. struct ath10k *ar = dev_get_drvdata(dev);
  2784. int ret;
  2785. ret = ath10k_pci_suspend(ar);
  2786. if (ret)
  2787. ath10k_warn(ar, "failed to suspend hif: %d\n", ret);
  2788. return ret;
  2789. }
  2790. static __maybe_unused int ath10k_pci_pm_resume(struct device *dev)
  2791. {
  2792. struct ath10k *ar = dev_get_drvdata(dev);
  2793. int ret;
  2794. ret = ath10k_pci_resume(ar);
  2795. if (ret)
  2796. ath10k_warn(ar, "failed to resume hif: %d\n", ret);
  2797. return ret;
  2798. }
  2799. static SIMPLE_DEV_PM_OPS(ath10k_pci_pm_ops,
  2800. ath10k_pci_pm_suspend,
  2801. ath10k_pci_pm_resume);
  2802. static struct pci_driver ath10k_pci_driver = {
  2803. .name = "ath10k_pci",
  2804. .id_table = ath10k_pci_id_table,
  2805. .probe = ath10k_pci_probe,
  2806. .remove = ath10k_pci_remove,
  2807. #ifdef CONFIG_PM
  2808. .driver.pm = &ath10k_pci_pm_ops,
  2809. #endif
  2810. };
  2811. static int __init ath10k_pci_init(void)
  2812. {
  2813. int ret;
  2814. ret = pci_register_driver(&ath10k_pci_driver);
  2815. if (ret)
  2816. printk(KERN_ERR "failed to register ath10k pci driver: %d\n",
  2817. ret);
  2818. ret = ath10k_ahb_init();
  2819. if (ret)
  2820. printk(KERN_ERR "ahb init failed: %d\n", ret);
  2821. return ret;
  2822. }
  2823. module_init(ath10k_pci_init);
  2824. static void __exit ath10k_pci_exit(void)
  2825. {
  2826. pci_unregister_driver(&ath10k_pci_driver);
  2827. ath10k_ahb_exit();
  2828. }
  2829. module_exit(ath10k_pci_exit);
  2830. MODULE_AUTHOR("Qualcomm Atheros");
  2831. MODULE_DESCRIPTION("Driver support for Qualcomm Atheros 802.11ac WLAN PCIe/AHB devices");
  2832. MODULE_LICENSE("Dual BSD/GPL");
  2833. /* QCA988x 2.0 firmware files */
  2834. MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" ATH10K_FW_API2_FILE);
  2835. MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" ATH10K_FW_API3_FILE);
  2836. MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" ATH10K_FW_API4_FILE);
  2837. MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" ATH10K_FW_API5_FILE);
  2838. MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" QCA988X_HW_2_0_BOARD_DATA_FILE);
  2839. MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" ATH10K_BOARD_API2_FILE);
  2840. /* QCA9887 1.0 firmware files */
  2841. MODULE_FIRMWARE(QCA9887_HW_1_0_FW_DIR "/" ATH10K_FW_API5_FILE);
  2842. MODULE_FIRMWARE(QCA9887_HW_1_0_FW_DIR "/" QCA9887_HW_1_0_BOARD_DATA_FILE);
  2843. MODULE_FIRMWARE(QCA9887_HW_1_0_FW_DIR "/" ATH10K_BOARD_API2_FILE);
  2844. /* QCA6174 2.1 firmware files */
  2845. MODULE_FIRMWARE(QCA6174_HW_2_1_FW_DIR "/" ATH10K_FW_API4_FILE);
  2846. MODULE_FIRMWARE(QCA6174_HW_2_1_FW_DIR "/" ATH10K_FW_API5_FILE);
  2847. MODULE_FIRMWARE(QCA6174_HW_2_1_FW_DIR "/" QCA6174_HW_2_1_BOARD_DATA_FILE);
  2848. MODULE_FIRMWARE(QCA6174_HW_2_1_FW_DIR "/" ATH10K_BOARD_API2_FILE);
  2849. /* QCA6174 3.1 firmware files */
  2850. MODULE_FIRMWARE(QCA6174_HW_3_0_FW_DIR "/" ATH10K_FW_API4_FILE);
  2851. MODULE_FIRMWARE(QCA6174_HW_3_0_FW_DIR "/" ATH10K_FW_API5_FILE);
  2852. MODULE_FIRMWARE(QCA6174_HW_3_0_FW_DIR "/" ATH10K_FW_API6_FILE);
  2853. MODULE_FIRMWARE(QCA6174_HW_3_0_FW_DIR "/" QCA6174_HW_3_0_BOARD_DATA_FILE);
  2854. MODULE_FIRMWARE(QCA6174_HW_3_0_FW_DIR "/" ATH10K_BOARD_API2_FILE);
  2855. /* QCA9377 1.0 firmware files */
  2856. MODULE_FIRMWARE(QCA9377_HW_1_0_FW_DIR "/" ATH10K_FW_API5_FILE);
  2857. MODULE_FIRMWARE(QCA9377_HW_1_0_FW_DIR "/" QCA9377_HW_1_0_BOARD_DATA_FILE);