pci.c 80 KB

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