pci.c 201 KB

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