pci.c 78 KB

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