pci.c 61 KB

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