pci.c 82 KB

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