pci.c 61 KB

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