pci.c 94 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798
  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, alloc_nbytes, remaining_bytes;
  874. struct ath10k_ce_pipe *ce_diag;
  875. void *data_buf = NULL;
  876. dma_addr_t ce_data_base = 0;
  877. int i;
  878. spin_lock_bh(&ce->ce_lock);
  879. ce_diag = ar_pci->ce_diag;
  880. /*
  881. * Allocate a temporary bounce buffer to hold caller's data
  882. * to be DMA'ed to Target. This guarantees
  883. * 1) 4-byte alignment
  884. * 2) Buffer in DMA-able space
  885. */
  886. alloc_nbytes = min_t(unsigned int, nbytes, DIAG_TRANSFER_LIMIT);
  887. data_buf = (unsigned char *)dma_alloc_coherent(ar->dev,
  888. alloc_nbytes,
  889. &ce_data_base,
  890. GFP_ATOMIC);
  891. if (!data_buf) {
  892. ret = -ENOMEM;
  893. goto done;
  894. }
  895. /*
  896. * The address supplied by the caller is in the
  897. * Target CPU virtual address space.
  898. *
  899. * In order to use this address with the diagnostic CE,
  900. * convert it from
  901. * Target CPU virtual address space
  902. * to
  903. * CE address space
  904. */
  905. address = ath10k_pci_targ_cpu_to_ce_addr(ar, address);
  906. remaining_bytes = nbytes;
  907. while (remaining_bytes) {
  908. /* FIXME: check cast */
  909. nbytes = min_t(int, remaining_bytes, DIAG_TRANSFER_LIMIT);
  910. /* Copy caller's data to allocated DMA buf */
  911. memcpy(data_buf, data, nbytes);
  912. /* Set up to receive directly into Target(!) address */
  913. ret = ce_diag->ops->ce_rx_post_buf(ce_diag, &address, address);
  914. if (ret != 0)
  915. goto done;
  916. /*
  917. * Request CE to send caller-supplied data that
  918. * was copied to bounce buffer to Target(!) address.
  919. */
  920. ret = ath10k_ce_send_nolock(ce_diag, NULL, ce_data_base,
  921. nbytes, 0, 0);
  922. if (ret != 0)
  923. goto done;
  924. i = 0;
  925. while (ath10k_ce_completed_send_next_nolock(ce_diag,
  926. NULL) != 0) {
  927. mdelay(1);
  928. if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
  929. ret = -EBUSY;
  930. goto done;
  931. }
  932. }
  933. i = 0;
  934. while (ath10k_ce_completed_recv_next_nolock(ce_diag,
  935. (void **)&buf,
  936. &completed_nbytes)
  937. != 0) {
  938. mdelay(1);
  939. if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
  940. ret = -EBUSY;
  941. goto done;
  942. }
  943. }
  944. if (nbytes != completed_nbytes) {
  945. ret = -EIO;
  946. goto done;
  947. }
  948. if (*buf != address) {
  949. ret = -EIO;
  950. goto done;
  951. }
  952. remaining_bytes -= nbytes;
  953. address += nbytes;
  954. data += nbytes;
  955. }
  956. done:
  957. if (data_buf) {
  958. dma_free_coherent(ar->dev, alloc_nbytes, data_buf,
  959. ce_data_base);
  960. }
  961. if (ret != 0)
  962. ath10k_warn(ar, "failed to write diag value at 0x%x: %d\n",
  963. address, ret);
  964. spin_unlock_bh(&ce->ce_lock);
  965. return ret;
  966. }
  967. static int ath10k_pci_diag_write32(struct ath10k *ar, u32 address, u32 value)
  968. {
  969. __le32 val = __cpu_to_le32(value);
  970. return ath10k_pci_diag_write_mem(ar, address, &val, sizeof(val));
  971. }
  972. /* Called by lower (CE) layer when a send to Target completes. */
  973. static void ath10k_pci_htc_tx_cb(struct ath10k_ce_pipe *ce_state)
  974. {
  975. struct ath10k *ar = ce_state->ar;
  976. struct sk_buff_head list;
  977. struct sk_buff *skb;
  978. __skb_queue_head_init(&list);
  979. while (ath10k_ce_completed_send_next(ce_state, (void **)&skb) == 0) {
  980. /* no need to call tx completion for NULL pointers */
  981. if (skb == NULL)
  982. continue;
  983. __skb_queue_tail(&list, skb);
  984. }
  985. while ((skb = __skb_dequeue(&list)))
  986. ath10k_htc_tx_completion_handler(ar, skb);
  987. }
  988. static void ath10k_pci_process_rx_cb(struct ath10k_ce_pipe *ce_state,
  989. void (*callback)(struct ath10k *ar,
  990. struct sk_buff *skb))
  991. {
  992. struct ath10k *ar = ce_state->ar;
  993. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  994. struct ath10k_pci_pipe *pipe_info = &ar_pci->pipe_info[ce_state->id];
  995. struct sk_buff *skb;
  996. struct sk_buff_head list;
  997. void *transfer_context;
  998. unsigned int nbytes, max_nbytes;
  999. __skb_queue_head_init(&list);
  1000. while (ath10k_ce_completed_recv_next(ce_state, &transfer_context,
  1001. &nbytes) == 0) {
  1002. skb = transfer_context;
  1003. max_nbytes = skb->len + skb_tailroom(skb);
  1004. dma_unmap_single(ar->dev, ATH10K_SKB_RXCB(skb)->paddr,
  1005. max_nbytes, DMA_FROM_DEVICE);
  1006. if (unlikely(max_nbytes < nbytes)) {
  1007. ath10k_warn(ar, "rxed more than expected (nbytes %d, max %d)",
  1008. nbytes, max_nbytes);
  1009. dev_kfree_skb_any(skb);
  1010. continue;
  1011. }
  1012. skb_put(skb, nbytes);
  1013. __skb_queue_tail(&list, skb);
  1014. }
  1015. while ((skb = __skb_dequeue(&list))) {
  1016. ath10k_dbg(ar, ATH10K_DBG_PCI, "pci rx ce pipe %d len %d\n",
  1017. ce_state->id, skb->len);
  1018. ath10k_dbg_dump(ar, ATH10K_DBG_PCI_DUMP, NULL, "pci rx: ",
  1019. skb->data, skb->len);
  1020. callback(ar, skb);
  1021. }
  1022. ath10k_pci_rx_post_pipe(pipe_info);
  1023. }
  1024. static void ath10k_pci_process_htt_rx_cb(struct ath10k_ce_pipe *ce_state,
  1025. void (*callback)(struct ath10k *ar,
  1026. struct sk_buff *skb))
  1027. {
  1028. struct ath10k *ar = ce_state->ar;
  1029. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1030. struct ath10k_pci_pipe *pipe_info = &ar_pci->pipe_info[ce_state->id];
  1031. struct ath10k_ce_pipe *ce_pipe = pipe_info->ce_hdl;
  1032. struct sk_buff *skb;
  1033. struct sk_buff_head list;
  1034. void *transfer_context;
  1035. unsigned int nbytes, max_nbytes, nentries;
  1036. int orig_len;
  1037. /* No need to aquire ce_lock for CE5, since this is the only place CE5
  1038. * is processed other than init and deinit. Before releasing CE5
  1039. * buffers, interrupts are disabled. Thus CE5 access is serialized.
  1040. */
  1041. __skb_queue_head_init(&list);
  1042. while (ath10k_ce_completed_recv_next_nolock(ce_state, &transfer_context,
  1043. &nbytes) == 0) {
  1044. skb = transfer_context;
  1045. max_nbytes = skb->len + skb_tailroom(skb);
  1046. if (unlikely(max_nbytes < nbytes)) {
  1047. ath10k_warn(ar, "rxed more than expected (nbytes %d, max %d)",
  1048. nbytes, max_nbytes);
  1049. continue;
  1050. }
  1051. dma_sync_single_for_cpu(ar->dev, ATH10K_SKB_RXCB(skb)->paddr,
  1052. max_nbytes, DMA_FROM_DEVICE);
  1053. skb_put(skb, nbytes);
  1054. __skb_queue_tail(&list, skb);
  1055. }
  1056. nentries = skb_queue_len(&list);
  1057. while ((skb = __skb_dequeue(&list))) {
  1058. ath10k_dbg(ar, ATH10K_DBG_PCI, "pci rx ce pipe %d len %d\n",
  1059. ce_state->id, skb->len);
  1060. ath10k_dbg_dump(ar, ATH10K_DBG_PCI_DUMP, NULL, "pci rx: ",
  1061. skb->data, skb->len);
  1062. orig_len = skb->len;
  1063. callback(ar, skb);
  1064. skb_push(skb, orig_len - skb->len);
  1065. skb_reset_tail_pointer(skb);
  1066. skb_trim(skb, 0);
  1067. /*let device gain the buffer again*/
  1068. dma_sync_single_for_device(ar->dev, ATH10K_SKB_RXCB(skb)->paddr,
  1069. skb->len + skb_tailroom(skb),
  1070. DMA_FROM_DEVICE);
  1071. }
  1072. ath10k_ce_rx_update_write_idx(ce_pipe, nentries);
  1073. }
  1074. /* Called by lower (CE) layer when data is received from the Target. */
  1075. static void ath10k_pci_htc_rx_cb(struct ath10k_ce_pipe *ce_state)
  1076. {
  1077. ath10k_pci_process_rx_cb(ce_state, ath10k_htc_rx_completion_handler);
  1078. }
  1079. static void ath10k_pci_htt_htc_rx_cb(struct ath10k_ce_pipe *ce_state)
  1080. {
  1081. /* CE4 polling needs to be done whenever CE pipe which transports
  1082. * HTT Rx (target->host) is processed.
  1083. */
  1084. ath10k_ce_per_engine_service(ce_state->ar, 4);
  1085. ath10k_pci_process_rx_cb(ce_state, ath10k_htc_rx_completion_handler);
  1086. }
  1087. /* Called by lower (CE) layer when data is received from the Target.
  1088. * Only 10.4 firmware uses separate CE to transfer pktlog data.
  1089. */
  1090. static void ath10k_pci_pktlog_rx_cb(struct ath10k_ce_pipe *ce_state)
  1091. {
  1092. ath10k_pci_process_rx_cb(ce_state,
  1093. ath10k_htt_rx_pktlog_completion_handler);
  1094. }
  1095. /* Called by lower (CE) layer when a send to HTT Target completes. */
  1096. static void ath10k_pci_htt_tx_cb(struct ath10k_ce_pipe *ce_state)
  1097. {
  1098. struct ath10k *ar = ce_state->ar;
  1099. struct sk_buff *skb;
  1100. while (ath10k_ce_completed_send_next(ce_state, (void **)&skb) == 0) {
  1101. /* no need to call tx completion for NULL pointers */
  1102. if (!skb)
  1103. continue;
  1104. dma_unmap_single(ar->dev, ATH10K_SKB_CB(skb)->paddr,
  1105. skb->len, DMA_TO_DEVICE);
  1106. ath10k_htt_hif_tx_complete(ar, skb);
  1107. }
  1108. }
  1109. static void ath10k_pci_htt_rx_deliver(struct ath10k *ar, struct sk_buff *skb)
  1110. {
  1111. skb_pull(skb, sizeof(struct ath10k_htc_hdr));
  1112. ath10k_htt_t2h_msg_handler(ar, skb);
  1113. }
  1114. /* Called by lower (CE) layer when HTT data is received from the Target. */
  1115. static void ath10k_pci_htt_rx_cb(struct ath10k_ce_pipe *ce_state)
  1116. {
  1117. /* CE4 polling needs to be done whenever CE pipe which transports
  1118. * HTT Rx (target->host) is processed.
  1119. */
  1120. ath10k_ce_per_engine_service(ce_state->ar, 4);
  1121. ath10k_pci_process_htt_rx_cb(ce_state, ath10k_pci_htt_rx_deliver);
  1122. }
  1123. int ath10k_pci_hif_tx_sg(struct ath10k *ar, u8 pipe_id,
  1124. struct ath10k_hif_sg_item *items, int n_items)
  1125. {
  1126. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1127. struct ath10k_ce *ce = ath10k_ce_priv(ar);
  1128. struct ath10k_pci_pipe *pci_pipe = &ar_pci->pipe_info[pipe_id];
  1129. struct ath10k_ce_pipe *ce_pipe = pci_pipe->ce_hdl;
  1130. struct ath10k_ce_ring *src_ring = ce_pipe->src_ring;
  1131. unsigned int nentries_mask;
  1132. unsigned int sw_index;
  1133. unsigned int write_index;
  1134. int err, i = 0;
  1135. spin_lock_bh(&ce->ce_lock);
  1136. nentries_mask = src_ring->nentries_mask;
  1137. sw_index = src_ring->sw_index;
  1138. write_index = src_ring->write_index;
  1139. if (unlikely(CE_RING_DELTA(nentries_mask,
  1140. write_index, sw_index - 1) < n_items)) {
  1141. err = -ENOBUFS;
  1142. goto err;
  1143. }
  1144. for (i = 0; i < n_items - 1; i++) {
  1145. ath10k_dbg(ar, ATH10K_DBG_PCI,
  1146. "pci tx item %d paddr %pad len %d n_items %d\n",
  1147. i, &items[i].paddr, items[i].len, n_items);
  1148. ath10k_dbg_dump(ar, ATH10K_DBG_PCI_DUMP, NULL, "pci tx data: ",
  1149. items[i].vaddr, items[i].len);
  1150. err = ath10k_ce_send_nolock(ce_pipe,
  1151. items[i].transfer_context,
  1152. items[i].paddr,
  1153. items[i].len,
  1154. items[i].transfer_id,
  1155. CE_SEND_FLAG_GATHER);
  1156. if (err)
  1157. goto err;
  1158. }
  1159. /* `i` is equal to `n_items -1` after for() */
  1160. ath10k_dbg(ar, ATH10K_DBG_PCI,
  1161. "pci tx item %d paddr %pad len %d n_items %d\n",
  1162. i, &items[i].paddr, items[i].len, n_items);
  1163. ath10k_dbg_dump(ar, ATH10K_DBG_PCI_DUMP, NULL, "pci tx data: ",
  1164. items[i].vaddr, items[i].len);
  1165. err = ath10k_ce_send_nolock(ce_pipe,
  1166. items[i].transfer_context,
  1167. items[i].paddr,
  1168. items[i].len,
  1169. items[i].transfer_id,
  1170. 0);
  1171. if (err)
  1172. goto err;
  1173. spin_unlock_bh(&ce->ce_lock);
  1174. return 0;
  1175. err:
  1176. for (; i > 0; i--)
  1177. __ath10k_ce_send_revert(ce_pipe);
  1178. spin_unlock_bh(&ce->ce_lock);
  1179. return err;
  1180. }
  1181. int ath10k_pci_hif_diag_read(struct ath10k *ar, u32 address, void *buf,
  1182. size_t buf_len)
  1183. {
  1184. return ath10k_pci_diag_read_mem(ar, address, buf, buf_len);
  1185. }
  1186. u16 ath10k_pci_hif_get_free_queue_number(struct ath10k *ar, u8 pipe)
  1187. {
  1188. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1189. ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif get free queue number\n");
  1190. return ath10k_ce_num_free_src_entries(ar_pci->pipe_info[pipe].ce_hdl);
  1191. }
  1192. static void ath10k_pci_dump_registers(struct ath10k *ar,
  1193. struct ath10k_fw_crash_data *crash_data)
  1194. {
  1195. __le32 reg_dump_values[REG_DUMP_COUNT_QCA988X] = {};
  1196. int i, ret;
  1197. lockdep_assert_held(&ar->data_lock);
  1198. ret = ath10k_pci_diag_read_hi(ar, &reg_dump_values[0],
  1199. hi_failure_state,
  1200. REG_DUMP_COUNT_QCA988X * sizeof(__le32));
  1201. if (ret) {
  1202. ath10k_err(ar, "failed to read firmware dump area: %d\n", ret);
  1203. return;
  1204. }
  1205. BUILD_BUG_ON(REG_DUMP_COUNT_QCA988X % 4);
  1206. ath10k_err(ar, "firmware register dump:\n");
  1207. for (i = 0; i < REG_DUMP_COUNT_QCA988X; i += 4)
  1208. ath10k_err(ar, "[%02d]: 0x%08X 0x%08X 0x%08X 0x%08X\n",
  1209. i,
  1210. __le32_to_cpu(reg_dump_values[i]),
  1211. __le32_to_cpu(reg_dump_values[i + 1]),
  1212. __le32_to_cpu(reg_dump_values[i + 2]),
  1213. __le32_to_cpu(reg_dump_values[i + 3]));
  1214. if (!crash_data)
  1215. return;
  1216. for (i = 0; i < REG_DUMP_COUNT_QCA988X; i++)
  1217. crash_data->registers[i] = reg_dump_values[i];
  1218. }
  1219. static int ath10k_pci_dump_memory_section(struct ath10k *ar,
  1220. const struct ath10k_mem_region *mem_region,
  1221. u8 *buf, size_t buf_len)
  1222. {
  1223. const struct ath10k_mem_section *cur_section, *next_section;
  1224. unsigned int count, section_size, skip_size;
  1225. int ret, i, j;
  1226. if (!mem_region || !buf)
  1227. return 0;
  1228. cur_section = &mem_region->section_table.sections[0];
  1229. if (mem_region->start > cur_section->start) {
  1230. ath10k_warn(ar, "incorrect memdump region 0x%x with section start address 0x%x.\n",
  1231. mem_region->start, cur_section->start);
  1232. return 0;
  1233. }
  1234. skip_size = cur_section->start - mem_region->start;
  1235. /* fill the gap between the first register section and register
  1236. * start address
  1237. */
  1238. for (i = 0; i < skip_size; i++) {
  1239. *buf = ATH10K_MAGIC_NOT_COPIED;
  1240. buf++;
  1241. }
  1242. count = 0;
  1243. for (i = 0; cur_section != NULL; i++) {
  1244. section_size = cur_section->end - cur_section->start;
  1245. if (section_size <= 0) {
  1246. ath10k_warn(ar, "incorrect ramdump format with start address 0x%x and stop address 0x%x\n",
  1247. cur_section->start,
  1248. cur_section->end);
  1249. break;
  1250. }
  1251. if ((i + 1) == mem_region->section_table.size) {
  1252. /* last section */
  1253. next_section = NULL;
  1254. skip_size = 0;
  1255. } else {
  1256. next_section = cur_section + 1;
  1257. if (cur_section->end > next_section->start) {
  1258. ath10k_warn(ar, "next ramdump section 0x%x is smaller than current end address 0x%x\n",
  1259. next_section->start,
  1260. cur_section->end);
  1261. break;
  1262. }
  1263. skip_size = next_section->start - cur_section->end;
  1264. }
  1265. if (buf_len < (skip_size + section_size)) {
  1266. ath10k_warn(ar, "ramdump buffer is too small: %zu\n", buf_len);
  1267. break;
  1268. }
  1269. buf_len -= skip_size + section_size;
  1270. /* read section to dest memory */
  1271. ret = ath10k_pci_diag_read_mem(ar, cur_section->start,
  1272. buf, section_size);
  1273. if (ret) {
  1274. ath10k_warn(ar, "failed to read ramdump from section 0x%x: %d\n",
  1275. cur_section->start, ret);
  1276. break;
  1277. }
  1278. buf += section_size;
  1279. count += section_size;
  1280. /* fill in the gap between this section and the next */
  1281. for (j = 0; j < skip_size; j++) {
  1282. *buf = ATH10K_MAGIC_NOT_COPIED;
  1283. buf++;
  1284. }
  1285. count += skip_size;
  1286. if (!next_section)
  1287. /* this was the last section */
  1288. break;
  1289. cur_section = next_section;
  1290. }
  1291. return count;
  1292. }
  1293. static int ath10k_pci_set_ram_config(struct ath10k *ar, u32 config)
  1294. {
  1295. u32 val;
  1296. ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS +
  1297. FW_RAM_CONFIG_ADDRESS, config);
  1298. val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
  1299. FW_RAM_CONFIG_ADDRESS);
  1300. if (val != config) {
  1301. ath10k_warn(ar, "failed to set RAM config from 0x%x to 0x%x\n",
  1302. val, config);
  1303. return -EIO;
  1304. }
  1305. return 0;
  1306. }
  1307. /* if an error happened returns < 0, otherwise the length */
  1308. static int ath10k_pci_dump_memory_sram(struct ath10k *ar,
  1309. const struct ath10k_mem_region *region,
  1310. u8 *buf)
  1311. {
  1312. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1313. u32 base_addr, i;
  1314. base_addr = ioread32(ar_pci->mem + QCA99X0_PCIE_BAR0_START_REG);
  1315. base_addr += region->start;
  1316. for (i = 0; i < region->len; i += 4) {
  1317. iowrite32(base_addr + i, ar_pci->mem + QCA99X0_CPU_MEM_ADDR_REG);
  1318. *(u32 *)(buf + i) = ioread32(ar_pci->mem + QCA99X0_CPU_MEM_DATA_REG);
  1319. }
  1320. return region->len;
  1321. }
  1322. /* if an error happened returns < 0, otherwise the length */
  1323. static int ath10k_pci_dump_memory_reg(struct ath10k *ar,
  1324. const struct ath10k_mem_region *region,
  1325. u8 *buf)
  1326. {
  1327. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1328. u32 i;
  1329. int ret;
  1330. mutex_lock(&ar->conf_mutex);
  1331. if (ar->state != ATH10K_STATE_ON) {
  1332. ath10k_warn(ar, "Skipping pci_dump_memory_reg invalid state\n");
  1333. ret = -EIO;
  1334. goto done;
  1335. }
  1336. for (i = 0; i < region->len; i += 4)
  1337. *(u32 *)(buf + i) = ioread32(ar_pci->mem + region->start + i);
  1338. ret = region->len;
  1339. done:
  1340. mutex_unlock(&ar->conf_mutex);
  1341. return ret;
  1342. }
  1343. /* if an error happened returns < 0, otherwise the length */
  1344. static int ath10k_pci_dump_memory_generic(struct ath10k *ar,
  1345. const struct ath10k_mem_region *current_region,
  1346. u8 *buf)
  1347. {
  1348. int ret;
  1349. if (current_region->section_table.size > 0)
  1350. /* Copy each section individually. */
  1351. return ath10k_pci_dump_memory_section(ar,
  1352. current_region,
  1353. buf,
  1354. current_region->len);
  1355. /* No individiual memory sections defined so we can
  1356. * copy the entire memory region.
  1357. */
  1358. ret = ath10k_pci_diag_read_mem(ar,
  1359. current_region->start,
  1360. buf,
  1361. current_region->len);
  1362. if (ret) {
  1363. ath10k_warn(ar, "failed to copy ramdump region %s: %d\n",
  1364. current_region->name, ret);
  1365. return ret;
  1366. }
  1367. return current_region->len;
  1368. }
  1369. static void ath10k_pci_dump_memory(struct ath10k *ar,
  1370. struct ath10k_fw_crash_data *crash_data)
  1371. {
  1372. const struct ath10k_hw_mem_layout *mem_layout;
  1373. const struct ath10k_mem_region *current_region;
  1374. struct ath10k_dump_ram_data_hdr *hdr;
  1375. u32 count, shift;
  1376. size_t buf_len;
  1377. int ret, i;
  1378. u8 *buf;
  1379. lockdep_assert_held(&ar->data_lock);
  1380. if (!crash_data)
  1381. return;
  1382. mem_layout = ath10k_coredump_get_mem_layout(ar);
  1383. if (!mem_layout)
  1384. return;
  1385. current_region = &mem_layout->region_table.regions[0];
  1386. buf = crash_data->ramdump_buf;
  1387. buf_len = crash_data->ramdump_buf_len;
  1388. memset(buf, 0, buf_len);
  1389. for (i = 0; i < mem_layout->region_table.size; i++) {
  1390. count = 0;
  1391. if (current_region->len > buf_len) {
  1392. ath10k_warn(ar, "memory region %s size %d is larger that remaining ramdump buffer size %zu\n",
  1393. current_region->name,
  1394. current_region->len,
  1395. buf_len);
  1396. break;
  1397. }
  1398. /* To get IRAM dump, the host driver needs to switch target
  1399. * ram config from DRAM to IRAM.
  1400. */
  1401. if (current_region->type == ATH10K_MEM_REGION_TYPE_IRAM1 ||
  1402. current_region->type == ATH10K_MEM_REGION_TYPE_IRAM2) {
  1403. shift = current_region->start >> 20;
  1404. ret = ath10k_pci_set_ram_config(ar, shift);
  1405. if (ret) {
  1406. ath10k_warn(ar, "failed to switch ram config to IRAM for section %s: %d\n",
  1407. current_region->name, ret);
  1408. break;
  1409. }
  1410. }
  1411. /* Reserve space for the header. */
  1412. hdr = (void *)buf;
  1413. buf += sizeof(*hdr);
  1414. buf_len -= sizeof(*hdr);
  1415. switch (current_region->type) {
  1416. case ATH10K_MEM_REGION_TYPE_IOSRAM:
  1417. count = ath10k_pci_dump_memory_sram(ar, current_region, buf);
  1418. break;
  1419. case ATH10K_MEM_REGION_TYPE_IOREG:
  1420. ret = ath10k_pci_dump_memory_reg(ar, current_region, buf);
  1421. if (ret < 0)
  1422. break;
  1423. count = ret;
  1424. break;
  1425. default:
  1426. ret = ath10k_pci_dump_memory_generic(ar, current_region, buf);
  1427. if (ret < 0)
  1428. break;
  1429. count = ret;
  1430. break;
  1431. }
  1432. hdr->region_type = cpu_to_le32(current_region->type);
  1433. hdr->start = cpu_to_le32(current_region->start);
  1434. hdr->length = cpu_to_le32(count);
  1435. if (count == 0)
  1436. /* Note: the header remains, just with zero length. */
  1437. break;
  1438. buf += count;
  1439. buf_len -= count;
  1440. current_region++;
  1441. }
  1442. }
  1443. static void ath10k_pci_fw_crashed_dump(struct ath10k *ar)
  1444. {
  1445. struct ath10k_fw_crash_data *crash_data;
  1446. char guid[UUID_STRING_LEN + 1];
  1447. spin_lock_bh(&ar->data_lock);
  1448. ar->stats.fw_crash_counter++;
  1449. crash_data = ath10k_coredump_new(ar);
  1450. if (crash_data)
  1451. scnprintf(guid, sizeof(guid), "%pUl", &crash_data->guid);
  1452. else
  1453. scnprintf(guid, sizeof(guid), "n/a");
  1454. ath10k_err(ar, "firmware crashed! (guid %s)\n", guid);
  1455. ath10k_print_driver_info(ar);
  1456. ath10k_pci_dump_registers(ar, crash_data);
  1457. ath10k_ce_dump_registers(ar, crash_data);
  1458. ath10k_pci_dump_memory(ar, crash_data);
  1459. spin_unlock_bh(&ar->data_lock);
  1460. queue_work(ar->workqueue, &ar->restart_work);
  1461. }
  1462. void ath10k_pci_hif_send_complete_check(struct ath10k *ar, u8 pipe,
  1463. int force)
  1464. {
  1465. ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif send complete check\n");
  1466. if (!force) {
  1467. int resources;
  1468. /*
  1469. * Decide whether to actually poll for completions, or just
  1470. * wait for a later chance.
  1471. * If there seem to be plenty of resources left, then just wait
  1472. * since checking involves reading a CE register, which is a
  1473. * relatively expensive operation.
  1474. */
  1475. resources = ath10k_pci_hif_get_free_queue_number(ar, pipe);
  1476. /*
  1477. * If at least 50% of the total resources are still available,
  1478. * don't bother checking again yet.
  1479. */
  1480. if (resources > (host_ce_config_wlan[pipe].src_nentries >> 1))
  1481. return;
  1482. }
  1483. ath10k_ce_per_engine_service(ar, pipe);
  1484. }
  1485. static void ath10k_pci_rx_retry_sync(struct ath10k *ar)
  1486. {
  1487. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1488. del_timer_sync(&ar_pci->rx_post_retry);
  1489. }
  1490. int ath10k_pci_hif_map_service_to_pipe(struct ath10k *ar, u16 service_id,
  1491. u8 *ul_pipe, u8 *dl_pipe)
  1492. {
  1493. const struct service_to_pipe *entry;
  1494. bool ul_set = false, dl_set = false;
  1495. int i;
  1496. ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif map service\n");
  1497. for (i = 0; i < ARRAY_SIZE(target_service_to_ce_map_wlan); i++) {
  1498. entry = &target_service_to_ce_map_wlan[i];
  1499. if (__le32_to_cpu(entry->service_id) != service_id)
  1500. continue;
  1501. switch (__le32_to_cpu(entry->pipedir)) {
  1502. case PIPEDIR_NONE:
  1503. break;
  1504. case PIPEDIR_IN:
  1505. WARN_ON(dl_set);
  1506. *dl_pipe = __le32_to_cpu(entry->pipenum);
  1507. dl_set = true;
  1508. break;
  1509. case PIPEDIR_OUT:
  1510. WARN_ON(ul_set);
  1511. *ul_pipe = __le32_to_cpu(entry->pipenum);
  1512. ul_set = true;
  1513. break;
  1514. case PIPEDIR_INOUT:
  1515. WARN_ON(dl_set);
  1516. WARN_ON(ul_set);
  1517. *dl_pipe = __le32_to_cpu(entry->pipenum);
  1518. *ul_pipe = __le32_to_cpu(entry->pipenum);
  1519. dl_set = true;
  1520. ul_set = true;
  1521. break;
  1522. }
  1523. }
  1524. if (WARN_ON(!ul_set || !dl_set))
  1525. return -ENOENT;
  1526. return 0;
  1527. }
  1528. void ath10k_pci_hif_get_default_pipe(struct ath10k *ar,
  1529. u8 *ul_pipe, u8 *dl_pipe)
  1530. {
  1531. ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif get default pipe\n");
  1532. (void)ath10k_pci_hif_map_service_to_pipe(ar,
  1533. ATH10K_HTC_SVC_ID_RSVD_CTRL,
  1534. ul_pipe, dl_pipe);
  1535. }
  1536. void ath10k_pci_irq_msi_fw_mask(struct ath10k *ar)
  1537. {
  1538. u32 val;
  1539. switch (ar->hw_rev) {
  1540. case ATH10K_HW_QCA988X:
  1541. case ATH10K_HW_QCA9887:
  1542. case ATH10K_HW_QCA6174:
  1543. case ATH10K_HW_QCA9377:
  1544. val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
  1545. CORE_CTRL_ADDRESS);
  1546. val &= ~CORE_CTRL_PCIE_REG_31_MASK;
  1547. ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS +
  1548. CORE_CTRL_ADDRESS, val);
  1549. break;
  1550. case ATH10K_HW_QCA99X0:
  1551. case ATH10K_HW_QCA9984:
  1552. case ATH10K_HW_QCA9888:
  1553. case ATH10K_HW_QCA4019:
  1554. /* TODO: Find appropriate register configuration for QCA99X0
  1555. * to mask irq/MSI.
  1556. */
  1557. break;
  1558. case ATH10K_HW_WCN3990:
  1559. break;
  1560. }
  1561. }
  1562. static void ath10k_pci_irq_msi_fw_unmask(struct ath10k *ar)
  1563. {
  1564. u32 val;
  1565. switch (ar->hw_rev) {
  1566. case ATH10K_HW_QCA988X:
  1567. case ATH10K_HW_QCA9887:
  1568. case ATH10K_HW_QCA6174:
  1569. case ATH10K_HW_QCA9377:
  1570. val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
  1571. CORE_CTRL_ADDRESS);
  1572. val |= CORE_CTRL_PCIE_REG_31_MASK;
  1573. ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS +
  1574. CORE_CTRL_ADDRESS, val);
  1575. break;
  1576. case ATH10K_HW_QCA99X0:
  1577. case ATH10K_HW_QCA9984:
  1578. case ATH10K_HW_QCA9888:
  1579. case ATH10K_HW_QCA4019:
  1580. /* TODO: Find appropriate register configuration for QCA99X0
  1581. * to unmask irq/MSI.
  1582. */
  1583. break;
  1584. case ATH10K_HW_WCN3990:
  1585. break;
  1586. }
  1587. }
  1588. static void ath10k_pci_irq_disable(struct ath10k *ar)
  1589. {
  1590. ath10k_ce_disable_interrupts(ar);
  1591. ath10k_pci_disable_and_clear_legacy_irq(ar);
  1592. ath10k_pci_irq_msi_fw_mask(ar);
  1593. }
  1594. static void ath10k_pci_irq_sync(struct ath10k *ar)
  1595. {
  1596. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1597. synchronize_irq(ar_pci->pdev->irq);
  1598. }
  1599. static void ath10k_pci_irq_enable(struct ath10k *ar)
  1600. {
  1601. ath10k_ce_enable_interrupts(ar);
  1602. ath10k_pci_enable_legacy_irq(ar);
  1603. ath10k_pci_irq_msi_fw_unmask(ar);
  1604. }
  1605. static int ath10k_pci_hif_start(struct ath10k *ar)
  1606. {
  1607. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1608. ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif start\n");
  1609. napi_enable(&ar->napi);
  1610. ath10k_pci_irq_enable(ar);
  1611. ath10k_pci_rx_post(ar);
  1612. pcie_capability_write_word(ar_pci->pdev, PCI_EXP_LNKCTL,
  1613. ar_pci->link_ctl);
  1614. return 0;
  1615. }
  1616. static void ath10k_pci_rx_pipe_cleanup(struct ath10k_pci_pipe *pci_pipe)
  1617. {
  1618. struct ath10k *ar;
  1619. struct ath10k_ce_pipe *ce_pipe;
  1620. struct ath10k_ce_ring *ce_ring;
  1621. struct sk_buff *skb;
  1622. int i;
  1623. ar = pci_pipe->hif_ce_state;
  1624. ce_pipe = pci_pipe->ce_hdl;
  1625. ce_ring = ce_pipe->dest_ring;
  1626. if (!ce_ring)
  1627. return;
  1628. if (!pci_pipe->buf_sz)
  1629. return;
  1630. for (i = 0; i < ce_ring->nentries; i++) {
  1631. skb = ce_ring->per_transfer_context[i];
  1632. if (!skb)
  1633. continue;
  1634. ce_ring->per_transfer_context[i] = NULL;
  1635. dma_unmap_single(ar->dev, ATH10K_SKB_RXCB(skb)->paddr,
  1636. skb->len + skb_tailroom(skb),
  1637. DMA_FROM_DEVICE);
  1638. dev_kfree_skb_any(skb);
  1639. }
  1640. }
  1641. static void ath10k_pci_tx_pipe_cleanup(struct ath10k_pci_pipe *pci_pipe)
  1642. {
  1643. struct ath10k *ar;
  1644. struct ath10k_ce_pipe *ce_pipe;
  1645. struct ath10k_ce_ring *ce_ring;
  1646. struct sk_buff *skb;
  1647. int i;
  1648. ar = pci_pipe->hif_ce_state;
  1649. ce_pipe = pci_pipe->ce_hdl;
  1650. ce_ring = ce_pipe->src_ring;
  1651. if (!ce_ring)
  1652. return;
  1653. if (!pci_pipe->buf_sz)
  1654. return;
  1655. for (i = 0; i < ce_ring->nentries; i++) {
  1656. skb = ce_ring->per_transfer_context[i];
  1657. if (!skb)
  1658. continue;
  1659. ce_ring->per_transfer_context[i] = NULL;
  1660. ath10k_htc_tx_completion_handler(ar, skb);
  1661. }
  1662. }
  1663. /*
  1664. * Cleanup residual buffers for device shutdown:
  1665. * buffers that were enqueued for receive
  1666. * buffers that were to be sent
  1667. * Note: Buffers that had completed but which were
  1668. * not yet processed are on a completion queue. They
  1669. * are handled when the completion thread shuts down.
  1670. */
  1671. static void ath10k_pci_buffer_cleanup(struct ath10k *ar)
  1672. {
  1673. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1674. int pipe_num;
  1675. for (pipe_num = 0; pipe_num < CE_COUNT; pipe_num++) {
  1676. struct ath10k_pci_pipe *pipe_info;
  1677. pipe_info = &ar_pci->pipe_info[pipe_num];
  1678. ath10k_pci_rx_pipe_cleanup(pipe_info);
  1679. ath10k_pci_tx_pipe_cleanup(pipe_info);
  1680. }
  1681. }
  1682. void ath10k_pci_ce_deinit(struct ath10k *ar)
  1683. {
  1684. int i;
  1685. for (i = 0; i < CE_COUNT; i++)
  1686. ath10k_ce_deinit_pipe(ar, i);
  1687. }
  1688. void ath10k_pci_flush(struct ath10k *ar)
  1689. {
  1690. ath10k_pci_rx_retry_sync(ar);
  1691. ath10k_pci_buffer_cleanup(ar);
  1692. }
  1693. static void ath10k_pci_hif_stop(struct ath10k *ar)
  1694. {
  1695. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1696. unsigned long flags;
  1697. ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif stop\n");
  1698. ath10k_pci_irq_disable(ar);
  1699. ath10k_pci_irq_sync(ar);
  1700. napi_synchronize(&ar->napi);
  1701. napi_disable(&ar->napi);
  1702. /* Most likely the device has HTT Rx ring configured. The only way to
  1703. * prevent the device from accessing (and possible corrupting) host
  1704. * memory is to reset the chip now.
  1705. *
  1706. * There's also no known way of masking MSI interrupts on the device.
  1707. * For ranged MSI the CE-related interrupts can be masked. However
  1708. * regardless how many MSI interrupts are assigned the first one
  1709. * is always used for firmware indications (crashes) and cannot be
  1710. * masked. To prevent the device from asserting the interrupt reset it
  1711. * before proceeding with cleanup.
  1712. */
  1713. ath10k_pci_safe_chip_reset(ar);
  1714. ath10k_pci_flush(ar);
  1715. spin_lock_irqsave(&ar_pci->ps_lock, flags);
  1716. WARN_ON(ar_pci->ps_wake_refcount > 0);
  1717. spin_unlock_irqrestore(&ar_pci->ps_lock, flags);
  1718. }
  1719. int ath10k_pci_hif_exchange_bmi_msg(struct ath10k *ar,
  1720. void *req, u32 req_len,
  1721. void *resp, u32 *resp_len)
  1722. {
  1723. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1724. struct ath10k_pci_pipe *pci_tx = &ar_pci->pipe_info[BMI_CE_NUM_TO_TARG];
  1725. struct ath10k_pci_pipe *pci_rx = &ar_pci->pipe_info[BMI_CE_NUM_TO_HOST];
  1726. struct ath10k_ce_pipe *ce_tx = pci_tx->ce_hdl;
  1727. struct ath10k_ce_pipe *ce_rx = pci_rx->ce_hdl;
  1728. dma_addr_t req_paddr = 0;
  1729. dma_addr_t resp_paddr = 0;
  1730. struct bmi_xfer xfer = {};
  1731. void *treq, *tresp = NULL;
  1732. int ret = 0;
  1733. might_sleep();
  1734. if (resp && !resp_len)
  1735. return -EINVAL;
  1736. if (resp && resp_len && *resp_len == 0)
  1737. return -EINVAL;
  1738. treq = kmemdup(req, req_len, GFP_KERNEL);
  1739. if (!treq)
  1740. return -ENOMEM;
  1741. req_paddr = dma_map_single(ar->dev, treq, req_len, DMA_TO_DEVICE);
  1742. ret = dma_mapping_error(ar->dev, req_paddr);
  1743. if (ret) {
  1744. ret = -EIO;
  1745. goto err_dma;
  1746. }
  1747. if (resp && resp_len) {
  1748. tresp = kzalloc(*resp_len, GFP_KERNEL);
  1749. if (!tresp) {
  1750. ret = -ENOMEM;
  1751. goto err_req;
  1752. }
  1753. resp_paddr = dma_map_single(ar->dev, tresp, *resp_len,
  1754. DMA_FROM_DEVICE);
  1755. ret = dma_mapping_error(ar->dev, resp_paddr);
  1756. if (ret) {
  1757. ret = -EIO;
  1758. goto err_req;
  1759. }
  1760. xfer.wait_for_resp = true;
  1761. xfer.resp_len = 0;
  1762. ath10k_ce_rx_post_buf(ce_rx, &xfer, resp_paddr);
  1763. }
  1764. ret = ath10k_ce_send(ce_tx, &xfer, req_paddr, req_len, -1, 0);
  1765. if (ret)
  1766. goto err_resp;
  1767. ret = ath10k_pci_bmi_wait(ar, ce_tx, ce_rx, &xfer);
  1768. if (ret) {
  1769. dma_addr_t unused_buffer;
  1770. unsigned int unused_nbytes;
  1771. unsigned int unused_id;
  1772. ath10k_ce_cancel_send_next(ce_tx, NULL, &unused_buffer,
  1773. &unused_nbytes, &unused_id);
  1774. } else {
  1775. /* non-zero means we did not time out */
  1776. ret = 0;
  1777. }
  1778. err_resp:
  1779. if (resp) {
  1780. dma_addr_t unused_buffer;
  1781. ath10k_ce_revoke_recv_next(ce_rx, NULL, &unused_buffer);
  1782. dma_unmap_single(ar->dev, resp_paddr,
  1783. *resp_len, DMA_FROM_DEVICE);
  1784. }
  1785. err_req:
  1786. dma_unmap_single(ar->dev, req_paddr, req_len, DMA_TO_DEVICE);
  1787. if (ret == 0 && resp_len) {
  1788. *resp_len = min(*resp_len, xfer.resp_len);
  1789. memcpy(resp, tresp, xfer.resp_len);
  1790. }
  1791. err_dma:
  1792. kfree(treq);
  1793. kfree(tresp);
  1794. return ret;
  1795. }
  1796. static void ath10k_pci_bmi_send_done(struct ath10k_ce_pipe *ce_state)
  1797. {
  1798. struct bmi_xfer *xfer;
  1799. if (ath10k_ce_completed_send_next(ce_state, (void **)&xfer))
  1800. return;
  1801. xfer->tx_done = true;
  1802. }
  1803. static void ath10k_pci_bmi_recv_data(struct ath10k_ce_pipe *ce_state)
  1804. {
  1805. struct ath10k *ar = ce_state->ar;
  1806. struct bmi_xfer *xfer;
  1807. unsigned int nbytes;
  1808. if (ath10k_ce_completed_recv_next(ce_state, (void **)&xfer,
  1809. &nbytes))
  1810. return;
  1811. if (WARN_ON_ONCE(!xfer))
  1812. return;
  1813. if (!xfer->wait_for_resp) {
  1814. ath10k_warn(ar, "unexpected: BMI data received; ignoring\n");
  1815. return;
  1816. }
  1817. xfer->resp_len = nbytes;
  1818. xfer->rx_done = true;
  1819. }
  1820. static int ath10k_pci_bmi_wait(struct ath10k *ar,
  1821. struct ath10k_ce_pipe *tx_pipe,
  1822. struct ath10k_ce_pipe *rx_pipe,
  1823. struct bmi_xfer *xfer)
  1824. {
  1825. unsigned long timeout = jiffies + BMI_COMMUNICATION_TIMEOUT_HZ;
  1826. unsigned long started = jiffies;
  1827. unsigned long dur;
  1828. int ret;
  1829. while (time_before_eq(jiffies, timeout)) {
  1830. ath10k_pci_bmi_send_done(tx_pipe);
  1831. ath10k_pci_bmi_recv_data(rx_pipe);
  1832. if (xfer->tx_done && (xfer->rx_done == xfer->wait_for_resp)) {
  1833. ret = 0;
  1834. goto out;
  1835. }
  1836. schedule();
  1837. }
  1838. ret = -ETIMEDOUT;
  1839. out:
  1840. dur = jiffies - started;
  1841. if (dur > HZ)
  1842. ath10k_dbg(ar, ATH10K_DBG_BMI,
  1843. "bmi cmd took %lu jiffies hz %d ret %d\n",
  1844. dur, HZ, ret);
  1845. return ret;
  1846. }
  1847. /*
  1848. * Send an interrupt to the device to wake up the Target CPU
  1849. * so it has an opportunity to notice any changed state.
  1850. */
  1851. static int ath10k_pci_wake_target_cpu(struct ath10k *ar)
  1852. {
  1853. u32 addr, val;
  1854. addr = SOC_CORE_BASE_ADDRESS + CORE_CTRL_ADDRESS;
  1855. val = ath10k_pci_read32(ar, addr);
  1856. val |= CORE_CTRL_CPU_INTR_MASK;
  1857. ath10k_pci_write32(ar, addr, val);
  1858. return 0;
  1859. }
  1860. static int ath10k_pci_get_num_banks(struct ath10k *ar)
  1861. {
  1862. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1863. switch (ar_pci->pdev->device) {
  1864. case QCA988X_2_0_DEVICE_ID_UBNT:
  1865. case QCA988X_2_0_DEVICE_ID:
  1866. case QCA99X0_2_0_DEVICE_ID:
  1867. case QCA9888_2_0_DEVICE_ID:
  1868. case QCA9984_1_0_DEVICE_ID:
  1869. case QCA9887_1_0_DEVICE_ID:
  1870. return 1;
  1871. case QCA6164_2_1_DEVICE_ID:
  1872. case QCA6174_2_1_DEVICE_ID:
  1873. switch (MS(ar->chip_id, SOC_CHIP_ID_REV)) {
  1874. case QCA6174_HW_1_0_CHIP_ID_REV:
  1875. case QCA6174_HW_1_1_CHIP_ID_REV:
  1876. case QCA6174_HW_2_1_CHIP_ID_REV:
  1877. case QCA6174_HW_2_2_CHIP_ID_REV:
  1878. return 3;
  1879. case QCA6174_HW_1_3_CHIP_ID_REV:
  1880. return 2;
  1881. case QCA6174_HW_3_0_CHIP_ID_REV:
  1882. case QCA6174_HW_3_1_CHIP_ID_REV:
  1883. case QCA6174_HW_3_2_CHIP_ID_REV:
  1884. return 9;
  1885. }
  1886. break;
  1887. case QCA9377_1_0_DEVICE_ID:
  1888. return 9;
  1889. }
  1890. ath10k_warn(ar, "unknown number of banks, assuming 1\n");
  1891. return 1;
  1892. }
  1893. static int ath10k_bus_get_num_banks(struct ath10k *ar)
  1894. {
  1895. struct ath10k_ce *ce = ath10k_ce_priv(ar);
  1896. return ce->bus_ops->get_num_banks(ar);
  1897. }
  1898. int ath10k_pci_init_config(struct ath10k *ar)
  1899. {
  1900. u32 interconnect_targ_addr;
  1901. u32 pcie_state_targ_addr = 0;
  1902. u32 pipe_cfg_targ_addr = 0;
  1903. u32 svc_to_pipe_map = 0;
  1904. u32 pcie_config_flags = 0;
  1905. u32 ealloc_value;
  1906. u32 ealloc_targ_addr;
  1907. u32 flag2_value;
  1908. u32 flag2_targ_addr;
  1909. int ret = 0;
  1910. /* Download to Target the CE Config and the service-to-CE map */
  1911. interconnect_targ_addr =
  1912. host_interest_item_address(HI_ITEM(hi_interconnect_state));
  1913. /* Supply Target-side CE configuration */
  1914. ret = ath10k_pci_diag_read32(ar, interconnect_targ_addr,
  1915. &pcie_state_targ_addr);
  1916. if (ret != 0) {
  1917. ath10k_err(ar, "Failed to get pcie state addr: %d\n", ret);
  1918. return ret;
  1919. }
  1920. if (pcie_state_targ_addr == 0) {
  1921. ret = -EIO;
  1922. ath10k_err(ar, "Invalid pcie state addr\n");
  1923. return ret;
  1924. }
  1925. ret = ath10k_pci_diag_read32(ar, (pcie_state_targ_addr +
  1926. offsetof(struct pcie_state,
  1927. pipe_cfg_addr)),
  1928. &pipe_cfg_targ_addr);
  1929. if (ret != 0) {
  1930. ath10k_err(ar, "Failed to get pipe cfg addr: %d\n", ret);
  1931. return ret;
  1932. }
  1933. if (pipe_cfg_targ_addr == 0) {
  1934. ret = -EIO;
  1935. ath10k_err(ar, "Invalid pipe cfg addr\n");
  1936. return ret;
  1937. }
  1938. ret = ath10k_pci_diag_write_mem(ar, pipe_cfg_targ_addr,
  1939. target_ce_config_wlan,
  1940. sizeof(struct ce_pipe_config) *
  1941. NUM_TARGET_CE_CONFIG_WLAN);
  1942. if (ret != 0) {
  1943. ath10k_err(ar, "Failed to write pipe cfg: %d\n", ret);
  1944. return ret;
  1945. }
  1946. ret = ath10k_pci_diag_read32(ar, (pcie_state_targ_addr +
  1947. offsetof(struct pcie_state,
  1948. svc_to_pipe_map)),
  1949. &svc_to_pipe_map);
  1950. if (ret != 0) {
  1951. ath10k_err(ar, "Failed to get svc/pipe map: %d\n", ret);
  1952. return ret;
  1953. }
  1954. if (svc_to_pipe_map == 0) {
  1955. ret = -EIO;
  1956. ath10k_err(ar, "Invalid svc_to_pipe map\n");
  1957. return ret;
  1958. }
  1959. ret = ath10k_pci_diag_write_mem(ar, svc_to_pipe_map,
  1960. target_service_to_ce_map_wlan,
  1961. sizeof(target_service_to_ce_map_wlan));
  1962. if (ret != 0) {
  1963. ath10k_err(ar, "Failed to write svc/pipe map: %d\n", ret);
  1964. return ret;
  1965. }
  1966. ret = ath10k_pci_diag_read32(ar, (pcie_state_targ_addr +
  1967. offsetof(struct pcie_state,
  1968. config_flags)),
  1969. &pcie_config_flags);
  1970. if (ret != 0) {
  1971. ath10k_err(ar, "Failed to get pcie config_flags: %d\n", ret);
  1972. return ret;
  1973. }
  1974. pcie_config_flags &= ~PCIE_CONFIG_FLAG_ENABLE_L1;
  1975. ret = ath10k_pci_diag_write32(ar, (pcie_state_targ_addr +
  1976. offsetof(struct pcie_state,
  1977. config_flags)),
  1978. pcie_config_flags);
  1979. if (ret != 0) {
  1980. ath10k_err(ar, "Failed to write pcie config_flags: %d\n", ret);
  1981. return ret;
  1982. }
  1983. /* configure early allocation */
  1984. ealloc_targ_addr = host_interest_item_address(HI_ITEM(hi_early_alloc));
  1985. ret = ath10k_pci_diag_read32(ar, ealloc_targ_addr, &ealloc_value);
  1986. if (ret != 0) {
  1987. ath10k_err(ar, "Failed to get early alloc val: %d\n", ret);
  1988. return ret;
  1989. }
  1990. /* first bank is switched to IRAM */
  1991. ealloc_value |= ((HI_EARLY_ALLOC_MAGIC << HI_EARLY_ALLOC_MAGIC_SHIFT) &
  1992. HI_EARLY_ALLOC_MAGIC_MASK);
  1993. ealloc_value |= ((ath10k_bus_get_num_banks(ar) <<
  1994. HI_EARLY_ALLOC_IRAM_BANKS_SHIFT) &
  1995. HI_EARLY_ALLOC_IRAM_BANKS_MASK);
  1996. ret = ath10k_pci_diag_write32(ar, ealloc_targ_addr, ealloc_value);
  1997. if (ret != 0) {
  1998. ath10k_err(ar, "Failed to set early alloc val: %d\n", ret);
  1999. return ret;
  2000. }
  2001. /* Tell Target to proceed with initialization */
  2002. flag2_targ_addr = host_interest_item_address(HI_ITEM(hi_option_flag2));
  2003. ret = ath10k_pci_diag_read32(ar, flag2_targ_addr, &flag2_value);
  2004. if (ret != 0) {
  2005. ath10k_err(ar, "Failed to get option val: %d\n", ret);
  2006. return ret;
  2007. }
  2008. flag2_value |= HI_OPTION_EARLY_CFG_DONE;
  2009. ret = ath10k_pci_diag_write32(ar, flag2_targ_addr, flag2_value);
  2010. if (ret != 0) {
  2011. ath10k_err(ar, "Failed to set option val: %d\n", ret);
  2012. return ret;
  2013. }
  2014. return 0;
  2015. }
  2016. static void ath10k_pci_override_ce_config(struct ath10k *ar)
  2017. {
  2018. struct ce_attr *attr;
  2019. struct ce_pipe_config *config;
  2020. /* For QCA6174 we're overriding the Copy Engine 5 configuration,
  2021. * since it is currently used for other feature.
  2022. */
  2023. /* Override Host's Copy Engine 5 configuration */
  2024. attr = &host_ce_config_wlan[5];
  2025. attr->src_sz_max = 0;
  2026. attr->dest_nentries = 0;
  2027. /* Override Target firmware's Copy Engine configuration */
  2028. config = &target_ce_config_wlan[5];
  2029. config->pipedir = __cpu_to_le32(PIPEDIR_OUT);
  2030. config->nbytes_max = __cpu_to_le32(2048);
  2031. /* Map from service/endpoint to Copy Engine */
  2032. target_service_to_ce_map_wlan[15].pipenum = __cpu_to_le32(1);
  2033. }
  2034. int ath10k_pci_alloc_pipes(struct ath10k *ar)
  2035. {
  2036. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  2037. struct ath10k_pci_pipe *pipe;
  2038. struct ath10k_ce *ce = ath10k_ce_priv(ar);
  2039. int i, ret;
  2040. for (i = 0; i < CE_COUNT; i++) {
  2041. pipe = &ar_pci->pipe_info[i];
  2042. pipe->ce_hdl = &ce->ce_states[i];
  2043. pipe->pipe_num = i;
  2044. pipe->hif_ce_state = ar;
  2045. ret = ath10k_ce_alloc_pipe(ar, i, &host_ce_config_wlan[i]);
  2046. if (ret) {
  2047. ath10k_err(ar, "failed to allocate copy engine pipe %d: %d\n",
  2048. i, ret);
  2049. return ret;
  2050. }
  2051. /* Last CE is Diagnostic Window */
  2052. if (i == CE_DIAG_PIPE) {
  2053. ar_pci->ce_diag = pipe->ce_hdl;
  2054. continue;
  2055. }
  2056. pipe->buf_sz = (size_t)(host_ce_config_wlan[i].src_sz_max);
  2057. }
  2058. return 0;
  2059. }
  2060. void ath10k_pci_free_pipes(struct ath10k *ar)
  2061. {
  2062. int i;
  2063. for (i = 0; i < CE_COUNT; i++)
  2064. ath10k_ce_free_pipe(ar, i);
  2065. }
  2066. int ath10k_pci_init_pipes(struct ath10k *ar)
  2067. {
  2068. int i, ret;
  2069. for (i = 0; i < CE_COUNT; i++) {
  2070. ret = ath10k_ce_init_pipe(ar, i, &host_ce_config_wlan[i]);
  2071. if (ret) {
  2072. ath10k_err(ar, "failed to initialize copy engine pipe %d: %d\n",
  2073. i, ret);
  2074. return ret;
  2075. }
  2076. }
  2077. return 0;
  2078. }
  2079. static bool ath10k_pci_has_fw_crashed(struct ath10k *ar)
  2080. {
  2081. return ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS) &
  2082. FW_IND_EVENT_PENDING;
  2083. }
  2084. static void ath10k_pci_fw_crashed_clear(struct ath10k *ar)
  2085. {
  2086. u32 val;
  2087. val = ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS);
  2088. val &= ~FW_IND_EVENT_PENDING;
  2089. ath10k_pci_write32(ar, FW_INDICATOR_ADDRESS, val);
  2090. }
  2091. static bool ath10k_pci_has_device_gone(struct ath10k *ar)
  2092. {
  2093. u32 val;
  2094. val = ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS);
  2095. return (val == 0xffffffff);
  2096. }
  2097. /* this function effectively clears target memory controller assert line */
  2098. static void ath10k_pci_warm_reset_si0(struct ath10k *ar)
  2099. {
  2100. u32 val;
  2101. val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
  2102. ath10k_pci_soc_write32(ar, SOC_RESET_CONTROL_ADDRESS,
  2103. val | SOC_RESET_CONTROL_SI0_RST_MASK);
  2104. val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
  2105. msleep(10);
  2106. val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
  2107. ath10k_pci_soc_write32(ar, SOC_RESET_CONTROL_ADDRESS,
  2108. val & ~SOC_RESET_CONTROL_SI0_RST_MASK);
  2109. val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
  2110. msleep(10);
  2111. }
  2112. static void ath10k_pci_warm_reset_cpu(struct ath10k *ar)
  2113. {
  2114. u32 val;
  2115. ath10k_pci_write32(ar, FW_INDICATOR_ADDRESS, 0);
  2116. val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
  2117. SOC_RESET_CONTROL_ADDRESS);
  2118. ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
  2119. val | SOC_RESET_CONTROL_CPU_WARM_RST_MASK);
  2120. }
  2121. static void ath10k_pci_warm_reset_ce(struct ath10k *ar)
  2122. {
  2123. u32 val;
  2124. val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
  2125. SOC_RESET_CONTROL_ADDRESS);
  2126. ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
  2127. val | SOC_RESET_CONTROL_CE_RST_MASK);
  2128. msleep(10);
  2129. ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
  2130. val & ~SOC_RESET_CONTROL_CE_RST_MASK);
  2131. }
  2132. static void ath10k_pci_warm_reset_clear_lf(struct ath10k *ar)
  2133. {
  2134. u32 val;
  2135. val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
  2136. SOC_LF_TIMER_CONTROL0_ADDRESS);
  2137. ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS +
  2138. SOC_LF_TIMER_CONTROL0_ADDRESS,
  2139. val & ~SOC_LF_TIMER_CONTROL0_ENABLE_MASK);
  2140. }
  2141. static int ath10k_pci_warm_reset(struct ath10k *ar)
  2142. {
  2143. int ret;
  2144. ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot warm reset\n");
  2145. spin_lock_bh(&ar->data_lock);
  2146. ar->stats.fw_warm_reset_counter++;
  2147. spin_unlock_bh(&ar->data_lock);
  2148. ath10k_pci_irq_disable(ar);
  2149. /* Make sure the target CPU is not doing anything dangerous, e.g. if it
  2150. * were to access copy engine while host performs copy engine reset
  2151. * then it is possible for the device to confuse pci-e controller to
  2152. * the point of bringing host system to a complete stop (i.e. hang).
  2153. */
  2154. ath10k_pci_warm_reset_si0(ar);
  2155. ath10k_pci_warm_reset_cpu(ar);
  2156. ath10k_pci_init_pipes(ar);
  2157. ath10k_pci_wait_for_target_init(ar);
  2158. ath10k_pci_warm_reset_clear_lf(ar);
  2159. ath10k_pci_warm_reset_ce(ar);
  2160. ath10k_pci_warm_reset_cpu(ar);
  2161. ath10k_pci_init_pipes(ar);
  2162. ret = ath10k_pci_wait_for_target_init(ar);
  2163. if (ret) {
  2164. ath10k_warn(ar, "failed to wait for target init: %d\n", ret);
  2165. return ret;
  2166. }
  2167. ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot warm reset complete\n");
  2168. return 0;
  2169. }
  2170. static int ath10k_pci_qca99x0_soft_chip_reset(struct ath10k *ar)
  2171. {
  2172. ath10k_pci_irq_disable(ar);
  2173. return ath10k_pci_qca99x0_chip_reset(ar);
  2174. }
  2175. static int ath10k_pci_safe_chip_reset(struct ath10k *ar)
  2176. {
  2177. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  2178. if (!ar_pci->pci_soft_reset)
  2179. return -ENOTSUPP;
  2180. return ar_pci->pci_soft_reset(ar);
  2181. }
  2182. static int ath10k_pci_qca988x_chip_reset(struct ath10k *ar)
  2183. {
  2184. int i, ret;
  2185. u32 val;
  2186. ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot 988x chip reset\n");
  2187. /* Some hardware revisions (e.g. CUS223v2) has issues with cold reset.
  2188. * It is thus preferred to use warm reset which is safer but may not be
  2189. * able to recover the device from all possible fail scenarios.
  2190. *
  2191. * Warm reset doesn't always work on first try so attempt it a few
  2192. * times before giving up.
  2193. */
  2194. for (i = 0; i < ATH10K_PCI_NUM_WARM_RESET_ATTEMPTS; i++) {
  2195. ret = ath10k_pci_warm_reset(ar);
  2196. if (ret) {
  2197. ath10k_warn(ar, "failed to warm reset attempt %d of %d: %d\n",
  2198. i + 1, ATH10K_PCI_NUM_WARM_RESET_ATTEMPTS,
  2199. ret);
  2200. continue;
  2201. }
  2202. /* FIXME: Sometimes copy engine doesn't recover after warm
  2203. * reset. In most cases this needs cold reset. In some of these
  2204. * cases the device is in such a state that a cold reset may
  2205. * lock up the host.
  2206. *
  2207. * Reading any host interest register via copy engine is
  2208. * sufficient to verify if device is capable of booting
  2209. * firmware blob.
  2210. */
  2211. ret = ath10k_pci_init_pipes(ar);
  2212. if (ret) {
  2213. ath10k_warn(ar, "failed to init copy engine: %d\n",
  2214. ret);
  2215. continue;
  2216. }
  2217. ret = ath10k_pci_diag_read32(ar, QCA988X_HOST_INTEREST_ADDRESS,
  2218. &val);
  2219. if (ret) {
  2220. ath10k_warn(ar, "failed to poke copy engine: %d\n",
  2221. ret);
  2222. continue;
  2223. }
  2224. ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot chip reset complete (warm)\n");
  2225. return 0;
  2226. }
  2227. if (ath10k_pci_reset_mode == ATH10K_PCI_RESET_WARM_ONLY) {
  2228. ath10k_warn(ar, "refusing cold reset as requested\n");
  2229. return -EPERM;
  2230. }
  2231. ret = ath10k_pci_cold_reset(ar);
  2232. if (ret) {
  2233. ath10k_warn(ar, "failed to cold reset: %d\n", ret);
  2234. return ret;
  2235. }
  2236. ret = ath10k_pci_wait_for_target_init(ar);
  2237. if (ret) {
  2238. ath10k_warn(ar, "failed to wait for target after cold reset: %d\n",
  2239. ret);
  2240. return ret;
  2241. }
  2242. ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot qca988x chip reset complete (cold)\n");
  2243. return 0;
  2244. }
  2245. static int ath10k_pci_qca6174_chip_reset(struct ath10k *ar)
  2246. {
  2247. int ret;
  2248. ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot qca6174 chip reset\n");
  2249. /* FIXME: QCA6174 requires cold + warm reset to work. */
  2250. ret = ath10k_pci_cold_reset(ar);
  2251. if (ret) {
  2252. ath10k_warn(ar, "failed to cold reset: %d\n", ret);
  2253. return ret;
  2254. }
  2255. ret = ath10k_pci_wait_for_target_init(ar);
  2256. if (ret) {
  2257. ath10k_warn(ar, "failed to wait for target after cold reset: %d\n",
  2258. ret);
  2259. return ret;
  2260. }
  2261. ret = ath10k_pci_warm_reset(ar);
  2262. if (ret) {
  2263. ath10k_warn(ar, "failed to warm reset: %d\n", ret);
  2264. return ret;
  2265. }
  2266. ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot qca6174 chip reset complete (cold)\n");
  2267. return 0;
  2268. }
  2269. static int ath10k_pci_qca99x0_chip_reset(struct ath10k *ar)
  2270. {
  2271. int ret;
  2272. ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot qca99x0 chip reset\n");
  2273. ret = ath10k_pci_cold_reset(ar);
  2274. if (ret) {
  2275. ath10k_warn(ar, "failed to cold reset: %d\n", ret);
  2276. return ret;
  2277. }
  2278. ret = ath10k_pci_wait_for_target_init(ar);
  2279. if (ret) {
  2280. ath10k_warn(ar, "failed to wait for target after cold reset: %d\n",
  2281. ret);
  2282. return ret;
  2283. }
  2284. ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot qca99x0 chip reset complete (cold)\n");
  2285. return 0;
  2286. }
  2287. static int ath10k_pci_chip_reset(struct ath10k *ar)
  2288. {
  2289. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  2290. if (WARN_ON(!ar_pci->pci_hard_reset))
  2291. return -ENOTSUPP;
  2292. return ar_pci->pci_hard_reset(ar);
  2293. }
  2294. static int ath10k_pci_hif_power_up(struct ath10k *ar)
  2295. {
  2296. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  2297. int ret;
  2298. ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif power up\n");
  2299. pcie_capability_read_word(ar_pci->pdev, PCI_EXP_LNKCTL,
  2300. &ar_pci->link_ctl);
  2301. pcie_capability_write_word(ar_pci->pdev, PCI_EXP_LNKCTL,
  2302. ar_pci->link_ctl & ~PCI_EXP_LNKCTL_ASPMC);
  2303. /*
  2304. * Bring the target up cleanly.
  2305. *
  2306. * The target may be in an undefined state with an AUX-powered Target
  2307. * and a Host in WoW mode. If the Host crashes, loses power, or is
  2308. * restarted (without unloading the driver) then the Target is left
  2309. * (aux) powered and running. On a subsequent driver load, the Target
  2310. * is in an unexpected state. We try to catch that here in order to
  2311. * reset the Target and retry the probe.
  2312. */
  2313. ret = ath10k_pci_chip_reset(ar);
  2314. if (ret) {
  2315. if (ath10k_pci_has_fw_crashed(ar)) {
  2316. ath10k_warn(ar, "firmware crashed during chip reset\n");
  2317. ath10k_pci_fw_crashed_clear(ar);
  2318. ath10k_pci_fw_crashed_dump(ar);
  2319. }
  2320. ath10k_err(ar, "failed to reset chip: %d\n", ret);
  2321. goto err_sleep;
  2322. }
  2323. ret = ath10k_pci_init_pipes(ar);
  2324. if (ret) {
  2325. ath10k_err(ar, "failed to initialize CE: %d\n", ret);
  2326. goto err_sleep;
  2327. }
  2328. ret = ath10k_pci_init_config(ar);
  2329. if (ret) {
  2330. ath10k_err(ar, "failed to setup init config: %d\n", ret);
  2331. goto err_ce;
  2332. }
  2333. ret = ath10k_pci_wake_target_cpu(ar);
  2334. if (ret) {
  2335. ath10k_err(ar, "could not wake up target CPU: %d\n", ret);
  2336. goto err_ce;
  2337. }
  2338. return 0;
  2339. err_ce:
  2340. ath10k_pci_ce_deinit(ar);
  2341. err_sleep:
  2342. return ret;
  2343. }
  2344. void ath10k_pci_hif_power_down(struct ath10k *ar)
  2345. {
  2346. ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif power down\n");
  2347. /* Currently hif_power_up performs effectively a reset and hif_stop
  2348. * resets the chip as well so there's no point in resetting here.
  2349. */
  2350. }
  2351. static int ath10k_pci_hif_suspend(struct ath10k *ar)
  2352. {
  2353. /* Nothing to do; the important stuff is in the driver suspend. */
  2354. return 0;
  2355. }
  2356. static int ath10k_pci_suspend(struct ath10k *ar)
  2357. {
  2358. /* The grace timer can still be counting down and ar->ps_awake be true.
  2359. * It is known that the device may be asleep after resuming regardless
  2360. * of the SoC powersave state before suspending. Hence make sure the
  2361. * device is asleep before proceeding.
  2362. */
  2363. ath10k_pci_sleep_sync(ar);
  2364. return 0;
  2365. }
  2366. static int ath10k_pci_hif_resume(struct ath10k *ar)
  2367. {
  2368. /* Nothing to do; the important stuff is in the driver resume. */
  2369. return 0;
  2370. }
  2371. static int ath10k_pci_resume(struct ath10k *ar)
  2372. {
  2373. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  2374. struct pci_dev *pdev = ar_pci->pdev;
  2375. u32 val;
  2376. int ret = 0;
  2377. ret = ath10k_pci_force_wake(ar);
  2378. if (ret) {
  2379. ath10k_err(ar, "failed to wake up target: %d\n", ret);
  2380. return ret;
  2381. }
  2382. /* Suspend/Resume resets the PCI configuration space, so we have to
  2383. * re-disable the RETRY_TIMEOUT register (0x41) to keep PCI Tx retries
  2384. * from interfering with C3 CPU state. pci_restore_state won't help
  2385. * here since it only restores the first 64 bytes pci config header.
  2386. */
  2387. pci_read_config_dword(pdev, 0x40, &val);
  2388. if ((val & 0x0000ff00) != 0)
  2389. pci_write_config_dword(pdev, 0x40, val & 0xffff00ff);
  2390. return ret;
  2391. }
  2392. static bool ath10k_pci_validate_cal(void *data, size_t size)
  2393. {
  2394. __le16 *cal_words = data;
  2395. u16 checksum = 0;
  2396. size_t i;
  2397. if (size % 2 != 0)
  2398. return false;
  2399. for (i = 0; i < size / 2; i++)
  2400. checksum ^= le16_to_cpu(cal_words[i]);
  2401. return checksum == 0xffff;
  2402. }
  2403. static void ath10k_pci_enable_eeprom(struct ath10k *ar)
  2404. {
  2405. /* Enable SI clock */
  2406. ath10k_pci_soc_write32(ar, CLOCK_CONTROL_OFFSET, 0x0);
  2407. /* Configure GPIOs for I2C operation */
  2408. ath10k_pci_write32(ar,
  2409. GPIO_BASE_ADDRESS + GPIO_PIN0_OFFSET +
  2410. 4 * QCA9887_1_0_I2C_SDA_GPIO_PIN,
  2411. SM(QCA9887_1_0_I2C_SDA_PIN_CONFIG,
  2412. GPIO_PIN0_CONFIG) |
  2413. SM(1, GPIO_PIN0_PAD_PULL));
  2414. ath10k_pci_write32(ar,
  2415. GPIO_BASE_ADDRESS + GPIO_PIN0_OFFSET +
  2416. 4 * QCA9887_1_0_SI_CLK_GPIO_PIN,
  2417. SM(QCA9887_1_0_SI_CLK_PIN_CONFIG, GPIO_PIN0_CONFIG) |
  2418. SM(1, GPIO_PIN0_PAD_PULL));
  2419. ath10k_pci_write32(ar,
  2420. GPIO_BASE_ADDRESS +
  2421. QCA9887_1_0_GPIO_ENABLE_W1TS_LOW_ADDRESS,
  2422. 1u << QCA9887_1_0_SI_CLK_GPIO_PIN);
  2423. /* In Swift ASIC - EEPROM clock will be (110MHz/512) = 214KHz */
  2424. ath10k_pci_write32(ar,
  2425. SI_BASE_ADDRESS + SI_CONFIG_OFFSET,
  2426. SM(1, SI_CONFIG_ERR_INT) |
  2427. SM(1, SI_CONFIG_BIDIR_OD_DATA) |
  2428. SM(1, SI_CONFIG_I2C) |
  2429. SM(1, SI_CONFIG_POS_SAMPLE) |
  2430. SM(1, SI_CONFIG_INACTIVE_DATA) |
  2431. SM(1, SI_CONFIG_INACTIVE_CLK) |
  2432. SM(8, SI_CONFIG_DIVIDER));
  2433. }
  2434. static int ath10k_pci_read_eeprom(struct ath10k *ar, u16 addr, u8 *out)
  2435. {
  2436. u32 reg;
  2437. int wait_limit;
  2438. /* set device select byte and for the read operation */
  2439. reg = QCA9887_EEPROM_SELECT_READ |
  2440. SM(addr, QCA9887_EEPROM_ADDR_LO) |
  2441. SM(addr >> 8, QCA9887_EEPROM_ADDR_HI);
  2442. ath10k_pci_write32(ar, SI_BASE_ADDRESS + SI_TX_DATA0_OFFSET, reg);
  2443. /* write transmit data, transfer length, and START bit */
  2444. ath10k_pci_write32(ar, SI_BASE_ADDRESS + SI_CS_OFFSET,
  2445. SM(1, SI_CS_START) | SM(1, SI_CS_RX_CNT) |
  2446. SM(4, SI_CS_TX_CNT));
  2447. /* wait max 1 sec */
  2448. wait_limit = 100000;
  2449. /* wait for SI_CS_DONE_INT */
  2450. do {
  2451. reg = ath10k_pci_read32(ar, SI_BASE_ADDRESS + SI_CS_OFFSET);
  2452. if (MS(reg, SI_CS_DONE_INT))
  2453. break;
  2454. wait_limit--;
  2455. udelay(10);
  2456. } while (wait_limit > 0);
  2457. if (!MS(reg, SI_CS_DONE_INT)) {
  2458. ath10k_err(ar, "timeout while reading device EEPROM at %04x\n",
  2459. addr);
  2460. return -ETIMEDOUT;
  2461. }
  2462. /* clear SI_CS_DONE_INT */
  2463. ath10k_pci_write32(ar, SI_BASE_ADDRESS + SI_CS_OFFSET, reg);
  2464. if (MS(reg, SI_CS_DONE_ERR)) {
  2465. ath10k_err(ar, "failed to read device EEPROM at %04x\n", addr);
  2466. return -EIO;
  2467. }
  2468. /* extract receive data */
  2469. reg = ath10k_pci_read32(ar, SI_BASE_ADDRESS + SI_RX_DATA0_OFFSET);
  2470. *out = reg;
  2471. return 0;
  2472. }
  2473. static int ath10k_pci_hif_fetch_cal_eeprom(struct ath10k *ar, void **data,
  2474. size_t *data_len)
  2475. {
  2476. u8 *caldata = NULL;
  2477. size_t calsize, i;
  2478. int ret;
  2479. if (!QCA_REV_9887(ar))
  2480. return -EOPNOTSUPP;
  2481. calsize = ar->hw_params.cal_data_len;
  2482. caldata = kmalloc(calsize, GFP_KERNEL);
  2483. if (!caldata)
  2484. return -ENOMEM;
  2485. ath10k_pci_enable_eeprom(ar);
  2486. for (i = 0; i < calsize; i++) {
  2487. ret = ath10k_pci_read_eeprom(ar, i, &caldata[i]);
  2488. if (ret)
  2489. goto err_free;
  2490. }
  2491. if (!ath10k_pci_validate_cal(caldata, calsize))
  2492. goto err_free;
  2493. *data = caldata;
  2494. *data_len = calsize;
  2495. return 0;
  2496. err_free:
  2497. kfree(caldata);
  2498. return -EINVAL;
  2499. }
  2500. static const struct ath10k_hif_ops ath10k_pci_hif_ops = {
  2501. .tx_sg = ath10k_pci_hif_tx_sg,
  2502. .diag_read = ath10k_pci_hif_diag_read,
  2503. .diag_write = ath10k_pci_diag_write_mem,
  2504. .exchange_bmi_msg = ath10k_pci_hif_exchange_bmi_msg,
  2505. .start = ath10k_pci_hif_start,
  2506. .stop = ath10k_pci_hif_stop,
  2507. .map_service_to_pipe = ath10k_pci_hif_map_service_to_pipe,
  2508. .get_default_pipe = ath10k_pci_hif_get_default_pipe,
  2509. .send_complete_check = ath10k_pci_hif_send_complete_check,
  2510. .get_free_queue_number = ath10k_pci_hif_get_free_queue_number,
  2511. .power_up = ath10k_pci_hif_power_up,
  2512. .power_down = ath10k_pci_hif_power_down,
  2513. .read32 = ath10k_pci_read32,
  2514. .write32 = ath10k_pci_write32,
  2515. .suspend = ath10k_pci_hif_suspend,
  2516. .resume = ath10k_pci_hif_resume,
  2517. .fetch_cal_eeprom = ath10k_pci_hif_fetch_cal_eeprom,
  2518. };
  2519. /*
  2520. * Top-level interrupt handler for all PCI interrupts from a Target.
  2521. * When a block of MSI interrupts is allocated, this top-level handler
  2522. * is not used; instead, we directly call the correct sub-handler.
  2523. */
  2524. static irqreturn_t ath10k_pci_interrupt_handler(int irq, void *arg)
  2525. {
  2526. struct ath10k *ar = arg;
  2527. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  2528. int ret;
  2529. if (ath10k_pci_has_device_gone(ar))
  2530. return IRQ_NONE;
  2531. ret = ath10k_pci_force_wake(ar);
  2532. if (ret) {
  2533. ath10k_warn(ar, "failed to wake device up on irq: %d\n", ret);
  2534. return IRQ_NONE;
  2535. }
  2536. if ((ar_pci->oper_irq_mode == ATH10K_PCI_IRQ_LEGACY) &&
  2537. !ath10k_pci_irq_pending(ar))
  2538. return IRQ_NONE;
  2539. ath10k_pci_disable_and_clear_legacy_irq(ar);
  2540. ath10k_pci_irq_msi_fw_mask(ar);
  2541. napi_schedule(&ar->napi);
  2542. return IRQ_HANDLED;
  2543. }
  2544. static int ath10k_pci_napi_poll(struct napi_struct *ctx, int budget)
  2545. {
  2546. struct ath10k *ar = container_of(ctx, struct ath10k, napi);
  2547. int done = 0;
  2548. if (ath10k_pci_has_fw_crashed(ar)) {
  2549. ath10k_pci_fw_crashed_clear(ar);
  2550. ath10k_pci_fw_crashed_dump(ar);
  2551. napi_complete(ctx);
  2552. return done;
  2553. }
  2554. ath10k_ce_per_engine_service_any(ar);
  2555. done = ath10k_htt_txrx_compl_task(ar, budget);
  2556. if (done < budget) {
  2557. napi_complete_done(ctx, done);
  2558. /* In case of MSI, it is possible that interrupts are received
  2559. * while NAPI poll is inprogress. So pending interrupts that are
  2560. * received after processing all copy engine pipes by NAPI poll
  2561. * will not be handled again. This is causing failure to
  2562. * complete boot sequence in x86 platform. So before enabling
  2563. * interrupts safer to check for pending interrupts for
  2564. * immediate servicing.
  2565. */
  2566. if (ath10k_ce_interrupt_summary(ar)) {
  2567. napi_reschedule(ctx);
  2568. goto out;
  2569. }
  2570. ath10k_pci_enable_legacy_irq(ar);
  2571. ath10k_pci_irq_msi_fw_unmask(ar);
  2572. }
  2573. out:
  2574. return done;
  2575. }
  2576. static int ath10k_pci_request_irq_msi(struct ath10k *ar)
  2577. {
  2578. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  2579. int ret;
  2580. ret = request_irq(ar_pci->pdev->irq,
  2581. ath10k_pci_interrupt_handler,
  2582. IRQF_SHARED, "ath10k_pci", ar);
  2583. if (ret) {
  2584. ath10k_warn(ar, "failed to request MSI irq %d: %d\n",
  2585. ar_pci->pdev->irq, ret);
  2586. return ret;
  2587. }
  2588. return 0;
  2589. }
  2590. static int ath10k_pci_request_irq_legacy(struct ath10k *ar)
  2591. {
  2592. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  2593. int ret;
  2594. ret = request_irq(ar_pci->pdev->irq,
  2595. ath10k_pci_interrupt_handler,
  2596. IRQF_SHARED, "ath10k_pci", ar);
  2597. if (ret) {
  2598. ath10k_warn(ar, "failed to request legacy irq %d: %d\n",
  2599. ar_pci->pdev->irq, ret);
  2600. return ret;
  2601. }
  2602. return 0;
  2603. }
  2604. static int ath10k_pci_request_irq(struct ath10k *ar)
  2605. {
  2606. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  2607. switch (ar_pci->oper_irq_mode) {
  2608. case ATH10K_PCI_IRQ_LEGACY:
  2609. return ath10k_pci_request_irq_legacy(ar);
  2610. case ATH10K_PCI_IRQ_MSI:
  2611. return ath10k_pci_request_irq_msi(ar);
  2612. default:
  2613. return -EINVAL;
  2614. }
  2615. }
  2616. static void ath10k_pci_free_irq(struct ath10k *ar)
  2617. {
  2618. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  2619. free_irq(ar_pci->pdev->irq, ar);
  2620. }
  2621. void ath10k_pci_init_napi(struct ath10k *ar)
  2622. {
  2623. netif_napi_add(&ar->napi_dev, &ar->napi, ath10k_pci_napi_poll,
  2624. ATH10K_NAPI_BUDGET);
  2625. }
  2626. static int ath10k_pci_init_irq(struct ath10k *ar)
  2627. {
  2628. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  2629. int ret;
  2630. ath10k_pci_init_napi(ar);
  2631. if (ath10k_pci_irq_mode != ATH10K_PCI_IRQ_AUTO)
  2632. ath10k_info(ar, "limiting irq mode to: %d\n",
  2633. ath10k_pci_irq_mode);
  2634. /* Try MSI */
  2635. if (ath10k_pci_irq_mode != ATH10K_PCI_IRQ_LEGACY) {
  2636. ar_pci->oper_irq_mode = ATH10K_PCI_IRQ_MSI;
  2637. ret = pci_enable_msi(ar_pci->pdev);
  2638. if (ret == 0)
  2639. return 0;
  2640. /* fall-through */
  2641. }
  2642. /* Try legacy irq
  2643. *
  2644. * A potential race occurs here: The CORE_BASE write
  2645. * depends on target correctly decoding AXI address but
  2646. * host won't know when target writes BAR to CORE_CTRL.
  2647. * This write might get lost if target has NOT written BAR.
  2648. * For now, fix the race by repeating the write in below
  2649. * synchronization checking.
  2650. */
  2651. ar_pci->oper_irq_mode = ATH10K_PCI_IRQ_LEGACY;
  2652. ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
  2653. PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL);
  2654. return 0;
  2655. }
  2656. static void ath10k_pci_deinit_irq_legacy(struct ath10k *ar)
  2657. {
  2658. ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
  2659. 0);
  2660. }
  2661. static int ath10k_pci_deinit_irq(struct ath10k *ar)
  2662. {
  2663. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  2664. switch (ar_pci->oper_irq_mode) {
  2665. case ATH10K_PCI_IRQ_LEGACY:
  2666. ath10k_pci_deinit_irq_legacy(ar);
  2667. break;
  2668. default:
  2669. pci_disable_msi(ar_pci->pdev);
  2670. break;
  2671. }
  2672. return 0;
  2673. }
  2674. int ath10k_pci_wait_for_target_init(struct ath10k *ar)
  2675. {
  2676. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  2677. unsigned long timeout;
  2678. u32 val;
  2679. ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot waiting target to initialise\n");
  2680. timeout = jiffies + msecs_to_jiffies(ATH10K_PCI_TARGET_WAIT);
  2681. do {
  2682. val = ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS);
  2683. ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot target indicator %x\n",
  2684. val);
  2685. /* target should never return this */
  2686. if (val == 0xffffffff)
  2687. continue;
  2688. /* the device has crashed so don't bother trying anymore */
  2689. if (val & FW_IND_EVENT_PENDING)
  2690. break;
  2691. if (val & FW_IND_INITIALIZED)
  2692. break;
  2693. if (ar_pci->oper_irq_mode == ATH10K_PCI_IRQ_LEGACY)
  2694. /* Fix potential race by repeating CORE_BASE writes */
  2695. ath10k_pci_enable_legacy_irq(ar);
  2696. mdelay(10);
  2697. } while (time_before(jiffies, timeout));
  2698. ath10k_pci_disable_and_clear_legacy_irq(ar);
  2699. ath10k_pci_irq_msi_fw_mask(ar);
  2700. if (val == 0xffffffff) {
  2701. ath10k_err(ar, "failed to read device register, device is gone\n");
  2702. return -EIO;
  2703. }
  2704. if (val & FW_IND_EVENT_PENDING) {
  2705. ath10k_warn(ar, "device has crashed during init\n");
  2706. return -ECOMM;
  2707. }
  2708. if (!(val & FW_IND_INITIALIZED)) {
  2709. ath10k_err(ar, "failed to receive initialized event from target: %08x\n",
  2710. val);
  2711. return -ETIMEDOUT;
  2712. }
  2713. ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot target initialised\n");
  2714. return 0;
  2715. }
  2716. static int ath10k_pci_cold_reset(struct ath10k *ar)
  2717. {
  2718. u32 val;
  2719. ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot cold reset\n");
  2720. spin_lock_bh(&ar->data_lock);
  2721. ar->stats.fw_cold_reset_counter++;
  2722. spin_unlock_bh(&ar->data_lock);
  2723. /* Put Target, including PCIe, into RESET. */
  2724. val = ath10k_pci_reg_read32(ar, SOC_GLOBAL_RESET_ADDRESS);
  2725. val |= 1;
  2726. ath10k_pci_reg_write32(ar, SOC_GLOBAL_RESET_ADDRESS, val);
  2727. /* After writing into SOC_GLOBAL_RESET to put device into
  2728. * reset and pulling out of reset pcie may not be stable
  2729. * for any immediate pcie register access and cause bus error,
  2730. * add delay before any pcie access request to fix this issue.
  2731. */
  2732. msleep(20);
  2733. /* Pull Target, including PCIe, out of RESET. */
  2734. val &= ~1;
  2735. ath10k_pci_reg_write32(ar, SOC_GLOBAL_RESET_ADDRESS, val);
  2736. msleep(20);
  2737. ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot cold reset complete\n");
  2738. return 0;
  2739. }
  2740. static int ath10k_pci_claim(struct ath10k *ar)
  2741. {
  2742. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  2743. struct pci_dev *pdev = ar_pci->pdev;
  2744. int ret;
  2745. pci_set_drvdata(pdev, ar);
  2746. ret = pci_enable_device(pdev);
  2747. if (ret) {
  2748. ath10k_err(ar, "failed to enable pci device: %d\n", ret);
  2749. return ret;
  2750. }
  2751. ret = pci_request_region(pdev, BAR_NUM, "ath");
  2752. if (ret) {
  2753. ath10k_err(ar, "failed to request region BAR%d: %d\n", BAR_NUM,
  2754. ret);
  2755. goto err_device;
  2756. }
  2757. /* Target expects 32 bit DMA. Enforce it. */
  2758. ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  2759. if (ret) {
  2760. ath10k_err(ar, "failed to set dma mask to 32-bit: %d\n", ret);
  2761. goto err_region;
  2762. }
  2763. ret = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  2764. if (ret) {
  2765. ath10k_err(ar, "failed to set consistent dma mask to 32-bit: %d\n",
  2766. ret);
  2767. goto err_region;
  2768. }
  2769. pci_set_master(pdev);
  2770. /* Arrange for access to Target SoC registers. */
  2771. ar_pci->mem_len = pci_resource_len(pdev, BAR_NUM);
  2772. ar_pci->mem = pci_iomap(pdev, BAR_NUM, 0);
  2773. if (!ar_pci->mem) {
  2774. ath10k_err(ar, "failed to iomap BAR%d\n", BAR_NUM);
  2775. ret = -EIO;
  2776. goto err_master;
  2777. }
  2778. ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot pci_mem 0x%pK\n", ar_pci->mem);
  2779. return 0;
  2780. err_master:
  2781. pci_clear_master(pdev);
  2782. err_region:
  2783. pci_release_region(pdev, BAR_NUM);
  2784. err_device:
  2785. pci_disable_device(pdev);
  2786. return ret;
  2787. }
  2788. static void ath10k_pci_release(struct ath10k *ar)
  2789. {
  2790. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  2791. struct pci_dev *pdev = ar_pci->pdev;
  2792. pci_iounmap(pdev, ar_pci->mem);
  2793. pci_release_region(pdev, BAR_NUM);
  2794. pci_clear_master(pdev);
  2795. pci_disable_device(pdev);
  2796. }
  2797. static bool ath10k_pci_chip_is_supported(u32 dev_id, u32 chip_id)
  2798. {
  2799. const struct ath10k_pci_supp_chip *supp_chip;
  2800. int i;
  2801. u32 rev_id = MS(chip_id, SOC_CHIP_ID_REV);
  2802. for (i = 0; i < ARRAY_SIZE(ath10k_pci_supp_chips); i++) {
  2803. supp_chip = &ath10k_pci_supp_chips[i];
  2804. if (supp_chip->dev_id == dev_id &&
  2805. supp_chip->rev_id == rev_id)
  2806. return true;
  2807. }
  2808. return false;
  2809. }
  2810. int ath10k_pci_setup_resource(struct ath10k *ar)
  2811. {
  2812. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  2813. struct ath10k_ce *ce = ath10k_ce_priv(ar);
  2814. int ret;
  2815. spin_lock_init(&ce->ce_lock);
  2816. spin_lock_init(&ar_pci->ps_lock);
  2817. timer_setup(&ar_pci->rx_post_retry, ath10k_pci_rx_replenish_retry, 0);
  2818. if (QCA_REV_6174(ar) || QCA_REV_9377(ar))
  2819. ath10k_pci_override_ce_config(ar);
  2820. ret = ath10k_pci_alloc_pipes(ar);
  2821. if (ret) {
  2822. ath10k_err(ar, "failed to allocate copy engine pipes: %d\n",
  2823. ret);
  2824. return ret;
  2825. }
  2826. return 0;
  2827. }
  2828. void ath10k_pci_release_resource(struct ath10k *ar)
  2829. {
  2830. ath10k_pci_rx_retry_sync(ar);
  2831. netif_napi_del(&ar->napi);
  2832. ath10k_pci_ce_deinit(ar);
  2833. ath10k_pci_free_pipes(ar);
  2834. }
  2835. static const struct ath10k_bus_ops ath10k_pci_bus_ops = {
  2836. .read32 = ath10k_bus_pci_read32,
  2837. .write32 = ath10k_bus_pci_write32,
  2838. .get_num_banks = ath10k_pci_get_num_banks,
  2839. };
  2840. static int ath10k_pci_probe(struct pci_dev *pdev,
  2841. const struct pci_device_id *pci_dev)
  2842. {
  2843. int ret = 0;
  2844. struct ath10k *ar;
  2845. struct ath10k_pci *ar_pci;
  2846. enum ath10k_hw_rev hw_rev;
  2847. u32 chip_id;
  2848. bool pci_ps;
  2849. int (*pci_soft_reset)(struct ath10k *ar);
  2850. int (*pci_hard_reset)(struct ath10k *ar);
  2851. u32 (*targ_cpu_to_ce_addr)(struct ath10k *ar, u32 addr);
  2852. switch (pci_dev->device) {
  2853. case QCA988X_2_0_DEVICE_ID_UBNT:
  2854. case QCA988X_2_0_DEVICE_ID:
  2855. hw_rev = ATH10K_HW_QCA988X;
  2856. pci_ps = false;
  2857. pci_soft_reset = ath10k_pci_warm_reset;
  2858. pci_hard_reset = ath10k_pci_qca988x_chip_reset;
  2859. targ_cpu_to_ce_addr = ath10k_pci_qca988x_targ_cpu_to_ce_addr;
  2860. break;
  2861. case QCA9887_1_0_DEVICE_ID:
  2862. hw_rev = ATH10K_HW_QCA9887;
  2863. pci_ps = false;
  2864. pci_soft_reset = ath10k_pci_warm_reset;
  2865. pci_hard_reset = ath10k_pci_qca988x_chip_reset;
  2866. targ_cpu_to_ce_addr = ath10k_pci_qca988x_targ_cpu_to_ce_addr;
  2867. break;
  2868. case QCA6164_2_1_DEVICE_ID:
  2869. case QCA6174_2_1_DEVICE_ID:
  2870. hw_rev = ATH10K_HW_QCA6174;
  2871. pci_ps = true;
  2872. pci_soft_reset = ath10k_pci_warm_reset;
  2873. pci_hard_reset = ath10k_pci_qca6174_chip_reset;
  2874. targ_cpu_to_ce_addr = ath10k_pci_qca988x_targ_cpu_to_ce_addr;
  2875. break;
  2876. case QCA99X0_2_0_DEVICE_ID:
  2877. hw_rev = ATH10K_HW_QCA99X0;
  2878. pci_ps = false;
  2879. pci_soft_reset = ath10k_pci_qca99x0_soft_chip_reset;
  2880. pci_hard_reset = ath10k_pci_qca99x0_chip_reset;
  2881. targ_cpu_to_ce_addr = ath10k_pci_qca99x0_targ_cpu_to_ce_addr;
  2882. break;
  2883. case QCA9984_1_0_DEVICE_ID:
  2884. hw_rev = ATH10K_HW_QCA9984;
  2885. pci_ps = false;
  2886. pci_soft_reset = ath10k_pci_qca99x0_soft_chip_reset;
  2887. pci_hard_reset = ath10k_pci_qca99x0_chip_reset;
  2888. targ_cpu_to_ce_addr = ath10k_pci_qca99x0_targ_cpu_to_ce_addr;
  2889. break;
  2890. case QCA9888_2_0_DEVICE_ID:
  2891. hw_rev = ATH10K_HW_QCA9888;
  2892. pci_ps = false;
  2893. pci_soft_reset = ath10k_pci_qca99x0_soft_chip_reset;
  2894. pci_hard_reset = ath10k_pci_qca99x0_chip_reset;
  2895. targ_cpu_to_ce_addr = ath10k_pci_qca99x0_targ_cpu_to_ce_addr;
  2896. break;
  2897. case QCA9377_1_0_DEVICE_ID:
  2898. hw_rev = ATH10K_HW_QCA9377;
  2899. pci_ps = true;
  2900. pci_soft_reset = NULL;
  2901. pci_hard_reset = ath10k_pci_qca6174_chip_reset;
  2902. targ_cpu_to_ce_addr = ath10k_pci_qca988x_targ_cpu_to_ce_addr;
  2903. break;
  2904. default:
  2905. WARN_ON(1);
  2906. return -ENOTSUPP;
  2907. }
  2908. ar = ath10k_core_create(sizeof(*ar_pci), &pdev->dev, ATH10K_BUS_PCI,
  2909. hw_rev, &ath10k_pci_hif_ops);
  2910. if (!ar) {
  2911. dev_err(&pdev->dev, "failed to allocate core\n");
  2912. return -ENOMEM;
  2913. }
  2914. ath10k_dbg(ar, ATH10K_DBG_BOOT, "pci probe %04x:%04x %04x:%04x\n",
  2915. pdev->vendor, pdev->device,
  2916. pdev->subsystem_vendor, pdev->subsystem_device);
  2917. ar_pci = ath10k_pci_priv(ar);
  2918. ar_pci->pdev = pdev;
  2919. ar_pci->dev = &pdev->dev;
  2920. ar_pci->ar = ar;
  2921. ar->dev_id = pci_dev->device;
  2922. ar_pci->pci_ps = pci_ps;
  2923. ar_pci->ce.bus_ops = &ath10k_pci_bus_ops;
  2924. ar_pci->pci_soft_reset = pci_soft_reset;
  2925. ar_pci->pci_hard_reset = pci_hard_reset;
  2926. ar_pci->targ_cpu_to_ce_addr = targ_cpu_to_ce_addr;
  2927. ar->ce_priv = &ar_pci->ce;
  2928. ar->id.vendor = pdev->vendor;
  2929. ar->id.device = pdev->device;
  2930. ar->id.subsystem_vendor = pdev->subsystem_vendor;
  2931. ar->id.subsystem_device = pdev->subsystem_device;
  2932. timer_setup(&ar_pci->ps_timer, ath10k_pci_ps_timer, 0);
  2933. ret = ath10k_pci_setup_resource(ar);
  2934. if (ret) {
  2935. ath10k_err(ar, "failed to setup resource: %d\n", ret);
  2936. goto err_core_destroy;
  2937. }
  2938. ret = ath10k_pci_claim(ar);
  2939. if (ret) {
  2940. ath10k_err(ar, "failed to claim device: %d\n", ret);
  2941. goto err_free_pipes;
  2942. }
  2943. ret = ath10k_pci_force_wake(ar);
  2944. if (ret) {
  2945. ath10k_warn(ar, "failed to wake up device : %d\n", ret);
  2946. goto err_sleep;
  2947. }
  2948. ath10k_pci_ce_deinit(ar);
  2949. ath10k_pci_irq_disable(ar);
  2950. ret = ath10k_pci_init_irq(ar);
  2951. if (ret) {
  2952. ath10k_err(ar, "failed to init irqs: %d\n", ret);
  2953. goto err_sleep;
  2954. }
  2955. ath10k_info(ar, "pci irq %s oper_irq_mode %d irq_mode %d reset_mode %d\n",
  2956. ath10k_pci_get_irq_method(ar), ar_pci->oper_irq_mode,
  2957. ath10k_pci_irq_mode, ath10k_pci_reset_mode);
  2958. ret = ath10k_pci_request_irq(ar);
  2959. if (ret) {
  2960. ath10k_warn(ar, "failed to request irqs: %d\n", ret);
  2961. goto err_deinit_irq;
  2962. }
  2963. ret = ath10k_pci_chip_reset(ar);
  2964. if (ret) {
  2965. ath10k_err(ar, "failed to reset chip: %d\n", ret);
  2966. goto err_free_irq;
  2967. }
  2968. chip_id = ath10k_pci_soc_read32(ar, SOC_CHIP_ID_ADDRESS);
  2969. if (chip_id == 0xffffffff) {
  2970. ath10k_err(ar, "failed to get chip id\n");
  2971. goto err_free_irq;
  2972. }
  2973. if (!ath10k_pci_chip_is_supported(pdev->device, chip_id)) {
  2974. ath10k_err(ar, "device %04x with chip_id %08x isn't supported\n",
  2975. pdev->device, chip_id);
  2976. goto err_free_irq;
  2977. }
  2978. ret = ath10k_core_register(ar, chip_id);
  2979. if (ret) {
  2980. ath10k_err(ar, "failed to register driver core: %d\n", ret);
  2981. goto err_free_irq;
  2982. }
  2983. return 0;
  2984. err_free_irq:
  2985. ath10k_pci_free_irq(ar);
  2986. ath10k_pci_rx_retry_sync(ar);
  2987. err_deinit_irq:
  2988. ath10k_pci_deinit_irq(ar);
  2989. err_sleep:
  2990. ath10k_pci_sleep_sync(ar);
  2991. ath10k_pci_release(ar);
  2992. err_free_pipes:
  2993. ath10k_pci_free_pipes(ar);
  2994. err_core_destroy:
  2995. ath10k_core_destroy(ar);
  2996. return ret;
  2997. }
  2998. static void ath10k_pci_remove(struct pci_dev *pdev)
  2999. {
  3000. struct ath10k *ar = pci_get_drvdata(pdev);
  3001. struct ath10k_pci *ar_pci;
  3002. ath10k_dbg(ar, ATH10K_DBG_PCI, "pci remove\n");
  3003. if (!ar)
  3004. return;
  3005. ar_pci = ath10k_pci_priv(ar);
  3006. if (!ar_pci)
  3007. return;
  3008. ath10k_core_unregister(ar);
  3009. ath10k_pci_free_irq(ar);
  3010. ath10k_pci_deinit_irq(ar);
  3011. ath10k_pci_release_resource(ar);
  3012. ath10k_pci_sleep_sync(ar);
  3013. ath10k_pci_release(ar);
  3014. ath10k_core_destroy(ar);
  3015. }
  3016. MODULE_DEVICE_TABLE(pci, ath10k_pci_id_table);
  3017. static __maybe_unused int ath10k_pci_pm_suspend(struct device *dev)
  3018. {
  3019. struct ath10k *ar = dev_get_drvdata(dev);
  3020. int ret;
  3021. ret = ath10k_pci_suspend(ar);
  3022. if (ret)
  3023. ath10k_warn(ar, "failed to suspend hif: %d\n", ret);
  3024. return ret;
  3025. }
  3026. static __maybe_unused int ath10k_pci_pm_resume(struct device *dev)
  3027. {
  3028. struct ath10k *ar = dev_get_drvdata(dev);
  3029. int ret;
  3030. ret = ath10k_pci_resume(ar);
  3031. if (ret)
  3032. ath10k_warn(ar, "failed to resume hif: %d\n", ret);
  3033. return ret;
  3034. }
  3035. static SIMPLE_DEV_PM_OPS(ath10k_pci_pm_ops,
  3036. ath10k_pci_pm_suspend,
  3037. ath10k_pci_pm_resume);
  3038. static struct pci_driver ath10k_pci_driver = {
  3039. .name = "ath10k_pci",
  3040. .id_table = ath10k_pci_id_table,
  3041. .probe = ath10k_pci_probe,
  3042. .remove = ath10k_pci_remove,
  3043. #ifdef CONFIG_PM
  3044. .driver.pm = &ath10k_pci_pm_ops,
  3045. #endif
  3046. };
  3047. static int __init ath10k_pci_init(void)
  3048. {
  3049. int ret;
  3050. ret = pci_register_driver(&ath10k_pci_driver);
  3051. if (ret)
  3052. printk(KERN_ERR "failed to register ath10k pci driver: %d\n",
  3053. ret);
  3054. ret = ath10k_ahb_init();
  3055. if (ret)
  3056. printk(KERN_ERR "ahb init failed: %d\n", ret);
  3057. return ret;
  3058. }
  3059. module_init(ath10k_pci_init);
  3060. static void __exit ath10k_pci_exit(void)
  3061. {
  3062. pci_unregister_driver(&ath10k_pci_driver);
  3063. ath10k_ahb_exit();
  3064. }
  3065. module_exit(ath10k_pci_exit);
  3066. MODULE_AUTHOR("Qualcomm Atheros");
  3067. MODULE_DESCRIPTION("Driver support for Qualcomm Atheros 802.11ac WLAN PCIe/AHB devices");
  3068. MODULE_LICENSE("Dual BSD/GPL");
  3069. /* QCA988x 2.0 firmware files */
  3070. MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" ATH10K_FW_API2_FILE);
  3071. MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" ATH10K_FW_API3_FILE);
  3072. MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" ATH10K_FW_API4_FILE);
  3073. MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" ATH10K_FW_API5_FILE);
  3074. MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" QCA988X_HW_2_0_BOARD_DATA_FILE);
  3075. MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" ATH10K_BOARD_API2_FILE);
  3076. /* QCA9887 1.0 firmware files */
  3077. MODULE_FIRMWARE(QCA9887_HW_1_0_FW_DIR "/" ATH10K_FW_API5_FILE);
  3078. MODULE_FIRMWARE(QCA9887_HW_1_0_FW_DIR "/" QCA9887_HW_1_0_BOARD_DATA_FILE);
  3079. MODULE_FIRMWARE(QCA9887_HW_1_0_FW_DIR "/" ATH10K_BOARD_API2_FILE);
  3080. /* QCA6174 2.1 firmware files */
  3081. MODULE_FIRMWARE(QCA6174_HW_2_1_FW_DIR "/" ATH10K_FW_API4_FILE);
  3082. MODULE_FIRMWARE(QCA6174_HW_2_1_FW_DIR "/" ATH10K_FW_API5_FILE);
  3083. MODULE_FIRMWARE(QCA6174_HW_2_1_FW_DIR "/" QCA6174_HW_2_1_BOARD_DATA_FILE);
  3084. MODULE_FIRMWARE(QCA6174_HW_2_1_FW_DIR "/" ATH10K_BOARD_API2_FILE);
  3085. /* QCA6174 3.1 firmware files */
  3086. MODULE_FIRMWARE(QCA6174_HW_3_0_FW_DIR "/" ATH10K_FW_API4_FILE);
  3087. MODULE_FIRMWARE(QCA6174_HW_3_0_FW_DIR "/" ATH10K_FW_API5_FILE);
  3088. MODULE_FIRMWARE(QCA6174_HW_3_0_FW_DIR "/" ATH10K_FW_API6_FILE);
  3089. MODULE_FIRMWARE(QCA6174_HW_3_0_FW_DIR "/" QCA6174_HW_3_0_BOARD_DATA_FILE);
  3090. MODULE_FIRMWARE(QCA6174_HW_3_0_FW_DIR "/" ATH10K_BOARD_API2_FILE);
  3091. /* QCA9377 1.0 firmware files */
  3092. MODULE_FIRMWARE(QCA9377_HW_1_0_FW_DIR "/" ATH10K_FW_API6_FILE);
  3093. MODULE_FIRMWARE(QCA9377_HW_1_0_FW_DIR "/" ATH10K_FW_API5_FILE);
  3094. MODULE_FIRMWARE(QCA9377_HW_1_0_FW_DIR "/" QCA9377_HW_1_0_BOARD_DATA_FILE);