pci.c 206 KB

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