pci.c 184 KB

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