pci.c 61 KB

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