pci.c 165 KB

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