pci.c 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461
  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. return ret;
  1684. for (i = MSI_ASSIGN_CE_INITIAL; i <= MSI_ASSIGN_CE_MAX; i++) {
  1685. ret = request_irq(ar_pci->pdev->irq + i,
  1686. ath10k_pci_per_engine_handler,
  1687. IRQF_SHARED, "ath10k_pci", ar);
  1688. if (ret) {
  1689. ath10k_warn("request_irq(%d) failed %d\n",
  1690. ar_pci->pdev->irq + i, ret);
  1691. for (i--; i >= MSI_ASSIGN_CE_INITIAL; i--)
  1692. free_irq(ar_pci->pdev->irq + i, ar);
  1693. free_irq(ar_pci->pdev->irq + MSI_ASSIGN_FW, ar);
  1694. pci_disable_msi(ar_pci->pdev);
  1695. return ret;
  1696. }
  1697. }
  1698. ath10k_info("MSI-X interrupt handling (%d intrs)\n", num);
  1699. return 0;
  1700. }
  1701. static int ath10k_pci_start_intr_msi(struct ath10k *ar)
  1702. {
  1703. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1704. int ret;
  1705. ret = pci_enable_msi(ar_pci->pdev);
  1706. if (ret < 0)
  1707. return ret;
  1708. ret = request_irq(ar_pci->pdev->irq,
  1709. ath10k_pci_interrupt_handler,
  1710. IRQF_SHARED, "ath10k_pci", ar);
  1711. if (ret < 0) {
  1712. pci_disable_msi(ar_pci->pdev);
  1713. return ret;
  1714. }
  1715. ath10k_info("MSI interrupt handling\n");
  1716. return 0;
  1717. }
  1718. static int ath10k_pci_start_intr_legacy(struct ath10k *ar)
  1719. {
  1720. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1721. int ret;
  1722. ret = request_irq(ar_pci->pdev->irq,
  1723. ath10k_pci_interrupt_handler,
  1724. IRQF_SHARED, "ath10k_pci", ar);
  1725. if (ret < 0)
  1726. return ret;
  1727. /*
  1728. * Make sure to wake the Target before enabling Legacy
  1729. * Interrupt.
  1730. */
  1731. iowrite32(PCIE_SOC_WAKE_V_MASK,
  1732. ar_pci->mem + PCIE_LOCAL_BASE_ADDRESS +
  1733. PCIE_SOC_WAKE_ADDRESS);
  1734. ath10k_pci_wait(ar);
  1735. /*
  1736. * A potential race occurs here: The CORE_BASE write
  1737. * depends on target correctly decoding AXI address but
  1738. * host won't know when target writes BAR to CORE_CTRL.
  1739. * This write might get lost if target has NOT written BAR.
  1740. * For now, fix the race by repeating the write in below
  1741. * synchronization checking.
  1742. */
  1743. iowrite32(PCIE_INTR_FIRMWARE_MASK |
  1744. PCIE_INTR_CE_MASK_ALL,
  1745. ar_pci->mem + (SOC_CORE_BASE_ADDRESS |
  1746. PCIE_INTR_ENABLE_ADDRESS));
  1747. iowrite32(PCIE_SOC_WAKE_RESET,
  1748. ar_pci->mem + PCIE_LOCAL_BASE_ADDRESS +
  1749. PCIE_SOC_WAKE_ADDRESS);
  1750. ath10k_info("legacy interrupt handling\n");
  1751. return 0;
  1752. }
  1753. static int ath10k_pci_start_intr(struct ath10k *ar)
  1754. {
  1755. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1756. int num = MSI_NUM_REQUEST;
  1757. int ret;
  1758. int i;
  1759. tasklet_init(&ar_pci->intr_tq, ath10k_pci_tasklet, (unsigned long) ar);
  1760. tasklet_init(&ar_pci->msi_fw_err, ath10k_msi_err_tasklet,
  1761. (unsigned long) ar);
  1762. for (i = 0; i < CE_COUNT; i++) {
  1763. ar_pci->pipe_info[i].ar_pci = ar_pci;
  1764. tasklet_init(&ar_pci->pipe_info[i].intr,
  1765. ath10k_pci_ce_tasklet,
  1766. (unsigned long)&ar_pci->pipe_info[i]);
  1767. }
  1768. if (!test_bit(ATH10K_PCI_FEATURE_MSI_X, ar_pci->features))
  1769. num = 1;
  1770. if (num > 1) {
  1771. ret = ath10k_pci_start_intr_msix(ar, num);
  1772. if (ret == 0)
  1773. goto exit;
  1774. ath10k_warn("MSI-X didn't succeed (%d), trying MSI\n", ret);
  1775. num = 1;
  1776. }
  1777. if (num == 1) {
  1778. ret = ath10k_pci_start_intr_msi(ar);
  1779. if (ret == 0)
  1780. goto exit;
  1781. ath10k_warn("MSI didn't succeed (%d), trying legacy INTR\n",
  1782. ret);
  1783. num = 0;
  1784. }
  1785. ret = ath10k_pci_start_intr_legacy(ar);
  1786. exit:
  1787. ar_pci->num_msi_intrs = num;
  1788. ar_pci->ce_count = CE_COUNT;
  1789. return ret;
  1790. }
  1791. static void ath10k_pci_stop_intr(struct ath10k *ar)
  1792. {
  1793. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1794. int i;
  1795. /* There's at least one interrupt irregardless whether its legacy INTR
  1796. * or MSI or MSI-X */
  1797. for (i = 0; i < max(1, ar_pci->num_msi_intrs); i++)
  1798. free_irq(ar_pci->pdev->irq + i, ar);
  1799. if (ar_pci->num_msi_intrs > 0)
  1800. pci_disable_msi(ar_pci->pdev);
  1801. }
  1802. static int ath10k_pci_reset_target(struct ath10k *ar)
  1803. {
  1804. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1805. int wait_limit = 300; /* 3 sec */
  1806. /* Wait for Target to finish initialization before we proceed. */
  1807. iowrite32(PCIE_SOC_WAKE_V_MASK,
  1808. ar_pci->mem + PCIE_LOCAL_BASE_ADDRESS +
  1809. PCIE_SOC_WAKE_ADDRESS);
  1810. ath10k_pci_wait(ar);
  1811. while (wait_limit-- &&
  1812. !(ioread32(ar_pci->mem + FW_INDICATOR_ADDRESS) &
  1813. FW_IND_INITIALIZED)) {
  1814. if (ar_pci->num_msi_intrs == 0)
  1815. /* Fix potential race by repeating CORE_BASE writes */
  1816. iowrite32(PCIE_INTR_FIRMWARE_MASK |
  1817. PCIE_INTR_CE_MASK_ALL,
  1818. ar_pci->mem + (SOC_CORE_BASE_ADDRESS |
  1819. PCIE_INTR_ENABLE_ADDRESS));
  1820. mdelay(10);
  1821. }
  1822. if (wait_limit < 0) {
  1823. ath10k_err("Target stalled\n");
  1824. iowrite32(PCIE_SOC_WAKE_RESET,
  1825. ar_pci->mem + PCIE_LOCAL_BASE_ADDRESS +
  1826. PCIE_SOC_WAKE_ADDRESS);
  1827. return -EIO;
  1828. }
  1829. iowrite32(PCIE_SOC_WAKE_RESET,
  1830. ar_pci->mem + PCIE_LOCAL_BASE_ADDRESS +
  1831. PCIE_SOC_WAKE_ADDRESS);
  1832. return 0;
  1833. }
  1834. static void ath10k_pci_device_reset(struct ath10k *ar)
  1835. {
  1836. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1837. void __iomem *mem = ar_pci->mem;
  1838. int i;
  1839. u32 val;
  1840. if (!SOC_GLOBAL_RESET_ADDRESS)
  1841. return;
  1842. if (!mem)
  1843. return;
  1844. ath10k_pci_reg_write32(mem, PCIE_SOC_WAKE_ADDRESS,
  1845. PCIE_SOC_WAKE_V_MASK);
  1846. for (i = 0; i < ATH_PCI_RESET_WAIT_MAX; i++) {
  1847. if (ath10k_pci_target_is_awake(ar))
  1848. break;
  1849. msleep(1);
  1850. }
  1851. /* Put Target, including PCIe, into RESET. */
  1852. val = ath10k_pci_reg_read32(mem, SOC_GLOBAL_RESET_ADDRESS);
  1853. val |= 1;
  1854. ath10k_pci_reg_write32(mem, SOC_GLOBAL_RESET_ADDRESS, val);
  1855. for (i = 0; i < ATH_PCI_RESET_WAIT_MAX; i++) {
  1856. if (ath10k_pci_reg_read32(mem, RTC_STATE_ADDRESS) &
  1857. RTC_STATE_COLD_RESET_MASK)
  1858. break;
  1859. msleep(1);
  1860. }
  1861. /* Pull Target, including PCIe, out of RESET. */
  1862. val &= ~1;
  1863. ath10k_pci_reg_write32(mem, SOC_GLOBAL_RESET_ADDRESS, val);
  1864. for (i = 0; i < ATH_PCI_RESET_WAIT_MAX; i++) {
  1865. if (!(ath10k_pci_reg_read32(mem, RTC_STATE_ADDRESS) &
  1866. RTC_STATE_COLD_RESET_MASK))
  1867. break;
  1868. msleep(1);
  1869. }
  1870. ath10k_pci_reg_write32(mem, PCIE_SOC_WAKE_ADDRESS, PCIE_SOC_WAKE_RESET);
  1871. }
  1872. static void ath10k_pci_dump_features(struct ath10k_pci *ar_pci)
  1873. {
  1874. int i;
  1875. for (i = 0; i < ATH10K_PCI_FEATURE_COUNT; i++) {
  1876. if (!test_bit(i, ar_pci->features))
  1877. continue;
  1878. switch (i) {
  1879. case ATH10K_PCI_FEATURE_MSI_X:
  1880. ath10k_dbg(ATH10K_DBG_PCI, "device supports MSI-X\n");
  1881. break;
  1882. case ATH10K_PCI_FEATURE_HW_1_0_WORKAROUND:
  1883. ath10k_dbg(ATH10K_DBG_PCI, "QCA988X_1.0 workaround enabled\n");
  1884. break;
  1885. }
  1886. }
  1887. }
  1888. static int ath10k_pci_probe(struct pci_dev *pdev,
  1889. const struct pci_device_id *pci_dev)
  1890. {
  1891. void __iomem *mem;
  1892. int ret = 0;
  1893. struct ath10k *ar;
  1894. struct ath10k_pci *ar_pci;
  1895. u32 lcr_val;
  1896. ath10k_dbg(ATH10K_DBG_PCI, "%s\n", __func__);
  1897. ar_pci = kzalloc(sizeof(*ar_pci), GFP_KERNEL);
  1898. if (ar_pci == NULL)
  1899. return -ENOMEM;
  1900. ar_pci->pdev = pdev;
  1901. ar_pci->dev = &pdev->dev;
  1902. switch (pci_dev->device) {
  1903. case QCA988X_1_0_DEVICE_ID:
  1904. set_bit(ATH10K_PCI_FEATURE_HW_1_0_WORKAROUND, ar_pci->features);
  1905. break;
  1906. case QCA988X_2_0_DEVICE_ID:
  1907. set_bit(ATH10K_PCI_FEATURE_MSI_X, ar_pci->features);
  1908. break;
  1909. default:
  1910. ret = -ENODEV;
  1911. ath10k_err("Unkown device ID: %d\n", pci_dev->device);
  1912. goto err_ar_pci;
  1913. }
  1914. ath10k_pci_dump_features(ar_pci);
  1915. ar = ath10k_core_create(ar_pci, ar_pci->dev, &ath10k_pci_hif_ops);
  1916. if (!ar) {
  1917. ath10k_err("ath10k_core_create failed!\n");
  1918. ret = -EINVAL;
  1919. goto err_ar_pci;
  1920. }
  1921. /* Enable QCA988X_1.0 HW workarounds */
  1922. if (test_bit(ATH10K_PCI_FEATURE_HW_1_0_WORKAROUND, ar_pci->features))
  1923. spin_lock_init(&ar_pci->hw_v1_workaround_lock);
  1924. ar_pci->ar = ar;
  1925. ar_pci->fw_indicator_address = FW_INDICATOR_ADDRESS;
  1926. atomic_set(&ar_pci->keep_awake_count, 0);
  1927. pci_set_drvdata(pdev, ar);
  1928. /*
  1929. * Without any knowledge of the Host, the Target may have been reset or
  1930. * power cycled and its Config Space may no longer reflect the PCI
  1931. * address space that was assigned earlier by the PCI infrastructure.
  1932. * Refresh it now.
  1933. */
  1934. ret = pci_assign_resource(pdev, BAR_NUM);
  1935. if (ret) {
  1936. ath10k_err("cannot assign PCI space: %d\n", ret);
  1937. goto err_ar;
  1938. }
  1939. ret = pci_enable_device(pdev);
  1940. if (ret) {
  1941. ath10k_err("cannot enable PCI device: %d\n", ret);
  1942. goto err_ar;
  1943. }
  1944. /* Request MMIO resources */
  1945. ret = pci_request_region(pdev, BAR_NUM, "ath");
  1946. if (ret) {
  1947. ath10k_err("PCI MMIO reservation error: %d\n", ret);
  1948. goto err_device;
  1949. }
  1950. /*
  1951. * Target structures have a limit of 32 bit DMA pointers.
  1952. * DMA pointers can be wider than 32 bits by default on some systems.
  1953. */
  1954. ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  1955. if (ret) {
  1956. ath10k_err("32-bit DMA not available: %d\n", ret);
  1957. goto err_region;
  1958. }
  1959. ret = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  1960. if (ret) {
  1961. ath10k_err("cannot enable 32-bit consistent DMA\n");
  1962. goto err_region;
  1963. }
  1964. /* Set bus master bit in PCI_COMMAND to enable DMA */
  1965. pci_set_master(pdev);
  1966. /*
  1967. * Temporary FIX: disable ASPM
  1968. * Will be removed after the OTP is programmed
  1969. */
  1970. pci_read_config_dword(pdev, 0x80, &lcr_val);
  1971. pci_write_config_dword(pdev, 0x80, (lcr_val & 0xffffff00));
  1972. /* Arrange for access to Target SoC registers. */
  1973. mem = pci_iomap(pdev, BAR_NUM, 0);
  1974. if (!mem) {
  1975. ath10k_err("PCI iomap error\n");
  1976. ret = -EIO;
  1977. goto err_master;
  1978. }
  1979. ar_pci->mem = mem;
  1980. spin_lock_init(&ar_pci->ce_lock);
  1981. ar_pci->cacheline_sz = dma_get_cache_alignment();
  1982. ret = ath10k_pci_start_intr(ar);
  1983. if (ret) {
  1984. ath10k_err("could not start interrupt handling (%d)\n", ret);
  1985. goto err_iomap;
  1986. }
  1987. ret = ath10k_core_register(ar);
  1988. if (ret) {
  1989. ath10k_err("could not register driver core (%d)\n", ret);
  1990. goto err_intr;
  1991. }
  1992. return 0;
  1993. err_intr:
  1994. ath10k_pci_stop_intr(ar);
  1995. err_iomap:
  1996. pci_iounmap(pdev, mem);
  1997. err_master:
  1998. pci_clear_master(pdev);
  1999. err_region:
  2000. pci_release_region(pdev, BAR_NUM);
  2001. err_device:
  2002. pci_disable_device(pdev);
  2003. err_ar:
  2004. pci_set_drvdata(pdev, NULL);
  2005. ath10k_core_destroy(ar);
  2006. err_ar_pci:
  2007. /* call HIF PCI free here */
  2008. kfree(ar_pci);
  2009. return ret;
  2010. }
  2011. static void ath10k_pci_remove(struct pci_dev *pdev)
  2012. {
  2013. struct ath10k *ar = pci_get_drvdata(pdev);
  2014. struct ath10k_pci *ar_pci;
  2015. ath10k_dbg(ATH10K_DBG_PCI, "%s\n", __func__);
  2016. if (!ar)
  2017. return;
  2018. ar_pci = ath10k_pci_priv(ar);
  2019. if (!ar_pci)
  2020. return;
  2021. tasklet_kill(&ar_pci->msi_fw_err);
  2022. ath10k_core_unregister(ar);
  2023. ath10k_pci_stop_intr(ar);
  2024. pci_set_drvdata(pdev, NULL);
  2025. pci_iounmap(pdev, ar_pci->mem);
  2026. pci_release_region(pdev, BAR_NUM);
  2027. pci_clear_master(pdev);
  2028. pci_disable_device(pdev);
  2029. ath10k_core_destroy(ar);
  2030. kfree(ar_pci);
  2031. }
  2032. MODULE_DEVICE_TABLE(pci, ath10k_pci_id_table);
  2033. static struct pci_driver ath10k_pci_driver = {
  2034. .name = "ath10k_pci",
  2035. .id_table = ath10k_pci_id_table,
  2036. .probe = ath10k_pci_probe,
  2037. .remove = ath10k_pci_remove,
  2038. };
  2039. static int __init ath10k_pci_init(void)
  2040. {
  2041. int ret;
  2042. ret = pci_register_driver(&ath10k_pci_driver);
  2043. if (ret)
  2044. ath10k_err("pci_register_driver failed [%d]\n", ret);
  2045. return ret;
  2046. }
  2047. module_init(ath10k_pci_init);
  2048. static void __exit ath10k_pci_exit(void)
  2049. {
  2050. pci_unregister_driver(&ath10k_pci_driver);
  2051. }
  2052. module_exit(ath10k_pci_exit);
  2053. MODULE_AUTHOR("Qualcomm Atheros");
  2054. MODULE_DESCRIPTION("Driver support for Atheros QCA988X PCIe devices");
  2055. MODULE_LICENSE("Dual BSD/GPL");
  2056. MODULE_FIRMWARE(QCA988X_HW_1_0_FW_DIR "/" QCA988X_HW_1_0_FW_FILE);
  2057. MODULE_FIRMWARE(QCA988X_HW_1_0_FW_DIR "/" QCA988X_HW_1_0_OTP_FILE);
  2058. MODULE_FIRMWARE(QCA988X_HW_1_0_FW_DIR "/" QCA988X_HW_1_0_BOARD_DATA_FILE);
  2059. MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" QCA988X_HW_2_0_FW_FILE);
  2060. MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" QCA988X_HW_2_0_OTP_FILE);
  2061. MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" QCA988X_HW_2_0_BOARD_DATA_FILE);