pci.c 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642
  1. /*
  2. * Copyright (c) 2005-2011 Atheros Communications Inc.
  3. * Copyright (c) 2011-2013 Qualcomm Atheros, Inc.
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  15. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #include <linux/pci.h>
  18. #include <linux/module.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/spinlock.h>
  21. #include <linux/bitops.h>
  22. #include "core.h"
  23. #include "debug.h"
  24. #include "targaddrs.h"
  25. #include "bmi.h"
  26. #include "hif.h"
  27. #include "htc.h"
  28. #include "ce.h"
  29. #include "pci.h"
  30. enum ath10k_pci_irq_mode {
  31. ATH10K_PCI_IRQ_AUTO = 0,
  32. ATH10K_PCI_IRQ_LEGACY = 1,
  33. ATH10K_PCI_IRQ_MSI = 2,
  34. };
  35. enum ath10k_pci_reset_mode {
  36. ATH10K_PCI_RESET_AUTO = 0,
  37. ATH10K_PCI_RESET_WARM_ONLY = 1,
  38. };
  39. static unsigned int ath10k_pci_irq_mode = ATH10K_PCI_IRQ_AUTO;
  40. static unsigned int ath10k_pci_reset_mode = ATH10K_PCI_RESET_AUTO;
  41. module_param_named(irq_mode, ath10k_pci_irq_mode, uint, 0644);
  42. MODULE_PARM_DESC(irq_mode, "0: auto, 1: legacy, 2: msi (default: 0)");
  43. module_param_named(reset_mode, ath10k_pci_reset_mode, uint, 0644);
  44. MODULE_PARM_DESC(reset_mode, "0: auto, 1: warm only (default: 0)");
  45. /* how long wait to wait for target to initialise, in ms */
  46. #define ATH10K_PCI_TARGET_WAIT 3000
  47. #define ATH10K_PCI_NUM_WARM_RESET_ATTEMPTS 3
  48. #define QCA988X_2_0_DEVICE_ID (0x003c)
  49. static const struct pci_device_id ath10k_pci_id_table[] = {
  50. { PCI_VDEVICE(ATHEROS, QCA988X_2_0_DEVICE_ID) }, /* PCI-E QCA988X V2 */
  51. {0}
  52. };
  53. static void ath10k_pci_buffer_cleanup(struct ath10k *ar);
  54. static int ath10k_pci_cold_reset(struct ath10k *ar);
  55. static int ath10k_pci_warm_reset(struct ath10k *ar);
  56. static int ath10k_pci_wait_for_target_init(struct ath10k *ar);
  57. static int ath10k_pci_init_irq(struct ath10k *ar);
  58. static int ath10k_pci_deinit_irq(struct ath10k *ar);
  59. static int ath10k_pci_request_irq(struct ath10k *ar);
  60. static void ath10k_pci_free_irq(struct ath10k *ar);
  61. static int ath10k_pci_bmi_wait(struct ath10k_ce_pipe *tx_pipe,
  62. struct ath10k_ce_pipe *rx_pipe,
  63. struct bmi_xfer *xfer);
  64. static const struct ce_attr host_ce_config_wlan[] = {
  65. /* CE0: host->target HTC control and raw streams */
  66. {
  67. .flags = CE_ATTR_FLAGS,
  68. .src_nentries = 16,
  69. .src_sz_max = 256,
  70. .dest_nentries = 0,
  71. },
  72. /* CE1: target->host HTT + HTC control */
  73. {
  74. .flags = CE_ATTR_FLAGS,
  75. .src_nentries = 0,
  76. .src_sz_max = 512,
  77. .dest_nentries = 512,
  78. },
  79. /* CE2: target->host WMI */
  80. {
  81. .flags = CE_ATTR_FLAGS,
  82. .src_nentries = 0,
  83. .src_sz_max = 2048,
  84. .dest_nentries = 32,
  85. },
  86. /* CE3: host->target WMI */
  87. {
  88. .flags = CE_ATTR_FLAGS,
  89. .src_nentries = 32,
  90. .src_sz_max = 2048,
  91. .dest_nentries = 0,
  92. },
  93. /* CE4: host->target HTT */
  94. {
  95. .flags = CE_ATTR_FLAGS | CE_ATTR_DIS_INTR,
  96. .src_nentries = CE_HTT_H2T_MSG_SRC_NENTRIES,
  97. .src_sz_max = 256,
  98. .dest_nentries = 0,
  99. },
  100. /* CE5: unused */
  101. {
  102. .flags = CE_ATTR_FLAGS,
  103. .src_nentries = 0,
  104. .src_sz_max = 0,
  105. .dest_nentries = 0,
  106. },
  107. /* CE6: target autonomous hif_memcpy */
  108. {
  109. .flags = CE_ATTR_FLAGS,
  110. .src_nentries = 0,
  111. .src_sz_max = 0,
  112. .dest_nentries = 0,
  113. },
  114. /* CE7: ce_diag, the Diagnostic Window */
  115. {
  116. .flags = CE_ATTR_FLAGS,
  117. .src_nentries = 2,
  118. .src_sz_max = DIAG_TRANSFER_LIMIT,
  119. .dest_nentries = 2,
  120. },
  121. };
  122. /* Target firmware's Copy Engine configuration. */
  123. static const struct ce_pipe_config target_ce_config_wlan[] = {
  124. /* CE0: host->target HTC control and raw streams */
  125. {
  126. .pipenum = __cpu_to_le32(0),
  127. .pipedir = __cpu_to_le32(PIPEDIR_OUT),
  128. .nentries = __cpu_to_le32(32),
  129. .nbytes_max = __cpu_to_le32(256),
  130. .flags = __cpu_to_le32(CE_ATTR_FLAGS),
  131. .reserved = __cpu_to_le32(0),
  132. },
  133. /* CE1: target->host HTT + HTC control */
  134. {
  135. .pipenum = __cpu_to_le32(1),
  136. .pipedir = __cpu_to_le32(PIPEDIR_IN),
  137. .nentries = __cpu_to_le32(32),
  138. .nbytes_max = __cpu_to_le32(512),
  139. .flags = __cpu_to_le32(CE_ATTR_FLAGS),
  140. .reserved = __cpu_to_le32(0),
  141. },
  142. /* CE2: target->host WMI */
  143. {
  144. .pipenum = __cpu_to_le32(2),
  145. .pipedir = __cpu_to_le32(PIPEDIR_IN),
  146. .nentries = __cpu_to_le32(32),
  147. .nbytes_max = __cpu_to_le32(2048),
  148. .flags = __cpu_to_le32(CE_ATTR_FLAGS),
  149. .reserved = __cpu_to_le32(0),
  150. },
  151. /* CE3: host->target WMI */
  152. {
  153. .pipenum = __cpu_to_le32(3),
  154. .pipedir = __cpu_to_le32(PIPEDIR_OUT),
  155. .nentries = __cpu_to_le32(32),
  156. .nbytes_max = __cpu_to_le32(2048),
  157. .flags = __cpu_to_le32(CE_ATTR_FLAGS),
  158. .reserved = __cpu_to_le32(0),
  159. },
  160. /* CE4: host->target HTT */
  161. {
  162. .pipenum = __cpu_to_le32(4),
  163. .pipedir = __cpu_to_le32(PIPEDIR_OUT),
  164. .nentries = __cpu_to_le32(256),
  165. .nbytes_max = __cpu_to_le32(256),
  166. .flags = __cpu_to_le32(CE_ATTR_FLAGS),
  167. .reserved = __cpu_to_le32(0),
  168. },
  169. /* NB: 50% of src nentries, since tx has 2 frags */
  170. /* CE5: unused */
  171. {
  172. .pipenum = __cpu_to_le32(5),
  173. .pipedir = __cpu_to_le32(PIPEDIR_OUT),
  174. .nentries = __cpu_to_le32(32),
  175. .nbytes_max = __cpu_to_le32(2048),
  176. .flags = __cpu_to_le32(CE_ATTR_FLAGS),
  177. .reserved = __cpu_to_le32(0),
  178. },
  179. /* CE6: Reserved for target autonomous hif_memcpy */
  180. {
  181. .pipenum = __cpu_to_le32(6),
  182. .pipedir = __cpu_to_le32(PIPEDIR_INOUT),
  183. .nentries = __cpu_to_le32(32),
  184. .nbytes_max = __cpu_to_le32(4096),
  185. .flags = __cpu_to_le32(CE_ATTR_FLAGS),
  186. .reserved = __cpu_to_le32(0),
  187. },
  188. /* CE7 used only by Host */
  189. };
  190. /*
  191. * Map from service/endpoint to Copy Engine.
  192. * This table is derived from the CE_PCI TABLE, above.
  193. * It is passed to the Target at startup for use by firmware.
  194. */
  195. static const struct service_to_pipe target_service_to_ce_map_wlan[] = {
  196. {
  197. __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_VO),
  198. __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
  199. __cpu_to_le32(3),
  200. },
  201. {
  202. __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_VO),
  203. __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
  204. __cpu_to_le32(2),
  205. },
  206. {
  207. __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_BK),
  208. __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
  209. __cpu_to_le32(3),
  210. },
  211. {
  212. __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_BK),
  213. __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
  214. __cpu_to_le32(2),
  215. },
  216. {
  217. __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_BE),
  218. __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
  219. __cpu_to_le32(3),
  220. },
  221. {
  222. __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_BE),
  223. __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
  224. __cpu_to_le32(2),
  225. },
  226. {
  227. __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_VI),
  228. __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
  229. __cpu_to_le32(3),
  230. },
  231. {
  232. __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_VI),
  233. __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
  234. __cpu_to_le32(2),
  235. },
  236. {
  237. __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_CONTROL),
  238. __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
  239. __cpu_to_le32(3),
  240. },
  241. {
  242. __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_CONTROL),
  243. __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
  244. __cpu_to_le32(2),
  245. },
  246. {
  247. __cpu_to_le32(ATH10K_HTC_SVC_ID_RSVD_CTRL),
  248. __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
  249. __cpu_to_le32(0),
  250. },
  251. {
  252. __cpu_to_le32(ATH10K_HTC_SVC_ID_RSVD_CTRL),
  253. __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
  254. __cpu_to_le32(1),
  255. },
  256. { /* not used */
  257. __cpu_to_le32(ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS),
  258. __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
  259. __cpu_to_le32(0),
  260. },
  261. { /* not used */
  262. __cpu_to_le32(ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS),
  263. __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
  264. __cpu_to_le32(1),
  265. },
  266. {
  267. __cpu_to_le32(ATH10K_HTC_SVC_ID_HTT_DATA_MSG),
  268. __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
  269. __cpu_to_le32(4),
  270. },
  271. {
  272. __cpu_to_le32(ATH10K_HTC_SVC_ID_HTT_DATA_MSG),
  273. __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
  274. __cpu_to_le32(1),
  275. },
  276. /* (Additions here) */
  277. { /* must be last */
  278. __cpu_to_le32(0),
  279. __cpu_to_le32(0),
  280. __cpu_to_le32(0),
  281. },
  282. };
  283. static bool ath10k_pci_irq_pending(struct ath10k *ar)
  284. {
  285. u32 cause;
  286. /* Check if the shared legacy irq is for us */
  287. cause = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
  288. PCIE_INTR_CAUSE_ADDRESS);
  289. if (cause & (PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL))
  290. return true;
  291. return false;
  292. }
  293. static void ath10k_pci_disable_and_clear_legacy_irq(struct ath10k *ar)
  294. {
  295. /* IMPORTANT: INTR_CLR register has to be set after
  296. * INTR_ENABLE is set to 0, otherwise interrupt can not be
  297. * really cleared. */
  298. ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
  299. 0);
  300. ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_CLR_ADDRESS,
  301. PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL);
  302. /* IMPORTANT: this extra read transaction is required to
  303. * flush the posted write buffer. */
  304. (void)ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
  305. PCIE_INTR_ENABLE_ADDRESS);
  306. }
  307. static void ath10k_pci_enable_legacy_irq(struct ath10k *ar)
  308. {
  309. ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS +
  310. PCIE_INTR_ENABLE_ADDRESS,
  311. PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL);
  312. /* IMPORTANT: this extra read transaction is required to
  313. * flush the posted write buffer. */
  314. (void)ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS +
  315. PCIE_INTR_ENABLE_ADDRESS);
  316. }
  317. static inline const char *ath10k_pci_get_irq_method(struct ath10k *ar)
  318. {
  319. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  320. if (ar_pci->num_msi_intrs > 1)
  321. return "msi-x";
  322. if (ar_pci->num_msi_intrs == 1)
  323. return "msi";
  324. return "legacy";
  325. }
  326. static int __ath10k_pci_rx_post_buf(struct ath10k_pci_pipe *pipe)
  327. {
  328. struct ath10k *ar = pipe->hif_ce_state;
  329. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  330. struct ath10k_ce_pipe *ce_pipe = pipe->ce_hdl;
  331. struct sk_buff *skb;
  332. dma_addr_t paddr;
  333. int ret;
  334. lockdep_assert_held(&ar_pci->ce_lock);
  335. skb = dev_alloc_skb(pipe->buf_sz);
  336. if (!skb)
  337. return -ENOMEM;
  338. WARN_ONCE((unsigned long)skb->data & 3, "unaligned skb");
  339. paddr = dma_map_single(ar->dev, skb->data,
  340. skb->len + skb_tailroom(skb),
  341. DMA_FROM_DEVICE);
  342. if (unlikely(dma_mapping_error(ar->dev, paddr))) {
  343. ath10k_warn(ar, "failed to dma map pci rx buf\n");
  344. dev_kfree_skb_any(skb);
  345. return -EIO;
  346. }
  347. ATH10K_SKB_CB(skb)->paddr = paddr;
  348. ret = __ath10k_ce_rx_post_buf(ce_pipe, skb, paddr);
  349. if (ret) {
  350. ath10k_warn(ar, "failed to post pci rx buf: %d\n", ret);
  351. dma_unmap_single(ar->dev, paddr, skb->len + skb_tailroom(skb),
  352. DMA_FROM_DEVICE);
  353. dev_kfree_skb_any(skb);
  354. return ret;
  355. }
  356. return 0;
  357. }
  358. static void __ath10k_pci_rx_post_pipe(struct ath10k_pci_pipe *pipe)
  359. {
  360. struct ath10k *ar = pipe->hif_ce_state;
  361. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  362. struct ath10k_ce_pipe *ce_pipe = pipe->ce_hdl;
  363. int ret, num;
  364. lockdep_assert_held(&ar_pci->ce_lock);
  365. if (pipe->buf_sz == 0)
  366. return;
  367. if (!ce_pipe->dest_ring)
  368. return;
  369. num = __ath10k_ce_rx_num_free_bufs(ce_pipe);
  370. while (num--) {
  371. ret = __ath10k_pci_rx_post_buf(pipe);
  372. if (ret) {
  373. ath10k_warn(ar, "failed to post pci rx buf: %d\n", ret);
  374. mod_timer(&ar_pci->rx_post_retry, jiffies +
  375. ATH10K_PCI_RX_POST_RETRY_MS);
  376. break;
  377. }
  378. }
  379. }
  380. static void ath10k_pci_rx_post_pipe(struct ath10k_pci_pipe *pipe)
  381. {
  382. struct ath10k *ar = pipe->hif_ce_state;
  383. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  384. spin_lock_bh(&ar_pci->ce_lock);
  385. __ath10k_pci_rx_post_pipe(pipe);
  386. spin_unlock_bh(&ar_pci->ce_lock);
  387. }
  388. static void ath10k_pci_rx_post(struct ath10k *ar)
  389. {
  390. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  391. int i;
  392. spin_lock_bh(&ar_pci->ce_lock);
  393. for (i = 0; i < CE_COUNT; i++)
  394. __ath10k_pci_rx_post_pipe(&ar_pci->pipe_info[i]);
  395. spin_unlock_bh(&ar_pci->ce_lock);
  396. }
  397. static void ath10k_pci_rx_replenish_retry(unsigned long ptr)
  398. {
  399. struct ath10k *ar = (void *)ptr;
  400. ath10k_pci_rx_post(ar);
  401. }
  402. /*
  403. * Diagnostic read/write access is provided for startup/config/debug usage.
  404. * Caller must guarantee proper alignment, when applicable, and single user
  405. * at any moment.
  406. */
  407. static int ath10k_pci_diag_read_mem(struct ath10k *ar, u32 address, void *data,
  408. int nbytes)
  409. {
  410. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  411. int ret = 0;
  412. u32 buf;
  413. unsigned int completed_nbytes, orig_nbytes, remaining_bytes;
  414. unsigned int id;
  415. unsigned int flags;
  416. struct ath10k_ce_pipe *ce_diag;
  417. /* Host buffer address in CE space */
  418. u32 ce_data;
  419. dma_addr_t ce_data_base = 0;
  420. void *data_buf = NULL;
  421. int i;
  422. spin_lock_bh(&ar_pci->ce_lock);
  423. ce_diag = ar_pci->ce_diag;
  424. /*
  425. * Allocate a temporary bounce buffer to hold caller's data
  426. * to be DMA'ed from Target. This guarantees
  427. * 1) 4-byte alignment
  428. * 2) Buffer in DMA-able space
  429. */
  430. orig_nbytes = nbytes;
  431. data_buf = (unsigned char *)dma_alloc_coherent(ar->dev,
  432. orig_nbytes,
  433. &ce_data_base,
  434. GFP_ATOMIC);
  435. if (!data_buf) {
  436. ret = -ENOMEM;
  437. goto done;
  438. }
  439. memset(data_buf, 0, orig_nbytes);
  440. remaining_bytes = orig_nbytes;
  441. ce_data = ce_data_base;
  442. while (remaining_bytes) {
  443. nbytes = min_t(unsigned int, remaining_bytes,
  444. DIAG_TRANSFER_LIMIT);
  445. ret = __ath10k_ce_rx_post_buf(ce_diag, NULL, ce_data);
  446. if (ret != 0)
  447. goto done;
  448. /* Request CE to send from Target(!) address to Host buffer */
  449. /*
  450. * The address supplied by the caller is in the
  451. * Target CPU virtual address space.
  452. *
  453. * In order to use this address with the diagnostic CE,
  454. * convert it from Target CPU virtual address space
  455. * to CE address space
  456. */
  457. address = TARG_CPU_SPACE_TO_CE_SPACE(ar, ar_pci->mem,
  458. address);
  459. ret = ath10k_ce_send_nolock(ce_diag, NULL, (u32)address, nbytes, 0,
  460. 0);
  461. if (ret)
  462. goto done;
  463. i = 0;
  464. while (ath10k_ce_completed_send_next_nolock(ce_diag, NULL, &buf,
  465. &completed_nbytes,
  466. &id) != 0) {
  467. mdelay(1);
  468. if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
  469. ret = -EBUSY;
  470. goto done;
  471. }
  472. }
  473. if (nbytes != completed_nbytes) {
  474. ret = -EIO;
  475. goto done;
  476. }
  477. if (buf != (u32)address) {
  478. ret = -EIO;
  479. goto done;
  480. }
  481. i = 0;
  482. while (ath10k_ce_completed_recv_next_nolock(ce_diag, NULL, &buf,
  483. &completed_nbytes,
  484. &id, &flags) != 0) {
  485. mdelay(1);
  486. if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
  487. ret = -EBUSY;
  488. goto done;
  489. }
  490. }
  491. if (nbytes != completed_nbytes) {
  492. ret = -EIO;
  493. goto done;
  494. }
  495. if (buf != ce_data) {
  496. ret = -EIO;
  497. goto done;
  498. }
  499. remaining_bytes -= nbytes;
  500. address += nbytes;
  501. ce_data += nbytes;
  502. }
  503. done:
  504. if (ret == 0)
  505. memcpy(data, data_buf, orig_nbytes);
  506. else
  507. ath10k_warn(ar, "failed to read diag value at 0x%x: %d\n",
  508. address, ret);
  509. if (data_buf)
  510. dma_free_coherent(ar->dev, orig_nbytes, data_buf,
  511. ce_data_base);
  512. spin_unlock_bh(&ar_pci->ce_lock);
  513. return ret;
  514. }
  515. static int ath10k_pci_diag_read32(struct ath10k *ar, u32 address, u32 *value)
  516. {
  517. __le32 val = 0;
  518. int ret;
  519. ret = ath10k_pci_diag_read_mem(ar, address, &val, sizeof(val));
  520. *value = __le32_to_cpu(val);
  521. return ret;
  522. }
  523. static int __ath10k_pci_diag_read_hi(struct ath10k *ar, void *dest,
  524. u32 src, u32 len)
  525. {
  526. u32 host_addr, addr;
  527. int ret;
  528. host_addr = host_interest_item_address(src);
  529. ret = ath10k_pci_diag_read32(ar, host_addr, &addr);
  530. if (ret != 0) {
  531. ath10k_warn(ar, "failed to get memcpy hi address for firmware address %d: %d\n",
  532. src, ret);
  533. return ret;
  534. }
  535. ret = ath10k_pci_diag_read_mem(ar, addr, dest, len);
  536. if (ret != 0) {
  537. ath10k_warn(ar, "failed to memcpy firmware memory from %d (%d B): %d\n",
  538. addr, len, ret);
  539. return ret;
  540. }
  541. return 0;
  542. }
  543. #define ath10k_pci_diag_read_hi(ar, dest, src, len) \
  544. __ath10k_pci_diag_read_hi(ar, dest, HI_ITEM(src), len)
  545. static int ath10k_pci_diag_write_mem(struct ath10k *ar, u32 address,
  546. const void *data, int nbytes)
  547. {
  548. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  549. int ret = 0;
  550. u32 buf;
  551. unsigned int completed_nbytes, orig_nbytes, remaining_bytes;
  552. unsigned int id;
  553. unsigned int flags;
  554. struct ath10k_ce_pipe *ce_diag;
  555. void *data_buf = NULL;
  556. u32 ce_data; /* Host buffer address in CE space */
  557. dma_addr_t ce_data_base = 0;
  558. int i;
  559. spin_lock_bh(&ar_pci->ce_lock);
  560. ce_diag = ar_pci->ce_diag;
  561. /*
  562. * Allocate a temporary bounce buffer to hold caller's data
  563. * to be DMA'ed to Target. This guarantees
  564. * 1) 4-byte alignment
  565. * 2) Buffer in DMA-able space
  566. */
  567. orig_nbytes = nbytes;
  568. data_buf = (unsigned char *)dma_alloc_coherent(ar->dev,
  569. orig_nbytes,
  570. &ce_data_base,
  571. GFP_ATOMIC);
  572. if (!data_buf) {
  573. ret = -ENOMEM;
  574. goto done;
  575. }
  576. /* Copy caller's data to allocated DMA buf */
  577. memcpy(data_buf, data, orig_nbytes);
  578. /*
  579. * The address supplied by the caller is in the
  580. * Target CPU virtual address space.
  581. *
  582. * In order to use this address with the diagnostic CE,
  583. * convert it from
  584. * Target CPU virtual address space
  585. * to
  586. * CE address space
  587. */
  588. address = TARG_CPU_SPACE_TO_CE_SPACE(ar, ar_pci->mem, address);
  589. remaining_bytes = orig_nbytes;
  590. ce_data = ce_data_base;
  591. while (remaining_bytes) {
  592. /* FIXME: check cast */
  593. nbytes = min_t(int, remaining_bytes, DIAG_TRANSFER_LIMIT);
  594. /* Set up to receive directly into Target(!) address */
  595. ret = __ath10k_ce_rx_post_buf(ce_diag, NULL, address);
  596. if (ret != 0)
  597. goto done;
  598. /*
  599. * Request CE to send caller-supplied data that
  600. * was copied to bounce buffer to Target(!) address.
  601. */
  602. ret = ath10k_ce_send_nolock(ce_diag, NULL, (u32)ce_data,
  603. nbytes, 0, 0);
  604. if (ret != 0)
  605. goto done;
  606. i = 0;
  607. while (ath10k_ce_completed_send_next_nolock(ce_diag, NULL, &buf,
  608. &completed_nbytes,
  609. &id) != 0) {
  610. mdelay(1);
  611. if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
  612. ret = -EBUSY;
  613. goto done;
  614. }
  615. }
  616. if (nbytes != completed_nbytes) {
  617. ret = -EIO;
  618. goto done;
  619. }
  620. if (buf != ce_data) {
  621. ret = -EIO;
  622. goto done;
  623. }
  624. i = 0;
  625. while (ath10k_ce_completed_recv_next_nolock(ce_diag, NULL, &buf,
  626. &completed_nbytes,
  627. &id, &flags) != 0) {
  628. mdelay(1);
  629. if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
  630. ret = -EBUSY;
  631. goto done;
  632. }
  633. }
  634. if (nbytes != completed_nbytes) {
  635. ret = -EIO;
  636. goto done;
  637. }
  638. if (buf != address) {
  639. ret = -EIO;
  640. goto done;
  641. }
  642. remaining_bytes -= nbytes;
  643. address += nbytes;
  644. ce_data += nbytes;
  645. }
  646. done:
  647. if (data_buf) {
  648. dma_free_coherent(ar->dev, orig_nbytes, data_buf,
  649. ce_data_base);
  650. }
  651. if (ret != 0)
  652. ath10k_warn(ar, "failed to write diag value at 0x%x: %d\n",
  653. address, ret);
  654. spin_unlock_bh(&ar_pci->ce_lock);
  655. return ret;
  656. }
  657. static int ath10k_pci_diag_write32(struct ath10k *ar, u32 address, u32 value)
  658. {
  659. __le32 val = __cpu_to_le32(value);
  660. return ath10k_pci_diag_write_mem(ar, address, &val, sizeof(val));
  661. }
  662. static bool ath10k_pci_is_awake(struct ath10k *ar)
  663. {
  664. u32 val = ath10k_pci_reg_read32(ar, RTC_STATE_ADDRESS);
  665. return RTC_STATE_V_GET(val) == RTC_STATE_V_ON;
  666. }
  667. static int ath10k_pci_wake_wait(struct ath10k *ar)
  668. {
  669. int tot_delay = 0;
  670. int curr_delay = 5;
  671. while (tot_delay < PCIE_WAKE_TIMEOUT) {
  672. if (ath10k_pci_is_awake(ar))
  673. return 0;
  674. udelay(curr_delay);
  675. tot_delay += curr_delay;
  676. if (curr_delay < 50)
  677. curr_delay += 5;
  678. }
  679. return -ETIMEDOUT;
  680. }
  681. static int ath10k_pci_wake(struct ath10k *ar)
  682. {
  683. ath10k_pci_reg_write32(ar, PCIE_SOC_WAKE_ADDRESS,
  684. PCIE_SOC_WAKE_V_MASK);
  685. return ath10k_pci_wake_wait(ar);
  686. }
  687. static void ath10k_pci_sleep(struct ath10k *ar)
  688. {
  689. ath10k_pci_reg_write32(ar, PCIE_SOC_WAKE_ADDRESS,
  690. PCIE_SOC_WAKE_RESET);
  691. }
  692. /* Called by lower (CE) layer when a send to Target completes. */
  693. static void ath10k_pci_ce_send_done(struct ath10k_ce_pipe *ce_state)
  694. {
  695. struct ath10k *ar = ce_state->ar;
  696. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  697. struct ath10k_hif_cb *cb = &ar_pci->msg_callbacks_current;
  698. struct sk_buff_head list;
  699. struct sk_buff *skb;
  700. u32 ce_data;
  701. unsigned int nbytes;
  702. unsigned int transfer_id;
  703. __skb_queue_head_init(&list);
  704. while (ath10k_ce_completed_send_next(ce_state, (void **)&skb, &ce_data,
  705. &nbytes, &transfer_id) == 0) {
  706. /* no need to call tx completion for NULL pointers */
  707. if (skb == NULL)
  708. continue;
  709. __skb_queue_tail(&list, skb);
  710. }
  711. while ((skb = __skb_dequeue(&list)))
  712. cb->tx_completion(ar, skb);
  713. }
  714. /* Called by lower (CE) layer when data is received from the Target. */
  715. static void ath10k_pci_ce_recv_data(struct ath10k_ce_pipe *ce_state)
  716. {
  717. struct ath10k *ar = ce_state->ar;
  718. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  719. struct ath10k_pci_pipe *pipe_info = &ar_pci->pipe_info[ce_state->id];
  720. struct ath10k_hif_cb *cb = &ar_pci->msg_callbacks_current;
  721. struct sk_buff *skb;
  722. struct sk_buff_head list;
  723. void *transfer_context;
  724. u32 ce_data;
  725. unsigned int nbytes, max_nbytes;
  726. unsigned int transfer_id;
  727. unsigned int flags;
  728. __skb_queue_head_init(&list);
  729. while (ath10k_ce_completed_recv_next(ce_state, &transfer_context,
  730. &ce_data, &nbytes, &transfer_id,
  731. &flags) == 0) {
  732. skb = transfer_context;
  733. max_nbytes = skb->len + skb_tailroom(skb);
  734. dma_unmap_single(ar->dev, ATH10K_SKB_CB(skb)->paddr,
  735. max_nbytes, DMA_FROM_DEVICE);
  736. if (unlikely(max_nbytes < nbytes)) {
  737. ath10k_warn(ar, "rxed more than expected (nbytes %d, max %d)",
  738. nbytes, max_nbytes);
  739. dev_kfree_skb_any(skb);
  740. continue;
  741. }
  742. skb_put(skb, nbytes);
  743. __skb_queue_tail(&list, skb);
  744. }
  745. while ((skb = __skb_dequeue(&list))) {
  746. ath10k_dbg(ar, ATH10K_DBG_PCI, "pci rx ce pipe %d len %d\n",
  747. ce_state->id, skb->len);
  748. ath10k_dbg_dump(ar, ATH10K_DBG_PCI_DUMP, NULL, "pci rx: ",
  749. skb->data, skb->len);
  750. cb->rx_completion(ar, skb);
  751. }
  752. ath10k_pci_rx_post_pipe(pipe_info);
  753. }
  754. static int ath10k_pci_hif_tx_sg(struct ath10k *ar, u8 pipe_id,
  755. struct ath10k_hif_sg_item *items, int n_items)
  756. {
  757. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  758. struct ath10k_pci_pipe *pci_pipe = &ar_pci->pipe_info[pipe_id];
  759. struct ath10k_ce_pipe *ce_pipe = pci_pipe->ce_hdl;
  760. struct ath10k_ce_ring *src_ring = ce_pipe->src_ring;
  761. unsigned int nentries_mask;
  762. unsigned int sw_index;
  763. unsigned int write_index;
  764. int err, i = 0;
  765. spin_lock_bh(&ar_pci->ce_lock);
  766. nentries_mask = src_ring->nentries_mask;
  767. sw_index = src_ring->sw_index;
  768. write_index = src_ring->write_index;
  769. if (unlikely(CE_RING_DELTA(nentries_mask,
  770. write_index, sw_index - 1) < n_items)) {
  771. err = -ENOBUFS;
  772. goto err;
  773. }
  774. for (i = 0; i < n_items - 1; i++) {
  775. ath10k_dbg(ar, ATH10K_DBG_PCI,
  776. "pci tx item %d paddr 0x%08x len %d n_items %d\n",
  777. i, items[i].paddr, items[i].len, n_items);
  778. ath10k_dbg_dump(ar, ATH10K_DBG_PCI_DUMP, NULL, "pci tx data: ",
  779. items[i].vaddr, items[i].len);
  780. err = ath10k_ce_send_nolock(ce_pipe,
  781. items[i].transfer_context,
  782. items[i].paddr,
  783. items[i].len,
  784. items[i].transfer_id,
  785. CE_SEND_FLAG_GATHER);
  786. if (err)
  787. goto err;
  788. }
  789. /* `i` is equal to `n_items -1` after for() */
  790. ath10k_dbg(ar, ATH10K_DBG_PCI,
  791. "pci tx item %d paddr 0x%08x len %d n_items %d\n",
  792. i, items[i].paddr, items[i].len, n_items);
  793. ath10k_dbg_dump(ar, ATH10K_DBG_PCI_DUMP, NULL, "pci tx data: ",
  794. items[i].vaddr, items[i].len);
  795. err = ath10k_ce_send_nolock(ce_pipe,
  796. items[i].transfer_context,
  797. items[i].paddr,
  798. items[i].len,
  799. items[i].transfer_id,
  800. 0);
  801. if (err)
  802. goto err;
  803. spin_unlock_bh(&ar_pci->ce_lock);
  804. return 0;
  805. err:
  806. for (; i > 0; i--)
  807. __ath10k_ce_send_revert(ce_pipe);
  808. spin_unlock_bh(&ar_pci->ce_lock);
  809. return err;
  810. }
  811. static int ath10k_pci_hif_diag_read(struct ath10k *ar, u32 address, void *buf,
  812. size_t buf_len)
  813. {
  814. return ath10k_pci_diag_read_mem(ar, address, buf, buf_len);
  815. }
  816. static u16 ath10k_pci_hif_get_free_queue_number(struct ath10k *ar, u8 pipe)
  817. {
  818. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  819. ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif get free queue number\n");
  820. return ath10k_ce_num_free_src_entries(ar_pci->pipe_info[pipe].ce_hdl);
  821. }
  822. static void ath10k_pci_dump_registers(struct ath10k *ar,
  823. struct ath10k_fw_crash_data *crash_data)
  824. {
  825. __le32 reg_dump_values[REG_DUMP_COUNT_QCA988X] = {};
  826. int i, ret;
  827. lockdep_assert_held(&ar->data_lock);
  828. ret = ath10k_pci_diag_read_hi(ar, &reg_dump_values[0],
  829. hi_failure_state,
  830. REG_DUMP_COUNT_QCA988X * sizeof(__le32));
  831. if (ret) {
  832. ath10k_err(ar, "failed to read firmware dump area: %d\n", ret);
  833. return;
  834. }
  835. BUILD_BUG_ON(REG_DUMP_COUNT_QCA988X % 4);
  836. ath10k_err(ar, "firmware register dump:\n");
  837. for (i = 0; i < REG_DUMP_COUNT_QCA988X; i += 4)
  838. ath10k_err(ar, "[%02d]: 0x%08X 0x%08X 0x%08X 0x%08X\n",
  839. i,
  840. __le32_to_cpu(reg_dump_values[i]),
  841. __le32_to_cpu(reg_dump_values[i + 1]),
  842. __le32_to_cpu(reg_dump_values[i + 2]),
  843. __le32_to_cpu(reg_dump_values[i + 3]));
  844. if (!crash_data)
  845. return;
  846. for (i = 0; i < REG_DUMP_COUNT_QCA988X; i++)
  847. crash_data->registers[i] = reg_dump_values[i];
  848. }
  849. static void ath10k_pci_fw_crashed_dump(struct ath10k *ar)
  850. {
  851. struct ath10k_fw_crash_data *crash_data;
  852. char uuid[50];
  853. spin_lock_bh(&ar->data_lock);
  854. ar->stats.fw_crash_counter++;
  855. crash_data = ath10k_debug_get_new_fw_crash_data(ar);
  856. if (crash_data)
  857. scnprintf(uuid, sizeof(uuid), "%pUl", &crash_data->uuid);
  858. else
  859. scnprintf(uuid, sizeof(uuid), "n/a");
  860. ath10k_err(ar, "firmware crashed! (uuid %s)\n", uuid);
  861. ath10k_print_driver_info(ar);
  862. ath10k_pci_dump_registers(ar, crash_data);
  863. spin_unlock_bh(&ar->data_lock);
  864. queue_work(ar->workqueue, &ar->restart_work);
  865. }
  866. static void ath10k_pci_hif_send_complete_check(struct ath10k *ar, u8 pipe,
  867. int force)
  868. {
  869. ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif send complete check\n");
  870. if (!force) {
  871. int resources;
  872. /*
  873. * Decide whether to actually poll for completions, or just
  874. * wait for a later chance.
  875. * If there seem to be plenty of resources left, then just wait
  876. * since checking involves reading a CE register, which is a
  877. * relatively expensive operation.
  878. */
  879. resources = ath10k_pci_hif_get_free_queue_number(ar, pipe);
  880. /*
  881. * If at least 50% of the total resources are still available,
  882. * don't bother checking again yet.
  883. */
  884. if (resources > (host_ce_config_wlan[pipe].src_nentries >> 1))
  885. return;
  886. }
  887. ath10k_ce_per_engine_service(ar, pipe);
  888. }
  889. static void ath10k_pci_hif_set_callbacks(struct ath10k *ar,
  890. struct ath10k_hif_cb *callbacks)
  891. {
  892. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  893. ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif set callbacks\n");
  894. memcpy(&ar_pci->msg_callbacks_current, callbacks,
  895. sizeof(ar_pci->msg_callbacks_current));
  896. }
  897. static void ath10k_pci_kill_tasklet(struct ath10k *ar)
  898. {
  899. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  900. int i;
  901. tasklet_kill(&ar_pci->intr_tq);
  902. tasklet_kill(&ar_pci->msi_fw_err);
  903. for (i = 0; i < CE_COUNT; i++)
  904. tasklet_kill(&ar_pci->pipe_info[i].intr);
  905. del_timer_sync(&ar_pci->rx_post_retry);
  906. }
  907. static int ath10k_pci_hif_map_service_to_pipe(struct ath10k *ar,
  908. u16 service_id, u8 *ul_pipe,
  909. u8 *dl_pipe, int *ul_is_polled,
  910. int *dl_is_polled)
  911. {
  912. const struct service_to_pipe *entry;
  913. bool ul_set = false, dl_set = false;
  914. int i;
  915. ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif map service\n");
  916. /* polling for received messages not supported */
  917. *dl_is_polled = 0;
  918. for (i = 0; i < ARRAY_SIZE(target_service_to_ce_map_wlan); i++) {
  919. entry = &target_service_to_ce_map_wlan[i];
  920. if (__le32_to_cpu(entry->service_id) != service_id)
  921. continue;
  922. switch (__le32_to_cpu(entry->pipedir)) {
  923. case PIPEDIR_NONE:
  924. break;
  925. case PIPEDIR_IN:
  926. WARN_ON(dl_set);
  927. *dl_pipe = __le32_to_cpu(entry->pipenum);
  928. dl_set = true;
  929. break;
  930. case PIPEDIR_OUT:
  931. WARN_ON(ul_set);
  932. *ul_pipe = __le32_to_cpu(entry->pipenum);
  933. ul_set = true;
  934. break;
  935. case PIPEDIR_INOUT:
  936. WARN_ON(dl_set);
  937. WARN_ON(ul_set);
  938. *dl_pipe = __le32_to_cpu(entry->pipenum);
  939. *ul_pipe = __le32_to_cpu(entry->pipenum);
  940. dl_set = true;
  941. ul_set = true;
  942. break;
  943. }
  944. }
  945. if (WARN_ON(!ul_set || !dl_set))
  946. return -ENOENT;
  947. *ul_is_polled =
  948. (host_ce_config_wlan[*ul_pipe].flags & CE_ATTR_DIS_INTR) != 0;
  949. return 0;
  950. }
  951. static void ath10k_pci_hif_get_default_pipe(struct ath10k *ar,
  952. u8 *ul_pipe, u8 *dl_pipe)
  953. {
  954. int ul_is_polled, dl_is_polled;
  955. ath10k_dbg(ar, ATH10K_DBG_PCI, "pci hif get default pipe\n");
  956. (void)ath10k_pci_hif_map_service_to_pipe(ar,
  957. ATH10K_HTC_SVC_ID_RSVD_CTRL,
  958. ul_pipe,
  959. dl_pipe,
  960. &ul_is_polled,
  961. &dl_is_polled);
  962. }
  963. static void ath10k_pci_irq_msi_fw_mask(struct ath10k *ar)
  964. {
  965. u32 val;
  966. val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS + CORE_CTRL_ADDRESS);
  967. val &= ~CORE_CTRL_PCIE_REG_31_MASK;
  968. ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + CORE_CTRL_ADDRESS, val);
  969. }
  970. static void ath10k_pci_irq_msi_fw_unmask(struct ath10k *ar)
  971. {
  972. u32 val;
  973. val = ath10k_pci_read32(ar, SOC_CORE_BASE_ADDRESS + CORE_CTRL_ADDRESS);
  974. val |= CORE_CTRL_PCIE_REG_31_MASK;
  975. ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + CORE_CTRL_ADDRESS, val);
  976. }
  977. static void ath10k_pci_irq_disable(struct ath10k *ar)
  978. {
  979. ath10k_ce_disable_interrupts(ar);
  980. ath10k_pci_disable_and_clear_legacy_irq(ar);
  981. ath10k_pci_irq_msi_fw_mask(ar);
  982. }
  983. static void ath10k_pci_irq_sync(struct ath10k *ar)
  984. {
  985. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  986. int i;
  987. for (i = 0; i < max(1, ar_pci->num_msi_intrs); i++)
  988. synchronize_irq(ar_pci->pdev->irq + i);
  989. }
  990. static void ath10k_pci_irq_enable(struct ath10k *ar)
  991. {
  992. ath10k_ce_enable_interrupts(ar);
  993. ath10k_pci_enable_legacy_irq(ar);
  994. ath10k_pci_irq_msi_fw_unmask(ar);
  995. }
  996. static int ath10k_pci_hif_start(struct ath10k *ar)
  997. {
  998. ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif start\n");
  999. ath10k_pci_irq_enable(ar);
  1000. ath10k_pci_rx_post(ar);
  1001. return 0;
  1002. }
  1003. static void ath10k_pci_rx_pipe_cleanup(struct ath10k_pci_pipe *pci_pipe)
  1004. {
  1005. struct ath10k *ar;
  1006. struct ath10k_ce_pipe *ce_pipe;
  1007. struct ath10k_ce_ring *ce_ring;
  1008. struct sk_buff *skb;
  1009. int i;
  1010. ar = pci_pipe->hif_ce_state;
  1011. ce_pipe = pci_pipe->ce_hdl;
  1012. ce_ring = ce_pipe->dest_ring;
  1013. if (!ce_ring)
  1014. return;
  1015. if (!pci_pipe->buf_sz)
  1016. return;
  1017. for (i = 0; i < ce_ring->nentries; i++) {
  1018. skb = ce_ring->per_transfer_context[i];
  1019. if (!skb)
  1020. continue;
  1021. ce_ring->per_transfer_context[i] = NULL;
  1022. dma_unmap_single(ar->dev, ATH10K_SKB_CB(skb)->paddr,
  1023. skb->len + skb_tailroom(skb),
  1024. DMA_FROM_DEVICE);
  1025. dev_kfree_skb_any(skb);
  1026. }
  1027. }
  1028. static void ath10k_pci_tx_pipe_cleanup(struct ath10k_pci_pipe *pci_pipe)
  1029. {
  1030. struct ath10k *ar;
  1031. struct ath10k_pci *ar_pci;
  1032. struct ath10k_ce_pipe *ce_pipe;
  1033. struct ath10k_ce_ring *ce_ring;
  1034. struct ce_desc *ce_desc;
  1035. struct sk_buff *skb;
  1036. unsigned int id;
  1037. int i;
  1038. ar = pci_pipe->hif_ce_state;
  1039. ar_pci = ath10k_pci_priv(ar);
  1040. ce_pipe = pci_pipe->ce_hdl;
  1041. ce_ring = ce_pipe->src_ring;
  1042. if (!ce_ring)
  1043. return;
  1044. if (!pci_pipe->buf_sz)
  1045. return;
  1046. ce_desc = ce_ring->shadow_base;
  1047. if (WARN_ON(!ce_desc))
  1048. return;
  1049. for (i = 0; i < ce_ring->nentries; i++) {
  1050. skb = ce_ring->per_transfer_context[i];
  1051. if (!skb)
  1052. continue;
  1053. ce_ring->per_transfer_context[i] = NULL;
  1054. id = MS(__le16_to_cpu(ce_desc[i].flags),
  1055. CE_DESC_FLAGS_META_DATA);
  1056. ar_pci->msg_callbacks_current.tx_completion(ar, skb);
  1057. }
  1058. }
  1059. /*
  1060. * Cleanup residual buffers for device shutdown:
  1061. * buffers that were enqueued for receive
  1062. * buffers that were to be sent
  1063. * Note: Buffers that had completed but which were
  1064. * not yet processed are on a completion queue. They
  1065. * are handled when the completion thread shuts down.
  1066. */
  1067. static void ath10k_pci_buffer_cleanup(struct ath10k *ar)
  1068. {
  1069. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1070. int pipe_num;
  1071. for (pipe_num = 0; pipe_num < CE_COUNT; pipe_num++) {
  1072. struct ath10k_pci_pipe *pipe_info;
  1073. pipe_info = &ar_pci->pipe_info[pipe_num];
  1074. ath10k_pci_rx_pipe_cleanup(pipe_info);
  1075. ath10k_pci_tx_pipe_cleanup(pipe_info);
  1076. }
  1077. }
  1078. static void ath10k_pci_ce_deinit(struct ath10k *ar)
  1079. {
  1080. int i;
  1081. for (i = 0; i < CE_COUNT; i++)
  1082. ath10k_ce_deinit_pipe(ar, i);
  1083. }
  1084. static void ath10k_pci_flush(struct ath10k *ar)
  1085. {
  1086. ath10k_pci_kill_tasklet(ar);
  1087. ath10k_pci_buffer_cleanup(ar);
  1088. }
  1089. static void ath10k_pci_hif_stop(struct ath10k *ar)
  1090. {
  1091. ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif stop\n");
  1092. /* Most likely the device has HTT Rx ring configured. The only way to
  1093. * prevent the device from accessing (and possible corrupting) host
  1094. * memory is to reset the chip now.
  1095. *
  1096. * There's also no known way of masking MSI interrupts on the device.
  1097. * For ranged MSI the CE-related interrupts can be masked. However
  1098. * regardless how many MSI interrupts are assigned the first one
  1099. * is always used for firmware indications (crashes) and cannot be
  1100. * masked. To prevent the device from asserting the interrupt reset it
  1101. * before proceeding with cleanup.
  1102. */
  1103. ath10k_pci_warm_reset(ar);
  1104. ath10k_pci_irq_disable(ar);
  1105. ath10k_pci_irq_sync(ar);
  1106. ath10k_pci_flush(ar);
  1107. }
  1108. static int ath10k_pci_hif_exchange_bmi_msg(struct ath10k *ar,
  1109. void *req, u32 req_len,
  1110. void *resp, u32 *resp_len)
  1111. {
  1112. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1113. struct ath10k_pci_pipe *pci_tx = &ar_pci->pipe_info[BMI_CE_NUM_TO_TARG];
  1114. struct ath10k_pci_pipe *pci_rx = &ar_pci->pipe_info[BMI_CE_NUM_TO_HOST];
  1115. struct ath10k_ce_pipe *ce_tx = pci_tx->ce_hdl;
  1116. struct ath10k_ce_pipe *ce_rx = pci_rx->ce_hdl;
  1117. dma_addr_t req_paddr = 0;
  1118. dma_addr_t resp_paddr = 0;
  1119. struct bmi_xfer xfer = {};
  1120. void *treq, *tresp = NULL;
  1121. int ret = 0;
  1122. might_sleep();
  1123. if (resp && !resp_len)
  1124. return -EINVAL;
  1125. if (resp && resp_len && *resp_len == 0)
  1126. return -EINVAL;
  1127. treq = kmemdup(req, req_len, GFP_KERNEL);
  1128. if (!treq)
  1129. return -ENOMEM;
  1130. req_paddr = dma_map_single(ar->dev, treq, req_len, DMA_TO_DEVICE);
  1131. ret = dma_mapping_error(ar->dev, req_paddr);
  1132. if (ret)
  1133. goto err_dma;
  1134. if (resp && resp_len) {
  1135. tresp = kzalloc(*resp_len, GFP_KERNEL);
  1136. if (!tresp) {
  1137. ret = -ENOMEM;
  1138. goto err_req;
  1139. }
  1140. resp_paddr = dma_map_single(ar->dev, tresp, *resp_len,
  1141. DMA_FROM_DEVICE);
  1142. ret = dma_mapping_error(ar->dev, resp_paddr);
  1143. if (ret)
  1144. goto err_req;
  1145. xfer.wait_for_resp = true;
  1146. xfer.resp_len = 0;
  1147. ath10k_ce_rx_post_buf(ce_rx, &xfer, resp_paddr);
  1148. }
  1149. ret = ath10k_ce_send(ce_tx, &xfer, req_paddr, req_len, -1, 0);
  1150. if (ret)
  1151. goto err_resp;
  1152. ret = ath10k_pci_bmi_wait(ce_tx, ce_rx, &xfer);
  1153. if (ret) {
  1154. u32 unused_buffer;
  1155. unsigned int unused_nbytes;
  1156. unsigned int unused_id;
  1157. ath10k_ce_cancel_send_next(ce_tx, NULL, &unused_buffer,
  1158. &unused_nbytes, &unused_id);
  1159. } else {
  1160. /* non-zero means we did not time out */
  1161. ret = 0;
  1162. }
  1163. err_resp:
  1164. if (resp) {
  1165. u32 unused_buffer;
  1166. ath10k_ce_revoke_recv_next(ce_rx, NULL, &unused_buffer);
  1167. dma_unmap_single(ar->dev, resp_paddr,
  1168. *resp_len, DMA_FROM_DEVICE);
  1169. }
  1170. err_req:
  1171. dma_unmap_single(ar->dev, req_paddr, req_len, DMA_TO_DEVICE);
  1172. if (ret == 0 && resp_len) {
  1173. *resp_len = min(*resp_len, xfer.resp_len);
  1174. memcpy(resp, tresp, xfer.resp_len);
  1175. }
  1176. err_dma:
  1177. kfree(treq);
  1178. kfree(tresp);
  1179. return ret;
  1180. }
  1181. static void ath10k_pci_bmi_send_done(struct ath10k_ce_pipe *ce_state)
  1182. {
  1183. struct bmi_xfer *xfer;
  1184. u32 ce_data;
  1185. unsigned int nbytes;
  1186. unsigned int transfer_id;
  1187. if (ath10k_ce_completed_send_next(ce_state, (void **)&xfer, &ce_data,
  1188. &nbytes, &transfer_id))
  1189. return;
  1190. xfer->tx_done = true;
  1191. }
  1192. static void ath10k_pci_bmi_recv_data(struct ath10k_ce_pipe *ce_state)
  1193. {
  1194. struct ath10k *ar = ce_state->ar;
  1195. struct bmi_xfer *xfer;
  1196. u32 ce_data;
  1197. unsigned int nbytes;
  1198. unsigned int transfer_id;
  1199. unsigned int flags;
  1200. if (ath10k_ce_completed_recv_next(ce_state, (void **)&xfer, &ce_data,
  1201. &nbytes, &transfer_id, &flags))
  1202. return;
  1203. if (WARN_ON_ONCE(!xfer))
  1204. return;
  1205. if (!xfer->wait_for_resp) {
  1206. ath10k_warn(ar, "unexpected: BMI data received; ignoring\n");
  1207. return;
  1208. }
  1209. xfer->resp_len = nbytes;
  1210. xfer->rx_done = true;
  1211. }
  1212. static int ath10k_pci_bmi_wait(struct ath10k_ce_pipe *tx_pipe,
  1213. struct ath10k_ce_pipe *rx_pipe,
  1214. struct bmi_xfer *xfer)
  1215. {
  1216. unsigned long timeout = jiffies + BMI_COMMUNICATION_TIMEOUT_HZ;
  1217. while (time_before_eq(jiffies, timeout)) {
  1218. ath10k_pci_bmi_send_done(tx_pipe);
  1219. ath10k_pci_bmi_recv_data(rx_pipe);
  1220. if (xfer->tx_done && (xfer->rx_done == xfer->wait_for_resp))
  1221. return 0;
  1222. schedule();
  1223. }
  1224. return -ETIMEDOUT;
  1225. }
  1226. /*
  1227. * Send an interrupt to the device to wake up the Target CPU
  1228. * so it has an opportunity to notice any changed state.
  1229. */
  1230. static int ath10k_pci_wake_target_cpu(struct ath10k *ar)
  1231. {
  1232. u32 addr, val;
  1233. addr = SOC_CORE_BASE_ADDRESS | CORE_CTRL_ADDRESS;
  1234. val = ath10k_pci_read32(ar, addr);
  1235. val |= CORE_CTRL_CPU_INTR_MASK;
  1236. ath10k_pci_write32(ar, addr, val);
  1237. return 0;
  1238. }
  1239. static int ath10k_pci_init_config(struct ath10k *ar)
  1240. {
  1241. u32 interconnect_targ_addr;
  1242. u32 pcie_state_targ_addr = 0;
  1243. u32 pipe_cfg_targ_addr = 0;
  1244. u32 svc_to_pipe_map = 0;
  1245. u32 pcie_config_flags = 0;
  1246. u32 ealloc_value;
  1247. u32 ealloc_targ_addr;
  1248. u32 flag2_value;
  1249. u32 flag2_targ_addr;
  1250. int ret = 0;
  1251. /* Download to Target the CE Config and the service-to-CE map */
  1252. interconnect_targ_addr =
  1253. host_interest_item_address(HI_ITEM(hi_interconnect_state));
  1254. /* Supply Target-side CE configuration */
  1255. ret = ath10k_pci_diag_read32(ar, interconnect_targ_addr,
  1256. &pcie_state_targ_addr);
  1257. if (ret != 0) {
  1258. ath10k_err(ar, "Failed to get pcie state addr: %d\n", ret);
  1259. return ret;
  1260. }
  1261. if (pcie_state_targ_addr == 0) {
  1262. ret = -EIO;
  1263. ath10k_err(ar, "Invalid pcie state addr\n");
  1264. return ret;
  1265. }
  1266. ret = ath10k_pci_diag_read32(ar, (pcie_state_targ_addr +
  1267. offsetof(struct pcie_state,
  1268. pipe_cfg_addr)),
  1269. &pipe_cfg_targ_addr);
  1270. if (ret != 0) {
  1271. ath10k_err(ar, "Failed to get pipe cfg addr: %d\n", ret);
  1272. return ret;
  1273. }
  1274. if (pipe_cfg_targ_addr == 0) {
  1275. ret = -EIO;
  1276. ath10k_err(ar, "Invalid pipe cfg addr\n");
  1277. return ret;
  1278. }
  1279. ret = ath10k_pci_diag_write_mem(ar, pipe_cfg_targ_addr,
  1280. target_ce_config_wlan,
  1281. sizeof(target_ce_config_wlan));
  1282. if (ret != 0) {
  1283. ath10k_err(ar, "Failed to write pipe cfg: %d\n", ret);
  1284. return ret;
  1285. }
  1286. ret = ath10k_pci_diag_read32(ar, (pcie_state_targ_addr +
  1287. offsetof(struct pcie_state,
  1288. svc_to_pipe_map)),
  1289. &svc_to_pipe_map);
  1290. if (ret != 0) {
  1291. ath10k_err(ar, "Failed to get svc/pipe map: %d\n", ret);
  1292. return ret;
  1293. }
  1294. if (svc_to_pipe_map == 0) {
  1295. ret = -EIO;
  1296. ath10k_err(ar, "Invalid svc_to_pipe map\n");
  1297. return ret;
  1298. }
  1299. ret = ath10k_pci_diag_write_mem(ar, svc_to_pipe_map,
  1300. target_service_to_ce_map_wlan,
  1301. sizeof(target_service_to_ce_map_wlan));
  1302. if (ret != 0) {
  1303. ath10k_err(ar, "Failed to write svc/pipe map: %d\n", ret);
  1304. return ret;
  1305. }
  1306. ret = ath10k_pci_diag_read32(ar, (pcie_state_targ_addr +
  1307. offsetof(struct pcie_state,
  1308. config_flags)),
  1309. &pcie_config_flags);
  1310. if (ret != 0) {
  1311. ath10k_err(ar, "Failed to get pcie config_flags: %d\n", ret);
  1312. return ret;
  1313. }
  1314. pcie_config_flags &= ~PCIE_CONFIG_FLAG_ENABLE_L1;
  1315. ret = ath10k_pci_diag_write32(ar, (pcie_state_targ_addr +
  1316. offsetof(struct pcie_state,
  1317. config_flags)),
  1318. pcie_config_flags);
  1319. if (ret != 0) {
  1320. ath10k_err(ar, "Failed to write pcie config_flags: %d\n", ret);
  1321. return ret;
  1322. }
  1323. /* configure early allocation */
  1324. ealloc_targ_addr = host_interest_item_address(HI_ITEM(hi_early_alloc));
  1325. ret = ath10k_pci_diag_read32(ar, ealloc_targ_addr, &ealloc_value);
  1326. if (ret != 0) {
  1327. ath10k_err(ar, "Faile to get early alloc val: %d\n", ret);
  1328. return ret;
  1329. }
  1330. /* first bank is switched to IRAM */
  1331. ealloc_value |= ((HI_EARLY_ALLOC_MAGIC << HI_EARLY_ALLOC_MAGIC_SHIFT) &
  1332. HI_EARLY_ALLOC_MAGIC_MASK);
  1333. ealloc_value |= ((1 << HI_EARLY_ALLOC_IRAM_BANKS_SHIFT) &
  1334. HI_EARLY_ALLOC_IRAM_BANKS_MASK);
  1335. ret = ath10k_pci_diag_write32(ar, ealloc_targ_addr, ealloc_value);
  1336. if (ret != 0) {
  1337. ath10k_err(ar, "Failed to set early alloc val: %d\n", ret);
  1338. return ret;
  1339. }
  1340. /* Tell Target to proceed with initialization */
  1341. flag2_targ_addr = host_interest_item_address(HI_ITEM(hi_option_flag2));
  1342. ret = ath10k_pci_diag_read32(ar, flag2_targ_addr, &flag2_value);
  1343. if (ret != 0) {
  1344. ath10k_err(ar, "Failed to get option val: %d\n", ret);
  1345. return ret;
  1346. }
  1347. flag2_value |= HI_OPTION_EARLY_CFG_DONE;
  1348. ret = ath10k_pci_diag_write32(ar, flag2_targ_addr, flag2_value);
  1349. if (ret != 0) {
  1350. ath10k_err(ar, "Failed to set option val: %d\n", ret);
  1351. return ret;
  1352. }
  1353. return 0;
  1354. }
  1355. static int ath10k_pci_alloc_pipes(struct ath10k *ar)
  1356. {
  1357. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1358. struct ath10k_pci_pipe *pipe;
  1359. int i, ret;
  1360. for (i = 0; i < CE_COUNT; i++) {
  1361. pipe = &ar_pci->pipe_info[i];
  1362. pipe->ce_hdl = &ar_pci->ce_states[i];
  1363. pipe->pipe_num = i;
  1364. pipe->hif_ce_state = ar;
  1365. ret = ath10k_ce_alloc_pipe(ar, i, &host_ce_config_wlan[i],
  1366. ath10k_pci_ce_send_done,
  1367. ath10k_pci_ce_recv_data);
  1368. if (ret) {
  1369. ath10k_err(ar, "failed to allocate copy engine pipe %d: %d\n",
  1370. i, ret);
  1371. return ret;
  1372. }
  1373. /* Last CE is Diagnostic Window */
  1374. if (i == CE_COUNT - 1) {
  1375. ar_pci->ce_diag = pipe->ce_hdl;
  1376. continue;
  1377. }
  1378. pipe->buf_sz = (size_t)(host_ce_config_wlan[i].src_sz_max);
  1379. }
  1380. return 0;
  1381. }
  1382. static void ath10k_pci_free_pipes(struct ath10k *ar)
  1383. {
  1384. int i;
  1385. for (i = 0; i < CE_COUNT; i++)
  1386. ath10k_ce_free_pipe(ar, i);
  1387. }
  1388. static int ath10k_pci_init_pipes(struct ath10k *ar)
  1389. {
  1390. int i, ret;
  1391. for (i = 0; i < CE_COUNT; i++) {
  1392. ret = ath10k_ce_init_pipe(ar, i, &host_ce_config_wlan[i]);
  1393. if (ret) {
  1394. ath10k_err(ar, "failed to initialize copy engine pipe %d: %d\n",
  1395. i, ret);
  1396. return ret;
  1397. }
  1398. }
  1399. return 0;
  1400. }
  1401. static bool ath10k_pci_has_fw_crashed(struct ath10k *ar)
  1402. {
  1403. return ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS) &
  1404. FW_IND_EVENT_PENDING;
  1405. }
  1406. static void ath10k_pci_fw_crashed_clear(struct ath10k *ar)
  1407. {
  1408. u32 val;
  1409. val = ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS);
  1410. val &= ~FW_IND_EVENT_PENDING;
  1411. ath10k_pci_write32(ar, FW_INDICATOR_ADDRESS, val);
  1412. }
  1413. /* this function effectively clears target memory controller assert line */
  1414. static void ath10k_pci_warm_reset_si0(struct ath10k *ar)
  1415. {
  1416. u32 val;
  1417. val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
  1418. ath10k_pci_soc_write32(ar, SOC_RESET_CONTROL_ADDRESS,
  1419. val | SOC_RESET_CONTROL_SI0_RST_MASK);
  1420. val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
  1421. msleep(10);
  1422. val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
  1423. ath10k_pci_soc_write32(ar, SOC_RESET_CONTROL_ADDRESS,
  1424. val & ~SOC_RESET_CONTROL_SI0_RST_MASK);
  1425. val = ath10k_pci_soc_read32(ar, SOC_RESET_CONTROL_ADDRESS);
  1426. msleep(10);
  1427. }
  1428. static void ath10k_pci_warm_reset_cpu(struct ath10k *ar)
  1429. {
  1430. u32 val;
  1431. ath10k_pci_write32(ar, FW_INDICATOR_ADDRESS, 0);
  1432. val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
  1433. SOC_RESET_CONTROL_ADDRESS);
  1434. ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
  1435. val | SOC_RESET_CONTROL_CPU_WARM_RST_MASK);
  1436. }
  1437. static void ath10k_pci_warm_reset_ce(struct ath10k *ar)
  1438. {
  1439. u32 val;
  1440. val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
  1441. SOC_RESET_CONTROL_ADDRESS);
  1442. ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
  1443. val | SOC_RESET_CONTROL_CE_RST_MASK);
  1444. msleep(10);
  1445. ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS + SOC_RESET_CONTROL_ADDRESS,
  1446. val & ~SOC_RESET_CONTROL_CE_RST_MASK);
  1447. }
  1448. static void ath10k_pci_warm_reset_clear_lf(struct ath10k *ar)
  1449. {
  1450. u32 val;
  1451. val = ath10k_pci_read32(ar, RTC_SOC_BASE_ADDRESS +
  1452. SOC_LF_TIMER_CONTROL0_ADDRESS);
  1453. ath10k_pci_write32(ar, RTC_SOC_BASE_ADDRESS +
  1454. SOC_LF_TIMER_CONTROL0_ADDRESS,
  1455. val & ~SOC_LF_TIMER_CONTROL0_ENABLE_MASK);
  1456. }
  1457. static int ath10k_pci_warm_reset(struct ath10k *ar)
  1458. {
  1459. int ret;
  1460. ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot warm reset\n");
  1461. spin_lock_bh(&ar->data_lock);
  1462. ar->stats.fw_warm_reset_counter++;
  1463. spin_unlock_bh(&ar->data_lock);
  1464. ath10k_pci_irq_disable(ar);
  1465. /* Make sure the target CPU is not doing anything dangerous, e.g. if it
  1466. * were to access copy engine while host performs copy engine reset
  1467. * then it is possible for the device to confuse pci-e controller to
  1468. * the point of bringing host system to a complete stop (i.e. hang).
  1469. */
  1470. ath10k_pci_warm_reset_si0(ar);
  1471. ath10k_pci_warm_reset_cpu(ar);
  1472. ath10k_pci_init_pipes(ar);
  1473. ath10k_pci_wait_for_target_init(ar);
  1474. ath10k_pci_warm_reset_clear_lf(ar);
  1475. ath10k_pci_warm_reset_ce(ar);
  1476. ath10k_pci_warm_reset_cpu(ar);
  1477. ath10k_pci_init_pipes(ar);
  1478. ret = ath10k_pci_wait_for_target_init(ar);
  1479. if (ret) {
  1480. ath10k_warn(ar, "failed to wait for target init: %d\n", ret);
  1481. return ret;
  1482. }
  1483. ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot warm reset complete\n");
  1484. return 0;
  1485. }
  1486. static int ath10k_pci_chip_reset(struct ath10k *ar)
  1487. {
  1488. int i, ret;
  1489. u32 val;
  1490. ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot chip reset\n");
  1491. /* Some hardware revisions (e.g. CUS223v2) has issues with cold reset.
  1492. * It is thus preferred to use warm reset which is safer but may not be
  1493. * able to recover the device from all possible fail scenarios.
  1494. *
  1495. * Warm reset doesn't always work on first try so attempt it a few
  1496. * times before giving up.
  1497. */
  1498. for (i = 0; i < ATH10K_PCI_NUM_WARM_RESET_ATTEMPTS; i++) {
  1499. ret = ath10k_pci_warm_reset(ar);
  1500. if (ret) {
  1501. ath10k_warn(ar, "failed to warm reset attempt %d of %d: %d\n",
  1502. i + 1, ATH10K_PCI_NUM_WARM_RESET_ATTEMPTS,
  1503. ret);
  1504. continue;
  1505. }
  1506. /* FIXME: Sometimes copy engine doesn't recover after warm
  1507. * reset. In most cases this needs cold reset. In some of these
  1508. * cases the device is in such a state that a cold reset may
  1509. * lock up the host.
  1510. *
  1511. * Reading any host interest register via copy engine is
  1512. * sufficient to verify if device is capable of booting
  1513. * firmware blob.
  1514. */
  1515. ret = ath10k_pci_init_pipes(ar);
  1516. if (ret) {
  1517. ath10k_warn(ar, "failed to init copy engine: %d\n",
  1518. ret);
  1519. continue;
  1520. }
  1521. ret = ath10k_pci_diag_read32(ar, QCA988X_HOST_INTEREST_ADDRESS,
  1522. &val);
  1523. if (ret) {
  1524. ath10k_warn(ar, "failed to poke copy engine: %d\n",
  1525. ret);
  1526. continue;
  1527. }
  1528. ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot chip reset complete (warm)\n");
  1529. return 0;
  1530. }
  1531. if (ath10k_pci_reset_mode == ATH10K_PCI_RESET_WARM_ONLY) {
  1532. ath10k_warn(ar, "refusing cold reset as requested\n");
  1533. return -EPERM;
  1534. }
  1535. ret = ath10k_pci_cold_reset(ar);
  1536. if (ret) {
  1537. ath10k_warn(ar, "failed to cold reset: %d\n", ret);
  1538. return ret;
  1539. }
  1540. ret = ath10k_pci_wait_for_target_init(ar);
  1541. if (ret) {
  1542. ath10k_warn(ar, "failed to wait for target after cold reset: %d\n",
  1543. ret);
  1544. return ret;
  1545. }
  1546. ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot chip reset complete (cold)\n");
  1547. return 0;
  1548. }
  1549. static int ath10k_pci_hif_power_up(struct ath10k *ar)
  1550. {
  1551. int ret;
  1552. ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif power up\n");
  1553. ret = ath10k_pci_wake(ar);
  1554. if (ret) {
  1555. ath10k_err(ar, "failed to wake up target: %d\n", ret);
  1556. return ret;
  1557. }
  1558. /*
  1559. * Bring the target up cleanly.
  1560. *
  1561. * The target may be in an undefined state with an AUX-powered Target
  1562. * and a Host in WoW mode. If the Host crashes, loses power, or is
  1563. * restarted (without unloading the driver) then the Target is left
  1564. * (aux) powered and running. On a subsequent driver load, the Target
  1565. * is in an unexpected state. We try to catch that here in order to
  1566. * reset the Target and retry the probe.
  1567. */
  1568. ret = ath10k_pci_chip_reset(ar);
  1569. if (ret) {
  1570. ath10k_err(ar, "failed to reset chip: %d\n", ret);
  1571. goto err_sleep;
  1572. }
  1573. ret = ath10k_pci_init_pipes(ar);
  1574. if (ret) {
  1575. ath10k_err(ar, "failed to initialize CE: %d\n", ret);
  1576. goto err_sleep;
  1577. }
  1578. ret = ath10k_pci_init_config(ar);
  1579. if (ret) {
  1580. ath10k_err(ar, "failed to setup init config: %d\n", ret);
  1581. goto err_ce;
  1582. }
  1583. ret = ath10k_pci_wake_target_cpu(ar);
  1584. if (ret) {
  1585. ath10k_err(ar, "could not wake up target CPU: %d\n", ret);
  1586. goto err_ce;
  1587. }
  1588. return 0;
  1589. err_ce:
  1590. ath10k_pci_ce_deinit(ar);
  1591. err_sleep:
  1592. ath10k_pci_sleep(ar);
  1593. return ret;
  1594. }
  1595. static void ath10k_pci_hif_power_down(struct ath10k *ar)
  1596. {
  1597. ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif power down\n");
  1598. /* Currently hif_power_up performs effectively a reset and hif_stop
  1599. * resets the chip as well so there's no point in resetting here.
  1600. */
  1601. ath10k_pci_sleep(ar);
  1602. }
  1603. #ifdef CONFIG_PM
  1604. #define ATH10K_PCI_PM_CONTROL 0x44
  1605. static int ath10k_pci_hif_suspend(struct ath10k *ar)
  1606. {
  1607. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1608. struct pci_dev *pdev = ar_pci->pdev;
  1609. u32 val;
  1610. pci_read_config_dword(pdev, ATH10K_PCI_PM_CONTROL, &val);
  1611. if ((val & 0x000000ff) != 0x3) {
  1612. pci_save_state(pdev);
  1613. pci_disable_device(pdev);
  1614. pci_write_config_dword(pdev, ATH10K_PCI_PM_CONTROL,
  1615. (val & 0xffffff00) | 0x03);
  1616. }
  1617. return 0;
  1618. }
  1619. static int ath10k_pci_hif_resume(struct ath10k *ar)
  1620. {
  1621. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1622. struct pci_dev *pdev = ar_pci->pdev;
  1623. u32 val;
  1624. pci_read_config_dword(pdev, ATH10K_PCI_PM_CONTROL, &val);
  1625. if ((val & 0x000000ff) != 0) {
  1626. pci_restore_state(pdev);
  1627. pci_write_config_dword(pdev, ATH10K_PCI_PM_CONTROL,
  1628. val & 0xffffff00);
  1629. /*
  1630. * Suspend/Resume resets the PCI configuration space,
  1631. * so we have to re-disable the RETRY_TIMEOUT register (0x41)
  1632. * to keep PCI Tx retries from interfering with C3 CPU state
  1633. */
  1634. pci_read_config_dword(pdev, 0x40, &val);
  1635. if ((val & 0x0000ff00) != 0)
  1636. pci_write_config_dword(pdev, 0x40, val & 0xffff00ff);
  1637. }
  1638. return 0;
  1639. }
  1640. #endif
  1641. static const struct ath10k_hif_ops ath10k_pci_hif_ops = {
  1642. .tx_sg = ath10k_pci_hif_tx_sg,
  1643. .diag_read = ath10k_pci_hif_diag_read,
  1644. .diag_write = ath10k_pci_diag_write_mem,
  1645. .exchange_bmi_msg = ath10k_pci_hif_exchange_bmi_msg,
  1646. .start = ath10k_pci_hif_start,
  1647. .stop = ath10k_pci_hif_stop,
  1648. .map_service_to_pipe = ath10k_pci_hif_map_service_to_pipe,
  1649. .get_default_pipe = ath10k_pci_hif_get_default_pipe,
  1650. .send_complete_check = ath10k_pci_hif_send_complete_check,
  1651. .set_callbacks = ath10k_pci_hif_set_callbacks,
  1652. .get_free_queue_number = ath10k_pci_hif_get_free_queue_number,
  1653. .power_up = ath10k_pci_hif_power_up,
  1654. .power_down = ath10k_pci_hif_power_down,
  1655. .read32 = ath10k_pci_read32,
  1656. .write32 = ath10k_pci_write32,
  1657. #ifdef CONFIG_PM
  1658. .suspend = ath10k_pci_hif_suspend,
  1659. .resume = ath10k_pci_hif_resume,
  1660. #endif
  1661. };
  1662. static void ath10k_pci_ce_tasklet(unsigned long ptr)
  1663. {
  1664. struct ath10k_pci_pipe *pipe = (struct ath10k_pci_pipe *)ptr;
  1665. struct ath10k_pci *ar_pci = pipe->ar_pci;
  1666. ath10k_ce_per_engine_service(ar_pci->ar, pipe->pipe_num);
  1667. }
  1668. static void ath10k_msi_err_tasklet(unsigned long data)
  1669. {
  1670. struct ath10k *ar = (struct ath10k *)data;
  1671. if (!ath10k_pci_has_fw_crashed(ar)) {
  1672. ath10k_warn(ar, "received unsolicited fw crash interrupt\n");
  1673. return;
  1674. }
  1675. ath10k_pci_fw_crashed_clear(ar);
  1676. ath10k_pci_fw_crashed_dump(ar);
  1677. }
  1678. /*
  1679. * Handler for a per-engine interrupt on a PARTICULAR CE.
  1680. * This is used in cases where each CE has a private MSI interrupt.
  1681. */
  1682. static irqreturn_t ath10k_pci_per_engine_handler(int irq, void *arg)
  1683. {
  1684. struct ath10k *ar = arg;
  1685. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1686. int ce_id = irq - ar_pci->pdev->irq - MSI_ASSIGN_CE_INITIAL;
  1687. if (ce_id < 0 || ce_id >= ARRAY_SIZE(ar_pci->pipe_info)) {
  1688. ath10k_warn(ar, "unexpected/invalid irq %d ce_id %d\n", irq,
  1689. ce_id);
  1690. return IRQ_HANDLED;
  1691. }
  1692. /*
  1693. * NOTE: We are able to derive ce_id from irq because we
  1694. * use a one-to-one mapping for CE's 0..5.
  1695. * CE's 6 & 7 do not use interrupts at all.
  1696. *
  1697. * This mapping must be kept in sync with the mapping
  1698. * used by firmware.
  1699. */
  1700. tasklet_schedule(&ar_pci->pipe_info[ce_id].intr);
  1701. return IRQ_HANDLED;
  1702. }
  1703. static irqreturn_t ath10k_pci_msi_fw_handler(int irq, void *arg)
  1704. {
  1705. struct ath10k *ar = arg;
  1706. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1707. tasklet_schedule(&ar_pci->msi_fw_err);
  1708. return IRQ_HANDLED;
  1709. }
  1710. /*
  1711. * Top-level interrupt handler for all PCI interrupts from a Target.
  1712. * When a block of MSI interrupts is allocated, this top-level handler
  1713. * is not used; instead, we directly call the correct sub-handler.
  1714. */
  1715. static irqreturn_t ath10k_pci_interrupt_handler(int irq, void *arg)
  1716. {
  1717. struct ath10k *ar = arg;
  1718. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1719. if (ar_pci->num_msi_intrs == 0) {
  1720. if (!ath10k_pci_irq_pending(ar))
  1721. return IRQ_NONE;
  1722. ath10k_pci_disable_and_clear_legacy_irq(ar);
  1723. }
  1724. tasklet_schedule(&ar_pci->intr_tq);
  1725. return IRQ_HANDLED;
  1726. }
  1727. static void ath10k_pci_tasklet(unsigned long data)
  1728. {
  1729. struct ath10k *ar = (struct ath10k *)data;
  1730. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1731. if (ath10k_pci_has_fw_crashed(ar)) {
  1732. ath10k_pci_fw_crashed_clear(ar);
  1733. ath10k_pci_fw_crashed_dump(ar);
  1734. return;
  1735. }
  1736. ath10k_ce_per_engine_service_any(ar);
  1737. /* Re-enable legacy irq that was disabled in the irq handler */
  1738. if (ar_pci->num_msi_intrs == 0)
  1739. ath10k_pci_enable_legacy_irq(ar);
  1740. }
  1741. static int ath10k_pci_request_irq_msix(struct ath10k *ar)
  1742. {
  1743. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1744. int ret, i;
  1745. ret = request_irq(ar_pci->pdev->irq + MSI_ASSIGN_FW,
  1746. ath10k_pci_msi_fw_handler,
  1747. IRQF_SHARED, "ath10k_pci", ar);
  1748. if (ret) {
  1749. ath10k_warn(ar, "failed to request MSI-X fw irq %d: %d\n",
  1750. ar_pci->pdev->irq + MSI_ASSIGN_FW, ret);
  1751. return ret;
  1752. }
  1753. for (i = MSI_ASSIGN_CE_INITIAL; i <= MSI_ASSIGN_CE_MAX; i++) {
  1754. ret = request_irq(ar_pci->pdev->irq + i,
  1755. ath10k_pci_per_engine_handler,
  1756. IRQF_SHARED, "ath10k_pci", ar);
  1757. if (ret) {
  1758. ath10k_warn(ar, "failed to request MSI-X ce irq %d: %d\n",
  1759. ar_pci->pdev->irq + i, ret);
  1760. for (i--; i >= MSI_ASSIGN_CE_INITIAL; i--)
  1761. free_irq(ar_pci->pdev->irq + i, ar);
  1762. free_irq(ar_pci->pdev->irq + MSI_ASSIGN_FW, ar);
  1763. return ret;
  1764. }
  1765. }
  1766. return 0;
  1767. }
  1768. static int ath10k_pci_request_irq_msi(struct ath10k *ar)
  1769. {
  1770. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1771. int ret;
  1772. ret = request_irq(ar_pci->pdev->irq,
  1773. ath10k_pci_interrupt_handler,
  1774. IRQF_SHARED, "ath10k_pci", ar);
  1775. if (ret) {
  1776. ath10k_warn(ar, "failed to request MSI irq %d: %d\n",
  1777. ar_pci->pdev->irq, ret);
  1778. return ret;
  1779. }
  1780. return 0;
  1781. }
  1782. static int ath10k_pci_request_irq_legacy(struct ath10k *ar)
  1783. {
  1784. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1785. int ret;
  1786. ret = request_irq(ar_pci->pdev->irq,
  1787. ath10k_pci_interrupt_handler,
  1788. IRQF_SHARED, "ath10k_pci", ar);
  1789. if (ret) {
  1790. ath10k_warn(ar, "failed to request legacy irq %d: %d\n",
  1791. ar_pci->pdev->irq, ret);
  1792. return ret;
  1793. }
  1794. return 0;
  1795. }
  1796. static int ath10k_pci_request_irq(struct ath10k *ar)
  1797. {
  1798. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1799. switch (ar_pci->num_msi_intrs) {
  1800. case 0:
  1801. return ath10k_pci_request_irq_legacy(ar);
  1802. case 1:
  1803. return ath10k_pci_request_irq_msi(ar);
  1804. case MSI_NUM_REQUEST:
  1805. return ath10k_pci_request_irq_msix(ar);
  1806. }
  1807. ath10k_warn(ar, "unknown irq configuration upon request\n");
  1808. return -EINVAL;
  1809. }
  1810. static void ath10k_pci_free_irq(struct ath10k *ar)
  1811. {
  1812. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1813. int i;
  1814. /* There's at least one interrupt irregardless whether its legacy INTR
  1815. * or MSI or MSI-X */
  1816. for (i = 0; i < max(1, ar_pci->num_msi_intrs); i++)
  1817. free_irq(ar_pci->pdev->irq + i, ar);
  1818. }
  1819. static void ath10k_pci_init_irq_tasklets(struct ath10k *ar)
  1820. {
  1821. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1822. int i;
  1823. tasklet_init(&ar_pci->intr_tq, ath10k_pci_tasklet, (unsigned long)ar);
  1824. tasklet_init(&ar_pci->msi_fw_err, ath10k_msi_err_tasklet,
  1825. (unsigned long)ar);
  1826. for (i = 0; i < CE_COUNT; i++) {
  1827. ar_pci->pipe_info[i].ar_pci = ar_pci;
  1828. tasklet_init(&ar_pci->pipe_info[i].intr, ath10k_pci_ce_tasklet,
  1829. (unsigned long)&ar_pci->pipe_info[i]);
  1830. }
  1831. }
  1832. static int ath10k_pci_init_irq(struct ath10k *ar)
  1833. {
  1834. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1835. int ret;
  1836. ath10k_pci_init_irq_tasklets(ar);
  1837. if (ath10k_pci_irq_mode != ATH10K_PCI_IRQ_AUTO)
  1838. ath10k_info(ar, "limiting irq mode to: %d\n",
  1839. ath10k_pci_irq_mode);
  1840. /* Try MSI-X */
  1841. if (ath10k_pci_irq_mode == ATH10K_PCI_IRQ_AUTO) {
  1842. ar_pci->num_msi_intrs = MSI_NUM_REQUEST;
  1843. ret = pci_enable_msi_range(ar_pci->pdev, ar_pci->num_msi_intrs,
  1844. ar_pci->num_msi_intrs);
  1845. if (ret > 0)
  1846. return 0;
  1847. /* fall-through */
  1848. }
  1849. /* Try MSI */
  1850. if (ath10k_pci_irq_mode != ATH10K_PCI_IRQ_LEGACY) {
  1851. ar_pci->num_msi_intrs = 1;
  1852. ret = pci_enable_msi(ar_pci->pdev);
  1853. if (ret == 0)
  1854. return 0;
  1855. /* fall-through */
  1856. }
  1857. /* Try legacy irq
  1858. *
  1859. * A potential race occurs here: The CORE_BASE write
  1860. * depends on target correctly decoding AXI address but
  1861. * host won't know when target writes BAR to CORE_CTRL.
  1862. * This write might get lost if target has NOT written BAR.
  1863. * For now, fix the race by repeating the write in below
  1864. * synchronization checking. */
  1865. ar_pci->num_msi_intrs = 0;
  1866. ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
  1867. PCIE_INTR_FIRMWARE_MASK | PCIE_INTR_CE_MASK_ALL);
  1868. return 0;
  1869. }
  1870. static void ath10k_pci_deinit_irq_legacy(struct ath10k *ar)
  1871. {
  1872. ath10k_pci_write32(ar, SOC_CORE_BASE_ADDRESS + PCIE_INTR_ENABLE_ADDRESS,
  1873. 0);
  1874. }
  1875. static int ath10k_pci_deinit_irq(struct ath10k *ar)
  1876. {
  1877. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1878. switch (ar_pci->num_msi_intrs) {
  1879. case 0:
  1880. ath10k_pci_deinit_irq_legacy(ar);
  1881. return 0;
  1882. case 1:
  1883. /* fall-through */
  1884. case MSI_NUM_REQUEST:
  1885. pci_disable_msi(ar_pci->pdev);
  1886. return 0;
  1887. default:
  1888. pci_disable_msi(ar_pci->pdev);
  1889. }
  1890. ath10k_warn(ar, "unknown irq configuration upon deinit\n");
  1891. return -EINVAL;
  1892. }
  1893. static int ath10k_pci_wait_for_target_init(struct ath10k *ar)
  1894. {
  1895. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1896. unsigned long timeout;
  1897. u32 val;
  1898. ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot waiting target to initialise\n");
  1899. timeout = jiffies + msecs_to_jiffies(ATH10K_PCI_TARGET_WAIT);
  1900. do {
  1901. val = ath10k_pci_read32(ar, FW_INDICATOR_ADDRESS);
  1902. ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot target indicator %x\n",
  1903. val);
  1904. /* target should never return this */
  1905. if (val == 0xffffffff)
  1906. continue;
  1907. /* the device has crashed so don't bother trying anymore */
  1908. if (val & FW_IND_EVENT_PENDING)
  1909. break;
  1910. if (val & FW_IND_INITIALIZED)
  1911. break;
  1912. if (ar_pci->num_msi_intrs == 0)
  1913. /* Fix potential race by repeating CORE_BASE writes */
  1914. ath10k_pci_enable_legacy_irq(ar);
  1915. mdelay(10);
  1916. } while (time_before(jiffies, timeout));
  1917. ath10k_pci_disable_and_clear_legacy_irq(ar);
  1918. ath10k_pci_irq_msi_fw_mask(ar);
  1919. if (val == 0xffffffff) {
  1920. ath10k_err(ar, "failed to read device register, device is gone\n");
  1921. return -EIO;
  1922. }
  1923. if (val & FW_IND_EVENT_PENDING) {
  1924. ath10k_warn(ar, "device has crashed during init\n");
  1925. ath10k_pci_fw_crashed_clear(ar);
  1926. ath10k_pci_fw_crashed_dump(ar);
  1927. return -ECOMM;
  1928. }
  1929. if (!(val & FW_IND_INITIALIZED)) {
  1930. ath10k_err(ar, "failed to receive initialized event from target: %08x\n",
  1931. val);
  1932. return -ETIMEDOUT;
  1933. }
  1934. ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot target initialised\n");
  1935. return 0;
  1936. }
  1937. static int ath10k_pci_cold_reset(struct ath10k *ar)
  1938. {
  1939. int i;
  1940. u32 val;
  1941. ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot cold reset\n");
  1942. spin_lock_bh(&ar->data_lock);
  1943. ar->stats.fw_cold_reset_counter++;
  1944. spin_unlock_bh(&ar->data_lock);
  1945. /* Put Target, including PCIe, into RESET. */
  1946. val = ath10k_pci_reg_read32(ar, SOC_GLOBAL_RESET_ADDRESS);
  1947. val |= 1;
  1948. ath10k_pci_reg_write32(ar, SOC_GLOBAL_RESET_ADDRESS, val);
  1949. for (i = 0; i < ATH_PCI_RESET_WAIT_MAX; i++) {
  1950. if (ath10k_pci_reg_read32(ar, RTC_STATE_ADDRESS) &
  1951. RTC_STATE_COLD_RESET_MASK)
  1952. break;
  1953. msleep(1);
  1954. }
  1955. /* Pull Target, including PCIe, out of RESET. */
  1956. val &= ~1;
  1957. ath10k_pci_reg_write32(ar, SOC_GLOBAL_RESET_ADDRESS, val);
  1958. for (i = 0; i < ATH_PCI_RESET_WAIT_MAX; i++) {
  1959. if (!(ath10k_pci_reg_read32(ar, RTC_STATE_ADDRESS) &
  1960. RTC_STATE_COLD_RESET_MASK))
  1961. break;
  1962. msleep(1);
  1963. }
  1964. ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot cold reset complete\n");
  1965. return 0;
  1966. }
  1967. static int ath10k_pci_claim(struct ath10k *ar)
  1968. {
  1969. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1970. struct pci_dev *pdev = ar_pci->pdev;
  1971. u32 lcr_val;
  1972. int ret;
  1973. pci_set_drvdata(pdev, ar);
  1974. ret = pci_enable_device(pdev);
  1975. if (ret) {
  1976. ath10k_err(ar, "failed to enable pci device: %d\n", ret);
  1977. return ret;
  1978. }
  1979. ret = pci_request_region(pdev, BAR_NUM, "ath");
  1980. if (ret) {
  1981. ath10k_err(ar, "failed to request region BAR%d: %d\n", BAR_NUM,
  1982. ret);
  1983. goto err_device;
  1984. }
  1985. /* Target expects 32 bit DMA. Enforce it. */
  1986. ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  1987. if (ret) {
  1988. ath10k_err(ar, "failed to set dma mask to 32-bit: %d\n", ret);
  1989. goto err_region;
  1990. }
  1991. ret = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  1992. if (ret) {
  1993. ath10k_err(ar, "failed to set consistent dma mask to 32-bit: %d\n",
  1994. ret);
  1995. goto err_region;
  1996. }
  1997. pci_set_master(pdev);
  1998. /* Workaround: Disable ASPM */
  1999. pci_read_config_dword(pdev, 0x80, &lcr_val);
  2000. pci_write_config_dword(pdev, 0x80, (lcr_val & 0xffffff00));
  2001. /* Arrange for access to Target SoC registers. */
  2002. ar_pci->mem = pci_iomap(pdev, BAR_NUM, 0);
  2003. if (!ar_pci->mem) {
  2004. ath10k_err(ar, "failed to iomap BAR%d\n", BAR_NUM);
  2005. ret = -EIO;
  2006. goto err_master;
  2007. }
  2008. ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot pci_mem 0x%p\n", ar_pci->mem);
  2009. return 0;
  2010. err_master:
  2011. pci_clear_master(pdev);
  2012. err_region:
  2013. pci_release_region(pdev, BAR_NUM);
  2014. err_device:
  2015. pci_disable_device(pdev);
  2016. return ret;
  2017. }
  2018. static void ath10k_pci_release(struct ath10k *ar)
  2019. {
  2020. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  2021. struct pci_dev *pdev = ar_pci->pdev;
  2022. pci_iounmap(pdev, ar_pci->mem);
  2023. pci_release_region(pdev, BAR_NUM);
  2024. pci_clear_master(pdev);
  2025. pci_disable_device(pdev);
  2026. }
  2027. static int ath10k_pci_probe(struct pci_dev *pdev,
  2028. const struct pci_device_id *pci_dev)
  2029. {
  2030. int ret = 0;
  2031. struct ath10k *ar;
  2032. struct ath10k_pci *ar_pci;
  2033. u32 chip_id;
  2034. ar = ath10k_core_create(sizeof(*ar_pci), &pdev->dev,
  2035. ATH10K_BUS_PCI,
  2036. &ath10k_pci_hif_ops);
  2037. if (!ar) {
  2038. dev_err(&pdev->dev, "failed to allocate core\n");
  2039. return -ENOMEM;
  2040. }
  2041. ath10k_dbg(ar, ATH10K_DBG_PCI, "pci probe\n");
  2042. ar_pci = ath10k_pci_priv(ar);
  2043. ar_pci->pdev = pdev;
  2044. ar_pci->dev = &pdev->dev;
  2045. ar_pci->ar = ar;
  2046. spin_lock_init(&ar_pci->ce_lock);
  2047. setup_timer(&ar_pci->rx_post_retry, ath10k_pci_rx_replenish_retry,
  2048. (unsigned long)ar);
  2049. ret = ath10k_pci_claim(ar);
  2050. if (ret) {
  2051. ath10k_err(ar, "failed to claim device: %d\n", ret);
  2052. goto err_core_destroy;
  2053. }
  2054. ret = ath10k_pci_wake(ar);
  2055. if (ret) {
  2056. ath10k_err(ar, "failed to wake up: %d\n", ret);
  2057. goto err_release;
  2058. }
  2059. chip_id = ath10k_pci_soc_read32(ar, SOC_CHIP_ID_ADDRESS);
  2060. if (chip_id == 0xffffffff) {
  2061. ath10k_err(ar, "failed to get chip id\n");
  2062. goto err_sleep;
  2063. }
  2064. ret = ath10k_pci_alloc_pipes(ar);
  2065. if (ret) {
  2066. ath10k_err(ar, "failed to allocate copy engine pipes: %d\n",
  2067. ret);
  2068. goto err_sleep;
  2069. }
  2070. ath10k_pci_ce_deinit(ar);
  2071. ath10k_pci_irq_disable(ar);
  2072. ret = ath10k_pci_init_irq(ar);
  2073. if (ret) {
  2074. ath10k_err(ar, "failed to init irqs: %d\n", ret);
  2075. goto err_free_pipes;
  2076. }
  2077. ath10k_info(ar, "pci irq %s interrupts %d irq_mode %d reset_mode %d\n",
  2078. ath10k_pci_get_irq_method(ar), ar_pci->num_msi_intrs,
  2079. ath10k_pci_irq_mode, ath10k_pci_reset_mode);
  2080. ret = ath10k_pci_request_irq(ar);
  2081. if (ret) {
  2082. ath10k_warn(ar, "failed to request irqs: %d\n", ret);
  2083. goto err_deinit_irq;
  2084. }
  2085. ath10k_pci_sleep(ar);
  2086. ret = ath10k_core_register(ar, chip_id);
  2087. if (ret) {
  2088. ath10k_err(ar, "failed to register driver core: %d\n", ret);
  2089. goto err_free_irq;
  2090. }
  2091. return 0;
  2092. err_free_irq:
  2093. ath10k_pci_free_irq(ar);
  2094. ath10k_pci_kill_tasklet(ar);
  2095. err_deinit_irq:
  2096. ath10k_pci_deinit_irq(ar);
  2097. err_free_pipes:
  2098. ath10k_pci_free_pipes(ar);
  2099. err_sleep:
  2100. ath10k_pci_sleep(ar);
  2101. err_release:
  2102. ath10k_pci_release(ar);
  2103. err_core_destroy:
  2104. ath10k_core_destroy(ar);
  2105. return ret;
  2106. }
  2107. static void ath10k_pci_remove(struct pci_dev *pdev)
  2108. {
  2109. struct ath10k *ar = pci_get_drvdata(pdev);
  2110. struct ath10k_pci *ar_pci;
  2111. ath10k_dbg(ar, ATH10K_DBG_PCI, "pci remove\n");
  2112. if (!ar)
  2113. return;
  2114. ar_pci = ath10k_pci_priv(ar);
  2115. if (!ar_pci)
  2116. return;
  2117. ath10k_core_unregister(ar);
  2118. ath10k_pci_free_irq(ar);
  2119. ath10k_pci_kill_tasklet(ar);
  2120. ath10k_pci_deinit_irq(ar);
  2121. ath10k_pci_ce_deinit(ar);
  2122. ath10k_pci_free_pipes(ar);
  2123. ath10k_pci_release(ar);
  2124. ath10k_core_destroy(ar);
  2125. }
  2126. MODULE_DEVICE_TABLE(pci, ath10k_pci_id_table);
  2127. static struct pci_driver ath10k_pci_driver = {
  2128. .name = "ath10k_pci",
  2129. .id_table = ath10k_pci_id_table,
  2130. .probe = ath10k_pci_probe,
  2131. .remove = ath10k_pci_remove,
  2132. };
  2133. static int __init ath10k_pci_init(void)
  2134. {
  2135. int ret;
  2136. ret = pci_register_driver(&ath10k_pci_driver);
  2137. if (ret)
  2138. printk(KERN_ERR "failed to register ath10k pci driver: %d\n",
  2139. ret);
  2140. return ret;
  2141. }
  2142. module_init(ath10k_pci_init);
  2143. static void __exit ath10k_pci_exit(void)
  2144. {
  2145. pci_unregister_driver(&ath10k_pci_driver);
  2146. }
  2147. module_exit(ath10k_pci_exit);
  2148. MODULE_AUTHOR("Qualcomm Atheros");
  2149. MODULE_DESCRIPTION("Driver support for Atheros QCA988X PCIe devices");
  2150. MODULE_LICENSE("Dual BSD/GPL");
  2151. MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" QCA988X_HW_2_0_FW_FILE);
  2152. MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" ATH10K_FW_API2_FILE);
  2153. MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" ATH10K_FW_API3_FILE);
  2154. MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" QCA988X_HW_2_0_BOARD_DATA_FILE);