pci.c 166 KB

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