pci.c 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218
  1. /*
  2. * Copyright (c) 2005-2011 Atheros Communications Inc.
  3. * Copyright (c) 2011-2013 Qualcomm Atheros, Inc.
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  15. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #include <linux/pci.h>
  18. #include <linux/module.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/spinlock.h>
  21. #include <linux/bitops.h>
  22. #include "core.h"
  23. #include "debug.h"
  24. #include "targaddrs.h"
  25. #include "bmi.h"
  26. #include "hif.h"
  27. #include "htc.h"
  28. #include "ce.h"
  29. #include "pci.h"
  30. enum ath10k_pci_irq_mode {
  31. ATH10K_PCI_IRQ_AUTO = 0,
  32. ATH10K_PCI_IRQ_LEGACY = 1,
  33. ATH10K_PCI_IRQ_MSI = 2,
  34. };
  35. enum ath10k_pci_reset_mode {
  36. ATH10K_PCI_RESET_AUTO = 0,
  37. ATH10K_PCI_RESET_WARM_ONLY = 1,
  38. };
  39. static unsigned int ath10k_pci_irq_mode = ATH10K_PCI_IRQ_AUTO;
  40. static unsigned int ath10k_pci_reset_mode = ATH10K_PCI_RESET_AUTO;
  41. module_param_named(irq_mode, ath10k_pci_irq_mode, uint, 0644);
  42. MODULE_PARM_DESC(irq_mode, "0: auto, 1: legacy, 2: msi (default: 0)");
  43. module_param_named(reset_mode, ath10k_pci_reset_mode, uint, 0644);
  44. MODULE_PARM_DESC(reset_mode, "0: auto, 1: warm only (default: 0)");
  45. /* how long wait to wait for target to initialise, in ms */
  46. #define ATH10K_PCI_TARGET_WAIT 3000
  47. #define ATH10K_PCI_NUM_WARM_RESET_ATTEMPTS 3
  48. static const struct pci_device_id ath10k_pci_id_table[] = {
  49. { PCI_VDEVICE(ATHEROS, QCA988X_2_0_DEVICE_ID) }, /* PCI-E QCA988X V2 */
  50. { PCI_VDEVICE(ATHEROS, QCA6164_2_1_DEVICE_ID) }, /* PCI-E QCA6164 V2.1 */
  51. { PCI_VDEVICE(ATHEROS, QCA6174_2_1_DEVICE_ID) }, /* PCI-E QCA6174 V2.1 */
  52. { PCI_VDEVICE(ATHEROS, QCA99X0_2_0_DEVICE_ID) }, /* PCI-E QCA99X0 V2 */
  53. { PCI_VDEVICE(ATHEROS, QCA9377_1_0_DEVICE_ID) }, /* PCI-E QCA9377 V1 */
  54. {0}
  55. };
  56. static const struct ath10k_pci_supp_chip ath10k_pci_supp_chips[] = {
  57. /* QCA988X pre 2.0 chips are not supported because they need some nasty
  58. * hacks. ath10k doesn't have them and these devices crash horribly
  59. * because of that.
  60. */
  61. { QCA988X_2_0_DEVICE_ID, QCA988X_HW_2_0_CHIP_ID_REV },
  62. { QCA6164_2_1_DEVICE_ID, QCA6174_HW_2_1_CHIP_ID_REV },
  63. { QCA6164_2_1_DEVICE_ID, QCA6174_HW_2_2_CHIP_ID_REV },
  64. { QCA6164_2_1_DEVICE_ID, QCA6174_HW_3_0_CHIP_ID_REV },
  65. { QCA6164_2_1_DEVICE_ID, QCA6174_HW_3_1_CHIP_ID_REV },
  66. { QCA6164_2_1_DEVICE_ID, QCA6174_HW_3_2_CHIP_ID_REV },
  67. { QCA6174_2_1_DEVICE_ID, QCA6174_HW_2_1_CHIP_ID_REV },
  68. { QCA6174_2_1_DEVICE_ID, QCA6174_HW_2_2_CHIP_ID_REV },
  69. { QCA6174_2_1_DEVICE_ID, QCA6174_HW_3_0_CHIP_ID_REV },
  70. { QCA6174_2_1_DEVICE_ID, QCA6174_HW_3_1_CHIP_ID_REV },
  71. { QCA6174_2_1_DEVICE_ID, QCA6174_HW_3_2_CHIP_ID_REV },
  72. { QCA99X0_2_0_DEVICE_ID, QCA99X0_HW_2_0_CHIP_ID_REV },
  73. { QCA9377_1_0_DEVICE_ID, QCA9377_HW_1_0_CHIP_ID_REV },
  74. { QCA9377_1_0_DEVICE_ID, QCA9377_HW_1_1_CHIP_ID_REV },
  75. };
  76. static void ath10k_pci_buffer_cleanup(struct ath10k *ar);
  77. static int ath10k_pci_cold_reset(struct ath10k *ar);
  78. static int ath10k_pci_safe_chip_reset(struct ath10k *ar);
  79. static int ath10k_pci_wait_for_target_init(struct ath10k *ar);
  80. static int ath10k_pci_init_irq(struct ath10k *ar);
  81. static int ath10k_pci_deinit_irq(struct ath10k *ar);
  82. static int ath10k_pci_request_irq(struct ath10k *ar);
  83. static void ath10k_pci_free_irq(struct ath10k *ar);
  84. static int ath10k_pci_bmi_wait(struct ath10k_ce_pipe *tx_pipe,
  85. struct ath10k_ce_pipe *rx_pipe,
  86. struct bmi_xfer *xfer);
  87. static int ath10k_pci_qca99x0_chip_reset(struct ath10k *ar);
  88. static void ath10k_pci_htc_tx_cb(struct ath10k_ce_pipe *ce_state);
  89. static void ath10k_pci_htc_rx_cb(struct ath10k_ce_pipe *ce_state);
  90. static void ath10k_pci_htt_tx_cb(struct ath10k_ce_pipe *ce_state);
  91. static void ath10k_pci_htt_rx_cb(struct ath10k_ce_pipe *ce_state);
  92. static void ath10k_pci_htt_htc_rx_cb(struct ath10k_ce_pipe *ce_state);
  93. static struct ce_attr host_ce_config_wlan[] = {
  94. /* CE0: host->target HTC control and raw streams */
  95. {
  96. .flags = CE_ATTR_FLAGS,
  97. .src_nentries = 16,
  98. .src_sz_max = 256,
  99. .dest_nentries = 0,
  100. .send_cb = ath10k_pci_htc_tx_cb,
  101. },
  102. /* CE1: target->host HTT + HTC control */
  103. {
  104. .flags = CE_ATTR_FLAGS,
  105. .src_nentries = 0,
  106. .src_sz_max = 2048,
  107. .dest_nentries = 512,
  108. .recv_cb = ath10k_pci_htt_htc_rx_cb,
  109. },
  110. /* CE2: target->host WMI */
  111. {
  112. .flags = CE_ATTR_FLAGS,
  113. .src_nentries = 0,
  114. .src_sz_max = 2048,
  115. .dest_nentries = 128,
  116. .recv_cb = ath10k_pci_htc_rx_cb,
  117. },
  118. /* CE3: host->target WMI */
  119. {
  120. .flags = CE_ATTR_FLAGS,
  121. .src_nentries = 32,
  122. .src_sz_max = 2048,
  123. .dest_nentries = 0,
  124. .send_cb = ath10k_pci_htc_tx_cb,
  125. },
  126. /* CE4: host->target HTT */
  127. {
  128. .flags = CE_ATTR_FLAGS | CE_ATTR_DIS_INTR,
  129. .src_nentries = CE_HTT_H2T_MSG_SRC_NENTRIES,
  130. .src_sz_max = 256,
  131. .dest_nentries = 0,
  132. .send_cb = ath10k_pci_htt_tx_cb,
  133. },
  134. /* CE5: target->host HTT (HIF->HTT) */
  135. {
  136. .flags = CE_ATTR_FLAGS,
  137. .src_nentries = 0,
  138. .src_sz_max = 512,
  139. .dest_nentries = 512,
  140. .recv_cb = ath10k_pci_htt_rx_cb,
  141. },
  142. /* CE6: target autonomous hif_memcpy */
  143. {
  144. .flags = CE_ATTR_FLAGS,
  145. .src_nentries = 0,
  146. .src_sz_max = 0,
  147. .dest_nentries = 0,
  148. },
  149. /* CE7: ce_diag, the Diagnostic Window */
  150. {
  151. .flags = CE_ATTR_FLAGS,
  152. .src_nentries = 2,
  153. .src_sz_max = DIAG_TRANSFER_LIMIT,
  154. .dest_nentries = 2,
  155. },
  156. /* CE8: target->host pktlog */
  157. {
  158. .flags = CE_ATTR_FLAGS,
  159. .src_nentries = 0,
  160. .src_sz_max = 2048,
  161. .dest_nentries = 128,
  162. },
  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 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 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. case ATH10K_HW_QCA9377:
  693. val = (ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
  694. CORE_CTRL_ADDRESS) &
  695. 0x7ff) << 21;
  696. break;
  697. case ATH10K_HW_QCA99X0:
  698. val = ath10k_pci_read32(ar, PCIE_BAR_REG_ADDRESS);
  699. break;
  700. }
  701. val |= 0x100000 | (addr & 0xfffff);
  702. return val;
  703. }
  704. /*
  705. * Diagnostic read/write access is provided for startup/config/debug usage.
  706. * Caller must guarantee proper alignment, when applicable, and single user
  707. * at any moment.
  708. */
  709. static int ath10k_pci_diag_read_mem(struct ath10k *ar, u32 address, void *data,
  710. int nbytes)
  711. {
  712. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  713. int ret = 0;
  714. u32 buf;
  715. unsigned int completed_nbytes, orig_nbytes, remaining_bytes;
  716. unsigned int id;
  717. unsigned int flags;
  718. struct ath10k_ce_pipe *ce_diag;
  719. /* Host buffer address in CE space */
  720. u32 ce_data;
  721. dma_addr_t ce_data_base = 0;
  722. void *data_buf = NULL;
  723. int i;
  724. spin_lock_bh(&ar_pci->ce_lock);
  725. ce_diag = ar_pci->ce_diag;
  726. /*
  727. * Allocate a temporary bounce buffer to hold caller's data
  728. * to be DMA'ed from Target. This guarantees
  729. * 1) 4-byte alignment
  730. * 2) Buffer in DMA-able space
  731. */
  732. orig_nbytes = nbytes;
  733. data_buf = (unsigned char *)dma_alloc_coherent(ar->dev,
  734. orig_nbytes,
  735. &ce_data_base,
  736. GFP_ATOMIC);
  737. if (!data_buf) {
  738. ret = -ENOMEM;
  739. goto done;
  740. }
  741. memset(data_buf, 0, orig_nbytes);
  742. remaining_bytes = orig_nbytes;
  743. ce_data = ce_data_base;
  744. while (remaining_bytes) {
  745. nbytes = min_t(unsigned int, remaining_bytes,
  746. DIAG_TRANSFER_LIMIT);
  747. ret = __ath10k_ce_rx_post_buf(ce_diag, NULL, ce_data);
  748. if (ret != 0)
  749. goto done;
  750. /* Request CE to send from Target(!) address to Host buffer */
  751. /*
  752. * The address supplied by the caller is in the
  753. * Target CPU virtual address space.
  754. *
  755. * In order to use this address with the diagnostic CE,
  756. * convert it from Target CPU virtual address space
  757. * to CE address space
  758. */
  759. address = ath10k_pci_targ_cpu_to_ce_addr(ar, address);
  760. ret = ath10k_ce_send_nolock(ce_diag, NULL, (u32)address, nbytes, 0,
  761. 0);
  762. if (ret)
  763. goto done;
  764. i = 0;
  765. while (ath10k_ce_completed_send_next_nolock(ce_diag,
  766. NULL) != 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,
  900. NULL) != 0) {
  901. mdelay(1);
  902. if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
  903. ret = -EBUSY;
  904. goto done;
  905. }
  906. }
  907. i = 0;
  908. while (ath10k_ce_completed_recv_next_nolock(ce_diag, NULL, &buf,
  909. &completed_nbytes,
  910. &id, &flags) != 0) {
  911. mdelay(1);
  912. if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
  913. ret = -EBUSY;
  914. goto done;
  915. }
  916. }
  917. if (nbytes != completed_nbytes) {
  918. ret = -EIO;
  919. goto done;
  920. }
  921. if (buf != address) {
  922. ret = -EIO;
  923. goto done;
  924. }
  925. remaining_bytes -= nbytes;
  926. address += nbytes;
  927. ce_data += nbytes;
  928. }
  929. done:
  930. if (data_buf) {
  931. dma_free_coherent(ar->dev, orig_nbytes, data_buf,
  932. ce_data_base);
  933. }
  934. if (ret != 0)
  935. ath10k_warn(ar, "failed to write diag value at 0x%x: %d\n",
  936. address, ret);
  937. spin_unlock_bh(&ar_pci->ce_lock);
  938. return ret;
  939. }
  940. static int ath10k_pci_diag_write32(struct ath10k *ar, u32 address, u32 value)
  941. {
  942. __le32 val = __cpu_to_le32(value);
  943. return ath10k_pci_diag_write_mem(ar, address, &val, sizeof(val));
  944. }
  945. /* Called by lower (CE) layer when a send to Target completes. */
  946. static void ath10k_pci_htc_tx_cb(struct ath10k_ce_pipe *ce_state)
  947. {
  948. struct ath10k *ar = ce_state->ar;
  949. struct sk_buff_head list;
  950. struct sk_buff *skb;
  951. __skb_queue_head_init(&list);
  952. while (ath10k_ce_completed_send_next(ce_state, (void **)&skb) == 0) {
  953. /* no need to call tx completion for NULL pointers */
  954. if (skb == NULL)
  955. continue;
  956. __skb_queue_tail(&list, skb);
  957. }
  958. while ((skb = __skb_dequeue(&list)))
  959. ath10k_htc_tx_completion_handler(ar, skb);
  960. }
  961. static void ath10k_pci_process_rx_cb(struct ath10k_ce_pipe *ce_state,
  962. void (*callback)(struct ath10k *ar,
  963. struct sk_buff *skb))
  964. {
  965. struct ath10k *ar = ce_state->ar;
  966. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  967. struct ath10k_pci_pipe *pipe_info = &ar_pci->pipe_info[ce_state->id];
  968. struct sk_buff *skb;
  969. struct sk_buff_head list;
  970. void *transfer_context;
  971. u32 ce_data;
  972. unsigned int nbytes, max_nbytes;
  973. unsigned int transfer_id;
  974. unsigned int flags;
  975. __skb_queue_head_init(&list);
  976. while (ath10k_ce_completed_recv_next(ce_state, &transfer_context,
  977. &ce_data, &nbytes, &transfer_id,
  978. &flags) == 0) {
  979. skb = transfer_context;
  980. max_nbytes = skb->len + skb_tailroom(skb);
  981. dma_unmap_single(ar->dev, ATH10K_SKB_RXCB(skb)->paddr,
  982. max_nbytes, DMA_FROM_DEVICE);
  983. if (unlikely(max_nbytes < nbytes)) {
  984. ath10k_warn(ar, "rxed more than expected (nbytes %d, max %d)",
  985. nbytes, max_nbytes);
  986. dev_kfree_skb_any(skb);
  987. continue;
  988. }
  989. skb_put(skb, nbytes);
  990. __skb_queue_tail(&list, skb);
  991. }
  992. while ((skb = __skb_dequeue(&list))) {
  993. ath10k_dbg(ar, ATH10K_DBG_PCI, "pci rx ce pipe %d len %d\n",
  994. ce_state->id, skb->len);
  995. ath10k_dbg_dump(ar, ATH10K_DBG_PCI_DUMP, NULL, "pci rx: ",
  996. skb->data, skb->len);
  997. callback(ar, skb);
  998. }
  999. ath10k_pci_rx_post_pipe(pipe_info);
  1000. }
  1001. /* Called by lower (CE) layer when data is received from the Target. */
  1002. static void ath10k_pci_htc_rx_cb(struct ath10k_ce_pipe *ce_state)
  1003. {
  1004. ath10k_pci_process_rx_cb(ce_state, ath10k_htc_rx_completion_handler);
  1005. }
  1006. static void ath10k_pci_htt_htc_rx_cb(struct ath10k_ce_pipe *ce_state)
  1007. {
  1008. /* CE4 polling needs to be done whenever CE pipe which transports
  1009. * HTT Rx (target->host) is processed.
  1010. */
  1011. ath10k_ce_per_engine_service(ce_state->ar, 4);
  1012. ath10k_pci_process_rx_cb(ce_state, ath10k_htc_rx_completion_handler);
  1013. }
  1014. /* Called by lower (CE) layer when a send to HTT Target completes. */
  1015. static void ath10k_pci_htt_tx_cb(struct ath10k_ce_pipe *ce_state)
  1016. {
  1017. struct ath10k *ar = ce_state->ar;
  1018. struct sk_buff *skb;
  1019. while (ath10k_ce_completed_send_next(ce_state, (void **)&skb) == 0) {
  1020. /* no need to call tx completion for NULL pointers */
  1021. if (!skb)
  1022. continue;
  1023. dma_unmap_single(ar->dev, ATH10K_SKB_CB(skb)->paddr,
  1024. skb->len, DMA_TO_DEVICE);
  1025. ath10k_htt_hif_tx_complete(ar, skb);
  1026. }
  1027. }
  1028. static void ath10k_pci_htt_rx_deliver(struct ath10k *ar, struct sk_buff *skb)
  1029. {
  1030. skb_pull(skb, sizeof(struct ath10k_htc_hdr));
  1031. ath10k_htt_t2h_msg_handler(ar, skb);
  1032. }
  1033. /* Called by lower (CE) layer when HTT data is received from the Target. */
  1034. static void ath10k_pci_htt_rx_cb(struct ath10k_ce_pipe *ce_state)
  1035. {
  1036. /* CE4 polling needs to be done whenever CE pipe which transports
  1037. * HTT Rx (target->host) is processed.
  1038. */
  1039. ath10k_ce_per_engine_service(ce_state->ar, 4);
  1040. ath10k_pci_process_rx_cb(ce_state, ath10k_pci_htt_rx_deliver);
  1041. }
  1042. static int ath10k_pci_hif_tx_sg(struct ath10k *ar, u8 pipe_id,
  1043. struct ath10k_hif_sg_item *items, int n_items)
  1044. {
  1045. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1046. struct ath10k_pci_pipe *pci_pipe = &ar_pci->pipe_info[pipe_id];
  1047. struct ath10k_ce_pipe *ce_pipe = pci_pipe->ce_hdl;
  1048. struct ath10k_ce_ring *src_ring = ce_pipe->src_ring;
  1049. unsigned int nentries_mask;
  1050. unsigned int sw_index;
  1051. unsigned int write_index;
  1052. int err, i = 0;
  1053. spin_lock_bh(&ar_pci->ce_lock);
  1054. nentries_mask = src_ring->nentries_mask;
  1055. sw_index = src_ring->sw_index;
  1056. write_index = src_ring->write_index;
  1057. if (unlikely(CE_RING_DELTA(nentries_mask,
  1058. write_index, sw_index - 1) < n_items)) {
  1059. err = -ENOBUFS;
  1060. goto err;
  1061. }
  1062. for (i = 0; i < n_items - 1; i++) {
  1063. ath10k_dbg(ar, ATH10K_DBG_PCI,
  1064. "pci tx item %d paddr 0x%08x len %d n_items %d\n",
  1065. i, items[i].paddr, items[i].len, n_items);
  1066. ath10k_dbg_dump(ar, ATH10K_DBG_PCI_DUMP, NULL, "pci tx data: ",
  1067. items[i].vaddr, items[i].len);
  1068. err = ath10k_ce_send_nolock(ce_pipe,
  1069. items[i].transfer_context,
  1070. items[i].paddr,
  1071. items[i].len,
  1072. items[i].transfer_id,
  1073. CE_SEND_FLAG_GATHER);
  1074. if (err)
  1075. goto err;
  1076. }
  1077. /* `i` is equal to `n_items -1` after for() */
  1078. ath10k_dbg(ar, ATH10K_DBG_PCI,
  1079. "pci tx item %d paddr 0x%08x len %d n_items %d\n",
  1080. i, items[i].paddr, items[i].len, n_items);
  1081. ath10k_dbg_dump(ar, ATH10K_DBG_PCI_DUMP, NULL, "pci tx data: ",
  1082. items[i].vaddr, items[i].len);
  1083. err = ath10k_ce_send_nolock(ce_pipe,
  1084. items[i].transfer_context,
  1085. items[i].paddr,
  1086. items[i].len,
  1087. items[i].transfer_id,
  1088. 0);
  1089. if (err)
  1090. goto err;
  1091. spin_unlock_bh(&ar_pci->ce_lock);
  1092. return 0;
  1093. err:
  1094. for (; i > 0; i--)
  1095. __ath10k_ce_send_revert(ce_pipe);
  1096. spin_unlock_bh(&ar_pci->ce_lock);
  1097. return err;
  1098. }
  1099. static int ath10k_pci_hif_diag_read(struct ath10k *ar, u32 address, void *buf,
  1100. size_t buf_len)
  1101. {
  1102. return ath10k_pci_diag_read_mem(ar, address, buf, buf_len);
  1103. }
  1104. static u16 ath10k_pci_hif_get_free_queue_number(struct ath10k *ar, u8 pipe)
  1105. {
  1106. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1107. ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif get free queue number\n");
  1108. return ath10k_ce_num_free_src_entries(ar_pci->pipe_info[pipe].ce_hdl);
  1109. }
  1110. static void ath10k_pci_dump_registers(struct ath10k *ar,
  1111. struct ath10k_fw_crash_data *crash_data)
  1112. {
  1113. __le32 reg_dump_values[REG_DUMP_COUNT_QCA988X] = {};
  1114. int i, ret;
  1115. lockdep_assert_held(&ar->data_lock);
  1116. ret = ath10k_pci_diag_read_hi(ar, &reg_dump_values[0],
  1117. hi_failure_state,
  1118. REG_DUMP_COUNT_QCA988X * sizeof(__le32));
  1119. if (ret) {
  1120. ath10k_err(ar, "failed to read firmware dump area: %d\n", ret);
  1121. return;
  1122. }
  1123. BUILD_BUG_ON(REG_DUMP_COUNT_QCA988X % 4);
  1124. ath10k_err(ar, "firmware register dump:\n");
  1125. for (i = 0; i < REG_DUMP_COUNT_QCA988X; i += 4)
  1126. ath10k_err(ar, "[%02d]: 0x%08X 0x%08X 0x%08X 0x%08X\n",
  1127. i,
  1128. __le32_to_cpu(reg_dump_values[i]),
  1129. __le32_to_cpu(reg_dump_values[i + 1]),
  1130. __le32_to_cpu(reg_dump_values[i + 2]),
  1131. __le32_to_cpu(reg_dump_values[i + 3]));
  1132. if (!crash_data)
  1133. return;
  1134. for (i = 0; i < REG_DUMP_COUNT_QCA988X; i++)
  1135. crash_data->registers[i] = reg_dump_values[i];
  1136. }
  1137. static void ath10k_pci_fw_crashed_dump(struct ath10k *ar)
  1138. {
  1139. struct ath10k_fw_crash_data *crash_data;
  1140. char uuid[50];
  1141. spin_lock_bh(&ar->data_lock);
  1142. ar->stats.fw_crash_counter++;
  1143. crash_data = ath10k_debug_get_new_fw_crash_data(ar);
  1144. if (crash_data)
  1145. scnprintf(uuid, sizeof(uuid), "%pUl", &crash_data->uuid);
  1146. else
  1147. scnprintf(uuid, sizeof(uuid), "n/a");
  1148. ath10k_err(ar, "firmware crashed! (uuid %s)\n", uuid);
  1149. ath10k_print_driver_info(ar);
  1150. ath10k_pci_dump_registers(ar, crash_data);
  1151. spin_unlock_bh(&ar->data_lock);
  1152. queue_work(ar->workqueue, &ar->restart_work);
  1153. }
  1154. static void ath10k_pci_hif_send_complete_check(struct ath10k *ar, u8 pipe,
  1155. int force)
  1156. {
  1157. ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif send complete check\n");
  1158. if (!force) {
  1159. int resources;
  1160. /*
  1161. * Decide whether to actually poll for completions, or just
  1162. * wait for a later chance.
  1163. * If there seem to be plenty of resources left, then just wait
  1164. * since checking involves reading a CE register, which is a
  1165. * relatively expensive operation.
  1166. */
  1167. resources = ath10k_pci_hif_get_free_queue_number(ar, pipe);
  1168. /*
  1169. * If at least 50% of the total resources are still available,
  1170. * don't bother checking again yet.
  1171. */
  1172. if (resources > (host_ce_config_wlan[pipe].src_nentries >> 1))
  1173. return;
  1174. }
  1175. ath10k_ce_per_engine_service(ar, pipe);
  1176. }
  1177. static void ath10k_pci_kill_tasklet(struct ath10k *ar)
  1178. {
  1179. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1180. int i;
  1181. tasklet_kill(&ar_pci->intr_tq);
  1182. tasklet_kill(&ar_pci->msi_fw_err);
  1183. for (i = 0; i < CE_COUNT; i++)
  1184. tasklet_kill(&ar_pci->pipe_info[i].intr);
  1185. del_timer_sync(&ar_pci->rx_post_retry);
  1186. }
  1187. static int ath10k_pci_hif_map_service_to_pipe(struct ath10k *ar, u16 service_id,
  1188. u8 *ul_pipe, u8 *dl_pipe)
  1189. {
  1190. const struct service_to_pipe *entry;
  1191. bool ul_set = false, dl_set = false;
  1192. int i;
  1193. ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif map service\n");
  1194. for (i = 0; i < ARRAY_SIZE(target_service_to_ce_map_wlan); i++) {
  1195. entry = &target_service_to_ce_map_wlan[i];
  1196. if (__le32_to_cpu(entry->service_id) != service_id)
  1197. continue;
  1198. switch (__le32_to_cpu(entry->pipedir)) {
  1199. case PIPEDIR_NONE:
  1200. break;
  1201. case PIPEDIR_IN:
  1202. WARN_ON(dl_set);
  1203. *dl_pipe = __le32_to_cpu(entry->pipenum);
  1204. dl_set = true;
  1205. break;
  1206. case PIPEDIR_OUT:
  1207. WARN_ON(ul_set);
  1208. *ul_pipe = __le32_to_cpu(entry->pipenum);
  1209. ul_set = true;
  1210. break;
  1211. case PIPEDIR_INOUT:
  1212. WARN_ON(dl_set);
  1213. WARN_ON(ul_set);
  1214. *dl_pipe = __le32_to_cpu(entry->pipenum);
  1215. *ul_pipe = __le32_to_cpu(entry->pipenum);
  1216. dl_set = true;
  1217. ul_set = true;
  1218. break;
  1219. }
  1220. }
  1221. if (WARN_ON(!ul_set || !dl_set))
  1222. return -ENOENT;
  1223. return 0;
  1224. }
  1225. static void ath10k_pci_hif_get_default_pipe(struct ath10k *ar,
  1226. u8 *ul_pipe, u8 *dl_pipe)
  1227. {
  1228. ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif get default pipe\n");
  1229. (void)ath10k_pci_hif_map_service_to_pipe(ar,
  1230. ATH10K_HTC_SVC_ID_RSVD_CTRL,
  1231. ul_pipe, dl_pipe);
  1232. }
  1233. static void ath10k_pci_irq_msi_fw_mask(struct ath10k *ar)
  1234. {
  1235. u32 val;
  1236. switch (ar->hw_rev) {
  1237. case ATH10K_HW_QCA988X:
  1238. case ATH10K_HW_QCA6174:
  1239. case ATH10K_HW_QCA9377:
  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. case ATH10K_HW_QCA9377:
  1260. val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
  1261. CORE_CTRL_ADDRESS);
  1262. val |= CORE_CTRL_PCIE_REG_31_MASK;
  1263. ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS +
  1264. CORE_CTRL_ADDRESS, val);
  1265. break;
  1266. case ATH10K_HW_QCA99X0:
  1267. /* TODO: Find appropriate register configuration for QCA99X0
  1268. * to unmask irq/MSI.
  1269. */
  1270. break;
  1271. }
  1272. }
  1273. static void ath10k_pci_irq_disable(struct ath10k *ar)
  1274. {
  1275. ath10k_ce_disable_interrupts(ar);
  1276. ath10k_pci_disable_and_clear_legacy_irq(ar);
  1277. ath10k_pci_irq_msi_fw_mask(ar);
  1278. }
  1279. static void ath10k_pci_irq_sync(struct ath10k *ar)
  1280. {
  1281. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1282. int i;
  1283. for (i = 0; i < max(1, ar_pci->num_msi_intrs); i++)
  1284. synchronize_irq(ar_pci->pdev->irq + i);
  1285. }
  1286. static void ath10k_pci_irq_enable(struct ath10k *ar)
  1287. {
  1288. ath10k_ce_enable_interrupts(ar);
  1289. ath10k_pci_enable_legacy_irq(ar);
  1290. ath10k_pci_irq_msi_fw_unmask(ar);
  1291. }
  1292. static int ath10k_pci_hif_start(struct ath10k *ar)
  1293. {
  1294. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1295. ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif start\n");
  1296. ath10k_pci_irq_enable(ar);
  1297. ath10k_pci_rx_post(ar);
  1298. pcie_capability_write_word(ar_pci->pdev, PCI_EXP_LNKCTL,
  1299. ar_pci->link_ctl);
  1300. return 0;
  1301. }
  1302. static void ath10k_pci_rx_pipe_cleanup(struct ath10k_pci_pipe *pci_pipe)
  1303. {
  1304. struct ath10k *ar;
  1305. struct ath10k_ce_pipe *ce_pipe;
  1306. struct ath10k_ce_ring *ce_ring;
  1307. struct sk_buff *skb;
  1308. int i;
  1309. ar = pci_pipe->hif_ce_state;
  1310. ce_pipe = pci_pipe->ce_hdl;
  1311. ce_ring = ce_pipe->dest_ring;
  1312. if (!ce_ring)
  1313. return;
  1314. if (!pci_pipe->buf_sz)
  1315. return;
  1316. for (i = 0; i < ce_ring->nentries; i++) {
  1317. skb = ce_ring->per_transfer_context[i];
  1318. if (!skb)
  1319. continue;
  1320. ce_ring->per_transfer_context[i] = NULL;
  1321. dma_unmap_single(ar->dev, ATH10K_SKB_RXCB(skb)->paddr,
  1322. skb->len + skb_tailroom(skb),
  1323. DMA_FROM_DEVICE);
  1324. dev_kfree_skb_any(skb);
  1325. }
  1326. }
  1327. static void ath10k_pci_tx_pipe_cleanup(struct ath10k_pci_pipe *pci_pipe)
  1328. {
  1329. struct ath10k *ar;
  1330. struct ath10k_pci *ar_pci;
  1331. struct ath10k_ce_pipe *ce_pipe;
  1332. struct ath10k_ce_ring *ce_ring;
  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. for (i = 0; i < ce_ring->nentries; i++) {
  1344. skb = ce_ring->per_transfer_context[i];
  1345. if (!skb)
  1346. continue;
  1347. ce_ring->per_transfer_context[i] = NULL;
  1348. ath10k_htc_tx_completion_handler(ar, skb);
  1349. }
  1350. }
  1351. /*
  1352. * Cleanup residual buffers for device shutdown:
  1353. * buffers that were enqueued for receive
  1354. * buffers that were to be sent
  1355. * Note: Buffers that had completed but which were
  1356. * not yet processed are on a completion queue. They
  1357. * are handled when the completion thread shuts down.
  1358. */
  1359. static void ath10k_pci_buffer_cleanup(struct ath10k *ar)
  1360. {
  1361. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1362. int pipe_num;
  1363. for (pipe_num = 0; pipe_num < CE_COUNT; pipe_num++) {
  1364. struct ath10k_pci_pipe *pipe_info;
  1365. pipe_info = &ar_pci->pipe_info[pipe_num];
  1366. ath10k_pci_rx_pipe_cleanup(pipe_info);
  1367. ath10k_pci_tx_pipe_cleanup(pipe_info);
  1368. }
  1369. }
  1370. static void ath10k_pci_ce_deinit(struct ath10k *ar)
  1371. {
  1372. int i;
  1373. for (i = 0; i < CE_COUNT; i++)
  1374. ath10k_ce_deinit_pipe(ar, i);
  1375. }
  1376. static void ath10k_pci_flush(struct ath10k *ar)
  1377. {
  1378. ath10k_pci_kill_tasklet(ar);
  1379. ath10k_pci_buffer_cleanup(ar);
  1380. }
  1381. static void ath10k_pci_hif_stop(struct ath10k *ar)
  1382. {
  1383. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1384. unsigned long flags;
  1385. ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif stop\n");
  1386. /* Most likely the device has HTT Rx ring configured. The only way to
  1387. * prevent the device from accessing (and possible corrupting) host
  1388. * memory is to reset the chip now.
  1389. *
  1390. * There's also no known way of masking MSI interrupts on the device.
  1391. * For ranged MSI the CE-related interrupts can be masked. However
  1392. * regardless how many MSI interrupts are assigned the first one
  1393. * is always used for firmware indications (crashes) and cannot be
  1394. * masked. To prevent the device from asserting the interrupt reset it
  1395. * before proceeding with cleanup.
  1396. */
  1397. ath10k_pci_safe_chip_reset(ar);
  1398. ath10k_pci_irq_disable(ar);
  1399. ath10k_pci_irq_sync(ar);
  1400. ath10k_pci_flush(ar);
  1401. spin_lock_irqsave(&ar_pci->ps_lock, flags);
  1402. WARN_ON(ar_pci->ps_wake_refcount > 0);
  1403. spin_unlock_irqrestore(&ar_pci->ps_lock, flags);
  1404. }
  1405. static int ath10k_pci_hif_exchange_bmi_msg(struct ath10k *ar,
  1406. void *req, u32 req_len,
  1407. void *resp, u32 *resp_len)
  1408. {
  1409. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1410. struct ath10k_pci_pipe *pci_tx = &ar_pci->pipe_info[BMI_CE_NUM_TO_TARG];
  1411. struct ath10k_pci_pipe *pci_rx = &ar_pci->pipe_info[BMI_CE_NUM_TO_HOST];
  1412. struct ath10k_ce_pipe *ce_tx = pci_tx->ce_hdl;
  1413. struct ath10k_ce_pipe *ce_rx = pci_rx->ce_hdl;
  1414. dma_addr_t req_paddr = 0;
  1415. dma_addr_t resp_paddr = 0;
  1416. struct bmi_xfer xfer = {};
  1417. void *treq, *tresp = NULL;
  1418. int ret = 0;
  1419. might_sleep();
  1420. if (resp && !resp_len)
  1421. return -EINVAL;
  1422. if (resp && resp_len && *resp_len == 0)
  1423. return -EINVAL;
  1424. treq = kmemdup(req, req_len, GFP_KERNEL);
  1425. if (!treq)
  1426. return -ENOMEM;
  1427. req_paddr = dma_map_single(ar->dev, treq, req_len, DMA_TO_DEVICE);
  1428. ret = dma_mapping_error(ar->dev, req_paddr);
  1429. if (ret) {
  1430. ret = -EIO;
  1431. goto err_dma;
  1432. }
  1433. if (resp && resp_len) {
  1434. tresp = kzalloc(*resp_len, GFP_KERNEL);
  1435. if (!tresp) {
  1436. ret = -ENOMEM;
  1437. goto err_req;
  1438. }
  1439. resp_paddr = dma_map_single(ar->dev, tresp, *resp_len,
  1440. DMA_FROM_DEVICE);
  1441. ret = dma_mapping_error(ar->dev, resp_paddr);
  1442. if (ret) {
  1443. ret = EIO;
  1444. goto err_req;
  1445. }
  1446. xfer.wait_for_resp = true;
  1447. xfer.resp_len = 0;
  1448. ath10k_ce_rx_post_buf(ce_rx, &xfer, resp_paddr);
  1449. }
  1450. ret = ath10k_ce_send(ce_tx, &xfer, req_paddr, req_len, -1, 0);
  1451. if (ret)
  1452. goto err_resp;
  1453. ret = ath10k_pci_bmi_wait(ce_tx, ce_rx, &xfer);
  1454. if (ret) {
  1455. u32 unused_buffer;
  1456. unsigned int unused_nbytes;
  1457. unsigned int unused_id;
  1458. ath10k_ce_cancel_send_next(ce_tx, NULL, &unused_buffer,
  1459. &unused_nbytes, &unused_id);
  1460. } else {
  1461. /* non-zero means we did not time out */
  1462. ret = 0;
  1463. }
  1464. err_resp:
  1465. if (resp) {
  1466. u32 unused_buffer;
  1467. ath10k_ce_revoke_recv_next(ce_rx, NULL, &unused_buffer);
  1468. dma_unmap_single(ar->dev, resp_paddr,
  1469. *resp_len, DMA_FROM_DEVICE);
  1470. }
  1471. err_req:
  1472. dma_unmap_single(ar->dev, req_paddr, req_len, DMA_TO_DEVICE);
  1473. if (ret == 0 && resp_len) {
  1474. *resp_len = min(*resp_len, xfer.resp_len);
  1475. memcpy(resp, tresp, xfer.resp_len);
  1476. }
  1477. err_dma:
  1478. kfree(treq);
  1479. kfree(tresp);
  1480. return ret;
  1481. }
  1482. static void ath10k_pci_bmi_send_done(struct ath10k_ce_pipe *ce_state)
  1483. {
  1484. struct bmi_xfer *xfer;
  1485. if (ath10k_ce_completed_send_next(ce_state, (void **)&xfer))
  1486. return;
  1487. xfer->tx_done = true;
  1488. }
  1489. static void ath10k_pci_bmi_recv_data(struct ath10k_ce_pipe *ce_state)
  1490. {
  1491. struct ath10k *ar = ce_state->ar;
  1492. struct bmi_xfer *xfer;
  1493. u32 ce_data;
  1494. unsigned int nbytes;
  1495. unsigned int transfer_id;
  1496. unsigned int flags;
  1497. if (ath10k_ce_completed_recv_next(ce_state, (void **)&xfer, &ce_data,
  1498. &nbytes, &transfer_id, &flags))
  1499. return;
  1500. if (WARN_ON_ONCE(!xfer))
  1501. return;
  1502. if (!xfer->wait_for_resp) {
  1503. ath10k_warn(ar, "unexpected: BMI data received; ignoring\n");
  1504. return;
  1505. }
  1506. xfer->resp_len = nbytes;
  1507. xfer->rx_done = true;
  1508. }
  1509. static int ath10k_pci_bmi_wait(struct ath10k_ce_pipe *tx_pipe,
  1510. struct ath10k_ce_pipe *rx_pipe,
  1511. struct bmi_xfer *xfer)
  1512. {
  1513. unsigned long timeout = jiffies + BMI_COMMUNICATION_TIMEOUT_HZ;
  1514. while (time_before_eq(jiffies, timeout)) {
  1515. ath10k_pci_bmi_send_done(tx_pipe);
  1516. ath10k_pci_bmi_recv_data(rx_pipe);
  1517. if (xfer->tx_done && (xfer->rx_done == xfer->wait_for_resp))
  1518. return 0;
  1519. schedule();
  1520. }
  1521. return -ETIMEDOUT;
  1522. }
  1523. /*
  1524. * Send an interrupt to the device to wake up the Target CPU
  1525. * so it has an opportunity to notice any changed state.
  1526. */
  1527. static int ath10k_pci_wake_target_cpu(struct ath10k *ar)
  1528. {
  1529. u32 addr, val;
  1530. addr = SOC_CORE_BASE_ADDRESS | CORE_CTRL_ADDRESS;
  1531. val = ath10k_pci_read32(ar, addr);
  1532. val |= CORE_CTRL_CPU_INTR_MASK;
  1533. ath10k_pci_write32(ar, addr, val);
  1534. return 0;
  1535. }
  1536. static int ath10k_pci_get_num_banks(struct ath10k *ar)
  1537. {
  1538. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1539. switch (ar_pci->pdev->device) {
  1540. case QCA988X_2_0_DEVICE_ID:
  1541. case QCA99X0_2_0_DEVICE_ID:
  1542. return 1;
  1543. case QCA6164_2_1_DEVICE_ID:
  1544. case QCA6174_2_1_DEVICE_ID:
  1545. switch (MS(ar->chip_id, SOC_CHIP_ID_REV)) {
  1546. case QCA6174_HW_1_0_CHIP_ID_REV:
  1547. case QCA6174_HW_1_1_CHIP_ID_REV:
  1548. case QCA6174_HW_2_1_CHIP_ID_REV:
  1549. case QCA6174_HW_2_2_CHIP_ID_REV:
  1550. return 3;
  1551. case QCA6174_HW_1_3_CHIP_ID_REV:
  1552. return 2;
  1553. case QCA6174_HW_3_0_CHIP_ID_REV:
  1554. case QCA6174_HW_3_1_CHIP_ID_REV:
  1555. case QCA6174_HW_3_2_CHIP_ID_REV:
  1556. return 9;
  1557. }
  1558. break;
  1559. case QCA9377_1_0_DEVICE_ID:
  1560. return 2;
  1561. }
  1562. ath10k_warn(ar, "unknown number of banks, assuming 1\n");
  1563. return 1;
  1564. }
  1565. static int ath10k_pci_init_config(struct ath10k *ar)
  1566. {
  1567. u32 interconnect_targ_addr;
  1568. u32 pcie_state_targ_addr = 0;
  1569. u32 pipe_cfg_targ_addr = 0;
  1570. u32 svc_to_pipe_map = 0;
  1571. u32 pcie_config_flags = 0;
  1572. u32 ealloc_value;
  1573. u32 ealloc_targ_addr;
  1574. u32 flag2_value;
  1575. u32 flag2_targ_addr;
  1576. int ret = 0;
  1577. /* Download to Target the CE Config and the service-to-CE map */
  1578. interconnect_targ_addr =
  1579. host_interest_item_address(HI_ITEM(hi_interconnect_state));
  1580. /* Supply Target-side CE configuration */
  1581. ret = ath10k_pci_diag_read32(ar, interconnect_targ_addr,
  1582. &pcie_state_targ_addr);
  1583. if (ret != 0) {
  1584. ath10k_err(ar, "Failed to get pcie state addr: %d\n", ret);
  1585. return ret;
  1586. }
  1587. if (pcie_state_targ_addr == 0) {
  1588. ret = -EIO;
  1589. ath10k_err(ar, "Invalid pcie state addr\n");
  1590. return ret;
  1591. }
  1592. ret = ath10k_pci_diag_read32(ar, (pcie_state_targ_addr +
  1593. offsetof(struct pcie_state,
  1594. pipe_cfg_addr)),
  1595. &pipe_cfg_targ_addr);
  1596. if (ret != 0) {
  1597. ath10k_err(ar, "Failed to get pipe cfg addr: %d\n", ret);
  1598. return ret;
  1599. }
  1600. if (pipe_cfg_targ_addr == 0) {
  1601. ret = -EIO;
  1602. ath10k_err(ar, "Invalid pipe cfg addr\n");
  1603. return ret;
  1604. }
  1605. ret = ath10k_pci_diag_write_mem(ar, pipe_cfg_targ_addr,
  1606. target_ce_config_wlan,
  1607. sizeof(struct ce_pipe_config) *
  1608. NUM_TARGET_CE_CONFIG_WLAN);
  1609. if (ret != 0) {
  1610. ath10k_err(ar, "Failed to write pipe cfg: %d\n", ret);
  1611. return ret;
  1612. }
  1613. ret = ath10k_pci_diag_read32(ar, (pcie_state_targ_addr +
  1614. offsetof(struct pcie_state,
  1615. svc_to_pipe_map)),
  1616. &svc_to_pipe_map);
  1617. if (ret != 0) {
  1618. ath10k_err(ar, "Failed to get svc/pipe map: %d\n", ret);
  1619. return ret;
  1620. }
  1621. if (svc_to_pipe_map == 0) {
  1622. ret = -EIO;
  1623. ath10k_err(ar, "Invalid svc_to_pipe map\n");
  1624. return ret;
  1625. }
  1626. ret = ath10k_pci_diag_write_mem(ar, svc_to_pipe_map,
  1627. target_service_to_ce_map_wlan,
  1628. sizeof(target_service_to_ce_map_wlan));
  1629. if (ret != 0) {
  1630. ath10k_err(ar, "Failed to write svc/pipe map: %d\n", ret);
  1631. return ret;
  1632. }
  1633. ret = ath10k_pci_diag_read32(ar, (pcie_state_targ_addr +
  1634. offsetof(struct pcie_state,
  1635. config_flags)),
  1636. &pcie_config_flags);
  1637. if (ret != 0) {
  1638. ath10k_err(ar, "Failed to get pcie config_flags: %d\n", ret);
  1639. return ret;
  1640. }
  1641. pcie_config_flags &= ~PCIE_CONFIG_FLAG_ENABLE_L1;
  1642. ret = ath10k_pci_diag_write32(ar, (pcie_state_targ_addr +
  1643. offsetof(struct pcie_state,
  1644. config_flags)),
  1645. pcie_config_flags);
  1646. if (ret != 0) {
  1647. ath10k_err(ar, "Failed to write pcie config_flags: %d\n", ret);
  1648. return ret;
  1649. }
  1650. /* configure early allocation */
  1651. ealloc_targ_addr = host_interest_item_address(HI_ITEM(hi_early_alloc));
  1652. ret = ath10k_pci_diag_read32(ar, ealloc_targ_addr, &ealloc_value);
  1653. if (ret != 0) {
  1654. ath10k_err(ar, "Faile to get early alloc val: %d\n", ret);
  1655. return ret;
  1656. }
  1657. /* first bank is switched to IRAM */
  1658. ealloc_value |= ((HI_EARLY_ALLOC_MAGIC << HI_EARLY_ALLOC_MAGIC_SHIFT) &
  1659. HI_EARLY_ALLOC_MAGIC_MASK);
  1660. ealloc_value |= ((ath10k_pci_get_num_banks(ar) <<
  1661. HI_EARLY_ALLOC_IRAM_BANKS_SHIFT) &
  1662. HI_EARLY_ALLOC_IRAM_BANKS_MASK);
  1663. ret = ath10k_pci_diag_write32(ar, ealloc_targ_addr, ealloc_value);
  1664. if (ret != 0) {
  1665. ath10k_err(ar, "Failed to set early alloc val: %d\n", ret);
  1666. return ret;
  1667. }
  1668. /* Tell Target to proceed with initialization */
  1669. flag2_targ_addr = host_interest_item_address(HI_ITEM(hi_option_flag2));
  1670. ret = ath10k_pci_diag_read32(ar, flag2_targ_addr, &flag2_value);
  1671. if (ret != 0) {
  1672. ath10k_err(ar, "Failed to get option val: %d\n", ret);
  1673. return ret;
  1674. }
  1675. flag2_value |= HI_OPTION_EARLY_CFG_DONE;
  1676. ret = ath10k_pci_diag_write32(ar, flag2_targ_addr, flag2_value);
  1677. if (ret != 0) {
  1678. ath10k_err(ar, "Failed to set option val: %d\n", ret);
  1679. return ret;
  1680. }
  1681. return 0;
  1682. }
  1683. static void ath10k_pci_override_ce_config(struct ath10k *ar)
  1684. {
  1685. struct ce_attr *attr;
  1686. struct ce_pipe_config *config;
  1687. /* For QCA6174 we're overriding the Copy Engine 5 configuration,
  1688. * since it is currently used for other feature.
  1689. */
  1690. /* Override Host's Copy Engine 5 configuration */
  1691. attr = &host_ce_config_wlan[5];
  1692. attr->src_sz_max = 0;
  1693. attr->dest_nentries = 0;
  1694. /* Override Target firmware's Copy Engine configuration */
  1695. config = &target_ce_config_wlan[5];
  1696. config->pipedir = __cpu_to_le32(PIPEDIR_OUT);
  1697. config->nbytes_max = __cpu_to_le32(2048);
  1698. /* Map from service/endpoint to Copy Engine */
  1699. target_service_to_ce_map_wlan[15].pipenum = __cpu_to_le32(1);
  1700. }
  1701. static int ath10k_pci_alloc_pipes(struct ath10k *ar)
  1702. {
  1703. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1704. struct ath10k_pci_pipe *pipe;
  1705. int i, ret;
  1706. for (i = 0; i < CE_COUNT; i++) {
  1707. pipe = &ar_pci->pipe_info[i];
  1708. pipe->ce_hdl = &ar_pci->ce_states[i];
  1709. pipe->pipe_num = i;
  1710. pipe->hif_ce_state = ar;
  1711. ret = ath10k_ce_alloc_pipe(ar, i, &host_ce_config_wlan[i]);
  1712. if (ret) {
  1713. ath10k_err(ar, "failed to allocate copy engine pipe %d: %d\n",
  1714. i, ret);
  1715. return ret;
  1716. }
  1717. /* Last CE is Diagnostic Window */
  1718. if (i == CE_DIAG_PIPE) {
  1719. ar_pci->ce_diag = pipe->ce_hdl;
  1720. continue;
  1721. }
  1722. pipe->buf_sz = (size_t)(host_ce_config_wlan[i].src_sz_max);
  1723. }
  1724. return 0;
  1725. }
  1726. static void ath10k_pci_free_pipes(struct ath10k *ar)
  1727. {
  1728. int i;
  1729. for (i = 0; i < CE_COUNT; i++)
  1730. ath10k_ce_free_pipe(ar, i);
  1731. }
  1732. static int ath10k_pci_init_pipes(struct ath10k *ar)
  1733. {
  1734. int i, ret;
  1735. for (i = 0; i < CE_COUNT; i++) {
  1736. ret = ath10k_ce_init_pipe(ar, i, &host_ce_config_wlan[i]);
  1737. if (ret) {
  1738. ath10k_err(ar, "failed to initialize copy engine pipe %d: %d\n",
  1739. i, ret);
  1740. return ret;
  1741. }
  1742. }
  1743. return 0;
  1744. }
  1745. static bool ath10k_pci_has_fw_crashed(struct ath10k *ar)
  1746. {
  1747. return ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS) &
  1748. FW_IND_EVENT_PENDING;
  1749. }
  1750. static void ath10k_pci_fw_crashed_clear(struct ath10k *ar)
  1751. {
  1752. u32 val;
  1753. val = ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS);
  1754. val &= ~FW_IND_EVENT_PENDING;
  1755. ath10k_pci_write32(ar, FW_INDICATOR_ADDRESS, val);
  1756. }
  1757. /* this function effectively clears target memory controller assert line */
  1758. static void ath10k_pci_warm_reset_si0(struct ath10k *ar)
  1759. {
  1760. u32 val;
  1761. val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
  1762. ath10k_pci_soc_write32(ar, SOC_RESET_CONTROL_ADDRESS,
  1763. val | SOC_RESET_CONTROL_SI0_RST_MASK);
  1764. val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
  1765. msleep(10);
  1766. val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
  1767. ath10k_pci_soc_write32(ar, SOC_RESET_CONTROL_ADDRESS,
  1768. val & ~SOC_RESET_CONTROL_SI0_RST_MASK);
  1769. val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
  1770. msleep(10);
  1771. }
  1772. static void ath10k_pci_warm_reset_cpu(struct ath10k *ar)
  1773. {
  1774. u32 val;
  1775. ath10k_pci_write32(ar, FW_INDICATOR_ADDRESS, 0);
  1776. val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
  1777. SOC_RESET_CONTROL_ADDRESS);
  1778. ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
  1779. val | SOC_RESET_CONTROL_CPU_WARM_RST_MASK);
  1780. }
  1781. static void ath10k_pci_warm_reset_ce(struct ath10k *ar)
  1782. {
  1783. u32 val;
  1784. val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
  1785. SOC_RESET_CONTROL_ADDRESS);
  1786. ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
  1787. val | SOC_RESET_CONTROL_CE_RST_MASK);
  1788. msleep(10);
  1789. ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
  1790. val & ~SOC_RESET_CONTROL_CE_RST_MASK);
  1791. }
  1792. static void ath10k_pci_warm_reset_clear_lf(struct ath10k *ar)
  1793. {
  1794. u32 val;
  1795. val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
  1796. SOC_LF_TIMER_CONTROL0_ADDRESS);
  1797. ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS +
  1798. SOC_LF_TIMER_CONTROL0_ADDRESS,
  1799. val & ~SOC_LF_TIMER_CONTROL0_ENABLE_MASK);
  1800. }
  1801. static int ath10k_pci_warm_reset(struct ath10k *ar)
  1802. {
  1803. int ret;
  1804. ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot warm reset\n");
  1805. spin_lock_bh(&ar->data_lock);
  1806. ar->stats.fw_warm_reset_counter++;
  1807. spin_unlock_bh(&ar->data_lock);
  1808. ath10k_pci_irq_disable(ar);
  1809. /* Make sure the target CPU is not doing anything dangerous, e.g. if it
  1810. * were to access copy engine while host performs copy engine reset
  1811. * then it is possible for the device to confuse pci-e controller to
  1812. * the point of bringing host system to a complete stop (i.e. hang).
  1813. */
  1814. ath10k_pci_warm_reset_si0(ar);
  1815. ath10k_pci_warm_reset_cpu(ar);
  1816. ath10k_pci_init_pipes(ar);
  1817. ath10k_pci_wait_for_target_init(ar);
  1818. ath10k_pci_warm_reset_clear_lf(ar);
  1819. ath10k_pci_warm_reset_ce(ar);
  1820. ath10k_pci_warm_reset_cpu(ar);
  1821. ath10k_pci_init_pipes(ar);
  1822. ret = ath10k_pci_wait_for_target_init(ar);
  1823. if (ret) {
  1824. ath10k_warn(ar, "failed to wait for target init: %d\n", ret);
  1825. return ret;
  1826. }
  1827. ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot warm reset complete\n");
  1828. return 0;
  1829. }
  1830. static int ath10k_pci_safe_chip_reset(struct ath10k *ar)
  1831. {
  1832. if (QCA_REV_988X(ar) || QCA_REV_6174(ar)) {
  1833. return ath10k_pci_warm_reset(ar);
  1834. } else if (QCA_REV_99X0(ar)) {
  1835. ath10k_pci_irq_disable(ar);
  1836. return ath10k_pci_qca99x0_chip_reset(ar);
  1837. } else {
  1838. return -ENOTSUPP;
  1839. }
  1840. }
  1841. static int ath10k_pci_qca988x_chip_reset(struct ath10k *ar)
  1842. {
  1843. int i, ret;
  1844. u32 val;
  1845. ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot 988x chip reset\n");
  1846. /* Some hardware revisions (e.g. CUS223v2) has issues with cold reset.
  1847. * It is thus preferred to use warm reset which is safer but may not be
  1848. * able to recover the device from all possible fail scenarios.
  1849. *
  1850. * Warm reset doesn't always work on first try so attempt it a few
  1851. * times before giving up.
  1852. */
  1853. for (i = 0; i < ATH10K_PCI_NUM_WARM_RESET_ATTEMPTS; i++) {
  1854. ret = ath10k_pci_warm_reset(ar);
  1855. if (ret) {
  1856. ath10k_warn(ar, "failed to warm reset attempt %d of %d: %d\n",
  1857. i + 1, ATH10K_PCI_NUM_WARM_RESET_ATTEMPTS,
  1858. ret);
  1859. continue;
  1860. }
  1861. /* FIXME: Sometimes copy engine doesn't recover after warm
  1862. * reset. In most cases this needs cold reset. In some of these
  1863. * cases the device is in such a state that a cold reset may
  1864. * lock up the host.
  1865. *
  1866. * Reading any host interest register via copy engine is
  1867. * sufficient to verify if device is capable of booting
  1868. * firmware blob.
  1869. */
  1870. ret = ath10k_pci_init_pipes(ar);
  1871. if (ret) {
  1872. ath10k_warn(ar, "failed to init copy engine: %d\n",
  1873. ret);
  1874. continue;
  1875. }
  1876. ret = ath10k_pci_diag_read32(ar, QCA988X_HOST_INTEREST_ADDRESS,
  1877. &val);
  1878. if (ret) {
  1879. ath10k_warn(ar, "failed to poke copy engine: %d\n",
  1880. ret);
  1881. continue;
  1882. }
  1883. ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot chip reset complete (warm)\n");
  1884. return 0;
  1885. }
  1886. if (ath10k_pci_reset_mode == ATH10K_PCI_RESET_WARM_ONLY) {
  1887. ath10k_warn(ar, "refusing cold reset as requested\n");
  1888. return -EPERM;
  1889. }
  1890. ret = ath10k_pci_cold_reset(ar);
  1891. if (ret) {
  1892. ath10k_warn(ar, "failed to cold reset: %d\n", ret);
  1893. return ret;
  1894. }
  1895. ret = ath10k_pci_wait_for_target_init(ar);
  1896. if (ret) {
  1897. ath10k_warn(ar, "failed to wait for target after cold reset: %d\n",
  1898. ret);
  1899. return ret;
  1900. }
  1901. ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot qca988x chip reset complete (cold)\n");
  1902. return 0;
  1903. }
  1904. static int ath10k_pci_qca6174_chip_reset(struct ath10k *ar)
  1905. {
  1906. int ret;
  1907. ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot qca6174 chip reset\n");
  1908. /* FIXME: QCA6174 requires cold + warm reset to work. */
  1909. ret = ath10k_pci_cold_reset(ar);
  1910. if (ret) {
  1911. ath10k_warn(ar, "failed to cold reset: %d\n", ret);
  1912. return ret;
  1913. }
  1914. ret = ath10k_pci_wait_for_target_init(ar);
  1915. if (ret) {
  1916. ath10k_warn(ar, "failed to wait for target after cold reset: %d\n",
  1917. ret);
  1918. return ret;
  1919. }
  1920. ret = ath10k_pci_warm_reset(ar);
  1921. if (ret) {
  1922. ath10k_warn(ar, "failed to warm reset: %d\n", ret);
  1923. return ret;
  1924. }
  1925. ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot qca6174 chip reset complete (cold)\n");
  1926. return 0;
  1927. }
  1928. static int ath10k_pci_qca99x0_chip_reset(struct ath10k *ar)
  1929. {
  1930. int ret;
  1931. ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot qca99x0 chip reset\n");
  1932. ret = ath10k_pci_cold_reset(ar);
  1933. if (ret) {
  1934. ath10k_warn(ar, "failed to cold reset: %d\n", ret);
  1935. return ret;
  1936. }
  1937. ret = ath10k_pci_wait_for_target_init(ar);
  1938. if (ret) {
  1939. ath10k_warn(ar, "failed to wait for target after cold reset: %d\n",
  1940. ret);
  1941. return ret;
  1942. }
  1943. ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot qca99x0 chip reset complete (cold)\n");
  1944. return 0;
  1945. }
  1946. static int ath10k_pci_chip_reset(struct ath10k *ar)
  1947. {
  1948. if (QCA_REV_988X(ar))
  1949. return ath10k_pci_qca988x_chip_reset(ar);
  1950. else if (QCA_REV_6174(ar))
  1951. return ath10k_pci_qca6174_chip_reset(ar);
  1952. else if (QCA_REV_9377(ar))
  1953. return ath10k_pci_qca6174_chip_reset(ar);
  1954. else if (QCA_REV_99X0(ar))
  1955. return ath10k_pci_qca99x0_chip_reset(ar);
  1956. else
  1957. return -ENOTSUPP;
  1958. }
  1959. static int ath10k_pci_hif_power_up(struct ath10k *ar)
  1960. {
  1961. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1962. int ret;
  1963. ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif power up\n");
  1964. pcie_capability_read_word(ar_pci->pdev, PCI_EXP_LNKCTL,
  1965. &ar_pci->link_ctl);
  1966. pcie_capability_write_word(ar_pci->pdev, PCI_EXP_LNKCTL,
  1967. ar_pci->link_ctl & ~PCI_EXP_LNKCTL_ASPMC);
  1968. /*
  1969. * Bring the target up cleanly.
  1970. *
  1971. * The target may be in an undefined state with an AUX-powered Target
  1972. * and a Host in WoW mode. If the Host crashes, loses power, or is
  1973. * restarted (without unloading the driver) then the Target is left
  1974. * (aux) powered and running. On a subsequent driver load, the Target
  1975. * is in an unexpected state. We try to catch that here in order to
  1976. * reset the Target and retry the probe.
  1977. */
  1978. ret = ath10k_pci_chip_reset(ar);
  1979. if (ret) {
  1980. if (ath10k_pci_has_fw_crashed(ar)) {
  1981. ath10k_warn(ar, "firmware crashed during chip reset\n");
  1982. ath10k_pci_fw_crashed_clear(ar);
  1983. ath10k_pci_fw_crashed_dump(ar);
  1984. }
  1985. ath10k_err(ar, "failed to reset chip: %d\n", ret);
  1986. goto err_sleep;
  1987. }
  1988. ret = ath10k_pci_init_pipes(ar);
  1989. if (ret) {
  1990. ath10k_err(ar, "failed to initialize CE: %d\n", ret);
  1991. goto err_sleep;
  1992. }
  1993. ret = ath10k_pci_init_config(ar);
  1994. if (ret) {
  1995. ath10k_err(ar, "failed to setup init config: %d\n", ret);
  1996. goto err_ce;
  1997. }
  1998. ret = ath10k_pci_wake_target_cpu(ar);
  1999. if (ret) {
  2000. ath10k_err(ar, "could not wake up target CPU: %d\n", ret);
  2001. goto err_ce;
  2002. }
  2003. return 0;
  2004. err_ce:
  2005. ath10k_pci_ce_deinit(ar);
  2006. err_sleep:
  2007. return ret;
  2008. }
  2009. static void ath10k_pci_hif_power_down(struct ath10k *ar)
  2010. {
  2011. ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif power down\n");
  2012. /* Currently hif_power_up performs effectively a reset and hif_stop
  2013. * resets the chip as well so there's no point in resetting here.
  2014. */
  2015. }
  2016. #ifdef CONFIG_PM
  2017. static int ath10k_pci_hif_suspend(struct ath10k *ar)
  2018. {
  2019. /* The grace timer can still be counting down and ar->ps_awake be true.
  2020. * It is known that the device may be asleep after resuming regardless
  2021. * of the SoC powersave state before suspending. Hence make sure the
  2022. * device is asleep before proceeding.
  2023. */
  2024. ath10k_pci_sleep_sync(ar);
  2025. return 0;
  2026. }
  2027. static int ath10k_pci_hif_resume(struct ath10k *ar)
  2028. {
  2029. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  2030. struct pci_dev *pdev = ar_pci->pdev;
  2031. u32 val;
  2032. int ret = 0;
  2033. if (ar_pci->pci_ps == 0) {
  2034. ret = ath10k_pci_force_wake(ar);
  2035. if (ret) {
  2036. ath10k_err(ar, "failed to wake up target: %d\n", ret);
  2037. return ret;
  2038. }
  2039. }
  2040. /* Suspend/Resume resets the PCI configuration space, so we have to
  2041. * re-disable the RETRY_TIMEOUT register (0x41) to keep PCI Tx retries
  2042. * from interfering with C3 CPU state. pci_restore_state won't help
  2043. * here since it only restores the first 64 bytes pci config header.
  2044. */
  2045. pci_read_config_dword(pdev, 0x40, &val);
  2046. if ((val & 0x0000ff00) != 0)
  2047. pci_write_config_dword(pdev, 0x40, val & 0xffff00ff);
  2048. return ret;
  2049. }
  2050. #endif
  2051. static const struct ath10k_hif_ops ath10k_pci_hif_ops = {
  2052. .tx_sg = ath10k_pci_hif_tx_sg,
  2053. .diag_read = ath10k_pci_hif_diag_read,
  2054. .diag_write = ath10k_pci_diag_write_mem,
  2055. .exchange_bmi_msg = ath10k_pci_hif_exchange_bmi_msg,
  2056. .start = ath10k_pci_hif_start,
  2057. .stop = ath10k_pci_hif_stop,
  2058. .map_service_to_pipe = ath10k_pci_hif_map_service_to_pipe,
  2059. .get_default_pipe = ath10k_pci_hif_get_default_pipe,
  2060. .send_complete_check = ath10k_pci_hif_send_complete_check,
  2061. .get_free_queue_number = ath10k_pci_hif_get_free_queue_number,
  2062. .power_up = ath10k_pci_hif_power_up,
  2063. .power_down = ath10k_pci_hif_power_down,
  2064. .read32 = ath10k_pci_read32,
  2065. .write32 = ath10k_pci_write32,
  2066. #ifdef CONFIG_PM
  2067. .suspend = ath10k_pci_hif_suspend,
  2068. .resume = ath10k_pci_hif_resume,
  2069. #endif
  2070. };
  2071. static void ath10k_pci_ce_tasklet(unsigned long ptr)
  2072. {
  2073. struct ath10k_pci_pipe *pipe = (struct ath10k_pci_pipe *)ptr;
  2074. struct ath10k_pci *ar_pci = pipe->ar_pci;
  2075. ath10k_ce_per_engine_service(ar_pci->ar, pipe->pipe_num);
  2076. }
  2077. static void ath10k_msi_err_tasklet(unsigned long data)
  2078. {
  2079. struct ath10k *ar = (struct ath10k *)data;
  2080. if (!ath10k_pci_has_fw_crashed(ar)) {
  2081. ath10k_warn(ar, "received unsolicited fw crash interrupt\n");
  2082. return;
  2083. }
  2084. ath10k_pci_irq_disable(ar);
  2085. ath10k_pci_fw_crashed_clear(ar);
  2086. ath10k_pci_fw_crashed_dump(ar);
  2087. }
  2088. /*
  2089. * Handler for a per-engine interrupt on a PARTICULAR CE.
  2090. * This is used in cases where each CE has a private MSI interrupt.
  2091. */
  2092. static irqreturn_t ath10k_pci_per_engine_handler(int irq, void *arg)
  2093. {
  2094. struct ath10k *ar = arg;
  2095. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  2096. int ce_id = irq - ar_pci->pdev->irq - MSI_ASSIGN_CE_INITIAL;
  2097. if (ce_id < 0 || ce_id >= ARRAY_SIZE(ar_pci->pipe_info)) {
  2098. ath10k_warn(ar, "unexpected/invalid irq %d ce_id %d\n", irq,
  2099. ce_id);
  2100. return IRQ_HANDLED;
  2101. }
  2102. /*
  2103. * NOTE: We are able to derive ce_id from irq because we
  2104. * use a one-to-one mapping for CE's 0..5.
  2105. * CE's 6 & 7 do not use interrupts at all.
  2106. *
  2107. * This mapping must be kept in sync with the mapping
  2108. * used by firmware.
  2109. */
  2110. tasklet_schedule(&ar_pci->pipe_info[ce_id].intr);
  2111. return IRQ_HANDLED;
  2112. }
  2113. static irqreturn_t ath10k_pci_msi_fw_handler(int irq, void *arg)
  2114. {
  2115. struct ath10k *ar = arg;
  2116. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  2117. tasklet_schedule(&ar_pci->msi_fw_err);
  2118. return IRQ_HANDLED;
  2119. }
  2120. /*
  2121. * Top-level interrupt handler for all PCI interrupts from a Target.
  2122. * When a block of MSI interrupts is allocated, this top-level handler
  2123. * is not used; instead, we directly call the correct sub-handler.
  2124. */
  2125. static irqreturn_t ath10k_pci_interrupt_handler(int irq, void *arg)
  2126. {
  2127. struct ath10k *ar = arg;
  2128. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  2129. int ret;
  2130. if (ar_pci->pci_ps == 0) {
  2131. ret = ath10k_pci_force_wake(ar);
  2132. if (ret) {
  2133. ath10k_warn(ar, "failed to wake device up on irq: %d\n",
  2134. ret);
  2135. return IRQ_NONE;
  2136. }
  2137. }
  2138. if (ar_pci->num_msi_intrs == 0) {
  2139. if (!ath10k_pci_irq_pending(ar))
  2140. return IRQ_NONE;
  2141. ath10k_pci_disable_and_clear_legacy_irq(ar);
  2142. }
  2143. tasklet_schedule(&ar_pci->intr_tq);
  2144. return IRQ_HANDLED;
  2145. }
  2146. static void ath10k_pci_tasklet(unsigned long data)
  2147. {
  2148. struct ath10k *ar = (struct ath10k *)data;
  2149. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  2150. if (ath10k_pci_has_fw_crashed(ar)) {
  2151. ath10k_pci_irq_disable(ar);
  2152. ath10k_pci_fw_crashed_clear(ar);
  2153. ath10k_pci_fw_crashed_dump(ar);
  2154. return;
  2155. }
  2156. ath10k_ce_per_engine_service_any(ar);
  2157. /* Re-enable legacy irq that was disabled in the irq handler */
  2158. if (ar_pci->num_msi_intrs == 0)
  2159. ath10k_pci_enable_legacy_irq(ar);
  2160. }
  2161. static int ath10k_pci_request_irq_msix(struct ath10k *ar)
  2162. {
  2163. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  2164. int ret, i;
  2165. ret = request_irq(ar_pci->pdev->irq + MSI_ASSIGN_FW,
  2166. ath10k_pci_msi_fw_handler,
  2167. IRQF_SHARED, "ath10k_pci", ar);
  2168. if (ret) {
  2169. ath10k_warn(ar, "failed to request MSI-X fw irq %d: %d\n",
  2170. ar_pci->pdev->irq + MSI_ASSIGN_FW, ret);
  2171. return ret;
  2172. }
  2173. for (i = MSI_ASSIGN_CE_INITIAL; i <= MSI_ASSIGN_CE_MAX; i++) {
  2174. ret = request_irq(ar_pci->pdev->irq + i,
  2175. ath10k_pci_per_engine_handler,
  2176. IRQF_SHARED, "ath10k_pci", ar);
  2177. if (ret) {
  2178. ath10k_warn(ar, "failed to request MSI-X ce irq %d: %d\n",
  2179. ar_pci->pdev->irq + i, ret);
  2180. for (i--; i >= MSI_ASSIGN_CE_INITIAL; i--)
  2181. free_irq(ar_pci->pdev->irq + i, ar);
  2182. free_irq(ar_pci->pdev->irq + MSI_ASSIGN_FW, ar);
  2183. return ret;
  2184. }
  2185. }
  2186. return 0;
  2187. }
  2188. static int ath10k_pci_request_irq_msi(struct ath10k *ar)
  2189. {
  2190. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  2191. int ret;
  2192. ret = request_irq(ar_pci->pdev->irq,
  2193. ath10k_pci_interrupt_handler,
  2194. IRQF_SHARED, "ath10k_pci", ar);
  2195. if (ret) {
  2196. ath10k_warn(ar, "failed to request MSI irq %d: %d\n",
  2197. ar_pci->pdev->irq, ret);
  2198. return ret;
  2199. }
  2200. return 0;
  2201. }
  2202. static int ath10k_pci_request_irq_legacy(struct ath10k *ar)
  2203. {
  2204. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  2205. int ret;
  2206. ret = request_irq(ar_pci->pdev->irq,
  2207. ath10k_pci_interrupt_handler,
  2208. IRQF_SHARED, "ath10k_pci", ar);
  2209. if (ret) {
  2210. ath10k_warn(ar, "failed to request legacy irq %d: %d\n",
  2211. ar_pci->pdev->irq, ret);
  2212. return ret;
  2213. }
  2214. return 0;
  2215. }
  2216. static int ath10k_pci_request_irq(struct ath10k *ar)
  2217. {
  2218. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  2219. switch (ar_pci->num_msi_intrs) {
  2220. case 0:
  2221. return ath10k_pci_request_irq_legacy(ar);
  2222. case 1:
  2223. return ath10k_pci_request_irq_msi(ar);
  2224. default:
  2225. return ath10k_pci_request_irq_msix(ar);
  2226. }
  2227. }
  2228. static void ath10k_pci_free_irq(struct ath10k *ar)
  2229. {
  2230. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  2231. int i;
  2232. /* There's at least one interrupt irregardless whether its legacy INTR
  2233. * or MSI or MSI-X */
  2234. for (i = 0; i < max(1, ar_pci->num_msi_intrs); i++)
  2235. free_irq(ar_pci->pdev->irq + i, ar);
  2236. }
  2237. static void ath10k_pci_init_irq_tasklets(struct ath10k *ar)
  2238. {
  2239. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  2240. int i;
  2241. tasklet_init(&ar_pci->intr_tq, ath10k_pci_tasklet, (unsigned long)ar);
  2242. tasklet_init(&ar_pci->msi_fw_err, ath10k_msi_err_tasklet,
  2243. (unsigned long)ar);
  2244. for (i = 0; i < CE_COUNT; i++) {
  2245. ar_pci->pipe_info[i].ar_pci = ar_pci;
  2246. tasklet_init(&ar_pci->pipe_info[i].intr, ath10k_pci_ce_tasklet,
  2247. (unsigned long)&ar_pci->pipe_info[i]);
  2248. }
  2249. }
  2250. static int ath10k_pci_init_irq(struct ath10k *ar)
  2251. {
  2252. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  2253. int ret;
  2254. ath10k_pci_init_irq_tasklets(ar);
  2255. if (ath10k_pci_irq_mode != ATH10K_PCI_IRQ_AUTO)
  2256. ath10k_info(ar, "limiting irq mode to: %d\n",
  2257. ath10k_pci_irq_mode);
  2258. /* Try MSI-X */
  2259. if (ath10k_pci_irq_mode == ATH10K_PCI_IRQ_AUTO) {
  2260. ar_pci->num_msi_intrs = MSI_ASSIGN_CE_MAX + 1;
  2261. ret = pci_enable_msi_range(ar_pci->pdev, ar_pci->num_msi_intrs,
  2262. ar_pci->num_msi_intrs);
  2263. if (ret > 0)
  2264. return 0;
  2265. /* fall-through */
  2266. }
  2267. /* Try MSI */
  2268. if (ath10k_pci_irq_mode != ATH10K_PCI_IRQ_LEGACY) {
  2269. ar_pci->num_msi_intrs = 1;
  2270. ret = pci_enable_msi(ar_pci->pdev);
  2271. if (ret == 0)
  2272. return 0;
  2273. /* fall-through */
  2274. }
  2275. /* Try legacy irq
  2276. *
  2277. * A potential race occurs here: The CORE_BASE write
  2278. * depends on target correctly decoding AXI address but
  2279. * host won't know when target writes BAR to CORE_CTRL.
  2280. * This write might get lost if target has NOT written BAR.
  2281. * For now, fix the race by repeating the write in below
  2282. * synchronization checking. */
  2283. ar_pci->num_msi_intrs = 0;
  2284. ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
  2285. PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL);
  2286. return 0;
  2287. }
  2288. static void ath10k_pci_deinit_irq_legacy(struct ath10k *ar)
  2289. {
  2290. ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
  2291. 0);
  2292. }
  2293. static int ath10k_pci_deinit_irq(struct ath10k *ar)
  2294. {
  2295. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  2296. switch (ar_pci->num_msi_intrs) {
  2297. case 0:
  2298. ath10k_pci_deinit_irq_legacy(ar);
  2299. break;
  2300. default:
  2301. pci_disable_msi(ar_pci->pdev);
  2302. break;
  2303. }
  2304. return 0;
  2305. }
  2306. static int ath10k_pci_wait_for_target_init(struct ath10k *ar)
  2307. {
  2308. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  2309. unsigned long timeout;
  2310. u32 val;
  2311. ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot waiting target to initialise\n");
  2312. timeout = jiffies + msecs_to_jiffies(ATH10K_PCI_TARGET_WAIT);
  2313. do {
  2314. val = ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS);
  2315. ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot target indicator %x\n",
  2316. val);
  2317. /* target should never return this */
  2318. if (val == 0xffffffff)
  2319. continue;
  2320. /* the device has crashed so don't bother trying anymore */
  2321. if (val & FW_IND_EVENT_PENDING)
  2322. break;
  2323. if (val & FW_IND_INITIALIZED)
  2324. break;
  2325. if (ar_pci->num_msi_intrs == 0)
  2326. /* Fix potential race by repeating CORE_BASE writes */
  2327. ath10k_pci_enable_legacy_irq(ar);
  2328. mdelay(10);
  2329. } while (time_before(jiffies, timeout));
  2330. ath10k_pci_disable_and_clear_legacy_irq(ar);
  2331. ath10k_pci_irq_msi_fw_mask(ar);
  2332. if (val == 0xffffffff) {
  2333. ath10k_err(ar, "failed to read device register, device is gone\n");
  2334. return -EIO;
  2335. }
  2336. if (val & FW_IND_EVENT_PENDING) {
  2337. ath10k_warn(ar, "device has crashed during init\n");
  2338. return -ECOMM;
  2339. }
  2340. if (!(val & FW_IND_INITIALIZED)) {
  2341. ath10k_err(ar, "failed to receive initialized event from target: %08x\n",
  2342. val);
  2343. return -ETIMEDOUT;
  2344. }
  2345. ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot target initialised\n");
  2346. return 0;
  2347. }
  2348. static int ath10k_pci_cold_reset(struct ath10k *ar)
  2349. {
  2350. u32 val;
  2351. ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot cold reset\n");
  2352. spin_lock_bh(&ar->data_lock);
  2353. ar->stats.fw_cold_reset_counter++;
  2354. spin_unlock_bh(&ar->data_lock);
  2355. /* Put Target, including PCIe, into RESET. */
  2356. val = ath10k_pci_reg_read32(ar, SOC_GLOBAL_RESET_ADDRESS);
  2357. val |= 1;
  2358. ath10k_pci_reg_write32(ar, SOC_GLOBAL_RESET_ADDRESS, val);
  2359. /* After writing into SOC_GLOBAL_RESET to put device into
  2360. * reset and pulling out of reset pcie may not be stable
  2361. * for any immediate pcie register access and cause bus error,
  2362. * add delay before any pcie access request to fix this issue.
  2363. */
  2364. msleep(20);
  2365. /* Pull Target, including PCIe, out of RESET. */
  2366. val &= ~1;
  2367. ath10k_pci_reg_write32(ar, SOC_GLOBAL_RESET_ADDRESS, val);
  2368. msleep(20);
  2369. ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot cold reset complete\n");
  2370. return 0;
  2371. }
  2372. static int ath10k_pci_claim(struct ath10k *ar)
  2373. {
  2374. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  2375. struct pci_dev *pdev = ar_pci->pdev;
  2376. int ret;
  2377. pci_set_drvdata(pdev, ar);
  2378. ret = pci_enable_device(pdev);
  2379. if (ret) {
  2380. ath10k_err(ar, "failed to enable pci device: %d\n", ret);
  2381. return ret;
  2382. }
  2383. ret = pci_request_region(pdev, BAR_NUM, "ath");
  2384. if (ret) {
  2385. ath10k_err(ar, "failed to request region BAR%d: %d\n", BAR_NUM,
  2386. ret);
  2387. goto err_device;
  2388. }
  2389. /* Target expects 32 bit DMA. Enforce it. */
  2390. ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  2391. if (ret) {
  2392. ath10k_err(ar, "failed to set dma mask to 32-bit: %d\n", ret);
  2393. goto err_region;
  2394. }
  2395. ret = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  2396. if (ret) {
  2397. ath10k_err(ar, "failed to set consistent dma mask to 32-bit: %d\n",
  2398. ret);
  2399. goto err_region;
  2400. }
  2401. pci_set_master(pdev);
  2402. /* Arrange for access to Target SoC registers. */
  2403. ar_pci->mem_len = pci_resource_len(pdev, BAR_NUM);
  2404. ar_pci->mem = pci_iomap(pdev, BAR_NUM, 0);
  2405. if (!ar_pci->mem) {
  2406. ath10k_err(ar, "failed to iomap BAR%d\n", BAR_NUM);
  2407. ret = -EIO;
  2408. goto err_master;
  2409. }
  2410. ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot pci_mem 0x%p\n", ar_pci->mem);
  2411. return 0;
  2412. err_master:
  2413. pci_clear_master(pdev);
  2414. err_region:
  2415. pci_release_region(pdev, BAR_NUM);
  2416. err_device:
  2417. pci_disable_device(pdev);
  2418. return ret;
  2419. }
  2420. static void ath10k_pci_release(struct ath10k *ar)
  2421. {
  2422. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  2423. struct pci_dev *pdev = ar_pci->pdev;
  2424. pci_iounmap(pdev, ar_pci->mem);
  2425. pci_release_region(pdev, BAR_NUM);
  2426. pci_clear_master(pdev);
  2427. pci_disable_device(pdev);
  2428. }
  2429. static bool ath10k_pci_chip_is_supported(u32 dev_id, u32 chip_id)
  2430. {
  2431. const struct ath10k_pci_supp_chip *supp_chip;
  2432. int i;
  2433. u32 rev_id = MS(chip_id, SOC_CHIP_ID_REV);
  2434. for (i = 0; i < ARRAY_SIZE(ath10k_pci_supp_chips); i++) {
  2435. supp_chip = &ath10k_pci_supp_chips[i];
  2436. if (supp_chip->dev_id == dev_id &&
  2437. supp_chip->rev_id == rev_id)
  2438. return true;
  2439. }
  2440. return false;
  2441. }
  2442. static int ath10k_pci_probe(struct pci_dev *pdev,
  2443. const struct pci_device_id *pci_dev)
  2444. {
  2445. int ret = 0;
  2446. struct ath10k *ar;
  2447. struct ath10k_pci *ar_pci;
  2448. enum ath10k_hw_rev hw_rev;
  2449. u32 chip_id;
  2450. bool pci_ps;
  2451. switch (pci_dev->device) {
  2452. case QCA988X_2_0_DEVICE_ID:
  2453. hw_rev = ATH10K_HW_QCA988X;
  2454. pci_ps = false;
  2455. break;
  2456. case QCA6164_2_1_DEVICE_ID:
  2457. case QCA6174_2_1_DEVICE_ID:
  2458. hw_rev = ATH10K_HW_QCA6174;
  2459. pci_ps = true;
  2460. break;
  2461. case QCA99X0_2_0_DEVICE_ID:
  2462. hw_rev = ATH10K_HW_QCA99X0;
  2463. pci_ps = false;
  2464. break;
  2465. case QCA9377_1_0_DEVICE_ID:
  2466. hw_rev = ATH10K_HW_QCA9377;
  2467. pci_ps = true;
  2468. break;
  2469. default:
  2470. WARN_ON(1);
  2471. return -ENOTSUPP;
  2472. }
  2473. ar = ath10k_core_create(sizeof(*ar_pci), &pdev->dev, ATH10K_BUS_PCI,
  2474. hw_rev, &ath10k_pci_hif_ops);
  2475. if (!ar) {
  2476. dev_err(&pdev->dev, "failed to allocate core\n");
  2477. return -ENOMEM;
  2478. }
  2479. ath10k_dbg(ar, ATH10K_DBG_BOOT, "pci probe %04x:%04x %04x:%04x\n",
  2480. pdev->vendor, pdev->device,
  2481. pdev->subsystem_vendor, pdev->subsystem_device);
  2482. ar_pci = ath10k_pci_priv(ar);
  2483. ar_pci->pdev = pdev;
  2484. ar_pci->dev = &pdev->dev;
  2485. ar_pci->ar = ar;
  2486. ar->dev_id = pci_dev->device;
  2487. ar_pci->pci_ps = pci_ps;
  2488. ar->id.vendor = pdev->vendor;
  2489. ar->id.device = pdev->device;
  2490. ar->id.subsystem_vendor = pdev->subsystem_vendor;
  2491. ar->id.subsystem_device = pdev->subsystem_device;
  2492. spin_lock_init(&ar_pci->ce_lock);
  2493. spin_lock_init(&ar_pci->ps_lock);
  2494. setup_timer(&ar_pci->rx_post_retry, ath10k_pci_rx_replenish_retry,
  2495. (unsigned long)ar);
  2496. setup_timer(&ar_pci->ps_timer, ath10k_pci_ps_timer,
  2497. (unsigned long)ar);
  2498. ret = ath10k_pci_claim(ar);
  2499. if (ret) {
  2500. ath10k_err(ar, "failed to claim device: %d\n", ret);
  2501. goto err_core_destroy;
  2502. }
  2503. if (QCA_REV_6174(ar) || QCA_REV_9377(ar))
  2504. ath10k_pci_override_ce_config(ar);
  2505. ret = ath10k_pci_alloc_pipes(ar);
  2506. if (ret) {
  2507. ath10k_err(ar, "failed to allocate copy engine pipes: %d\n",
  2508. ret);
  2509. goto err_sleep;
  2510. }
  2511. ath10k_pci_ce_deinit(ar);
  2512. ath10k_pci_irq_disable(ar);
  2513. if (ar_pci->pci_ps == 0) {
  2514. ret = ath10k_pci_force_wake(ar);
  2515. if (ret) {
  2516. ath10k_warn(ar, "failed to wake up device : %d\n", ret);
  2517. goto err_free_pipes;
  2518. }
  2519. }
  2520. ret = ath10k_pci_init_irq(ar);
  2521. if (ret) {
  2522. ath10k_err(ar, "failed to init irqs: %d\n", ret);
  2523. goto err_free_pipes;
  2524. }
  2525. ath10k_info(ar, "pci irq %s interrupts %d irq_mode %d reset_mode %d\n",
  2526. ath10k_pci_get_irq_method(ar), ar_pci->num_msi_intrs,
  2527. ath10k_pci_irq_mode, ath10k_pci_reset_mode);
  2528. ret = ath10k_pci_request_irq(ar);
  2529. if (ret) {
  2530. ath10k_warn(ar, "failed to request irqs: %d\n", ret);
  2531. goto err_deinit_irq;
  2532. }
  2533. ret = ath10k_pci_chip_reset(ar);
  2534. if (ret) {
  2535. ath10k_err(ar, "failed to reset chip: %d\n", ret);
  2536. goto err_free_irq;
  2537. }
  2538. chip_id = ath10k_pci_soc_read32(ar, SOC_CHIP_ID_ADDRESS);
  2539. if (chip_id == 0xffffffff) {
  2540. ath10k_err(ar, "failed to get chip id\n");
  2541. goto err_free_irq;
  2542. }
  2543. if (!ath10k_pci_chip_is_supported(pdev->device, chip_id)) {
  2544. ath10k_err(ar, "device %04x with chip_id %08x isn't supported\n",
  2545. pdev->device, chip_id);
  2546. goto err_free_irq;
  2547. }
  2548. ret = ath10k_core_register(ar, chip_id);
  2549. if (ret) {
  2550. ath10k_err(ar, "failed to register driver core: %d\n", ret);
  2551. goto err_free_irq;
  2552. }
  2553. return 0;
  2554. err_free_irq:
  2555. ath10k_pci_free_irq(ar);
  2556. ath10k_pci_kill_tasklet(ar);
  2557. err_deinit_irq:
  2558. ath10k_pci_deinit_irq(ar);
  2559. err_free_pipes:
  2560. ath10k_pci_free_pipes(ar);
  2561. err_sleep:
  2562. ath10k_pci_sleep_sync(ar);
  2563. ath10k_pci_release(ar);
  2564. err_core_destroy:
  2565. ath10k_core_destroy(ar);
  2566. return ret;
  2567. }
  2568. static void ath10k_pci_remove(struct pci_dev *pdev)
  2569. {
  2570. struct ath10k *ar = pci_get_drvdata(pdev);
  2571. struct ath10k_pci *ar_pci;
  2572. ath10k_dbg(ar, ATH10K_DBG_PCI, "pci remove\n");
  2573. if (!ar)
  2574. return;
  2575. ar_pci = ath10k_pci_priv(ar);
  2576. if (!ar_pci)
  2577. return;
  2578. ath10k_core_unregister(ar);
  2579. ath10k_pci_free_irq(ar);
  2580. ath10k_pci_kill_tasklet(ar);
  2581. ath10k_pci_deinit_irq(ar);
  2582. ath10k_pci_ce_deinit(ar);
  2583. ath10k_pci_free_pipes(ar);
  2584. ath10k_pci_sleep_sync(ar);
  2585. ath10k_pci_release(ar);
  2586. ath10k_core_destroy(ar);
  2587. }
  2588. MODULE_DEVICE_TABLE(pci, ath10k_pci_id_table);
  2589. static struct pci_driver ath10k_pci_driver = {
  2590. .name = "ath10k_pci",
  2591. .id_table = ath10k_pci_id_table,
  2592. .probe = ath10k_pci_probe,
  2593. .remove = ath10k_pci_remove,
  2594. };
  2595. static int __init ath10k_pci_init(void)
  2596. {
  2597. int ret;
  2598. ret = pci_register_driver(&ath10k_pci_driver);
  2599. if (ret)
  2600. printk(KERN_ERR "failed to register ath10k pci driver: %d\n",
  2601. ret);
  2602. return ret;
  2603. }
  2604. module_init(ath10k_pci_init);
  2605. static void __exit ath10k_pci_exit(void)
  2606. {
  2607. pci_unregister_driver(&ath10k_pci_driver);
  2608. }
  2609. module_exit(ath10k_pci_exit);
  2610. MODULE_AUTHOR("Qualcomm Atheros");
  2611. MODULE_DESCRIPTION("Driver support for Atheros QCA988X PCIe devices");
  2612. MODULE_LICENSE("Dual BSD/GPL");
  2613. /* QCA988x 2.0 firmware files */
  2614. MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" QCA988X_HW_2_0_FW_FILE);
  2615. MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" ATH10K_FW_API2_FILE);
  2616. MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" ATH10K_FW_API3_FILE);
  2617. MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" ATH10K_FW_API4_FILE);
  2618. MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" ATH10K_FW_API5_FILE);
  2619. MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" QCA988X_HW_2_0_BOARD_DATA_FILE);
  2620. MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" ATH10K_BOARD_API2_FILE);
  2621. /* QCA6174 2.1 firmware files */
  2622. MODULE_FIRMWARE(QCA6174_HW_2_1_FW_DIR "/" ATH10K_FW_API4_FILE);
  2623. MODULE_FIRMWARE(QCA6174_HW_2_1_FW_DIR "/" ATH10K_FW_API5_FILE);
  2624. MODULE_FIRMWARE(QCA6174_HW_2_1_FW_DIR "/" QCA6174_HW_2_1_BOARD_DATA_FILE);
  2625. MODULE_FIRMWARE(QCA6174_HW_2_1_FW_DIR "/" ATH10K_BOARD_API2_FILE);
  2626. /* QCA6174 3.1 firmware files */
  2627. MODULE_FIRMWARE(QCA6174_HW_3_0_FW_DIR "/" ATH10K_FW_API4_FILE);
  2628. MODULE_FIRMWARE(QCA6174_HW_3_0_FW_DIR "/" ATH10K_FW_API5_FILE);
  2629. MODULE_FIRMWARE(QCA6174_HW_3_0_FW_DIR "/" QCA6174_HW_3_0_BOARD_DATA_FILE);
  2630. MODULE_FIRMWARE(QCA6174_HW_3_0_FW_DIR "/" ATH10K_BOARD_API2_FILE);
  2631. /* QCA9377 1.0 firmware files */
  2632. MODULE_FIRMWARE(QCA9377_HW_1_0_FW_DIR "/" ATH10K_FW_API5_FILE);
  2633. MODULE_FIRMWARE(QCA9377_HW_1_0_FW_DIR "/" QCA9377_HW_1_0_BOARD_DATA_FILE);