pci.c 61 KB

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