pci.c 184 KB

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