pci.c 205 KB

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