pci.c 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507
  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 "core.h"
  22. #include "debug.h"
  23. #include "targaddrs.h"
  24. #include "bmi.h"
  25. #include "hif.h"
  26. #include "htc.h"
  27. #include "ce.h"
  28. #include "pci.h"
  29. unsigned int ath10k_target_ps;
  30. module_param(ath10k_target_ps, uint, 0644);
  31. MODULE_PARM_DESC(ath10k_target_ps, "Enable ath10k Target (SoC) PS option");
  32. #define QCA988X_1_0_DEVICE_ID (0xabcd)
  33. #define QCA988X_2_0_DEVICE_ID (0x003c)
  34. static DEFINE_PCI_DEVICE_TABLE(ath10k_pci_id_table) = {
  35. { PCI_VDEVICE(ATHEROS, QCA988X_1_0_DEVICE_ID) }, /* PCI-E QCA988X V1 */
  36. { PCI_VDEVICE(ATHEROS, QCA988X_2_0_DEVICE_ID) }, /* PCI-E QCA988X V2 */
  37. {0}
  38. };
  39. static int ath10k_pci_diag_read_access(struct ath10k *ar, u32 address,
  40. u32 *data);
  41. static void ath10k_pci_process_ce(struct ath10k *ar);
  42. static int ath10k_pci_post_rx(struct ath10k *ar);
  43. static int ath10k_pci_post_rx_pipe(struct hif_ce_pipe_info *pipe_info,
  44. int num);
  45. static void ath10k_pci_rx_pipe_cleanup(struct hif_ce_pipe_info *pipe_info);
  46. static void ath10k_pci_stop_ce(struct ath10k *ar);
  47. static const struct ce_attr host_ce_config_wlan[] = {
  48. /* host->target HTC control and raw streams */
  49. { /* CE0 */ CE_ATTR_FLAGS, 0, 16, 256, 0, NULL,},
  50. /* could be moved to share CE3 */
  51. /* target->host HTT + HTC control */
  52. { /* CE1 */ CE_ATTR_FLAGS, 0, 0, 512, 512, NULL,},
  53. /* target->host WMI */
  54. { /* CE2 */ CE_ATTR_FLAGS, 0, 0, 2048, 32, NULL,},
  55. /* host->target WMI */
  56. { /* CE3 */ CE_ATTR_FLAGS, 0, 32, 2048, 0, NULL,},
  57. /* host->target HTT */
  58. { /* CE4 */ CE_ATTR_FLAGS | CE_ATTR_DIS_INTR, 0,
  59. CE_HTT_H2T_MSG_SRC_NENTRIES, 256, 0, NULL,},
  60. /* unused */
  61. { /* CE5 */ CE_ATTR_FLAGS, 0, 0, 0, 0, NULL,},
  62. /* Target autonomous hif_memcpy */
  63. { /* CE6 */ CE_ATTR_FLAGS, 0, 0, 0, 0, NULL,},
  64. /* ce_diag, the Diagnostic Window */
  65. { /* CE7 */ CE_ATTR_FLAGS, 0, 2, DIAG_TRANSFER_LIMIT, 2, NULL,},
  66. };
  67. /* Target firmware's Copy Engine configuration. */
  68. static const struct ce_pipe_config target_ce_config_wlan[] = {
  69. /* host->target HTC control and raw streams */
  70. { /* CE0 */ 0, PIPEDIR_OUT, 32, 256, CE_ATTR_FLAGS, 0,},
  71. /* target->host HTT + HTC control */
  72. { /* CE1 */ 1, PIPEDIR_IN, 32, 512, CE_ATTR_FLAGS, 0,},
  73. /* target->host WMI */
  74. { /* CE2 */ 2, PIPEDIR_IN, 32, 2048, CE_ATTR_FLAGS, 0,},
  75. /* host->target WMI */
  76. { /* CE3 */ 3, PIPEDIR_OUT, 32, 2048, CE_ATTR_FLAGS, 0,},
  77. /* host->target HTT */
  78. { /* CE4 */ 4, PIPEDIR_OUT, 256, 256, CE_ATTR_FLAGS, 0,},
  79. /* NB: 50% of src nentries, since tx has 2 frags */
  80. /* unused */
  81. { /* CE5 */ 5, PIPEDIR_OUT, 32, 2048, CE_ATTR_FLAGS, 0,},
  82. /* Reserved for target autonomous hif_memcpy */
  83. { /* CE6 */ 6, PIPEDIR_INOUT, 32, 4096, CE_ATTR_FLAGS, 0,},
  84. /* CE7 used only by Host */
  85. };
  86. /*
  87. * Diagnostic read/write access is provided for startup/config/debug usage.
  88. * Caller must guarantee proper alignment, when applicable, and single user
  89. * at any moment.
  90. */
  91. static int ath10k_pci_diag_read_mem(struct ath10k *ar, u32 address, void *data,
  92. int nbytes)
  93. {
  94. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  95. int ret = 0;
  96. u32 buf;
  97. unsigned int completed_nbytes, orig_nbytes, remaining_bytes;
  98. unsigned int id;
  99. unsigned int flags;
  100. struct ce_state *ce_diag;
  101. /* Host buffer address in CE space */
  102. u32 ce_data;
  103. dma_addr_t ce_data_base = 0;
  104. void *data_buf = NULL;
  105. int i;
  106. /*
  107. * This code cannot handle reads to non-memory space. Redirect to the
  108. * register read fn but preserve the multi word read capability of
  109. * this fn
  110. */
  111. if (address < DRAM_BASE_ADDRESS) {
  112. if (!IS_ALIGNED(address, 4) ||
  113. !IS_ALIGNED((unsigned long)data, 4))
  114. return -EIO;
  115. while ((nbytes >= 4) && ((ret = ath10k_pci_diag_read_access(
  116. ar, address, (u32 *)data)) == 0)) {
  117. nbytes -= sizeof(u32);
  118. address += sizeof(u32);
  119. data += sizeof(u32);
  120. }
  121. return ret;
  122. }
  123. ce_diag = ar_pci->ce_diag;
  124. /*
  125. * Allocate a temporary bounce buffer to hold caller's data
  126. * to be DMA'ed from Target. This guarantees
  127. * 1) 4-byte alignment
  128. * 2) Buffer in DMA-able space
  129. */
  130. orig_nbytes = nbytes;
  131. data_buf = (unsigned char *)pci_alloc_consistent(ar_pci->pdev,
  132. orig_nbytes,
  133. &ce_data_base);
  134. if (!data_buf) {
  135. ret = -ENOMEM;
  136. goto done;
  137. }
  138. memset(data_buf, 0, orig_nbytes);
  139. remaining_bytes = orig_nbytes;
  140. ce_data = ce_data_base;
  141. while (remaining_bytes) {
  142. nbytes = min_t(unsigned int, remaining_bytes,
  143. DIAG_TRANSFER_LIMIT);
  144. ret = ath10k_ce_recv_buf_enqueue(ce_diag, NULL, ce_data);
  145. if (ret != 0)
  146. goto done;
  147. /* Request CE to send from Target(!) address to Host buffer */
  148. /*
  149. * The address supplied by the caller is in the
  150. * Target CPU virtual address space.
  151. *
  152. * In order to use this address with the diagnostic CE,
  153. * convert it from Target CPU virtual address space
  154. * to CE address space
  155. */
  156. ath10k_pci_wake(ar);
  157. address = TARG_CPU_SPACE_TO_CE_SPACE(ar, ar_pci->mem,
  158. address);
  159. ath10k_pci_sleep(ar);
  160. ret = ath10k_ce_send(ce_diag, NULL, (u32)address, nbytes, 0,
  161. 0);
  162. if (ret)
  163. goto done;
  164. i = 0;
  165. while (ath10k_ce_completed_send_next(ce_diag, NULL, &buf,
  166. &completed_nbytes,
  167. &id) != 0) {
  168. mdelay(1);
  169. if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
  170. ret = -EBUSY;
  171. goto done;
  172. }
  173. }
  174. if (nbytes != completed_nbytes) {
  175. ret = -EIO;
  176. goto done;
  177. }
  178. if (buf != (u32) address) {
  179. ret = -EIO;
  180. goto done;
  181. }
  182. i = 0;
  183. while (ath10k_ce_completed_recv_next(ce_diag, NULL, &buf,
  184. &completed_nbytes,
  185. &id, &flags) != 0) {
  186. mdelay(1);
  187. if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
  188. ret = -EBUSY;
  189. goto done;
  190. }
  191. }
  192. if (nbytes != completed_nbytes) {
  193. ret = -EIO;
  194. goto done;
  195. }
  196. if (buf != ce_data) {
  197. ret = -EIO;
  198. goto done;
  199. }
  200. remaining_bytes -= nbytes;
  201. address += nbytes;
  202. ce_data += nbytes;
  203. }
  204. done:
  205. if (ret == 0) {
  206. /* Copy data from allocated DMA buf to caller's buf */
  207. WARN_ON_ONCE(orig_nbytes & 3);
  208. for (i = 0; i < orig_nbytes / sizeof(__le32); i++) {
  209. ((u32 *)data)[i] =
  210. __le32_to_cpu(((__le32 *)data_buf)[i]);
  211. }
  212. } else
  213. ath10k_dbg(ATH10K_DBG_PCI, "%s failure (0x%x)\n",
  214. __func__, address);
  215. if (data_buf)
  216. pci_free_consistent(ar_pci->pdev, orig_nbytes,
  217. data_buf, ce_data_base);
  218. return ret;
  219. }
  220. /* Read 4-byte aligned data from Target memory or register */
  221. static int ath10k_pci_diag_read_access(struct ath10k *ar, u32 address,
  222. u32 *data)
  223. {
  224. /* Assume range doesn't cross this boundary */
  225. if (address >= DRAM_BASE_ADDRESS)
  226. return ath10k_pci_diag_read_mem(ar, address, data, sizeof(u32));
  227. ath10k_pci_wake(ar);
  228. *data = ath10k_pci_read32(ar, address);
  229. ath10k_pci_sleep(ar);
  230. return 0;
  231. }
  232. static int ath10k_pci_diag_write_mem(struct ath10k *ar, u32 address,
  233. const void *data, int nbytes)
  234. {
  235. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  236. int ret = 0;
  237. u32 buf;
  238. unsigned int completed_nbytes, orig_nbytes, remaining_bytes;
  239. unsigned int id;
  240. unsigned int flags;
  241. struct ce_state *ce_diag;
  242. void *data_buf = NULL;
  243. u32 ce_data; /* Host buffer address in CE space */
  244. dma_addr_t ce_data_base = 0;
  245. int i;
  246. ce_diag = ar_pci->ce_diag;
  247. /*
  248. * Allocate a temporary bounce buffer to hold caller's data
  249. * to be DMA'ed to Target. This guarantees
  250. * 1) 4-byte alignment
  251. * 2) Buffer in DMA-able space
  252. */
  253. orig_nbytes = nbytes;
  254. data_buf = (unsigned char *)pci_alloc_consistent(ar_pci->pdev,
  255. orig_nbytes,
  256. &ce_data_base);
  257. if (!data_buf) {
  258. ret = -ENOMEM;
  259. goto done;
  260. }
  261. /* Copy caller's data to allocated DMA buf */
  262. WARN_ON_ONCE(orig_nbytes & 3);
  263. for (i = 0; i < orig_nbytes / sizeof(__le32); i++)
  264. ((__le32 *)data_buf)[i] = __cpu_to_le32(((u32 *)data)[i]);
  265. /*
  266. * The address supplied by the caller is in the
  267. * Target CPU virtual address space.
  268. *
  269. * In order to use this address with the diagnostic CE,
  270. * convert it from
  271. * Target CPU virtual address space
  272. * to
  273. * CE address space
  274. */
  275. ath10k_pci_wake(ar);
  276. address = TARG_CPU_SPACE_TO_CE_SPACE(ar, ar_pci->mem, address);
  277. ath10k_pci_sleep(ar);
  278. remaining_bytes = orig_nbytes;
  279. ce_data = ce_data_base;
  280. while (remaining_bytes) {
  281. /* FIXME: check cast */
  282. nbytes = min_t(int, remaining_bytes, DIAG_TRANSFER_LIMIT);
  283. /* Set up to receive directly into Target(!) address */
  284. ret = ath10k_ce_recv_buf_enqueue(ce_diag, NULL, address);
  285. if (ret != 0)
  286. goto done;
  287. /*
  288. * Request CE to send caller-supplied data that
  289. * was copied to bounce buffer to Target(!) address.
  290. */
  291. ret = ath10k_ce_send(ce_diag, NULL, (u32) ce_data,
  292. nbytes, 0, 0);
  293. if (ret != 0)
  294. goto done;
  295. i = 0;
  296. while (ath10k_ce_completed_send_next(ce_diag, NULL, &buf,
  297. &completed_nbytes,
  298. &id) != 0) {
  299. mdelay(1);
  300. if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
  301. ret = -EBUSY;
  302. goto done;
  303. }
  304. }
  305. if (nbytes != completed_nbytes) {
  306. ret = -EIO;
  307. goto done;
  308. }
  309. if (buf != ce_data) {
  310. ret = -EIO;
  311. goto done;
  312. }
  313. i = 0;
  314. while (ath10k_ce_completed_recv_next(ce_diag, NULL, &buf,
  315. &completed_nbytes,
  316. &id, &flags) != 0) {
  317. mdelay(1);
  318. if (i++ > DIAG_ACCESS_CE_TIMEOUT_MS) {
  319. ret = -EBUSY;
  320. goto done;
  321. }
  322. }
  323. if (nbytes != completed_nbytes) {
  324. ret = -EIO;
  325. goto done;
  326. }
  327. if (buf != address) {
  328. ret = -EIO;
  329. goto done;
  330. }
  331. remaining_bytes -= nbytes;
  332. address += nbytes;
  333. ce_data += nbytes;
  334. }
  335. done:
  336. if (data_buf) {
  337. pci_free_consistent(ar_pci->pdev, orig_nbytes, data_buf,
  338. ce_data_base);
  339. }
  340. if (ret != 0)
  341. ath10k_dbg(ATH10K_DBG_PCI, "%s failure (0x%x)\n", __func__,
  342. address);
  343. return ret;
  344. }
  345. /* Write 4B data to Target memory or register */
  346. static int ath10k_pci_diag_write_access(struct ath10k *ar, u32 address,
  347. u32 data)
  348. {
  349. /* Assume range doesn't cross this boundary */
  350. if (address >= DRAM_BASE_ADDRESS)
  351. return ath10k_pci_diag_write_mem(ar, address, &data,
  352. sizeof(u32));
  353. ath10k_pci_wake(ar);
  354. ath10k_pci_write32(ar, address, data);
  355. ath10k_pci_sleep(ar);
  356. return 0;
  357. }
  358. static bool ath10k_pci_target_is_awake(struct ath10k *ar)
  359. {
  360. void __iomem *mem = ath10k_pci_priv(ar)->mem;
  361. u32 val;
  362. val = ioread32(mem + PCIE_LOCAL_BASE_ADDRESS +
  363. RTC_STATE_ADDRESS);
  364. return (RTC_STATE_V_GET(val) == RTC_STATE_V_ON);
  365. }
  366. static void ath10k_pci_wait(struct ath10k *ar)
  367. {
  368. int n = 100;
  369. while (n-- && !ath10k_pci_target_is_awake(ar))
  370. msleep(10);
  371. if (n < 0)
  372. ath10k_warn("Unable to wakeup target\n");
  373. }
  374. void ath10k_do_pci_wake(struct ath10k *ar)
  375. {
  376. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  377. void __iomem *pci_addr = ar_pci->mem;
  378. int tot_delay = 0;
  379. int curr_delay = 5;
  380. if (atomic_read(&ar_pci->keep_awake_count) == 0) {
  381. /* Force AWAKE */
  382. iowrite32(PCIE_SOC_WAKE_V_MASK,
  383. pci_addr + PCIE_LOCAL_BASE_ADDRESS +
  384. PCIE_SOC_WAKE_ADDRESS);
  385. }
  386. atomic_inc(&ar_pci->keep_awake_count);
  387. if (ar_pci->verified_awake)
  388. return;
  389. for (;;) {
  390. if (ath10k_pci_target_is_awake(ar)) {
  391. ar_pci->verified_awake = true;
  392. break;
  393. }
  394. if (tot_delay > PCIE_WAKE_TIMEOUT) {
  395. ath10k_warn("target takes too long to wake up (awake count %d)\n",
  396. atomic_read(&ar_pci->keep_awake_count));
  397. break;
  398. }
  399. udelay(curr_delay);
  400. tot_delay += curr_delay;
  401. if (curr_delay < 50)
  402. curr_delay += 5;
  403. }
  404. }
  405. void ath10k_do_pci_sleep(struct ath10k *ar)
  406. {
  407. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  408. void __iomem *pci_addr = ar_pci->mem;
  409. if (atomic_dec_and_test(&ar_pci->keep_awake_count)) {
  410. /* Allow sleep */
  411. ar_pci->verified_awake = false;
  412. iowrite32(PCIE_SOC_WAKE_RESET,
  413. pci_addr + PCIE_LOCAL_BASE_ADDRESS +
  414. PCIE_SOC_WAKE_ADDRESS);
  415. }
  416. }
  417. /*
  418. * FIXME: Handle OOM properly.
  419. */
  420. static inline
  421. struct ath10k_pci_compl *get_free_compl(struct hif_ce_pipe_info *pipe_info)
  422. {
  423. struct ath10k_pci_compl *compl = NULL;
  424. spin_lock_bh(&pipe_info->pipe_lock);
  425. if (list_empty(&pipe_info->compl_free)) {
  426. ath10k_warn("Completion buffers are full\n");
  427. goto exit;
  428. }
  429. compl = list_first_entry(&pipe_info->compl_free,
  430. struct ath10k_pci_compl, list);
  431. list_del(&compl->list);
  432. exit:
  433. spin_unlock_bh(&pipe_info->pipe_lock);
  434. return compl;
  435. }
  436. /* Called by lower (CE) layer when a send to Target completes. */
  437. static void ath10k_pci_ce_send_done(struct ce_state *ce_state,
  438. void *transfer_context,
  439. u32 ce_data,
  440. unsigned int nbytes,
  441. unsigned int transfer_id)
  442. {
  443. struct ath10k *ar = ce_state->ar;
  444. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  445. struct hif_ce_pipe_info *pipe_info = &ar_pci->pipe_info[ce_state->id];
  446. struct ath10k_pci_compl *compl;
  447. bool process = false;
  448. do {
  449. /*
  450. * For the send completion of an item in sendlist, just
  451. * increment num_sends_allowed. The upper layer callback will
  452. * be triggered when last fragment is done with send.
  453. */
  454. if (transfer_context == CE_SENDLIST_ITEM_CTXT) {
  455. spin_lock_bh(&pipe_info->pipe_lock);
  456. pipe_info->num_sends_allowed++;
  457. spin_unlock_bh(&pipe_info->pipe_lock);
  458. continue;
  459. }
  460. compl = get_free_compl(pipe_info);
  461. if (!compl)
  462. break;
  463. compl->send_or_recv = HIF_CE_COMPLETE_SEND;
  464. compl->ce_state = ce_state;
  465. compl->pipe_info = pipe_info;
  466. compl->transfer_context = transfer_context;
  467. compl->nbytes = nbytes;
  468. compl->transfer_id = transfer_id;
  469. compl->flags = 0;
  470. /*
  471. * Add the completion to the processing queue.
  472. */
  473. spin_lock_bh(&ar_pci->compl_lock);
  474. list_add_tail(&compl->list, &ar_pci->compl_process);
  475. spin_unlock_bh(&ar_pci->compl_lock);
  476. process = true;
  477. } while (ath10k_ce_completed_send_next(ce_state,
  478. &transfer_context,
  479. &ce_data, &nbytes,
  480. &transfer_id) == 0);
  481. /*
  482. * If only some of the items within a sendlist have completed,
  483. * don't invoke completion processing until the entire sendlist
  484. * has been sent.
  485. */
  486. if (!process)
  487. return;
  488. ath10k_pci_process_ce(ar);
  489. }
  490. /* Called by lower (CE) layer when data is received from the Target. */
  491. static void ath10k_pci_ce_recv_data(struct ce_state *ce_state,
  492. void *transfer_context, u32 ce_data,
  493. unsigned int nbytes,
  494. unsigned int transfer_id,
  495. unsigned int flags)
  496. {
  497. struct ath10k *ar = ce_state->ar;
  498. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  499. struct hif_ce_pipe_info *pipe_info = &ar_pci->pipe_info[ce_state->id];
  500. struct ath10k_pci_compl *compl;
  501. struct sk_buff *skb;
  502. do {
  503. compl = get_free_compl(pipe_info);
  504. if (!compl)
  505. break;
  506. compl->send_or_recv = HIF_CE_COMPLETE_RECV;
  507. compl->ce_state = ce_state;
  508. compl->pipe_info = pipe_info;
  509. compl->transfer_context = transfer_context;
  510. compl->nbytes = nbytes;
  511. compl->transfer_id = transfer_id;
  512. compl->flags = flags;
  513. skb = transfer_context;
  514. dma_unmap_single(ar->dev, ATH10K_SKB_CB(skb)->paddr,
  515. skb->len + skb_tailroom(skb),
  516. DMA_FROM_DEVICE);
  517. /*
  518. * Add the completion to the processing queue.
  519. */
  520. spin_lock_bh(&ar_pci->compl_lock);
  521. list_add_tail(&compl->list, &ar_pci->compl_process);
  522. spin_unlock_bh(&ar_pci->compl_lock);
  523. } while (ath10k_ce_completed_recv_next(ce_state,
  524. &transfer_context,
  525. &ce_data, &nbytes,
  526. &transfer_id,
  527. &flags) == 0);
  528. ath10k_pci_process_ce(ar);
  529. }
  530. /* Send the first nbytes bytes of the buffer */
  531. static int ath10k_pci_hif_send_head(struct ath10k *ar, u8 pipe_id,
  532. unsigned int transfer_id,
  533. unsigned int bytes, struct sk_buff *nbuf)
  534. {
  535. struct ath10k_skb_cb *skb_cb = ATH10K_SKB_CB(nbuf);
  536. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  537. struct hif_ce_pipe_info *pipe_info = &(ar_pci->pipe_info[pipe_id]);
  538. struct ce_state *ce_hdl = pipe_info->ce_hdl;
  539. struct ce_sendlist sendlist;
  540. unsigned int len;
  541. u32 flags = 0;
  542. int ret;
  543. memset(&sendlist, 0, sizeof(struct ce_sendlist));
  544. len = min(bytes, nbuf->len);
  545. bytes -= len;
  546. if (len & 3)
  547. ath10k_warn("skb not aligned to 4-byte boundary (%d)\n", len);
  548. ath10k_dbg(ATH10K_DBG_PCI,
  549. "pci send data vaddr %p paddr 0x%llx len %d as %d bytes\n",
  550. nbuf->data, (unsigned long long) skb_cb->paddr,
  551. nbuf->len, len);
  552. ath10k_dbg_dump(ATH10K_DBG_PCI_DUMP, NULL,
  553. "ath10k tx: data: ",
  554. nbuf->data, nbuf->len);
  555. ath10k_ce_sendlist_buf_add(&sendlist, skb_cb->paddr, len, flags);
  556. /* Make sure we have resources to handle this request */
  557. spin_lock_bh(&pipe_info->pipe_lock);
  558. if (!pipe_info->num_sends_allowed) {
  559. ath10k_warn("Pipe: %d is full\n", pipe_id);
  560. spin_unlock_bh(&pipe_info->pipe_lock);
  561. return -ENOSR;
  562. }
  563. pipe_info->num_sends_allowed--;
  564. spin_unlock_bh(&pipe_info->pipe_lock);
  565. ret = ath10k_ce_sendlist_send(ce_hdl, nbuf, &sendlist, transfer_id);
  566. if (ret)
  567. ath10k_warn("CE send failed: %p\n", nbuf);
  568. return ret;
  569. }
  570. static u16 ath10k_pci_hif_get_free_queue_number(struct ath10k *ar, u8 pipe)
  571. {
  572. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  573. struct hif_ce_pipe_info *pipe_info = &(ar_pci->pipe_info[pipe]);
  574. int ret;
  575. spin_lock_bh(&pipe_info->pipe_lock);
  576. ret = pipe_info->num_sends_allowed;
  577. spin_unlock_bh(&pipe_info->pipe_lock);
  578. return ret;
  579. }
  580. static void ath10k_pci_hif_dump_area(struct ath10k *ar)
  581. {
  582. u32 reg_dump_area = 0;
  583. u32 reg_dump_values[REG_DUMP_COUNT_QCA988X] = {};
  584. u32 host_addr;
  585. int ret;
  586. u32 i;
  587. ath10k_err("firmware crashed!\n");
  588. ath10k_err("hardware name %s version 0x%x\n",
  589. ar->hw_params.name, ar->target_version);
  590. ath10k_err("firmware version: %u.%u.%u.%u\n", ar->fw_version_major,
  591. ar->fw_version_minor, ar->fw_version_release,
  592. ar->fw_version_build);
  593. host_addr = host_interest_item_address(HI_ITEM(hi_failure_state));
  594. if (ath10k_pci_diag_read_mem(ar, host_addr,
  595. &reg_dump_area, sizeof(u32)) != 0) {
  596. ath10k_warn("could not read hi_failure_state\n");
  597. return;
  598. }
  599. ath10k_err("target register Dump Location: 0x%08X\n", reg_dump_area);
  600. ret = ath10k_pci_diag_read_mem(ar, reg_dump_area,
  601. &reg_dump_values[0],
  602. REG_DUMP_COUNT_QCA988X * sizeof(u32));
  603. if (ret != 0) {
  604. ath10k_err("could not dump FW Dump Area\n");
  605. return;
  606. }
  607. BUILD_BUG_ON(REG_DUMP_COUNT_QCA988X % 4);
  608. ath10k_err("target Register Dump\n");
  609. for (i = 0; i < REG_DUMP_COUNT_QCA988X; i += 4)
  610. ath10k_err("[%02d]: 0x%08X 0x%08X 0x%08X 0x%08X\n",
  611. i,
  612. reg_dump_values[i],
  613. reg_dump_values[i + 1],
  614. reg_dump_values[i + 2],
  615. reg_dump_values[i + 3]);
  616. }
  617. static void ath10k_pci_hif_send_complete_check(struct ath10k *ar, u8 pipe,
  618. int force)
  619. {
  620. if (!force) {
  621. int resources;
  622. /*
  623. * Decide whether to actually poll for completions, or just
  624. * wait for a later chance.
  625. * If there seem to be plenty of resources left, then just wait
  626. * since checking involves reading a CE register, which is a
  627. * relatively expensive operation.
  628. */
  629. resources = ath10k_pci_hif_get_free_queue_number(ar, pipe);
  630. /*
  631. * If at least 50% of the total resources are still available,
  632. * don't bother checking again yet.
  633. */
  634. if (resources > (host_ce_config_wlan[pipe].src_nentries >> 1))
  635. return;
  636. }
  637. ath10k_ce_per_engine_service(ar, pipe);
  638. }
  639. static void ath10k_pci_hif_post_init(struct ath10k *ar,
  640. struct ath10k_hif_cb *callbacks)
  641. {
  642. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  643. ath10k_dbg(ATH10K_DBG_PCI, "%s\n", __func__);
  644. memcpy(&ar_pci->msg_callbacks_current, callbacks,
  645. sizeof(ar_pci->msg_callbacks_current));
  646. }
  647. static int ath10k_pci_start_ce(struct ath10k *ar)
  648. {
  649. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  650. struct ce_state *ce_diag = ar_pci->ce_diag;
  651. const struct ce_attr *attr;
  652. struct hif_ce_pipe_info *pipe_info;
  653. struct ath10k_pci_compl *compl;
  654. int i, pipe_num, completions, disable_interrupts;
  655. spin_lock_init(&ar_pci->compl_lock);
  656. INIT_LIST_HEAD(&ar_pci->compl_process);
  657. for (pipe_num = 0; pipe_num < ar_pci->ce_count; pipe_num++) {
  658. pipe_info = &ar_pci->pipe_info[pipe_num];
  659. spin_lock_init(&pipe_info->pipe_lock);
  660. INIT_LIST_HEAD(&pipe_info->compl_free);
  661. /* Handle Diagnostic CE specially */
  662. if (pipe_info->ce_hdl == ce_diag)
  663. continue;
  664. attr = &host_ce_config_wlan[pipe_num];
  665. completions = 0;
  666. if (attr->src_nentries) {
  667. disable_interrupts = attr->flags & CE_ATTR_DIS_INTR;
  668. ath10k_ce_send_cb_register(pipe_info->ce_hdl,
  669. ath10k_pci_ce_send_done,
  670. disable_interrupts);
  671. completions += attr->src_nentries;
  672. pipe_info->num_sends_allowed = attr->src_nentries - 1;
  673. }
  674. if (attr->dest_nentries) {
  675. ath10k_ce_recv_cb_register(pipe_info->ce_hdl,
  676. ath10k_pci_ce_recv_data);
  677. completions += attr->dest_nentries;
  678. }
  679. if (completions == 0)
  680. continue;
  681. for (i = 0; i < completions; i++) {
  682. compl = kmalloc(sizeof(struct ath10k_pci_compl),
  683. GFP_KERNEL);
  684. if (!compl) {
  685. ath10k_warn("No memory for completion state\n");
  686. ath10k_pci_stop_ce(ar);
  687. return -ENOMEM;
  688. }
  689. compl->send_or_recv = HIF_CE_COMPLETE_FREE;
  690. list_add_tail(&compl->list, &pipe_info->compl_free);
  691. }
  692. }
  693. return 0;
  694. }
  695. static void ath10k_pci_stop_ce(struct ath10k *ar)
  696. {
  697. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  698. struct ath10k_pci_compl *compl;
  699. struct sk_buff *skb;
  700. int i;
  701. ath10k_ce_disable_interrupts(ar);
  702. /* Cancel the pending tasklet */
  703. tasklet_kill(&ar_pci->intr_tq);
  704. for (i = 0; i < CE_COUNT; i++)
  705. tasklet_kill(&ar_pci->pipe_info[i].intr);
  706. /* Mark pending completions as aborted, so that upper layers free up
  707. * their associated resources */
  708. spin_lock_bh(&ar_pci->compl_lock);
  709. list_for_each_entry(compl, &ar_pci->compl_process, list) {
  710. skb = (struct sk_buff *)compl->transfer_context;
  711. ATH10K_SKB_CB(skb)->is_aborted = true;
  712. }
  713. spin_unlock_bh(&ar_pci->compl_lock);
  714. }
  715. static void ath10k_pci_cleanup_ce(struct ath10k *ar)
  716. {
  717. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  718. struct ath10k_pci_compl *compl, *tmp;
  719. struct hif_ce_pipe_info *pipe_info;
  720. struct sk_buff *netbuf;
  721. int pipe_num;
  722. /* Free pending completions. */
  723. spin_lock_bh(&ar_pci->compl_lock);
  724. if (!list_empty(&ar_pci->compl_process))
  725. ath10k_warn("pending completions still present! possible memory leaks.\n");
  726. list_for_each_entry_safe(compl, tmp, &ar_pci->compl_process, list) {
  727. list_del(&compl->list);
  728. netbuf = (struct sk_buff *)compl->transfer_context;
  729. dev_kfree_skb_any(netbuf);
  730. kfree(compl);
  731. }
  732. spin_unlock_bh(&ar_pci->compl_lock);
  733. /* Free unused completions for each pipe. */
  734. for (pipe_num = 0; pipe_num < ar_pci->ce_count; pipe_num++) {
  735. pipe_info = &ar_pci->pipe_info[pipe_num];
  736. spin_lock_bh(&pipe_info->pipe_lock);
  737. list_for_each_entry_safe(compl, tmp,
  738. &pipe_info->compl_free, list) {
  739. list_del(&compl->list);
  740. kfree(compl);
  741. }
  742. spin_unlock_bh(&pipe_info->pipe_lock);
  743. }
  744. }
  745. static void ath10k_pci_process_ce(struct ath10k *ar)
  746. {
  747. struct ath10k_pci *ar_pci = ar->hif.priv;
  748. struct ath10k_hif_cb *cb = &ar_pci->msg_callbacks_current;
  749. struct ath10k_pci_compl *compl;
  750. struct sk_buff *skb;
  751. unsigned int nbytes;
  752. int ret, send_done = 0;
  753. /* Upper layers aren't ready to handle tx/rx completions in parallel so
  754. * we must serialize all completion processing. */
  755. spin_lock_bh(&ar_pci->compl_lock);
  756. if (ar_pci->compl_processing) {
  757. spin_unlock_bh(&ar_pci->compl_lock);
  758. return;
  759. }
  760. ar_pci->compl_processing = true;
  761. spin_unlock_bh(&ar_pci->compl_lock);
  762. for (;;) {
  763. spin_lock_bh(&ar_pci->compl_lock);
  764. if (list_empty(&ar_pci->compl_process)) {
  765. spin_unlock_bh(&ar_pci->compl_lock);
  766. break;
  767. }
  768. compl = list_first_entry(&ar_pci->compl_process,
  769. struct ath10k_pci_compl, list);
  770. list_del(&compl->list);
  771. spin_unlock_bh(&ar_pci->compl_lock);
  772. if (compl->send_or_recv == HIF_CE_COMPLETE_SEND) {
  773. cb->tx_completion(ar,
  774. compl->transfer_context,
  775. compl->transfer_id);
  776. send_done = 1;
  777. } else {
  778. ret = ath10k_pci_post_rx_pipe(compl->pipe_info, 1);
  779. if (ret) {
  780. ath10k_warn("Unable to post recv buffer for pipe: %d\n",
  781. compl->pipe_info->pipe_num);
  782. break;
  783. }
  784. skb = (struct sk_buff *)compl->transfer_context;
  785. nbytes = compl->nbytes;
  786. ath10k_dbg(ATH10K_DBG_PCI,
  787. "ath10k_pci_ce_recv_data netbuf=%p nbytes=%d\n",
  788. skb, nbytes);
  789. ath10k_dbg_dump(ATH10K_DBG_PCI_DUMP, NULL,
  790. "ath10k rx: ", skb->data, nbytes);
  791. if (skb->len + skb_tailroom(skb) >= nbytes) {
  792. skb_trim(skb, 0);
  793. skb_put(skb, nbytes);
  794. cb->rx_completion(ar, skb,
  795. compl->pipe_info->pipe_num);
  796. } else {
  797. ath10k_warn("rxed more than expected (nbytes %d, max %d)",
  798. nbytes,
  799. skb->len + skb_tailroom(skb));
  800. }
  801. }
  802. compl->send_or_recv = HIF_CE_COMPLETE_FREE;
  803. /*
  804. * Add completion back to the pipe's free list.
  805. */
  806. spin_lock_bh(&compl->pipe_info->pipe_lock);
  807. list_add_tail(&compl->list, &compl->pipe_info->compl_free);
  808. compl->pipe_info->num_sends_allowed += send_done;
  809. spin_unlock_bh(&compl->pipe_info->pipe_lock);
  810. }
  811. spin_lock_bh(&ar_pci->compl_lock);
  812. ar_pci->compl_processing = false;
  813. spin_unlock_bh(&ar_pci->compl_lock);
  814. }
  815. /* TODO - temporary mapping while we have too few CE's */
  816. static int ath10k_pci_hif_map_service_to_pipe(struct ath10k *ar,
  817. u16 service_id, u8 *ul_pipe,
  818. u8 *dl_pipe, int *ul_is_polled,
  819. int *dl_is_polled)
  820. {
  821. int ret = 0;
  822. /* polling for received messages not supported */
  823. *dl_is_polled = 0;
  824. switch (service_id) {
  825. case ATH10K_HTC_SVC_ID_HTT_DATA_MSG:
  826. /*
  827. * Host->target HTT gets its own pipe, so it can be polled
  828. * while other pipes are interrupt driven.
  829. */
  830. *ul_pipe = 4;
  831. /*
  832. * Use the same target->host pipe for HTC ctrl, HTC raw
  833. * streams, and HTT.
  834. */
  835. *dl_pipe = 1;
  836. break;
  837. case ATH10K_HTC_SVC_ID_RSVD_CTRL:
  838. case ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS:
  839. /*
  840. * Note: HTC_RAW_STREAMS_SVC is currently unused, and
  841. * HTC_CTRL_RSVD_SVC could share the same pipe as the
  842. * WMI services. So, if another CE is needed, change
  843. * this to *ul_pipe = 3, which frees up CE 0.
  844. */
  845. /* *ul_pipe = 3; */
  846. *ul_pipe = 0;
  847. *dl_pipe = 1;
  848. break;
  849. case ATH10K_HTC_SVC_ID_WMI_DATA_BK:
  850. case ATH10K_HTC_SVC_ID_WMI_DATA_BE:
  851. case ATH10K_HTC_SVC_ID_WMI_DATA_VI:
  852. case ATH10K_HTC_SVC_ID_WMI_DATA_VO:
  853. case ATH10K_HTC_SVC_ID_WMI_CONTROL:
  854. *ul_pipe = 3;
  855. *dl_pipe = 2;
  856. break;
  857. /* pipe 5 unused */
  858. /* pipe 6 reserved */
  859. /* pipe 7 reserved */
  860. default:
  861. ret = -1;
  862. break;
  863. }
  864. *ul_is_polled =
  865. (host_ce_config_wlan[*ul_pipe].flags & CE_ATTR_DIS_INTR) != 0;
  866. return ret;
  867. }
  868. static void ath10k_pci_hif_get_default_pipe(struct ath10k *ar,
  869. u8 *ul_pipe, u8 *dl_pipe)
  870. {
  871. int ul_is_polled, dl_is_polled;
  872. (void)ath10k_pci_hif_map_service_to_pipe(ar,
  873. ATH10K_HTC_SVC_ID_RSVD_CTRL,
  874. ul_pipe,
  875. dl_pipe,
  876. &ul_is_polled,
  877. &dl_is_polled);
  878. }
  879. static int ath10k_pci_post_rx_pipe(struct hif_ce_pipe_info *pipe_info,
  880. int num)
  881. {
  882. struct ath10k *ar = pipe_info->hif_ce_state;
  883. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  884. struct ce_state *ce_state = pipe_info->ce_hdl;
  885. struct sk_buff *skb;
  886. dma_addr_t ce_data;
  887. int i, ret = 0;
  888. if (pipe_info->buf_sz == 0)
  889. return 0;
  890. for (i = 0; i < num; i++) {
  891. skb = dev_alloc_skb(pipe_info->buf_sz);
  892. if (!skb) {
  893. ath10k_warn("could not allocate skbuff for pipe %d\n",
  894. num);
  895. ret = -ENOMEM;
  896. goto err;
  897. }
  898. WARN_ONCE((unsigned long)skb->data & 3, "unaligned skb");
  899. ce_data = dma_map_single(ar->dev, skb->data,
  900. skb->len + skb_tailroom(skb),
  901. DMA_FROM_DEVICE);
  902. if (unlikely(dma_mapping_error(ar->dev, ce_data))) {
  903. ath10k_warn("could not dma map skbuff\n");
  904. dev_kfree_skb_any(skb);
  905. ret = -EIO;
  906. goto err;
  907. }
  908. ATH10K_SKB_CB(skb)->paddr = ce_data;
  909. pci_dma_sync_single_for_device(ar_pci->pdev, ce_data,
  910. pipe_info->buf_sz,
  911. PCI_DMA_FROMDEVICE);
  912. ret = ath10k_ce_recv_buf_enqueue(ce_state, (void *)skb,
  913. ce_data);
  914. if (ret) {
  915. ath10k_warn("could not enqueue to pipe %d (%d)\n",
  916. num, ret);
  917. goto err;
  918. }
  919. }
  920. return ret;
  921. err:
  922. ath10k_pci_rx_pipe_cleanup(pipe_info);
  923. return ret;
  924. }
  925. static int ath10k_pci_post_rx(struct ath10k *ar)
  926. {
  927. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  928. struct hif_ce_pipe_info *pipe_info;
  929. const struct ce_attr *attr;
  930. int pipe_num, ret = 0;
  931. for (pipe_num = 0; pipe_num < ar_pci->ce_count; pipe_num++) {
  932. pipe_info = &ar_pci->pipe_info[pipe_num];
  933. attr = &host_ce_config_wlan[pipe_num];
  934. if (attr->dest_nentries == 0)
  935. continue;
  936. ret = ath10k_pci_post_rx_pipe(pipe_info,
  937. attr->dest_nentries - 1);
  938. if (ret) {
  939. ath10k_warn("Unable to replenish recv buffers for pipe: %d\n",
  940. pipe_num);
  941. for (; pipe_num >= 0; pipe_num--) {
  942. pipe_info = &ar_pci->pipe_info[pipe_num];
  943. ath10k_pci_rx_pipe_cleanup(pipe_info);
  944. }
  945. return ret;
  946. }
  947. }
  948. return 0;
  949. }
  950. static int ath10k_pci_hif_start(struct ath10k *ar)
  951. {
  952. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  953. int ret;
  954. ret = ath10k_pci_start_ce(ar);
  955. if (ret) {
  956. ath10k_warn("could not start CE (%d)\n", ret);
  957. return ret;
  958. }
  959. /* Post buffers once to start things off. */
  960. ret = ath10k_pci_post_rx(ar);
  961. if (ret) {
  962. ath10k_warn("could not post rx pipes (%d)\n", ret);
  963. return ret;
  964. }
  965. ar_pci->started = 1;
  966. return 0;
  967. }
  968. static void ath10k_pci_rx_pipe_cleanup(struct hif_ce_pipe_info *pipe_info)
  969. {
  970. struct ath10k *ar;
  971. struct ath10k_pci *ar_pci;
  972. struct ce_state *ce_hdl;
  973. u32 buf_sz;
  974. struct sk_buff *netbuf;
  975. u32 ce_data;
  976. buf_sz = pipe_info->buf_sz;
  977. /* Unused Copy Engine */
  978. if (buf_sz == 0)
  979. return;
  980. ar = pipe_info->hif_ce_state;
  981. ar_pci = ath10k_pci_priv(ar);
  982. if (!ar_pci->started)
  983. return;
  984. ce_hdl = pipe_info->ce_hdl;
  985. while (ath10k_ce_revoke_recv_next(ce_hdl, (void **)&netbuf,
  986. &ce_data) == 0) {
  987. dma_unmap_single(ar->dev, ATH10K_SKB_CB(netbuf)->paddr,
  988. netbuf->len + skb_tailroom(netbuf),
  989. DMA_FROM_DEVICE);
  990. dev_kfree_skb_any(netbuf);
  991. }
  992. }
  993. static void ath10k_pci_tx_pipe_cleanup(struct hif_ce_pipe_info *pipe_info)
  994. {
  995. struct ath10k *ar;
  996. struct ath10k_pci *ar_pci;
  997. struct ce_state *ce_hdl;
  998. struct sk_buff *netbuf;
  999. u32 ce_data;
  1000. unsigned int nbytes;
  1001. unsigned int id;
  1002. u32 buf_sz;
  1003. buf_sz = pipe_info->buf_sz;
  1004. /* Unused Copy Engine */
  1005. if (buf_sz == 0)
  1006. return;
  1007. ar = pipe_info->hif_ce_state;
  1008. ar_pci = ath10k_pci_priv(ar);
  1009. if (!ar_pci->started)
  1010. return;
  1011. ce_hdl = pipe_info->ce_hdl;
  1012. while (ath10k_ce_cancel_send_next(ce_hdl, (void **)&netbuf,
  1013. &ce_data, &nbytes, &id) == 0) {
  1014. if (netbuf != CE_SENDLIST_ITEM_CTXT)
  1015. /*
  1016. * Indicate the completion to higer layer to free
  1017. * the buffer
  1018. */
  1019. ATH10K_SKB_CB(netbuf)->is_aborted = true;
  1020. ar_pci->msg_callbacks_current.tx_completion(ar,
  1021. netbuf,
  1022. id);
  1023. }
  1024. }
  1025. /*
  1026. * Cleanup residual buffers for device shutdown:
  1027. * buffers that were enqueued for receive
  1028. * buffers that were to be sent
  1029. * Note: Buffers that had completed but which were
  1030. * not yet processed are on a completion queue. They
  1031. * are handled when the completion thread shuts down.
  1032. */
  1033. static void ath10k_pci_buffer_cleanup(struct ath10k *ar)
  1034. {
  1035. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1036. int pipe_num;
  1037. for (pipe_num = 0; pipe_num < ar_pci->ce_count; pipe_num++) {
  1038. struct hif_ce_pipe_info *pipe_info;
  1039. pipe_info = &ar_pci->pipe_info[pipe_num];
  1040. ath10k_pci_rx_pipe_cleanup(pipe_info);
  1041. ath10k_pci_tx_pipe_cleanup(pipe_info);
  1042. }
  1043. }
  1044. static void ath10k_pci_ce_deinit(struct ath10k *ar)
  1045. {
  1046. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1047. struct hif_ce_pipe_info *pipe_info;
  1048. int pipe_num;
  1049. for (pipe_num = 0; pipe_num < ar_pci->ce_count; pipe_num++) {
  1050. pipe_info = &ar_pci->pipe_info[pipe_num];
  1051. if (pipe_info->ce_hdl) {
  1052. ath10k_ce_deinit(pipe_info->ce_hdl);
  1053. pipe_info->ce_hdl = NULL;
  1054. pipe_info->buf_sz = 0;
  1055. }
  1056. }
  1057. }
  1058. static void ath10k_pci_hif_stop(struct ath10k *ar)
  1059. {
  1060. ath10k_dbg(ATH10K_DBG_PCI, "%s\n", __func__);
  1061. ath10k_pci_stop_ce(ar);
  1062. /* At this point, asynchronous threads are stopped, the target should
  1063. * not DMA nor interrupt. We process the leftovers and then free
  1064. * everything else up. */
  1065. ath10k_pci_process_ce(ar);
  1066. ath10k_pci_cleanup_ce(ar);
  1067. ath10k_pci_buffer_cleanup(ar);
  1068. ath10k_pci_ce_deinit(ar);
  1069. }
  1070. static int ath10k_pci_hif_exchange_bmi_msg(struct ath10k *ar,
  1071. void *req, u32 req_len,
  1072. void *resp, u32 *resp_len)
  1073. {
  1074. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1075. struct ce_state *ce_tx = ar_pci->pipe_info[BMI_CE_NUM_TO_TARG].ce_hdl;
  1076. struct ce_state *ce_rx = ar_pci->pipe_info[BMI_CE_NUM_TO_HOST].ce_hdl;
  1077. dma_addr_t req_paddr = 0;
  1078. dma_addr_t resp_paddr = 0;
  1079. struct bmi_xfer xfer = {};
  1080. void *treq, *tresp = NULL;
  1081. int ret = 0;
  1082. if (resp && !resp_len)
  1083. return -EINVAL;
  1084. if (resp && resp_len && *resp_len == 0)
  1085. return -EINVAL;
  1086. treq = kmemdup(req, req_len, GFP_KERNEL);
  1087. if (!treq)
  1088. return -ENOMEM;
  1089. req_paddr = dma_map_single(ar->dev, treq, req_len, DMA_TO_DEVICE);
  1090. ret = dma_mapping_error(ar->dev, req_paddr);
  1091. if (ret)
  1092. goto err_dma;
  1093. if (resp && resp_len) {
  1094. tresp = kzalloc(*resp_len, GFP_KERNEL);
  1095. if (!tresp) {
  1096. ret = -ENOMEM;
  1097. goto err_req;
  1098. }
  1099. resp_paddr = dma_map_single(ar->dev, tresp, *resp_len,
  1100. DMA_FROM_DEVICE);
  1101. ret = dma_mapping_error(ar->dev, resp_paddr);
  1102. if (ret)
  1103. goto err_req;
  1104. xfer.wait_for_resp = true;
  1105. xfer.resp_len = 0;
  1106. ath10k_ce_recv_buf_enqueue(ce_rx, &xfer, resp_paddr);
  1107. }
  1108. init_completion(&xfer.done);
  1109. ret = ath10k_ce_send(ce_tx, &xfer, req_paddr, req_len, -1, 0);
  1110. if (ret)
  1111. goto err_resp;
  1112. ret = wait_for_completion_timeout(&xfer.done,
  1113. BMI_COMMUNICATION_TIMEOUT_HZ);
  1114. if (ret <= 0) {
  1115. u32 unused_buffer;
  1116. unsigned int unused_nbytes;
  1117. unsigned int unused_id;
  1118. ret = -ETIMEDOUT;
  1119. ath10k_ce_cancel_send_next(ce_tx, NULL, &unused_buffer,
  1120. &unused_nbytes, &unused_id);
  1121. } else {
  1122. /* non-zero means we did not time out */
  1123. ret = 0;
  1124. }
  1125. err_resp:
  1126. if (resp) {
  1127. u32 unused_buffer;
  1128. ath10k_ce_revoke_recv_next(ce_rx, NULL, &unused_buffer);
  1129. dma_unmap_single(ar->dev, resp_paddr,
  1130. *resp_len, DMA_FROM_DEVICE);
  1131. }
  1132. err_req:
  1133. dma_unmap_single(ar->dev, req_paddr, req_len, DMA_TO_DEVICE);
  1134. if (ret == 0 && resp_len) {
  1135. *resp_len = min(*resp_len, xfer.resp_len);
  1136. memcpy(resp, tresp, xfer.resp_len);
  1137. }
  1138. err_dma:
  1139. kfree(treq);
  1140. kfree(tresp);
  1141. return ret;
  1142. }
  1143. static void ath10k_pci_bmi_send_done(struct ce_state *ce_state,
  1144. void *transfer_context,
  1145. u32 data,
  1146. unsigned int nbytes,
  1147. unsigned int transfer_id)
  1148. {
  1149. struct bmi_xfer *xfer = transfer_context;
  1150. if (xfer->wait_for_resp)
  1151. return;
  1152. complete(&xfer->done);
  1153. }
  1154. static void ath10k_pci_bmi_recv_data(struct ce_state *ce_state,
  1155. void *transfer_context,
  1156. u32 data,
  1157. unsigned int nbytes,
  1158. unsigned int transfer_id,
  1159. unsigned int flags)
  1160. {
  1161. struct bmi_xfer *xfer = transfer_context;
  1162. if (!xfer->wait_for_resp) {
  1163. ath10k_warn("unexpected: BMI data received; ignoring\n");
  1164. return;
  1165. }
  1166. xfer->resp_len = nbytes;
  1167. complete(&xfer->done);
  1168. }
  1169. /*
  1170. * Map from service/endpoint to Copy Engine.
  1171. * This table is derived from the CE_PCI TABLE, above.
  1172. * It is passed to the Target at startup for use by firmware.
  1173. */
  1174. static const struct service_to_pipe target_service_to_ce_map_wlan[] = {
  1175. {
  1176. ATH10K_HTC_SVC_ID_WMI_DATA_VO,
  1177. PIPEDIR_OUT, /* out = UL = host -> target */
  1178. 3,
  1179. },
  1180. {
  1181. ATH10K_HTC_SVC_ID_WMI_DATA_VO,
  1182. PIPEDIR_IN, /* in = DL = target -> host */
  1183. 2,
  1184. },
  1185. {
  1186. ATH10K_HTC_SVC_ID_WMI_DATA_BK,
  1187. PIPEDIR_OUT, /* out = UL = host -> target */
  1188. 3,
  1189. },
  1190. {
  1191. ATH10K_HTC_SVC_ID_WMI_DATA_BK,
  1192. PIPEDIR_IN, /* in = DL = target -> host */
  1193. 2,
  1194. },
  1195. {
  1196. ATH10K_HTC_SVC_ID_WMI_DATA_BE,
  1197. PIPEDIR_OUT, /* out = UL = host -> target */
  1198. 3,
  1199. },
  1200. {
  1201. ATH10K_HTC_SVC_ID_WMI_DATA_BE,
  1202. PIPEDIR_IN, /* in = DL = target -> host */
  1203. 2,
  1204. },
  1205. {
  1206. ATH10K_HTC_SVC_ID_WMI_DATA_VI,
  1207. PIPEDIR_OUT, /* out = UL = host -> target */
  1208. 3,
  1209. },
  1210. {
  1211. ATH10K_HTC_SVC_ID_WMI_DATA_VI,
  1212. PIPEDIR_IN, /* in = DL = target -> host */
  1213. 2,
  1214. },
  1215. {
  1216. ATH10K_HTC_SVC_ID_WMI_CONTROL,
  1217. PIPEDIR_OUT, /* out = UL = host -> target */
  1218. 3,
  1219. },
  1220. {
  1221. ATH10K_HTC_SVC_ID_WMI_CONTROL,
  1222. PIPEDIR_IN, /* in = DL = target -> host */
  1223. 2,
  1224. },
  1225. {
  1226. ATH10K_HTC_SVC_ID_RSVD_CTRL,
  1227. PIPEDIR_OUT, /* out = UL = host -> target */
  1228. 0, /* could be moved to 3 (share with WMI) */
  1229. },
  1230. {
  1231. ATH10K_HTC_SVC_ID_RSVD_CTRL,
  1232. PIPEDIR_IN, /* in = DL = target -> host */
  1233. 1,
  1234. },
  1235. {
  1236. ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS, /* not currently used */
  1237. PIPEDIR_OUT, /* out = UL = host -> target */
  1238. 0,
  1239. },
  1240. {
  1241. ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS, /* not currently used */
  1242. PIPEDIR_IN, /* in = DL = target -> host */
  1243. 1,
  1244. },
  1245. {
  1246. ATH10K_HTC_SVC_ID_HTT_DATA_MSG,
  1247. PIPEDIR_OUT, /* out = UL = host -> target */
  1248. 4,
  1249. },
  1250. {
  1251. ATH10K_HTC_SVC_ID_HTT_DATA_MSG,
  1252. PIPEDIR_IN, /* in = DL = target -> host */
  1253. 1,
  1254. },
  1255. /* (Additions here) */
  1256. { /* Must be last */
  1257. 0,
  1258. 0,
  1259. 0,
  1260. },
  1261. };
  1262. /*
  1263. * Send an interrupt to the device to wake up the Target CPU
  1264. * so it has an opportunity to notice any changed state.
  1265. */
  1266. static int ath10k_pci_wake_target_cpu(struct ath10k *ar)
  1267. {
  1268. int ret;
  1269. u32 core_ctrl;
  1270. ret = ath10k_pci_diag_read_access(ar, SOC_CORE_BASE_ADDRESS |
  1271. CORE_CTRL_ADDRESS,
  1272. &core_ctrl);
  1273. if (ret) {
  1274. ath10k_warn("Unable to read core ctrl\n");
  1275. return ret;
  1276. }
  1277. /* A_INUM_FIRMWARE interrupt to Target CPU */
  1278. core_ctrl |= CORE_CTRL_CPU_INTR_MASK;
  1279. ret = ath10k_pci_diag_write_access(ar, SOC_CORE_BASE_ADDRESS |
  1280. CORE_CTRL_ADDRESS,
  1281. core_ctrl);
  1282. if (ret)
  1283. ath10k_warn("Unable to set interrupt mask\n");
  1284. return ret;
  1285. }
  1286. static int ath10k_pci_init_config(struct ath10k *ar)
  1287. {
  1288. u32 interconnect_targ_addr;
  1289. u32 pcie_state_targ_addr = 0;
  1290. u32 pipe_cfg_targ_addr = 0;
  1291. u32 svc_to_pipe_map = 0;
  1292. u32 pcie_config_flags = 0;
  1293. u32 ealloc_value;
  1294. u32 ealloc_targ_addr;
  1295. u32 flag2_value;
  1296. u32 flag2_targ_addr;
  1297. int ret = 0;
  1298. /* Download to Target the CE Config and the service-to-CE map */
  1299. interconnect_targ_addr =
  1300. host_interest_item_address(HI_ITEM(hi_interconnect_state));
  1301. /* Supply Target-side CE configuration */
  1302. ret = ath10k_pci_diag_read_access(ar, interconnect_targ_addr,
  1303. &pcie_state_targ_addr);
  1304. if (ret != 0) {
  1305. ath10k_err("Failed to get pcie state addr: %d\n", ret);
  1306. return ret;
  1307. }
  1308. if (pcie_state_targ_addr == 0) {
  1309. ret = -EIO;
  1310. ath10k_err("Invalid pcie state addr\n");
  1311. return ret;
  1312. }
  1313. ret = ath10k_pci_diag_read_access(ar, pcie_state_targ_addr +
  1314. offsetof(struct pcie_state,
  1315. pipe_cfg_addr),
  1316. &pipe_cfg_targ_addr);
  1317. if (ret != 0) {
  1318. ath10k_err("Failed to get pipe cfg addr: %d\n", ret);
  1319. return ret;
  1320. }
  1321. if (pipe_cfg_targ_addr == 0) {
  1322. ret = -EIO;
  1323. ath10k_err("Invalid pipe cfg addr\n");
  1324. return ret;
  1325. }
  1326. ret = ath10k_pci_diag_write_mem(ar, pipe_cfg_targ_addr,
  1327. target_ce_config_wlan,
  1328. sizeof(target_ce_config_wlan));
  1329. if (ret != 0) {
  1330. ath10k_err("Failed to write pipe cfg: %d\n", ret);
  1331. return ret;
  1332. }
  1333. ret = ath10k_pci_diag_read_access(ar, pcie_state_targ_addr +
  1334. offsetof(struct pcie_state,
  1335. svc_to_pipe_map),
  1336. &svc_to_pipe_map);
  1337. if (ret != 0) {
  1338. ath10k_err("Failed to get svc/pipe map: %d\n", ret);
  1339. return ret;
  1340. }
  1341. if (svc_to_pipe_map == 0) {
  1342. ret = -EIO;
  1343. ath10k_err("Invalid svc_to_pipe map\n");
  1344. return ret;
  1345. }
  1346. ret = ath10k_pci_diag_write_mem(ar, svc_to_pipe_map,
  1347. target_service_to_ce_map_wlan,
  1348. sizeof(target_service_to_ce_map_wlan));
  1349. if (ret != 0) {
  1350. ath10k_err("Failed to write svc/pipe map: %d\n", ret);
  1351. return ret;
  1352. }
  1353. ret = ath10k_pci_diag_read_access(ar, pcie_state_targ_addr +
  1354. offsetof(struct pcie_state,
  1355. config_flags),
  1356. &pcie_config_flags);
  1357. if (ret != 0) {
  1358. ath10k_err("Failed to get pcie config_flags: %d\n", ret);
  1359. return ret;
  1360. }
  1361. pcie_config_flags &= ~PCIE_CONFIG_FLAG_ENABLE_L1;
  1362. ret = ath10k_pci_diag_write_mem(ar, pcie_state_targ_addr +
  1363. offsetof(struct pcie_state, config_flags),
  1364. &pcie_config_flags,
  1365. sizeof(pcie_config_flags));
  1366. if (ret != 0) {
  1367. ath10k_err("Failed to write pcie config_flags: %d\n", ret);
  1368. return ret;
  1369. }
  1370. /* configure early allocation */
  1371. ealloc_targ_addr = host_interest_item_address(HI_ITEM(hi_early_alloc));
  1372. ret = ath10k_pci_diag_read_access(ar, ealloc_targ_addr, &ealloc_value);
  1373. if (ret != 0) {
  1374. ath10k_err("Faile to get early alloc val: %d\n", ret);
  1375. return ret;
  1376. }
  1377. /* first bank is switched to IRAM */
  1378. ealloc_value |= ((HI_EARLY_ALLOC_MAGIC << HI_EARLY_ALLOC_MAGIC_SHIFT) &
  1379. HI_EARLY_ALLOC_MAGIC_MASK);
  1380. ealloc_value |= ((1 << HI_EARLY_ALLOC_IRAM_BANKS_SHIFT) &
  1381. HI_EARLY_ALLOC_IRAM_BANKS_MASK);
  1382. ret = ath10k_pci_diag_write_access(ar, ealloc_targ_addr, ealloc_value);
  1383. if (ret != 0) {
  1384. ath10k_err("Failed to set early alloc val: %d\n", ret);
  1385. return ret;
  1386. }
  1387. /* Tell Target to proceed with initialization */
  1388. flag2_targ_addr = host_interest_item_address(HI_ITEM(hi_option_flag2));
  1389. ret = ath10k_pci_diag_read_access(ar, flag2_targ_addr, &flag2_value);
  1390. if (ret != 0) {
  1391. ath10k_err("Failed to get option val: %d\n", ret);
  1392. return ret;
  1393. }
  1394. flag2_value |= HI_OPTION_EARLY_CFG_DONE;
  1395. ret = ath10k_pci_diag_write_access(ar, flag2_targ_addr, flag2_value);
  1396. if (ret != 0) {
  1397. ath10k_err("Failed to set option val: %d\n", ret);
  1398. return ret;
  1399. }
  1400. return 0;
  1401. }
  1402. static int ath10k_pci_ce_init(struct ath10k *ar)
  1403. {
  1404. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1405. struct hif_ce_pipe_info *pipe_info;
  1406. const struct ce_attr *attr;
  1407. int pipe_num;
  1408. for (pipe_num = 0; pipe_num < ar_pci->ce_count; pipe_num++) {
  1409. pipe_info = &ar_pci->pipe_info[pipe_num];
  1410. pipe_info->pipe_num = pipe_num;
  1411. pipe_info->hif_ce_state = ar;
  1412. attr = &host_ce_config_wlan[pipe_num];
  1413. pipe_info->ce_hdl = ath10k_ce_init(ar, pipe_num, attr);
  1414. if (pipe_info->ce_hdl == NULL) {
  1415. ath10k_err("Unable to initialize CE for pipe: %d\n",
  1416. pipe_num);
  1417. /* It is safe to call it here. It checks if ce_hdl is
  1418. * valid for each pipe */
  1419. ath10k_pci_ce_deinit(ar);
  1420. return -1;
  1421. }
  1422. if (pipe_num == ar_pci->ce_count - 1) {
  1423. /*
  1424. * Reserve the ultimate CE for
  1425. * diagnostic Window support
  1426. */
  1427. ar_pci->ce_diag =
  1428. ar_pci->pipe_info[ar_pci->ce_count - 1].ce_hdl;
  1429. continue;
  1430. }
  1431. pipe_info->buf_sz = (size_t) (attr->src_sz_max);
  1432. }
  1433. /*
  1434. * Initially, establish CE completion handlers for use with BMI.
  1435. * These are overwritten with generic handlers after we exit BMI phase.
  1436. */
  1437. pipe_info = &ar_pci->pipe_info[BMI_CE_NUM_TO_TARG];
  1438. ath10k_ce_send_cb_register(pipe_info->ce_hdl,
  1439. ath10k_pci_bmi_send_done, 0);
  1440. pipe_info = &ar_pci->pipe_info[BMI_CE_NUM_TO_HOST];
  1441. ath10k_ce_recv_cb_register(pipe_info->ce_hdl,
  1442. ath10k_pci_bmi_recv_data);
  1443. return 0;
  1444. }
  1445. static void ath10k_pci_fw_interrupt_handler(struct ath10k *ar)
  1446. {
  1447. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1448. u32 fw_indicator_address, fw_indicator;
  1449. ath10k_pci_wake(ar);
  1450. fw_indicator_address = ar_pci->fw_indicator_address;
  1451. fw_indicator = ath10k_pci_read32(ar, fw_indicator_address);
  1452. if (fw_indicator & FW_IND_EVENT_PENDING) {
  1453. /* ACK: clear Target-side pending event */
  1454. ath10k_pci_write32(ar, fw_indicator_address,
  1455. fw_indicator & ~FW_IND_EVENT_PENDING);
  1456. if (ar_pci->started) {
  1457. ath10k_pci_hif_dump_area(ar);
  1458. } else {
  1459. /*
  1460. * Probable Target failure before we're prepared
  1461. * to handle it. Generally unexpected.
  1462. */
  1463. ath10k_warn("early firmware event indicated\n");
  1464. }
  1465. }
  1466. ath10k_pci_sleep(ar);
  1467. }
  1468. static const struct ath10k_hif_ops ath10k_pci_hif_ops = {
  1469. .send_head = ath10k_pci_hif_send_head,
  1470. .exchange_bmi_msg = ath10k_pci_hif_exchange_bmi_msg,
  1471. .start = ath10k_pci_hif_start,
  1472. .stop = ath10k_pci_hif_stop,
  1473. .map_service_to_pipe = ath10k_pci_hif_map_service_to_pipe,
  1474. .get_default_pipe = ath10k_pci_hif_get_default_pipe,
  1475. .send_complete_check = ath10k_pci_hif_send_complete_check,
  1476. .init = ath10k_pci_hif_post_init,
  1477. .get_free_queue_number = ath10k_pci_hif_get_free_queue_number,
  1478. };
  1479. static void ath10k_pci_ce_tasklet(unsigned long ptr)
  1480. {
  1481. struct hif_ce_pipe_info *pipe = (struct hif_ce_pipe_info *)ptr;
  1482. struct ath10k_pci *ar_pci = pipe->ar_pci;
  1483. ath10k_ce_per_engine_service(ar_pci->ar, pipe->pipe_num);
  1484. }
  1485. static void ath10k_msi_err_tasklet(unsigned long data)
  1486. {
  1487. struct ath10k *ar = (struct ath10k *)data;
  1488. ath10k_pci_fw_interrupt_handler(ar);
  1489. }
  1490. /*
  1491. * Handler for a per-engine interrupt on a PARTICULAR CE.
  1492. * This is used in cases where each CE has a private MSI interrupt.
  1493. */
  1494. static irqreturn_t ath10k_pci_per_engine_handler(int irq, void *arg)
  1495. {
  1496. struct ath10k *ar = arg;
  1497. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1498. int ce_id = irq - ar_pci->pdev->irq - MSI_ASSIGN_CE_INITIAL;
  1499. if (ce_id < 0 || ce_id >= ARRAY_SIZE(ar_pci->pipe_info)) {
  1500. ath10k_warn("unexpected/invalid irq %d ce_id %d\n", irq, ce_id);
  1501. return IRQ_HANDLED;
  1502. }
  1503. /*
  1504. * NOTE: We are able to derive ce_id from irq because we
  1505. * use a one-to-one mapping for CE's 0..5.
  1506. * CE's 6 & 7 do not use interrupts at all.
  1507. *
  1508. * This mapping must be kept in sync with the mapping
  1509. * used by firmware.
  1510. */
  1511. tasklet_schedule(&ar_pci->pipe_info[ce_id].intr);
  1512. return IRQ_HANDLED;
  1513. }
  1514. static irqreturn_t ath10k_pci_msi_fw_handler(int irq, void *arg)
  1515. {
  1516. struct ath10k *ar = arg;
  1517. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1518. tasklet_schedule(&ar_pci->msi_fw_err);
  1519. return IRQ_HANDLED;
  1520. }
  1521. /*
  1522. * Top-level interrupt handler for all PCI interrupts from a Target.
  1523. * When a block of MSI interrupts is allocated, this top-level handler
  1524. * is not used; instead, we directly call the correct sub-handler.
  1525. */
  1526. static irqreturn_t ath10k_pci_interrupt_handler(int irq, void *arg)
  1527. {
  1528. struct ath10k *ar = arg;
  1529. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1530. if (ar_pci->num_msi_intrs == 0) {
  1531. /*
  1532. * IMPORTANT: INTR_CLR regiser has to be set after
  1533. * INTR_ENABLE is set to 0, otherwise interrupt can not be
  1534. * really cleared.
  1535. */
  1536. iowrite32(0, ar_pci->mem +
  1537. (SOC_CORE_BASE_ADDRESS |
  1538. PCIE_INTR_ENABLE_ADDRESS));
  1539. iowrite32(PCIE_INTR_FIRMWARE_MASK |
  1540. PCIE_INTR_CE_MASK_ALL,
  1541. ar_pci->mem + (SOC_CORE_BASE_ADDRESS |
  1542. PCIE_INTR_CLR_ADDRESS));
  1543. /*
  1544. * IMPORTANT: this extra read transaction is required to
  1545. * flush the posted write buffer.
  1546. */
  1547. (void) ioread32(ar_pci->mem +
  1548. (SOC_CORE_BASE_ADDRESS |
  1549. PCIE_INTR_ENABLE_ADDRESS));
  1550. }
  1551. tasklet_schedule(&ar_pci->intr_tq);
  1552. return IRQ_HANDLED;
  1553. }
  1554. static void ath10k_pci_tasklet(unsigned long data)
  1555. {
  1556. struct ath10k *ar = (struct ath10k *)data;
  1557. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1558. ath10k_pci_fw_interrupt_handler(ar); /* FIXME: Handle FW error */
  1559. ath10k_ce_per_engine_service_any(ar);
  1560. if (ar_pci->num_msi_intrs == 0) {
  1561. /* Enable Legacy PCI line interrupts */
  1562. iowrite32(PCIE_INTR_FIRMWARE_MASK |
  1563. PCIE_INTR_CE_MASK_ALL,
  1564. ar_pci->mem + (SOC_CORE_BASE_ADDRESS |
  1565. PCIE_INTR_ENABLE_ADDRESS));
  1566. /*
  1567. * IMPORTANT: this extra read transaction is required to
  1568. * flush the posted write buffer
  1569. */
  1570. (void) ioread32(ar_pci->mem +
  1571. (SOC_CORE_BASE_ADDRESS |
  1572. PCIE_INTR_ENABLE_ADDRESS));
  1573. }
  1574. }
  1575. static int ath10k_pci_start_intr_msix(struct ath10k *ar, int num)
  1576. {
  1577. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1578. int ret;
  1579. int i;
  1580. ret = pci_enable_msi_block(ar_pci->pdev, num);
  1581. if (ret)
  1582. return ret;
  1583. ret = request_irq(ar_pci->pdev->irq + MSI_ASSIGN_FW,
  1584. ath10k_pci_msi_fw_handler,
  1585. IRQF_SHARED, "ath10k_pci", ar);
  1586. if (ret)
  1587. return ret;
  1588. for (i = MSI_ASSIGN_CE_INITIAL; i <= MSI_ASSIGN_CE_MAX; i++) {
  1589. ret = request_irq(ar_pci->pdev->irq + i,
  1590. ath10k_pci_per_engine_handler,
  1591. IRQF_SHARED, "ath10k_pci", ar);
  1592. if (ret) {
  1593. ath10k_warn("request_irq(%d) failed %d\n",
  1594. ar_pci->pdev->irq + i, ret);
  1595. for (i--; i >= MSI_ASSIGN_CE_INITIAL; i--)
  1596. free_irq(ar_pci->pdev->irq + i, ar);
  1597. free_irq(ar_pci->pdev->irq + MSI_ASSIGN_FW, ar);
  1598. pci_disable_msi(ar_pci->pdev);
  1599. return ret;
  1600. }
  1601. }
  1602. ath10k_info("MSI-X interrupt handling (%d intrs)\n", num);
  1603. return 0;
  1604. }
  1605. static int ath10k_pci_start_intr_msi(struct ath10k *ar)
  1606. {
  1607. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1608. int ret;
  1609. ret = pci_enable_msi(ar_pci->pdev);
  1610. if (ret < 0)
  1611. return ret;
  1612. ret = request_irq(ar_pci->pdev->irq,
  1613. ath10k_pci_interrupt_handler,
  1614. IRQF_SHARED, "ath10k_pci", ar);
  1615. if (ret < 0) {
  1616. pci_disable_msi(ar_pci->pdev);
  1617. return ret;
  1618. }
  1619. ath10k_info("MSI interrupt handling\n");
  1620. return 0;
  1621. }
  1622. static int ath10k_pci_start_intr_legacy(struct ath10k *ar)
  1623. {
  1624. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1625. int ret;
  1626. ret = request_irq(ar_pci->pdev->irq,
  1627. ath10k_pci_interrupt_handler,
  1628. IRQF_SHARED, "ath10k_pci", ar);
  1629. if (ret < 0)
  1630. return ret;
  1631. /*
  1632. * Make sure to wake the Target before enabling Legacy
  1633. * Interrupt.
  1634. */
  1635. iowrite32(PCIE_SOC_WAKE_V_MASK,
  1636. ar_pci->mem + PCIE_LOCAL_BASE_ADDRESS +
  1637. PCIE_SOC_WAKE_ADDRESS);
  1638. ath10k_pci_wait(ar);
  1639. /*
  1640. * A potential race occurs here: The CORE_BASE write
  1641. * depends on target correctly decoding AXI address but
  1642. * host won't know when target writes BAR to CORE_CTRL.
  1643. * This write might get lost if target has NOT written BAR.
  1644. * For now, fix the race by repeating the write in below
  1645. * synchronization checking.
  1646. */
  1647. iowrite32(PCIE_INTR_FIRMWARE_MASK |
  1648. PCIE_INTR_CE_MASK_ALL,
  1649. ar_pci->mem + (SOC_CORE_BASE_ADDRESS |
  1650. PCIE_INTR_ENABLE_ADDRESS));
  1651. iowrite32(PCIE_SOC_WAKE_RESET,
  1652. ar_pci->mem + PCIE_LOCAL_BASE_ADDRESS +
  1653. PCIE_SOC_WAKE_ADDRESS);
  1654. ath10k_info("legacy interrupt handling\n");
  1655. return 0;
  1656. }
  1657. static int ath10k_pci_start_intr(struct ath10k *ar)
  1658. {
  1659. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1660. int num = MSI_NUM_REQUEST;
  1661. int ret;
  1662. int i;
  1663. tasklet_init(&ar_pci->intr_tq, ath10k_pci_tasklet, (unsigned long) ar);
  1664. tasklet_init(&ar_pci->msi_fw_err, ath10k_msi_err_tasklet,
  1665. (unsigned long) ar);
  1666. for (i = 0; i < CE_COUNT; i++) {
  1667. ar_pci->pipe_info[i].ar_pci = ar_pci;
  1668. tasklet_init(&ar_pci->pipe_info[i].intr,
  1669. ath10k_pci_ce_tasklet,
  1670. (unsigned long)&ar_pci->pipe_info[i]);
  1671. }
  1672. if (!test_bit(ATH10K_PCI_FEATURE_MSI_X, ar_pci->features))
  1673. num = 1;
  1674. if (num > 1) {
  1675. ret = ath10k_pci_start_intr_msix(ar, num);
  1676. if (ret == 0)
  1677. goto exit;
  1678. ath10k_warn("MSI-X didn't succeed (%d), trying MSI\n", ret);
  1679. num = 1;
  1680. }
  1681. if (num == 1) {
  1682. ret = ath10k_pci_start_intr_msi(ar);
  1683. if (ret == 0)
  1684. goto exit;
  1685. ath10k_warn("MSI didn't succeed (%d), trying legacy INTR\n",
  1686. ret);
  1687. num = 0;
  1688. }
  1689. ret = ath10k_pci_start_intr_legacy(ar);
  1690. exit:
  1691. ar_pci->num_msi_intrs = num;
  1692. ar_pci->ce_count = CE_COUNT;
  1693. return ret;
  1694. }
  1695. static void ath10k_pci_stop_intr(struct ath10k *ar)
  1696. {
  1697. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1698. int i;
  1699. /* There's at least one interrupt irregardless whether its legacy INTR
  1700. * or MSI or MSI-X */
  1701. for (i = 0; i < max(1, ar_pci->num_msi_intrs); i++)
  1702. free_irq(ar_pci->pdev->irq + i, ar);
  1703. if (ar_pci->num_msi_intrs > 0)
  1704. pci_disable_msi(ar_pci->pdev);
  1705. }
  1706. static int ath10k_pci_reset_target(struct ath10k *ar)
  1707. {
  1708. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1709. int wait_limit = 300; /* 3 sec */
  1710. /* Wait for Target to finish initialization before we proceed. */
  1711. iowrite32(PCIE_SOC_WAKE_V_MASK,
  1712. ar_pci->mem + PCIE_LOCAL_BASE_ADDRESS +
  1713. PCIE_SOC_WAKE_ADDRESS);
  1714. ath10k_pci_wait(ar);
  1715. while (wait_limit-- &&
  1716. !(ioread32(ar_pci->mem + FW_INDICATOR_ADDRESS) &
  1717. FW_IND_INITIALIZED)) {
  1718. if (ar_pci->num_msi_intrs == 0)
  1719. /* Fix potential race by repeating CORE_BASE writes */
  1720. iowrite32(PCIE_INTR_FIRMWARE_MASK |
  1721. PCIE_INTR_CE_MASK_ALL,
  1722. ar_pci->mem + (SOC_CORE_BASE_ADDRESS |
  1723. PCIE_INTR_ENABLE_ADDRESS));
  1724. mdelay(10);
  1725. }
  1726. if (wait_limit < 0) {
  1727. ath10k_err("Target stalled\n");
  1728. iowrite32(PCIE_SOC_WAKE_RESET,
  1729. ar_pci->mem + PCIE_LOCAL_BASE_ADDRESS +
  1730. PCIE_SOC_WAKE_ADDRESS);
  1731. return -EIO;
  1732. }
  1733. iowrite32(PCIE_SOC_WAKE_RESET,
  1734. ar_pci->mem + PCIE_LOCAL_BASE_ADDRESS +
  1735. PCIE_SOC_WAKE_ADDRESS);
  1736. return 0;
  1737. }
  1738. static void ath10k_pci_device_reset(struct ath10k_pci *ar_pci)
  1739. {
  1740. struct ath10k *ar = ar_pci->ar;
  1741. void __iomem *mem = ar_pci->mem;
  1742. int i;
  1743. u32 val;
  1744. if (!SOC_GLOBAL_RESET_ADDRESS)
  1745. return;
  1746. if (!mem)
  1747. return;
  1748. ath10k_pci_reg_write32(mem, PCIE_SOC_WAKE_ADDRESS,
  1749. PCIE_SOC_WAKE_V_MASK);
  1750. for (i = 0; i < ATH_PCI_RESET_WAIT_MAX; i++) {
  1751. if (ath10k_pci_target_is_awake(ar))
  1752. break;
  1753. msleep(1);
  1754. }
  1755. /* Put Target, including PCIe, into RESET. */
  1756. val = ath10k_pci_reg_read32(mem, SOC_GLOBAL_RESET_ADDRESS);
  1757. val |= 1;
  1758. ath10k_pci_reg_write32(mem, SOC_GLOBAL_RESET_ADDRESS, val);
  1759. for (i = 0; i < ATH_PCI_RESET_WAIT_MAX; i++) {
  1760. if (ath10k_pci_reg_read32(mem, RTC_STATE_ADDRESS) &
  1761. RTC_STATE_COLD_RESET_MASK)
  1762. break;
  1763. msleep(1);
  1764. }
  1765. /* Pull Target, including PCIe, out of RESET. */
  1766. val &= ~1;
  1767. ath10k_pci_reg_write32(mem, SOC_GLOBAL_RESET_ADDRESS, val);
  1768. for (i = 0; i < ATH_PCI_RESET_WAIT_MAX; i++) {
  1769. if (!(ath10k_pci_reg_read32(mem, RTC_STATE_ADDRESS) &
  1770. RTC_STATE_COLD_RESET_MASK))
  1771. break;
  1772. msleep(1);
  1773. }
  1774. ath10k_pci_reg_write32(mem, PCIE_SOC_WAKE_ADDRESS, PCIE_SOC_WAKE_RESET);
  1775. }
  1776. static void ath10k_pci_dump_features(struct ath10k_pci *ar_pci)
  1777. {
  1778. int i;
  1779. for (i = 0; i < ATH10K_PCI_FEATURE_COUNT; i++) {
  1780. if (!test_bit(i, ar_pci->features))
  1781. continue;
  1782. switch (i) {
  1783. case ATH10K_PCI_FEATURE_MSI_X:
  1784. ath10k_dbg(ATH10K_DBG_PCI, "device supports MSI-X\n");
  1785. break;
  1786. case ATH10K_PCI_FEATURE_HW_1_0_WARKAROUND:
  1787. ath10k_dbg(ATH10K_DBG_PCI, "QCA988X_1.0 workaround enabled\n");
  1788. break;
  1789. }
  1790. }
  1791. }
  1792. static int ath10k_pci_probe(struct pci_dev *pdev,
  1793. const struct pci_device_id *pci_dev)
  1794. {
  1795. void __iomem *mem;
  1796. int ret = 0;
  1797. struct ath10k *ar;
  1798. struct ath10k_pci *ar_pci;
  1799. u32 lcr_val;
  1800. ath10k_dbg(ATH10K_DBG_PCI, "%s\n", __func__);
  1801. ar_pci = kzalloc(sizeof(*ar_pci), GFP_KERNEL);
  1802. if (ar_pci == NULL)
  1803. return -ENOMEM;
  1804. ar_pci->pdev = pdev;
  1805. ar_pci->dev = &pdev->dev;
  1806. switch (pci_dev->device) {
  1807. case QCA988X_1_0_DEVICE_ID:
  1808. set_bit(ATH10K_PCI_FEATURE_HW_1_0_WARKAROUND, ar_pci->features);
  1809. break;
  1810. case QCA988X_2_0_DEVICE_ID:
  1811. set_bit(ATH10K_PCI_FEATURE_MSI_X, ar_pci->features);
  1812. break;
  1813. default:
  1814. ret = -ENODEV;
  1815. ath10k_err("Unkown device ID: %d\n", pci_dev->device);
  1816. goto err_ar_pci;
  1817. }
  1818. ath10k_pci_dump_features(ar_pci);
  1819. ar = ath10k_core_create(ar_pci, ar_pci->dev, ATH10K_BUS_PCI,
  1820. &ath10k_pci_hif_ops);
  1821. if (!ar) {
  1822. ath10k_err("ath10k_core_create failed!\n");
  1823. ret = -EINVAL;
  1824. goto err_ar_pci;
  1825. }
  1826. /* Enable QCA988X_1.0 HW workarounds */
  1827. if (test_bit(ATH10K_PCI_FEATURE_HW_1_0_WARKAROUND, ar_pci->features))
  1828. spin_lock_init(&ar_pci->hw_v1_workaround_lock);
  1829. ar_pci->ar = ar;
  1830. ar_pci->fw_indicator_address = FW_INDICATOR_ADDRESS;
  1831. atomic_set(&ar_pci->keep_awake_count, 0);
  1832. pci_set_drvdata(pdev, ar);
  1833. /*
  1834. * Without any knowledge of the Host, the Target may have been reset or
  1835. * power cycled and its Config Space may no longer reflect the PCI
  1836. * address space that was assigned earlier by the PCI infrastructure.
  1837. * Refresh it now.
  1838. */
  1839. ret = pci_assign_resource(pdev, BAR_NUM);
  1840. if (ret) {
  1841. ath10k_err("cannot assign PCI space: %d\n", ret);
  1842. goto err_ar;
  1843. }
  1844. ret = pci_enable_device(pdev);
  1845. if (ret) {
  1846. ath10k_err("cannot enable PCI device: %d\n", ret);
  1847. goto err_ar;
  1848. }
  1849. /* Request MMIO resources */
  1850. ret = pci_request_region(pdev, BAR_NUM, "ath");
  1851. if (ret) {
  1852. ath10k_err("PCI MMIO reservation error: %d\n", ret);
  1853. goto err_device;
  1854. }
  1855. /*
  1856. * Target structures have a limit of 32 bit DMA pointers.
  1857. * DMA pointers can be wider than 32 bits by default on some systems.
  1858. */
  1859. ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  1860. if (ret) {
  1861. ath10k_err("32-bit DMA not available: %d\n", ret);
  1862. goto err_region;
  1863. }
  1864. ret = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  1865. if (ret) {
  1866. ath10k_err("cannot enable 32-bit consistent DMA\n");
  1867. goto err_region;
  1868. }
  1869. /* Set bus master bit in PCI_COMMAND to enable DMA */
  1870. pci_set_master(pdev);
  1871. /*
  1872. * Temporary FIX: disable ASPM
  1873. * Will be removed after the OTP is programmed
  1874. */
  1875. pci_read_config_dword(pdev, 0x80, &lcr_val);
  1876. pci_write_config_dword(pdev, 0x80, (lcr_val & 0xffffff00));
  1877. /* Arrange for access to Target SoC registers. */
  1878. mem = pci_iomap(pdev, BAR_NUM, 0);
  1879. if (!mem) {
  1880. ath10k_err("PCI iomap error\n");
  1881. ret = -EIO;
  1882. goto err_master;
  1883. }
  1884. ar_pci->mem = mem;
  1885. spin_lock_init(&ar_pci->ce_lock);
  1886. ar_pci->cacheline_sz = dma_get_cache_alignment();
  1887. ret = ath10k_pci_start_intr(ar);
  1888. if (ret) {
  1889. ath10k_err("could not start interrupt handling (%d)\n", ret);
  1890. goto err_iomap;
  1891. }
  1892. /*
  1893. * Bring the target up cleanly.
  1894. *
  1895. * The target may be in an undefined state with an AUX-powered Target
  1896. * and a Host in WoW mode. If the Host crashes, loses power, or is
  1897. * restarted (without unloading the driver) then the Target is left
  1898. * (aux) powered and running. On a subsequent driver load, the Target
  1899. * is in an unexpected state. We try to catch that here in order to
  1900. * reset the Target and retry the probe.
  1901. */
  1902. ath10k_pci_device_reset(ar_pci);
  1903. ret = ath10k_pci_reset_target(ar);
  1904. if (ret)
  1905. goto err_intr;
  1906. if (ath10k_target_ps) {
  1907. ath10k_dbg(ATH10K_DBG_PCI, "on-chip power save enabled\n");
  1908. } else {
  1909. /* Force AWAKE forever */
  1910. ath10k_dbg(ATH10K_DBG_PCI, "on-chip power save disabled\n");
  1911. ath10k_do_pci_wake(ar);
  1912. }
  1913. ret = ath10k_pci_ce_init(ar);
  1914. if (ret)
  1915. goto err_intr;
  1916. ret = ath10k_pci_init_config(ar);
  1917. if (ret)
  1918. goto err_ce;
  1919. ret = ath10k_pci_wake_target_cpu(ar);
  1920. if (ret) {
  1921. ath10k_err("could not wake up target CPU (%d)\n", ret);
  1922. goto err_ce;
  1923. }
  1924. ret = ath10k_core_register(ar);
  1925. if (ret) {
  1926. ath10k_err("could not register driver core (%d)\n", ret);
  1927. goto err_ce;
  1928. }
  1929. return 0;
  1930. err_ce:
  1931. ath10k_pci_ce_deinit(ar);
  1932. err_intr:
  1933. ath10k_pci_stop_intr(ar);
  1934. err_iomap:
  1935. pci_iounmap(pdev, mem);
  1936. err_master:
  1937. pci_clear_master(pdev);
  1938. err_region:
  1939. pci_release_region(pdev, BAR_NUM);
  1940. err_device:
  1941. pci_disable_device(pdev);
  1942. err_ar:
  1943. pci_set_drvdata(pdev, NULL);
  1944. ath10k_core_destroy(ar);
  1945. err_ar_pci:
  1946. /* call HIF PCI free here */
  1947. kfree(ar_pci);
  1948. return ret;
  1949. }
  1950. static void ath10k_pci_remove(struct pci_dev *pdev)
  1951. {
  1952. struct ath10k *ar = pci_get_drvdata(pdev);
  1953. struct ath10k_pci *ar_pci;
  1954. ath10k_dbg(ATH10K_DBG_PCI, "%s\n", __func__);
  1955. if (!ar)
  1956. return;
  1957. ar_pci = ath10k_pci_priv(ar);
  1958. if (!ar_pci)
  1959. return;
  1960. tasklet_kill(&ar_pci->msi_fw_err);
  1961. ath10k_core_unregister(ar);
  1962. ath10k_pci_stop_intr(ar);
  1963. pci_set_drvdata(pdev, NULL);
  1964. pci_iounmap(pdev, ar_pci->mem);
  1965. pci_release_region(pdev, BAR_NUM);
  1966. pci_clear_master(pdev);
  1967. pci_disable_device(pdev);
  1968. ath10k_core_destroy(ar);
  1969. kfree(ar_pci);
  1970. }
  1971. #if defined(CONFIG_PM_SLEEP)
  1972. #define ATH10K_PCI_PM_CONTROL 0x44
  1973. static int ath10k_pci_suspend(struct device *device)
  1974. {
  1975. struct pci_dev *pdev = to_pci_dev(device);
  1976. struct ath10k *ar = pci_get_drvdata(pdev);
  1977. struct ath10k_pci *ar_pci;
  1978. u32 val;
  1979. int ret, retval;
  1980. ath10k_dbg(ATH10K_DBG_PCI, "%s\n", __func__);
  1981. if (!ar)
  1982. return -ENODEV;
  1983. ar_pci = ath10k_pci_priv(ar);
  1984. if (!ar_pci)
  1985. return -ENODEV;
  1986. if (ath10k_core_target_suspend(ar))
  1987. return -EBUSY;
  1988. ret = wait_event_interruptible_timeout(ar->event_queue,
  1989. ar->is_target_paused == true,
  1990. 1 * HZ);
  1991. if (ret < 0) {
  1992. ath10k_warn("suspend interrupted (%d)\n", ret);
  1993. retval = ret;
  1994. goto resume;
  1995. } else if (ret == 0) {
  1996. ath10k_warn("suspend timed out - target pause event never came\n");
  1997. retval = EIO;
  1998. goto resume;
  1999. }
  2000. /*
  2001. * reset is_target_paused and host can check that in next time,
  2002. * or it will always be TRUE and host just skip the waiting
  2003. * condition, it causes target assert due to host already
  2004. * suspend
  2005. */
  2006. ar->is_target_paused = false;
  2007. pci_read_config_dword(pdev, ATH10K_PCI_PM_CONTROL, &val);
  2008. if ((val & 0x000000ff) != 0x3) {
  2009. pci_save_state(pdev);
  2010. pci_disable_device(pdev);
  2011. pci_write_config_dword(pdev, ATH10K_PCI_PM_CONTROL,
  2012. (val & 0xffffff00) | 0x03);
  2013. }
  2014. return 0;
  2015. resume:
  2016. ret = ath10k_core_target_resume(ar);
  2017. if (ret)
  2018. ath10k_warn("could not resume (%d)\n", ret);
  2019. return retval;
  2020. }
  2021. static int ath10k_pci_resume(struct device *device)
  2022. {
  2023. struct pci_dev *pdev = to_pci_dev(device);
  2024. struct ath10k *ar = pci_get_drvdata(pdev);
  2025. struct ath10k_pci *ar_pci;
  2026. int ret;
  2027. u32 val;
  2028. ath10k_dbg(ATH10K_DBG_PCI, "%s\n", __func__);
  2029. if (!ar)
  2030. return -ENODEV;
  2031. ar_pci = ath10k_pci_priv(ar);
  2032. if (!ar_pci)
  2033. return -ENODEV;
  2034. ret = pci_enable_device(pdev);
  2035. if (ret) {
  2036. ath10k_warn("cannot enable PCI device: %d\n", ret);
  2037. return ret;
  2038. }
  2039. pci_read_config_dword(pdev, ATH10K_PCI_PM_CONTROL, &val);
  2040. if ((val & 0x000000ff) != 0) {
  2041. pci_restore_state(pdev);
  2042. pci_write_config_dword(pdev, ATH10K_PCI_PM_CONTROL,
  2043. val & 0xffffff00);
  2044. /*
  2045. * Suspend/Resume resets the PCI configuration space,
  2046. * so we have to re-disable the RETRY_TIMEOUT register (0x41)
  2047. * to keep PCI Tx retries from interfering with C3 CPU state
  2048. */
  2049. pci_read_config_dword(pdev, 0x40, &val);
  2050. if ((val & 0x0000ff00) != 0)
  2051. pci_write_config_dword(pdev, 0x40, val & 0xffff00ff);
  2052. }
  2053. ret = ath10k_core_target_resume(ar);
  2054. if (ret)
  2055. ath10k_warn("target resume failed: %d\n", ret);
  2056. return ret;
  2057. }
  2058. static SIMPLE_DEV_PM_OPS(ath10k_dev_pm_ops,
  2059. ath10k_pci_suspend,
  2060. ath10k_pci_resume);
  2061. #define ATH10K_PCI_PM_OPS (&ath10k_dev_pm_ops)
  2062. #else
  2063. #define ATH10K_PCI_PM_OPS NULL
  2064. #endif /* CONFIG_PM_SLEEP */
  2065. MODULE_DEVICE_TABLE(pci, ath10k_pci_id_table);
  2066. static struct pci_driver ath10k_pci_driver = {
  2067. .name = "ath10k_pci",
  2068. .id_table = ath10k_pci_id_table,
  2069. .probe = ath10k_pci_probe,
  2070. .remove = ath10k_pci_remove,
  2071. .driver.pm = ATH10K_PCI_PM_OPS,
  2072. };
  2073. static int __init ath10k_pci_init(void)
  2074. {
  2075. int ret;
  2076. ret = pci_register_driver(&ath10k_pci_driver);
  2077. if (ret)
  2078. ath10k_err("pci_register_driver failed [%d]\n", ret);
  2079. return ret;
  2080. }
  2081. module_init(ath10k_pci_init);
  2082. static void __exit ath10k_pci_exit(void)
  2083. {
  2084. pci_unregister_driver(&ath10k_pci_driver);
  2085. }
  2086. module_exit(ath10k_pci_exit);
  2087. MODULE_AUTHOR("Qualcomm Atheros");
  2088. MODULE_DESCRIPTION("Driver support for Atheros QCA988X PCIe devices");
  2089. MODULE_LICENSE("Dual BSD/GPL");
  2090. MODULE_FIRMWARE(QCA988X_HW_1_0_FW_DIR "/" QCA988X_HW_1_0_FW_FILE);
  2091. MODULE_FIRMWARE(QCA988X_HW_1_0_FW_DIR "/" QCA988X_HW_1_0_OTP_FILE);
  2092. MODULE_FIRMWARE(QCA988X_HW_1_0_FW_DIR "/" QCA988X_HW_1_0_BOARD_DATA_FILE);
  2093. MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" QCA988X_HW_2_0_FW_FILE);
  2094. MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" QCA988X_HW_2_0_OTP_FILE);
  2095. MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" QCA988X_HW_2_0_BOARD_DATA_FILE);