pci.c 67 KB

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