pci.c 61 KB

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