pci.c 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476
  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 hif_ce_pipe_info *pipe_info,
  42. int num);
  43. static void ath10k_pci_rx_pipe_cleanup(struct hif_ce_pipe_info *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, 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_disable_irqs(struct ath10k *ar)
  1062. {
  1063. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1064. int i;
  1065. for (i = 0; i < max(1, ar_pci->num_msi_intrs); i++)
  1066. disable_irq(ar_pci->pdev->irq + i);
  1067. }
  1068. static void ath10k_pci_hif_stop(struct ath10k *ar)
  1069. {
  1070. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1071. ath10k_dbg(ATH10K_DBG_PCI, "%s\n", __func__);
  1072. /* Irqs are never explicitly re-enabled. They are implicitly re-enabled
  1073. * by ath10k_pci_start_intr(). */
  1074. ath10k_pci_disable_irqs(ar);
  1075. ath10k_pci_stop_ce(ar);
  1076. /* At this point, asynchronous threads are stopped, the target should
  1077. * not DMA nor interrupt. We process the leftovers and then free
  1078. * everything else up. */
  1079. ath10k_pci_process_ce(ar);
  1080. ath10k_pci_cleanup_ce(ar);
  1081. ath10k_pci_buffer_cleanup(ar);
  1082. ar_pci->started = 0;
  1083. }
  1084. static int ath10k_pci_hif_exchange_bmi_msg(struct ath10k *ar,
  1085. void *req, u32 req_len,
  1086. void *resp, u32 *resp_len)
  1087. {
  1088. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1089. struct ce_state *ce_tx = ar_pci->pipe_info[BMI_CE_NUM_TO_TARG].ce_hdl;
  1090. struct ce_state *ce_rx = ar_pci->pipe_info[BMI_CE_NUM_TO_HOST].ce_hdl;
  1091. dma_addr_t req_paddr = 0;
  1092. dma_addr_t resp_paddr = 0;
  1093. struct bmi_xfer xfer = {};
  1094. void *treq, *tresp = NULL;
  1095. int ret = 0;
  1096. if (resp && !resp_len)
  1097. return -EINVAL;
  1098. if (resp && resp_len && *resp_len == 0)
  1099. return -EINVAL;
  1100. treq = kmemdup(req, req_len, GFP_KERNEL);
  1101. if (!treq)
  1102. return -ENOMEM;
  1103. req_paddr = dma_map_single(ar->dev, treq, req_len, DMA_TO_DEVICE);
  1104. ret = dma_mapping_error(ar->dev, req_paddr);
  1105. if (ret)
  1106. goto err_dma;
  1107. if (resp && resp_len) {
  1108. tresp = kzalloc(*resp_len, GFP_KERNEL);
  1109. if (!tresp) {
  1110. ret = -ENOMEM;
  1111. goto err_req;
  1112. }
  1113. resp_paddr = dma_map_single(ar->dev, tresp, *resp_len,
  1114. DMA_FROM_DEVICE);
  1115. ret = dma_mapping_error(ar->dev, resp_paddr);
  1116. if (ret)
  1117. goto err_req;
  1118. xfer.wait_for_resp = true;
  1119. xfer.resp_len = 0;
  1120. ath10k_ce_recv_buf_enqueue(ce_rx, &xfer, resp_paddr);
  1121. }
  1122. init_completion(&xfer.done);
  1123. ret = ath10k_ce_send(ce_tx, &xfer, req_paddr, req_len, -1, 0);
  1124. if (ret)
  1125. goto err_resp;
  1126. ret = wait_for_completion_timeout(&xfer.done,
  1127. BMI_COMMUNICATION_TIMEOUT_HZ);
  1128. if (ret <= 0) {
  1129. u32 unused_buffer;
  1130. unsigned int unused_nbytes;
  1131. unsigned int unused_id;
  1132. ret = -ETIMEDOUT;
  1133. ath10k_ce_cancel_send_next(ce_tx, NULL, &unused_buffer,
  1134. &unused_nbytes, &unused_id);
  1135. } else {
  1136. /* non-zero means we did not time out */
  1137. ret = 0;
  1138. }
  1139. err_resp:
  1140. if (resp) {
  1141. u32 unused_buffer;
  1142. ath10k_ce_revoke_recv_next(ce_rx, NULL, &unused_buffer);
  1143. dma_unmap_single(ar->dev, resp_paddr,
  1144. *resp_len, DMA_FROM_DEVICE);
  1145. }
  1146. err_req:
  1147. dma_unmap_single(ar->dev, req_paddr, req_len, DMA_TO_DEVICE);
  1148. if (ret == 0 && resp_len) {
  1149. *resp_len = min(*resp_len, xfer.resp_len);
  1150. memcpy(resp, tresp, xfer.resp_len);
  1151. }
  1152. err_dma:
  1153. kfree(treq);
  1154. kfree(tresp);
  1155. return ret;
  1156. }
  1157. static void ath10k_pci_bmi_send_done(struct ce_state *ce_state,
  1158. void *transfer_context,
  1159. u32 data,
  1160. unsigned int nbytes,
  1161. unsigned int transfer_id)
  1162. {
  1163. struct bmi_xfer *xfer = transfer_context;
  1164. if (xfer->wait_for_resp)
  1165. return;
  1166. complete(&xfer->done);
  1167. }
  1168. static void ath10k_pci_bmi_recv_data(struct ce_state *ce_state,
  1169. void *transfer_context,
  1170. u32 data,
  1171. unsigned int nbytes,
  1172. unsigned int transfer_id,
  1173. unsigned int flags)
  1174. {
  1175. struct bmi_xfer *xfer = transfer_context;
  1176. if (!xfer->wait_for_resp) {
  1177. ath10k_warn("unexpected: BMI data received; ignoring\n");
  1178. return;
  1179. }
  1180. xfer->resp_len = nbytes;
  1181. complete(&xfer->done);
  1182. }
  1183. /*
  1184. * Map from service/endpoint to Copy Engine.
  1185. * This table is derived from the CE_PCI TABLE, above.
  1186. * It is passed to the Target at startup for use by firmware.
  1187. */
  1188. static const struct service_to_pipe target_service_to_ce_map_wlan[] = {
  1189. {
  1190. ATH10K_HTC_SVC_ID_WMI_DATA_VO,
  1191. PIPEDIR_OUT, /* out = UL = host -> target */
  1192. 3,
  1193. },
  1194. {
  1195. ATH10K_HTC_SVC_ID_WMI_DATA_VO,
  1196. PIPEDIR_IN, /* in = DL = target -> host */
  1197. 2,
  1198. },
  1199. {
  1200. ATH10K_HTC_SVC_ID_WMI_DATA_BK,
  1201. PIPEDIR_OUT, /* out = UL = host -> target */
  1202. 3,
  1203. },
  1204. {
  1205. ATH10K_HTC_SVC_ID_WMI_DATA_BK,
  1206. PIPEDIR_IN, /* in = DL = target -> host */
  1207. 2,
  1208. },
  1209. {
  1210. ATH10K_HTC_SVC_ID_WMI_DATA_BE,
  1211. PIPEDIR_OUT, /* out = UL = host -> target */
  1212. 3,
  1213. },
  1214. {
  1215. ATH10K_HTC_SVC_ID_WMI_DATA_BE,
  1216. PIPEDIR_IN, /* in = DL = target -> host */
  1217. 2,
  1218. },
  1219. {
  1220. ATH10K_HTC_SVC_ID_WMI_DATA_VI,
  1221. PIPEDIR_OUT, /* out = UL = host -> target */
  1222. 3,
  1223. },
  1224. {
  1225. ATH10K_HTC_SVC_ID_WMI_DATA_VI,
  1226. PIPEDIR_IN, /* in = DL = target -> host */
  1227. 2,
  1228. },
  1229. {
  1230. ATH10K_HTC_SVC_ID_WMI_CONTROL,
  1231. PIPEDIR_OUT, /* out = UL = host -> target */
  1232. 3,
  1233. },
  1234. {
  1235. ATH10K_HTC_SVC_ID_WMI_CONTROL,
  1236. PIPEDIR_IN, /* in = DL = target -> host */
  1237. 2,
  1238. },
  1239. {
  1240. ATH10K_HTC_SVC_ID_RSVD_CTRL,
  1241. PIPEDIR_OUT, /* out = UL = host -> target */
  1242. 0, /* could be moved to 3 (share with WMI) */
  1243. },
  1244. {
  1245. ATH10K_HTC_SVC_ID_RSVD_CTRL,
  1246. PIPEDIR_IN, /* in = DL = target -> host */
  1247. 1,
  1248. },
  1249. {
  1250. ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS, /* not currently used */
  1251. PIPEDIR_OUT, /* out = UL = host -> target */
  1252. 0,
  1253. },
  1254. {
  1255. ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS, /* not currently used */
  1256. PIPEDIR_IN, /* in = DL = target -> host */
  1257. 1,
  1258. },
  1259. {
  1260. ATH10K_HTC_SVC_ID_HTT_DATA_MSG,
  1261. PIPEDIR_OUT, /* out = UL = host -> target */
  1262. 4,
  1263. },
  1264. {
  1265. ATH10K_HTC_SVC_ID_HTT_DATA_MSG,
  1266. PIPEDIR_IN, /* in = DL = target -> host */
  1267. 1,
  1268. },
  1269. /* (Additions here) */
  1270. { /* Must be last */
  1271. 0,
  1272. 0,
  1273. 0,
  1274. },
  1275. };
  1276. /*
  1277. * Send an interrupt to the device to wake up the Target CPU
  1278. * so it has an opportunity to notice any changed state.
  1279. */
  1280. static int ath10k_pci_wake_target_cpu(struct ath10k *ar)
  1281. {
  1282. int ret;
  1283. u32 core_ctrl;
  1284. ret = ath10k_pci_diag_read_access(ar, SOC_CORE_BASE_ADDRESS |
  1285. CORE_CTRL_ADDRESS,
  1286. &core_ctrl);
  1287. if (ret) {
  1288. ath10k_warn("Unable to read core ctrl\n");
  1289. return ret;
  1290. }
  1291. /* A_INUM_FIRMWARE interrupt to Target CPU */
  1292. core_ctrl |= CORE_CTRL_CPU_INTR_MASK;
  1293. ret = ath10k_pci_diag_write_access(ar, SOC_CORE_BASE_ADDRESS |
  1294. CORE_CTRL_ADDRESS,
  1295. core_ctrl);
  1296. if (ret)
  1297. ath10k_warn("Unable to set interrupt mask\n");
  1298. return ret;
  1299. }
  1300. static int ath10k_pci_init_config(struct ath10k *ar)
  1301. {
  1302. u32 interconnect_targ_addr;
  1303. u32 pcie_state_targ_addr = 0;
  1304. u32 pipe_cfg_targ_addr = 0;
  1305. u32 svc_to_pipe_map = 0;
  1306. u32 pcie_config_flags = 0;
  1307. u32 ealloc_value;
  1308. u32 ealloc_targ_addr;
  1309. u32 flag2_value;
  1310. u32 flag2_targ_addr;
  1311. int ret = 0;
  1312. /* Download to Target the CE Config and the service-to-CE map */
  1313. interconnect_targ_addr =
  1314. host_interest_item_address(HI_ITEM(hi_interconnect_state));
  1315. /* Supply Target-side CE configuration */
  1316. ret = ath10k_pci_diag_read_access(ar, interconnect_targ_addr,
  1317. &pcie_state_targ_addr);
  1318. if (ret != 0) {
  1319. ath10k_err("Failed to get pcie state addr: %d\n", ret);
  1320. return ret;
  1321. }
  1322. if (pcie_state_targ_addr == 0) {
  1323. ret = -EIO;
  1324. ath10k_err("Invalid pcie state addr\n");
  1325. return ret;
  1326. }
  1327. ret = ath10k_pci_diag_read_access(ar, pcie_state_targ_addr +
  1328. offsetof(struct pcie_state,
  1329. pipe_cfg_addr),
  1330. &pipe_cfg_targ_addr);
  1331. if (ret != 0) {
  1332. ath10k_err("Failed to get pipe cfg addr: %d\n", ret);
  1333. return ret;
  1334. }
  1335. if (pipe_cfg_targ_addr == 0) {
  1336. ret = -EIO;
  1337. ath10k_err("Invalid pipe cfg addr\n");
  1338. return ret;
  1339. }
  1340. ret = ath10k_pci_diag_write_mem(ar, pipe_cfg_targ_addr,
  1341. target_ce_config_wlan,
  1342. sizeof(target_ce_config_wlan));
  1343. if (ret != 0) {
  1344. ath10k_err("Failed to write pipe cfg: %d\n", ret);
  1345. return ret;
  1346. }
  1347. ret = ath10k_pci_diag_read_access(ar, pcie_state_targ_addr +
  1348. offsetof(struct pcie_state,
  1349. svc_to_pipe_map),
  1350. &svc_to_pipe_map);
  1351. if (ret != 0) {
  1352. ath10k_err("Failed to get svc/pipe map: %d\n", ret);
  1353. return ret;
  1354. }
  1355. if (svc_to_pipe_map == 0) {
  1356. ret = -EIO;
  1357. ath10k_err("Invalid svc_to_pipe map\n");
  1358. return ret;
  1359. }
  1360. ret = ath10k_pci_diag_write_mem(ar, svc_to_pipe_map,
  1361. target_service_to_ce_map_wlan,
  1362. sizeof(target_service_to_ce_map_wlan));
  1363. if (ret != 0) {
  1364. ath10k_err("Failed to write svc/pipe map: %d\n", ret);
  1365. return ret;
  1366. }
  1367. ret = ath10k_pci_diag_read_access(ar, pcie_state_targ_addr +
  1368. offsetof(struct pcie_state,
  1369. config_flags),
  1370. &pcie_config_flags);
  1371. if (ret != 0) {
  1372. ath10k_err("Failed to get pcie config_flags: %d\n", ret);
  1373. return ret;
  1374. }
  1375. pcie_config_flags &= ~PCIE_CONFIG_FLAG_ENABLE_L1;
  1376. ret = ath10k_pci_diag_write_mem(ar, pcie_state_targ_addr +
  1377. offsetof(struct pcie_state, config_flags),
  1378. &pcie_config_flags,
  1379. sizeof(pcie_config_flags));
  1380. if (ret != 0) {
  1381. ath10k_err("Failed to write pcie config_flags: %d\n", ret);
  1382. return ret;
  1383. }
  1384. /* configure early allocation */
  1385. ealloc_targ_addr = host_interest_item_address(HI_ITEM(hi_early_alloc));
  1386. ret = ath10k_pci_diag_read_access(ar, ealloc_targ_addr, &ealloc_value);
  1387. if (ret != 0) {
  1388. ath10k_err("Faile to get early alloc val: %d\n", ret);
  1389. return ret;
  1390. }
  1391. /* first bank is switched to IRAM */
  1392. ealloc_value |= ((HI_EARLY_ALLOC_MAGIC << HI_EARLY_ALLOC_MAGIC_SHIFT) &
  1393. HI_EARLY_ALLOC_MAGIC_MASK);
  1394. ealloc_value |= ((1 << HI_EARLY_ALLOC_IRAM_BANKS_SHIFT) &
  1395. HI_EARLY_ALLOC_IRAM_BANKS_MASK);
  1396. ret = ath10k_pci_diag_write_access(ar, ealloc_targ_addr, ealloc_value);
  1397. if (ret != 0) {
  1398. ath10k_err("Failed to set early alloc val: %d\n", ret);
  1399. return ret;
  1400. }
  1401. /* Tell Target to proceed with initialization */
  1402. flag2_targ_addr = host_interest_item_address(HI_ITEM(hi_option_flag2));
  1403. ret = ath10k_pci_diag_read_access(ar, flag2_targ_addr, &flag2_value);
  1404. if (ret != 0) {
  1405. ath10k_err("Failed to get option val: %d\n", ret);
  1406. return ret;
  1407. }
  1408. flag2_value |= HI_OPTION_EARLY_CFG_DONE;
  1409. ret = ath10k_pci_diag_write_access(ar, flag2_targ_addr, flag2_value);
  1410. if (ret != 0) {
  1411. ath10k_err("Failed to set option val: %d\n", ret);
  1412. return ret;
  1413. }
  1414. return 0;
  1415. }
  1416. static int ath10k_pci_ce_init(struct ath10k *ar)
  1417. {
  1418. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1419. struct hif_ce_pipe_info *pipe_info;
  1420. const struct ce_attr *attr;
  1421. int pipe_num;
  1422. for (pipe_num = 0; pipe_num < ar_pci->ce_count; pipe_num++) {
  1423. pipe_info = &ar_pci->pipe_info[pipe_num];
  1424. pipe_info->pipe_num = pipe_num;
  1425. pipe_info->hif_ce_state = ar;
  1426. attr = &host_ce_config_wlan[pipe_num];
  1427. pipe_info->ce_hdl = ath10k_ce_init(ar, pipe_num, attr);
  1428. if (pipe_info->ce_hdl == NULL) {
  1429. ath10k_err("Unable to initialize CE for pipe: %d\n",
  1430. pipe_num);
  1431. /* It is safe to call it here. It checks if ce_hdl is
  1432. * valid for each pipe */
  1433. ath10k_pci_ce_deinit(ar);
  1434. return -1;
  1435. }
  1436. if (pipe_num == ar_pci->ce_count - 1) {
  1437. /*
  1438. * Reserve the ultimate CE for
  1439. * diagnostic Window support
  1440. */
  1441. ar_pci->ce_diag =
  1442. ar_pci->pipe_info[ar_pci->ce_count - 1].ce_hdl;
  1443. continue;
  1444. }
  1445. pipe_info->buf_sz = (size_t) (attr->src_sz_max);
  1446. }
  1447. /*
  1448. * Initially, establish CE completion handlers for use with BMI.
  1449. * These are overwritten with generic handlers after we exit BMI phase.
  1450. */
  1451. pipe_info = &ar_pci->pipe_info[BMI_CE_NUM_TO_TARG];
  1452. ath10k_ce_send_cb_register(pipe_info->ce_hdl,
  1453. ath10k_pci_bmi_send_done, 0);
  1454. pipe_info = &ar_pci->pipe_info[BMI_CE_NUM_TO_HOST];
  1455. ath10k_ce_recv_cb_register(pipe_info->ce_hdl,
  1456. ath10k_pci_bmi_recv_data);
  1457. return 0;
  1458. }
  1459. static void ath10k_pci_fw_interrupt_handler(struct ath10k *ar)
  1460. {
  1461. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1462. u32 fw_indicator_address, fw_indicator;
  1463. ath10k_pci_wake(ar);
  1464. fw_indicator_address = ar_pci->fw_indicator_address;
  1465. fw_indicator = ath10k_pci_read32(ar, fw_indicator_address);
  1466. if (fw_indicator & FW_IND_EVENT_PENDING) {
  1467. /* ACK: clear Target-side pending event */
  1468. ath10k_pci_write32(ar, fw_indicator_address,
  1469. fw_indicator & ~FW_IND_EVENT_PENDING);
  1470. if (ar_pci->started) {
  1471. ath10k_pci_hif_dump_area(ar);
  1472. } else {
  1473. /*
  1474. * Probable Target failure before we're prepared
  1475. * to handle it. Generally unexpected.
  1476. */
  1477. ath10k_warn("early firmware event indicated\n");
  1478. }
  1479. }
  1480. ath10k_pci_sleep(ar);
  1481. }
  1482. static int ath10k_pci_hif_power_up(struct ath10k *ar)
  1483. {
  1484. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1485. int ret;
  1486. ret = ath10k_pci_start_intr(ar);
  1487. if (ret) {
  1488. ath10k_err("could not start interrupt handling (%d)\n", ret);
  1489. goto err;
  1490. }
  1491. /*
  1492. * Bring the target up cleanly.
  1493. *
  1494. * The target may be in an undefined state with an AUX-powered Target
  1495. * and a Host in WoW mode. If the Host crashes, loses power, or is
  1496. * restarted (without unloading the driver) then the Target is left
  1497. * (aux) powered and running. On a subsequent driver load, the Target
  1498. * is in an unexpected state. We try to catch that here in order to
  1499. * reset the Target and retry the probe.
  1500. */
  1501. ath10k_pci_device_reset(ar);
  1502. ret = ath10k_pci_reset_target(ar);
  1503. if (ret)
  1504. goto err_irq;
  1505. if (!test_bit(ATH10K_PCI_FEATURE_SOC_POWER_SAVE, ar_pci->features))
  1506. /* Force AWAKE forever */
  1507. ath10k_do_pci_wake(ar);
  1508. ret = ath10k_pci_ce_init(ar);
  1509. if (ret)
  1510. goto err_ps;
  1511. ret = ath10k_pci_init_config(ar);
  1512. if (ret)
  1513. goto err_ce;
  1514. ret = ath10k_pci_wake_target_cpu(ar);
  1515. if (ret) {
  1516. ath10k_err("could not wake up target CPU (%d)\n", ret);
  1517. goto err_ce;
  1518. }
  1519. return 0;
  1520. err_ce:
  1521. ath10k_pci_ce_deinit(ar);
  1522. err_ps:
  1523. if (!test_bit(ATH10K_PCI_FEATURE_SOC_POWER_SAVE, ar_pci->features))
  1524. ath10k_do_pci_sleep(ar);
  1525. err_irq:
  1526. ath10k_pci_stop_intr(ar);
  1527. err:
  1528. return ret;
  1529. }
  1530. static void ath10k_pci_hif_power_down(struct ath10k *ar)
  1531. {
  1532. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1533. ath10k_pci_stop_intr(ar);
  1534. ath10k_pci_ce_deinit(ar);
  1535. if (!test_bit(ATH10K_PCI_FEATURE_SOC_POWER_SAVE, ar_pci->features))
  1536. ath10k_do_pci_sleep(ar);
  1537. }
  1538. #ifdef CONFIG_PM
  1539. #define ATH10K_PCI_PM_CONTROL 0x44
  1540. static int ath10k_pci_hif_suspend(struct ath10k *ar)
  1541. {
  1542. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1543. struct pci_dev *pdev = ar_pci->pdev;
  1544. u32 val;
  1545. pci_read_config_dword(pdev, ATH10K_PCI_PM_CONTROL, &val);
  1546. if ((val & 0x000000ff) != 0x3) {
  1547. pci_save_state(pdev);
  1548. pci_disable_device(pdev);
  1549. pci_write_config_dword(pdev, ATH10K_PCI_PM_CONTROL,
  1550. (val & 0xffffff00) | 0x03);
  1551. }
  1552. return 0;
  1553. }
  1554. static int ath10k_pci_hif_resume(struct ath10k *ar)
  1555. {
  1556. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1557. struct pci_dev *pdev = ar_pci->pdev;
  1558. u32 val;
  1559. pci_read_config_dword(pdev, ATH10K_PCI_PM_CONTROL, &val);
  1560. if ((val & 0x000000ff) != 0) {
  1561. pci_restore_state(pdev);
  1562. pci_write_config_dword(pdev, ATH10K_PCI_PM_CONTROL,
  1563. val & 0xffffff00);
  1564. /*
  1565. * Suspend/Resume resets the PCI configuration space,
  1566. * so we have to re-disable the RETRY_TIMEOUT register (0x41)
  1567. * to keep PCI Tx retries from interfering with C3 CPU state
  1568. */
  1569. pci_read_config_dword(pdev, 0x40, &val);
  1570. if ((val & 0x0000ff00) != 0)
  1571. pci_write_config_dword(pdev, 0x40, val & 0xffff00ff);
  1572. }
  1573. return 0;
  1574. }
  1575. #endif
  1576. static const struct ath10k_hif_ops ath10k_pci_hif_ops = {
  1577. .send_head = ath10k_pci_hif_send_head,
  1578. .exchange_bmi_msg = ath10k_pci_hif_exchange_bmi_msg,
  1579. .start = ath10k_pci_hif_start,
  1580. .stop = ath10k_pci_hif_stop,
  1581. .map_service_to_pipe = ath10k_pci_hif_map_service_to_pipe,
  1582. .get_default_pipe = ath10k_pci_hif_get_default_pipe,
  1583. .send_complete_check = ath10k_pci_hif_send_complete_check,
  1584. .set_callbacks = ath10k_pci_hif_set_callbacks,
  1585. .get_free_queue_number = ath10k_pci_hif_get_free_queue_number,
  1586. .power_up = ath10k_pci_hif_power_up,
  1587. .power_down = ath10k_pci_hif_power_down,
  1588. #ifdef CONFIG_PM
  1589. .suspend = ath10k_pci_hif_suspend,
  1590. .resume = ath10k_pci_hif_resume,
  1591. #endif
  1592. };
  1593. static void ath10k_pci_ce_tasklet(unsigned long ptr)
  1594. {
  1595. struct hif_ce_pipe_info *pipe = (struct hif_ce_pipe_info *)ptr;
  1596. struct ath10k_pci *ar_pci = pipe->ar_pci;
  1597. ath10k_ce_per_engine_service(ar_pci->ar, pipe->pipe_num);
  1598. }
  1599. static void ath10k_msi_err_tasklet(unsigned long data)
  1600. {
  1601. struct ath10k *ar = (struct ath10k *)data;
  1602. ath10k_pci_fw_interrupt_handler(ar);
  1603. }
  1604. /*
  1605. * Handler for a per-engine interrupt on a PARTICULAR CE.
  1606. * This is used in cases where each CE has a private MSI interrupt.
  1607. */
  1608. static irqreturn_t ath10k_pci_per_engine_handler(int irq, void *arg)
  1609. {
  1610. struct ath10k *ar = arg;
  1611. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1612. int ce_id = irq - ar_pci->pdev->irq - MSI_ASSIGN_CE_INITIAL;
  1613. if (ce_id < 0 || ce_id >= ARRAY_SIZE(ar_pci->pipe_info)) {
  1614. ath10k_warn("unexpected/invalid irq %d ce_id %d\n", irq, ce_id);
  1615. return IRQ_HANDLED;
  1616. }
  1617. /*
  1618. * NOTE: We are able to derive ce_id from irq because we
  1619. * use a one-to-one mapping for CE's 0..5.
  1620. * CE's 6 & 7 do not use interrupts at all.
  1621. *
  1622. * This mapping must be kept in sync with the mapping
  1623. * used by firmware.
  1624. */
  1625. tasklet_schedule(&ar_pci->pipe_info[ce_id].intr);
  1626. return IRQ_HANDLED;
  1627. }
  1628. static irqreturn_t ath10k_pci_msi_fw_handler(int irq, void *arg)
  1629. {
  1630. struct ath10k *ar = arg;
  1631. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1632. tasklet_schedule(&ar_pci->msi_fw_err);
  1633. return IRQ_HANDLED;
  1634. }
  1635. /*
  1636. * Top-level interrupt handler for all PCI interrupts from a Target.
  1637. * When a block of MSI interrupts is allocated, this top-level handler
  1638. * is not used; instead, we directly call the correct sub-handler.
  1639. */
  1640. static irqreturn_t ath10k_pci_interrupt_handler(int irq, void *arg)
  1641. {
  1642. struct ath10k *ar = arg;
  1643. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1644. if (ar_pci->num_msi_intrs == 0) {
  1645. /*
  1646. * IMPORTANT: INTR_CLR regiser has to be set after
  1647. * INTR_ENABLE is set to 0, otherwise interrupt can not be
  1648. * really cleared.
  1649. */
  1650. iowrite32(0, ar_pci->mem +
  1651. (SOC_CORE_BASE_ADDRESS |
  1652. PCIE_INTR_ENABLE_ADDRESS));
  1653. iowrite32(PCIE_INTR_FIRMWARE_MASK |
  1654. PCIE_INTR_CE_MASK_ALL,
  1655. ar_pci->mem + (SOC_CORE_BASE_ADDRESS |
  1656. PCIE_INTR_CLR_ADDRESS));
  1657. /*
  1658. * IMPORTANT: this extra read transaction is required to
  1659. * flush the posted write buffer.
  1660. */
  1661. (void) ioread32(ar_pci->mem +
  1662. (SOC_CORE_BASE_ADDRESS |
  1663. PCIE_INTR_ENABLE_ADDRESS));
  1664. }
  1665. tasklet_schedule(&ar_pci->intr_tq);
  1666. return IRQ_HANDLED;
  1667. }
  1668. static void ath10k_pci_tasklet(unsigned long data)
  1669. {
  1670. struct ath10k *ar = (struct ath10k *)data;
  1671. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1672. ath10k_pci_fw_interrupt_handler(ar); /* FIXME: Handle FW error */
  1673. ath10k_ce_per_engine_service_any(ar);
  1674. if (ar_pci->num_msi_intrs == 0) {
  1675. /* Enable Legacy PCI line interrupts */
  1676. iowrite32(PCIE_INTR_FIRMWARE_MASK |
  1677. PCIE_INTR_CE_MASK_ALL,
  1678. ar_pci->mem + (SOC_CORE_BASE_ADDRESS |
  1679. PCIE_INTR_ENABLE_ADDRESS));
  1680. /*
  1681. * IMPORTANT: this extra read transaction is required to
  1682. * flush the posted write buffer
  1683. */
  1684. (void) ioread32(ar_pci->mem +
  1685. (SOC_CORE_BASE_ADDRESS |
  1686. PCIE_INTR_ENABLE_ADDRESS));
  1687. }
  1688. }
  1689. static int ath10k_pci_start_intr_msix(struct ath10k *ar, int num)
  1690. {
  1691. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1692. int ret;
  1693. int i;
  1694. ret = pci_enable_msi_block(ar_pci->pdev, num);
  1695. if (ret)
  1696. return ret;
  1697. ret = request_irq(ar_pci->pdev->irq + MSI_ASSIGN_FW,
  1698. ath10k_pci_msi_fw_handler,
  1699. IRQF_SHARED, "ath10k_pci", ar);
  1700. if (ret) {
  1701. ath10k_warn("request_irq(%d) failed %d\n",
  1702. ar_pci->pdev->irq + MSI_ASSIGN_FW, ret);
  1703. pci_disable_msi(ar_pci->pdev);
  1704. return ret;
  1705. }
  1706. for (i = MSI_ASSIGN_CE_INITIAL; i <= MSI_ASSIGN_CE_MAX; i++) {
  1707. ret = request_irq(ar_pci->pdev->irq + i,
  1708. ath10k_pci_per_engine_handler,
  1709. IRQF_SHARED, "ath10k_pci", ar);
  1710. if (ret) {
  1711. ath10k_warn("request_irq(%d) failed %d\n",
  1712. ar_pci->pdev->irq + i, ret);
  1713. for (i--; i >= MSI_ASSIGN_CE_INITIAL; i--)
  1714. free_irq(ar_pci->pdev->irq + i, ar);
  1715. free_irq(ar_pci->pdev->irq + MSI_ASSIGN_FW, ar);
  1716. pci_disable_msi(ar_pci->pdev);
  1717. return ret;
  1718. }
  1719. }
  1720. ath10k_info("MSI-X interrupt handling (%d intrs)\n", num);
  1721. return 0;
  1722. }
  1723. static int ath10k_pci_start_intr_msi(struct ath10k *ar)
  1724. {
  1725. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1726. int ret;
  1727. ret = pci_enable_msi(ar_pci->pdev);
  1728. if (ret < 0)
  1729. return ret;
  1730. ret = request_irq(ar_pci->pdev->irq,
  1731. ath10k_pci_interrupt_handler,
  1732. IRQF_SHARED, "ath10k_pci", ar);
  1733. if (ret < 0) {
  1734. pci_disable_msi(ar_pci->pdev);
  1735. return ret;
  1736. }
  1737. ath10k_info("MSI interrupt handling\n");
  1738. return 0;
  1739. }
  1740. static int ath10k_pci_start_intr_legacy(struct ath10k *ar)
  1741. {
  1742. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1743. int ret;
  1744. ret = request_irq(ar_pci->pdev->irq,
  1745. ath10k_pci_interrupt_handler,
  1746. IRQF_SHARED, "ath10k_pci", ar);
  1747. if (ret < 0)
  1748. return ret;
  1749. /*
  1750. * Make sure to wake the Target before enabling Legacy
  1751. * Interrupt.
  1752. */
  1753. iowrite32(PCIE_SOC_WAKE_V_MASK,
  1754. ar_pci->mem + PCIE_LOCAL_BASE_ADDRESS +
  1755. PCIE_SOC_WAKE_ADDRESS);
  1756. ath10k_pci_wait(ar);
  1757. /*
  1758. * A potential race occurs here: The CORE_BASE write
  1759. * depends on target correctly decoding AXI address but
  1760. * host won't know when target writes BAR to CORE_CTRL.
  1761. * This write might get lost if target has NOT written BAR.
  1762. * For now, fix the race by repeating the write in below
  1763. * synchronization checking.
  1764. */
  1765. iowrite32(PCIE_INTR_FIRMWARE_MASK |
  1766. PCIE_INTR_CE_MASK_ALL,
  1767. ar_pci->mem + (SOC_CORE_BASE_ADDRESS |
  1768. PCIE_INTR_ENABLE_ADDRESS));
  1769. iowrite32(PCIE_SOC_WAKE_RESET,
  1770. ar_pci->mem + PCIE_LOCAL_BASE_ADDRESS +
  1771. PCIE_SOC_WAKE_ADDRESS);
  1772. ath10k_info("legacy interrupt handling\n");
  1773. return 0;
  1774. }
  1775. static int ath10k_pci_start_intr(struct ath10k *ar)
  1776. {
  1777. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1778. int num = MSI_NUM_REQUEST;
  1779. int ret;
  1780. int i;
  1781. tasklet_init(&ar_pci->intr_tq, ath10k_pci_tasklet, (unsigned long) ar);
  1782. tasklet_init(&ar_pci->msi_fw_err, ath10k_msi_err_tasklet,
  1783. (unsigned long) ar);
  1784. for (i = 0; i < CE_COUNT; i++) {
  1785. ar_pci->pipe_info[i].ar_pci = ar_pci;
  1786. tasklet_init(&ar_pci->pipe_info[i].intr,
  1787. ath10k_pci_ce_tasklet,
  1788. (unsigned long)&ar_pci->pipe_info[i]);
  1789. }
  1790. if (!test_bit(ATH10K_PCI_FEATURE_MSI_X, ar_pci->features))
  1791. num = 1;
  1792. if (num > 1) {
  1793. ret = ath10k_pci_start_intr_msix(ar, num);
  1794. if (ret == 0)
  1795. goto exit;
  1796. ath10k_warn("MSI-X didn't succeed (%d), trying MSI\n", ret);
  1797. num = 1;
  1798. }
  1799. if (num == 1) {
  1800. ret = ath10k_pci_start_intr_msi(ar);
  1801. if (ret == 0)
  1802. goto exit;
  1803. ath10k_warn("MSI didn't succeed (%d), trying legacy INTR\n",
  1804. ret);
  1805. num = 0;
  1806. }
  1807. ret = ath10k_pci_start_intr_legacy(ar);
  1808. exit:
  1809. ar_pci->num_msi_intrs = num;
  1810. ar_pci->ce_count = CE_COUNT;
  1811. return ret;
  1812. }
  1813. static void ath10k_pci_stop_intr(struct ath10k *ar)
  1814. {
  1815. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1816. int i;
  1817. /* There's at least one interrupt irregardless whether its legacy INTR
  1818. * or MSI or MSI-X */
  1819. for (i = 0; i < max(1, ar_pci->num_msi_intrs); i++)
  1820. free_irq(ar_pci->pdev->irq + i, ar);
  1821. if (ar_pci->num_msi_intrs > 0)
  1822. pci_disable_msi(ar_pci->pdev);
  1823. }
  1824. static int ath10k_pci_reset_target(struct ath10k *ar)
  1825. {
  1826. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1827. int wait_limit = 300; /* 3 sec */
  1828. /* Wait for Target to finish initialization before we proceed. */
  1829. iowrite32(PCIE_SOC_WAKE_V_MASK,
  1830. ar_pci->mem + PCIE_LOCAL_BASE_ADDRESS +
  1831. PCIE_SOC_WAKE_ADDRESS);
  1832. ath10k_pci_wait(ar);
  1833. while (wait_limit-- &&
  1834. !(ioread32(ar_pci->mem + FW_INDICATOR_ADDRESS) &
  1835. FW_IND_INITIALIZED)) {
  1836. if (ar_pci->num_msi_intrs == 0)
  1837. /* Fix potential race by repeating CORE_BASE writes */
  1838. iowrite32(PCIE_INTR_FIRMWARE_MASK |
  1839. PCIE_INTR_CE_MASK_ALL,
  1840. ar_pci->mem + (SOC_CORE_BASE_ADDRESS |
  1841. PCIE_INTR_ENABLE_ADDRESS));
  1842. mdelay(10);
  1843. }
  1844. if (wait_limit < 0) {
  1845. ath10k_err("Target stalled\n");
  1846. iowrite32(PCIE_SOC_WAKE_RESET,
  1847. ar_pci->mem + PCIE_LOCAL_BASE_ADDRESS +
  1848. PCIE_SOC_WAKE_ADDRESS);
  1849. return -EIO;
  1850. }
  1851. iowrite32(PCIE_SOC_WAKE_RESET,
  1852. ar_pci->mem + PCIE_LOCAL_BASE_ADDRESS +
  1853. PCIE_SOC_WAKE_ADDRESS);
  1854. return 0;
  1855. }
  1856. static void ath10k_pci_device_reset(struct ath10k *ar)
  1857. {
  1858. struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
  1859. void __iomem *mem = ar_pci->mem;
  1860. int i;
  1861. u32 val;
  1862. if (!SOC_GLOBAL_RESET_ADDRESS)
  1863. return;
  1864. if (!mem)
  1865. return;
  1866. ath10k_pci_reg_write32(mem, PCIE_SOC_WAKE_ADDRESS,
  1867. PCIE_SOC_WAKE_V_MASK);
  1868. for (i = 0; i < ATH_PCI_RESET_WAIT_MAX; i++) {
  1869. if (ath10k_pci_target_is_awake(ar))
  1870. break;
  1871. msleep(1);
  1872. }
  1873. /* Put Target, including PCIe, into RESET. */
  1874. val = ath10k_pci_reg_read32(mem, SOC_GLOBAL_RESET_ADDRESS);
  1875. val |= 1;
  1876. ath10k_pci_reg_write32(mem, SOC_GLOBAL_RESET_ADDRESS, val);
  1877. for (i = 0; i < ATH_PCI_RESET_WAIT_MAX; i++) {
  1878. if (ath10k_pci_reg_read32(mem, RTC_STATE_ADDRESS) &
  1879. RTC_STATE_COLD_RESET_MASK)
  1880. break;
  1881. msleep(1);
  1882. }
  1883. /* Pull Target, including PCIe, out of RESET. */
  1884. val &= ~1;
  1885. ath10k_pci_reg_write32(mem, SOC_GLOBAL_RESET_ADDRESS, val);
  1886. for (i = 0; i < ATH_PCI_RESET_WAIT_MAX; i++) {
  1887. if (!(ath10k_pci_reg_read32(mem, RTC_STATE_ADDRESS) &
  1888. RTC_STATE_COLD_RESET_MASK))
  1889. break;
  1890. msleep(1);
  1891. }
  1892. ath10k_pci_reg_write32(mem, PCIE_SOC_WAKE_ADDRESS, PCIE_SOC_WAKE_RESET);
  1893. }
  1894. static void ath10k_pci_dump_features(struct ath10k_pci *ar_pci)
  1895. {
  1896. int i;
  1897. for (i = 0; i < ATH10K_PCI_FEATURE_COUNT; i++) {
  1898. if (!test_bit(i, ar_pci->features))
  1899. continue;
  1900. switch (i) {
  1901. case ATH10K_PCI_FEATURE_MSI_X:
  1902. ath10k_dbg(ATH10K_DBG_PCI, "device supports MSI-X\n");
  1903. break;
  1904. case ATH10K_PCI_FEATURE_SOC_POWER_SAVE:
  1905. ath10k_dbg(ATH10K_DBG_PCI, "QCA98XX SoC power save enabled\n");
  1906. break;
  1907. }
  1908. }
  1909. }
  1910. static int ath10k_pci_probe(struct pci_dev *pdev,
  1911. const struct pci_device_id *pci_dev)
  1912. {
  1913. void __iomem *mem;
  1914. int ret = 0;
  1915. struct ath10k *ar;
  1916. struct ath10k_pci *ar_pci;
  1917. u32 lcr_val;
  1918. ath10k_dbg(ATH10K_DBG_PCI, "%s\n", __func__);
  1919. ar_pci = kzalloc(sizeof(*ar_pci), GFP_KERNEL);
  1920. if (ar_pci == NULL)
  1921. return -ENOMEM;
  1922. ar_pci->pdev = pdev;
  1923. ar_pci->dev = &pdev->dev;
  1924. switch (pci_dev->device) {
  1925. case QCA988X_2_0_DEVICE_ID:
  1926. set_bit(ATH10K_PCI_FEATURE_MSI_X, ar_pci->features);
  1927. break;
  1928. default:
  1929. ret = -ENODEV;
  1930. ath10k_err("Unkown device ID: %d\n", pci_dev->device);
  1931. goto err_ar_pci;
  1932. }
  1933. if (ath10k_target_ps)
  1934. set_bit(ATH10K_PCI_FEATURE_SOC_POWER_SAVE, ar_pci->features);
  1935. ath10k_pci_dump_features(ar_pci);
  1936. ar = ath10k_core_create(ar_pci, ar_pci->dev, &ath10k_pci_hif_ops);
  1937. if (!ar) {
  1938. ath10k_err("ath10k_core_create failed!\n");
  1939. ret = -EINVAL;
  1940. goto err_ar_pci;
  1941. }
  1942. ar_pci->ar = ar;
  1943. ar_pci->fw_indicator_address = FW_INDICATOR_ADDRESS;
  1944. atomic_set(&ar_pci->keep_awake_count, 0);
  1945. pci_set_drvdata(pdev, ar);
  1946. /*
  1947. * Without any knowledge of the Host, the Target may have been reset or
  1948. * power cycled and its Config Space may no longer reflect the PCI
  1949. * address space that was assigned earlier by the PCI infrastructure.
  1950. * Refresh it now.
  1951. */
  1952. ret = pci_assign_resource(pdev, BAR_NUM);
  1953. if (ret) {
  1954. ath10k_err("cannot assign PCI space: %d\n", ret);
  1955. goto err_ar;
  1956. }
  1957. ret = pci_enable_device(pdev);
  1958. if (ret) {
  1959. ath10k_err("cannot enable PCI device: %d\n", ret);
  1960. goto err_ar;
  1961. }
  1962. /* Request MMIO resources */
  1963. ret = pci_request_region(pdev, BAR_NUM, "ath");
  1964. if (ret) {
  1965. ath10k_err("PCI MMIO reservation error: %d\n", ret);
  1966. goto err_device;
  1967. }
  1968. /*
  1969. * Target structures have a limit of 32 bit DMA pointers.
  1970. * DMA pointers can be wider than 32 bits by default on some systems.
  1971. */
  1972. ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  1973. if (ret) {
  1974. ath10k_err("32-bit DMA not available: %d\n", ret);
  1975. goto err_region;
  1976. }
  1977. ret = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  1978. if (ret) {
  1979. ath10k_err("cannot enable 32-bit consistent DMA\n");
  1980. goto err_region;
  1981. }
  1982. /* Set bus master bit in PCI_COMMAND to enable DMA */
  1983. pci_set_master(pdev);
  1984. /*
  1985. * Temporary FIX: disable ASPM
  1986. * Will be removed after the OTP is programmed
  1987. */
  1988. pci_read_config_dword(pdev, 0x80, &lcr_val);
  1989. pci_write_config_dword(pdev, 0x80, (lcr_val & 0xffffff00));
  1990. /* Arrange for access to Target SoC registers. */
  1991. mem = pci_iomap(pdev, BAR_NUM, 0);
  1992. if (!mem) {
  1993. ath10k_err("PCI iomap error\n");
  1994. ret = -EIO;
  1995. goto err_master;
  1996. }
  1997. ar_pci->mem = mem;
  1998. spin_lock_init(&ar_pci->ce_lock);
  1999. ar_pci->cacheline_sz = dma_get_cache_alignment();
  2000. ret = ath10k_core_register(ar);
  2001. if (ret) {
  2002. ath10k_err("could not register driver core (%d)\n", ret);
  2003. goto err_iomap;
  2004. }
  2005. return 0;
  2006. err_iomap:
  2007. pci_iounmap(pdev, mem);
  2008. err_master:
  2009. pci_clear_master(pdev);
  2010. err_region:
  2011. pci_release_region(pdev, BAR_NUM);
  2012. err_device:
  2013. pci_disable_device(pdev);
  2014. err_ar:
  2015. pci_set_drvdata(pdev, NULL);
  2016. ath10k_core_destroy(ar);
  2017. err_ar_pci:
  2018. /* call HIF PCI free here */
  2019. kfree(ar_pci);
  2020. return ret;
  2021. }
  2022. static void ath10k_pci_remove(struct pci_dev *pdev)
  2023. {
  2024. struct ath10k *ar = pci_get_drvdata(pdev);
  2025. struct ath10k_pci *ar_pci;
  2026. ath10k_dbg(ATH10K_DBG_PCI, "%s\n", __func__);
  2027. if (!ar)
  2028. return;
  2029. ar_pci = ath10k_pci_priv(ar);
  2030. if (!ar_pci)
  2031. return;
  2032. tasklet_kill(&ar_pci->msi_fw_err);
  2033. ath10k_core_unregister(ar);
  2034. pci_set_drvdata(pdev, NULL);
  2035. pci_iounmap(pdev, ar_pci->mem);
  2036. pci_release_region(pdev, BAR_NUM);
  2037. pci_clear_master(pdev);
  2038. pci_disable_device(pdev);
  2039. ath10k_core_destroy(ar);
  2040. kfree(ar_pci);
  2041. }
  2042. MODULE_DEVICE_TABLE(pci, ath10k_pci_id_table);
  2043. static struct pci_driver ath10k_pci_driver = {
  2044. .name = "ath10k_pci",
  2045. .id_table = ath10k_pci_id_table,
  2046. .probe = ath10k_pci_probe,
  2047. .remove = ath10k_pci_remove,
  2048. };
  2049. static int __init ath10k_pci_init(void)
  2050. {
  2051. int ret;
  2052. ret = pci_register_driver(&ath10k_pci_driver);
  2053. if (ret)
  2054. ath10k_err("pci_register_driver failed [%d]\n", ret);
  2055. return ret;
  2056. }
  2057. module_init(ath10k_pci_init);
  2058. static void __exit ath10k_pci_exit(void)
  2059. {
  2060. pci_unregister_driver(&ath10k_pci_driver);
  2061. }
  2062. module_exit(ath10k_pci_exit);
  2063. MODULE_AUTHOR("Qualcomm Atheros");
  2064. MODULE_DESCRIPTION("Driver support for Atheros QCA988X PCIe devices");
  2065. MODULE_LICENSE("Dual BSD/GPL");
  2066. MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" QCA988X_HW_2_0_FW_FILE);
  2067. MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" QCA988X_HW_2_0_OTP_FILE);
  2068. MODULE_FIRMWARE(QCA988X_HW_2_0_FW_DIR "/" QCA988X_HW_2_0_BOARD_DATA_FILE);