pci.c 62 KB

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