pci.c 180 KB

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