pci.c 196 KB

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