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