main.c 114 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2016-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2021-2022 Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #include <linux/delay.h>
  7. #include <linux/devcoredump.h>
  8. #include <linux/elf.h>
  9. #include <linux/jiffies.h>
  10. #include <linux/module.h>
  11. #include <linux/of.h>
  12. #include <linux/of_device.h>
  13. #include <linux/of_gpio.h>
  14. #include <linux/pm_wakeup.h>
  15. #include <linux/reboot.h>
  16. #include <linux/rwsem.h>
  17. #include <linux/suspend.h>
  18. #include <linux/timer.h>
  19. #include <linux/version.h>
  20. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 14, 0))
  21. #include <linux/panic_notifier.h>
  22. #endif
  23. #if IS_ENABLED(CONFIG_QCOM_MINIDUMP)
  24. #include <soc/qcom/minidump.h>
  25. #endif
  26. #include "cnss_plat_ipc_qmi.h"
  27. #include "cnss_utils.h"
  28. #include "main.h"
  29. #include "bus.h"
  30. #include "debug.h"
  31. #include "genl.h"
  32. #include "reg.h"
  33. #ifdef CONFIG_CNSS_HW_SECURE_DISABLE
  34. #include "smcinvoke.h"
  35. #include "smcinvoke_object.h"
  36. #include "IClientEnv.h"
  37. #define HW_STATE_UID 0x108
  38. #define HW_OP_GET_STATE 1
  39. #define HW_WIFI_UID 0x508
  40. #define FEATURE_NOT_SUPPORTED 12
  41. #define PERIPHERAL_NOT_FOUND 10
  42. #endif
  43. #define CNSS_DUMP_FORMAT_VER 0x11
  44. #define CNSS_DUMP_FORMAT_VER_V2 0x22
  45. #define CNSS_DUMP_MAGIC_VER_V2 0x42445953
  46. #define CNSS_DUMP_NAME "CNSS_WLAN"
  47. #define CNSS_DUMP_DESC_SIZE 0x1000
  48. #define CNSS_DUMP_SEG_VER 0x1
  49. #define FILE_SYSTEM_READY 1
  50. #define FW_READY_TIMEOUT 20000
  51. #define FW_ASSERT_TIMEOUT 5000
  52. #define CNSS_EVENT_PENDING 2989
  53. #define POWER_RESET_MIN_DELAY_MS 100
  54. #define CNSS_QUIRKS_DEFAULT 0
  55. #ifdef CONFIG_CNSS_EMULATION
  56. #define CNSS_MHI_TIMEOUT_DEFAULT 90000
  57. #define CNSS_MHI_M2_TIMEOUT_DEFAULT 2000
  58. #define CNSS_QMI_TIMEOUT_DEFAULT 90000
  59. #else
  60. #define CNSS_MHI_TIMEOUT_DEFAULT 0
  61. #define CNSS_MHI_M2_TIMEOUT_DEFAULT 25
  62. #define CNSS_QMI_TIMEOUT_DEFAULT 10000
  63. #endif
  64. #define CNSS_BDF_TYPE_DEFAULT CNSS_BDF_ELF
  65. #define CNSS_TIME_SYNC_PERIOD_DEFAULT 900000
  66. #define CNSS_MIN_TIME_SYNC_PERIOD 2000
  67. #define CNSS_DMS_QMI_CONNECTION_WAIT_MS 50
  68. #define CNSS_DMS_QMI_CONNECTION_WAIT_RETRY 200
  69. #define CNSS_DAEMON_CONNECT_TIMEOUT_MS 30000
  70. #define CNSS_CAL_DB_FILE_NAME "wlfw_cal_db.bin"
  71. #define CNSS_CAL_START_PROBE_WAIT_RETRY_MAX 100
  72. #define CNSS_CAL_START_PROBE_WAIT_MS 500
  73. enum cnss_cal_db_op {
  74. CNSS_CAL_DB_UPLOAD,
  75. CNSS_CAL_DB_DOWNLOAD,
  76. CNSS_CAL_DB_INVALID_OP,
  77. };
  78. enum cnss_recovery_type {
  79. CNSS_WLAN_RECOVERY = 0x1,
  80. CNSS_PCSS_RECOVERY = 0x2,
  81. };
  82. static struct cnss_plat_data *plat_env;
  83. static bool cnss_allow_driver_loading;
  84. static struct cnss_fw_files FW_FILES_QCA6174_FW_3_0 = {
  85. "qwlan30.bin", "bdwlan30.bin", "otp30.bin", "utf30.bin",
  86. "utfbd30.bin", "epping30.bin", "evicted30.bin"
  87. };
  88. static struct cnss_fw_files FW_FILES_DEFAULT = {
  89. "qwlan.bin", "bdwlan.bin", "otp.bin", "utf.bin",
  90. "utfbd.bin", "epping.bin", "evicted.bin"
  91. };
  92. struct cnss_driver_event {
  93. struct list_head list;
  94. enum cnss_driver_event_type type;
  95. bool sync;
  96. struct completion complete;
  97. int ret;
  98. void *data;
  99. };
  100. static void cnss_set_plat_priv(struct platform_device *plat_dev,
  101. struct cnss_plat_data *plat_priv)
  102. {
  103. plat_env = plat_priv;
  104. }
  105. bool cnss_check_driver_loading_allowed(void)
  106. {
  107. return cnss_allow_driver_loading;
  108. }
  109. struct cnss_plat_data *cnss_get_plat_priv(struct platform_device *plat_dev)
  110. {
  111. return plat_env;
  112. }
  113. /**
  114. * cnss_get_mem_seg_count - Get segment count of memory
  115. * @type: memory type
  116. * @seg: segment count
  117. *
  118. * Return: 0 on success, negative value on failure
  119. */
  120. int cnss_get_mem_seg_count(enum cnss_remote_mem_type type, u32 *seg)
  121. {
  122. struct cnss_plat_data *plat_priv;
  123. plat_priv = cnss_get_plat_priv(NULL);
  124. if (!plat_priv)
  125. return -ENODEV;
  126. switch (type) {
  127. case CNSS_REMOTE_MEM_TYPE_FW:
  128. *seg = plat_priv->fw_mem_seg_len;
  129. break;
  130. case CNSS_REMOTE_MEM_TYPE_QDSS:
  131. *seg = plat_priv->qdss_mem_seg_len;
  132. break;
  133. default:
  134. return -EINVAL;
  135. }
  136. return 0;
  137. }
  138. EXPORT_SYMBOL(cnss_get_mem_seg_count);
  139. /**
  140. * cnss_get_wifi_kobject -return wifi kobject
  141. * Return: Null, to maintain driver comnpatibilty
  142. */
  143. struct kobject *cnss_get_wifi_kobj(struct device *dev)
  144. {
  145. struct cnss_plat_data *plat_priv;
  146. plat_priv = cnss_get_plat_priv(NULL);
  147. if (!plat_priv)
  148. return NULL;
  149. return plat_priv->wifi_kobj;
  150. }
  151. EXPORT_SYMBOL(cnss_get_wifi_kobj);
  152. /**
  153. * cnss_get_mem_segment_info - Get memory info of different type
  154. * @type: memory type
  155. * @segment: array to save the segment info
  156. * @seg: segment count
  157. *
  158. * Return: 0 on success, negative value on failure
  159. */
  160. int cnss_get_mem_segment_info(enum cnss_remote_mem_type type,
  161. struct cnss_mem_segment segment[],
  162. u32 segment_count)
  163. {
  164. struct cnss_plat_data *plat_priv;
  165. u32 i;
  166. plat_priv = cnss_get_plat_priv(NULL);
  167. if (!plat_priv)
  168. return -ENODEV;
  169. switch (type) {
  170. case CNSS_REMOTE_MEM_TYPE_FW:
  171. if (segment_count > plat_priv->fw_mem_seg_len)
  172. segment_count = plat_priv->fw_mem_seg_len;
  173. for (i = 0; i < segment_count; i++) {
  174. segment[i].size = plat_priv->fw_mem[i].size;
  175. segment[i].va = plat_priv->fw_mem[i].va;
  176. segment[i].pa = plat_priv->fw_mem[i].pa;
  177. }
  178. break;
  179. case CNSS_REMOTE_MEM_TYPE_QDSS:
  180. if (segment_count > plat_priv->qdss_mem_seg_len)
  181. segment_count = plat_priv->qdss_mem_seg_len;
  182. for (i = 0; i < segment_count; i++) {
  183. segment[i].size = plat_priv->qdss_mem[i].size;
  184. segment[i].va = plat_priv->qdss_mem[i].va;
  185. segment[i].pa = plat_priv->qdss_mem[i].pa;
  186. }
  187. break;
  188. default:
  189. return -EINVAL;
  190. }
  191. return 0;
  192. }
  193. EXPORT_SYMBOL(cnss_get_mem_segment_info);
  194. static int cnss_get_audio_iommu_domain(struct cnss_plat_data *plat_priv)
  195. {
  196. struct device_node *audio_ion_node;
  197. struct platform_device *audio_ion_pdev;
  198. audio_ion_node = of_find_compatible_node(NULL, NULL,
  199. "qcom,msm-audio-ion");
  200. if (!audio_ion_node) {
  201. cnss_pr_err("Unable to get Audio ion node");
  202. return -EINVAL;
  203. }
  204. audio_ion_pdev = of_find_device_by_node(audio_ion_node);
  205. of_node_put(audio_ion_node);
  206. if (!audio_ion_pdev) {
  207. cnss_pr_err("Unable to get Audio ion platform device");
  208. return -EINVAL;
  209. }
  210. plat_priv->audio_iommu_domain =
  211. iommu_get_domain_for_dev(&audio_ion_pdev->dev);
  212. put_device(&audio_ion_pdev->dev);
  213. if (!plat_priv->audio_iommu_domain) {
  214. cnss_pr_err("Unable to get Audio ion iommu domain");
  215. return -EINVAL;
  216. }
  217. return 0;
  218. }
  219. int cnss_set_feature_list(struct cnss_plat_data *plat_priv,
  220. enum cnss_feature_v01 feature)
  221. {
  222. if (unlikely(!plat_priv || feature >= CNSS_MAX_FEATURE_V01))
  223. return -EINVAL;
  224. plat_priv->feature_list |= 1 << feature;
  225. return 0;
  226. }
  227. int cnss_clear_feature_list(struct cnss_plat_data *plat_priv,
  228. enum cnss_feature_v01 feature)
  229. {
  230. if (unlikely(!plat_priv || feature >= CNSS_MAX_FEATURE_V01))
  231. return -EINVAL;
  232. plat_priv->feature_list &= ~(1 << feature);
  233. return 0;
  234. }
  235. int cnss_get_feature_list(struct cnss_plat_data *plat_priv,
  236. u64 *feature_list)
  237. {
  238. if (unlikely(!plat_priv))
  239. return -EINVAL;
  240. *feature_list = plat_priv->feature_list;
  241. return 0;
  242. }
  243. void cnss_pm_stay_awake(struct cnss_plat_data *plat_priv)
  244. {
  245. if (atomic_inc_return(&plat_priv->pm_count) != 1)
  246. return;
  247. cnss_pr_dbg("PM stay awake, state: 0x%lx, count: %d\n",
  248. plat_priv->driver_state,
  249. atomic_read(&plat_priv->pm_count));
  250. pm_stay_awake(&plat_priv->plat_dev->dev);
  251. }
  252. void cnss_pm_relax(struct cnss_plat_data *plat_priv)
  253. {
  254. int r = atomic_dec_return(&plat_priv->pm_count);
  255. WARN_ON(r < 0);
  256. if (r != 0)
  257. return;
  258. cnss_pr_dbg("PM relax, state: 0x%lx, count: %d\n",
  259. plat_priv->driver_state,
  260. atomic_read(&plat_priv->pm_count));
  261. pm_relax(&plat_priv->plat_dev->dev);
  262. }
  263. int cnss_get_fw_files_for_target(struct device *dev,
  264. struct cnss_fw_files *pfw_files,
  265. u32 target_type, u32 target_version)
  266. {
  267. if (!pfw_files)
  268. return -ENODEV;
  269. switch (target_version) {
  270. case QCA6174_REV3_VERSION:
  271. case QCA6174_REV3_2_VERSION:
  272. memcpy(pfw_files, &FW_FILES_QCA6174_FW_3_0, sizeof(*pfw_files));
  273. break;
  274. default:
  275. memcpy(pfw_files, &FW_FILES_DEFAULT, sizeof(*pfw_files));
  276. cnss_pr_err("Unknown target version, type: 0x%X, version: 0x%X",
  277. target_type, target_version);
  278. break;
  279. }
  280. return 0;
  281. }
  282. EXPORT_SYMBOL(cnss_get_fw_files_for_target);
  283. int cnss_get_platform_cap(struct device *dev, struct cnss_platform_cap *cap)
  284. {
  285. struct cnss_plat_data *plat_priv = cnss_bus_dev_to_plat_priv(dev);
  286. if (!plat_priv)
  287. return -ENODEV;
  288. if (!cap)
  289. return -EINVAL;
  290. *cap = plat_priv->cap;
  291. cnss_pr_dbg("Platform cap_flag is 0x%x\n", cap->cap_flag);
  292. return 0;
  293. }
  294. EXPORT_SYMBOL(cnss_get_platform_cap);
  295. /**
  296. * cnss_get_fw_cap - Check whether FW supports specific capability or not
  297. * @dev: Device
  298. * @fw_cap: FW Capability which needs to be checked
  299. *
  300. * Return: TRUE if supported, FALSE on failure or if not supported
  301. */
  302. bool cnss_get_fw_cap(struct device *dev, enum cnss_fw_caps fw_cap)
  303. {
  304. struct cnss_plat_data *plat_priv = cnss_bus_dev_to_plat_priv(dev);
  305. bool is_supported = false;
  306. if (!plat_priv)
  307. return is_supported;
  308. if (!plat_priv->fw_caps)
  309. return is_supported;
  310. switch (fw_cap) {
  311. case CNSS_FW_CAP_DIRECT_LINK_SUPPORT:
  312. is_supported = !!(plat_priv->fw_caps &
  313. QMI_WLFW_DIRECT_LINK_SUPPORT_V01);
  314. if (is_supported && cnss_get_audio_iommu_domain(plat_priv))
  315. is_supported = false;
  316. break;
  317. default:
  318. cnss_pr_err("Invalid FW Capability: 0x%x\n", fw_cap);
  319. }
  320. cnss_pr_dbg("FW Capability 0x%x is %s\n", fw_cap,
  321. is_supported ? "supported" : "not supported");
  322. return is_supported;
  323. }
  324. EXPORT_SYMBOL(cnss_get_fw_cap);
  325. void cnss_request_pm_qos(struct device *dev, u32 qos_val)
  326. {
  327. struct cnss_plat_data *plat_priv = cnss_bus_dev_to_plat_priv(dev);
  328. if (!plat_priv)
  329. return;
  330. cpu_latency_qos_add_request(&plat_priv->qos_request, qos_val);
  331. }
  332. EXPORT_SYMBOL(cnss_request_pm_qos);
  333. void cnss_remove_pm_qos(struct device *dev)
  334. {
  335. struct cnss_plat_data *plat_priv = cnss_bus_dev_to_plat_priv(dev);
  336. if (!plat_priv)
  337. return;
  338. cpu_latency_qos_remove_request(&plat_priv->qos_request);
  339. }
  340. EXPORT_SYMBOL(cnss_remove_pm_qos);
  341. int cnss_wlan_enable(struct device *dev,
  342. struct cnss_wlan_enable_cfg *config,
  343. enum cnss_driver_mode mode,
  344. const char *host_version)
  345. {
  346. int ret = 0;
  347. struct cnss_plat_data *plat_priv;
  348. if (!dev) {
  349. cnss_pr_err("Invalid dev pointer\n");
  350. return -EINVAL;
  351. }
  352. plat_priv = cnss_bus_dev_to_plat_priv(dev);
  353. if (!plat_priv)
  354. return -ENODEV;
  355. if (plat_priv->device_id == QCA6174_DEVICE_ID)
  356. return 0;
  357. if (test_bit(QMI_BYPASS, &plat_priv->ctrl_params.quirks))
  358. return 0;
  359. if (!config || !host_version) {
  360. cnss_pr_err("Invalid config or host_version pointer\n");
  361. return -EINVAL;
  362. }
  363. cnss_pr_dbg("Mode: %d, config: %pK, host_version: %s\n",
  364. mode, config, host_version);
  365. if (mode == CNSS_WALTEST || mode == CNSS_CCPM)
  366. goto skip_cfg;
  367. ret = cnss_wlfw_wlan_cfg_send_sync(plat_priv, config, host_version);
  368. if (ret)
  369. goto out;
  370. skip_cfg:
  371. ret = cnss_wlfw_wlan_mode_send_sync(plat_priv, mode);
  372. out:
  373. return ret;
  374. }
  375. EXPORT_SYMBOL(cnss_wlan_enable);
  376. int cnss_wlan_disable(struct device *dev, enum cnss_driver_mode mode)
  377. {
  378. int ret = 0;
  379. struct cnss_plat_data *plat_priv;
  380. if (!dev) {
  381. cnss_pr_err("Invalid dev pointer\n");
  382. return -EINVAL;
  383. }
  384. plat_priv = cnss_bus_dev_to_plat_priv(dev);
  385. if (!plat_priv)
  386. return -ENODEV;
  387. if (plat_priv->device_id == QCA6174_DEVICE_ID)
  388. return 0;
  389. if (test_bit(QMI_BYPASS, &plat_priv->ctrl_params.quirks))
  390. return 0;
  391. ret = cnss_wlfw_wlan_mode_send_sync(plat_priv, CNSS_OFF);
  392. cnss_bus_free_qdss_mem(plat_priv);
  393. return ret;
  394. }
  395. EXPORT_SYMBOL(cnss_wlan_disable);
  396. int cnss_audio_smmu_map(struct device *dev, phys_addr_t paddr,
  397. dma_addr_t iova, size_t size)
  398. {
  399. struct cnss_plat_data *plat_priv = cnss_bus_dev_to_plat_priv(dev);
  400. uint32_t page_offset;
  401. if (!plat_priv)
  402. return -ENODEV;
  403. if (!plat_priv->audio_iommu_domain)
  404. return -EINVAL;
  405. page_offset = iova & (PAGE_SIZE - 1);
  406. if (page_offset + size > PAGE_SIZE)
  407. size += PAGE_SIZE;
  408. iova -= page_offset;
  409. paddr -= page_offset;
  410. return iommu_map(plat_priv->audio_iommu_domain, iova, paddr,
  411. roundup(size, PAGE_SIZE), IOMMU_READ | IOMMU_WRITE);
  412. }
  413. EXPORT_SYMBOL(cnss_audio_smmu_map);
  414. void cnss_audio_smmu_unmap(struct device *dev, dma_addr_t iova, size_t size)
  415. {
  416. struct cnss_plat_data *plat_priv = cnss_bus_dev_to_plat_priv(dev);
  417. uint32_t page_offset;
  418. if (!plat_priv)
  419. return;
  420. if (!plat_priv->audio_iommu_domain)
  421. return;
  422. page_offset = iova & (PAGE_SIZE - 1);
  423. if (page_offset + size > PAGE_SIZE)
  424. size += PAGE_SIZE;
  425. iova -= page_offset;
  426. iommu_unmap(plat_priv->audio_iommu_domain, iova,
  427. roundup(size, PAGE_SIZE));
  428. }
  429. EXPORT_SYMBOL(cnss_audio_smmu_unmap);
  430. int cnss_athdiag_read(struct device *dev, u32 offset, u32 mem_type,
  431. u32 data_len, u8 *output)
  432. {
  433. struct cnss_plat_data *plat_priv = cnss_bus_dev_to_plat_priv(dev);
  434. int ret = 0;
  435. if (!plat_priv) {
  436. cnss_pr_err("plat_priv is NULL!\n");
  437. return -EINVAL;
  438. }
  439. if (plat_priv->device_id == QCA6174_DEVICE_ID)
  440. return 0;
  441. if (!test_bit(CNSS_FW_READY, &plat_priv->driver_state)) {
  442. cnss_pr_err("Invalid state for athdiag read: 0x%lx\n",
  443. plat_priv->driver_state);
  444. ret = -EINVAL;
  445. goto out;
  446. }
  447. ret = cnss_wlfw_athdiag_read_send_sync(plat_priv, offset, mem_type,
  448. data_len, output);
  449. out:
  450. return ret;
  451. }
  452. EXPORT_SYMBOL(cnss_athdiag_read);
  453. int cnss_athdiag_write(struct device *dev, u32 offset, u32 mem_type,
  454. u32 data_len, u8 *input)
  455. {
  456. struct cnss_plat_data *plat_priv = cnss_bus_dev_to_plat_priv(dev);
  457. int ret = 0;
  458. if (!plat_priv) {
  459. cnss_pr_err("plat_priv is NULL!\n");
  460. return -EINVAL;
  461. }
  462. if (plat_priv->device_id == QCA6174_DEVICE_ID)
  463. return 0;
  464. if (!test_bit(CNSS_FW_READY, &plat_priv->driver_state)) {
  465. cnss_pr_err("Invalid state for athdiag write: 0x%lx\n",
  466. plat_priv->driver_state);
  467. ret = -EINVAL;
  468. goto out;
  469. }
  470. ret = cnss_wlfw_athdiag_write_send_sync(plat_priv, offset, mem_type,
  471. data_len, input);
  472. out:
  473. return ret;
  474. }
  475. EXPORT_SYMBOL(cnss_athdiag_write);
  476. int cnss_set_fw_log_mode(struct device *dev, u8 fw_log_mode)
  477. {
  478. struct cnss_plat_data *plat_priv;
  479. if (!dev) {
  480. cnss_pr_err("Invalid dev pointer\n");
  481. return -EINVAL;
  482. }
  483. plat_priv = cnss_bus_dev_to_plat_priv(dev);
  484. if (!plat_priv)
  485. return -ENODEV;
  486. if (plat_priv->device_id == QCA6174_DEVICE_ID)
  487. return 0;
  488. return cnss_wlfw_ini_send_sync(plat_priv, fw_log_mode);
  489. }
  490. EXPORT_SYMBOL(cnss_set_fw_log_mode);
  491. int cnss_set_pcie_gen_speed(struct device *dev, u8 pcie_gen_speed)
  492. {
  493. struct cnss_plat_data *plat_priv = cnss_bus_dev_to_plat_priv(dev);
  494. if (!plat_priv)
  495. return -EINVAL;
  496. if (!plat_priv->fw_pcie_gen_switch) {
  497. cnss_pr_err("Firmware does not support PCIe gen switch\n");
  498. return -EOPNOTSUPP;
  499. }
  500. if (pcie_gen_speed < QMI_PCIE_GEN_SPEED_1_V01 ||
  501. pcie_gen_speed > QMI_PCIE_GEN_SPEED_3_V01)
  502. return -EINVAL;
  503. cnss_pr_dbg("WLAN provided PCIE gen speed: %d\n", pcie_gen_speed);
  504. plat_priv->pcie_gen_speed = pcie_gen_speed;
  505. return 0;
  506. }
  507. EXPORT_SYMBOL(cnss_set_pcie_gen_speed);
  508. static int cnss_fw_mem_ready_hdlr(struct cnss_plat_data *plat_priv)
  509. {
  510. int ret = 0;
  511. if (!plat_priv)
  512. return -ENODEV;
  513. set_bit(CNSS_FW_MEM_READY, &plat_priv->driver_state);
  514. ret = cnss_wlfw_tgt_cap_send_sync(plat_priv);
  515. if (ret)
  516. goto out;
  517. if (plat_priv->hds_enabled)
  518. cnss_wlfw_bdf_dnld_send_sync(plat_priv, CNSS_BDF_HDS);
  519. cnss_wlfw_bdf_dnld_send_sync(plat_priv, CNSS_BDF_REGDB);
  520. cnss_wlfw_ini_file_send_sync(plat_priv, WLFW_CONN_ROAM_INI_V01);
  521. ret = cnss_wlfw_bdf_dnld_send_sync(plat_priv,
  522. plat_priv->ctrl_params.bdf_type);
  523. if (ret)
  524. goto out;
  525. ret = cnss_bus_load_m3(plat_priv);
  526. if (ret)
  527. goto out;
  528. ret = cnss_wlfw_m3_dnld_send_sync(plat_priv);
  529. if (ret)
  530. goto out;
  531. cnss_wlfw_qdss_dnld_send_sync(plat_priv);
  532. return 0;
  533. out:
  534. return ret;
  535. }
  536. static int cnss_request_antenna_sharing(struct cnss_plat_data *plat_priv)
  537. {
  538. int ret = 0;
  539. if (!plat_priv->antenna) {
  540. ret = cnss_wlfw_antenna_switch_send_sync(plat_priv);
  541. if (ret)
  542. goto out;
  543. }
  544. if (test_bit(CNSS_COEX_CONNECTED, &plat_priv->driver_state)) {
  545. ret = coex_antenna_switch_to_wlan_send_sync_msg(plat_priv);
  546. if (ret)
  547. goto out;
  548. }
  549. ret = cnss_wlfw_antenna_grant_send_sync(plat_priv);
  550. if (ret)
  551. goto out;
  552. return 0;
  553. out:
  554. return ret;
  555. }
  556. static void cnss_release_antenna_sharing(struct cnss_plat_data *plat_priv)
  557. {
  558. if (test_bit(CNSS_COEX_CONNECTED, &plat_priv->driver_state))
  559. coex_antenna_switch_to_mdm_send_sync_msg(plat_priv);
  560. }
  561. static int cnss_setup_dms_mac(struct cnss_plat_data *plat_priv)
  562. {
  563. u32 i;
  564. int ret = 0;
  565. struct cnss_plat_ipc_daemon_config *cfg;
  566. ret = cnss_qmi_get_dms_mac(plat_priv);
  567. if (ret == 0 && plat_priv->dms.mac_valid)
  568. goto qmi_send;
  569. /* DTSI property use-nv-mac is used to force DMS MAC address for WLAN.
  570. * Thus assert on failure to get MAC from DMS even after retries
  571. */
  572. if (plat_priv->use_nv_mac) {
  573. /* Check if Daemon says platform support DMS MAC provisioning */
  574. cfg = cnss_plat_ipc_qmi_daemon_config();
  575. if (cfg) {
  576. if (!cfg->dms_mac_addr_supported) {
  577. cnss_pr_err("DMS MAC address not supported\n");
  578. CNSS_ASSERT(0);
  579. return -EINVAL;
  580. }
  581. }
  582. for (i = 0; i < CNSS_DMS_QMI_CONNECTION_WAIT_RETRY; i++) {
  583. if (plat_priv->dms.mac_valid)
  584. break;
  585. ret = cnss_qmi_get_dms_mac(plat_priv);
  586. if (ret == 0)
  587. break;
  588. msleep(CNSS_DMS_QMI_CONNECTION_WAIT_MS);
  589. }
  590. if (!plat_priv->dms.mac_valid) {
  591. cnss_pr_err("Unable to get MAC from DMS after retries\n");
  592. CNSS_ASSERT(0);
  593. return -EINVAL;
  594. }
  595. }
  596. qmi_send:
  597. if (plat_priv->dms.mac_valid)
  598. ret =
  599. cnss_wlfw_wlan_mac_req_send_sync(plat_priv, plat_priv->dms.mac,
  600. ARRAY_SIZE(plat_priv->dms.mac));
  601. return ret;
  602. }
  603. static int cnss_cal_db_mem_update(struct cnss_plat_data *plat_priv,
  604. enum cnss_cal_db_op op, u32 *size)
  605. {
  606. int ret = 0;
  607. u32 timeout = cnss_get_timeout(plat_priv,
  608. CNSS_TIMEOUT_DAEMON_CONNECTION);
  609. enum cnss_plat_ipc_qmi_client_id_v01 client_id =
  610. CNSS_PLAT_IPC_DAEMON_QMI_CLIENT_V01;
  611. if (op >= CNSS_CAL_DB_INVALID_OP)
  612. return -EINVAL;
  613. if (!plat_priv->cbc_file_download) {
  614. cnss_pr_info("CAL DB file not required as per BDF\n");
  615. return 0;
  616. }
  617. if (*size == 0) {
  618. cnss_pr_err("Invalid cal file size\n");
  619. return -EINVAL;
  620. }
  621. if (!test_bit(CNSS_DAEMON_CONNECTED, &plat_priv->driver_state)) {
  622. cnss_pr_info("Waiting for CNSS Daemon connection\n");
  623. ret = wait_for_completion_timeout(&plat_priv->daemon_connected,
  624. msecs_to_jiffies(timeout));
  625. if (!ret) {
  626. cnss_pr_err("Daemon not yet connected\n");
  627. CNSS_ASSERT(0);
  628. return ret;
  629. }
  630. }
  631. if (!plat_priv->cal_mem->va) {
  632. cnss_pr_err("CAL DB Memory not setup for FW\n");
  633. return -EINVAL;
  634. }
  635. /* Copy CAL DB file contents to/from CAL_TYPE_DDR mem allocated to FW */
  636. if (op == CNSS_CAL_DB_DOWNLOAD) {
  637. cnss_pr_dbg("Initiating Calibration file download to mem\n");
  638. ret = cnss_plat_ipc_qmi_file_download(client_id,
  639. CNSS_CAL_DB_FILE_NAME,
  640. plat_priv->cal_mem->va,
  641. size);
  642. } else {
  643. cnss_pr_dbg("Initiating Calibration mem upload to file\n");
  644. ret = cnss_plat_ipc_qmi_file_upload(client_id,
  645. CNSS_CAL_DB_FILE_NAME,
  646. plat_priv->cal_mem->va,
  647. *size);
  648. }
  649. if (ret)
  650. cnss_pr_err("Cal DB file %s %s failure\n",
  651. CNSS_CAL_DB_FILE_NAME,
  652. op == CNSS_CAL_DB_DOWNLOAD ? "download" : "upload");
  653. else
  654. cnss_pr_dbg("Cal DB file %s %s size %d done\n",
  655. CNSS_CAL_DB_FILE_NAME,
  656. op == CNSS_CAL_DB_DOWNLOAD ? "download" : "upload",
  657. *size);
  658. return ret;
  659. }
  660. static int cnss_cal_mem_upload_to_file(struct cnss_plat_data *plat_priv)
  661. {
  662. if (plat_priv->cal_file_size > plat_priv->cal_mem->size) {
  663. cnss_pr_err("Cal file size is larger than Cal DB Mem size\n");
  664. return -EINVAL;
  665. }
  666. return cnss_cal_db_mem_update(plat_priv, CNSS_CAL_DB_UPLOAD,
  667. &plat_priv->cal_file_size);
  668. }
  669. static int cnss_cal_file_download_to_mem(struct cnss_plat_data *plat_priv,
  670. u32 *cal_file_size)
  671. {
  672. /* To download pass the total size of cal DB mem allocated.
  673. * After cal file is download to mem, its size is updated in
  674. * return pointer
  675. */
  676. *cal_file_size = plat_priv->cal_mem->size;
  677. return cnss_cal_db_mem_update(plat_priv, CNSS_CAL_DB_DOWNLOAD,
  678. cal_file_size);
  679. }
  680. static int cnss_fw_ready_hdlr(struct cnss_plat_data *plat_priv)
  681. {
  682. int ret = 0;
  683. u32 cal_file_size = 0;
  684. if (!plat_priv)
  685. return -ENODEV;
  686. if (test_bit(CNSS_IN_REBOOT, &plat_priv->driver_state)) {
  687. cnss_pr_err("Reboot is in progress, ignore FW ready\n");
  688. return -EINVAL;
  689. }
  690. cnss_pr_dbg("Processing FW Init Done..\n");
  691. del_timer(&plat_priv->fw_boot_timer);
  692. set_bit(CNSS_FW_READY, &plat_priv->driver_state);
  693. clear_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state);
  694. cnss_wlfw_send_pcie_gen_speed_sync(plat_priv);
  695. if (test_bit(CNSS_FW_BOOT_RECOVERY, &plat_priv->driver_state)) {
  696. clear_bit(CNSS_FW_BOOT_RECOVERY, &plat_priv->driver_state);
  697. clear_bit(CNSS_DRIVER_RECOVERY, &plat_priv->driver_state);
  698. }
  699. if (test_bit(ENABLE_WALTEST, &plat_priv->ctrl_params.quirks)) {
  700. ret = cnss_wlfw_wlan_mode_send_sync(plat_priv,
  701. CNSS_WALTEST);
  702. } else if (test_bit(CNSS_IN_COLD_BOOT_CAL, &plat_priv->driver_state)) {
  703. cnss_request_antenna_sharing(plat_priv);
  704. cnss_cal_file_download_to_mem(plat_priv, &cal_file_size);
  705. cnss_wlfw_cal_report_req_send_sync(plat_priv, cal_file_size);
  706. plat_priv->cal_time = jiffies;
  707. ret = cnss_wlfw_wlan_mode_send_sync(plat_priv,
  708. CNSS_CALIBRATION);
  709. } else {
  710. ret = cnss_setup_dms_mac(plat_priv);
  711. ret = cnss_bus_call_driver_probe(plat_priv);
  712. }
  713. if (ret && test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state))
  714. goto out;
  715. else if (ret)
  716. goto shutdown;
  717. cnss_vreg_unvote_type(plat_priv, CNSS_VREG_PRIM);
  718. return 0;
  719. shutdown:
  720. cnss_bus_dev_shutdown(plat_priv);
  721. clear_bit(CNSS_FW_READY, &plat_priv->driver_state);
  722. clear_bit(CNSS_FW_MEM_READY, &plat_priv->driver_state);
  723. out:
  724. return ret;
  725. }
  726. static char *cnss_driver_event_to_str(enum cnss_driver_event_type type)
  727. {
  728. switch (type) {
  729. case CNSS_DRIVER_EVENT_SERVER_ARRIVE:
  730. return "SERVER_ARRIVE";
  731. case CNSS_DRIVER_EVENT_SERVER_EXIT:
  732. return "SERVER_EXIT";
  733. case CNSS_DRIVER_EVENT_REQUEST_MEM:
  734. return "REQUEST_MEM";
  735. case CNSS_DRIVER_EVENT_FW_MEM_READY:
  736. return "FW_MEM_READY";
  737. case CNSS_DRIVER_EVENT_FW_READY:
  738. return "FW_READY";
  739. case CNSS_DRIVER_EVENT_COLD_BOOT_CAL_START:
  740. return "COLD_BOOT_CAL_START";
  741. case CNSS_DRIVER_EVENT_COLD_BOOT_CAL_DONE:
  742. return "COLD_BOOT_CAL_DONE";
  743. case CNSS_DRIVER_EVENT_REGISTER_DRIVER:
  744. return "REGISTER_DRIVER";
  745. case CNSS_DRIVER_EVENT_UNREGISTER_DRIVER:
  746. return "UNREGISTER_DRIVER";
  747. case CNSS_DRIVER_EVENT_RECOVERY:
  748. return "RECOVERY";
  749. case CNSS_DRIVER_EVENT_FORCE_FW_ASSERT:
  750. return "FORCE_FW_ASSERT";
  751. case CNSS_DRIVER_EVENT_POWER_UP:
  752. return "POWER_UP";
  753. case CNSS_DRIVER_EVENT_POWER_DOWN:
  754. return "POWER_DOWN";
  755. case CNSS_DRIVER_EVENT_IDLE_RESTART:
  756. return "IDLE_RESTART";
  757. case CNSS_DRIVER_EVENT_IDLE_SHUTDOWN:
  758. return "IDLE_SHUTDOWN";
  759. case CNSS_DRIVER_EVENT_IMS_WFC_CALL_IND:
  760. return "IMS_WFC_CALL_IND";
  761. case CNSS_DRIVER_EVENT_WLFW_TWT_CFG_IND:
  762. return "WLFW_TWC_CFG_IND";
  763. case CNSS_DRIVER_EVENT_QDSS_TRACE_REQ_MEM:
  764. return "QDSS_TRACE_REQ_MEM";
  765. case CNSS_DRIVER_EVENT_FW_MEM_FILE_SAVE:
  766. return "FW_MEM_FILE_SAVE";
  767. case CNSS_DRIVER_EVENT_QDSS_TRACE_FREE:
  768. return "QDSS_TRACE_FREE";
  769. case CNSS_DRIVER_EVENT_QDSS_TRACE_REQ_DATA:
  770. return "QDSS_TRACE_REQ_DATA";
  771. case CNSS_DRIVER_EVENT_MAX:
  772. return "EVENT_MAX";
  773. }
  774. return "UNKNOWN";
  775. };
  776. int cnss_driver_event_post(struct cnss_plat_data *plat_priv,
  777. enum cnss_driver_event_type type,
  778. u32 flags, void *data)
  779. {
  780. struct cnss_driver_event *event;
  781. unsigned long irq_flags;
  782. int gfp = GFP_KERNEL;
  783. int ret = 0;
  784. if (!plat_priv)
  785. return -ENODEV;
  786. cnss_pr_dbg("Posting event: %s(%d)%s, state: 0x%lx flags: 0x%0x\n",
  787. cnss_driver_event_to_str(type), type,
  788. flags ? "-sync" : "", plat_priv->driver_state, flags);
  789. if (type >= CNSS_DRIVER_EVENT_MAX) {
  790. cnss_pr_err("Invalid Event type: %d, can't post", type);
  791. return -EINVAL;
  792. }
  793. if (in_interrupt() || irqs_disabled())
  794. gfp = GFP_ATOMIC;
  795. event = kzalloc(sizeof(*event), gfp);
  796. if (!event)
  797. return -ENOMEM;
  798. cnss_pm_stay_awake(plat_priv);
  799. event->type = type;
  800. event->data = data;
  801. init_completion(&event->complete);
  802. event->ret = CNSS_EVENT_PENDING;
  803. event->sync = !!(flags & CNSS_EVENT_SYNC);
  804. spin_lock_irqsave(&plat_priv->event_lock, irq_flags);
  805. list_add_tail(&event->list, &plat_priv->event_list);
  806. spin_unlock_irqrestore(&plat_priv->event_lock, irq_flags);
  807. queue_work(plat_priv->event_wq, &plat_priv->event_work);
  808. if (!(flags & CNSS_EVENT_SYNC))
  809. goto out;
  810. if (flags & CNSS_EVENT_UNKILLABLE)
  811. wait_for_completion(&event->complete);
  812. else if (flags & CNSS_EVENT_UNINTERRUPTIBLE)
  813. ret = wait_for_completion_killable(&event->complete);
  814. else
  815. ret = wait_for_completion_interruptible(&event->complete);
  816. cnss_pr_dbg("Completed event: %s(%d), state: 0x%lx, ret: %d/%d\n",
  817. cnss_driver_event_to_str(type), type,
  818. plat_priv->driver_state, ret, event->ret);
  819. spin_lock_irqsave(&plat_priv->event_lock, irq_flags);
  820. if (ret == -ERESTARTSYS && event->ret == CNSS_EVENT_PENDING) {
  821. event->sync = false;
  822. spin_unlock_irqrestore(&plat_priv->event_lock, irq_flags);
  823. ret = -EINTR;
  824. goto out;
  825. }
  826. spin_unlock_irqrestore(&plat_priv->event_lock, irq_flags);
  827. ret = event->ret;
  828. kfree(event);
  829. out:
  830. cnss_pm_relax(plat_priv);
  831. return ret;
  832. }
  833. /**
  834. * cnss_get_timeout - Get timeout for corresponding type.
  835. * @plat_priv: Pointer to platform driver context.
  836. * @cnss_timeout_type: Timeout type.
  837. *
  838. * Return: Timeout in milliseconds.
  839. */
  840. unsigned int cnss_get_timeout(struct cnss_plat_data *plat_priv,
  841. enum cnss_timeout_type timeout_type)
  842. {
  843. unsigned int qmi_timeout = cnss_get_qmi_timeout(plat_priv);
  844. switch (timeout_type) {
  845. case CNSS_TIMEOUT_QMI:
  846. return qmi_timeout;
  847. case CNSS_TIMEOUT_POWER_UP:
  848. return (qmi_timeout << 2);
  849. case CNSS_TIMEOUT_IDLE_RESTART:
  850. /* In idle restart power up sequence, we have fw_boot_timer to
  851. * handle FW initialization failure.
  852. * It uses WLAN_MISSION_MODE_TIMEOUT, so setup 3x that time to
  853. * account for FW dump collection and FW re-initialization on
  854. * retry.
  855. */
  856. return (qmi_timeout + WLAN_MISSION_MODE_TIMEOUT * 3);
  857. case CNSS_TIMEOUT_CALIBRATION:
  858. /* Similar to mission mode, in CBC if FW init fails
  859. * fw recovery is tried. Thus return 2x the CBC timeout.
  860. */
  861. return (qmi_timeout + WLAN_COLD_BOOT_CAL_TIMEOUT * 2);
  862. case CNSS_TIMEOUT_WLAN_WATCHDOG:
  863. return ((qmi_timeout << 1) + WLAN_WD_TIMEOUT_MS);
  864. case CNSS_TIMEOUT_RDDM:
  865. return CNSS_RDDM_TIMEOUT_MS;
  866. case CNSS_TIMEOUT_RECOVERY:
  867. return RECOVERY_TIMEOUT;
  868. case CNSS_TIMEOUT_DAEMON_CONNECTION:
  869. return qmi_timeout + CNSS_DAEMON_CONNECT_TIMEOUT_MS;
  870. default:
  871. return qmi_timeout;
  872. }
  873. }
  874. unsigned int cnss_get_boot_timeout(struct device *dev)
  875. {
  876. struct cnss_plat_data *plat_priv = cnss_bus_dev_to_plat_priv(dev);
  877. if (!plat_priv) {
  878. cnss_pr_err("plat_priv is NULL\n");
  879. return 0;
  880. }
  881. return cnss_get_timeout(plat_priv, CNSS_TIMEOUT_QMI);
  882. }
  883. EXPORT_SYMBOL(cnss_get_boot_timeout);
  884. int cnss_power_up(struct device *dev)
  885. {
  886. int ret = 0;
  887. struct cnss_plat_data *plat_priv = cnss_bus_dev_to_plat_priv(dev);
  888. unsigned int timeout;
  889. if (!plat_priv) {
  890. cnss_pr_err("plat_priv is NULL\n");
  891. return -ENODEV;
  892. }
  893. cnss_pr_dbg("Powering up device\n");
  894. ret = cnss_driver_event_post(plat_priv,
  895. CNSS_DRIVER_EVENT_POWER_UP,
  896. CNSS_EVENT_SYNC, NULL);
  897. if (ret)
  898. goto out;
  899. if (plat_priv->device_id == QCA6174_DEVICE_ID)
  900. goto out;
  901. timeout = cnss_get_timeout(plat_priv, CNSS_TIMEOUT_POWER_UP);
  902. reinit_completion(&plat_priv->power_up_complete);
  903. ret = wait_for_completion_timeout(&plat_priv->power_up_complete,
  904. msecs_to_jiffies(timeout));
  905. if (!ret) {
  906. cnss_pr_err("Timeout (%ums) waiting for power up to complete\n",
  907. timeout);
  908. ret = -EAGAIN;
  909. goto out;
  910. }
  911. return 0;
  912. out:
  913. return ret;
  914. }
  915. EXPORT_SYMBOL(cnss_power_up);
  916. int cnss_power_down(struct device *dev)
  917. {
  918. struct cnss_plat_data *plat_priv = cnss_bus_dev_to_plat_priv(dev);
  919. if (!plat_priv) {
  920. cnss_pr_err("plat_priv is NULL\n");
  921. return -ENODEV;
  922. }
  923. cnss_pr_dbg("Powering down device\n");
  924. return cnss_driver_event_post(plat_priv,
  925. CNSS_DRIVER_EVENT_POWER_DOWN,
  926. CNSS_EVENT_SYNC, NULL);
  927. }
  928. EXPORT_SYMBOL(cnss_power_down);
  929. int cnss_idle_restart(struct device *dev)
  930. {
  931. struct cnss_plat_data *plat_priv = cnss_bus_dev_to_plat_priv(dev);
  932. unsigned int timeout;
  933. int ret = 0;
  934. if (!plat_priv) {
  935. cnss_pr_err("plat_priv is NULL\n");
  936. return -ENODEV;
  937. }
  938. if (!mutex_trylock(&plat_priv->driver_ops_lock)) {
  939. cnss_pr_dbg("Another driver operation is in progress, ignore idle restart\n");
  940. return -EBUSY;
  941. }
  942. cnss_pr_dbg("Doing idle restart\n");
  943. reinit_completion(&plat_priv->power_up_complete);
  944. if (test_bit(CNSS_IN_REBOOT, &plat_priv->driver_state)) {
  945. cnss_pr_dbg("Reboot or shutdown is in progress, ignore idle restart\n");
  946. ret = -EINVAL;
  947. goto out;
  948. }
  949. ret = cnss_driver_event_post(plat_priv,
  950. CNSS_DRIVER_EVENT_IDLE_RESTART,
  951. CNSS_EVENT_SYNC_UNINTERRUPTIBLE, NULL);
  952. if (ret)
  953. goto out;
  954. if (plat_priv->device_id == QCA6174_DEVICE_ID) {
  955. ret = cnss_bus_call_driver_probe(plat_priv);
  956. goto out;
  957. }
  958. timeout = cnss_get_timeout(plat_priv, CNSS_TIMEOUT_IDLE_RESTART);
  959. ret = wait_for_completion_timeout(&plat_priv->power_up_complete,
  960. msecs_to_jiffies(timeout));
  961. if (plat_priv->power_up_error) {
  962. ret = plat_priv->power_up_error;
  963. clear_bit(CNSS_DRIVER_IDLE_RESTART, &plat_priv->driver_state);
  964. cnss_pr_dbg("Power up error:%d, exiting\n",
  965. plat_priv->power_up_error);
  966. goto out;
  967. }
  968. if (!ret) {
  969. /* This exception occurs after attempting retry of FW recovery.
  970. * Thus we can safely power off the device.
  971. */
  972. cnss_fatal_err("Timeout (%ums) waiting for idle restart to complete\n",
  973. timeout);
  974. ret = -ETIMEDOUT;
  975. cnss_power_down(dev);
  976. CNSS_ASSERT(0);
  977. goto out;
  978. }
  979. if (test_bit(CNSS_IN_REBOOT, &plat_priv->driver_state)) {
  980. cnss_pr_dbg("Reboot or shutdown is in progress, ignore idle restart\n");
  981. del_timer(&plat_priv->fw_boot_timer);
  982. ret = -EINVAL;
  983. goto out;
  984. }
  985. /* In non-DRV mode, remove MHI satellite configuration. Switching to
  986. * non-DRV is supported only once after device reboots and before wifi
  987. * is turned on. We do not allow switching back to DRV.
  988. * To bring device back into DRV, user needs to reboot device.
  989. */
  990. if (test_bit(DISABLE_DRV, &plat_priv->ctrl_params.quirks)) {
  991. cnss_pr_dbg("DRV is disabled\n");
  992. cnss_bus_disable_mhi_satellite_cfg(plat_priv);
  993. }
  994. mutex_unlock(&plat_priv->driver_ops_lock);
  995. return 0;
  996. out:
  997. mutex_unlock(&plat_priv->driver_ops_lock);
  998. return ret;
  999. }
  1000. EXPORT_SYMBOL(cnss_idle_restart);
  1001. int cnss_idle_shutdown(struct device *dev)
  1002. {
  1003. struct cnss_plat_data *plat_priv = cnss_bus_dev_to_plat_priv(dev);
  1004. unsigned int timeout;
  1005. int ret;
  1006. if (!plat_priv) {
  1007. cnss_pr_err("plat_priv is NULL\n");
  1008. return -ENODEV;
  1009. }
  1010. if (test_bit(CNSS_IN_SUSPEND_RESUME, &plat_priv->driver_state)) {
  1011. cnss_pr_dbg("System suspend or resume in progress, ignore idle shutdown\n");
  1012. return -EAGAIN;
  1013. }
  1014. cnss_pr_dbg("Doing idle shutdown\n");
  1015. if (!test_bit(CNSS_DRIVER_RECOVERY, &plat_priv->driver_state) &&
  1016. !test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state))
  1017. goto skip_wait;
  1018. reinit_completion(&plat_priv->recovery_complete);
  1019. timeout = cnss_get_timeout(plat_priv, CNSS_TIMEOUT_RECOVERY);
  1020. ret = wait_for_completion_timeout(&plat_priv->recovery_complete,
  1021. msecs_to_jiffies(timeout));
  1022. if (!ret) {
  1023. cnss_pr_err("Timeout (%ums) waiting for recovery to complete\n",
  1024. timeout);
  1025. CNSS_ASSERT(0);
  1026. }
  1027. skip_wait:
  1028. return cnss_driver_event_post(plat_priv,
  1029. CNSS_DRIVER_EVENT_IDLE_SHUTDOWN,
  1030. CNSS_EVENT_SYNC_UNINTERRUPTIBLE, NULL);
  1031. }
  1032. EXPORT_SYMBOL(cnss_idle_shutdown);
  1033. static int cnss_get_resources(struct cnss_plat_data *plat_priv)
  1034. {
  1035. int ret = 0;
  1036. ret = cnss_get_vreg_type(plat_priv, CNSS_VREG_PRIM);
  1037. if (ret) {
  1038. cnss_pr_err("Failed to get vreg, err = %d\n", ret);
  1039. goto out;
  1040. }
  1041. ret = cnss_get_clk(plat_priv);
  1042. if (ret) {
  1043. cnss_pr_err("Failed to get clocks, err = %d\n", ret);
  1044. goto put_vreg;
  1045. }
  1046. ret = cnss_get_pinctrl(plat_priv);
  1047. if (ret) {
  1048. cnss_pr_err("Failed to get pinctrl, err = %d\n", ret);
  1049. goto put_clk;
  1050. }
  1051. return 0;
  1052. put_clk:
  1053. cnss_put_clk(plat_priv);
  1054. put_vreg:
  1055. cnss_put_vreg_type(plat_priv, CNSS_VREG_PRIM);
  1056. out:
  1057. return ret;
  1058. }
  1059. static void cnss_put_resources(struct cnss_plat_data *plat_priv)
  1060. {
  1061. cnss_put_clk(plat_priv);
  1062. cnss_put_vreg_type(plat_priv, CNSS_VREG_PRIM);
  1063. }
  1064. #if IS_ENABLED(CONFIG_ESOC) && IS_ENABLED(CONFIG_MSM_SUBSYSTEM_RESTART)
  1065. static int cnss_modem_notifier_nb(struct notifier_block *nb,
  1066. unsigned long code,
  1067. void *ss_handle)
  1068. {
  1069. struct cnss_plat_data *plat_priv =
  1070. container_of(nb, struct cnss_plat_data, modem_nb);
  1071. struct cnss_esoc_info *esoc_info;
  1072. cnss_pr_dbg("Modem notifier: event %lu\n", code);
  1073. if (!plat_priv)
  1074. return NOTIFY_DONE;
  1075. esoc_info = &plat_priv->esoc_info;
  1076. if (code == SUBSYS_AFTER_POWERUP)
  1077. esoc_info->modem_current_status = 1;
  1078. else if (code == SUBSYS_BEFORE_SHUTDOWN)
  1079. esoc_info->modem_current_status = 0;
  1080. else
  1081. return NOTIFY_DONE;
  1082. if (!cnss_bus_call_driver_modem_status(plat_priv,
  1083. esoc_info->modem_current_status))
  1084. return NOTIFY_DONE;
  1085. return NOTIFY_OK;
  1086. }
  1087. static int cnss_register_esoc(struct cnss_plat_data *plat_priv)
  1088. {
  1089. int ret = 0;
  1090. struct device *dev;
  1091. struct cnss_esoc_info *esoc_info;
  1092. struct esoc_desc *esoc_desc;
  1093. const char *client_desc;
  1094. dev = &plat_priv->plat_dev->dev;
  1095. esoc_info = &plat_priv->esoc_info;
  1096. esoc_info->notify_modem_status =
  1097. of_property_read_bool(dev->of_node,
  1098. "qcom,notify-modem-status");
  1099. if (!esoc_info->notify_modem_status)
  1100. goto out;
  1101. ret = of_property_read_string_index(dev->of_node, "esoc-names", 0,
  1102. &client_desc);
  1103. if (ret) {
  1104. cnss_pr_dbg("esoc-names is not defined in DT, skip!\n");
  1105. } else {
  1106. esoc_desc = devm_register_esoc_client(dev, client_desc);
  1107. if (IS_ERR_OR_NULL(esoc_desc)) {
  1108. ret = PTR_RET(esoc_desc);
  1109. cnss_pr_err("Failed to register esoc_desc, err = %d\n",
  1110. ret);
  1111. goto out;
  1112. }
  1113. esoc_info->esoc_desc = esoc_desc;
  1114. }
  1115. plat_priv->modem_nb.notifier_call = cnss_modem_notifier_nb;
  1116. esoc_info->modem_current_status = 0;
  1117. esoc_info->modem_notify_handler =
  1118. subsys_notif_register_notifier(esoc_info->esoc_desc ?
  1119. esoc_info->esoc_desc->name :
  1120. "modem", &plat_priv->modem_nb);
  1121. if (IS_ERR(esoc_info->modem_notify_handler)) {
  1122. ret = PTR_ERR(esoc_info->modem_notify_handler);
  1123. cnss_pr_err("Failed to register esoc notifier, err = %d\n",
  1124. ret);
  1125. goto unreg_esoc;
  1126. }
  1127. return 0;
  1128. unreg_esoc:
  1129. if (esoc_info->esoc_desc)
  1130. devm_unregister_esoc_client(dev, esoc_info->esoc_desc);
  1131. out:
  1132. return ret;
  1133. }
  1134. static void cnss_unregister_esoc(struct cnss_plat_data *plat_priv)
  1135. {
  1136. struct device *dev;
  1137. struct cnss_esoc_info *esoc_info;
  1138. dev = &plat_priv->plat_dev->dev;
  1139. esoc_info = &plat_priv->esoc_info;
  1140. if (esoc_info->notify_modem_status)
  1141. subsys_notif_unregister_notifier
  1142. (esoc_info->modem_notify_handler,
  1143. &plat_priv->modem_nb);
  1144. if (esoc_info->esoc_desc)
  1145. devm_unregister_esoc_client(dev, esoc_info->esoc_desc);
  1146. }
  1147. #else
  1148. static inline int cnss_register_esoc(struct cnss_plat_data *plat_priv)
  1149. {
  1150. return 0;
  1151. }
  1152. static inline void cnss_unregister_esoc(struct cnss_plat_data *plat_priv) {}
  1153. #endif
  1154. int cnss_enable_dev_sol_irq(struct cnss_plat_data *plat_priv)
  1155. {
  1156. struct cnss_sol_gpio *sol_gpio = &plat_priv->sol_gpio;
  1157. int ret = 0;
  1158. if (sol_gpio->dev_sol_gpio < 0 || sol_gpio->dev_sol_irq <= 0)
  1159. return 0;
  1160. enable_irq(sol_gpio->dev_sol_irq);
  1161. ret = enable_irq_wake(sol_gpio->dev_sol_irq);
  1162. if (ret)
  1163. cnss_pr_err("Failed to enable device SOL as wake IRQ, err = %d\n",
  1164. ret);
  1165. return ret;
  1166. }
  1167. int cnss_disable_dev_sol_irq(struct cnss_plat_data *plat_priv)
  1168. {
  1169. struct cnss_sol_gpio *sol_gpio = &plat_priv->sol_gpio;
  1170. int ret = 0;
  1171. if (sol_gpio->dev_sol_gpio < 0 || sol_gpio->dev_sol_irq <= 0)
  1172. return 0;
  1173. ret = disable_irq_wake(sol_gpio->dev_sol_irq);
  1174. if (ret)
  1175. cnss_pr_err("Failed to disable device SOL as wake IRQ, err = %d\n",
  1176. ret);
  1177. disable_irq(sol_gpio->dev_sol_irq);
  1178. return ret;
  1179. }
  1180. int cnss_get_dev_sol_value(struct cnss_plat_data *plat_priv)
  1181. {
  1182. struct cnss_sol_gpio *sol_gpio = &plat_priv->sol_gpio;
  1183. if (sol_gpio->dev_sol_gpio < 0)
  1184. return -EINVAL;
  1185. return gpio_get_value(sol_gpio->dev_sol_gpio);
  1186. }
  1187. static irqreturn_t cnss_dev_sol_handler(int irq, void *data)
  1188. {
  1189. struct cnss_plat_data *plat_priv = data;
  1190. struct cnss_sol_gpio *sol_gpio = &plat_priv->sol_gpio;
  1191. sol_gpio->dev_sol_counter++;
  1192. cnss_pr_dbg("WLAN device SOL IRQ (%u) is asserted #%u\n",
  1193. irq, sol_gpio->dev_sol_counter);
  1194. /* Make sure abort current suspend */
  1195. cnss_pm_stay_awake(plat_priv);
  1196. cnss_pm_relax(plat_priv);
  1197. pm_system_wakeup();
  1198. cnss_bus_handle_dev_sol_irq(plat_priv);
  1199. return IRQ_HANDLED;
  1200. }
  1201. static int cnss_init_dev_sol_gpio(struct cnss_plat_data *plat_priv)
  1202. {
  1203. struct device *dev = &plat_priv->plat_dev->dev;
  1204. struct cnss_sol_gpio *sol_gpio = &plat_priv->sol_gpio;
  1205. int ret = 0;
  1206. sol_gpio->dev_sol_gpio = of_get_named_gpio(dev->of_node,
  1207. "wlan-dev-sol-gpio", 0);
  1208. if (sol_gpio->dev_sol_gpio < 0)
  1209. goto out;
  1210. cnss_pr_dbg("Get device SOL GPIO (%d) from device node\n",
  1211. sol_gpio->dev_sol_gpio);
  1212. ret = gpio_request(sol_gpio->dev_sol_gpio, "wlan_dev_sol_gpio");
  1213. if (ret) {
  1214. cnss_pr_err("Failed to request device SOL GPIO, err = %d\n",
  1215. ret);
  1216. goto out;
  1217. }
  1218. gpio_direction_input(sol_gpio->dev_sol_gpio);
  1219. sol_gpio->dev_sol_irq = gpio_to_irq(sol_gpio->dev_sol_gpio);
  1220. ret = request_irq(sol_gpio->dev_sol_irq, cnss_dev_sol_handler,
  1221. IRQF_TRIGGER_FALLING, "wlan_dev_sol_irq", plat_priv);
  1222. if (ret) {
  1223. cnss_pr_err("Failed to request device SOL IRQ, err = %d\n", ret);
  1224. goto free_gpio;
  1225. }
  1226. return 0;
  1227. free_gpio:
  1228. gpio_free(sol_gpio->dev_sol_gpio);
  1229. out:
  1230. return ret;
  1231. }
  1232. static void cnss_deinit_dev_sol_gpio(struct cnss_plat_data *plat_priv)
  1233. {
  1234. struct cnss_sol_gpio *sol_gpio = &plat_priv->sol_gpio;
  1235. if (sol_gpio->dev_sol_gpio < 0)
  1236. return;
  1237. free_irq(sol_gpio->dev_sol_irq, plat_priv);
  1238. gpio_free(sol_gpio->dev_sol_gpio);
  1239. }
  1240. int cnss_set_host_sol_value(struct cnss_plat_data *plat_priv, int value)
  1241. {
  1242. struct cnss_sol_gpio *sol_gpio = &plat_priv->sol_gpio;
  1243. if (sol_gpio->host_sol_gpio < 0)
  1244. return -EINVAL;
  1245. if (value)
  1246. cnss_pr_dbg("Assert host SOL GPIO\n");
  1247. gpio_set_value(sol_gpio->host_sol_gpio, value);
  1248. return 0;
  1249. }
  1250. int cnss_get_host_sol_value(struct cnss_plat_data *plat_priv)
  1251. {
  1252. struct cnss_sol_gpio *sol_gpio = &plat_priv->sol_gpio;
  1253. if (sol_gpio->host_sol_gpio < 0)
  1254. return -EINVAL;
  1255. return gpio_get_value(sol_gpio->host_sol_gpio);
  1256. }
  1257. static int cnss_init_host_sol_gpio(struct cnss_plat_data *plat_priv)
  1258. {
  1259. struct device *dev = &plat_priv->plat_dev->dev;
  1260. struct cnss_sol_gpio *sol_gpio = &plat_priv->sol_gpio;
  1261. int ret = 0;
  1262. sol_gpio->host_sol_gpio = of_get_named_gpio(dev->of_node,
  1263. "wlan-host-sol-gpio", 0);
  1264. if (sol_gpio->host_sol_gpio < 0)
  1265. goto out;
  1266. cnss_pr_dbg("Get host SOL GPIO (%d) from device node\n",
  1267. sol_gpio->host_sol_gpio);
  1268. ret = gpio_request(sol_gpio->host_sol_gpio, "wlan_host_sol_gpio");
  1269. if (ret) {
  1270. cnss_pr_err("Failed to request host SOL GPIO, err = %d\n",
  1271. ret);
  1272. goto out;
  1273. }
  1274. gpio_direction_output(sol_gpio->host_sol_gpio, 0);
  1275. return 0;
  1276. out:
  1277. return ret;
  1278. }
  1279. static void cnss_deinit_host_sol_gpio(struct cnss_plat_data *plat_priv)
  1280. {
  1281. struct cnss_sol_gpio *sol_gpio = &plat_priv->sol_gpio;
  1282. if (sol_gpio->host_sol_gpio < 0)
  1283. return;
  1284. gpio_free(sol_gpio->host_sol_gpio);
  1285. }
  1286. static int cnss_init_sol_gpio(struct cnss_plat_data *plat_priv)
  1287. {
  1288. int ret;
  1289. ret = cnss_init_dev_sol_gpio(plat_priv);
  1290. if (ret)
  1291. goto out;
  1292. ret = cnss_init_host_sol_gpio(plat_priv);
  1293. if (ret)
  1294. goto deinit_dev_sol;
  1295. return 0;
  1296. deinit_dev_sol:
  1297. cnss_deinit_dev_sol_gpio(plat_priv);
  1298. out:
  1299. return ret;
  1300. }
  1301. static void cnss_deinit_sol_gpio(struct cnss_plat_data *plat_priv)
  1302. {
  1303. cnss_deinit_host_sol_gpio(plat_priv);
  1304. cnss_deinit_dev_sol_gpio(plat_priv);
  1305. }
  1306. #if IS_ENABLED(CONFIG_MSM_SUBSYSTEM_RESTART)
  1307. static int cnss_subsys_powerup(const struct subsys_desc *subsys_desc)
  1308. {
  1309. struct cnss_plat_data *plat_priv;
  1310. int ret = 0;
  1311. if (!subsys_desc->dev) {
  1312. cnss_pr_err("dev from subsys_desc is NULL\n");
  1313. return -ENODEV;
  1314. }
  1315. plat_priv = dev_get_drvdata(subsys_desc->dev);
  1316. if (!plat_priv) {
  1317. cnss_pr_err("plat_priv is NULL\n");
  1318. return -ENODEV;
  1319. }
  1320. if (!plat_priv->driver_state) {
  1321. cnss_pr_dbg("subsys powerup is ignored\n");
  1322. return 0;
  1323. }
  1324. ret = cnss_bus_dev_powerup(plat_priv);
  1325. if (ret)
  1326. __pm_relax(plat_priv->recovery_ws);
  1327. return ret;
  1328. }
  1329. static int cnss_subsys_shutdown(const struct subsys_desc *subsys_desc,
  1330. bool force_stop)
  1331. {
  1332. struct cnss_plat_data *plat_priv;
  1333. if (!subsys_desc->dev) {
  1334. cnss_pr_err("dev from subsys_desc is NULL\n");
  1335. return -ENODEV;
  1336. }
  1337. plat_priv = dev_get_drvdata(subsys_desc->dev);
  1338. if (!plat_priv) {
  1339. cnss_pr_err("plat_priv is NULL\n");
  1340. return -ENODEV;
  1341. }
  1342. if (!plat_priv->driver_state) {
  1343. cnss_pr_dbg("subsys shutdown is ignored\n");
  1344. return 0;
  1345. }
  1346. return cnss_bus_dev_shutdown(plat_priv);
  1347. }
  1348. void cnss_device_crashed(struct device *dev)
  1349. {
  1350. struct cnss_plat_data *plat_priv = cnss_bus_dev_to_plat_priv(dev);
  1351. struct cnss_subsys_info *subsys_info;
  1352. if (!plat_priv)
  1353. return;
  1354. subsys_info = &plat_priv->subsys_info;
  1355. if (subsys_info->subsys_device) {
  1356. set_bit(CNSS_DRIVER_RECOVERY, &plat_priv->driver_state);
  1357. subsys_set_crash_status(subsys_info->subsys_device, true);
  1358. subsystem_restart_dev(subsys_info->subsys_device);
  1359. }
  1360. }
  1361. EXPORT_SYMBOL(cnss_device_crashed);
  1362. static void cnss_subsys_crash_shutdown(const struct subsys_desc *subsys_desc)
  1363. {
  1364. struct cnss_plat_data *plat_priv = dev_get_drvdata(subsys_desc->dev);
  1365. if (!plat_priv) {
  1366. cnss_pr_err("plat_priv is NULL\n");
  1367. return;
  1368. }
  1369. cnss_bus_dev_crash_shutdown(plat_priv);
  1370. }
  1371. static int cnss_subsys_ramdump(int enable,
  1372. const struct subsys_desc *subsys_desc)
  1373. {
  1374. struct cnss_plat_data *plat_priv = dev_get_drvdata(subsys_desc->dev);
  1375. if (!plat_priv) {
  1376. cnss_pr_err("plat_priv is NULL\n");
  1377. return -ENODEV;
  1378. }
  1379. if (!enable)
  1380. return 0;
  1381. return cnss_bus_dev_ramdump(plat_priv);
  1382. }
  1383. static void cnss_recovery_work_handler(struct work_struct *work)
  1384. {
  1385. }
  1386. #else
  1387. static void cnss_recovery_work_handler(struct work_struct *work)
  1388. {
  1389. int ret;
  1390. struct cnss_plat_data *plat_priv =
  1391. container_of(work, struct cnss_plat_data, recovery_work);
  1392. if (!plat_priv->recovery_enabled)
  1393. panic("subsys-restart: Resetting the SoC wlan crashed\n");
  1394. cnss_bus_dev_shutdown(plat_priv);
  1395. cnss_bus_dev_ramdump(plat_priv);
  1396. msleep(POWER_RESET_MIN_DELAY_MS);
  1397. ret = cnss_bus_dev_powerup(plat_priv);
  1398. if (ret)
  1399. __pm_relax(plat_priv->recovery_ws);
  1400. return;
  1401. }
  1402. void cnss_device_crashed(struct device *dev)
  1403. {
  1404. struct cnss_plat_data *plat_priv = cnss_bus_dev_to_plat_priv(dev);
  1405. if (!plat_priv)
  1406. return;
  1407. set_bit(CNSS_DRIVER_RECOVERY, &plat_priv->driver_state);
  1408. schedule_work(&plat_priv->recovery_work);
  1409. }
  1410. EXPORT_SYMBOL(cnss_device_crashed);
  1411. #endif /* CONFIG_MSM_SUBSYSTEM_RESTART */
  1412. void *cnss_get_virt_ramdump_mem(struct device *dev, unsigned long *size)
  1413. {
  1414. struct cnss_plat_data *plat_priv = cnss_bus_dev_to_plat_priv(dev);
  1415. struct cnss_ramdump_info *ramdump_info;
  1416. if (!plat_priv)
  1417. return NULL;
  1418. ramdump_info = &plat_priv->ramdump_info;
  1419. *size = ramdump_info->ramdump_size;
  1420. return ramdump_info->ramdump_va;
  1421. }
  1422. EXPORT_SYMBOL(cnss_get_virt_ramdump_mem);
  1423. static const char *cnss_recovery_reason_to_str(enum cnss_recovery_reason reason)
  1424. {
  1425. switch (reason) {
  1426. case CNSS_REASON_DEFAULT:
  1427. return "DEFAULT";
  1428. case CNSS_REASON_LINK_DOWN:
  1429. return "LINK_DOWN";
  1430. case CNSS_REASON_RDDM:
  1431. return "RDDM";
  1432. case CNSS_REASON_TIMEOUT:
  1433. return "TIMEOUT";
  1434. }
  1435. return "UNKNOWN";
  1436. };
  1437. static int cnss_do_recovery(struct cnss_plat_data *plat_priv,
  1438. enum cnss_recovery_reason reason)
  1439. {
  1440. plat_priv->recovery_count++;
  1441. if (plat_priv->device_id == QCA6174_DEVICE_ID)
  1442. goto self_recovery;
  1443. if (test_bit(SKIP_RECOVERY, &plat_priv->ctrl_params.quirks)) {
  1444. cnss_pr_dbg("Skip device recovery\n");
  1445. return 0;
  1446. }
  1447. /* FW recovery sequence has multiple steps and firmware load requires
  1448. * linux PM in awake state. Thus hold the cnss wake source until
  1449. * WLAN MISSION enabled. CNSS_TIMEOUT_RECOVERY option should cover all
  1450. * time taken in this process.
  1451. */
  1452. pm_wakeup_ws_event(plat_priv->recovery_ws,
  1453. cnss_get_timeout(plat_priv, CNSS_TIMEOUT_RECOVERY),
  1454. true);
  1455. switch (reason) {
  1456. case CNSS_REASON_LINK_DOWN:
  1457. if (!cnss_bus_check_link_status(plat_priv)) {
  1458. cnss_pr_dbg("Skip link down recovery as link is already up\n");
  1459. return 0;
  1460. }
  1461. if (test_bit(LINK_DOWN_SELF_RECOVERY,
  1462. &plat_priv->ctrl_params.quirks))
  1463. goto self_recovery;
  1464. if (!cnss_bus_recover_link_down(plat_priv)) {
  1465. /* clear recovery bit here to avoid skipping
  1466. * the recovery work for RDDM later
  1467. */
  1468. clear_bit(CNSS_DRIVER_RECOVERY,
  1469. &plat_priv->driver_state);
  1470. return 0;
  1471. }
  1472. break;
  1473. case CNSS_REASON_RDDM:
  1474. cnss_bus_collect_dump_info(plat_priv, false);
  1475. break;
  1476. case CNSS_REASON_DEFAULT:
  1477. case CNSS_REASON_TIMEOUT:
  1478. break;
  1479. default:
  1480. cnss_pr_err("Unsupported recovery reason: %s(%d)\n",
  1481. cnss_recovery_reason_to_str(reason), reason);
  1482. break;
  1483. }
  1484. cnss_bus_device_crashed(plat_priv);
  1485. return 0;
  1486. self_recovery:
  1487. cnss_pr_dbg("Going for self recovery\n");
  1488. cnss_bus_dev_shutdown(plat_priv);
  1489. if (test_bit(LINK_DOWN_SELF_RECOVERY, &plat_priv->ctrl_params.quirks))
  1490. clear_bit(LINK_DOWN_SELF_RECOVERY,
  1491. &plat_priv->ctrl_params.quirks);
  1492. cnss_bus_dev_powerup(plat_priv);
  1493. return 0;
  1494. }
  1495. static int cnss_driver_recovery_hdlr(struct cnss_plat_data *plat_priv,
  1496. void *data)
  1497. {
  1498. struct cnss_recovery_data *recovery_data = data;
  1499. int ret = 0;
  1500. cnss_pr_dbg("Driver recovery is triggered with reason: %s(%d)\n",
  1501. cnss_recovery_reason_to_str(recovery_data->reason),
  1502. recovery_data->reason);
  1503. if (!plat_priv->driver_state) {
  1504. cnss_pr_err("Improper driver state, ignore recovery\n");
  1505. ret = -EINVAL;
  1506. goto out;
  1507. }
  1508. if (test_bit(CNSS_IN_REBOOT, &plat_priv->driver_state)) {
  1509. cnss_pr_err("Reboot is in progress, ignore recovery\n");
  1510. ret = -EINVAL;
  1511. goto out;
  1512. }
  1513. if (test_bit(CNSS_DRIVER_RECOVERY, &plat_priv->driver_state)) {
  1514. cnss_pr_err("Recovery is already in progress\n");
  1515. CNSS_ASSERT(0);
  1516. ret = -EINVAL;
  1517. goto out;
  1518. }
  1519. if (test_bit(CNSS_DRIVER_UNLOADING, &plat_priv->driver_state) ||
  1520. test_bit(CNSS_DRIVER_IDLE_SHUTDOWN, &plat_priv->driver_state)) {
  1521. cnss_pr_err("Driver unload or idle shutdown is in progress, ignore recovery\n");
  1522. ret = -EINVAL;
  1523. goto out;
  1524. }
  1525. switch (plat_priv->device_id) {
  1526. case QCA6174_DEVICE_ID:
  1527. if (test_bit(CNSS_DRIVER_LOADING, &plat_priv->driver_state) ||
  1528. test_bit(CNSS_DRIVER_IDLE_RESTART,
  1529. &plat_priv->driver_state)) {
  1530. cnss_pr_err("Driver load or idle restart is in progress, ignore recovery\n");
  1531. ret = -EINVAL;
  1532. goto out;
  1533. }
  1534. break;
  1535. default:
  1536. if (!test_bit(CNSS_FW_READY, &plat_priv->driver_state)) {
  1537. set_bit(CNSS_FW_BOOT_RECOVERY,
  1538. &plat_priv->driver_state);
  1539. }
  1540. break;
  1541. }
  1542. set_bit(CNSS_DRIVER_RECOVERY, &plat_priv->driver_state);
  1543. ret = cnss_do_recovery(plat_priv, recovery_data->reason);
  1544. out:
  1545. kfree(data);
  1546. return ret;
  1547. }
  1548. int cnss_self_recovery(struct device *dev,
  1549. enum cnss_recovery_reason reason)
  1550. {
  1551. cnss_schedule_recovery(dev, reason);
  1552. return 0;
  1553. }
  1554. EXPORT_SYMBOL(cnss_self_recovery);
  1555. void cnss_schedule_recovery(struct device *dev,
  1556. enum cnss_recovery_reason reason)
  1557. {
  1558. struct cnss_plat_data *plat_priv = cnss_bus_dev_to_plat_priv(dev);
  1559. struct cnss_recovery_data *data;
  1560. int gfp = GFP_KERNEL;
  1561. if (!test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state))
  1562. cnss_bus_update_status(plat_priv, CNSS_FW_DOWN);
  1563. if (test_bit(CNSS_DRIVER_UNLOADING, &plat_priv->driver_state) ||
  1564. test_bit(CNSS_DRIVER_IDLE_SHUTDOWN, &plat_priv->driver_state)) {
  1565. cnss_pr_dbg("Driver unload or idle shutdown is in progress, ignore schedule recovery\n");
  1566. return;
  1567. }
  1568. if (in_interrupt() || irqs_disabled())
  1569. gfp = GFP_ATOMIC;
  1570. data = kzalloc(sizeof(*data), gfp);
  1571. if (!data)
  1572. return;
  1573. data->reason = reason;
  1574. cnss_driver_event_post(plat_priv,
  1575. CNSS_DRIVER_EVENT_RECOVERY,
  1576. 0, data);
  1577. }
  1578. EXPORT_SYMBOL(cnss_schedule_recovery);
  1579. int cnss_force_fw_assert(struct device *dev)
  1580. {
  1581. struct cnss_plat_data *plat_priv = cnss_bus_dev_to_plat_priv(dev);
  1582. if (!plat_priv) {
  1583. cnss_pr_err("plat_priv is NULL\n");
  1584. return -ENODEV;
  1585. }
  1586. if (plat_priv->device_id == QCA6174_DEVICE_ID) {
  1587. cnss_pr_info("Forced FW assert is not supported\n");
  1588. return -EOPNOTSUPP;
  1589. }
  1590. if (cnss_bus_is_device_down(plat_priv)) {
  1591. cnss_pr_info("Device is already in bad state, ignore force assert\n");
  1592. return 0;
  1593. }
  1594. if (test_bit(CNSS_DRIVER_RECOVERY, &plat_priv->driver_state)) {
  1595. cnss_pr_info("Recovery is already in progress, ignore forced FW assert\n");
  1596. return 0;
  1597. }
  1598. if (in_interrupt() || irqs_disabled())
  1599. cnss_driver_event_post(plat_priv,
  1600. CNSS_DRIVER_EVENT_FORCE_FW_ASSERT,
  1601. 0, NULL);
  1602. else
  1603. cnss_bus_force_fw_assert_hdlr(plat_priv);
  1604. return 0;
  1605. }
  1606. EXPORT_SYMBOL(cnss_force_fw_assert);
  1607. int cnss_force_collect_rddm(struct device *dev)
  1608. {
  1609. struct cnss_plat_data *plat_priv = cnss_bus_dev_to_plat_priv(dev);
  1610. unsigned int timeout;
  1611. int ret = 0;
  1612. if (!plat_priv) {
  1613. cnss_pr_err("plat_priv is NULL\n");
  1614. return -ENODEV;
  1615. }
  1616. if (plat_priv->device_id == QCA6174_DEVICE_ID) {
  1617. cnss_pr_info("Force collect rddm is not supported\n");
  1618. return -EOPNOTSUPP;
  1619. }
  1620. if (cnss_bus_is_device_down(plat_priv)) {
  1621. cnss_pr_info("Device is already in bad state, wait to collect rddm\n");
  1622. goto wait_rddm;
  1623. }
  1624. if (test_bit(CNSS_DRIVER_RECOVERY, &plat_priv->driver_state)) {
  1625. cnss_pr_info("Recovery is already in progress, wait to collect rddm\n");
  1626. goto wait_rddm;
  1627. }
  1628. if (test_bit(CNSS_DRIVER_LOADING, &plat_priv->driver_state) ||
  1629. test_bit(CNSS_DRIVER_UNLOADING, &plat_priv->driver_state) ||
  1630. test_bit(CNSS_DRIVER_IDLE_RESTART, &plat_priv->driver_state) ||
  1631. test_bit(CNSS_DRIVER_IDLE_SHUTDOWN, &plat_priv->driver_state)) {
  1632. cnss_pr_info("Loading/Unloading/idle restart/shutdown is in progress, ignore forced collect rddm\n");
  1633. return 0;
  1634. }
  1635. ret = cnss_bus_force_fw_assert_hdlr(plat_priv);
  1636. if (ret)
  1637. return ret;
  1638. wait_rddm:
  1639. reinit_completion(&plat_priv->rddm_complete);
  1640. timeout = cnss_get_timeout(plat_priv, CNSS_TIMEOUT_RDDM);
  1641. ret = wait_for_completion_timeout(&plat_priv->rddm_complete,
  1642. msecs_to_jiffies(timeout));
  1643. if (!ret) {
  1644. cnss_pr_err("Timeout (%ums) waiting for RDDM to complete\n",
  1645. timeout);
  1646. ret = -ETIMEDOUT;
  1647. } else if (ret > 0) {
  1648. ret = 0;
  1649. }
  1650. return ret;
  1651. }
  1652. EXPORT_SYMBOL(cnss_force_collect_rddm);
  1653. int cnss_qmi_send_get(struct device *dev)
  1654. {
  1655. struct cnss_plat_data *plat_priv = cnss_bus_dev_to_plat_priv(dev);
  1656. if (!test_bit(CNSS_QMI_WLFW_CONNECTED, &plat_priv->driver_state))
  1657. return 0;
  1658. return cnss_bus_qmi_send_get(plat_priv);
  1659. }
  1660. EXPORT_SYMBOL(cnss_qmi_send_get);
  1661. int cnss_qmi_send_put(struct device *dev)
  1662. {
  1663. struct cnss_plat_data *plat_priv = cnss_bus_dev_to_plat_priv(dev);
  1664. if (!test_bit(CNSS_QMI_WLFW_CONNECTED, &plat_priv->driver_state))
  1665. return 0;
  1666. return cnss_bus_qmi_send_put(plat_priv);
  1667. }
  1668. EXPORT_SYMBOL(cnss_qmi_send_put);
  1669. int cnss_qmi_send(struct device *dev, int type, void *cmd,
  1670. int cmd_len, void *cb_ctx,
  1671. int (*cb)(void *ctx, void *event, int event_len))
  1672. {
  1673. struct cnss_plat_data *plat_priv = cnss_bus_dev_to_plat_priv(dev);
  1674. int ret;
  1675. if (!plat_priv)
  1676. return -ENODEV;
  1677. if (!test_bit(CNSS_QMI_WLFW_CONNECTED, &plat_priv->driver_state))
  1678. return -EINVAL;
  1679. plat_priv->get_info_cb = cb;
  1680. plat_priv->get_info_cb_ctx = cb_ctx;
  1681. ret = cnss_wlfw_get_info_send_sync(plat_priv, type, cmd, cmd_len);
  1682. if (ret) {
  1683. plat_priv->get_info_cb = NULL;
  1684. plat_priv->get_info_cb_ctx = NULL;
  1685. }
  1686. return ret;
  1687. }
  1688. EXPORT_SYMBOL(cnss_qmi_send);
  1689. static int cnss_cold_boot_cal_start_hdlr(struct cnss_plat_data *plat_priv)
  1690. {
  1691. int ret = 0;
  1692. u32 retry = 0, timeout;
  1693. if (test_bit(CNSS_COLD_BOOT_CAL_DONE, &plat_priv->driver_state)) {
  1694. cnss_pr_dbg("Calibration complete. Ignore calibration req\n");
  1695. goto out;
  1696. } else if (test_bit(CNSS_IN_COLD_BOOT_CAL, &plat_priv->driver_state)) {
  1697. cnss_pr_dbg("Calibration in progress. Ignore new calibration req\n");
  1698. goto out;
  1699. } else if (test_bit(CNSS_WLAN_HW_DISABLED, &plat_priv->driver_state)) {
  1700. cnss_pr_dbg("Calibration deferred as WLAN device disabled\n");
  1701. goto out;
  1702. }
  1703. if (test_bit(CNSS_DRIVER_LOADING, &plat_priv->driver_state) ||
  1704. test_bit(CNSS_DRIVER_PROBED, &plat_priv->driver_state) ||
  1705. test_bit(CNSS_FW_READY, &plat_priv->driver_state)) {
  1706. cnss_pr_err("WLAN in mission mode before cold boot calibration\n");
  1707. CNSS_ASSERT(0);
  1708. return -EINVAL;
  1709. }
  1710. while (retry++ < CNSS_CAL_START_PROBE_WAIT_RETRY_MAX) {
  1711. if (test_bit(CNSS_PCI_PROBE_DONE, &plat_priv->driver_state))
  1712. break;
  1713. msleep(CNSS_CAL_START_PROBE_WAIT_MS);
  1714. if (retry == CNSS_CAL_START_PROBE_WAIT_RETRY_MAX) {
  1715. cnss_pr_err("Calibration start failed as PCI probe not complete\n");
  1716. CNSS_ASSERT(0);
  1717. ret = -EINVAL;
  1718. goto mark_cal_fail;
  1719. }
  1720. }
  1721. switch (plat_priv->device_id) {
  1722. case QCA6290_DEVICE_ID:
  1723. case QCA6390_DEVICE_ID:
  1724. case QCA6490_DEVICE_ID:
  1725. case KIWI_DEVICE_ID:
  1726. case MANGO_DEVICE_ID:
  1727. break;
  1728. default:
  1729. cnss_pr_err("Not supported for device ID 0x%lx\n",
  1730. plat_priv->device_id);
  1731. ret = -EINVAL;
  1732. goto mark_cal_fail;
  1733. }
  1734. set_bit(CNSS_IN_COLD_BOOT_CAL, &plat_priv->driver_state);
  1735. if (test_bit(CNSS_DRIVER_REGISTER, &plat_priv->driver_state)) {
  1736. timeout = cnss_get_timeout(plat_priv,
  1737. CNSS_TIMEOUT_CALIBRATION);
  1738. cnss_pr_dbg("Restarting calibration %ds timeout\n",
  1739. timeout / 1000);
  1740. if (cancel_delayed_work_sync(&plat_priv->wlan_reg_driver_work))
  1741. schedule_delayed_work(&plat_priv->wlan_reg_driver_work,
  1742. msecs_to_jiffies(timeout));
  1743. }
  1744. reinit_completion(&plat_priv->cal_complete);
  1745. ret = cnss_bus_dev_powerup(plat_priv);
  1746. mark_cal_fail:
  1747. if (ret) {
  1748. complete(&plat_priv->cal_complete);
  1749. clear_bit(CNSS_IN_COLD_BOOT_CAL, &plat_priv->driver_state);
  1750. /* Set CBC done in driver state to mark attempt and note error
  1751. * since calibration cannot be retried at boot.
  1752. */
  1753. plat_priv->cal_done = CNSS_CAL_FAILURE;
  1754. set_bit(CNSS_COLD_BOOT_CAL_DONE, &plat_priv->driver_state);
  1755. if (plat_priv->device_id == QCA6174_DEVICE_ID ||
  1756. plat_priv->device_id == QCN7605_DEVICE_ID) {
  1757. if (!test_bit(CNSS_DRIVER_REGISTER, &plat_priv->driver_state))
  1758. goto out;
  1759. cnss_pr_info("Schedule WLAN driver load\n");
  1760. if (cancel_delayed_work_sync(&plat_priv->wlan_reg_driver_work))
  1761. schedule_delayed_work(&plat_priv->wlan_reg_driver_work,
  1762. 0);
  1763. }
  1764. }
  1765. out:
  1766. return ret;
  1767. }
  1768. static int cnss_cold_boot_cal_done_hdlr(struct cnss_plat_data *plat_priv,
  1769. void *data)
  1770. {
  1771. struct cnss_cal_info *cal_info = data;
  1772. if (!test_bit(CNSS_IN_COLD_BOOT_CAL, &plat_priv->driver_state) ||
  1773. test_bit(CNSS_COLD_BOOT_CAL_DONE, &plat_priv->driver_state))
  1774. goto out;
  1775. switch (cal_info->cal_status) {
  1776. case CNSS_CAL_DONE:
  1777. cnss_pr_dbg("Calibration completed successfully\n");
  1778. plat_priv->cal_done = true;
  1779. break;
  1780. case CNSS_CAL_TIMEOUT:
  1781. case CNSS_CAL_FAILURE:
  1782. cnss_pr_dbg("Calibration failed. Status: %d, force shutdown\n",
  1783. cal_info->cal_status);
  1784. break;
  1785. default:
  1786. cnss_pr_err("Unknown calibration status: %u\n",
  1787. cal_info->cal_status);
  1788. break;
  1789. }
  1790. cnss_wlfw_wlan_mode_send_sync(plat_priv, CNSS_OFF);
  1791. cnss_bus_free_qdss_mem(plat_priv);
  1792. cnss_release_antenna_sharing(plat_priv);
  1793. cnss_bus_dev_shutdown(plat_priv);
  1794. msleep(POWER_RESET_MIN_DELAY_MS);
  1795. complete(&plat_priv->cal_complete);
  1796. clear_bit(CNSS_IN_COLD_BOOT_CAL, &plat_priv->driver_state);
  1797. set_bit(CNSS_COLD_BOOT_CAL_DONE, &plat_priv->driver_state);
  1798. if (cal_info->cal_status == CNSS_CAL_DONE) {
  1799. cnss_cal_mem_upload_to_file(plat_priv);
  1800. if (!test_bit(CNSS_DRIVER_REGISTER, &plat_priv->driver_state))
  1801. goto out;
  1802. cnss_pr_dbg("Schedule WLAN driver load\n");
  1803. if (cancel_delayed_work_sync(&plat_priv->wlan_reg_driver_work))
  1804. schedule_delayed_work(&plat_priv->wlan_reg_driver_work,
  1805. 0);
  1806. }
  1807. out:
  1808. kfree(data);
  1809. return 0;
  1810. }
  1811. static int cnss_power_up_hdlr(struct cnss_plat_data *plat_priv)
  1812. {
  1813. int ret;
  1814. ret = cnss_bus_dev_powerup(plat_priv);
  1815. if (ret)
  1816. clear_bit(CNSS_DRIVER_IDLE_RESTART, &plat_priv->driver_state);
  1817. return ret;
  1818. }
  1819. static int cnss_power_down_hdlr(struct cnss_plat_data *plat_priv)
  1820. {
  1821. cnss_bus_dev_shutdown(plat_priv);
  1822. return 0;
  1823. }
  1824. static int cnss_qdss_trace_req_mem_hdlr(struct cnss_plat_data *plat_priv)
  1825. {
  1826. int ret = 0;
  1827. ret = cnss_bus_alloc_qdss_mem(plat_priv);
  1828. if (ret < 0)
  1829. return ret;
  1830. return cnss_wlfw_qdss_trace_mem_info_send_sync(plat_priv);
  1831. }
  1832. static void *cnss_get_fw_mem_pa_to_va(struct cnss_fw_mem *fw_mem,
  1833. u32 mem_seg_len, u64 pa, u32 size)
  1834. {
  1835. int i = 0;
  1836. u64 offset = 0;
  1837. void *va = NULL;
  1838. u64 local_pa;
  1839. u32 local_size;
  1840. for (i = 0; i < mem_seg_len; i++) {
  1841. local_pa = (u64)fw_mem[i].pa;
  1842. local_size = (u32)fw_mem[i].size;
  1843. if (pa == local_pa && size <= local_size) {
  1844. va = fw_mem[i].va;
  1845. break;
  1846. }
  1847. if (pa > local_pa &&
  1848. pa < local_pa + local_size &&
  1849. pa + size <= local_pa + local_size) {
  1850. offset = pa - local_pa;
  1851. va = fw_mem[i].va + offset;
  1852. break;
  1853. }
  1854. }
  1855. return va;
  1856. }
  1857. static int cnss_fw_mem_file_save_hdlr(struct cnss_plat_data *plat_priv,
  1858. void *data)
  1859. {
  1860. struct cnss_qmi_event_fw_mem_file_save_data *event_data = data;
  1861. struct cnss_fw_mem *fw_mem_seg;
  1862. int ret = 0L;
  1863. void *va = NULL;
  1864. u32 i, fw_mem_seg_len;
  1865. switch (event_data->mem_type) {
  1866. case QMI_WLFW_MEM_TYPE_DDR_V01:
  1867. if (!plat_priv->fw_mem_seg_len)
  1868. goto invalid_mem_save;
  1869. fw_mem_seg = plat_priv->fw_mem;
  1870. fw_mem_seg_len = plat_priv->fw_mem_seg_len;
  1871. break;
  1872. case QMI_WLFW_MEM_QDSS_V01:
  1873. if (!plat_priv->qdss_mem_seg_len)
  1874. goto invalid_mem_save;
  1875. fw_mem_seg = plat_priv->qdss_mem;
  1876. fw_mem_seg_len = plat_priv->qdss_mem_seg_len;
  1877. break;
  1878. default:
  1879. goto invalid_mem_save;
  1880. }
  1881. for (i = 0; i < event_data->mem_seg_len; i++) {
  1882. va = cnss_get_fw_mem_pa_to_va(fw_mem_seg, fw_mem_seg_len,
  1883. event_data->mem_seg[i].addr,
  1884. event_data->mem_seg[i].size);
  1885. if (!va) {
  1886. cnss_pr_err("Fail to find matching va of pa %pa for mem type: %d\n",
  1887. &event_data->mem_seg[i].addr,
  1888. event_data->mem_type);
  1889. ret = -EINVAL;
  1890. break;
  1891. }
  1892. ret = cnss_genl_send_msg(va, CNSS_GENL_MSG_TYPE_QDSS,
  1893. event_data->file_name,
  1894. event_data->mem_seg[i].size);
  1895. if (ret < 0) {
  1896. cnss_pr_err("Fail to save fw mem data: %d\n",
  1897. ret);
  1898. break;
  1899. }
  1900. }
  1901. kfree(data);
  1902. return ret;
  1903. invalid_mem_save:
  1904. cnss_pr_err("FW Mem type %d not allocated. Invalid save request\n",
  1905. event_data->mem_type);
  1906. kfree(data);
  1907. return -EINVAL;
  1908. }
  1909. static int cnss_qdss_trace_free_hdlr(struct cnss_plat_data *plat_priv)
  1910. {
  1911. cnss_bus_free_qdss_mem(plat_priv);
  1912. return 0;
  1913. }
  1914. static int cnss_qdss_trace_req_data_hdlr(struct cnss_plat_data *plat_priv,
  1915. void *data)
  1916. {
  1917. int ret = 0;
  1918. struct cnss_qmi_event_fw_mem_file_save_data *event_data = data;
  1919. if (!plat_priv)
  1920. return -ENODEV;
  1921. ret = cnss_wlfw_qdss_data_send_sync(plat_priv, event_data->file_name,
  1922. event_data->total_size);
  1923. kfree(data);
  1924. return ret;
  1925. }
  1926. static void cnss_driver_event_work(struct work_struct *work)
  1927. {
  1928. struct cnss_plat_data *plat_priv =
  1929. container_of(work, struct cnss_plat_data, event_work);
  1930. struct cnss_driver_event *event;
  1931. unsigned long flags;
  1932. int ret = 0;
  1933. if (!plat_priv) {
  1934. cnss_pr_err("plat_priv is NULL!\n");
  1935. return;
  1936. }
  1937. cnss_pm_stay_awake(plat_priv);
  1938. spin_lock_irqsave(&plat_priv->event_lock, flags);
  1939. while (!list_empty(&plat_priv->event_list)) {
  1940. event = list_first_entry(&plat_priv->event_list,
  1941. struct cnss_driver_event, list);
  1942. list_del(&event->list);
  1943. spin_unlock_irqrestore(&plat_priv->event_lock, flags);
  1944. cnss_pr_dbg("Processing driver event: %s%s(%d), state: 0x%lx\n",
  1945. cnss_driver_event_to_str(event->type),
  1946. event->sync ? "-sync" : "", event->type,
  1947. plat_priv->driver_state);
  1948. switch (event->type) {
  1949. case CNSS_DRIVER_EVENT_SERVER_ARRIVE:
  1950. ret = cnss_wlfw_server_arrive(plat_priv, event->data);
  1951. break;
  1952. case CNSS_DRIVER_EVENT_SERVER_EXIT:
  1953. ret = cnss_wlfw_server_exit(plat_priv);
  1954. break;
  1955. case CNSS_DRIVER_EVENT_REQUEST_MEM:
  1956. ret = cnss_bus_alloc_fw_mem(plat_priv);
  1957. if (ret)
  1958. break;
  1959. ret = cnss_wlfw_respond_mem_send_sync(plat_priv);
  1960. break;
  1961. case CNSS_DRIVER_EVENT_FW_MEM_READY:
  1962. ret = cnss_fw_mem_ready_hdlr(plat_priv);
  1963. break;
  1964. case CNSS_DRIVER_EVENT_FW_READY:
  1965. ret = cnss_fw_ready_hdlr(plat_priv);
  1966. break;
  1967. case CNSS_DRIVER_EVENT_COLD_BOOT_CAL_START:
  1968. ret = cnss_cold_boot_cal_start_hdlr(plat_priv);
  1969. break;
  1970. case CNSS_DRIVER_EVENT_COLD_BOOT_CAL_DONE:
  1971. ret = cnss_cold_boot_cal_done_hdlr(plat_priv,
  1972. event->data);
  1973. break;
  1974. case CNSS_DRIVER_EVENT_REGISTER_DRIVER:
  1975. ret = cnss_bus_register_driver_hdlr(plat_priv,
  1976. event->data);
  1977. break;
  1978. case CNSS_DRIVER_EVENT_UNREGISTER_DRIVER:
  1979. ret = cnss_bus_unregister_driver_hdlr(plat_priv);
  1980. break;
  1981. case CNSS_DRIVER_EVENT_RECOVERY:
  1982. ret = cnss_driver_recovery_hdlr(plat_priv,
  1983. event->data);
  1984. break;
  1985. case CNSS_DRIVER_EVENT_FORCE_FW_ASSERT:
  1986. ret = cnss_bus_force_fw_assert_hdlr(plat_priv);
  1987. break;
  1988. case CNSS_DRIVER_EVENT_IDLE_RESTART:
  1989. set_bit(CNSS_DRIVER_IDLE_RESTART,
  1990. &plat_priv->driver_state);
  1991. fallthrough;
  1992. case CNSS_DRIVER_EVENT_POWER_UP:
  1993. ret = cnss_power_up_hdlr(plat_priv);
  1994. break;
  1995. case CNSS_DRIVER_EVENT_IDLE_SHUTDOWN:
  1996. set_bit(CNSS_DRIVER_IDLE_SHUTDOWN,
  1997. &plat_priv->driver_state);
  1998. fallthrough;
  1999. case CNSS_DRIVER_EVENT_POWER_DOWN:
  2000. ret = cnss_power_down_hdlr(plat_priv);
  2001. break;
  2002. case CNSS_DRIVER_EVENT_IMS_WFC_CALL_IND:
  2003. ret = cnss_process_wfc_call_ind_event(plat_priv,
  2004. event->data);
  2005. break;
  2006. case CNSS_DRIVER_EVENT_WLFW_TWT_CFG_IND:
  2007. ret = cnss_process_twt_cfg_ind_event(plat_priv,
  2008. event->data);
  2009. break;
  2010. case CNSS_DRIVER_EVENT_QDSS_TRACE_REQ_MEM:
  2011. ret = cnss_qdss_trace_req_mem_hdlr(plat_priv);
  2012. break;
  2013. case CNSS_DRIVER_EVENT_FW_MEM_FILE_SAVE:
  2014. ret = cnss_fw_mem_file_save_hdlr(plat_priv,
  2015. event->data);
  2016. break;
  2017. case CNSS_DRIVER_EVENT_QDSS_TRACE_FREE:
  2018. ret = cnss_qdss_trace_free_hdlr(plat_priv);
  2019. break;
  2020. case CNSS_DRIVER_EVENT_QDSS_TRACE_REQ_DATA:
  2021. ret = cnss_qdss_trace_req_data_hdlr(plat_priv,
  2022. event->data);
  2023. break;
  2024. default:
  2025. cnss_pr_err("Invalid driver event type: %d",
  2026. event->type);
  2027. kfree(event);
  2028. spin_lock_irqsave(&plat_priv->event_lock, flags);
  2029. continue;
  2030. }
  2031. spin_lock_irqsave(&plat_priv->event_lock, flags);
  2032. if (event->sync) {
  2033. event->ret = ret;
  2034. complete(&event->complete);
  2035. continue;
  2036. }
  2037. spin_unlock_irqrestore(&plat_priv->event_lock, flags);
  2038. kfree(event);
  2039. spin_lock_irqsave(&plat_priv->event_lock, flags);
  2040. }
  2041. spin_unlock_irqrestore(&plat_priv->event_lock, flags);
  2042. cnss_pm_relax(plat_priv);
  2043. }
  2044. #if IS_ENABLED(CONFIG_MSM_SUBSYSTEM_RESTART)
  2045. int cnss_register_subsys(struct cnss_plat_data *plat_priv)
  2046. {
  2047. int ret = 0;
  2048. struct cnss_subsys_info *subsys_info;
  2049. subsys_info = &plat_priv->subsys_info;
  2050. subsys_info->subsys_desc.name = "wlan";
  2051. subsys_info->subsys_desc.owner = THIS_MODULE;
  2052. subsys_info->subsys_desc.powerup = cnss_subsys_powerup;
  2053. subsys_info->subsys_desc.shutdown = cnss_subsys_shutdown;
  2054. subsys_info->subsys_desc.ramdump = cnss_subsys_ramdump;
  2055. subsys_info->subsys_desc.crash_shutdown = cnss_subsys_crash_shutdown;
  2056. subsys_info->subsys_desc.dev = &plat_priv->plat_dev->dev;
  2057. subsys_info->subsys_device = subsys_register(&subsys_info->subsys_desc);
  2058. if (IS_ERR(subsys_info->subsys_device)) {
  2059. ret = PTR_ERR(subsys_info->subsys_device);
  2060. cnss_pr_err("Failed to register subsys, err = %d\n", ret);
  2061. goto out;
  2062. }
  2063. subsys_info->subsys_handle =
  2064. subsystem_get(subsys_info->subsys_desc.name);
  2065. if (!subsys_info->subsys_handle) {
  2066. cnss_pr_err("Failed to get subsys_handle!\n");
  2067. ret = -EINVAL;
  2068. goto unregister_subsys;
  2069. } else if (IS_ERR(subsys_info->subsys_handle)) {
  2070. ret = PTR_ERR(subsys_info->subsys_handle);
  2071. cnss_pr_err("Failed to do subsystem_get, err = %d\n", ret);
  2072. goto unregister_subsys;
  2073. }
  2074. return 0;
  2075. unregister_subsys:
  2076. subsys_unregister(subsys_info->subsys_device);
  2077. out:
  2078. return ret;
  2079. }
  2080. void cnss_unregister_subsys(struct cnss_plat_data *plat_priv)
  2081. {
  2082. struct cnss_subsys_info *subsys_info;
  2083. subsys_info = &plat_priv->subsys_info;
  2084. subsystem_put(subsys_info->subsys_handle);
  2085. subsys_unregister(subsys_info->subsys_device);
  2086. }
  2087. static void *cnss_create_ramdump_device(struct cnss_plat_data *plat_priv)
  2088. {
  2089. struct cnss_subsys_info *subsys_info = &plat_priv->subsys_info;
  2090. return create_ramdump_device(subsys_info->subsys_desc.name,
  2091. subsys_info->subsys_desc.dev);
  2092. }
  2093. static void cnss_destroy_ramdump_device(struct cnss_plat_data *plat_priv,
  2094. void *ramdump_dev)
  2095. {
  2096. destroy_ramdump_device(ramdump_dev);
  2097. }
  2098. int cnss_do_ramdump(struct cnss_plat_data *plat_priv)
  2099. {
  2100. struct cnss_ramdump_info *ramdump_info = &plat_priv->ramdump_info;
  2101. struct ramdump_segment segment;
  2102. memset(&segment, 0, sizeof(segment));
  2103. segment.v_address = (void __iomem *)ramdump_info->ramdump_va;
  2104. segment.size = ramdump_info->ramdump_size;
  2105. return qcom_ramdump(ramdump_info->ramdump_dev, &segment, 1);
  2106. }
  2107. int cnss_do_elf_ramdump(struct cnss_plat_data *plat_priv)
  2108. {
  2109. struct cnss_ramdump_info_v2 *info_v2 = &plat_priv->ramdump_info_v2;
  2110. struct cnss_dump_data *dump_data = &info_v2->dump_data;
  2111. struct cnss_dump_seg *dump_seg = info_v2->dump_data_vaddr;
  2112. struct ramdump_segment *ramdump_segs, *s;
  2113. struct cnss_dump_meta_info meta_info = {0};
  2114. int i, ret = 0;
  2115. ramdump_segs = kcalloc(dump_data->nentries + 1,
  2116. sizeof(*ramdump_segs),
  2117. GFP_KERNEL);
  2118. if (!ramdump_segs)
  2119. return -ENOMEM;
  2120. s = ramdump_segs + 1;
  2121. for (i = 0; i < dump_data->nentries; i++) {
  2122. if (dump_seg->type >= CNSS_FW_DUMP_TYPE_MAX) {
  2123. cnss_pr_err("Unsupported dump type: %d",
  2124. dump_seg->type);
  2125. continue;
  2126. }
  2127. if (meta_info.entry[dump_seg->type].entry_start == 0) {
  2128. meta_info.entry[dump_seg->type].type = dump_seg->type;
  2129. meta_info.entry[dump_seg->type].entry_start = i + 1;
  2130. }
  2131. meta_info.entry[dump_seg->type].entry_num++;
  2132. s->address = dump_seg->address;
  2133. s->v_address = (void __iomem *)dump_seg->v_address;
  2134. s->size = dump_seg->size;
  2135. s++;
  2136. dump_seg++;
  2137. }
  2138. meta_info.magic = CNSS_RAMDUMP_MAGIC;
  2139. meta_info.version = CNSS_RAMDUMP_VERSION;
  2140. meta_info.chipset = plat_priv->device_id;
  2141. meta_info.total_entries = CNSS_FW_DUMP_TYPE_MAX;
  2142. ramdump_segs->v_address = (void __iomem *)(&meta_info);
  2143. ramdump_segs->size = sizeof(meta_info);
  2144. ret = qcom_elf_ramdump(info_v2->ramdump_dev, ramdump_segs,
  2145. dump_data->nentries + 1);
  2146. kfree(ramdump_segs);
  2147. return ret;
  2148. }
  2149. #else
  2150. static int cnss_panic_handler(struct notifier_block *nb, unsigned long action,
  2151. void *data)
  2152. {
  2153. struct cnss_plat_data *plat_priv =
  2154. container_of(nb, struct cnss_plat_data, panic_nb);
  2155. cnss_bus_dev_crash_shutdown(plat_priv);
  2156. return NOTIFY_DONE;
  2157. }
  2158. int cnss_register_subsys(struct cnss_plat_data *plat_priv)
  2159. {
  2160. int ret;
  2161. if (!plat_priv)
  2162. return -ENODEV;
  2163. plat_priv->panic_nb.notifier_call = cnss_panic_handler;
  2164. ret = atomic_notifier_chain_register(&panic_notifier_list,
  2165. &plat_priv->panic_nb);
  2166. if (ret) {
  2167. cnss_pr_err("Failed to register panic handler\n");
  2168. return -EINVAL;
  2169. }
  2170. return 0;
  2171. }
  2172. void cnss_unregister_subsys(struct cnss_plat_data *plat_priv)
  2173. {
  2174. int ret;
  2175. ret = atomic_notifier_chain_unregister(&panic_notifier_list,
  2176. &plat_priv->panic_nb);
  2177. if (ret)
  2178. cnss_pr_err("Failed to unregister panic handler\n");
  2179. }
  2180. #if IS_ENABLED(CONFIG_QCOM_MEMORY_DUMP_V2)
  2181. static void *cnss_create_ramdump_device(struct cnss_plat_data *plat_priv)
  2182. {
  2183. return &plat_priv->plat_dev->dev;
  2184. }
  2185. static void cnss_destroy_ramdump_device(struct cnss_plat_data *plat_priv,
  2186. void *ramdump_dev)
  2187. {
  2188. }
  2189. #endif
  2190. #if IS_ENABLED(CONFIG_QCOM_RAMDUMP)
  2191. int cnss_do_ramdump(struct cnss_plat_data *plat_priv)
  2192. {
  2193. struct cnss_ramdump_info *ramdump_info = &plat_priv->ramdump_info;
  2194. struct qcom_dump_segment segment;
  2195. struct list_head head;
  2196. INIT_LIST_HEAD(&head);
  2197. memset(&segment, 0, sizeof(segment));
  2198. segment.va = ramdump_info->ramdump_va;
  2199. segment.size = ramdump_info->ramdump_size;
  2200. list_add(&segment.node, &head);
  2201. return qcom_dump(&head, ramdump_info->ramdump_dev);
  2202. }
  2203. #else
  2204. int cnss_do_ramdump(struct cnss_plat_data *plat_priv)
  2205. {
  2206. return 0;
  2207. }
  2208. /* Using completion event inside dynamically allocated ramdump_desc
  2209. * may result a race between freeing the event after setting it to
  2210. * complete inside dev coredump free callback and the thread that is
  2211. * waiting for completion.
  2212. */
  2213. DECLARE_COMPLETION(dump_done);
  2214. #define TIMEOUT_SAVE_DUMP_MS 30000
  2215. #define SIZEOF_ELF_STRUCT(__xhdr) \
  2216. static inline size_t sizeof_elf_##__xhdr(unsigned char class) \
  2217. { \
  2218. if (class == ELFCLASS32) \
  2219. return sizeof(struct elf32_##__xhdr); \
  2220. else \
  2221. return sizeof(struct elf64_##__xhdr); \
  2222. }
  2223. SIZEOF_ELF_STRUCT(phdr)
  2224. SIZEOF_ELF_STRUCT(hdr)
  2225. #define set_xhdr_property(__xhdr, arg, class, member, value) \
  2226. do { \
  2227. if (class == ELFCLASS32) \
  2228. ((struct elf32_##__xhdr *)arg)->member = value; \
  2229. else \
  2230. ((struct elf64_##__xhdr *)arg)->member = value; \
  2231. } while (0)
  2232. #define set_ehdr_property(arg, class, member, value) \
  2233. set_xhdr_property(hdr, arg, class, member, value)
  2234. #define set_phdr_property(arg, class, member, value) \
  2235. set_xhdr_property(phdr, arg, class, member, value)
  2236. /* These replace qcom_ramdump driver APIs called from common API
  2237. * cnss_do_elf_dump() by the ones defined here.
  2238. */
  2239. #define qcom_dump_segment cnss_qcom_dump_segment
  2240. #define qcom_elf_dump cnss_qcom_elf_dump
  2241. #define dump_enabled cnss_dump_enabled
  2242. struct cnss_qcom_dump_segment {
  2243. struct list_head node;
  2244. dma_addr_t da;
  2245. void *va;
  2246. size_t size;
  2247. };
  2248. struct cnss_qcom_ramdump_desc {
  2249. void *data;
  2250. struct completion dump_done;
  2251. };
  2252. static ssize_t cnss_qcom_devcd_readv(char *buffer, loff_t offset, size_t count,
  2253. void *data, size_t datalen)
  2254. {
  2255. struct cnss_qcom_ramdump_desc *desc = data;
  2256. return memory_read_from_buffer(buffer, count, &offset, desc->data,
  2257. datalen);
  2258. }
  2259. static void cnss_qcom_devcd_freev(void *data)
  2260. {
  2261. struct cnss_qcom_ramdump_desc *desc = data;
  2262. cnss_pr_dbg("Free dump data for dev coredump\n");
  2263. complete(&dump_done);
  2264. vfree(desc->data);
  2265. kfree(desc);
  2266. }
  2267. static int cnss_qcom_devcd_dump(struct device *dev, void *data, size_t datalen,
  2268. gfp_t gfp)
  2269. {
  2270. struct cnss_qcom_ramdump_desc *desc;
  2271. unsigned int timeout = TIMEOUT_SAVE_DUMP_MS;
  2272. int ret;
  2273. desc = kmalloc(sizeof(*desc), GFP_KERNEL);
  2274. if (!desc)
  2275. return -ENOMEM;
  2276. desc->data = data;
  2277. reinit_completion(&dump_done);
  2278. dev_coredumpm(dev, NULL, desc, datalen, gfp,
  2279. cnss_qcom_devcd_readv, cnss_qcom_devcd_freev);
  2280. ret = wait_for_completion_timeout(&dump_done,
  2281. msecs_to_jiffies(timeout));
  2282. if (!ret)
  2283. cnss_pr_err("Timeout waiting (%dms) for saving dump to file system\n",
  2284. timeout);
  2285. return ret ? 0 : -ETIMEDOUT;
  2286. }
  2287. /* Since the elf32 and elf64 identification is identical apart from
  2288. * the class, use elf32 by default.
  2289. */
  2290. static void init_elf_identification(struct elf32_hdr *ehdr, unsigned char class)
  2291. {
  2292. memcpy(ehdr->e_ident, ELFMAG, SELFMAG);
  2293. ehdr->e_ident[EI_CLASS] = class;
  2294. ehdr->e_ident[EI_DATA] = ELFDATA2LSB;
  2295. ehdr->e_ident[EI_VERSION] = EV_CURRENT;
  2296. ehdr->e_ident[EI_OSABI] = ELFOSABI_NONE;
  2297. }
  2298. int cnss_qcom_elf_dump(struct list_head *segs, struct device *dev,
  2299. unsigned char class)
  2300. {
  2301. struct cnss_qcom_dump_segment *segment;
  2302. void *phdr, *ehdr;
  2303. size_t data_size, offset;
  2304. int phnum = 0;
  2305. void *data;
  2306. void __iomem *ptr;
  2307. if (!segs || list_empty(segs))
  2308. return -EINVAL;
  2309. data_size = sizeof_elf_hdr(class);
  2310. list_for_each_entry(segment, segs, node) {
  2311. data_size += sizeof_elf_phdr(class) + segment->size;
  2312. phnum++;
  2313. }
  2314. data = vmalloc(data_size);
  2315. if (!data)
  2316. return -ENOMEM;
  2317. cnss_pr_dbg("Creating ELF file with size %d\n", data_size);
  2318. ehdr = data;
  2319. memset(ehdr, 0, sizeof_elf_hdr(class));
  2320. init_elf_identification(ehdr, class);
  2321. set_ehdr_property(ehdr, class, e_type, ET_CORE);
  2322. set_ehdr_property(ehdr, class, e_machine, EM_NONE);
  2323. set_ehdr_property(ehdr, class, e_version, EV_CURRENT);
  2324. set_ehdr_property(ehdr, class, e_phoff, sizeof_elf_hdr(class));
  2325. set_ehdr_property(ehdr, class, e_ehsize, sizeof_elf_hdr(class));
  2326. set_ehdr_property(ehdr, class, e_phentsize, sizeof_elf_phdr(class));
  2327. set_ehdr_property(ehdr, class, e_phnum, phnum);
  2328. phdr = data + sizeof_elf_hdr(class);
  2329. offset = sizeof_elf_hdr(class) + sizeof_elf_phdr(class) * phnum;
  2330. list_for_each_entry(segment, segs, node) {
  2331. memset(phdr, 0, sizeof_elf_phdr(class));
  2332. set_phdr_property(phdr, class, p_type, PT_LOAD);
  2333. set_phdr_property(phdr, class, p_offset, offset);
  2334. set_phdr_property(phdr, class, p_vaddr, segment->da);
  2335. set_phdr_property(phdr, class, p_paddr, segment->da);
  2336. set_phdr_property(phdr, class, p_filesz, segment->size);
  2337. set_phdr_property(phdr, class, p_memsz, segment->size);
  2338. set_phdr_property(phdr, class, p_flags, PF_R | PF_W | PF_X);
  2339. set_phdr_property(phdr, class, p_align, 0);
  2340. if (segment->va) {
  2341. memcpy(data + offset, segment->va, segment->size);
  2342. } else {
  2343. ptr = devm_ioremap(dev, segment->da, segment->size);
  2344. if (!ptr) {
  2345. cnss_pr_err("Invalid coredump segment (%pad, %zu)\n",
  2346. &segment->da, segment->size);
  2347. memset(data + offset, 0xff, segment->size);
  2348. } else {
  2349. memcpy_fromio(data + offset, ptr,
  2350. segment->size);
  2351. }
  2352. }
  2353. offset += segment->size;
  2354. phdr += sizeof_elf_phdr(class);
  2355. }
  2356. return cnss_qcom_devcd_dump(dev, data, data_size, GFP_KERNEL);
  2357. }
  2358. /* Saving dump to file system is always needed in this case. */
  2359. static bool cnss_dump_enabled(void)
  2360. {
  2361. return true;
  2362. }
  2363. #endif /* CONFIG_QCOM_RAMDUMP */
  2364. int cnss_do_elf_ramdump(struct cnss_plat_data *plat_priv)
  2365. {
  2366. struct cnss_ramdump_info_v2 *info_v2 = &plat_priv->ramdump_info_v2;
  2367. struct cnss_dump_data *dump_data = &info_v2->dump_data;
  2368. struct cnss_dump_seg *dump_seg = info_v2->dump_data_vaddr;
  2369. struct qcom_dump_segment *seg;
  2370. struct cnss_dump_meta_info meta_info = {0};
  2371. struct list_head head;
  2372. int i, ret = 0;
  2373. if (!dump_enabled()) {
  2374. cnss_pr_info("Dump collection is not enabled\n");
  2375. return ret;
  2376. }
  2377. INIT_LIST_HEAD(&head);
  2378. for (i = 0; i < dump_data->nentries; i++) {
  2379. if (dump_seg->type >= CNSS_FW_DUMP_TYPE_MAX) {
  2380. cnss_pr_err("Unsupported dump type: %d",
  2381. dump_seg->type);
  2382. continue;
  2383. }
  2384. seg = kcalloc(1, sizeof(*seg), GFP_KERNEL);
  2385. if (!seg)
  2386. continue;
  2387. if (meta_info.entry[dump_seg->type].entry_start == 0) {
  2388. meta_info.entry[dump_seg->type].type = dump_seg->type;
  2389. meta_info.entry[dump_seg->type].entry_start = i + 1;
  2390. }
  2391. meta_info.entry[dump_seg->type].entry_num++;
  2392. seg->da = dump_seg->address;
  2393. seg->va = dump_seg->v_address;
  2394. seg->size = dump_seg->size;
  2395. list_add_tail(&seg->node, &head);
  2396. dump_seg++;
  2397. }
  2398. seg = kcalloc(1, sizeof(*seg), GFP_KERNEL);
  2399. if (!seg)
  2400. goto do_elf_dump;
  2401. meta_info.magic = CNSS_RAMDUMP_MAGIC;
  2402. meta_info.version = CNSS_RAMDUMP_VERSION;
  2403. meta_info.chipset = plat_priv->device_id;
  2404. meta_info.total_entries = CNSS_FW_DUMP_TYPE_MAX;
  2405. seg->va = &meta_info;
  2406. seg->size = sizeof(meta_info);
  2407. list_add(&seg->node, &head);
  2408. do_elf_dump:
  2409. ret = qcom_elf_dump(&head, info_v2->ramdump_dev, ELF_CLASS);
  2410. while (!list_empty(&head)) {
  2411. seg = list_first_entry(&head, struct qcom_dump_segment, node);
  2412. list_del(&seg->node);
  2413. kfree(seg);
  2414. }
  2415. return ret;
  2416. }
  2417. #endif /* CONFIG_MSM_SUBSYSTEM_RESTART */
  2418. #if IS_ENABLED(CONFIG_QCOM_MEMORY_DUMP_V2)
  2419. static int cnss_init_dump_entry(struct cnss_plat_data *plat_priv)
  2420. {
  2421. struct cnss_ramdump_info *ramdump_info;
  2422. struct msm_dump_entry dump_entry;
  2423. ramdump_info = &plat_priv->ramdump_info;
  2424. ramdump_info->dump_data.addr = ramdump_info->ramdump_pa;
  2425. ramdump_info->dump_data.len = ramdump_info->ramdump_size;
  2426. ramdump_info->dump_data.version = CNSS_DUMP_FORMAT_VER;
  2427. ramdump_info->dump_data.magic = CNSS_DUMP_MAGIC_VER_V2;
  2428. strlcpy(ramdump_info->dump_data.name, CNSS_DUMP_NAME,
  2429. sizeof(ramdump_info->dump_data.name));
  2430. dump_entry.id = MSM_DUMP_DATA_CNSS_WLAN;
  2431. dump_entry.addr = virt_to_phys(&ramdump_info->dump_data);
  2432. return msm_dump_data_register_nominidump(MSM_DUMP_TABLE_APPS,
  2433. &dump_entry);
  2434. }
  2435. static int cnss_register_ramdump_v1(struct cnss_plat_data *plat_priv)
  2436. {
  2437. int ret = 0;
  2438. struct device *dev;
  2439. struct cnss_ramdump_info *ramdump_info;
  2440. u32 ramdump_size = 0;
  2441. dev = &plat_priv->plat_dev->dev;
  2442. ramdump_info = &plat_priv->ramdump_info;
  2443. if (plat_priv->dt_type != CNSS_DTT_MULTIEXCHG) {
  2444. /* dt type: legacy or converged */
  2445. ret = of_property_read_u32(dev->of_node,
  2446. "qcom,wlan-ramdump-dynamic",
  2447. &ramdump_size);
  2448. } else {
  2449. ret = of_property_read_u32(plat_priv->dev_node,
  2450. "qcom,wlan-ramdump-dynamic",
  2451. &ramdump_size);
  2452. }
  2453. if (ret == 0) {
  2454. ramdump_info->ramdump_va =
  2455. dma_alloc_coherent(dev, ramdump_size,
  2456. &ramdump_info->ramdump_pa,
  2457. GFP_KERNEL);
  2458. if (ramdump_info->ramdump_va)
  2459. ramdump_info->ramdump_size = ramdump_size;
  2460. }
  2461. cnss_pr_dbg("ramdump va: %pK, pa: %pa\n",
  2462. ramdump_info->ramdump_va, &ramdump_info->ramdump_pa);
  2463. if (ramdump_info->ramdump_size == 0) {
  2464. cnss_pr_info("Ramdump will not be collected");
  2465. goto out;
  2466. }
  2467. ret = cnss_init_dump_entry(plat_priv);
  2468. if (ret) {
  2469. cnss_pr_err("Failed to setup dump table, err = %d\n", ret);
  2470. goto free_ramdump;
  2471. }
  2472. ramdump_info->ramdump_dev = cnss_create_ramdump_device(plat_priv);
  2473. if (!ramdump_info->ramdump_dev) {
  2474. cnss_pr_err("Failed to create ramdump device!");
  2475. ret = -ENOMEM;
  2476. goto free_ramdump;
  2477. }
  2478. return 0;
  2479. free_ramdump:
  2480. dma_free_coherent(dev, ramdump_info->ramdump_size,
  2481. ramdump_info->ramdump_va, ramdump_info->ramdump_pa);
  2482. out:
  2483. return ret;
  2484. }
  2485. static void cnss_unregister_ramdump_v1(struct cnss_plat_data *plat_priv)
  2486. {
  2487. struct device *dev;
  2488. struct cnss_ramdump_info *ramdump_info;
  2489. dev = &plat_priv->plat_dev->dev;
  2490. ramdump_info = &plat_priv->ramdump_info;
  2491. if (ramdump_info->ramdump_dev)
  2492. cnss_destroy_ramdump_device(plat_priv,
  2493. ramdump_info->ramdump_dev);
  2494. if (ramdump_info->ramdump_va)
  2495. dma_free_coherent(dev, ramdump_info->ramdump_size,
  2496. ramdump_info->ramdump_va,
  2497. ramdump_info->ramdump_pa);
  2498. }
  2499. /**
  2500. * cnss_ignore_dump_data_reg_fail - Ignore Ramdump table register failure
  2501. * @ret: Error returned by msm_dump_data_register_nominidump
  2502. *
  2503. * For Lahaina GKI boot, we dont have support for mem dump feature. So
  2504. * ignore failure.
  2505. *
  2506. * Return: Same given error code if mem dump feature enabled, 0 otherwise
  2507. */
  2508. static int cnss_ignore_dump_data_reg_fail(int ret)
  2509. {
  2510. return ret;
  2511. }
  2512. static int cnss_register_ramdump_v2(struct cnss_plat_data *plat_priv)
  2513. {
  2514. int ret = 0;
  2515. struct cnss_ramdump_info_v2 *info_v2;
  2516. struct cnss_dump_data *dump_data;
  2517. struct msm_dump_entry dump_entry;
  2518. struct device *dev = &plat_priv->plat_dev->dev;
  2519. u32 ramdump_size = 0;
  2520. info_v2 = &plat_priv->ramdump_info_v2;
  2521. dump_data = &info_v2->dump_data;
  2522. if (plat_priv->dt_type != CNSS_DTT_MULTIEXCHG) {
  2523. /* dt type: legacy or converged */
  2524. ret = of_property_read_u32(dev->of_node,
  2525. "qcom,wlan-ramdump-dynamic",
  2526. &ramdump_size);
  2527. } else {
  2528. ret = of_property_read_u32(plat_priv->dev_node,
  2529. "qcom,wlan-ramdump-dynamic",
  2530. &ramdump_size);
  2531. }
  2532. if (ret == 0)
  2533. info_v2->ramdump_size = ramdump_size;
  2534. cnss_pr_dbg("Ramdump size 0x%lx\n", info_v2->ramdump_size);
  2535. info_v2->dump_data_vaddr = kzalloc(CNSS_DUMP_DESC_SIZE, GFP_KERNEL);
  2536. if (!info_v2->dump_data_vaddr)
  2537. return -ENOMEM;
  2538. dump_data->paddr = virt_to_phys(info_v2->dump_data_vaddr);
  2539. dump_data->version = CNSS_DUMP_FORMAT_VER_V2;
  2540. dump_data->magic = CNSS_DUMP_MAGIC_VER_V2;
  2541. dump_data->seg_version = CNSS_DUMP_SEG_VER;
  2542. strlcpy(dump_data->name, CNSS_DUMP_NAME,
  2543. sizeof(dump_data->name));
  2544. dump_entry.id = MSM_DUMP_DATA_CNSS_WLAN;
  2545. dump_entry.addr = virt_to_phys(dump_data);
  2546. ret = msm_dump_data_register_nominidump(MSM_DUMP_TABLE_APPS,
  2547. &dump_entry);
  2548. if (ret) {
  2549. ret = cnss_ignore_dump_data_reg_fail(ret);
  2550. cnss_pr_err("Failed to setup dump table, %s (%d)\n",
  2551. ret ? "Error" : "Ignoring", ret);
  2552. goto free_ramdump;
  2553. }
  2554. info_v2->ramdump_dev = cnss_create_ramdump_device(plat_priv);
  2555. if (!info_v2->ramdump_dev) {
  2556. cnss_pr_err("Failed to create ramdump device!\n");
  2557. ret = -ENOMEM;
  2558. goto free_ramdump;
  2559. }
  2560. return 0;
  2561. free_ramdump:
  2562. kfree(info_v2->dump_data_vaddr);
  2563. info_v2->dump_data_vaddr = NULL;
  2564. return ret;
  2565. }
  2566. static void cnss_unregister_ramdump_v2(struct cnss_plat_data *plat_priv)
  2567. {
  2568. struct cnss_ramdump_info_v2 *info_v2;
  2569. info_v2 = &plat_priv->ramdump_info_v2;
  2570. if (info_v2->ramdump_dev)
  2571. cnss_destroy_ramdump_device(plat_priv, info_v2->ramdump_dev);
  2572. kfree(info_v2->dump_data_vaddr);
  2573. info_v2->dump_data_vaddr = NULL;
  2574. info_v2->dump_data_valid = false;
  2575. }
  2576. int cnss_register_ramdump(struct cnss_plat_data *plat_priv)
  2577. {
  2578. int ret = 0;
  2579. switch (plat_priv->device_id) {
  2580. case QCA6174_DEVICE_ID:
  2581. ret = cnss_register_ramdump_v1(plat_priv);
  2582. break;
  2583. case QCA6290_DEVICE_ID:
  2584. case QCA6390_DEVICE_ID:
  2585. case QCA6490_DEVICE_ID:
  2586. case KIWI_DEVICE_ID:
  2587. case MANGO_DEVICE_ID:
  2588. ret = cnss_register_ramdump_v2(plat_priv);
  2589. break;
  2590. default:
  2591. cnss_pr_err("Unknown device ID: 0x%lx\n", plat_priv->device_id);
  2592. ret = -ENODEV;
  2593. break;
  2594. }
  2595. return ret;
  2596. }
  2597. void cnss_unregister_ramdump(struct cnss_plat_data *plat_priv)
  2598. {
  2599. switch (plat_priv->device_id) {
  2600. case QCA6174_DEVICE_ID:
  2601. cnss_unregister_ramdump_v1(plat_priv);
  2602. break;
  2603. case QCA6290_DEVICE_ID:
  2604. case QCA6390_DEVICE_ID:
  2605. case QCA6490_DEVICE_ID:
  2606. case KIWI_DEVICE_ID:
  2607. case MANGO_DEVICE_ID:
  2608. cnss_unregister_ramdump_v2(plat_priv);
  2609. break;
  2610. default:
  2611. cnss_pr_err("Unknown device ID: 0x%lx\n", plat_priv->device_id);
  2612. break;
  2613. }
  2614. }
  2615. #else
  2616. int cnss_register_ramdump(struct cnss_plat_data *plat_priv)
  2617. {
  2618. struct cnss_ramdump_info_v2 *info_v2 = &plat_priv->ramdump_info_v2;
  2619. struct cnss_dump_data *dump_data = dump_data = &info_v2->dump_data;
  2620. struct device *dev = &plat_priv->plat_dev->dev;
  2621. u32 ramdump_size = 0;
  2622. if (of_property_read_u32(dev->of_node, "qcom,wlan-ramdump-dynamic",
  2623. &ramdump_size) == 0)
  2624. info_v2->ramdump_size = ramdump_size;
  2625. cnss_pr_dbg("Ramdump size 0x%lx\n", info_v2->ramdump_size);
  2626. info_v2->dump_data_vaddr = kzalloc(CNSS_DUMP_DESC_SIZE, GFP_KERNEL);
  2627. if (!info_v2->dump_data_vaddr)
  2628. return -ENOMEM;
  2629. dump_data->paddr = virt_to_phys(info_v2->dump_data_vaddr);
  2630. dump_data->version = CNSS_DUMP_FORMAT_VER_V2;
  2631. dump_data->magic = CNSS_DUMP_MAGIC_VER_V2;
  2632. dump_data->seg_version = CNSS_DUMP_SEG_VER;
  2633. strlcpy(dump_data->name, CNSS_DUMP_NAME,
  2634. sizeof(dump_data->name));
  2635. info_v2->ramdump_dev = dev;
  2636. return 0;
  2637. }
  2638. void cnss_unregister_ramdump(struct cnss_plat_data *plat_priv)
  2639. {
  2640. struct cnss_ramdump_info_v2 *info_v2 = &plat_priv->ramdump_info_v2;
  2641. info_v2->ramdump_dev = NULL;
  2642. kfree(info_v2->dump_data_vaddr);
  2643. info_v2->dump_data_vaddr = NULL;
  2644. info_v2->dump_data_valid = false;
  2645. }
  2646. #endif /* CONFIG_QCOM_MEMORY_DUMP_V2 */
  2647. #if IS_ENABLED(CONFIG_QCOM_MINIDUMP)
  2648. int cnss_va_to_pa(struct device *dev, size_t size, void *va, dma_addr_t dma,
  2649. phys_addr_t *pa, unsigned long attrs)
  2650. {
  2651. struct sg_table sgt;
  2652. int ret;
  2653. ret = dma_get_sgtable_attrs(dev, &sgt, va, dma, size, attrs);
  2654. if (ret) {
  2655. cnss_pr_err("Failed to get sgtable for va: 0x%pK, dma: %pa, size: 0x%zx, attrs: 0x%x\n",
  2656. va, &dma, size, attrs);
  2657. return -EINVAL;
  2658. }
  2659. *pa = page_to_phys(sg_page(sgt.sgl));
  2660. sg_free_table(&sgt);
  2661. return 0;
  2662. }
  2663. int cnss_minidump_add_region(struct cnss_plat_data *plat_priv,
  2664. enum cnss_fw_dump_type type, int seg_no,
  2665. void *va, phys_addr_t pa, size_t size)
  2666. {
  2667. struct md_region md_entry;
  2668. int ret;
  2669. switch (type) {
  2670. case CNSS_FW_IMAGE:
  2671. snprintf(md_entry.name, sizeof(md_entry.name), "FBC_%X",
  2672. seg_no);
  2673. break;
  2674. case CNSS_FW_RDDM:
  2675. snprintf(md_entry.name, sizeof(md_entry.name), "RDDM_%X",
  2676. seg_no);
  2677. break;
  2678. case CNSS_FW_REMOTE_HEAP:
  2679. snprintf(md_entry.name, sizeof(md_entry.name), "RHEAP_%X",
  2680. seg_no);
  2681. break;
  2682. default:
  2683. cnss_pr_err("Unknown dump type ID: %d\n", type);
  2684. return -EINVAL;
  2685. }
  2686. md_entry.phys_addr = pa;
  2687. md_entry.virt_addr = (uintptr_t)va;
  2688. md_entry.size = size;
  2689. md_entry.id = MSM_DUMP_DATA_CNSS_WLAN;
  2690. cnss_pr_dbg("Mini dump region: %s, va: %pK, pa: %pa, size: 0x%zx\n",
  2691. md_entry.name, va, &pa, size);
  2692. ret = msm_minidump_add_region(&md_entry);
  2693. if (ret < 0)
  2694. cnss_pr_err("Failed to add mini dump region, err = %d\n", ret);
  2695. return ret;
  2696. }
  2697. int cnss_minidump_remove_region(struct cnss_plat_data *plat_priv,
  2698. enum cnss_fw_dump_type type, int seg_no,
  2699. void *va, phys_addr_t pa, size_t size)
  2700. {
  2701. struct md_region md_entry;
  2702. int ret;
  2703. switch (type) {
  2704. case CNSS_FW_IMAGE:
  2705. snprintf(md_entry.name, sizeof(md_entry.name), "FBC_%X",
  2706. seg_no);
  2707. break;
  2708. case CNSS_FW_RDDM:
  2709. snprintf(md_entry.name, sizeof(md_entry.name), "RDDM_%X",
  2710. seg_no);
  2711. break;
  2712. case CNSS_FW_REMOTE_HEAP:
  2713. snprintf(md_entry.name, sizeof(md_entry.name), "RHEAP_%X",
  2714. seg_no);
  2715. break;
  2716. default:
  2717. cnss_pr_err("Unknown dump type ID: %d\n", type);
  2718. return -EINVAL;
  2719. }
  2720. md_entry.phys_addr = pa;
  2721. md_entry.virt_addr = (uintptr_t)va;
  2722. md_entry.size = size;
  2723. md_entry.id = MSM_DUMP_DATA_CNSS_WLAN;
  2724. cnss_pr_dbg("Remove mini dump region: %s, va: %pK, pa: %pa, size: 0x%zx\n",
  2725. md_entry.name, va, &pa, size);
  2726. ret = msm_minidump_remove_region(&md_entry);
  2727. if (ret)
  2728. cnss_pr_err("Failed to remove mini dump region, err = %d\n",
  2729. ret);
  2730. return ret;
  2731. }
  2732. #else
  2733. int cnss_va_to_pa(struct device *dev, size_t size, void *va, dma_addr_t dma,
  2734. phys_addr_t *pa, unsigned long attrs)
  2735. {
  2736. return 0;
  2737. }
  2738. int cnss_minidump_add_region(struct cnss_plat_data *plat_priv,
  2739. enum cnss_fw_dump_type type, int seg_no,
  2740. void *va, phys_addr_t pa, size_t size)
  2741. {
  2742. return 0;
  2743. }
  2744. int cnss_minidump_remove_region(struct cnss_plat_data *plat_priv,
  2745. enum cnss_fw_dump_type type, int seg_no,
  2746. void *va, phys_addr_t pa, size_t size)
  2747. {
  2748. return 0;
  2749. }
  2750. #endif /* CONFIG_QCOM_MINIDUMP */
  2751. int cnss_request_firmware_direct(struct cnss_plat_data *plat_priv,
  2752. const struct firmware **fw_entry,
  2753. const char *filename)
  2754. {
  2755. if (IS_ENABLED(CONFIG_CNSS_REQ_FW_DIRECT))
  2756. return request_firmware_direct(fw_entry, filename,
  2757. &plat_priv->plat_dev->dev);
  2758. else
  2759. return firmware_request_nowarn(fw_entry, filename,
  2760. &plat_priv->plat_dev->dev);
  2761. }
  2762. #if IS_ENABLED(CONFIG_INTERCONNECT)
  2763. /**
  2764. * cnss_register_bus_scale() - Setup interconnect voting data
  2765. * @plat_priv: Platform data structure
  2766. *
  2767. * For different interconnect path configured in device tree setup voting data
  2768. * for list of bandwidth requirements.
  2769. *
  2770. * Result: 0 for success. -EINVAL if not configured
  2771. */
  2772. static int cnss_register_bus_scale(struct cnss_plat_data *plat_priv)
  2773. {
  2774. int ret = -EINVAL;
  2775. u32 idx, i, j, cfg_arr_size, *cfg_arr = NULL;
  2776. struct cnss_bus_bw_info *bus_bw_info, *tmp;
  2777. struct device *dev = &plat_priv->plat_dev->dev;
  2778. INIT_LIST_HEAD(&plat_priv->icc.list_head);
  2779. ret = of_property_read_u32(dev->of_node,
  2780. "qcom,icc-path-count",
  2781. &plat_priv->icc.path_count);
  2782. if (ret) {
  2783. cnss_pr_dbg("Platform Bus Interconnect path not configured\n");
  2784. return 0;
  2785. }
  2786. ret = of_property_read_u32(plat_priv->plat_dev->dev.of_node,
  2787. "qcom,bus-bw-cfg-count",
  2788. &plat_priv->icc.bus_bw_cfg_count);
  2789. if (ret) {
  2790. cnss_pr_err("Failed to get Bus BW Config table size\n");
  2791. goto cleanup;
  2792. }
  2793. cfg_arr_size = plat_priv->icc.path_count *
  2794. plat_priv->icc.bus_bw_cfg_count * CNSS_ICC_VOTE_MAX;
  2795. cfg_arr = kcalloc(cfg_arr_size, sizeof(*cfg_arr), GFP_KERNEL);
  2796. if (!cfg_arr) {
  2797. cnss_pr_err("Failed to alloc cfg table mem\n");
  2798. ret = -ENOMEM;
  2799. goto cleanup;
  2800. }
  2801. ret = of_property_read_u32_array(plat_priv->plat_dev->dev.of_node,
  2802. "qcom,bus-bw-cfg", cfg_arr,
  2803. cfg_arr_size);
  2804. if (ret) {
  2805. cnss_pr_err("Invalid Bus BW Config Table\n");
  2806. goto cleanup;
  2807. }
  2808. cnss_pr_dbg("ICC Path_Count: %d BW_CFG_Count: %d\n",
  2809. plat_priv->icc.path_count, plat_priv->icc.bus_bw_cfg_count);
  2810. for (idx = 0; idx < plat_priv->icc.path_count; idx++) {
  2811. bus_bw_info = devm_kzalloc(dev, sizeof(*bus_bw_info),
  2812. GFP_KERNEL);
  2813. if (!bus_bw_info) {
  2814. ret = -ENOMEM;
  2815. goto out;
  2816. }
  2817. ret = of_property_read_string_index(dev->of_node,
  2818. "interconnect-names", idx,
  2819. &bus_bw_info->icc_name);
  2820. if (ret)
  2821. goto out;
  2822. bus_bw_info->icc_path =
  2823. of_icc_get(&plat_priv->plat_dev->dev,
  2824. bus_bw_info->icc_name);
  2825. if (IS_ERR(bus_bw_info->icc_path)) {
  2826. ret = PTR_ERR(bus_bw_info->icc_path);
  2827. if (ret != -EPROBE_DEFER) {
  2828. cnss_pr_err("Failed to get Interconnect path for %s. Err: %d\n",
  2829. bus_bw_info->icc_name, ret);
  2830. goto out;
  2831. }
  2832. }
  2833. bus_bw_info->cfg_table =
  2834. devm_kcalloc(dev, plat_priv->icc.bus_bw_cfg_count,
  2835. sizeof(*bus_bw_info->cfg_table),
  2836. GFP_KERNEL);
  2837. if (!bus_bw_info->cfg_table) {
  2838. ret = -ENOMEM;
  2839. goto out;
  2840. }
  2841. cnss_pr_dbg("ICC Vote CFG for path: %s\n",
  2842. bus_bw_info->icc_name);
  2843. for (i = 0, j = (idx * plat_priv->icc.bus_bw_cfg_count *
  2844. CNSS_ICC_VOTE_MAX);
  2845. i < plat_priv->icc.bus_bw_cfg_count;
  2846. i++, j += 2) {
  2847. bus_bw_info->cfg_table[i].avg_bw = cfg_arr[j];
  2848. bus_bw_info->cfg_table[i].peak_bw = cfg_arr[j + 1];
  2849. cnss_pr_dbg("ICC Vote BW: %d avg: %d peak: %d\n",
  2850. i, bus_bw_info->cfg_table[i].avg_bw,
  2851. bus_bw_info->cfg_table[i].peak_bw);
  2852. }
  2853. list_add_tail(&bus_bw_info->list,
  2854. &plat_priv->icc.list_head);
  2855. }
  2856. kfree(cfg_arr);
  2857. return 0;
  2858. out:
  2859. list_for_each_entry_safe(bus_bw_info, tmp,
  2860. &plat_priv->icc.list_head, list) {
  2861. list_del(&bus_bw_info->list);
  2862. }
  2863. cleanup:
  2864. kfree(cfg_arr);
  2865. memset(&plat_priv->icc, 0, sizeof(plat_priv->icc));
  2866. return ret;
  2867. }
  2868. static void cnss_unregister_bus_scale(struct cnss_plat_data *plat_priv)
  2869. {
  2870. struct cnss_bus_bw_info *bus_bw_info, *tmp;
  2871. list_for_each_entry_safe(bus_bw_info, tmp,
  2872. &plat_priv->icc.list_head, list) {
  2873. list_del(&bus_bw_info->list);
  2874. if (bus_bw_info->icc_path)
  2875. icc_put(bus_bw_info->icc_path);
  2876. }
  2877. memset(&plat_priv->icc, 0, sizeof(plat_priv->icc));
  2878. }
  2879. #else
  2880. static int cnss_register_bus_scale(struct cnss_plat_data *plat_priv)
  2881. {
  2882. return 0;
  2883. }
  2884. static void cnss_unregister_bus_scale(struct cnss_plat_data *plat_priv) {}
  2885. #endif /* CONFIG_INTERCONNECT */
  2886. void cnss_daemon_connection_update_cb(void *cb_ctx, bool status)
  2887. {
  2888. struct cnss_plat_data *plat_priv = cb_ctx;
  2889. if (!plat_priv) {
  2890. cnss_pr_err("%s: Invalid context\n", __func__);
  2891. return;
  2892. }
  2893. if (status) {
  2894. cnss_pr_info("CNSS Daemon connected\n");
  2895. set_bit(CNSS_DAEMON_CONNECTED, &plat_priv->driver_state);
  2896. complete(&plat_priv->daemon_connected);
  2897. } else {
  2898. cnss_pr_info("CNSS Daemon disconnected\n");
  2899. reinit_completion(&plat_priv->daemon_connected);
  2900. clear_bit(CNSS_DAEMON_CONNECTED, &plat_priv->driver_state);
  2901. }
  2902. }
  2903. static ssize_t enable_hds_store(struct device *dev,
  2904. struct device_attribute *attr,
  2905. const char *buf, size_t count)
  2906. {
  2907. struct cnss_plat_data *plat_priv = dev_get_drvdata(dev);
  2908. unsigned int enable_hds = 0;
  2909. if (!plat_priv)
  2910. return -ENODEV;
  2911. if (sscanf(buf, "%du", &enable_hds) != 1) {
  2912. cnss_pr_err("Invalid enable_hds sysfs command\n");
  2913. return -EINVAL;
  2914. }
  2915. if (enable_hds)
  2916. plat_priv->hds_enabled = true;
  2917. else
  2918. plat_priv->hds_enabled = false;
  2919. cnss_pr_dbg("%s HDS file download, count is %zu\n",
  2920. plat_priv->hds_enabled ? "Enable" : "Disable", count);
  2921. return count;
  2922. }
  2923. static ssize_t recovery_show(struct device *dev,
  2924. struct device_attribute *attr,
  2925. char *buf)
  2926. {
  2927. struct cnss_plat_data *plat_priv = dev_get_drvdata(dev);
  2928. u32 buf_size = PAGE_SIZE;
  2929. u32 curr_len = 0;
  2930. u32 buf_written = 0;
  2931. if (!plat_priv)
  2932. return -ENODEV;
  2933. buf_written = scnprintf(buf, buf_size,
  2934. "Usage: echo [recovery_bitmap] > /sys/kernel/cnss/recovery\n"
  2935. "BIT0 -- wlan fw recovery\n"
  2936. "BIT1 -- wlan pcss recovery\n"
  2937. "---------------------------------\n");
  2938. curr_len += buf_written;
  2939. buf_written = scnprintf(buf + curr_len, buf_size - curr_len,
  2940. "WLAN recovery %s[%d]\n",
  2941. plat_priv->recovery_enabled ? "Enabled" : "Disabled",
  2942. plat_priv->recovery_enabled);
  2943. curr_len += buf_written;
  2944. buf_written = scnprintf(buf + curr_len, buf_size - curr_len,
  2945. "WLAN PCSS recovery %s[%d]\n",
  2946. plat_priv->recovery_pcss_enabled ? "Enabled" : "Disabled",
  2947. plat_priv->recovery_pcss_enabled);
  2948. curr_len += buf_written;
  2949. /*
  2950. * Now size of curr_len is not over page size for sure,
  2951. * later if new item or none-fixed size item added, need
  2952. * add check to make sure curr_len is not over page size.
  2953. */
  2954. return curr_len;
  2955. }
  2956. static ssize_t time_sync_period_show(struct device *dev,
  2957. struct device_attribute *attr,
  2958. char *buf)
  2959. {
  2960. struct cnss_plat_data *plat_priv = dev_get_drvdata(dev);
  2961. return scnprintf(buf, PAGE_SIZE, "%u ms\n",
  2962. plat_priv->ctrl_params.time_sync_period);
  2963. }
  2964. static ssize_t time_sync_period_store(struct device *dev,
  2965. struct device_attribute *attr,
  2966. const char *buf, size_t count)
  2967. {
  2968. struct cnss_plat_data *plat_priv = dev_get_drvdata(dev);
  2969. unsigned int time_sync_period = 0;
  2970. if (!plat_priv)
  2971. return -ENODEV;
  2972. if (sscanf(buf, "%du", &time_sync_period) != 1) {
  2973. cnss_pr_err("Invalid time sync sysfs command\n");
  2974. return -EINVAL;
  2975. }
  2976. if (time_sync_period >= CNSS_MIN_TIME_SYNC_PERIOD)
  2977. cnss_bus_update_time_sync_period(plat_priv, time_sync_period);
  2978. return count;
  2979. }
  2980. static ssize_t recovery_store(struct device *dev,
  2981. struct device_attribute *attr,
  2982. const char *buf, size_t count)
  2983. {
  2984. struct cnss_plat_data *plat_priv = dev_get_drvdata(dev);
  2985. unsigned int recovery = 0;
  2986. int ret;
  2987. if (!plat_priv)
  2988. return -ENODEV;
  2989. if (sscanf(buf, "%du", &recovery) != 1) {
  2990. cnss_pr_err("Invalid recovery sysfs command\n");
  2991. return -EINVAL;
  2992. }
  2993. plat_priv->recovery_enabled = !!(recovery & CNSS_WLAN_RECOVERY);
  2994. plat_priv->recovery_pcss_enabled = !!(recovery & CNSS_PCSS_RECOVERY);
  2995. cnss_pr_dbg("%s WLAN recovery, count is %zu\n",
  2996. plat_priv->recovery_enabled ? "Enable" : "Disable", count);
  2997. cnss_pr_dbg("%s PCSS recovery, count is %zu\n",
  2998. plat_priv->recovery_pcss_enabled ? "Enable" : "Disable", count);
  2999. ret = cnss_send_subsys_restart_level_msg(plat_priv);
  3000. if (ret < 0) {
  3001. cnss_pr_err("pcss recovery setting failed with ret %d\n", ret);
  3002. plat_priv->recovery_pcss_enabled = false;
  3003. return -EINVAL;
  3004. }
  3005. return count;
  3006. }
  3007. static ssize_t shutdown_store(struct device *dev,
  3008. struct device_attribute *attr,
  3009. const char *buf, size_t count)
  3010. {
  3011. struct cnss_plat_data *plat_priv = dev_get_drvdata(dev);
  3012. if (plat_priv) {
  3013. set_bit(CNSS_IN_REBOOT, &plat_priv->driver_state);
  3014. del_timer(&plat_priv->fw_boot_timer);
  3015. complete_all(&plat_priv->power_up_complete);
  3016. complete_all(&plat_priv->cal_complete);
  3017. }
  3018. cnss_pr_dbg("Received shutdown notification\n");
  3019. return count;
  3020. }
  3021. static ssize_t fs_ready_store(struct device *dev,
  3022. struct device_attribute *attr,
  3023. const char *buf, size_t count)
  3024. {
  3025. int fs_ready = 0;
  3026. struct cnss_plat_data *plat_priv = dev_get_drvdata(dev);
  3027. if (sscanf(buf, "%du", &fs_ready) != 1)
  3028. return -EINVAL;
  3029. cnss_pr_dbg("File system is ready, fs_ready is %d, count is %zu\n",
  3030. fs_ready, count);
  3031. if (!plat_priv) {
  3032. cnss_pr_err("plat_priv is NULL\n");
  3033. return count;
  3034. }
  3035. if (test_bit(QMI_BYPASS, &plat_priv->ctrl_params.quirks)) {
  3036. cnss_pr_dbg("QMI is bypassed\n");
  3037. return count;
  3038. }
  3039. set_bit(CNSS_FS_READY, &plat_priv->driver_state);
  3040. if (fs_ready == FILE_SYSTEM_READY && plat_priv->cbc_enabled) {
  3041. cnss_driver_event_post(plat_priv,
  3042. CNSS_DRIVER_EVENT_COLD_BOOT_CAL_START,
  3043. 0, NULL);
  3044. }
  3045. return count;
  3046. }
  3047. static ssize_t qdss_trace_start_store(struct device *dev,
  3048. struct device_attribute *attr,
  3049. const char *buf, size_t count)
  3050. {
  3051. struct cnss_plat_data *plat_priv = dev_get_drvdata(dev);
  3052. wlfw_qdss_trace_start(plat_priv);
  3053. cnss_pr_dbg("Received QDSS start command\n");
  3054. return count;
  3055. }
  3056. static ssize_t qdss_trace_stop_store(struct device *dev,
  3057. struct device_attribute *attr,
  3058. const char *buf, size_t count)
  3059. {
  3060. struct cnss_plat_data *plat_priv = dev_get_drvdata(dev);
  3061. u32 option = 0;
  3062. if (sscanf(buf, "%du", &option) != 1)
  3063. return -EINVAL;
  3064. wlfw_qdss_trace_stop(plat_priv, option);
  3065. cnss_pr_dbg("Received QDSS stop command\n");
  3066. return count;
  3067. }
  3068. static ssize_t qdss_conf_download_store(struct device *dev,
  3069. struct device_attribute *attr,
  3070. const char *buf, size_t count)
  3071. {
  3072. struct cnss_plat_data *plat_priv = dev_get_drvdata(dev);
  3073. cnss_wlfw_qdss_dnld_send_sync(plat_priv);
  3074. cnss_pr_dbg("Received QDSS download config command\n");
  3075. return count;
  3076. }
  3077. static ssize_t hw_trace_override_store(struct device *dev,
  3078. struct device_attribute *attr,
  3079. const char *buf, size_t count)
  3080. {
  3081. struct cnss_plat_data *plat_priv = dev_get_drvdata(dev);
  3082. int tmp = 0;
  3083. if (sscanf(buf, "%du", &tmp) != 1)
  3084. return -EINVAL;
  3085. plat_priv->hw_trc_override = tmp;
  3086. cnss_pr_dbg("Received QDSS hw_trc_override indication\n");
  3087. return count;
  3088. }
  3089. static ssize_t charger_mode_store(struct device *dev,
  3090. struct device_attribute *attr,
  3091. const char *buf, size_t count)
  3092. {
  3093. struct cnss_plat_data *plat_priv = dev_get_drvdata(dev);
  3094. int tmp = 0;
  3095. if (sscanf(buf, "%du", &tmp) != 1)
  3096. return -EINVAL;
  3097. plat_priv->charger_mode = tmp;
  3098. cnss_pr_dbg("Received Charger Mode: %d\n", tmp);
  3099. return count;
  3100. }
  3101. static DEVICE_ATTR_WO(fs_ready);
  3102. static DEVICE_ATTR_WO(shutdown);
  3103. static DEVICE_ATTR_RW(recovery);
  3104. static DEVICE_ATTR_WO(enable_hds);
  3105. static DEVICE_ATTR_WO(qdss_trace_start);
  3106. static DEVICE_ATTR_WO(qdss_trace_stop);
  3107. static DEVICE_ATTR_WO(qdss_conf_download);
  3108. static DEVICE_ATTR_WO(hw_trace_override);
  3109. static DEVICE_ATTR_WO(charger_mode);
  3110. static DEVICE_ATTR_RW(time_sync_period);
  3111. static struct attribute *cnss_attrs[] = {
  3112. &dev_attr_fs_ready.attr,
  3113. &dev_attr_shutdown.attr,
  3114. &dev_attr_recovery.attr,
  3115. &dev_attr_enable_hds.attr,
  3116. &dev_attr_qdss_trace_start.attr,
  3117. &dev_attr_qdss_trace_stop.attr,
  3118. &dev_attr_qdss_conf_download.attr,
  3119. &dev_attr_hw_trace_override.attr,
  3120. &dev_attr_charger_mode.attr,
  3121. &dev_attr_time_sync_period.attr,
  3122. NULL,
  3123. };
  3124. static struct attribute_group cnss_attr_group = {
  3125. .attrs = cnss_attrs,
  3126. };
  3127. static int cnss_create_sysfs_link(struct cnss_plat_data *plat_priv)
  3128. {
  3129. struct device *dev = &plat_priv->plat_dev->dev;
  3130. int ret;
  3131. ret = sysfs_create_link(kernel_kobj, &dev->kobj, "cnss");
  3132. if (ret) {
  3133. cnss_pr_err("Failed to create cnss link, err = %d\n",
  3134. ret);
  3135. goto out;
  3136. }
  3137. /* This is only for backward compatibility. */
  3138. ret = sysfs_create_link(kernel_kobj, &dev->kobj, "shutdown_wlan");
  3139. if (ret) {
  3140. cnss_pr_err("Failed to create shutdown_wlan link, err = %d\n",
  3141. ret);
  3142. goto rm_cnss_link;
  3143. }
  3144. return 0;
  3145. rm_cnss_link:
  3146. sysfs_remove_link(kernel_kobj, "cnss");
  3147. out:
  3148. return ret;
  3149. }
  3150. static void cnss_remove_sysfs_link(struct cnss_plat_data *plat_priv)
  3151. {
  3152. sysfs_remove_link(kernel_kobj, "shutdown_wlan");
  3153. sysfs_remove_link(kernel_kobj, "cnss");
  3154. }
  3155. static int cnss_create_sysfs(struct cnss_plat_data *plat_priv)
  3156. {
  3157. int ret = 0;
  3158. ret = devm_device_add_group(&plat_priv->plat_dev->dev,
  3159. &cnss_attr_group);
  3160. if (ret) {
  3161. cnss_pr_err("Failed to create cnss device group, err = %d\n",
  3162. ret);
  3163. goto out;
  3164. }
  3165. cnss_create_sysfs_link(plat_priv);
  3166. return 0;
  3167. out:
  3168. return ret;
  3169. }
  3170. static void cnss_remove_sysfs(struct cnss_plat_data *plat_priv)
  3171. {
  3172. cnss_remove_sysfs_link(plat_priv);
  3173. devm_device_remove_group(&plat_priv->plat_dev->dev, &cnss_attr_group);
  3174. }
  3175. static int cnss_event_work_init(struct cnss_plat_data *plat_priv)
  3176. {
  3177. spin_lock_init(&plat_priv->event_lock);
  3178. plat_priv->event_wq = alloc_workqueue("cnss_driver_event",
  3179. WQ_UNBOUND, 1);
  3180. if (!plat_priv->event_wq) {
  3181. cnss_pr_err("Failed to create event workqueue!\n");
  3182. return -EFAULT;
  3183. }
  3184. INIT_WORK(&plat_priv->event_work, cnss_driver_event_work);
  3185. INIT_LIST_HEAD(&plat_priv->event_list);
  3186. return 0;
  3187. }
  3188. static void cnss_event_work_deinit(struct cnss_plat_data *plat_priv)
  3189. {
  3190. destroy_workqueue(plat_priv->event_wq);
  3191. }
  3192. static int cnss_reboot_notifier(struct notifier_block *nb,
  3193. unsigned long action,
  3194. void *data)
  3195. {
  3196. struct cnss_plat_data *plat_priv =
  3197. container_of(nb, struct cnss_plat_data, reboot_nb);
  3198. set_bit(CNSS_IN_REBOOT, &plat_priv->driver_state);
  3199. del_timer(&plat_priv->fw_boot_timer);
  3200. complete_all(&plat_priv->power_up_complete);
  3201. complete_all(&plat_priv->cal_complete);
  3202. cnss_pr_dbg("Reboot is in progress with action %d\n", action);
  3203. return NOTIFY_DONE;
  3204. }
  3205. #ifdef CONFIG_CNSS_HW_SECURE_DISABLE
  3206. int cnss_wlan_hw_disable_check(struct cnss_plat_data *plat_priv)
  3207. {
  3208. struct Object client_env;
  3209. struct Object app_object;
  3210. u32 wifi_uid = HW_WIFI_UID;
  3211. union ObjectArg obj_arg[2] = {{{0, 0}}};
  3212. int ret;
  3213. u8 state = 0;
  3214. /* Once this flag is set, secure peripheral feature
  3215. * will not be supported till next reboot
  3216. */
  3217. if (plat_priv->sec_peri_feature_disable)
  3218. return 0;
  3219. /* get rootObj */
  3220. ret = get_client_env_object(&client_env);
  3221. if (ret) {
  3222. cnss_pr_dbg("Failed to get client_env_object, ret: %d\n", ret);
  3223. goto end;
  3224. }
  3225. ret = IClientEnv_open(client_env, HW_STATE_UID, &app_object);
  3226. if (ret) {
  3227. cnss_pr_dbg("Failed to get app_object, ret: %d\n", ret);
  3228. if (ret == FEATURE_NOT_SUPPORTED) {
  3229. ret = 0; /* Do not Assert */
  3230. plat_priv->sec_peri_feature_disable = true;
  3231. cnss_pr_dbg("Secure HW feature not supported\n");
  3232. }
  3233. goto exit_release_clientenv;
  3234. }
  3235. obj_arg[0].b = (struct ObjectBuf) {&wifi_uid, sizeof(u32)};
  3236. obj_arg[1].b = (struct ObjectBuf) {&state, sizeof(u8)};
  3237. ret = Object_invoke(app_object, HW_OP_GET_STATE, obj_arg,
  3238. ObjectCounts_pack(1, 1, 0, 0));
  3239. cnss_pr_dbg("SMC invoke ret: %d state: %d\n", ret, state);
  3240. if (ret) {
  3241. if (ret == PERIPHERAL_NOT_FOUND) {
  3242. ret = 0; /* Do not Assert */
  3243. plat_priv->sec_peri_feature_disable = true;
  3244. cnss_pr_dbg("Secure HW mode is not updated. Peripheral not found\n");
  3245. }
  3246. goto exit_release_app_obj;
  3247. }
  3248. if (state == 1)
  3249. set_bit(CNSS_WLAN_HW_DISABLED,
  3250. &plat_priv->driver_state);
  3251. else
  3252. clear_bit(CNSS_WLAN_HW_DISABLED,
  3253. &plat_priv->driver_state);
  3254. exit_release_app_obj:
  3255. Object_release(app_object);
  3256. exit_release_clientenv:
  3257. Object_release(client_env);
  3258. end:
  3259. if (ret) {
  3260. cnss_pr_err("Unable to get HW disable status\n");
  3261. CNSS_ASSERT(0);
  3262. }
  3263. return ret;
  3264. }
  3265. #else
  3266. int cnss_wlan_hw_disable_check(struct cnss_plat_data *plat_priv)
  3267. {
  3268. return 0;
  3269. }
  3270. #endif
  3271. static int cnss_misc_init(struct cnss_plat_data *plat_priv)
  3272. {
  3273. int ret;
  3274. ret = cnss_init_sol_gpio(plat_priv);
  3275. if (ret)
  3276. return ret;
  3277. timer_setup(&plat_priv->fw_boot_timer,
  3278. cnss_bus_fw_boot_timeout_hdlr, 0);
  3279. plat_priv->reboot_nb.notifier_call = cnss_reboot_notifier;
  3280. ret = register_reboot_notifier(&plat_priv->reboot_nb);
  3281. if (ret)
  3282. cnss_pr_err("Failed to register reboot notifier, err = %d\n",
  3283. ret);
  3284. ret = device_init_wakeup(&plat_priv->plat_dev->dev, true);
  3285. if (ret)
  3286. cnss_pr_err("Failed to init platform device wakeup source, err = %d\n",
  3287. ret);
  3288. INIT_WORK(&plat_priv->recovery_work, cnss_recovery_work_handler);
  3289. init_completion(&plat_priv->power_up_complete);
  3290. init_completion(&plat_priv->cal_complete);
  3291. init_completion(&plat_priv->rddm_complete);
  3292. init_completion(&plat_priv->recovery_complete);
  3293. init_completion(&plat_priv->daemon_connected);
  3294. mutex_init(&plat_priv->dev_lock);
  3295. mutex_init(&plat_priv->driver_ops_lock);
  3296. plat_priv->recovery_ws =
  3297. wakeup_source_register(&plat_priv->plat_dev->dev,
  3298. "CNSS_FW_RECOVERY");
  3299. if (!plat_priv->recovery_ws)
  3300. cnss_pr_err("Failed to setup FW recovery wake source\n");
  3301. ret = cnss_plat_ipc_register(CNSS_PLAT_IPC_DAEMON_QMI_CLIENT_V01,
  3302. cnss_daemon_connection_update_cb,
  3303. plat_priv);
  3304. if (ret)
  3305. cnss_pr_err("QMI IPC connection call back register failed, err = %d\n",
  3306. ret);
  3307. if (plat_priv->device_id == QCA6490_DEVICE_ID &&
  3308. cnss_get_host_build_type() == QMI_HOST_BUILD_TYPE_PRIMARY_V01)
  3309. plat_priv->sram_dump = kcalloc(SRAM_DUMP_SIZE, 1, GFP_KERNEL);
  3310. if (of_property_read_bool(plat_priv->plat_dev->dev.of_node,
  3311. "qcom,rc-ep-short-channel"))
  3312. cnss_set_feature_list(plat_priv, CNSS_RC_EP_ULTRASHORT_CHANNEL_V01);
  3313. return 0;
  3314. }
  3315. static void cnss_misc_deinit(struct cnss_plat_data *plat_priv)
  3316. {
  3317. cnss_plat_ipc_unregister(CNSS_PLAT_IPC_DAEMON_QMI_CLIENT_V01,
  3318. plat_priv);
  3319. complete_all(&plat_priv->recovery_complete);
  3320. complete_all(&plat_priv->rddm_complete);
  3321. complete_all(&plat_priv->cal_complete);
  3322. complete_all(&plat_priv->power_up_complete);
  3323. complete_all(&plat_priv->daemon_connected);
  3324. device_init_wakeup(&plat_priv->plat_dev->dev, false);
  3325. unregister_reboot_notifier(&plat_priv->reboot_nb);
  3326. del_timer(&plat_priv->fw_boot_timer);
  3327. wakeup_source_unregister(plat_priv->recovery_ws);
  3328. cnss_deinit_sol_gpio(plat_priv);
  3329. kfree(plat_priv->sram_dump);
  3330. }
  3331. static void cnss_init_control_params(struct cnss_plat_data *plat_priv)
  3332. {
  3333. plat_priv->ctrl_params.quirks = CNSS_QUIRKS_DEFAULT;
  3334. plat_priv->cbc_enabled = !IS_ENABLED(CONFIG_CNSS_EMULATION) &&
  3335. of_property_read_bool(plat_priv->plat_dev->dev.of_node,
  3336. "qcom,wlan-cbc-enabled");
  3337. plat_priv->ctrl_params.mhi_timeout = CNSS_MHI_TIMEOUT_DEFAULT;
  3338. plat_priv->ctrl_params.mhi_m2_timeout = CNSS_MHI_M2_TIMEOUT_DEFAULT;
  3339. plat_priv->ctrl_params.qmi_timeout = CNSS_QMI_TIMEOUT_DEFAULT;
  3340. plat_priv->ctrl_params.bdf_type = CNSS_BDF_TYPE_DEFAULT;
  3341. plat_priv->ctrl_params.time_sync_period = CNSS_TIME_SYNC_PERIOD_DEFAULT;
  3342. /* Set adsp_pc_enabled default value to true as ADSP pc is always
  3343. * enabled by default
  3344. */
  3345. plat_priv->adsp_pc_enabled = true;
  3346. }
  3347. static void cnss_get_pm_domain_info(struct cnss_plat_data *plat_priv)
  3348. {
  3349. struct device *dev = &plat_priv->plat_dev->dev;
  3350. plat_priv->use_pm_domain =
  3351. of_property_read_bool(dev->of_node, "use-pm-domain");
  3352. cnss_pr_dbg("use-pm-domain is %d\n", plat_priv->use_pm_domain);
  3353. }
  3354. static void cnss_get_wlaon_pwr_ctrl_info(struct cnss_plat_data *plat_priv)
  3355. {
  3356. struct device *dev = &plat_priv->plat_dev->dev;
  3357. plat_priv->set_wlaon_pwr_ctrl =
  3358. of_property_read_bool(dev->of_node, "qcom,set-wlaon-pwr-ctrl");
  3359. cnss_pr_dbg("set_wlaon_pwr_ctrl is %d\n",
  3360. plat_priv->set_wlaon_pwr_ctrl);
  3361. }
  3362. static bool cnss_use_fw_path_with_prefix(struct cnss_plat_data *plat_priv)
  3363. {
  3364. return (of_property_read_bool(plat_priv->plat_dev->dev.of_node,
  3365. "qcom,converged-dt") ||
  3366. of_property_read_bool(plat_priv->plat_dev->dev.of_node,
  3367. "qcom,same-dt-multi-dev") ||
  3368. of_property_read_bool(plat_priv->plat_dev->dev.of_node,
  3369. "qcom,multi-wlan-exchg"));
  3370. }
  3371. static const struct platform_device_id cnss_platform_id_table[] = {
  3372. { .name = "qca6174", .driver_data = QCA6174_DEVICE_ID, },
  3373. { .name = "qca6290", .driver_data = QCA6290_DEVICE_ID, },
  3374. { .name = "qca6390", .driver_data = QCA6390_DEVICE_ID, },
  3375. { .name = "qca6490", .driver_data = QCA6490_DEVICE_ID, },
  3376. { .name = "kiwi", .driver_data = KIWI_DEVICE_ID, },
  3377. { .name = "mango", .driver_data = MANGO_DEVICE_ID, },
  3378. { .name = "qcaconv", .driver_data = 0, },
  3379. { },
  3380. };
  3381. static const struct of_device_id cnss_of_match_table[] = {
  3382. {
  3383. .compatible = "qcom,cnss",
  3384. .data = (void *)&cnss_platform_id_table[0]},
  3385. {
  3386. .compatible = "qcom,cnss-qca6290",
  3387. .data = (void *)&cnss_platform_id_table[1]},
  3388. {
  3389. .compatible = "qcom,cnss-qca6390",
  3390. .data = (void *)&cnss_platform_id_table[2]},
  3391. {
  3392. .compatible = "qcom,cnss-qca6490",
  3393. .data = (void *)&cnss_platform_id_table[3]},
  3394. {
  3395. .compatible = "qcom,cnss-kiwi",
  3396. .data = (void *)&cnss_platform_id_table[4]},
  3397. {
  3398. .compatible = "qcom,cnss-mango",
  3399. .data = (void *)&cnss_platform_id_table[5]},
  3400. {
  3401. .compatible = "qcom,cnss-qca-converged",
  3402. .data = (void *)&cnss_platform_id_table[6]},
  3403. { },
  3404. };
  3405. MODULE_DEVICE_TABLE(of, cnss_of_match_table);
  3406. static inline bool
  3407. cnss_use_nv_mac(struct cnss_plat_data *plat_priv)
  3408. {
  3409. return of_property_read_bool(plat_priv->plat_dev->dev.of_node,
  3410. "use-nv-mac");
  3411. }
  3412. static int cnss_get_dev_cfg_node(struct cnss_plat_data *plat_priv)
  3413. {
  3414. struct device_node *child;
  3415. u32 id, i;
  3416. int id_n, device_identifier_gpio, ret;
  3417. u8 gpio_value;
  3418. if (plat_priv->dt_type != CNSS_DTT_CONVERGED)
  3419. return 0;
  3420. /* Parses the wlan_sw_ctrl gpio which is used to identify device */
  3421. ret = cnss_get_wlan_sw_ctrl(plat_priv);
  3422. if (ret) {
  3423. cnss_pr_dbg("Failed to parse wlan_sw_ctrl gpio, error:%d", ret);
  3424. return ret;
  3425. }
  3426. device_identifier_gpio = plat_priv->pinctrl_info.wlan_sw_ctrl_gpio;
  3427. gpio_value = gpio_get_value(device_identifier_gpio);
  3428. cnss_pr_dbg("Value of Device Identifier GPIO: %d\n", gpio_value);
  3429. for_each_available_child_of_node(plat_priv->plat_dev->dev.of_node,
  3430. child) {
  3431. if (strcmp(child->name, "chip_cfg"))
  3432. continue;
  3433. id_n = of_property_count_u32_elems(child, "supported-ids");
  3434. if (id_n <= 0) {
  3435. cnss_pr_err("Device id is NOT set\n");
  3436. return -EINVAL;
  3437. }
  3438. for (i = 0; i < id_n; i++) {
  3439. ret = of_property_read_u32_index(child,
  3440. "supported-ids",
  3441. i, &id);
  3442. if (ret) {
  3443. cnss_pr_err("Failed to read supported ids\n");
  3444. return -EINVAL;
  3445. }
  3446. if (gpio_value && id == QCA6490_DEVICE_ID) {
  3447. plat_priv->plat_dev->dev.of_node = child;
  3448. plat_priv->device_id = QCA6490_DEVICE_ID;
  3449. cnss_utils_update_device_type(CNSS_HSP_DEVICE_TYPE);
  3450. cnss_pr_dbg("got node[%s@%d] for device[0x%x]\n",
  3451. child->name, i, id);
  3452. return 0;
  3453. } else if (!gpio_value && id == KIWI_DEVICE_ID) {
  3454. plat_priv->plat_dev->dev.of_node = child;
  3455. plat_priv->device_id = KIWI_DEVICE_ID;
  3456. cnss_utils_update_device_type(CNSS_HMT_DEVICE_TYPE);
  3457. cnss_pr_dbg("got node[%s@%d] for device[0x%x]\n",
  3458. child->name, i, id);
  3459. return 0;
  3460. }
  3461. }
  3462. }
  3463. return -EINVAL;
  3464. }
  3465. static inline u32
  3466. cnss_dt_type(struct cnss_plat_data *plat_priv)
  3467. {
  3468. bool is_converged_dt = of_property_read_bool(
  3469. plat_priv->plat_dev->dev.of_node, "qcom,converged-dt");
  3470. bool is_multi_wlan_xchg;
  3471. if (is_converged_dt)
  3472. return CNSS_DTT_CONVERGED;
  3473. is_multi_wlan_xchg = of_property_read_bool(
  3474. plat_priv->plat_dev->dev.of_node, "qcom,multi-wlan-exchg");
  3475. if (is_multi_wlan_xchg)
  3476. return CNSS_DTT_MULTIEXCHG;
  3477. return CNSS_DTT_LEGACY;
  3478. }
  3479. static int cnss_wlan_device_init(struct cnss_plat_data *plat_priv)
  3480. {
  3481. int ret = 0;
  3482. int retry = 0;
  3483. if (test_bit(SKIP_DEVICE_BOOT, &plat_priv->ctrl_params.quirks))
  3484. return 0;
  3485. retry:
  3486. ret = cnss_power_on_device(plat_priv, true);
  3487. if (ret)
  3488. goto end;
  3489. ret = cnss_bus_init(plat_priv);
  3490. if (ret) {
  3491. if ((ret != -EPROBE_DEFER) &&
  3492. retry++ < POWER_ON_RETRY_MAX_TIMES) {
  3493. cnss_power_off_device(plat_priv);
  3494. cnss_pr_dbg("Retry cnss_bus_init #%d\n", retry);
  3495. msleep(POWER_ON_RETRY_DELAY_MS * retry);
  3496. goto retry;
  3497. }
  3498. goto power_off;
  3499. }
  3500. return 0;
  3501. power_off:
  3502. cnss_power_off_device(plat_priv);
  3503. end:
  3504. return ret;
  3505. }
  3506. int cnss_wlan_hw_enable(void)
  3507. {
  3508. struct cnss_plat_data *plat_priv = cnss_get_plat_priv(NULL);
  3509. int ret = 0;
  3510. clear_bit(CNSS_WLAN_HW_DISABLED, &plat_priv->driver_state);
  3511. if (test_bit(CNSS_PCI_PROBE_DONE, &plat_priv->driver_state))
  3512. goto register_driver;
  3513. ret = cnss_wlan_device_init(plat_priv);
  3514. if (ret) {
  3515. if (!test_bit(CNSS_WLAN_HW_DISABLED, &plat_priv->driver_state))
  3516. CNSS_ASSERT(0);
  3517. return ret;
  3518. }
  3519. if (test_bit(CNSS_FS_READY, &plat_priv->driver_state))
  3520. cnss_driver_event_post(plat_priv,
  3521. CNSS_DRIVER_EVENT_COLD_BOOT_CAL_START,
  3522. 0, NULL);
  3523. register_driver:
  3524. if (plat_priv->driver_ops)
  3525. ret = cnss_wlan_register_driver(plat_priv->driver_ops);
  3526. return ret;
  3527. }
  3528. EXPORT_SYMBOL(cnss_wlan_hw_enable);
  3529. int cnss_set_wfc_mode(struct device *dev, struct cnss_wfc_cfg cfg)
  3530. {
  3531. struct cnss_plat_data *plat_priv = cnss_bus_dev_to_plat_priv(dev);
  3532. int ret = 0;
  3533. if (!plat_priv)
  3534. return -ENODEV;
  3535. /* If IMS server is connected, return success without QMI send */
  3536. if (test_bit(CNSS_IMS_CONNECTED, &plat_priv->driver_state)) {
  3537. cnss_pr_dbg("Ignore host request as IMS server is connected");
  3538. return ret;
  3539. }
  3540. ret = cnss_wlfw_send_host_wfc_call_status(plat_priv, cfg);
  3541. return ret;
  3542. }
  3543. EXPORT_SYMBOL(cnss_set_wfc_mode);
  3544. static int cnss_probe(struct platform_device *plat_dev)
  3545. {
  3546. int ret = 0;
  3547. struct cnss_plat_data *plat_priv;
  3548. const struct of_device_id *of_id;
  3549. const struct platform_device_id *device_id;
  3550. if (cnss_get_plat_priv(plat_dev)) {
  3551. cnss_pr_err("Driver is already initialized!\n");
  3552. ret = -EEXIST;
  3553. goto out;
  3554. }
  3555. of_id = of_match_device(cnss_of_match_table, &plat_dev->dev);
  3556. if (!of_id || !of_id->data) {
  3557. cnss_pr_err("Failed to find of match device!\n");
  3558. ret = -ENODEV;
  3559. goto out;
  3560. }
  3561. device_id = of_id->data;
  3562. plat_priv = devm_kzalloc(&plat_dev->dev, sizeof(*plat_priv),
  3563. GFP_KERNEL);
  3564. if (!plat_priv) {
  3565. ret = -ENOMEM;
  3566. goto out;
  3567. }
  3568. plat_priv->plat_dev = plat_dev;
  3569. plat_priv->dev_node = NULL;
  3570. plat_priv->device_id = device_id->driver_data;
  3571. plat_priv->dt_type = cnss_dt_type(plat_priv);
  3572. cnss_pr_dbg("Probing platform driver from dt type: %d\n",
  3573. plat_priv->dt_type);
  3574. plat_priv->use_fw_path_with_prefix =
  3575. cnss_use_fw_path_with_prefix(plat_priv);
  3576. ret = cnss_get_dev_cfg_node(plat_priv);
  3577. if (ret) {
  3578. cnss_pr_err("Failed to get device cfg node, err = %d\n", ret);
  3579. goto reset_plat_dev;
  3580. }
  3581. plat_priv->bus_type = cnss_get_bus_type(plat_priv);
  3582. plat_priv->use_nv_mac = cnss_use_nv_mac(plat_priv);
  3583. plat_priv->driver_mode = CNSS_DRIVER_MODE_MAX;
  3584. cnss_set_plat_priv(plat_dev, plat_priv);
  3585. platform_set_drvdata(plat_dev, plat_priv);
  3586. INIT_LIST_HEAD(&plat_priv->vreg_list);
  3587. INIT_LIST_HEAD(&plat_priv->clk_list);
  3588. cnss_get_pm_domain_info(plat_priv);
  3589. cnss_get_wlaon_pwr_ctrl_info(plat_priv);
  3590. cnss_power_misc_params_init(plat_priv);
  3591. cnss_get_tcs_info(plat_priv);
  3592. cnss_get_cpr_info(plat_priv);
  3593. cnss_aop_mbox_init(plat_priv);
  3594. cnss_init_control_params(plat_priv);
  3595. ret = cnss_get_resources(plat_priv);
  3596. if (ret)
  3597. goto reset_ctx;
  3598. ret = cnss_register_esoc(plat_priv);
  3599. if (ret)
  3600. goto free_res;
  3601. ret = cnss_register_bus_scale(plat_priv);
  3602. if (ret)
  3603. goto unreg_esoc;
  3604. ret = cnss_create_sysfs(plat_priv);
  3605. if (ret)
  3606. goto unreg_bus_scale;
  3607. ret = cnss_event_work_init(plat_priv);
  3608. if (ret)
  3609. goto remove_sysfs;
  3610. ret = cnss_qmi_init(plat_priv);
  3611. if (ret)
  3612. goto deinit_event_work;
  3613. ret = cnss_dms_init(plat_priv);
  3614. if (ret)
  3615. goto deinit_qmi;
  3616. ret = cnss_debugfs_create(plat_priv);
  3617. if (ret)
  3618. goto deinit_dms;
  3619. ret = cnss_misc_init(plat_priv);
  3620. if (ret)
  3621. goto destroy_debugfs;
  3622. ret = cnss_wlan_hw_disable_check(plat_priv);
  3623. if (ret)
  3624. goto deinit_misc;
  3625. /* Make sure all platform related init are done before
  3626. * device power on and bus init.
  3627. */
  3628. if (!test_bit(CNSS_WLAN_HW_DISABLED, &plat_priv->driver_state)) {
  3629. ret = cnss_wlan_device_init(plat_priv);
  3630. if (ret)
  3631. goto deinit_misc;
  3632. } else {
  3633. cnss_pr_info("WLAN HW Disabled. Defer PCI enumeration\n");
  3634. }
  3635. cnss_register_coex_service(plat_priv);
  3636. cnss_register_ims_service(plat_priv);
  3637. ret = cnss_genl_init();
  3638. if (ret < 0)
  3639. cnss_pr_err("CNSS genl init failed %d\n", ret);
  3640. cnss_pr_info("Platform driver probed successfully.\n");
  3641. return 0;
  3642. deinit_misc:
  3643. cnss_misc_deinit(plat_priv);
  3644. destroy_debugfs:
  3645. cnss_debugfs_destroy(plat_priv);
  3646. deinit_dms:
  3647. cnss_dms_deinit(plat_priv);
  3648. deinit_qmi:
  3649. cnss_qmi_deinit(plat_priv);
  3650. deinit_event_work:
  3651. cnss_event_work_deinit(plat_priv);
  3652. remove_sysfs:
  3653. cnss_remove_sysfs(plat_priv);
  3654. unreg_bus_scale:
  3655. cnss_unregister_bus_scale(plat_priv);
  3656. unreg_esoc:
  3657. cnss_unregister_esoc(plat_priv);
  3658. free_res:
  3659. cnss_put_resources(plat_priv);
  3660. reset_ctx:
  3661. platform_set_drvdata(plat_dev, NULL);
  3662. reset_plat_dev:
  3663. cnss_set_plat_priv(plat_dev, NULL);
  3664. out:
  3665. return ret;
  3666. }
  3667. static int cnss_remove(struct platform_device *plat_dev)
  3668. {
  3669. struct cnss_plat_data *plat_priv = platform_get_drvdata(plat_dev);
  3670. plat_priv->audio_iommu_domain = NULL;
  3671. cnss_genl_exit();
  3672. cnss_unregister_ims_service(plat_priv);
  3673. cnss_unregister_coex_service(plat_priv);
  3674. cnss_bus_deinit(plat_priv);
  3675. cnss_misc_deinit(plat_priv);
  3676. cnss_debugfs_destroy(plat_priv);
  3677. cnss_dms_deinit(plat_priv);
  3678. cnss_qmi_deinit(plat_priv);
  3679. cnss_event_work_deinit(plat_priv);
  3680. cnss_cancel_dms_work();
  3681. cnss_remove_sysfs(plat_priv);
  3682. cnss_unregister_bus_scale(plat_priv);
  3683. cnss_unregister_esoc(plat_priv);
  3684. cnss_put_resources(plat_priv);
  3685. if (!IS_ERR_OR_NULL(plat_priv->mbox_chan))
  3686. mbox_free_channel(plat_priv->mbox_chan);
  3687. platform_set_drvdata(plat_dev, NULL);
  3688. plat_env = NULL;
  3689. return 0;
  3690. }
  3691. static struct platform_driver cnss_platform_driver = {
  3692. .probe = cnss_probe,
  3693. .remove = cnss_remove,
  3694. .driver = {
  3695. .name = "cnss2",
  3696. .of_match_table = cnss_of_match_table,
  3697. #ifdef CONFIG_CNSS_ASYNC
  3698. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  3699. #endif
  3700. },
  3701. };
  3702. static bool cnss_check_compatible_node(void)
  3703. {
  3704. struct device_node *dn = NULL;
  3705. for_each_matching_node(dn, cnss_of_match_table) {
  3706. if (of_device_is_available(dn)) {
  3707. cnss_allow_driver_loading = true;
  3708. return true;
  3709. }
  3710. }
  3711. return false;
  3712. }
  3713. /**
  3714. * cnss_is_valid_dt_node_found - Check if valid device tree node present
  3715. *
  3716. * Valid device tree node means a node with "compatible" property from the
  3717. * device match table and "status" property is not disabled.
  3718. *
  3719. * Return: true if valid device tree node found, false if not found
  3720. */
  3721. static bool cnss_is_valid_dt_node_found(void)
  3722. {
  3723. struct device_node *dn = NULL;
  3724. for_each_matching_node(dn, cnss_of_match_table) {
  3725. if (of_device_is_available(dn))
  3726. break;
  3727. }
  3728. if (dn)
  3729. return true;
  3730. return false;
  3731. }
  3732. static int __init cnss_initialize(void)
  3733. {
  3734. int ret = 0;
  3735. if (!cnss_is_valid_dt_node_found())
  3736. return -ENODEV;
  3737. if (!cnss_check_compatible_node())
  3738. return ret;
  3739. cnss_debug_init();
  3740. ret = platform_driver_register(&cnss_platform_driver);
  3741. if (ret)
  3742. cnss_debug_deinit();
  3743. return ret;
  3744. }
  3745. static void __exit cnss_exit(void)
  3746. {
  3747. platform_driver_unregister(&cnss_platform_driver);
  3748. cnss_debug_deinit();
  3749. }
  3750. module_init(cnss_initialize);
  3751. module_exit(cnss_exit);
  3752. MODULE_LICENSE("GPL v2");
  3753. MODULE_DESCRIPTION("CNSS2 Platform Driver");