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. }
  1069. static int ath10k_pci_hif_exchange_bmi_msg(struct ath10k *ar,
  1070. void *req, u32 req_len,
  1071. void *resp, u32 *resp_len)
  1072. {
  1073. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1074. struct ce_state *ce_tx = ar_pci->pipe_info[BMI_CE_NUM_TO_TARG].ce_hdl;
  1075. struct ce_state *ce_rx = ar_pci->pipe_info[BMI_CE_NUM_TO_HOST].ce_hdl;
  1076. dma_addr_t req_paddr = 0;
  1077. dma_addr_t resp_paddr = 0;
  1078. struct bmi_xfer xfer = {};
  1079. void *treq, *tresp = NULL;
  1080. int ret = 0;
  1081. if (resp && !resp_len)
  1082. return -EINVAL;
  1083. if (resp && resp_len && *resp_len == 0)
  1084. return -EINVAL;
  1085. treq = kmemdup(req, req_len, GFP_KERNEL);
  1086. if (!treq)
  1087. return -ENOMEM;
  1088. req_paddr = dma_map_single(ar->dev, treq, req_len, DMA_TO_DEVICE);
  1089. ret = dma_mapping_error(ar->dev, req_paddr);
  1090. if (ret)
  1091. goto err_dma;
  1092. if (resp && resp_len) {
  1093. tresp = kzalloc(*resp_len, GFP_KERNEL);
  1094. if (!tresp) {
  1095. ret = -ENOMEM;
  1096. goto err_req;
  1097. }
  1098. resp_paddr = dma_map_single(ar->dev, tresp, *resp_len,
  1099. DMA_FROM_DEVICE);
  1100. ret = dma_mapping_error(ar->dev, resp_paddr);
  1101. if (ret)
  1102. goto err_req;
  1103. xfer.wait_for_resp = true;
  1104. xfer.resp_len = 0;
  1105. ath10k_ce_recv_buf_enqueue(ce_rx, &xfer, resp_paddr);
  1106. }
  1107. init_completion(&xfer.done);
  1108. ret = ath10k_ce_send(ce_tx, &xfer, req_paddr, req_len, -1, 0);
  1109. if (ret)
  1110. goto err_resp;
  1111. ret = wait_for_completion_timeout(&xfer.done,
  1112. BMI_COMMUNICATION_TIMEOUT_HZ);
  1113. if (ret <= 0) {
  1114. u32 unused_buffer;
  1115. unsigned int unused_nbytes;
  1116. unsigned int unused_id;
  1117. ret = -ETIMEDOUT;
  1118. ath10k_ce_cancel_send_next(ce_tx, NULL, &unused_buffer,
  1119. &unused_nbytes, &unused_id);
  1120. } else {
  1121. /* non-zero means we did not time out */
  1122. ret = 0;
  1123. }
  1124. err_resp:
  1125. if (resp) {
  1126. u32 unused_buffer;
  1127. ath10k_ce_revoke_recv_next(ce_rx, NULL, &unused_buffer);
  1128. dma_unmap_single(ar->dev, resp_paddr,
  1129. *resp_len, DMA_FROM_DEVICE);
  1130. }
  1131. err_req:
  1132. dma_unmap_single(ar->dev, req_paddr, req_len, DMA_TO_DEVICE);
  1133. if (ret == 0 && resp_len) {
  1134. *resp_len = min(*resp_len, xfer.resp_len);
  1135. memcpy(resp, tresp, xfer.resp_len);
  1136. }
  1137. err_dma:
  1138. kfree(treq);
  1139. kfree(tresp);
  1140. return ret;
  1141. }
  1142. static void ath10k_pci_bmi_send_done(struct ce_state *ce_state,
  1143. void *transfer_context,
  1144. u32 data,
  1145. unsigned int nbytes,
  1146. unsigned int transfer_id)
  1147. {
  1148. struct bmi_xfer *xfer = transfer_context;
  1149. if (xfer->wait_for_resp)
  1150. return;
  1151. complete(&xfer->done);
  1152. }
  1153. static void ath10k_pci_bmi_recv_data(struct ce_state *ce_state,
  1154. void *transfer_context,
  1155. u32 data,
  1156. unsigned int nbytes,
  1157. unsigned int transfer_id,
  1158. unsigned int flags)
  1159. {
  1160. struct bmi_xfer *xfer = transfer_context;
  1161. if (!xfer->wait_for_resp) {
  1162. ath10k_warn("unexpected: BMI data received; ignoring\n");
  1163. return;
  1164. }
  1165. xfer->resp_len = nbytes;
  1166. complete(&xfer->done);
  1167. }
  1168. /*
  1169. * Map from service/endpoint to Copy Engine.
  1170. * This table is derived from the CE_PCI TABLE, above.
  1171. * It is passed to the Target at startup for use by firmware.
  1172. */
  1173. static const struct service_to_pipe target_service_to_ce_map_wlan[] = {
  1174. {
  1175. ATH10K_HTC_SVC_ID_WMI_DATA_VO,
  1176. PIPEDIR_OUT, /* out = UL = host -> target */
  1177. 3,
  1178. },
  1179. {
  1180. ATH10K_HTC_SVC_ID_WMI_DATA_VO,
  1181. PIPEDIR_IN, /* in = DL = target -> host */
  1182. 2,
  1183. },
  1184. {
  1185. ATH10K_HTC_SVC_ID_WMI_DATA_BK,
  1186. PIPEDIR_OUT, /* out = UL = host -> target */
  1187. 3,
  1188. },
  1189. {
  1190. ATH10K_HTC_SVC_ID_WMI_DATA_BK,
  1191. PIPEDIR_IN, /* in = DL = target -> host */
  1192. 2,
  1193. },
  1194. {
  1195. ATH10K_HTC_SVC_ID_WMI_DATA_BE,
  1196. PIPEDIR_OUT, /* out = UL = host -> target */
  1197. 3,
  1198. },
  1199. {
  1200. ATH10K_HTC_SVC_ID_WMI_DATA_BE,
  1201. PIPEDIR_IN, /* in = DL = target -> host */
  1202. 2,
  1203. },
  1204. {
  1205. ATH10K_HTC_SVC_ID_WMI_DATA_VI,
  1206. PIPEDIR_OUT, /* out = UL = host -> target */
  1207. 3,
  1208. },
  1209. {
  1210. ATH10K_HTC_SVC_ID_WMI_DATA_VI,
  1211. PIPEDIR_IN, /* in = DL = target -> host */
  1212. 2,
  1213. },
  1214. {
  1215. ATH10K_HTC_SVC_ID_WMI_CONTROL,
  1216. PIPEDIR_OUT, /* out = UL = host -> target */
  1217. 3,
  1218. },
  1219. {
  1220. ATH10K_HTC_SVC_ID_WMI_CONTROL,
  1221. PIPEDIR_IN, /* in = DL = target -> host */
  1222. 2,
  1223. },
  1224. {
  1225. ATH10K_HTC_SVC_ID_RSVD_CTRL,
  1226. PIPEDIR_OUT, /* out = UL = host -> target */
  1227. 0, /* could be moved to 3 (share with WMI) */
  1228. },
  1229. {
  1230. ATH10K_HTC_SVC_ID_RSVD_CTRL,
  1231. PIPEDIR_IN, /* in = DL = target -> host */
  1232. 1,
  1233. },
  1234. {
  1235. ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS, /* not currently used */
  1236. PIPEDIR_OUT, /* out = UL = host -> target */
  1237. 0,
  1238. },
  1239. {
  1240. ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS, /* not currently used */
  1241. PIPEDIR_IN, /* in = DL = target -> host */
  1242. 1,
  1243. },
  1244. {
  1245. ATH10K_HTC_SVC_ID_HTT_DATA_MSG,
  1246. PIPEDIR_OUT, /* out = UL = host -> target */
  1247. 4,
  1248. },
  1249. {
  1250. ATH10K_HTC_SVC_ID_HTT_DATA_MSG,
  1251. PIPEDIR_IN, /* in = DL = target -> host */
  1252. 1,
  1253. },
  1254. /* (Additions here) */
  1255. { /* Must be last */
  1256. 0,
  1257. 0,
  1258. 0,
  1259. },
  1260. };
  1261. /*
  1262. * Send an interrupt to the device to wake up the Target CPU
  1263. * so it has an opportunity to notice any changed state.
  1264. */
  1265. static int ath10k_pci_wake_target_cpu(struct ath10k *ar)
  1266. {
  1267. int ret;
  1268. u32 core_ctrl;
  1269. ret = ath10k_pci_diag_read_access(ar, SOC_CORE_BASE_ADDRESS |
  1270. CORE_CTRL_ADDRESS,
  1271. &core_ctrl);
  1272. if (ret) {
  1273. ath10k_warn("Unable to read core ctrl\n");
  1274. return ret;
  1275. }
  1276. /* A_INUM_FIRMWARE interrupt to Target CPU */
  1277. core_ctrl |= CORE_CTRL_CPU_INTR_MASK;
  1278. ret = ath10k_pci_diag_write_access(ar, SOC_CORE_BASE_ADDRESS |
  1279. CORE_CTRL_ADDRESS,
  1280. core_ctrl);
  1281. if (ret)
  1282. ath10k_warn("Unable to set interrupt mask\n");
  1283. return ret;
  1284. }
  1285. static int ath10k_pci_init_config(struct ath10k *ar)
  1286. {
  1287. u32 interconnect_targ_addr;
  1288. u32 pcie_state_targ_addr = 0;
  1289. u32 pipe_cfg_targ_addr = 0;
  1290. u32 svc_to_pipe_map = 0;
  1291. u32 pcie_config_flags = 0;
  1292. u32 ealloc_value;
  1293. u32 ealloc_targ_addr;
  1294. u32 flag2_value;
  1295. u32 flag2_targ_addr;
  1296. int ret = 0;
  1297. /* Download to Target the CE Config and the service-to-CE map */
  1298. interconnect_targ_addr =
  1299. host_interest_item_address(HI_ITEM(hi_interconnect_state));
  1300. /* Supply Target-side CE configuration */
  1301. ret = ath10k_pci_diag_read_access(ar, interconnect_targ_addr,
  1302. &pcie_state_targ_addr);
  1303. if (ret != 0) {
  1304. ath10k_err("Failed to get pcie state addr: %d\n", ret);
  1305. return ret;
  1306. }
  1307. if (pcie_state_targ_addr == 0) {
  1308. ret = -EIO;
  1309. ath10k_err("Invalid pcie state addr\n");
  1310. return ret;
  1311. }
  1312. ret = ath10k_pci_diag_read_access(ar, pcie_state_targ_addr +
  1313. offsetof(struct pcie_state,
  1314. pipe_cfg_addr),
  1315. &pipe_cfg_targ_addr);
  1316. if (ret != 0) {
  1317. ath10k_err("Failed to get pipe cfg addr: %d\n", ret);
  1318. return ret;
  1319. }
  1320. if (pipe_cfg_targ_addr == 0) {
  1321. ret = -EIO;
  1322. ath10k_err("Invalid pipe cfg addr\n");
  1323. return ret;
  1324. }
  1325. ret = ath10k_pci_diag_write_mem(ar, pipe_cfg_targ_addr,
  1326. target_ce_config_wlan,
  1327. sizeof(target_ce_config_wlan));
  1328. if (ret != 0) {
  1329. ath10k_err("Failed to write pipe cfg: %d\n", ret);
  1330. return ret;
  1331. }
  1332. ret = ath10k_pci_diag_read_access(ar, pcie_state_targ_addr +
  1333. offsetof(struct pcie_state,
  1334. svc_to_pipe_map),
  1335. &svc_to_pipe_map);
  1336. if (ret != 0) {
  1337. ath10k_err("Failed to get svc/pipe map: %d\n", ret);
  1338. return ret;
  1339. }
  1340. if (svc_to_pipe_map == 0) {
  1341. ret = -EIO;
  1342. ath10k_err("Invalid svc_to_pipe map\n");
  1343. return ret;
  1344. }
  1345. ret = ath10k_pci_diag_write_mem(ar, svc_to_pipe_map,
  1346. target_service_to_ce_map_wlan,
  1347. sizeof(target_service_to_ce_map_wlan));
  1348. if (ret != 0) {
  1349. ath10k_err("Failed to write svc/pipe map: %d\n", ret);
  1350. return ret;
  1351. }
  1352. ret = ath10k_pci_diag_read_access(ar, pcie_state_targ_addr +
  1353. offsetof(struct pcie_state,
  1354. config_flags),
  1355. &pcie_config_flags);
  1356. if (ret != 0) {
  1357. ath10k_err("Failed to get pcie config_flags: %d\n", ret);
  1358. return ret;
  1359. }
  1360. pcie_config_flags &= ~PCIE_CONFIG_FLAG_ENABLE_L1;
  1361. ret = ath10k_pci_diag_write_mem(ar, pcie_state_targ_addr +
  1362. offsetof(struct pcie_state, config_flags),
  1363. &pcie_config_flags,
  1364. sizeof(pcie_config_flags));
  1365. if (ret != 0) {
  1366. ath10k_err("Failed to write pcie config_flags: %d\n", ret);
  1367. return ret;
  1368. }
  1369. /* configure early allocation */
  1370. ealloc_targ_addr = host_interest_item_address(HI_ITEM(hi_early_alloc));
  1371. ret = ath10k_pci_diag_read_access(ar, ealloc_targ_addr, &ealloc_value);
  1372. if (ret != 0) {
  1373. ath10k_err("Faile to get early alloc val: %d\n", ret);
  1374. return ret;
  1375. }
  1376. /* first bank is switched to IRAM */
  1377. ealloc_value |= ((HI_EARLY_ALLOC_MAGIC << HI_EARLY_ALLOC_MAGIC_SHIFT) &
  1378. HI_EARLY_ALLOC_MAGIC_MASK);
  1379. ealloc_value |= ((1 << HI_EARLY_ALLOC_IRAM_BANKS_SHIFT) &
  1380. HI_EARLY_ALLOC_IRAM_BANKS_MASK);
  1381. ret = ath10k_pci_diag_write_access(ar, ealloc_targ_addr, ealloc_value);
  1382. if (ret != 0) {
  1383. ath10k_err("Failed to set early alloc val: %d\n", ret);
  1384. return ret;
  1385. }
  1386. /* Tell Target to proceed with initialization */
  1387. flag2_targ_addr = host_interest_item_address(HI_ITEM(hi_option_flag2));
  1388. ret = ath10k_pci_diag_read_access(ar, flag2_targ_addr, &flag2_value);
  1389. if (ret != 0) {
  1390. ath10k_err("Failed to get option val: %d\n", ret);
  1391. return ret;
  1392. }
  1393. flag2_value |= HI_OPTION_EARLY_CFG_DONE;
  1394. ret = ath10k_pci_diag_write_access(ar, flag2_targ_addr, flag2_value);
  1395. if (ret != 0) {
  1396. ath10k_err("Failed to set option val: %d\n", ret);
  1397. return ret;
  1398. }
  1399. return 0;
  1400. }
  1401. static int ath10k_pci_ce_init(struct ath10k *ar)
  1402. {
  1403. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1404. struct hif_ce_pipe_info *pipe_info;
  1405. const struct ce_attr *attr;
  1406. int pipe_num;
  1407. for (pipe_num = 0; pipe_num < ar_pci->ce_count; pipe_num++) {
  1408. pipe_info = &ar_pci->pipe_info[pipe_num];
  1409. pipe_info->pipe_num = pipe_num;
  1410. pipe_info->hif_ce_state = ar;
  1411. attr = &host_ce_config_wlan[pipe_num];
  1412. pipe_info->ce_hdl = ath10k_ce_init(ar, pipe_num, attr);
  1413. if (pipe_info->ce_hdl == NULL) {
  1414. ath10k_err("Unable to initialize CE for pipe: %d\n",
  1415. pipe_num);
  1416. /* It is safe to call it here. It checks if ce_hdl is
  1417. * valid for each pipe */
  1418. ath10k_pci_ce_deinit(ar);
  1419. return -1;
  1420. }
  1421. if (pipe_num == ar_pci->ce_count - 1) {
  1422. /*
  1423. * Reserve the ultimate CE for
  1424. * diagnostic Window support
  1425. */
  1426. ar_pci->ce_diag =
  1427. ar_pci->pipe_info[ar_pci->ce_count - 1].ce_hdl;
  1428. continue;
  1429. }
  1430. pipe_info->buf_sz = (size_t) (attr->src_sz_max);
  1431. }
  1432. /*
  1433. * Initially, establish CE completion handlers for use with BMI.
  1434. * These are overwritten with generic handlers after we exit BMI phase.
  1435. */
  1436. pipe_info = &ar_pci->pipe_info[BMI_CE_NUM_TO_TARG];
  1437. ath10k_ce_send_cb_register(pipe_info->ce_hdl,
  1438. ath10k_pci_bmi_send_done, 0);
  1439. pipe_info = &ar_pci->pipe_info[BMI_CE_NUM_TO_HOST];
  1440. ath10k_ce_recv_cb_register(pipe_info->ce_hdl,
  1441. ath10k_pci_bmi_recv_data);
  1442. return 0;
  1443. }
  1444. static void ath10k_pci_fw_interrupt_handler(struct ath10k *ar)
  1445. {
  1446. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1447. u32 fw_indicator_address, fw_indicator;
  1448. ath10k_pci_wake(ar);
  1449. fw_indicator_address = ar_pci->fw_indicator_address;
  1450. fw_indicator = ath10k_pci_read32(ar, fw_indicator_address);
  1451. if (fw_indicator & FW_IND_EVENT_PENDING) {
  1452. /* ACK: clear Target-side pending event */
  1453. ath10k_pci_write32(ar, fw_indicator_address,
  1454. fw_indicator & ~FW_IND_EVENT_PENDING);
  1455. if (ar_pci->started) {
  1456. ath10k_pci_hif_dump_area(ar);
  1457. } else {
  1458. /*
  1459. * Probable Target failure before we're prepared
  1460. * to handle it. Generally unexpected.
  1461. */
  1462. ath10k_warn("early firmware event indicated\n");
  1463. }
  1464. }
  1465. ath10k_pci_sleep(ar);
  1466. }
  1467. static const struct ath10k_hif_ops ath10k_pci_hif_ops = {
  1468. .send_head = ath10k_pci_hif_send_head,
  1469. .exchange_bmi_msg = ath10k_pci_hif_exchange_bmi_msg,
  1470. .start = ath10k_pci_hif_start,
  1471. .stop = ath10k_pci_hif_stop,
  1472. .map_service_to_pipe = ath10k_pci_hif_map_service_to_pipe,
  1473. .get_default_pipe = ath10k_pci_hif_get_default_pipe,
  1474. .send_complete_check = ath10k_pci_hif_send_complete_check,
  1475. .init = ath10k_pci_hif_post_init,
  1476. .get_free_queue_number = ath10k_pci_hif_get_free_queue_number,
  1477. };
  1478. static void ath10k_pci_ce_tasklet(unsigned long ptr)
  1479. {
  1480. struct hif_ce_pipe_info *pipe = (struct hif_ce_pipe_info *)ptr;
  1481. struct ath10k_pci *ar_pci = pipe->ar_pci;
  1482. ath10k_ce_per_engine_service(ar_pci->ar, pipe->pipe_num);
  1483. }
  1484. static void ath10k_msi_err_tasklet(unsigned long data)
  1485. {
  1486. struct ath10k *ar = (struct ath10k *)data;
  1487. ath10k_pci_fw_interrupt_handler(ar);
  1488. }
  1489. /*
  1490. * Handler for a per-engine interrupt on a PARTICULAR CE.
  1491. * This is used in cases where each CE has a private MSI interrupt.
  1492. */
  1493. static irqreturn_t ath10k_pci_per_engine_handler(int irq, void *arg)
  1494. {
  1495. struct ath10k *ar = arg;
  1496. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1497. int ce_id = irq - ar_pci->pdev->irq - MSI_ASSIGN_CE_INITIAL;
  1498. if (ce_id < 0 || ce_id >= ARRAY_SIZE(ar_pci->pipe_info)) {
  1499. ath10k_warn("unexpected/invalid irq %d ce_id %d\n", irq, ce_id);
  1500. return IRQ_HANDLED;
  1501. }
  1502. /*
  1503. * NOTE: We are able to derive ce_id from irq because we
  1504. * use a one-to-one mapping for CE's 0..5.
  1505. * CE's 6 & 7 do not use interrupts at all.
  1506. *
  1507. * This mapping must be kept in sync with the mapping
  1508. * used by firmware.
  1509. */
  1510. tasklet_schedule(&ar_pci->pipe_info[ce_id].intr);
  1511. return IRQ_HANDLED;
  1512. }
  1513. static irqreturn_t ath10k_pci_msi_fw_handler(int irq, void *arg)
  1514. {
  1515. struct ath10k *ar = arg;
  1516. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1517. tasklet_schedule(&ar_pci->msi_fw_err);
  1518. return IRQ_HANDLED;
  1519. }
  1520. /*
  1521. * Top-level interrupt handler for all PCI interrupts from a Target.
  1522. * When a block of MSI interrupts is allocated, this top-level handler
  1523. * is not used; instead, we directly call the correct sub-handler.
  1524. */
  1525. static irqreturn_t ath10k_pci_interrupt_handler(int irq, void *arg)
  1526. {
  1527. struct ath10k *ar = arg;
  1528. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1529. if (ar_pci->num_msi_intrs == 0) {
  1530. /*
  1531. * IMPORTANT: INTR_CLR regiser has to be set after
  1532. * INTR_ENABLE is set to 0, otherwise interrupt can not be
  1533. * really cleared.
  1534. */
  1535. iowrite32(0, ar_pci->mem +
  1536. (SOC_CORE_BASE_ADDRESS |
  1537. PCIE_INTR_ENABLE_ADDRESS));
  1538. iowrite32(PCIE_INTR_FIRMWARE_MASK |
  1539. PCIE_INTR_CE_MASK_ALL,
  1540. ar_pci->mem + (SOC_CORE_BASE_ADDRESS |
  1541. PCIE_INTR_CLR_ADDRESS));
  1542. /*
  1543. * IMPORTANT: this extra read transaction is required to
  1544. * flush the posted write buffer.
  1545. */
  1546. (void) ioread32(ar_pci->mem +
  1547. (SOC_CORE_BASE_ADDRESS |
  1548. PCIE_INTR_ENABLE_ADDRESS));
  1549. }
  1550. tasklet_schedule(&ar_pci->intr_tq);
  1551. return IRQ_HANDLED;
  1552. }
  1553. static void ath10k_pci_tasklet(unsigned long data)
  1554. {
  1555. struct ath10k *ar = (struct ath10k *)data;
  1556. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1557. ath10k_pci_fw_interrupt_handler(ar); /* FIXME: Handle FW error */
  1558. ath10k_ce_per_engine_service_any(ar);
  1559. if (ar_pci->num_msi_intrs == 0) {
  1560. /* Enable Legacy PCI line interrupts */
  1561. iowrite32(PCIE_INTR_FIRMWARE_MASK |
  1562. PCIE_INTR_CE_MASK_ALL,
  1563. ar_pci->mem + (SOC_CORE_BASE_ADDRESS |
  1564. PCIE_INTR_ENABLE_ADDRESS));
  1565. /*
  1566. * IMPORTANT: this extra read transaction is required to
  1567. * flush the posted write buffer
  1568. */
  1569. (void) ioread32(ar_pci->mem +
  1570. (SOC_CORE_BASE_ADDRESS |
  1571. PCIE_INTR_ENABLE_ADDRESS));
  1572. }
  1573. }
  1574. static int ath10k_pci_start_intr_msix(struct ath10k *ar, int num)
  1575. {
  1576. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1577. int ret;
  1578. int i;
  1579. ret = pci_enable_msi_block(ar_pci->pdev, num);
  1580. if (ret)
  1581. return ret;
  1582. ret = request_irq(ar_pci->pdev->irq + MSI_ASSIGN_FW,
  1583. ath10k_pci_msi_fw_handler,
  1584. IRQF_SHARED, "ath10k_pci", ar);
  1585. if (ret)
  1586. return ret;
  1587. for (i = MSI_ASSIGN_CE_INITIAL; i <= MSI_ASSIGN_CE_MAX; i++) {
  1588. ret = request_irq(ar_pci->pdev->irq + i,
  1589. ath10k_pci_per_engine_handler,
  1590. IRQF_SHARED, "ath10k_pci", ar);
  1591. if (ret) {
  1592. ath10k_warn("request_irq(%d) failed %d\n",
  1593. ar_pci->pdev->irq + i, ret);
  1594. for (i--; i >= MSI_ASSIGN_CE_INITIAL; i--)
  1595. free_irq(ar_pci->pdev->irq + i, ar);
  1596. free_irq(ar_pci->pdev->irq + MSI_ASSIGN_FW, ar);
  1597. pci_disable_msi(ar_pci->pdev);
  1598. return ret;
  1599. }
  1600. }
  1601. ath10k_info("MSI-X interrupt handling (%d intrs)\n", num);
  1602. return 0;
  1603. }
  1604. static int ath10k_pci_start_intr_msi(struct ath10k *ar)
  1605. {
  1606. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1607. int ret;
  1608. ret = pci_enable_msi(ar_pci->pdev);
  1609. if (ret < 0)
  1610. return ret;
  1611. ret = request_irq(ar_pci->pdev->irq,
  1612. ath10k_pci_interrupt_handler,
  1613. IRQF_SHARED, "ath10k_pci", ar);
  1614. if (ret < 0) {
  1615. pci_disable_msi(ar_pci->pdev);
  1616. return ret;
  1617. }
  1618. ath10k_info("MSI interrupt handling\n");
  1619. return 0;
  1620. }
  1621. static int ath10k_pci_start_intr_legacy(struct ath10k *ar)
  1622. {
  1623. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1624. int ret;
  1625. ret = request_irq(ar_pci->pdev->irq,
  1626. ath10k_pci_interrupt_handler,
  1627. IRQF_SHARED, "ath10k_pci", ar);
  1628. if (ret < 0)
  1629. return ret;
  1630. /*
  1631. * Make sure to wake the Target before enabling Legacy
  1632. * Interrupt.
  1633. */
  1634. iowrite32(PCIE_SOC_WAKE_V_MASK,
  1635. ar_pci->mem + PCIE_LOCAL_BASE_ADDRESS +
  1636. PCIE_SOC_WAKE_ADDRESS);
  1637. ath10k_pci_wait(ar);
  1638. /*
  1639. * A potential race occurs here: The CORE_BASE write
  1640. * depends on target correctly decoding AXI address but
  1641. * host won't know when target writes BAR to CORE_CTRL.
  1642. * This write might get lost if target has NOT written BAR.
  1643. * For now, fix the race by repeating the write in below
  1644. * synchronization checking.
  1645. */
  1646. iowrite32(PCIE_INTR_FIRMWARE_MASK |
  1647. PCIE_INTR_CE_MASK_ALL,
  1648. ar_pci->mem + (SOC_CORE_BASE_ADDRESS |
  1649. PCIE_INTR_ENABLE_ADDRESS));
  1650. iowrite32(PCIE_SOC_WAKE_RESET,
  1651. ar_pci->mem + PCIE_LOCAL_BASE_ADDRESS +
  1652. PCIE_SOC_WAKE_ADDRESS);
  1653. ath10k_info("legacy interrupt handling\n");
  1654. return 0;
  1655. }
  1656. static int ath10k_pci_start_intr(struct ath10k *ar)
  1657. {
  1658. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1659. int num = MSI_NUM_REQUEST;
  1660. int ret;
  1661. int i;
  1662. tasklet_init(&ar_pci->intr_tq, ath10k_pci_tasklet, (unsigned long) ar);
  1663. tasklet_init(&ar_pci->msi_fw_err, ath10k_msi_err_tasklet,
  1664. (unsigned long) ar);
  1665. for (i = 0; i < CE_COUNT; i++) {
  1666. ar_pci->pipe_info[i].ar_pci = ar_pci;
  1667. tasklet_init(&ar_pci->pipe_info[i].intr,
  1668. ath10k_pci_ce_tasklet,
  1669. (unsigned long)&ar_pci->pipe_info[i]);
  1670. }
  1671. if (!test_bit(ATH10K_PCI_FEATURE_MSI_X, ar_pci->features))
  1672. num = 1;
  1673. if (num > 1) {
  1674. ret = ath10k_pci_start_intr_msix(ar, num);
  1675. if (ret == 0)
  1676. goto exit;
  1677. ath10k_warn("MSI-X didn't succeed (%d), trying MSI\n", ret);
  1678. num = 1;
  1679. }
  1680. if (num == 1) {
  1681. ret = ath10k_pci_start_intr_msi(ar);
  1682. if (ret == 0)
  1683. goto exit;
  1684. ath10k_warn("MSI didn't succeed (%d), trying legacy INTR\n",
  1685. ret);
  1686. num = 0;
  1687. }
  1688. ret = ath10k_pci_start_intr_legacy(ar);
  1689. exit:
  1690. ar_pci->num_msi_intrs = num;
  1691. ar_pci->ce_count = CE_COUNT;
  1692. return ret;
  1693. }
  1694. static void ath10k_pci_stop_intr(struct ath10k *ar)
  1695. {
  1696. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1697. int i;
  1698. /* There's at least one interrupt irregardless whether its legacy INTR
  1699. * or MSI or MSI-X */
  1700. for (i = 0; i < max(1, ar_pci->num_msi_intrs); i++)
  1701. free_irq(ar_pci->pdev->irq + i, ar);
  1702. if (ar_pci->num_msi_intrs > 0)
  1703. pci_disable_msi(ar_pci->pdev);
  1704. }
  1705. static int ath10k_pci_reset_target(struct ath10k *ar)
  1706. {
  1707. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1708. int wait_limit = 300; /* 3 sec */
  1709. /* Wait for Target to finish initialization before we proceed. */
  1710. iowrite32(PCIE_SOC_WAKE_V_MASK,
  1711. ar_pci->mem + PCIE_LOCAL_BASE_ADDRESS +
  1712. PCIE_SOC_WAKE_ADDRESS);
  1713. ath10k_pci_wait(ar);
  1714. while (wait_limit-- &&
  1715. !(ioread32(ar_pci->mem + FW_INDICATOR_ADDRESS) &
  1716. FW_IND_INITIALIZED)) {
  1717. if (ar_pci->num_msi_intrs == 0)
  1718. /* Fix potential race by repeating CORE_BASE writes */
  1719. iowrite32(PCIE_INTR_FIRMWARE_MASK |
  1720. PCIE_INTR_CE_MASK_ALL,
  1721. ar_pci->mem + (SOC_CORE_BASE_ADDRESS |
  1722. PCIE_INTR_ENABLE_ADDRESS));
  1723. mdelay(10);
  1724. }
  1725. if (wait_limit < 0) {
  1726. ath10k_err("Target stalled\n");
  1727. iowrite32(PCIE_SOC_WAKE_RESET,
  1728. ar_pci->mem + PCIE_LOCAL_BASE_ADDRESS +
  1729. PCIE_SOC_WAKE_ADDRESS);
  1730. return -EIO;
  1731. }
  1732. iowrite32(PCIE_SOC_WAKE_RESET,
  1733. ar_pci->mem + PCIE_LOCAL_BASE_ADDRESS +
  1734. PCIE_SOC_WAKE_ADDRESS);
  1735. return 0;
  1736. }
  1737. static void ath10k_pci_device_reset(struct ath10k_pci *ar_pci)
  1738. {
  1739. struct ath10k *ar = ar_pci->ar;
  1740. void __iomem *mem = ar_pci->mem;
  1741. int i;
  1742. u32 val;
  1743. if (!SOC_GLOBAL_RESET_ADDRESS)
  1744. return;
  1745. if (!mem)
  1746. return;
  1747. ath10k_pci_reg_write32(mem, PCIE_SOC_WAKE_ADDRESS,
  1748. PCIE_SOC_WAKE_V_MASK);
  1749. for (i = 0; i < ATH_PCI_RESET_WAIT_MAX; i++) {
  1750. if (ath10k_pci_target_is_awake(ar))
  1751. break;
  1752. msleep(1);
  1753. }
  1754. /* Put Target, including PCIe, into RESET. */
  1755. val = ath10k_pci_reg_read32(mem, SOC_GLOBAL_RESET_ADDRESS);
  1756. val |= 1;
  1757. ath10k_pci_reg_write32(mem, SOC_GLOBAL_RESET_ADDRESS, val);
  1758. for (i = 0; i < ATH_PCI_RESET_WAIT_MAX; i++) {
  1759. if (ath10k_pci_reg_read32(mem, RTC_STATE_ADDRESS) &
  1760. RTC_STATE_COLD_RESET_MASK)
  1761. break;
  1762. msleep(1);
  1763. }
  1764. /* Pull Target, including PCIe, out of RESET. */
  1765. val &= ~1;
  1766. ath10k_pci_reg_write32(mem, SOC_GLOBAL_RESET_ADDRESS, val);
  1767. for (i = 0; i < ATH_PCI_RESET_WAIT_MAX; i++) {
  1768. if (!(ath10k_pci_reg_read32(mem, RTC_STATE_ADDRESS) &
  1769. RTC_STATE_COLD_RESET_MASK))
  1770. break;
  1771. msleep(1);
  1772. }
  1773. ath10k_pci_reg_write32(mem, PCIE_SOC_WAKE_ADDRESS, PCIE_SOC_WAKE_RESET);
  1774. }
  1775. static void ath10k_pci_dump_features(struct ath10k_pci *ar_pci)
  1776. {
  1777. int i;
  1778. for (i = 0; i < ATH10K_PCI_FEATURE_COUNT; i++) {
  1779. if (!test_bit(i, ar_pci->features))
  1780. continue;
  1781. switch (i) {
  1782. case ATH10K_PCI_FEATURE_MSI_X:
  1783. ath10k_dbg(ATH10K_DBG_PCI, "device supports MSI-X\n");
  1784. break;
  1785. case ATH10K_PCI_FEATURE_HW_1_0_WARKAROUND:
  1786. ath10k_dbg(ATH10K_DBG_PCI, "QCA988X_1.0 workaround enabled\n");
  1787. break;
  1788. }
  1789. }
  1790. }
  1791. static int ath10k_pci_probe(struct pci_dev *pdev,
  1792. const struct pci_device_id *pci_dev)
  1793. {
  1794. void __iomem *mem;
  1795. int ret = 0;
  1796. struct ath10k *ar;
  1797. struct ath10k_pci *ar_pci;
  1798. u32 lcr_val;
  1799. ath10k_dbg(ATH10K_DBG_PCI, "%s\n", __func__);
  1800. ar_pci = kzalloc(sizeof(*ar_pci), GFP_KERNEL);
  1801. if (ar_pci == NULL)
  1802. return -ENOMEM;
  1803. ar_pci->pdev = pdev;
  1804. ar_pci->dev = &pdev->dev;
  1805. switch (pci_dev->device) {
  1806. case QCA988X_1_0_DEVICE_ID:
  1807. set_bit(ATH10K_PCI_FEATURE_HW_1_0_WARKAROUND, ar_pci->features);
  1808. break;
  1809. case QCA988X_2_0_DEVICE_ID:
  1810. set_bit(ATH10K_PCI_FEATURE_MSI_X, ar_pci->features);
  1811. break;
  1812. default:
  1813. ret = -ENODEV;
  1814. ath10k_err("Unkown device ID: %d\n", pci_dev->device);
  1815. goto err_ar_pci;
  1816. }
  1817. ath10k_pci_dump_features(ar_pci);
  1818. ar = ath10k_core_create(ar_pci, ar_pci->dev, ATH10K_BUS_PCI,
  1819. &ath10k_pci_hif_ops);
  1820. if (!ar) {
  1821. ath10k_err("ath10k_core_create failed!\n");
  1822. ret = -EINVAL;
  1823. goto err_ar_pci;
  1824. }
  1825. /* Enable QCA988X_1.0 HW workarounds */
  1826. if (test_bit(ATH10K_PCI_FEATURE_HW_1_0_WARKAROUND, ar_pci->features))
  1827. spin_lock_init(&ar_pci->hw_v1_workaround_lock);
  1828. ar_pci->ar = ar;
  1829. ar_pci->fw_indicator_address = FW_INDICATOR_ADDRESS;
  1830. atomic_set(&ar_pci->keep_awake_count, 0);
  1831. pci_set_drvdata(pdev, ar);
  1832. /*
  1833. * Without any knowledge of the Host, the Target may have been reset or
  1834. * power cycled and its Config Space may no longer reflect the PCI
  1835. * address space that was assigned earlier by the PCI infrastructure.
  1836. * Refresh it now.
  1837. */
  1838. ret = pci_assign_resource(pdev, BAR_NUM);
  1839. if (ret) {
  1840. ath10k_err("cannot assign PCI space: %d\n", ret);
  1841. goto err_ar;
  1842. }
  1843. ret = pci_enable_device(pdev);
  1844. if (ret) {
  1845. ath10k_err("cannot enable PCI device: %d\n", ret);
  1846. goto err_ar;
  1847. }
  1848. /* Request MMIO resources */
  1849. ret = pci_request_region(pdev, BAR_NUM, "ath");
  1850. if (ret) {
  1851. ath10k_err("PCI MMIO reservation error: %d\n", ret);
  1852. goto err_device;
  1853. }
  1854. /*
  1855. * Target structures have a limit of 32 bit DMA pointers.
  1856. * DMA pointers can be wider than 32 bits by default on some systems.
  1857. */
  1858. ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  1859. if (ret) {
  1860. ath10k_err("32-bit DMA not available: %d\n", ret);
  1861. goto err_region;
  1862. }
  1863. ret = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  1864. if (ret) {
  1865. ath10k_err("cannot enable 32-bit consistent DMA\n");
  1866. goto err_region;
  1867. }
  1868. /* Set bus master bit in PCI_COMMAND to enable DMA */
  1869. pci_set_master(pdev);
  1870. /*
  1871. * Temporary FIX: disable ASPM
  1872. * Will be removed after the OTP is programmed
  1873. */
  1874. pci_read_config_dword(pdev, 0x80, &lcr_val);
  1875. pci_write_config_dword(pdev, 0x80, (lcr_val & 0xffffff00));
  1876. /* Arrange for access to Target SoC registers. */
  1877. mem = pci_iomap(pdev, BAR_NUM, 0);
  1878. if (!mem) {
  1879. ath10k_err("PCI iomap error\n");
  1880. ret = -EIO;
  1881. goto err_master;
  1882. }
  1883. ar_pci->mem = mem;
  1884. spin_lock_init(&ar_pci->ce_lock);
  1885. ar_pci->cacheline_sz = dma_get_cache_alignment();
  1886. ret = ath10k_pci_start_intr(ar);
  1887. if (ret) {
  1888. ath10k_err("could not start interrupt handling (%d)\n", ret);
  1889. goto err_iomap;
  1890. }
  1891. /*
  1892. * Bring the target up cleanly.
  1893. *
  1894. * The target may be in an undefined state with an AUX-powered Target
  1895. * and a Host in WoW mode. If the Host crashes, loses power, or is
  1896. * restarted (without unloading the driver) then the Target is left
  1897. * (aux) powered and running. On a subsequent driver load, the Target
  1898. * is in an unexpected state. We try to catch that here in order to
  1899. * reset the Target and retry the probe.
  1900. */
  1901. ath10k_pci_device_reset(ar_pci);
  1902. ret = ath10k_pci_reset_target(ar);
  1903. if (ret)
  1904. goto err_intr;
  1905. if (ath10k_target_ps) {
  1906. ath10k_dbg(ATH10K_DBG_PCI, "on-chip power save enabled\n");
  1907. } else {
  1908. /* Force AWAKE forever */
  1909. ath10k_dbg(ATH10K_DBG_PCI, "on-chip power save disabled\n");
  1910. ath10k_do_pci_wake(ar);
  1911. }
  1912. ret = ath10k_pci_ce_init(ar);
  1913. if (ret)
  1914. goto err_intr;
  1915. ret = ath10k_pci_init_config(ar);
  1916. if (ret)
  1917. goto err_ce;
  1918. ret = ath10k_pci_wake_target_cpu(ar);
  1919. if (ret) {
  1920. ath10k_err("could not wake up target CPU (%d)\n", ret);
  1921. goto err_ce;
  1922. }
  1923. ret = ath10k_core_register(ar);
  1924. if (ret) {
  1925. ath10k_err("could not register driver core (%d)\n", ret);
  1926. goto err_ce;
  1927. }
  1928. return 0;
  1929. err_ce:
  1930. ath10k_pci_ce_deinit(ar);
  1931. err_intr:
  1932. ath10k_pci_stop_intr(ar);
  1933. err_iomap:
  1934. pci_iounmap(pdev, mem);
  1935. err_master:
  1936. pci_clear_master(pdev);
  1937. err_region:
  1938. pci_release_region(pdev, BAR_NUM);
  1939. err_device:
  1940. pci_disable_device(pdev);
  1941. err_ar:
  1942. pci_set_drvdata(pdev, NULL);
  1943. ath10k_core_destroy(ar);
  1944. err_ar_pci:
  1945. /* call HIF PCI free here */
  1946. kfree(ar_pci);
  1947. return ret;
  1948. }
  1949. static void ath10k_pci_remove(struct pci_dev *pdev)
  1950. {
  1951. struct ath10k *ar = pci_get_drvdata(pdev);
  1952. struct ath10k_pci *ar_pci;
  1953. ath10k_dbg(ATH10K_DBG_PCI, "%s\n", __func__);
  1954. if (!ar)
  1955. return;
  1956. ar_pci = ath10k_pci_priv(ar);
  1957. if (!ar_pci)
  1958. return;
  1959. tasklet_kill(&ar_pci->msi_fw_err);
  1960. ath10k_core_unregister(ar);
  1961. ath10k_pci_ce_deinit(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);