pci.c 94 KB

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