pci.c 77 KB

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