main.c 128 KB

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