pci.c 160 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2016-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2021-2022 Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #include <linux/completion.h>
  7. #include <linux/io.h>
  8. #include <linux/irq.h>
  9. #include <linux/memblock.h>
  10. #include <linux/module.h>
  11. #include <linux/msi.h>
  12. #include <linux/of.h>
  13. #include <linux/of_gpio.h>
  14. #include <linux/pm_runtime.h>
  15. #include <linux/suspend.h>
  16. #include <linux/version.h>
  17. #include <linux/sched.h>
  18. #include "main.h"
  19. #include "bus.h"
  20. #include "debug.h"
  21. #include "pci.h"
  22. #include "pci_platform.h"
  23. #include "reg.h"
  24. #define PCI_LINK_UP 1
  25. #define PCI_LINK_DOWN 0
  26. #define SAVE_PCI_CONFIG_SPACE 1
  27. #define RESTORE_PCI_CONFIG_SPACE 0
  28. #define PCI_BAR_NUM 0
  29. #define PCI_INVALID_READ(val) ((val) == U32_MAX)
  30. #define PCI_DMA_MASK_32_BIT DMA_BIT_MASK(32)
  31. #define PCI_DMA_MASK_36_BIT DMA_BIT_MASK(36)
  32. #define PCI_DMA_MASK_64_BIT DMA_BIT_MASK(64)
  33. #define MHI_NODE_NAME "qcom,mhi"
  34. #define MHI_MSI_NAME "MHI"
  35. #define QCA6390_PATH_PREFIX "qca6390/"
  36. #define QCA6490_PATH_PREFIX "qca6490/"
  37. #define KIWI_PATH_PREFIX "kiwi/"
  38. #define MANGO_PATH_PREFIX "mango/"
  39. #define DEFAULT_PHY_M3_FILE_NAME "m3.bin"
  40. #define DEFAULT_PHY_UCODE_FILE_NAME "phy_ucode.elf"
  41. #define PHY_UCODE_V2_FILE_NAME "phy_ucode20.elf"
  42. #define DEFAULT_FW_FILE_NAME "amss.bin"
  43. #define FW_V2_FILE_NAME "amss20.bin"
  44. #define FW_V2_FTM_FILE_NAME "amss20_ftm.bin"
  45. #define DEVICE_MAJOR_VERSION_MASK 0xF
  46. #define WAKE_MSI_NAME "WAKE"
  47. #define DEV_RDDM_TIMEOUT 5000
  48. #define WAKE_EVENT_TIMEOUT 5000
  49. #ifdef CONFIG_CNSS_EMULATION
  50. #define EMULATION_HW 1
  51. #else
  52. #define EMULATION_HW 0
  53. #endif
  54. #define RAMDUMP_SIZE_DEFAULT 0x420000
  55. #define CNSS_256KB_SIZE 0x40000
  56. #define DEVICE_RDDM_COOKIE 0xCAFECACE
  57. static DEFINE_SPINLOCK(pci_link_down_lock);
  58. static DEFINE_SPINLOCK(pci_reg_window_lock);
  59. static DEFINE_SPINLOCK(time_sync_lock);
  60. #define MHI_TIMEOUT_OVERWRITE_MS (plat_priv->ctrl_params.mhi_timeout)
  61. #define MHI_M2_TIMEOUT_MS (plat_priv->ctrl_params.mhi_m2_timeout)
  62. #define WLAON_PWR_CTRL_SHUTDOWN_DELAY_MIN_US 1000
  63. #define WLAON_PWR_CTRL_SHUTDOWN_DELAY_MAX_US 2000
  64. #define FORCE_WAKE_DELAY_MIN_US 4000
  65. #define FORCE_WAKE_DELAY_MAX_US 6000
  66. #define FORCE_WAKE_DELAY_TIMEOUT_US 60000
  67. #define MHI_SUSPEND_RETRY_MAX_TIMES 3
  68. #define MHI_SUSPEND_RETRY_DELAY_US 5000
  69. #define BOOT_DEBUG_TIMEOUT_MS 7000
  70. #define HANG_DATA_LENGTH 384
  71. #define HST_HANG_DATA_OFFSET ((3 * 1024 * 1024) - HANG_DATA_LENGTH)
  72. #define HSP_HANG_DATA_OFFSET ((2 * 1024 * 1024) - HANG_DATA_LENGTH)
  73. static const struct mhi_channel_config cnss_mhi_channels[] = {
  74. {
  75. .num = 0,
  76. .name = "LOOPBACK",
  77. .num_elements = 32,
  78. .event_ring = 1,
  79. .dir = DMA_TO_DEVICE,
  80. .ee_mask = 0x4,
  81. .pollcfg = 0,
  82. .doorbell = MHI_DB_BRST_DISABLE,
  83. .lpm_notify = false,
  84. .offload_channel = false,
  85. .doorbell_mode_switch = false,
  86. .auto_queue = false,
  87. },
  88. {
  89. .num = 1,
  90. .name = "LOOPBACK",
  91. .num_elements = 32,
  92. .event_ring = 1,
  93. .dir = DMA_FROM_DEVICE,
  94. .ee_mask = 0x4,
  95. .pollcfg = 0,
  96. .doorbell = MHI_DB_BRST_DISABLE,
  97. .lpm_notify = false,
  98. .offload_channel = false,
  99. .doorbell_mode_switch = false,
  100. .auto_queue = false,
  101. },
  102. {
  103. .num = 4,
  104. .name = "DIAG",
  105. .num_elements = 64,
  106. .event_ring = 1,
  107. .dir = DMA_TO_DEVICE,
  108. .ee_mask = 0x4,
  109. .pollcfg = 0,
  110. .doorbell = MHI_DB_BRST_DISABLE,
  111. .lpm_notify = false,
  112. .offload_channel = false,
  113. .doorbell_mode_switch = false,
  114. .auto_queue = false,
  115. },
  116. {
  117. .num = 5,
  118. .name = "DIAG",
  119. .num_elements = 64,
  120. .event_ring = 1,
  121. .dir = DMA_FROM_DEVICE,
  122. .ee_mask = 0x4,
  123. .pollcfg = 0,
  124. .doorbell = MHI_DB_BRST_DISABLE,
  125. .lpm_notify = false,
  126. .offload_channel = false,
  127. .doorbell_mode_switch = false,
  128. .auto_queue = false,
  129. },
  130. {
  131. .num = 20,
  132. .name = "IPCR",
  133. .num_elements = 64,
  134. .event_ring = 1,
  135. .dir = DMA_TO_DEVICE,
  136. .ee_mask = 0x4,
  137. .pollcfg = 0,
  138. .doorbell = MHI_DB_BRST_DISABLE,
  139. .lpm_notify = false,
  140. .offload_channel = false,
  141. .doorbell_mode_switch = false,
  142. .auto_queue = false,
  143. },
  144. {
  145. .num = 21,
  146. .name = "IPCR",
  147. .num_elements = 64,
  148. .event_ring = 1,
  149. .dir = DMA_FROM_DEVICE,
  150. .ee_mask = 0x4,
  151. .pollcfg = 0,
  152. .doorbell = MHI_DB_BRST_DISABLE,
  153. .lpm_notify = false,
  154. .offload_channel = false,
  155. .doorbell_mode_switch = false,
  156. .auto_queue = true,
  157. },
  158. /* All MHI satellite config to be at the end of data struct */
  159. #if IS_ENABLED(CONFIG_MHI_SATELLITE)
  160. {
  161. .num = 50,
  162. .name = "ADSP_0",
  163. .num_elements = 64,
  164. .event_ring = 3,
  165. .dir = DMA_BIDIRECTIONAL,
  166. .ee_mask = 0x4,
  167. .pollcfg = 0,
  168. .doorbell = MHI_DB_BRST_DISABLE,
  169. .lpm_notify = false,
  170. .offload_channel = true,
  171. .doorbell_mode_switch = false,
  172. .auto_queue = false,
  173. },
  174. {
  175. .num = 51,
  176. .name = "ADSP_1",
  177. .num_elements = 64,
  178. .event_ring = 3,
  179. .dir = DMA_BIDIRECTIONAL,
  180. .ee_mask = 0x4,
  181. .pollcfg = 0,
  182. .doorbell = MHI_DB_BRST_DISABLE,
  183. .lpm_notify = false,
  184. .offload_channel = true,
  185. .doorbell_mode_switch = false,
  186. .auto_queue = false,
  187. },
  188. {
  189. .num = 70,
  190. .name = "ADSP_2",
  191. .num_elements = 64,
  192. .event_ring = 3,
  193. .dir = DMA_BIDIRECTIONAL,
  194. .ee_mask = 0x4,
  195. .pollcfg = 0,
  196. .doorbell = MHI_DB_BRST_DISABLE,
  197. .lpm_notify = false,
  198. .offload_channel = true,
  199. .doorbell_mode_switch = false,
  200. .auto_queue = false,
  201. },
  202. {
  203. .num = 71,
  204. .name = "ADSP_3",
  205. .num_elements = 64,
  206. .event_ring = 3,
  207. .dir = DMA_BIDIRECTIONAL,
  208. .ee_mask = 0x4,
  209. .pollcfg = 0,
  210. .doorbell = MHI_DB_BRST_DISABLE,
  211. .lpm_notify = false,
  212. .offload_channel = true,
  213. .doorbell_mode_switch = false,
  214. .auto_queue = false,
  215. },
  216. #endif
  217. };
  218. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 12, 0))
  219. static struct mhi_event_config cnss_mhi_events[] = {
  220. #else
  221. static const struct mhi_event_config cnss_mhi_events[] = {
  222. #endif
  223. {
  224. .num_elements = 32,
  225. .irq_moderation_ms = 0,
  226. .irq = 1,
  227. .mode = MHI_DB_BRST_DISABLE,
  228. .data_type = MHI_ER_CTRL,
  229. .priority = 0,
  230. .hardware_event = false,
  231. .client_managed = false,
  232. .offload_channel = false,
  233. },
  234. {
  235. .num_elements = 256,
  236. .irq_moderation_ms = 0,
  237. .irq = 2,
  238. .mode = MHI_DB_BRST_DISABLE,
  239. .priority = 1,
  240. .hardware_event = false,
  241. .client_managed = false,
  242. .offload_channel = false,
  243. },
  244. #if IS_ENABLED(CONFIG_MHI_BUS_MISC)
  245. {
  246. .num_elements = 32,
  247. .irq_moderation_ms = 0,
  248. .irq = 1,
  249. .mode = MHI_DB_BRST_DISABLE,
  250. .data_type = MHI_ER_BW_SCALE,
  251. .priority = 2,
  252. .hardware_event = false,
  253. .client_managed = false,
  254. .offload_channel = false,
  255. },
  256. #endif
  257. #if IS_ENABLED(CONFIG_MHI_SATELLITE)
  258. {
  259. .num_elements = 256,
  260. .irq_moderation_ms = 0,
  261. .irq = 2,
  262. .mode = MHI_DB_BRST_DISABLE,
  263. .data_type = MHI_ER_DATA,
  264. .priority = 1,
  265. .hardware_event = false,
  266. .client_managed = true,
  267. .offload_channel = true,
  268. },
  269. #endif
  270. };
  271. #if IS_ENABLED(CONFIG_MHI_SATELLITE)
  272. #define CNSS_MHI_SATELLITE_CH_CFG_COUNT 4
  273. #define CNSS_MHI_SATELLITE_EVT_COUNT 1
  274. #else
  275. #define CNSS_MHI_SATELLITE_CH_CFG_COUNT 0
  276. #define CNSS_MHI_SATELLITE_EVT_COUNT 0
  277. #endif
  278. static const struct mhi_controller_config cnss_mhi_config_default = {
  279. #if IS_ENABLED(CONFIG_MHI_SATELLITE)
  280. .max_channels = 72,
  281. #else
  282. .max_channels = 32,
  283. #endif
  284. .timeout_ms = 10000,
  285. .use_bounce_buf = false,
  286. .buf_len = 0x8000,
  287. .num_channels = ARRAY_SIZE(cnss_mhi_channels),
  288. .ch_cfg = cnss_mhi_channels,
  289. .num_events = ARRAY_SIZE(cnss_mhi_events),
  290. .event_cfg = cnss_mhi_events,
  291. .m2_no_db = true,
  292. };
  293. static const struct mhi_controller_config cnss_mhi_config_no_satellite = {
  294. .max_channels = 32,
  295. .timeout_ms = 10000,
  296. .use_bounce_buf = false,
  297. .buf_len = 0x8000,
  298. .num_channels = ARRAY_SIZE(cnss_mhi_channels) -
  299. CNSS_MHI_SATELLITE_CH_CFG_COUNT,
  300. .ch_cfg = cnss_mhi_channels,
  301. .num_events = ARRAY_SIZE(cnss_mhi_events) -
  302. CNSS_MHI_SATELLITE_EVT_COUNT,
  303. .event_cfg = cnss_mhi_events,
  304. .m2_no_db = true,
  305. };
  306. static struct cnss_pci_reg ce_src[] = {
  307. { "SRC_RING_BASE_LSB", CE_SRC_RING_BASE_LSB_OFFSET },
  308. { "SRC_RING_BASE_MSB", CE_SRC_RING_BASE_MSB_OFFSET },
  309. { "SRC_RING_ID", CE_SRC_RING_ID_OFFSET },
  310. { "SRC_RING_MISC", CE_SRC_RING_MISC_OFFSET },
  311. { "SRC_CTRL", CE_SRC_CTRL_OFFSET },
  312. { "SRC_R0_CE_CH_SRC_IS", CE_SRC_R0_CE_CH_SRC_IS_OFFSET },
  313. { "SRC_RING_HP", CE_SRC_RING_HP_OFFSET },
  314. { "SRC_RING_TP", CE_SRC_RING_TP_OFFSET },
  315. { NULL },
  316. };
  317. static struct cnss_pci_reg ce_dst[] = {
  318. { "DEST_RING_BASE_LSB", CE_DEST_RING_BASE_LSB_OFFSET },
  319. { "DEST_RING_BASE_MSB", CE_DEST_RING_BASE_MSB_OFFSET },
  320. { "DEST_RING_ID", CE_DEST_RING_ID_OFFSET },
  321. { "DEST_RING_MISC", CE_DEST_RING_MISC_OFFSET },
  322. { "DEST_CTRL", CE_DEST_CTRL_OFFSET },
  323. { "CE_CH_DST_IS", CE_CH_DST_IS_OFFSET },
  324. { "CE_CH_DEST_CTRL2", CE_CH_DEST_CTRL2_OFFSET },
  325. { "DEST_RING_HP", CE_DEST_RING_HP_OFFSET },
  326. { "DEST_RING_TP", CE_DEST_RING_TP_OFFSET },
  327. { "STATUS_RING_BASE_LSB", CE_STATUS_RING_BASE_LSB_OFFSET },
  328. { "STATUS_RING_BASE_MSB", CE_STATUS_RING_BASE_MSB_OFFSET },
  329. { "STATUS_RING_ID", CE_STATUS_RING_ID_OFFSET },
  330. { "STATUS_RING_MISC", CE_STATUS_RING_MISC_OFFSET },
  331. { "STATUS_RING_HP", CE_STATUS_RING_HP_OFFSET },
  332. { "STATUS_RING_TP", CE_STATUS_RING_TP_OFFSET },
  333. { NULL },
  334. };
  335. static struct cnss_pci_reg ce_cmn[] = {
  336. { "GXI_ERR_INTS", CE_COMMON_GXI_ERR_INTS },
  337. { "GXI_ERR_STATS", CE_COMMON_GXI_ERR_STATS },
  338. { "GXI_WDOG_STATUS", CE_COMMON_GXI_WDOG_STATUS },
  339. { "TARGET_IE_0", CE_COMMON_TARGET_IE_0 },
  340. { "TARGET_IE_1", CE_COMMON_TARGET_IE_1 },
  341. { NULL },
  342. };
  343. static struct cnss_pci_reg qdss_csr[] = {
  344. { "QDSSCSR_ETRIRQCTRL", QDSS_APB_DEC_CSR_ETRIRQCTRL_OFFSET },
  345. { "QDSSCSR_PRESERVEETF", QDSS_APB_DEC_CSR_PRESERVEETF_OFFSET },
  346. { "QDSSCSR_PRESERVEETR0", QDSS_APB_DEC_CSR_PRESERVEETR0_OFFSET },
  347. { "QDSSCSR_PRESERVEETR1", QDSS_APB_DEC_CSR_PRESERVEETR1_OFFSET },
  348. { NULL },
  349. };
  350. static struct cnss_pci_reg pci_scratch[] = {
  351. { "PCIE_SCRATCH_0", PCIE_SCRATCH_0_SOC_PCIE_REG },
  352. { "PCIE_SCRATCH_1", PCIE_SCRATCH_1_SOC_PCIE_REG },
  353. { "PCIE_SCRATCH_2", PCIE_SCRATCH_2_SOC_PCIE_REG },
  354. { NULL },
  355. };
  356. /* First field of the structure is the device bit mask. Use
  357. * enum cnss_pci_reg_mask as reference for the value.
  358. */
  359. static struct cnss_misc_reg wcss_reg_access_seq[] = {
  360. {1, 0, QCA6390_GCC_DEBUG_CLK_CTL, 0},
  361. {1, 1, QCA6390_GCC_DEBUG_CLK_CTL, 0x802},
  362. {1, 0, QCA6390_GCC_DEBUG_CLK_CTL, 0},
  363. {1, 0, QCA6390_WCSS_Q6SS_PUBCSR_QDSP6SS_PLL_MODE, 0},
  364. {1, 1, QCA6390_GCC_DEBUG_CLK_CTL, 0x805},
  365. {1, 0, QCA6390_GCC_DEBUG_CLK_CTL, 0},
  366. {1, 0, QCA6390_WCSS_WFSS_PMM_WFSS_PMM_R0_PMM_CTRL, 0},
  367. {1, 0, QCA6390_WCSS_PMM_TOP_PMU_CX_CSR, 0},
  368. {1, 0, QCA6390_WCSS_PMM_TOP_AON_INT_RAW_STAT, 0},
  369. {1, 0, QCA6390_WCSS_PMM_TOP_AON_INT_EN, 0},
  370. {1, 0, QCA6390_WCSS_PMM_TOP_PMU_TESTBUS_STS, 0},
  371. {1, 1, QCA6390_WCSS_PMM_TOP_PMU_TESTBUS_CTL, 0xD},
  372. {1, 0, QCA6390_WCSS_PMM_TOP_TESTBUS_STS, 0},
  373. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_SAW2_CFG, 0},
  374. {1, 1, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_SAW2_CFG, 0},
  375. {1, 1, QCA6390_WCSS_Q6SS_PUBCSR_QDSP6SS_TEST_BUS_CTL, 0x8},
  376. {1, 0, QCA6390_WCSS_Q6SS_PUBCSR_QDSP6SS_TEST_BUS_VALUE, 0},
  377. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_SAW2_SPM_STS, 0},
  378. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_SAW2_SPM_CTL, 0},
  379. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_SAW2_SPM_SLP_SEQ_ENTRY_0, 0},
  380. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_SAW2_SPM_SLP_SEQ_ENTRY_9, 0},
  381. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_L2VIC_INT_STATUS0, 0},
  382. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_L2VIC_INT_STATUS1, 0},
  383. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_L2VIC_INT_STATUS2, 0},
  384. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_L2VIC_INT_STATUS3, 0},
  385. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_L2VIC_INT_STATUS4, 0},
  386. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_L2VIC_INT_STATUS5, 0},
  387. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_L2VIC_INT_STATUS6, 0},
  388. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_L2VIC_INT_ENABLE0, 0},
  389. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_L2VIC_INT_ENABLE1, 0},
  390. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_L2VIC_INT_ENABLE2, 0},
  391. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_L2VIC_INT_ENABLE3, 0},
  392. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_L2VIC_INT_ENABLE4, 0},
  393. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_L2VIC_INT_ENABLE5, 0},
  394. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_L2VIC_INT_ENABLE6, 0},
  395. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_L2VIC_INT_PENDING0, 0},
  396. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_L2VIC_INT_PENDING1, 0},
  397. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_L2VIC_INT_PENDING2, 0},
  398. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_L2VIC_INT_PENDING3, 0},
  399. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_L2VIC_INT_PENDING4, 0},
  400. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_L2VIC_INT_PENDING5, 0},
  401. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_L2VIC_INT_PENDING6, 0},
  402. {1, 1, QCA6390_WCSS_Q6SS_PUBCSR_QDSP6SS_TEST_BUS_CTL, 0x30040},
  403. {1, 0, QCA6390_WCSS_Q6SS_PUBCSR_QDSP6SS_TEST_BUS_CTL, 0},
  404. {1, 0, QCA6390_WCSS_Q6SS_PUBCSR_QDSP6SS_TEST_BUS_VALUE, 0},
  405. {1, 0, QCA6390_WCSS_Q6SS_PUBCSR_QDSP6SS_TEST_BUS_VALUE, 0},
  406. {1, 0, QCA6390_WCSS_Q6SS_PUBCSR_QDSP6SS_TEST_BUS_VALUE, 0},
  407. {1, 1, QCA6390_WCSS_Q6SS_PUBCSR_QDSP6SS_TEST_BUS_CTL, 0x30105},
  408. {1, 0, QCA6390_WCSS_Q6SS_PUBCSR_QDSP6SS_TEST_BUS_CTL, 0},
  409. {1, 0, QCA6390_WCSS_Q6SS_PUBCSR_QDSP6SS_TEST_BUS_VALUE, 0},
  410. {1, 0, QCA6390_WCSS_Q6SS_PUBCSR_QDSP6SS_TEST_BUS_VALUE, 0},
  411. {1, 0, QCA6390_WCSS_Q6SS_PUBCSR_QDSP6SS_TEST_BUS_VALUE, 0},
  412. {1, 0, QCA6390_WCSS_Q6SS_PUBCSR_QDSP6SS_TEST_BUS_VALUE, 0},
  413. {1, 0, QCA6390_WCSS_Q6SS_PUBCSR_QDSP6SS_TEST_BUS_VALUE, 0},
  414. {1, 0, QCA6390_WCSS_Q6SS_PUBCSR_QDSP6SS_TEST_BUS_VALUE, 0},
  415. {1, 0, QCA6390_WCSS_Q6SS_PUBCSR_QDSP6SS_TEST_BUS_VALUE, 0},
  416. {1, 0, QCA6390_WCSS_Q6SS_PUBCSR_QDSP6SS_TEST_BUS_CTL, 0},
  417. {1, 0, QCA6390_WCSS_CC_WCSS_UMAC_NOC_CBCR, 0},
  418. {1, 0, QCA6390_WCSS_CC_WCSS_UMAC_AHB_CBCR, 0},
  419. {1, 0, QCA6390_WCSS_CC_WCSS_UMAC_GDSCR, 0},
  420. {1, 0, QCA6390_WCSS_CC_WCSS_WLAN1_GDSCR, 0},
  421. {1, 0, QCA6390_WCSS_CC_WCSS_WLAN2_GDSCR, 0},
  422. {1, 0, QCA6390_WCSS_PMM_TOP_PMM_INT_CLR, 0},
  423. {1, 0, QCA6390_WCSS_PMM_TOP_AON_INT_STICKY_EN, 0},
  424. };
  425. static struct cnss_misc_reg pcie_reg_access_seq[] = {
  426. {1, 0, QCA6390_PCIE_PCIE_WCSS_STATUS_FOR_DEBUG_LOW_PCIE_LOCAL_REG, 0},
  427. {1, 0, QCA6390_PCIE_SOC_PCIE_WRAP_INTR_MASK_SOC_PCIE_REG, 0},
  428. {1, 1, QCA6390_PCIE_SOC_PCIE_WRAP_INTR_MASK_SOC_PCIE_REG, 0x18},
  429. {1, 0, QCA6390_PCIE_SOC_PCIE_WRAP_INTR_MASK_SOC_PCIE_REG, 0},
  430. {1, 0, QCA6390_PCIE_SOC_PCIE_WRAP_INTR_MASK_SOC_PCIE_REG, 0},
  431. {1, 0, QCA6390_PCIE_SOC_PCIE_WRAP_INTR_STATUS_SOC_PCIE_REG, 0},
  432. {1, 0, QCA6390_PCIE_SOC_COMMIT_REPLAY_SOC_PCIE_REG, 0},
  433. {1, 0, QCA6390_TLMM_GPIO_IN_OUT57, 0},
  434. {1, 0, QCA6390_TLMM_GPIO_INTR_CFG57, 0},
  435. {1, 0, QCA6390_TLMM_GPIO_INTR_STATUS57, 0},
  436. {1, 0, QCA6390_TLMM_GPIO_IN_OUT59, 0},
  437. {1, 0, QCA6390_TLMM_GPIO_INTR_CFG59, 0},
  438. {1, 0, QCA6390_TLMM_GPIO_INTR_STATUS59, 0},
  439. {1, 0, QCA6390_PCIE_PCIE_PARF_LTSSM, 0},
  440. {1, 0, QCA6390_PCIE_PCIE_PARF_PM_STTS, 0},
  441. {1, 0, QCA6390_PCIE_PCIE_PARF_PM_STTS_1, 0},
  442. {1, 0, QCA6390_PCIE_PCIE_PARF_INT_STATUS, 0},
  443. {1, 0, QCA6390_PCIE_PCIE_INT_ALL_STATUS, 0},
  444. {1, 0, QCA6390_PCIE_PCIE_INT_ALL_MASK, 0},
  445. {1, 0, QCA6390_PCIE_PCIE_PARF_BDF_TO_SID_CFG, 0},
  446. {1, 0, QCA6390_PCIE_PCIE_PARF_L1SS_SLEEP_NO_MHI_ACCESS_HANDLER_RD_4, 0},
  447. {1, 0, QCA6390_PCIE_PCIE_PARF_L1SS_SLEEP_NO_MHI_ACCESS_HANDLER_RD_3, 0},
  448. {1, 0, QCA6390_PCIE_PCIE_PARF_MHI_CLOCK_RESET_CTRL, 0},
  449. {1, 0, QCA6390_PCIE_PCIE_PARF_MHI_BASE_ADDR_LOWER, 0},
  450. {1, 0, QCA6390_PCIE_PCIE_PARF_L1SS_SLEEP_MODE_HANDLER_STATUS, 0},
  451. {1, 0, QCA6390_PCIE_PCIE_PARF_L1SS_SLEEP_MODE_HANDLER_CFG, 0},
  452. {1, 0, QCA6390_PCIE_PCIE_PARF_DEBUG_CNT_AUX_CLK_IN_L1SUB_L2, 0},
  453. {1, 0, QCA6390_PCIE_PCIE_PARF_DEBUG_CNT_PM_LINKST_IN_L1SUB, 0},
  454. {1, 0, QCA6390_PCIE_PCIE_CORE_CONFIG, 0},
  455. {1, 0, QCA6390_PCIE_PCIE_PARF_L1SS_SLEEP_NO_MHI_ACCESS_HANDLER_RD_4, 0},
  456. {1, 0, QCA6390_PCIE_PCIE_PARF_DEBUG_CNT_PM_LINKST_IN_L2, 0},
  457. {1, 0, QCA6390_PCIE_PCIE_PARF_DEBUG_CNT_PM_LINKST_IN_L1, 0},
  458. {1, 0, QCA6390_PCIE_PCIE_PARF_DEBUG_CNT_AUX_CLK_IN_L1SUB_L1, 0},
  459. {1, 0, QCA6390_PCIE_PCIE_PARF_DEBUG_CNT_AUX_CLK_IN_L1SUB_L2, 0},
  460. {1, 0, QCA6390_PCIE_PCIE_LOCAL_REG_WCSSAON_PCIE_SR_STATUS_HIGH, 0},
  461. {1, 0, QCA6390_PCIE_PCIE_LOCAL_REG_WCSSAON_PCIE_SR_STATUS_LOW, 0},
  462. {1, 0, QCA6390_PCIE_PCIE_LOCAL_REG_WCSS_STATUS_FOR_DEBUG_HIGH, 0},
  463. {1, 0, QCA6390_PCIE_PCIE_LOCAL_REG_WCSS_STATUS_FOR_DEBUG_LOW, 0},
  464. {1, 0, QCA6390_WFSS_PMM_WFSS_PMM_R0_WLAN1_STATUS_REG2, 0},
  465. {1, 0, QCA6390_WFSS_PMM_WFSS_PMM_R0_WLAN2_STATUS_REG2, 0},
  466. {1, 0, QCA6390_WFSS_PMM_WFSS_PMM_R0_PMM_WLAN2_CFG_REG1, 0},
  467. {1, 0, QCA6390_WFSS_PMM_WFSS_PMM_R0_PMM_WLAN1_CFG_REG1, 0},
  468. {1, 0, QCA6390_WFSS_PMM_WFSS_PMM_R0_WLAN2_APS_STATUS_REG1, 0},
  469. {1, 0, QCA6390_WFSS_PMM_WFSS_PMM_R0_WLAN1_APS_STATUS_REG1, 0},
  470. {1, 0, QCA6390_PCIE_PCIE_BHI_EXECENV_REG, 0},
  471. };
  472. static struct cnss_misc_reg wlaon_reg_access_seq[] = {
  473. {3, 0, WLAON_SOC_POWER_CTRL, 0},
  474. {3, 0, WLAON_SOC_PWR_WDG_BARK_THRSHD, 0},
  475. {3, 0, WLAON_SOC_PWR_WDG_BITE_THRSHD, 0},
  476. {3, 0, WLAON_SW_COLD_RESET, 0},
  477. {3, 0, WLAON_RFA_MEM_SLP_NRET_N_OVERRIDE, 0},
  478. {3, 0, WLAON_GDSC_DELAY_SETTING, 0},
  479. {3, 0, WLAON_GDSC_DELAY_SETTING2, 0},
  480. {3, 0, WLAON_WL_PWR_STATUS_REG, 0},
  481. {3, 0, WLAON_WL_AON_DBG_CFG_REG, 0},
  482. {2, 0, WLAON_WL_AON_DBG_ENABLE_GRP0_REG, 0},
  483. {2, 0, WLAON_WL_AON_DBG_ENABLE_GRP1_REG, 0},
  484. {2, 0, WLAON_WL_AON_APM_CFG_CTRL0, 0},
  485. {2, 0, WLAON_WL_AON_APM_CFG_CTRL1, 0},
  486. {2, 0, WLAON_WL_AON_APM_CFG_CTRL2, 0},
  487. {2, 0, WLAON_WL_AON_APM_CFG_CTRL3, 0},
  488. {2, 0, WLAON_WL_AON_APM_CFG_CTRL4, 0},
  489. {2, 0, WLAON_WL_AON_APM_CFG_CTRL5, 0},
  490. {2, 0, WLAON_WL_AON_APM_CFG_CTRL5_1, 0},
  491. {2, 0, WLAON_WL_AON_APM_CFG_CTRL6, 0},
  492. {2, 0, WLAON_WL_AON_APM_CFG_CTRL6_1, 0},
  493. {2, 0, WLAON_WL_AON_APM_CFG_CTRL7, 0},
  494. {2, 0, WLAON_WL_AON_APM_CFG_CTRL8, 0},
  495. {2, 0, WLAON_WL_AON_APM_CFG_CTRL8_1, 0},
  496. {2, 0, WLAON_WL_AON_APM_CFG_CTRL9, 0},
  497. {2, 0, WLAON_WL_AON_APM_CFG_CTRL9_1, 0},
  498. {2, 0, WLAON_WL_AON_APM_CFG_CTRL10, 0},
  499. {2, 0, WLAON_WL_AON_APM_CFG_CTRL11, 0},
  500. {2, 0, WLAON_WL_AON_APM_CFG_CTRL12, 0},
  501. {2, 0, WLAON_WL_AON_APM_OVERRIDE_REG, 0},
  502. {2, 0, WLAON_WL_AON_CXPC_REG, 0},
  503. {2, 0, WLAON_WL_AON_APM_STATUS0, 0},
  504. {2, 0, WLAON_WL_AON_APM_STATUS1, 0},
  505. {2, 0, WLAON_WL_AON_APM_STATUS2, 0},
  506. {2, 0, WLAON_WL_AON_APM_STATUS3, 0},
  507. {2, 0, WLAON_WL_AON_APM_STATUS4, 0},
  508. {2, 0, WLAON_WL_AON_APM_STATUS5, 0},
  509. {2, 0, WLAON_WL_AON_APM_STATUS6, 0},
  510. {3, 0, WLAON_GLOBAL_COUNTER_CTRL1, 0},
  511. {3, 0, WLAON_GLOBAL_COUNTER_CTRL6, 0},
  512. {3, 0, WLAON_GLOBAL_COUNTER_CTRL7, 0},
  513. {3, 0, WLAON_GLOBAL_COUNTER_CTRL3, 0},
  514. {3, 0, WLAON_GLOBAL_COUNTER_CTRL4, 0},
  515. {3, 0, WLAON_GLOBAL_COUNTER_CTRL5, 0},
  516. {3, 0, WLAON_GLOBAL_COUNTER_CTRL8, 0},
  517. {3, 0, WLAON_GLOBAL_COUNTER_CTRL2, 0},
  518. {3, 0, WLAON_GLOBAL_COUNTER_CTRL9, 0},
  519. {3, 0, WLAON_RTC_CLK_CAL_CTRL1, 0},
  520. {3, 0, WLAON_RTC_CLK_CAL_CTRL2, 0},
  521. {3, 0, WLAON_RTC_CLK_CAL_CTRL3, 0},
  522. {3, 0, WLAON_RTC_CLK_CAL_CTRL4, 0},
  523. {3, 0, WLAON_RTC_CLK_CAL_CTRL5, 0},
  524. {3, 0, WLAON_RTC_CLK_CAL_CTRL6, 0},
  525. {3, 0, WLAON_RTC_CLK_CAL_CTRL7, 0},
  526. {3, 0, WLAON_RTC_CLK_CAL_CTRL8, 0},
  527. {3, 0, WLAON_RTC_CLK_CAL_CTRL9, 0},
  528. {3, 0, WLAON_WCSSAON_CONFIG_REG, 0},
  529. {3, 0, WLAON_WLAN_OEM_DEBUG_REG, 0},
  530. {3, 0, WLAON_WLAN_RAM_DUMP_REG, 0},
  531. {3, 0, WLAON_QDSS_WCSS_REG, 0},
  532. {3, 0, WLAON_QDSS_WCSS_ACK, 0},
  533. {3, 0, WLAON_WL_CLK_CNTL_KDF_REG, 0},
  534. {3, 0, WLAON_WL_CLK_CNTL_PMU_HFRC_REG, 0},
  535. {3, 0, WLAON_QFPROM_PWR_CTRL_REG, 0},
  536. {3, 0, WLAON_DLY_CONFIG, 0},
  537. {3, 0, WLAON_WLAON_Q6_IRQ_REG, 0},
  538. {3, 0, WLAON_PCIE_INTF_SW_CFG_REG, 0},
  539. {3, 0, WLAON_PCIE_INTF_STICKY_SW_CFG_REG, 0},
  540. {3, 0, WLAON_PCIE_INTF_PHY_SW_CFG_REG, 0},
  541. {3, 0, WLAON_PCIE_INTF_PHY_NOCSR_SW_CFG_REG, 0},
  542. {3, 0, WLAON_Q6_COOKIE_BIT, 0},
  543. {3, 0, WLAON_WARM_SW_ENTRY, 0},
  544. {3, 0, WLAON_RESET_DBG_SW_ENTRY, 0},
  545. {3, 0, WLAON_WL_PMUNOC_CFG_REG, 0},
  546. {3, 0, WLAON_RESET_CAUSE_CFG_REG, 0},
  547. {3, 0, WLAON_SOC_WCSSAON_WAKEUP_IRQ_7_EN_REG, 0},
  548. {3, 0, WLAON_DEBUG, 0},
  549. {3, 0, WLAON_SOC_PARAMETERS, 0},
  550. {3, 0, WLAON_WLPM_SIGNAL, 0},
  551. {3, 0, WLAON_SOC_RESET_CAUSE_REG, 0},
  552. {3, 0, WLAON_WAKEUP_PCIE_SOC_REG, 0},
  553. {3, 0, WLAON_PBL_STACK_CANARY, 0},
  554. {3, 0, WLAON_MEM_TOT_NUM_GRP_REG, 0},
  555. {3, 0, WLAON_MEM_TOT_BANKS_IN_GRP0_REG, 0},
  556. {3, 0, WLAON_MEM_TOT_BANKS_IN_GRP1_REG, 0},
  557. {3, 0, WLAON_MEM_TOT_BANKS_IN_GRP2_REG, 0},
  558. {3, 0, WLAON_MEM_TOT_BANKS_IN_GRP3_REG, 0},
  559. {3, 0, WLAON_MEM_TOT_SIZE_IN_GRP0_REG, 0},
  560. {3, 0, WLAON_MEM_TOT_SIZE_IN_GRP1_REG, 0},
  561. {3, 0, WLAON_MEM_TOT_SIZE_IN_GRP2_REG, 0},
  562. {3, 0, WLAON_MEM_TOT_SIZE_IN_GRP3_REG, 0},
  563. {3, 0, WLAON_MEM_SLP_NRET_OVERRIDE_GRP0_REG, 0},
  564. {3, 0, WLAON_MEM_SLP_NRET_OVERRIDE_GRP1_REG, 0},
  565. {3, 0, WLAON_MEM_SLP_NRET_OVERRIDE_GRP2_REG, 0},
  566. {3, 0, WLAON_MEM_SLP_NRET_OVERRIDE_GRP3_REG, 0},
  567. {3, 0, WLAON_MEM_SLP_RET_OVERRIDE_GRP0_REG, 0},
  568. {3, 0, WLAON_MEM_SLP_RET_OVERRIDE_GRP1_REG, 0},
  569. {3, 0, WLAON_MEM_SLP_RET_OVERRIDE_GRP2_REG, 0},
  570. {3, 0, WLAON_MEM_SLP_RET_OVERRIDE_GRP3_REG, 0},
  571. {3, 0, WLAON_MEM_CNT_SEL_REG, 0},
  572. {3, 0, WLAON_MEM_NO_EXTBHS_REG, 0},
  573. {3, 0, WLAON_MEM_DEBUG_REG, 0},
  574. {3, 0, WLAON_MEM_DEBUG_BUS_REG, 0},
  575. {3, 0, WLAON_MEM_REDUN_CFG_REG, 0},
  576. {3, 0, WLAON_WL_AON_SPARE2, 0},
  577. {3, 0, WLAON_VSEL_CFG_FOR_WL_RET_DISABLE_REG, 0},
  578. {3, 0, WLAON_BTFM_WLAN_IPC_STATUS_REG, 0},
  579. {3, 0, WLAON_MPM_COUNTER_CHICKEN_BITS, 0},
  580. {3, 0, WLAON_WLPM_CHICKEN_BITS, 0},
  581. {3, 0, WLAON_PCIE_PHY_PWR_REG, 0},
  582. {3, 0, WLAON_WL_CLK_CNTL_PMU_LPO2M_REG, 0},
  583. {3, 0, WLAON_WL_SS_ROOT_CLK_SWITCH_REG, 0},
  584. {3, 0, WLAON_POWERCTRL_PMU_REG, 0},
  585. {3, 0, WLAON_POWERCTRL_MEM_REG, 0},
  586. {3, 0, WLAON_PCIE_PWR_CTRL_REG, 0},
  587. {3, 0, WLAON_SOC_PWR_PROFILE_REG, 0},
  588. {3, 0, WLAON_WCSSAON_PCIE_SR_STATUS_HI_REG, 0},
  589. {3, 0, WLAON_WCSSAON_PCIE_SR_STATUS_LO_REG, 0},
  590. {3, 0, WLAON_WCSS_TCSR_PMM_SR_STATUS_HI_REG, 0},
  591. {3, 0, WLAON_WCSS_TCSR_PMM_SR_STATUS_LO_REG, 0},
  592. {3, 0, WLAON_MEM_SVS_CFG_REG, 0},
  593. {3, 0, WLAON_CMN_AON_MISC_REG, 0},
  594. {3, 0, WLAON_INTR_STATUS, 0},
  595. {2, 0, WLAON_INTR_ENABLE, 0},
  596. {2, 0, WLAON_NOC_DBG_BUS_SEL_REG, 0},
  597. {2, 0, WLAON_NOC_DBG_BUS_REG, 0},
  598. {2, 0, WLAON_WL_CTRL_MISC_REG, 0},
  599. {2, 0, WLAON_DBG_STATUS0, 0},
  600. {2, 0, WLAON_DBG_STATUS1, 0},
  601. {2, 0, WLAON_TIMERSYNC_OFFSET_L, 0},
  602. {2, 0, WLAON_TIMERSYNC_OFFSET_H, 0},
  603. {2, 0, WLAON_PMU_LDO_SETTLE_REG, 0},
  604. };
  605. static struct cnss_misc_reg syspm_reg_access_seq[] = {
  606. {1, 0, QCA6390_SYSPM_SYSPM_PWR_STATUS, 0},
  607. {1, 0, QCA6390_SYSPM_DBG_BTFM_AON_REG, 0},
  608. {1, 0, QCA6390_SYSPM_DBG_BUS_SEL_REG, 0},
  609. {1, 0, QCA6390_SYSPM_WCSSAON_SR_STATUS, 0},
  610. {1, 0, QCA6390_SYSPM_WCSSAON_SR_STATUS, 0},
  611. {1, 0, QCA6390_SYSPM_WCSSAON_SR_STATUS, 0},
  612. {1, 0, QCA6390_SYSPM_WCSSAON_SR_STATUS, 0},
  613. {1, 0, QCA6390_SYSPM_WCSSAON_SR_STATUS, 0},
  614. {1, 0, QCA6390_SYSPM_WCSSAON_SR_STATUS, 0},
  615. {1, 0, QCA6390_SYSPM_WCSSAON_SR_STATUS, 0},
  616. {1, 0, QCA6390_SYSPM_WCSSAON_SR_STATUS, 0},
  617. {1, 0, QCA6390_SYSPM_WCSSAON_SR_STATUS, 0},
  618. {1, 0, QCA6390_SYSPM_WCSSAON_SR_STATUS, 0},
  619. };
  620. static struct cnss_print_optimize print_optimize;
  621. #define WCSS_REG_SIZE ARRAY_SIZE(wcss_reg_access_seq)
  622. #define PCIE_REG_SIZE ARRAY_SIZE(pcie_reg_access_seq)
  623. #define WLAON_REG_SIZE ARRAY_SIZE(wlaon_reg_access_seq)
  624. #define SYSPM_REG_SIZE ARRAY_SIZE(syspm_reg_access_seq)
  625. static int cnss_pci_update_fw_name(struct cnss_pci_data *pci_priv);
  626. #if IS_ENABLED(CONFIG_MHI_BUS_MISC)
  627. static void cnss_mhi_debug_reg_dump(struct cnss_pci_data *pci_priv)
  628. {
  629. mhi_debug_reg_dump(pci_priv->mhi_ctrl);
  630. }
  631. static void cnss_mhi_dump_sfr(struct cnss_pci_data *pci_priv)
  632. {
  633. mhi_dump_sfr(pci_priv->mhi_ctrl);
  634. }
  635. static bool cnss_mhi_scan_rddm_cookie(struct cnss_pci_data *pci_priv,
  636. u32 cookie)
  637. {
  638. return mhi_scan_rddm_cookie(pci_priv->mhi_ctrl, cookie);
  639. }
  640. static int cnss_mhi_pm_fast_suspend(struct cnss_pci_data *pci_priv,
  641. bool notify_clients)
  642. {
  643. return mhi_pm_fast_suspend(pci_priv->mhi_ctrl, notify_clients);
  644. }
  645. static int cnss_mhi_pm_fast_resume(struct cnss_pci_data *pci_priv,
  646. bool notify_clients)
  647. {
  648. return mhi_pm_fast_resume(pci_priv->mhi_ctrl, notify_clients);
  649. }
  650. static void cnss_mhi_set_m2_timeout_ms(struct cnss_pci_data *pci_priv,
  651. u32 timeout)
  652. {
  653. return mhi_set_m2_timeout_ms(pci_priv->mhi_ctrl, timeout);
  654. }
  655. static int cnss_mhi_device_get_sync_atomic(struct cnss_pci_data *pci_priv,
  656. int timeout_us, bool in_panic)
  657. {
  658. return mhi_device_get_sync_atomic(pci_priv->mhi_ctrl->mhi_dev,
  659. timeout_us, in_panic);
  660. }
  661. static void
  662. cnss_mhi_controller_set_bw_scale_cb(struct cnss_pci_data *pci_priv,
  663. int (*cb)(struct mhi_controller *mhi_ctrl,
  664. struct mhi_link_info *link_info))
  665. {
  666. mhi_controller_set_bw_scale_cb(pci_priv->mhi_ctrl, cb);
  667. }
  668. static int cnss_mhi_force_reset(struct cnss_pci_data *pci_priv)
  669. {
  670. return mhi_force_reset(pci_priv->mhi_ctrl);
  671. }
  672. static void cnss_mhi_controller_set_base(struct cnss_pci_data *pci_priv,
  673. phys_addr_t base)
  674. {
  675. return mhi_controller_set_base(pci_priv->mhi_ctrl, base);
  676. }
  677. #else
  678. static void cnss_mhi_debug_reg_dump(struct cnss_pci_data *pci_priv)
  679. {
  680. }
  681. static void cnss_mhi_dump_sfr(struct cnss_pci_data *pci_priv)
  682. {
  683. }
  684. static bool cnss_mhi_scan_rddm_cookie(struct cnss_pci_data *pci_priv,
  685. u32 cookie)
  686. {
  687. return false;
  688. }
  689. static int cnss_mhi_pm_fast_suspend(struct cnss_pci_data *pci_priv,
  690. bool notify_clients)
  691. {
  692. return -EOPNOTSUPP;
  693. }
  694. static int cnss_mhi_pm_fast_resume(struct cnss_pci_data *pci_priv,
  695. bool notify_clients)
  696. {
  697. return -EOPNOTSUPP;
  698. }
  699. static void cnss_mhi_set_m2_timeout_ms(struct cnss_pci_data *pci_priv,
  700. u32 timeout)
  701. {
  702. }
  703. static int cnss_mhi_device_get_sync_atomic(struct cnss_pci_data *pci_priv,
  704. int timeout_us, bool in_panic)
  705. {
  706. return -EOPNOTSUPP;
  707. }
  708. static void
  709. cnss_mhi_controller_set_bw_scale_cb(struct cnss_pci_data *pci_priv,
  710. int (*cb)(struct mhi_controller *mhi_ctrl,
  711. struct mhi_link_info *link_info))
  712. {
  713. }
  714. static int cnss_mhi_force_reset(struct cnss_pci_data *pci_priv)
  715. {
  716. return -EOPNOTSUPP;
  717. }
  718. static void cnss_mhi_controller_set_base(struct cnss_pci_data *pci_priv,
  719. phys_addr_t base)
  720. {
  721. }
  722. #endif /* CONFIG_MHI_BUS_MISC */
  723. int cnss_pci_check_link_status(struct cnss_pci_data *pci_priv)
  724. {
  725. u16 device_id;
  726. if (pci_priv->pci_link_state == PCI_LINK_DOWN) {
  727. cnss_pr_dbg("%ps: PCIe link is in suspend state\n",
  728. (void *)_RET_IP_);
  729. return -EACCES;
  730. }
  731. if (pci_priv->pci_link_down_ind) {
  732. cnss_pr_err("%ps: PCIe link is down\n", (void *)_RET_IP_);
  733. return -EIO;
  734. }
  735. pci_read_config_word(pci_priv->pci_dev, PCI_DEVICE_ID, &device_id);
  736. if (device_id != pci_priv->device_id) {
  737. cnss_fatal_err("%ps: PCI device ID mismatch, link possibly down, current read ID: 0x%x, record ID: 0x%x\n",
  738. (void *)_RET_IP_, device_id,
  739. pci_priv->device_id);
  740. return -EIO;
  741. }
  742. return 0;
  743. }
  744. static void cnss_pci_select_window(struct cnss_pci_data *pci_priv, u32 offset)
  745. {
  746. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  747. u32 window = (offset >> WINDOW_SHIFT) & WINDOW_VALUE_MASK;
  748. u32 window_enable = WINDOW_ENABLE_BIT | window;
  749. u32 val;
  750. writel_relaxed(window_enable, pci_priv->bar +
  751. QCA6390_PCIE_REMAP_BAR_CTRL_OFFSET);
  752. if (window != pci_priv->remap_window) {
  753. pci_priv->remap_window = window;
  754. cnss_pr_dbg("Config PCIe remap window register to 0x%x\n",
  755. window_enable);
  756. }
  757. /* Read it back to make sure the write has taken effect */
  758. val = readl_relaxed(pci_priv->bar + QCA6390_PCIE_REMAP_BAR_CTRL_OFFSET);
  759. if (val != window_enable) {
  760. cnss_pr_err("Failed to config window register to 0x%x, current value: 0x%x\n",
  761. window_enable, val);
  762. if (!cnss_pci_check_link_status(pci_priv) &&
  763. !test_bit(CNSS_IN_PANIC, &plat_priv->driver_state))
  764. CNSS_ASSERT(0);
  765. }
  766. }
  767. static int cnss_pci_reg_read(struct cnss_pci_data *pci_priv,
  768. u32 offset, u32 *val)
  769. {
  770. int ret;
  771. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  772. if (!in_interrupt() && !irqs_disabled()) {
  773. ret = cnss_pci_check_link_status(pci_priv);
  774. if (ret)
  775. return ret;
  776. }
  777. if (pci_priv->pci_dev->device == QCA6174_DEVICE_ID ||
  778. offset < MAX_UNWINDOWED_ADDRESS) {
  779. *val = readl_relaxed(pci_priv->bar + offset);
  780. return 0;
  781. }
  782. /* If in panic, assumption is kernel panic handler will hold all threads
  783. * and interrupts. Further pci_reg_window_lock could be held before
  784. * panic. So only lock during normal operation.
  785. */
  786. if (test_bit(CNSS_IN_PANIC, &plat_priv->driver_state)) {
  787. cnss_pci_select_window(pci_priv, offset);
  788. *val = readl_relaxed(pci_priv->bar + WINDOW_START +
  789. (offset & WINDOW_RANGE_MASK));
  790. } else {
  791. spin_lock_bh(&pci_reg_window_lock);
  792. cnss_pci_select_window(pci_priv, offset);
  793. *val = readl_relaxed(pci_priv->bar + WINDOW_START +
  794. (offset & WINDOW_RANGE_MASK));
  795. spin_unlock_bh(&pci_reg_window_lock);
  796. }
  797. return 0;
  798. }
  799. static int cnss_pci_reg_write(struct cnss_pci_data *pci_priv, u32 offset,
  800. u32 val)
  801. {
  802. int ret;
  803. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  804. if (!in_interrupt() && !irqs_disabled()) {
  805. ret = cnss_pci_check_link_status(pci_priv);
  806. if (ret)
  807. return ret;
  808. }
  809. if (pci_priv->pci_dev->device == QCA6174_DEVICE_ID ||
  810. offset < MAX_UNWINDOWED_ADDRESS) {
  811. writel_relaxed(val, pci_priv->bar + offset);
  812. return 0;
  813. }
  814. /* Same constraint as PCI register read in panic */
  815. if (test_bit(CNSS_IN_PANIC, &plat_priv->driver_state)) {
  816. cnss_pci_select_window(pci_priv, offset);
  817. writel_relaxed(val, pci_priv->bar + WINDOW_START +
  818. (offset & WINDOW_RANGE_MASK));
  819. } else {
  820. spin_lock_bh(&pci_reg_window_lock);
  821. cnss_pci_select_window(pci_priv, offset);
  822. writel_relaxed(val, pci_priv->bar + WINDOW_START +
  823. (offset & WINDOW_RANGE_MASK));
  824. spin_unlock_bh(&pci_reg_window_lock);
  825. }
  826. return 0;
  827. }
  828. static int cnss_pci_force_wake_get(struct cnss_pci_data *pci_priv)
  829. {
  830. struct device *dev = &pci_priv->pci_dev->dev;
  831. int ret;
  832. ret = cnss_pci_force_wake_request_sync(dev,
  833. FORCE_WAKE_DELAY_TIMEOUT_US);
  834. if (ret) {
  835. if (ret != -EAGAIN)
  836. cnss_pr_err("Failed to request force wake\n");
  837. return ret;
  838. }
  839. /* If device's M1 state-change event races here, it can be ignored,
  840. * as the device is expected to immediately move from M2 to M0
  841. * without entering low power state.
  842. */
  843. if (cnss_pci_is_device_awake(dev) != true)
  844. cnss_pr_warn("MHI not in M0, while reg still accessible\n");
  845. return 0;
  846. }
  847. static int cnss_pci_force_wake_put(struct cnss_pci_data *pci_priv)
  848. {
  849. struct device *dev = &pci_priv->pci_dev->dev;
  850. int ret;
  851. ret = cnss_pci_force_wake_release(dev);
  852. if (ret && ret != -EAGAIN)
  853. cnss_pr_err("Failed to release force wake\n");
  854. return ret;
  855. }
  856. #if IS_ENABLED(CONFIG_INTERCONNECT)
  857. /**
  858. * cnss_setup_bus_bandwidth() - Setup interconnect vote for given bandwidth
  859. * @plat_priv: Platform private data struct
  860. * @bw: bandwidth
  861. * @save: toggle flag to save bandwidth to current_bw_vote
  862. *
  863. * Setup bandwidth votes for configured interconnect paths
  864. *
  865. * Return: 0 for success
  866. */
  867. static int cnss_setup_bus_bandwidth(struct cnss_plat_data *plat_priv,
  868. u32 bw, bool save)
  869. {
  870. int ret = 0;
  871. struct cnss_bus_bw_info *bus_bw_info;
  872. if (!plat_priv->icc.path_count)
  873. return -EOPNOTSUPP;
  874. if (bw >= plat_priv->icc.bus_bw_cfg_count) {
  875. cnss_pr_err("Invalid bus bandwidth Type: %d", bw);
  876. return -EINVAL;
  877. }
  878. cnss_pr_vdbg("Bandwidth vote to %d, save %d\n", bw, save);
  879. list_for_each_entry(bus_bw_info, &plat_priv->icc.list_head, list) {
  880. ret = icc_set_bw(bus_bw_info->icc_path,
  881. bus_bw_info->cfg_table[bw].avg_bw,
  882. bus_bw_info->cfg_table[bw].peak_bw);
  883. if (ret) {
  884. cnss_pr_err("Could not set BW Cfg: %d, err = %d ICC Path: %s Val: %d %d\n",
  885. bw, ret, bus_bw_info->icc_name,
  886. bus_bw_info->cfg_table[bw].avg_bw,
  887. bus_bw_info->cfg_table[bw].peak_bw);
  888. break;
  889. }
  890. }
  891. if (ret == 0 && save)
  892. plat_priv->icc.current_bw_vote = bw;
  893. return ret;
  894. }
  895. int cnss_request_bus_bandwidth(struct device *dev, int bandwidth)
  896. {
  897. struct cnss_plat_data *plat_priv = cnss_bus_dev_to_plat_priv(dev);
  898. if (!plat_priv)
  899. return -ENODEV;
  900. if (bandwidth < 0)
  901. return -EINVAL;
  902. return cnss_setup_bus_bandwidth(plat_priv, (u32)bandwidth, true);
  903. }
  904. #else
  905. static int cnss_setup_bus_bandwidth(struct cnss_plat_data *plat_priv,
  906. u32 bw, bool save)
  907. {
  908. return 0;
  909. }
  910. int cnss_request_bus_bandwidth(struct device *dev, int bandwidth)
  911. {
  912. return 0;
  913. }
  914. #endif
  915. EXPORT_SYMBOL(cnss_request_bus_bandwidth);
  916. int cnss_pci_debug_reg_read(struct cnss_pci_data *pci_priv, u32 offset,
  917. u32 *val, bool raw_access)
  918. {
  919. int ret = 0;
  920. bool do_force_wake_put = true;
  921. if (raw_access) {
  922. ret = cnss_pci_reg_read(pci_priv, offset, val);
  923. goto out;
  924. }
  925. ret = cnss_pci_is_device_down(&pci_priv->pci_dev->dev);
  926. if (ret)
  927. goto out;
  928. ret = cnss_pci_pm_runtime_get_sync(pci_priv, RTPM_ID_CNSS);
  929. if (ret < 0)
  930. goto runtime_pm_put;
  931. ret = cnss_pci_force_wake_get(pci_priv);
  932. if (ret)
  933. do_force_wake_put = false;
  934. ret = cnss_pci_reg_read(pci_priv, offset, val);
  935. if (ret) {
  936. cnss_pr_err("Failed to read register offset 0x%x, err = %d\n",
  937. offset, ret);
  938. goto force_wake_put;
  939. }
  940. force_wake_put:
  941. if (do_force_wake_put)
  942. cnss_pci_force_wake_put(pci_priv);
  943. runtime_pm_put:
  944. cnss_pci_pm_runtime_mark_last_busy(pci_priv);
  945. cnss_pci_pm_runtime_put_autosuspend(pci_priv, RTPM_ID_CNSS);
  946. out:
  947. return ret;
  948. }
  949. int cnss_pci_debug_reg_write(struct cnss_pci_data *pci_priv, u32 offset,
  950. u32 val, bool raw_access)
  951. {
  952. int ret = 0;
  953. bool do_force_wake_put = true;
  954. if (raw_access) {
  955. ret = cnss_pci_reg_write(pci_priv, offset, val);
  956. goto out;
  957. }
  958. ret = cnss_pci_is_device_down(&pci_priv->pci_dev->dev);
  959. if (ret)
  960. goto out;
  961. ret = cnss_pci_pm_runtime_get_sync(pci_priv, RTPM_ID_CNSS);
  962. if (ret < 0)
  963. goto runtime_pm_put;
  964. ret = cnss_pci_force_wake_get(pci_priv);
  965. if (ret)
  966. do_force_wake_put = false;
  967. ret = cnss_pci_reg_write(pci_priv, offset, val);
  968. if (ret) {
  969. cnss_pr_err("Failed to write 0x%x to register offset 0x%x, err = %d\n",
  970. val, offset, ret);
  971. goto force_wake_put;
  972. }
  973. force_wake_put:
  974. if (do_force_wake_put)
  975. cnss_pci_force_wake_put(pci_priv);
  976. runtime_pm_put:
  977. cnss_pci_pm_runtime_mark_last_busy(pci_priv);
  978. cnss_pci_pm_runtime_put_autosuspend(pci_priv, RTPM_ID_CNSS);
  979. out:
  980. return ret;
  981. }
  982. static int cnss_set_pci_config_space(struct cnss_pci_data *pci_priv, bool save)
  983. {
  984. struct pci_dev *pci_dev = pci_priv->pci_dev;
  985. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  986. bool link_down_or_recovery;
  987. if (!plat_priv)
  988. return -ENODEV;
  989. link_down_or_recovery = pci_priv->pci_link_down_ind ||
  990. (test_bit(CNSS_DRIVER_RECOVERY, &plat_priv->driver_state));
  991. if (save) {
  992. if (link_down_or_recovery) {
  993. pci_priv->saved_state = NULL;
  994. } else {
  995. pci_save_state(pci_dev);
  996. pci_priv->saved_state = pci_store_saved_state(pci_dev);
  997. }
  998. } else {
  999. if (link_down_or_recovery) {
  1000. pci_load_saved_state(pci_dev, pci_priv->default_state);
  1001. pci_restore_state(pci_dev);
  1002. } else if (pci_priv->saved_state) {
  1003. pci_load_and_free_saved_state(pci_dev,
  1004. &pci_priv->saved_state);
  1005. pci_restore_state(pci_dev);
  1006. }
  1007. }
  1008. return 0;
  1009. }
  1010. static int cnss_pci_get_link_status(struct cnss_pci_data *pci_priv)
  1011. {
  1012. u16 link_status;
  1013. int ret;
  1014. ret = pcie_capability_read_word(pci_priv->pci_dev, PCI_EXP_LNKSTA,
  1015. &link_status);
  1016. if (ret)
  1017. return ret;
  1018. cnss_pr_dbg("Get PCI link status register: %u\n", link_status);
  1019. pci_priv->def_link_speed = link_status & PCI_EXP_LNKSTA_CLS;
  1020. pci_priv->def_link_width =
  1021. (link_status & PCI_EXP_LNKSTA_NLW) >> PCI_EXP_LNKSTA_NLW_SHIFT;
  1022. pci_priv->cur_link_speed = pci_priv->def_link_speed;
  1023. cnss_pr_dbg("Default PCI link speed is 0x%x, link width is 0x%x\n",
  1024. pci_priv->def_link_speed, pci_priv->def_link_width);
  1025. return 0;
  1026. }
  1027. static void cnss_pci_soc_scratch_reg_dump(struct cnss_pci_data *pci_priv)
  1028. {
  1029. u32 reg_offset, val;
  1030. int i;
  1031. switch (pci_priv->device_id) {
  1032. case QCA6390_DEVICE_ID:
  1033. case QCA6490_DEVICE_ID:
  1034. break;
  1035. default:
  1036. return;
  1037. }
  1038. if (in_interrupt() || irqs_disabled())
  1039. return;
  1040. if (cnss_pci_check_link_status(pci_priv))
  1041. return;
  1042. cnss_pr_dbg("Start to dump SOC Scratch registers\n");
  1043. for (i = 0; pci_scratch[i].name; i++) {
  1044. reg_offset = pci_scratch[i].offset;
  1045. if (cnss_pci_reg_read(pci_priv, reg_offset, &val))
  1046. return;
  1047. cnss_pr_dbg("PCIE_SOC_REG_%s = 0x%x\n",
  1048. pci_scratch[i].name, val);
  1049. }
  1050. }
  1051. int cnss_suspend_pci_link(struct cnss_pci_data *pci_priv)
  1052. {
  1053. int ret = 0;
  1054. if (!pci_priv)
  1055. return -ENODEV;
  1056. if (pci_priv->pci_link_state == PCI_LINK_DOWN) {
  1057. cnss_pr_info("PCI link is already suspended\n");
  1058. goto out;
  1059. }
  1060. pci_clear_master(pci_priv->pci_dev);
  1061. ret = cnss_set_pci_config_space(pci_priv, SAVE_PCI_CONFIG_SPACE);
  1062. if (ret)
  1063. goto out;
  1064. pci_disable_device(pci_priv->pci_dev);
  1065. if (pci_priv->pci_dev->device != QCA6174_DEVICE_ID) {
  1066. if (pci_set_power_state(pci_priv->pci_dev, PCI_D3hot))
  1067. cnss_pr_err("Failed to set D3Hot, err = %d\n", ret);
  1068. }
  1069. /* Always do PCIe L2 suspend during power off/PCIe link recovery */
  1070. pci_priv->drv_connected_last = 0;
  1071. ret = cnss_set_pci_link(pci_priv, PCI_LINK_DOWN);
  1072. if (ret)
  1073. goto out;
  1074. pci_priv->pci_link_state = PCI_LINK_DOWN;
  1075. return 0;
  1076. out:
  1077. return ret;
  1078. }
  1079. int cnss_resume_pci_link(struct cnss_pci_data *pci_priv)
  1080. {
  1081. int ret = 0;
  1082. if (!pci_priv)
  1083. return -ENODEV;
  1084. if (pci_priv->pci_link_state == PCI_LINK_UP) {
  1085. cnss_pr_info("PCI link is already resumed\n");
  1086. goto out;
  1087. }
  1088. ret = cnss_set_pci_link(pci_priv, PCI_LINK_UP);
  1089. if (ret) {
  1090. ret = -EAGAIN;
  1091. goto out;
  1092. }
  1093. pci_priv->pci_link_state = PCI_LINK_UP;
  1094. if (pci_priv->pci_dev->device != QCA6174_DEVICE_ID) {
  1095. ret = pci_set_power_state(pci_priv->pci_dev, PCI_D0);
  1096. if (ret) {
  1097. cnss_pr_err("Failed to set D0, err = %d\n", ret);
  1098. goto out;
  1099. }
  1100. }
  1101. ret = pci_enable_device(pci_priv->pci_dev);
  1102. if (ret) {
  1103. cnss_pr_err("Failed to enable PCI device, err = %d\n", ret);
  1104. goto out;
  1105. }
  1106. ret = cnss_set_pci_config_space(pci_priv, RESTORE_PCI_CONFIG_SPACE);
  1107. if (ret)
  1108. goto out;
  1109. pci_set_master(pci_priv->pci_dev);
  1110. if (pci_priv->pci_link_down_ind)
  1111. pci_priv->pci_link_down_ind = false;
  1112. return 0;
  1113. out:
  1114. return ret;
  1115. }
  1116. int cnss_pci_recover_link_down(struct cnss_pci_data *pci_priv)
  1117. {
  1118. int ret;
  1119. switch (pci_priv->device_id) {
  1120. case QCA6390_DEVICE_ID:
  1121. case QCA6490_DEVICE_ID:
  1122. case KIWI_DEVICE_ID:
  1123. case MANGO_DEVICE_ID:
  1124. break;
  1125. default:
  1126. return -EOPNOTSUPP;
  1127. }
  1128. /* Always wait here to avoid missing WAKE assert for RDDM
  1129. * before link recovery
  1130. */
  1131. msleep(WAKE_EVENT_TIMEOUT);
  1132. ret = cnss_suspend_pci_link(pci_priv);
  1133. if (ret)
  1134. cnss_pr_err("Failed to suspend PCI link, err = %d\n", ret);
  1135. ret = cnss_resume_pci_link(pci_priv);
  1136. if (ret) {
  1137. cnss_pr_err("Failed to resume PCI link, err = %d\n", ret);
  1138. del_timer(&pci_priv->dev_rddm_timer);
  1139. return ret;
  1140. }
  1141. mod_timer(&pci_priv->dev_rddm_timer,
  1142. jiffies + msecs_to_jiffies(DEV_RDDM_TIMEOUT));
  1143. cnss_mhi_debug_reg_dump(pci_priv);
  1144. cnss_pci_soc_scratch_reg_dump(pci_priv);
  1145. return 0;
  1146. }
  1147. static void cnss_pci_update_link_event(struct cnss_pci_data *pci_priv,
  1148. enum cnss_bus_event_type type,
  1149. void *data)
  1150. {
  1151. struct cnss_bus_event bus_event;
  1152. bus_event.etype = type;
  1153. bus_event.event_data = data;
  1154. cnss_pci_call_driver_uevent(pci_priv, CNSS_BUS_EVENT, &bus_event);
  1155. }
  1156. void cnss_pci_handle_linkdown(struct cnss_pci_data *pci_priv)
  1157. {
  1158. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  1159. struct pci_dev *pci_dev = pci_priv->pci_dev;
  1160. unsigned long flags;
  1161. if (test_bit(ENABLE_PCI_LINK_DOWN_PANIC,
  1162. &plat_priv->ctrl_params.quirks))
  1163. panic("cnss: PCI link is down\n");
  1164. spin_lock_irqsave(&pci_link_down_lock, flags);
  1165. if (pci_priv->pci_link_down_ind) {
  1166. cnss_pr_dbg("PCI link down recovery is in progress, ignore\n");
  1167. spin_unlock_irqrestore(&pci_link_down_lock, flags);
  1168. return;
  1169. }
  1170. pci_priv->pci_link_down_ind = true;
  1171. spin_unlock_irqrestore(&pci_link_down_lock, flags);
  1172. /* Notify MHI about link down*/
  1173. mhi_report_error(pci_priv->mhi_ctrl);
  1174. if (pci_dev->device == QCA6174_DEVICE_ID)
  1175. disable_irq(pci_dev->irq);
  1176. /* Notify bus related event. Now for all supported chips.
  1177. * Here PCIe LINK_DOWN notification taken care.
  1178. * uevent buffer can be extended later, to cover more bus info.
  1179. */
  1180. cnss_pci_update_link_event(pci_priv, BUS_EVENT_PCI_LINK_DOWN, NULL);
  1181. cnss_fatal_err("PCI link down, schedule recovery\n");
  1182. cnss_schedule_recovery(&pci_dev->dev, CNSS_REASON_LINK_DOWN);
  1183. }
  1184. int cnss_pci_link_down(struct device *dev)
  1185. {
  1186. struct pci_dev *pci_dev = to_pci_dev(dev);
  1187. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  1188. struct cnss_plat_data *plat_priv = NULL;
  1189. int ret;
  1190. if (!pci_priv) {
  1191. cnss_pr_err("pci_priv is NULL\n");
  1192. return -EINVAL;
  1193. }
  1194. plat_priv = pci_priv->plat_priv;
  1195. if (!plat_priv) {
  1196. cnss_pr_err("plat_priv is NULL\n");
  1197. return -ENODEV;
  1198. }
  1199. if (pci_priv->pci_link_down_ind) {
  1200. cnss_pr_dbg("PCI link down recovery is already in progress\n");
  1201. return -EBUSY;
  1202. }
  1203. if (pci_priv->drv_connected_last &&
  1204. of_property_read_bool(plat_priv->plat_dev->dev.of_node,
  1205. "cnss-enable-self-recovery"))
  1206. plat_priv->ctrl_params.quirks |= BIT(LINK_DOWN_SELF_RECOVERY);
  1207. cnss_pr_err("PCI link down is detected by drivers\n");
  1208. ret = cnss_pci_assert_perst(pci_priv);
  1209. if (ret)
  1210. cnss_pci_handle_linkdown(pci_priv);
  1211. return ret;
  1212. }
  1213. EXPORT_SYMBOL(cnss_pci_link_down);
  1214. int cnss_pcie_is_device_down(struct cnss_pci_data *pci_priv)
  1215. {
  1216. struct cnss_plat_data *plat_priv;
  1217. if (!pci_priv) {
  1218. cnss_pr_err("pci_priv is NULL\n");
  1219. return -ENODEV;
  1220. }
  1221. plat_priv = pci_priv->plat_priv;
  1222. if (!plat_priv) {
  1223. cnss_pr_err("plat_priv is NULL\n");
  1224. return -ENODEV;
  1225. }
  1226. return test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state) |
  1227. pci_priv->pci_link_down_ind;
  1228. }
  1229. int cnss_pci_is_device_down(struct device *dev)
  1230. {
  1231. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(to_pci_dev(dev));
  1232. return cnss_pcie_is_device_down(pci_priv);
  1233. }
  1234. EXPORT_SYMBOL(cnss_pci_is_device_down);
  1235. void cnss_pci_lock_reg_window(struct device *dev, unsigned long *flags)
  1236. {
  1237. spin_lock_bh(&pci_reg_window_lock);
  1238. }
  1239. EXPORT_SYMBOL(cnss_pci_lock_reg_window);
  1240. void cnss_pci_unlock_reg_window(struct device *dev, unsigned long *flags)
  1241. {
  1242. spin_unlock_bh(&pci_reg_window_lock);
  1243. }
  1244. EXPORT_SYMBOL(cnss_pci_unlock_reg_window);
  1245. int cnss_get_pci_slot(struct device *dev)
  1246. {
  1247. struct pci_dev *pci_dev = to_pci_dev(dev);
  1248. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  1249. struct cnss_plat_data *plat_priv = NULL;
  1250. if (!pci_priv) {
  1251. cnss_pr_err("pci_priv is NULL\n");
  1252. return -EINVAL;
  1253. }
  1254. plat_priv = pci_priv->plat_priv;
  1255. if (!plat_priv) {
  1256. cnss_pr_err("plat_priv is NULL\n");
  1257. return -ENODEV;
  1258. }
  1259. return plat_priv->rc_num;
  1260. }
  1261. EXPORT_SYMBOL(cnss_get_pci_slot);
  1262. /**
  1263. * cnss_pci_dump_bl_sram_mem - Dump WLAN device bootloader debug log
  1264. * @pci_priv: driver PCI bus context pointer
  1265. *
  1266. * Dump primary and secondary bootloader debug log data. For SBL check the
  1267. * log struct address and size for validity.
  1268. *
  1269. * Return: None
  1270. */
  1271. static void cnss_pci_dump_bl_sram_mem(struct cnss_pci_data *pci_priv)
  1272. {
  1273. u32 mem_addr, val, pbl_log_max_size, sbl_log_max_size;
  1274. u32 pbl_log_sram_start;
  1275. u32 pbl_stage, sbl_log_start, sbl_log_size;
  1276. u32 pbl_wlan_boot_cfg, pbl_bootstrap_status;
  1277. u32 pbl_bootstrap_status_reg = PBL_BOOTSTRAP_STATUS;
  1278. u32 sbl_log_def_start = SRAM_START;
  1279. u32 sbl_log_def_end = SRAM_END;
  1280. int i;
  1281. switch (pci_priv->device_id) {
  1282. case QCA6390_DEVICE_ID:
  1283. pbl_log_sram_start = QCA6390_DEBUG_PBL_LOG_SRAM_START;
  1284. pbl_log_max_size = QCA6390_DEBUG_PBL_LOG_SRAM_MAX_SIZE;
  1285. sbl_log_max_size = QCA6390_DEBUG_SBL_LOG_SRAM_MAX_SIZE;
  1286. break;
  1287. case QCA6490_DEVICE_ID:
  1288. pbl_log_sram_start = QCA6490_DEBUG_PBL_LOG_SRAM_START;
  1289. pbl_log_max_size = QCA6490_DEBUG_PBL_LOG_SRAM_MAX_SIZE;
  1290. sbl_log_max_size = QCA6490_DEBUG_SBL_LOG_SRAM_MAX_SIZE;
  1291. break;
  1292. case KIWI_DEVICE_ID:
  1293. pbl_bootstrap_status_reg = KIWI_PBL_BOOTSTRAP_STATUS;
  1294. pbl_log_sram_start = KIWI_DEBUG_PBL_LOG_SRAM_START;
  1295. pbl_log_max_size = KIWI_DEBUG_PBL_LOG_SRAM_MAX_SIZE;
  1296. sbl_log_max_size = KIWI_DEBUG_SBL_LOG_SRAM_MAX_SIZE;
  1297. break;
  1298. case MANGO_DEVICE_ID:
  1299. pbl_bootstrap_status_reg = MANGO_PBL_BOOTSTRAP_STATUS;
  1300. pbl_log_sram_start = MANGO_DEBUG_PBL_LOG_SRAM_START;
  1301. pbl_log_max_size = MANGO_DEBUG_PBL_LOG_SRAM_MAX_SIZE;
  1302. sbl_log_max_size = MANGO_DEBUG_SBL_LOG_SRAM_MAX_SIZE;
  1303. break;
  1304. default:
  1305. return;
  1306. }
  1307. if (cnss_pci_check_link_status(pci_priv))
  1308. return;
  1309. cnss_pci_reg_read(pci_priv, TCSR_PBL_LOGGING_REG, &pbl_stage);
  1310. cnss_pci_reg_read(pci_priv, PCIE_BHI_ERRDBG2_REG, &sbl_log_start);
  1311. cnss_pci_reg_read(pci_priv, PCIE_BHI_ERRDBG3_REG, &sbl_log_size);
  1312. cnss_pci_reg_read(pci_priv, PBL_WLAN_BOOT_CFG, &pbl_wlan_boot_cfg);
  1313. cnss_pci_reg_read(pci_priv, pbl_bootstrap_status_reg,
  1314. &pbl_bootstrap_status);
  1315. cnss_pr_dbg("TCSR_PBL_LOGGING: 0x%08x PCIE_BHI_ERRDBG: Start: 0x%08x Size:0x%08x\n",
  1316. pbl_stage, sbl_log_start, sbl_log_size);
  1317. cnss_pr_dbg("PBL_WLAN_BOOT_CFG: 0x%08x PBL_BOOTSTRAP_STATUS: 0x%08x\n",
  1318. pbl_wlan_boot_cfg, pbl_bootstrap_status);
  1319. cnss_pr_dbg("Dumping PBL log data\n");
  1320. for (i = 0; i < pbl_log_max_size; i += sizeof(val)) {
  1321. mem_addr = pbl_log_sram_start + i;
  1322. if (cnss_pci_reg_read(pci_priv, mem_addr, &val))
  1323. break;
  1324. cnss_pr_dbg("SRAM[0x%x] = 0x%x\n", mem_addr, val);
  1325. }
  1326. sbl_log_size = (sbl_log_size > sbl_log_max_size ?
  1327. sbl_log_max_size : sbl_log_size);
  1328. if (sbl_log_start < sbl_log_def_start ||
  1329. sbl_log_start > sbl_log_def_end ||
  1330. (sbl_log_start + sbl_log_size) > sbl_log_def_end) {
  1331. cnss_pr_err("Invalid SBL log data\n");
  1332. return;
  1333. }
  1334. cnss_pr_dbg("Dumping SBL log data\n");
  1335. for (i = 0; i < sbl_log_size; i += sizeof(val)) {
  1336. mem_addr = sbl_log_start + i;
  1337. if (cnss_pci_reg_read(pci_priv, mem_addr, &val))
  1338. break;
  1339. cnss_pr_dbg("SRAM[0x%x] = 0x%x\n", mem_addr, val);
  1340. }
  1341. }
  1342. static void cnss_pci_dump_sram(struct cnss_pci_data *pci_priv)
  1343. {
  1344. struct cnss_plat_data *plat_priv;
  1345. u32 i, mem_addr;
  1346. u32 *dump_ptr;
  1347. plat_priv = pci_priv->plat_priv;
  1348. if (plat_priv->device_id != QCA6490_DEVICE_ID ||
  1349. cnss_get_host_build_type() != QMI_HOST_BUILD_TYPE_PRIMARY_V01)
  1350. return;
  1351. if (!plat_priv->sram_dump) {
  1352. cnss_pr_err("SRAM dump memory is not allocated\n");
  1353. return;
  1354. }
  1355. if (cnss_pci_check_link_status(pci_priv))
  1356. return;
  1357. cnss_pr_dbg("Dumping SRAM at 0x%lx\n", plat_priv->sram_dump);
  1358. for (i = 0; i < SRAM_DUMP_SIZE; i += sizeof(u32)) {
  1359. mem_addr = SRAM_START + i;
  1360. dump_ptr = (u32 *)(plat_priv->sram_dump + i);
  1361. if (cnss_pci_reg_read(pci_priv, mem_addr, dump_ptr)) {
  1362. cnss_pr_err("SRAM Dump failed at 0x%x\n", mem_addr);
  1363. break;
  1364. }
  1365. /* Relinquish CPU after dumping 256KB chunks*/
  1366. if (!(i % CNSS_256KB_SIZE))
  1367. cond_resched();
  1368. }
  1369. }
  1370. static int cnss_pci_handle_mhi_poweron_timeout(struct cnss_pci_data *pci_priv)
  1371. {
  1372. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  1373. cnss_fatal_err("MHI power up returns timeout\n");
  1374. if (cnss_mhi_scan_rddm_cookie(pci_priv, DEVICE_RDDM_COOKIE) ||
  1375. cnss_get_dev_sol_value(plat_priv) > 0) {
  1376. /* Wait for RDDM if RDDM cookie is set or device SOL GPIO is
  1377. * high. If RDDM times out, PBL/SBL error region may have been
  1378. * erased so no need to dump them either.
  1379. */
  1380. if (!test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state) &&
  1381. !pci_priv->pci_link_down_ind) {
  1382. mod_timer(&pci_priv->dev_rddm_timer,
  1383. jiffies + msecs_to_jiffies(DEV_RDDM_TIMEOUT));
  1384. }
  1385. } else {
  1386. cnss_pr_dbg("RDDM cookie is not set and device SOL is low\n");
  1387. cnss_mhi_debug_reg_dump(pci_priv);
  1388. cnss_pci_soc_scratch_reg_dump(pci_priv);
  1389. /* Dump PBL/SBL error log if RDDM cookie is not set */
  1390. cnss_pci_dump_bl_sram_mem(pci_priv);
  1391. cnss_pci_dump_sram(pci_priv);
  1392. return -ETIMEDOUT;
  1393. }
  1394. return 0;
  1395. }
  1396. static char *cnss_mhi_state_to_str(enum cnss_mhi_state mhi_state)
  1397. {
  1398. switch (mhi_state) {
  1399. case CNSS_MHI_INIT:
  1400. return "INIT";
  1401. case CNSS_MHI_DEINIT:
  1402. return "DEINIT";
  1403. case CNSS_MHI_POWER_ON:
  1404. return "POWER_ON";
  1405. case CNSS_MHI_POWERING_OFF:
  1406. return "POWERING_OFF";
  1407. case CNSS_MHI_POWER_OFF:
  1408. return "POWER_OFF";
  1409. case CNSS_MHI_FORCE_POWER_OFF:
  1410. return "FORCE_POWER_OFF";
  1411. case CNSS_MHI_SUSPEND:
  1412. return "SUSPEND";
  1413. case CNSS_MHI_RESUME:
  1414. return "RESUME";
  1415. case CNSS_MHI_TRIGGER_RDDM:
  1416. return "TRIGGER_RDDM";
  1417. case CNSS_MHI_RDDM_DONE:
  1418. return "RDDM_DONE";
  1419. default:
  1420. return "UNKNOWN";
  1421. }
  1422. };
  1423. static int cnss_pci_check_mhi_state_bit(struct cnss_pci_data *pci_priv,
  1424. enum cnss_mhi_state mhi_state)
  1425. {
  1426. switch (mhi_state) {
  1427. case CNSS_MHI_INIT:
  1428. if (!test_bit(CNSS_MHI_INIT, &pci_priv->mhi_state))
  1429. return 0;
  1430. break;
  1431. case CNSS_MHI_DEINIT:
  1432. case CNSS_MHI_POWER_ON:
  1433. if (test_bit(CNSS_MHI_INIT, &pci_priv->mhi_state) &&
  1434. !test_bit(CNSS_MHI_POWER_ON, &pci_priv->mhi_state))
  1435. return 0;
  1436. break;
  1437. case CNSS_MHI_FORCE_POWER_OFF:
  1438. if (test_bit(CNSS_MHI_POWER_ON, &pci_priv->mhi_state))
  1439. return 0;
  1440. break;
  1441. case CNSS_MHI_POWER_OFF:
  1442. case CNSS_MHI_SUSPEND:
  1443. if (test_bit(CNSS_MHI_POWER_ON, &pci_priv->mhi_state) &&
  1444. !test_bit(CNSS_MHI_SUSPEND, &pci_priv->mhi_state))
  1445. return 0;
  1446. break;
  1447. case CNSS_MHI_RESUME:
  1448. if (test_bit(CNSS_MHI_SUSPEND, &pci_priv->mhi_state))
  1449. return 0;
  1450. break;
  1451. case CNSS_MHI_TRIGGER_RDDM:
  1452. if (test_bit(CNSS_MHI_POWER_ON, &pci_priv->mhi_state) &&
  1453. !test_bit(CNSS_MHI_TRIGGER_RDDM, &pci_priv->mhi_state))
  1454. return 0;
  1455. break;
  1456. case CNSS_MHI_RDDM_DONE:
  1457. return 0;
  1458. default:
  1459. cnss_pr_err("Unhandled MHI state: %s(%d)\n",
  1460. cnss_mhi_state_to_str(mhi_state), mhi_state);
  1461. }
  1462. cnss_pr_err("Cannot set MHI state %s(%d) in current MHI state (0x%lx)\n",
  1463. cnss_mhi_state_to_str(mhi_state), mhi_state,
  1464. pci_priv->mhi_state);
  1465. if (mhi_state != CNSS_MHI_TRIGGER_RDDM)
  1466. CNSS_ASSERT(0);
  1467. return -EINVAL;
  1468. }
  1469. static void cnss_pci_set_mhi_state_bit(struct cnss_pci_data *pci_priv,
  1470. enum cnss_mhi_state mhi_state)
  1471. {
  1472. switch (mhi_state) {
  1473. case CNSS_MHI_INIT:
  1474. set_bit(CNSS_MHI_INIT, &pci_priv->mhi_state);
  1475. break;
  1476. case CNSS_MHI_DEINIT:
  1477. clear_bit(CNSS_MHI_INIT, &pci_priv->mhi_state);
  1478. break;
  1479. case CNSS_MHI_POWER_ON:
  1480. set_bit(CNSS_MHI_POWER_ON, &pci_priv->mhi_state);
  1481. break;
  1482. case CNSS_MHI_POWERING_OFF:
  1483. set_bit(CNSS_MHI_POWERING_OFF, &pci_priv->mhi_state);
  1484. break;
  1485. case CNSS_MHI_POWER_OFF:
  1486. case CNSS_MHI_FORCE_POWER_OFF:
  1487. clear_bit(CNSS_MHI_POWER_ON, &pci_priv->mhi_state);
  1488. clear_bit(CNSS_MHI_POWERING_OFF, &pci_priv->mhi_state);
  1489. clear_bit(CNSS_MHI_TRIGGER_RDDM, &pci_priv->mhi_state);
  1490. clear_bit(CNSS_MHI_RDDM_DONE, &pci_priv->mhi_state);
  1491. break;
  1492. case CNSS_MHI_SUSPEND:
  1493. set_bit(CNSS_MHI_SUSPEND, &pci_priv->mhi_state);
  1494. break;
  1495. case CNSS_MHI_RESUME:
  1496. clear_bit(CNSS_MHI_SUSPEND, &pci_priv->mhi_state);
  1497. break;
  1498. case CNSS_MHI_TRIGGER_RDDM:
  1499. set_bit(CNSS_MHI_TRIGGER_RDDM, &pci_priv->mhi_state);
  1500. break;
  1501. case CNSS_MHI_RDDM_DONE:
  1502. set_bit(CNSS_MHI_RDDM_DONE, &pci_priv->mhi_state);
  1503. break;
  1504. default:
  1505. cnss_pr_err("Unhandled MHI state (%d)\n", mhi_state);
  1506. }
  1507. }
  1508. static int cnss_pci_set_mhi_state(struct cnss_pci_data *pci_priv,
  1509. enum cnss_mhi_state mhi_state)
  1510. {
  1511. int ret = 0, retry = 0;
  1512. if (pci_priv->device_id == QCA6174_DEVICE_ID)
  1513. return 0;
  1514. if (mhi_state < 0) {
  1515. cnss_pr_err("Invalid MHI state (%d)\n", mhi_state);
  1516. return -EINVAL;
  1517. }
  1518. ret = cnss_pci_check_mhi_state_bit(pci_priv, mhi_state);
  1519. if (ret)
  1520. goto out;
  1521. cnss_pr_vdbg("Setting MHI state: %s(%d)\n",
  1522. cnss_mhi_state_to_str(mhi_state), mhi_state);
  1523. switch (mhi_state) {
  1524. case CNSS_MHI_INIT:
  1525. ret = mhi_prepare_for_power_up(pci_priv->mhi_ctrl);
  1526. break;
  1527. case CNSS_MHI_DEINIT:
  1528. mhi_unprepare_after_power_down(pci_priv->mhi_ctrl);
  1529. ret = 0;
  1530. break;
  1531. case CNSS_MHI_POWER_ON:
  1532. ret = mhi_sync_power_up(pci_priv->mhi_ctrl);
  1533. #if IS_ENABLED(CONFIG_MHI_BUS_MISC)
  1534. /* Only set img_pre_alloc when power up succeeds */
  1535. if (!ret && !pci_priv->mhi_ctrl->img_pre_alloc) {
  1536. cnss_pr_dbg("Notify MHI to use already allocated images\n");
  1537. pci_priv->mhi_ctrl->img_pre_alloc = true;
  1538. }
  1539. #endif
  1540. break;
  1541. case CNSS_MHI_POWER_OFF:
  1542. mhi_power_down(pci_priv->mhi_ctrl, true);
  1543. ret = 0;
  1544. break;
  1545. case CNSS_MHI_FORCE_POWER_OFF:
  1546. mhi_power_down(pci_priv->mhi_ctrl, false);
  1547. ret = 0;
  1548. break;
  1549. case CNSS_MHI_SUSPEND:
  1550. retry_mhi_suspend:
  1551. mutex_lock(&pci_priv->mhi_ctrl->pm_mutex);
  1552. if (pci_priv->drv_connected_last)
  1553. ret = cnss_mhi_pm_fast_suspend(pci_priv, true);
  1554. else
  1555. ret = mhi_pm_suspend(pci_priv->mhi_ctrl);
  1556. mutex_unlock(&pci_priv->mhi_ctrl->pm_mutex);
  1557. if (ret == -EBUSY && retry++ < MHI_SUSPEND_RETRY_MAX_TIMES) {
  1558. cnss_pr_dbg("Retry MHI suspend #%d\n", retry);
  1559. usleep_range(MHI_SUSPEND_RETRY_DELAY_US,
  1560. MHI_SUSPEND_RETRY_DELAY_US + 1000);
  1561. goto retry_mhi_suspend;
  1562. }
  1563. break;
  1564. case CNSS_MHI_RESUME:
  1565. mutex_lock(&pci_priv->mhi_ctrl->pm_mutex);
  1566. if (pci_priv->drv_connected_last) {
  1567. ret = cnss_pci_prevent_l1(&pci_priv->pci_dev->dev);
  1568. if (ret) {
  1569. mutex_unlock(&pci_priv->mhi_ctrl->pm_mutex);
  1570. break;
  1571. }
  1572. ret = cnss_mhi_pm_fast_resume(pci_priv, true);
  1573. cnss_pci_allow_l1(&pci_priv->pci_dev->dev);
  1574. } else {
  1575. ret = mhi_pm_resume(pci_priv->mhi_ctrl);
  1576. }
  1577. mutex_unlock(&pci_priv->mhi_ctrl->pm_mutex);
  1578. break;
  1579. case CNSS_MHI_TRIGGER_RDDM:
  1580. ret = mhi_force_rddm_mode(pci_priv->mhi_ctrl);
  1581. if (ret) {
  1582. cnss_pr_err("Failed to trigger RDDM, err = %d\n", ret);
  1583. cnss_pr_dbg("Sending host reset req\n");
  1584. ret = cnss_mhi_force_reset(pci_priv);
  1585. }
  1586. break;
  1587. case CNSS_MHI_RDDM_DONE:
  1588. break;
  1589. default:
  1590. cnss_pr_err("Unhandled MHI state (%d)\n", mhi_state);
  1591. ret = -EINVAL;
  1592. }
  1593. if (ret)
  1594. goto out;
  1595. cnss_pci_set_mhi_state_bit(pci_priv, mhi_state);
  1596. return 0;
  1597. out:
  1598. cnss_pr_err("Failed to set MHI state: %s(%d), err = %d\n",
  1599. cnss_mhi_state_to_str(mhi_state), mhi_state, ret);
  1600. return ret;
  1601. }
  1602. int cnss_pci_start_mhi(struct cnss_pci_data *pci_priv)
  1603. {
  1604. int ret = 0;
  1605. struct cnss_plat_data *plat_priv;
  1606. unsigned int timeout = 0;
  1607. if (!pci_priv) {
  1608. cnss_pr_err("pci_priv is NULL\n");
  1609. return -ENODEV;
  1610. }
  1611. plat_priv = pci_priv->plat_priv;
  1612. if (test_bit(FBC_BYPASS, &plat_priv->ctrl_params.quirks))
  1613. return 0;
  1614. if (MHI_TIMEOUT_OVERWRITE_MS)
  1615. pci_priv->mhi_ctrl->timeout_ms = MHI_TIMEOUT_OVERWRITE_MS;
  1616. cnss_mhi_set_m2_timeout_ms(pci_priv, MHI_M2_TIMEOUT_MS);
  1617. ret = cnss_pci_set_mhi_state(pci_priv, CNSS_MHI_INIT);
  1618. if (ret)
  1619. return ret;
  1620. timeout = pci_priv->mhi_ctrl->timeout_ms;
  1621. /* For non-perf builds the timeout is 10 (default) * 6 seconds */
  1622. if (cnss_get_host_build_type() == QMI_HOST_BUILD_TYPE_PRIMARY_V01)
  1623. pci_priv->mhi_ctrl->timeout_ms *= 6;
  1624. else /* For perf builds the timeout is 10 (default) * 3 seconds */
  1625. pci_priv->mhi_ctrl->timeout_ms *= 3;
  1626. /* Start the timer to dump MHI/PBL/SBL debug data periodically */
  1627. mod_timer(&pci_priv->boot_debug_timer,
  1628. jiffies + msecs_to_jiffies(BOOT_DEBUG_TIMEOUT_MS));
  1629. ret = cnss_pci_set_mhi_state(pci_priv, CNSS_MHI_POWER_ON);
  1630. del_timer_sync(&pci_priv->boot_debug_timer);
  1631. if (ret == 0)
  1632. cnss_wlan_adsp_pc_enable(pci_priv, false);
  1633. pci_priv->mhi_ctrl->timeout_ms = timeout;
  1634. if (ret == -ETIMEDOUT) {
  1635. /* This is a special case needs to be handled that if MHI
  1636. * power on returns -ETIMEDOUT, controller needs to take care
  1637. * the cleanup by calling MHI power down. Force to set the bit
  1638. * for driver internal MHI state to make sure it can be handled
  1639. * properly later.
  1640. */
  1641. set_bit(CNSS_MHI_POWER_ON, &pci_priv->mhi_state);
  1642. ret = cnss_pci_handle_mhi_poweron_timeout(pci_priv);
  1643. }
  1644. return ret;
  1645. }
  1646. static void cnss_pci_power_off_mhi(struct cnss_pci_data *pci_priv)
  1647. {
  1648. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  1649. if (test_bit(FBC_BYPASS, &plat_priv->ctrl_params.quirks))
  1650. return;
  1651. if (!test_bit(CNSS_MHI_POWER_ON, &pci_priv->mhi_state)) {
  1652. cnss_pr_dbg("MHI is already powered off\n");
  1653. return;
  1654. }
  1655. cnss_wlan_adsp_pc_enable(pci_priv, true);
  1656. cnss_pci_set_mhi_state_bit(pci_priv, CNSS_MHI_RESUME);
  1657. cnss_pci_set_mhi_state_bit(pci_priv, CNSS_MHI_POWERING_OFF);
  1658. if (!pci_priv->pci_link_down_ind)
  1659. cnss_pci_set_mhi_state(pci_priv, CNSS_MHI_POWER_OFF);
  1660. else
  1661. cnss_pci_set_mhi_state(pci_priv, CNSS_MHI_FORCE_POWER_OFF);
  1662. }
  1663. static void cnss_pci_deinit_mhi(struct cnss_pci_data *pci_priv)
  1664. {
  1665. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  1666. if (test_bit(FBC_BYPASS, &plat_priv->ctrl_params.quirks))
  1667. return;
  1668. if (!test_bit(CNSS_MHI_INIT, &pci_priv->mhi_state)) {
  1669. cnss_pr_dbg("MHI is already deinited\n");
  1670. return;
  1671. }
  1672. cnss_pci_set_mhi_state(pci_priv, CNSS_MHI_DEINIT);
  1673. }
  1674. static void cnss_pci_set_wlaon_pwr_ctrl(struct cnss_pci_data *pci_priv,
  1675. bool set_vddd4blow, bool set_shutdown,
  1676. bool do_force_wake)
  1677. {
  1678. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  1679. int ret;
  1680. u32 val;
  1681. if (!plat_priv->set_wlaon_pwr_ctrl)
  1682. return;
  1683. if (pci_priv->pci_link_state == PCI_LINK_DOWN ||
  1684. pci_priv->pci_link_down_ind)
  1685. return;
  1686. if (do_force_wake)
  1687. if (cnss_pci_force_wake_get(pci_priv))
  1688. return;
  1689. ret = cnss_pci_reg_read(pci_priv, WLAON_QFPROM_PWR_CTRL_REG, &val);
  1690. if (ret) {
  1691. cnss_pr_err("Failed to read register offset 0x%x, err = %d\n",
  1692. WLAON_QFPROM_PWR_CTRL_REG, ret);
  1693. goto force_wake_put;
  1694. }
  1695. cnss_pr_dbg("Read register offset 0x%x, val = 0x%x\n",
  1696. WLAON_QFPROM_PWR_CTRL_REG, val);
  1697. if (set_vddd4blow)
  1698. val |= QFPROM_PWR_CTRL_VDD4BLOW_SW_EN_MASK;
  1699. else
  1700. val &= ~QFPROM_PWR_CTRL_VDD4BLOW_SW_EN_MASK;
  1701. if (set_shutdown)
  1702. val |= QFPROM_PWR_CTRL_SHUTDOWN_EN_MASK;
  1703. else
  1704. val &= ~QFPROM_PWR_CTRL_SHUTDOWN_EN_MASK;
  1705. ret = cnss_pci_reg_write(pci_priv, WLAON_QFPROM_PWR_CTRL_REG, val);
  1706. if (ret) {
  1707. cnss_pr_err("Failed to write register offset 0x%x, err = %d\n",
  1708. WLAON_QFPROM_PWR_CTRL_REG, ret);
  1709. goto force_wake_put;
  1710. }
  1711. cnss_pr_dbg("Write val 0x%x to register offset 0x%x\n", val,
  1712. WLAON_QFPROM_PWR_CTRL_REG);
  1713. if (set_shutdown)
  1714. usleep_range(WLAON_PWR_CTRL_SHUTDOWN_DELAY_MIN_US,
  1715. WLAON_PWR_CTRL_SHUTDOWN_DELAY_MAX_US);
  1716. force_wake_put:
  1717. if (do_force_wake)
  1718. cnss_pci_force_wake_put(pci_priv);
  1719. }
  1720. static int cnss_pci_get_device_timestamp(struct cnss_pci_data *pci_priv,
  1721. u64 *time_us)
  1722. {
  1723. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  1724. u32 low, high;
  1725. u64 device_ticks;
  1726. if (!plat_priv->device_freq_hz) {
  1727. cnss_pr_err("Device time clock frequency is not valid\n");
  1728. return -EINVAL;
  1729. }
  1730. switch (pci_priv->device_id) {
  1731. case KIWI_DEVICE_ID:
  1732. case MANGO_DEVICE_ID:
  1733. cnss_pci_reg_read(pci_priv, PCIE_MHI_TIME_LOW, &low);
  1734. cnss_pci_reg_read(pci_priv, PCIE_MHI_TIME_HIGH, &high);
  1735. break;
  1736. default:
  1737. cnss_pci_reg_read(pci_priv, WLAON_GLOBAL_COUNTER_CTRL3, &low);
  1738. cnss_pci_reg_read(pci_priv, WLAON_GLOBAL_COUNTER_CTRL4, &high);
  1739. break;
  1740. }
  1741. device_ticks = (u64)high << 32 | low;
  1742. do_div(device_ticks, plat_priv->device_freq_hz / 100000);
  1743. *time_us = device_ticks * 10;
  1744. return 0;
  1745. }
  1746. static void cnss_pci_enable_time_sync_counter(struct cnss_pci_data *pci_priv)
  1747. {
  1748. switch (pci_priv->device_id) {
  1749. case KIWI_DEVICE_ID:
  1750. case MANGO_DEVICE_ID:
  1751. return;
  1752. default:
  1753. break;
  1754. }
  1755. cnss_pci_reg_write(pci_priv, WLAON_GLOBAL_COUNTER_CTRL5,
  1756. TIME_SYNC_ENABLE);
  1757. }
  1758. static void cnss_pci_clear_time_sync_counter(struct cnss_pci_data *pci_priv)
  1759. {
  1760. switch (pci_priv->device_id) {
  1761. case KIWI_DEVICE_ID:
  1762. case MANGO_DEVICE_ID:
  1763. return;
  1764. default:
  1765. break;
  1766. }
  1767. cnss_pci_reg_write(pci_priv, WLAON_GLOBAL_COUNTER_CTRL5,
  1768. TIME_SYNC_CLEAR);
  1769. }
  1770. static void cnss_pci_time_sync_reg_update(struct cnss_pci_data *pci_priv,
  1771. u32 low, u32 high)
  1772. {
  1773. u32 time_reg_low;
  1774. u32 time_reg_high;
  1775. switch (pci_priv->device_id) {
  1776. case KIWI_DEVICE_ID:
  1777. case MANGO_DEVICE_ID:
  1778. /* Use the next two shadow registers after host's usage */
  1779. time_reg_low = PCIE_SHADOW_REG_VALUE_0 +
  1780. (pci_priv->plat_priv->num_shadow_regs_v3 *
  1781. SHADOW_REG_LEN_BYTES);
  1782. time_reg_high = time_reg_low + SHADOW_REG_LEN_BYTES;
  1783. break;
  1784. default:
  1785. time_reg_low = PCIE_SHADOW_REG_VALUE_34;
  1786. time_reg_high = PCIE_SHADOW_REG_VALUE_35;
  1787. break;
  1788. }
  1789. cnss_pci_reg_write(pci_priv, time_reg_low, low);
  1790. cnss_pci_reg_write(pci_priv, time_reg_high, high);
  1791. cnss_pci_reg_read(pci_priv, time_reg_low, &low);
  1792. cnss_pci_reg_read(pci_priv, time_reg_high, &high);
  1793. cnss_pr_dbg("Updated time sync regs [0x%x] = 0x%x, [0x%x] = 0x%x\n",
  1794. time_reg_low, low, time_reg_high, high);
  1795. }
  1796. static int cnss_pci_update_timestamp(struct cnss_pci_data *pci_priv)
  1797. {
  1798. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  1799. struct device *dev = &pci_priv->pci_dev->dev;
  1800. unsigned long flags = 0;
  1801. u64 host_time_us, device_time_us, offset;
  1802. u32 low, high;
  1803. int ret;
  1804. ret = cnss_pci_prevent_l1(dev);
  1805. if (ret)
  1806. goto out;
  1807. ret = cnss_pci_force_wake_get(pci_priv);
  1808. if (ret)
  1809. goto allow_l1;
  1810. spin_lock_irqsave(&time_sync_lock, flags);
  1811. cnss_pci_clear_time_sync_counter(pci_priv);
  1812. cnss_pci_enable_time_sync_counter(pci_priv);
  1813. host_time_us = cnss_get_host_timestamp(plat_priv);
  1814. ret = cnss_pci_get_device_timestamp(pci_priv, &device_time_us);
  1815. cnss_pci_clear_time_sync_counter(pci_priv);
  1816. spin_unlock_irqrestore(&time_sync_lock, flags);
  1817. if (ret)
  1818. goto force_wake_put;
  1819. if (host_time_us < device_time_us) {
  1820. cnss_pr_err("Host time (%llu us) is smaller than device time (%llu us), stop\n",
  1821. host_time_us, device_time_us);
  1822. ret = -EINVAL;
  1823. goto force_wake_put;
  1824. }
  1825. offset = host_time_us - device_time_us;
  1826. cnss_pr_dbg("Host time = %llu us, device time = %llu us, offset = %llu us\n",
  1827. host_time_us, device_time_us, offset);
  1828. low = offset & 0xFFFFFFFF;
  1829. high = offset >> 32;
  1830. cnss_pci_time_sync_reg_update(pci_priv, low, high);
  1831. force_wake_put:
  1832. cnss_pci_force_wake_put(pci_priv);
  1833. allow_l1:
  1834. cnss_pci_allow_l1(dev);
  1835. out:
  1836. return ret;
  1837. }
  1838. static void cnss_pci_time_sync_work_hdlr(struct work_struct *work)
  1839. {
  1840. struct cnss_pci_data *pci_priv =
  1841. container_of(work, struct cnss_pci_data, time_sync_work.work);
  1842. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  1843. unsigned int time_sync_period_ms =
  1844. plat_priv->ctrl_params.time_sync_period;
  1845. if (test_bit(DISABLE_TIME_SYNC, &plat_priv->ctrl_params.quirks)) {
  1846. cnss_pr_dbg("Time sync is disabled\n");
  1847. return;
  1848. }
  1849. if (!time_sync_period_ms) {
  1850. cnss_pr_dbg("Skip time sync as time period is 0\n");
  1851. return;
  1852. }
  1853. if (cnss_pci_is_device_down(&pci_priv->pci_dev->dev))
  1854. return;
  1855. if (cnss_pci_pm_runtime_get_sync(pci_priv, RTPM_ID_CNSS) < 0)
  1856. goto runtime_pm_put;
  1857. mutex_lock(&pci_priv->bus_lock);
  1858. cnss_pci_update_timestamp(pci_priv);
  1859. mutex_unlock(&pci_priv->bus_lock);
  1860. schedule_delayed_work(&pci_priv->time_sync_work,
  1861. msecs_to_jiffies(time_sync_period_ms));
  1862. runtime_pm_put:
  1863. cnss_pci_pm_runtime_mark_last_busy(pci_priv);
  1864. cnss_pci_pm_runtime_put_autosuspend(pci_priv, RTPM_ID_CNSS);
  1865. }
  1866. static int cnss_pci_start_time_sync_update(struct cnss_pci_data *pci_priv)
  1867. {
  1868. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  1869. switch (pci_priv->device_id) {
  1870. case QCA6390_DEVICE_ID:
  1871. case QCA6490_DEVICE_ID:
  1872. case KIWI_DEVICE_ID:
  1873. case MANGO_DEVICE_ID:
  1874. break;
  1875. default:
  1876. return -EOPNOTSUPP;
  1877. }
  1878. if (!plat_priv->device_freq_hz) {
  1879. cnss_pr_dbg("Device time clock frequency is not valid, skip time sync\n");
  1880. return -EINVAL;
  1881. }
  1882. cnss_pci_time_sync_work_hdlr(&pci_priv->time_sync_work.work);
  1883. return 0;
  1884. }
  1885. static void cnss_pci_stop_time_sync_update(struct cnss_pci_data *pci_priv)
  1886. {
  1887. switch (pci_priv->device_id) {
  1888. case QCA6390_DEVICE_ID:
  1889. case QCA6490_DEVICE_ID:
  1890. case KIWI_DEVICE_ID:
  1891. case MANGO_DEVICE_ID:
  1892. break;
  1893. default:
  1894. return;
  1895. }
  1896. cancel_delayed_work_sync(&pci_priv->time_sync_work);
  1897. }
  1898. int cnss_pci_call_driver_probe(struct cnss_pci_data *pci_priv)
  1899. {
  1900. int ret = 0;
  1901. struct cnss_plat_data *plat_priv;
  1902. if (!pci_priv)
  1903. return -ENODEV;
  1904. plat_priv = pci_priv->plat_priv;
  1905. if (test_bit(CNSS_DRIVER_DEBUG, &plat_priv->driver_state)) {
  1906. clear_bit(CNSS_DRIVER_RECOVERY, &plat_priv->driver_state);
  1907. cnss_pr_dbg("Skip driver probe\n");
  1908. goto out;
  1909. }
  1910. if (!pci_priv->driver_ops) {
  1911. cnss_pr_err("driver_ops is NULL\n");
  1912. ret = -EINVAL;
  1913. goto out;
  1914. }
  1915. if (test_bit(CNSS_DRIVER_RECOVERY, &plat_priv->driver_state) &&
  1916. test_bit(CNSS_DRIVER_PROBED, &plat_priv->driver_state)) {
  1917. ret = pci_priv->driver_ops->reinit(pci_priv->pci_dev,
  1918. pci_priv->pci_device_id);
  1919. if (ret) {
  1920. cnss_pr_err("Failed to reinit host driver, err = %d\n",
  1921. ret);
  1922. goto out;
  1923. }
  1924. complete(&plat_priv->recovery_complete);
  1925. } else if (test_bit(CNSS_DRIVER_LOADING, &plat_priv->driver_state)) {
  1926. ret = pci_priv->driver_ops->probe(pci_priv->pci_dev,
  1927. pci_priv->pci_device_id);
  1928. if (ret) {
  1929. cnss_pr_err("Failed to probe host driver, err = %d\n",
  1930. ret);
  1931. goto out;
  1932. }
  1933. clear_bit(CNSS_DRIVER_LOADING, &plat_priv->driver_state);
  1934. set_bit(CNSS_DRIVER_PROBED, &plat_priv->driver_state);
  1935. complete_all(&plat_priv->power_up_complete);
  1936. } else if (test_bit(CNSS_DRIVER_IDLE_RESTART,
  1937. &plat_priv->driver_state)) {
  1938. ret = pci_priv->driver_ops->idle_restart(pci_priv->pci_dev,
  1939. pci_priv->pci_device_id);
  1940. if (ret) {
  1941. cnss_pr_err("Failed to idle restart host driver, err = %d\n",
  1942. ret);
  1943. plat_priv->power_up_error = ret;
  1944. complete_all(&plat_priv->power_up_complete);
  1945. goto out;
  1946. }
  1947. clear_bit(CNSS_DRIVER_IDLE_RESTART, &plat_priv->driver_state);
  1948. complete_all(&plat_priv->power_up_complete);
  1949. } else {
  1950. complete(&plat_priv->power_up_complete);
  1951. }
  1952. if (test_bit(CNSS_DRIVER_RECOVERY, &plat_priv->driver_state)) {
  1953. clear_bit(CNSS_DRIVER_RECOVERY, &plat_priv->driver_state);
  1954. __pm_relax(plat_priv->recovery_ws);
  1955. }
  1956. cnss_pci_start_time_sync_update(pci_priv);
  1957. return 0;
  1958. out:
  1959. return ret;
  1960. }
  1961. int cnss_pci_call_driver_remove(struct cnss_pci_data *pci_priv)
  1962. {
  1963. struct cnss_plat_data *plat_priv;
  1964. int ret;
  1965. if (!pci_priv)
  1966. return -ENODEV;
  1967. plat_priv = pci_priv->plat_priv;
  1968. if (test_bit(CNSS_IN_COLD_BOOT_CAL, &plat_priv->driver_state) ||
  1969. test_bit(CNSS_FW_BOOT_RECOVERY, &plat_priv->driver_state) ||
  1970. test_bit(CNSS_DRIVER_DEBUG, &plat_priv->driver_state)) {
  1971. cnss_pr_dbg("Skip driver remove\n");
  1972. return 0;
  1973. }
  1974. if (!pci_priv->driver_ops) {
  1975. cnss_pr_err("driver_ops is NULL\n");
  1976. return -EINVAL;
  1977. }
  1978. cnss_pci_stop_time_sync_update(pci_priv);
  1979. if (test_bit(CNSS_DRIVER_RECOVERY, &plat_priv->driver_state) &&
  1980. test_bit(CNSS_DRIVER_PROBED, &plat_priv->driver_state)) {
  1981. pci_priv->driver_ops->shutdown(pci_priv->pci_dev);
  1982. } else if (test_bit(CNSS_DRIVER_UNLOADING, &plat_priv->driver_state)) {
  1983. pci_priv->driver_ops->remove(pci_priv->pci_dev);
  1984. clear_bit(CNSS_DRIVER_PROBED, &plat_priv->driver_state);
  1985. } else if (test_bit(CNSS_DRIVER_IDLE_SHUTDOWN,
  1986. &plat_priv->driver_state)) {
  1987. ret = pci_priv->driver_ops->idle_shutdown(pci_priv->pci_dev);
  1988. if (ret == -EAGAIN) {
  1989. clear_bit(CNSS_DRIVER_IDLE_SHUTDOWN,
  1990. &plat_priv->driver_state);
  1991. return ret;
  1992. }
  1993. }
  1994. plat_priv->get_info_cb_ctx = NULL;
  1995. plat_priv->get_info_cb = NULL;
  1996. return 0;
  1997. }
  1998. int cnss_pci_call_driver_modem_status(struct cnss_pci_data *pci_priv,
  1999. int modem_current_status)
  2000. {
  2001. struct cnss_wlan_driver *driver_ops;
  2002. if (!pci_priv)
  2003. return -ENODEV;
  2004. driver_ops = pci_priv->driver_ops;
  2005. if (!driver_ops || !driver_ops->modem_status)
  2006. return -EINVAL;
  2007. driver_ops->modem_status(pci_priv->pci_dev, modem_current_status);
  2008. return 0;
  2009. }
  2010. int cnss_pci_update_status(struct cnss_pci_data *pci_priv,
  2011. enum cnss_driver_status status)
  2012. {
  2013. struct cnss_wlan_driver *driver_ops;
  2014. if (!pci_priv)
  2015. return -ENODEV;
  2016. driver_ops = pci_priv->driver_ops;
  2017. if (!driver_ops || !driver_ops->update_status)
  2018. return -EINVAL;
  2019. cnss_pr_dbg("Update driver status: %d\n", status);
  2020. driver_ops->update_status(pci_priv->pci_dev, status);
  2021. return 0;
  2022. }
  2023. static void cnss_pci_misc_reg_dump(struct cnss_pci_data *pci_priv,
  2024. struct cnss_misc_reg *misc_reg,
  2025. u32 misc_reg_size,
  2026. char *reg_name)
  2027. {
  2028. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2029. bool do_force_wake_put = true;
  2030. int i;
  2031. if (!misc_reg)
  2032. return;
  2033. if (in_interrupt() || irqs_disabled())
  2034. return;
  2035. if (cnss_pci_check_link_status(pci_priv))
  2036. return;
  2037. if (cnss_pci_force_wake_get(pci_priv)) {
  2038. /* Continue to dump when device has entered RDDM already */
  2039. if (!test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state))
  2040. return;
  2041. do_force_wake_put = false;
  2042. }
  2043. cnss_pr_dbg("Start to dump %s registers\n", reg_name);
  2044. for (i = 0; i < misc_reg_size; i++) {
  2045. if (!test_bit(pci_priv->misc_reg_dev_mask,
  2046. &misc_reg[i].dev_mask))
  2047. continue;
  2048. if (misc_reg[i].wr) {
  2049. if (misc_reg[i].offset ==
  2050. QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_SAW2_CFG &&
  2051. i >= 1)
  2052. misc_reg[i].val =
  2053. QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_SAW2_CFG_MSK |
  2054. misc_reg[i - 1].val;
  2055. if (cnss_pci_reg_write(pci_priv,
  2056. misc_reg[i].offset,
  2057. misc_reg[i].val))
  2058. goto force_wake_put;
  2059. cnss_pr_vdbg("Write 0x%X to 0x%X\n",
  2060. misc_reg[i].val,
  2061. misc_reg[i].offset);
  2062. } else {
  2063. if (cnss_pci_reg_read(pci_priv,
  2064. misc_reg[i].offset,
  2065. &misc_reg[i].val))
  2066. goto force_wake_put;
  2067. }
  2068. }
  2069. force_wake_put:
  2070. if (do_force_wake_put)
  2071. cnss_pci_force_wake_put(pci_priv);
  2072. }
  2073. static void cnss_pci_dump_misc_reg(struct cnss_pci_data *pci_priv)
  2074. {
  2075. if (in_interrupt() || irqs_disabled())
  2076. return;
  2077. if (cnss_pci_check_link_status(pci_priv))
  2078. return;
  2079. cnss_pci_misc_reg_dump(pci_priv, pci_priv->wcss_reg,
  2080. WCSS_REG_SIZE, "wcss");
  2081. cnss_pci_misc_reg_dump(pci_priv, pci_priv->pcie_reg,
  2082. PCIE_REG_SIZE, "pcie");
  2083. cnss_pci_misc_reg_dump(pci_priv, pci_priv->wlaon_reg,
  2084. WLAON_REG_SIZE, "wlaon");
  2085. cnss_pci_misc_reg_dump(pci_priv, pci_priv->syspm_reg,
  2086. SYSPM_REG_SIZE, "syspm");
  2087. }
  2088. static void cnss_pci_dump_shadow_reg(struct cnss_pci_data *pci_priv)
  2089. {
  2090. int i, j = 0, array_size = SHADOW_REG_COUNT + SHADOW_REG_INTER_COUNT;
  2091. u32 reg_offset;
  2092. bool do_force_wake_put = true;
  2093. if (in_interrupt() || irqs_disabled())
  2094. return;
  2095. if (cnss_pci_check_link_status(pci_priv))
  2096. return;
  2097. if (!pci_priv->debug_reg) {
  2098. pci_priv->debug_reg = devm_kzalloc(&pci_priv->pci_dev->dev,
  2099. sizeof(*pci_priv->debug_reg)
  2100. * array_size, GFP_KERNEL);
  2101. if (!pci_priv->debug_reg)
  2102. return;
  2103. }
  2104. if (cnss_pci_force_wake_get(pci_priv))
  2105. do_force_wake_put = false;
  2106. cnss_pr_dbg("Start to dump shadow registers\n");
  2107. for (i = 0; i < SHADOW_REG_COUNT; i++, j++) {
  2108. reg_offset = PCIE_SHADOW_REG_VALUE_0 + i * 4;
  2109. pci_priv->debug_reg[j].offset = reg_offset;
  2110. if (cnss_pci_reg_read(pci_priv, reg_offset,
  2111. &pci_priv->debug_reg[j].val))
  2112. goto force_wake_put;
  2113. }
  2114. for (i = 0; i < SHADOW_REG_INTER_COUNT; i++, j++) {
  2115. reg_offset = PCIE_SHADOW_REG_INTER_0 + i * 4;
  2116. pci_priv->debug_reg[j].offset = reg_offset;
  2117. if (cnss_pci_reg_read(pci_priv, reg_offset,
  2118. &pci_priv->debug_reg[j].val))
  2119. goto force_wake_put;
  2120. }
  2121. force_wake_put:
  2122. if (do_force_wake_put)
  2123. cnss_pci_force_wake_put(pci_priv);
  2124. }
  2125. static int cnss_qca6174_powerup(struct cnss_pci_data *pci_priv)
  2126. {
  2127. int ret = 0;
  2128. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2129. ret = cnss_power_on_device(plat_priv);
  2130. if (ret) {
  2131. cnss_pr_err("Failed to power on device, err = %d\n", ret);
  2132. goto out;
  2133. }
  2134. ret = cnss_resume_pci_link(pci_priv);
  2135. if (ret) {
  2136. cnss_pr_err("Failed to resume PCI link, err = %d\n", ret);
  2137. goto power_off;
  2138. }
  2139. ret = cnss_pci_call_driver_probe(pci_priv);
  2140. if (ret)
  2141. goto suspend_link;
  2142. return 0;
  2143. suspend_link:
  2144. cnss_suspend_pci_link(pci_priv);
  2145. power_off:
  2146. cnss_power_off_device(plat_priv);
  2147. out:
  2148. return ret;
  2149. }
  2150. static int cnss_qca6174_shutdown(struct cnss_pci_data *pci_priv)
  2151. {
  2152. int ret = 0;
  2153. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2154. cnss_pci_pm_runtime_resume(pci_priv);
  2155. ret = cnss_pci_call_driver_remove(pci_priv);
  2156. if (ret == -EAGAIN)
  2157. goto out;
  2158. cnss_request_bus_bandwidth(&plat_priv->plat_dev->dev,
  2159. CNSS_BUS_WIDTH_NONE);
  2160. cnss_pci_set_monitor_wake_intr(pci_priv, false);
  2161. cnss_pci_set_auto_suspended(pci_priv, 0);
  2162. ret = cnss_suspend_pci_link(pci_priv);
  2163. if (ret)
  2164. cnss_pr_err("Failed to suspend PCI link, err = %d\n", ret);
  2165. cnss_power_off_device(plat_priv);
  2166. clear_bit(CNSS_DRIVER_UNLOADING, &plat_priv->driver_state);
  2167. clear_bit(CNSS_DRIVER_IDLE_SHUTDOWN, &plat_priv->driver_state);
  2168. out:
  2169. return ret;
  2170. }
  2171. static void cnss_qca6174_crash_shutdown(struct cnss_pci_data *pci_priv)
  2172. {
  2173. if (pci_priv->driver_ops && pci_priv->driver_ops->crash_shutdown)
  2174. pci_priv->driver_ops->crash_shutdown(pci_priv->pci_dev);
  2175. }
  2176. static int cnss_qca6174_ramdump(struct cnss_pci_data *pci_priv)
  2177. {
  2178. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2179. struct cnss_ramdump_info *ramdump_info;
  2180. ramdump_info = &plat_priv->ramdump_info;
  2181. if (!ramdump_info->ramdump_size)
  2182. return -EINVAL;
  2183. return cnss_do_ramdump(plat_priv);
  2184. }
  2185. static void cnss_get_driver_mode_update_fw_name(struct cnss_plat_data *plat_priv)
  2186. {
  2187. struct cnss_pci_data *pci_priv;
  2188. struct cnss_wlan_driver *driver_ops;
  2189. pci_priv = plat_priv->bus_priv;
  2190. driver_ops = pci_priv->driver_ops;
  2191. if (driver_ops && driver_ops->get_driver_mode) {
  2192. plat_priv->driver_mode = driver_ops->get_driver_mode();
  2193. cnss_pci_update_fw_name(pci_priv);
  2194. cnss_pr_dbg("New driver mode is %d", plat_priv->driver_mode);
  2195. }
  2196. }
  2197. static int cnss_qca6290_powerup(struct cnss_pci_data *pci_priv)
  2198. {
  2199. int ret = 0;
  2200. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2201. unsigned int timeout;
  2202. int retry = 0, bt_en_gpio = plat_priv->pinctrl_info.bt_en_gpio;
  2203. int sw_ctrl_gpio = plat_priv->pinctrl_info.sw_ctrl_gpio;
  2204. if (plat_priv->ramdump_info_v2.dump_data_valid) {
  2205. cnss_pci_clear_dump_info(pci_priv);
  2206. cnss_pci_power_off_mhi(pci_priv);
  2207. cnss_suspend_pci_link(pci_priv);
  2208. cnss_pci_deinit_mhi(pci_priv);
  2209. cnss_power_off_device(plat_priv);
  2210. }
  2211. /* Clear QMI send usage count during every power up */
  2212. pci_priv->qmi_send_usage_count = 0;
  2213. plat_priv->power_up_error = 0;
  2214. cnss_get_driver_mode_update_fw_name(plat_priv);
  2215. retry:
  2216. ret = cnss_power_on_device(plat_priv);
  2217. if (ret) {
  2218. cnss_pr_err("Failed to power on device, err = %d\n", ret);
  2219. goto out;
  2220. }
  2221. ret = cnss_resume_pci_link(pci_priv);
  2222. if (ret) {
  2223. cnss_pr_err("Failed to resume PCI link, err = %d\n", ret);
  2224. cnss_pr_dbg("Value of SW_CTRL GPIO: %d\n",
  2225. cnss_get_input_gpio_value(plat_priv, sw_ctrl_gpio));
  2226. if (test_bit(IGNORE_PCI_LINK_FAILURE,
  2227. &plat_priv->ctrl_params.quirks)) {
  2228. cnss_pr_dbg("Ignore PCI link resume failure\n");
  2229. ret = 0;
  2230. goto out;
  2231. }
  2232. if (ret == -EAGAIN && retry++ < POWER_ON_RETRY_MAX_TIMES) {
  2233. cnss_power_off_device(plat_priv);
  2234. /* Force toggle BT_EN GPIO low */
  2235. if (retry == POWER_ON_RETRY_MAX_TIMES) {
  2236. cnss_pr_dbg("Retry #%d. Set BT_EN GPIO(%u) low\n",
  2237. retry, bt_en_gpio);
  2238. if (bt_en_gpio >= 0)
  2239. gpio_direction_output(bt_en_gpio, 0);
  2240. cnss_pr_dbg("BT_EN GPIO val: %d\n",
  2241. gpio_get_value(bt_en_gpio));
  2242. }
  2243. cnss_pr_dbg("Retry to resume PCI link #%d\n", retry);
  2244. cnss_pr_dbg("Value of SW_CTRL GPIO: %d\n",
  2245. cnss_get_input_gpio_value(plat_priv,
  2246. sw_ctrl_gpio));
  2247. msleep(POWER_ON_RETRY_DELAY_MS * retry);
  2248. goto retry;
  2249. }
  2250. /* Assert when it reaches maximum retries */
  2251. CNSS_ASSERT(0);
  2252. goto power_off;
  2253. }
  2254. cnss_pci_set_wlaon_pwr_ctrl(pci_priv, false, false, false);
  2255. timeout = cnss_get_timeout(plat_priv, CNSS_TIMEOUT_QMI);
  2256. ret = cnss_pci_start_mhi(pci_priv);
  2257. if (ret) {
  2258. cnss_fatal_err("Failed to start MHI, err = %d\n", ret);
  2259. if (!test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state) &&
  2260. !pci_priv->pci_link_down_ind && timeout) {
  2261. /* Start recovery directly for MHI start failures */
  2262. cnss_schedule_recovery(&pci_priv->pci_dev->dev,
  2263. CNSS_REASON_DEFAULT);
  2264. }
  2265. return 0;
  2266. }
  2267. if (test_bit(USE_CORE_ONLY_FW, &plat_priv->ctrl_params.quirks)) {
  2268. clear_bit(CNSS_FW_BOOT_RECOVERY, &plat_priv->driver_state);
  2269. clear_bit(CNSS_DRIVER_RECOVERY, &plat_priv->driver_state);
  2270. return 0;
  2271. }
  2272. cnss_set_pin_connect_status(plat_priv);
  2273. if (test_bit(QMI_BYPASS, &plat_priv->ctrl_params.quirks)) {
  2274. ret = cnss_pci_call_driver_probe(pci_priv);
  2275. if (ret)
  2276. goto stop_mhi;
  2277. } else if (timeout) {
  2278. if (test_bit(CNSS_IN_COLD_BOOT_CAL, &plat_priv->driver_state))
  2279. timeout += WLAN_COLD_BOOT_CAL_TIMEOUT;
  2280. else
  2281. timeout += WLAN_MISSION_MODE_TIMEOUT;
  2282. mod_timer(&plat_priv->fw_boot_timer,
  2283. jiffies + msecs_to_jiffies(timeout));
  2284. }
  2285. return 0;
  2286. stop_mhi:
  2287. cnss_pci_set_wlaon_pwr_ctrl(pci_priv, false, true, true);
  2288. cnss_pci_power_off_mhi(pci_priv);
  2289. cnss_suspend_pci_link(pci_priv);
  2290. cnss_pci_deinit_mhi(pci_priv);
  2291. power_off:
  2292. cnss_power_off_device(plat_priv);
  2293. out:
  2294. return ret;
  2295. }
  2296. static int cnss_qca6290_shutdown(struct cnss_pci_data *pci_priv)
  2297. {
  2298. int ret = 0;
  2299. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2300. int do_force_wake = true;
  2301. cnss_pci_pm_runtime_resume(pci_priv);
  2302. ret = cnss_pci_call_driver_remove(pci_priv);
  2303. if (ret == -EAGAIN)
  2304. goto out;
  2305. cnss_request_bus_bandwidth(&plat_priv->plat_dev->dev,
  2306. CNSS_BUS_WIDTH_NONE);
  2307. cnss_pci_set_monitor_wake_intr(pci_priv, false);
  2308. cnss_pci_set_auto_suspended(pci_priv, 0);
  2309. if ((test_bit(CNSS_DRIVER_LOADING, &plat_priv->driver_state) ||
  2310. test_bit(CNSS_DRIVER_UNLOADING, &plat_priv->driver_state) ||
  2311. test_bit(CNSS_DRIVER_IDLE_RESTART, &plat_priv->driver_state) ||
  2312. test_bit(CNSS_DRIVER_IDLE_SHUTDOWN, &plat_priv->driver_state) ||
  2313. test_bit(CNSS_IN_COLD_BOOT_CAL, &plat_priv->driver_state)) &&
  2314. test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state)) {
  2315. del_timer(&pci_priv->dev_rddm_timer);
  2316. cnss_pci_collect_dump_info(pci_priv, false);
  2317. CNSS_ASSERT(0);
  2318. }
  2319. if (!cnss_is_device_powered_on(plat_priv)) {
  2320. cnss_pr_dbg("Device is already powered off, ignore\n");
  2321. goto skip_power_off;
  2322. }
  2323. if (test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state))
  2324. do_force_wake = false;
  2325. cnss_pci_set_wlaon_pwr_ctrl(pci_priv, false, true, do_force_wake);
  2326. /* FBC image will be freed after powering off MHI, so skip
  2327. * if RAM dump data is still valid.
  2328. */
  2329. if (plat_priv->ramdump_info_v2.dump_data_valid)
  2330. goto skip_power_off;
  2331. cnss_pci_power_off_mhi(pci_priv);
  2332. ret = cnss_suspend_pci_link(pci_priv);
  2333. if (ret)
  2334. cnss_pr_err("Failed to suspend PCI link, err = %d\n", ret);
  2335. cnss_pci_deinit_mhi(pci_priv);
  2336. cnss_power_off_device(plat_priv);
  2337. skip_power_off:
  2338. pci_priv->remap_window = 0;
  2339. clear_bit(CNSS_FW_READY, &plat_priv->driver_state);
  2340. clear_bit(CNSS_FW_MEM_READY, &plat_priv->driver_state);
  2341. if (test_bit(CNSS_DRIVER_UNLOADING, &plat_priv->driver_state) ||
  2342. test_bit(CNSS_DRIVER_IDLE_SHUTDOWN, &plat_priv->driver_state)) {
  2343. clear_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state);
  2344. pci_priv->pci_link_down_ind = false;
  2345. }
  2346. clear_bit(CNSS_DRIVER_UNLOADING, &plat_priv->driver_state);
  2347. clear_bit(CNSS_DRIVER_IDLE_SHUTDOWN, &plat_priv->driver_state);
  2348. memset(&print_optimize, 0, sizeof(print_optimize));
  2349. out:
  2350. return ret;
  2351. }
  2352. static void cnss_qca6290_crash_shutdown(struct cnss_pci_data *pci_priv)
  2353. {
  2354. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2355. set_bit(CNSS_IN_PANIC, &plat_priv->driver_state);
  2356. cnss_pr_dbg("Crash shutdown with driver_state 0x%lx\n",
  2357. plat_priv->driver_state);
  2358. cnss_pci_collect_dump_info(pci_priv, true);
  2359. clear_bit(CNSS_IN_PANIC, &plat_priv->driver_state);
  2360. }
  2361. static int cnss_qca6290_ramdump(struct cnss_pci_data *pci_priv)
  2362. {
  2363. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2364. struct cnss_ramdump_info_v2 *info_v2 = &plat_priv->ramdump_info_v2;
  2365. struct cnss_dump_data *dump_data = &info_v2->dump_data;
  2366. struct cnss_dump_seg *dump_seg = info_v2->dump_data_vaddr;
  2367. int ret = 0;
  2368. if (!info_v2->dump_data_valid || !dump_seg ||
  2369. dump_data->nentries == 0)
  2370. return 0;
  2371. ret = cnss_do_elf_ramdump(plat_priv);
  2372. cnss_pci_clear_dump_info(pci_priv);
  2373. cnss_pci_power_off_mhi(pci_priv);
  2374. cnss_suspend_pci_link(pci_priv);
  2375. cnss_pci_deinit_mhi(pci_priv);
  2376. cnss_power_off_device(plat_priv);
  2377. return ret;
  2378. }
  2379. int cnss_pci_dev_powerup(struct cnss_pci_data *pci_priv)
  2380. {
  2381. int ret = 0;
  2382. if (!pci_priv) {
  2383. cnss_pr_err("pci_priv is NULL\n");
  2384. return -ENODEV;
  2385. }
  2386. switch (pci_priv->device_id) {
  2387. case QCA6174_DEVICE_ID:
  2388. ret = cnss_qca6174_powerup(pci_priv);
  2389. break;
  2390. case QCA6290_DEVICE_ID:
  2391. case QCA6390_DEVICE_ID:
  2392. case QCA6490_DEVICE_ID:
  2393. case KIWI_DEVICE_ID:
  2394. case MANGO_DEVICE_ID:
  2395. ret = cnss_qca6290_powerup(pci_priv);
  2396. break;
  2397. default:
  2398. cnss_pr_err("Unknown device_id found: 0x%x\n",
  2399. pci_priv->device_id);
  2400. ret = -ENODEV;
  2401. }
  2402. return ret;
  2403. }
  2404. int cnss_pci_dev_shutdown(struct cnss_pci_data *pci_priv)
  2405. {
  2406. int ret = 0;
  2407. if (!pci_priv) {
  2408. cnss_pr_err("pci_priv is NULL\n");
  2409. return -ENODEV;
  2410. }
  2411. switch (pci_priv->device_id) {
  2412. case QCA6174_DEVICE_ID:
  2413. ret = cnss_qca6174_shutdown(pci_priv);
  2414. break;
  2415. case QCA6290_DEVICE_ID:
  2416. case QCA6390_DEVICE_ID:
  2417. case QCA6490_DEVICE_ID:
  2418. case KIWI_DEVICE_ID:
  2419. case MANGO_DEVICE_ID:
  2420. ret = cnss_qca6290_shutdown(pci_priv);
  2421. break;
  2422. default:
  2423. cnss_pr_err("Unknown device_id found: 0x%x\n",
  2424. pci_priv->device_id);
  2425. ret = -ENODEV;
  2426. }
  2427. return ret;
  2428. }
  2429. int cnss_pci_dev_crash_shutdown(struct cnss_pci_data *pci_priv)
  2430. {
  2431. int ret = 0;
  2432. if (!pci_priv) {
  2433. cnss_pr_err("pci_priv is NULL\n");
  2434. return -ENODEV;
  2435. }
  2436. switch (pci_priv->device_id) {
  2437. case QCA6174_DEVICE_ID:
  2438. cnss_qca6174_crash_shutdown(pci_priv);
  2439. break;
  2440. case QCA6290_DEVICE_ID:
  2441. case QCA6390_DEVICE_ID:
  2442. case QCA6490_DEVICE_ID:
  2443. case KIWI_DEVICE_ID:
  2444. case MANGO_DEVICE_ID:
  2445. cnss_qca6290_crash_shutdown(pci_priv);
  2446. break;
  2447. default:
  2448. cnss_pr_err("Unknown device_id found: 0x%x\n",
  2449. pci_priv->device_id);
  2450. ret = -ENODEV;
  2451. }
  2452. return ret;
  2453. }
  2454. int cnss_pci_dev_ramdump(struct cnss_pci_data *pci_priv)
  2455. {
  2456. int ret = 0;
  2457. if (!pci_priv) {
  2458. cnss_pr_err("pci_priv is NULL\n");
  2459. return -ENODEV;
  2460. }
  2461. switch (pci_priv->device_id) {
  2462. case QCA6174_DEVICE_ID:
  2463. ret = cnss_qca6174_ramdump(pci_priv);
  2464. break;
  2465. case QCA6290_DEVICE_ID:
  2466. case QCA6390_DEVICE_ID:
  2467. case QCA6490_DEVICE_ID:
  2468. case KIWI_DEVICE_ID:
  2469. case MANGO_DEVICE_ID:
  2470. ret = cnss_qca6290_ramdump(pci_priv);
  2471. break;
  2472. default:
  2473. cnss_pr_err("Unknown device_id found: 0x%x\n",
  2474. pci_priv->device_id);
  2475. ret = -ENODEV;
  2476. }
  2477. return ret;
  2478. }
  2479. int cnss_pci_is_drv_connected(struct device *dev)
  2480. {
  2481. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(to_pci_dev(dev));
  2482. if (!pci_priv)
  2483. return -ENODEV;
  2484. return pci_priv->drv_connected_last;
  2485. }
  2486. EXPORT_SYMBOL(cnss_pci_is_drv_connected);
  2487. static void cnss_wlan_reg_driver_work(struct work_struct *work)
  2488. {
  2489. struct cnss_plat_data *plat_priv =
  2490. container_of(work, struct cnss_plat_data, wlan_reg_driver_work.work);
  2491. struct cnss_pci_data *pci_priv = plat_priv->bus_priv;
  2492. struct cnss_cal_info *cal_info;
  2493. unsigned int timeout;
  2494. if (test_bit(CNSS_COLD_BOOT_CAL_DONE, &plat_priv->driver_state)) {
  2495. goto reg_driver;
  2496. } else {
  2497. if (plat_priv->charger_mode) {
  2498. cnss_pr_err("Ignore calibration timeout in charger mode\n");
  2499. return;
  2500. }
  2501. if (!test_bit(CNSS_IN_COLD_BOOT_CAL,
  2502. &plat_priv->driver_state)) {
  2503. timeout = cnss_get_timeout(plat_priv,
  2504. CNSS_TIMEOUT_CALIBRATION);
  2505. cnss_pr_dbg("File system not ready to start calibration. Wait for %ds..\n",
  2506. timeout / 1000);
  2507. schedule_delayed_work(&plat_priv->wlan_reg_driver_work,
  2508. msecs_to_jiffies(timeout));
  2509. return;
  2510. }
  2511. del_timer(&plat_priv->fw_boot_timer);
  2512. if (test_bit(CNSS_IN_COLD_BOOT_CAL, &plat_priv->driver_state) &&
  2513. !test_bit(CNSS_IN_REBOOT, &plat_priv->driver_state)) {
  2514. cnss_pr_err("Timeout waiting for calibration to complete\n");
  2515. CNSS_ASSERT(0);
  2516. }
  2517. cal_info = kzalloc(sizeof(*cal_info), GFP_KERNEL);
  2518. if (!cal_info)
  2519. return;
  2520. cal_info->cal_status = CNSS_CAL_TIMEOUT;
  2521. cnss_driver_event_post(plat_priv,
  2522. CNSS_DRIVER_EVENT_COLD_BOOT_CAL_DONE,
  2523. 0, cal_info);
  2524. }
  2525. reg_driver:
  2526. if (test_bit(CNSS_IN_REBOOT, &plat_priv->driver_state)) {
  2527. cnss_pr_dbg("Reboot/Shutdown is in progress, ignore register driver\n");
  2528. return;
  2529. }
  2530. reinit_completion(&plat_priv->power_up_complete);
  2531. cnss_driver_event_post(plat_priv,
  2532. CNSS_DRIVER_EVENT_REGISTER_DRIVER,
  2533. CNSS_EVENT_SYNC_UNKILLABLE,
  2534. pci_priv->driver_ops);
  2535. }
  2536. int cnss_wlan_register_driver(struct cnss_wlan_driver *driver_ops)
  2537. {
  2538. int ret = 0;
  2539. struct cnss_plat_data *plat_priv = cnss_bus_dev_to_plat_priv(NULL);
  2540. struct cnss_pci_data *pci_priv;
  2541. const struct pci_device_id *id_table = driver_ops->id_table;
  2542. unsigned int timeout;
  2543. if (!cnss_check_driver_loading_allowed()) {
  2544. cnss_pr_info("No cnss2 dtsi entry present");
  2545. return -ENODEV;
  2546. }
  2547. if (!plat_priv) {
  2548. cnss_pr_buf("plat_priv is not ready for register driver\n");
  2549. return -EAGAIN;
  2550. }
  2551. if (test_bit(CNSS_WLAN_HW_DISABLED, &plat_priv->driver_state)) {
  2552. while (id_table && id_table->device) {
  2553. if (plat_priv->device_id == id_table->device) {
  2554. if (plat_priv->device_id == KIWI_DEVICE_ID &&
  2555. driver_ops->chip_version != 2) {
  2556. cnss_pr_err("WLAN HW disabled. kiwi_v2 only supported\n");
  2557. return -ENODEV;
  2558. }
  2559. cnss_pr_info("WLAN register driver deferred for device ID: 0x%x due to HW disable\n",
  2560. id_table->device);
  2561. plat_priv->driver_ops = driver_ops;
  2562. return 0;
  2563. }
  2564. id_table++;
  2565. }
  2566. return -ENODEV;
  2567. }
  2568. if (!test_bit(CNSS_PCI_PROBE_DONE, &plat_priv->driver_state)) {
  2569. cnss_pr_info("pci probe not yet done for register driver\n");
  2570. return -EAGAIN;
  2571. }
  2572. pci_priv = plat_priv->bus_priv;
  2573. if (pci_priv->driver_ops) {
  2574. cnss_pr_err("Driver has already registered\n");
  2575. return -EEXIST;
  2576. }
  2577. if (test_bit(CNSS_IN_REBOOT, &plat_priv->driver_state)) {
  2578. cnss_pr_dbg("Reboot/Shutdown is in progress, ignore register driver\n");
  2579. return -EINVAL;
  2580. }
  2581. if (!id_table || !pci_dev_present(id_table)) {
  2582. /* id_table pointer will move from pci_dev_present(),
  2583. * so check again using local pointer.
  2584. */
  2585. id_table = driver_ops->id_table;
  2586. while (id_table && id_table->vendor) {
  2587. cnss_pr_info("Host driver is built for PCIe device ID 0x%x\n",
  2588. id_table->device);
  2589. id_table++;
  2590. }
  2591. cnss_pr_err("Enumerated PCIe device id is 0x%x, reject unsupported driver\n",
  2592. pci_priv->device_id);
  2593. return -ENODEV;
  2594. }
  2595. if (driver_ops->chip_version != CNSS_CHIP_VER_ANY &&
  2596. driver_ops->chip_version != plat_priv->device_version.major_version) {
  2597. cnss_pr_err("Driver built for chip ver 0x%x, enumerated ver 0x%x, reject unsupported driver\n",
  2598. driver_ops->chip_version,
  2599. plat_priv->device_version.major_version);
  2600. return -ENODEV;
  2601. }
  2602. cnss_get_driver_mode_update_fw_name(plat_priv);
  2603. set_bit(CNSS_DRIVER_REGISTER, &plat_priv->driver_state);
  2604. if (!plat_priv->cbc_enabled ||
  2605. test_bit(CNSS_COLD_BOOT_CAL_DONE, &plat_priv->driver_state))
  2606. goto register_driver;
  2607. pci_priv->driver_ops = driver_ops;
  2608. /* If Cold Boot Calibration is enabled, it is the 1st step in init
  2609. * sequence.CBC is done on file system_ready trigger. Qcacld will be
  2610. * loaded from vendor_modprobe.sh at early boot and must be deferred
  2611. * until CBC is complete
  2612. */
  2613. timeout = cnss_get_timeout(plat_priv, CNSS_TIMEOUT_CALIBRATION);
  2614. INIT_DELAYED_WORK(&plat_priv->wlan_reg_driver_work,
  2615. cnss_wlan_reg_driver_work);
  2616. schedule_delayed_work(&plat_priv->wlan_reg_driver_work,
  2617. msecs_to_jiffies(timeout));
  2618. cnss_pr_info("WLAN register driver deferred for Calibration\n");
  2619. return 0;
  2620. register_driver:
  2621. reinit_completion(&plat_priv->power_up_complete);
  2622. ret = cnss_driver_event_post(plat_priv,
  2623. CNSS_DRIVER_EVENT_REGISTER_DRIVER,
  2624. CNSS_EVENT_SYNC_UNKILLABLE,
  2625. driver_ops);
  2626. return ret;
  2627. }
  2628. EXPORT_SYMBOL(cnss_wlan_register_driver);
  2629. void cnss_wlan_unregister_driver(struct cnss_wlan_driver *driver_ops)
  2630. {
  2631. struct cnss_plat_data *plat_priv = cnss_bus_dev_to_plat_priv(NULL);
  2632. int ret = 0;
  2633. unsigned int timeout;
  2634. if (!plat_priv) {
  2635. cnss_pr_err("plat_priv is NULL\n");
  2636. return;
  2637. }
  2638. mutex_lock(&plat_priv->driver_ops_lock);
  2639. if (plat_priv->device_id == QCA6174_DEVICE_ID)
  2640. goto skip_wait_power_up;
  2641. timeout = cnss_get_timeout(plat_priv, CNSS_TIMEOUT_WLAN_WATCHDOG);
  2642. ret = wait_for_completion_timeout(&plat_priv->power_up_complete,
  2643. msecs_to_jiffies(timeout));
  2644. if (!ret) {
  2645. cnss_pr_err("Timeout (%ums) waiting for driver power up to complete\n",
  2646. timeout);
  2647. CNSS_ASSERT(0);
  2648. }
  2649. skip_wait_power_up:
  2650. if (!test_bit(CNSS_DRIVER_RECOVERY, &plat_priv->driver_state) &&
  2651. !test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state))
  2652. goto skip_wait_recovery;
  2653. reinit_completion(&plat_priv->recovery_complete);
  2654. timeout = cnss_get_timeout(plat_priv, CNSS_TIMEOUT_RECOVERY);
  2655. ret = wait_for_completion_timeout(&plat_priv->recovery_complete,
  2656. msecs_to_jiffies(timeout));
  2657. if (!ret) {
  2658. cnss_pr_err("Timeout (%ums) waiting for recovery to complete\n",
  2659. timeout);
  2660. CNSS_ASSERT(0);
  2661. }
  2662. skip_wait_recovery:
  2663. cnss_driver_event_post(plat_priv,
  2664. CNSS_DRIVER_EVENT_UNREGISTER_DRIVER,
  2665. CNSS_EVENT_SYNC_UNKILLABLE, NULL);
  2666. mutex_unlock(&plat_priv->driver_ops_lock);
  2667. }
  2668. EXPORT_SYMBOL(cnss_wlan_unregister_driver);
  2669. int cnss_pci_register_driver_hdlr(struct cnss_pci_data *pci_priv,
  2670. void *data)
  2671. {
  2672. int ret = 0;
  2673. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2674. if (test_bit(CNSS_IN_REBOOT, &plat_priv->driver_state)) {
  2675. cnss_pr_dbg("Reboot or shutdown is in progress, ignore register driver\n");
  2676. return -EINVAL;
  2677. }
  2678. set_bit(CNSS_DRIVER_LOADING, &plat_priv->driver_state);
  2679. pci_priv->driver_ops = data;
  2680. ret = cnss_pci_dev_powerup(pci_priv);
  2681. if (ret) {
  2682. clear_bit(CNSS_DRIVER_LOADING, &plat_priv->driver_state);
  2683. pci_priv->driver_ops = NULL;
  2684. }
  2685. return ret;
  2686. }
  2687. int cnss_pci_unregister_driver_hdlr(struct cnss_pci_data *pci_priv)
  2688. {
  2689. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2690. set_bit(CNSS_DRIVER_UNLOADING, &plat_priv->driver_state);
  2691. cnss_pci_dev_shutdown(pci_priv);
  2692. pci_priv->driver_ops = NULL;
  2693. return 0;
  2694. }
  2695. static int cnss_pci_suspend_driver(struct cnss_pci_data *pci_priv)
  2696. {
  2697. struct pci_dev *pci_dev = pci_priv->pci_dev;
  2698. struct cnss_wlan_driver *driver_ops = pci_priv->driver_ops;
  2699. int ret = 0;
  2700. pm_message_t state = { .event = PM_EVENT_SUSPEND };
  2701. if (driver_ops && driver_ops->suspend) {
  2702. ret = driver_ops->suspend(pci_dev, state);
  2703. if (ret) {
  2704. cnss_pr_err("Failed to suspend host driver, err = %d\n",
  2705. ret);
  2706. ret = -EAGAIN;
  2707. }
  2708. }
  2709. return ret;
  2710. }
  2711. static int cnss_pci_resume_driver(struct cnss_pci_data *pci_priv)
  2712. {
  2713. struct pci_dev *pci_dev = pci_priv->pci_dev;
  2714. struct cnss_wlan_driver *driver_ops = pci_priv->driver_ops;
  2715. int ret = 0;
  2716. if (driver_ops && driver_ops->resume) {
  2717. ret = driver_ops->resume(pci_dev);
  2718. if (ret)
  2719. cnss_pr_err("Failed to resume host driver, err = %d\n",
  2720. ret);
  2721. }
  2722. return ret;
  2723. }
  2724. int cnss_pci_suspend_bus(struct cnss_pci_data *pci_priv)
  2725. {
  2726. struct pci_dev *pci_dev = pci_priv->pci_dev;
  2727. int ret = 0;
  2728. if (pci_priv->pci_link_state == PCI_LINK_DOWN)
  2729. goto out;
  2730. if (cnss_pci_set_mhi_state(pci_priv, CNSS_MHI_SUSPEND)) {
  2731. ret = -EAGAIN;
  2732. goto out;
  2733. }
  2734. if (pci_priv->drv_connected_last)
  2735. goto skip_disable_pci;
  2736. pci_clear_master(pci_dev);
  2737. cnss_set_pci_config_space(pci_priv, SAVE_PCI_CONFIG_SPACE);
  2738. pci_disable_device(pci_dev);
  2739. ret = pci_set_power_state(pci_dev, PCI_D3hot);
  2740. if (ret)
  2741. cnss_pr_err("Failed to set D3Hot, err = %d\n", ret);
  2742. skip_disable_pci:
  2743. if (cnss_set_pci_link(pci_priv, PCI_LINK_DOWN)) {
  2744. ret = -EAGAIN;
  2745. goto resume_mhi;
  2746. }
  2747. pci_priv->pci_link_state = PCI_LINK_DOWN;
  2748. return 0;
  2749. resume_mhi:
  2750. if (!pci_is_enabled(pci_dev))
  2751. if (pci_enable_device(pci_dev))
  2752. cnss_pr_err("Failed to enable PCI device\n");
  2753. if (pci_priv->saved_state)
  2754. cnss_set_pci_config_space(pci_priv, RESTORE_PCI_CONFIG_SPACE);
  2755. pci_set_master(pci_dev);
  2756. cnss_pci_set_mhi_state(pci_priv, CNSS_MHI_RESUME);
  2757. out:
  2758. return ret;
  2759. }
  2760. int cnss_pci_resume_bus(struct cnss_pci_data *pci_priv)
  2761. {
  2762. struct pci_dev *pci_dev = pci_priv->pci_dev;
  2763. int ret = 0;
  2764. if (pci_priv->pci_link_state == PCI_LINK_UP)
  2765. goto out;
  2766. if (cnss_set_pci_link(pci_priv, PCI_LINK_UP)) {
  2767. cnss_fatal_err("Failed to resume PCI link from suspend\n");
  2768. cnss_pci_link_down(&pci_dev->dev);
  2769. ret = -EAGAIN;
  2770. goto out;
  2771. }
  2772. pci_priv->pci_link_state = PCI_LINK_UP;
  2773. if (pci_priv->drv_connected_last)
  2774. goto skip_enable_pci;
  2775. ret = pci_enable_device(pci_dev);
  2776. if (ret) {
  2777. cnss_pr_err("Failed to enable PCI device, err = %d\n",
  2778. ret);
  2779. goto out;
  2780. }
  2781. if (pci_priv->saved_state)
  2782. cnss_set_pci_config_space(pci_priv,
  2783. RESTORE_PCI_CONFIG_SPACE);
  2784. pci_set_master(pci_dev);
  2785. skip_enable_pci:
  2786. cnss_pci_set_mhi_state(pci_priv, CNSS_MHI_RESUME);
  2787. out:
  2788. return ret;
  2789. }
  2790. static int cnss_pci_suspend(struct device *dev)
  2791. {
  2792. int ret = 0;
  2793. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(to_pci_dev(dev));
  2794. struct cnss_plat_data *plat_priv;
  2795. if (!pci_priv)
  2796. goto out;
  2797. plat_priv = pci_priv->plat_priv;
  2798. if (!plat_priv)
  2799. goto out;
  2800. if (!cnss_is_device_powered_on(plat_priv))
  2801. goto out;
  2802. if (!test_bit(DISABLE_DRV, &plat_priv->ctrl_params.quirks) &&
  2803. pci_priv->drv_supported) {
  2804. pci_priv->drv_connected_last =
  2805. cnss_pci_get_drv_connected(pci_priv);
  2806. if (!pci_priv->drv_connected_last) {
  2807. cnss_pr_dbg("Firmware does not support non-DRV suspend, reject\n");
  2808. ret = -EAGAIN;
  2809. goto out;
  2810. }
  2811. }
  2812. set_bit(CNSS_IN_SUSPEND_RESUME, &plat_priv->driver_state);
  2813. ret = cnss_pci_suspend_driver(pci_priv);
  2814. if (ret)
  2815. goto clear_flag;
  2816. if (!pci_priv->disable_pc) {
  2817. mutex_lock(&pci_priv->bus_lock);
  2818. ret = cnss_pci_suspend_bus(pci_priv);
  2819. mutex_unlock(&pci_priv->bus_lock);
  2820. if (ret)
  2821. goto resume_driver;
  2822. }
  2823. cnss_pci_set_monitor_wake_intr(pci_priv, false);
  2824. return 0;
  2825. resume_driver:
  2826. cnss_pci_resume_driver(pci_priv);
  2827. clear_flag:
  2828. pci_priv->drv_connected_last = 0;
  2829. clear_bit(CNSS_IN_SUSPEND_RESUME, &plat_priv->driver_state);
  2830. out:
  2831. return ret;
  2832. }
  2833. static int cnss_pci_resume(struct device *dev)
  2834. {
  2835. int ret = 0;
  2836. struct pci_dev *pci_dev = to_pci_dev(dev);
  2837. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  2838. struct cnss_plat_data *plat_priv;
  2839. if (!pci_priv)
  2840. goto out;
  2841. plat_priv = pci_priv->plat_priv;
  2842. if (!plat_priv)
  2843. goto out;
  2844. if (pci_priv->pci_link_down_ind)
  2845. goto out;
  2846. if (!cnss_is_device_powered_on(pci_priv->plat_priv))
  2847. goto out;
  2848. if (!pci_priv->disable_pc) {
  2849. ret = cnss_pci_resume_bus(pci_priv);
  2850. if (ret)
  2851. goto out;
  2852. }
  2853. ret = cnss_pci_resume_driver(pci_priv);
  2854. pci_priv->drv_connected_last = 0;
  2855. clear_bit(CNSS_IN_SUSPEND_RESUME, &plat_priv->driver_state);
  2856. out:
  2857. return ret;
  2858. }
  2859. static int cnss_pci_suspend_noirq(struct device *dev)
  2860. {
  2861. int ret = 0;
  2862. struct pci_dev *pci_dev = to_pci_dev(dev);
  2863. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  2864. struct cnss_wlan_driver *driver_ops;
  2865. if (!pci_priv)
  2866. goto out;
  2867. if (!cnss_is_device_powered_on(pci_priv->plat_priv))
  2868. goto out;
  2869. driver_ops = pci_priv->driver_ops;
  2870. if (driver_ops && driver_ops->suspend_noirq)
  2871. ret = driver_ops->suspend_noirq(pci_dev);
  2872. if (pci_priv->disable_pc && !pci_dev->state_saved &&
  2873. !pci_priv->plat_priv->use_pm_domain)
  2874. pci_save_state(pci_dev);
  2875. out:
  2876. return ret;
  2877. }
  2878. static int cnss_pci_resume_noirq(struct device *dev)
  2879. {
  2880. int ret = 0;
  2881. struct pci_dev *pci_dev = to_pci_dev(dev);
  2882. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  2883. struct cnss_wlan_driver *driver_ops;
  2884. if (!pci_priv)
  2885. goto out;
  2886. if (!cnss_is_device_powered_on(pci_priv->plat_priv))
  2887. goto out;
  2888. driver_ops = pci_priv->driver_ops;
  2889. if (driver_ops && driver_ops->resume_noirq &&
  2890. !pci_priv->pci_link_down_ind)
  2891. ret = driver_ops->resume_noirq(pci_dev);
  2892. out:
  2893. return ret;
  2894. }
  2895. static int cnss_pci_runtime_suspend(struct device *dev)
  2896. {
  2897. int ret = 0;
  2898. struct pci_dev *pci_dev = to_pci_dev(dev);
  2899. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  2900. struct cnss_plat_data *plat_priv;
  2901. struct cnss_wlan_driver *driver_ops;
  2902. if (!pci_priv)
  2903. return -EAGAIN;
  2904. plat_priv = pci_priv->plat_priv;
  2905. if (!plat_priv)
  2906. return -EAGAIN;
  2907. if (!cnss_is_device_powered_on(pci_priv->plat_priv))
  2908. return -EAGAIN;
  2909. if (pci_priv->pci_link_down_ind) {
  2910. cnss_pr_dbg("PCI link down recovery is in progress!\n");
  2911. return -EAGAIN;
  2912. }
  2913. if (!test_bit(DISABLE_DRV, &plat_priv->ctrl_params.quirks) &&
  2914. pci_priv->drv_supported) {
  2915. pci_priv->drv_connected_last =
  2916. cnss_pci_get_drv_connected(pci_priv);
  2917. if (!pci_priv->drv_connected_last) {
  2918. cnss_pr_dbg("Firmware does not support non-DRV suspend, reject\n");
  2919. return -EAGAIN;
  2920. }
  2921. }
  2922. cnss_pr_vdbg("Runtime suspend start\n");
  2923. driver_ops = pci_priv->driver_ops;
  2924. if (driver_ops && driver_ops->runtime_ops &&
  2925. driver_ops->runtime_ops->runtime_suspend)
  2926. ret = driver_ops->runtime_ops->runtime_suspend(pci_dev);
  2927. else
  2928. ret = cnss_auto_suspend(dev);
  2929. if (ret)
  2930. pci_priv->drv_connected_last = 0;
  2931. cnss_pr_vdbg("Runtime suspend status: %d\n", ret);
  2932. return ret;
  2933. }
  2934. static int cnss_pci_runtime_resume(struct device *dev)
  2935. {
  2936. int ret = 0;
  2937. struct pci_dev *pci_dev = to_pci_dev(dev);
  2938. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  2939. struct cnss_wlan_driver *driver_ops;
  2940. if (!pci_priv)
  2941. return -EAGAIN;
  2942. if (!cnss_is_device_powered_on(pci_priv->plat_priv))
  2943. return -EAGAIN;
  2944. if (pci_priv->pci_link_down_ind) {
  2945. cnss_pr_dbg("PCI link down recovery is in progress!\n");
  2946. return -EAGAIN;
  2947. }
  2948. cnss_pr_vdbg("Runtime resume start\n");
  2949. driver_ops = pci_priv->driver_ops;
  2950. if (driver_ops && driver_ops->runtime_ops &&
  2951. driver_ops->runtime_ops->runtime_resume)
  2952. ret = driver_ops->runtime_ops->runtime_resume(pci_dev);
  2953. else
  2954. ret = cnss_auto_resume(dev);
  2955. if (!ret)
  2956. pci_priv->drv_connected_last = 0;
  2957. cnss_pr_vdbg("Runtime resume status: %d\n", ret);
  2958. return ret;
  2959. }
  2960. static int cnss_pci_runtime_idle(struct device *dev)
  2961. {
  2962. cnss_pr_vdbg("Runtime idle\n");
  2963. pm_request_autosuspend(dev);
  2964. return -EBUSY;
  2965. }
  2966. int cnss_wlan_pm_control(struct device *dev, bool vote)
  2967. {
  2968. struct pci_dev *pci_dev = to_pci_dev(dev);
  2969. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  2970. int ret = 0;
  2971. if (!pci_priv)
  2972. return -ENODEV;
  2973. ret = cnss_pci_disable_pc(pci_priv, vote);
  2974. if (ret)
  2975. return ret;
  2976. pci_priv->disable_pc = vote;
  2977. cnss_pr_dbg("%s PCIe power collapse\n", vote ? "disable" : "enable");
  2978. return 0;
  2979. }
  2980. EXPORT_SYMBOL(cnss_wlan_pm_control);
  2981. static void cnss_pci_pm_runtime_get_record(struct cnss_pci_data *pci_priv,
  2982. enum cnss_rtpm_id id)
  2983. {
  2984. if (id >= RTPM_ID_MAX)
  2985. return;
  2986. atomic_inc(&pci_priv->pm_stats.runtime_get);
  2987. atomic_inc(&pci_priv->pm_stats.runtime_get_id[id]);
  2988. pci_priv->pm_stats.runtime_get_timestamp_id[id] =
  2989. cnss_get_host_timestamp(pci_priv->plat_priv);
  2990. }
  2991. static void cnss_pci_pm_runtime_put_record(struct cnss_pci_data *pci_priv,
  2992. enum cnss_rtpm_id id)
  2993. {
  2994. if (id >= RTPM_ID_MAX)
  2995. return;
  2996. atomic_inc(&pci_priv->pm_stats.runtime_put);
  2997. atomic_inc(&pci_priv->pm_stats.runtime_put_id[id]);
  2998. pci_priv->pm_stats.runtime_put_timestamp_id[id] =
  2999. cnss_get_host_timestamp(pci_priv->plat_priv);
  3000. }
  3001. void cnss_pci_pm_runtime_show_usage_count(struct cnss_pci_data *pci_priv)
  3002. {
  3003. struct device *dev;
  3004. if (!pci_priv)
  3005. return;
  3006. dev = &pci_priv->pci_dev->dev;
  3007. cnss_pr_dbg("Runtime PM usage count: %d\n",
  3008. atomic_read(&dev->power.usage_count));
  3009. }
  3010. int cnss_pci_pm_request_resume(struct cnss_pci_data *pci_priv)
  3011. {
  3012. struct device *dev;
  3013. enum rpm_status status;
  3014. if (!pci_priv)
  3015. return -ENODEV;
  3016. dev = &pci_priv->pci_dev->dev;
  3017. status = dev->power.runtime_status;
  3018. if (status == RPM_SUSPENDING || status == RPM_SUSPENDED)
  3019. cnss_pr_vdbg("Runtime PM resume is requested by %ps\n",
  3020. (void *)_RET_IP_);
  3021. return pm_request_resume(dev);
  3022. }
  3023. int cnss_pci_pm_runtime_resume(struct cnss_pci_data *pci_priv)
  3024. {
  3025. struct device *dev;
  3026. enum rpm_status status;
  3027. if (!pci_priv)
  3028. return -ENODEV;
  3029. dev = &pci_priv->pci_dev->dev;
  3030. status = dev->power.runtime_status;
  3031. if (status == RPM_SUSPENDING || status == RPM_SUSPENDED)
  3032. cnss_pr_vdbg("Runtime PM resume is requested by %ps\n",
  3033. (void *)_RET_IP_);
  3034. return pm_runtime_resume(dev);
  3035. }
  3036. int cnss_pci_pm_runtime_get(struct cnss_pci_data *pci_priv,
  3037. enum cnss_rtpm_id id)
  3038. {
  3039. struct device *dev;
  3040. enum rpm_status status;
  3041. if (!pci_priv)
  3042. return -ENODEV;
  3043. dev = &pci_priv->pci_dev->dev;
  3044. status = dev->power.runtime_status;
  3045. if (status == RPM_SUSPENDING || status == RPM_SUSPENDED)
  3046. cnss_pr_vdbg("Runtime PM resume is requested by %ps\n",
  3047. (void *)_RET_IP_);
  3048. cnss_pci_pm_runtime_get_record(pci_priv, id);
  3049. return pm_runtime_get(dev);
  3050. }
  3051. int cnss_pci_pm_runtime_get_sync(struct cnss_pci_data *pci_priv,
  3052. enum cnss_rtpm_id id)
  3053. {
  3054. struct device *dev;
  3055. enum rpm_status status;
  3056. if (!pci_priv)
  3057. return -ENODEV;
  3058. dev = &pci_priv->pci_dev->dev;
  3059. status = dev->power.runtime_status;
  3060. if (status == RPM_SUSPENDING || status == RPM_SUSPENDED)
  3061. cnss_pr_vdbg("Runtime PM resume is requested by %ps\n",
  3062. (void *)_RET_IP_);
  3063. cnss_pci_pm_runtime_get_record(pci_priv, id);
  3064. return pm_runtime_get_sync(dev);
  3065. }
  3066. void cnss_pci_pm_runtime_get_noresume(struct cnss_pci_data *pci_priv,
  3067. enum cnss_rtpm_id id)
  3068. {
  3069. if (!pci_priv)
  3070. return;
  3071. cnss_pci_pm_runtime_get_record(pci_priv, id);
  3072. pm_runtime_get_noresume(&pci_priv->pci_dev->dev);
  3073. }
  3074. int cnss_pci_pm_runtime_put_autosuspend(struct cnss_pci_data *pci_priv,
  3075. enum cnss_rtpm_id id)
  3076. {
  3077. struct device *dev;
  3078. if (!pci_priv)
  3079. return -ENODEV;
  3080. dev = &pci_priv->pci_dev->dev;
  3081. if (atomic_read(&dev->power.usage_count) == 0) {
  3082. cnss_pr_dbg("Ignore excessive runtime PM put operation\n");
  3083. return -EINVAL;
  3084. }
  3085. cnss_pci_pm_runtime_put_record(pci_priv, id);
  3086. return pm_runtime_put_autosuspend(&pci_priv->pci_dev->dev);
  3087. }
  3088. void cnss_pci_pm_runtime_put_noidle(struct cnss_pci_data *pci_priv,
  3089. enum cnss_rtpm_id id)
  3090. {
  3091. struct device *dev;
  3092. if (!pci_priv)
  3093. return;
  3094. dev = &pci_priv->pci_dev->dev;
  3095. if (atomic_read(&dev->power.usage_count) == 0) {
  3096. cnss_pr_dbg("Ignore excessive runtime PM put operation\n");
  3097. return;
  3098. }
  3099. cnss_pci_pm_runtime_put_record(pci_priv, id);
  3100. pm_runtime_put_noidle(&pci_priv->pci_dev->dev);
  3101. }
  3102. void cnss_pci_pm_runtime_mark_last_busy(struct cnss_pci_data *pci_priv)
  3103. {
  3104. if (!pci_priv)
  3105. return;
  3106. pm_runtime_mark_last_busy(&pci_priv->pci_dev->dev);
  3107. }
  3108. int cnss_auto_suspend(struct device *dev)
  3109. {
  3110. int ret = 0;
  3111. struct pci_dev *pci_dev = to_pci_dev(dev);
  3112. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  3113. struct cnss_plat_data *plat_priv;
  3114. if (!pci_priv)
  3115. return -ENODEV;
  3116. plat_priv = pci_priv->plat_priv;
  3117. if (!plat_priv)
  3118. return -ENODEV;
  3119. mutex_lock(&pci_priv->bus_lock);
  3120. if (!pci_priv->qmi_send_usage_count) {
  3121. ret = cnss_pci_suspend_bus(pci_priv);
  3122. if (ret) {
  3123. mutex_unlock(&pci_priv->bus_lock);
  3124. return ret;
  3125. }
  3126. }
  3127. cnss_pci_set_auto_suspended(pci_priv, 1);
  3128. mutex_unlock(&pci_priv->bus_lock);
  3129. cnss_pci_set_monitor_wake_intr(pci_priv, true);
  3130. /* For suspend temporarily set bandwidth vote to NONE and dont save in
  3131. * current_bw_vote as in resume path we should vote for last used
  3132. * bandwidth vote. Also ignore error if bw voting is not setup.
  3133. */
  3134. cnss_setup_bus_bandwidth(plat_priv, CNSS_BUS_WIDTH_NONE, false);
  3135. return 0;
  3136. }
  3137. EXPORT_SYMBOL(cnss_auto_suspend);
  3138. int cnss_auto_resume(struct device *dev)
  3139. {
  3140. int ret = 0;
  3141. struct pci_dev *pci_dev = to_pci_dev(dev);
  3142. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  3143. struct cnss_plat_data *plat_priv;
  3144. if (!pci_priv)
  3145. return -ENODEV;
  3146. plat_priv = pci_priv->plat_priv;
  3147. if (!plat_priv)
  3148. return -ENODEV;
  3149. mutex_lock(&pci_priv->bus_lock);
  3150. ret = cnss_pci_resume_bus(pci_priv);
  3151. if (ret) {
  3152. mutex_unlock(&pci_priv->bus_lock);
  3153. return ret;
  3154. }
  3155. cnss_pci_set_auto_suspended(pci_priv, 0);
  3156. mutex_unlock(&pci_priv->bus_lock);
  3157. cnss_request_bus_bandwidth(dev, plat_priv->icc.current_bw_vote);
  3158. return 0;
  3159. }
  3160. EXPORT_SYMBOL(cnss_auto_resume);
  3161. int cnss_pci_force_wake_request_sync(struct device *dev, int timeout_us)
  3162. {
  3163. struct pci_dev *pci_dev = to_pci_dev(dev);
  3164. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  3165. struct cnss_plat_data *plat_priv;
  3166. struct mhi_controller *mhi_ctrl;
  3167. if (!pci_priv)
  3168. return -ENODEV;
  3169. switch (pci_priv->device_id) {
  3170. case QCA6390_DEVICE_ID:
  3171. case QCA6490_DEVICE_ID:
  3172. case KIWI_DEVICE_ID:
  3173. case MANGO_DEVICE_ID:
  3174. break;
  3175. default:
  3176. return 0;
  3177. }
  3178. mhi_ctrl = pci_priv->mhi_ctrl;
  3179. if (!mhi_ctrl)
  3180. return -EINVAL;
  3181. plat_priv = pci_priv->plat_priv;
  3182. if (!plat_priv)
  3183. return -ENODEV;
  3184. if (test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state))
  3185. return -EAGAIN;
  3186. if (timeout_us) {
  3187. /* Busy wait for timeout_us */
  3188. return cnss_mhi_device_get_sync_atomic(pci_priv,
  3189. timeout_us, false);
  3190. } else {
  3191. /* Sleep wait for mhi_ctrl->timeout_ms */
  3192. return mhi_device_get_sync(mhi_ctrl->mhi_dev);
  3193. }
  3194. }
  3195. EXPORT_SYMBOL(cnss_pci_force_wake_request_sync);
  3196. int cnss_pci_force_wake_request(struct device *dev)
  3197. {
  3198. struct pci_dev *pci_dev = to_pci_dev(dev);
  3199. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  3200. struct cnss_plat_data *plat_priv;
  3201. struct mhi_controller *mhi_ctrl;
  3202. if (!pci_priv)
  3203. return -ENODEV;
  3204. switch (pci_priv->device_id) {
  3205. case QCA6390_DEVICE_ID:
  3206. case QCA6490_DEVICE_ID:
  3207. case KIWI_DEVICE_ID:
  3208. case MANGO_DEVICE_ID:
  3209. break;
  3210. default:
  3211. return 0;
  3212. }
  3213. mhi_ctrl = pci_priv->mhi_ctrl;
  3214. if (!mhi_ctrl)
  3215. return -EINVAL;
  3216. plat_priv = pci_priv->plat_priv;
  3217. if (!plat_priv)
  3218. return -ENODEV;
  3219. if (test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state))
  3220. return -EAGAIN;
  3221. mhi_device_get(mhi_ctrl->mhi_dev);
  3222. return 0;
  3223. }
  3224. EXPORT_SYMBOL(cnss_pci_force_wake_request);
  3225. int cnss_pci_is_device_awake(struct device *dev)
  3226. {
  3227. struct pci_dev *pci_dev = to_pci_dev(dev);
  3228. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  3229. struct mhi_controller *mhi_ctrl;
  3230. if (!pci_priv)
  3231. return -ENODEV;
  3232. switch (pci_priv->device_id) {
  3233. case QCA6390_DEVICE_ID:
  3234. case QCA6490_DEVICE_ID:
  3235. case KIWI_DEVICE_ID:
  3236. case MANGO_DEVICE_ID:
  3237. break;
  3238. default:
  3239. return 0;
  3240. }
  3241. mhi_ctrl = pci_priv->mhi_ctrl;
  3242. if (!mhi_ctrl)
  3243. return -EINVAL;
  3244. return (mhi_ctrl->dev_state == MHI_STATE_M0);
  3245. }
  3246. EXPORT_SYMBOL(cnss_pci_is_device_awake);
  3247. int cnss_pci_force_wake_release(struct device *dev)
  3248. {
  3249. struct pci_dev *pci_dev = to_pci_dev(dev);
  3250. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  3251. struct cnss_plat_data *plat_priv;
  3252. struct mhi_controller *mhi_ctrl;
  3253. if (!pci_priv)
  3254. return -ENODEV;
  3255. switch (pci_priv->device_id) {
  3256. case QCA6390_DEVICE_ID:
  3257. case QCA6490_DEVICE_ID:
  3258. case KIWI_DEVICE_ID:
  3259. case MANGO_DEVICE_ID:
  3260. break;
  3261. default:
  3262. return 0;
  3263. }
  3264. mhi_ctrl = pci_priv->mhi_ctrl;
  3265. if (!mhi_ctrl)
  3266. return -EINVAL;
  3267. plat_priv = pci_priv->plat_priv;
  3268. if (!plat_priv)
  3269. return -ENODEV;
  3270. if (test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state))
  3271. return -EAGAIN;
  3272. mhi_device_put(mhi_ctrl->mhi_dev);
  3273. return 0;
  3274. }
  3275. EXPORT_SYMBOL(cnss_pci_force_wake_release);
  3276. int cnss_pci_qmi_send_get(struct cnss_pci_data *pci_priv)
  3277. {
  3278. int ret = 0;
  3279. if (!pci_priv)
  3280. return -ENODEV;
  3281. mutex_lock(&pci_priv->bus_lock);
  3282. if (cnss_pci_get_auto_suspended(pci_priv) &&
  3283. !pci_priv->qmi_send_usage_count)
  3284. ret = cnss_pci_resume_bus(pci_priv);
  3285. pci_priv->qmi_send_usage_count++;
  3286. cnss_pr_buf("Increased QMI send usage count to %d\n",
  3287. pci_priv->qmi_send_usage_count);
  3288. mutex_unlock(&pci_priv->bus_lock);
  3289. return ret;
  3290. }
  3291. int cnss_pci_qmi_send_put(struct cnss_pci_data *pci_priv)
  3292. {
  3293. int ret = 0;
  3294. if (!pci_priv)
  3295. return -ENODEV;
  3296. mutex_lock(&pci_priv->bus_lock);
  3297. if (pci_priv->qmi_send_usage_count)
  3298. pci_priv->qmi_send_usage_count--;
  3299. cnss_pr_buf("Decreased QMI send usage count to %d\n",
  3300. pci_priv->qmi_send_usage_count);
  3301. if (cnss_pci_get_auto_suspended(pci_priv) &&
  3302. !pci_priv->qmi_send_usage_count &&
  3303. !cnss_pcie_is_device_down(pci_priv))
  3304. ret = cnss_pci_suspend_bus(pci_priv);
  3305. mutex_unlock(&pci_priv->bus_lock);
  3306. return ret;
  3307. }
  3308. int cnss_pci_alloc_fw_mem(struct cnss_pci_data *pci_priv)
  3309. {
  3310. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  3311. struct cnss_fw_mem *fw_mem = plat_priv->fw_mem;
  3312. struct device *dev = &pci_priv->pci_dev->dev;
  3313. int i;
  3314. for (i = 0; i < plat_priv->fw_mem_seg_len; i++) {
  3315. if (!fw_mem[i].va && fw_mem[i].size) {
  3316. retry:
  3317. fw_mem[i].va =
  3318. dma_alloc_attrs(dev, fw_mem[i].size,
  3319. &fw_mem[i].pa, GFP_KERNEL,
  3320. fw_mem[i].attrs);
  3321. if (!fw_mem[i].va) {
  3322. if ((fw_mem[i].attrs &
  3323. DMA_ATTR_FORCE_CONTIGUOUS)) {
  3324. fw_mem[i].attrs &=
  3325. ~DMA_ATTR_FORCE_CONTIGUOUS;
  3326. cnss_pr_dbg("Fallback to non-contiguous memory for FW, Mem type: %u\n",
  3327. fw_mem[i].type);
  3328. goto retry;
  3329. }
  3330. cnss_pr_err("Failed to allocate memory for FW, size: 0x%zx, type: %u\n",
  3331. fw_mem[i].size, fw_mem[i].type);
  3332. CNSS_ASSERT(0);
  3333. return -ENOMEM;
  3334. }
  3335. }
  3336. }
  3337. return 0;
  3338. }
  3339. static void cnss_pci_free_fw_mem(struct cnss_pci_data *pci_priv)
  3340. {
  3341. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  3342. struct cnss_fw_mem *fw_mem = plat_priv->fw_mem;
  3343. struct device *dev = &pci_priv->pci_dev->dev;
  3344. int i;
  3345. for (i = 0; i < plat_priv->fw_mem_seg_len; i++) {
  3346. if (fw_mem[i].va && fw_mem[i].size) {
  3347. cnss_pr_dbg("Freeing memory for FW, va: 0x%pK, pa: %pa, size: 0x%zx, type: %u\n",
  3348. fw_mem[i].va, &fw_mem[i].pa,
  3349. fw_mem[i].size, fw_mem[i].type);
  3350. dma_free_attrs(dev, fw_mem[i].size,
  3351. fw_mem[i].va, fw_mem[i].pa,
  3352. fw_mem[i].attrs);
  3353. fw_mem[i].va = NULL;
  3354. fw_mem[i].pa = 0;
  3355. fw_mem[i].size = 0;
  3356. fw_mem[i].type = 0;
  3357. }
  3358. }
  3359. plat_priv->fw_mem_seg_len = 0;
  3360. }
  3361. int cnss_pci_alloc_qdss_mem(struct cnss_pci_data *pci_priv)
  3362. {
  3363. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  3364. struct cnss_fw_mem *qdss_mem = plat_priv->qdss_mem;
  3365. int i, j;
  3366. for (i = 0; i < plat_priv->qdss_mem_seg_len; i++) {
  3367. if (!qdss_mem[i].va && qdss_mem[i].size) {
  3368. qdss_mem[i].va =
  3369. dma_alloc_coherent(&pci_priv->pci_dev->dev,
  3370. qdss_mem[i].size,
  3371. &qdss_mem[i].pa,
  3372. GFP_KERNEL);
  3373. if (!qdss_mem[i].va) {
  3374. cnss_pr_err("Failed to allocate QDSS memory for FW, size: 0x%zx, type: %u, chuck-ID: %d\n",
  3375. qdss_mem[i].size,
  3376. qdss_mem[i].type, i);
  3377. break;
  3378. }
  3379. }
  3380. }
  3381. /* Best-effort allocation for QDSS trace */
  3382. if (i < plat_priv->qdss_mem_seg_len) {
  3383. for (j = i; j < plat_priv->qdss_mem_seg_len; j++) {
  3384. qdss_mem[j].type = 0;
  3385. qdss_mem[j].size = 0;
  3386. }
  3387. plat_priv->qdss_mem_seg_len = i;
  3388. }
  3389. return 0;
  3390. }
  3391. void cnss_pci_free_qdss_mem(struct cnss_pci_data *pci_priv)
  3392. {
  3393. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  3394. struct cnss_fw_mem *qdss_mem = plat_priv->qdss_mem;
  3395. int i;
  3396. for (i = 0; i < plat_priv->qdss_mem_seg_len; i++) {
  3397. if (qdss_mem[i].va && qdss_mem[i].size) {
  3398. cnss_pr_dbg("Freeing memory for QDSS: pa: %pa, size: 0x%zx, type: %u\n",
  3399. &qdss_mem[i].pa, qdss_mem[i].size,
  3400. qdss_mem[i].type);
  3401. dma_free_coherent(&pci_priv->pci_dev->dev,
  3402. qdss_mem[i].size, qdss_mem[i].va,
  3403. qdss_mem[i].pa);
  3404. qdss_mem[i].va = NULL;
  3405. qdss_mem[i].pa = 0;
  3406. qdss_mem[i].size = 0;
  3407. qdss_mem[i].type = 0;
  3408. }
  3409. }
  3410. plat_priv->qdss_mem_seg_len = 0;
  3411. }
  3412. int cnss_pci_load_m3(struct cnss_pci_data *pci_priv)
  3413. {
  3414. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  3415. struct cnss_fw_mem *m3_mem = &plat_priv->m3_mem;
  3416. char filename[MAX_FIRMWARE_NAME_LEN];
  3417. char *phy_filename = DEFAULT_PHY_UCODE_FILE_NAME;
  3418. const struct firmware *fw_entry;
  3419. int ret = 0;
  3420. /* Use forward compatibility here since for any recent device
  3421. * it should use DEFAULT_PHY_UCODE_FILE_NAME.
  3422. */
  3423. switch (pci_priv->device_id) {
  3424. case QCA6174_DEVICE_ID:
  3425. cnss_pr_err("Invalid device ID (0x%x) to load phy image\n",
  3426. pci_priv->device_id);
  3427. return -EINVAL;
  3428. case QCA6290_DEVICE_ID:
  3429. case QCA6390_DEVICE_ID:
  3430. case QCA6490_DEVICE_ID:
  3431. phy_filename = DEFAULT_PHY_M3_FILE_NAME;
  3432. break;
  3433. case KIWI_DEVICE_ID:
  3434. case MANGO_DEVICE_ID:
  3435. switch (plat_priv->device_version.major_version) {
  3436. case FW_V2_NUMBER:
  3437. phy_filename = PHY_UCODE_V2_FILE_NAME;
  3438. break;
  3439. default:
  3440. break;
  3441. }
  3442. break;
  3443. default:
  3444. break;
  3445. }
  3446. if (!m3_mem->va && !m3_mem->size) {
  3447. cnss_pci_add_fw_prefix_name(pci_priv, filename,
  3448. phy_filename);
  3449. ret = firmware_request_nowarn(&fw_entry, filename,
  3450. &pci_priv->pci_dev->dev);
  3451. if (ret) {
  3452. cnss_pr_err("Failed to load M3 image: %s\n", filename);
  3453. return ret;
  3454. }
  3455. m3_mem->va = dma_alloc_coherent(&pci_priv->pci_dev->dev,
  3456. fw_entry->size, &m3_mem->pa,
  3457. GFP_KERNEL);
  3458. if (!m3_mem->va) {
  3459. cnss_pr_err("Failed to allocate memory for M3, size: 0x%zx\n",
  3460. fw_entry->size);
  3461. release_firmware(fw_entry);
  3462. return -ENOMEM;
  3463. }
  3464. memcpy(m3_mem->va, fw_entry->data, fw_entry->size);
  3465. m3_mem->size = fw_entry->size;
  3466. release_firmware(fw_entry);
  3467. }
  3468. return 0;
  3469. }
  3470. static void cnss_pci_free_m3_mem(struct cnss_pci_data *pci_priv)
  3471. {
  3472. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  3473. struct cnss_fw_mem *m3_mem = &plat_priv->m3_mem;
  3474. if (m3_mem->va && m3_mem->size) {
  3475. cnss_pr_dbg("Freeing memory for M3, va: 0x%pK, pa: %pa, size: 0x%zx\n",
  3476. m3_mem->va, &m3_mem->pa, m3_mem->size);
  3477. dma_free_coherent(&pci_priv->pci_dev->dev, m3_mem->size,
  3478. m3_mem->va, m3_mem->pa);
  3479. }
  3480. m3_mem->va = NULL;
  3481. m3_mem->pa = 0;
  3482. m3_mem->size = 0;
  3483. }
  3484. void cnss_pci_fw_boot_timeout_hdlr(struct cnss_pci_data *pci_priv)
  3485. {
  3486. struct cnss_plat_data *plat_priv;
  3487. if (!pci_priv)
  3488. return;
  3489. cnss_fatal_err("Timeout waiting for FW ready indication\n");
  3490. plat_priv = pci_priv->plat_priv;
  3491. if (!plat_priv)
  3492. return;
  3493. if (test_bit(CNSS_IN_COLD_BOOT_CAL, &plat_priv->driver_state)) {
  3494. cnss_pr_dbg("Ignore FW ready timeout for calibration mode\n");
  3495. return;
  3496. }
  3497. cnss_schedule_recovery(&pci_priv->pci_dev->dev,
  3498. CNSS_REASON_TIMEOUT);
  3499. }
  3500. static void cnss_pci_deinit_smmu(struct cnss_pci_data *pci_priv)
  3501. {
  3502. pci_priv->iommu_domain = NULL;
  3503. }
  3504. int cnss_pci_get_iova(struct cnss_pci_data *pci_priv, u64 *addr, u64 *size)
  3505. {
  3506. if (!pci_priv)
  3507. return -ENODEV;
  3508. if (!pci_priv->smmu_iova_len)
  3509. return -EINVAL;
  3510. *addr = pci_priv->smmu_iova_start;
  3511. *size = pci_priv->smmu_iova_len;
  3512. return 0;
  3513. }
  3514. int cnss_pci_get_iova_ipa(struct cnss_pci_data *pci_priv, u64 *addr, u64 *size)
  3515. {
  3516. if (!pci_priv)
  3517. return -ENODEV;
  3518. if (!pci_priv->smmu_iova_ipa_len)
  3519. return -EINVAL;
  3520. *addr = pci_priv->smmu_iova_ipa_start;
  3521. *size = pci_priv->smmu_iova_ipa_len;
  3522. return 0;
  3523. }
  3524. struct iommu_domain *cnss_smmu_get_domain(struct device *dev)
  3525. {
  3526. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(to_pci_dev(dev));
  3527. if (!pci_priv)
  3528. return NULL;
  3529. return pci_priv->iommu_domain;
  3530. }
  3531. EXPORT_SYMBOL(cnss_smmu_get_domain);
  3532. int cnss_smmu_map(struct device *dev,
  3533. phys_addr_t paddr, uint32_t *iova_addr, size_t size)
  3534. {
  3535. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(to_pci_dev(dev));
  3536. struct cnss_plat_data *plat_priv;
  3537. unsigned long iova;
  3538. size_t len;
  3539. int ret = 0;
  3540. int flag = IOMMU_READ | IOMMU_WRITE;
  3541. struct pci_dev *root_port;
  3542. struct device_node *root_of_node;
  3543. bool dma_coherent = false;
  3544. if (!pci_priv)
  3545. return -ENODEV;
  3546. if (!iova_addr) {
  3547. cnss_pr_err("iova_addr is NULL, paddr %pa, size %zu\n",
  3548. &paddr, size);
  3549. return -EINVAL;
  3550. }
  3551. plat_priv = pci_priv->plat_priv;
  3552. len = roundup(size + paddr - rounddown(paddr, PAGE_SIZE), PAGE_SIZE);
  3553. iova = roundup(pci_priv->smmu_iova_ipa_current, PAGE_SIZE);
  3554. if (pci_priv->iommu_geometry &&
  3555. iova >= pci_priv->smmu_iova_ipa_start +
  3556. pci_priv->smmu_iova_ipa_len) {
  3557. cnss_pr_err("No IOVA space to map, iova %lx, smmu_iova_ipa_start %pad, smmu_iova_ipa_len %zu\n",
  3558. iova,
  3559. &pci_priv->smmu_iova_ipa_start,
  3560. pci_priv->smmu_iova_ipa_len);
  3561. return -ENOMEM;
  3562. }
  3563. if (!test_bit(DISABLE_IO_COHERENCY,
  3564. &plat_priv->ctrl_params.quirks)) {
  3565. root_port = pcie_find_root_port(pci_priv->pci_dev);
  3566. if (!root_port) {
  3567. cnss_pr_err("Root port is null, so dma_coherent is disabled\n");
  3568. } else {
  3569. root_of_node = root_port->dev.of_node;
  3570. if (root_of_node && root_of_node->parent) {
  3571. dma_coherent =
  3572. of_property_read_bool(root_of_node->parent,
  3573. "dma-coherent");
  3574. cnss_pr_dbg("dma-coherent is %s\n",
  3575. dma_coherent ? "enabled" : "disabled");
  3576. if (dma_coherent)
  3577. flag |= IOMMU_CACHE;
  3578. }
  3579. }
  3580. }
  3581. cnss_pr_dbg("IOMMU map: iova %lx, len %zu\n", iova, len);
  3582. ret = iommu_map(pci_priv->iommu_domain, iova,
  3583. rounddown(paddr, PAGE_SIZE), len, flag);
  3584. if (ret) {
  3585. cnss_pr_err("PA to IOVA mapping failed, ret %d\n", ret);
  3586. return ret;
  3587. }
  3588. pci_priv->smmu_iova_ipa_current = iova + len;
  3589. *iova_addr = (uint32_t)(iova + paddr - rounddown(paddr, PAGE_SIZE));
  3590. cnss_pr_dbg("IOMMU map: iova_addr %lx\n", *iova_addr);
  3591. return 0;
  3592. }
  3593. EXPORT_SYMBOL(cnss_smmu_map);
  3594. int cnss_smmu_unmap(struct device *dev, uint32_t iova_addr, size_t size)
  3595. {
  3596. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(to_pci_dev(dev));
  3597. unsigned long iova;
  3598. size_t unmapped;
  3599. size_t len;
  3600. if (!pci_priv)
  3601. return -ENODEV;
  3602. iova = rounddown(iova_addr, PAGE_SIZE);
  3603. len = roundup(size + iova_addr - iova, PAGE_SIZE);
  3604. if (iova >= pci_priv->smmu_iova_ipa_start +
  3605. pci_priv->smmu_iova_ipa_len) {
  3606. cnss_pr_err("Out of IOVA space to unmap, iova %lx, smmu_iova_ipa_start %pad, smmu_iova_ipa_len %zu\n",
  3607. iova,
  3608. &pci_priv->smmu_iova_ipa_start,
  3609. pci_priv->smmu_iova_ipa_len);
  3610. return -ENOMEM;
  3611. }
  3612. cnss_pr_dbg("IOMMU unmap: iova %lx, len %zu\n", iova, len);
  3613. unmapped = iommu_unmap(pci_priv->iommu_domain, iova, len);
  3614. if (unmapped != len) {
  3615. cnss_pr_err("IOMMU unmap failed, unmapped = %zu, requested = %zu\n",
  3616. unmapped, len);
  3617. return -EINVAL;
  3618. }
  3619. pci_priv->smmu_iova_ipa_current = iova;
  3620. return 0;
  3621. }
  3622. EXPORT_SYMBOL(cnss_smmu_unmap);
  3623. int cnss_get_soc_info(struct device *dev, struct cnss_soc_info *info)
  3624. {
  3625. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(to_pci_dev(dev));
  3626. struct cnss_plat_data *plat_priv;
  3627. if (!pci_priv)
  3628. return -ENODEV;
  3629. plat_priv = pci_priv->plat_priv;
  3630. if (!plat_priv)
  3631. return -ENODEV;
  3632. info->va = pci_priv->bar;
  3633. info->pa = pci_resource_start(pci_priv->pci_dev, PCI_BAR_NUM);
  3634. info->chip_id = plat_priv->chip_info.chip_id;
  3635. info->chip_family = plat_priv->chip_info.chip_family;
  3636. info->board_id = plat_priv->board_info.board_id;
  3637. info->soc_id = plat_priv->soc_info.soc_id;
  3638. info->fw_version = plat_priv->fw_version_info.fw_version;
  3639. strlcpy(info->fw_build_timestamp,
  3640. plat_priv->fw_version_info.fw_build_timestamp,
  3641. sizeof(info->fw_build_timestamp));
  3642. memcpy(&info->device_version, &plat_priv->device_version,
  3643. sizeof(info->device_version));
  3644. memcpy(&info->dev_mem_info, &plat_priv->dev_mem_info,
  3645. sizeof(info->dev_mem_info));
  3646. return 0;
  3647. }
  3648. EXPORT_SYMBOL(cnss_get_soc_info);
  3649. static int cnss_pci_enable_msi(struct cnss_pci_data *pci_priv)
  3650. {
  3651. int ret = 0;
  3652. struct pci_dev *pci_dev = pci_priv->pci_dev;
  3653. int num_vectors;
  3654. struct cnss_msi_config *msi_config;
  3655. struct msi_desc *msi_desc;
  3656. if (pci_priv->device_id == QCA6174_DEVICE_ID)
  3657. return 0;
  3658. ret = cnss_pci_get_msi_assignment(pci_priv);
  3659. if (ret) {
  3660. cnss_pr_err("Failed to get MSI assignment, err = %d\n", ret);
  3661. goto out;
  3662. }
  3663. msi_config = pci_priv->msi_config;
  3664. if (!msi_config) {
  3665. cnss_pr_err("msi_config is NULL!\n");
  3666. ret = -EINVAL;
  3667. goto out;
  3668. }
  3669. num_vectors = pci_alloc_irq_vectors(pci_dev,
  3670. msi_config->total_vectors,
  3671. msi_config->total_vectors,
  3672. PCI_IRQ_MSI);
  3673. if (num_vectors != msi_config->total_vectors) {
  3674. cnss_pr_err("Failed to get enough MSI vectors (%d), available vectors = %d",
  3675. msi_config->total_vectors, num_vectors);
  3676. if (num_vectors >= 0)
  3677. ret = -EINVAL;
  3678. goto reset_msi_config;
  3679. }
  3680. msi_desc = irq_get_msi_desc(pci_dev->irq);
  3681. if (!msi_desc) {
  3682. cnss_pr_err("msi_desc is NULL!\n");
  3683. ret = -EINVAL;
  3684. goto free_msi_vector;
  3685. }
  3686. pci_priv->msi_ep_base_data = msi_desc->msg.data;
  3687. cnss_pr_dbg("MSI base data is %d\n", pci_priv->msi_ep_base_data);
  3688. return 0;
  3689. free_msi_vector:
  3690. pci_free_irq_vectors(pci_priv->pci_dev);
  3691. reset_msi_config:
  3692. pci_priv->msi_config = NULL;
  3693. out:
  3694. return ret;
  3695. }
  3696. static void cnss_pci_disable_msi(struct cnss_pci_data *pci_priv)
  3697. {
  3698. if (pci_priv->device_id == QCA6174_DEVICE_ID)
  3699. return;
  3700. pci_free_irq_vectors(pci_priv->pci_dev);
  3701. }
  3702. int cnss_get_user_msi_assignment(struct device *dev, char *user_name,
  3703. int *num_vectors, u32 *user_base_data,
  3704. u32 *base_vector)
  3705. {
  3706. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(to_pci_dev(dev));
  3707. struct cnss_msi_config *msi_config;
  3708. int idx;
  3709. if (!pci_priv)
  3710. return -ENODEV;
  3711. msi_config = pci_priv->msi_config;
  3712. if (!msi_config) {
  3713. cnss_pr_err("MSI is not supported.\n");
  3714. return -EINVAL;
  3715. }
  3716. for (idx = 0; idx < msi_config->total_users; idx++) {
  3717. if (strcmp(user_name, msi_config->users[idx].name) == 0) {
  3718. *num_vectors = msi_config->users[idx].num_vectors;
  3719. *user_base_data = msi_config->users[idx].base_vector
  3720. + pci_priv->msi_ep_base_data;
  3721. *base_vector = msi_config->users[idx].base_vector;
  3722. /*Add only single print for each user*/
  3723. if (print_optimize.msi_log_chk[idx]++)
  3724. goto skip_print;
  3725. cnss_pr_dbg("Assign MSI to user: %s, num_vectors: %d, user_base_data: %u, base_vector: %u\n",
  3726. user_name, *num_vectors, *user_base_data,
  3727. *base_vector);
  3728. skip_print:
  3729. return 0;
  3730. }
  3731. }
  3732. cnss_pr_err("Failed to find MSI assignment for %s!\n", user_name);
  3733. return -EINVAL;
  3734. }
  3735. EXPORT_SYMBOL(cnss_get_user_msi_assignment);
  3736. int cnss_get_msi_irq(struct device *dev, unsigned int vector)
  3737. {
  3738. struct pci_dev *pci_dev = to_pci_dev(dev);
  3739. int irq_num;
  3740. irq_num = pci_irq_vector(pci_dev, vector);
  3741. cnss_pr_dbg("Get IRQ number %d for vector index %d\n", irq_num, vector);
  3742. return irq_num;
  3743. }
  3744. EXPORT_SYMBOL(cnss_get_msi_irq);
  3745. void cnss_get_msi_address(struct device *dev, u32 *msi_addr_low,
  3746. u32 *msi_addr_high)
  3747. {
  3748. struct pci_dev *pci_dev = to_pci_dev(dev);
  3749. u16 control;
  3750. pci_read_config_word(pci_dev, pci_dev->msi_cap + PCI_MSI_FLAGS,
  3751. &control);
  3752. pci_read_config_dword(pci_dev, pci_dev->msi_cap + PCI_MSI_ADDRESS_LO,
  3753. msi_addr_low);
  3754. /* Return MSI high address only when device supports 64-bit MSI */
  3755. if (control & PCI_MSI_FLAGS_64BIT)
  3756. pci_read_config_dword(pci_dev,
  3757. pci_dev->msi_cap + PCI_MSI_ADDRESS_HI,
  3758. msi_addr_high);
  3759. else
  3760. *msi_addr_high = 0;
  3761. /*Add only single print as the address is constant*/
  3762. if (!print_optimize.msi_addr_chk++)
  3763. cnss_pr_dbg("Get MSI low addr = 0x%x, high addr = 0x%x\n",
  3764. *msi_addr_low, *msi_addr_high);
  3765. }
  3766. EXPORT_SYMBOL(cnss_get_msi_address);
  3767. u32 cnss_pci_get_wake_msi(struct cnss_pci_data *pci_priv)
  3768. {
  3769. int ret, num_vectors;
  3770. u32 user_base_data, base_vector;
  3771. if (!pci_priv)
  3772. return -ENODEV;
  3773. ret = cnss_get_user_msi_assignment(&pci_priv->pci_dev->dev,
  3774. WAKE_MSI_NAME, &num_vectors,
  3775. &user_base_data, &base_vector);
  3776. if (ret) {
  3777. cnss_pr_err("WAKE MSI is not valid\n");
  3778. return 0;
  3779. }
  3780. return user_base_data;
  3781. }
  3782. static int cnss_pci_enable_bus(struct cnss_pci_data *pci_priv)
  3783. {
  3784. int ret = 0;
  3785. struct pci_dev *pci_dev = pci_priv->pci_dev;
  3786. u16 device_id;
  3787. pci_read_config_word(pci_dev, PCI_DEVICE_ID, &device_id);
  3788. if (device_id != pci_priv->pci_device_id->device) {
  3789. cnss_pr_err("PCI device ID mismatch, config ID: 0x%x, probe ID: 0x%x\n",
  3790. device_id, pci_priv->pci_device_id->device);
  3791. ret = -EIO;
  3792. goto out;
  3793. }
  3794. ret = pci_assign_resource(pci_dev, PCI_BAR_NUM);
  3795. if (ret) {
  3796. pr_err("Failed to assign PCI resource, err = %d\n", ret);
  3797. goto out;
  3798. }
  3799. ret = pci_enable_device(pci_dev);
  3800. if (ret) {
  3801. cnss_pr_err("Failed to enable PCI device, err = %d\n", ret);
  3802. goto out;
  3803. }
  3804. ret = pci_request_region(pci_dev, PCI_BAR_NUM, "cnss");
  3805. if (ret) {
  3806. cnss_pr_err("Failed to request PCI region, err = %d\n", ret);
  3807. goto disable_device;
  3808. }
  3809. switch (device_id) {
  3810. case QCA6174_DEVICE_ID:
  3811. pci_priv->dma_bit_mask = PCI_DMA_MASK_32_BIT;
  3812. break;
  3813. case QCA6390_DEVICE_ID:
  3814. case QCA6490_DEVICE_ID:
  3815. case KIWI_DEVICE_ID:
  3816. case MANGO_DEVICE_ID:
  3817. pci_priv->dma_bit_mask = PCI_DMA_MASK_36_BIT;
  3818. break;
  3819. default:
  3820. pci_priv->dma_bit_mask = PCI_DMA_MASK_32_BIT;
  3821. break;
  3822. }
  3823. cnss_pr_dbg("Set PCI DMA MASK (0x%llx)\n", pci_priv->dma_bit_mask);
  3824. ret = pci_set_dma_mask(pci_dev, pci_priv->dma_bit_mask);
  3825. if (ret) {
  3826. cnss_pr_err("Failed to set PCI DMA mask, err = %d\n", ret);
  3827. goto release_region;
  3828. }
  3829. ret = pci_set_consistent_dma_mask(pci_dev, pci_priv->dma_bit_mask);
  3830. if (ret) {
  3831. cnss_pr_err("Failed to set PCI consistent DMA mask, err = %d\n",
  3832. ret);
  3833. goto release_region;
  3834. }
  3835. pci_priv->bar = pci_iomap(pci_dev, PCI_BAR_NUM, 0);
  3836. if (!pci_priv->bar) {
  3837. cnss_pr_err("Failed to do PCI IO map!\n");
  3838. ret = -EIO;
  3839. goto release_region;
  3840. }
  3841. /* Save default config space without BME enabled */
  3842. pci_save_state(pci_dev);
  3843. pci_priv->default_state = pci_store_saved_state(pci_dev);
  3844. pci_set_master(pci_dev);
  3845. return 0;
  3846. release_region:
  3847. pci_release_region(pci_dev, PCI_BAR_NUM);
  3848. disable_device:
  3849. pci_disable_device(pci_dev);
  3850. out:
  3851. return ret;
  3852. }
  3853. static void cnss_pci_disable_bus(struct cnss_pci_data *pci_priv)
  3854. {
  3855. struct pci_dev *pci_dev = pci_priv->pci_dev;
  3856. pci_clear_master(pci_dev);
  3857. pci_load_and_free_saved_state(pci_dev, &pci_priv->saved_state);
  3858. pci_load_and_free_saved_state(pci_dev, &pci_priv->default_state);
  3859. if (pci_priv->bar) {
  3860. pci_iounmap(pci_dev, pci_priv->bar);
  3861. pci_priv->bar = NULL;
  3862. }
  3863. pci_release_region(pci_dev, PCI_BAR_NUM);
  3864. if (pci_is_enabled(pci_dev))
  3865. pci_disable_device(pci_dev);
  3866. }
  3867. static void cnss_pci_dump_qdss_reg(struct cnss_pci_data *pci_priv)
  3868. {
  3869. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  3870. int i, array_size = ARRAY_SIZE(qdss_csr) - 1;
  3871. gfp_t gfp = GFP_KERNEL;
  3872. u32 reg_offset;
  3873. if (in_interrupt() || irqs_disabled())
  3874. gfp = GFP_ATOMIC;
  3875. if (!plat_priv->qdss_reg) {
  3876. plat_priv->qdss_reg = devm_kzalloc(&pci_priv->pci_dev->dev,
  3877. sizeof(*plat_priv->qdss_reg)
  3878. * array_size, gfp);
  3879. if (!plat_priv->qdss_reg)
  3880. return;
  3881. }
  3882. cnss_pr_dbg("Start to dump qdss registers\n");
  3883. for (i = 0; qdss_csr[i].name; i++) {
  3884. reg_offset = QDSS_APB_DEC_CSR_BASE + qdss_csr[i].offset;
  3885. if (cnss_pci_reg_read(pci_priv, reg_offset,
  3886. &plat_priv->qdss_reg[i]))
  3887. return;
  3888. cnss_pr_dbg("%s[0x%x] = 0x%x\n", qdss_csr[i].name, reg_offset,
  3889. plat_priv->qdss_reg[i]);
  3890. }
  3891. }
  3892. static void cnss_pci_dump_ce_reg(struct cnss_pci_data *pci_priv,
  3893. enum cnss_ce_index ce)
  3894. {
  3895. int i;
  3896. u32 ce_base = ce * CE_REG_INTERVAL;
  3897. u32 reg_offset, src_ring_base, dst_ring_base, cmn_base, val;
  3898. switch (pci_priv->device_id) {
  3899. case QCA6390_DEVICE_ID:
  3900. src_ring_base = QCA6390_CE_SRC_RING_REG_BASE;
  3901. dst_ring_base = QCA6390_CE_DST_RING_REG_BASE;
  3902. cmn_base = QCA6390_CE_COMMON_REG_BASE;
  3903. break;
  3904. case QCA6490_DEVICE_ID:
  3905. src_ring_base = QCA6490_CE_SRC_RING_REG_BASE;
  3906. dst_ring_base = QCA6490_CE_DST_RING_REG_BASE;
  3907. cmn_base = QCA6490_CE_COMMON_REG_BASE;
  3908. break;
  3909. default:
  3910. return;
  3911. }
  3912. switch (ce) {
  3913. case CNSS_CE_09:
  3914. case CNSS_CE_10:
  3915. for (i = 0; ce_src[i].name; i++) {
  3916. reg_offset = src_ring_base + ce_base + ce_src[i].offset;
  3917. if (cnss_pci_reg_read(pci_priv, reg_offset, &val))
  3918. return;
  3919. cnss_pr_dbg("CE_%02d_%s[0x%x] = 0x%x\n",
  3920. ce, ce_src[i].name, reg_offset, val);
  3921. }
  3922. for (i = 0; ce_dst[i].name; i++) {
  3923. reg_offset = dst_ring_base + ce_base + ce_dst[i].offset;
  3924. if (cnss_pci_reg_read(pci_priv, reg_offset, &val))
  3925. return;
  3926. cnss_pr_dbg("CE_%02d_%s[0x%x] = 0x%x\n",
  3927. ce, ce_dst[i].name, reg_offset, val);
  3928. }
  3929. break;
  3930. case CNSS_CE_COMMON:
  3931. for (i = 0; ce_cmn[i].name; i++) {
  3932. reg_offset = cmn_base + ce_cmn[i].offset;
  3933. if (cnss_pci_reg_read(pci_priv, reg_offset, &val))
  3934. return;
  3935. cnss_pr_dbg("CE_COMMON_%s[0x%x] = 0x%x\n",
  3936. ce_cmn[i].name, reg_offset, val);
  3937. }
  3938. break;
  3939. default:
  3940. cnss_pr_err("Unsupported CE[%d] registers dump\n", ce);
  3941. }
  3942. }
  3943. static void cnss_pci_dump_debug_reg(struct cnss_pci_data *pci_priv)
  3944. {
  3945. if (cnss_pci_check_link_status(pci_priv))
  3946. return;
  3947. cnss_pr_dbg("Start to dump debug registers\n");
  3948. cnss_mhi_debug_reg_dump(pci_priv);
  3949. cnss_pci_soc_scratch_reg_dump(pci_priv);
  3950. cnss_pci_dump_ce_reg(pci_priv, CNSS_CE_COMMON);
  3951. cnss_pci_dump_ce_reg(pci_priv, CNSS_CE_09);
  3952. cnss_pci_dump_ce_reg(pci_priv, CNSS_CE_10);
  3953. }
  3954. static int cnss_pci_assert_host_sol(struct cnss_pci_data *pci_priv)
  3955. {
  3956. if (cnss_get_host_sol_value(pci_priv->plat_priv))
  3957. return -EINVAL;
  3958. cnss_pr_dbg("Assert host SOL GPIO to retry RDDM, expecting link down\n");
  3959. cnss_set_host_sol_value(pci_priv->plat_priv, 1);
  3960. return 0;
  3961. }
  3962. int cnss_pci_force_fw_assert_hdlr(struct cnss_pci_data *pci_priv)
  3963. {
  3964. int ret;
  3965. struct cnss_plat_data *plat_priv;
  3966. if (!pci_priv)
  3967. return -ENODEV;
  3968. plat_priv = pci_priv->plat_priv;
  3969. if (!plat_priv)
  3970. return -ENODEV;
  3971. if (!test_bit(CNSS_MHI_POWER_ON, &pci_priv->mhi_state) ||
  3972. test_bit(CNSS_MHI_POWERING_OFF, &pci_priv->mhi_state))
  3973. return -EINVAL;
  3974. cnss_auto_resume(&pci_priv->pci_dev->dev);
  3975. if (!cnss_pci_check_link_status(pci_priv))
  3976. cnss_mhi_debug_reg_dump(pci_priv);
  3977. cnss_pci_soc_scratch_reg_dump(pci_priv);
  3978. cnss_pci_dump_misc_reg(pci_priv);
  3979. cnss_pci_dump_shadow_reg(pci_priv);
  3980. /* If link is still down here, directly trigger link down recovery */
  3981. ret = cnss_pci_check_link_status(pci_priv);
  3982. if (ret) {
  3983. cnss_pci_link_down(&pci_priv->pci_dev->dev);
  3984. return 0;
  3985. }
  3986. ret = cnss_pci_set_mhi_state(pci_priv, CNSS_MHI_TRIGGER_RDDM);
  3987. if (ret) {
  3988. if (!test_bit(CNSS_MHI_POWER_ON, &pci_priv->mhi_state) ||
  3989. test_bit(CNSS_MHI_POWERING_OFF, &pci_priv->mhi_state)) {
  3990. cnss_pr_dbg("MHI is not powered on, ignore RDDM failure\n");
  3991. return 0;
  3992. }
  3993. cnss_fatal_err("Failed to trigger RDDM, err = %d\n", ret);
  3994. if (!cnss_pci_assert_host_sol(pci_priv))
  3995. return 0;
  3996. cnss_pci_dump_debug_reg(pci_priv);
  3997. cnss_schedule_recovery(&pci_priv->pci_dev->dev,
  3998. CNSS_REASON_DEFAULT);
  3999. return ret;
  4000. }
  4001. if (!test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state)) {
  4002. mod_timer(&pci_priv->dev_rddm_timer,
  4003. jiffies + msecs_to_jiffies(DEV_RDDM_TIMEOUT));
  4004. }
  4005. return 0;
  4006. }
  4007. static void cnss_pci_add_dump_seg(struct cnss_pci_data *pci_priv,
  4008. struct cnss_dump_seg *dump_seg,
  4009. enum cnss_fw_dump_type type, int seg_no,
  4010. void *va, dma_addr_t dma, size_t size)
  4011. {
  4012. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  4013. struct device *dev = &pci_priv->pci_dev->dev;
  4014. phys_addr_t pa;
  4015. dump_seg->address = dma;
  4016. dump_seg->v_address = va;
  4017. dump_seg->size = size;
  4018. dump_seg->type = type;
  4019. cnss_pr_dbg("Seg: %x, va: %pK, dma: %pa, size: 0x%zx\n",
  4020. seg_no, va, &dma, size);
  4021. if (cnss_va_to_pa(dev, size, va, dma, &pa, DMA_ATTR_FORCE_CONTIGUOUS))
  4022. return;
  4023. cnss_minidump_add_region(plat_priv, type, seg_no, va, pa, size);
  4024. }
  4025. static void cnss_pci_remove_dump_seg(struct cnss_pci_data *pci_priv,
  4026. struct cnss_dump_seg *dump_seg,
  4027. enum cnss_fw_dump_type type, int seg_no,
  4028. void *va, dma_addr_t dma, size_t size)
  4029. {
  4030. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  4031. struct device *dev = &pci_priv->pci_dev->dev;
  4032. phys_addr_t pa;
  4033. cnss_va_to_pa(dev, size, va, dma, &pa, DMA_ATTR_FORCE_CONTIGUOUS);
  4034. cnss_minidump_remove_region(plat_priv, type, seg_no, va, pa, size);
  4035. }
  4036. int cnss_pci_call_driver_uevent(struct cnss_pci_data *pci_priv,
  4037. enum cnss_driver_status status, void *data)
  4038. {
  4039. struct cnss_uevent_data uevent_data;
  4040. struct cnss_wlan_driver *driver_ops;
  4041. driver_ops = pci_priv->driver_ops;
  4042. if (!driver_ops || !driver_ops->update_event) {
  4043. cnss_pr_dbg("Hang event driver ops is NULL\n");
  4044. return -EINVAL;
  4045. }
  4046. cnss_pr_dbg("Calling driver uevent: %d\n", status);
  4047. uevent_data.status = status;
  4048. uevent_data.data = data;
  4049. return driver_ops->update_event(pci_priv->pci_dev, &uevent_data);
  4050. }
  4051. static void cnss_pci_send_hang_event(struct cnss_pci_data *pci_priv)
  4052. {
  4053. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  4054. struct cnss_fw_mem *fw_mem = plat_priv->fw_mem;
  4055. struct cnss_hang_event hang_event;
  4056. void *hang_data_va = NULL;
  4057. u64 offset = 0;
  4058. u16 length = 0;
  4059. int i = 0;
  4060. if (!fw_mem || !plat_priv->fw_mem_seg_len)
  4061. return;
  4062. memset(&hang_event, 0, sizeof(hang_event));
  4063. switch (pci_priv->device_id) {
  4064. case QCA6390_DEVICE_ID:
  4065. offset = HST_HANG_DATA_OFFSET;
  4066. length = HANG_DATA_LENGTH;
  4067. break;
  4068. case QCA6490_DEVICE_ID:
  4069. /* Fallback to hard-coded values if hang event params not
  4070. * present in QMI. Once all the firmware branches have the
  4071. * fix to send params over QMI, this can be removed.
  4072. */
  4073. if (plat_priv->hang_event_data_len) {
  4074. offset = plat_priv->hang_data_addr_offset;
  4075. length = plat_priv->hang_event_data_len;
  4076. } else {
  4077. offset = HSP_HANG_DATA_OFFSET;
  4078. length = HANG_DATA_LENGTH;
  4079. }
  4080. break;
  4081. case KIWI_DEVICE_ID:
  4082. case MANGO_DEVICE_ID:
  4083. offset = plat_priv->hang_data_addr_offset;
  4084. length = plat_priv->hang_event_data_len;
  4085. break;
  4086. default:
  4087. cnss_pr_err("Skip Hang Event Data as unsupported Device ID received: %d\n",
  4088. pci_priv->device_id);
  4089. return;
  4090. }
  4091. for (i = 0; i < plat_priv->fw_mem_seg_len; i++) {
  4092. if (fw_mem[i].type == QMI_WLFW_MEM_TYPE_DDR_V01 &&
  4093. fw_mem[i].va) {
  4094. /* The offset must be < (fw_mem size- hangdata length) */
  4095. if (!(offset <= fw_mem[i].size - length))
  4096. goto exit;
  4097. hang_data_va = fw_mem[i].va + offset;
  4098. hang_event.hang_event_data = kmemdup(hang_data_va,
  4099. length,
  4100. GFP_ATOMIC);
  4101. if (!hang_event.hang_event_data) {
  4102. cnss_pr_dbg("Hang data memory alloc failed\n");
  4103. return;
  4104. }
  4105. hang_event.hang_event_data_len = length;
  4106. break;
  4107. }
  4108. }
  4109. cnss_pci_call_driver_uevent(pci_priv, CNSS_HANG_EVENT, &hang_event);
  4110. kfree(hang_event.hang_event_data);
  4111. hang_event.hang_event_data = NULL;
  4112. return;
  4113. exit:
  4114. cnss_pr_dbg("Invalid hang event params, offset:0x%x, length:0x%x\n",
  4115. plat_priv->hang_data_addr_offset,
  4116. plat_priv->hang_event_data_len);
  4117. }
  4118. void cnss_pci_collect_dump_info(struct cnss_pci_data *pci_priv, bool in_panic)
  4119. {
  4120. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  4121. struct cnss_dump_data *dump_data =
  4122. &plat_priv->ramdump_info_v2.dump_data;
  4123. struct cnss_dump_seg *dump_seg =
  4124. plat_priv->ramdump_info_v2.dump_data_vaddr;
  4125. struct image_info *fw_image, *rddm_image;
  4126. struct cnss_fw_mem *fw_mem = plat_priv->fw_mem;
  4127. int ret, i, j;
  4128. if (test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state) &&
  4129. !test_bit(CNSS_IN_PANIC, &plat_priv->driver_state))
  4130. cnss_pci_send_hang_event(pci_priv);
  4131. if (test_bit(CNSS_MHI_RDDM_DONE, &pci_priv->mhi_state)) {
  4132. cnss_pr_dbg("RAM dump is already collected, skip\n");
  4133. return;
  4134. }
  4135. if (!cnss_is_device_powered_on(plat_priv)) {
  4136. cnss_pr_dbg("Device is already powered off, skip\n");
  4137. return;
  4138. }
  4139. if (!in_panic) {
  4140. mutex_lock(&pci_priv->bus_lock);
  4141. ret = cnss_pci_check_link_status(pci_priv);
  4142. if (ret) {
  4143. if (ret != -EACCES) {
  4144. mutex_unlock(&pci_priv->bus_lock);
  4145. return;
  4146. }
  4147. if (cnss_pci_resume_bus(pci_priv)) {
  4148. mutex_unlock(&pci_priv->bus_lock);
  4149. return;
  4150. }
  4151. }
  4152. mutex_unlock(&pci_priv->bus_lock);
  4153. } else {
  4154. if (cnss_pci_check_link_status(pci_priv))
  4155. return;
  4156. /* Inside panic handler, reduce timeout for RDDM to avoid
  4157. * unnecessary hypervisor watchdog bite.
  4158. */
  4159. pci_priv->mhi_ctrl->timeout_ms /= 2;
  4160. }
  4161. cnss_mhi_debug_reg_dump(pci_priv);
  4162. cnss_pci_soc_scratch_reg_dump(pci_priv);
  4163. cnss_pci_dump_misc_reg(pci_priv);
  4164. cnss_pci_dump_shadow_reg(pci_priv);
  4165. ret = mhi_download_rddm_image(pci_priv->mhi_ctrl, in_panic);
  4166. if (ret) {
  4167. cnss_fatal_err("Failed to download RDDM image, err = %d\n",
  4168. ret);
  4169. if (!cnss_pci_assert_host_sol(pci_priv))
  4170. return;
  4171. cnss_pci_dump_debug_reg(pci_priv);
  4172. return;
  4173. }
  4174. fw_image = pci_priv->mhi_ctrl->fbc_image;
  4175. rddm_image = pci_priv->mhi_ctrl->rddm_image;
  4176. dump_data->nentries = 0;
  4177. if (plat_priv->qdss_mem_seg_len)
  4178. cnss_pci_dump_qdss_reg(pci_priv);
  4179. cnss_mhi_dump_sfr(pci_priv);
  4180. if (!dump_seg) {
  4181. cnss_pr_warn("FW image dump collection not setup");
  4182. goto skip_dump;
  4183. }
  4184. cnss_pr_dbg("Collect FW image dump segment, nentries %d\n",
  4185. fw_image->entries);
  4186. for (i = 0; i < fw_image->entries; i++) {
  4187. cnss_pci_add_dump_seg(pci_priv, dump_seg, CNSS_FW_IMAGE, i,
  4188. fw_image->mhi_buf[i].buf,
  4189. fw_image->mhi_buf[i].dma_addr,
  4190. fw_image->mhi_buf[i].len);
  4191. dump_seg++;
  4192. }
  4193. dump_data->nentries += fw_image->entries;
  4194. cnss_pr_dbg("Collect RDDM image dump segment, nentries %d\n",
  4195. rddm_image->entries);
  4196. for (i = 0; i < rddm_image->entries; i++) {
  4197. cnss_pci_add_dump_seg(pci_priv, dump_seg, CNSS_FW_RDDM, i,
  4198. rddm_image->mhi_buf[i].buf,
  4199. rddm_image->mhi_buf[i].dma_addr,
  4200. rddm_image->mhi_buf[i].len);
  4201. dump_seg++;
  4202. }
  4203. dump_data->nentries += rddm_image->entries;
  4204. for (i = 0, j = 0; i < plat_priv->fw_mem_seg_len; i++) {
  4205. if (fw_mem[i].type == CNSS_MEM_TYPE_DDR) {
  4206. if (fw_mem[i].attrs & DMA_ATTR_FORCE_CONTIGUOUS) {
  4207. cnss_pr_dbg("Collect remote heap dump segment\n");
  4208. cnss_pci_add_dump_seg(pci_priv, dump_seg,
  4209. CNSS_FW_REMOTE_HEAP, j,
  4210. fw_mem[i].va,
  4211. fw_mem[i].pa,
  4212. fw_mem[i].size);
  4213. dump_seg++;
  4214. dump_data->nentries++;
  4215. j++;
  4216. } else {
  4217. cnss_pr_dbg("Skip remote heap dumps as it is non-contiguous\n");
  4218. }
  4219. }
  4220. }
  4221. if (dump_data->nentries > 0)
  4222. plat_priv->ramdump_info_v2.dump_data_valid = true;
  4223. cnss_pci_set_mhi_state(pci_priv, CNSS_MHI_RDDM_DONE);
  4224. skip_dump:
  4225. complete(&plat_priv->rddm_complete);
  4226. }
  4227. void cnss_pci_clear_dump_info(struct cnss_pci_data *pci_priv)
  4228. {
  4229. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  4230. struct cnss_dump_seg *dump_seg =
  4231. plat_priv->ramdump_info_v2.dump_data_vaddr;
  4232. struct image_info *fw_image, *rddm_image;
  4233. struct cnss_fw_mem *fw_mem = plat_priv->fw_mem;
  4234. int i, j;
  4235. if (!dump_seg)
  4236. return;
  4237. fw_image = pci_priv->mhi_ctrl->fbc_image;
  4238. rddm_image = pci_priv->mhi_ctrl->rddm_image;
  4239. for (i = 0; i < fw_image->entries; i++) {
  4240. cnss_pci_remove_dump_seg(pci_priv, dump_seg, CNSS_FW_IMAGE, i,
  4241. fw_image->mhi_buf[i].buf,
  4242. fw_image->mhi_buf[i].dma_addr,
  4243. fw_image->mhi_buf[i].len);
  4244. dump_seg++;
  4245. }
  4246. for (i = 0; i < rddm_image->entries; i++) {
  4247. cnss_pci_remove_dump_seg(pci_priv, dump_seg, CNSS_FW_RDDM, i,
  4248. rddm_image->mhi_buf[i].buf,
  4249. rddm_image->mhi_buf[i].dma_addr,
  4250. rddm_image->mhi_buf[i].len);
  4251. dump_seg++;
  4252. }
  4253. for (i = 0, j = 0; i < plat_priv->fw_mem_seg_len; i++) {
  4254. if (fw_mem[i].type == CNSS_MEM_TYPE_DDR &&
  4255. (fw_mem[i].attrs & DMA_ATTR_FORCE_CONTIGUOUS)) {
  4256. cnss_pci_remove_dump_seg(pci_priv, dump_seg,
  4257. CNSS_FW_REMOTE_HEAP, j,
  4258. fw_mem[i].va, fw_mem[i].pa,
  4259. fw_mem[i].size);
  4260. dump_seg++;
  4261. j++;
  4262. }
  4263. }
  4264. plat_priv->ramdump_info_v2.dump_data.nentries = 0;
  4265. plat_priv->ramdump_info_v2.dump_data_valid = false;
  4266. }
  4267. void cnss_pci_device_crashed(struct cnss_pci_data *pci_priv)
  4268. {
  4269. if (!pci_priv)
  4270. return;
  4271. cnss_device_crashed(&pci_priv->pci_dev->dev);
  4272. }
  4273. static int cnss_mhi_pm_runtime_get(struct mhi_controller *mhi_ctrl)
  4274. {
  4275. struct cnss_pci_data *pci_priv = dev_get_drvdata(mhi_ctrl->cntrl_dev);
  4276. return cnss_pci_pm_runtime_get(pci_priv, RTPM_ID_MHI);
  4277. }
  4278. static void cnss_mhi_pm_runtime_put_noidle(struct mhi_controller *mhi_ctrl)
  4279. {
  4280. struct cnss_pci_data *pci_priv = dev_get_drvdata(mhi_ctrl->cntrl_dev);
  4281. cnss_pci_pm_runtime_put_noidle(pci_priv, RTPM_ID_MHI);
  4282. }
  4283. void cnss_pci_add_fw_prefix_name(struct cnss_pci_data *pci_priv,
  4284. char *prefix_name, char *name)
  4285. {
  4286. struct cnss_plat_data *plat_priv;
  4287. if (!pci_priv)
  4288. return;
  4289. plat_priv = pci_priv->plat_priv;
  4290. if (!plat_priv->use_fw_path_with_prefix) {
  4291. scnprintf(prefix_name, MAX_FIRMWARE_NAME_LEN, "%s", name);
  4292. return;
  4293. }
  4294. switch (pci_priv->device_id) {
  4295. case QCA6390_DEVICE_ID:
  4296. scnprintf(prefix_name, MAX_FIRMWARE_NAME_LEN,
  4297. QCA6390_PATH_PREFIX "%s", name);
  4298. break;
  4299. case QCA6490_DEVICE_ID:
  4300. scnprintf(prefix_name, MAX_FIRMWARE_NAME_LEN,
  4301. QCA6490_PATH_PREFIX "%s", name);
  4302. break;
  4303. case KIWI_DEVICE_ID:
  4304. scnprintf(prefix_name, MAX_FIRMWARE_NAME_LEN,
  4305. KIWI_PATH_PREFIX "%s", name);
  4306. break;
  4307. case MANGO_DEVICE_ID:
  4308. scnprintf(prefix_name, MAX_FIRMWARE_NAME_LEN,
  4309. MANGO_PATH_PREFIX "%s", name);
  4310. break;
  4311. default:
  4312. scnprintf(prefix_name, MAX_FIRMWARE_NAME_LEN, "%s", name);
  4313. break;
  4314. }
  4315. cnss_pr_dbg("FW name added with prefix: %s\n", prefix_name);
  4316. }
  4317. static int cnss_pci_update_fw_name(struct cnss_pci_data *pci_priv)
  4318. {
  4319. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  4320. switch (pci_priv->device_id) {
  4321. case QCA6390_DEVICE_ID:
  4322. if (plat_priv->device_version.major_version < FW_V2_NUMBER) {
  4323. cnss_pr_dbg("Device ID:version (0x%lx:%d) is not supported\n",
  4324. pci_priv->device_id,
  4325. plat_priv->device_version.major_version);
  4326. return -EINVAL;
  4327. }
  4328. cnss_pci_add_fw_prefix_name(pci_priv, plat_priv->firmware_name,
  4329. FW_V2_FILE_NAME);
  4330. snprintf(plat_priv->fw_fallback_name, MAX_FIRMWARE_NAME_LEN,
  4331. FW_V2_FILE_NAME);
  4332. break;
  4333. case QCA6490_DEVICE_ID:
  4334. switch (plat_priv->device_version.major_version) {
  4335. case FW_V2_NUMBER:
  4336. cnss_pci_add_fw_prefix_name(pci_priv,
  4337. plat_priv->firmware_name,
  4338. FW_V2_FILE_NAME);
  4339. snprintf(plat_priv->fw_fallback_name,
  4340. MAX_FIRMWARE_NAME_LEN,
  4341. FW_V2_FILE_NAME);
  4342. break;
  4343. default:
  4344. cnss_pci_add_fw_prefix_name(pci_priv,
  4345. plat_priv->firmware_name,
  4346. DEFAULT_FW_FILE_NAME);
  4347. snprintf(plat_priv->fw_fallback_name,
  4348. MAX_FIRMWARE_NAME_LEN,
  4349. DEFAULT_FW_FILE_NAME);
  4350. break;
  4351. }
  4352. break;
  4353. case KIWI_DEVICE_ID:
  4354. case MANGO_DEVICE_ID:
  4355. switch (plat_priv->device_version.major_version) {
  4356. case FW_V2_NUMBER:
  4357. /*
  4358. * kiwiv2 using seprate fw binary for MM and FTM mode,
  4359. * platform driver loads corresponding binary according
  4360. * to current mode indicated by wlan driver. Otherwise
  4361. * use default binary.
  4362. * Mission mode using same binary name as before,
  4363. * if seprate binary is not there, fall back to default.
  4364. */
  4365. if (plat_priv->driver_mode == CNSS_MISSION) {
  4366. cnss_pci_add_fw_prefix_name(pci_priv,
  4367. plat_priv->firmware_name,
  4368. FW_V2_FILE_NAME);
  4369. cnss_pci_add_fw_prefix_name(pci_priv,
  4370. plat_priv->fw_fallback_name,
  4371. FW_V2_FILE_NAME);
  4372. } else if (plat_priv->driver_mode == CNSS_FTM) {
  4373. cnss_pci_add_fw_prefix_name(pci_priv,
  4374. plat_priv->firmware_name,
  4375. FW_V2_FTM_FILE_NAME);
  4376. cnss_pci_add_fw_prefix_name(pci_priv,
  4377. plat_priv->fw_fallback_name,
  4378. FW_V2_FILE_NAME);
  4379. } else {
  4380. /*
  4381. * Since during cold boot calibration phase,
  4382. * wlan driver has not registered, so default
  4383. * fw binary will be used.
  4384. */
  4385. cnss_pci_add_fw_prefix_name(pci_priv,
  4386. plat_priv->firmware_name,
  4387. FW_V2_FILE_NAME);
  4388. snprintf(plat_priv->fw_fallback_name,
  4389. MAX_FIRMWARE_NAME_LEN,
  4390. FW_V2_FILE_NAME);
  4391. }
  4392. break;
  4393. default:
  4394. cnss_pci_add_fw_prefix_name(pci_priv,
  4395. plat_priv->firmware_name,
  4396. DEFAULT_FW_FILE_NAME);
  4397. snprintf(plat_priv->fw_fallback_name,
  4398. MAX_FIRMWARE_NAME_LEN,
  4399. DEFAULT_FW_FILE_NAME);
  4400. break;
  4401. }
  4402. break;
  4403. default:
  4404. cnss_pci_add_fw_prefix_name(pci_priv, plat_priv->firmware_name,
  4405. DEFAULT_FW_FILE_NAME);
  4406. snprintf(plat_priv->fw_fallback_name, MAX_FIRMWARE_NAME_LEN,
  4407. DEFAULT_FW_FILE_NAME);
  4408. break;
  4409. }
  4410. cnss_pr_dbg("FW name is %s, FW fallback name is %s\n",
  4411. plat_priv->firmware_name, plat_priv->fw_fallback_name);
  4412. return 0;
  4413. }
  4414. static char *cnss_mhi_notify_status_to_str(enum mhi_callback status)
  4415. {
  4416. switch (status) {
  4417. case MHI_CB_IDLE:
  4418. return "IDLE";
  4419. case MHI_CB_EE_RDDM:
  4420. return "RDDM";
  4421. case MHI_CB_SYS_ERROR:
  4422. return "SYS_ERROR";
  4423. case MHI_CB_FATAL_ERROR:
  4424. return "FATAL_ERROR";
  4425. case MHI_CB_EE_MISSION_MODE:
  4426. return "MISSION_MODE";
  4427. #if IS_ENABLED(CONFIG_MHI_BUS_MISC)
  4428. case MHI_CB_FALLBACK_IMG:
  4429. return "FW_FALLBACK";
  4430. #endif
  4431. default:
  4432. return "UNKNOWN";
  4433. }
  4434. };
  4435. static void cnss_dev_rddm_timeout_hdlr(struct timer_list *t)
  4436. {
  4437. struct cnss_pci_data *pci_priv =
  4438. from_timer(pci_priv, t, dev_rddm_timer);
  4439. enum mhi_ee_type mhi_ee;
  4440. if (!pci_priv)
  4441. return;
  4442. cnss_fatal_err("Timeout waiting for RDDM notification\n");
  4443. if (!cnss_pci_assert_host_sol(pci_priv))
  4444. return;
  4445. mhi_ee = mhi_get_exec_env(pci_priv->mhi_ctrl);
  4446. if (mhi_ee == MHI_EE_PBL)
  4447. cnss_pr_err("Unable to collect ramdumps due to abrupt reset\n");
  4448. if (mhi_ee == MHI_EE_RDDM) {
  4449. cnss_pr_info("Device MHI EE is RDDM, try to collect dump\n");
  4450. cnss_schedule_recovery(&pci_priv->pci_dev->dev,
  4451. CNSS_REASON_RDDM);
  4452. } else {
  4453. cnss_mhi_debug_reg_dump(pci_priv);
  4454. cnss_pci_soc_scratch_reg_dump(pci_priv);
  4455. cnss_schedule_recovery(&pci_priv->pci_dev->dev,
  4456. CNSS_REASON_TIMEOUT);
  4457. }
  4458. }
  4459. static void cnss_boot_debug_timeout_hdlr(struct timer_list *t)
  4460. {
  4461. struct cnss_pci_data *pci_priv =
  4462. from_timer(pci_priv, t, boot_debug_timer);
  4463. if (!pci_priv)
  4464. return;
  4465. if (cnss_pci_check_link_status(pci_priv))
  4466. return;
  4467. if (cnss_pci_is_device_down(&pci_priv->pci_dev->dev))
  4468. return;
  4469. if (test_bit(CNSS_MHI_POWER_ON, &pci_priv->mhi_state))
  4470. return;
  4471. if (cnss_mhi_scan_rddm_cookie(pci_priv, DEVICE_RDDM_COOKIE))
  4472. return;
  4473. cnss_pr_dbg("Dump MHI/PBL/SBL debug data every %ds during MHI power on\n",
  4474. BOOT_DEBUG_TIMEOUT_MS / 1000);
  4475. cnss_mhi_debug_reg_dump(pci_priv);
  4476. cnss_pci_soc_scratch_reg_dump(pci_priv);
  4477. cnss_pci_dump_bl_sram_mem(pci_priv);
  4478. mod_timer(&pci_priv->boot_debug_timer,
  4479. jiffies + msecs_to_jiffies(BOOT_DEBUG_TIMEOUT_MS));
  4480. }
  4481. static int cnss_pci_handle_mhi_sys_err(struct cnss_pci_data *pci_priv)
  4482. {
  4483. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  4484. cnss_ignore_qmi_failure(true);
  4485. set_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state);
  4486. del_timer(&plat_priv->fw_boot_timer);
  4487. mod_timer(&pci_priv->dev_rddm_timer,
  4488. jiffies + msecs_to_jiffies(DEV_RDDM_TIMEOUT));
  4489. cnss_pci_update_status(pci_priv, CNSS_FW_DOWN);
  4490. return 0;
  4491. }
  4492. int cnss_pci_handle_dev_sol_irq(struct cnss_pci_data *pci_priv)
  4493. {
  4494. return cnss_pci_handle_mhi_sys_err(pci_priv);
  4495. }
  4496. static void cnss_mhi_notify_status(struct mhi_controller *mhi_ctrl,
  4497. enum mhi_callback reason)
  4498. {
  4499. struct cnss_pci_data *pci_priv = dev_get_drvdata(mhi_ctrl->cntrl_dev);
  4500. struct cnss_plat_data *plat_priv;
  4501. enum cnss_recovery_reason cnss_reason;
  4502. if (!pci_priv) {
  4503. cnss_pr_err("pci_priv is NULL");
  4504. return;
  4505. }
  4506. plat_priv = pci_priv->plat_priv;
  4507. if (reason != MHI_CB_IDLE)
  4508. cnss_pr_dbg("MHI status cb is called with reason %s(%d)\n",
  4509. cnss_mhi_notify_status_to_str(reason), reason);
  4510. switch (reason) {
  4511. case MHI_CB_IDLE:
  4512. case MHI_CB_EE_MISSION_MODE:
  4513. return;
  4514. case MHI_CB_FATAL_ERROR:
  4515. cnss_ignore_qmi_failure(true);
  4516. set_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state);
  4517. del_timer(&plat_priv->fw_boot_timer);
  4518. cnss_pci_update_status(pci_priv, CNSS_FW_DOWN);
  4519. cnss_reason = CNSS_REASON_DEFAULT;
  4520. break;
  4521. case MHI_CB_SYS_ERROR:
  4522. cnss_pci_handle_mhi_sys_err(pci_priv);
  4523. return;
  4524. case MHI_CB_EE_RDDM:
  4525. cnss_ignore_qmi_failure(true);
  4526. set_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state);
  4527. del_timer(&plat_priv->fw_boot_timer);
  4528. del_timer(&pci_priv->dev_rddm_timer);
  4529. cnss_pci_update_status(pci_priv, CNSS_FW_DOWN);
  4530. cnss_reason = CNSS_REASON_RDDM;
  4531. break;
  4532. #if IS_ENABLED(CONFIG_MHI_BUS_MISC)
  4533. case MHI_CB_FALLBACK_IMG:
  4534. /* for kiwi_v2 binary fallback is used, skip path fallback here */
  4535. if (!(pci_priv->device_id == KIWI_DEVICE_ID &&
  4536. plat_priv->device_version.major_version == FW_V2_NUMBER)) {
  4537. plat_priv->use_fw_path_with_prefix = false;
  4538. cnss_pci_update_fw_name(pci_priv);
  4539. }
  4540. return;
  4541. #endif
  4542. default:
  4543. cnss_pr_err("Unsupported MHI status cb reason: %d\n", reason);
  4544. return;
  4545. }
  4546. cnss_schedule_recovery(&pci_priv->pci_dev->dev, cnss_reason);
  4547. }
  4548. static int cnss_pci_get_mhi_msi(struct cnss_pci_data *pci_priv)
  4549. {
  4550. int ret, num_vectors, i;
  4551. u32 user_base_data, base_vector;
  4552. int *irq;
  4553. ret = cnss_get_user_msi_assignment(&pci_priv->pci_dev->dev,
  4554. MHI_MSI_NAME, &num_vectors,
  4555. &user_base_data, &base_vector);
  4556. if (ret)
  4557. return ret;
  4558. cnss_pr_dbg("Number of assigned MSI for MHI is %d, base vector is %d\n",
  4559. num_vectors, base_vector);
  4560. irq = kcalloc(num_vectors, sizeof(int), GFP_KERNEL);
  4561. if (!irq)
  4562. return -ENOMEM;
  4563. for (i = 0; i < num_vectors; i++)
  4564. irq[i] = cnss_get_msi_irq(&pci_priv->pci_dev->dev,
  4565. base_vector + i);
  4566. pci_priv->mhi_ctrl->irq = irq;
  4567. pci_priv->mhi_ctrl->nr_irqs = num_vectors;
  4568. return 0;
  4569. }
  4570. static int cnss_mhi_bw_scale(struct mhi_controller *mhi_ctrl,
  4571. struct mhi_link_info *link_info)
  4572. {
  4573. struct cnss_pci_data *pci_priv = dev_get_drvdata(mhi_ctrl->cntrl_dev);
  4574. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  4575. int ret = 0;
  4576. cnss_pr_dbg("Setting link speed:0x%x, width:0x%x\n",
  4577. link_info->target_link_speed,
  4578. link_info->target_link_width);
  4579. /* It has to set target link speed here before setting link bandwidth
  4580. * when device requests link speed change. This can avoid setting link
  4581. * bandwidth getting rejected if requested link speed is higher than
  4582. * current one.
  4583. */
  4584. ret = cnss_pci_set_max_link_speed(pci_priv, plat_priv->rc_num,
  4585. link_info->target_link_speed);
  4586. if (ret)
  4587. cnss_pr_err("Failed to set target link speed to 0x%x, err = %d\n",
  4588. link_info->target_link_speed, ret);
  4589. ret = cnss_pci_set_link_bandwidth(pci_priv,
  4590. link_info->target_link_speed,
  4591. link_info->target_link_width);
  4592. if (ret) {
  4593. cnss_pr_err("Failed to set link bandwidth, err = %d\n", ret);
  4594. return ret;
  4595. }
  4596. pci_priv->def_link_speed = link_info->target_link_speed;
  4597. pci_priv->def_link_width = link_info->target_link_width;
  4598. return 0;
  4599. }
  4600. static int cnss_mhi_read_reg(struct mhi_controller *mhi_ctrl,
  4601. void __iomem *addr, u32 *out)
  4602. {
  4603. struct cnss_pci_data *pci_priv = dev_get_drvdata(mhi_ctrl->cntrl_dev);
  4604. u32 tmp = readl_relaxed(addr);
  4605. /* Unexpected value, query the link status */
  4606. if (PCI_INVALID_READ(tmp) &&
  4607. cnss_pci_check_link_status(pci_priv))
  4608. return -EIO;
  4609. *out = tmp;
  4610. return 0;
  4611. }
  4612. static void cnss_mhi_write_reg(struct mhi_controller *mhi_ctrl,
  4613. void __iomem *addr, u32 val)
  4614. {
  4615. writel_relaxed(val, addr);
  4616. }
  4617. static int cnss_get_mhi_soc_info(struct cnss_plat_data *plat_priv,
  4618. struct mhi_controller *mhi_ctrl)
  4619. {
  4620. int ret = 0;
  4621. ret = mhi_get_soc_info(mhi_ctrl);
  4622. if (ret)
  4623. goto exit;
  4624. plat_priv->device_version.family_number = mhi_ctrl->family_number;
  4625. plat_priv->device_version.device_number = mhi_ctrl->device_number;
  4626. plat_priv->device_version.major_version = mhi_ctrl->major_version;
  4627. plat_priv->device_version.minor_version = mhi_ctrl->minor_version;
  4628. cnss_pr_dbg("Get device version info, family number: 0x%x, device number: 0x%x, major version: 0x%x, minor version: 0x%x\n",
  4629. plat_priv->device_version.family_number,
  4630. plat_priv->device_version.device_number,
  4631. plat_priv->device_version.major_version,
  4632. plat_priv->device_version.minor_version);
  4633. /* Only keep lower 4 bits as real device major version */
  4634. plat_priv->device_version.major_version &= DEVICE_MAJOR_VERSION_MASK;
  4635. exit:
  4636. return ret;
  4637. }
  4638. static int cnss_pci_register_mhi(struct cnss_pci_data *pci_priv)
  4639. {
  4640. int ret = 0;
  4641. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  4642. struct pci_dev *pci_dev = pci_priv->pci_dev;
  4643. struct mhi_controller *mhi_ctrl;
  4644. phys_addr_t bar_start;
  4645. const struct mhi_controller_config *cnss_mhi_config =
  4646. &cnss_mhi_config_default;
  4647. if (pci_priv->device_id == QCA6174_DEVICE_ID)
  4648. return 0;
  4649. mhi_ctrl = mhi_alloc_controller();
  4650. if (!mhi_ctrl) {
  4651. cnss_pr_err("Invalid MHI controller context\n");
  4652. return -EINVAL;
  4653. }
  4654. pci_priv->mhi_ctrl = mhi_ctrl;
  4655. mhi_ctrl->cntrl_dev = &pci_dev->dev;
  4656. mhi_ctrl->fw_image = plat_priv->firmware_name;
  4657. #if IS_ENABLED(CONFIG_MHI_BUS_MISC)
  4658. mhi_ctrl->fallback_fw_image = plat_priv->fw_fallback_name;
  4659. #endif
  4660. mhi_ctrl->regs = pci_priv->bar;
  4661. mhi_ctrl->reg_len = pci_resource_len(pci_priv->pci_dev, PCI_BAR_NUM);
  4662. bar_start = pci_resource_start(pci_priv->pci_dev, PCI_BAR_NUM);
  4663. cnss_pr_dbg("BAR starts at %pa, length is %x\n",
  4664. &bar_start, mhi_ctrl->reg_len);
  4665. ret = cnss_pci_get_mhi_msi(pci_priv);
  4666. if (ret) {
  4667. cnss_pr_err("Failed to get MSI for MHI, err = %d\n", ret);
  4668. goto free_mhi_ctrl;
  4669. }
  4670. if (pci_priv->smmu_s1_enable) {
  4671. mhi_ctrl->iova_start = pci_priv->smmu_iova_start;
  4672. mhi_ctrl->iova_stop = pci_priv->smmu_iova_start +
  4673. pci_priv->smmu_iova_len;
  4674. } else {
  4675. mhi_ctrl->iova_start = 0;
  4676. mhi_ctrl->iova_stop = pci_priv->dma_bit_mask;
  4677. }
  4678. mhi_ctrl->status_cb = cnss_mhi_notify_status;
  4679. mhi_ctrl->runtime_get = cnss_mhi_pm_runtime_get;
  4680. mhi_ctrl->runtime_put = cnss_mhi_pm_runtime_put_noidle;
  4681. mhi_ctrl->read_reg = cnss_mhi_read_reg;
  4682. mhi_ctrl->write_reg = cnss_mhi_write_reg;
  4683. mhi_ctrl->rddm_size = pci_priv->plat_priv->ramdump_info_v2.ramdump_size;
  4684. if (!mhi_ctrl->rddm_size)
  4685. mhi_ctrl->rddm_size = RAMDUMP_SIZE_DEFAULT;
  4686. mhi_ctrl->sbl_size = SZ_512K;
  4687. mhi_ctrl->seg_len = SZ_512K;
  4688. mhi_ctrl->fbc_download = true;
  4689. ret = cnss_get_mhi_soc_info(plat_priv, mhi_ctrl);
  4690. if (ret)
  4691. goto free_mhi_irq;
  4692. /* Satellite config only supported on KIWI V2 and later chipset */
  4693. if (plat_priv->device_id <= QCA6490_DEVICE_ID ||
  4694. (plat_priv->device_id == KIWI_DEVICE_ID &&
  4695. plat_priv->device_version.major_version == 1))
  4696. cnss_mhi_config = &cnss_mhi_config_no_satellite;
  4697. ret = mhi_register_controller(mhi_ctrl, cnss_mhi_config);
  4698. if (ret) {
  4699. cnss_pr_err("Failed to register to MHI bus, err = %d\n", ret);
  4700. goto free_mhi_irq;
  4701. }
  4702. /* MHI satellite driver only needs to connect when DRV is supported */
  4703. if (cnss_pci_is_drv_supported(pci_priv))
  4704. cnss_mhi_controller_set_base(pci_priv, bar_start);
  4705. /* BW scale CB needs to be set after registering MHI per requirement */
  4706. cnss_mhi_controller_set_bw_scale_cb(pci_priv, cnss_mhi_bw_scale);
  4707. ret = cnss_pci_update_fw_name(pci_priv);
  4708. if (ret)
  4709. goto unreg_mhi;
  4710. return 0;
  4711. unreg_mhi:
  4712. mhi_unregister_controller(mhi_ctrl);
  4713. free_mhi_irq:
  4714. kfree(mhi_ctrl->irq);
  4715. free_mhi_ctrl:
  4716. mhi_free_controller(mhi_ctrl);
  4717. return ret;
  4718. }
  4719. static void cnss_pci_unregister_mhi(struct cnss_pci_data *pci_priv)
  4720. {
  4721. struct mhi_controller *mhi_ctrl = pci_priv->mhi_ctrl;
  4722. if (pci_priv->device_id == QCA6174_DEVICE_ID)
  4723. return;
  4724. mhi_unregister_controller(mhi_ctrl);
  4725. kfree(mhi_ctrl->irq);
  4726. mhi_free_controller(mhi_ctrl);
  4727. }
  4728. static void cnss_pci_config_regs(struct cnss_pci_data *pci_priv)
  4729. {
  4730. switch (pci_priv->device_id) {
  4731. case QCA6390_DEVICE_ID:
  4732. pci_priv->misc_reg_dev_mask = REG_MASK_QCA6390;
  4733. pci_priv->wcss_reg = wcss_reg_access_seq;
  4734. pci_priv->pcie_reg = pcie_reg_access_seq;
  4735. pci_priv->wlaon_reg = wlaon_reg_access_seq;
  4736. pci_priv->syspm_reg = syspm_reg_access_seq;
  4737. /* Configure WDOG register with specific value so that we can
  4738. * know if HW is in the process of WDOG reset recovery or not
  4739. * when reading the registers.
  4740. */
  4741. cnss_pci_reg_write
  4742. (pci_priv,
  4743. QCA6390_PCIE_SOC_WDOG_DISC_BAD_DATA_LOW_CFG_SOC_PCIE_REG,
  4744. QCA6390_PCIE_SOC_WDOG_DISC_BAD_DATA_LOW_CFG_SOC_PCIE_REG_VAL);
  4745. break;
  4746. case QCA6490_DEVICE_ID:
  4747. pci_priv->misc_reg_dev_mask = REG_MASK_QCA6490;
  4748. pci_priv->wlaon_reg = wlaon_reg_access_seq;
  4749. break;
  4750. default:
  4751. return;
  4752. }
  4753. }
  4754. #if !IS_ENABLED(CONFIG_ARCH_QCOM)
  4755. static int cnss_pci_of_reserved_mem_device_init(struct cnss_pci_data *pci_priv)
  4756. {
  4757. return 0;
  4758. }
  4759. static irqreturn_t cnss_pci_wake_handler(int irq, void *data)
  4760. {
  4761. struct cnss_pci_data *pci_priv = data;
  4762. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  4763. enum rpm_status status;
  4764. struct device *dev;
  4765. pci_priv->wake_counter++;
  4766. cnss_pr_dbg("WLAN PCI wake IRQ (%u) is asserted #%u\n",
  4767. pci_priv->wake_irq, pci_priv->wake_counter);
  4768. /* Make sure abort current suspend */
  4769. cnss_pm_stay_awake(plat_priv);
  4770. cnss_pm_relax(plat_priv);
  4771. /* Above two pm* API calls will abort system suspend only when
  4772. * plat_dev->dev->ws is initiated by device_init_wakeup() API, and
  4773. * calling pm_system_wakeup() is just to guarantee system suspend
  4774. * can be aborted if it is not initiated in any case.
  4775. */
  4776. pm_system_wakeup();
  4777. dev = &pci_priv->pci_dev->dev;
  4778. status = dev->power.runtime_status;
  4779. if ((cnss_pci_get_monitor_wake_intr(pci_priv) &&
  4780. cnss_pci_get_auto_suspended(pci_priv)) ||
  4781. (status == RPM_SUSPENDING || status == RPM_SUSPENDED)) {
  4782. cnss_pci_set_monitor_wake_intr(pci_priv, false);
  4783. cnss_pci_pm_request_resume(pci_priv);
  4784. }
  4785. return IRQ_HANDLED;
  4786. }
  4787. /**
  4788. * cnss_pci_wake_gpio_init() - Setup PCI wake GPIO for WLAN
  4789. * @pci_priv: driver PCI bus context pointer
  4790. *
  4791. * This function initializes WLAN PCI wake GPIO and corresponding
  4792. * interrupt. It should be used in non-MSM platforms whose PCIe
  4793. * root complex driver doesn't handle the GPIO.
  4794. *
  4795. * Return: 0 for success or skip, negative value for error
  4796. */
  4797. static int cnss_pci_wake_gpio_init(struct cnss_pci_data *pci_priv)
  4798. {
  4799. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  4800. struct device *dev = &plat_priv->plat_dev->dev;
  4801. int ret = 0;
  4802. pci_priv->wake_gpio = of_get_named_gpio(dev->of_node,
  4803. "wlan-pci-wake-gpio", 0);
  4804. if (pci_priv->wake_gpio < 0)
  4805. goto out;
  4806. cnss_pr_dbg("Get PCI wake GPIO (%d) from device node\n",
  4807. pci_priv->wake_gpio);
  4808. ret = gpio_request(pci_priv->wake_gpio, "wlan_pci_wake_gpio");
  4809. if (ret) {
  4810. cnss_pr_err("Failed to request PCI wake GPIO, err = %d\n",
  4811. ret);
  4812. goto out;
  4813. }
  4814. gpio_direction_input(pci_priv->wake_gpio);
  4815. pci_priv->wake_irq = gpio_to_irq(pci_priv->wake_gpio);
  4816. ret = request_irq(pci_priv->wake_irq, cnss_pci_wake_handler,
  4817. IRQF_TRIGGER_FALLING, "wlan_pci_wake_irq", pci_priv);
  4818. if (ret) {
  4819. cnss_pr_err("Failed to request PCI wake IRQ, err = %d\n", ret);
  4820. goto free_gpio;
  4821. }
  4822. ret = enable_irq_wake(pci_priv->wake_irq);
  4823. if (ret) {
  4824. cnss_pr_err("Failed to enable PCI wake IRQ, err = %d\n", ret);
  4825. goto free_irq;
  4826. }
  4827. return 0;
  4828. free_irq:
  4829. free_irq(pci_priv->wake_irq, pci_priv);
  4830. free_gpio:
  4831. gpio_free(pci_priv->wake_gpio);
  4832. out:
  4833. return ret;
  4834. }
  4835. static void cnss_pci_wake_gpio_deinit(struct cnss_pci_data *pci_priv)
  4836. {
  4837. if (pci_priv->wake_gpio < 0)
  4838. return;
  4839. disable_irq_wake(pci_priv->wake_irq);
  4840. free_irq(pci_priv->wake_irq, pci_priv);
  4841. gpio_free(pci_priv->wake_gpio);
  4842. }
  4843. #endif
  4844. /* Setting to use this cnss_pm_domain ops will let PM framework override the
  4845. * ops from dev->bus->pm which is pci_dev_pm_ops from pci-driver.c. This ops
  4846. * has to take care everything device driver needed which is currently done
  4847. * from pci_dev_pm_ops.
  4848. */
  4849. static struct dev_pm_domain cnss_pm_domain = {
  4850. .ops = {
  4851. SET_SYSTEM_SLEEP_PM_OPS(cnss_pci_suspend, cnss_pci_resume)
  4852. SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(cnss_pci_suspend_noirq,
  4853. cnss_pci_resume_noirq)
  4854. SET_RUNTIME_PM_OPS(cnss_pci_runtime_suspend,
  4855. cnss_pci_runtime_resume,
  4856. cnss_pci_runtime_idle)
  4857. }
  4858. };
  4859. static int cnss_pci_probe(struct pci_dev *pci_dev,
  4860. const struct pci_device_id *id)
  4861. {
  4862. int ret = 0;
  4863. struct cnss_pci_data *pci_priv;
  4864. struct cnss_plat_data *plat_priv = cnss_bus_dev_to_plat_priv(NULL);
  4865. struct device *dev = &pci_dev->dev;
  4866. cnss_pr_dbg("PCI is probing, vendor ID: 0x%x, device ID: 0x%x\n",
  4867. id->vendor, pci_dev->device);
  4868. pci_priv = devm_kzalloc(dev, sizeof(*pci_priv), GFP_KERNEL);
  4869. if (!pci_priv) {
  4870. ret = -ENOMEM;
  4871. goto out;
  4872. }
  4873. pci_priv->pci_link_state = PCI_LINK_UP;
  4874. pci_priv->plat_priv = plat_priv;
  4875. pci_priv->pci_dev = pci_dev;
  4876. pci_priv->pci_device_id = id;
  4877. pci_priv->device_id = pci_dev->device;
  4878. cnss_set_pci_priv(pci_dev, pci_priv);
  4879. plat_priv->device_id = pci_dev->device;
  4880. plat_priv->bus_priv = pci_priv;
  4881. mutex_init(&pci_priv->bus_lock);
  4882. if (plat_priv->use_pm_domain)
  4883. dev->pm_domain = &cnss_pm_domain;
  4884. cnss_pci_of_reserved_mem_device_init(pci_priv);
  4885. ret = cnss_register_subsys(plat_priv);
  4886. if (ret)
  4887. goto reset_ctx;
  4888. ret = cnss_register_ramdump(plat_priv);
  4889. if (ret)
  4890. goto unregister_subsys;
  4891. ret = cnss_pci_init_smmu(pci_priv);
  4892. if (ret)
  4893. goto unregister_ramdump;
  4894. ret = cnss_reg_pci_event(pci_priv);
  4895. if (ret) {
  4896. cnss_pr_err("Failed to register PCI event, err = %d\n", ret);
  4897. goto deinit_smmu;
  4898. }
  4899. ret = cnss_pci_enable_bus(pci_priv);
  4900. if (ret)
  4901. goto dereg_pci_event;
  4902. ret = cnss_pci_enable_msi(pci_priv);
  4903. if (ret)
  4904. goto disable_bus;
  4905. ret = cnss_pci_register_mhi(pci_priv);
  4906. if (ret)
  4907. goto disable_msi;
  4908. switch (pci_dev->device) {
  4909. case QCA6174_DEVICE_ID:
  4910. pci_read_config_word(pci_dev, QCA6174_REV_ID_OFFSET,
  4911. &pci_priv->revision_id);
  4912. break;
  4913. case QCA6290_DEVICE_ID:
  4914. case QCA6390_DEVICE_ID:
  4915. case QCA6490_DEVICE_ID:
  4916. case KIWI_DEVICE_ID:
  4917. case MANGO_DEVICE_ID:
  4918. cnss_pci_set_wlaon_pwr_ctrl(pci_priv, false, false, false);
  4919. timer_setup(&pci_priv->dev_rddm_timer,
  4920. cnss_dev_rddm_timeout_hdlr, 0);
  4921. timer_setup(&pci_priv->boot_debug_timer,
  4922. cnss_boot_debug_timeout_hdlr, 0);
  4923. INIT_DELAYED_WORK(&pci_priv->time_sync_work,
  4924. cnss_pci_time_sync_work_hdlr);
  4925. cnss_pci_get_link_status(pci_priv);
  4926. cnss_pci_set_wlaon_pwr_ctrl(pci_priv, false, true, false);
  4927. cnss_pci_wake_gpio_init(pci_priv);
  4928. break;
  4929. default:
  4930. cnss_pr_err("Unknown PCI device found: 0x%x\n",
  4931. pci_dev->device);
  4932. ret = -ENODEV;
  4933. goto unreg_mhi;
  4934. }
  4935. cnss_pci_config_regs(pci_priv);
  4936. if (EMULATION_HW)
  4937. goto out;
  4938. ret = cnss_suspend_pci_link(pci_priv);
  4939. if (ret)
  4940. cnss_pr_err("Failed to suspend PCI link, err = %d\n", ret);
  4941. cnss_power_off_device(plat_priv);
  4942. set_bit(CNSS_PCI_PROBE_DONE, &plat_priv->driver_state);
  4943. return 0;
  4944. unreg_mhi:
  4945. cnss_pci_unregister_mhi(pci_priv);
  4946. disable_msi:
  4947. cnss_pci_disable_msi(pci_priv);
  4948. disable_bus:
  4949. cnss_pci_disable_bus(pci_priv);
  4950. dereg_pci_event:
  4951. cnss_dereg_pci_event(pci_priv);
  4952. deinit_smmu:
  4953. cnss_pci_deinit_smmu(pci_priv);
  4954. unregister_ramdump:
  4955. cnss_unregister_ramdump(plat_priv);
  4956. unregister_subsys:
  4957. cnss_unregister_subsys(plat_priv);
  4958. reset_ctx:
  4959. plat_priv->bus_priv = NULL;
  4960. out:
  4961. return ret;
  4962. }
  4963. static void cnss_pci_remove(struct pci_dev *pci_dev)
  4964. {
  4965. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  4966. struct cnss_plat_data *plat_priv =
  4967. cnss_bus_dev_to_plat_priv(&pci_dev->dev);
  4968. clear_bit(CNSS_PCI_PROBE_DONE, &plat_priv->driver_state);
  4969. cnss_pci_free_m3_mem(pci_priv);
  4970. cnss_pci_free_fw_mem(pci_priv);
  4971. cnss_pci_free_qdss_mem(pci_priv);
  4972. switch (pci_dev->device) {
  4973. case QCA6290_DEVICE_ID:
  4974. case QCA6390_DEVICE_ID:
  4975. case QCA6490_DEVICE_ID:
  4976. case KIWI_DEVICE_ID:
  4977. case MANGO_DEVICE_ID:
  4978. cnss_pci_wake_gpio_deinit(pci_priv);
  4979. del_timer(&pci_priv->boot_debug_timer);
  4980. del_timer(&pci_priv->dev_rddm_timer);
  4981. break;
  4982. default:
  4983. break;
  4984. }
  4985. cnss_pci_unregister_mhi(pci_priv);
  4986. cnss_pci_disable_msi(pci_priv);
  4987. cnss_pci_disable_bus(pci_priv);
  4988. cnss_dereg_pci_event(pci_priv);
  4989. cnss_pci_deinit_smmu(pci_priv);
  4990. if (plat_priv) {
  4991. cnss_unregister_ramdump(plat_priv);
  4992. cnss_unregister_subsys(plat_priv);
  4993. plat_priv->bus_priv = NULL;
  4994. } else {
  4995. cnss_pr_err("Plat_priv is null, Unable to unregister ramdump,subsys\n");
  4996. }
  4997. }
  4998. static const struct pci_device_id cnss_pci_id_table[] = {
  4999. { QCA6174_VENDOR_ID, QCA6174_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID },
  5000. { QCA6290_VENDOR_ID, QCA6290_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID },
  5001. { QCA6390_VENDOR_ID, QCA6390_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID },
  5002. { QCA6490_VENDOR_ID, QCA6490_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID },
  5003. { KIWI_VENDOR_ID, KIWI_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID },
  5004. { MANGO_VENDOR_ID, MANGO_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID },
  5005. { 0 }
  5006. };
  5007. MODULE_DEVICE_TABLE(pci, cnss_pci_id_table);
  5008. static const struct dev_pm_ops cnss_pm_ops = {
  5009. SET_SYSTEM_SLEEP_PM_OPS(cnss_pci_suspend, cnss_pci_resume)
  5010. SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(cnss_pci_suspend_noirq,
  5011. cnss_pci_resume_noirq)
  5012. SET_RUNTIME_PM_OPS(cnss_pci_runtime_suspend, cnss_pci_runtime_resume,
  5013. cnss_pci_runtime_idle)
  5014. };
  5015. struct pci_driver cnss_pci_driver = {
  5016. .name = "cnss_pci",
  5017. .id_table = cnss_pci_id_table,
  5018. .probe = cnss_pci_probe,
  5019. .remove = cnss_pci_remove,
  5020. .driver = {
  5021. .pm = &cnss_pm_ops,
  5022. },
  5023. };
  5024. static int cnss_pci_enumerate(struct cnss_plat_data *plat_priv, u32 rc_num)
  5025. {
  5026. int ret, retry = 0;
  5027. /* Always set initial target PCIe link speed to Gen2 for QCA6490 device
  5028. * since there may be link issues if it boots up with Gen3 link speed.
  5029. * Device is able to change it later at any time. It will be rejected
  5030. * if requested speed is higher than the one specified in PCIe DT.
  5031. */
  5032. if (plat_priv->device_id == QCA6490_DEVICE_ID) {
  5033. ret = cnss_pci_set_max_link_speed(plat_priv->bus_priv, rc_num,
  5034. PCI_EXP_LNKSTA_CLS_5_0GB);
  5035. if (ret && ret != -EPROBE_DEFER)
  5036. cnss_pr_err("Failed to set max PCIe RC%x link speed to Gen2, err = %d\n",
  5037. rc_num, ret);
  5038. }
  5039. cnss_pr_dbg("Trying to enumerate with PCIe RC%x\n", rc_num);
  5040. retry:
  5041. ret = _cnss_pci_enumerate(plat_priv, rc_num);
  5042. if (ret) {
  5043. if (ret == -EPROBE_DEFER) {
  5044. cnss_pr_dbg("PCIe RC driver is not ready, defer probe\n");
  5045. goto out;
  5046. }
  5047. cnss_pr_err("Failed to enable PCIe RC%x, err = %d\n",
  5048. rc_num, ret);
  5049. if (retry++ < LINK_TRAINING_RETRY_MAX_TIMES) {
  5050. cnss_pr_dbg("Retry PCI link training #%d\n", retry);
  5051. goto retry;
  5052. } else {
  5053. goto out;
  5054. }
  5055. }
  5056. plat_priv->rc_num = rc_num;
  5057. out:
  5058. return ret;
  5059. }
  5060. int cnss_pci_init(struct cnss_plat_data *plat_priv)
  5061. {
  5062. struct device *dev = &plat_priv->plat_dev->dev;
  5063. const __be32 *prop;
  5064. int ret = 0, prop_len = 0, rc_count, i;
  5065. prop = of_get_property(dev->of_node, "qcom,wlan-rc-num", &prop_len);
  5066. if (!prop || !prop_len) {
  5067. cnss_pr_err("Failed to get PCIe RC number from DT\n");
  5068. goto out;
  5069. }
  5070. rc_count = prop_len / sizeof(__be32);
  5071. for (i = 0; i < rc_count; i++) {
  5072. ret = cnss_pci_enumerate(plat_priv, be32_to_cpup(&prop[i]));
  5073. if (!ret)
  5074. break;
  5075. else if (ret == -EPROBE_DEFER || (ret && i == rc_count - 1))
  5076. goto out;
  5077. }
  5078. ret = pci_register_driver(&cnss_pci_driver);
  5079. if (ret) {
  5080. cnss_pr_err("Failed to register to PCI framework, err = %d\n",
  5081. ret);
  5082. goto out;
  5083. }
  5084. if (!plat_priv->bus_priv) {
  5085. cnss_pr_err("Failed to probe PCI driver\n");
  5086. ret = -ENODEV;
  5087. goto unreg_pci;
  5088. }
  5089. return 0;
  5090. unreg_pci:
  5091. pci_unregister_driver(&cnss_pci_driver);
  5092. out:
  5093. return ret;
  5094. }
  5095. void cnss_pci_deinit(struct cnss_plat_data *plat_priv)
  5096. {
  5097. pci_unregister_driver(&cnss_pci_driver);
  5098. }