pci.c 161 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105
  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_pci_get_reg_dump(struct device *dev, uint8_t *buffer, uint32_t len)
  1215. {
  1216. struct pci_dev *pci_dev = to_pci_dev(dev);
  1217. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  1218. if (!pci_priv) {
  1219. cnss_pr_err("pci_priv is NULL\n");
  1220. return -ENODEV;
  1221. }
  1222. if (pci_priv->pci_link_state == PCI_LINK_DOWN) {
  1223. cnss_pr_dbg("No PCIe reg dump since PCIe is suspended(D3)\n");
  1224. return -EACCES;
  1225. }
  1226. cnss_pr_dbg("Start to get PCIe reg dump\n");
  1227. return _cnss_pci_get_reg_dump(pci_priv, buffer, len);
  1228. }
  1229. EXPORT_SYMBOL(cnss_pci_get_reg_dump);
  1230. int cnss_pcie_is_device_down(struct cnss_pci_data *pci_priv)
  1231. {
  1232. struct cnss_plat_data *plat_priv;
  1233. if (!pci_priv) {
  1234. cnss_pr_err("pci_priv is NULL\n");
  1235. return -ENODEV;
  1236. }
  1237. plat_priv = pci_priv->plat_priv;
  1238. if (!plat_priv) {
  1239. cnss_pr_err("plat_priv is NULL\n");
  1240. return -ENODEV;
  1241. }
  1242. return test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state) |
  1243. pci_priv->pci_link_down_ind;
  1244. }
  1245. int cnss_pci_is_device_down(struct device *dev)
  1246. {
  1247. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(to_pci_dev(dev));
  1248. return cnss_pcie_is_device_down(pci_priv);
  1249. }
  1250. EXPORT_SYMBOL(cnss_pci_is_device_down);
  1251. void cnss_pci_lock_reg_window(struct device *dev, unsigned long *flags)
  1252. {
  1253. spin_lock_bh(&pci_reg_window_lock);
  1254. }
  1255. EXPORT_SYMBOL(cnss_pci_lock_reg_window);
  1256. void cnss_pci_unlock_reg_window(struct device *dev, unsigned long *flags)
  1257. {
  1258. spin_unlock_bh(&pci_reg_window_lock);
  1259. }
  1260. EXPORT_SYMBOL(cnss_pci_unlock_reg_window);
  1261. int cnss_get_pci_slot(struct device *dev)
  1262. {
  1263. struct pci_dev *pci_dev = to_pci_dev(dev);
  1264. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  1265. struct cnss_plat_data *plat_priv = NULL;
  1266. if (!pci_priv) {
  1267. cnss_pr_err("pci_priv is NULL\n");
  1268. return -EINVAL;
  1269. }
  1270. plat_priv = pci_priv->plat_priv;
  1271. if (!plat_priv) {
  1272. cnss_pr_err("plat_priv is NULL\n");
  1273. return -ENODEV;
  1274. }
  1275. return plat_priv->rc_num;
  1276. }
  1277. EXPORT_SYMBOL(cnss_get_pci_slot);
  1278. /**
  1279. * cnss_pci_dump_bl_sram_mem - Dump WLAN device bootloader debug log
  1280. * @pci_priv: driver PCI bus context pointer
  1281. *
  1282. * Dump primary and secondary bootloader debug log data. For SBL check the
  1283. * log struct address and size for validity.
  1284. *
  1285. * Return: None
  1286. */
  1287. static void cnss_pci_dump_bl_sram_mem(struct cnss_pci_data *pci_priv)
  1288. {
  1289. u32 mem_addr, val, pbl_log_max_size, sbl_log_max_size;
  1290. u32 pbl_log_sram_start;
  1291. u32 pbl_stage, sbl_log_start, sbl_log_size;
  1292. u32 pbl_wlan_boot_cfg, pbl_bootstrap_status;
  1293. u32 pbl_bootstrap_status_reg = PBL_BOOTSTRAP_STATUS;
  1294. u32 sbl_log_def_start = SRAM_START;
  1295. u32 sbl_log_def_end = SRAM_END;
  1296. int i;
  1297. switch (pci_priv->device_id) {
  1298. case QCA6390_DEVICE_ID:
  1299. pbl_log_sram_start = QCA6390_DEBUG_PBL_LOG_SRAM_START;
  1300. pbl_log_max_size = QCA6390_DEBUG_PBL_LOG_SRAM_MAX_SIZE;
  1301. sbl_log_max_size = QCA6390_DEBUG_SBL_LOG_SRAM_MAX_SIZE;
  1302. break;
  1303. case QCA6490_DEVICE_ID:
  1304. pbl_log_sram_start = QCA6490_DEBUG_PBL_LOG_SRAM_START;
  1305. pbl_log_max_size = QCA6490_DEBUG_PBL_LOG_SRAM_MAX_SIZE;
  1306. sbl_log_max_size = QCA6490_DEBUG_SBL_LOG_SRAM_MAX_SIZE;
  1307. break;
  1308. case KIWI_DEVICE_ID:
  1309. pbl_bootstrap_status_reg = KIWI_PBL_BOOTSTRAP_STATUS;
  1310. pbl_log_sram_start = KIWI_DEBUG_PBL_LOG_SRAM_START;
  1311. pbl_log_max_size = KIWI_DEBUG_PBL_LOG_SRAM_MAX_SIZE;
  1312. sbl_log_max_size = KIWI_DEBUG_SBL_LOG_SRAM_MAX_SIZE;
  1313. break;
  1314. case MANGO_DEVICE_ID:
  1315. pbl_bootstrap_status_reg = MANGO_PBL_BOOTSTRAP_STATUS;
  1316. pbl_log_sram_start = MANGO_DEBUG_PBL_LOG_SRAM_START;
  1317. pbl_log_max_size = MANGO_DEBUG_PBL_LOG_SRAM_MAX_SIZE;
  1318. sbl_log_max_size = MANGO_DEBUG_SBL_LOG_SRAM_MAX_SIZE;
  1319. break;
  1320. default:
  1321. return;
  1322. }
  1323. if (cnss_pci_check_link_status(pci_priv))
  1324. return;
  1325. cnss_pci_reg_read(pci_priv, TCSR_PBL_LOGGING_REG, &pbl_stage);
  1326. cnss_pci_reg_read(pci_priv, PCIE_BHI_ERRDBG2_REG, &sbl_log_start);
  1327. cnss_pci_reg_read(pci_priv, PCIE_BHI_ERRDBG3_REG, &sbl_log_size);
  1328. cnss_pci_reg_read(pci_priv, PBL_WLAN_BOOT_CFG, &pbl_wlan_boot_cfg);
  1329. cnss_pci_reg_read(pci_priv, pbl_bootstrap_status_reg,
  1330. &pbl_bootstrap_status);
  1331. cnss_pr_dbg("TCSR_PBL_LOGGING: 0x%08x PCIE_BHI_ERRDBG: Start: 0x%08x Size:0x%08x\n",
  1332. pbl_stage, sbl_log_start, sbl_log_size);
  1333. cnss_pr_dbg("PBL_WLAN_BOOT_CFG: 0x%08x PBL_BOOTSTRAP_STATUS: 0x%08x\n",
  1334. pbl_wlan_boot_cfg, pbl_bootstrap_status);
  1335. cnss_pr_dbg("Dumping PBL log data\n");
  1336. for (i = 0; i < pbl_log_max_size; i += sizeof(val)) {
  1337. mem_addr = pbl_log_sram_start + i;
  1338. if (cnss_pci_reg_read(pci_priv, mem_addr, &val))
  1339. break;
  1340. cnss_pr_dbg("SRAM[0x%x] = 0x%x\n", mem_addr, val);
  1341. }
  1342. sbl_log_size = (sbl_log_size > sbl_log_max_size ?
  1343. sbl_log_max_size : sbl_log_size);
  1344. if (sbl_log_start < sbl_log_def_start ||
  1345. sbl_log_start > sbl_log_def_end ||
  1346. (sbl_log_start + sbl_log_size) > sbl_log_def_end) {
  1347. cnss_pr_err("Invalid SBL log data\n");
  1348. return;
  1349. }
  1350. cnss_pr_dbg("Dumping SBL log data\n");
  1351. for (i = 0; i < sbl_log_size; i += sizeof(val)) {
  1352. mem_addr = sbl_log_start + i;
  1353. if (cnss_pci_reg_read(pci_priv, mem_addr, &val))
  1354. break;
  1355. cnss_pr_dbg("SRAM[0x%x] = 0x%x\n", mem_addr, val);
  1356. }
  1357. }
  1358. static void cnss_pci_dump_sram(struct cnss_pci_data *pci_priv)
  1359. {
  1360. struct cnss_plat_data *plat_priv;
  1361. u32 i, mem_addr;
  1362. u32 *dump_ptr;
  1363. plat_priv = pci_priv->plat_priv;
  1364. if (plat_priv->device_id != QCA6490_DEVICE_ID ||
  1365. cnss_get_host_build_type() != QMI_HOST_BUILD_TYPE_PRIMARY_V01)
  1366. return;
  1367. if (!plat_priv->sram_dump) {
  1368. cnss_pr_err("SRAM dump memory is not allocated\n");
  1369. return;
  1370. }
  1371. if (cnss_pci_check_link_status(pci_priv))
  1372. return;
  1373. cnss_pr_dbg("Dumping SRAM at 0x%lx\n", plat_priv->sram_dump);
  1374. for (i = 0; i < SRAM_DUMP_SIZE; i += sizeof(u32)) {
  1375. mem_addr = SRAM_START + i;
  1376. dump_ptr = (u32 *)(plat_priv->sram_dump + i);
  1377. if (cnss_pci_reg_read(pci_priv, mem_addr, dump_ptr)) {
  1378. cnss_pr_err("SRAM Dump failed at 0x%x\n", mem_addr);
  1379. break;
  1380. }
  1381. /* Relinquish CPU after dumping 256KB chunks*/
  1382. if (!(i % CNSS_256KB_SIZE))
  1383. cond_resched();
  1384. }
  1385. }
  1386. static int cnss_pci_handle_mhi_poweron_timeout(struct cnss_pci_data *pci_priv)
  1387. {
  1388. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  1389. cnss_fatal_err("MHI power up returns timeout\n");
  1390. if (cnss_mhi_scan_rddm_cookie(pci_priv, DEVICE_RDDM_COOKIE) ||
  1391. cnss_get_dev_sol_value(plat_priv) > 0) {
  1392. /* Wait for RDDM if RDDM cookie is set or device SOL GPIO is
  1393. * high. If RDDM times out, PBL/SBL error region may have been
  1394. * erased so no need to dump them either.
  1395. */
  1396. if (!test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state) &&
  1397. !pci_priv->pci_link_down_ind) {
  1398. mod_timer(&pci_priv->dev_rddm_timer,
  1399. jiffies + msecs_to_jiffies(DEV_RDDM_TIMEOUT));
  1400. }
  1401. } else {
  1402. cnss_pr_dbg("RDDM cookie is not set and device SOL is low\n");
  1403. cnss_mhi_debug_reg_dump(pci_priv);
  1404. cnss_pci_soc_scratch_reg_dump(pci_priv);
  1405. /* Dump PBL/SBL error log if RDDM cookie is not set */
  1406. cnss_pci_dump_bl_sram_mem(pci_priv);
  1407. cnss_pci_dump_sram(pci_priv);
  1408. return -ETIMEDOUT;
  1409. }
  1410. return 0;
  1411. }
  1412. static char *cnss_mhi_state_to_str(enum cnss_mhi_state mhi_state)
  1413. {
  1414. switch (mhi_state) {
  1415. case CNSS_MHI_INIT:
  1416. return "INIT";
  1417. case CNSS_MHI_DEINIT:
  1418. return "DEINIT";
  1419. case CNSS_MHI_POWER_ON:
  1420. return "POWER_ON";
  1421. case CNSS_MHI_POWERING_OFF:
  1422. return "POWERING_OFF";
  1423. case CNSS_MHI_POWER_OFF:
  1424. return "POWER_OFF";
  1425. case CNSS_MHI_FORCE_POWER_OFF:
  1426. return "FORCE_POWER_OFF";
  1427. case CNSS_MHI_SUSPEND:
  1428. return "SUSPEND";
  1429. case CNSS_MHI_RESUME:
  1430. return "RESUME";
  1431. case CNSS_MHI_TRIGGER_RDDM:
  1432. return "TRIGGER_RDDM";
  1433. case CNSS_MHI_RDDM_DONE:
  1434. return "RDDM_DONE";
  1435. default:
  1436. return "UNKNOWN";
  1437. }
  1438. };
  1439. static int cnss_pci_check_mhi_state_bit(struct cnss_pci_data *pci_priv,
  1440. enum cnss_mhi_state mhi_state)
  1441. {
  1442. switch (mhi_state) {
  1443. case CNSS_MHI_INIT:
  1444. if (!test_bit(CNSS_MHI_INIT, &pci_priv->mhi_state))
  1445. return 0;
  1446. break;
  1447. case CNSS_MHI_DEINIT:
  1448. case CNSS_MHI_POWER_ON:
  1449. if (test_bit(CNSS_MHI_INIT, &pci_priv->mhi_state) &&
  1450. !test_bit(CNSS_MHI_POWER_ON, &pci_priv->mhi_state))
  1451. return 0;
  1452. break;
  1453. case CNSS_MHI_FORCE_POWER_OFF:
  1454. if (test_bit(CNSS_MHI_POWER_ON, &pci_priv->mhi_state))
  1455. return 0;
  1456. break;
  1457. case CNSS_MHI_POWER_OFF:
  1458. case CNSS_MHI_SUSPEND:
  1459. if (test_bit(CNSS_MHI_POWER_ON, &pci_priv->mhi_state) &&
  1460. !test_bit(CNSS_MHI_SUSPEND, &pci_priv->mhi_state))
  1461. return 0;
  1462. break;
  1463. case CNSS_MHI_RESUME:
  1464. if (test_bit(CNSS_MHI_SUSPEND, &pci_priv->mhi_state))
  1465. return 0;
  1466. break;
  1467. case CNSS_MHI_TRIGGER_RDDM:
  1468. if (test_bit(CNSS_MHI_POWER_ON, &pci_priv->mhi_state) &&
  1469. !test_bit(CNSS_MHI_TRIGGER_RDDM, &pci_priv->mhi_state))
  1470. return 0;
  1471. break;
  1472. case CNSS_MHI_RDDM_DONE:
  1473. return 0;
  1474. default:
  1475. cnss_pr_err("Unhandled MHI state: %s(%d)\n",
  1476. cnss_mhi_state_to_str(mhi_state), mhi_state);
  1477. }
  1478. cnss_pr_err("Cannot set MHI state %s(%d) in current MHI state (0x%lx)\n",
  1479. cnss_mhi_state_to_str(mhi_state), mhi_state,
  1480. pci_priv->mhi_state);
  1481. if (mhi_state != CNSS_MHI_TRIGGER_RDDM)
  1482. CNSS_ASSERT(0);
  1483. return -EINVAL;
  1484. }
  1485. static void cnss_pci_set_mhi_state_bit(struct cnss_pci_data *pci_priv,
  1486. enum cnss_mhi_state mhi_state)
  1487. {
  1488. switch (mhi_state) {
  1489. case CNSS_MHI_INIT:
  1490. set_bit(CNSS_MHI_INIT, &pci_priv->mhi_state);
  1491. break;
  1492. case CNSS_MHI_DEINIT:
  1493. clear_bit(CNSS_MHI_INIT, &pci_priv->mhi_state);
  1494. break;
  1495. case CNSS_MHI_POWER_ON:
  1496. set_bit(CNSS_MHI_POWER_ON, &pci_priv->mhi_state);
  1497. break;
  1498. case CNSS_MHI_POWERING_OFF:
  1499. set_bit(CNSS_MHI_POWERING_OFF, &pci_priv->mhi_state);
  1500. break;
  1501. case CNSS_MHI_POWER_OFF:
  1502. case CNSS_MHI_FORCE_POWER_OFF:
  1503. clear_bit(CNSS_MHI_POWER_ON, &pci_priv->mhi_state);
  1504. clear_bit(CNSS_MHI_POWERING_OFF, &pci_priv->mhi_state);
  1505. clear_bit(CNSS_MHI_TRIGGER_RDDM, &pci_priv->mhi_state);
  1506. clear_bit(CNSS_MHI_RDDM_DONE, &pci_priv->mhi_state);
  1507. break;
  1508. case CNSS_MHI_SUSPEND:
  1509. set_bit(CNSS_MHI_SUSPEND, &pci_priv->mhi_state);
  1510. break;
  1511. case CNSS_MHI_RESUME:
  1512. clear_bit(CNSS_MHI_SUSPEND, &pci_priv->mhi_state);
  1513. break;
  1514. case CNSS_MHI_TRIGGER_RDDM:
  1515. set_bit(CNSS_MHI_TRIGGER_RDDM, &pci_priv->mhi_state);
  1516. break;
  1517. case CNSS_MHI_RDDM_DONE:
  1518. set_bit(CNSS_MHI_RDDM_DONE, &pci_priv->mhi_state);
  1519. break;
  1520. default:
  1521. cnss_pr_err("Unhandled MHI state (%d)\n", mhi_state);
  1522. }
  1523. }
  1524. static int cnss_pci_set_mhi_state(struct cnss_pci_data *pci_priv,
  1525. enum cnss_mhi_state mhi_state)
  1526. {
  1527. int ret = 0, retry = 0;
  1528. if (pci_priv->device_id == QCA6174_DEVICE_ID)
  1529. return 0;
  1530. if (mhi_state < 0) {
  1531. cnss_pr_err("Invalid MHI state (%d)\n", mhi_state);
  1532. return -EINVAL;
  1533. }
  1534. ret = cnss_pci_check_mhi_state_bit(pci_priv, mhi_state);
  1535. if (ret)
  1536. goto out;
  1537. cnss_pr_vdbg("Setting MHI state: %s(%d)\n",
  1538. cnss_mhi_state_to_str(mhi_state), mhi_state);
  1539. switch (mhi_state) {
  1540. case CNSS_MHI_INIT:
  1541. ret = mhi_prepare_for_power_up(pci_priv->mhi_ctrl);
  1542. break;
  1543. case CNSS_MHI_DEINIT:
  1544. mhi_unprepare_after_power_down(pci_priv->mhi_ctrl);
  1545. ret = 0;
  1546. break;
  1547. case CNSS_MHI_POWER_ON:
  1548. ret = mhi_sync_power_up(pci_priv->mhi_ctrl);
  1549. #if IS_ENABLED(CONFIG_MHI_BUS_MISC)
  1550. /* Only set img_pre_alloc when power up succeeds */
  1551. if (!ret && !pci_priv->mhi_ctrl->img_pre_alloc) {
  1552. cnss_pr_dbg("Notify MHI to use already allocated images\n");
  1553. pci_priv->mhi_ctrl->img_pre_alloc = true;
  1554. }
  1555. #endif
  1556. break;
  1557. case CNSS_MHI_POWER_OFF:
  1558. mhi_power_down(pci_priv->mhi_ctrl, true);
  1559. ret = 0;
  1560. break;
  1561. case CNSS_MHI_FORCE_POWER_OFF:
  1562. mhi_power_down(pci_priv->mhi_ctrl, false);
  1563. ret = 0;
  1564. break;
  1565. case CNSS_MHI_SUSPEND:
  1566. retry_mhi_suspend:
  1567. mutex_lock(&pci_priv->mhi_ctrl->pm_mutex);
  1568. if (pci_priv->drv_connected_last)
  1569. ret = cnss_mhi_pm_fast_suspend(pci_priv, true);
  1570. else
  1571. ret = mhi_pm_suspend(pci_priv->mhi_ctrl);
  1572. mutex_unlock(&pci_priv->mhi_ctrl->pm_mutex);
  1573. if (ret == -EBUSY && retry++ < MHI_SUSPEND_RETRY_MAX_TIMES) {
  1574. cnss_pr_dbg("Retry MHI suspend #%d\n", retry);
  1575. usleep_range(MHI_SUSPEND_RETRY_DELAY_US,
  1576. MHI_SUSPEND_RETRY_DELAY_US + 1000);
  1577. goto retry_mhi_suspend;
  1578. }
  1579. break;
  1580. case CNSS_MHI_RESUME:
  1581. mutex_lock(&pci_priv->mhi_ctrl->pm_mutex);
  1582. if (pci_priv->drv_connected_last) {
  1583. ret = cnss_pci_prevent_l1(&pci_priv->pci_dev->dev);
  1584. if (ret) {
  1585. mutex_unlock(&pci_priv->mhi_ctrl->pm_mutex);
  1586. break;
  1587. }
  1588. ret = cnss_mhi_pm_fast_resume(pci_priv, true);
  1589. cnss_pci_allow_l1(&pci_priv->pci_dev->dev);
  1590. } else {
  1591. ret = mhi_pm_resume(pci_priv->mhi_ctrl);
  1592. }
  1593. mutex_unlock(&pci_priv->mhi_ctrl->pm_mutex);
  1594. break;
  1595. case CNSS_MHI_TRIGGER_RDDM:
  1596. ret = mhi_force_rddm_mode(pci_priv->mhi_ctrl);
  1597. if (ret) {
  1598. cnss_pr_err("Failed to trigger RDDM, err = %d\n", ret);
  1599. cnss_pr_dbg("Sending host reset req\n");
  1600. ret = cnss_mhi_force_reset(pci_priv);
  1601. }
  1602. break;
  1603. case CNSS_MHI_RDDM_DONE:
  1604. break;
  1605. default:
  1606. cnss_pr_err("Unhandled MHI state (%d)\n", mhi_state);
  1607. ret = -EINVAL;
  1608. }
  1609. if (ret)
  1610. goto out;
  1611. cnss_pci_set_mhi_state_bit(pci_priv, mhi_state);
  1612. return 0;
  1613. out:
  1614. cnss_pr_err("Failed to set MHI state: %s(%d), err = %d\n",
  1615. cnss_mhi_state_to_str(mhi_state), mhi_state, ret);
  1616. return ret;
  1617. }
  1618. int cnss_pci_start_mhi(struct cnss_pci_data *pci_priv)
  1619. {
  1620. int ret = 0;
  1621. struct cnss_plat_data *plat_priv;
  1622. unsigned int timeout = 0;
  1623. if (!pci_priv) {
  1624. cnss_pr_err("pci_priv is NULL\n");
  1625. return -ENODEV;
  1626. }
  1627. plat_priv = pci_priv->plat_priv;
  1628. if (test_bit(FBC_BYPASS, &plat_priv->ctrl_params.quirks))
  1629. return 0;
  1630. if (MHI_TIMEOUT_OVERWRITE_MS)
  1631. pci_priv->mhi_ctrl->timeout_ms = MHI_TIMEOUT_OVERWRITE_MS;
  1632. cnss_mhi_set_m2_timeout_ms(pci_priv, MHI_M2_TIMEOUT_MS);
  1633. ret = cnss_pci_set_mhi_state(pci_priv, CNSS_MHI_INIT);
  1634. if (ret)
  1635. return ret;
  1636. timeout = pci_priv->mhi_ctrl->timeout_ms;
  1637. /* For non-perf builds the timeout is 10 (default) * 6 seconds */
  1638. if (cnss_get_host_build_type() == QMI_HOST_BUILD_TYPE_PRIMARY_V01)
  1639. pci_priv->mhi_ctrl->timeout_ms *= 6;
  1640. else /* For perf builds the timeout is 10 (default) * 3 seconds */
  1641. pci_priv->mhi_ctrl->timeout_ms *= 3;
  1642. /* Start the timer to dump MHI/PBL/SBL debug data periodically */
  1643. mod_timer(&pci_priv->boot_debug_timer,
  1644. jiffies + msecs_to_jiffies(BOOT_DEBUG_TIMEOUT_MS));
  1645. ret = cnss_pci_set_mhi_state(pci_priv, CNSS_MHI_POWER_ON);
  1646. del_timer_sync(&pci_priv->boot_debug_timer);
  1647. if (ret == 0)
  1648. cnss_wlan_adsp_pc_enable(pci_priv, false);
  1649. pci_priv->mhi_ctrl->timeout_ms = timeout;
  1650. if (ret == -ETIMEDOUT) {
  1651. /* This is a special case needs to be handled that if MHI
  1652. * power on returns -ETIMEDOUT, controller needs to take care
  1653. * the cleanup by calling MHI power down. Force to set the bit
  1654. * for driver internal MHI state to make sure it can be handled
  1655. * properly later.
  1656. */
  1657. set_bit(CNSS_MHI_POWER_ON, &pci_priv->mhi_state);
  1658. ret = cnss_pci_handle_mhi_poweron_timeout(pci_priv);
  1659. }
  1660. return ret;
  1661. }
  1662. static void cnss_pci_power_off_mhi(struct cnss_pci_data *pci_priv)
  1663. {
  1664. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  1665. if (test_bit(FBC_BYPASS, &plat_priv->ctrl_params.quirks))
  1666. return;
  1667. if (!test_bit(CNSS_MHI_POWER_ON, &pci_priv->mhi_state)) {
  1668. cnss_pr_dbg("MHI is already powered off\n");
  1669. return;
  1670. }
  1671. cnss_wlan_adsp_pc_enable(pci_priv, true);
  1672. cnss_pci_set_mhi_state_bit(pci_priv, CNSS_MHI_RESUME);
  1673. cnss_pci_set_mhi_state_bit(pci_priv, CNSS_MHI_POWERING_OFF);
  1674. if (!pci_priv->pci_link_down_ind)
  1675. cnss_pci_set_mhi_state(pci_priv, CNSS_MHI_POWER_OFF);
  1676. else
  1677. cnss_pci_set_mhi_state(pci_priv, CNSS_MHI_FORCE_POWER_OFF);
  1678. }
  1679. static void cnss_pci_deinit_mhi(struct cnss_pci_data *pci_priv)
  1680. {
  1681. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  1682. if (test_bit(FBC_BYPASS, &plat_priv->ctrl_params.quirks))
  1683. return;
  1684. if (!test_bit(CNSS_MHI_INIT, &pci_priv->mhi_state)) {
  1685. cnss_pr_dbg("MHI is already deinited\n");
  1686. return;
  1687. }
  1688. cnss_pci_set_mhi_state(pci_priv, CNSS_MHI_DEINIT);
  1689. }
  1690. static void cnss_pci_set_wlaon_pwr_ctrl(struct cnss_pci_data *pci_priv,
  1691. bool set_vddd4blow, bool set_shutdown,
  1692. bool do_force_wake)
  1693. {
  1694. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  1695. int ret;
  1696. u32 val;
  1697. if (!plat_priv->set_wlaon_pwr_ctrl)
  1698. return;
  1699. if (pci_priv->pci_link_state == PCI_LINK_DOWN ||
  1700. pci_priv->pci_link_down_ind)
  1701. return;
  1702. if (do_force_wake)
  1703. if (cnss_pci_force_wake_get(pci_priv))
  1704. return;
  1705. ret = cnss_pci_reg_read(pci_priv, WLAON_QFPROM_PWR_CTRL_REG, &val);
  1706. if (ret) {
  1707. cnss_pr_err("Failed to read register offset 0x%x, err = %d\n",
  1708. WLAON_QFPROM_PWR_CTRL_REG, ret);
  1709. goto force_wake_put;
  1710. }
  1711. cnss_pr_dbg("Read register offset 0x%x, val = 0x%x\n",
  1712. WLAON_QFPROM_PWR_CTRL_REG, val);
  1713. if (set_vddd4blow)
  1714. val |= QFPROM_PWR_CTRL_VDD4BLOW_SW_EN_MASK;
  1715. else
  1716. val &= ~QFPROM_PWR_CTRL_VDD4BLOW_SW_EN_MASK;
  1717. if (set_shutdown)
  1718. val |= QFPROM_PWR_CTRL_SHUTDOWN_EN_MASK;
  1719. else
  1720. val &= ~QFPROM_PWR_CTRL_SHUTDOWN_EN_MASK;
  1721. ret = cnss_pci_reg_write(pci_priv, WLAON_QFPROM_PWR_CTRL_REG, val);
  1722. if (ret) {
  1723. cnss_pr_err("Failed to write register offset 0x%x, err = %d\n",
  1724. WLAON_QFPROM_PWR_CTRL_REG, ret);
  1725. goto force_wake_put;
  1726. }
  1727. cnss_pr_dbg("Write val 0x%x to register offset 0x%x\n", val,
  1728. WLAON_QFPROM_PWR_CTRL_REG);
  1729. if (set_shutdown)
  1730. usleep_range(WLAON_PWR_CTRL_SHUTDOWN_DELAY_MIN_US,
  1731. WLAON_PWR_CTRL_SHUTDOWN_DELAY_MAX_US);
  1732. force_wake_put:
  1733. if (do_force_wake)
  1734. cnss_pci_force_wake_put(pci_priv);
  1735. }
  1736. static int cnss_pci_get_device_timestamp(struct cnss_pci_data *pci_priv,
  1737. u64 *time_us)
  1738. {
  1739. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  1740. u32 low, high;
  1741. u64 device_ticks;
  1742. if (!plat_priv->device_freq_hz) {
  1743. cnss_pr_err("Device time clock frequency is not valid\n");
  1744. return -EINVAL;
  1745. }
  1746. switch (pci_priv->device_id) {
  1747. case KIWI_DEVICE_ID:
  1748. case MANGO_DEVICE_ID:
  1749. cnss_pci_reg_read(pci_priv, PCIE_MHI_TIME_LOW, &low);
  1750. cnss_pci_reg_read(pci_priv, PCIE_MHI_TIME_HIGH, &high);
  1751. break;
  1752. default:
  1753. cnss_pci_reg_read(pci_priv, WLAON_GLOBAL_COUNTER_CTRL3, &low);
  1754. cnss_pci_reg_read(pci_priv, WLAON_GLOBAL_COUNTER_CTRL4, &high);
  1755. break;
  1756. }
  1757. device_ticks = (u64)high << 32 | low;
  1758. do_div(device_ticks, plat_priv->device_freq_hz / 100000);
  1759. *time_us = device_ticks * 10;
  1760. return 0;
  1761. }
  1762. static void cnss_pci_enable_time_sync_counter(struct cnss_pci_data *pci_priv)
  1763. {
  1764. switch (pci_priv->device_id) {
  1765. case KIWI_DEVICE_ID:
  1766. case MANGO_DEVICE_ID:
  1767. return;
  1768. default:
  1769. break;
  1770. }
  1771. cnss_pci_reg_write(pci_priv, WLAON_GLOBAL_COUNTER_CTRL5,
  1772. TIME_SYNC_ENABLE);
  1773. }
  1774. static void cnss_pci_clear_time_sync_counter(struct cnss_pci_data *pci_priv)
  1775. {
  1776. switch (pci_priv->device_id) {
  1777. case KIWI_DEVICE_ID:
  1778. case MANGO_DEVICE_ID:
  1779. return;
  1780. default:
  1781. break;
  1782. }
  1783. cnss_pci_reg_write(pci_priv, WLAON_GLOBAL_COUNTER_CTRL5,
  1784. TIME_SYNC_CLEAR);
  1785. }
  1786. static void cnss_pci_time_sync_reg_update(struct cnss_pci_data *pci_priv,
  1787. u32 low, u32 high)
  1788. {
  1789. u32 time_reg_low;
  1790. u32 time_reg_high;
  1791. switch (pci_priv->device_id) {
  1792. case KIWI_DEVICE_ID:
  1793. case MANGO_DEVICE_ID:
  1794. /* Use the next two shadow registers after host's usage */
  1795. time_reg_low = PCIE_SHADOW_REG_VALUE_0 +
  1796. (pci_priv->plat_priv->num_shadow_regs_v3 *
  1797. SHADOW_REG_LEN_BYTES);
  1798. time_reg_high = time_reg_low + SHADOW_REG_LEN_BYTES;
  1799. break;
  1800. default:
  1801. time_reg_low = PCIE_SHADOW_REG_VALUE_34;
  1802. time_reg_high = PCIE_SHADOW_REG_VALUE_35;
  1803. break;
  1804. }
  1805. cnss_pci_reg_write(pci_priv, time_reg_low, low);
  1806. cnss_pci_reg_write(pci_priv, time_reg_high, high);
  1807. cnss_pci_reg_read(pci_priv, time_reg_low, &low);
  1808. cnss_pci_reg_read(pci_priv, time_reg_high, &high);
  1809. cnss_pr_dbg("Updated time sync regs [0x%x] = 0x%x, [0x%x] = 0x%x\n",
  1810. time_reg_low, low, time_reg_high, high);
  1811. }
  1812. static int cnss_pci_update_timestamp(struct cnss_pci_data *pci_priv)
  1813. {
  1814. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  1815. struct device *dev = &pci_priv->pci_dev->dev;
  1816. unsigned long flags = 0;
  1817. u64 host_time_us, device_time_us, offset;
  1818. u32 low, high;
  1819. int ret;
  1820. ret = cnss_pci_prevent_l1(dev);
  1821. if (ret)
  1822. goto out;
  1823. ret = cnss_pci_force_wake_get(pci_priv);
  1824. if (ret)
  1825. goto allow_l1;
  1826. spin_lock_irqsave(&time_sync_lock, flags);
  1827. cnss_pci_clear_time_sync_counter(pci_priv);
  1828. cnss_pci_enable_time_sync_counter(pci_priv);
  1829. host_time_us = cnss_get_host_timestamp(plat_priv);
  1830. ret = cnss_pci_get_device_timestamp(pci_priv, &device_time_us);
  1831. cnss_pci_clear_time_sync_counter(pci_priv);
  1832. spin_unlock_irqrestore(&time_sync_lock, flags);
  1833. if (ret)
  1834. goto force_wake_put;
  1835. if (host_time_us < device_time_us) {
  1836. cnss_pr_err("Host time (%llu us) is smaller than device time (%llu us), stop\n",
  1837. host_time_us, device_time_us);
  1838. ret = -EINVAL;
  1839. goto force_wake_put;
  1840. }
  1841. offset = host_time_us - device_time_us;
  1842. cnss_pr_dbg("Host time = %llu us, device time = %llu us, offset = %llu us\n",
  1843. host_time_us, device_time_us, offset);
  1844. low = offset & 0xFFFFFFFF;
  1845. high = offset >> 32;
  1846. cnss_pci_time_sync_reg_update(pci_priv, low, high);
  1847. force_wake_put:
  1848. cnss_pci_force_wake_put(pci_priv);
  1849. allow_l1:
  1850. cnss_pci_allow_l1(dev);
  1851. out:
  1852. return ret;
  1853. }
  1854. static void cnss_pci_time_sync_work_hdlr(struct work_struct *work)
  1855. {
  1856. struct cnss_pci_data *pci_priv =
  1857. container_of(work, struct cnss_pci_data, time_sync_work.work);
  1858. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  1859. unsigned int time_sync_period_ms =
  1860. plat_priv->ctrl_params.time_sync_period;
  1861. if (test_bit(DISABLE_TIME_SYNC, &plat_priv->ctrl_params.quirks)) {
  1862. cnss_pr_dbg("Time sync is disabled\n");
  1863. return;
  1864. }
  1865. if (!time_sync_period_ms) {
  1866. cnss_pr_dbg("Skip time sync as time period is 0\n");
  1867. return;
  1868. }
  1869. if (cnss_pci_is_device_down(&pci_priv->pci_dev->dev))
  1870. return;
  1871. if (cnss_pci_pm_runtime_get_sync(pci_priv, RTPM_ID_CNSS) < 0)
  1872. goto runtime_pm_put;
  1873. mutex_lock(&pci_priv->bus_lock);
  1874. cnss_pci_update_timestamp(pci_priv);
  1875. mutex_unlock(&pci_priv->bus_lock);
  1876. schedule_delayed_work(&pci_priv->time_sync_work,
  1877. msecs_to_jiffies(time_sync_period_ms));
  1878. runtime_pm_put:
  1879. cnss_pci_pm_runtime_mark_last_busy(pci_priv);
  1880. cnss_pci_pm_runtime_put_autosuspend(pci_priv, RTPM_ID_CNSS);
  1881. }
  1882. static int cnss_pci_start_time_sync_update(struct cnss_pci_data *pci_priv)
  1883. {
  1884. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  1885. switch (pci_priv->device_id) {
  1886. case QCA6390_DEVICE_ID:
  1887. case QCA6490_DEVICE_ID:
  1888. case KIWI_DEVICE_ID:
  1889. case MANGO_DEVICE_ID:
  1890. break;
  1891. default:
  1892. return -EOPNOTSUPP;
  1893. }
  1894. if (!plat_priv->device_freq_hz) {
  1895. cnss_pr_dbg("Device time clock frequency is not valid, skip time sync\n");
  1896. return -EINVAL;
  1897. }
  1898. cnss_pci_time_sync_work_hdlr(&pci_priv->time_sync_work.work);
  1899. return 0;
  1900. }
  1901. static void cnss_pci_stop_time_sync_update(struct cnss_pci_data *pci_priv)
  1902. {
  1903. switch (pci_priv->device_id) {
  1904. case QCA6390_DEVICE_ID:
  1905. case QCA6490_DEVICE_ID:
  1906. case KIWI_DEVICE_ID:
  1907. case MANGO_DEVICE_ID:
  1908. break;
  1909. default:
  1910. return;
  1911. }
  1912. cancel_delayed_work_sync(&pci_priv->time_sync_work);
  1913. }
  1914. int cnss_pci_call_driver_probe(struct cnss_pci_data *pci_priv)
  1915. {
  1916. int ret = 0;
  1917. struct cnss_plat_data *plat_priv;
  1918. if (!pci_priv)
  1919. return -ENODEV;
  1920. plat_priv = pci_priv->plat_priv;
  1921. if (test_bit(CNSS_DRIVER_DEBUG, &plat_priv->driver_state)) {
  1922. clear_bit(CNSS_DRIVER_RECOVERY, &plat_priv->driver_state);
  1923. cnss_pr_dbg("Skip driver probe\n");
  1924. goto out;
  1925. }
  1926. if (!pci_priv->driver_ops) {
  1927. cnss_pr_err("driver_ops is NULL\n");
  1928. ret = -EINVAL;
  1929. goto out;
  1930. }
  1931. if (test_bit(CNSS_DRIVER_RECOVERY, &plat_priv->driver_state) &&
  1932. test_bit(CNSS_DRIVER_PROBED, &plat_priv->driver_state)) {
  1933. ret = pci_priv->driver_ops->reinit(pci_priv->pci_dev,
  1934. pci_priv->pci_device_id);
  1935. if (ret) {
  1936. cnss_pr_err("Failed to reinit host driver, err = %d\n",
  1937. ret);
  1938. goto out;
  1939. }
  1940. complete(&plat_priv->recovery_complete);
  1941. } else if (test_bit(CNSS_DRIVER_LOADING, &plat_priv->driver_state)) {
  1942. ret = pci_priv->driver_ops->probe(pci_priv->pci_dev,
  1943. pci_priv->pci_device_id);
  1944. if (ret) {
  1945. cnss_pr_err("Failed to probe host driver, err = %d\n",
  1946. ret);
  1947. goto out;
  1948. }
  1949. clear_bit(CNSS_DRIVER_LOADING, &plat_priv->driver_state);
  1950. set_bit(CNSS_DRIVER_PROBED, &plat_priv->driver_state);
  1951. complete_all(&plat_priv->power_up_complete);
  1952. } else if (test_bit(CNSS_DRIVER_IDLE_RESTART,
  1953. &plat_priv->driver_state)) {
  1954. ret = pci_priv->driver_ops->idle_restart(pci_priv->pci_dev,
  1955. pci_priv->pci_device_id);
  1956. if (ret) {
  1957. cnss_pr_err("Failed to idle restart host driver, err = %d\n",
  1958. ret);
  1959. plat_priv->power_up_error = ret;
  1960. complete_all(&plat_priv->power_up_complete);
  1961. goto out;
  1962. }
  1963. clear_bit(CNSS_DRIVER_IDLE_RESTART, &plat_priv->driver_state);
  1964. complete_all(&plat_priv->power_up_complete);
  1965. } else {
  1966. complete(&plat_priv->power_up_complete);
  1967. }
  1968. if (test_bit(CNSS_DRIVER_RECOVERY, &plat_priv->driver_state)) {
  1969. clear_bit(CNSS_DRIVER_RECOVERY, &plat_priv->driver_state);
  1970. __pm_relax(plat_priv->recovery_ws);
  1971. }
  1972. cnss_pci_start_time_sync_update(pci_priv);
  1973. return 0;
  1974. out:
  1975. return ret;
  1976. }
  1977. int cnss_pci_call_driver_remove(struct cnss_pci_data *pci_priv)
  1978. {
  1979. struct cnss_plat_data *plat_priv;
  1980. int ret;
  1981. if (!pci_priv)
  1982. return -ENODEV;
  1983. plat_priv = pci_priv->plat_priv;
  1984. if (test_bit(CNSS_IN_COLD_BOOT_CAL, &plat_priv->driver_state) ||
  1985. test_bit(CNSS_FW_BOOT_RECOVERY, &plat_priv->driver_state) ||
  1986. test_bit(CNSS_DRIVER_DEBUG, &plat_priv->driver_state)) {
  1987. cnss_pr_dbg("Skip driver remove\n");
  1988. return 0;
  1989. }
  1990. if (!pci_priv->driver_ops) {
  1991. cnss_pr_err("driver_ops is NULL\n");
  1992. return -EINVAL;
  1993. }
  1994. cnss_pci_stop_time_sync_update(pci_priv);
  1995. if (test_bit(CNSS_DRIVER_RECOVERY, &plat_priv->driver_state) &&
  1996. test_bit(CNSS_DRIVER_PROBED, &plat_priv->driver_state)) {
  1997. pci_priv->driver_ops->shutdown(pci_priv->pci_dev);
  1998. } else if (test_bit(CNSS_DRIVER_UNLOADING, &plat_priv->driver_state)) {
  1999. pci_priv->driver_ops->remove(pci_priv->pci_dev);
  2000. clear_bit(CNSS_DRIVER_PROBED, &plat_priv->driver_state);
  2001. } else if (test_bit(CNSS_DRIVER_IDLE_SHUTDOWN,
  2002. &plat_priv->driver_state)) {
  2003. ret = pci_priv->driver_ops->idle_shutdown(pci_priv->pci_dev);
  2004. if (ret == -EAGAIN) {
  2005. clear_bit(CNSS_DRIVER_IDLE_SHUTDOWN,
  2006. &plat_priv->driver_state);
  2007. return ret;
  2008. }
  2009. }
  2010. plat_priv->get_info_cb_ctx = NULL;
  2011. plat_priv->get_info_cb = NULL;
  2012. return 0;
  2013. }
  2014. int cnss_pci_call_driver_modem_status(struct cnss_pci_data *pci_priv,
  2015. int modem_current_status)
  2016. {
  2017. struct cnss_wlan_driver *driver_ops;
  2018. if (!pci_priv)
  2019. return -ENODEV;
  2020. driver_ops = pci_priv->driver_ops;
  2021. if (!driver_ops || !driver_ops->modem_status)
  2022. return -EINVAL;
  2023. driver_ops->modem_status(pci_priv->pci_dev, modem_current_status);
  2024. return 0;
  2025. }
  2026. int cnss_pci_update_status(struct cnss_pci_data *pci_priv,
  2027. enum cnss_driver_status status)
  2028. {
  2029. struct cnss_wlan_driver *driver_ops;
  2030. if (!pci_priv)
  2031. return -ENODEV;
  2032. driver_ops = pci_priv->driver_ops;
  2033. if (!driver_ops || !driver_ops->update_status)
  2034. return -EINVAL;
  2035. cnss_pr_dbg("Update driver status: %d\n", status);
  2036. driver_ops->update_status(pci_priv->pci_dev, status);
  2037. return 0;
  2038. }
  2039. static void cnss_pci_misc_reg_dump(struct cnss_pci_data *pci_priv,
  2040. struct cnss_misc_reg *misc_reg,
  2041. u32 misc_reg_size,
  2042. char *reg_name)
  2043. {
  2044. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2045. bool do_force_wake_put = true;
  2046. int i;
  2047. if (!misc_reg)
  2048. return;
  2049. if (in_interrupt() || irqs_disabled())
  2050. return;
  2051. if (cnss_pci_check_link_status(pci_priv))
  2052. return;
  2053. if (cnss_pci_force_wake_get(pci_priv)) {
  2054. /* Continue to dump when device has entered RDDM already */
  2055. if (!test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state))
  2056. return;
  2057. do_force_wake_put = false;
  2058. }
  2059. cnss_pr_dbg("Start to dump %s registers\n", reg_name);
  2060. for (i = 0; i < misc_reg_size; i++) {
  2061. if (!test_bit(pci_priv->misc_reg_dev_mask,
  2062. &misc_reg[i].dev_mask))
  2063. continue;
  2064. if (misc_reg[i].wr) {
  2065. if (misc_reg[i].offset ==
  2066. QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_SAW2_CFG &&
  2067. i >= 1)
  2068. misc_reg[i].val =
  2069. QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_SAW2_CFG_MSK |
  2070. misc_reg[i - 1].val;
  2071. if (cnss_pci_reg_write(pci_priv,
  2072. misc_reg[i].offset,
  2073. misc_reg[i].val))
  2074. goto force_wake_put;
  2075. cnss_pr_vdbg("Write 0x%X to 0x%X\n",
  2076. misc_reg[i].val,
  2077. misc_reg[i].offset);
  2078. } else {
  2079. if (cnss_pci_reg_read(pci_priv,
  2080. misc_reg[i].offset,
  2081. &misc_reg[i].val))
  2082. goto force_wake_put;
  2083. }
  2084. }
  2085. force_wake_put:
  2086. if (do_force_wake_put)
  2087. cnss_pci_force_wake_put(pci_priv);
  2088. }
  2089. static void cnss_pci_dump_misc_reg(struct cnss_pci_data *pci_priv)
  2090. {
  2091. if (in_interrupt() || irqs_disabled())
  2092. return;
  2093. if (cnss_pci_check_link_status(pci_priv))
  2094. return;
  2095. cnss_pci_misc_reg_dump(pci_priv, pci_priv->wcss_reg,
  2096. WCSS_REG_SIZE, "wcss");
  2097. cnss_pci_misc_reg_dump(pci_priv, pci_priv->pcie_reg,
  2098. PCIE_REG_SIZE, "pcie");
  2099. cnss_pci_misc_reg_dump(pci_priv, pci_priv->wlaon_reg,
  2100. WLAON_REG_SIZE, "wlaon");
  2101. cnss_pci_misc_reg_dump(pci_priv, pci_priv->syspm_reg,
  2102. SYSPM_REG_SIZE, "syspm");
  2103. }
  2104. static void cnss_pci_dump_shadow_reg(struct cnss_pci_data *pci_priv)
  2105. {
  2106. int i, j = 0, array_size = SHADOW_REG_COUNT + SHADOW_REG_INTER_COUNT;
  2107. u32 reg_offset;
  2108. bool do_force_wake_put = true;
  2109. if (in_interrupt() || irqs_disabled())
  2110. return;
  2111. if (cnss_pci_check_link_status(pci_priv))
  2112. return;
  2113. if (!pci_priv->debug_reg) {
  2114. pci_priv->debug_reg = devm_kzalloc(&pci_priv->pci_dev->dev,
  2115. sizeof(*pci_priv->debug_reg)
  2116. * array_size, GFP_KERNEL);
  2117. if (!pci_priv->debug_reg)
  2118. return;
  2119. }
  2120. if (cnss_pci_force_wake_get(pci_priv))
  2121. do_force_wake_put = false;
  2122. cnss_pr_dbg("Start to dump shadow registers\n");
  2123. for (i = 0; i < SHADOW_REG_COUNT; i++, j++) {
  2124. reg_offset = PCIE_SHADOW_REG_VALUE_0 + i * 4;
  2125. pci_priv->debug_reg[j].offset = reg_offset;
  2126. if (cnss_pci_reg_read(pci_priv, reg_offset,
  2127. &pci_priv->debug_reg[j].val))
  2128. goto force_wake_put;
  2129. }
  2130. for (i = 0; i < SHADOW_REG_INTER_COUNT; i++, j++) {
  2131. reg_offset = PCIE_SHADOW_REG_INTER_0 + i * 4;
  2132. pci_priv->debug_reg[j].offset = reg_offset;
  2133. if (cnss_pci_reg_read(pci_priv, reg_offset,
  2134. &pci_priv->debug_reg[j].val))
  2135. goto force_wake_put;
  2136. }
  2137. force_wake_put:
  2138. if (do_force_wake_put)
  2139. cnss_pci_force_wake_put(pci_priv);
  2140. }
  2141. static int cnss_qca6174_powerup(struct cnss_pci_data *pci_priv)
  2142. {
  2143. int ret = 0;
  2144. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2145. ret = cnss_power_on_device(plat_priv);
  2146. if (ret) {
  2147. cnss_pr_err("Failed to power on device, err = %d\n", ret);
  2148. goto out;
  2149. }
  2150. ret = cnss_resume_pci_link(pci_priv);
  2151. if (ret) {
  2152. cnss_pr_err("Failed to resume PCI link, err = %d\n", ret);
  2153. goto power_off;
  2154. }
  2155. ret = cnss_pci_call_driver_probe(pci_priv);
  2156. if (ret)
  2157. goto suspend_link;
  2158. return 0;
  2159. suspend_link:
  2160. cnss_suspend_pci_link(pci_priv);
  2161. power_off:
  2162. cnss_power_off_device(plat_priv);
  2163. out:
  2164. return ret;
  2165. }
  2166. static int cnss_qca6174_shutdown(struct cnss_pci_data *pci_priv)
  2167. {
  2168. int ret = 0;
  2169. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2170. cnss_pci_pm_runtime_resume(pci_priv);
  2171. ret = cnss_pci_call_driver_remove(pci_priv);
  2172. if (ret == -EAGAIN)
  2173. goto out;
  2174. cnss_request_bus_bandwidth(&plat_priv->plat_dev->dev,
  2175. CNSS_BUS_WIDTH_NONE);
  2176. cnss_pci_set_monitor_wake_intr(pci_priv, false);
  2177. cnss_pci_set_auto_suspended(pci_priv, 0);
  2178. ret = cnss_suspend_pci_link(pci_priv);
  2179. if (ret)
  2180. cnss_pr_err("Failed to suspend PCI link, err = %d\n", ret);
  2181. cnss_power_off_device(plat_priv);
  2182. clear_bit(CNSS_DRIVER_UNLOADING, &plat_priv->driver_state);
  2183. clear_bit(CNSS_DRIVER_IDLE_SHUTDOWN, &plat_priv->driver_state);
  2184. out:
  2185. return ret;
  2186. }
  2187. static void cnss_qca6174_crash_shutdown(struct cnss_pci_data *pci_priv)
  2188. {
  2189. if (pci_priv->driver_ops && pci_priv->driver_ops->crash_shutdown)
  2190. pci_priv->driver_ops->crash_shutdown(pci_priv->pci_dev);
  2191. }
  2192. static int cnss_qca6174_ramdump(struct cnss_pci_data *pci_priv)
  2193. {
  2194. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2195. struct cnss_ramdump_info *ramdump_info;
  2196. ramdump_info = &plat_priv->ramdump_info;
  2197. if (!ramdump_info->ramdump_size)
  2198. return -EINVAL;
  2199. return cnss_do_ramdump(plat_priv);
  2200. }
  2201. static void cnss_get_driver_mode_update_fw_name(struct cnss_plat_data *plat_priv)
  2202. {
  2203. struct cnss_pci_data *pci_priv;
  2204. struct cnss_wlan_driver *driver_ops;
  2205. pci_priv = plat_priv->bus_priv;
  2206. driver_ops = pci_priv->driver_ops;
  2207. if (driver_ops && driver_ops->get_driver_mode) {
  2208. plat_priv->driver_mode = driver_ops->get_driver_mode();
  2209. cnss_pci_update_fw_name(pci_priv);
  2210. cnss_pr_dbg("New driver mode is %d", plat_priv->driver_mode);
  2211. }
  2212. }
  2213. static int cnss_qca6290_powerup(struct cnss_pci_data *pci_priv)
  2214. {
  2215. int ret = 0;
  2216. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2217. unsigned int timeout;
  2218. int retry = 0, bt_en_gpio = plat_priv->pinctrl_info.bt_en_gpio;
  2219. int sw_ctrl_gpio = plat_priv->pinctrl_info.sw_ctrl_gpio;
  2220. if (plat_priv->ramdump_info_v2.dump_data_valid) {
  2221. cnss_pci_clear_dump_info(pci_priv);
  2222. cnss_pci_power_off_mhi(pci_priv);
  2223. cnss_suspend_pci_link(pci_priv);
  2224. cnss_pci_deinit_mhi(pci_priv);
  2225. cnss_power_off_device(plat_priv);
  2226. }
  2227. /* Clear QMI send usage count during every power up */
  2228. pci_priv->qmi_send_usage_count = 0;
  2229. plat_priv->power_up_error = 0;
  2230. cnss_get_driver_mode_update_fw_name(plat_priv);
  2231. retry:
  2232. ret = cnss_power_on_device(plat_priv);
  2233. if (ret) {
  2234. cnss_pr_err("Failed to power on device, err = %d\n", ret);
  2235. goto out;
  2236. }
  2237. ret = cnss_resume_pci_link(pci_priv);
  2238. if (ret) {
  2239. cnss_pr_err("Failed to resume PCI link, err = %d\n", ret);
  2240. cnss_pr_dbg("Value of SW_CTRL GPIO: %d\n",
  2241. cnss_get_input_gpio_value(plat_priv, sw_ctrl_gpio));
  2242. if (test_bit(IGNORE_PCI_LINK_FAILURE,
  2243. &plat_priv->ctrl_params.quirks)) {
  2244. cnss_pr_dbg("Ignore PCI link resume failure\n");
  2245. ret = 0;
  2246. goto out;
  2247. }
  2248. if (ret == -EAGAIN && retry++ < POWER_ON_RETRY_MAX_TIMES) {
  2249. cnss_power_off_device(plat_priv);
  2250. /* Force toggle BT_EN GPIO low */
  2251. if (retry == POWER_ON_RETRY_MAX_TIMES) {
  2252. cnss_pr_dbg("Retry #%d. Set BT_EN GPIO(%u) low\n",
  2253. retry, bt_en_gpio);
  2254. if (bt_en_gpio >= 0)
  2255. gpio_direction_output(bt_en_gpio, 0);
  2256. cnss_pr_dbg("BT_EN GPIO val: %d\n",
  2257. gpio_get_value(bt_en_gpio));
  2258. }
  2259. cnss_pr_dbg("Retry to resume PCI link #%d\n", retry);
  2260. cnss_pr_dbg("Value of SW_CTRL GPIO: %d\n",
  2261. cnss_get_input_gpio_value(plat_priv,
  2262. sw_ctrl_gpio));
  2263. msleep(POWER_ON_RETRY_DELAY_MS * retry);
  2264. goto retry;
  2265. }
  2266. /* Assert when it reaches maximum retries */
  2267. CNSS_ASSERT(0);
  2268. goto power_off;
  2269. }
  2270. cnss_pci_set_wlaon_pwr_ctrl(pci_priv, false, false, false);
  2271. timeout = cnss_get_timeout(plat_priv, CNSS_TIMEOUT_QMI);
  2272. ret = cnss_pci_start_mhi(pci_priv);
  2273. if (ret) {
  2274. cnss_fatal_err("Failed to start MHI, err = %d\n", ret);
  2275. if (!test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state) &&
  2276. !pci_priv->pci_link_down_ind && timeout) {
  2277. /* Start recovery directly for MHI start failures */
  2278. cnss_schedule_recovery(&pci_priv->pci_dev->dev,
  2279. CNSS_REASON_DEFAULT);
  2280. }
  2281. return 0;
  2282. }
  2283. if (test_bit(USE_CORE_ONLY_FW, &plat_priv->ctrl_params.quirks)) {
  2284. clear_bit(CNSS_FW_BOOT_RECOVERY, &plat_priv->driver_state);
  2285. clear_bit(CNSS_DRIVER_RECOVERY, &plat_priv->driver_state);
  2286. return 0;
  2287. }
  2288. cnss_set_pin_connect_status(plat_priv);
  2289. if (test_bit(QMI_BYPASS, &plat_priv->ctrl_params.quirks)) {
  2290. ret = cnss_pci_call_driver_probe(pci_priv);
  2291. if (ret)
  2292. goto stop_mhi;
  2293. } else if (timeout) {
  2294. if (test_bit(CNSS_IN_COLD_BOOT_CAL, &plat_priv->driver_state))
  2295. timeout += WLAN_COLD_BOOT_CAL_TIMEOUT;
  2296. else
  2297. timeout += WLAN_MISSION_MODE_TIMEOUT;
  2298. mod_timer(&plat_priv->fw_boot_timer,
  2299. jiffies + msecs_to_jiffies(timeout));
  2300. }
  2301. return 0;
  2302. stop_mhi:
  2303. cnss_pci_set_wlaon_pwr_ctrl(pci_priv, false, true, true);
  2304. cnss_pci_power_off_mhi(pci_priv);
  2305. cnss_suspend_pci_link(pci_priv);
  2306. cnss_pci_deinit_mhi(pci_priv);
  2307. power_off:
  2308. cnss_power_off_device(plat_priv);
  2309. out:
  2310. return ret;
  2311. }
  2312. static int cnss_qca6290_shutdown(struct cnss_pci_data *pci_priv)
  2313. {
  2314. int ret = 0;
  2315. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2316. int do_force_wake = true;
  2317. cnss_pci_pm_runtime_resume(pci_priv);
  2318. ret = cnss_pci_call_driver_remove(pci_priv);
  2319. if (ret == -EAGAIN)
  2320. goto out;
  2321. cnss_request_bus_bandwidth(&plat_priv->plat_dev->dev,
  2322. CNSS_BUS_WIDTH_NONE);
  2323. cnss_pci_set_monitor_wake_intr(pci_priv, false);
  2324. cnss_pci_set_auto_suspended(pci_priv, 0);
  2325. if ((test_bit(CNSS_DRIVER_LOADING, &plat_priv->driver_state) ||
  2326. test_bit(CNSS_DRIVER_UNLOADING, &plat_priv->driver_state) ||
  2327. test_bit(CNSS_DRIVER_IDLE_RESTART, &plat_priv->driver_state) ||
  2328. test_bit(CNSS_DRIVER_IDLE_SHUTDOWN, &plat_priv->driver_state) ||
  2329. test_bit(CNSS_IN_COLD_BOOT_CAL, &plat_priv->driver_state)) &&
  2330. test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state)) {
  2331. del_timer(&pci_priv->dev_rddm_timer);
  2332. cnss_pci_collect_dump_info(pci_priv, false);
  2333. CNSS_ASSERT(0);
  2334. }
  2335. if (!cnss_is_device_powered_on(plat_priv)) {
  2336. cnss_pr_dbg("Device is already powered off, ignore\n");
  2337. goto skip_power_off;
  2338. }
  2339. if (test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state))
  2340. do_force_wake = false;
  2341. cnss_pci_set_wlaon_pwr_ctrl(pci_priv, false, true, do_force_wake);
  2342. /* FBC image will be freed after powering off MHI, so skip
  2343. * if RAM dump data is still valid.
  2344. */
  2345. if (plat_priv->ramdump_info_v2.dump_data_valid)
  2346. goto skip_power_off;
  2347. cnss_pci_power_off_mhi(pci_priv);
  2348. ret = cnss_suspend_pci_link(pci_priv);
  2349. if (ret)
  2350. cnss_pr_err("Failed to suspend PCI link, err = %d\n", ret);
  2351. cnss_pci_deinit_mhi(pci_priv);
  2352. cnss_power_off_device(plat_priv);
  2353. skip_power_off:
  2354. pci_priv->remap_window = 0;
  2355. clear_bit(CNSS_FW_READY, &plat_priv->driver_state);
  2356. clear_bit(CNSS_FW_MEM_READY, &plat_priv->driver_state);
  2357. if (test_bit(CNSS_DRIVER_UNLOADING, &plat_priv->driver_state) ||
  2358. test_bit(CNSS_DRIVER_IDLE_SHUTDOWN, &plat_priv->driver_state)) {
  2359. clear_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state);
  2360. pci_priv->pci_link_down_ind = false;
  2361. }
  2362. clear_bit(CNSS_DRIVER_UNLOADING, &plat_priv->driver_state);
  2363. clear_bit(CNSS_DRIVER_IDLE_SHUTDOWN, &plat_priv->driver_state);
  2364. memset(&print_optimize, 0, sizeof(print_optimize));
  2365. out:
  2366. return ret;
  2367. }
  2368. static void cnss_qca6290_crash_shutdown(struct cnss_pci_data *pci_priv)
  2369. {
  2370. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2371. set_bit(CNSS_IN_PANIC, &plat_priv->driver_state);
  2372. cnss_pr_dbg("Crash shutdown with driver_state 0x%lx\n",
  2373. plat_priv->driver_state);
  2374. cnss_pci_collect_dump_info(pci_priv, true);
  2375. clear_bit(CNSS_IN_PANIC, &plat_priv->driver_state);
  2376. }
  2377. static int cnss_qca6290_ramdump(struct cnss_pci_data *pci_priv)
  2378. {
  2379. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2380. struct cnss_ramdump_info_v2 *info_v2 = &plat_priv->ramdump_info_v2;
  2381. struct cnss_dump_data *dump_data = &info_v2->dump_data;
  2382. struct cnss_dump_seg *dump_seg = info_v2->dump_data_vaddr;
  2383. int ret = 0;
  2384. if (!info_v2->dump_data_valid || !dump_seg ||
  2385. dump_data->nentries == 0)
  2386. return 0;
  2387. ret = cnss_do_elf_ramdump(plat_priv);
  2388. cnss_pci_clear_dump_info(pci_priv);
  2389. cnss_pci_power_off_mhi(pci_priv);
  2390. cnss_suspend_pci_link(pci_priv);
  2391. cnss_pci_deinit_mhi(pci_priv);
  2392. cnss_power_off_device(plat_priv);
  2393. return ret;
  2394. }
  2395. int cnss_pci_dev_powerup(struct cnss_pci_data *pci_priv)
  2396. {
  2397. int ret = 0;
  2398. if (!pci_priv) {
  2399. cnss_pr_err("pci_priv is NULL\n");
  2400. return -ENODEV;
  2401. }
  2402. switch (pci_priv->device_id) {
  2403. case QCA6174_DEVICE_ID:
  2404. ret = cnss_qca6174_powerup(pci_priv);
  2405. break;
  2406. case QCA6290_DEVICE_ID:
  2407. case QCA6390_DEVICE_ID:
  2408. case QCA6490_DEVICE_ID:
  2409. case KIWI_DEVICE_ID:
  2410. case MANGO_DEVICE_ID:
  2411. ret = cnss_qca6290_powerup(pci_priv);
  2412. break;
  2413. default:
  2414. cnss_pr_err("Unknown device_id found: 0x%x\n",
  2415. pci_priv->device_id);
  2416. ret = -ENODEV;
  2417. }
  2418. return ret;
  2419. }
  2420. int cnss_pci_dev_shutdown(struct cnss_pci_data *pci_priv)
  2421. {
  2422. int ret = 0;
  2423. if (!pci_priv) {
  2424. cnss_pr_err("pci_priv is NULL\n");
  2425. return -ENODEV;
  2426. }
  2427. switch (pci_priv->device_id) {
  2428. case QCA6174_DEVICE_ID:
  2429. ret = cnss_qca6174_shutdown(pci_priv);
  2430. break;
  2431. case QCA6290_DEVICE_ID:
  2432. case QCA6390_DEVICE_ID:
  2433. case QCA6490_DEVICE_ID:
  2434. case KIWI_DEVICE_ID:
  2435. case MANGO_DEVICE_ID:
  2436. ret = cnss_qca6290_shutdown(pci_priv);
  2437. break;
  2438. default:
  2439. cnss_pr_err("Unknown device_id found: 0x%x\n",
  2440. pci_priv->device_id);
  2441. ret = -ENODEV;
  2442. }
  2443. return ret;
  2444. }
  2445. int cnss_pci_dev_crash_shutdown(struct cnss_pci_data *pci_priv)
  2446. {
  2447. int ret = 0;
  2448. if (!pci_priv) {
  2449. cnss_pr_err("pci_priv is NULL\n");
  2450. return -ENODEV;
  2451. }
  2452. switch (pci_priv->device_id) {
  2453. case QCA6174_DEVICE_ID:
  2454. cnss_qca6174_crash_shutdown(pci_priv);
  2455. break;
  2456. case QCA6290_DEVICE_ID:
  2457. case QCA6390_DEVICE_ID:
  2458. case QCA6490_DEVICE_ID:
  2459. case KIWI_DEVICE_ID:
  2460. case MANGO_DEVICE_ID:
  2461. cnss_qca6290_crash_shutdown(pci_priv);
  2462. break;
  2463. default:
  2464. cnss_pr_err("Unknown device_id found: 0x%x\n",
  2465. pci_priv->device_id);
  2466. ret = -ENODEV;
  2467. }
  2468. return ret;
  2469. }
  2470. int cnss_pci_dev_ramdump(struct cnss_pci_data *pci_priv)
  2471. {
  2472. int ret = 0;
  2473. if (!pci_priv) {
  2474. cnss_pr_err("pci_priv is NULL\n");
  2475. return -ENODEV;
  2476. }
  2477. switch (pci_priv->device_id) {
  2478. case QCA6174_DEVICE_ID:
  2479. ret = cnss_qca6174_ramdump(pci_priv);
  2480. break;
  2481. case QCA6290_DEVICE_ID:
  2482. case QCA6390_DEVICE_ID:
  2483. case QCA6490_DEVICE_ID:
  2484. case KIWI_DEVICE_ID:
  2485. case MANGO_DEVICE_ID:
  2486. ret = cnss_qca6290_ramdump(pci_priv);
  2487. break;
  2488. default:
  2489. cnss_pr_err("Unknown device_id found: 0x%x\n",
  2490. pci_priv->device_id);
  2491. ret = -ENODEV;
  2492. }
  2493. return ret;
  2494. }
  2495. int cnss_pci_is_drv_connected(struct device *dev)
  2496. {
  2497. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(to_pci_dev(dev));
  2498. if (!pci_priv)
  2499. return -ENODEV;
  2500. return pci_priv->drv_connected_last;
  2501. }
  2502. EXPORT_SYMBOL(cnss_pci_is_drv_connected);
  2503. static void cnss_wlan_reg_driver_work(struct work_struct *work)
  2504. {
  2505. struct cnss_plat_data *plat_priv =
  2506. container_of(work, struct cnss_plat_data, wlan_reg_driver_work.work);
  2507. struct cnss_pci_data *pci_priv = plat_priv->bus_priv;
  2508. struct cnss_cal_info *cal_info;
  2509. unsigned int timeout;
  2510. if (test_bit(CNSS_COLD_BOOT_CAL_DONE, &plat_priv->driver_state)) {
  2511. goto reg_driver;
  2512. } else {
  2513. if (plat_priv->charger_mode) {
  2514. cnss_pr_err("Ignore calibration timeout in charger mode\n");
  2515. return;
  2516. }
  2517. if (!test_bit(CNSS_IN_COLD_BOOT_CAL,
  2518. &plat_priv->driver_state)) {
  2519. timeout = cnss_get_timeout(plat_priv,
  2520. CNSS_TIMEOUT_CALIBRATION);
  2521. cnss_pr_dbg("File system not ready to start calibration. Wait for %ds..\n",
  2522. timeout / 1000);
  2523. schedule_delayed_work(&plat_priv->wlan_reg_driver_work,
  2524. msecs_to_jiffies(timeout));
  2525. return;
  2526. }
  2527. del_timer(&plat_priv->fw_boot_timer);
  2528. if (test_bit(CNSS_IN_COLD_BOOT_CAL, &plat_priv->driver_state) &&
  2529. !test_bit(CNSS_IN_REBOOT, &plat_priv->driver_state)) {
  2530. cnss_pr_err("Timeout waiting for calibration to complete\n");
  2531. CNSS_ASSERT(0);
  2532. }
  2533. cal_info = kzalloc(sizeof(*cal_info), GFP_KERNEL);
  2534. if (!cal_info)
  2535. return;
  2536. cal_info->cal_status = CNSS_CAL_TIMEOUT;
  2537. cnss_driver_event_post(plat_priv,
  2538. CNSS_DRIVER_EVENT_COLD_BOOT_CAL_DONE,
  2539. 0, cal_info);
  2540. }
  2541. reg_driver:
  2542. if (test_bit(CNSS_IN_REBOOT, &plat_priv->driver_state)) {
  2543. cnss_pr_dbg("Reboot/Shutdown is in progress, ignore register driver\n");
  2544. return;
  2545. }
  2546. reinit_completion(&plat_priv->power_up_complete);
  2547. cnss_driver_event_post(plat_priv,
  2548. CNSS_DRIVER_EVENT_REGISTER_DRIVER,
  2549. CNSS_EVENT_SYNC_UNKILLABLE,
  2550. pci_priv->driver_ops);
  2551. }
  2552. int cnss_wlan_register_driver(struct cnss_wlan_driver *driver_ops)
  2553. {
  2554. int ret = 0;
  2555. struct cnss_plat_data *plat_priv = cnss_bus_dev_to_plat_priv(NULL);
  2556. struct cnss_pci_data *pci_priv;
  2557. const struct pci_device_id *id_table = driver_ops->id_table;
  2558. unsigned int timeout;
  2559. if (!cnss_check_driver_loading_allowed()) {
  2560. cnss_pr_info("No cnss2 dtsi entry present");
  2561. return -ENODEV;
  2562. }
  2563. if (!plat_priv) {
  2564. cnss_pr_buf("plat_priv is not ready for register driver\n");
  2565. return -EAGAIN;
  2566. }
  2567. if (test_bit(CNSS_WLAN_HW_DISABLED, &plat_priv->driver_state)) {
  2568. while (id_table && id_table->device) {
  2569. if (plat_priv->device_id == id_table->device) {
  2570. if (plat_priv->device_id == KIWI_DEVICE_ID &&
  2571. driver_ops->chip_version != 2) {
  2572. cnss_pr_err("WLAN HW disabled. kiwi_v2 only supported\n");
  2573. return -ENODEV;
  2574. }
  2575. cnss_pr_info("WLAN register driver deferred for device ID: 0x%x due to HW disable\n",
  2576. id_table->device);
  2577. plat_priv->driver_ops = driver_ops;
  2578. return 0;
  2579. }
  2580. id_table++;
  2581. }
  2582. return -ENODEV;
  2583. }
  2584. if (!test_bit(CNSS_PCI_PROBE_DONE, &plat_priv->driver_state)) {
  2585. cnss_pr_info("pci probe not yet done for register driver\n");
  2586. return -EAGAIN;
  2587. }
  2588. pci_priv = plat_priv->bus_priv;
  2589. if (pci_priv->driver_ops) {
  2590. cnss_pr_err("Driver has already registered\n");
  2591. return -EEXIST;
  2592. }
  2593. if (test_bit(CNSS_IN_REBOOT, &plat_priv->driver_state)) {
  2594. cnss_pr_dbg("Reboot/Shutdown is in progress, ignore register driver\n");
  2595. return -EINVAL;
  2596. }
  2597. if (!id_table || !pci_dev_present(id_table)) {
  2598. /* id_table pointer will move from pci_dev_present(),
  2599. * so check again using local pointer.
  2600. */
  2601. id_table = driver_ops->id_table;
  2602. while (id_table && id_table->vendor) {
  2603. cnss_pr_info("Host driver is built for PCIe device ID 0x%x\n",
  2604. id_table->device);
  2605. id_table++;
  2606. }
  2607. cnss_pr_err("Enumerated PCIe device id is 0x%x, reject unsupported driver\n",
  2608. pci_priv->device_id);
  2609. return -ENODEV;
  2610. }
  2611. if (driver_ops->chip_version != CNSS_CHIP_VER_ANY &&
  2612. driver_ops->chip_version != plat_priv->device_version.major_version) {
  2613. cnss_pr_err("Driver built for chip ver 0x%x, enumerated ver 0x%x, reject unsupported driver\n",
  2614. driver_ops->chip_version,
  2615. plat_priv->device_version.major_version);
  2616. return -ENODEV;
  2617. }
  2618. cnss_get_driver_mode_update_fw_name(plat_priv);
  2619. set_bit(CNSS_DRIVER_REGISTER, &plat_priv->driver_state);
  2620. if (!plat_priv->cbc_enabled ||
  2621. test_bit(CNSS_COLD_BOOT_CAL_DONE, &plat_priv->driver_state))
  2622. goto register_driver;
  2623. pci_priv->driver_ops = driver_ops;
  2624. /* If Cold Boot Calibration is enabled, it is the 1st step in init
  2625. * sequence.CBC is done on file system_ready trigger. Qcacld will be
  2626. * loaded from vendor_modprobe.sh at early boot and must be deferred
  2627. * until CBC is complete
  2628. */
  2629. timeout = cnss_get_timeout(plat_priv, CNSS_TIMEOUT_CALIBRATION);
  2630. INIT_DELAYED_WORK(&plat_priv->wlan_reg_driver_work,
  2631. cnss_wlan_reg_driver_work);
  2632. schedule_delayed_work(&plat_priv->wlan_reg_driver_work,
  2633. msecs_to_jiffies(timeout));
  2634. cnss_pr_info("WLAN register driver deferred for Calibration\n");
  2635. return 0;
  2636. register_driver:
  2637. reinit_completion(&plat_priv->power_up_complete);
  2638. ret = cnss_driver_event_post(plat_priv,
  2639. CNSS_DRIVER_EVENT_REGISTER_DRIVER,
  2640. CNSS_EVENT_SYNC_UNKILLABLE,
  2641. driver_ops);
  2642. return ret;
  2643. }
  2644. EXPORT_SYMBOL(cnss_wlan_register_driver);
  2645. void cnss_wlan_unregister_driver(struct cnss_wlan_driver *driver_ops)
  2646. {
  2647. struct cnss_plat_data *plat_priv = cnss_bus_dev_to_plat_priv(NULL);
  2648. int ret = 0;
  2649. unsigned int timeout;
  2650. if (!plat_priv) {
  2651. cnss_pr_err("plat_priv is NULL\n");
  2652. return;
  2653. }
  2654. mutex_lock(&plat_priv->driver_ops_lock);
  2655. if (plat_priv->device_id == QCA6174_DEVICE_ID)
  2656. goto skip_wait_power_up;
  2657. timeout = cnss_get_timeout(plat_priv, CNSS_TIMEOUT_WLAN_WATCHDOG);
  2658. ret = wait_for_completion_timeout(&plat_priv->power_up_complete,
  2659. msecs_to_jiffies(timeout));
  2660. if (!ret) {
  2661. cnss_pr_err("Timeout (%ums) waiting for driver power up to complete\n",
  2662. timeout);
  2663. CNSS_ASSERT(0);
  2664. }
  2665. skip_wait_power_up:
  2666. if (!test_bit(CNSS_DRIVER_RECOVERY, &plat_priv->driver_state) &&
  2667. !test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state))
  2668. goto skip_wait_recovery;
  2669. reinit_completion(&plat_priv->recovery_complete);
  2670. timeout = cnss_get_timeout(plat_priv, CNSS_TIMEOUT_RECOVERY);
  2671. ret = wait_for_completion_timeout(&plat_priv->recovery_complete,
  2672. msecs_to_jiffies(timeout));
  2673. if (!ret) {
  2674. cnss_pr_err("Timeout (%ums) waiting for recovery to complete\n",
  2675. timeout);
  2676. CNSS_ASSERT(0);
  2677. }
  2678. skip_wait_recovery:
  2679. cnss_driver_event_post(plat_priv,
  2680. CNSS_DRIVER_EVENT_UNREGISTER_DRIVER,
  2681. CNSS_EVENT_SYNC_UNKILLABLE, NULL);
  2682. mutex_unlock(&plat_priv->driver_ops_lock);
  2683. }
  2684. EXPORT_SYMBOL(cnss_wlan_unregister_driver);
  2685. int cnss_pci_register_driver_hdlr(struct cnss_pci_data *pci_priv,
  2686. void *data)
  2687. {
  2688. int ret = 0;
  2689. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2690. if (test_bit(CNSS_IN_REBOOT, &plat_priv->driver_state)) {
  2691. cnss_pr_dbg("Reboot or shutdown is in progress, ignore register driver\n");
  2692. return -EINVAL;
  2693. }
  2694. set_bit(CNSS_DRIVER_LOADING, &plat_priv->driver_state);
  2695. pci_priv->driver_ops = data;
  2696. ret = cnss_pci_dev_powerup(pci_priv);
  2697. if (ret) {
  2698. clear_bit(CNSS_DRIVER_LOADING, &plat_priv->driver_state);
  2699. pci_priv->driver_ops = NULL;
  2700. }
  2701. return ret;
  2702. }
  2703. int cnss_pci_unregister_driver_hdlr(struct cnss_pci_data *pci_priv)
  2704. {
  2705. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2706. set_bit(CNSS_DRIVER_UNLOADING, &plat_priv->driver_state);
  2707. cnss_pci_dev_shutdown(pci_priv);
  2708. pci_priv->driver_ops = NULL;
  2709. return 0;
  2710. }
  2711. static int cnss_pci_suspend_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. pm_message_t state = { .event = PM_EVENT_SUSPEND };
  2717. if (driver_ops && driver_ops->suspend) {
  2718. ret = driver_ops->suspend(pci_dev, state);
  2719. if (ret) {
  2720. cnss_pr_err("Failed to suspend host driver, err = %d\n",
  2721. ret);
  2722. ret = -EAGAIN;
  2723. }
  2724. }
  2725. return ret;
  2726. }
  2727. static int cnss_pci_resume_driver(struct cnss_pci_data *pci_priv)
  2728. {
  2729. struct pci_dev *pci_dev = pci_priv->pci_dev;
  2730. struct cnss_wlan_driver *driver_ops = pci_priv->driver_ops;
  2731. int ret = 0;
  2732. if (driver_ops && driver_ops->resume) {
  2733. ret = driver_ops->resume(pci_dev);
  2734. if (ret)
  2735. cnss_pr_err("Failed to resume host driver, err = %d\n",
  2736. ret);
  2737. }
  2738. return ret;
  2739. }
  2740. int cnss_pci_suspend_bus(struct cnss_pci_data *pci_priv)
  2741. {
  2742. struct pci_dev *pci_dev = pci_priv->pci_dev;
  2743. int ret = 0;
  2744. if (pci_priv->pci_link_state == PCI_LINK_DOWN)
  2745. goto out;
  2746. if (cnss_pci_set_mhi_state(pci_priv, CNSS_MHI_SUSPEND)) {
  2747. ret = -EAGAIN;
  2748. goto out;
  2749. }
  2750. if (pci_priv->drv_connected_last)
  2751. goto skip_disable_pci;
  2752. pci_clear_master(pci_dev);
  2753. cnss_set_pci_config_space(pci_priv, SAVE_PCI_CONFIG_SPACE);
  2754. pci_disable_device(pci_dev);
  2755. ret = pci_set_power_state(pci_dev, PCI_D3hot);
  2756. if (ret)
  2757. cnss_pr_err("Failed to set D3Hot, err = %d\n", ret);
  2758. skip_disable_pci:
  2759. if (cnss_set_pci_link(pci_priv, PCI_LINK_DOWN)) {
  2760. ret = -EAGAIN;
  2761. goto resume_mhi;
  2762. }
  2763. pci_priv->pci_link_state = PCI_LINK_DOWN;
  2764. return 0;
  2765. resume_mhi:
  2766. if (!pci_is_enabled(pci_dev))
  2767. if (pci_enable_device(pci_dev))
  2768. cnss_pr_err("Failed to enable PCI device\n");
  2769. if (pci_priv->saved_state)
  2770. cnss_set_pci_config_space(pci_priv, RESTORE_PCI_CONFIG_SPACE);
  2771. pci_set_master(pci_dev);
  2772. cnss_pci_set_mhi_state(pci_priv, CNSS_MHI_RESUME);
  2773. out:
  2774. return ret;
  2775. }
  2776. int cnss_pci_resume_bus(struct cnss_pci_data *pci_priv)
  2777. {
  2778. struct pci_dev *pci_dev = pci_priv->pci_dev;
  2779. int ret = 0;
  2780. if (pci_priv->pci_link_state == PCI_LINK_UP)
  2781. goto out;
  2782. if (cnss_set_pci_link(pci_priv, PCI_LINK_UP)) {
  2783. cnss_fatal_err("Failed to resume PCI link from suspend\n");
  2784. cnss_pci_link_down(&pci_dev->dev);
  2785. ret = -EAGAIN;
  2786. goto out;
  2787. }
  2788. pci_priv->pci_link_state = PCI_LINK_UP;
  2789. if (pci_priv->drv_connected_last)
  2790. goto skip_enable_pci;
  2791. ret = pci_enable_device(pci_dev);
  2792. if (ret) {
  2793. cnss_pr_err("Failed to enable PCI device, err = %d\n",
  2794. ret);
  2795. goto out;
  2796. }
  2797. if (pci_priv->saved_state)
  2798. cnss_set_pci_config_space(pci_priv,
  2799. RESTORE_PCI_CONFIG_SPACE);
  2800. pci_set_master(pci_dev);
  2801. skip_enable_pci:
  2802. cnss_pci_set_mhi_state(pci_priv, CNSS_MHI_RESUME);
  2803. out:
  2804. return ret;
  2805. }
  2806. static int cnss_pci_suspend(struct device *dev)
  2807. {
  2808. int ret = 0;
  2809. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(to_pci_dev(dev));
  2810. struct cnss_plat_data *plat_priv;
  2811. if (!pci_priv)
  2812. goto out;
  2813. plat_priv = pci_priv->plat_priv;
  2814. if (!plat_priv)
  2815. goto out;
  2816. if (!cnss_is_device_powered_on(plat_priv))
  2817. goto out;
  2818. if (!test_bit(DISABLE_DRV, &plat_priv->ctrl_params.quirks) &&
  2819. pci_priv->drv_supported) {
  2820. pci_priv->drv_connected_last =
  2821. cnss_pci_get_drv_connected(pci_priv);
  2822. if (!pci_priv->drv_connected_last) {
  2823. cnss_pr_dbg("Firmware does not support non-DRV suspend, reject\n");
  2824. ret = -EAGAIN;
  2825. goto out;
  2826. }
  2827. }
  2828. set_bit(CNSS_IN_SUSPEND_RESUME, &plat_priv->driver_state);
  2829. ret = cnss_pci_suspend_driver(pci_priv);
  2830. if (ret)
  2831. goto clear_flag;
  2832. if (!pci_priv->disable_pc) {
  2833. mutex_lock(&pci_priv->bus_lock);
  2834. ret = cnss_pci_suspend_bus(pci_priv);
  2835. mutex_unlock(&pci_priv->bus_lock);
  2836. if (ret)
  2837. goto resume_driver;
  2838. }
  2839. cnss_pci_set_monitor_wake_intr(pci_priv, false);
  2840. return 0;
  2841. resume_driver:
  2842. cnss_pci_resume_driver(pci_priv);
  2843. clear_flag:
  2844. pci_priv->drv_connected_last = 0;
  2845. clear_bit(CNSS_IN_SUSPEND_RESUME, &plat_priv->driver_state);
  2846. out:
  2847. return ret;
  2848. }
  2849. static int cnss_pci_resume(struct device *dev)
  2850. {
  2851. int ret = 0;
  2852. struct pci_dev *pci_dev = to_pci_dev(dev);
  2853. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  2854. struct cnss_plat_data *plat_priv;
  2855. if (!pci_priv)
  2856. goto out;
  2857. plat_priv = pci_priv->plat_priv;
  2858. if (!plat_priv)
  2859. goto out;
  2860. if (pci_priv->pci_link_down_ind)
  2861. goto out;
  2862. if (!cnss_is_device_powered_on(pci_priv->plat_priv))
  2863. goto out;
  2864. if (!pci_priv->disable_pc) {
  2865. ret = cnss_pci_resume_bus(pci_priv);
  2866. if (ret)
  2867. goto out;
  2868. }
  2869. ret = cnss_pci_resume_driver(pci_priv);
  2870. pci_priv->drv_connected_last = 0;
  2871. clear_bit(CNSS_IN_SUSPEND_RESUME, &plat_priv->driver_state);
  2872. out:
  2873. return ret;
  2874. }
  2875. static int cnss_pci_suspend_noirq(struct device *dev)
  2876. {
  2877. int ret = 0;
  2878. struct pci_dev *pci_dev = to_pci_dev(dev);
  2879. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  2880. struct cnss_wlan_driver *driver_ops;
  2881. if (!pci_priv)
  2882. goto out;
  2883. if (!cnss_is_device_powered_on(pci_priv->plat_priv))
  2884. goto out;
  2885. driver_ops = pci_priv->driver_ops;
  2886. if (driver_ops && driver_ops->suspend_noirq)
  2887. ret = driver_ops->suspend_noirq(pci_dev);
  2888. if (pci_priv->disable_pc && !pci_dev->state_saved &&
  2889. !pci_priv->plat_priv->use_pm_domain)
  2890. pci_save_state(pci_dev);
  2891. out:
  2892. return ret;
  2893. }
  2894. static int cnss_pci_resume_noirq(struct device *dev)
  2895. {
  2896. int ret = 0;
  2897. struct pci_dev *pci_dev = to_pci_dev(dev);
  2898. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  2899. struct cnss_wlan_driver *driver_ops;
  2900. if (!pci_priv)
  2901. goto out;
  2902. if (!cnss_is_device_powered_on(pci_priv->plat_priv))
  2903. goto out;
  2904. driver_ops = pci_priv->driver_ops;
  2905. if (driver_ops && driver_ops->resume_noirq &&
  2906. !pci_priv->pci_link_down_ind)
  2907. ret = driver_ops->resume_noirq(pci_dev);
  2908. out:
  2909. return ret;
  2910. }
  2911. static int cnss_pci_runtime_suspend(struct device *dev)
  2912. {
  2913. int ret = 0;
  2914. struct pci_dev *pci_dev = to_pci_dev(dev);
  2915. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  2916. struct cnss_plat_data *plat_priv;
  2917. struct cnss_wlan_driver *driver_ops;
  2918. if (!pci_priv)
  2919. return -EAGAIN;
  2920. plat_priv = pci_priv->plat_priv;
  2921. if (!plat_priv)
  2922. return -EAGAIN;
  2923. if (!cnss_is_device_powered_on(pci_priv->plat_priv))
  2924. return -EAGAIN;
  2925. if (pci_priv->pci_link_down_ind) {
  2926. cnss_pr_dbg("PCI link down recovery is in progress!\n");
  2927. return -EAGAIN;
  2928. }
  2929. if (!test_bit(DISABLE_DRV, &plat_priv->ctrl_params.quirks) &&
  2930. pci_priv->drv_supported) {
  2931. pci_priv->drv_connected_last =
  2932. cnss_pci_get_drv_connected(pci_priv);
  2933. if (!pci_priv->drv_connected_last) {
  2934. cnss_pr_dbg("Firmware does not support non-DRV suspend, reject\n");
  2935. return -EAGAIN;
  2936. }
  2937. }
  2938. cnss_pr_vdbg("Runtime suspend start\n");
  2939. driver_ops = pci_priv->driver_ops;
  2940. if (driver_ops && driver_ops->runtime_ops &&
  2941. driver_ops->runtime_ops->runtime_suspend)
  2942. ret = driver_ops->runtime_ops->runtime_suspend(pci_dev);
  2943. else
  2944. ret = cnss_auto_suspend(dev);
  2945. if (ret)
  2946. pci_priv->drv_connected_last = 0;
  2947. cnss_pr_vdbg("Runtime suspend status: %d\n", ret);
  2948. return ret;
  2949. }
  2950. static int cnss_pci_runtime_resume(struct device *dev)
  2951. {
  2952. int ret = 0;
  2953. struct pci_dev *pci_dev = to_pci_dev(dev);
  2954. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  2955. struct cnss_wlan_driver *driver_ops;
  2956. if (!pci_priv)
  2957. return -EAGAIN;
  2958. if (!cnss_is_device_powered_on(pci_priv->plat_priv))
  2959. return -EAGAIN;
  2960. if (pci_priv->pci_link_down_ind) {
  2961. cnss_pr_dbg("PCI link down recovery is in progress!\n");
  2962. return -EAGAIN;
  2963. }
  2964. cnss_pr_vdbg("Runtime resume start\n");
  2965. driver_ops = pci_priv->driver_ops;
  2966. if (driver_ops && driver_ops->runtime_ops &&
  2967. driver_ops->runtime_ops->runtime_resume)
  2968. ret = driver_ops->runtime_ops->runtime_resume(pci_dev);
  2969. else
  2970. ret = cnss_auto_resume(dev);
  2971. if (!ret)
  2972. pci_priv->drv_connected_last = 0;
  2973. cnss_pr_vdbg("Runtime resume status: %d\n", ret);
  2974. return ret;
  2975. }
  2976. static int cnss_pci_runtime_idle(struct device *dev)
  2977. {
  2978. cnss_pr_vdbg("Runtime idle\n");
  2979. pm_request_autosuspend(dev);
  2980. return -EBUSY;
  2981. }
  2982. int cnss_wlan_pm_control(struct device *dev, bool vote)
  2983. {
  2984. struct pci_dev *pci_dev = to_pci_dev(dev);
  2985. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  2986. int ret = 0;
  2987. if (!pci_priv)
  2988. return -ENODEV;
  2989. ret = cnss_pci_disable_pc(pci_priv, vote);
  2990. if (ret)
  2991. return ret;
  2992. pci_priv->disable_pc = vote;
  2993. cnss_pr_dbg("%s PCIe power collapse\n", vote ? "disable" : "enable");
  2994. return 0;
  2995. }
  2996. EXPORT_SYMBOL(cnss_wlan_pm_control);
  2997. static void cnss_pci_pm_runtime_get_record(struct cnss_pci_data *pci_priv,
  2998. enum cnss_rtpm_id id)
  2999. {
  3000. if (id >= RTPM_ID_MAX)
  3001. return;
  3002. atomic_inc(&pci_priv->pm_stats.runtime_get);
  3003. atomic_inc(&pci_priv->pm_stats.runtime_get_id[id]);
  3004. pci_priv->pm_stats.runtime_get_timestamp_id[id] =
  3005. cnss_get_host_timestamp(pci_priv->plat_priv);
  3006. }
  3007. static void cnss_pci_pm_runtime_put_record(struct cnss_pci_data *pci_priv,
  3008. enum cnss_rtpm_id id)
  3009. {
  3010. if (id >= RTPM_ID_MAX)
  3011. return;
  3012. atomic_inc(&pci_priv->pm_stats.runtime_put);
  3013. atomic_inc(&pci_priv->pm_stats.runtime_put_id[id]);
  3014. pci_priv->pm_stats.runtime_put_timestamp_id[id] =
  3015. cnss_get_host_timestamp(pci_priv->plat_priv);
  3016. }
  3017. void cnss_pci_pm_runtime_show_usage_count(struct cnss_pci_data *pci_priv)
  3018. {
  3019. struct device *dev;
  3020. if (!pci_priv)
  3021. return;
  3022. dev = &pci_priv->pci_dev->dev;
  3023. cnss_pr_dbg("Runtime PM usage count: %d\n",
  3024. atomic_read(&dev->power.usage_count));
  3025. }
  3026. int cnss_pci_pm_request_resume(struct cnss_pci_data *pci_priv)
  3027. {
  3028. struct device *dev;
  3029. enum rpm_status status;
  3030. if (!pci_priv)
  3031. return -ENODEV;
  3032. dev = &pci_priv->pci_dev->dev;
  3033. status = dev->power.runtime_status;
  3034. if (status == RPM_SUSPENDING || status == RPM_SUSPENDED)
  3035. cnss_pr_vdbg("Runtime PM resume is requested by %ps\n",
  3036. (void *)_RET_IP_);
  3037. return pm_request_resume(dev);
  3038. }
  3039. int cnss_pci_pm_runtime_resume(struct cnss_pci_data *pci_priv)
  3040. {
  3041. struct device *dev;
  3042. enum rpm_status status;
  3043. if (!pci_priv)
  3044. return -ENODEV;
  3045. dev = &pci_priv->pci_dev->dev;
  3046. status = dev->power.runtime_status;
  3047. if (status == RPM_SUSPENDING || status == RPM_SUSPENDED)
  3048. cnss_pr_vdbg("Runtime PM resume is requested by %ps\n",
  3049. (void *)_RET_IP_);
  3050. return pm_runtime_resume(dev);
  3051. }
  3052. int cnss_pci_pm_runtime_get(struct cnss_pci_data *pci_priv,
  3053. enum cnss_rtpm_id id)
  3054. {
  3055. struct device *dev;
  3056. enum rpm_status status;
  3057. if (!pci_priv)
  3058. return -ENODEV;
  3059. dev = &pci_priv->pci_dev->dev;
  3060. status = dev->power.runtime_status;
  3061. if (status == RPM_SUSPENDING || status == RPM_SUSPENDED)
  3062. cnss_pr_vdbg("Runtime PM resume is requested by %ps\n",
  3063. (void *)_RET_IP_);
  3064. cnss_pci_pm_runtime_get_record(pci_priv, id);
  3065. return pm_runtime_get(dev);
  3066. }
  3067. int cnss_pci_pm_runtime_get_sync(struct cnss_pci_data *pci_priv,
  3068. enum cnss_rtpm_id id)
  3069. {
  3070. struct device *dev;
  3071. enum rpm_status status;
  3072. if (!pci_priv)
  3073. return -ENODEV;
  3074. dev = &pci_priv->pci_dev->dev;
  3075. status = dev->power.runtime_status;
  3076. if (status == RPM_SUSPENDING || status == RPM_SUSPENDED)
  3077. cnss_pr_vdbg("Runtime PM resume is requested by %ps\n",
  3078. (void *)_RET_IP_);
  3079. cnss_pci_pm_runtime_get_record(pci_priv, id);
  3080. return pm_runtime_get_sync(dev);
  3081. }
  3082. void cnss_pci_pm_runtime_get_noresume(struct cnss_pci_data *pci_priv,
  3083. enum cnss_rtpm_id id)
  3084. {
  3085. if (!pci_priv)
  3086. return;
  3087. cnss_pci_pm_runtime_get_record(pci_priv, id);
  3088. pm_runtime_get_noresume(&pci_priv->pci_dev->dev);
  3089. }
  3090. int cnss_pci_pm_runtime_put_autosuspend(struct cnss_pci_data *pci_priv,
  3091. enum cnss_rtpm_id id)
  3092. {
  3093. struct device *dev;
  3094. if (!pci_priv)
  3095. return -ENODEV;
  3096. dev = &pci_priv->pci_dev->dev;
  3097. if (atomic_read(&dev->power.usage_count) == 0) {
  3098. cnss_pr_dbg("Ignore excessive runtime PM put operation\n");
  3099. return -EINVAL;
  3100. }
  3101. cnss_pci_pm_runtime_put_record(pci_priv, id);
  3102. return pm_runtime_put_autosuspend(&pci_priv->pci_dev->dev);
  3103. }
  3104. void cnss_pci_pm_runtime_put_noidle(struct cnss_pci_data *pci_priv,
  3105. enum cnss_rtpm_id id)
  3106. {
  3107. struct device *dev;
  3108. if (!pci_priv)
  3109. return;
  3110. dev = &pci_priv->pci_dev->dev;
  3111. if (atomic_read(&dev->power.usage_count) == 0) {
  3112. cnss_pr_dbg("Ignore excessive runtime PM put operation\n");
  3113. return;
  3114. }
  3115. cnss_pci_pm_runtime_put_record(pci_priv, id);
  3116. pm_runtime_put_noidle(&pci_priv->pci_dev->dev);
  3117. }
  3118. void cnss_pci_pm_runtime_mark_last_busy(struct cnss_pci_data *pci_priv)
  3119. {
  3120. if (!pci_priv)
  3121. return;
  3122. pm_runtime_mark_last_busy(&pci_priv->pci_dev->dev);
  3123. }
  3124. int cnss_auto_suspend(struct device *dev)
  3125. {
  3126. int ret = 0;
  3127. struct pci_dev *pci_dev = to_pci_dev(dev);
  3128. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  3129. struct cnss_plat_data *plat_priv;
  3130. if (!pci_priv)
  3131. return -ENODEV;
  3132. plat_priv = pci_priv->plat_priv;
  3133. if (!plat_priv)
  3134. return -ENODEV;
  3135. mutex_lock(&pci_priv->bus_lock);
  3136. if (!pci_priv->qmi_send_usage_count) {
  3137. ret = cnss_pci_suspend_bus(pci_priv);
  3138. if (ret) {
  3139. mutex_unlock(&pci_priv->bus_lock);
  3140. return ret;
  3141. }
  3142. }
  3143. cnss_pci_set_auto_suspended(pci_priv, 1);
  3144. mutex_unlock(&pci_priv->bus_lock);
  3145. cnss_pci_set_monitor_wake_intr(pci_priv, true);
  3146. /* For suspend temporarily set bandwidth vote to NONE and dont save in
  3147. * current_bw_vote as in resume path we should vote for last used
  3148. * bandwidth vote. Also ignore error if bw voting is not setup.
  3149. */
  3150. cnss_setup_bus_bandwidth(plat_priv, CNSS_BUS_WIDTH_NONE, false);
  3151. return 0;
  3152. }
  3153. EXPORT_SYMBOL(cnss_auto_suspend);
  3154. int cnss_auto_resume(struct device *dev)
  3155. {
  3156. int ret = 0;
  3157. struct pci_dev *pci_dev = to_pci_dev(dev);
  3158. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  3159. struct cnss_plat_data *plat_priv;
  3160. if (!pci_priv)
  3161. return -ENODEV;
  3162. plat_priv = pci_priv->plat_priv;
  3163. if (!plat_priv)
  3164. return -ENODEV;
  3165. mutex_lock(&pci_priv->bus_lock);
  3166. ret = cnss_pci_resume_bus(pci_priv);
  3167. if (ret) {
  3168. mutex_unlock(&pci_priv->bus_lock);
  3169. return ret;
  3170. }
  3171. cnss_pci_set_auto_suspended(pci_priv, 0);
  3172. mutex_unlock(&pci_priv->bus_lock);
  3173. cnss_request_bus_bandwidth(dev, plat_priv->icc.current_bw_vote);
  3174. return 0;
  3175. }
  3176. EXPORT_SYMBOL(cnss_auto_resume);
  3177. int cnss_pci_force_wake_request_sync(struct device *dev, int timeout_us)
  3178. {
  3179. struct pci_dev *pci_dev = to_pci_dev(dev);
  3180. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  3181. struct cnss_plat_data *plat_priv;
  3182. struct mhi_controller *mhi_ctrl;
  3183. if (!pci_priv)
  3184. return -ENODEV;
  3185. switch (pci_priv->device_id) {
  3186. case QCA6390_DEVICE_ID:
  3187. case QCA6490_DEVICE_ID:
  3188. case KIWI_DEVICE_ID:
  3189. case MANGO_DEVICE_ID:
  3190. break;
  3191. default:
  3192. return 0;
  3193. }
  3194. mhi_ctrl = pci_priv->mhi_ctrl;
  3195. if (!mhi_ctrl)
  3196. return -EINVAL;
  3197. plat_priv = pci_priv->plat_priv;
  3198. if (!plat_priv)
  3199. return -ENODEV;
  3200. if (test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state))
  3201. return -EAGAIN;
  3202. if (timeout_us) {
  3203. /* Busy wait for timeout_us */
  3204. return cnss_mhi_device_get_sync_atomic(pci_priv,
  3205. timeout_us, false);
  3206. } else {
  3207. /* Sleep wait for mhi_ctrl->timeout_ms */
  3208. return mhi_device_get_sync(mhi_ctrl->mhi_dev);
  3209. }
  3210. }
  3211. EXPORT_SYMBOL(cnss_pci_force_wake_request_sync);
  3212. int cnss_pci_force_wake_request(struct device *dev)
  3213. {
  3214. struct pci_dev *pci_dev = to_pci_dev(dev);
  3215. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  3216. struct cnss_plat_data *plat_priv;
  3217. struct mhi_controller *mhi_ctrl;
  3218. if (!pci_priv)
  3219. return -ENODEV;
  3220. switch (pci_priv->device_id) {
  3221. case QCA6390_DEVICE_ID:
  3222. case QCA6490_DEVICE_ID:
  3223. case KIWI_DEVICE_ID:
  3224. case MANGO_DEVICE_ID:
  3225. break;
  3226. default:
  3227. return 0;
  3228. }
  3229. mhi_ctrl = pci_priv->mhi_ctrl;
  3230. if (!mhi_ctrl)
  3231. return -EINVAL;
  3232. plat_priv = pci_priv->plat_priv;
  3233. if (!plat_priv)
  3234. return -ENODEV;
  3235. if (test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state))
  3236. return -EAGAIN;
  3237. mhi_device_get(mhi_ctrl->mhi_dev);
  3238. return 0;
  3239. }
  3240. EXPORT_SYMBOL(cnss_pci_force_wake_request);
  3241. int cnss_pci_is_device_awake(struct device *dev)
  3242. {
  3243. struct pci_dev *pci_dev = to_pci_dev(dev);
  3244. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  3245. struct mhi_controller *mhi_ctrl;
  3246. if (!pci_priv)
  3247. return -ENODEV;
  3248. switch (pci_priv->device_id) {
  3249. case QCA6390_DEVICE_ID:
  3250. case QCA6490_DEVICE_ID:
  3251. case KIWI_DEVICE_ID:
  3252. case MANGO_DEVICE_ID:
  3253. break;
  3254. default:
  3255. return 0;
  3256. }
  3257. mhi_ctrl = pci_priv->mhi_ctrl;
  3258. if (!mhi_ctrl)
  3259. return -EINVAL;
  3260. return (mhi_ctrl->dev_state == MHI_STATE_M0);
  3261. }
  3262. EXPORT_SYMBOL(cnss_pci_is_device_awake);
  3263. int cnss_pci_force_wake_release(struct device *dev)
  3264. {
  3265. struct pci_dev *pci_dev = to_pci_dev(dev);
  3266. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  3267. struct cnss_plat_data *plat_priv;
  3268. struct mhi_controller *mhi_ctrl;
  3269. if (!pci_priv)
  3270. return -ENODEV;
  3271. switch (pci_priv->device_id) {
  3272. case QCA6390_DEVICE_ID:
  3273. case QCA6490_DEVICE_ID:
  3274. case KIWI_DEVICE_ID:
  3275. case MANGO_DEVICE_ID:
  3276. break;
  3277. default:
  3278. return 0;
  3279. }
  3280. mhi_ctrl = pci_priv->mhi_ctrl;
  3281. if (!mhi_ctrl)
  3282. return -EINVAL;
  3283. plat_priv = pci_priv->plat_priv;
  3284. if (!plat_priv)
  3285. return -ENODEV;
  3286. if (test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state))
  3287. return -EAGAIN;
  3288. mhi_device_put(mhi_ctrl->mhi_dev);
  3289. return 0;
  3290. }
  3291. EXPORT_SYMBOL(cnss_pci_force_wake_release);
  3292. int cnss_pci_qmi_send_get(struct cnss_pci_data *pci_priv)
  3293. {
  3294. int ret = 0;
  3295. if (!pci_priv)
  3296. return -ENODEV;
  3297. mutex_lock(&pci_priv->bus_lock);
  3298. if (cnss_pci_get_auto_suspended(pci_priv) &&
  3299. !pci_priv->qmi_send_usage_count)
  3300. ret = cnss_pci_resume_bus(pci_priv);
  3301. pci_priv->qmi_send_usage_count++;
  3302. cnss_pr_buf("Increased QMI send usage count to %d\n",
  3303. pci_priv->qmi_send_usage_count);
  3304. mutex_unlock(&pci_priv->bus_lock);
  3305. return ret;
  3306. }
  3307. int cnss_pci_qmi_send_put(struct cnss_pci_data *pci_priv)
  3308. {
  3309. int ret = 0;
  3310. if (!pci_priv)
  3311. return -ENODEV;
  3312. mutex_lock(&pci_priv->bus_lock);
  3313. if (pci_priv->qmi_send_usage_count)
  3314. pci_priv->qmi_send_usage_count--;
  3315. cnss_pr_buf("Decreased QMI send usage count to %d\n",
  3316. pci_priv->qmi_send_usage_count);
  3317. if (cnss_pci_get_auto_suspended(pci_priv) &&
  3318. !pci_priv->qmi_send_usage_count &&
  3319. !cnss_pcie_is_device_down(pci_priv))
  3320. ret = cnss_pci_suspend_bus(pci_priv);
  3321. mutex_unlock(&pci_priv->bus_lock);
  3322. return ret;
  3323. }
  3324. int cnss_pci_alloc_fw_mem(struct cnss_pci_data *pci_priv)
  3325. {
  3326. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  3327. struct cnss_fw_mem *fw_mem = plat_priv->fw_mem;
  3328. struct device *dev = &pci_priv->pci_dev->dev;
  3329. int i;
  3330. for (i = 0; i < plat_priv->fw_mem_seg_len; i++) {
  3331. if (!fw_mem[i].va && fw_mem[i].size) {
  3332. retry:
  3333. fw_mem[i].va =
  3334. dma_alloc_attrs(dev, fw_mem[i].size,
  3335. &fw_mem[i].pa, GFP_KERNEL,
  3336. fw_mem[i].attrs);
  3337. if (!fw_mem[i].va) {
  3338. if ((fw_mem[i].attrs &
  3339. DMA_ATTR_FORCE_CONTIGUOUS)) {
  3340. fw_mem[i].attrs &=
  3341. ~DMA_ATTR_FORCE_CONTIGUOUS;
  3342. cnss_pr_dbg("Fallback to non-contiguous memory for FW, Mem type: %u\n",
  3343. fw_mem[i].type);
  3344. goto retry;
  3345. }
  3346. cnss_pr_err("Failed to allocate memory for FW, size: 0x%zx, type: %u\n",
  3347. fw_mem[i].size, fw_mem[i].type);
  3348. CNSS_ASSERT(0);
  3349. return -ENOMEM;
  3350. }
  3351. }
  3352. }
  3353. return 0;
  3354. }
  3355. static void cnss_pci_free_fw_mem(struct cnss_pci_data *pci_priv)
  3356. {
  3357. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  3358. struct cnss_fw_mem *fw_mem = plat_priv->fw_mem;
  3359. struct device *dev = &pci_priv->pci_dev->dev;
  3360. int i;
  3361. for (i = 0; i < plat_priv->fw_mem_seg_len; i++) {
  3362. if (fw_mem[i].va && fw_mem[i].size) {
  3363. cnss_pr_dbg("Freeing memory for FW, va: 0x%pK, pa: %pa, size: 0x%zx, type: %u\n",
  3364. fw_mem[i].va, &fw_mem[i].pa,
  3365. fw_mem[i].size, fw_mem[i].type);
  3366. dma_free_attrs(dev, fw_mem[i].size,
  3367. fw_mem[i].va, fw_mem[i].pa,
  3368. fw_mem[i].attrs);
  3369. fw_mem[i].va = NULL;
  3370. fw_mem[i].pa = 0;
  3371. fw_mem[i].size = 0;
  3372. fw_mem[i].type = 0;
  3373. }
  3374. }
  3375. plat_priv->fw_mem_seg_len = 0;
  3376. }
  3377. int cnss_pci_alloc_qdss_mem(struct cnss_pci_data *pci_priv)
  3378. {
  3379. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  3380. struct cnss_fw_mem *qdss_mem = plat_priv->qdss_mem;
  3381. int i, j;
  3382. for (i = 0; i < plat_priv->qdss_mem_seg_len; i++) {
  3383. if (!qdss_mem[i].va && qdss_mem[i].size) {
  3384. qdss_mem[i].va =
  3385. dma_alloc_coherent(&pci_priv->pci_dev->dev,
  3386. qdss_mem[i].size,
  3387. &qdss_mem[i].pa,
  3388. GFP_KERNEL);
  3389. if (!qdss_mem[i].va) {
  3390. cnss_pr_err("Failed to allocate QDSS memory for FW, size: 0x%zx, type: %u, chuck-ID: %d\n",
  3391. qdss_mem[i].size,
  3392. qdss_mem[i].type, i);
  3393. break;
  3394. }
  3395. }
  3396. }
  3397. /* Best-effort allocation for QDSS trace */
  3398. if (i < plat_priv->qdss_mem_seg_len) {
  3399. for (j = i; j < plat_priv->qdss_mem_seg_len; j++) {
  3400. qdss_mem[j].type = 0;
  3401. qdss_mem[j].size = 0;
  3402. }
  3403. plat_priv->qdss_mem_seg_len = i;
  3404. }
  3405. return 0;
  3406. }
  3407. void cnss_pci_free_qdss_mem(struct cnss_pci_data *pci_priv)
  3408. {
  3409. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  3410. struct cnss_fw_mem *qdss_mem = plat_priv->qdss_mem;
  3411. int i;
  3412. for (i = 0; i < plat_priv->qdss_mem_seg_len; i++) {
  3413. if (qdss_mem[i].va && qdss_mem[i].size) {
  3414. cnss_pr_dbg("Freeing memory for QDSS: pa: %pa, size: 0x%zx, type: %u\n",
  3415. &qdss_mem[i].pa, qdss_mem[i].size,
  3416. qdss_mem[i].type);
  3417. dma_free_coherent(&pci_priv->pci_dev->dev,
  3418. qdss_mem[i].size, qdss_mem[i].va,
  3419. qdss_mem[i].pa);
  3420. qdss_mem[i].va = NULL;
  3421. qdss_mem[i].pa = 0;
  3422. qdss_mem[i].size = 0;
  3423. qdss_mem[i].type = 0;
  3424. }
  3425. }
  3426. plat_priv->qdss_mem_seg_len = 0;
  3427. }
  3428. int cnss_pci_load_m3(struct cnss_pci_data *pci_priv)
  3429. {
  3430. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  3431. struct cnss_fw_mem *m3_mem = &plat_priv->m3_mem;
  3432. char filename[MAX_FIRMWARE_NAME_LEN];
  3433. char *phy_filename = DEFAULT_PHY_UCODE_FILE_NAME;
  3434. const struct firmware *fw_entry;
  3435. int ret = 0;
  3436. /* Use forward compatibility here since for any recent device
  3437. * it should use DEFAULT_PHY_UCODE_FILE_NAME.
  3438. */
  3439. switch (pci_priv->device_id) {
  3440. case QCA6174_DEVICE_ID:
  3441. cnss_pr_err("Invalid device ID (0x%x) to load phy image\n",
  3442. pci_priv->device_id);
  3443. return -EINVAL;
  3444. case QCA6290_DEVICE_ID:
  3445. case QCA6390_DEVICE_ID:
  3446. case QCA6490_DEVICE_ID:
  3447. phy_filename = DEFAULT_PHY_M3_FILE_NAME;
  3448. break;
  3449. case KIWI_DEVICE_ID:
  3450. case MANGO_DEVICE_ID:
  3451. switch (plat_priv->device_version.major_version) {
  3452. case FW_V2_NUMBER:
  3453. phy_filename = PHY_UCODE_V2_FILE_NAME;
  3454. break;
  3455. default:
  3456. break;
  3457. }
  3458. break;
  3459. default:
  3460. break;
  3461. }
  3462. if (!m3_mem->va && !m3_mem->size) {
  3463. cnss_pci_add_fw_prefix_name(pci_priv, filename,
  3464. phy_filename);
  3465. ret = firmware_request_nowarn(&fw_entry, filename,
  3466. &pci_priv->pci_dev->dev);
  3467. if (ret) {
  3468. cnss_pr_err("Failed to load M3 image: %s\n", filename);
  3469. return ret;
  3470. }
  3471. m3_mem->va = dma_alloc_coherent(&pci_priv->pci_dev->dev,
  3472. fw_entry->size, &m3_mem->pa,
  3473. GFP_KERNEL);
  3474. if (!m3_mem->va) {
  3475. cnss_pr_err("Failed to allocate memory for M3, size: 0x%zx\n",
  3476. fw_entry->size);
  3477. release_firmware(fw_entry);
  3478. return -ENOMEM;
  3479. }
  3480. memcpy(m3_mem->va, fw_entry->data, fw_entry->size);
  3481. m3_mem->size = fw_entry->size;
  3482. release_firmware(fw_entry);
  3483. }
  3484. return 0;
  3485. }
  3486. static void cnss_pci_free_m3_mem(struct cnss_pci_data *pci_priv)
  3487. {
  3488. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  3489. struct cnss_fw_mem *m3_mem = &plat_priv->m3_mem;
  3490. if (m3_mem->va && m3_mem->size) {
  3491. cnss_pr_dbg("Freeing memory for M3, va: 0x%pK, pa: %pa, size: 0x%zx\n",
  3492. m3_mem->va, &m3_mem->pa, m3_mem->size);
  3493. dma_free_coherent(&pci_priv->pci_dev->dev, m3_mem->size,
  3494. m3_mem->va, m3_mem->pa);
  3495. }
  3496. m3_mem->va = NULL;
  3497. m3_mem->pa = 0;
  3498. m3_mem->size = 0;
  3499. }
  3500. void cnss_pci_fw_boot_timeout_hdlr(struct cnss_pci_data *pci_priv)
  3501. {
  3502. struct cnss_plat_data *plat_priv;
  3503. if (!pci_priv)
  3504. return;
  3505. cnss_fatal_err("Timeout waiting for FW ready indication\n");
  3506. plat_priv = pci_priv->plat_priv;
  3507. if (!plat_priv)
  3508. return;
  3509. if (test_bit(CNSS_IN_COLD_BOOT_CAL, &plat_priv->driver_state)) {
  3510. cnss_pr_dbg("Ignore FW ready timeout for calibration mode\n");
  3511. return;
  3512. }
  3513. cnss_schedule_recovery(&pci_priv->pci_dev->dev,
  3514. CNSS_REASON_TIMEOUT);
  3515. }
  3516. static void cnss_pci_deinit_smmu(struct cnss_pci_data *pci_priv)
  3517. {
  3518. pci_priv->iommu_domain = NULL;
  3519. }
  3520. int cnss_pci_get_iova(struct cnss_pci_data *pci_priv, u64 *addr, u64 *size)
  3521. {
  3522. if (!pci_priv)
  3523. return -ENODEV;
  3524. if (!pci_priv->smmu_iova_len)
  3525. return -EINVAL;
  3526. *addr = pci_priv->smmu_iova_start;
  3527. *size = pci_priv->smmu_iova_len;
  3528. return 0;
  3529. }
  3530. int cnss_pci_get_iova_ipa(struct cnss_pci_data *pci_priv, u64 *addr, u64 *size)
  3531. {
  3532. if (!pci_priv)
  3533. return -ENODEV;
  3534. if (!pci_priv->smmu_iova_ipa_len)
  3535. return -EINVAL;
  3536. *addr = pci_priv->smmu_iova_ipa_start;
  3537. *size = pci_priv->smmu_iova_ipa_len;
  3538. return 0;
  3539. }
  3540. struct iommu_domain *cnss_smmu_get_domain(struct device *dev)
  3541. {
  3542. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(to_pci_dev(dev));
  3543. if (!pci_priv)
  3544. return NULL;
  3545. return pci_priv->iommu_domain;
  3546. }
  3547. EXPORT_SYMBOL(cnss_smmu_get_domain);
  3548. int cnss_smmu_map(struct device *dev,
  3549. phys_addr_t paddr, uint32_t *iova_addr, size_t size)
  3550. {
  3551. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(to_pci_dev(dev));
  3552. struct cnss_plat_data *plat_priv;
  3553. unsigned long iova;
  3554. size_t len;
  3555. int ret = 0;
  3556. int flag = IOMMU_READ | IOMMU_WRITE;
  3557. struct pci_dev *root_port;
  3558. struct device_node *root_of_node;
  3559. bool dma_coherent = false;
  3560. if (!pci_priv)
  3561. return -ENODEV;
  3562. if (!iova_addr) {
  3563. cnss_pr_err("iova_addr is NULL, paddr %pa, size %zu\n",
  3564. &paddr, size);
  3565. return -EINVAL;
  3566. }
  3567. plat_priv = pci_priv->plat_priv;
  3568. len = roundup(size + paddr - rounddown(paddr, PAGE_SIZE), PAGE_SIZE);
  3569. iova = roundup(pci_priv->smmu_iova_ipa_current, PAGE_SIZE);
  3570. if (pci_priv->iommu_geometry &&
  3571. iova >= pci_priv->smmu_iova_ipa_start +
  3572. pci_priv->smmu_iova_ipa_len) {
  3573. cnss_pr_err("No IOVA space to map, iova %lx, smmu_iova_ipa_start %pad, smmu_iova_ipa_len %zu\n",
  3574. iova,
  3575. &pci_priv->smmu_iova_ipa_start,
  3576. pci_priv->smmu_iova_ipa_len);
  3577. return -ENOMEM;
  3578. }
  3579. if (!test_bit(DISABLE_IO_COHERENCY,
  3580. &plat_priv->ctrl_params.quirks)) {
  3581. root_port = pcie_find_root_port(pci_priv->pci_dev);
  3582. if (!root_port) {
  3583. cnss_pr_err("Root port is null, so dma_coherent is disabled\n");
  3584. } else {
  3585. root_of_node = root_port->dev.of_node;
  3586. if (root_of_node && root_of_node->parent) {
  3587. dma_coherent =
  3588. of_property_read_bool(root_of_node->parent,
  3589. "dma-coherent");
  3590. cnss_pr_dbg("dma-coherent is %s\n",
  3591. dma_coherent ? "enabled" : "disabled");
  3592. if (dma_coherent)
  3593. flag |= IOMMU_CACHE;
  3594. }
  3595. }
  3596. }
  3597. cnss_pr_dbg("IOMMU map: iova %lx, len %zu\n", iova, len);
  3598. ret = iommu_map(pci_priv->iommu_domain, iova,
  3599. rounddown(paddr, PAGE_SIZE), len, flag);
  3600. if (ret) {
  3601. cnss_pr_err("PA to IOVA mapping failed, ret %d\n", ret);
  3602. return ret;
  3603. }
  3604. pci_priv->smmu_iova_ipa_current = iova + len;
  3605. *iova_addr = (uint32_t)(iova + paddr - rounddown(paddr, PAGE_SIZE));
  3606. cnss_pr_dbg("IOMMU map: iova_addr %lx\n", *iova_addr);
  3607. return 0;
  3608. }
  3609. EXPORT_SYMBOL(cnss_smmu_map);
  3610. int cnss_smmu_unmap(struct device *dev, uint32_t iova_addr, size_t size)
  3611. {
  3612. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(to_pci_dev(dev));
  3613. unsigned long iova;
  3614. size_t unmapped;
  3615. size_t len;
  3616. if (!pci_priv)
  3617. return -ENODEV;
  3618. iova = rounddown(iova_addr, PAGE_SIZE);
  3619. len = roundup(size + iova_addr - iova, PAGE_SIZE);
  3620. if (iova >= pci_priv->smmu_iova_ipa_start +
  3621. pci_priv->smmu_iova_ipa_len) {
  3622. cnss_pr_err("Out of IOVA space to unmap, iova %lx, smmu_iova_ipa_start %pad, smmu_iova_ipa_len %zu\n",
  3623. iova,
  3624. &pci_priv->smmu_iova_ipa_start,
  3625. pci_priv->smmu_iova_ipa_len);
  3626. return -ENOMEM;
  3627. }
  3628. cnss_pr_dbg("IOMMU unmap: iova %lx, len %zu\n", iova, len);
  3629. unmapped = iommu_unmap(pci_priv->iommu_domain, iova, len);
  3630. if (unmapped != len) {
  3631. cnss_pr_err("IOMMU unmap failed, unmapped = %zu, requested = %zu\n",
  3632. unmapped, len);
  3633. return -EINVAL;
  3634. }
  3635. pci_priv->smmu_iova_ipa_current = iova;
  3636. return 0;
  3637. }
  3638. EXPORT_SYMBOL(cnss_smmu_unmap);
  3639. int cnss_get_soc_info(struct device *dev, struct cnss_soc_info *info)
  3640. {
  3641. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(to_pci_dev(dev));
  3642. struct cnss_plat_data *plat_priv;
  3643. if (!pci_priv)
  3644. return -ENODEV;
  3645. plat_priv = pci_priv->plat_priv;
  3646. if (!plat_priv)
  3647. return -ENODEV;
  3648. info->va = pci_priv->bar;
  3649. info->pa = pci_resource_start(pci_priv->pci_dev, PCI_BAR_NUM);
  3650. info->chip_id = plat_priv->chip_info.chip_id;
  3651. info->chip_family = plat_priv->chip_info.chip_family;
  3652. info->board_id = plat_priv->board_info.board_id;
  3653. info->soc_id = plat_priv->soc_info.soc_id;
  3654. info->fw_version = plat_priv->fw_version_info.fw_version;
  3655. strlcpy(info->fw_build_timestamp,
  3656. plat_priv->fw_version_info.fw_build_timestamp,
  3657. sizeof(info->fw_build_timestamp));
  3658. memcpy(&info->device_version, &plat_priv->device_version,
  3659. sizeof(info->device_version));
  3660. memcpy(&info->dev_mem_info, &plat_priv->dev_mem_info,
  3661. sizeof(info->dev_mem_info));
  3662. return 0;
  3663. }
  3664. EXPORT_SYMBOL(cnss_get_soc_info);
  3665. static int cnss_pci_enable_msi(struct cnss_pci_data *pci_priv)
  3666. {
  3667. int ret = 0;
  3668. struct pci_dev *pci_dev = pci_priv->pci_dev;
  3669. int num_vectors;
  3670. struct cnss_msi_config *msi_config;
  3671. struct msi_desc *msi_desc;
  3672. if (pci_priv->device_id == QCA6174_DEVICE_ID)
  3673. return 0;
  3674. ret = cnss_pci_get_msi_assignment(pci_priv);
  3675. if (ret) {
  3676. cnss_pr_err("Failed to get MSI assignment, err = %d\n", ret);
  3677. goto out;
  3678. }
  3679. msi_config = pci_priv->msi_config;
  3680. if (!msi_config) {
  3681. cnss_pr_err("msi_config is NULL!\n");
  3682. ret = -EINVAL;
  3683. goto out;
  3684. }
  3685. num_vectors = pci_alloc_irq_vectors(pci_dev,
  3686. msi_config->total_vectors,
  3687. msi_config->total_vectors,
  3688. PCI_IRQ_MSI);
  3689. if (num_vectors != msi_config->total_vectors) {
  3690. cnss_pr_err("Failed to get enough MSI vectors (%d), available vectors = %d",
  3691. msi_config->total_vectors, num_vectors);
  3692. if (num_vectors >= 0)
  3693. ret = -EINVAL;
  3694. goto reset_msi_config;
  3695. }
  3696. msi_desc = irq_get_msi_desc(pci_dev->irq);
  3697. if (!msi_desc) {
  3698. cnss_pr_err("msi_desc is NULL!\n");
  3699. ret = -EINVAL;
  3700. goto free_msi_vector;
  3701. }
  3702. pci_priv->msi_ep_base_data = msi_desc->msg.data;
  3703. cnss_pr_dbg("MSI base data is %d\n", pci_priv->msi_ep_base_data);
  3704. return 0;
  3705. free_msi_vector:
  3706. pci_free_irq_vectors(pci_priv->pci_dev);
  3707. reset_msi_config:
  3708. pci_priv->msi_config = NULL;
  3709. out:
  3710. return ret;
  3711. }
  3712. static void cnss_pci_disable_msi(struct cnss_pci_data *pci_priv)
  3713. {
  3714. if (pci_priv->device_id == QCA6174_DEVICE_ID)
  3715. return;
  3716. pci_free_irq_vectors(pci_priv->pci_dev);
  3717. }
  3718. int cnss_get_user_msi_assignment(struct device *dev, char *user_name,
  3719. int *num_vectors, u32 *user_base_data,
  3720. u32 *base_vector)
  3721. {
  3722. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(to_pci_dev(dev));
  3723. struct cnss_msi_config *msi_config;
  3724. int idx;
  3725. if (!pci_priv)
  3726. return -ENODEV;
  3727. msi_config = pci_priv->msi_config;
  3728. if (!msi_config) {
  3729. cnss_pr_err("MSI is not supported.\n");
  3730. return -EINVAL;
  3731. }
  3732. for (idx = 0; idx < msi_config->total_users; idx++) {
  3733. if (strcmp(user_name, msi_config->users[idx].name) == 0) {
  3734. *num_vectors = msi_config->users[idx].num_vectors;
  3735. *user_base_data = msi_config->users[idx].base_vector
  3736. + pci_priv->msi_ep_base_data;
  3737. *base_vector = msi_config->users[idx].base_vector;
  3738. /*Add only single print for each user*/
  3739. if (print_optimize.msi_log_chk[idx]++)
  3740. goto skip_print;
  3741. cnss_pr_dbg("Assign MSI to user: %s, num_vectors: %d, user_base_data: %u, base_vector: %u\n",
  3742. user_name, *num_vectors, *user_base_data,
  3743. *base_vector);
  3744. skip_print:
  3745. return 0;
  3746. }
  3747. }
  3748. cnss_pr_err("Failed to find MSI assignment for %s!\n", user_name);
  3749. return -EINVAL;
  3750. }
  3751. EXPORT_SYMBOL(cnss_get_user_msi_assignment);
  3752. int cnss_get_msi_irq(struct device *dev, unsigned int vector)
  3753. {
  3754. struct pci_dev *pci_dev = to_pci_dev(dev);
  3755. int irq_num;
  3756. irq_num = pci_irq_vector(pci_dev, vector);
  3757. cnss_pr_dbg("Get IRQ number %d for vector index %d\n", irq_num, vector);
  3758. return irq_num;
  3759. }
  3760. EXPORT_SYMBOL(cnss_get_msi_irq);
  3761. void cnss_get_msi_address(struct device *dev, u32 *msi_addr_low,
  3762. u32 *msi_addr_high)
  3763. {
  3764. struct pci_dev *pci_dev = to_pci_dev(dev);
  3765. u16 control;
  3766. pci_read_config_word(pci_dev, pci_dev->msi_cap + PCI_MSI_FLAGS,
  3767. &control);
  3768. pci_read_config_dword(pci_dev, pci_dev->msi_cap + PCI_MSI_ADDRESS_LO,
  3769. msi_addr_low);
  3770. /* Return MSI high address only when device supports 64-bit MSI */
  3771. if (control & PCI_MSI_FLAGS_64BIT)
  3772. pci_read_config_dword(pci_dev,
  3773. pci_dev->msi_cap + PCI_MSI_ADDRESS_HI,
  3774. msi_addr_high);
  3775. else
  3776. *msi_addr_high = 0;
  3777. /*Add only single print as the address is constant*/
  3778. if (!print_optimize.msi_addr_chk++)
  3779. cnss_pr_dbg("Get MSI low addr = 0x%x, high addr = 0x%x\n",
  3780. *msi_addr_low, *msi_addr_high);
  3781. }
  3782. EXPORT_SYMBOL(cnss_get_msi_address);
  3783. u32 cnss_pci_get_wake_msi(struct cnss_pci_data *pci_priv)
  3784. {
  3785. int ret, num_vectors;
  3786. u32 user_base_data, base_vector;
  3787. if (!pci_priv)
  3788. return -ENODEV;
  3789. ret = cnss_get_user_msi_assignment(&pci_priv->pci_dev->dev,
  3790. WAKE_MSI_NAME, &num_vectors,
  3791. &user_base_data, &base_vector);
  3792. if (ret) {
  3793. cnss_pr_err("WAKE MSI is not valid\n");
  3794. return 0;
  3795. }
  3796. return user_base_data;
  3797. }
  3798. static int cnss_pci_enable_bus(struct cnss_pci_data *pci_priv)
  3799. {
  3800. int ret = 0;
  3801. struct pci_dev *pci_dev = pci_priv->pci_dev;
  3802. u16 device_id;
  3803. pci_read_config_word(pci_dev, PCI_DEVICE_ID, &device_id);
  3804. if (device_id != pci_priv->pci_device_id->device) {
  3805. cnss_pr_err("PCI device ID mismatch, config ID: 0x%x, probe ID: 0x%x\n",
  3806. device_id, pci_priv->pci_device_id->device);
  3807. ret = -EIO;
  3808. goto out;
  3809. }
  3810. ret = pci_assign_resource(pci_dev, PCI_BAR_NUM);
  3811. if (ret) {
  3812. pr_err("Failed to assign PCI resource, err = %d\n", ret);
  3813. goto out;
  3814. }
  3815. ret = pci_enable_device(pci_dev);
  3816. if (ret) {
  3817. cnss_pr_err("Failed to enable PCI device, err = %d\n", ret);
  3818. goto out;
  3819. }
  3820. ret = pci_request_region(pci_dev, PCI_BAR_NUM, "cnss");
  3821. if (ret) {
  3822. cnss_pr_err("Failed to request PCI region, err = %d\n", ret);
  3823. goto disable_device;
  3824. }
  3825. switch (device_id) {
  3826. case QCA6174_DEVICE_ID:
  3827. pci_priv->dma_bit_mask = PCI_DMA_MASK_32_BIT;
  3828. break;
  3829. case QCA6390_DEVICE_ID:
  3830. case QCA6490_DEVICE_ID:
  3831. case KIWI_DEVICE_ID:
  3832. case MANGO_DEVICE_ID:
  3833. pci_priv->dma_bit_mask = PCI_DMA_MASK_36_BIT;
  3834. break;
  3835. default:
  3836. pci_priv->dma_bit_mask = PCI_DMA_MASK_32_BIT;
  3837. break;
  3838. }
  3839. cnss_pr_dbg("Set PCI DMA MASK (0x%llx)\n", pci_priv->dma_bit_mask);
  3840. ret = pci_set_dma_mask(pci_dev, pci_priv->dma_bit_mask);
  3841. if (ret) {
  3842. cnss_pr_err("Failed to set PCI DMA mask, err = %d\n", ret);
  3843. goto release_region;
  3844. }
  3845. ret = pci_set_consistent_dma_mask(pci_dev, pci_priv->dma_bit_mask);
  3846. if (ret) {
  3847. cnss_pr_err("Failed to set PCI consistent DMA mask, err = %d\n",
  3848. ret);
  3849. goto release_region;
  3850. }
  3851. pci_priv->bar = pci_iomap(pci_dev, PCI_BAR_NUM, 0);
  3852. if (!pci_priv->bar) {
  3853. cnss_pr_err("Failed to do PCI IO map!\n");
  3854. ret = -EIO;
  3855. goto release_region;
  3856. }
  3857. /* Save default config space without BME enabled */
  3858. pci_save_state(pci_dev);
  3859. pci_priv->default_state = pci_store_saved_state(pci_dev);
  3860. pci_set_master(pci_dev);
  3861. return 0;
  3862. release_region:
  3863. pci_release_region(pci_dev, PCI_BAR_NUM);
  3864. disable_device:
  3865. pci_disable_device(pci_dev);
  3866. out:
  3867. return ret;
  3868. }
  3869. static void cnss_pci_disable_bus(struct cnss_pci_data *pci_priv)
  3870. {
  3871. struct pci_dev *pci_dev = pci_priv->pci_dev;
  3872. pci_clear_master(pci_dev);
  3873. pci_load_and_free_saved_state(pci_dev, &pci_priv->saved_state);
  3874. pci_load_and_free_saved_state(pci_dev, &pci_priv->default_state);
  3875. if (pci_priv->bar) {
  3876. pci_iounmap(pci_dev, pci_priv->bar);
  3877. pci_priv->bar = NULL;
  3878. }
  3879. pci_release_region(pci_dev, PCI_BAR_NUM);
  3880. if (pci_is_enabled(pci_dev))
  3881. pci_disable_device(pci_dev);
  3882. }
  3883. static void cnss_pci_dump_qdss_reg(struct cnss_pci_data *pci_priv)
  3884. {
  3885. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  3886. int i, array_size = ARRAY_SIZE(qdss_csr) - 1;
  3887. gfp_t gfp = GFP_KERNEL;
  3888. u32 reg_offset;
  3889. if (in_interrupt() || irqs_disabled())
  3890. gfp = GFP_ATOMIC;
  3891. if (!plat_priv->qdss_reg) {
  3892. plat_priv->qdss_reg = devm_kzalloc(&pci_priv->pci_dev->dev,
  3893. sizeof(*plat_priv->qdss_reg)
  3894. * array_size, gfp);
  3895. if (!plat_priv->qdss_reg)
  3896. return;
  3897. }
  3898. cnss_pr_dbg("Start to dump qdss registers\n");
  3899. for (i = 0; qdss_csr[i].name; i++) {
  3900. reg_offset = QDSS_APB_DEC_CSR_BASE + qdss_csr[i].offset;
  3901. if (cnss_pci_reg_read(pci_priv, reg_offset,
  3902. &plat_priv->qdss_reg[i]))
  3903. return;
  3904. cnss_pr_dbg("%s[0x%x] = 0x%x\n", qdss_csr[i].name, reg_offset,
  3905. plat_priv->qdss_reg[i]);
  3906. }
  3907. }
  3908. static void cnss_pci_dump_ce_reg(struct cnss_pci_data *pci_priv,
  3909. enum cnss_ce_index ce)
  3910. {
  3911. int i;
  3912. u32 ce_base = ce * CE_REG_INTERVAL;
  3913. u32 reg_offset, src_ring_base, dst_ring_base, cmn_base, val;
  3914. switch (pci_priv->device_id) {
  3915. case QCA6390_DEVICE_ID:
  3916. src_ring_base = QCA6390_CE_SRC_RING_REG_BASE;
  3917. dst_ring_base = QCA6390_CE_DST_RING_REG_BASE;
  3918. cmn_base = QCA6390_CE_COMMON_REG_BASE;
  3919. break;
  3920. case QCA6490_DEVICE_ID:
  3921. src_ring_base = QCA6490_CE_SRC_RING_REG_BASE;
  3922. dst_ring_base = QCA6490_CE_DST_RING_REG_BASE;
  3923. cmn_base = QCA6490_CE_COMMON_REG_BASE;
  3924. break;
  3925. default:
  3926. return;
  3927. }
  3928. switch (ce) {
  3929. case CNSS_CE_09:
  3930. case CNSS_CE_10:
  3931. for (i = 0; ce_src[i].name; i++) {
  3932. reg_offset = src_ring_base + ce_base + ce_src[i].offset;
  3933. if (cnss_pci_reg_read(pci_priv, reg_offset, &val))
  3934. return;
  3935. cnss_pr_dbg("CE_%02d_%s[0x%x] = 0x%x\n",
  3936. ce, ce_src[i].name, reg_offset, val);
  3937. }
  3938. for (i = 0; ce_dst[i].name; i++) {
  3939. reg_offset = dst_ring_base + ce_base + ce_dst[i].offset;
  3940. if (cnss_pci_reg_read(pci_priv, reg_offset, &val))
  3941. return;
  3942. cnss_pr_dbg("CE_%02d_%s[0x%x] = 0x%x\n",
  3943. ce, ce_dst[i].name, reg_offset, val);
  3944. }
  3945. break;
  3946. case CNSS_CE_COMMON:
  3947. for (i = 0; ce_cmn[i].name; i++) {
  3948. reg_offset = cmn_base + ce_cmn[i].offset;
  3949. if (cnss_pci_reg_read(pci_priv, reg_offset, &val))
  3950. return;
  3951. cnss_pr_dbg("CE_COMMON_%s[0x%x] = 0x%x\n",
  3952. ce_cmn[i].name, reg_offset, val);
  3953. }
  3954. break;
  3955. default:
  3956. cnss_pr_err("Unsupported CE[%d] registers dump\n", ce);
  3957. }
  3958. }
  3959. static void cnss_pci_dump_debug_reg(struct cnss_pci_data *pci_priv)
  3960. {
  3961. if (cnss_pci_check_link_status(pci_priv))
  3962. return;
  3963. cnss_pr_dbg("Start to dump debug registers\n");
  3964. cnss_mhi_debug_reg_dump(pci_priv);
  3965. cnss_pci_soc_scratch_reg_dump(pci_priv);
  3966. cnss_pci_dump_ce_reg(pci_priv, CNSS_CE_COMMON);
  3967. cnss_pci_dump_ce_reg(pci_priv, CNSS_CE_09);
  3968. cnss_pci_dump_ce_reg(pci_priv, CNSS_CE_10);
  3969. }
  3970. static int cnss_pci_assert_host_sol(struct cnss_pci_data *pci_priv)
  3971. {
  3972. if (cnss_get_host_sol_value(pci_priv->plat_priv))
  3973. return -EINVAL;
  3974. cnss_pr_dbg("Assert host SOL GPIO to retry RDDM, expecting link down\n");
  3975. cnss_set_host_sol_value(pci_priv->plat_priv, 1);
  3976. return 0;
  3977. }
  3978. int cnss_pci_force_fw_assert_hdlr(struct cnss_pci_data *pci_priv)
  3979. {
  3980. int ret;
  3981. struct cnss_plat_data *plat_priv;
  3982. if (!pci_priv)
  3983. return -ENODEV;
  3984. plat_priv = pci_priv->plat_priv;
  3985. if (!plat_priv)
  3986. return -ENODEV;
  3987. if (!test_bit(CNSS_MHI_POWER_ON, &pci_priv->mhi_state) ||
  3988. test_bit(CNSS_MHI_POWERING_OFF, &pci_priv->mhi_state))
  3989. return -EINVAL;
  3990. cnss_auto_resume(&pci_priv->pci_dev->dev);
  3991. if (!cnss_pci_check_link_status(pci_priv))
  3992. cnss_mhi_debug_reg_dump(pci_priv);
  3993. cnss_pci_soc_scratch_reg_dump(pci_priv);
  3994. cnss_pci_dump_misc_reg(pci_priv);
  3995. cnss_pci_dump_shadow_reg(pci_priv);
  3996. /* If link is still down here, directly trigger link down recovery */
  3997. ret = cnss_pci_check_link_status(pci_priv);
  3998. if (ret) {
  3999. cnss_pci_link_down(&pci_priv->pci_dev->dev);
  4000. return 0;
  4001. }
  4002. ret = cnss_pci_set_mhi_state(pci_priv, CNSS_MHI_TRIGGER_RDDM);
  4003. if (ret) {
  4004. if (!test_bit(CNSS_MHI_POWER_ON, &pci_priv->mhi_state) ||
  4005. test_bit(CNSS_MHI_POWERING_OFF, &pci_priv->mhi_state)) {
  4006. cnss_pr_dbg("MHI is not powered on, ignore RDDM failure\n");
  4007. return 0;
  4008. }
  4009. cnss_fatal_err("Failed to trigger RDDM, err = %d\n", ret);
  4010. if (!cnss_pci_assert_host_sol(pci_priv))
  4011. return 0;
  4012. cnss_pci_dump_debug_reg(pci_priv);
  4013. cnss_schedule_recovery(&pci_priv->pci_dev->dev,
  4014. CNSS_REASON_DEFAULT);
  4015. return ret;
  4016. }
  4017. if (!test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state)) {
  4018. mod_timer(&pci_priv->dev_rddm_timer,
  4019. jiffies + msecs_to_jiffies(DEV_RDDM_TIMEOUT));
  4020. }
  4021. return 0;
  4022. }
  4023. static void cnss_pci_add_dump_seg(struct cnss_pci_data *pci_priv,
  4024. struct cnss_dump_seg *dump_seg,
  4025. enum cnss_fw_dump_type type, int seg_no,
  4026. void *va, dma_addr_t dma, size_t size)
  4027. {
  4028. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  4029. struct device *dev = &pci_priv->pci_dev->dev;
  4030. phys_addr_t pa;
  4031. dump_seg->address = dma;
  4032. dump_seg->v_address = va;
  4033. dump_seg->size = size;
  4034. dump_seg->type = type;
  4035. cnss_pr_dbg("Seg: %x, va: %pK, dma: %pa, size: 0x%zx\n",
  4036. seg_no, va, &dma, size);
  4037. if (cnss_va_to_pa(dev, size, va, dma, &pa, DMA_ATTR_FORCE_CONTIGUOUS))
  4038. return;
  4039. cnss_minidump_add_region(plat_priv, type, seg_no, va, pa, size);
  4040. }
  4041. static void cnss_pci_remove_dump_seg(struct cnss_pci_data *pci_priv,
  4042. struct cnss_dump_seg *dump_seg,
  4043. enum cnss_fw_dump_type type, int seg_no,
  4044. void *va, dma_addr_t dma, size_t size)
  4045. {
  4046. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  4047. struct device *dev = &pci_priv->pci_dev->dev;
  4048. phys_addr_t pa;
  4049. cnss_va_to_pa(dev, size, va, dma, &pa, DMA_ATTR_FORCE_CONTIGUOUS);
  4050. cnss_minidump_remove_region(plat_priv, type, seg_no, va, pa, size);
  4051. }
  4052. int cnss_pci_call_driver_uevent(struct cnss_pci_data *pci_priv,
  4053. enum cnss_driver_status status, void *data)
  4054. {
  4055. struct cnss_uevent_data uevent_data;
  4056. struct cnss_wlan_driver *driver_ops;
  4057. driver_ops = pci_priv->driver_ops;
  4058. if (!driver_ops || !driver_ops->update_event) {
  4059. cnss_pr_dbg("Hang event driver ops is NULL\n");
  4060. return -EINVAL;
  4061. }
  4062. cnss_pr_dbg("Calling driver uevent: %d\n", status);
  4063. uevent_data.status = status;
  4064. uevent_data.data = data;
  4065. return driver_ops->update_event(pci_priv->pci_dev, &uevent_data);
  4066. }
  4067. static void cnss_pci_send_hang_event(struct cnss_pci_data *pci_priv)
  4068. {
  4069. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  4070. struct cnss_fw_mem *fw_mem = plat_priv->fw_mem;
  4071. struct cnss_hang_event hang_event;
  4072. void *hang_data_va = NULL;
  4073. u64 offset = 0;
  4074. u16 length = 0;
  4075. int i = 0;
  4076. if (!fw_mem || !plat_priv->fw_mem_seg_len)
  4077. return;
  4078. memset(&hang_event, 0, sizeof(hang_event));
  4079. switch (pci_priv->device_id) {
  4080. case QCA6390_DEVICE_ID:
  4081. offset = HST_HANG_DATA_OFFSET;
  4082. length = HANG_DATA_LENGTH;
  4083. break;
  4084. case QCA6490_DEVICE_ID:
  4085. /* Fallback to hard-coded values if hang event params not
  4086. * present in QMI. Once all the firmware branches have the
  4087. * fix to send params over QMI, this can be removed.
  4088. */
  4089. if (plat_priv->hang_event_data_len) {
  4090. offset = plat_priv->hang_data_addr_offset;
  4091. length = plat_priv->hang_event_data_len;
  4092. } else {
  4093. offset = HSP_HANG_DATA_OFFSET;
  4094. length = HANG_DATA_LENGTH;
  4095. }
  4096. break;
  4097. case KIWI_DEVICE_ID:
  4098. case MANGO_DEVICE_ID:
  4099. offset = plat_priv->hang_data_addr_offset;
  4100. length = plat_priv->hang_event_data_len;
  4101. break;
  4102. default:
  4103. cnss_pr_err("Skip Hang Event Data as unsupported Device ID received: %d\n",
  4104. pci_priv->device_id);
  4105. return;
  4106. }
  4107. for (i = 0; i < plat_priv->fw_mem_seg_len; i++) {
  4108. if (fw_mem[i].type == QMI_WLFW_MEM_TYPE_DDR_V01 &&
  4109. fw_mem[i].va) {
  4110. /* The offset must be < (fw_mem size- hangdata length) */
  4111. if (!(offset <= fw_mem[i].size - length))
  4112. goto exit;
  4113. hang_data_va = fw_mem[i].va + offset;
  4114. hang_event.hang_event_data = kmemdup(hang_data_va,
  4115. length,
  4116. GFP_ATOMIC);
  4117. if (!hang_event.hang_event_data) {
  4118. cnss_pr_dbg("Hang data memory alloc failed\n");
  4119. return;
  4120. }
  4121. hang_event.hang_event_data_len = length;
  4122. break;
  4123. }
  4124. }
  4125. cnss_pci_call_driver_uevent(pci_priv, CNSS_HANG_EVENT, &hang_event);
  4126. kfree(hang_event.hang_event_data);
  4127. hang_event.hang_event_data = NULL;
  4128. return;
  4129. exit:
  4130. cnss_pr_dbg("Invalid hang event params, offset:0x%x, length:0x%x\n",
  4131. plat_priv->hang_data_addr_offset,
  4132. plat_priv->hang_event_data_len);
  4133. }
  4134. void cnss_pci_collect_dump_info(struct cnss_pci_data *pci_priv, bool in_panic)
  4135. {
  4136. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  4137. struct cnss_dump_data *dump_data =
  4138. &plat_priv->ramdump_info_v2.dump_data;
  4139. struct cnss_dump_seg *dump_seg =
  4140. plat_priv->ramdump_info_v2.dump_data_vaddr;
  4141. struct image_info *fw_image, *rddm_image;
  4142. struct cnss_fw_mem *fw_mem = plat_priv->fw_mem;
  4143. int ret, i, j;
  4144. if (test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state) &&
  4145. !test_bit(CNSS_IN_PANIC, &plat_priv->driver_state))
  4146. cnss_pci_send_hang_event(pci_priv);
  4147. if (test_bit(CNSS_MHI_RDDM_DONE, &pci_priv->mhi_state)) {
  4148. cnss_pr_dbg("RAM dump is already collected, skip\n");
  4149. return;
  4150. }
  4151. if (!cnss_is_device_powered_on(plat_priv)) {
  4152. cnss_pr_dbg("Device is already powered off, skip\n");
  4153. return;
  4154. }
  4155. if (!in_panic) {
  4156. mutex_lock(&pci_priv->bus_lock);
  4157. ret = cnss_pci_check_link_status(pci_priv);
  4158. if (ret) {
  4159. if (ret != -EACCES) {
  4160. mutex_unlock(&pci_priv->bus_lock);
  4161. return;
  4162. }
  4163. if (cnss_pci_resume_bus(pci_priv)) {
  4164. mutex_unlock(&pci_priv->bus_lock);
  4165. return;
  4166. }
  4167. }
  4168. mutex_unlock(&pci_priv->bus_lock);
  4169. } else {
  4170. if (cnss_pci_check_link_status(pci_priv))
  4171. return;
  4172. /* Inside panic handler, reduce timeout for RDDM to avoid
  4173. * unnecessary hypervisor watchdog bite.
  4174. */
  4175. pci_priv->mhi_ctrl->timeout_ms /= 2;
  4176. }
  4177. cnss_mhi_debug_reg_dump(pci_priv);
  4178. cnss_pci_soc_scratch_reg_dump(pci_priv);
  4179. cnss_pci_dump_misc_reg(pci_priv);
  4180. cnss_pci_dump_shadow_reg(pci_priv);
  4181. ret = mhi_download_rddm_image(pci_priv->mhi_ctrl, in_panic);
  4182. if (ret) {
  4183. cnss_fatal_err("Failed to download RDDM image, err = %d\n",
  4184. ret);
  4185. if (!cnss_pci_assert_host_sol(pci_priv))
  4186. return;
  4187. cnss_pci_dump_debug_reg(pci_priv);
  4188. return;
  4189. }
  4190. fw_image = pci_priv->mhi_ctrl->fbc_image;
  4191. rddm_image = pci_priv->mhi_ctrl->rddm_image;
  4192. dump_data->nentries = 0;
  4193. if (plat_priv->qdss_mem_seg_len)
  4194. cnss_pci_dump_qdss_reg(pci_priv);
  4195. cnss_mhi_dump_sfr(pci_priv);
  4196. if (!dump_seg) {
  4197. cnss_pr_warn("FW image dump collection not setup");
  4198. goto skip_dump;
  4199. }
  4200. cnss_pr_dbg("Collect FW image dump segment, nentries %d\n",
  4201. fw_image->entries);
  4202. for (i = 0; i < fw_image->entries; i++) {
  4203. cnss_pci_add_dump_seg(pci_priv, dump_seg, CNSS_FW_IMAGE, i,
  4204. fw_image->mhi_buf[i].buf,
  4205. fw_image->mhi_buf[i].dma_addr,
  4206. fw_image->mhi_buf[i].len);
  4207. dump_seg++;
  4208. }
  4209. dump_data->nentries += fw_image->entries;
  4210. cnss_pr_dbg("Collect RDDM image dump segment, nentries %d\n",
  4211. rddm_image->entries);
  4212. for (i = 0; i < rddm_image->entries; i++) {
  4213. cnss_pci_add_dump_seg(pci_priv, dump_seg, CNSS_FW_RDDM, i,
  4214. rddm_image->mhi_buf[i].buf,
  4215. rddm_image->mhi_buf[i].dma_addr,
  4216. rddm_image->mhi_buf[i].len);
  4217. dump_seg++;
  4218. }
  4219. dump_data->nentries += rddm_image->entries;
  4220. for (i = 0, j = 0; i < plat_priv->fw_mem_seg_len; i++) {
  4221. if (fw_mem[i].type == CNSS_MEM_TYPE_DDR) {
  4222. if (fw_mem[i].attrs & DMA_ATTR_FORCE_CONTIGUOUS) {
  4223. cnss_pr_dbg("Collect remote heap dump segment\n");
  4224. cnss_pci_add_dump_seg(pci_priv, dump_seg,
  4225. CNSS_FW_REMOTE_HEAP, j,
  4226. fw_mem[i].va,
  4227. fw_mem[i].pa,
  4228. fw_mem[i].size);
  4229. dump_seg++;
  4230. dump_data->nentries++;
  4231. j++;
  4232. } else {
  4233. cnss_pr_dbg("Skip remote heap dumps as it is non-contiguous\n");
  4234. }
  4235. }
  4236. }
  4237. if (dump_data->nentries > 0)
  4238. plat_priv->ramdump_info_v2.dump_data_valid = true;
  4239. cnss_pci_set_mhi_state(pci_priv, CNSS_MHI_RDDM_DONE);
  4240. skip_dump:
  4241. complete(&plat_priv->rddm_complete);
  4242. }
  4243. void cnss_pci_clear_dump_info(struct cnss_pci_data *pci_priv)
  4244. {
  4245. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  4246. struct cnss_dump_seg *dump_seg =
  4247. plat_priv->ramdump_info_v2.dump_data_vaddr;
  4248. struct image_info *fw_image, *rddm_image;
  4249. struct cnss_fw_mem *fw_mem = plat_priv->fw_mem;
  4250. int i, j;
  4251. if (!dump_seg)
  4252. return;
  4253. fw_image = pci_priv->mhi_ctrl->fbc_image;
  4254. rddm_image = pci_priv->mhi_ctrl->rddm_image;
  4255. for (i = 0; i < fw_image->entries; i++) {
  4256. cnss_pci_remove_dump_seg(pci_priv, dump_seg, CNSS_FW_IMAGE, i,
  4257. fw_image->mhi_buf[i].buf,
  4258. fw_image->mhi_buf[i].dma_addr,
  4259. fw_image->mhi_buf[i].len);
  4260. dump_seg++;
  4261. }
  4262. for (i = 0; i < rddm_image->entries; i++) {
  4263. cnss_pci_remove_dump_seg(pci_priv, dump_seg, CNSS_FW_RDDM, i,
  4264. rddm_image->mhi_buf[i].buf,
  4265. rddm_image->mhi_buf[i].dma_addr,
  4266. rddm_image->mhi_buf[i].len);
  4267. dump_seg++;
  4268. }
  4269. for (i = 0, j = 0; i < plat_priv->fw_mem_seg_len; i++) {
  4270. if (fw_mem[i].type == CNSS_MEM_TYPE_DDR &&
  4271. (fw_mem[i].attrs & DMA_ATTR_FORCE_CONTIGUOUS)) {
  4272. cnss_pci_remove_dump_seg(pci_priv, dump_seg,
  4273. CNSS_FW_REMOTE_HEAP, j,
  4274. fw_mem[i].va, fw_mem[i].pa,
  4275. fw_mem[i].size);
  4276. dump_seg++;
  4277. j++;
  4278. }
  4279. }
  4280. plat_priv->ramdump_info_v2.dump_data.nentries = 0;
  4281. plat_priv->ramdump_info_v2.dump_data_valid = false;
  4282. }
  4283. void cnss_pci_device_crashed(struct cnss_pci_data *pci_priv)
  4284. {
  4285. if (!pci_priv)
  4286. return;
  4287. cnss_device_crashed(&pci_priv->pci_dev->dev);
  4288. }
  4289. static int cnss_mhi_pm_runtime_get(struct mhi_controller *mhi_ctrl)
  4290. {
  4291. struct cnss_pci_data *pci_priv = dev_get_drvdata(mhi_ctrl->cntrl_dev);
  4292. return cnss_pci_pm_runtime_get(pci_priv, RTPM_ID_MHI);
  4293. }
  4294. static void cnss_mhi_pm_runtime_put_noidle(struct mhi_controller *mhi_ctrl)
  4295. {
  4296. struct cnss_pci_data *pci_priv = dev_get_drvdata(mhi_ctrl->cntrl_dev);
  4297. cnss_pci_pm_runtime_put_noidle(pci_priv, RTPM_ID_MHI);
  4298. }
  4299. void cnss_pci_add_fw_prefix_name(struct cnss_pci_data *pci_priv,
  4300. char *prefix_name, char *name)
  4301. {
  4302. struct cnss_plat_data *plat_priv;
  4303. if (!pci_priv)
  4304. return;
  4305. plat_priv = pci_priv->plat_priv;
  4306. if (!plat_priv->use_fw_path_with_prefix) {
  4307. scnprintf(prefix_name, MAX_FIRMWARE_NAME_LEN, "%s", name);
  4308. return;
  4309. }
  4310. switch (pci_priv->device_id) {
  4311. case QCA6390_DEVICE_ID:
  4312. scnprintf(prefix_name, MAX_FIRMWARE_NAME_LEN,
  4313. QCA6390_PATH_PREFIX "%s", name);
  4314. break;
  4315. case QCA6490_DEVICE_ID:
  4316. scnprintf(prefix_name, MAX_FIRMWARE_NAME_LEN,
  4317. QCA6490_PATH_PREFIX "%s", name);
  4318. break;
  4319. case KIWI_DEVICE_ID:
  4320. scnprintf(prefix_name, MAX_FIRMWARE_NAME_LEN,
  4321. KIWI_PATH_PREFIX "%s", name);
  4322. break;
  4323. case MANGO_DEVICE_ID:
  4324. scnprintf(prefix_name, MAX_FIRMWARE_NAME_LEN,
  4325. MANGO_PATH_PREFIX "%s", name);
  4326. break;
  4327. default:
  4328. scnprintf(prefix_name, MAX_FIRMWARE_NAME_LEN, "%s", name);
  4329. break;
  4330. }
  4331. cnss_pr_dbg("FW name added with prefix: %s\n", prefix_name);
  4332. }
  4333. static int cnss_pci_update_fw_name(struct cnss_pci_data *pci_priv)
  4334. {
  4335. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  4336. switch (pci_priv->device_id) {
  4337. case QCA6390_DEVICE_ID:
  4338. if (plat_priv->device_version.major_version < FW_V2_NUMBER) {
  4339. cnss_pr_dbg("Device ID:version (0x%lx:%d) is not supported\n",
  4340. pci_priv->device_id,
  4341. plat_priv->device_version.major_version);
  4342. return -EINVAL;
  4343. }
  4344. cnss_pci_add_fw_prefix_name(pci_priv, plat_priv->firmware_name,
  4345. FW_V2_FILE_NAME);
  4346. snprintf(plat_priv->fw_fallback_name, MAX_FIRMWARE_NAME_LEN,
  4347. FW_V2_FILE_NAME);
  4348. break;
  4349. case QCA6490_DEVICE_ID:
  4350. switch (plat_priv->device_version.major_version) {
  4351. case FW_V2_NUMBER:
  4352. cnss_pci_add_fw_prefix_name(pci_priv,
  4353. plat_priv->firmware_name,
  4354. FW_V2_FILE_NAME);
  4355. snprintf(plat_priv->fw_fallback_name,
  4356. MAX_FIRMWARE_NAME_LEN,
  4357. FW_V2_FILE_NAME);
  4358. break;
  4359. default:
  4360. cnss_pci_add_fw_prefix_name(pci_priv,
  4361. plat_priv->firmware_name,
  4362. DEFAULT_FW_FILE_NAME);
  4363. snprintf(plat_priv->fw_fallback_name,
  4364. MAX_FIRMWARE_NAME_LEN,
  4365. DEFAULT_FW_FILE_NAME);
  4366. break;
  4367. }
  4368. break;
  4369. case KIWI_DEVICE_ID:
  4370. case MANGO_DEVICE_ID:
  4371. switch (plat_priv->device_version.major_version) {
  4372. case FW_V2_NUMBER:
  4373. /*
  4374. * kiwiv2 using seprate fw binary for MM and FTM mode,
  4375. * platform driver loads corresponding binary according
  4376. * to current mode indicated by wlan driver. Otherwise
  4377. * use default binary.
  4378. * Mission mode using same binary name as before,
  4379. * if seprate binary is not there, fall back to default.
  4380. */
  4381. if (plat_priv->driver_mode == CNSS_MISSION) {
  4382. cnss_pci_add_fw_prefix_name(pci_priv,
  4383. plat_priv->firmware_name,
  4384. FW_V2_FILE_NAME);
  4385. cnss_pci_add_fw_prefix_name(pci_priv,
  4386. plat_priv->fw_fallback_name,
  4387. FW_V2_FILE_NAME);
  4388. } else if (plat_priv->driver_mode == CNSS_FTM) {
  4389. cnss_pci_add_fw_prefix_name(pci_priv,
  4390. plat_priv->firmware_name,
  4391. FW_V2_FTM_FILE_NAME);
  4392. cnss_pci_add_fw_prefix_name(pci_priv,
  4393. plat_priv->fw_fallback_name,
  4394. FW_V2_FILE_NAME);
  4395. } else {
  4396. /*
  4397. * Since during cold boot calibration phase,
  4398. * wlan driver has not registered, so default
  4399. * fw binary will be used.
  4400. */
  4401. cnss_pci_add_fw_prefix_name(pci_priv,
  4402. plat_priv->firmware_name,
  4403. FW_V2_FILE_NAME);
  4404. snprintf(plat_priv->fw_fallback_name,
  4405. MAX_FIRMWARE_NAME_LEN,
  4406. FW_V2_FILE_NAME);
  4407. }
  4408. break;
  4409. default:
  4410. cnss_pci_add_fw_prefix_name(pci_priv,
  4411. plat_priv->firmware_name,
  4412. DEFAULT_FW_FILE_NAME);
  4413. snprintf(plat_priv->fw_fallback_name,
  4414. MAX_FIRMWARE_NAME_LEN,
  4415. DEFAULT_FW_FILE_NAME);
  4416. break;
  4417. }
  4418. break;
  4419. default:
  4420. cnss_pci_add_fw_prefix_name(pci_priv, plat_priv->firmware_name,
  4421. DEFAULT_FW_FILE_NAME);
  4422. snprintf(plat_priv->fw_fallback_name, MAX_FIRMWARE_NAME_LEN,
  4423. DEFAULT_FW_FILE_NAME);
  4424. break;
  4425. }
  4426. cnss_pr_dbg("FW name is %s, FW fallback name is %s\n",
  4427. plat_priv->firmware_name, plat_priv->fw_fallback_name);
  4428. return 0;
  4429. }
  4430. static char *cnss_mhi_notify_status_to_str(enum mhi_callback status)
  4431. {
  4432. switch (status) {
  4433. case MHI_CB_IDLE:
  4434. return "IDLE";
  4435. case MHI_CB_EE_RDDM:
  4436. return "RDDM";
  4437. case MHI_CB_SYS_ERROR:
  4438. return "SYS_ERROR";
  4439. case MHI_CB_FATAL_ERROR:
  4440. return "FATAL_ERROR";
  4441. case MHI_CB_EE_MISSION_MODE:
  4442. return "MISSION_MODE";
  4443. #if IS_ENABLED(CONFIG_MHI_BUS_MISC)
  4444. case MHI_CB_FALLBACK_IMG:
  4445. return "FW_FALLBACK";
  4446. #endif
  4447. default:
  4448. return "UNKNOWN";
  4449. }
  4450. };
  4451. static void cnss_dev_rddm_timeout_hdlr(struct timer_list *t)
  4452. {
  4453. struct cnss_pci_data *pci_priv =
  4454. from_timer(pci_priv, t, dev_rddm_timer);
  4455. enum mhi_ee_type mhi_ee;
  4456. if (!pci_priv)
  4457. return;
  4458. cnss_fatal_err("Timeout waiting for RDDM notification\n");
  4459. if (!cnss_pci_assert_host_sol(pci_priv))
  4460. return;
  4461. mhi_ee = mhi_get_exec_env(pci_priv->mhi_ctrl);
  4462. if (mhi_ee == MHI_EE_PBL)
  4463. cnss_pr_err("Unable to collect ramdumps due to abrupt reset\n");
  4464. if (mhi_ee == MHI_EE_RDDM) {
  4465. cnss_pr_info("Device MHI EE is RDDM, try to collect dump\n");
  4466. cnss_schedule_recovery(&pci_priv->pci_dev->dev,
  4467. CNSS_REASON_RDDM);
  4468. } else {
  4469. cnss_mhi_debug_reg_dump(pci_priv);
  4470. cnss_pci_soc_scratch_reg_dump(pci_priv);
  4471. cnss_schedule_recovery(&pci_priv->pci_dev->dev,
  4472. CNSS_REASON_TIMEOUT);
  4473. }
  4474. }
  4475. static void cnss_boot_debug_timeout_hdlr(struct timer_list *t)
  4476. {
  4477. struct cnss_pci_data *pci_priv =
  4478. from_timer(pci_priv, t, boot_debug_timer);
  4479. if (!pci_priv)
  4480. return;
  4481. if (cnss_pci_check_link_status(pci_priv))
  4482. return;
  4483. if (cnss_pci_is_device_down(&pci_priv->pci_dev->dev))
  4484. return;
  4485. if (test_bit(CNSS_MHI_POWER_ON, &pci_priv->mhi_state))
  4486. return;
  4487. if (cnss_mhi_scan_rddm_cookie(pci_priv, DEVICE_RDDM_COOKIE))
  4488. return;
  4489. cnss_pr_dbg("Dump MHI/PBL/SBL debug data every %ds during MHI power on\n",
  4490. BOOT_DEBUG_TIMEOUT_MS / 1000);
  4491. cnss_mhi_debug_reg_dump(pci_priv);
  4492. cnss_pci_soc_scratch_reg_dump(pci_priv);
  4493. cnss_pci_dump_bl_sram_mem(pci_priv);
  4494. mod_timer(&pci_priv->boot_debug_timer,
  4495. jiffies + msecs_to_jiffies(BOOT_DEBUG_TIMEOUT_MS));
  4496. }
  4497. static int cnss_pci_handle_mhi_sys_err(struct cnss_pci_data *pci_priv)
  4498. {
  4499. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  4500. cnss_ignore_qmi_failure(true);
  4501. set_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state);
  4502. del_timer(&plat_priv->fw_boot_timer);
  4503. mod_timer(&pci_priv->dev_rddm_timer,
  4504. jiffies + msecs_to_jiffies(DEV_RDDM_TIMEOUT));
  4505. cnss_pci_update_status(pci_priv, CNSS_FW_DOWN);
  4506. return 0;
  4507. }
  4508. int cnss_pci_handle_dev_sol_irq(struct cnss_pci_data *pci_priv)
  4509. {
  4510. return cnss_pci_handle_mhi_sys_err(pci_priv);
  4511. }
  4512. static void cnss_mhi_notify_status(struct mhi_controller *mhi_ctrl,
  4513. enum mhi_callback reason)
  4514. {
  4515. struct cnss_pci_data *pci_priv = dev_get_drvdata(mhi_ctrl->cntrl_dev);
  4516. struct cnss_plat_data *plat_priv;
  4517. enum cnss_recovery_reason cnss_reason;
  4518. if (!pci_priv) {
  4519. cnss_pr_err("pci_priv is NULL");
  4520. return;
  4521. }
  4522. plat_priv = pci_priv->plat_priv;
  4523. if (reason != MHI_CB_IDLE)
  4524. cnss_pr_dbg("MHI status cb is called with reason %s(%d)\n",
  4525. cnss_mhi_notify_status_to_str(reason), reason);
  4526. switch (reason) {
  4527. case MHI_CB_IDLE:
  4528. case MHI_CB_EE_MISSION_MODE:
  4529. return;
  4530. case MHI_CB_FATAL_ERROR:
  4531. cnss_ignore_qmi_failure(true);
  4532. set_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state);
  4533. del_timer(&plat_priv->fw_boot_timer);
  4534. cnss_pci_update_status(pci_priv, CNSS_FW_DOWN);
  4535. cnss_reason = CNSS_REASON_DEFAULT;
  4536. break;
  4537. case MHI_CB_SYS_ERROR:
  4538. cnss_pci_handle_mhi_sys_err(pci_priv);
  4539. return;
  4540. case MHI_CB_EE_RDDM:
  4541. cnss_ignore_qmi_failure(true);
  4542. set_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state);
  4543. del_timer(&plat_priv->fw_boot_timer);
  4544. del_timer(&pci_priv->dev_rddm_timer);
  4545. cnss_pci_update_status(pci_priv, CNSS_FW_DOWN);
  4546. cnss_reason = CNSS_REASON_RDDM;
  4547. break;
  4548. #if IS_ENABLED(CONFIG_MHI_BUS_MISC)
  4549. case MHI_CB_FALLBACK_IMG:
  4550. /* for kiwi_v2 binary fallback is used, skip path fallback here */
  4551. if (!(pci_priv->device_id == KIWI_DEVICE_ID &&
  4552. plat_priv->device_version.major_version == FW_V2_NUMBER)) {
  4553. plat_priv->use_fw_path_with_prefix = false;
  4554. cnss_pci_update_fw_name(pci_priv);
  4555. }
  4556. return;
  4557. #endif
  4558. default:
  4559. cnss_pr_err("Unsupported MHI status cb reason: %d\n", reason);
  4560. return;
  4561. }
  4562. cnss_schedule_recovery(&pci_priv->pci_dev->dev, cnss_reason);
  4563. }
  4564. static int cnss_pci_get_mhi_msi(struct cnss_pci_data *pci_priv)
  4565. {
  4566. int ret, num_vectors, i;
  4567. u32 user_base_data, base_vector;
  4568. int *irq;
  4569. ret = cnss_get_user_msi_assignment(&pci_priv->pci_dev->dev,
  4570. MHI_MSI_NAME, &num_vectors,
  4571. &user_base_data, &base_vector);
  4572. if (ret)
  4573. return ret;
  4574. cnss_pr_dbg("Number of assigned MSI for MHI is %d, base vector is %d\n",
  4575. num_vectors, base_vector);
  4576. irq = kcalloc(num_vectors, sizeof(int), GFP_KERNEL);
  4577. if (!irq)
  4578. return -ENOMEM;
  4579. for (i = 0; i < num_vectors; i++)
  4580. irq[i] = cnss_get_msi_irq(&pci_priv->pci_dev->dev,
  4581. base_vector + i);
  4582. pci_priv->mhi_ctrl->irq = irq;
  4583. pci_priv->mhi_ctrl->nr_irqs = num_vectors;
  4584. return 0;
  4585. }
  4586. static int cnss_mhi_bw_scale(struct mhi_controller *mhi_ctrl,
  4587. struct mhi_link_info *link_info)
  4588. {
  4589. struct cnss_pci_data *pci_priv = dev_get_drvdata(mhi_ctrl->cntrl_dev);
  4590. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  4591. int ret = 0;
  4592. cnss_pr_dbg("Setting link speed:0x%x, width:0x%x\n",
  4593. link_info->target_link_speed,
  4594. link_info->target_link_width);
  4595. /* It has to set target link speed here before setting link bandwidth
  4596. * when device requests link speed change. This can avoid setting link
  4597. * bandwidth getting rejected if requested link speed is higher than
  4598. * current one.
  4599. */
  4600. ret = cnss_pci_set_max_link_speed(pci_priv, plat_priv->rc_num,
  4601. link_info->target_link_speed);
  4602. if (ret)
  4603. cnss_pr_err("Failed to set target link speed to 0x%x, err = %d\n",
  4604. link_info->target_link_speed, ret);
  4605. ret = cnss_pci_set_link_bandwidth(pci_priv,
  4606. link_info->target_link_speed,
  4607. link_info->target_link_width);
  4608. if (ret) {
  4609. cnss_pr_err("Failed to set link bandwidth, err = %d\n", ret);
  4610. return ret;
  4611. }
  4612. pci_priv->def_link_speed = link_info->target_link_speed;
  4613. pci_priv->def_link_width = link_info->target_link_width;
  4614. return 0;
  4615. }
  4616. static int cnss_mhi_read_reg(struct mhi_controller *mhi_ctrl,
  4617. void __iomem *addr, u32 *out)
  4618. {
  4619. struct cnss_pci_data *pci_priv = dev_get_drvdata(mhi_ctrl->cntrl_dev);
  4620. u32 tmp = readl_relaxed(addr);
  4621. /* Unexpected value, query the link status */
  4622. if (PCI_INVALID_READ(tmp) &&
  4623. cnss_pci_check_link_status(pci_priv))
  4624. return -EIO;
  4625. *out = tmp;
  4626. return 0;
  4627. }
  4628. static void cnss_mhi_write_reg(struct mhi_controller *mhi_ctrl,
  4629. void __iomem *addr, u32 val)
  4630. {
  4631. writel_relaxed(val, addr);
  4632. }
  4633. static int cnss_get_mhi_soc_info(struct cnss_plat_data *plat_priv,
  4634. struct mhi_controller *mhi_ctrl)
  4635. {
  4636. int ret = 0;
  4637. ret = mhi_get_soc_info(mhi_ctrl);
  4638. if (ret)
  4639. goto exit;
  4640. plat_priv->device_version.family_number = mhi_ctrl->family_number;
  4641. plat_priv->device_version.device_number = mhi_ctrl->device_number;
  4642. plat_priv->device_version.major_version = mhi_ctrl->major_version;
  4643. plat_priv->device_version.minor_version = mhi_ctrl->minor_version;
  4644. cnss_pr_dbg("Get device version info, family number: 0x%x, device number: 0x%x, major version: 0x%x, minor version: 0x%x\n",
  4645. plat_priv->device_version.family_number,
  4646. plat_priv->device_version.device_number,
  4647. plat_priv->device_version.major_version,
  4648. plat_priv->device_version.minor_version);
  4649. /* Only keep lower 4 bits as real device major version */
  4650. plat_priv->device_version.major_version &= DEVICE_MAJOR_VERSION_MASK;
  4651. exit:
  4652. return ret;
  4653. }
  4654. static int cnss_pci_register_mhi(struct cnss_pci_data *pci_priv)
  4655. {
  4656. int ret = 0;
  4657. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  4658. struct pci_dev *pci_dev = pci_priv->pci_dev;
  4659. struct mhi_controller *mhi_ctrl;
  4660. phys_addr_t bar_start;
  4661. const struct mhi_controller_config *cnss_mhi_config =
  4662. &cnss_mhi_config_default;
  4663. if (pci_priv->device_id == QCA6174_DEVICE_ID)
  4664. return 0;
  4665. mhi_ctrl = mhi_alloc_controller();
  4666. if (!mhi_ctrl) {
  4667. cnss_pr_err("Invalid MHI controller context\n");
  4668. return -EINVAL;
  4669. }
  4670. pci_priv->mhi_ctrl = mhi_ctrl;
  4671. mhi_ctrl->cntrl_dev = &pci_dev->dev;
  4672. mhi_ctrl->fw_image = plat_priv->firmware_name;
  4673. #if IS_ENABLED(CONFIG_MHI_BUS_MISC)
  4674. mhi_ctrl->fallback_fw_image = plat_priv->fw_fallback_name;
  4675. #endif
  4676. mhi_ctrl->regs = pci_priv->bar;
  4677. mhi_ctrl->reg_len = pci_resource_len(pci_priv->pci_dev, PCI_BAR_NUM);
  4678. bar_start = pci_resource_start(pci_priv->pci_dev, PCI_BAR_NUM);
  4679. cnss_pr_dbg("BAR starts at %pa, length is %x\n",
  4680. &bar_start, mhi_ctrl->reg_len);
  4681. ret = cnss_pci_get_mhi_msi(pci_priv);
  4682. if (ret) {
  4683. cnss_pr_err("Failed to get MSI for MHI, err = %d\n", ret);
  4684. goto free_mhi_ctrl;
  4685. }
  4686. if (pci_priv->smmu_s1_enable) {
  4687. mhi_ctrl->iova_start = pci_priv->smmu_iova_start;
  4688. mhi_ctrl->iova_stop = pci_priv->smmu_iova_start +
  4689. pci_priv->smmu_iova_len;
  4690. } else {
  4691. mhi_ctrl->iova_start = 0;
  4692. mhi_ctrl->iova_stop = pci_priv->dma_bit_mask;
  4693. }
  4694. mhi_ctrl->status_cb = cnss_mhi_notify_status;
  4695. mhi_ctrl->runtime_get = cnss_mhi_pm_runtime_get;
  4696. mhi_ctrl->runtime_put = cnss_mhi_pm_runtime_put_noidle;
  4697. mhi_ctrl->read_reg = cnss_mhi_read_reg;
  4698. mhi_ctrl->write_reg = cnss_mhi_write_reg;
  4699. mhi_ctrl->rddm_size = pci_priv->plat_priv->ramdump_info_v2.ramdump_size;
  4700. if (!mhi_ctrl->rddm_size)
  4701. mhi_ctrl->rddm_size = RAMDUMP_SIZE_DEFAULT;
  4702. mhi_ctrl->sbl_size = SZ_512K;
  4703. mhi_ctrl->seg_len = SZ_512K;
  4704. mhi_ctrl->fbc_download = true;
  4705. ret = cnss_get_mhi_soc_info(plat_priv, mhi_ctrl);
  4706. if (ret)
  4707. goto free_mhi_irq;
  4708. /* Satellite config only supported on KIWI V2 and later chipset */
  4709. if (plat_priv->device_id <= QCA6490_DEVICE_ID ||
  4710. (plat_priv->device_id == KIWI_DEVICE_ID &&
  4711. plat_priv->device_version.major_version == 1))
  4712. cnss_mhi_config = &cnss_mhi_config_no_satellite;
  4713. ret = mhi_register_controller(mhi_ctrl, cnss_mhi_config);
  4714. if (ret) {
  4715. cnss_pr_err("Failed to register to MHI bus, err = %d\n", ret);
  4716. goto free_mhi_irq;
  4717. }
  4718. /* MHI satellite driver only needs to connect when DRV is supported */
  4719. if (cnss_pci_is_drv_supported(pci_priv))
  4720. cnss_mhi_controller_set_base(pci_priv, bar_start);
  4721. /* BW scale CB needs to be set after registering MHI per requirement */
  4722. cnss_mhi_controller_set_bw_scale_cb(pci_priv, cnss_mhi_bw_scale);
  4723. ret = cnss_pci_update_fw_name(pci_priv);
  4724. if (ret)
  4725. goto unreg_mhi;
  4726. return 0;
  4727. unreg_mhi:
  4728. mhi_unregister_controller(mhi_ctrl);
  4729. free_mhi_irq:
  4730. kfree(mhi_ctrl->irq);
  4731. free_mhi_ctrl:
  4732. mhi_free_controller(mhi_ctrl);
  4733. return ret;
  4734. }
  4735. static void cnss_pci_unregister_mhi(struct cnss_pci_data *pci_priv)
  4736. {
  4737. struct mhi_controller *mhi_ctrl = pci_priv->mhi_ctrl;
  4738. if (pci_priv->device_id == QCA6174_DEVICE_ID)
  4739. return;
  4740. mhi_unregister_controller(mhi_ctrl);
  4741. kfree(mhi_ctrl->irq);
  4742. mhi_free_controller(mhi_ctrl);
  4743. }
  4744. static void cnss_pci_config_regs(struct cnss_pci_data *pci_priv)
  4745. {
  4746. switch (pci_priv->device_id) {
  4747. case QCA6390_DEVICE_ID:
  4748. pci_priv->misc_reg_dev_mask = REG_MASK_QCA6390;
  4749. pci_priv->wcss_reg = wcss_reg_access_seq;
  4750. pci_priv->pcie_reg = pcie_reg_access_seq;
  4751. pci_priv->wlaon_reg = wlaon_reg_access_seq;
  4752. pci_priv->syspm_reg = syspm_reg_access_seq;
  4753. /* Configure WDOG register with specific value so that we can
  4754. * know if HW is in the process of WDOG reset recovery or not
  4755. * when reading the registers.
  4756. */
  4757. cnss_pci_reg_write
  4758. (pci_priv,
  4759. QCA6390_PCIE_SOC_WDOG_DISC_BAD_DATA_LOW_CFG_SOC_PCIE_REG,
  4760. QCA6390_PCIE_SOC_WDOG_DISC_BAD_DATA_LOW_CFG_SOC_PCIE_REG_VAL);
  4761. break;
  4762. case QCA6490_DEVICE_ID:
  4763. pci_priv->misc_reg_dev_mask = REG_MASK_QCA6490;
  4764. pci_priv->wlaon_reg = wlaon_reg_access_seq;
  4765. break;
  4766. default:
  4767. return;
  4768. }
  4769. }
  4770. #if !IS_ENABLED(CONFIG_ARCH_QCOM)
  4771. static int cnss_pci_of_reserved_mem_device_init(struct cnss_pci_data *pci_priv)
  4772. {
  4773. return 0;
  4774. }
  4775. static irqreturn_t cnss_pci_wake_handler(int irq, void *data)
  4776. {
  4777. struct cnss_pci_data *pci_priv = data;
  4778. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  4779. enum rpm_status status;
  4780. struct device *dev;
  4781. pci_priv->wake_counter++;
  4782. cnss_pr_dbg("WLAN PCI wake IRQ (%u) is asserted #%u\n",
  4783. pci_priv->wake_irq, pci_priv->wake_counter);
  4784. /* Make sure abort current suspend */
  4785. cnss_pm_stay_awake(plat_priv);
  4786. cnss_pm_relax(plat_priv);
  4787. /* Above two pm* API calls will abort system suspend only when
  4788. * plat_dev->dev->ws is initiated by device_init_wakeup() API, and
  4789. * calling pm_system_wakeup() is just to guarantee system suspend
  4790. * can be aborted if it is not initiated in any case.
  4791. */
  4792. pm_system_wakeup();
  4793. dev = &pci_priv->pci_dev->dev;
  4794. status = dev->power.runtime_status;
  4795. if ((cnss_pci_get_monitor_wake_intr(pci_priv) &&
  4796. cnss_pci_get_auto_suspended(pci_priv)) ||
  4797. (status == RPM_SUSPENDING || status == RPM_SUSPENDED)) {
  4798. cnss_pci_set_monitor_wake_intr(pci_priv, false);
  4799. cnss_pci_pm_request_resume(pci_priv);
  4800. }
  4801. return IRQ_HANDLED;
  4802. }
  4803. /**
  4804. * cnss_pci_wake_gpio_init() - Setup PCI wake GPIO for WLAN
  4805. * @pci_priv: driver PCI bus context pointer
  4806. *
  4807. * This function initializes WLAN PCI wake GPIO and corresponding
  4808. * interrupt. It should be used in non-MSM platforms whose PCIe
  4809. * root complex driver doesn't handle the GPIO.
  4810. *
  4811. * Return: 0 for success or skip, negative value for error
  4812. */
  4813. static int cnss_pci_wake_gpio_init(struct cnss_pci_data *pci_priv)
  4814. {
  4815. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  4816. struct device *dev = &plat_priv->plat_dev->dev;
  4817. int ret = 0;
  4818. pci_priv->wake_gpio = of_get_named_gpio(dev->of_node,
  4819. "wlan-pci-wake-gpio", 0);
  4820. if (pci_priv->wake_gpio < 0)
  4821. goto out;
  4822. cnss_pr_dbg("Get PCI wake GPIO (%d) from device node\n",
  4823. pci_priv->wake_gpio);
  4824. ret = gpio_request(pci_priv->wake_gpio, "wlan_pci_wake_gpio");
  4825. if (ret) {
  4826. cnss_pr_err("Failed to request PCI wake GPIO, err = %d\n",
  4827. ret);
  4828. goto out;
  4829. }
  4830. gpio_direction_input(pci_priv->wake_gpio);
  4831. pci_priv->wake_irq = gpio_to_irq(pci_priv->wake_gpio);
  4832. ret = request_irq(pci_priv->wake_irq, cnss_pci_wake_handler,
  4833. IRQF_TRIGGER_FALLING, "wlan_pci_wake_irq", pci_priv);
  4834. if (ret) {
  4835. cnss_pr_err("Failed to request PCI wake IRQ, err = %d\n", ret);
  4836. goto free_gpio;
  4837. }
  4838. ret = enable_irq_wake(pci_priv->wake_irq);
  4839. if (ret) {
  4840. cnss_pr_err("Failed to enable PCI wake IRQ, err = %d\n", ret);
  4841. goto free_irq;
  4842. }
  4843. return 0;
  4844. free_irq:
  4845. free_irq(pci_priv->wake_irq, pci_priv);
  4846. free_gpio:
  4847. gpio_free(pci_priv->wake_gpio);
  4848. out:
  4849. return ret;
  4850. }
  4851. static void cnss_pci_wake_gpio_deinit(struct cnss_pci_data *pci_priv)
  4852. {
  4853. if (pci_priv->wake_gpio < 0)
  4854. return;
  4855. disable_irq_wake(pci_priv->wake_irq);
  4856. free_irq(pci_priv->wake_irq, pci_priv);
  4857. gpio_free(pci_priv->wake_gpio);
  4858. }
  4859. #endif
  4860. /* Setting to use this cnss_pm_domain ops will let PM framework override the
  4861. * ops from dev->bus->pm which is pci_dev_pm_ops from pci-driver.c. This ops
  4862. * has to take care everything device driver needed which is currently done
  4863. * from pci_dev_pm_ops.
  4864. */
  4865. static struct dev_pm_domain cnss_pm_domain = {
  4866. .ops = {
  4867. SET_SYSTEM_SLEEP_PM_OPS(cnss_pci_suspend, cnss_pci_resume)
  4868. SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(cnss_pci_suspend_noirq,
  4869. cnss_pci_resume_noirq)
  4870. SET_RUNTIME_PM_OPS(cnss_pci_runtime_suspend,
  4871. cnss_pci_runtime_resume,
  4872. cnss_pci_runtime_idle)
  4873. }
  4874. };
  4875. #ifdef CONFIG_CNSS2_CONDITIONAL_POWEROFF
  4876. static bool cnss_should_suspend_pwroff(struct pci_dev *pci_dev)
  4877. {
  4878. bool suspend_pwroff;
  4879. switch (pci_dev->device) {
  4880. case QCA6390_DEVICE_ID:
  4881. case QCA6490_DEVICE_ID:
  4882. suspend_pwroff = false;
  4883. break;
  4884. default:
  4885. suspend_pwroff = true;
  4886. }
  4887. return suspend_pwroff;
  4888. }
  4889. #else
  4890. static bool cnss_should_suspend_pwroff(struct pci_dev *pci_dev)
  4891. {
  4892. return true;
  4893. }
  4894. #endif
  4895. static void cnss_pci_suspend_pwroff(struct pci_dev *pci_dev)
  4896. {
  4897. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  4898. struct cnss_plat_data *plat_priv = cnss_bus_dev_to_plat_priv(NULL);
  4899. int ret = 0;
  4900. bool suspend_pwroff = cnss_should_suspend_pwroff(pci_dev);
  4901. if (suspend_pwroff) {
  4902. ret = cnss_suspend_pci_link(pci_priv);
  4903. if (ret)
  4904. cnss_pr_err("Failed to suspend PCI link, err = %d\n",
  4905. ret);
  4906. cnss_power_off_device(plat_priv);
  4907. } else {
  4908. cnss_pr_dbg("bus suspend and dev power off disabled for device [0x%x]\n",
  4909. pci_dev->device);
  4910. }
  4911. }
  4912. static int cnss_pci_probe(struct pci_dev *pci_dev,
  4913. const struct pci_device_id *id)
  4914. {
  4915. int ret = 0;
  4916. struct cnss_pci_data *pci_priv;
  4917. struct cnss_plat_data *plat_priv = cnss_bus_dev_to_plat_priv(NULL);
  4918. struct device *dev = &pci_dev->dev;
  4919. cnss_pr_dbg("PCI is probing, vendor ID: 0x%x, device ID: 0x%x\n",
  4920. id->vendor, pci_dev->device);
  4921. pci_priv = devm_kzalloc(dev, sizeof(*pci_priv), GFP_KERNEL);
  4922. if (!pci_priv) {
  4923. ret = -ENOMEM;
  4924. goto out;
  4925. }
  4926. pci_priv->pci_link_state = PCI_LINK_UP;
  4927. pci_priv->plat_priv = plat_priv;
  4928. pci_priv->pci_dev = pci_dev;
  4929. pci_priv->pci_device_id = id;
  4930. pci_priv->device_id = pci_dev->device;
  4931. cnss_set_pci_priv(pci_dev, pci_priv);
  4932. plat_priv->device_id = pci_dev->device;
  4933. plat_priv->bus_priv = pci_priv;
  4934. mutex_init(&pci_priv->bus_lock);
  4935. if (plat_priv->use_pm_domain)
  4936. dev->pm_domain = &cnss_pm_domain;
  4937. cnss_pci_of_reserved_mem_device_init(pci_priv);
  4938. ret = cnss_register_subsys(plat_priv);
  4939. if (ret)
  4940. goto reset_ctx;
  4941. ret = cnss_register_ramdump(plat_priv);
  4942. if (ret)
  4943. goto unregister_subsys;
  4944. ret = cnss_pci_init_smmu(pci_priv);
  4945. if (ret)
  4946. goto unregister_ramdump;
  4947. ret = cnss_reg_pci_event(pci_priv);
  4948. if (ret) {
  4949. cnss_pr_err("Failed to register PCI event, err = %d\n", ret);
  4950. goto deinit_smmu;
  4951. }
  4952. ret = cnss_pci_enable_bus(pci_priv);
  4953. if (ret)
  4954. goto dereg_pci_event;
  4955. ret = cnss_pci_enable_msi(pci_priv);
  4956. if (ret)
  4957. goto disable_bus;
  4958. ret = cnss_pci_register_mhi(pci_priv);
  4959. if (ret)
  4960. goto disable_msi;
  4961. switch (pci_dev->device) {
  4962. case QCA6174_DEVICE_ID:
  4963. pci_read_config_word(pci_dev, QCA6174_REV_ID_OFFSET,
  4964. &pci_priv->revision_id);
  4965. break;
  4966. case QCA6290_DEVICE_ID:
  4967. case QCA6390_DEVICE_ID:
  4968. case QCA6490_DEVICE_ID:
  4969. case KIWI_DEVICE_ID:
  4970. case MANGO_DEVICE_ID:
  4971. cnss_pci_set_wlaon_pwr_ctrl(pci_priv, false, false, false);
  4972. timer_setup(&pci_priv->dev_rddm_timer,
  4973. cnss_dev_rddm_timeout_hdlr, 0);
  4974. timer_setup(&pci_priv->boot_debug_timer,
  4975. cnss_boot_debug_timeout_hdlr, 0);
  4976. INIT_DELAYED_WORK(&pci_priv->time_sync_work,
  4977. cnss_pci_time_sync_work_hdlr);
  4978. cnss_pci_get_link_status(pci_priv);
  4979. cnss_pci_set_wlaon_pwr_ctrl(pci_priv, false, true, false);
  4980. cnss_pci_wake_gpio_init(pci_priv);
  4981. break;
  4982. default:
  4983. cnss_pr_err("Unknown PCI device found: 0x%x\n",
  4984. pci_dev->device);
  4985. ret = -ENODEV;
  4986. goto unreg_mhi;
  4987. }
  4988. cnss_pci_config_regs(pci_priv);
  4989. if (EMULATION_HW)
  4990. goto out;
  4991. cnss_pci_suspend_pwroff(pci_dev);
  4992. set_bit(CNSS_PCI_PROBE_DONE, &plat_priv->driver_state);
  4993. return 0;
  4994. unreg_mhi:
  4995. cnss_pci_unregister_mhi(pci_priv);
  4996. disable_msi:
  4997. cnss_pci_disable_msi(pci_priv);
  4998. disable_bus:
  4999. cnss_pci_disable_bus(pci_priv);
  5000. dereg_pci_event:
  5001. cnss_dereg_pci_event(pci_priv);
  5002. deinit_smmu:
  5003. cnss_pci_deinit_smmu(pci_priv);
  5004. unregister_ramdump:
  5005. cnss_unregister_ramdump(plat_priv);
  5006. unregister_subsys:
  5007. cnss_unregister_subsys(plat_priv);
  5008. reset_ctx:
  5009. plat_priv->bus_priv = NULL;
  5010. out:
  5011. return ret;
  5012. }
  5013. static void cnss_pci_remove(struct pci_dev *pci_dev)
  5014. {
  5015. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  5016. struct cnss_plat_data *plat_priv =
  5017. cnss_bus_dev_to_plat_priv(&pci_dev->dev);
  5018. clear_bit(CNSS_PCI_PROBE_DONE, &plat_priv->driver_state);
  5019. cnss_pci_free_m3_mem(pci_priv);
  5020. cnss_pci_free_fw_mem(pci_priv);
  5021. cnss_pci_free_qdss_mem(pci_priv);
  5022. switch (pci_dev->device) {
  5023. case QCA6290_DEVICE_ID:
  5024. case QCA6390_DEVICE_ID:
  5025. case QCA6490_DEVICE_ID:
  5026. case KIWI_DEVICE_ID:
  5027. case MANGO_DEVICE_ID:
  5028. cnss_pci_wake_gpio_deinit(pci_priv);
  5029. del_timer(&pci_priv->boot_debug_timer);
  5030. del_timer(&pci_priv->dev_rddm_timer);
  5031. break;
  5032. default:
  5033. break;
  5034. }
  5035. cnss_pci_unregister_mhi(pci_priv);
  5036. cnss_pci_disable_msi(pci_priv);
  5037. cnss_pci_disable_bus(pci_priv);
  5038. cnss_dereg_pci_event(pci_priv);
  5039. cnss_pci_deinit_smmu(pci_priv);
  5040. if (plat_priv) {
  5041. cnss_unregister_ramdump(plat_priv);
  5042. cnss_unregister_subsys(plat_priv);
  5043. plat_priv->bus_priv = NULL;
  5044. } else {
  5045. cnss_pr_err("Plat_priv is null, Unable to unregister ramdump,subsys\n");
  5046. }
  5047. }
  5048. static const struct pci_device_id cnss_pci_id_table[] = {
  5049. { QCA6174_VENDOR_ID, QCA6174_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID },
  5050. { QCA6290_VENDOR_ID, QCA6290_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID },
  5051. { QCA6390_VENDOR_ID, QCA6390_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID },
  5052. { QCA6490_VENDOR_ID, QCA6490_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID },
  5053. { KIWI_VENDOR_ID, KIWI_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID },
  5054. { MANGO_VENDOR_ID, MANGO_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID },
  5055. { 0 }
  5056. };
  5057. MODULE_DEVICE_TABLE(pci, cnss_pci_id_table);
  5058. static const struct dev_pm_ops cnss_pm_ops = {
  5059. SET_SYSTEM_SLEEP_PM_OPS(cnss_pci_suspend, cnss_pci_resume)
  5060. SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(cnss_pci_suspend_noirq,
  5061. cnss_pci_resume_noirq)
  5062. SET_RUNTIME_PM_OPS(cnss_pci_runtime_suspend, cnss_pci_runtime_resume,
  5063. cnss_pci_runtime_idle)
  5064. };
  5065. struct pci_driver cnss_pci_driver = {
  5066. .name = "cnss_pci",
  5067. .id_table = cnss_pci_id_table,
  5068. .probe = cnss_pci_probe,
  5069. .remove = cnss_pci_remove,
  5070. .driver = {
  5071. .pm = &cnss_pm_ops,
  5072. },
  5073. };
  5074. static int cnss_pci_enumerate(struct cnss_plat_data *plat_priv, u32 rc_num)
  5075. {
  5076. int ret, retry = 0;
  5077. /* Always set initial target PCIe link speed to Gen2 for QCA6490 device
  5078. * since there may be link issues if it boots up with Gen3 link speed.
  5079. * Device is able to change it later at any time. It will be rejected
  5080. * if requested speed is higher than the one specified in PCIe DT.
  5081. */
  5082. if (plat_priv->device_id == QCA6490_DEVICE_ID) {
  5083. ret = cnss_pci_set_max_link_speed(plat_priv->bus_priv, rc_num,
  5084. PCI_EXP_LNKSTA_CLS_5_0GB);
  5085. if (ret && ret != -EPROBE_DEFER)
  5086. cnss_pr_err("Failed to set max PCIe RC%x link speed to Gen2, err = %d\n",
  5087. rc_num, ret);
  5088. }
  5089. cnss_pr_dbg("Trying to enumerate with PCIe RC%x\n", rc_num);
  5090. retry:
  5091. ret = _cnss_pci_enumerate(plat_priv, rc_num);
  5092. if (ret) {
  5093. if (ret == -EPROBE_DEFER) {
  5094. cnss_pr_dbg("PCIe RC driver is not ready, defer probe\n");
  5095. goto out;
  5096. }
  5097. cnss_pr_err("Failed to enable PCIe RC%x, err = %d\n",
  5098. rc_num, ret);
  5099. if (retry++ < LINK_TRAINING_RETRY_MAX_TIMES) {
  5100. cnss_pr_dbg("Retry PCI link training #%d\n", retry);
  5101. goto retry;
  5102. } else {
  5103. goto out;
  5104. }
  5105. }
  5106. plat_priv->rc_num = rc_num;
  5107. out:
  5108. return ret;
  5109. }
  5110. int cnss_pci_init(struct cnss_plat_data *plat_priv)
  5111. {
  5112. struct device *dev = &plat_priv->plat_dev->dev;
  5113. const __be32 *prop;
  5114. int ret = 0, prop_len = 0, rc_count, i;
  5115. prop = of_get_property(dev->of_node, "qcom,wlan-rc-num", &prop_len);
  5116. if (!prop || !prop_len) {
  5117. cnss_pr_err("Failed to get PCIe RC number from DT\n");
  5118. goto out;
  5119. }
  5120. rc_count = prop_len / sizeof(__be32);
  5121. for (i = 0; i < rc_count; i++) {
  5122. ret = cnss_pci_enumerate(plat_priv, be32_to_cpup(&prop[i]));
  5123. if (!ret)
  5124. break;
  5125. else if (ret == -EPROBE_DEFER || (ret && i == rc_count - 1))
  5126. goto out;
  5127. }
  5128. ret = pci_register_driver(&cnss_pci_driver);
  5129. if (ret) {
  5130. cnss_pr_err("Failed to register to PCI framework, err = %d\n",
  5131. ret);
  5132. goto out;
  5133. }
  5134. if (!plat_priv->bus_priv) {
  5135. cnss_pr_err("Failed to probe PCI driver\n");
  5136. ret = -ENODEV;
  5137. goto unreg_pci;
  5138. }
  5139. return 0;
  5140. unreg_pci:
  5141. pci_unregister_driver(&cnss_pci_driver);
  5142. out:
  5143. return ret;
  5144. }
  5145. void cnss_pci_deinit(struct cnss_plat_data *plat_priv)
  5146. {
  5147. pci_unregister_driver(&cnss_pci_driver);
  5148. }