pci.c 96 KB

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