pci.c 61 KB

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