pci.c 60 KB

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