pci.c 60 KB

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