ipa.c 237 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2012-2020, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/clk.h>
  6. #include <linux/compat.h>
  7. #include <linux/device.h>
  8. #include <linux/dmapool.h>
  9. #include <linux/fs.h>
  10. #include <linux/genalloc.h>
  11. #include <linux/init.h>
  12. #include <linux/kernel.h>
  13. #include <linux/mm.h>
  14. #include <linux/module.h>
  15. #include <linux/of.h>
  16. #include <linux/of_platform.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/rbtree.h>
  19. #include <linux/uaccess.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/interconnect.h>
  22. #include <linux/netdevice.h>
  23. #include <linux/delay.h>
  24. #include <linux/msm_gsi.h>
  25. #include <linux/time.h>
  26. #include <linux/hashtable.h>
  27. #include <linux/jhash.h>
  28. #include <linux/pci.h>
  29. #include <soc/qcom/subsystem_restart.h>
  30. #include <linux/soc/qcom/smem.h>
  31. #include <linux/qcom_scm.h>
  32. #include <asm/cacheflush.h>
  33. #include <linux/soc/qcom/smem_state.h>
  34. #include <linux/of_irq.h>
  35. #include <linux/ctype.h>
  36. #ifdef CONFIG_ARM64
  37. /* Outer caches unsupported on ARM64 platforms */
  38. #define outer_flush_range(x, y)
  39. #define __cpuc_flush_dcache_area __flush_dcache_area
  40. #endif
  41. #define IPA_SUBSYSTEM_NAME "ipa_fws"
  42. #define IPA_UC_SUBSYSTEM_NAME "ipa_uc"
  43. #include "ipa_i.h"
  44. #include "../ipa_rm_i.h"
  45. #include "ipahal/ipahal.h"
  46. #include "ipahal/ipahal_fltrt.h"
  47. #define CREATE_TRACE_POINTS
  48. #include "ipa_trace.h"
  49. #include "ipa_odl.h"
  50. #define IPA_SUSPEND_BUSY_TIMEOUT (msecs_to_jiffies(10))
  51. /*
  52. * The following for adding code (ie. for EMULATION) not found on x86.
  53. */
  54. #if defined(CONFIG_IPA_EMULATION)
  55. # include "ipa_emulation_stubs.h"
  56. #endif
  57. #ifdef CONFIG_COMPAT
  58. /**
  59. * struct ipa3_ioc_nat_alloc_mem32 - nat table memory allocation
  60. * properties
  61. * @dev_name: input parameter, the name of table
  62. * @size: input parameter, size of table in bytes
  63. * @offset: output parameter, offset into page in case of system memory
  64. */
  65. struct ipa3_ioc_nat_alloc_mem32 {
  66. char dev_name[IPA_RESOURCE_NAME_MAX];
  67. compat_size_t size;
  68. compat_off_t offset;
  69. };
  70. /**
  71. * struct ipa_ioc_nat_ipv6ct_table_alloc32 - table memory allocation
  72. * properties
  73. * @size: input parameter, size of table in bytes
  74. * @offset: output parameter, offset into page in case of system memory
  75. */
  76. struct ipa_ioc_nat_ipv6ct_table_alloc32 {
  77. compat_size_t size;
  78. compat_off_t offset;
  79. };
  80. #endif /* #ifdef CONFIG_COMPAT */
  81. #define IPA_TZ_UNLOCK_ATTRIBUTE 0x0C0311
  82. struct tz_smmu_ipa_protect_region_iovec_s {
  83. u64 input_addr;
  84. u64 output_addr;
  85. u64 size;
  86. u32 attr;
  87. } __packed;
  88. struct tz_smmu_ipa_protect_region_s {
  89. phys_addr_t iovec_buf;
  90. u32 size_bytes;
  91. } __packed;
  92. static void ipa3_start_tag_process(struct work_struct *work);
  93. static DECLARE_WORK(ipa3_tag_work, ipa3_start_tag_process);
  94. static void ipa3_transport_release_resource(struct work_struct *work);
  95. static DECLARE_DELAYED_WORK(ipa3_transport_release_resource_work,
  96. ipa3_transport_release_resource);
  97. static void ipa_gsi_notify_cb(struct gsi_per_notify *notify);
  98. static int ipa3_attach_to_smmu(void);
  99. static int ipa3_alloc_pkt_init(void);
  100. static void ipa3_load_ipa_fw(struct work_struct *work);
  101. static DECLARE_WORK(ipa3_fw_loading_work, ipa3_load_ipa_fw);
  102. static void ipa_dec_clients_disable_clks_on_wq(struct work_struct *work);
  103. static DECLARE_DELAYED_WORK(ipa_dec_clients_disable_clks_on_wq_work,
  104. ipa_dec_clients_disable_clks_on_wq);
  105. static int ipa3_ioctl_add_rt_rule_v2(unsigned long arg);
  106. static int ipa3_ioctl_add_rt_rule_ext_v2(unsigned long arg);
  107. static int ipa3_ioctl_add_rt_rule_after_v2(unsigned long arg);
  108. static int ipa3_ioctl_mdfy_rt_rule_v2(unsigned long arg);
  109. static int ipa3_ioctl_add_flt_rule_v2(unsigned long arg);
  110. static int ipa3_ioctl_add_flt_rule_after_v2(unsigned long arg);
  111. static int ipa3_ioctl_mdfy_flt_rule_v2(unsigned long arg);
  112. static int ipa3_ioctl_fnr_counter_alloc(unsigned long arg);
  113. static int ipa3_ioctl_fnr_counter_query(unsigned long arg);
  114. static int ipa3_ioctl_fnr_counter_set(unsigned long arg);
  115. static struct ipa3_plat_drv_res ipa3_res = {0, };
  116. static struct clk *ipa3_clk;
  117. struct ipa3_context *ipa3_ctx;
  118. static struct {
  119. bool present[IPA_SMMU_CB_MAX];
  120. bool arm_smmu;
  121. bool use_64_bit_dma_mask;
  122. u32 ipa_base;
  123. u32 ipa_size;
  124. } smmu_info;
  125. static char *active_clients_table_buf;
  126. int ipa3_active_clients_log_print_buffer(char *buf, int size)
  127. {
  128. int i;
  129. int nbytes;
  130. int cnt = 0;
  131. int start_idx;
  132. int end_idx;
  133. unsigned long flags;
  134. spin_lock_irqsave(&ipa3_ctx->ipa3_active_clients_logging.lock, flags);
  135. start_idx = (ipa3_ctx->ipa3_active_clients_logging.log_tail + 1) %
  136. IPA3_ACTIVE_CLIENTS_LOG_BUFFER_SIZE_LINES;
  137. end_idx = ipa3_ctx->ipa3_active_clients_logging.log_head;
  138. for (i = start_idx; i != end_idx;
  139. i = (i + 1) % IPA3_ACTIVE_CLIENTS_LOG_BUFFER_SIZE_LINES) {
  140. nbytes = scnprintf(buf + cnt, size - cnt, "%s\n",
  141. ipa3_ctx->ipa3_active_clients_logging
  142. .log_buffer[i]);
  143. cnt += nbytes;
  144. }
  145. spin_unlock_irqrestore(&ipa3_ctx->ipa3_active_clients_logging.lock,
  146. flags);
  147. return cnt;
  148. }
  149. int ipa3_active_clients_log_print_table(char *buf, int size)
  150. {
  151. int i;
  152. struct ipa3_active_client_htable_entry *iterator;
  153. int cnt = 0;
  154. unsigned long flags;
  155. spin_lock_irqsave(&ipa3_ctx->ipa3_active_clients_logging.lock, flags);
  156. cnt = scnprintf(buf, size, "\n---- Active Clients Table ----\n");
  157. hash_for_each(ipa3_ctx->ipa3_active_clients_logging.htable, i,
  158. iterator, list) {
  159. switch (iterator->type) {
  160. case IPA3_ACTIVE_CLIENT_LOG_TYPE_EP:
  161. cnt += scnprintf(buf + cnt, size - cnt,
  162. "%-40s %-3d ENDPOINT\n",
  163. iterator->id_string, iterator->count);
  164. break;
  165. case IPA3_ACTIVE_CLIENT_LOG_TYPE_SIMPLE:
  166. cnt += scnprintf(buf + cnt, size - cnt,
  167. "%-40s %-3d SIMPLE\n",
  168. iterator->id_string, iterator->count);
  169. break;
  170. case IPA3_ACTIVE_CLIENT_LOG_TYPE_RESOURCE:
  171. cnt += scnprintf(buf + cnt, size - cnt,
  172. "%-40s %-3d RESOURCE\n",
  173. iterator->id_string, iterator->count);
  174. break;
  175. case IPA3_ACTIVE_CLIENT_LOG_TYPE_SPECIAL:
  176. cnt += scnprintf(buf + cnt, size - cnt,
  177. "%-40s %-3d SPECIAL\n",
  178. iterator->id_string, iterator->count);
  179. break;
  180. default:
  181. IPAERR("Trying to print illegal active_clients type");
  182. break;
  183. }
  184. }
  185. cnt += scnprintf(buf + cnt, size - cnt,
  186. "\nTotal active clients count: %d\n",
  187. atomic_read(&ipa3_ctx->ipa3_active_clients.cnt));
  188. if (ipa3_is_mhip_offload_enabled())
  189. cnt += ipa_mpm_panic_handler(buf + cnt, size - cnt);
  190. spin_unlock_irqrestore(&ipa3_ctx->ipa3_active_clients_logging.lock,
  191. flags);
  192. return cnt;
  193. }
  194. static int ipa3_clean_modem_rule(void)
  195. {
  196. struct ipa_install_fltr_rule_req_msg_v01 *req;
  197. struct ipa_install_fltr_rule_req_ex_msg_v01 *req_ex;
  198. int val = 0;
  199. if (ipa3_ctx->ipa_hw_type < IPA_HW_v3_0) {
  200. req = kzalloc(
  201. sizeof(struct ipa_install_fltr_rule_req_msg_v01),
  202. GFP_KERNEL);
  203. if (!req) {
  204. IPAERR("mem allocated failed!\n");
  205. return -ENOMEM;
  206. }
  207. req->filter_spec_list_valid = false;
  208. req->filter_spec_list_len = 0;
  209. req->source_pipe_index_valid = 0;
  210. val = ipa3_qmi_filter_request_send(req);
  211. kfree(req);
  212. } else {
  213. req_ex = kzalloc(
  214. sizeof(struct ipa_install_fltr_rule_req_ex_msg_v01),
  215. GFP_KERNEL);
  216. if (!req_ex) {
  217. IPAERR("mem allocated failed!\n");
  218. return -ENOMEM;
  219. }
  220. req_ex->filter_spec_ex_list_valid = false;
  221. req_ex->filter_spec_ex_list_len = 0;
  222. req_ex->source_pipe_index_valid = 0;
  223. val = ipa3_qmi_filter_request_ex_send(req_ex);
  224. kfree(req_ex);
  225. }
  226. return val;
  227. }
  228. static int ipa3_clean_mhip_dl_rule(void)
  229. {
  230. struct ipa_remove_offload_connection_req_msg_v01 req;
  231. memset(&req, 0, sizeof(struct
  232. ipa_remove_offload_connection_req_msg_v01));
  233. req.clean_all_rules_valid = true;
  234. req.clean_all_rules = true;
  235. if (ipa3_qmi_rmv_offload_request_send(&req)) {
  236. IPAWANDBG("clean dl rule cache failed\n");
  237. return -EFAULT;
  238. }
  239. return 0;
  240. }
  241. static int ipa3_active_clients_panic_notifier(struct notifier_block *this,
  242. unsigned long event, void *ptr)
  243. {
  244. ipa3_active_clients_log_print_table(active_clients_table_buf,
  245. IPA3_ACTIVE_CLIENTS_TABLE_BUF_SIZE);
  246. IPAERR("%s\n", active_clients_table_buf);
  247. return NOTIFY_DONE;
  248. }
  249. static struct notifier_block ipa3_active_clients_panic_blk = {
  250. .notifier_call = ipa3_active_clients_panic_notifier,
  251. };
  252. #ifdef CONFIG_IPA_DEBUG
  253. static int ipa3_active_clients_log_insert(const char *string)
  254. {
  255. int head;
  256. int tail;
  257. if (!ipa3_ctx->ipa3_active_clients_logging.log_rdy)
  258. return -EPERM;
  259. head = ipa3_ctx->ipa3_active_clients_logging.log_head;
  260. tail = ipa3_ctx->ipa3_active_clients_logging.log_tail;
  261. memset(ipa3_ctx->ipa3_active_clients_logging.log_buffer[head], '_',
  262. IPA3_ACTIVE_CLIENTS_LOG_LINE_LEN);
  263. strlcpy(ipa3_ctx->ipa3_active_clients_logging.log_buffer[head], string,
  264. (size_t)IPA3_ACTIVE_CLIENTS_LOG_LINE_LEN);
  265. head = (head + 1) % IPA3_ACTIVE_CLIENTS_LOG_BUFFER_SIZE_LINES;
  266. if (tail == head)
  267. tail = (tail + 1) % IPA3_ACTIVE_CLIENTS_LOG_BUFFER_SIZE_LINES;
  268. ipa3_ctx->ipa3_active_clients_logging.log_tail = tail;
  269. ipa3_ctx->ipa3_active_clients_logging.log_head = head;
  270. return 0;
  271. }
  272. #endif
  273. static int ipa3_active_clients_log_init(void)
  274. {
  275. int i;
  276. spin_lock_init(&ipa3_ctx->ipa3_active_clients_logging.lock);
  277. ipa3_ctx->ipa3_active_clients_logging.log_buffer[0] = kcalloc(
  278. IPA3_ACTIVE_CLIENTS_LOG_BUFFER_SIZE_LINES,
  279. sizeof(char[IPA3_ACTIVE_CLIENTS_LOG_LINE_LEN]),
  280. GFP_KERNEL);
  281. active_clients_table_buf = kzalloc(sizeof(
  282. char[IPA3_ACTIVE_CLIENTS_TABLE_BUF_SIZE]), GFP_KERNEL);
  283. if (ipa3_ctx->ipa3_active_clients_logging.log_buffer == NULL) {
  284. pr_err("Active Clients Logging memory allocation failed\n");
  285. goto bail;
  286. }
  287. for (i = 0; i < IPA3_ACTIVE_CLIENTS_LOG_BUFFER_SIZE_LINES; i++) {
  288. ipa3_ctx->ipa3_active_clients_logging.log_buffer[i] =
  289. ipa3_ctx->ipa3_active_clients_logging.log_buffer[0] +
  290. (IPA3_ACTIVE_CLIENTS_LOG_LINE_LEN * i);
  291. }
  292. ipa3_ctx->ipa3_active_clients_logging.log_head = 0;
  293. ipa3_ctx->ipa3_active_clients_logging.log_tail =
  294. IPA3_ACTIVE_CLIENTS_LOG_BUFFER_SIZE_LINES - 1;
  295. hash_init(ipa3_ctx->ipa3_active_clients_logging.htable);
  296. atomic_notifier_chain_register(&panic_notifier_list,
  297. &ipa3_active_clients_panic_blk);
  298. ipa3_ctx->ipa3_active_clients_logging.log_rdy = true;
  299. return 0;
  300. bail:
  301. return -ENOMEM;
  302. }
  303. void ipa3_active_clients_log_clear(void)
  304. {
  305. unsigned long flags;
  306. spin_lock_irqsave(&ipa3_ctx->ipa3_active_clients_logging.lock, flags);
  307. ipa3_ctx->ipa3_active_clients_logging.log_head = 0;
  308. ipa3_ctx->ipa3_active_clients_logging.log_tail =
  309. IPA3_ACTIVE_CLIENTS_LOG_BUFFER_SIZE_LINES - 1;
  310. spin_unlock_irqrestore(&ipa3_ctx->ipa3_active_clients_logging.lock,
  311. flags);
  312. }
  313. static void ipa3_active_clients_log_destroy(void)
  314. {
  315. unsigned long flags;
  316. spin_lock_irqsave(&ipa3_ctx->ipa3_active_clients_logging.lock, flags);
  317. ipa3_ctx->ipa3_active_clients_logging.log_rdy = false;
  318. kfree(active_clients_table_buf);
  319. active_clients_table_buf = NULL;
  320. kfree(ipa3_ctx->ipa3_active_clients_logging.log_buffer[0]);
  321. ipa3_ctx->ipa3_active_clients_logging.log_head = 0;
  322. ipa3_ctx->ipa3_active_clients_logging.log_tail =
  323. IPA3_ACTIVE_CLIENTS_LOG_BUFFER_SIZE_LINES - 1;
  324. spin_unlock_irqrestore(&ipa3_ctx->ipa3_active_clients_logging.lock,
  325. flags);
  326. }
  327. static struct ipa_smmu_cb_ctx smmu_cb[IPA_SMMU_CB_MAX];
  328. struct iommu_domain *ipa3_get_smmu_domain_by_type(enum ipa_smmu_cb_type cb_type)
  329. {
  330. if (VALID_IPA_SMMU_CB_TYPE(cb_type) && smmu_cb[cb_type].valid)
  331. return smmu_cb[cb_type].iommu_domain;
  332. IPAERR("cb_type(%d) not valid\n", cb_type);
  333. return NULL;
  334. }
  335. struct iommu_domain *ipa3_get_smmu_domain(void)
  336. {
  337. return ipa3_get_smmu_domain_by_type(IPA_SMMU_CB_AP);
  338. }
  339. struct iommu_domain *ipa3_get_uc_smmu_domain(void)
  340. {
  341. return ipa3_get_smmu_domain_by_type(IPA_SMMU_CB_UC);
  342. }
  343. struct iommu_domain *ipa3_get_wlan_smmu_domain(void)
  344. {
  345. return ipa3_get_smmu_domain_by_type(IPA_SMMU_CB_WLAN);
  346. }
  347. struct iommu_domain *ipa3_get_11ad_smmu_domain(void)
  348. {
  349. return ipa3_get_smmu_domain_by_type(IPA_SMMU_CB_11AD);
  350. }
  351. struct device *ipa3_get_dma_dev(void)
  352. {
  353. return ipa3_ctx->pdev;
  354. }
  355. /**
  356. * ipa3_get_smmu_ctx()- Return smmu context for the given cb_type
  357. *
  358. * Return value: pointer to smmu context address
  359. */
  360. struct ipa_smmu_cb_ctx *ipa3_get_smmu_ctx(enum ipa_smmu_cb_type cb_type)
  361. {
  362. return &smmu_cb[cb_type];
  363. }
  364. static int ipa3_open(struct inode *inode, struct file *filp)
  365. {
  366. IPADBG_LOW("ENTER\n");
  367. filp->private_data = ipa3_ctx;
  368. return 0;
  369. }
  370. static void ipa3_wan_msg_free_cb(void *buff, u32 len, u32 type)
  371. {
  372. if (!buff) {
  373. IPAERR("Null buffer\n");
  374. return;
  375. }
  376. if (type != WAN_UPSTREAM_ROUTE_ADD &&
  377. type != WAN_UPSTREAM_ROUTE_DEL &&
  378. type != WAN_EMBMS_CONNECT) {
  379. IPAERR("Wrong type given. buff %pK type %d\n", buff, type);
  380. return;
  381. }
  382. kfree(buff);
  383. }
  384. static int ipa3_send_wan_msg(unsigned long usr_param,
  385. uint8_t msg_type, bool is_cache)
  386. {
  387. int retval;
  388. struct ipa_wan_msg *wan_msg;
  389. struct ipa_msg_meta msg_meta;
  390. struct ipa_wan_msg cache_wan_msg;
  391. wan_msg = kzalloc(sizeof(*wan_msg), GFP_KERNEL);
  392. if (!wan_msg)
  393. return -ENOMEM;
  394. if (copy_from_user(wan_msg, (const void __user *)usr_param,
  395. sizeof(struct ipa_wan_msg))) {
  396. kfree(wan_msg);
  397. return -EFAULT;
  398. }
  399. memcpy(&cache_wan_msg, wan_msg, sizeof(cache_wan_msg));
  400. memset(&msg_meta, 0, sizeof(struct ipa_msg_meta));
  401. msg_meta.msg_type = msg_type;
  402. msg_meta.msg_len = sizeof(struct ipa_wan_msg);
  403. retval = ipa3_send_msg(&msg_meta, wan_msg, ipa3_wan_msg_free_cb);
  404. if (retval) {
  405. IPAERR_RL("ipa3_send_msg failed: %d\n", retval);
  406. kfree(wan_msg);
  407. return retval;
  408. }
  409. if (is_cache) {
  410. mutex_lock(&ipa3_ctx->ipa_cne_evt_lock);
  411. /* cache the cne event */
  412. memcpy(&ipa3_ctx->ipa_cne_evt_req_cache[
  413. ipa3_ctx->num_ipa_cne_evt_req].wan_msg,
  414. &cache_wan_msg,
  415. sizeof(cache_wan_msg));
  416. memcpy(&ipa3_ctx->ipa_cne_evt_req_cache[
  417. ipa3_ctx->num_ipa_cne_evt_req].msg_meta,
  418. &msg_meta,
  419. sizeof(struct ipa_msg_meta));
  420. ipa3_ctx->num_ipa_cne_evt_req++;
  421. ipa3_ctx->num_ipa_cne_evt_req %= IPA_MAX_NUM_REQ_CACHE;
  422. mutex_unlock(&ipa3_ctx->ipa_cne_evt_lock);
  423. }
  424. return 0;
  425. }
  426. static void ipa3_vlan_l2tp_msg_free_cb(void *buff, u32 len, u32 type)
  427. {
  428. if (!buff) {
  429. IPAERR("Null buffer\n");
  430. return;
  431. }
  432. switch (type) {
  433. case ADD_VLAN_IFACE:
  434. case DEL_VLAN_IFACE:
  435. case ADD_L2TP_VLAN_MAPPING:
  436. case DEL_L2TP_VLAN_MAPPING:
  437. case ADD_BRIDGE_VLAN_MAPPING:
  438. case DEL_BRIDGE_VLAN_MAPPING:
  439. break;
  440. default:
  441. IPAERR("Wrong type given. buff %pK type %d\n", buff, type);
  442. return;
  443. }
  444. kfree(buff);
  445. }
  446. static int ipa3_send_vlan_l2tp_msg(unsigned long usr_param, uint8_t msg_type)
  447. {
  448. int retval;
  449. struct ipa_ioc_vlan_iface_info *vlan_info;
  450. struct ipa_ioc_l2tp_vlan_mapping_info *mapping_info;
  451. struct ipa_ioc_bridge_vlan_mapping_info *bridge_vlan_info;
  452. struct ipa_msg_meta msg_meta;
  453. void *buff;
  454. IPADBG("type %d\n", msg_type);
  455. memset(&msg_meta, 0, sizeof(msg_meta));
  456. msg_meta.msg_type = msg_type;
  457. if ((msg_type == ADD_VLAN_IFACE) ||
  458. (msg_type == DEL_VLAN_IFACE)) {
  459. vlan_info = kzalloc(sizeof(struct ipa_ioc_vlan_iface_info),
  460. GFP_KERNEL);
  461. if (!vlan_info)
  462. return -ENOMEM;
  463. if (copy_from_user((u8 *)vlan_info, (void __user *)usr_param,
  464. sizeof(struct ipa_ioc_vlan_iface_info))) {
  465. kfree(vlan_info);
  466. return -EFAULT;
  467. }
  468. msg_meta.msg_len = sizeof(struct ipa_ioc_vlan_iface_info);
  469. buff = vlan_info;
  470. } else if ((msg_type == ADD_L2TP_VLAN_MAPPING) ||
  471. (msg_type == DEL_L2TP_VLAN_MAPPING)) {
  472. mapping_info = kzalloc(sizeof(struct
  473. ipa_ioc_l2tp_vlan_mapping_info), GFP_KERNEL);
  474. if (!mapping_info)
  475. return -ENOMEM;
  476. if (copy_from_user((u8 *)mapping_info,
  477. (void __user *)usr_param,
  478. sizeof(struct ipa_ioc_l2tp_vlan_mapping_info))) {
  479. kfree(mapping_info);
  480. return -EFAULT;
  481. }
  482. msg_meta.msg_len = sizeof(struct
  483. ipa_ioc_l2tp_vlan_mapping_info);
  484. buff = mapping_info;
  485. } else if ((msg_type == ADD_BRIDGE_VLAN_MAPPING) ||
  486. (msg_type == DEL_BRIDGE_VLAN_MAPPING)) {
  487. bridge_vlan_info = kzalloc(
  488. sizeof(struct ipa_ioc_bridge_vlan_mapping_info),
  489. GFP_KERNEL);
  490. if (!bridge_vlan_info)
  491. return -ENOMEM;
  492. if (copy_from_user((u8 *)bridge_vlan_info,
  493. (void __user *)usr_param,
  494. sizeof(struct ipa_ioc_bridge_vlan_mapping_info))) {
  495. kfree(bridge_vlan_info);
  496. IPAERR("copy from user failed\n");
  497. return -EFAULT;
  498. }
  499. msg_meta.msg_len = sizeof(struct
  500. ipa_ioc_bridge_vlan_mapping_info);
  501. buff = bridge_vlan_info;
  502. } else {
  503. IPAERR("Unexpected event\n");
  504. return -EFAULT;
  505. }
  506. retval = ipa3_send_msg(&msg_meta, buff,
  507. ipa3_vlan_l2tp_msg_free_cb);
  508. if (retval) {
  509. IPAERR("ipa3_send_msg failed: %d, msg_type %d\n",
  510. retval,
  511. msg_type);
  512. kfree(buff);
  513. return retval;
  514. }
  515. IPADBG("exit\n");
  516. return 0;
  517. }
  518. static void ipa3_gsb_msg_free_cb(void *buff, u32 len, u32 type)
  519. {
  520. if (!buff) {
  521. IPAERR("Null buffer\n");
  522. return;
  523. }
  524. switch (type) {
  525. case IPA_GSB_CONNECT:
  526. case IPA_GSB_DISCONNECT:
  527. break;
  528. default:
  529. IPAERR("Wrong type given. buff %pK type %d\n", buff, type);
  530. return;
  531. }
  532. kfree(buff);
  533. }
  534. static int ipa3_send_gsb_msg(unsigned long usr_param, uint8_t msg_type)
  535. {
  536. int retval;
  537. struct ipa_ioc_gsb_info *gsb_info;
  538. struct ipa_msg_meta msg_meta;
  539. void *buff;
  540. IPADBG("type %d\n", msg_type);
  541. memset(&msg_meta, 0, sizeof(msg_meta));
  542. msg_meta.msg_type = msg_type;
  543. if ((msg_type == IPA_GSB_CONNECT) ||
  544. (msg_type == IPA_GSB_DISCONNECT)) {
  545. gsb_info = kzalloc(sizeof(struct ipa_ioc_gsb_info),
  546. GFP_KERNEL);
  547. if (!gsb_info) {
  548. IPAERR("no memory\n");
  549. return -ENOMEM;
  550. }
  551. if (copy_from_user((u8 *)gsb_info, (void __user *)usr_param,
  552. sizeof(struct ipa_ioc_gsb_info))) {
  553. kfree(gsb_info);
  554. return -EFAULT;
  555. }
  556. msg_meta.msg_len = sizeof(struct ipa_ioc_gsb_info);
  557. buff = gsb_info;
  558. } else {
  559. IPAERR("Unexpected event\n");
  560. return -EFAULT;
  561. }
  562. retval = ipa3_send_msg(&msg_meta, buff,
  563. ipa3_gsb_msg_free_cb);
  564. if (retval) {
  565. IPAERR("ipa3_send_msg failed: %d, msg_type %d\n",
  566. retval,
  567. msg_type);
  568. kfree(buff);
  569. return retval;
  570. }
  571. IPADBG("exit\n");
  572. return 0;
  573. }
  574. static int ipa3_ioctl_add_rt_rule_v2(unsigned long arg)
  575. {
  576. int retval = 0;
  577. int i;
  578. u8 header[128] = { 0 };
  579. int pre_entry;
  580. u32 usr_pyld_sz;
  581. u32 pyld_sz;
  582. u64 uptr = 0;
  583. u8 *param = NULL;
  584. u8 *kptr = NULL;
  585. if (copy_from_user(header, (const void __user *)arg,
  586. sizeof(struct ipa_ioc_add_rt_rule_v2))) {
  587. IPAERR_RL("copy_from_user fails\n");
  588. retval = -EFAULT;
  589. goto free_param_kptr;
  590. }
  591. pre_entry =
  592. ((struct ipa_ioc_add_rt_rule_v2 *)header)->num_rules;
  593. if (unlikely(((struct ipa_ioc_add_rt_rule_v2 *)
  594. header)->rule_add_size >
  595. sizeof(struct ipa_rt_rule_add_i))) {
  596. IPAERR_RL("unexpected rule_add_size %d\n",
  597. ((struct ipa_ioc_add_rt_rule_v2 *)
  598. header)->rule_add_size);
  599. retval = -EPERM;
  600. goto free_param_kptr;
  601. }
  602. /* user payload size */
  603. usr_pyld_sz = ((struct ipa_ioc_add_rt_rule_v2 *)
  604. header)->rule_add_size * pre_entry;
  605. /* actual payload structure size in kernel */
  606. pyld_sz = sizeof(struct ipa_rt_rule_add_i) * pre_entry;
  607. uptr = ((struct ipa_ioc_add_rt_rule_v2 *)
  608. header)->rules;
  609. if (unlikely(!uptr)) {
  610. IPAERR_RL("unexpected NULL rules\n");
  611. retval = -EPERM;
  612. goto free_param_kptr;
  613. }
  614. /* alloc param with same payload size as user payload */
  615. param = memdup_user((const void __user *)uptr,
  616. usr_pyld_sz);
  617. if (IS_ERR(param)) {
  618. retval = -EFAULT;
  619. goto free_param_kptr;
  620. }
  621. /* alloc kernel pointer with actual payload size */
  622. kptr = kzalloc(pyld_sz, GFP_KERNEL);
  623. if (!kptr) {
  624. retval = -ENOMEM;
  625. goto free_param_kptr;
  626. }
  627. for (i = 0; i < pre_entry; i++)
  628. memcpy(kptr + i * sizeof(struct ipa_rt_rule_add_i),
  629. (void *)param + i *
  630. ((struct ipa_ioc_add_rt_rule_v2 *)
  631. header)->rule_add_size,
  632. ((struct ipa_ioc_add_rt_rule_v2 *)
  633. header)->rule_add_size);
  634. /* modify the rule pointer to the kernel pointer */
  635. ((struct ipa_ioc_add_rt_rule_v2 *)header)->rules =
  636. (u64)kptr;
  637. if (ipa3_add_rt_rule_usr_v2(
  638. (struct ipa_ioc_add_rt_rule_v2 *)header, true)) {
  639. IPAERR_RL("ipa3_add_rt_rule_usr_v2 fails\n");
  640. retval = -EPERM;
  641. goto free_param_kptr;
  642. }
  643. for (i = 0; i < pre_entry; i++)
  644. memcpy((void *)param + i *
  645. ((struct ipa_ioc_add_rt_rule_v2 *)
  646. header)->rule_add_size,
  647. kptr + i * sizeof(struct ipa_rt_rule_add_i),
  648. ((struct ipa_ioc_add_rt_rule_v2 *)
  649. header)->rule_add_size);
  650. if (copy_to_user((void __user *)uptr, param,
  651. usr_pyld_sz)) {
  652. IPAERR_RL("copy_to_user fails\n");
  653. retval = -EFAULT;
  654. goto free_param_kptr;
  655. }
  656. free_param_kptr:
  657. if (!IS_ERR(param))
  658. kfree(param);
  659. kfree(kptr);
  660. return retval;
  661. }
  662. static int ipa3_ioctl_add_rt_rule_ext_v2(unsigned long arg)
  663. {
  664. int retval = 0;
  665. int i;
  666. u8 header[128] = { 0 };
  667. int pre_entry;
  668. u32 usr_pyld_sz;
  669. u32 pyld_sz;
  670. u64 uptr = 0;
  671. u8 *param = NULL;
  672. u8 *kptr = NULL;
  673. if (copy_from_user(header,
  674. (const void __user *)arg,
  675. sizeof(struct ipa_ioc_add_rt_rule_ext_v2))) {
  676. IPAERR_RL("copy_from_user fails\n");
  677. retval = -EFAULT;
  678. goto free_param_kptr;
  679. }
  680. pre_entry =
  681. ((struct ipa_ioc_add_rt_rule_ext_v2 *)
  682. header)->num_rules;
  683. if (unlikely(((struct ipa_ioc_add_rt_rule_ext_v2 *)
  684. header)->rule_add_ext_size >
  685. sizeof(struct ipa_rt_rule_add_ext_i))) {
  686. IPAERR_RL("unexpected rule_add_size %d\n",
  687. ((struct ipa_ioc_add_rt_rule_ext_v2 *)
  688. header)->rule_add_ext_size);
  689. retval = -EPERM;
  690. goto free_param_kptr;
  691. }
  692. /* user payload size */
  693. usr_pyld_sz = ((struct ipa_ioc_add_rt_rule_ext_v2 *)
  694. header)->rule_add_ext_size * pre_entry;
  695. /* actual payload structure size in kernel */
  696. pyld_sz = sizeof(struct ipa_rt_rule_add_ext_i)
  697. * pre_entry;
  698. uptr = ((struct ipa_ioc_add_rt_rule_ext_v2 *)
  699. header)->rules;
  700. if (unlikely(!uptr)) {
  701. IPAERR_RL("unexpected NULL rules\n");
  702. retval = -EPERM;
  703. goto free_param_kptr;
  704. }
  705. /* alloc param with same payload size as user payload */
  706. param = memdup_user((const void __user *)uptr,
  707. usr_pyld_sz);
  708. if (IS_ERR(param)) {
  709. retval = -EFAULT;
  710. goto free_param_kptr;
  711. }
  712. /* alloc kernel pointer with actual payload size */
  713. kptr = kzalloc(pyld_sz, GFP_KERNEL);
  714. if (!kptr) {
  715. retval = -ENOMEM;
  716. goto free_param_kptr;
  717. }
  718. for (i = 0; i < pre_entry; i++)
  719. memcpy(kptr + i *
  720. sizeof(struct ipa_rt_rule_add_ext_i),
  721. (void *)param + i *
  722. ((struct ipa_ioc_add_rt_rule_ext_v2 *)
  723. header)->rule_add_ext_size,
  724. ((struct ipa_ioc_add_rt_rule_ext_v2 *)
  725. header)->rule_add_ext_size);
  726. /* modify the rule pointer to the kernel pointer */
  727. ((struct ipa_ioc_add_rt_rule_ext_v2 *)header)->rules =
  728. (u64)kptr;
  729. if (ipa3_add_rt_rule_ext_v2(
  730. (struct ipa_ioc_add_rt_rule_ext_v2 *)header)) {
  731. IPAERR_RL("ipa3_add_rt_rule_ext_v2 fails\n");
  732. retval = -EPERM;
  733. goto free_param_kptr;
  734. }
  735. for (i = 0; i < pre_entry; i++)
  736. memcpy((void *)param + i *
  737. ((struct ipa_ioc_add_rt_rule_ext_v2 *)
  738. header)->rule_add_ext_size,
  739. kptr + i *
  740. sizeof(struct ipa_rt_rule_add_ext_i),
  741. ((struct ipa_ioc_add_rt_rule_ext_v2 *)
  742. header)->rule_add_ext_size);
  743. if (copy_to_user((void __user *)uptr, param,
  744. usr_pyld_sz)) {
  745. IPAERR_RL("copy_to_user fails\n");
  746. retval = -EFAULT;
  747. goto free_param_kptr;
  748. }
  749. free_param_kptr:
  750. if (!IS_ERR(param))
  751. kfree(param);
  752. kfree(kptr);
  753. return retval;
  754. }
  755. static int ipa3_ioctl_add_rt_rule_after_v2(unsigned long arg)
  756. {
  757. int retval = 0;
  758. int i;
  759. u8 header[128] = { 0 };
  760. int pre_entry;
  761. u32 usr_pyld_sz;
  762. u32 pyld_sz;
  763. u64 uptr = 0;
  764. u8 *param = NULL;
  765. u8 *kptr = NULL;
  766. if (copy_from_user(header, (const void __user *)arg,
  767. sizeof(struct ipa_ioc_add_rt_rule_after_v2))) {
  768. IPAERR_RL("copy_from_user fails\n");
  769. retval = -EFAULT;
  770. goto free_param_kptr;
  771. }
  772. pre_entry =
  773. ((struct ipa_ioc_add_rt_rule_after_v2 *)
  774. header)->num_rules;
  775. if (unlikely(((struct ipa_ioc_add_rt_rule_after_v2 *)
  776. header)->rule_add_size >
  777. sizeof(struct ipa_rt_rule_add_i))) {
  778. IPAERR_RL("unexpected rule_add_size %d\n",
  779. ((struct ipa_ioc_add_rt_rule_after_v2 *)
  780. header)->rule_add_size);
  781. retval = -EPERM;
  782. goto free_param_kptr;
  783. }
  784. /* user payload size */
  785. usr_pyld_sz = ((struct ipa_ioc_add_rt_rule_after_v2 *)
  786. header)->rule_add_size * pre_entry;
  787. /* actual payload structure size in kernel */
  788. pyld_sz = sizeof(struct ipa_rt_rule_add_i)
  789. * pre_entry;
  790. uptr = ((struct ipa_ioc_add_rt_rule_after_v2 *)
  791. header)->rules;
  792. if (unlikely(!uptr)) {
  793. IPAERR_RL("unexpected NULL rules\n");
  794. retval = -EPERM;
  795. goto free_param_kptr;
  796. }
  797. /* alloc param with same payload size as user payload */
  798. param = memdup_user((const void __user *)uptr,
  799. usr_pyld_sz);
  800. if (IS_ERR(param)) {
  801. retval = -EFAULT;
  802. goto free_param_kptr;
  803. }
  804. /* alloc kernel pointer with actual payload size */
  805. kptr = kzalloc(pyld_sz, GFP_KERNEL);
  806. if (!kptr) {
  807. retval = -ENOMEM;
  808. goto free_param_kptr;
  809. }
  810. for (i = 0; i < pre_entry; i++)
  811. memcpy(kptr + i * sizeof(struct ipa_rt_rule_add_i),
  812. (void *)param + i *
  813. ((struct ipa_ioc_add_rt_rule_after_v2 *)
  814. header)->rule_add_size,
  815. ((struct ipa_ioc_add_rt_rule_after_v2 *)
  816. header)->rule_add_size);
  817. /* modify the rule pointer to the kernel pointer */
  818. ((struct ipa_ioc_add_rt_rule_after_v2 *)header)->rules =
  819. (u64)kptr;
  820. if (ipa3_add_rt_rule_after_v2(
  821. (struct ipa_ioc_add_rt_rule_after_v2 *)header)) {
  822. IPAERR_RL("ipa3_add_rt_rule_after_v2 fails\n");
  823. retval = -EPERM;
  824. goto free_param_kptr;
  825. }
  826. for (i = 0; i < pre_entry; i++)
  827. memcpy((void *)param + i *
  828. ((struct ipa_ioc_add_rt_rule_after_v2 *)
  829. header)->rule_add_size,
  830. kptr + i * sizeof(struct ipa_rt_rule_add_i),
  831. ((struct ipa_ioc_add_rt_rule_after_v2 *)
  832. header)->rule_add_size);
  833. if (copy_to_user((void __user *)uptr, param,
  834. usr_pyld_sz)) {
  835. IPAERR_RL("copy_to_user fails\n");
  836. retval = -EFAULT;
  837. goto free_param_kptr;
  838. }
  839. free_param_kptr:
  840. if (!IS_ERR(param))
  841. kfree(param);
  842. kfree(kptr);
  843. return retval;
  844. }
  845. static int ipa3_ioctl_mdfy_rt_rule_v2(unsigned long arg)
  846. {
  847. int retval = 0;
  848. int i;
  849. u8 header[128] = { 0 };
  850. int pre_entry;
  851. u32 usr_pyld_sz;
  852. u32 pyld_sz;
  853. u64 uptr = 0;
  854. u8 *param = NULL;
  855. u8 *kptr = NULL;
  856. if (copy_from_user(header, (const void __user *)arg,
  857. sizeof(struct ipa_ioc_mdfy_rt_rule_v2))) {
  858. IPAERR_RL("copy_from_user fails\n");
  859. retval = -EFAULT;
  860. goto free_param_kptr;
  861. }
  862. pre_entry =
  863. ((struct ipa_ioc_mdfy_rt_rule_v2 *)
  864. header)->num_rules;
  865. if (unlikely(((struct ipa_ioc_mdfy_rt_rule_v2 *)
  866. header)->rule_mdfy_size >
  867. sizeof(struct ipa_rt_rule_mdfy_i))) {
  868. IPAERR_RL("unexpected rule_add_size %d\n",
  869. ((struct ipa_ioc_mdfy_rt_rule_v2 *)
  870. header)->rule_mdfy_size);
  871. retval = -EPERM;
  872. goto free_param_kptr;
  873. }
  874. /* user payload size */
  875. usr_pyld_sz = ((struct ipa_ioc_mdfy_rt_rule_v2 *)
  876. header)->rule_mdfy_size * pre_entry;
  877. /* actual payload structure size in kernel */
  878. pyld_sz = sizeof(struct ipa_rt_rule_mdfy_i)
  879. * pre_entry;
  880. uptr = ((struct ipa_ioc_mdfy_rt_rule_v2 *)
  881. header)->rules;
  882. if (unlikely(!uptr)) {
  883. IPAERR_RL("unexpected NULL rules\n");
  884. retval = -EPERM;
  885. goto free_param_kptr;
  886. }
  887. /* alloc param with same payload size as user payload */
  888. param = memdup_user((const void __user *)uptr,
  889. usr_pyld_sz);
  890. if (IS_ERR(param)) {
  891. retval = -EFAULT;
  892. goto free_param_kptr;
  893. }
  894. /* alloc kernel pointer with actual payload size */
  895. kptr = kzalloc(pyld_sz, GFP_KERNEL);
  896. if (!kptr) {
  897. retval = -ENOMEM;
  898. goto free_param_kptr;
  899. }
  900. for (i = 0; i < pre_entry; i++)
  901. memcpy(kptr + i * sizeof(struct ipa_rt_rule_mdfy_i),
  902. (void *)param + i *
  903. ((struct ipa_ioc_mdfy_rt_rule_v2 *)
  904. header)->rule_mdfy_size,
  905. ((struct ipa_ioc_mdfy_rt_rule_v2 *)
  906. header)->rule_mdfy_size);
  907. /* modify the rule pointer to the kernel pointer */
  908. ((struct ipa_ioc_mdfy_rt_rule_v2 *)header)->rules =
  909. (u64)kptr;
  910. if (ipa3_mdfy_rt_rule_v2((struct ipa_ioc_mdfy_rt_rule_v2 *)
  911. header)) {
  912. IPAERR_RL("ipa3_mdfy_rt_rule_v2 fails\n");
  913. retval = -EPERM;
  914. goto free_param_kptr;
  915. }
  916. for (i = 0; i < pre_entry; i++)
  917. memcpy((void *)param + i *
  918. ((struct ipa_ioc_mdfy_rt_rule_v2 *)
  919. header)->rule_mdfy_size,
  920. kptr + i * sizeof(struct ipa_rt_rule_mdfy_i),
  921. ((struct ipa_ioc_mdfy_rt_rule_v2 *)
  922. header)->rule_mdfy_size);
  923. if (copy_to_user((void __user *)uptr, param,
  924. usr_pyld_sz)) {
  925. IPAERR_RL("copy_to_user fails\n");
  926. retval = -EFAULT;
  927. goto free_param_kptr;
  928. }
  929. free_param_kptr:
  930. if (!IS_ERR(param))
  931. kfree(param);
  932. kfree(kptr);
  933. return retval;
  934. }
  935. static int ipa3_ioctl_add_flt_rule_v2(unsigned long arg)
  936. {
  937. int retval = 0;
  938. int i;
  939. u8 header[128] = { 0 };
  940. int pre_entry;
  941. u32 usr_pyld_sz;
  942. u32 pyld_sz;
  943. u64 uptr = 0;
  944. u8 *param = NULL;
  945. u8 *kptr = NULL;
  946. if (copy_from_user(header, (const void __user *)arg,
  947. sizeof(struct ipa_ioc_add_flt_rule_v2))) {
  948. IPAERR_RL("copy_from_user fails\n");
  949. retval = -EFAULT;
  950. goto free_param_kptr;
  951. }
  952. pre_entry =
  953. ((struct ipa_ioc_add_flt_rule_v2 *)header)->num_rules;
  954. if (unlikely(((struct ipa_ioc_add_flt_rule_v2 *)
  955. header)->flt_rule_size >
  956. sizeof(struct ipa_flt_rule_add_i))) {
  957. IPAERR_RL("unexpected rule_add_size %d\n",
  958. ((struct ipa_ioc_add_flt_rule_v2 *)
  959. header)->flt_rule_size);
  960. retval = -EPERM;
  961. goto free_param_kptr;
  962. }
  963. /* user payload size */
  964. usr_pyld_sz = ((struct ipa_ioc_add_flt_rule_v2 *)
  965. header)->flt_rule_size * pre_entry;
  966. /* actual payload structure size in kernel */
  967. pyld_sz = sizeof(struct ipa_flt_rule_add_i)
  968. * pre_entry;
  969. uptr = ((struct ipa_ioc_add_flt_rule_v2 *)
  970. header)->rules;
  971. if (unlikely(!uptr)) {
  972. IPAERR_RL("unexpected NULL rules\n");
  973. retval = -EPERM;
  974. goto free_param_kptr;
  975. }
  976. /* alloc param with same payload size as user payload */
  977. param = memdup_user((const void __user *)uptr,
  978. usr_pyld_sz);
  979. if (IS_ERR(param)) {
  980. retval = -EFAULT;
  981. goto free_param_kptr;
  982. }
  983. /* alloc kernel pointer with actual payload size */
  984. kptr = kzalloc(pyld_sz, GFP_KERNEL);
  985. if (!kptr) {
  986. retval = -ENOMEM;
  987. goto free_param_kptr;
  988. }
  989. for (i = 0; i < pre_entry; i++)
  990. memcpy(kptr + i * sizeof(struct ipa_flt_rule_add_i),
  991. (void *)param + i *
  992. ((struct ipa_ioc_add_flt_rule_v2 *)
  993. header)->flt_rule_size,
  994. ((struct ipa_ioc_add_flt_rule_v2 *)
  995. header)->flt_rule_size);
  996. /* modify the rule pointer to the kernel pointer */
  997. ((struct ipa_ioc_add_flt_rule_v2 *)header)->rules =
  998. (u64)kptr;
  999. if (ipa3_add_flt_rule_usr_v2((struct ipa_ioc_add_flt_rule_v2 *)
  1000. header, true)) {
  1001. IPAERR_RL("ipa3_add_flt_rule_usr_v2 fails\n");
  1002. retval = -EPERM;
  1003. goto free_param_kptr;
  1004. }
  1005. for (i = 0; i < pre_entry; i++)
  1006. memcpy((void *)param + i *
  1007. ((struct ipa_ioc_add_flt_rule_v2 *)
  1008. header)->flt_rule_size,
  1009. kptr + i * sizeof(struct ipa_flt_rule_add_i),
  1010. ((struct ipa_ioc_add_flt_rule_v2 *)
  1011. header)->flt_rule_size);
  1012. if (copy_to_user((void __user *)uptr, param,
  1013. usr_pyld_sz)) {
  1014. IPAERR_RL("copy_to_user fails\n");
  1015. retval = -EFAULT;
  1016. goto free_param_kptr;
  1017. }
  1018. free_param_kptr:
  1019. if (!IS_ERR(param))
  1020. kfree(param);
  1021. kfree(kptr);
  1022. return retval;
  1023. }
  1024. static int ipa3_ioctl_add_flt_rule_after_v2(unsigned long arg)
  1025. {
  1026. int retval = 0;
  1027. int i;
  1028. u8 header[128] = { 0 };
  1029. int pre_entry;
  1030. u32 usr_pyld_sz;
  1031. u32 pyld_sz;
  1032. u64 uptr = 0;
  1033. u8 *param = NULL;
  1034. u8 *kptr = NULL;
  1035. if (copy_from_user(header, (const void __user *)arg,
  1036. sizeof(struct ipa_ioc_add_flt_rule_after_v2))) {
  1037. IPAERR_RL("copy_from_user fails\n");
  1038. retval = -EFAULT;
  1039. goto free_param_kptr;
  1040. }
  1041. pre_entry =
  1042. ((struct ipa_ioc_add_flt_rule_after_v2 *)
  1043. header)->num_rules;
  1044. if (unlikely(((struct ipa_ioc_add_flt_rule_after_v2 *)
  1045. header)->flt_rule_size >
  1046. sizeof(struct ipa_flt_rule_add_i))) {
  1047. IPAERR_RL("unexpected rule_add_size %d\n",
  1048. ((struct ipa_ioc_add_flt_rule_after_v2 *)
  1049. header)->flt_rule_size);
  1050. retval = -EPERM;
  1051. goto free_param_kptr;
  1052. }
  1053. /* user payload size */
  1054. usr_pyld_sz = ((struct ipa_ioc_add_flt_rule_after_v2 *)
  1055. header)->flt_rule_size * pre_entry;
  1056. /* actual payload structure size in kernel */
  1057. pyld_sz = sizeof(struct ipa_flt_rule_add_i)
  1058. * pre_entry;
  1059. uptr = ((struct ipa_ioc_add_flt_rule_after_v2 *)
  1060. header)->rules;
  1061. if (unlikely(!uptr)) {
  1062. IPAERR_RL("unexpected NULL rules\n");
  1063. retval = -EPERM;
  1064. goto free_param_kptr;
  1065. }
  1066. /* alloc param with same payload size as user payload */
  1067. param = memdup_user((const void __user *)uptr,
  1068. usr_pyld_sz);
  1069. if (IS_ERR(param)) {
  1070. retval = -EFAULT;
  1071. goto free_param_kptr;
  1072. }
  1073. /* alloc kernel pointer with actual payload size */
  1074. kptr = kzalloc(pyld_sz, GFP_KERNEL);
  1075. if (!kptr) {
  1076. retval = -ENOMEM;
  1077. goto free_param_kptr;
  1078. }
  1079. for (i = 0; i < pre_entry; i++)
  1080. memcpy(kptr + i * sizeof(struct ipa_flt_rule_add_i),
  1081. (void *)param + i *
  1082. ((struct ipa_ioc_add_flt_rule_after_v2 *)
  1083. header)->flt_rule_size,
  1084. ((struct ipa_ioc_add_flt_rule_after_v2 *)
  1085. header)->flt_rule_size);
  1086. /* modify the rule pointer to the kernel pointer */
  1087. ((struct ipa_ioc_add_flt_rule_after_v2 *)header)->rules =
  1088. (u64)kptr;
  1089. if (ipa3_add_flt_rule_after_v2(
  1090. (struct ipa_ioc_add_flt_rule_after_v2 *)header)) {
  1091. IPAERR_RL("ipa3_add_flt_rule_after_v2 fails\n");
  1092. retval = -EPERM;
  1093. goto free_param_kptr;
  1094. }
  1095. for (i = 0; i < pre_entry; i++)
  1096. memcpy((void *)param + i *
  1097. ((struct ipa_ioc_add_flt_rule_after_v2 *)
  1098. header)->flt_rule_size,
  1099. kptr + i * sizeof(struct ipa_flt_rule_add_i),
  1100. ((struct ipa_ioc_add_flt_rule_after_v2 *)
  1101. header)->flt_rule_size);
  1102. if (copy_to_user((void __user *)uptr, param,
  1103. usr_pyld_sz)) {
  1104. IPAERR_RL("copy_to_user fails\n");
  1105. retval = -EFAULT;
  1106. goto free_param_kptr;
  1107. }
  1108. free_param_kptr:
  1109. if (!IS_ERR(param))
  1110. kfree(param);
  1111. kfree(kptr);
  1112. return retval;
  1113. }
  1114. static int ipa3_ioctl_mdfy_flt_rule_v2(unsigned long arg)
  1115. {
  1116. int retval = 0;
  1117. int i;
  1118. u8 header[128] = { 0 };
  1119. int pre_entry;
  1120. u32 usr_pyld_sz;
  1121. u32 pyld_sz;
  1122. u64 uptr = 0;
  1123. u8 *param = NULL;
  1124. u8 *kptr = NULL;
  1125. if (copy_from_user(header, (const void __user *)arg,
  1126. sizeof(struct ipa_ioc_mdfy_flt_rule_v2))) {
  1127. IPAERR_RL("copy_from_user fails\n");
  1128. retval = -EFAULT;
  1129. goto free_param_kptr;
  1130. }
  1131. pre_entry =
  1132. ((struct ipa_ioc_mdfy_flt_rule_v2 *)
  1133. header)->num_rules;
  1134. if (unlikely(((struct ipa_ioc_mdfy_flt_rule_v2 *)
  1135. header)->rule_mdfy_size >
  1136. sizeof(struct ipa_flt_rule_mdfy_i))) {
  1137. IPAERR_RL("unexpected rule_add_size %d\n",
  1138. ((struct ipa_ioc_mdfy_flt_rule_v2 *)
  1139. header)->rule_mdfy_size);
  1140. retval = -EPERM;
  1141. goto free_param_kptr;
  1142. }
  1143. /* user payload size */
  1144. usr_pyld_sz = ((struct ipa_ioc_mdfy_flt_rule_v2 *)
  1145. header)->rule_mdfy_size * pre_entry;
  1146. /* actual payload structure size in kernel */
  1147. pyld_sz = sizeof(struct ipa_flt_rule_mdfy_i)
  1148. * pre_entry;
  1149. uptr = ((struct ipa_ioc_mdfy_flt_rule_v2 *)
  1150. header)->rules;
  1151. if (unlikely(!uptr)) {
  1152. IPAERR_RL("unexpected NULL rules\n");
  1153. retval = -EPERM;
  1154. goto free_param_kptr;
  1155. }
  1156. /* alloc param with same payload size as user payload */
  1157. param = memdup_user((const void __user *)uptr,
  1158. usr_pyld_sz);
  1159. if (IS_ERR(param)) {
  1160. retval = -EFAULT;
  1161. goto free_param_kptr;
  1162. }
  1163. /* alloc kernel pointer with actual payload size */
  1164. kptr = kzalloc(pyld_sz, GFP_KERNEL);
  1165. if (!kptr) {
  1166. retval = -ENOMEM;
  1167. goto free_param_kptr;
  1168. }
  1169. for (i = 0; i < pre_entry; i++)
  1170. memcpy(kptr + i * sizeof(struct ipa_flt_rule_mdfy_i),
  1171. (void *)param + i *
  1172. ((struct ipa_ioc_mdfy_flt_rule_v2 *)
  1173. header)->rule_mdfy_size,
  1174. ((struct ipa_ioc_mdfy_flt_rule_v2 *)
  1175. header)->rule_mdfy_size);
  1176. /* modify the rule pointer to the kernel pointer */
  1177. ((struct ipa_ioc_mdfy_flt_rule_v2 *)header)->rules =
  1178. (u64)kptr;
  1179. if (ipa3_mdfy_flt_rule_v2
  1180. ((struct ipa_ioc_mdfy_flt_rule_v2 *)header)) {
  1181. IPAERR_RL("ipa3_mdfy_flt_rule_v2 fails\n");
  1182. retval = -EPERM;
  1183. goto free_param_kptr;
  1184. }
  1185. for (i = 0; i < pre_entry; i++)
  1186. memcpy((void *)param + i *
  1187. ((struct ipa_ioc_mdfy_flt_rule_v2 *)
  1188. header)->rule_mdfy_size,
  1189. kptr + i * sizeof(struct ipa_flt_rule_mdfy_i),
  1190. ((struct ipa_ioc_mdfy_flt_rule_v2 *)
  1191. header)->rule_mdfy_size);
  1192. if (copy_to_user((void __user *)uptr, param,
  1193. usr_pyld_sz)) {
  1194. IPAERR_RL("copy_to_user fails\n");
  1195. retval = -EFAULT;
  1196. goto free_param_kptr;
  1197. }
  1198. free_param_kptr:
  1199. if (!IS_ERR(param))
  1200. kfree(param);
  1201. kfree(kptr);
  1202. return retval;
  1203. }
  1204. static int ipa3_ioctl_fnr_counter_alloc(unsigned long arg)
  1205. {
  1206. int retval = 0;
  1207. u8 header[128] = { 0 };
  1208. if (copy_from_user(header, (const void __user *)arg,
  1209. sizeof(struct ipa_ioc_flt_rt_counter_alloc))) {
  1210. IPAERR("copy_from_user fails\n");
  1211. return -EFAULT;
  1212. }
  1213. if (((struct ipa_ioc_flt_rt_counter_alloc *)
  1214. header)->hw_counter.num_counters >
  1215. IPA_FLT_RT_HW_COUNTER ||
  1216. ((struct ipa_ioc_flt_rt_counter_alloc *)
  1217. header)->sw_counter.num_counters >
  1218. IPA_FLT_RT_SW_COUNTER) {
  1219. IPAERR("failed: wrong sw/hw num_counters\n");
  1220. return -EPERM;
  1221. }
  1222. if (((struct ipa_ioc_flt_rt_counter_alloc *)
  1223. header)->hw_counter.num_counters == 0 &&
  1224. ((struct ipa_ioc_flt_rt_counter_alloc *)
  1225. header)->sw_counter.num_counters == 0) {
  1226. IPAERR("failed: both sw/hw num_counters 0\n");
  1227. return -EPERM;
  1228. }
  1229. retval = ipa3_alloc_counter_id
  1230. ((struct ipa_ioc_flt_rt_counter_alloc *)header);
  1231. if (retval < 0) {
  1232. IPAERR("ipa3_alloc_counter_id failed\n");
  1233. return retval;
  1234. }
  1235. if (copy_to_user((void __user *)arg, header,
  1236. sizeof(struct ipa_ioc_flt_rt_counter_alloc))) {
  1237. IPAERR("copy_to_user fails\n");
  1238. ipa3_counter_remove_hdl(
  1239. ((struct ipa_ioc_flt_rt_counter_alloc *)
  1240. header)->hdl);
  1241. return -EFAULT;
  1242. }
  1243. return 0;
  1244. }
  1245. static int ipa3_ioctl_fnr_counter_query(unsigned long arg)
  1246. {
  1247. int retval = 0;
  1248. int i;
  1249. u8 header[128] = { 0 };
  1250. int pre_entry;
  1251. u32 usr_pyld_sz;
  1252. u32 pyld_sz;
  1253. u64 uptr = 0;
  1254. u8 *param = NULL;
  1255. u8 *kptr = NULL;
  1256. if (copy_from_user(header, (const void __user *)arg,
  1257. sizeof(struct ipa_ioc_flt_rt_query))) {
  1258. IPAERR_RL("copy_from_user fails\n");
  1259. retval = -EFAULT;
  1260. goto free_param_kptr;
  1261. }
  1262. pre_entry =
  1263. ((struct ipa_ioc_flt_rt_query *)
  1264. header)->end_id - ((struct ipa_ioc_flt_rt_query *)
  1265. header)->start_id + 1;
  1266. if (pre_entry <= 0 || pre_entry > IPA_MAX_FLT_RT_CNT_INDEX) {
  1267. IPAERR("IPA_IOC_FNR_COUNTER_QUERY failed: num %d\n",
  1268. pre_entry);
  1269. retval = -EPERM;
  1270. goto free_param_kptr;
  1271. }
  1272. if (((struct ipa_ioc_flt_rt_query *)header)->stats_size
  1273. > sizeof(struct ipa_flt_rt_stats)) {
  1274. IPAERR_RL("unexpected stats_size %d\n",
  1275. ((struct ipa_ioc_flt_rt_query *)header)->stats_size);
  1276. retval = -EPERM;
  1277. goto free_param_kptr;
  1278. }
  1279. /* user payload size */
  1280. usr_pyld_sz = ((struct ipa_ioc_flt_rt_query *)
  1281. header)->stats_size * pre_entry;
  1282. /* actual payload structure size in kernel */
  1283. pyld_sz = sizeof(struct ipa_flt_rt_stats) * pre_entry;
  1284. uptr = ((struct ipa_ioc_flt_rt_query *)
  1285. header)->stats;
  1286. if (unlikely(!uptr)) {
  1287. IPAERR_RL("unexpected NULL rules\n");
  1288. retval = -EPERM;
  1289. goto free_param_kptr;
  1290. }
  1291. /* alloc param with same payload size as user payload */
  1292. param = memdup_user((const void __user *)uptr,
  1293. usr_pyld_sz);
  1294. if (IS_ERR(param)) {
  1295. retval = -EFAULT;
  1296. goto free_param_kptr;
  1297. }
  1298. /* alloc kernel pointer with actual payload size */
  1299. kptr = kzalloc(pyld_sz, GFP_KERNEL);
  1300. if (!kptr) {
  1301. retval = -ENOMEM;
  1302. goto free_param_kptr;
  1303. }
  1304. for (i = 0; i < pre_entry; i++)
  1305. memcpy(kptr + i * sizeof(struct ipa_flt_rt_stats),
  1306. (void *)param + i *
  1307. ((struct ipa_ioc_flt_rt_query *)
  1308. header)->stats_size,
  1309. ((struct ipa_ioc_flt_rt_query *)
  1310. header)->stats_size);
  1311. /* modify the rule pointer to the kernel pointer */
  1312. ((struct ipa_ioc_flt_rt_query *)
  1313. header)->stats = (u64)kptr;
  1314. retval = ipa_get_flt_rt_stats
  1315. ((struct ipa_ioc_flt_rt_query *)header);
  1316. if (retval < 0) {
  1317. IPAERR("ipa_get_flt_rt_stats failed\n");
  1318. retval = -EPERM;
  1319. goto free_param_kptr;
  1320. }
  1321. for (i = 0; i < pre_entry; i++)
  1322. memcpy((void *)param + i *
  1323. ((struct ipa_ioc_flt_rt_query *)
  1324. header)->stats_size,
  1325. kptr + i * sizeof(struct ipa_flt_rt_stats),
  1326. ((struct ipa_ioc_flt_rt_query *)
  1327. header)->stats_size);
  1328. if (copy_to_user((void __user *)uptr, param,
  1329. usr_pyld_sz)) {
  1330. IPAERR_RL("copy_to_user fails\n");
  1331. retval = -EFAULT;
  1332. goto free_param_kptr;
  1333. }
  1334. free_param_kptr:
  1335. if (!IS_ERR(param))
  1336. kfree(param);
  1337. kfree(kptr);
  1338. return retval;
  1339. }
  1340. static int ipa3_ioctl_fnr_counter_set(unsigned long arg)
  1341. {
  1342. u8 header[128] = { 0 };
  1343. uint8_t value;
  1344. if (copy_from_user(header, (const void __user *)arg,
  1345. sizeof(struct ipa_ioc_fnr_index_info))) {
  1346. IPAERR_RL("copy_from_user fails\n");
  1347. return -EFAULT;
  1348. }
  1349. value = ((struct ipa_ioc_fnr_index_info *)
  1350. header)->hw_counter_offset;
  1351. if (value <= 0 || value > IPA_MAX_FLT_RT_CNT_INDEX) {
  1352. IPAERR("hw_counter_offset failed: num %d\n",
  1353. value);
  1354. return -EPERM;
  1355. }
  1356. ipa3_ctx->fnr_info.hw_counter_offset = value;
  1357. value = ((struct ipa_ioc_fnr_index_info *)
  1358. header)->sw_counter_offset;
  1359. if (value <= 0 || value > IPA_MAX_FLT_RT_CNT_INDEX) {
  1360. IPAERR("sw_counter_offset failed: num %d\n",
  1361. value);
  1362. return -EPERM;
  1363. }
  1364. ipa3_ctx->fnr_info.sw_counter_offset = value;
  1365. /* reset when ipacm-cleanup */
  1366. ipa3_ctx->fnr_info.valid = true;
  1367. IPADBG("fnr_info hw=%d, hw=%d\n",
  1368. ipa3_ctx->fnr_info.hw_counter_offset,
  1369. ipa3_ctx->fnr_info.sw_counter_offset);
  1370. return 0;
  1371. }
  1372. static int proc_sram_info_rqst(
  1373. unsigned long arg)
  1374. {
  1375. struct ipa_nat_in_sram_info sram_info = { 0 };
  1376. if (ipa3_nat_get_sram_info(&sram_info))
  1377. return -EFAULT;
  1378. if (copy_to_user(
  1379. (void __user *) arg,
  1380. &sram_info,
  1381. sizeof(struct ipa_nat_in_sram_info)))
  1382. return -EFAULT;
  1383. return 0;
  1384. }
  1385. static long ipa3_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  1386. {
  1387. int retval = 0;
  1388. u32 pyld_sz;
  1389. u8 header[128] = { 0 };
  1390. u8 *param = NULL;
  1391. bool is_vlan_mode;
  1392. struct ipa_ioc_nat_alloc_mem nat_mem;
  1393. struct ipa_ioc_nat_ipv6ct_table_alloc table_alloc;
  1394. struct ipa_ioc_v4_nat_init nat_init;
  1395. struct ipa_ioc_ipv6ct_init ipv6ct_init;
  1396. struct ipa_ioc_v4_nat_del nat_del;
  1397. struct ipa_ioc_nat_ipv6ct_table_del table_del;
  1398. struct ipa_ioc_nat_pdn_entry mdfy_pdn;
  1399. struct ipa_ioc_nat_dma_cmd *table_dma_cmd;
  1400. struct ipa_ioc_get_vlan_mode vlan_mode;
  1401. struct ipa_ioc_wigig_fst_switch fst_switch;
  1402. size_t sz;
  1403. int pre_entry;
  1404. int hdl;
  1405. IPADBG("cmd=%x nr=%d\n", cmd, _IOC_NR(cmd));
  1406. if (_IOC_TYPE(cmd) != IPA_IOC_MAGIC)
  1407. return -ENOTTY;
  1408. if (!ipa3_is_ready()) {
  1409. IPAERR("IPA not ready, waiting for init completion\n");
  1410. wait_for_completion(&ipa3_ctx->init_completion_obj);
  1411. }
  1412. IPA_ACTIVE_CLIENTS_INC_SIMPLE();
  1413. switch (cmd) {
  1414. case IPA_IOC_ALLOC_NAT_MEM:
  1415. if (copy_from_user(&nat_mem, (const void __user *)arg,
  1416. sizeof(struct ipa_ioc_nat_alloc_mem))) {
  1417. retval = -EFAULT;
  1418. break;
  1419. }
  1420. /* null terminate the string */
  1421. nat_mem.dev_name[IPA_RESOURCE_NAME_MAX - 1] = '\0';
  1422. if (ipa3_allocate_nat_device(&nat_mem)) {
  1423. retval = -EFAULT;
  1424. break;
  1425. }
  1426. if (copy_to_user((void __user *)arg, &nat_mem,
  1427. sizeof(struct ipa_ioc_nat_alloc_mem))) {
  1428. retval = -EFAULT;
  1429. break;
  1430. }
  1431. break;
  1432. case IPA_IOC_ALLOC_NAT_TABLE:
  1433. if (copy_from_user(&table_alloc, (const void __user *)arg,
  1434. sizeof(struct ipa_ioc_nat_ipv6ct_table_alloc))) {
  1435. retval = -EFAULT;
  1436. break;
  1437. }
  1438. if (ipa3_allocate_nat_table(&table_alloc)) {
  1439. retval = -EFAULT;
  1440. break;
  1441. }
  1442. if (table_alloc.offset &&
  1443. copy_to_user((void __user *)arg, &table_alloc, sizeof(
  1444. struct ipa_ioc_nat_ipv6ct_table_alloc))) {
  1445. retval = -EFAULT;
  1446. break;
  1447. }
  1448. break;
  1449. case IPA_IOC_ALLOC_IPV6CT_TABLE:
  1450. if (copy_from_user(&table_alloc, (const void __user *)arg,
  1451. sizeof(struct ipa_ioc_nat_ipv6ct_table_alloc))) {
  1452. retval = -EFAULT;
  1453. break;
  1454. }
  1455. if (ipa3_allocate_ipv6ct_table(&table_alloc)) {
  1456. retval = -EFAULT;
  1457. break;
  1458. }
  1459. if (table_alloc.offset &&
  1460. copy_to_user((void __user *)arg, &table_alloc, sizeof(
  1461. struct ipa_ioc_nat_ipv6ct_table_alloc))) {
  1462. retval = -EFAULT;
  1463. break;
  1464. }
  1465. break;
  1466. case IPA_IOC_V4_INIT_NAT:
  1467. if (copy_from_user(&nat_init, (const void __user *)arg,
  1468. sizeof(struct ipa_ioc_v4_nat_init))) {
  1469. retval = -EFAULT;
  1470. break;
  1471. }
  1472. if (ipa3_nat_init_cmd(&nat_init)) {
  1473. retval = -EFAULT;
  1474. break;
  1475. }
  1476. break;
  1477. case IPA_IOC_INIT_IPV6CT_TABLE:
  1478. if (copy_from_user(&ipv6ct_init, (const void __user *)arg,
  1479. sizeof(struct ipa_ioc_ipv6ct_init))) {
  1480. retval = -EFAULT;
  1481. break;
  1482. }
  1483. if (ipa3_ipv6ct_init_cmd(&ipv6ct_init)) {
  1484. retval = -EFAULT;
  1485. break;
  1486. }
  1487. break;
  1488. case IPA_IOC_TABLE_DMA_CMD:
  1489. table_dma_cmd = (struct ipa_ioc_nat_dma_cmd *)header;
  1490. if (copy_from_user(header, (const void __user *)arg,
  1491. sizeof(struct ipa_ioc_nat_dma_cmd))) {
  1492. retval = -EFAULT;
  1493. break;
  1494. }
  1495. pre_entry = table_dma_cmd->entries;
  1496. pyld_sz = sizeof(struct ipa_ioc_nat_dma_cmd) +
  1497. pre_entry * sizeof(struct ipa_ioc_nat_dma_one);
  1498. param = memdup_user((const void __user *)arg, pyld_sz);
  1499. if (IS_ERR(param)) {
  1500. retval = PTR_ERR(param);
  1501. break;
  1502. }
  1503. table_dma_cmd = (struct ipa_ioc_nat_dma_cmd *)param;
  1504. /* add check in case user-space module compromised */
  1505. if (unlikely(table_dma_cmd->entries != pre_entry)) {
  1506. IPAERR_RL("current %d pre %d\n",
  1507. table_dma_cmd->entries, pre_entry);
  1508. retval = -EFAULT;
  1509. break;
  1510. }
  1511. if (ipa3_table_dma_cmd(table_dma_cmd)) {
  1512. retval = -EFAULT;
  1513. break;
  1514. }
  1515. break;
  1516. case IPA_IOC_V4_DEL_NAT:
  1517. if (copy_from_user(&nat_del, (const void __user *)arg,
  1518. sizeof(struct ipa_ioc_v4_nat_del))) {
  1519. retval = -EFAULT;
  1520. break;
  1521. }
  1522. if (ipa3_nat_del_cmd(&nat_del)) {
  1523. retval = -EFAULT;
  1524. break;
  1525. }
  1526. break;
  1527. case IPA_IOC_DEL_NAT_TABLE:
  1528. if (copy_from_user(&table_del, (const void __user *)arg,
  1529. sizeof(struct ipa_ioc_nat_ipv6ct_table_del))) {
  1530. retval = -EFAULT;
  1531. break;
  1532. }
  1533. if (ipa3_del_nat_table(&table_del)) {
  1534. retval = -EFAULT;
  1535. break;
  1536. }
  1537. break;
  1538. case IPA_IOC_DEL_IPV6CT_TABLE:
  1539. if (copy_from_user(&table_del, (const void __user *)arg,
  1540. sizeof(struct ipa_ioc_nat_ipv6ct_table_del))) {
  1541. retval = -EFAULT;
  1542. break;
  1543. }
  1544. if (ipa3_del_ipv6ct_table(&table_del)) {
  1545. retval = -EFAULT;
  1546. break;
  1547. }
  1548. break;
  1549. case IPA_IOC_NAT_MODIFY_PDN:
  1550. if (copy_from_user(&mdfy_pdn, (const void __user *)arg,
  1551. sizeof(struct ipa_ioc_nat_pdn_entry))) {
  1552. retval = -EFAULT;
  1553. break;
  1554. }
  1555. if (ipa3_nat_mdfy_pdn(&mdfy_pdn)) {
  1556. retval = -EFAULT;
  1557. break;
  1558. }
  1559. break;
  1560. case IPA_IOC_ADD_HDR:
  1561. if (copy_from_user(header, (const void __user *)arg,
  1562. sizeof(struct ipa_ioc_add_hdr))) {
  1563. retval = -EFAULT;
  1564. break;
  1565. }
  1566. pre_entry =
  1567. ((struct ipa_ioc_add_hdr *)header)->num_hdrs;
  1568. pyld_sz =
  1569. sizeof(struct ipa_ioc_add_hdr) +
  1570. pre_entry * sizeof(struct ipa_hdr_add);
  1571. param = memdup_user((const void __user *)arg, pyld_sz);
  1572. if (IS_ERR(param)) {
  1573. retval = PTR_ERR(param);
  1574. break;
  1575. }
  1576. /* add check in case user-space module compromised */
  1577. if (unlikely(((struct ipa_ioc_add_hdr *)param)->num_hdrs
  1578. != pre_entry)) {
  1579. IPAERR_RL("current %d pre %d\n",
  1580. ((struct ipa_ioc_add_hdr *)param)->num_hdrs,
  1581. pre_entry);
  1582. retval = -EFAULT;
  1583. break;
  1584. }
  1585. if (ipa3_add_hdr_usr((struct ipa_ioc_add_hdr *)param,
  1586. true)) {
  1587. retval = -EFAULT;
  1588. break;
  1589. }
  1590. if (copy_to_user((void __user *)arg, param, pyld_sz)) {
  1591. retval = -EFAULT;
  1592. break;
  1593. }
  1594. break;
  1595. case IPA_IOC_DEL_HDR:
  1596. if (copy_from_user(header, (const void __user *)arg,
  1597. sizeof(struct ipa_ioc_del_hdr))) {
  1598. retval = -EFAULT;
  1599. break;
  1600. }
  1601. pre_entry =
  1602. ((struct ipa_ioc_del_hdr *)header)->num_hdls;
  1603. pyld_sz =
  1604. sizeof(struct ipa_ioc_del_hdr) +
  1605. pre_entry * sizeof(struct ipa_hdr_del);
  1606. param = memdup_user((const void __user *)arg, pyld_sz);
  1607. if (IS_ERR(param)) {
  1608. retval = PTR_ERR(param);
  1609. break;
  1610. }
  1611. /* add check in case user-space module compromised */
  1612. if (unlikely(((struct ipa_ioc_del_hdr *)param)->num_hdls
  1613. != pre_entry)) {
  1614. IPAERR_RL("current %d pre %d\n",
  1615. ((struct ipa_ioc_del_hdr *)param)->num_hdls,
  1616. pre_entry);
  1617. retval = -EFAULT;
  1618. break;
  1619. }
  1620. if (ipa3_del_hdr_by_user((struct ipa_ioc_del_hdr *)param,
  1621. true)) {
  1622. retval = -EFAULT;
  1623. break;
  1624. }
  1625. if (copy_to_user((void __user *)arg, param, pyld_sz)) {
  1626. retval = -EFAULT;
  1627. break;
  1628. }
  1629. break;
  1630. case IPA_IOC_ADD_RT_RULE:
  1631. if (copy_from_user(header, (const void __user *)arg,
  1632. sizeof(struct ipa_ioc_add_rt_rule))) {
  1633. retval = -EFAULT;
  1634. break;
  1635. }
  1636. pre_entry =
  1637. ((struct ipa_ioc_add_rt_rule *)header)->num_rules;
  1638. pyld_sz =
  1639. sizeof(struct ipa_ioc_add_rt_rule) +
  1640. pre_entry * sizeof(struct ipa_rt_rule_add);
  1641. param = memdup_user((const void __user *)arg, pyld_sz);
  1642. if (IS_ERR(param)) {
  1643. retval = PTR_ERR(param);
  1644. break;
  1645. }
  1646. /* add check in case user-space module compromised */
  1647. if (unlikely(((struct ipa_ioc_add_rt_rule *)param)->num_rules
  1648. != pre_entry)) {
  1649. IPAERR_RL("current %d pre %d\n",
  1650. ((struct ipa_ioc_add_rt_rule *)param)->
  1651. num_rules,
  1652. pre_entry);
  1653. retval = -EFAULT;
  1654. break;
  1655. }
  1656. if (ipa3_add_rt_rule_usr((struct ipa_ioc_add_rt_rule *)param,
  1657. true)) {
  1658. retval = -EFAULT;
  1659. break;
  1660. }
  1661. if (copy_to_user((void __user *)arg, param, pyld_sz)) {
  1662. retval = -EFAULT;
  1663. break;
  1664. }
  1665. break;
  1666. case IPA_IOC_ADD_RT_RULE_EXT:
  1667. if (copy_from_user(header,
  1668. (const void __user *)arg,
  1669. sizeof(struct ipa_ioc_add_rt_rule_ext))) {
  1670. retval = -EFAULT;
  1671. break;
  1672. }
  1673. pre_entry =
  1674. ((struct ipa_ioc_add_rt_rule_ext *)header)->num_rules;
  1675. pyld_sz =
  1676. sizeof(struct ipa_ioc_add_rt_rule_ext) +
  1677. pre_entry * sizeof(struct ipa_rt_rule_add_ext);
  1678. param = memdup_user((const void __user *)arg, pyld_sz);
  1679. if (IS_ERR(param)) {
  1680. retval = PTR_ERR(param);
  1681. break;
  1682. }
  1683. /* add check in case user-space module compromised */
  1684. if (unlikely(
  1685. ((struct ipa_ioc_add_rt_rule_ext *)param)->num_rules
  1686. != pre_entry)) {
  1687. IPAERR(" prevent memory corruption(%d not match %d)\n",
  1688. ((struct ipa_ioc_add_rt_rule_ext *)param)->
  1689. num_rules,
  1690. pre_entry);
  1691. retval = -EINVAL;
  1692. break;
  1693. }
  1694. if (ipa3_add_rt_rule_ext(
  1695. (struct ipa_ioc_add_rt_rule_ext *)param)) {
  1696. retval = -EFAULT;
  1697. break;
  1698. }
  1699. if (copy_to_user((void __user *)arg, param, pyld_sz)) {
  1700. retval = -EFAULT;
  1701. break;
  1702. }
  1703. break;
  1704. case IPA_IOC_ADD_RT_RULE_AFTER:
  1705. if (copy_from_user(header, (const void __user *)arg,
  1706. sizeof(struct ipa_ioc_add_rt_rule_after))) {
  1707. retval = -EFAULT;
  1708. break;
  1709. }
  1710. pre_entry =
  1711. ((struct ipa_ioc_add_rt_rule_after *)header)->num_rules;
  1712. pyld_sz =
  1713. sizeof(struct ipa_ioc_add_rt_rule_after) +
  1714. pre_entry * sizeof(struct ipa_rt_rule_add);
  1715. param = memdup_user((const void __user *)arg, pyld_sz);
  1716. if (IS_ERR(param)) {
  1717. retval = PTR_ERR(param);
  1718. break;
  1719. }
  1720. /* add check in case user-space module compromised */
  1721. if (unlikely(((struct ipa_ioc_add_rt_rule_after *)param)->
  1722. num_rules != pre_entry)) {
  1723. IPAERR_RL("current %d pre %d\n",
  1724. ((struct ipa_ioc_add_rt_rule_after *)param)->
  1725. num_rules,
  1726. pre_entry);
  1727. retval = -EFAULT;
  1728. break;
  1729. }
  1730. if (ipa3_add_rt_rule_after(
  1731. (struct ipa_ioc_add_rt_rule_after *)param)) {
  1732. retval = -EFAULT;
  1733. break;
  1734. }
  1735. if (copy_to_user((void __user *)arg, param, pyld_sz)) {
  1736. retval = -EFAULT;
  1737. break;
  1738. }
  1739. break;
  1740. case IPA_IOC_MDFY_RT_RULE:
  1741. if (copy_from_user(header, (const void __user *)arg,
  1742. sizeof(struct ipa_ioc_mdfy_rt_rule))) {
  1743. retval = -EFAULT;
  1744. break;
  1745. }
  1746. pre_entry =
  1747. ((struct ipa_ioc_mdfy_rt_rule *)header)->num_rules;
  1748. pyld_sz =
  1749. sizeof(struct ipa_ioc_mdfy_rt_rule) +
  1750. pre_entry * sizeof(struct ipa_rt_rule_mdfy);
  1751. param = memdup_user((const void __user *)arg, pyld_sz);
  1752. if (IS_ERR(param)) {
  1753. retval = PTR_ERR(param);
  1754. break;
  1755. }
  1756. /* add check in case user-space module compromised */
  1757. if (unlikely(((struct ipa_ioc_mdfy_rt_rule *)param)->num_rules
  1758. != pre_entry)) {
  1759. IPAERR_RL("current %d pre %d\n",
  1760. ((struct ipa_ioc_mdfy_rt_rule *)param)->
  1761. num_rules,
  1762. pre_entry);
  1763. retval = -EFAULT;
  1764. break;
  1765. }
  1766. if (ipa3_mdfy_rt_rule((struct ipa_ioc_mdfy_rt_rule *)param)) {
  1767. retval = -EFAULT;
  1768. break;
  1769. }
  1770. if (copy_to_user((void __user *)arg, param, pyld_sz)) {
  1771. retval = -EFAULT;
  1772. break;
  1773. }
  1774. break;
  1775. case IPA_IOC_DEL_RT_RULE:
  1776. if (copy_from_user(header, (const void __user *)arg,
  1777. sizeof(struct ipa_ioc_del_rt_rule))) {
  1778. retval = -EFAULT;
  1779. break;
  1780. }
  1781. pre_entry =
  1782. ((struct ipa_ioc_del_rt_rule *)header)->num_hdls;
  1783. pyld_sz =
  1784. sizeof(struct ipa_ioc_del_rt_rule) +
  1785. pre_entry * sizeof(struct ipa_rt_rule_del);
  1786. param = memdup_user((const void __user *)arg, pyld_sz);
  1787. if (IS_ERR(param)) {
  1788. retval = PTR_ERR(param);
  1789. break;
  1790. }
  1791. /* add check in case user-space module compromised */
  1792. if (unlikely(((struct ipa_ioc_del_rt_rule *)param)->num_hdls
  1793. != pre_entry)) {
  1794. IPAERR_RL("current %d pre %d\n",
  1795. ((struct ipa_ioc_del_rt_rule *)param)->num_hdls,
  1796. pre_entry);
  1797. retval = -EFAULT;
  1798. break;
  1799. }
  1800. if (ipa3_del_rt_rule((struct ipa_ioc_del_rt_rule *)param)) {
  1801. retval = -EFAULT;
  1802. break;
  1803. }
  1804. if (copy_to_user((void __user *)arg, param, pyld_sz)) {
  1805. retval = -EFAULT;
  1806. break;
  1807. }
  1808. break;
  1809. case IPA_IOC_ADD_FLT_RULE:
  1810. if (copy_from_user(header, (const void __user *)arg,
  1811. sizeof(struct ipa_ioc_add_flt_rule))) {
  1812. retval = -EFAULT;
  1813. break;
  1814. }
  1815. pre_entry =
  1816. ((struct ipa_ioc_add_flt_rule *)header)->num_rules;
  1817. pyld_sz =
  1818. sizeof(struct ipa_ioc_add_flt_rule) +
  1819. pre_entry * sizeof(struct ipa_flt_rule_add);
  1820. param = memdup_user((const void __user *)arg, pyld_sz);
  1821. if (IS_ERR(param)) {
  1822. retval = PTR_ERR(param);
  1823. break;
  1824. }
  1825. /* add check in case user-space module compromised */
  1826. if (unlikely(((struct ipa_ioc_add_flt_rule *)param)->num_rules
  1827. != pre_entry)) {
  1828. IPAERR_RL("current %d pre %d\n",
  1829. ((struct ipa_ioc_add_flt_rule *)param)->
  1830. num_rules,
  1831. pre_entry);
  1832. retval = -EFAULT;
  1833. break;
  1834. }
  1835. if (ipa3_add_flt_rule_usr((struct ipa_ioc_add_flt_rule *)param,
  1836. true)) {
  1837. retval = -EFAULT;
  1838. break;
  1839. }
  1840. if (copy_to_user((void __user *)arg, param, pyld_sz)) {
  1841. retval = -EFAULT;
  1842. break;
  1843. }
  1844. break;
  1845. case IPA_IOC_ADD_FLT_RULE_AFTER:
  1846. if (copy_from_user(header, (const void __user *)arg,
  1847. sizeof(struct ipa_ioc_add_flt_rule_after))) {
  1848. retval = -EFAULT;
  1849. break;
  1850. }
  1851. pre_entry =
  1852. ((struct ipa_ioc_add_flt_rule_after *)header)->
  1853. num_rules;
  1854. pyld_sz =
  1855. sizeof(struct ipa_ioc_add_flt_rule_after) +
  1856. pre_entry * sizeof(struct ipa_flt_rule_add);
  1857. param = memdup_user((const void __user *)arg, pyld_sz);
  1858. if (IS_ERR(param)) {
  1859. retval = PTR_ERR(param);
  1860. break;
  1861. }
  1862. /* add check in case user-space module compromised */
  1863. if (unlikely(((struct ipa_ioc_add_flt_rule_after *)param)->
  1864. num_rules != pre_entry)) {
  1865. IPAERR_RL("current %d pre %d\n",
  1866. ((struct ipa_ioc_add_flt_rule_after *)param)->
  1867. num_rules,
  1868. pre_entry);
  1869. retval = -EFAULT;
  1870. break;
  1871. }
  1872. if (ipa3_add_flt_rule_after(
  1873. (struct ipa_ioc_add_flt_rule_after *)param)) {
  1874. retval = -EFAULT;
  1875. break;
  1876. }
  1877. if (copy_to_user((void __user *)arg, param, pyld_sz)) {
  1878. retval = -EFAULT;
  1879. break;
  1880. }
  1881. break;
  1882. case IPA_IOC_DEL_FLT_RULE:
  1883. if (copy_from_user(header, (const void __user *)arg,
  1884. sizeof(struct ipa_ioc_del_flt_rule))) {
  1885. retval = -EFAULT;
  1886. break;
  1887. }
  1888. pre_entry =
  1889. ((struct ipa_ioc_del_flt_rule *)header)->num_hdls;
  1890. pyld_sz =
  1891. sizeof(struct ipa_ioc_del_flt_rule) +
  1892. pre_entry * sizeof(struct ipa_flt_rule_del);
  1893. param = memdup_user((const void __user *)arg, pyld_sz);
  1894. if (IS_ERR(param)) {
  1895. retval = PTR_ERR(param);
  1896. break;
  1897. }
  1898. /* add check in case user-space module compromised */
  1899. if (unlikely(((struct ipa_ioc_del_flt_rule *)param)->num_hdls
  1900. != pre_entry)) {
  1901. IPAERR_RL("current %d pre %d\n",
  1902. ((struct ipa_ioc_del_flt_rule *)param)->
  1903. num_hdls,
  1904. pre_entry);
  1905. retval = -EFAULT;
  1906. break;
  1907. }
  1908. if (ipa3_del_flt_rule((struct ipa_ioc_del_flt_rule *)param)) {
  1909. retval = -EFAULT;
  1910. break;
  1911. }
  1912. if (copy_to_user((void __user *)arg, param, pyld_sz)) {
  1913. retval = -EFAULT;
  1914. break;
  1915. }
  1916. break;
  1917. case IPA_IOC_MDFY_FLT_RULE:
  1918. if (copy_from_user(header, (const void __user *)arg,
  1919. sizeof(struct ipa_ioc_mdfy_flt_rule))) {
  1920. retval = -EFAULT;
  1921. break;
  1922. }
  1923. pre_entry =
  1924. ((struct ipa_ioc_mdfy_flt_rule *)header)->num_rules;
  1925. pyld_sz =
  1926. sizeof(struct ipa_ioc_mdfy_flt_rule) +
  1927. pre_entry * sizeof(struct ipa_flt_rule_mdfy);
  1928. param = memdup_user((const void __user *)arg, pyld_sz);
  1929. if (IS_ERR(param)) {
  1930. retval = PTR_ERR(param);
  1931. break;
  1932. }
  1933. /* add check in case user-space module compromised */
  1934. if (unlikely(((struct ipa_ioc_mdfy_flt_rule *)param)->num_rules
  1935. != pre_entry)) {
  1936. IPAERR_RL("current %d pre %d\n",
  1937. ((struct ipa_ioc_mdfy_flt_rule *)param)->
  1938. num_rules,
  1939. pre_entry);
  1940. retval = -EFAULT;
  1941. break;
  1942. }
  1943. if (ipa3_mdfy_flt_rule((struct ipa_ioc_mdfy_flt_rule *)param)) {
  1944. retval = -EFAULT;
  1945. break;
  1946. }
  1947. if (copy_to_user((void __user *)arg, param, pyld_sz)) {
  1948. retval = -EFAULT;
  1949. break;
  1950. }
  1951. break;
  1952. case IPA_IOC_COMMIT_HDR:
  1953. retval = ipa3_commit_hdr();
  1954. break;
  1955. case IPA_IOC_RESET_HDR:
  1956. retval = ipa3_reset_hdr(false);
  1957. break;
  1958. case IPA_IOC_COMMIT_RT:
  1959. retval = ipa3_commit_rt(arg);
  1960. break;
  1961. case IPA_IOC_RESET_RT:
  1962. retval = ipa3_reset_rt(arg, false);
  1963. break;
  1964. case IPA_IOC_COMMIT_FLT:
  1965. retval = ipa3_commit_flt(arg);
  1966. break;
  1967. case IPA_IOC_RESET_FLT:
  1968. retval = ipa3_reset_flt(arg, false);
  1969. break;
  1970. case IPA_IOC_GET_RT_TBL:
  1971. if (copy_from_user(header, (const void __user *)arg,
  1972. sizeof(struct ipa_ioc_get_rt_tbl))) {
  1973. retval = -EFAULT;
  1974. break;
  1975. }
  1976. if (ipa3_get_rt_tbl((struct ipa_ioc_get_rt_tbl *)header)) {
  1977. retval = -EFAULT;
  1978. break;
  1979. }
  1980. if (copy_to_user((void __user *)arg, header,
  1981. sizeof(struct ipa_ioc_get_rt_tbl))) {
  1982. retval = -EFAULT;
  1983. break;
  1984. }
  1985. break;
  1986. case IPA_IOC_PUT_RT_TBL:
  1987. retval = ipa3_put_rt_tbl(arg);
  1988. break;
  1989. case IPA_IOC_GET_HDR:
  1990. if (copy_from_user(header, (const void __user *)arg,
  1991. sizeof(struct ipa_ioc_get_hdr))) {
  1992. retval = -EFAULT;
  1993. break;
  1994. }
  1995. if (ipa3_get_hdr((struct ipa_ioc_get_hdr *)header)) {
  1996. retval = -EFAULT;
  1997. break;
  1998. }
  1999. if (copy_to_user((void __user *)arg, header,
  2000. sizeof(struct ipa_ioc_get_hdr))) {
  2001. retval = -EFAULT;
  2002. break;
  2003. }
  2004. break;
  2005. case IPA_IOC_PUT_HDR:
  2006. retval = ipa3_put_hdr(arg);
  2007. break;
  2008. case IPA_IOC_SET_FLT:
  2009. retval = ipa3_cfg_filter(arg);
  2010. break;
  2011. case IPA_IOC_COPY_HDR:
  2012. if (copy_from_user(header, (const void __user *)arg,
  2013. sizeof(struct ipa_ioc_copy_hdr))) {
  2014. retval = -EFAULT;
  2015. break;
  2016. }
  2017. if (ipa3_copy_hdr((struct ipa_ioc_copy_hdr *)header)) {
  2018. retval = -EFAULT;
  2019. break;
  2020. }
  2021. if (copy_to_user((void __user *)arg, header,
  2022. sizeof(struct ipa_ioc_copy_hdr))) {
  2023. retval = -EFAULT;
  2024. break;
  2025. }
  2026. break;
  2027. case IPA_IOC_QUERY_INTF:
  2028. if (copy_from_user(header, (const void __user *)arg,
  2029. sizeof(struct ipa_ioc_query_intf))) {
  2030. retval = -EFAULT;
  2031. break;
  2032. }
  2033. if (ipa3_query_intf((struct ipa_ioc_query_intf *)header)) {
  2034. retval = -1;
  2035. break;
  2036. }
  2037. if (copy_to_user((void __user *)arg, header,
  2038. sizeof(struct ipa_ioc_query_intf))) {
  2039. retval = -EFAULT;
  2040. break;
  2041. }
  2042. break;
  2043. case IPA_IOC_QUERY_INTF_TX_PROPS:
  2044. sz = sizeof(struct ipa_ioc_query_intf_tx_props);
  2045. if (copy_from_user(header, (const void __user *)arg, sz)) {
  2046. retval = -EFAULT;
  2047. break;
  2048. }
  2049. if (((struct ipa_ioc_query_intf_tx_props *)header)->num_tx_props
  2050. > IPA_NUM_PROPS_MAX) {
  2051. retval = -EFAULT;
  2052. break;
  2053. }
  2054. pre_entry =
  2055. ((struct ipa_ioc_query_intf_tx_props *)
  2056. header)->num_tx_props;
  2057. pyld_sz = sz + pre_entry *
  2058. sizeof(struct ipa_ioc_tx_intf_prop);
  2059. param = memdup_user((const void __user *)arg, pyld_sz);
  2060. if (IS_ERR(param)) {
  2061. retval = PTR_ERR(param);
  2062. break;
  2063. }
  2064. /* add check in case user-space module compromised */
  2065. if (unlikely(((struct ipa_ioc_query_intf_tx_props *)
  2066. param)->num_tx_props
  2067. != pre_entry)) {
  2068. IPAERR_RL("current %d pre %d\n",
  2069. ((struct ipa_ioc_query_intf_tx_props *)
  2070. param)->num_tx_props, pre_entry);
  2071. retval = -EFAULT;
  2072. break;
  2073. }
  2074. if (ipa3_query_intf_tx_props(
  2075. (struct ipa_ioc_query_intf_tx_props *)param)) {
  2076. retval = -1;
  2077. break;
  2078. }
  2079. if (copy_to_user((void __user *)arg, param, pyld_sz)) {
  2080. retval = -EFAULT;
  2081. break;
  2082. }
  2083. break;
  2084. case IPA_IOC_QUERY_INTF_RX_PROPS:
  2085. sz = sizeof(struct ipa_ioc_query_intf_rx_props);
  2086. if (copy_from_user(header, (const void __user *)arg, sz)) {
  2087. retval = -EFAULT;
  2088. break;
  2089. }
  2090. if (((struct ipa_ioc_query_intf_rx_props *)header)->num_rx_props
  2091. > IPA_NUM_PROPS_MAX) {
  2092. retval = -EFAULT;
  2093. break;
  2094. }
  2095. pre_entry =
  2096. ((struct ipa_ioc_query_intf_rx_props *)
  2097. header)->num_rx_props;
  2098. pyld_sz = sz + pre_entry *
  2099. sizeof(struct ipa_ioc_rx_intf_prop);
  2100. param = memdup_user((const void __user *)arg, pyld_sz);
  2101. if (IS_ERR(param)) {
  2102. retval = PTR_ERR(param);
  2103. break;
  2104. }
  2105. /* add check in case user-space module compromised */
  2106. if (unlikely(((struct ipa_ioc_query_intf_rx_props *)
  2107. param)->num_rx_props != pre_entry)) {
  2108. IPAERR_RL("current %d pre %d\n",
  2109. ((struct ipa_ioc_query_intf_rx_props *)
  2110. param)->num_rx_props, pre_entry);
  2111. retval = -EFAULT;
  2112. break;
  2113. }
  2114. if (ipa3_query_intf_rx_props(
  2115. (struct ipa_ioc_query_intf_rx_props *)param)) {
  2116. retval = -1;
  2117. break;
  2118. }
  2119. if (copy_to_user((void __user *)arg, param, pyld_sz)) {
  2120. retval = -EFAULT;
  2121. break;
  2122. }
  2123. break;
  2124. case IPA_IOC_QUERY_INTF_EXT_PROPS:
  2125. sz = sizeof(struct ipa_ioc_query_intf_ext_props);
  2126. if (copy_from_user(header, (const void __user *)arg, sz)) {
  2127. retval = -EFAULT;
  2128. break;
  2129. }
  2130. if (((struct ipa_ioc_query_intf_ext_props *)
  2131. header)->num_ext_props > IPA_NUM_PROPS_MAX) {
  2132. retval = -EFAULT;
  2133. break;
  2134. }
  2135. pre_entry =
  2136. ((struct ipa_ioc_query_intf_ext_props *)
  2137. header)->num_ext_props;
  2138. pyld_sz = sz + pre_entry *
  2139. sizeof(struct ipa_ioc_ext_intf_prop);
  2140. param = memdup_user((const void __user *)arg, pyld_sz);
  2141. if (IS_ERR(param)) {
  2142. retval = PTR_ERR(param);
  2143. break;
  2144. }
  2145. /* add check in case user-space module compromised */
  2146. if (unlikely(((struct ipa_ioc_query_intf_ext_props *)
  2147. param)->num_ext_props != pre_entry)) {
  2148. IPAERR_RL("current %d pre %d\n",
  2149. ((struct ipa_ioc_query_intf_ext_props *)
  2150. param)->num_ext_props, pre_entry);
  2151. retval = -EFAULT;
  2152. break;
  2153. }
  2154. if (ipa3_query_intf_ext_props(
  2155. (struct ipa_ioc_query_intf_ext_props *)param)) {
  2156. retval = -1;
  2157. break;
  2158. }
  2159. if (copy_to_user((void __user *)arg, param, pyld_sz)) {
  2160. retval = -EFAULT;
  2161. break;
  2162. }
  2163. break;
  2164. case IPA_IOC_PULL_MSG:
  2165. if (copy_from_user(header, (const void __user *)arg,
  2166. sizeof(struct ipa_msg_meta))) {
  2167. retval = -EFAULT;
  2168. break;
  2169. }
  2170. pre_entry =
  2171. ((struct ipa_msg_meta *)header)->msg_len;
  2172. pyld_sz = sizeof(struct ipa_msg_meta) +
  2173. pre_entry;
  2174. param = memdup_user((const void __user *)arg, pyld_sz);
  2175. if (IS_ERR(param)) {
  2176. retval = PTR_ERR(param);
  2177. break;
  2178. }
  2179. /* add check in case user-space module compromised */
  2180. if (unlikely(((struct ipa_msg_meta *)param)->msg_len
  2181. != pre_entry)) {
  2182. IPAERR_RL("current %d pre %d\n",
  2183. ((struct ipa_msg_meta *)param)->msg_len,
  2184. pre_entry);
  2185. retval = -EFAULT;
  2186. break;
  2187. }
  2188. if (ipa3_pull_msg((struct ipa_msg_meta *)param,
  2189. (char *)param + sizeof(struct ipa_msg_meta),
  2190. ((struct ipa_msg_meta *)param)->msg_len) !=
  2191. ((struct ipa_msg_meta *)param)->msg_len) {
  2192. retval = -1;
  2193. break;
  2194. }
  2195. if (copy_to_user((void __user *)arg, param, pyld_sz)) {
  2196. retval = -EFAULT;
  2197. break;
  2198. }
  2199. break;
  2200. case IPA_IOC_RM_ADD_DEPENDENCY:
  2201. /* IPA RM is deprecate because IPA PM is used */
  2202. IPAERR("using obselete command: IPA_IOC_RM_ADD_DEPENDENCY");
  2203. return -EINVAL;
  2204. case IPA_IOC_RM_DEL_DEPENDENCY:
  2205. /* IPA RM is deprecate because IPA PM is used */
  2206. IPAERR("using obselete command: IPA_IOC_RM_DEL_DEPENDENCY");
  2207. return -EINVAL;
  2208. case IPA_IOC_GENERATE_FLT_EQ:
  2209. {
  2210. struct ipa_ioc_generate_flt_eq flt_eq;
  2211. if (copy_from_user(&flt_eq, (const void __user *)arg,
  2212. sizeof(struct ipa_ioc_generate_flt_eq))) {
  2213. retval = -EFAULT;
  2214. break;
  2215. }
  2216. if (ipahal_flt_generate_equation(flt_eq.ip,
  2217. &flt_eq.attrib, &flt_eq.eq_attrib)) {
  2218. retval = -EFAULT;
  2219. break;
  2220. }
  2221. if (copy_to_user((void __user *)arg, &flt_eq,
  2222. sizeof(struct ipa_ioc_generate_flt_eq))) {
  2223. retval = -EFAULT;
  2224. break;
  2225. }
  2226. break;
  2227. }
  2228. case IPA_IOC_QUERY_EP_MAPPING:
  2229. {
  2230. retval = ipa3_get_ep_mapping(arg);
  2231. break;
  2232. }
  2233. case IPA_IOC_QUERY_RT_TBL_INDEX:
  2234. if (copy_from_user(header, (const void __user *)arg,
  2235. sizeof(struct ipa_ioc_get_rt_tbl_indx))) {
  2236. retval = -EFAULT;
  2237. break;
  2238. }
  2239. if (ipa3_query_rt_index(
  2240. (struct ipa_ioc_get_rt_tbl_indx *)header)) {
  2241. retval = -EFAULT;
  2242. break;
  2243. }
  2244. if (copy_to_user((void __user *)arg, header,
  2245. sizeof(struct ipa_ioc_get_rt_tbl_indx))) {
  2246. retval = -EFAULT;
  2247. break;
  2248. }
  2249. break;
  2250. case IPA_IOC_WRITE_QMAPID:
  2251. if (copy_from_user(header, (const void __user *)arg,
  2252. sizeof(struct ipa_ioc_write_qmapid))) {
  2253. retval = -EFAULT;
  2254. break;
  2255. }
  2256. if (ipa3_write_qmap_id((struct ipa_ioc_write_qmapid *)header)) {
  2257. retval = -EFAULT;
  2258. break;
  2259. }
  2260. if (copy_to_user((void __user *)arg, header,
  2261. sizeof(struct ipa_ioc_write_qmapid))) {
  2262. retval = -EFAULT;
  2263. break;
  2264. }
  2265. break;
  2266. case IPA_IOC_NOTIFY_WAN_UPSTREAM_ROUTE_ADD:
  2267. retval = ipa3_send_wan_msg(arg, WAN_UPSTREAM_ROUTE_ADD, true);
  2268. if (retval) {
  2269. IPAERR("ipa3_send_wan_msg failed: %d\n", retval);
  2270. break;
  2271. }
  2272. break;
  2273. case IPA_IOC_NOTIFY_WAN_UPSTREAM_ROUTE_DEL:
  2274. retval = ipa3_send_wan_msg(arg, WAN_UPSTREAM_ROUTE_DEL, true);
  2275. if (retval) {
  2276. IPAERR("ipa3_send_wan_msg failed: %d\n", retval);
  2277. break;
  2278. }
  2279. break;
  2280. case IPA_IOC_NOTIFY_WAN_EMBMS_CONNECTED:
  2281. retval = ipa3_send_wan_msg(arg, WAN_EMBMS_CONNECT, false);
  2282. if (retval) {
  2283. IPAERR("ipa3_send_wan_msg failed: %d\n", retval);
  2284. break;
  2285. }
  2286. break;
  2287. case IPA_IOC_ADD_HDR_PROC_CTX:
  2288. if (copy_from_user(header, (const void __user *)arg,
  2289. sizeof(struct ipa_ioc_add_hdr_proc_ctx))) {
  2290. retval = -EFAULT;
  2291. break;
  2292. }
  2293. pre_entry =
  2294. ((struct ipa_ioc_add_hdr_proc_ctx *)
  2295. header)->num_proc_ctxs;
  2296. pyld_sz =
  2297. sizeof(struct ipa_ioc_add_hdr_proc_ctx) +
  2298. pre_entry * sizeof(struct ipa_hdr_proc_ctx_add);
  2299. param = memdup_user((const void __user *)arg, pyld_sz);
  2300. if (IS_ERR(param)) {
  2301. retval = PTR_ERR(param);
  2302. break;
  2303. }
  2304. /* add check in case user-space module compromised */
  2305. if (unlikely(((struct ipa_ioc_add_hdr_proc_ctx *)
  2306. param)->num_proc_ctxs != pre_entry)) {
  2307. IPAERR_RL("current %d pre %d\n",
  2308. ((struct ipa_ioc_add_hdr_proc_ctx *)
  2309. param)->num_proc_ctxs, pre_entry);
  2310. retval = -EFAULT;
  2311. break;
  2312. }
  2313. if (ipa3_add_hdr_proc_ctx(
  2314. (struct ipa_ioc_add_hdr_proc_ctx *)param, true)) {
  2315. retval = -EFAULT;
  2316. break;
  2317. }
  2318. if (copy_to_user((void __user *)arg, param, pyld_sz)) {
  2319. retval = -EFAULT;
  2320. break;
  2321. }
  2322. break;
  2323. case IPA_IOC_DEL_HDR_PROC_CTX:
  2324. if (copy_from_user(header, (const void __user *)arg,
  2325. sizeof(struct ipa_ioc_del_hdr_proc_ctx))) {
  2326. retval = -EFAULT;
  2327. break;
  2328. }
  2329. pre_entry =
  2330. ((struct ipa_ioc_del_hdr_proc_ctx *)header)->num_hdls;
  2331. pyld_sz =
  2332. sizeof(struct ipa_ioc_del_hdr_proc_ctx) +
  2333. pre_entry * sizeof(struct ipa_hdr_proc_ctx_del);
  2334. param = memdup_user((const void __user *)arg, pyld_sz);
  2335. if (IS_ERR(param)) {
  2336. retval = PTR_ERR(param);
  2337. break;
  2338. }
  2339. /* add check in case user-space module compromised */
  2340. if (unlikely(((struct ipa_ioc_del_hdr_proc_ctx *)
  2341. param)->num_hdls != pre_entry)) {
  2342. IPAERR_RL("current %d pre %d\n",
  2343. ((struct ipa_ioc_del_hdr_proc_ctx *)param)->
  2344. num_hdls,
  2345. pre_entry);
  2346. retval = -EFAULT;
  2347. break;
  2348. }
  2349. if (ipa3_del_hdr_proc_ctx_by_user(
  2350. (struct ipa_ioc_del_hdr_proc_ctx *)param, true)) {
  2351. retval = -EFAULT;
  2352. break;
  2353. }
  2354. if (copy_to_user((void __user *)arg, param, pyld_sz)) {
  2355. retval = -EFAULT;
  2356. break;
  2357. }
  2358. break;
  2359. case IPA_IOC_GET_HW_VERSION:
  2360. pyld_sz = sizeof(enum ipa_hw_type);
  2361. param = kmemdup(&ipa3_ctx->ipa_hw_type, pyld_sz, GFP_KERNEL);
  2362. if (!param) {
  2363. retval = -ENOMEM;
  2364. break;
  2365. }
  2366. if (copy_to_user((void __user *)arg, param, pyld_sz)) {
  2367. retval = -EFAULT;
  2368. break;
  2369. }
  2370. break;
  2371. case IPA_IOC_GET_VLAN_MODE:
  2372. if (copy_from_user(&vlan_mode, (const void __user *)arg,
  2373. sizeof(struct ipa_ioc_get_vlan_mode))) {
  2374. retval = -EFAULT;
  2375. break;
  2376. }
  2377. retval = ipa3_is_vlan_mode(
  2378. vlan_mode.iface,
  2379. &is_vlan_mode);
  2380. if (retval)
  2381. break;
  2382. vlan_mode.is_vlan_mode = is_vlan_mode;
  2383. if (copy_to_user((void __user *)arg,
  2384. &vlan_mode,
  2385. sizeof(struct ipa_ioc_get_vlan_mode))) {
  2386. retval = -EFAULT;
  2387. break;
  2388. }
  2389. break;
  2390. case IPA_IOC_ADD_VLAN_IFACE:
  2391. if (ipa3_send_vlan_l2tp_msg(arg, ADD_VLAN_IFACE)) {
  2392. retval = -EFAULT;
  2393. break;
  2394. }
  2395. break;
  2396. case IPA_IOC_DEL_VLAN_IFACE:
  2397. if (ipa3_send_vlan_l2tp_msg(arg, DEL_VLAN_IFACE)) {
  2398. retval = -EFAULT;
  2399. break;
  2400. }
  2401. break;
  2402. case IPA_IOC_ADD_BRIDGE_VLAN_MAPPING:
  2403. if (ipa3_send_vlan_l2tp_msg(arg, ADD_BRIDGE_VLAN_MAPPING)) {
  2404. retval = -EFAULT;
  2405. break;
  2406. }
  2407. break;
  2408. case IPA_IOC_DEL_BRIDGE_VLAN_MAPPING:
  2409. if (ipa3_send_vlan_l2tp_msg(arg, DEL_BRIDGE_VLAN_MAPPING)) {
  2410. retval = -EFAULT;
  2411. break;
  2412. }
  2413. break;
  2414. case IPA_IOC_ADD_L2TP_VLAN_MAPPING:
  2415. if (ipa3_send_vlan_l2tp_msg(arg, ADD_L2TP_VLAN_MAPPING)) {
  2416. retval = -EFAULT;
  2417. break;
  2418. }
  2419. break;
  2420. case IPA_IOC_DEL_L2TP_VLAN_MAPPING:
  2421. if (ipa3_send_vlan_l2tp_msg(arg, DEL_L2TP_VLAN_MAPPING)) {
  2422. retval = -EFAULT;
  2423. break;
  2424. }
  2425. break;
  2426. case IPA_IOC_CLEANUP:
  2427. /*Route and filter rules will also be clean*/
  2428. IPADBG("Got IPA_IOC_CLEANUP\n");
  2429. retval = ipa3_reset_hdr(true);
  2430. memset(&nat_del, 0, sizeof(nat_del));
  2431. nat_del.table_index = 0;
  2432. retval = ipa3_nat_del_cmd(&nat_del);
  2433. if (ipa3_ctx->platform_type == IPA_PLAT_TYPE_APQ)
  2434. retval = ipa3_clean_mhip_dl_rule();
  2435. else
  2436. retval = ipa3_clean_modem_rule();
  2437. ipa3_counter_id_remove_all();
  2438. break;
  2439. case IPA_IOC_QUERY_WLAN_CLIENT:
  2440. IPADBG("Got IPA_IOC_QUERY_WLAN_CLIENT\n");
  2441. retval = ipa3_resend_wlan_msg();
  2442. break;
  2443. case IPA_IOC_GSB_CONNECT:
  2444. IPADBG("Got IPA_IOC_GSB_CONNECT\n");
  2445. if (ipa3_send_gsb_msg(arg, IPA_GSB_CONNECT)) {
  2446. retval = -EFAULT;
  2447. break;
  2448. }
  2449. break;
  2450. case IPA_IOC_GSB_DISCONNECT:
  2451. IPADBG("Got IPA_IOC_GSB_DISCONNECT\n");
  2452. if (ipa3_send_gsb_msg(arg, IPA_GSB_DISCONNECT)) {
  2453. retval = -EFAULT;
  2454. break;
  2455. }
  2456. break;
  2457. case IPA_IOC_ADD_RT_RULE_V2:
  2458. retval = ipa3_ioctl_add_rt_rule_v2(arg);
  2459. break;
  2460. case IPA_IOC_ADD_RT_RULE_EXT_V2:
  2461. retval = ipa3_ioctl_add_rt_rule_ext_v2(arg);
  2462. break;
  2463. case IPA_IOC_ADD_RT_RULE_AFTER_V2:
  2464. retval = ipa3_ioctl_add_rt_rule_after_v2(arg);
  2465. break;
  2466. case IPA_IOC_MDFY_RT_RULE_V2:
  2467. retval = ipa3_ioctl_mdfy_rt_rule_v2(arg);
  2468. break;
  2469. case IPA_IOC_ADD_FLT_RULE_V2:
  2470. retval = ipa3_ioctl_add_flt_rule_v2(arg);
  2471. break;
  2472. case IPA_IOC_ADD_FLT_RULE_AFTER_V2:
  2473. retval = ipa3_ioctl_add_flt_rule_after_v2(arg);
  2474. break;
  2475. case IPA_IOC_MDFY_FLT_RULE_V2:
  2476. retval = ipa3_ioctl_mdfy_flt_rule_v2(arg);
  2477. break;
  2478. case IPA_IOC_FNR_COUNTER_ALLOC:
  2479. if (ipa3_ctx->ipa_hw_type < IPA_HW_v4_5) {
  2480. IPAERR("FNR stats not supported on IPA ver %d",
  2481. ipa3_ctx->ipa_hw_type);
  2482. retval = -EFAULT;
  2483. break;
  2484. }
  2485. retval = ipa3_ioctl_fnr_counter_alloc(arg);
  2486. break;
  2487. case IPA_IOC_FNR_COUNTER_DEALLOC:
  2488. if (ipa3_ctx->ipa_hw_type < IPA_HW_v4_5) {
  2489. IPAERR("FNR stats not supported on IPA ver %d",
  2490. ipa3_ctx->ipa_hw_type);
  2491. retval = -EFAULT;
  2492. break;
  2493. }
  2494. hdl = (int)arg;
  2495. if (hdl < 0) {
  2496. IPAERR("IPA_FNR_COUNTER_DEALLOC failed: hdl %d\n",
  2497. hdl);
  2498. retval = -EPERM;
  2499. break;
  2500. }
  2501. ipa3_counter_remove_hdl(hdl);
  2502. break;
  2503. case IPA_IOC_FNR_COUNTER_QUERY:
  2504. if (ipa3_ctx->ipa_hw_type < IPA_HW_v4_5) {
  2505. IPAERR("FNR stats not supported on IPA ver %d",
  2506. ipa3_ctx->ipa_hw_type);
  2507. retval = -EFAULT;
  2508. break;
  2509. }
  2510. retval = ipa3_ioctl_fnr_counter_query(arg);
  2511. break;
  2512. case IPA_IOC_SET_FNR_COUNTER_INFO:
  2513. if (ipa3_ctx->ipa_hw_type < IPA_HW_v4_5) {
  2514. IPAERR("FNR stats not supported on IPA ver %d",
  2515. ipa3_ctx->ipa_hw_type);
  2516. retval = -EFAULT;
  2517. break;
  2518. }
  2519. retval = ipa3_ioctl_fnr_counter_set(arg);
  2520. break;
  2521. case IPA_IOC_WIGIG_FST_SWITCH:
  2522. IPADBG("Got IPA_IOCTL_WIGIG_FST_SWITCH\n");
  2523. if (copy_from_user(&fst_switch, (const void __user *)arg,
  2524. sizeof(struct ipa_ioc_wigig_fst_switch))) {
  2525. retval = -EFAULT;
  2526. break;
  2527. }
  2528. /* null terminate the string */
  2529. fst_switch.netdev_name[IPA_RESOURCE_NAME_MAX - 1] = '\0';
  2530. retval = ipa_wigig_send_msg(WIGIG_FST_SWITCH,
  2531. fst_switch.netdev_name,
  2532. fst_switch.client_mac_addr,
  2533. IPA_CLIENT_MAX,
  2534. fst_switch.to_wigig);
  2535. break;
  2536. case IPA_IOC_GET_NAT_IN_SRAM_INFO:
  2537. retval = proc_sram_info_rqst(arg);
  2538. break;
  2539. case IPA_IOC_APP_CLOCK_VOTE:
  2540. retval = ipa3_app_clk_vote(
  2541. (enum ipa_app_clock_vote_type) arg);
  2542. break;
  2543. default:
  2544. IPA_ACTIVE_CLIENTS_DEC_SIMPLE();
  2545. return -ENOTTY;
  2546. }
  2547. if (!IS_ERR(param))
  2548. kfree(param);
  2549. IPA_ACTIVE_CLIENTS_DEC_SIMPLE();
  2550. return retval;
  2551. }
  2552. /**
  2553. * ipa3_setup_dflt_rt_tables() - Setup default routing tables
  2554. *
  2555. * Return codes:
  2556. * 0: success
  2557. * -ENOMEM: failed to allocate memory
  2558. * -EPERM: failed to add the tables
  2559. */
  2560. int ipa3_setup_dflt_rt_tables(void)
  2561. {
  2562. struct ipa_ioc_add_rt_rule *rt_rule;
  2563. struct ipa_rt_rule_add *rt_rule_entry;
  2564. rt_rule =
  2565. kzalloc(sizeof(struct ipa_ioc_add_rt_rule) + 1 *
  2566. sizeof(struct ipa_rt_rule_add), GFP_KERNEL);
  2567. if (!rt_rule)
  2568. return -ENOMEM;
  2569. /* setup a default v4 route to point to Apps */
  2570. rt_rule->num_rules = 1;
  2571. rt_rule->commit = 1;
  2572. rt_rule->ip = IPA_IP_v4;
  2573. strlcpy(rt_rule->rt_tbl_name, IPA_DFLT_RT_TBL_NAME,
  2574. IPA_RESOURCE_NAME_MAX);
  2575. rt_rule_entry = &rt_rule->rules[0];
  2576. rt_rule_entry->at_rear = 1;
  2577. rt_rule_entry->rule.dst = IPA_CLIENT_APPS_LAN_CONS;
  2578. rt_rule_entry->rule.hdr_hdl = ipa3_ctx->excp_hdr_hdl;
  2579. rt_rule_entry->rule.retain_hdr = 1;
  2580. if (ipa3_add_rt_rule(rt_rule)) {
  2581. IPAERR("fail to add dflt v4 rule\n");
  2582. kfree(rt_rule);
  2583. return -EPERM;
  2584. }
  2585. IPADBG("dflt v4 rt rule hdl=%x\n", rt_rule_entry->rt_rule_hdl);
  2586. ipa3_ctx->dflt_v4_rt_rule_hdl = rt_rule_entry->rt_rule_hdl;
  2587. /* setup a default v6 route to point to A5 */
  2588. rt_rule->ip = IPA_IP_v6;
  2589. if (ipa3_add_rt_rule(rt_rule)) {
  2590. IPAERR("fail to add dflt v6 rule\n");
  2591. kfree(rt_rule);
  2592. return -EPERM;
  2593. }
  2594. IPADBG("dflt v6 rt rule hdl=%x\n", rt_rule_entry->rt_rule_hdl);
  2595. ipa3_ctx->dflt_v6_rt_rule_hdl = rt_rule_entry->rt_rule_hdl;
  2596. /*
  2597. * because these tables are the very first to be added, they will both
  2598. * have the same index (0) which is essential for programming the
  2599. * "route" end-point config
  2600. */
  2601. kfree(rt_rule);
  2602. return 0;
  2603. }
  2604. static int ipa3_setup_exception_path(void)
  2605. {
  2606. struct ipa_ioc_add_hdr *hdr;
  2607. struct ipa_hdr_add *hdr_entry;
  2608. struct ipahal_reg_route route = { 0 };
  2609. int ret;
  2610. /* install the basic exception header */
  2611. hdr = kzalloc(sizeof(struct ipa_ioc_add_hdr) + 1 *
  2612. sizeof(struct ipa_hdr_add), GFP_KERNEL);
  2613. if (!hdr)
  2614. return -ENOMEM;
  2615. hdr->num_hdrs = 1;
  2616. hdr->commit = 1;
  2617. hdr_entry = &hdr->hdr[0];
  2618. strlcpy(hdr_entry->name, IPA_LAN_RX_HDR_NAME, IPA_RESOURCE_NAME_MAX);
  2619. hdr_entry->hdr_len = IPA_LAN_RX_HEADER_LENGTH;
  2620. if (ipa3_add_hdr(hdr)) {
  2621. IPAERR("fail to add exception hdr\n");
  2622. ret = -EPERM;
  2623. goto bail;
  2624. }
  2625. if (hdr_entry->status) {
  2626. IPAERR("fail to add exception hdr\n");
  2627. ret = -EPERM;
  2628. goto bail;
  2629. }
  2630. ipa3_ctx->excp_hdr_hdl = hdr_entry->hdr_hdl;
  2631. /* set the route register to pass exception packets to Apps */
  2632. route.route_def_pipe = ipa3_get_ep_mapping(IPA_CLIENT_APPS_LAN_CONS);
  2633. route.route_frag_def_pipe = ipa3_get_ep_mapping(
  2634. IPA_CLIENT_APPS_LAN_CONS);
  2635. route.route_def_hdr_table = !ipa3_ctx->hdr_tbl_lcl;
  2636. route.route_def_retain_hdr = 1;
  2637. if (ipa3_cfg_route(&route)) {
  2638. IPAERR("fail to add exception hdr\n");
  2639. ret = -EPERM;
  2640. goto bail;
  2641. }
  2642. ret = 0;
  2643. bail:
  2644. kfree(hdr);
  2645. return ret;
  2646. }
  2647. static int ipa3_init_smem_region(int memory_region_size,
  2648. int memory_region_offset)
  2649. {
  2650. struct ipahal_imm_cmd_dma_shared_mem cmd;
  2651. struct ipahal_imm_cmd_pyld *cmd_pyld;
  2652. struct ipa3_desc desc;
  2653. struct ipa_mem_buffer mem;
  2654. int rc;
  2655. if (memory_region_size == 0)
  2656. return 0;
  2657. memset(&desc, 0, sizeof(desc));
  2658. memset(&cmd, 0, sizeof(cmd));
  2659. memset(&mem, 0, sizeof(mem));
  2660. mem.size = memory_region_size;
  2661. mem.base = dma_alloc_coherent(ipa3_ctx->pdev, mem.size,
  2662. &mem.phys_base, GFP_KERNEL);
  2663. if (!mem.base) {
  2664. IPAERR("failed to alloc DMA buff of size %d\n", mem.size);
  2665. return -ENOMEM;
  2666. }
  2667. cmd.is_read = false;
  2668. cmd.skip_pipeline_clear = false;
  2669. cmd.pipeline_clear_options = IPAHAL_HPS_CLEAR;
  2670. cmd.size = mem.size;
  2671. cmd.system_addr = mem.phys_base;
  2672. cmd.local_addr = ipa3_ctx->smem_restricted_bytes +
  2673. memory_region_offset;
  2674. cmd_pyld = ipahal_construct_imm_cmd(
  2675. IPA_IMM_CMD_DMA_SHARED_MEM, &cmd, false);
  2676. if (!cmd_pyld) {
  2677. IPAERR("failed to construct dma_shared_mem imm cmd\n");
  2678. return -ENOMEM;
  2679. }
  2680. ipa3_init_imm_cmd_desc(&desc, cmd_pyld);
  2681. rc = ipa3_send_cmd(1, &desc);
  2682. if (rc) {
  2683. IPAERR("failed to send immediate command (error %d)\n", rc);
  2684. rc = -EFAULT;
  2685. }
  2686. ipahal_destroy_imm_cmd(cmd_pyld);
  2687. dma_free_coherent(ipa3_ctx->pdev, mem.size, mem.base,
  2688. mem.phys_base);
  2689. return rc;
  2690. }
  2691. /**
  2692. * ipa3_init_q6_smem() - Initialize Q6 general memory and
  2693. * header memory regions in IPA.
  2694. *
  2695. * Return codes:
  2696. * 0: success
  2697. * -ENOMEM: failed to allocate dma memory
  2698. * -EFAULT: failed to send IPA command to initialize the memory
  2699. */
  2700. int ipa3_init_q6_smem(void)
  2701. {
  2702. int rc;
  2703. IPA_ACTIVE_CLIENTS_INC_SIMPLE();
  2704. rc = ipa3_init_smem_region(IPA_MEM_PART(modem_size),
  2705. IPA_MEM_PART(modem_ofst));
  2706. if (rc) {
  2707. IPAERR("failed to initialize Modem RAM memory\n");
  2708. IPA_ACTIVE_CLIENTS_DEC_SIMPLE();
  2709. return rc;
  2710. }
  2711. rc = ipa3_init_smem_region(IPA_MEM_PART(modem_hdr_size),
  2712. IPA_MEM_PART(modem_hdr_ofst));
  2713. if (rc) {
  2714. IPAERR("failed to initialize Modem HDRs RAM memory\n");
  2715. IPA_ACTIVE_CLIENTS_DEC_SIMPLE();
  2716. return rc;
  2717. }
  2718. rc = ipa3_init_smem_region(IPA_MEM_PART(modem_hdr_proc_ctx_size),
  2719. IPA_MEM_PART(modem_hdr_proc_ctx_ofst));
  2720. if (rc) {
  2721. IPAERR("failed to initialize Modem proc ctx RAM memory\n");
  2722. IPA_ACTIVE_CLIENTS_DEC_SIMPLE();
  2723. return rc;
  2724. }
  2725. rc = ipa3_init_smem_region(IPA_MEM_PART(modem_comp_decomp_size),
  2726. IPA_MEM_PART(modem_comp_decomp_ofst));
  2727. if (rc) {
  2728. IPAERR("failed to initialize Modem Comp/Decomp RAM memory\n");
  2729. IPA_ACTIVE_CLIENTS_DEC_SIMPLE();
  2730. return rc;
  2731. }
  2732. IPA_ACTIVE_CLIENTS_DEC_SIMPLE();
  2733. return rc;
  2734. }
  2735. static void ipa3_destroy_imm(void *user1, int user2)
  2736. {
  2737. ipahal_destroy_imm_cmd(user1);
  2738. }
  2739. static void ipa3_q6_pipe_delay(bool delay)
  2740. {
  2741. int client_idx;
  2742. int ep_idx;
  2743. struct ipa_ep_cfg_ctrl ep_ctrl;
  2744. memset(&ep_ctrl, 0, sizeof(struct ipa_ep_cfg_ctrl));
  2745. ep_ctrl.ipa_ep_delay = delay;
  2746. for (client_idx = 0; client_idx < IPA_CLIENT_MAX; client_idx++) {
  2747. if (IPA_CLIENT_IS_Q6_PROD(client_idx)) {
  2748. ep_idx = ipa3_get_ep_mapping(client_idx);
  2749. if (ep_idx == -1)
  2750. continue;
  2751. ipahal_write_reg_n_fields(IPA_ENDP_INIT_CTRL_n,
  2752. ep_idx, &ep_ctrl);
  2753. }
  2754. }
  2755. }
  2756. static void ipa3_q6_avoid_holb(void)
  2757. {
  2758. int ep_idx;
  2759. int client_idx;
  2760. struct ipa_ep_cfg_ctrl ep_suspend;
  2761. struct ipa_ep_cfg_holb ep_holb;
  2762. memset(&ep_suspend, 0, sizeof(ep_suspend));
  2763. memset(&ep_holb, 0, sizeof(ep_holb));
  2764. ep_suspend.ipa_ep_suspend = true;
  2765. ep_holb.tmr_val = 0;
  2766. ep_holb.en = 1;
  2767. if (ipa3_ctx->ipa_hw_type == IPA_HW_v4_2)
  2768. ipa3_cal_ep_holb_scale_base_val(ep_holb.tmr_val, &ep_holb);
  2769. for (client_idx = 0; client_idx < IPA_CLIENT_MAX; client_idx++) {
  2770. if (IPA_CLIENT_IS_Q6_CONS(client_idx)) {
  2771. ep_idx = ipa3_get_ep_mapping(client_idx);
  2772. if (ep_idx == -1)
  2773. continue;
  2774. /* from IPA 4.0 pipe suspend is not supported */
  2775. if (ipa3_ctx->ipa_hw_type < IPA_HW_v4_0)
  2776. ipahal_write_reg_n_fields(
  2777. IPA_ENDP_INIT_CTRL_n,
  2778. ep_idx, &ep_suspend);
  2779. /*
  2780. * ipa3_cfg_ep_holb is not used here because we are
  2781. * setting HOLB on Q6 pipes, and from APPS perspective
  2782. * they are not valid, therefore, the above function
  2783. * will fail.
  2784. */
  2785. ipahal_write_reg_n_fields(
  2786. IPA_ENDP_INIT_HOL_BLOCK_TIMER_n,
  2787. ep_idx, &ep_holb);
  2788. ipahal_write_reg_n_fields(
  2789. IPA_ENDP_INIT_HOL_BLOCK_EN_n,
  2790. ep_idx, &ep_holb);
  2791. /* IPA4.5 issue requires HOLB_EN to be written twice */
  2792. if (ipa3_ctx->ipa_hw_type >= IPA_HW_v4_5)
  2793. ipahal_write_reg_n_fields(
  2794. IPA_ENDP_INIT_HOL_BLOCK_EN_n,
  2795. ep_idx, &ep_holb);
  2796. }
  2797. }
  2798. }
  2799. static void ipa3_halt_q6_gsi_channels(bool prod)
  2800. {
  2801. int ep_idx;
  2802. int client_idx;
  2803. const struct ipa_gsi_ep_config *gsi_ep_cfg;
  2804. int i;
  2805. int ret;
  2806. int code = 0;
  2807. /* if prod flag is true, then we halt the producer channels also */
  2808. for (client_idx = 0; client_idx < IPA_CLIENT_MAX; client_idx++) {
  2809. if (IPA_CLIENT_IS_Q6_CONS(client_idx)
  2810. || (IPA_CLIENT_IS_Q6_PROD(client_idx) && prod)) {
  2811. ep_idx = ipa3_get_ep_mapping(client_idx);
  2812. if (ep_idx == -1)
  2813. continue;
  2814. gsi_ep_cfg = ipa3_get_gsi_ep_info(client_idx);
  2815. if (!gsi_ep_cfg) {
  2816. IPAERR("failed to get GSI config\n");
  2817. ipa_assert();
  2818. return;
  2819. }
  2820. ret = gsi_halt_channel_ee(
  2821. gsi_ep_cfg->ipa_gsi_chan_num, gsi_ep_cfg->ee,
  2822. &code);
  2823. for (i = 0; i < IPA_GSI_CHANNEL_STOP_MAX_RETRY &&
  2824. ret == -GSI_STATUS_AGAIN; i++) {
  2825. IPADBG(
  2826. "ch %d ee %d with code %d\n is busy try again",
  2827. gsi_ep_cfg->ipa_gsi_chan_num,
  2828. gsi_ep_cfg->ee,
  2829. code);
  2830. usleep_range(IPA_GSI_CHANNEL_HALT_MIN_SLEEP,
  2831. IPA_GSI_CHANNEL_HALT_MAX_SLEEP);
  2832. ret = gsi_halt_channel_ee(
  2833. gsi_ep_cfg->ipa_gsi_chan_num,
  2834. gsi_ep_cfg->ee, &code);
  2835. }
  2836. if (ret == GSI_STATUS_SUCCESS)
  2837. IPADBG("halted gsi ch %d ee %d with code %d\n",
  2838. gsi_ep_cfg->ipa_gsi_chan_num,
  2839. gsi_ep_cfg->ee,
  2840. code);
  2841. else
  2842. IPAERR("failed to halt ch %d ee %d code %d\n",
  2843. gsi_ep_cfg->ipa_gsi_chan_num,
  2844. gsi_ep_cfg->ee,
  2845. code);
  2846. }
  2847. }
  2848. }
  2849. static int ipa3_q6_clean_q6_flt_tbls(enum ipa_ip_type ip,
  2850. enum ipa_rule_type rlt)
  2851. {
  2852. struct ipa3_desc *desc;
  2853. struct ipahal_imm_cmd_dma_shared_mem cmd = {0};
  2854. struct ipahal_imm_cmd_pyld **cmd_pyld;
  2855. int retval = 0;
  2856. int pipe_idx;
  2857. int flt_idx = 0;
  2858. int num_cmds = 0;
  2859. int index;
  2860. u32 lcl_addr_mem_part;
  2861. u32 lcl_hdr_sz;
  2862. struct ipa_mem_buffer mem;
  2863. struct ipahal_reg_valmask valmask;
  2864. struct ipahal_imm_cmd_register_write reg_write_coal_close;
  2865. int i;
  2866. IPADBG("Entry\n");
  2867. if ((ip >= IPA_IP_MAX) || (rlt >= IPA_RULE_TYPE_MAX)) {
  2868. IPAERR("Input Err: ip=%d ; rlt=%d\n", ip, rlt);
  2869. return -EINVAL;
  2870. }
  2871. /*
  2872. * SRAM memory not allocated to hash tables. Cleaning the of hash table
  2873. * operation not supported.
  2874. */
  2875. if (rlt == IPA_RULE_HASHABLE && ipa3_ctx->ipa_fltrt_not_hashable) {
  2876. IPADBG("Clean hashable rules not supported\n");
  2877. return retval;
  2878. }
  2879. /* Up to filtering pipes we have filtering tables + 1 for coal close */
  2880. desc = kcalloc(ipa3_ctx->ep_flt_num + 1, sizeof(struct ipa3_desc),
  2881. GFP_KERNEL);
  2882. if (!desc)
  2883. return -ENOMEM;
  2884. cmd_pyld = kcalloc(ipa3_ctx->ep_flt_num + 1,
  2885. sizeof(struct ipahal_imm_cmd_pyld *), GFP_KERNEL);
  2886. if (!cmd_pyld) {
  2887. retval = -ENOMEM;
  2888. goto free_desc;
  2889. }
  2890. if (ip == IPA_IP_v4) {
  2891. if (rlt == IPA_RULE_HASHABLE) {
  2892. lcl_addr_mem_part = IPA_MEM_PART(v4_flt_hash_ofst);
  2893. lcl_hdr_sz = IPA_MEM_PART(v4_flt_hash_size);
  2894. } else {
  2895. lcl_addr_mem_part = IPA_MEM_PART(v4_flt_nhash_ofst);
  2896. lcl_hdr_sz = IPA_MEM_PART(v4_flt_nhash_size);
  2897. }
  2898. } else {
  2899. if (rlt == IPA_RULE_HASHABLE) {
  2900. lcl_addr_mem_part = IPA_MEM_PART(v6_flt_hash_ofst);
  2901. lcl_hdr_sz = IPA_MEM_PART(v6_flt_hash_size);
  2902. } else {
  2903. lcl_addr_mem_part = IPA_MEM_PART(v6_flt_nhash_ofst);
  2904. lcl_hdr_sz = IPA_MEM_PART(v6_flt_nhash_size);
  2905. }
  2906. }
  2907. retval = ipahal_flt_generate_empty_img(1, lcl_hdr_sz, lcl_hdr_sz,
  2908. 0, &mem, true);
  2909. if (retval) {
  2910. IPAERR("failed to generate flt single tbl empty img\n");
  2911. goto free_cmd_pyld;
  2912. }
  2913. /* IC to close the coal frame before HPS Clear if coal is enabled */
  2914. if (ipa3_get_ep_mapping(IPA_CLIENT_APPS_WAN_COAL_CONS) != -1) {
  2915. i = ipa3_get_ep_mapping(IPA_CLIENT_APPS_WAN_COAL_CONS);
  2916. reg_write_coal_close.skip_pipeline_clear = false;
  2917. reg_write_coal_close.pipeline_clear_options = IPAHAL_HPS_CLEAR;
  2918. reg_write_coal_close.offset = ipahal_get_reg_ofst(
  2919. IPA_AGGR_FORCE_CLOSE);
  2920. ipahal_get_aggr_force_close_valmask(i, &valmask);
  2921. reg_write_coal_close.value = valmask.val;
  2922. reg_write_coal_close.value_mask = valmask.mask;
  2923. cmd_pyld[num_cmds] = ipahal_construct_imm_cmd(
  2924. IPA_IMM_CMD_REGISTER_WRITE,
  2925. &reg_write_coal_close, false);
  2926. if (!cmd_pyld[num_cmds]) {
  2927. IPAERR("failed to construct coal close IC\n");
  2928. retval = -ENOMEM;
  2929. goto free_empty_img;
  2930. }
  2931. ipa3_init_imm_cmd_desc(&desc[num_cmds], cmd_pyld[num_cmds]);
  2932. ++num_cmds;
  2933. }
  2934. for (pipe_idx = 0; pipe_idx < ipa3_ctx->ipa_num_pipes; pipe_idx++) {
  2935. if (!ipa_is_ep_support_flt(pipe_idx))
  2936. continue;
  2937. /*
  2938. * Iterating over all the filtering pipes which are either
  2939. * invalid but connected or connected but not configured by AP.
  2940. */
  2941. if (!ipa3_ctx->ep[pipe_idx].valid ||
  2942. ipa3_ctx->ep[pipe_idx].skip_ep_cfg) {
  2943. if (num_cmds >= ipa3_ctx->ep_flt_num) {
  2944. IPAERR("number of commands is out of range\n");
  2945. retval = -ENOBUFS;
  2946. goto free_empty_img;
  2947. }
  2948. cmd.is_read = false;
  2949. cmd.skip_pipeline_clear = false;
  2950. cmd.pipeline_clear_options = IPAHAL_HPS_CLEAR;
  2951. cmd.size = mem.size;
  2952. cmd.system_addr = mem.phys_base;
  2953. cmd.local_addr =
  2954. ipa3_ctx->smem_restricted_bytes +
  2955. lcl_addr_mem_part +
  2956. ipahal_get_hw_tbl_hdr_width() +
  2957. flt_idx * ipahal_get_hw_tbl_hdr_width();
  2958. cmd_pyld[num_cmds] = ipahal_construct_imm_cmd(
  2959. IPA_IMM_CMD_DMA_SHARED_MEM, &cmd, false);
  2960. if (!cmd_pyld[num_cmds]) {
  2961. IPAERR("fail construct dma_shared_mem cmd\n");
  2962. retval = -ENOMEM;
  2963. goto free_empty_img;
  2964. }
  2965. ipa3_init_imm_cmd_desc(&desc[num_cmds],
  2966. cmd_pyld[num_cmds]);
  2967. ++num_cmds;
  2968. }
  2969. ++flt_idx;
  2970. }
  2971. IPADBG("Sending %d descriptors for flt tbl clearing\n", num_cmds);
  2972. retval = ipa3_send_cmd(num_cmds, desc);
  2973. if (retval) {
  2974. IPAERR("failed to send immediate command (err %d)\n", retval);
  2975. retval = -EFAULT;
  2976. }
  2977. free_empty_img:
  2978. ipahal_free_dma_mem(&mem);
  2979. free_cmd_pyld:
  2980. for (index = 0; index < num_cmds; index++)
  2981. ipahal_destroy_imm_cmd(cmd_pyld[index]);
  2982. kfree(cmd_pyld);
  2983. free_desc:
  2984. kfree(desc);
  2985. return retval;
  2986. }
  2987. static int ipa3_q6_clean_q6_rt_tbls(enum ipa_ip_type ip,
  2988. enum ipa_rule_type rlt)
  2989. {
  2990. struct ipa3_desc *desc;
  2991. struct ipahal_imm_cmd_dma_shared_mem cmd = {0};
  2992. struct ipahal_imm_cmd_pyld **cmd_pyld;
  2993. int retval = 0;
  2994. int num_cmds = 0;
  2995. u32 modem_rt_index_lo;
  2996. u32 modem_rt_index_hi;
  2997. u32 lcl_addr_mem_part;
  2998. u32 lcl_hdr_sz;
  2999. struct ipa_mem_buffer mem;
  3000. struct ipahal_reg_valmask valmask;
  3001. struct ipahal_imm_cmd_register_write reg_write_coal_close;
  3002. int i;
  3003. IPADBG("Entry\n");
  3004. if ((ip >= IPA_IP_MAX) || (rlt >= IPA_RULE_TYPE_MAX)) {
  3005. IPAERR("Input Err: ip=%d ; rlt=%d\n", ip, rlt);
  3006. return -EINVAL;
  3007. }
  3008. /*
  3009. * SRAM memory not allocated to hash tables. Cleaning the of hash table
  3010. * operation not supported.
  3011. */
  3012. if (rlt == IPA_RULE_HASHABLE && ipa3_ctx->ipa_fltrt_not_hashable) {
  3013. IPADBG("Clean hashable rules not supported\n");
  3014. return retval;
  3015. }
  3016. if (ip == IPA_IP_v4) {
  3017. modem_rt_index_lo = IPA_MEM_PART(v4_modem_rt_index_lo);
  3018. modem_rt_index_hi = IPA_MEM_PART(v4_modem_rt_index_hi);
  3019. if (rlt == IPA_RULE_HASHABLE) {
  3020. lcl_addr_mem_part = IPA_MEM_PART(v4_rt_hash_ofst);
  3021. lcl_hdr_sz = IPA_MEM_PART(v4_flt_hash_size);
  3022. } else {
  3023. lcl_addr_mem_part = IPA_MEM_PART(v4_rt_nhash_ofst);
  3024. lcl_hdr_sz = IPA_MEM_PART(v4_flt_nhash_size);
  3025. }
  3026. } else {
  3027. modem_rt_index_lo = IPA_MEM_PART(v6_modem_rt_index_lo);
  3028. modem_rt_index_hi = IPA_MEM_PART(v6_modem_rt_index_hi);
  3029. if (rlt == IPA_RULE_HASHABLE) {
  3030. lcl_addr_mem_part = IPA_MEM_PART(v6_rt_hash_ofst);
  3031. lcl_hdr_sz = IPA_MEM_PART(v6_flt_hash_size);
  3032. } else {
  3033. lcl_addr_mem_part = IPA_MEM_PART(v6_rt_nhash_ofst);
  3034. lcl_hdr_sz = IPA_MEM_PART(v6_flt_nhash_size);
  3035. }
  3036. }
  3037. retval = ipahal_rt_generate_empty_img(
  3038. modem_rt_index_hi - modem_rt_index_lo + 1,
  3039. lcl_hdr_sz, lcl_hdr_sz, &mem, true);
  3040. if (retval) {
  3041. IPAERR("fail generate empty rt img\n");
  3042. return -ENOMEM;
  3043. }
  3044. desc = kcalloc(2, sizeof(struct ipa3_desc), GFP_KERNEL);
  3045. if (!desc) {
  3046. retval = -ENOMEM;
  3047. goto free_empty_img;
  3048. }
  3049. cmd_pyld = kcalloc(2, sizeof(struct ipahal_imm_cmd_pyld *), GFP_KERNEL);
  3050. if (!cmd_pyld) {
  3051. retval = -ENOMEM;
  3052. goto free_desc;
  3053. }
  3054. /* IC to close the coal frame before HPS Clear if coal is enabled */
  3055. if (ipa3_get_ep_mapping(IPA_CLIENT_APPS_WAN_COAL_CONS) != -1) {
  3056. i = ipa3_get_ep_mapping(IPA_CLIENT_APPS_WAN_COAL_CONS);
  3057. reg_write_coal_close.skip_pipeline_clear = false;
  3058. reg_write_coal_close.pipeline_clear_options = IPAHAL_HPS_CLEAR;
  3059. reg_write_coal_close.offset = ipahal_get_reg_ofst(
  3060. IPA_AGGR_FORCE_CLOSE);
  3061. ipahal_get_aggr_force_close_valmask(i, &valmask);
  3062. reg_write_coal_close.value = valmask.val;
  3063. reg_write_coal_close.value_mask = valmask.mask;
  3064. cmd_pyld[num_cmds] = ipahal_construct_imm_cmd(
  3065. IPA_IMM_CMD_REGISTER_WRITE,
  3066. &reg_write_coal_close, false);
  3067. if (!cmd_pyld[num_cmds]) {
  3068. IPAERR("failed to construct coal close IC\n");
  3069. retval = -ENOMEM;
  3070. goto free_cmd_pyld;
  3071. }
  3072. ipa3_init_imm_cmd_desc(&desc[num_cmds], cmd_pyld[num_cmds]);
  3073. ++num_cmds;
  3074. }
  3075. cmd.is_read = false;
  3076. cmd.skip_pipeline_clear = false;
  3077. cmd.pipeline_clear_options = IPAHAL_HPS_CLEAR;
  3078. cmd.size = mem.size;
  3079. cmd.system_addr = mem.phys_base;
  3080. cmd.local_addr = ipa3_ctx->smem_restricted_bytes +
  3081. lcl_addr_mem_part +
  3082. modem_rt_index_lo * ipahal_get_hw_tbl_hdr_width();
  3083. cmd_pyld[num_cmds] = ipahal_construct_imm_cmd(
  3084. IPA_IMM_CMD_DMA_SHARED_MEM, &cmd, false);
  3085. if (!cmd_pyld[num_cmds]) {
  3086. IPAERR("failed to construct dma_shared_mem imm cmd\n");
  3087. retval = -ENOMEM;
  3088. goto free_cmd_pyld;
  3089. }
  3090. ipa3_init_imm_cmd_desc(&desc[num_cmds], cmd_pyld[num_cmds]);
  3091. ++num_cmds;
  3092. IPADBG("Sending 1 descriptor for rt tbl clearing\n");
  3093. retval = ipa3_send_cmd(num_cmds, desc);
  3094. if (retval) {
  3095. IPAERR("failed to send immediate command (err %d)\n", retval);
  3096. retval = -EFAULT;
  3097. }
  3098. free_cmd_pyld:
  3099. for (i = 0; i < num_cmds; i++)
  3100. ipahal_destroy_imm_cmd(cmd_pyld[i]);
  3101. kfree(cmd_pyld);
  3102. free_desc:
  3103. kfree(desc);
  3104. free_empty_img:
  3105. ipahal_free_dma_mem(&mem);
  3106. return retval;
  3107. }
  3108. static int ipa3_q6_clean_q6_tables(void)
  3109. {
  3110. struct ipa3_desc *desc;
  3111. struct ipahal_imm_cmd_pyld **cmd_pyld;
  3112. struct ipahal_imm_cmd_register_write reg_write_cmd = {0};
  3113. int retval = 0;
  3114. int num_cmds = 0;
  3115. struct ipahal_reg_fltrt_hash_flush flush;
  3116. struct ipahal_reg_valmask valmask;
  3117. struct ipahal_imm_cmd_register_write reg_write_coal_close;
  3118. int i;
  3119. IPADBG("Entry\n");
  3120. if (ipa3_q6_clean_q6_flt_tbls(IPA_IP_v4, IPA_RULE_HASHABLE)) {
  3121. IPAERR("failed to clean q6 flt tbls (v4/hashable)\n");
  3122. return -EFAULT;
  3123. }
  3124. if (ipa3_q6_clean_q6_flt_tbls(IPA_IP_v6, IPA_RULE_HASHABLE)) {
  3125. IPAERR("failed to clean q6 flt tbls (v6/hashable)\n");
  3126. return -EFAULT;
  3127. }
  3128. if (ipa3_q6_clean_q6_flt_tbls(IPA_IP_v4, IPA_RULE_NON_HASHABLE)) {
  3129. IPAERR("failed to clean q6 flt tbls (v4/non-hashable)\n");
  3130. return -EFAULT;
  3131. }
  3132. if (ipa3_q6_clean_q6_flt_tbls(IPA_IP_v6, IPA_RULE_NON_HASHABLE)) {
  3133. IPAERR("failed to clean q6 flt tbls (v6/non-hashable)\n");
  3134. return -EFAULT;
  3135. }
  3136. if (ipa3_q6_clean_q6_rt_tbls(IPA_IP_v4, IPA_RULE_HASHABLE)) {
  3137. IPAERR("failed to clean q6 rt tbls (v4/hashable)\n");
  3138. return -EFAULT;
  3139. }
  3140. if (ipa3_q6_clean_q6_rt_tbls(IPA_IP_v6, IPA_RULE_HASHABLE)) {
  3141. IPAERR("failed to clean q6 rt tbls (v6/hashable)\n");
  3142. return -EFAULT;
  3143. }
  3144. if (ipa3_q6_clean_q6_rt_tbls(IPA_IP_v4, IPA_RULE_NON_HASHABLE)) {
  3145. IPAERR("failed to clean q6 rt tbls (v4/non-hashable)\n");
  3146. return -EFAULT;
  3147. }
  3148. if (ipa3_q6_clean_q6_rt_tbls(IPA_IP_v6, IPA_RULE_NON_HASHABLE)) {
  3149. IPAERR("failed to clean q6 rt tbls (v6/non-hashable)\n");
  3150. return -EFAULT;
  3151. }
  3152. /*
  3153. * SRAM memory not allocated to hash tables. Cleaning the of hash table
  3154. * operation not supported.
  3155. */
  3156. if (ipa3_ctx->ipa_fltrt_not_hashable)
  3157. return retval;
  3158. /* Flush rules cache */
  3159. desc = kcalloc(2, sizeof(struct ipa3_desc), GFP_KERNEL);
  3160. if (!desc)
  3161. return -ENOMEM;
  3162. cmd_pyld = kcalloc(2, sizeof(struct ipahal_imm_cmd_pyld *), GFP_KERNEL);
  3163. if (!cmd_pyld) {
  3164. retval = -ENOMEM;
  3165. goto bail_desc;
  3166. }
  3167. /* IC to close the coal frame before HPS Clear if coal is enabled */
  3168. if (ipa3_get_ep_mapping(IPA_CLIENT_APPS_WAN_COAL_CONS) != -1) {
  3169. i = ipa3_get_ep_mapping(IPA_CLIENT_APPS_WAN_COAL_CONS);
  3170. reg_write_coal_close.skip_pipeline_clear = false;
  3171. reg_write_coal_close.pipeline_clear_options = IPAHAL_HPS_CLEAR;
  3172. reg_write_coal_close.offset = ipahal_get_reg_ofst(
  3173. IPA_AGGR_FORCE_CLOSE);
  3174. ipahal_get_aggr_force_close_valmask(i, &valmask);
  3175. reg_write_coal_close.value = valmask.val;
  3176. reg_write_coal_close.value_mask = valmask.mask;
  3177. cmd_pyld[num_cmds] = ipahal_construct_imm_cmd(
  3178. IPA_IMM_CMD_REGISTER_WRITE,
  3179. &reg_write_coal_close, false);
  3180. if (!cmd_pyld[num_cmds]) {
  3181. IPAERR("failed to construct coal close IC\n");
  3182. retval = -ENOMEM;
  3183. goto free_cmd_pyld;
  3184. }
  3185. ipa3_init_imm_cmd_desc(&desc[num_cmds], cmd_pyld[num_cmds]);
  3186. ++num_cmds;
  3187. }
  3188. flush.v4_flt = true;
  3189. flush.v4_rt = true;
  3190. flush.v6_flt = true;
  3191. flush.v6_rt = true;
  3192. ipahal_get_fltrt_hash_flush_valmask(&flush, &valmask);
  3193. reg_write_cmd.skip_pipeline_clear = false;
  3194. reg_write_cmd.pipeline_clear_options = IPAHAL_HPS_CLEAR;
  3195. reg_write_cmd.offset = ipahal_get_reg_ofst(IPA_FILT_ROUT_HASH_FLUSH);
  3196. reg_write_cmd.value = valmask.val;
  3197. reg_write_cmd.value_mask = valmask.mask;
  3198. cmd_pyld[num_cmds] = ipahal_construct_imm_cmd(
  3199. IPA_IMM_CMD_REGISTER_WRITE, &reg_write_cmd, false);
  3200. if (!cmd_pyld[num_cmds]) {
  3201. IPAERR("fail construct register_write imm cmd\n");
  3202. retval = -EFAULT;
  3203. goto free_cmd_pyld;
  3204. }
  3205. ipa3_init_imm_cmd_desc(&desc[num_cmds], cmd_pyld[num_cmds]);
  3206. ++num_cmds;
  3207. IPADBG("Sending 1 descriptor for tbls flush\n");
  3208. retval = ipa3_send_cmd(num_cmds, desc);
  3209. if (retval) {
  3210. IPAERR("failed to send immediate command (err %d)\n", retval);
  3211. retval = -EFAULT;
  3212. }
  3213. free_cmd_pyld:
  3214. for (i = 0; i < num_cmds; i++)
  3215. ipahal_destroy_imm_cmd(cmd_pyld[i]);
  3216. kfree(cmd_pyld);
  3217. bail_desc:
  3218. kfree(desc);
  3219. IPADBG("Done - retval = %d\n", retval);
  3220. return retval;
  3221. }
  3222. static int ipa3_q6_set_ex_path_to_apps(void)
  3223. {
  3224. int ep_idx;
  3225. int client_idx;
  3226. struct ipa3_desc *desc;
  3227. int num_descs = 0;
  3228. int index;
  3229. struct ipahal_imm_cmd_register_write reg_write;
  3230. struct ipahal_imm_cmd_pyld *cmd_pyld;
  3231. int retval;
  3232. struct ipahal_reg_valmask valmask;
  3233. struct ipahal_imm_cmd_register_write reg_write_coal_close;
  3234. int i;
  3235. desc = kcalloc(ipa3_ctx->ipa_num_pipes + 1, sizeof(struct ipa3_desc),
  3236. GFP_KERNEL);
  3237. if (!desc)
  3238. return -ENOMEM;
  3239. /* IC to close the coal frame before HPS Clear if coal is enabled */
  3240. if (ipa3_get_ep_mapping(IPA_CLIENT_APPS_WAN_COAL_CONS) != -1) {
  3241. i = ipa3_get_ep_mapping(IPA_CLIENT_APPS_WAN_COAL_CONS);
  3242. reg_write_coal_close.skip_pipeline_clear = false;
  3243. reg_write_coal_close.pipeline_clear_options = IPAHAL_HPS_CLEAR;
  3244. reg_write_coal_close.offset = ipahal_get_reg_ofst(
  3245. IPA_AGGR_FORCE_CLOSE);
  3246. ipahal_get_aggr_force_close_valmask(i, &valmask);
  3247. reg_write_coal_close.value = valmask.val;
  3248. reg_write_coal_close.value_mask = valmask.mask;
  3249. cmd_pyld = ipahal_construct_imm_cmd(
  3250. IPA_IMM_CMD_REGISTER_WRITE,
  3251. &reg_write_coal_close, false);
  3252. if (!cmd_pyld) {
  3253. IPAERR("failed to construct coal close IC\n");
  3254. ipa_assert();
  3255. return -ENOMEM;
  3256. }
  3257. ipa3_init_imm_cmd_desc(&desc[num_descs], cmd_pyld);
  3258. desc[num_descs].callback = ipa3_destroy_imm;
  3259. desc[num_descs].user1 = cmd_pyld;
  3260. ++num_descs;
  3261. }
  3262. /* Set the exception path to AP */
  3263. for (client_idx = 0; client_idx < IPA_CLIENT_MAX; client_idx++) {
  3264. ep_idx = ipa3_get_ep_mapping(client_idx);
  3265. if (ep_idx == -1 || (ep_idx >= IPA3_MAX_NUM_PIPES))
  3266. continue;
  3267. /* disable statuses for all modem controlled prod pipes */
  3268. if (!IPA_CLIENT_IS_TEST(client_idx) &&
  3269. (IPA_CLIENT_IS_Q6_PROD(client_idx) ||
  3270. (IPA_CLIENT_IS_PROD(client_idx) &&
  3271. ipa3_ctx->ep[ep_idx].valid &&
  3272. ipa3_ctx->ep[ep_idx].skip_ep_cfg) ||
  3273. (ipa3_ctx->ep[ep_idx].client == IPA_CLIENT_APPS_WAN_PROD
  3274. && ipa3_ctx->modem_cfg_emb_pipe_flt))) {
  3275. ipa_assert_on(num_descs >= ipa3_ctx->ipa_num_pipes);
  3276. ipa3_ctx->ep[ep_idx].status.status_en = false;
  3277. reg_write.skip_pipeline_clear = false;
  3278. reg_write.pipeline_clear_options =
  3279. IPAHAL_HPS_CLEAR;
  3280. reg_write.offset =
  3281. ipahal_get_reg_n_ofst(IPA_ENDP_STATUS_n,
  3282. ep_idx);
  3283. reg_write.value = 0;
  3284. reg_write.value_mask = ~0;
  3285. cmd_pyld = ipahal_construct_imm_cmd(
  3286. IPA_IMM_CMD_REGISTER_WRITE, &reg_write, false);
  3287. if (!cmd_pyld) {
  3288. IPAERR("fail construct register_write cmd\n");
  3289. ipa_assert();
  3290. return -ENOMEM;
  3291. }
  3292. ipa3_init_imm_cmd_desc(&desc[num_descs], cmd_pyld);
  3293. desc[num_descs].callback = ipa3_destroy_imm;
  3294. desc[num_descs].user1 = cmd_pyld;
  3295. ++num_descs;
  3296. }
  3297. }
  3298. /* Will wait 500msecs for IPA tag process completion */
  3299. retval = ipa3_tag_process(desc, num_descs,
  3300. msecs_to_jiffies(CLEANUP_TAG_PROCESS_TIMEOUT));
  3301. if (retval) {
  3302. IPAERR("TAG process failed! (error %d)\n", retval);
  3303. /* For timeout error ipa3_destroy_imm cb will destroy user1 */
  3304. if (retval != -ETIME) {
  3305. for (index = 0; index < num_descs; index++)
  3306. if (desc[index].callback)
  3307. desc[index].callback(desc[index].user1,
  3308. desc[index].user2);
  3309. retval = -EINVAL;
  3310. }
  3311. }
  3312. kfree(desc);
  3313. return retval;
  3314. }
  3315. /*
  3316. * ipa3_update_ssr_state() - updating current SSR state
  3317. * @is_ssr: [in] Current SSR state
  3318. */
  3319. void ipa3_update_ssr_state(bool is_ssr)
  3320. {
  3321. if (is_ssr)
  3322. atomic_set(&ipa3_ctx->is_ssr, 1);
  3323. else
  3324. atomic_set(&ipa3_ctx->is_ssr, 0);
  3325. }
  3326. /**
  3327. * ipa3_q6_pre_shutdown_cleanup() - A cleanup for all Q6 related configuration
  3328. * in IPA HW. This is performed in case of SSR.
  3329. *
  3330. * This is a mandatory procedure, in case one of the steps fails, the
  3331. * AP needs to restart.
  3332. */
  3333. void ipa3_q6_pre_shutdown_cleanup(void)
  3334. {
  3335. IPADBG_LOW("ENTER\n");
  3336. IPA_ACTIVE_CLIENTS_INC_SIMPLE();
  3337. ipa3_update_ssr_state(true);
  3338. if (!ipa3_ctx->ipa_endp_delay_wa)
  3339. ipa3_q6_pipe_delay(true);
  3340. ipa3_q6_avoid_holb();
  3341. if (ipa3_ctx->ipa_config_is_mhi)
  3342. ipa3_set_reset_client_cons_pipe_sus_holb(true,
  3343. IPA_CLIENT_MHI_CONS);
  3344. if (ipa3_q6_clean_q6_tables()) {
  3345. IPAERR("Failed to clean Q6 tables\n");
  3346. /*
  3347. * Indicates IPA hardware is stalled, unexpected
  3348. * hardware state.
  3349. */
  3350. ipa_assert();
  3351. }
  3352. if (ipa3_q6_set_ex_path_to_apps()) {
  3353. IPAERR("Failed to redirect exceptions to APPS\n");
  3354. /*
  3355. * Indicates IPA hardware is stalled, unexpected
  3356. * hardware state.
  3357. */
  3358. ipa_assert();
  3359. }
  3360. /* Remove delay from Q6 PRODs to avoid pending descriptors
  3361. * on pipe reset procedure
  3362. */
  3363. if (!ipa3_ctx->ipa_endp_delay_wa) {
  3364. ipa3_q6_pipe_delay(false);
  3365. ipa3_set_reset_client_prod_pipe_delay(true,
  3366. IPA_CLIENT_USB_PROD);
  3367. } else {
  3368. ipa3_start_stop_client_prod_gsi_chnl(IPA_CLIENT_USB_PROD,
  3369. false);
  3370. }
  3371. IPA_ACTIVE_CLIENTS_DEC_SIMPLE();
  3372. IPADBG_LOW("Exit with success\n");
  3373. }
  3374. /*
  3375. * ipa3_q6_post_shutdown_cleanup() - As part of this cleanup
  3376. * check if GSI channel related to Q6 producer client is empty.
  3377. *
  3378. * Q6 GSI channel emptiness is needed to garantee no descriptors with invalid
  3379. * info are injected into IPA RX from IPA_IF, while modem is restarting.
  3380. */
  3381. void ipa3_q6_post_shutdown_cleanup(void)
  3382. {
  3383. int client_idx;
  3384. int ep_idx;
  3385. bool prod = false;
  3386. IPADBG_LOW("ENTER\n");
  3387. IPA_ACTIVE_CLIENTS_INC_SIMPLE();
  3388. /* Handle the issue where SUSPEND was removed for some reason */
  3389. ipa3_q6_avoid_holb();
  3390. /* halt both prod and cons channels starting at IPAv4 */
  3391. if (ipa3_ctx->ipa_hw_type >= IPA_HW_v4_0) {
  3392. prod = true;
  3393. ipa3_halt_q6_gsi_channels(prod);
  3394. IPA_ACTIVE_CLIENTS_DEC_SIMPLE();
  3395. IPADBG("Exit without consumer check\n");
  3396. return;
  3397. }
  3398. ipa3_halt_q6_gsi_channels(prod);
  3399. if (!ipa3_ctx->uc_ctx.uc_loaded) {
  3400. IPAERR("uC is not loaded. Skipping\n");
  3401. return;
  3402. }
  3403. for (client_idx = 0; client_idx < IPA_CLIENT_MAX; client_idx++)
  3404. if (IPA_CLIENT_IS_Q6_PROD(client_idx)) {
  3405. ep_idx = ipa3_get_ep_mapping(client_idx);
  3406. if (ep_idx == -1)
  3407. continue;
  3408. if (ipa3_uc_is_gsi_channel_empty(client_idx)) {
  3409. IPAERR("fail to validate Q6 ch emptiness %d\n",
  3410. client_idx);
  3411. /*
  3412. * Indicates GSI hardware is stalled, unexpected
  3413. * hardware state.
  3414. * Remove bug for adb reboot issue.
  3415. */
  3416. }
  3417. }
  3418. IPA_ACTIVE_CLIENTS_DEC_SIMPLE();
  3419. IPADBG_LOW("Exit with success\n");
  3420. }
  3421. /**
  3422. * ipa3_q6_pre_powerup_cleanup() - A cleanup routine for pheripheral
  3423. * configuration in IPA HW. This is performed in case of SSR.
  3424. *
  3425. * This is a mandatory procedure, in case one of the steps fails, the
  3426. * AP needs to restart.
  3427. */
  3428. void ipa3_q6_pre_powerup_cleanup(void)
  3429. {
  3430. IPADBG_LOW("ENTER\n");
  3431. IPA_ACTIVE_CLIENTS_INC_SIMPLE();
  3432. if (ipa3_ctx->ipa_config_is_mhi)
  3433. ipa3_set_reset_client_prod_pipe_delay(true,
  3434. IPA_CLIENT_MHI_PROD);
  3435. IPA_ACTIVE_CLIENTS_DEC_SIMPLE();
  3436. IPADBG_LOW("Exit with success\n");
  3437. }
  3438. /*
  3439. * ipa3_client_prod_post_shutdown_cleanup () - As part of this function
  3440. * set end point delay client producer pipes and starting corresponding
  3441. * gsi channels
  3442. */
  3443. void ipa3_client_prod_post_shutdown_cleanup(void)
  3444. {
  3445. IPADBG_LOW("ENTER\n");
  3446. IPA_ACTIVE_CLIENTS_INC_SIMPLE();
  3447. ipa3_set_reset_client_prod_pipe_delay(true,
  3448. IPA_CLIENT_USB_PROD);
  3449. ipa3_start_stop_client_prod_gsi_chnl(IPA_CLIENT_USB_PROD, true);
  3450. IPA_ACTIVE_CLIENTS_DEC_SIMPLE();
  3451. IPADBG_LOW("Exit with success\n");
  3452. }
  3453. static inline void ipa3_sram_set_canary(u32 *sram_mmio, int offset)
  3454. {
  3455. /* Set 4 bytes of CANARY before the offset */
  3456. sram_mmio[(offset - 4) / 4] = IPA_MEM_CANARY_VAL;
  3457. }
  3458. /**
  3459. * _ipa_init_sram_v3() - Initialize IPA local SRAM.
  3460. *
  3461. * Return codes: 0 for success, negative value for failure
  3462. */
  3463. int _ipa_init_sram_v3(void)
  3464. {
  3465. u32 *ipa_sram_mmio;
  3466. unsigned long phys_addr;
  3467. IPADBG(
  3468. "ipa_wrapper_base(0x%08X) ipa_reg_base_ofst(0x%08X) IPA_SW_AREA_RAM_DIRECT_ACCESS_n(0x%08X) smem_restricted_bytes(0x%08X) smem_sz(0x%08X)\n",
  3469. ipa3_ctx->ipa_wrapper_base,
  3470. ipa3_ctx->ctrl->ipa_reg_base_ofst,
  3471. ipahal_get_reg_n_ofst(
  3472. IPA_SW_AREA_RAM_DIRECT_ACCESS_n,
  3473. ipa3_ctx->smem_restricted_bytes / 4),
  3474. ipa3_ctx->smem_restricted_bytes,
  3475. ipa3_ctx->smem_sz);
  3476. phys_addr = ipa3_ctx->ipa_wrapper_base +
  3477. ipa3_ctx->ctrl->ipa_reg_base_ofst +
  3478. ipahal_get_reg_n_ofst(IPA_SW_AREA_RAM_DIRECT_ACCESS_n,
  3479. ipa3_ctx->smem_restricted_bytes / 4);
  3480. ipa_sram_mmio = ioremap(phys_addr, ipa3_ctx->smem_sz);
  3481. if (!ipa_sram_mmio) {
  3482. IPAERR("fail to ioremap IPA SRAM\n");
  3483. return -ENOMEM;
  3484. }
  3485. /* Consult with ipa_i.h on the location of the CANARY values */
  3486. ipa3_sram_set_canary(ipa_sram_mmio, IPA_MEM_PART(v4_flt_hash_ofst) - 4);
  3487. ipa3_sram_set_canary(ipa_sram_mmio, IPA_MEM_PART(v4_flt_hash_ofst));
  3488. ipa3_sram_set_canary(ipa_sram_mmio,
  3489. IPA_MEM_PART(v4_flt_nhash_ofst) - 4);
  3490. ipa3_sram_set_canary(ipa_sram_mmio, IPA_MEM_PART(v4_flt_nhash_ofst));
  3491. ipa3_sram_set_canary(ipa_sram_mmio, IPA_MEM_PART(v6_flt_hash_ofst) - 4);
  3492. ipa3_sram_set_canary(ipa_sram_mmio, IPA_MEM_PART(v6_flt_hash_ofst));
  3493. ipa3_sram_set_canary(ipa_sram_mmio,
  3494. IPA_MEM_PART(v6_flt_nhash_ofst) - 4);
  3495. ipa3_sram_set_canary(ipa_sram_mmio, IPA_MEM_PART(v6_flt_nhash_ofst));
  3496. ipa3_sram_set_canary(ipa_sram_mmio, IPA_MEM_PART(v4_rt_hash_ofst) - 4);
  3497. ipa3_sram_set_canary(ipa_sram_mmio, IPA_MEM_PART(v4_rt_hash_ofst));
  3498. ipa3_sram_set_canary(ipa_sram_mmio, IPA_MEM_PART(v4_rt_nhash_ofst) - 4);
  3499. ipa3_sram_set_canary(ipa_sram_mmio, IPA_MEM_PART(v4_rt_nhash_ofst));
  3500. ipa3_sram_set_canary(ipa_sram_mmio, IPA_MEM_PART(v6_rt_hash_ofst) - 4);
  3501. ipa3_sram_set_canary(ipa_sram_mmio, IPA_MEM_PART(v6_rt_hash_ofst));
  3502. ipa3_sram_set_canary(ipa_sram_mmio, IPA_MEM_PART(v6_rt_nhash_ofst) - 4);
  3503. ipa3_sram_set_canary(ipa_sram_mmio, IPA_MEM_PART(v6_rt_nhash_ofst));
  3504. ipa3_sram_set_canary(ipa_sram_mmio, IPA_MEM_PART(modem_hdr_ofst) - 4);
  3505. ipa3_sram_set_canary(ipa_sram_mmio, IPA_MEM_PART(modem_hdr_ofst));
  3506. ipa3_sram_set_canary(ipa_sram_mmio,
  3507. IPA_MEM_PART(modem_hdr_proc_ctx_ofst) - 4);
  3508. ipa3_sram_set_canary(ipa_sram_mmio,
  3509. IPA_MEM_PART(modem_hdr_proc_ctx_ofst));
  3510. if (ipa_get_hw_type() >= IPA_HW_v4_5) {
  3511. ipa3_sram_set_canary(ipa_sram_mmio,
  3512. IPA_MEM_PART(nat_tbl_ofst) - 12);
  3513. ipa3_sram_set_canary(ipa_sram_mmio,
  3514. IPA_MEM_PART(nat_tbl_ofst) - 8);
  3515. ipa3_sram_set_canary(ipa_sram_mmio,
  3516. IPA_MEM_PART(nat_tbl_ofst) - 4);
  3517. ipa3_sram_set_canary(ipa_sram_mmio, IPA_MEM_PART(nat_tbl_ofst));
  3518. }
  3519. if (ipa_get_hw_type() >= IPA_HW_v4_0) {
  3520. ipa3_sram_set_canary(ipa_sram_mmio,
  3521. IPA_MEM_PART(pdn_config_ofst) - 4);
  3522. ipa3_sram_set_canary(ipa_sram_mmio,
  3523. IPA_MEM_PART(pdn_config_ofst));
  3524. ipa3_sram_set_canary(ipa_sram_mmio,
  3525. IPA_MEM_PART(stats_quota_ofst) - 4);
  3526. ipa3_sram_set_canary(ipa_sram_mmio,
  3527. IPA_MEM_PART(stats_quota_ofst));
  3528. }
  3529. if (ipa_get_hw_type() <= IPA_HW_v3_5 ||
  3530. ipa_get_hw_type() >= IPA_HW_v4_5) {
  3531. ipa3_sram_set_canary(ipa_sram_mmio,
  3532. IPA_MEM_PART(modem_ofst) - 4);
  3533. ipa3_sram_set_canary(ipa_sram_mmio, IPA_MEM_PART(modem_ofst));
  3534. }
  3535. ipa3_sram_set_canary(ipa_sram_mmio,
  3536. (ipa_get_hw_type() >= IPA_HW_v3_5) ?
  3537. IPA_MEM_PART(uc_descriptor_ram_ofst) :
  3538. IPA_MEM_PART(end_ofst));
  3539. iounmap(ipa_sram_mmio);
  3540. return 0;
  3541. }
  3542. /**
  3543. * _ipa_init_hdr_v3_0() - Initialize IPA header block.
  3544. *
  3545. * Return codes: 0 for success, negative value for failure
  3546. */
  3547. int _ipa_init_hdr_v3_0(void)
  3548. {
  3549. struct ipa3_desc hdr_init_desc;
  3550. struct ipa3_desc dma_cmd_desc[2];
  3551. struct ipa_mem_buffer mem;
  3552. struct ipahal_imm_cmd_hdr_init_local cmd = {0};
  3553. struct ipahal_imm_cmd_pyld *hdr_init_cmd_payload;
  3554. struct ipahal_imm_cmd_pyld *cmd_pyld[2];
  3555. struct ipahal_imm_cmd_dma_shared_mem dma_cmd = { 0 };
  3556. struct ipahal_reg_valmask valmask;
  3557. struct ipahal_imm_cmd_register_write reg_write_coal_close;
  3558. int num_cmds = 0;
  3559. int i;
  3560. mem.size = IPA_MEM_PART(modem_hdr_size) + IPA_MEM_PART(apps_hdr_size);
  3561. mem.base = dma_alloc_coherent(ipa3_ctx->pdev, mem.size, &mem.phys_base,
  3562. GFP_KERNEL);
  3563. if (!mem.base) {
  3564. IPAERR("fail to alloc DMA buff of size %d\n", mem.size);
  3565. return -ENOMEM;
  3566. }
  3567. cmd.hdr_table_addr = mem.phys_base;
  3568. cmd.size_hdr_table = mem.size;
  3569. cmd.hdr_addr = ipa3_ctx->smem_restricted_bytes +
  3570. IPA_MEM_PART(modem_hdr_ofst);
  3571. hdr_init_cmd_payload = ipahal_construct_imm_cmd(
  3572. IPA_IMM_CMD_HDR_INIT_LOCAL, &cmd, false);
  3573. if (!hdr_init_cmd_payload) {
  3574. IPAERR("fail to construct hdr_init_local imm cmd\n");
  3575. dma_free_coherent(ipa3_ctx->pdev,
  3576. mem.size, mem.base,
  3577. mem.phys_base);
  3578. return -EFAULT;
  3579. }
  3580. ipa3_init_imm_cmd_desc(&hdr_init_desc, hdr_init_cmd_payload);
  3581. IPA_DUMP_BUFF(mem.base, mem.phys_base, mem.size);
  3582. if (ipa3_send_cmd(1, &hdr_init_desc)) {
  3583. IPAERR("fail to send immediate command\n");
  3584. ipahal_destroy_imm_cmd(hdr_init_cmd_payload);
  3585. dma_free_coherent(ipa3_ctx->pdev,
  3586. mem.size, mem.base,
  3587. mem.phys_base);
  3588. return -EFAULT;
  3589. }
  3590. ipahal_destroy_imm_cmd(hdr_init_cmd_payload);
  3591. dma_free_coherent(ipa3_ctx->pdev, mem.size, mem.base, mem.phys_base);
  3592. /* IC to close the coal frame before HPS Clear if coal is enabled */
  3593. if (ipa3_get_ep_mapping(IPA_CLIENT_APPS_WAN_COAL_CONS) != -1) {
  3594. i = ipa3_get_ep_mapping(IPA_CLIENT_APPS_WAN_COAL_CONS);
  3595. reg_write_coal_close.skip_pipeline_clear = false;
  3596. reg_write_coal_close.pipeline_clear_options = IPAHAL_HPS_CLEAR;
  3597. reg_write_coal_close.offset = ipahal_get_reg_ofst(
  3598. IPA_AGGR_FORCE_CLOSE);
  3599. ipahal_get_aggr_force_close_valmask(i, &valmask);
  3600. reg_write_coal_close.value = valmask.val;
  3601. reg_write_coal_close.value_mask = valmask.mask;
  3602. cmd_pyld[num_cmds] = ipahal_construct_imm_cmd(
  3603. IPA_IMM_CMD_REGISTER_WRITE,
  3604. &reg_write_coal_close, false);
  3605. if (!cmd_pyld[num_cmds]) {
  3606. IPAERR("failed to construct coal close IC\n");
  3607. return -ENOMEM;
  3608. }
  3609. ipa3_init_imm_cmd_desc(&dma_cmd_desc[num_cmds],
  3610. cmd_pyld[num_cmds]);
  3611. ++num_cmds;
  3612. }
  3613. mem.size = IPA_MEM_PART(modem_hdr_proc_ctx_size) +
  3614. IPA_MEM_PART(apps_hdr_proc_ctx_size);
  3615. mem.base = dma_alloc_coherent(ipa3_ctx->pdev, mem.size, &mem.phys_base,
  3616. GFP_KERNEL);
  3617. if (!mem.base) {
  3618. IPAERR("fail to alloc DMA buff of size %d\n", mem.size);
  3619. return -ENOMEM;
  3620. }
  3621. dma_cmd.is_read = false;
  3622. dma_cmd.skip_pipeline_clear = false;
  3623. dma_cmd.pipeline_clear_options = IPAHAL_HPS_CLEAR;
  3624. dma_cmd.system_addr = mem.phys_base;
  3625. dma_cmd.local_addr = ipa3_ctx->smem_restricted_bytes +
  3626. IPA_MEM_PART(modem_hdr_proc_ctx_ofst);
  3627. dma_cmd.size = mem.size;
  3628. cmd_pyld[num_cmds] = ipahal_construct_imm_cmd(
  3629. IPA_IMM_CMD_DMA_SHARED_MEM, &dma_cmd, false);
  3630. if (!cmd_pyld[num_cmds]) {
  3631. IPAERR("fail to construct dma_shared_mem imm\n");
  3632. dma_free_coherent(ipa3_ctx->pdev,
  3633. mem.size, mem.base,
  3634. mem.phys_base);
  3635. return -ENOMEM;
  3636. }
  3637. ipa3_init_imm_cmd_desc(&dma_cmd_desc[num_cmds], cmd_pyld[num_cmds]);
  3638. ++num_cmds;
  3639. IPA_DUMP_BUFF(mem.base, mem.phys_base, mem.size);
  3640. if (ipa3_send_cmd(num_cmds, dma_cmd_desc)) {
  3641. IPAERR("fail to send immediate command\n");
  3642. for (i = 0; i < num_cmds; i++)
  3643. ipahal_destroy_imm_cmd(cmd_pyld[i]);
  3644. dma_free_coherent(ipa3_ctx->pdev,
  3645. mem.size,
  3646. mem.base,
  3647. mem.phys_base);
  3648. return -EBUSY;
  3649. }
  3650. for (i = 0; i < num_cmds; i++)
  3651. ipahal_destroy_imm_cmd(cmd_pyld[i]);
  3652. ipahal_write_reg(IPA_LOCAL_PKT_PROC_CNTXT_BASE, dma_cmd.local_addr);
  3653. dma_free_coherent(ipa3_ctx->pdev, mem.size, mem.base, mem.phys_base);
  3654. return 0;
  3655. }
  3656. /**
  3657. * _ipa_init_rt4_v3() - Initialize IPA routing block for IPv4.
  3658. *
  3659. * Return codes: 0 for success, negative value for failure
  3660. */
  3661. int _ipa_init_rt4_v3(void)
  3662. {
  3663. struct ipa3_desc desc;
  3664. struct ipa_mem_buffer mem;
  3665. struct ipahal_imm_cmd_ip_v4_routing_init v4_cmd;
  3666. struct ipahal_imm_cmd_pyld *cmd_pyld;
  3667. int i;
  3668. int rc = 0;
  3669. for (i = IPA_MEM_PART(v4_modem_rt_index_lo);
  3670. i <= IPA_MEM_PART(v4_modem_rt_index_hi);
  3671. i++)
  3672. ipa3_ctx->rt_idx_bitmap[IPA_IP_v4] |= (1 << i);
  3673. IPADBG("v4 rt bitmap 0x%lx\n", ipa3_ctx->rt_idx_bitmap[IPA_IP_v4]);
  3674. rc = ipahal_rt_generate_empty_img(IPA_MEM_PART(v4_rt_num_index),
  3675. IPA_MEM_PART(v4_rt_hash_size), IPA_MEM_PART(v4_rt_nhash_size),
  3676. &mem, false);
  3677. if (rc) {
  3678. IPAERR("fail generate empty v4 rt img\n");
  3679. return rc;
  3680. }
  3681. /*
  3682. * SRAM memory not allocated to hash tables. Initializing/Sending
  3683. * command to hash tables(filer/routing) operation not supported.
  3684. */
  3685. if (ipa3_ctx->ipa_fltrt_not_hashable) {
  3686. v4_cmd.hash_rules_addr = 0;
  3687. v4_cmd.hash_rules_size = 0;
  3688. v4_cmd.hash_local_addr = 0;
  3689. } else {
  3690. v4_cmd.hash_rules_addr = mem.phys_base;
  3691. v4_cmd.hash_rules_size = mem.size;
  3692. v4_cmd.hash_local_addr = ipa3_ctx->smem_restricted_bytes +
  3693. IPA_MEM_PART(v4_rt_hash_ofst);
  3694. }
  3695. v4_cmd.nhash_rules_addr = mem.phys_base;
  3696. v4_cmd.nhash_rules_size = mem.size;
  3697. v4_cmd.nhash_local_addr = ipa3_ctx->smem_restricted_bytes +
  3698. IPA_MEM_PART(v4_rt_nhash_ofst);
  3699. IPADBG("putting hashable routing IPv4 rules to phys 0x%x\n",
  3700. v4_cmd.hash_local_addr);
  3701. IPADBG("putting non-hashable routing IPv4 rules to phys 0x%x\n",
  3702. v4_cmd.nhash_local_addr);
  3703. cmd_pyld = ipahal_construct_imm_cmd(
  3704. IPA_IMM_CMD_IP_V4_ROUTING_INIT, &v4_cmd, false);
  3705. if (!cmd_pyld) {
  3706. IPAERR("fail construct ip_v4_rt_init imm cmd\n");
  3707. rc = -EPERM;
  3708. goto free_mem;
  3709. }
  3710. ipa3_init_imm_cmd_desc(&desc, cmd_pyld);
  3711. IPA_DUMP_BUFF(mem.base, mem.phys_base, mem.size);
  3712. if (ipa3_send_cmd(1, &desc)) {
  3713. IPAERR("fail to send immediate command\n");
  3714. rc = -EFAULT;
  3715. }
  3716. ipahal_destroy_imm_cmd(cmd_pyld);
  3717. free_mem:
  3718. ipahal_free_dma_mem(&mem);
  3719. return rc;
  3720. }
  3721. /**
  3722. * _ipa_init_rt6_v3() - Initialize IPA routing block for IPv6.
  3723. *
  3724. * Return codes: 0 for success, negative value for failure
  3725. */
  3726. int _ipa_init_rt6_v3(void)
  3727. {
  3728. struct ipa3_desc desc;
  3729. struct ipa_mem_buffer mem;
  3730. struct ipahal_imm_cmd_ip_v6_routing_init v6_cmd;
  3731. struct ipahal_imm_cmd_pyld *cmd_pyld;
  3732. int i;
  3733. int rc = 0;
  3734. for (i = IPA_MEM_PART(v6_modem_rt_index_lo);
  3735. i <= IPA_MEM_PART(v6_modem_rt_index_hi);
  3736. i++)
  3737. ipa3_ctx->rt_idx_bitmap[IPA_IP_v6] |= (1 << i);
  3738. IPADBG("v6 rt bitmap 0x%lx\n", ipa3_ctx->rt_idx_bitmap[IPA_IP_v6]);
  3739. rc = ipahal_rt_generate_empty_img(IPA_MEM_PART(v6_rt_num_index),
  3740. IPA_MEM_PART(v6_rt_hash_size), IPA_MEM_PART(v6_rt_nhash_size),
  3741. &mem, false);
  3742. if (rc) {
  3743. IPAERR("fail generate empty v6 rt img\n");
  3744. return rc;
  3745. }
  3746. /*
  3747. * SRAM memory not allocated to hash tables. Initializing/Sending
  3748. * command to hash tables(filer/routing) operation not supported.
  3749. */
  3750. if (ipa3_ctx->ipa_fltrt_not_hashable) {
  3751. v6_cmd.hash_rules_addr = 0;
  3752. v6_cmd.hash_rules_size = 0;
  3753. v6_cmd.hash_local_addr = 0;
  3754. } else {
  3755. v6_cmd.hash_rules_addr = mem.phys_base;
  3756. v6_cmd.hash_rules_size = mem.size;
  3757. v6_cmd.hash_local_addr = ipa3_ctx->smem_restricted_bytes +
  3758. IPA_MEM_PART(v6_rt_hash_ofst);
  3759. }
  3760. v6_cmd.nhash_rules_addr = mem.phys_base;
  3761. v6_cmd.nhash_rules_size = mem.size;
  3762. v6_cmd.nhash_local_addr = ipa3_ctx->smem_restricted_bytes +
  3763. IPA_MEM_PART(v6_rt_nhash_ofst);
  3764. IPADBG("putting hashable routing IPv6 rules to phys 0x%x\n",
  3765. v6_cmd.hash_local_addr);
  3766. IPADBG("putting non-hashable routing IPv6 rules to phys 0x%x\n",
  3767. v6_cmd.nhash_local_addr);
  3768. cmd_pyld = ipahal_construct_imm_cmd(
  3769. IPA_IMM_CMD_IP_V6_ROUTING_INIT, &v6_cmd, false);
  3770. if (!cmd_pyld) {
  3771. IPAERR("fail construct ip_v6_rt_init imm cmd\n");
  3772. rc = -EPERM;
  3773. goto free_mem;
  3774. }
  3775. ipa3_init_imm_cmd_desc(&desc, cmd_pyld);
  3776. IPA_DUMP_BUFF(mem.base, mem.phys_base, mem.size);
  3777. if (ipa3_send_cmd(1, &desc)) {
  3778. IPAERR("fail to send immediate command\n");
  3779. rc = -EFAULT;
  3780. }
  3781. ipahal_destroy_imm_cmd(cmd_pyld);
  3782. free_mem:
  3783. ipahal_free_dma_mem(&mem);
  3784. return rc;
  3785. }
  3786. /**
  3787. * _ipa_init_flt4_v3() - Initialize IPA filtering block for IPv4.
  3788. *
  3789. * Return codes: 0 for success, negative value for failure
  3790. */
  3791. int _ipa_init_flt4_v3(void)
  3792. {
  3793. struct ipa3_desc desc;
  3794. struct ipa_mem_buffer mem;
  3795. struct ipahal_imm_cmd_ip_v4_filter_init v4_cmd;
  3796. struct ipahal_imm_cmd_pyld *cmd_pyld;
  3797. int rc;
  3798. rc = ipahal_flt_generate_empty_img(ipa3_ctx->ep_flt_num,
  3799. IPA_MEM_PART(v4_flt_hash_size),
  3800. IPA_MEM_PART(v4_flt_nhash_size), ipa3_ctx->ep_flt_bitmap,
  3801. &mem, false);
  3802. if (rc) {
  3803. IPAERR("fail generate empty v4 flt img\n");
  3804. return rc;
  3805. }
  3806. /*
  3807. * SRAM memory not allocated to hash tables. Initializing/Sending
  3808. * command to hash tables(filer/routing) operation not supported.
  3809. */
  3810. if (ipa3_ctx->ipa_fltrt_not_hashable) {
  3811. v4_cmd.hash_rules_addr = 0;
  3812. v4_cmd.hash_rules_size = 0;
  3813. v4_cmd.hash_local_addr = 0;
  3814. } else {
  3815. v4_cmd.hash_rules_addr = mem.phys_base;
  3816. v4_cmd.hash_rules_size = mem.size;
  3817. v4_cmd.hash_local_addr = ipa3_ctx->smem_restricted_bytes +
  3818. IPA_MEM_PART(v4_flt_hash_ofst);
  3819. }
  3820. v4_cmd.nhash_rules_addr = mem.phys_base;
  3821. v4_cmd.nhash_rules_size = mem.size;
  3822. v4_cmd.nhash_local_addr = ipa3_ctx->smem_restricted_bytes +
  3823. IPA_MEM_PART(v4_flt_nhash_ofst);
  3824. IPADBG("putting hashable filtering IPv4 rules to phys 0x%x\n",
  3825. v4_cmd.hash_local_addr);
  3826. IPADBG("putting non-hashable filtering IPv4 rules to phys 0x%x\n",
  3827. v4_cmd.nhash_local_addr);
  3828. cmd_pyld = ipahal_construct_imm_cmd(
  3829. IPA_IMM_CMD_IP_V4_FILTER_INIT, &v4_cmd, false);
  3830. if (!cmd_pyld) {
  3831. IPAERR("fail construct ip_v4_flt_init imm cmd\n");
  3832. rc = -EPERM;
  3833. goto free_mem;
  3834. }
  3835. ipa3_init_imm_cmd_desc(&desc, cmd_pyld);
  3836. IPA_DUMP_BUFF(mem.base, mem.phys_base, mem.size);
  3837. if (ipa3_send_cmd(1, &desc)) {
  3838. IPAERR("fail to send immediate command\n");
  3839. rc = -EFAULT;
  3840. }
  3841. ipahal_destroy_imm_cmd(cmd_pyld);
  3842. free_mem:
  3843. ipahal_free_dma_mem(&mem);
  3844. return rc;
  3845. }
  3846. /**
  3847. * _ipa_init_flt6_v3() - Initialize IPA filtering block for IPv6.
  3848. *
  3849. * Return codes: 0 for success, negative value for failure
  3850. */
  3851. int _ipa_init_flt6_v3(void)
  3852. {
  3853. struct ipa3_desc desc;
  3854. struct ipa_mem_buffer mem;
  3855. struct ipahal_imm_cmd_ip_v6_filter_init v6_cmd;
  3856. struct ipahal_imm_cmd_pyld *cmd_pyld;
  3857. int rc;
  3858. rc = ipahal_flt_generate_empty_img(ipa3_ctx->ep_flt_num,
  3859. IPA_MEM_PART(v6_flt_hash_size),
  3860. IPA_MEM_PART(v6_flt_nhash_size), ipa3_ctx->ep_flt_bitmap,
  3861. &mem, false);
  3862. if (rc) {
  3863. IPAERR("fail generate empty v6 flt img\n");
  3864. return rc;
  3865. }
  3866. /*
  3867. * SRAM memory not allocated to hash tables. Initializing/Sending
  3868. * command to hash tables(filer/routing) operation not supported.
  3869. */
  3870. if (ipa3_ctx->ipa_fltrt_not_hashable) {
  3871. v6_cmd.hash_rules_addr = 0;
  3872. v6_cmd.hash_rules_size = 0;
  3873. v6_cmd.hash_local_addr = 0;
  3874. } else {
  3875. v6_cmd.hash_rules_addr = mem.phys_base;
  3876. v6_cmd.hash_rules_size = mem.size;
  3877. v6_cmd.hash_local_addr = ipa3_ctx->smem_restricted_bytes +
  3878. IPA_MEM_PART(v6_flt_hash_ofst);
  3879. }
  3880. v6_cmd.nhash_rules_addr = mem.phys_base;
  3881. v6_cmd.nhash_rules_size = mem.size;
  3882. v6_cmd.nhash_local_addr = ipa3_ctx->smem_restricted_bytes +
  3883. IPA_MEM_PART(v6_flt_nhash_ofst);
  3884. IPADBG("putting hashable filtering IPv6 rules to phys 0x%x\n",
  3885. v6_cmd.hash_local_addr);
  3886. IPADBG("putting non-hashable filtering IPv6 rules to phys 0x%x\n",
  3887. v6_cmd.nhash_local_addr);
  3888. cmd_pyld = ipahal_construct_imm_cmd(
  3889. IPA_IMM_CMD_IP_V6_FILTER_INIT, &v6_cmd, false);
  3890. if (!cmd_pyld) {
  3891. IPAERR("fail construct ip_v6_flt_init imm cmd\n");
  3892. rc = -EPERM;
  3893. goto free_mem;
  3894. }
  3895. ipa3_init_imm_cmd_desc(&desc, cmd_pyld);
  3896. IPA_DUMP_BUFF(mem.base, mem.phys_base, mem.size);
  3897. if (ipa3_send_cmd(1, &desc)) {
  3898. IPAERR("fail to send immediate command\n");
  3899. rc = -EFAULT;
  3900. }
  3901. ipahal_destroy_imm_cmd(cmd_pyld);
  3902. free_mem:
  3903. ipahal_free_dma_mem(&mem);
  3904. return rc;
  3905. }
  3906. static int ipa3_setup_flt_hash_tuple(void)
  3907. {
  3908. int pipe_idx;
  3909. struct ipahal_reg_hash_tuple tuple;
  3910. memset(&tuple, 0, sizeof(struct ipahal_reg_hash_tuple));
  3911. for (pipe_idx = 0; pipe_idx < ipa3_ctx->ipa_num_pipes ; pipe_idx++) {
  3912. if (!ipa_is_ep_support_flt(pipe_idx))
  3913. continue;
  3914. if (ipa_is_modem_pipe(pipe_idx))
  3915. continue;
  3916. if (ipa3_set_flt_tuple_mask(pipe_idx, &tuple)) {
  3917. IPAERR("failed to setup pipe %d flt tuple\n", pipe_idx);
  3918. return -EFAULT;
  3919. }
  3920. }
  3921. return 0;
  3922. }
  3923. static int ipa3_setup_rt_hash_tuple(void)
  3924. {
  3925. int tbl_idx;
  3926. struct ipahal_reg_hash_tuple tuple;
  3927. memset(&tuple, 0, sizeof(struct ipahal_reg_hash_tuple));
  3928. for (tbl_idx = 0;
  3929. tbl_idx < max(IPA_MEM_PART(v6_rt_num_index),
  3930. IPA_MEM_PART(v4_rt_num_index));
  3931. tbl_idx++) {
  3932. if (tbl_idx >= IPA_MEM_PART(v4_modem_rt_index_lo) &&
  3933. tbl_idx <= IPA_MEM_PART(v4_modem_rt_index_hi))
  3934. continue;
  3935. if (tbl_idx >= IPA_MEM_PART(v6_modem_rt_index_lo) &&
  3936. tbl_idx <= IPA_MEM_PART(v6_modem_rt_index_hi))
  3937. continue;
  3938. if (ipa3_set_rt_tuple_mask(tbl_idx, &tuple)) {
  3939. IPAERR("failed to setup tbl %d rt tuple\n", tbl_idx);
  3940. return -EFAULT;
  3941. }
  3942. }
  3943. return 0;
  3944. }
  3945. static int ipa3_setup_apps_pipes(void)
  3946. {
  3947. struct ipa_sys_connect_params sys_in;
  3948. int result = 0;
  3949. if (ipa3_ctx->gsi_ch20_wa) {
  3950. IPADBG("Allocating GSI physical channel 20\n");
  3951. result = ipa_gsi_ch20_wa();
  3952. if (result) {
  3953. IPAERR("ipa_gsi_ch20_wa failed %d\n", result);
  3954. goto fail_ch20_wa;
  3955. }
  3956. }
  3957. /* allocate the common PROD event ring */
  3958. if (ipa3_alloc_common_event_ring()) {
  3959. IPAERR("ipa3_alloc_common_event_ring failed.\n");
  3960. result = -EPERM;
  3961. goto fail_ch20_wa;
  3962. }
  3963. /* CMD OUT (AP->IPA) */
  3964. memset(&sys_in, 0, sizeof(struct ipa_sys_connect_params));
  3965. sys_in.client = IPA_CLIENT_APPS_CMD_PROD;
  3966. sys_in.desc_fifo_sz = IPA_SYS_DESC_FIFO_SZ;
  3967. sys_in.ipa_ep_cfg.mode.mode = IPA_DMA;
  3968. sys_in.ipa_ep_cfg.mode.dst = IPA_CLIENT_APPS_LAN_CONS;
  3969. if (ipa3_setup_sys_pipe(&sys_in, &ipa3_ctx->clnt_hdl_cmd)) {
  3970. IPAERR(":setup sys pipe (APPS_CMD_PROD) failed.\n");
  3971. result = -EPERM;
  3972. goto fail_ch20_wa;
  3973. }
  3974. IPADBG("Apps to IPA cmd pipe is connected\n");
  3975. IPADBG("Will initialize SRAM\n");
  3976. ipa3_ctx->ctrl->ipa_init_sram();
  3977. IPADBG("SRAM initialized\n");
  3978. IPADBG("Will initialize HDR\n");
  3979. ipa3_ctx->ctrl->ipa_init_hdr();
  3980. IPADBG("HDR initialized\n");
  3981. IPADBG("Will initialize V4 RT\n");
  3982. ipa3_ctx->ctrl->ipa_init_rt4();
  3983. IPADBG("V4 RT initialized\n");
  3984. IPADBG("Will initialize V6 RT\n");
  3985. ipa3_ctx->ctrl->ipa_init_rt6();
  3986. IPADBG("V6 RT initialized\n");
  3987. IPADBG("Will initialize V4 FLT\n");
  3988. ipa3_ctx->ctrl->ipa_init_flt4();
  3989. IPADBG("V4 FLT initialized\n");
  3990. IPADBG("Will initialize V6 FLT\n");
  3991. ipa3_ctx->ctrl->ipa_init_flt6();
  3992. IPADBG("V6 FLT initialized\n");
  3993. if (!ipa3_ctx->ipa_fltrt_not_hashable) {
  3994. if (ipa3_setup_flt_hash_tuple()) {
  3995. IPAERR(":fail to configure flt hash tuple\n");
  3996. result = -EPERM;
  3997. goto fail_flt_hash_tuple;
  3998. }
  3999. IPADBG("flt hash tuple is configured\n");
  4000. if (ipa3_setup_rt_hash_tuple()) {
  4001. IPAERR(":fail to configure rt hash tuple\n");
  4002. result = -EPERM;
  4003. goto fail_flt_hash_tuple;
  4004. }
  4005. IPADBG("rt hash tuple is configured\n");
  4006. }
  4007. if (ipa3_setup_exception_path()) {
  4008. IPAERR(":fail to setup excp path\n");
  4009. result = -EPERM;
  4010. goto fail_flt_hash_tuple;
  4011. }
  4012. IPADBG("Exception path was successfully set");
  4013. if (ipa3_setup_dflt_rt_tables()) {
  4014. IPAERR(":fail to setup dflt routes\n");
  4015. result = -EPERM;
  4016. goto fail_flt_hash_tuple;
  4017. }
  4018. IPADBG("default routing was set\n");
  4019. /* LAN IN (IPA->AP) */
  4020. memset(&sys_in, 0, sizeof(struct ipa_sys_connect_params));
  4021. sys_in.client = IPA_CLIENT_APPS_LAN_CONS;
  4022. sys_in.desc_fifo_sz = IPA_SYS_DESC_FIFO_SZ;
  4023. sys_in.notify = ipa3_lan_rx_cb;
  4024. sys_in.priv = NULL;
  4025. if (ipa3_ctx->lan_rx_napi_enable)
  4026. sys_in.napi_obj = &ipa3_ctx->napi_lan_rx;
  4027. sys_in.ipa_ep_cfg.hdr.hdr_len = IPA_LAN_RX_HEADER_LENGTH;
  4028. sys_in.ipa_ep_cfg.hdr_ext.hdr_little_endian = false;
  4029. sys_in.ipa_ep_cfg.hdr_ext.hdr_total_len_or_pad_valid = true;
  4030. sys_in.ipa_ep_cfg.hdr_ext.hdr_total_len_or_pad = IPA_HDR_PAD;
  4031. sys_in.ipa_ep_cfg.hdr_ext.hdr_payload_len_inc_padding = false;
  4032. sys_in.ipa_ep_cfg.hdr_ext.hdr_total_len_or_pad_offset = 0;
  4033. sys_in.ipa_ep_cfg.hdr_ext.hdr_pad_to_alignment = 2;
  4034. sys_in.ipa_ep_cfg.cfg.cs_offload_en = IPA_DISABLE_CS_OFFLOAD;
  4035. /**
  4036. * ipa_lan_rx_cb() intended to notify the source EP about packet
  4037. * being received on the LAN_CONS via calling the source EP call-back.
  4038. * There could be a race condition with calling this call-back. Other
  4039. * thread may nullify it - e.g. on EP disconnect.
  4040. * This lock intended to protect the access to the source EP call-back
  4041. */
  4042. spin_lock_init(&ipa3_ctx->disconnect_lock);
  4043. if (ipa3_setup_sys_pipe(&sys_in, &ipa3_ctx->clnt_hdl_data_in)) {
  4044. IPAERR(":setup sys pipe (LAN_CONS) failed.\n");
  4045. result = -EPERM;
  4046. goto fail_flt_hash_tuple;
  4047. }
  4048. /* LAN OUT (AP->IPA) */
  4049. if (!ipa3_ctx->ipa_config_is_mhi) {
  4050. memset(&sys_in, 0, sizeof(struct ipa_sys_connect_params));
  4051. sys_in.client = IPA_CLIENT_APPS_LAN_PROD;
  4052. sys_in.desc_fifo_sz = IPA_SYS_TX_DATA_DESC_FIFO_SZ;
  4053. sys_in.ipa_ep_cfg.mode.mode = IPA_BASIC;
  4054. if (ipa3_setup_sys_pipe(&sys_in,
  4055. &ipa3_ctx->clnt_hdl_data_out)) {
  4056. IPAERR(":setup sys pipe (LAN_PROD) failed.\n");
  4057. result = -EPERM;
  4058. goto fail_lan_data_out;
  4059. }
  4060. }
  4061. return 0;
  4062. fail_lan_data_out:
  4063. ipa3_teardown_sys_pipe(ipa3_ctx->clnt_hdl_data_in);
  4064. fail_flt_hash_tuple:
  4065. if (ipa3_ctx->dflt_v6_rt_rule_hdl)
  4066. __ipa3_del_rt_rule(ipa3_ctx->dflt_v6_rt_rule_hdl);
  4067. if (ipa3_ctx->dflt_v4_rt_rule_hdl)
  4068. __ipa3_del_rt_rule(ipa3_ctx->dflt_v4_rt_rule_hdl);
  4069. if (ipa3_ctx->excp_hdr_hdl)
  4070. __ipa3_del_hdr(ipa3_ctx->excp_hdr_hdl, false);
  4071. ipa3_teardown_sys_pipe(ipa3_ctx->clnt_hdl_cmd);
  4072. fail_ch20_wa:
  4073. return result;
  4074. }
  4075. static void ipa3_teardown_apps_pipes(void)
  4076. {
  4077. if (!ipa3_ctx->ipa_config_is_mhi)
  4078. ipa3_teardown_sys_pipe(ipa3_ctx->clnt_hdl_data_out);
  4079. ipa3_teardown_sys_pipe(ipa3_ctx->clnt_hdl_data_in);
  4080. __ipa3_del_rt_rule(ipa3_ctx->dflt_v6_rt_rule_hdl);
  4081. __ipa3_del_rt_rule(ipa3_ctx->dflt_v4_rt_rule_hdl);
  4082. __ipa3_del_hdr(ipa3_ctx->excp_hdr_hdl, false);
  4083. ipa3_teardown_sys_pipe(ipa3_ctx->clnt_hdl_cmd);
  4084. }
  4085. #ifdef CONFIG_COMPAT
  4086. static long compat_ipa3_nat_ipv6ct_alloc_table(unsigned long arg,
  4087. int (alloc_func)(struct ipa_ioc_nat_ipv6ct_table_alloc *))
  4088. {
  4089. long retval;
  4090. struct ipa_ioc_nat_ipv6ct_table_alloc32 table_alloc32;
  4091. struct ipa_ioc_nat_ipv6ct_table_alloc table_alloc;
  4092. retval = copy_from_user(&table_alloc32, (const void __user *)arg,
  4093. sizeof(struct ipa_ioc_nat_ipv6ct_table_alloc32));
  4094. if (retval)
  4095. return retval;
  4096. table_alloc.size = (size_t)table_alloc32.size;
  4097. table_alloc.offset = (off_t)table_alloc32.offset;
  4098. retval = alloc_func(&table_alloc);
  4099. if (retval)
  4100. return retval;
  4101. if (table_alloc.offset) {
  4102. table_alloc32.offset = (compat_off_t)table_alloc.offset;
  4103. retval = copy_to_user((void __user *)arg, &table_alloc32,
  4104. sizeof(struct ipa_ioc_nat_ipv6ct_table_alloc32));
  4105. }
  4106. return retval;
  4107. }
  4108. long compat_ipa3_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  4109. {
  4110. long retval = 0;
  4111. struct ipa3_ioc_nat_alloc_mem32 nat_mem32;
  4112. struct ipa_ioc_nat_alloc_mem nat_mem;
  4113. switch (cmd) {
  4114. case IPA_IOC_ADD_HDR32:
  4115. cmd = IPA_IOC_ADD_HDR;
  4116. break;
  4117. case IPA_IOC_DEL_HDR32:
  4118. cmd = IPA_IOC_DEL_HDR;
  4119. break;
  4120. case IPA_IOC_ADD_RT_RULE32:
  4121. cmd = IPA_IOC_ADD_RT_RULE;
  4122. break;
  4123. case IPA_IOC_DEL_RT_RULE32:
  4124. cmd = IPA_IOC_DEL_RT_RULE;
  4125. break;
  4126. case IPA_IOC_ADD_FLT_RULE32:
  4127. cmd = IPA_IOC_ADD_FLT_RULE;
  4128. break;
  4129. case IPA_IOC_DEL_FLT_RULE32:
  4130. cmd = IPA_IOC_DEL_FLT_RULE;
  4131. break;
  4132. case IPA_IOC_GET_RT_TBL32:
  4133. cmd = IPA_IOC_GET_RT_TBL;
  4134. break;
  4135. case IPA_IOC_COPY_HDR32:
  4136. cmd = IPA_IOC_COPY_HDR;
  4137. break;
  4138. case IPA_IOC_QUERY_INTF32:
  4139. cmd = IPA_IOC_QUERY_INTF;
  4140. break;
  4141. case IPA_IOC_QUERY_INTF_TX_PROPS32:
  4142. cmd = IPA_IOC_QUERY_INTF_TX_PROPS;
  4143. break;
  4144. case IPA_IOC_QUERY_INTF_RX_PROPS32:
  4145. cmd = IPA_IOC_QUERY_INTF_RX_PROPS;
  4146. break;
  4147. case IPA_IOC_QUERY_INTF_EXT_PROPS32:
  4148. cmd = IPA_IOC_QUERY_INTF_EXT_PROPS;
  4149. break;
  4150. case IPA_IOC_GET_HDR32:
  4151. cmd = IPA_IOC_GET_HDR;
  4152. break;
  4153. case IPA_IOC_ALLOC_NAT_MEM32:
  4154. retval = copy_from_user(&nat_mem32, (const void __user *)arg,
  4155. sizeof(struct ipa3_ioc_nat_alloc_mem32));
  4156. if (retval)
  4157. return retval;
  4158. memcpy(nat_mem.dev_name, nat_mem32.dev_name,
  4159. IPA_RESOURCE_NAME_MAX);
  4160. nat_mem.size = (size_t)nat_mem32.size;
  4161. nat_mem.offset = (off_t)nat_mem32.offset;
  4162. /* null terminate the string */
  4163. nat_mem.dev_name[IPA_RESOURCE_NAME_MAX - 1] = '\0';
  4164. retval = ipa3_allocate_nat_device(&nat_mem);
  4165. if (retval)
  4166. return retval;
  4167. nat_mem32.offset = (compat_off_t)nat_mem.offset;
  4168. retval = copy_to_user((void __user *)arg, &nat_mem32,
  4169. sizeof(struct ipa3_ioc_nat_alloc_mem32));
  4170. return retval;
  4171. case IPA_IOC_ALLOC_NAT_TABLE32:
  4172. return compat_ipa3_nat_ipv6ct_alloc_table(arg,
  4173. ipa3_allocate_nat_table);
  4174. case IPA_IOC_ALLOC_IPV6CT_TABLE32:
  4175. return compat_ipa3_nat_ipv6ct_alloc_table(arg,
  4176. ipa3_allocate_ipv6ct_table);
  4177. case IPA_IOC_V4_INIT_NAT32:
  4178. cmd = IPA_IOC_V4_INIT_NAT;
  4179. break;
  4180. case IPA_IOC_INIT_IPV6CT_TABLE32:
  4181. cmd = IPA_IOC_INIT_IPV6CT_TABLE;
  4182. break;
  4183. case IPA_IOC_TABLE_DMA_CMD32:
  4184. cmd = IPA_IOC_TABLE_DMA_CMD;
  4185. break;
  4186. case IPA_IOC_V4_DEL_NAT32:
  4187. cmd = IPA_IOC_V4_DEL_NAT;
  4188. break;
  4189. case IPA_IOC_DEL_NAT_TABLE32:
  4190. cmd = IPA_IOC_DEL_NAT_TABLE;
  4191. break;
  4192. case IPA_IOC_DEL_IPV6CT_TABLE32:
  4193. cmd = IPA_IOC_DEL_IPV6CT_TABLE;
  4194. break;
  4195. case IPA_IOC_NAT_MODIFY_PDN32:
  4196. cmd = IPA_IOC_NAT_MODIFY_PDN;
  4197. break;
  4198. case IPA_IOC_GET_NAT_OFFSET32:
  4199. cmd = IPA_IOC_GET_NAT_OFFSET;
  4200. break;
  4201. case IPA_IOC_PULL_MSG32:
  4202. cmd = IPA_IOC_PULL_MSG;
  4203. break;
  4204. case IPA_IOC_RM_ADD_DEPENDENCY32:
  4205. cmd = IPA_IOC_RM_ADD_DEPENDENCY;
  4206. break;
  4207. case IPA_IOC_RM_DEL_DEPENDENCY32:
  4208. cmd = IPA_IOC_RM_DEL_DEPENDENCY;
  4209. break;
  4210. case IPA_IOC_GENERATE_FLT_EQ32:
  4211. cmd = IPA_IOC_GENERATE_FLT_EQ;
  4212. break;
  4213. case IPA_IOC_QUERY_RT_TBL_INDEX32:
  4214. cmd = IPA_IOC_QUERY_RT_TBL_INDEX;
  4215. break;
  4216. case IPA_IOC_WRITE_QMAPID32:
  4217. cmd = IPA_IOC_WRITE_QMAPID;
  4218. break;
  4219. case IPA_IOC_MDFY_FLT_RULE32:
  4220. cmd = IPA_IOC_MDFY_FLT_RULE;
  4221. break;
  4222. case IPA_IOC_NOTIFY_WAN_UPSTREAM_ROUTE_ADD32:
  4223. cmd = IPA_IOC_NOTIFY_WAN_UPSTREAM_ROUTE_ADD;
  4224. break;
  4225. case IPA_IOC_NOTIFY_WAN_UPSTREAM_ROUTE_DEL32:
  4226. cmd = IPA_IOC_NOTIFY_WAN_UPSTREAM_ROUTE_DEL;
  4227. break;
  4228. case IPA_IOC_NOTIFY_WAN_EMBMS_CONNECTED32:
  4229. cmd = IPA_IOC_NOTIFY_WAN_EMBMS_CONNECTED;
  4230. break;
  4231. case IPA_IOC_MDFY_RT_RULE32:
  4232. cmd = IPA_IOC_MDFY_RT_RULE;
  4233. break;
  4234. case IPA_IOC_GET_NAT_IN_SRAM_INFO32:
  4235. cmd = IPA_IOC_GET_NAT_IN_SRAM_INFO;
  4236. break;
  4237. case IPA_IOC_APP_CLOCK_VOTE32:
  4238. cmd = IPA_IOC_APP_CLOCK_VOTE;
  4239. break;
  4240. case IPA_IOC_COMMIT_HDR:
  4241. case IPA_IOC_RESET_HDR:
  4242. case IPA_IOC_COMMIT_RT:
  4243. case IPA_IOC_RESET_RT:
  4244. case IPA_IOC_COMMIT_FLT:
  4245. case IPA_IOC_RESET_FLT:
  4246. case IPA_IOC_DUMP:
  4247. case IPA_IOC_PUT_RT_TBL:
  4248. case IPA_IOC_PUT_HDR:
  4249. case IPA_IOC_SET_FLT:
  4250. case IPA_IOC_QUERY_EP_MAPPING:
  4251. break;
  4252. default:
  4253. return -ENOIOCTLCMD;
  4254. }
  4255. return ipa3_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
  4256. }
  4257. #endif
  4258. static ssize_t ipa3_write(struct file *file, const char __user *buf,
  4259. size_t count, loff_t *ppos);
  4260. static const struct file_operations ipa3_drv_fops = {
  4261. .owner = THIS_MODULE,
  4262. .open = ipa3_open,
  4263. .read = ipa3_read,
  4264. .write = ipa3_write,
  4265. .unlocked_ioctl = ipa3_ioctl,
  4266. #ifdef CONFIG_COMPAT
  4267. .compat_ioctl = compat_ipa3_ioctl,
  4268. #endif
  4269. };
  4270. static int ipa3_get_clks(struct device *dev)
  4271. {
  4272. if (ipa3_ctx->ipa3_hw_mode != IPA_HW_MODE_NORMAL) {
  4273. IPADBG("not supported in this HW mode\n");
  4274. ipa3_clk = NULL;
  4275. return 0;
  4276. }
  4277. if (ipa3_res.use_bw_vote) {
  4278. IPADBG("Vote IPA clock by bw voting via bus scaling driver\n");
  4279. ipa3_clk = NULL;
  4280. return 0;
  4281. }
  4282. ipa3_clk = clk_get(dev, "core_clk");
  4283. if (IS_ERR(ipa3_clk)) {
  4284. if (ipa3_clk != ERR_PTR(-EPROBE_DEFER))
  4285. IPAERR("fail to get ipa clk\n");
  4286. return PTR_ERR(ipa3_clk);
  4287. }
  4288. return 0;
  4289. }
  4290. /**
  4291. * _ipa_enable_clks_v3_0() - Enable IPA clocks.
  4292. */
  4293. void _ipa_enable_clks_v3_0(void)
  4294. {
  4295. IPADBG_LOW("curr_ipa_clk_rate=%d", ipa3_ctx->curr_ipa_clk_rate);
  4296. if (ipa3_clk) {
  4297. IPADBG_LOW("enabling gcc_ipa_clk\n");
  4298. clk_prepare(ipa3_clk);
  4299. clk_enable(ipa3_clk);
  4300. clk_set_rate(ipa3_clk, ipa3_ctx->curr_ipa_clk_rate);
  4301. }
  4302. ipa3_uc_notify_clk_state(true);
  4303. }
  4304. static unsigned int ipa3_get_bus_vote(void)
  4305. {
  4306. unsigned int idx = 1;
  4307. if (ipa3_ctx->curr_ipa_clk_rate == ipa3_ctx->ctrl->ipa_clk_rate_svs2) {
  4308. idx = 1;
  4309. } else if (ipa3_ctx->curr_ipa_clk_rate ==
  4310. ipa3_ctx->ctrl->ipa_clk_rate_svs) {
  4311. idx = 2;
  4312. } else if (ipa3_ctx->curr_ipa_clk_rate ==
  4313. ipa3_ctx->ctrl->ipa_clk_rate_nominal) {
  4314. idx = 3;
  4315. } else if (ipa3_ctx->curr_ipa_clk_rate ==
  4316. ipa3_ctx->ctrl->ipa_clk_rate_turbo) {
  4317. idx = 4;
  4318. } else {
  4319. WARN(1, "unexpected clock rate");
  4320. }
  4321. IPADBG_LOW("curr %d idx %d\n", ipa3_ctx->curr_ipa_clk_rate, idx);
  4322. return idx;
  4323. }
  4324. /**
  4325. * ipa3_enable_clks() - Turn on IPA clocks
  4326. *
  4327. * Return codes:
  4328. * None
  4329. */
  4330. void ipa3_enable_clks(void)
  4331. {
  4332. int idx;
  4333. int i;
  4334. if (ipa3_ctx->ipa3_hw_mode != IPA_HW_MODE_NORMAL) {
  4335. IPAERR("not supported in this mode\n");
  4336. return;
  4337. }
  4338. IPADBG("enabling IPA clocks and bus voting\n");
  4339. idx = ipa3_get_bus_vote();
  4340. for (i = 0; i < ipa3_ctx->icc_num_paths; i++) {
  4341. if (ipa3_ctx->ctrl->icc_path[i] &&
  4342. icc_set_bw(
  4343. ipa3_ctx->ctrl->icc_path[i],
  4344. ipa3_ctx->icc_clk[idx][i][IPA_ICC_AB],
  4345. ipa3_ctx->icc_clk[idx][i][IPA_ICC_IB]))
  4346. WARN(1, "path %d bus scaling failed", i);
  4347. }
  4348. ipa3_ctx->ctrl->ipa3_enable_clks();
  4349. atomic_set(&ipa3_ctx->ipa_clk_vote, 1);
  4350. }
  4351. /**
  4352. * _ipa_disable_clks_v3_0() - Disable IPA clocks.
  4353. */
  4354. void _ipa_disable_clks_v3_0(void)
  4355. {
  4356. ipa3_uc_notify_clk_state(false);
  4357. if (ipa3_clk) {
  4358. IPADBG_LOW("disabling gcc_ipa_clk\n");
  4359. clk_disable_unprepare(ipa3_clk);
  4360. }
  4361. }
  4362. /**
  4363. * ipa3_disable_clks() - Turn off IPA clocks
  4364. *
  4365. * Return codes:
  4366. * None
  4367. */
  4368. void ipa3_disable_clks(void)
  4369. {
  4370. int i;
  4371. if (ipa3_ctx->ipa3_hw_mode != IPA_HW_MODE_NORMAL) {
  4372. IPAERR("not supported in this mode\n");
  4373. return;
  4374. }
  4375. IPADBG("disabling IPA clocks and bus voting\n");
  4376. ipa3_ctx->ctrl->ipa3_disable_clks();
  4377. ipa_pm_set_clock_index(0);
  4378. for (i = 0; i < ipa3_ctx->icc_num_paths; i++) {
  4379. if (ipa3_ctx->ctrl->icc_path[i] &&
  4380. icc_set_bw(
  4381. ipa3_ctx->ctrl->icc_path[i],
  4382. ipa3_ctx->icc_clk[IPA_ICC_NONE][i][IPA_ICC_AB],
  4383. ipa3_ctx->icc_clk[IPA_ICC_NONE][i][IPA_ICC_IB]))
  4384. WARN(1, "path %d bus off failed", i);
  4385. }
  4386. atomic_set(&ipa3_ctx->ipa_clk_vote, 0);
  4387. }
  4388. /**
  4389. * ipa3_start_tag_process() - Send TAG packet and wait for it to come back
  4390. *
  4391. * This function is called prior to clock gating when active client counter
  4392. * is 1. TAG process ensures that there are no packets inside IPA HW that
  4393. * were not submitted to the IPA client via the transport. During TAG process
  4394. * all aggregation frames are (force) closed.
  4395. *
  4396. * Return codes:
  4397. * None
  4398. */
  4399. static void ipa3_start_tag_process(struct work_struct *work)
  4400. {
  4401. int res;
  4402. IPADBG("starting TAG process\n");
  4403. /* close aggregation frames on all pipes */
  4404. res = ipa3_tag_aggr_force_close(-1);
  4405. if (res)
  4406. IPAERR("ipa3_tag_aggr_force_close failed %d\n", res);
  4407. IPA_ACTIVE_CLIENTS_DEC_SPECIAL("TAG_PROCESS");
  4408. IPADBG("TAG process done\n");
  4409. }
  4410. /**
  4411. * ipa3_active_clients_log_mod() - Log a modification in the active clients
  4412. * reference count
  4413. *
  4414. * This method logs any modification in the active clients reference count:
  4415. * It logs the modification in the circular history buffer
  4416. * It logs the modification in the hash table - looking for an entry,
  4417. * creating one if needed and deleting one if needed.
  4418. *
  4419. * @id: ipa3_active client logging info struct to hold the log information
  4420. * @inc: a boolean variable to indicate whether the modification is an increase
  4421. * or decrease
  4422. * @int_ctx: a boolean variable to indicate whether this call is being made from
  4423. * an interrupt context and therefore should allocate GFP_ATOMIC memory
  4424. *
  4425. * Method process:
  4426. * - Hash the unique identifier string
  4427. * - Find the hash in the table
  4428. * 1)If found, increase or decrease the reference count
  4429. * 2)If not found, allocate a new hash table entry struct and initialize it
  4430. * - Remove and deallocate unneeded data structure
  4431. * - Log the call in the circular history buffer (unless it is a simple call)
  4432. */
  4433. #ifdef CONFIG_IPA_DEBUG
  4434. static void ipa3_active_clients_log_mod(
  4435. struct ipa_active_client_logging_info *id,
  4436. bool inc, bool int_ctx)
  4437. {
  4438. char temp_str[IPA3_ACTIVE_CLIENTS_LOG_LINE_LEN];
  4439. unsigned long long t;
  4440. unsigned long nanosec_rem;
  4441. struct ipa3_active_client_htable_entry *hentry;
  4442. struct ipa3_active_client_htable_entry *hfound;
  4443. u32 hkey;
  4444. char str_to_hash[IPA3_ACTIVE_CLIENTS_LOG_NAME_LEN];
  4445. unsigned long flags;
  4446. spin_lock_irqsave(&ipa3_ctx->ipa3_active_clients_logging.lock, flags);
  4447. int_ctx = true;
  4448. hfound = NULL;
  4449. memset(str_to_hash, 0, IPA3_ACTIVE_CLIENTS_LOG_NAME_LEN);
  4450. strlcpy(str_to_hash, id->id_string, IPA3_ACTIVE_CLIENTS_LOG_NAME_LEN);
  4451. hkey = jhash(str_to_hash, IPA3_ACTIVE_CLIENTS_LOG_NAME_LEN,
  4452. 0);
  4453. hash_for_each_possible(ipa3_ctx->ipa3_active_clients_logging.htable,
  4454. hentry, list, hkey) {
  4455. if (!strcmp(hentry->id_string, id->id_string)) {
  4456. hentry->count = hentry->count + (inc ? 1 : -1);
  4457. hfound = hentry;
  4458. }
  4459. }
  4460. if (hfound == NULL) {
  4461. hentry = NULL;
  4462. hentry = kzalloc(sizeof(
  4463. struct ipa3_active_client_htable_entry),
  4464. int_ctx ? GFP_ATOMIC : GFP_KERNEL);
  4465. if (hentry == NULL) {
  4466. spin_unlock_irqrestore(
  4467. &ipa3_ctx->ipa3_active_clients_logging.lock,
  4468. flags);
  4469. return;
  4470. }
  4471. hentry->type = id->type;
  4472. strlcpy(hentry->id_string, id->id_string,
  4473. IPA3_ACTIVE_CLIENTS_LOG_NAME_LEN);
  4474. INIT_HLIST_NODE(&hentry->list);
  4475. hentry->count = inc ? 1 : -1;
  4476. hash_add(ipa3_ctx->ipa3_active_clients_logging.htable,
  4477. &hentry->list, hkey);
  4478. } else if (hfound->count == 0) {
  4479. hash_del(&hfound->list);
  4480. kfree(hfound);
  4481. }
  4482. if (id->type != SIMPLE) {
  4483. t = local_clock();
  4484. nanosec_rem = do_div(t, 1000000000) / 1000;
  4485. snprintf(temp_str, IPA3_ACTIVE_CLIENTS_LOG_LINE_LEN,
  4486. inc ? "[%5lu.%06lu] ^ %s, %s: %d" :
  4487. "[%5lu.%06lu] v %s, %s: %d",
  4488. (unsigned long)t, nanosec_rem,
  4489. id->id_string, id->file, id->line);
  4490. ipa3_active_clients_log_insert(temp_str);
  4491. }
  4492. spin_unlock_irqrestore(&ipa3_ctx->ipa3_active_clients_logging.lock,
  4493. flags);
  4494. }
  4495. #else
  4496. static void ipa3_active_clients_log_mod(
  4497. struct ipa_active_client_logging_info *id,
  4498. bool inc, bool int_ctx)
  4499. {
  4500. }
  4501. #endif
  4502. void ipa3_active_clients_log_dec(struct ipa_active_client_logging_info *id,
  4503. bool int_ctx)
  4504. {
  4505. ipa3_active_clients_log_mod(id, false, int_ctx);
  4506. }
  4507. void ipa3_active_clients_log_inc(struct ipa_active_client_logging_info *id,
  4508. bool int_ctx)
  4509. {
  4510. ipa3_active_clients_log_mod(id, true, int_ctx);
  4511. }
  4512. /**
  4513. * ipa3_inc_client_enable_clks() - Increase active clients counter, and
  4514. * enable ipa clocks if necessary
  4515. *
  4516. * Return codes:
  4517. * None
  4518. */
  4519. void ipa3_inc_client_enable_clks(struct ipa_active_client_logging_info *id)
  4520. {
  4521. int ret;
  4522. ipa3_active_clients_log_inc(id, false);
  4523. ret = atomic_inc_not_zero(&ipa3_ctx->ipa3_active_clients.cnt);
  4524. if (ret) {
  4525. IPADBG_LOW("active clients = %d\n",
  4526. atomic_read(&ipa3_ctx->ipa3_active_clients.cnt));
  4527. return;
  4528. }
  4529. mutex_lock(&ipa3_ctx->ipa3_active_clients.mutex);
  4530. /* somebody might voted to clocks meanwhile */
  4531. ret = atomic_inc_not_zero(&ipa3_ctx->ipa3_active_clients.cnt);
  4532. if (ret) {
  4533. mutex_unlock(&ipa3_ctx->ipa3_active_clients.mutex);
  4534. IPADBG_LOW("active clients = %d\n",
  4535. atomic_read(&ipa3_ctx->ipa3_active_clients.cnt));
  4536. return;
  4537. }
  4538. ipa3_enable_clks();
  4539. ipa3_suspend_apps_pipes(false);
  4540. atomic_inc(&ipa3_ctx->ipa3_active_clients.cnt);
  4541. IPADBG_LOW("active clients = %d\n",
  4542. atomic_read(&ipa3_ctx->ipa3_active_clients.cnt));
  4543. mutex_unlock(&ipa3_ctx->ipa3_active_clients.mutex);
  4544. }
  4545. /**
  4546. * ipa3_active_clks_status() - update the current msm bus clock vote
  4547. * status
  4548. */
  4549. int ipa3_active_clks_status(void)
  4550. {
  4551. return atomic_read(&ipa3_ctx->ipa_clk_vote);
  4552. }
  4553. /**
  4554. * ipa3_inc_client_enable_clks_no_block() - Only increment the number of active
  4555. * clients if no asynchronous actions should be done. Asynchronous actions are
  4556. * locking a mutex and waking up IPA HW.
  4557. *
  4558. * Return codes: 0 for success
  4559. * -EPERM if an asynchronous action should have been done
  4560. */
  4561. int ipa3_inc_client_enable_clks_no_block(struct ipa_active_client_logging_info
  4562. *id)
  4563. {
  4564. int ret;
  4565. ret = atomic_inc_not_zero(&ipa3_ctx->ipa3_active_clients.cnt);
  4566. if (ret) {
  4567. ipa3_active_clients_log_inc(id, true);
  4568. IPADBG_LOW("active clients = %d\n",
  4569. atomic_read(&ipa3_ctx->ipa3_active_clients.cnt));
  4570. return 0;
  4571. }
  4572. return -EPERM;
  4573. }
  4574. static void __ipa3_dec_client_disable_clks(void)
  4575. {
  4576. int ret;
  4577. if (!atomic_read(&ipa3_ctx->ipa3_active_clients.cnt)) {
  4578. IPAERR("trying to disable clocks with refcnt is 0\n");
  4579. ipa_assert();
  4580. return;
  4581. }
  4582. ret = atomic_add_unless(&ipa3_ctx->ipa3_active_clients.cnt, -1, 1);
  4583. if (ret)
  4584. goto bail;
  4585. /* Send force close coalsecing frame command in LPM mode before taking
  4586. * mutex lock and otherwise observing race condition.
  4587. */
  4588. if (atomic_read(&ipa3_ctx->ipa3_active_clients.cnt) == 1 &&
  4589. !ipa3_ctx->tag_process_before_gating) {
  4590. ipa3_force_close_coal();
  4591. /* While sending force close command setting
  4592. * tag process as true to make configure to
  4593. * original state
  4594. */
  4595. ipa3_ctx->tag_process_before_gating = false;
  4596. }
  4597. /* seems like this is the only client holding the clocks */
  4598. mutex_lock(&ipa3_ctx->ipa3_active_clients.mutex);
  4599. if (atomic_read(&ipa3_ctx->ipa3_active_clients.cnt) == 1 &&
  4600. ipa3_ctx->tag_process_before_gating) {
  4601. ipa3_ctx->tag_process_before_gating = false;
  4602. /*
  4603. * When TAG process ends, active clients will be
  4604. * decreased
  4605. */
  4606. queue_work(ipa3_ctx->power_mgmt_wq, &ipa3_tag_work);
  4607. goto unlock_mutex;
  4608. }
  4609. /* a different context might increase the clock reference meanwhile */
  4610. ret = atomic_sub_return(1, &ipa3_ctx->ipa3_active_clients.cnt);
  4611. if (ret > 0)
  4612. goto unlock_mutex;
  4613. ret = ipa3_suspend_apps_pipes(true);
  4614. if (ret) {
  4615. /* HW is busy, retry after some time */
  4616. atomic_inc(&ipa3_ctx->ipa3_active_clients.cnt);
  4617. queue_delayed_work(ipa3_ctx->power_mgmt_wq,
  4618. &ipa_dec_clients_disable_clks_on_wq_work,
  4619. IPA_SUSPEND_BUSY_TIMEOUT);
  4620. } else {
  4621. ipa3_disable_clks();
  4622. }
  4623. unlock_mutex:
  4624. mutex_unlock(&ipa3_ctx->ipa3_active_clients.mutex);
  4625. bail:
  4626. IPADBG_LOW("active clients = %d\n",
  4627. atomic_read(&ipa3_ctx->ipa3_active_clients.cnt));
  4628. }
  4629. /**
  4630. * ipa3_dec_client_disable_clks() - Decrease active clients counter
  4631. *
  4632. * In case that there are no active clients this function also starts
  4633. * TAG process. When TAG progress ends ipa clocks will be gated.
  4634. * start_tag_process_again flag is set during this function to signal TAG
  4635. * process to start again as there was another client that may send data to ipa
  4636. *
  4637. * Return codes:
  4638. * None
  4639. */
  4640. void ipa3_dec_client_disable_clks(struct ipa_active_client_logging_info *id)
  4641. {
  4642. ipa3_active_clients_log_dec(id, false);
  4643. __ipa3_dec_client_disable_clks();
  4644. }
  4645. static void ipa_dec_clients_disable_clks_on_wq(struct work_struct *work)
  4646. {
  4647. __ipa3_dec_client_disable_clks();
  4648. }
  4649. /**
  4650. * ipa3_dec_client_disable_clks_no_block() - Decrease active clients counter
  4651. * if possible without blocking. If this is the last client then the desrease
  4652. * will happen from work queue context.
  4653. *
  4654. * Return codes:
  4655. * None
  4656. */
  4657. void ipa3_dec_client_disable_clks_no_block(
  4658. struct ipa_active_client_logging_info *id)
  4659. {
  4660. int ret;
  4661. ipa3_active_clients_log_dec(id, true);
  4662. ret = atomic_add_unless(&ipa3_ctx->ipa3_active_clients.cnt, -1, 1);
  4663. if (ret) {
  4664. IPADBG_LOW("active clients = %d\n",
  4665. atomic_read(&ipa3_ctx->ipa3_active_clients.cnt));
  4666. return;
  4667. }
  4668. /* seems like this is the only client holding the clocks */
  4669. queue_delayed_work(ipa3_ctx->power_mgmt_wq,
  4670. &ipa_dec_clients_disable_clks_on_wq_work, 0);
  4671. }
  4672. /**
  4673. * ipa3_inc_acquire_wakelock() - Increase active clients counter, and
  4674. * acquire wakelock if necessary
  4675. *
  4676. * Return codes:
  4677. * None
  4678. */
  4679. void ipa3_inc_acquire_wakelock(void)
  4680. {
  4681. unsigned long flags;
  4682. spin_lock_irqsave(&ipa3_ctx->wakelock_ref_cnt.spinlock, flags);
  4683. ipa3_ctx->wakelock_ref_cnt.cnt++;
  4684. if (ipa3_ctx->wakelock_ref_cnt.cnt == 1)
  4685. __pm_stay_awake(ipa3_ctx->w_lock);
  4686. IPADBG_LOW("active wakelock ref cnt = %d\n",
  4687. ipa3_ctx->wakelock_ref_cnt.cnt);
  4688. spin_unlock_irqrestore(&ipa3_ctx->wakelock_ref_cnt.spinlock, flags);
  4689. }
  4690. /**
  4691. * ipa3_dec_release_wakelock() - Decrease active clients counter
  4692. *
  4693. * In case if the ref count is 0, release the wakelock.
  4694. *
  4695. * Return codes:
  4696. * None
  4697. */
  4698. void ipa3_dec_release_wakelock(void)
  4699. {
  4700. unsigned long flags;
  4701. spin_lock_irqsave(&ipa3_ctx->wakelock_ref_cnt.spinlock, flags);
  4702. ipa3_ctx->wakelock_ref_cnt.cnt--;
  4703. IPADBG_LOW("active wakelock ref cnt = %d\n",
  4704. ipa3_ctx->wakelock_ref_cnt.cnt);
  4705. if (ipa3_ctx->wakelock_ref_cnt.cnt == 0)
  4706. __pm_relax(ipa3_ctx->w_lock);
  4707. spin_unlock_irqrestore(&ipa3_ctx->wakelock_ref_cnt.spinlock, flags);
  4708. }
  4709. int ipa3_set_clock_plan_from_pm(int idx)
  4710. {
  4711. u32 clk_rate;
  4712. int i;
  4713. IPADBG_LOW("idx = %d\n", idx);
  4714. if (!ipa3_ctx->enable_clock_scaling) {
  4715. ipa3_ctx->ipa3_active_clients.bus_vote_idx = idx;
  4716. return 0;
  4717. }
  4718. if (ipa3_ctx->ipa3_hw_mode != IPA_HW_MODE_NORMAL) {
  4719. IPAERR("not supported in this mode\n");
  4720. return 0;
  4721. }
  4722. if (idx <= 0 || idx >= 5) {
  4723. IPAERR("bad voltage\n");
  4724. return -EINVAL;
  4725. }
  4726. if (idx == 1)
  4727. clk_rate = ipa3_ctx->ctrl->ipa_clk_rate_svs2;
  4728. else if (idx == 2)
  4729. clk_rate = ipa3_ctx->ctrl->ipa_clk_rate_svs;
  4730. else if (idx == 3)
  4731. clk_rate = ipa3_ctx->ctrl->ipa_clk_rate_nominal;
  4732. else if (idx == 4)
  4733. clk_rate = ipa3_ctx->ctrl->ipa_clk_rate_turbo;
  4734. else {
  4735. IPAERR("bad voltage\n");
  4736. WARN_ON(1);
  4737. return -EFAULT;
  4738. }
  4739. if (clk_rate == ipa3_ctx->curr_ipa_clk_rate) {
  4740. IPADBG_LOW("Same voltage\n");
  4741. return 0;
  4742. }
  4743. mutex_lock(&ipa3_ctx->ipa3_active_clients.mutex);
  4744. ipa3_ctx->curr_ipa_clk_rate = clk_rate;
  4745. ipa3_ctx->ipa3_active_clients.bus_vote_idx = idx;
  4746. IPADBG_LOW("setting clock rate to %u\n", ipa3_ctx->curr_ipa_clk_rate);
  4747. if (atomic_read(&ipa3_ctx->ipa3_active_clients.cnt) > 0) {
  4748. if (ipa3_clk)
  4749. clk_set_rate(ipa3_clk, ipa3_ctx->curr_ipa_clk_rate);
  4750. idx = ipa3_get_bus_vote();
  4751. for (i = 0; i < ipa3_ctx->icc_num_paths; i++) {
  4752. if (ipa3_ctx->ctrl->icc_path[i] &&
  4753. icc_set_bw(
  4754. ipa3_ctx->ctrl->icc_path[i],
  4755. ipa3_ctx->icc_clk[idx][i][IPA_ICC_AB],
  4756. ipa3_ctx->icc_clk[idx][i][IPA_ICC_IB])) {
  4757. WARN(1, "path %d bus scaling failed",
  4758. i);
  4759. }
  4760. }
  4761. } else {
  4762. IPADBG_LOW("clocks are gated, not setting rate\n");
  4763. }
  4764. mutex_unlock(&ipa3_ctx->ipa3_active_clients.mutex);
  4765. IPADBG_LOW("Done\n");
  4766. return 0;
  4767. }
  4768. int ipa3_set_required_perf_profile(enum ipa_voltage_level floor_voltage,
  4769. u32 bandwidth_mbps)
  4770. {
  4771. enum ipa_voltage_level needed_voltage;
  4772. u32 clk_rate;
  4773. int i;
  4774. int idx;
  4775. if (ipa3_ctx->ipa3_hw_mode != IPA_HW_MODE_NORMAL) {
  4776. IPAERR("not supported in this mode\n");
  4777. return 0;
  4778. }
  4779. IPADBG_LOW("floor_voltage=%d, bandwidth_mbps=%u",
  4780. floor_voltage, bandwidth_mbps);
  4781. if (floor_voltage < IPA_VOLTAGE_UNSPECIFIED ||
  4782. floor_voltage >= IPA_VOLTAGE_MAX) {
  4783. IPAERR("bad voltage\n");
  4784. return -EINVAL;
  4785. }
  4786. if (ipa3_ctx->enable_clock_scaling) {
  4787. IPADBG_LOW("Clock scaling is enabled\n");
  4788. if (bandwidth_mbps >=
  4789. ipa3_ctx->ctrl->clock_scaling_bw_threshold_turbo)
  4790. needed_voltage = IPA_VOLTAGE_TURBO;
  4791. else if (bandwidth_mbps >=
  4792. ipa3_ctx->ctrl->clock_scaling_bw_threshold_nominal)
  4793. needed_voltage = IPA_VOLTAGE_NOMINAL;
  4794. else if (bandwidth_mbps >=
  4795. ipa3_ctx->ctrl->clock_scaling_bw_threshold_svs)
  4796. needed_voltage = IPA_VOLTAGE_SVS;
  4797. else
  4798. needed_voltage = IPA_VOLTAGE_SVS2;
  4799. } else {
  4800. IPADBG_LOW("Clock scaling is disabled\n");
  4801. needed_voltage = IPA_VOLTAGE_NOMINAL;
  4802. }
  4803. needed_voltage = max(needed_voltage, floor_voltage);
  4804. switch (needed_voltage) {
  4805. case IPA_VOLTAGE_SVS2:
  4806. clk_rate = ipa3_ctx->ctrl->ipa_clk_rate_svs2;
  4807. break;
  4808. case IPA_VOLTAGE_SVS:
  4809. clk_rate = ipa3_ctx->ctrl->ipa_clk_rate_svs;
  4810. break;
  4811. case IPA_VOLTAGE_NOMINAL:
  4812. clk_rate = ipa3_ctx->ctrl->ipa_clk_rate_nominal;
  4813. break;
  4814. case IPA_VOLTAGE_TURBO:
  4815. clk_rate = ipa3_ctx->ctrl->ipa_clk_rate_turbo;
  4816. break;
  4817. default:
  4818. IPAERR("bad voltage\n");
  4819. WARN_ON(1);
  4820. return -EFAULT;
  4821. }
  4822. if (clk_rate == ipa3_ctx->curr_ipa_clk_rate) {
  4823. IPADBG_LOW("Same voltage\n");
  4824. return 0;
  4825. }
  4826. /* Hold the mutex to avoid race conditions with ipa3_enable_clocks() */
  4827. mutex_lock(&ipa3_ctx->ipa3_active_clients.mutex);
  4828. ipa3_ctx->curr_ipa_clk_rate = clk_rate;
  4829. IPADBG_LOW("setting clock rate to %u\n", ipa3_ctx->curr_ipa_clk_rate);
  4830. if (atomic_read(&ipa3_ctx->ipa3_active_clients.cnt) > 0) {
  4831. if (ipa3_clk)
  4832. clk_set_rate(ipa3_clk, ipa3_ctx->curr_ipa_clk_rate);
  4833. idx = ipa3_get_bus_vote();
  4834. for (i = 0; i < ipa3_ctx->icc_num_paths; i++) {
  4835. if (ipa3_ctx->ctrl->icc_path[i] &&
  4836. icc_set_bw(
  4837. ipa3_ctx->ctrl->icc_path[i],
  4838. ipa3_ctx->icc_clk[idx][i][IPA_ICC_AB],
  4839. ipa3_ctx->icc_clk[idx][i][IPA_ICC_IB]))
  4840. WARN(1, "path %d bus scaling failed", i);
  4841. }
  4842. } else {
  4843. IPADBG_LOW("clocks are gated, not setting rate\n");
  4844. }
  4845. mutex_unlock(&ipa3_ctx->ipa3_active_clients.mutex);
  4846. IPADBG_LOW("Done\n");
  4847. return 0;
  4848. }
  4849. static void ipa3_process_irq_schedule_rel(void)
  4850. {
  4851. queue_delayed_work(ipa3_ctx->transport_power_mgmt_wq,
  4852. &ipa3_transport_release_resource_work,
  4853. msecs_to_jiffies(IPA_TRANSPORT_PROD_TIMEOUT_MSEC));
  4854. }
  4855. /**
  4856. * ipa3_suspend_handler() - Handles the suspend interrupt:
  4857. * wakes up the suspended peripheral by requesting its consumer
  4858. * @interrupt: Interrupt type
  4859. * @private_data: The client's private data
  4860. * @interrupt_data: Interrupt specific information data
  4861. */
  4862. void ipa3_suspend_handler(enum ipa_irq_type interrupt,
  4863. void *private_data,
  4864. void *interrupt_data)
  4865. {
  4866. u32 suspend_data =
  4867. ((struct ipa_tx_suspend_irq_data *)interrupt_data)->endpoints;
  4868. u32 bmsk = 1;
  4869. u32 i = 0;
  4870. int res;
  4871. struct ipa_ep_cfg_holb holb_cfg;
  4872. u32 pipe_bitmask = 0;
  4873. IPADBG("interrupt=%d, interrupt_data=%u\n",
  4874. interrupt, suspend_data);
  4875. memset(&holb_cfg, 0, sizeof(holb_cfg));
  4876. for (i = 0; i < ipa3_ctx->ipa_num_pipes; i++, bmsk = bmsk << 1)
  4877. if ((suspend_data & bmsk) && (ipa3_ctx->ep[i].valid))
  4878. pipe_bitmask |= bmsk;
  4879. res = ipa_pm_handle_suspend(pipe_bitmask);
  4880. if (res) {
  4881. IPAERR("ipa_pm_handle_suspend failed %d\n", res);
  4882. return;
  4883. }
  4884. }
  4885. /**
  4886. * ipa3_restore_suspend_handler() - restores the original suspend IRQ handler
  4887. * as it was registered in the IPA init sequence.
  4888. * Return codes:
  4889. * 0: success
  4890. * -EPERM: failed to remove current handler or failed to add original handler
  4891. */
  4892. int ipa3_restore_suspend_handler(void)
  4893. {
  4894. int result = 0;
  4895. result = ipa3_remove_interrupt_handler(IPA_TX_SUSPEND_IRQ);
  4896. if (result) {
  4897. IPAERR("remove handler for suspend interrupt failed\n");
  4898. return -EPERM;
  4899. }
  4900. result = ipa3_add_interrupt_handler(IPA_TX_SUSPEND_IRQ,
  4901. ipa3_suspend_handler, false, NULL);
  4902. if (result) {
  4903. IPAERR("register handler for suspend interrupt failed\n");
  4904. result = -EPERM;
  4905. }
  4906. IPADBG("suspend handler successfully restored\n");
  4907. return result;
  4908. }
  4909. static int ipa3_apps_cons_release_resource(void)
  4910. {
  4911. return 0;
  4912. }
  4913. static int ipa3_apps_cons_request_resource(void)
  4914. {
  4915. return 0;
  4916. }
  4917. static void ipa3_transport_release_resource(struct work_struct *work)
  4918. {
  4919. mutex_lock(&ipa3_ctx->transport_pm.transport_pm_mutex);
  4920. /* check whether still need to decrease client usage */
  4921. if (atomic_read(&ipa3_ctx->transport_pm.dec_clients)) {
  4922. if (atomic_read(&ipa3_ctx->transport_pm.eot_activity)) {
  4923. IPADBG("EOT pending Re-scheduling\n");
  4924. ipa3_process_irq_schedule_rel();
  4925. } else {
  4926. atomic_set(&ipa3_ctx->transport_pm.dec_clients, 0);
  4927. ipa3_dec_release_wakelock();
  4928. IPA_ACTIVE_CLIENTS_DEC_SPECIAL("TRANSPORT_RESOURCE");
  4929. }
  4930. }
  4931. atomic_set(&ipa3_ctx->transport_pm.eot_activity, 0);
  4932. mutex_unlock(&ipa3_ctx->transport_pm.transport_pm_mutex);
  4933. }
  4934. int ipa3_create_apps_resource(void)
  4935. {
  4936. struct ipa_rm_create_params apps_cons_create_params;
  4937. struct ipa_rm_perf_profile profile;
  4938. int result = 0;
  4939. memset(&apps_cons_create_params, 0,
  4940. sizeof(apps_cons_create_params));
  4941. apps_cons_create_params.name = IPA_RM_RESOURCE_APPS_CONS;
  4942. apps_cons_create_params.request_resource =
  4943. ipa3_apps_cons_request_resource;
  4944. apps_cons_create_params.release_resource =
  4945. ipa3_apps_cons_release_resource;
  4946. result = ipa_rm_create_resource(&apps_cons_create_params);
  4947. if (result) {
  4948. IPAERR("ipa_rm_create_resource failed\n");
  4949. return result;
  4950. }
  4951. profile.max_supported_bandwidth_mbps = IPA_APPS_MAX_BW_IN_MBPS;
  4952. ipa_rm_set_perf_profile(IPA_RM_RESOURCE_APPS_CONS, &profile);
  4953. return result;
  4954. }
  4955. /**
  4956. * ipa3_init_interrupts() - Register to IPA IRQs
  4957. *
  4958. * Return codes: 0 in success, negative in failure
  4959. *
  4960. */
  4961. int ipa3_init_interrupts(void)
  4962. {
  4963. int result;
  4964. /*register IPA IRQ handler*/
  4965. result = ipa3_interrupts_init(ipa3_res.ipa_irq, 0,
  4966. &ipa3_ctx->master_pdev->dev);
  4967. if (result) {
  4968. IPAERR("ipa interrupts initialization failed\n");
  4969. return -ENODEV;
  4970. }
  4971. /*add handler for suspend interrupt*/
  4972. result = ipa3_add_interrupt_handler(IPA_TX_SUSPEND_IRQ,
  4973. ipa3_suspend_handler, false, NULL);
  4974. if (result) {
  4975. IPAERR("register handler for suspend interrupt failed\n");
  4976. result = -ENODEV;
  4977. goto fail_add_interrupt_handler;
  4978. }
  4979. return 0;
  4980. fail_add_interrupt_handler:
  4981. ipa3_interrupts_destroy(ipa3_res.ipa_irq, &ipa3_ctx->master_pdev->dev);
  4982. return result;
  4983. }
  4984. /**
  4985. * ipa3_destroy_flt_tbl_idrs() - destroy the idr structure for flt tables
  4986. * The idr strcuture per filtering table is intended for rule id generation
  4987. * per filtering rule.
  4988. */
  4989. static void ipa3_destroy_flt_tbl_idrs(void)
  4990. {
  4991. int i;
  4992. struct ipa3_flt_tbl *flt_tbl;
  4993. idr_destroy(&ipa3_ctx->flt_rule_ids[IPA_IP_v4]);
  4994. idr_destroy(&ipa3_ctx->flt_rule_ids[IPA_IP_v6]);
  4995. for (i = 0; i < ipa3_ctx->ipa_num_pipes; i++) {
  4996. if (!ipa_is_ep_support_flt(i))
  4997. continue;
  4998. flt_tbl = &ipa3_ctx->flt_tbl[i][IPA_IP_v4];
  4999. flt_tbl->rule_ids = NULL;
  5000. flt_tbl = &ipa3_ctx->flt_tbl[i][IPA_IP_v6];
  5001. flt_tbl->rule_ids = NULL;
  5002. }
  5003. }
  5004. static void ipa3_freeze_clock_vote_and_notify_modem(void)
  5005. {
  5006. int res;
  5007. struct ipa_active_client_logging_info log_info;
  5008. if (ipa3_ctx->platform_type == IPA_PLAT_TYPE_APQ) {
  5009. IPADBG("Ignore smp2p on APQ platform\n");
  5010. return;
  5011. }
  5012. if (ipa3_ctx->smp2p_info.res_sent)
  5013. return;
  5014. if (IS_ERR(ipa3_ctx->smp2p_info.smem_state)) {
  5015. IPAERR("fail to get smp2p clk resp bit %ld\n",
  5016. PTR_ERR(ipa3_ctx->smp2p_info.smem_state));
  5017. return;
  5018. }
  5019. IPA_ACTIVE_CLIENTS_PREP_SPECIAL(log_info, "FREEZE_VOTE");
  5020. res = ipa3_inc_client_enable_clks_no_block(&log_info);
  5021. if (res)
  5022. ipa3_ctx->smp2p_info.ipa_clk_on = false;
  5023. else
  5024. ipa3_ctx->smp2p_info.ipa_clk_on = true;
  5025. qcom_smem_state_update_bits(ipa3_ctx->smp2p_info.smem_state,
  5026. IPA_SMP2P_SMEM_STATE_MASK,
  5027. ((ipa3_ctx->smp2p_info.ipa_clk_on <<
  5028. IPA_SMP2P_OUT_CLK_VOTE_IDX) |
  5029. (1 << IPA_SMP2P_OUT_CLK_RSP_CMPLT_IDX)));
  5030. ipa3_ctx->smp2p_info.res_sent = true;
  5031. IPADBG("IPA clocks are %s\n",
  5032. ipa3_ctx->smp2p_info.ipa_clk_on ? "ON" : "OFF");
  5033. }
  5034. void ipa3_reset_freeze_vote(void)
  5035. {
  5036. if (!ipa3_ctx->smp2p_info.res_sent)
  5037. return;
  5038. if (ipa3_ctx->smp2p_info.ipa_clk_on)
  5039. IPA_ACTIVE_CLIENTS_DEC_SPECIAL("FREEZE_VOTE");
  5040. qcom_smem_state_update_bits(ipa3_ctx->smp2p_info.smem_state,
  5041. IPA_SMP2P_SMEM_STATE_MASK,
  5042. ((0 <<
  5043. IPA_SMP2P_OUT_CLK_VOTE_IDX) |
  5044. (0 << IPA_SMP2P_OUT_CLK_RSP_CMPLT_IDX)));
  5045. ipa3_ctx->smp2p_info.res_sent = false;
  5046. ipa3_ctx->smp2p_info.ipa_clk_on = false;
  5047. }
  5048. static int ipa3_panic_notifier(struct notifier_block *this,
  5049. unsigned long event, void *ptr)
  5050. {
  5051. int res;
  5052. ipa3_freeze_clock_vote_and_notify_modem();
  5053. IPADBG("Calling uC panic handler\n");
  5054. res = ipa3_uc_panic_notifier(this, event, ptr);
  5055. if (res)
  5056. IPAERR("uC panic handler failed %d\n", res);
  5057. if (atomic_read(&ipa3_ctx->ipa_clk_vote)) {
  5058. ipahal_print_all_regs(false);
  5059. ipa_save_registers();
  5060. ipa_wigig_save_regs();
  5061. }
  5062. return NOTIFY_DONE;
  5063. }
  5064. static struct notifier_block ipa3_panic_blk = {
  5065. .notifier_call = ipa3_panic_notifier,
  5066. /* IPA panic handler needs to run before modem shuts down */
  5067. .priority = INT_MAX,
  5068. };
  5069. static void ipa3_register_panic_hdlr(void)
  5070. {
  5071. atomic_notifier_chain_register(&panic_notifier_list,
  5072. &ipa3_panic_blk);
  5073. }
  5074. static void ipa3_trigger_ipa_ready_cbs(void)
  5075. {
  5076. struct ipa3_ready_cb_info *info;
  5077. mutex_lock(&ipa3_ctx->lock);
  5078. /* Call all the CBs */
  5079. list_for_each_entry(info, &ipa3_ctx->ipa_ready_cb_list, link)
  5080. if (info->ready_cb)
  5081. info->ready_cb(info->user_data);
  5082. mutex_unlock(&ipa3_ctx->lock);
  5083. }
  5084. static void ipa3_uc_is_loaded(void)
  5085. {
  5086. IPADBG("\n");
  5087. complete_all(&ipa3_ctx->uc_loaded_completion_obj);
  5088. }
  5089. static enum gsi_ver ipa3_get_gsi_ver(enum ipa_hw_type ipa_hw_type)
  5090. {
  5091. enum gsi_ver gsi_ver;
  5092. switch (ipa_hw_type) {
  5093. case IPA_HW_v3_0:
  5094. case IPA_HW_v3_1:
  5095. gsi_ver = GSI_VER_1_0;
  5096. break;
  5097. case IPA_HW_v3_5:
  5098. gsi_ver = GSI_VER_1_2;
  5099. break;
  5100. case IPA_HW_v3_5_1:
  5101. gsi_ver = GSI_VER_1_3;
  5102. break;
  5103. case IPA_HW_v4_0:
  5104. case IPA_HW_v4_1:
  5105. gsi_ver = GSI_VER_2_0;
  5106. break;
  5107. case IPA_HW_v4_2:
  5108. gsi_ver = GSI_VER_2_2;
  5109. break;
  5110. case IPA_HW_v4_5:
  5111. gsi_ver = GSI_VER_2_5;
  5112. break;
  5113. case IPA_HW_v4_7:
  5114. gsi_ver = GSI_VER_2_7;
  5115. break;
  5116. case IPA_HW_v4_9:
  5117. gsi_ver = GSI_VER_2_9;
  5118. break;
  5119. default:
  5120. IPAERR("No GSI version for ipa type %d\n", ipa_hw_type);
  5121. WARN_ON(1);
  5122. gsi_ver = GSI_VER_ERR;
  5123. }
  5124. IPADBG("GSI version %d\n", gsi_ver);
  5125. return gsi_ver;
  5126. }
  5127. static int ipa3_gsi_pre_fw_load_init(void)
  5128. {
  5129. int result;
  5130. result = gsi_configure_regs(
  5131. ipa3_res.ipa_mem_base,
  5132. ipa3_get_gsi_ver(ipa3_res.ipa_hw_type));
  5133. if (result) {
  5134. IPAERR("Failed to configure GSI registers\n");
  5135. return -EINVAL;
  5136. }
  5137. return 0;
  5138. }
  5139. static int ipa3_alloc_gsi_channel(void)
  5140. {
  5141. const struct ipa_gsi_ep_config *gsi_ep_cfg;
  5142. enum ipa_client_type type;
  5143. int code = 0;
  5144. int ret = 0;
  5145. int i;
  5146. for (i = 0; i < ipa3_ctx->ipa_num_pipes; i++) {
  5147. type = ipa3_get_client_by_pipe(i);
  5148. gsi_ep_cfg = ipa3_get_gsi_ep_info(type);
  5149. IPADBG("for ep %d client is %d\n", i, type);
  5150. if (!gsi_ep_cfg)
  5151. continue;
  5152. ret = gsi_alloc_channel_ee(gsi_ep_cfg->ipa_gsi_chan_num,
  5153. gsi_ep_cfg->ee, &code);
  5154. if (ret == GSI_STATUS_SUCCESS) {
  5155. IPADBG("alloc gsi ch %d ee %d with code %d\n",
  5156. gsi_ep_cfg->ipa_gsi_chan_num,
  5157. gsi_ep_cfg->ee,
  5158. code);
  5159. } else {
  5160. IPAERR("failed to alloc ch %d ee %d code %d\n",
  5161. gsi_ep_cfg->ipa_gsi_chan_num,
  5162. gsi_ep_cfg->ee,
  5163. code);
  5164. return ret;
  5165. }
  5166. }
  5167. return ret;
  5168. }
  5169. static inline void ipa3_enable_napi_lan_rx(void)
  5170. {
  5171. if (ipa3_ctx->lan_rx_napi_enable)
  5172. napi_enable(&ipa3_ctx->napi_lan_rx);
  5173. }
  5174. /**
  5175. * ipa3_post_init() - Initialize the IPA Driver (Part II).
  5176. * This part contains all initialization which requires interaction with
  5177. * IPA HW (via GSI).
  5178. *
  5179. * @resource_p: contain platform specific values from DST file
  5180. * @pdev: The platform device structure representing the IPA driver
  5181. *
  5182. * Function initialization process:
  5183. * - Initialize endpoints bitmaps
  5184. * - Initialize resource groups min and max values
  5185. * - Initialize filtering lists heads and idr
  5186. * - Initialize interrupts
  5187. * - Register GSI
  5188. * - Setup APPS pipes
  5189. * - Initialize tethering bridge
  5190. * - Initialize IPA debugfs
  5191. * - Initialize IPA uC interface
  5192. * - Initialize WDI interface
  5193. * - Initialize USB interface
  5194. * - Register for panic handler
  5195. * - Trigger IPA ready callbacks (to all subscribers)
  5196. * - Trigger IPA completion object (to all who wait on it)
  5197. */
  5198. static int ipa3_post_init(const struct ipa3_plat_drv_res *resource_p,
  5199. struct device *ipa_dev)
  5200. {
  5201. int result;
  5202. struct gsi_per_props gsi_props;
  5203. struct ipa3_uc_hdlrs uc_hdlrs = { 0 };
  5204. struct ipa3_flt_tbl *flt_tbl;
  5205. int i;
  5206. struct idr *idr;
  5207. if (ipa3_ctx == NULL) {
  5208. IPADBG("IPA driver haven't initialized\n");
  5209. return -ENXIO;
  5210. }
  5211. /* Prevent consequent calls from trying to load the FW again. */
  5212. if (ipa3_ctx->ipa_initialization_complete)
  5213. return 0;
  5214. IPADBG("active clients = %d\n",
  5215. atomic_read(&ipa3_ctx->ipa3_active_clients.cnt));
  5216. /* move proxy vote for modem on ipa3_post_init */
  5217. if (ipa3_ctx->ipa_hw_type != IPA_HW_v4_0)
  5218. ipa3_proxy_clk_vote();
  5219. /* The following will retrieve and save the gsi fw version */
  5220. ipa_save_gsi_ver();
  5221. if (ipahal_init(ipa3_ctx->ipa_hw_type, ipa3_ctx->mmio,
  5222. ipa3_ctx->pdev)) {
  5223. IPAERR("fail to init ipahal\n");
  5224. result = -EFAULT;
  5225. goto fail_ipahal;
  5226. }
  5227. result = ipa3_init_hw();
  5228. if (result) {
  5229. IPAERR(":error initializing HW\n");
  5230. result = -ENODEV;
  5231. goto fail_init_hw;
  5232. }
  5233. IPADBG("IPA HW initialization sequence completed");
  5234. ipa3_ctx->ipa_num_pipes = ipa3_get_num_pipes();
  5235. IPADBG("IPA Pipes num %u\n", ipa3_ctx->ipa_num_pipes);
  5236. if (ipa3_ctx->ipa_num_pipes > IPA3_MAX_NUM_PIPES) {
  5237. IPAERR("IPA has more pipes then supported has %d, max %d\n",
  5238. ipa3_ctx->ipa_num_pipes, IPA3_MAX_NUM_PIPES);
  5239. result = -ENODEV;
  5240. goto fail_init_hw;
  5241. }
  5242. ipa3_ctx->ctrl->ipa_sram_read_settings();
  5243. IPADBG("SRAM, size: 0x%x, restricted bytes: 0x%x\n",
  5244. ipa3_ctx->smem_sz, ipa3_ctx->smem_restricted_bytes);
  5245. IPADBG("hdr_lcl=%u ip4_rt_hash=%u ip4_rt_nonhash=%u\n",
  5246. ipa3_ctx->hdr_tbl_lcl, ipa3_ctx->ip4_rt_tbl_hash_lcl,
  5247. ipa3_ctx->ip4_rt_tbl_nhash_lcl);
  5248. IPADBG("ip6_rt_hash=%u ip6_rt_nonhash=%u\n",
  5249. ipa3_ctx->ip6_rt_tbl_hash_lcl, ipa3_ctx->ip6_rt_tbl_nhash_lcl);
  5250. IPADBG("ip4_flt_hash=%u ip4_flt_nonhash=%u\n",
  5251. ipa3_ctx->ip4_flt_tbl_hash_lcl,
  5252. ipa3_ctx->ip4_flt_tbl_nhash_lcl);
  5253. IPADBG("ip6_flt_hash=%u ip6_flt_nonhash=%u\n",
  5254. ipa3_ctx->ip6_flt_tbl_hash_lcl,
  5255. ipa3_ctx->ip6_flt_tbl_nhash_lcl);
  5256. if (ipa3_ctx->smem_reqd_sz > ipa3_ctx->smem_sz) {
  5257. IPAERR("SW expect more core memory, needed %d, avail %d\n",
  5258. ipa3_ctx->smem_reqd_sz, ipa3_ctx->smem_sz);
  5259. result = -ENOMEM;
  5260. goto fail_init_hw;
  5261. }
  5262. result = ipa3_allocate_dma_task_for_gsi();
  5263. if (result) {
  5264. IPAERR("failed to allocate dma task\n");
  5265. goto fail_dma_task;
  5266. }
  5267. result = ipa3_allocate_coal_close_frame();
  5268. if (result) {
  5269. IPAERR("failed to allocate coal frame cmd\n");
  5270. goto fail_coal_frame;
  5271. }
  5272. if (ipa3_nat_ipv6ct_init_devices()) {
  5273. IPAERR("unable to init NAT and IPv6CT devices\n");
  5274. result = -ENODEV;
  5275. goto fail_nat_ipv6ct_init_dev;
  5276. }
  5277. result = ipa3_alloc_pkt_init();
  5278. if (result) {
  5279. IPAERR("Failed to alloc pkt_init payload\n");
  5280. result = -ENODEV;
  5281. goto fail_allok_pkt_init;
  5282. }
  5283. if (ipa3_ctx->ipa_hw_type >= IPA_HW_v3_5)
  5284. ipa3_enable_dcd();
  5285. /*
  5286. * indication whether working in MHI config or non MHI config is given
  5287. * in ipa3_write which is launched before ipa3_post_init. i.e. from
  5288. * this point it is safe to use ipa3_ep_mapping array and the correct
  5289. * entry will be returned from ipa3_get_hw_type_index()
  5290. */
  5291. ipa_init_ep_flt_bitmap();
  5292. IPADBG("EP with flt support bitmap 0x%x (%u pipes)\n",
  5293. ipa3_ctx->ep_flt_bitmap, ipa3_ctx->ep_flt_num);
  5294. /* Assign resource limitation to each group */
  5295. ipa3_set_resorce_groups_min_max_limits();
  5296. idr = &(ipa3_ctx->flt_rule_ids[IPA_IP_v4]);
  5297. idr_init(idr);
  5298. idr = &(ipa3_ctx->flt_rule_ids[IPA_IP_v6]);
  5299. idr_init(idr);
  5300. for (i = 0; i < ipa3_ctx->ipa_num_pipes; i++) {
  5301. if (!ipa_is_ep_support_flt(i))
  5302. continue;
  5303. flt_tbl = &ipa3_ctx->flt_tbl[i][IPA_IP_v4];
  5304. INIT_LIST_HEAD(&flt_tbl->head_flt_rule_list);
  5305. flt_tbl->in_sys[IPA_RULE_HASHABLE] =
  5306. !ipa3_ctx->ip4_flt_tbl_hash_lcl;
  5307. flt_tbl->in_sys[IPA_RULE_NON_HASHABLE] =
  5308. !ipa3_ctx->ip4_flt_tbl_nhash_lcl;
  5309. flt_tbl->rule_ids = &ipa3_ctx->flt_rule_ids[IPA_IP_v4];
  5310. flt_tbl = &ipa3_ctx->flt_tbl[i][IPA_IP_v6];
  5311. INIT_LIST_HEAD(&flt_tbl->head_flt_rule_list);
  5312. flt_tbl->in_sys[IPA_RULE_HASHABLE] =
  5313. !ipa3_ctx->ip6_flt_tbl_hash_lcl;
  5314. flt_tbl->in_sys[IPA_RULE_NON_HASHABLE] =
  5315. !ipa3_ctx->ip6_flt_tbl_nhash_lcl;
  5316. flt_tbl->rule_ids = &ipa3_ctx->flt_rule_ids[IPA_IP_v6];
  5317. }
  5318. result = ipa3_init_interrupts();
  5319. if (result) {
  5320. IPAERR("ipa initialization of interrupts failed\n");
  5321. result = -ENODEV;
  5322. goto fail_init_interrupts;
  5323. }
  5324. /*
  5325. * Disable prefetch for USB or MHI at IPAv3.5/IPA.3.5.1
  5326. * This is to allow MBIM to work.
  5327. */
  5328. if ((ipa3_ctx->ipa_hw_type >= IPA_HW_v3_5
  5329. && ipa3_ctx->ipa_hw_type < IPA_HW_v4_0) &&
  5330. (!ipa3_ctx->ipa_config_is_mhi))
  5331. ipa3_disable_prefetch(IPA_CLIENT_USB_CONS);
  5332. if ((ipa3_ctx->ipa_hw_type >= IPA_HW_v3_5
  5333. && ipa3_ctx->ipa_hw_type < IPA_HW_v4_0) &&
  5334. (ipa3_ctx->ipa_config_is_mhi))
  5335. ipa3_disable_prefetch(IPA_CLIENT_MHI_CONS);
  5336. memset(&gsi_props, 0, sizeof(gsi_props));
  5337. gsi_props.ver = ipa3_get_gsi_ver(resource_p->ipa_hw_type);
  5338. gsi_props.ee = resource_p->ee;
  5339. gsi_props.intr = GSI_INTR_IRQ;
  5340. gsi_props.phys_addr = resource_p->transport_mem_base;
  5341. gsi_props.size = resource_p->transport_mem_size;
  5342. if (ipa3_ctx->ipa3_hw_mode == IPA_HW_MODE_EMULATION) {
  5343. gsi_props.irq = resource_p->emulator_irq;
  5344. gsi_props.emulator_intcntrlr_client_isr = ipa3_get_isr();
  5345. gsi_props.emulator_intcntrlr_addr =
  5346. resource_p->emulator_intcntrlr_mem_base;
  5347. gsi_props.emulator_intcntrlr_size =
  5348. resource_p->emulator_intcntrlr_mem_size;
  5349. } else {
  5350. gsi_props.irq = resource_p->transport_irq;
  5351. }
  5352. gsi_props.notify_cb = ipa_gsi_notify_cb;
  5353. gsi_props.req_clk_cb = NULL;
  5354. gsi_props.rel_clk_cb = NULL;
  5355. gsi_props.clk_status_cb = ipa3_active_clks_status;
  5356. if (ipa3_ctx->ipa_config_is_mhi) {
  5357. gsi_props.mhi_er_id_limits_valid = true;
  5358. gsi_props.mhi_er_id_limits[0] = resource_p->mhi_evid_limits[0];
  5359. gsi_props.mhi_er_id_limits[1] = resource_p->mhi_evid_limits[1];
  5360. }
  5361. gsi_props.skip_ieob_mask_wa = resource_p->skip_ieob_mask_wa;
  5362. result = gsi_register_device(&gsi_props,
  5363. &ipa3_ctx->gsi_dev_hdl);
  5364. if (result != GSI_STATUS_SUCCESS) {
  5365. IPAERR(":gsi register error - %d\n", result);
  5366. result = -ENODEV;
  5367. goto fail_register_device;
  5368. }
  5369. IPADBG("IPA gsi is registered\n");
  5370. /* GSI 2.2 requires to allocate all EE GSI channel
  5371. * during device bootup.
  5372. */
  5373. if (ipa3_get_gsi_ver(resource_p->ipa_hw_type) == GSI_VER_2_2) {
  5374. result = ipa3_alloc_gsi_channel();
  5375. if (result) {
  5376. IPAERR("Failed to alloc the GSI channels\n");
  5377. result = -ENODEV;
  5378. goto fail_alloc_gsi_channel;
  5379. }
  5380. }
  5381. /* setup the AP-IPA pipes */
  5382. if (ipa3_setup_apps_pipes()) {
  5383. IPAERR(":failed to setup IPA-Apps pipes\n");
  5384. result = -ENODEV;
  5385. goto fail_setup_apps_pipes;
  5386. }
  5387. IPADBG("IPA GPI pipes were connected\n");
  5388. if (ipa3_ctx->use_ipa_teth_bridge) {
  5389. /* Initialize the tethering bridge driver */
  5390. result = ipa3_teth_bridge_driver_init();
  5391. if (result) {
  5392. IPAERR(":teth_bridge init failed (%d)\n", -result);
  5393. result = -ENODEV;
  5394. goto fail_teth_bridge_driver_init;
  5395. }
  5396. IPADBG("teth_bridge initialized");
  5397. }
  5398. result = ipa3_uc_interface_init();
  5399. if (result)
  5400. IPAERR(":ipa Uc interface init failed (%d)\n", -result);
  5401. else
  5402. IPADBG(":ipa Uc interface init ok\n");
  5403. uc_hdlrs.ipa_uc_loaded_hdlr = ipa3_uc_is_loaded;
  5404. ipa3_uc_register_handlers(IPA_HW_FEATURE_COMMON, &uc_hdlrs);
  5405. result = ipa3_wdi_init();
  5406. if (result)
  5407. IPAERR(":wdi init failed (%d)\n", -result);
  5408. else
  5409. IPADBG(":wdi init ok\n");
  5410. result = ipa3_wigig_init_i();
  5411. if (result)
  5412. IPAERR(":wigig init failed (%d)\n", -result);
  5413. else
  5414. IPADBG(":wigig init ok\n");
  5415. result = ipa3_ntn_init();
  5416. if (result)
  5417. IPAERR(":ntn init failed (%d)\n", -result);
  5418. else
  5419. IPADBG(":ntn init ok\n");
  5420. result = ipa_hw_stats_init();
  5421. if (result)
  5422. IPAERR("fail to init stats %d\n", result);
  5423. else
  5424. IPADBG(":stats init ok\n");
  5425. ipa3_register_panic_hdlr();
  5426. ipa3_debugfs_init();
  5427. mutex_lock(&ipa3_ctx->lock);
  5428. ipa3_ctx->ipa_initialization_complete = true;
  5429. mutex_unlock(&ipa3_ctx->lock);
  5430. ipa3_enable_napi_lan_rx();
  5431. ipa3_trigger_ipa_ready_cbs();
  5432. complete_all(&ipa3_ctx->init_completion_obj);
  5433. ipa_ut_module_init();
  5434. pr_info("IPA driver initialization was successful.\n");
  5435. return 0;
  5436. fail_teth_bridge_driver_init:
  5437. ipa3_teardown_apps_pipes();
  5438. fail_alloc_gsi_channel:
  5439. fail_setup_apps_pipes:
  5440. gsi_deregister_device(ipa3_ctx->gsi_dev_hdl, false);
  5441. fail_register_device:
  5442. ipa3_destroy_flt_tbl_idrs();
  5443. fail_init_interrupts:
  5444. ipa3_remove_interrupt_handler(IPA_TX_SUSPEND_IRQ);
  5445. ipa3_interrupts_destroy(ipa3_res.ipa_irq, &ipa3_ctx->master_pdev->dev);
  5446. fail_allok_pkt_init:
  5447. ipa3_nat_ipv6ct_destroy_devices();
  5448. fail_nat_ipv6ct_init_dev:
  5449. ipa3_free_coal_close_frame();
  5450. fail_coal_frame:
  5451. ipa3_free_dma_task_for_gsi();
  5452. fail_dma_task:
  5453. fail_init_hw:
  5454. ipahal_destroy();
  5455. fail_ipahal:
  5456. ipa3_proxy_clk_unvote();
  5457. return result;
  5458. }
  5459. static int ipa3_manual_load_ipa_fws(void)
  5460. {
  5461. int result;
  5462. const struct firmware *fw;
  5463. const char *path = IPA_FWS_PATH;
  5464. if (ipa3_ctx->ipa3_hw_mode == IPA_HW_MODE_EMULATION) {
  5465. switch (ipa3_get_emulation_type()) {
  5466. case IPA_HW_v3_5_1:
  5467. path = IPA_FWS_PATH_3_5_1;
  5468. break;
  5469. case IPA_HW_v4_0:
  5470. path = IPA_FWS_PATH_4_0;
  5471. break;
  5472. case IPA_HW_v4_5:
  5473. path = IPA_FWS_PATH_4_5;
  5474. break;
  5475. default:
  5476. break;
  5477. }
  5478. }
  5479. IPADBG("Manual FW loading (%s) process initiated\n", path);
  5480. result = request_firmware(&fw, path, ipa3_ctx->cdev.dev);
  5481. if (result < 0) {
  5482. IPAERR("request_firmware failed, error %d\n", result);
  5483. return result;
  5484. }
  5485. IPADBG("FWs are available for loading\n");
  5486. if (ipa3_ctx->ipa3_hw_mode == IPA_HW_MODE_EMULATION) {
  5487. result = emulator_load_fws(fw,
  5488. ipa3_res.transport_mem_base,
  5489. ipa3_res.transport_mem_size,
  5490. ipa3_get_gsi_ver(ipa3_res.ipa_hw_type));
  5491. } else {
  5492. result = ipa3_load_fws(fw, ipa3_res.transport_mem_base,
  5493. ipa3_get_gsi_ver(ipa3_res.ipa_hw_type));
  5494. }
  5495. if (result) {
  5496. IPAERR("Manual IPA FWs loading has failed\n");
  5497. release_firmware(fw);
  5498. return result;
  5499. }
  5500. result = gsi_enable_fw(ipa3_res.transport_mem_base,
  5501. ipa3_res.transport_mem_size,
  5502. ipa3_get_gsi_ver(ipa3_res.ipa_hw_type));
  5503. if (result) {
  5504. IPAERR("Failed to enable GSI FW\n");
  5505. release_firmware(fw);
  5506. return result;
  5507. }
  5508. release_firmware(fw);
  5509. IPADBG("Manual FW loading process is complete\n");
  5510. return 0;
  5511. }
  5512. static int ipa3_pil_load_ipa_fws(const char *sub_sys)
  5513. {
  5514. void *subsystem_get_retval = NULL;
  5515. IPADBG("PIL FW loading process initiated sub_sys=%s\n",
  5516. sub_sys);
  5517. subsystem_get_retval = subsystem_get(sub_sys);
  5518. if (IS_ERR_OR_NULL(subsystem_get_retval)) {
  5519. IPAERR("Unable to PIL load FW for sub_sys=%s\n", sub_sys);
  5520. return -EINVAL;
  5521. }
  5522. IPADBG("PIL FW loading process is complete sub_sys=%s\n", sub_sys);
  5523. return 0;
  5524. }
  5525. static void ipa3_load_ipa_fw(struct work_struct *work)
  5526. {
  5527. int result;
  5528. IPADBG("Entry\n");
  5529. IPA_ACTIVE_CLIENTS_INC_SIMPLE();
  5530. result = ipa3_attach_to_smmu();
  5531. if (result) {
  5532. IPAERR("IPA attach to smmu failed %d\n", result);
  5533. IPA_ACTIVE_CLIENTS_DEC_SIMPLE();
  5534. return;
  5535. }
  5536. if (ipa3_ctx->ipa3_hw_mode != IPA_HW_MODE_EMULATION &&
  5537. ((ipa3_ctx->platform_type != IPA_PLAT_TYPE_MDM) ||
  5538. (ipa3_ctx->ipa_hw_type >= IPA_HW_v3_5)))
  5539. result = ipa3_pil_load_ipa_fws(IPA_SUBSYSTEM_NAME);
  5540. else
  5541. result = ipa3_manual_load_ipa_fws();
  5542. IPA_ACTIVE_CLIENTS_DEC_SIMPLE();
  5543. if (result) {
  5544. IPAERR("IPA FW loading process has failed result=%d\n",
  5545. result);
  5546. return;
  5547. }
  5548. mutex_lock(&ipa3_ctx->fw_load_data.lock);
  5549. ipa3_ctx->fw_load_data.state = IPA_FW_LOAD_STATE_LOADED;
  5550. mutex_unlock(&ipa3_ctx->fw_load_data.lock);
  5551. pr_info("IPA FW loaded successfully\n");
  5552. result = ipa3_post_init(&ipa3_res, ipa3_ctx->cdev.dev);
  5553. if (result) {
  5554. IPAERR("IPA post init failed %d\n", result);
  5555. return;
  5556. }
  5557. if (ipa3_ctx->platform_type == IPA_PLAT_TYPE_APQ &&
  5558. ipa3_ctx->ipa3_hw_mode != IPA_HW_MODE_VIRTUAL &&
  5559. ipa3_ctx->ipa3_hw_mode != IPA_HW_MODE_EMULATION) {
  5560. IPADBG("Loading IPA uC via PIL\n");
  5561. /* Unvoting will happen when uC loaded event received. */
  5562. ipa3_proxy_clk_vote();
  5563. result = ipa3_pil_load_ipa_fws(IPA_UC_SUBSYSTEM_NAME);
  5564. if (result) {
  5565. IPAERR("IPA uC loading process has failed result=%d\n",
  5566. result);
  5567. return;
  5568. }
  5569. IPADBG("IPA uC PIL loading succeeded\n");
  5570. }
  5571. }
  5572. static void ipa_fw_load_sm_handle_event(enum ipa_fw_load_event ev)
  5573. {
  5574. mutex_lock(&ipa3_ctx->fw_load_data.lock);
  5575. IPADBG("state=%d event=%d\n", ipa3_ctx->fw_load_data.state, ev);
  5576. if (ev == IPA_FW_LOAD_EVNT_FWFILE_READY) {
  5577. if (ipa3_ctx->fw_load_data.state == IPA_FW_LOAD_STATE_INIT) {
  5578. ipa3_ctx->fw_load_data.state =
  5579. IPA_FW_LOAD_STATE_FWFILE_READY;
  5580. goto out;
  5581. }
  5582. if (ipa3_ctx->fw_load_data.state ==
  5583. IPA_FW_LOAD_STATE_SMMU_DONE) {
  5584. ipa3_ctx->fw_load_data.state =
  5585. IPA_FW_LOAD_STATE_LOAD_READY;
  5586. goto sched_fw_load;
  5587. }
  5588. IPAERR("ignore multiple requests to load FW\n");
  5589. goto out;
  5590. }
  5591. if (ev == IPA_FW_LOAD_EVNT_SMMU_DONE) {
  5592. if (ipa3_ctx->fw_load_data.state == IPA_FW_LOAD_STATE_INIT) {
  5593. ipa3_ctx->fw_load_data.state =
  5594. IPA_FW_LOAD_STATE_SMMU_DONE;
  5595. goto out;
  5596. }
  5597. if (ipa3_ctx->fw_load_data.state ==
  5598. IPA_FW_LOAD_STATE_FWFILE_READY) {
  5599. ipa3_ctx->fw_load_data.state =
  5600. IPA_FW_LOAD_STATE_LOAD_READY;
  5601. goto sched_fw_load;
  5602. }
  5603. IPAERR("ignore multiple smmu done events\n");
  5604. goto out;
  5605. }
  5606. IPAERR("invalid event ev=%d\n", ev);
  5607. mutex_unlock(&ipa3_ctx->fw_load_data.lock);
  5608. ipa_assert();
  5609. return;
  5610. out:
  5611. mutex_unlock(&ipa3_ctx->fw_load_data.lock);
  5612. return;
  5613. sched_fw_load:
  5614. IPADBG("Scheduled a work to load IPA FW\n");
  5615. mutex_unlock(&ipa3_ctx->fw_load_data.lock);
  5616. queue_work(ipa3_ctx->transport_power_mgmt_wq,
  5617. &ipa3_fw_loading_work);
  5618. }
  5619. static ssize_t ipa3_write(struct file *file, const char __user *buf,
  5620. size_t count, loff_t *ppos)
  5621. {
  5622. unsigned long missing;
  5623. char dbg_buff[32] = { 0 };
  5624. int i = 0;
  5625. if (count >= sizeof(dbg_buff))
  5626. return -EFAULT;
  5627. missing = copy_from_user(dbg_buff, buf, count);
  5628. if (missing) {
  5629. IPAERR("Unable to copy data from user\n");
  5630. return -EFAULT;
  5631. }
  5632. if (count > 0)
  5633. dbg_buff[count] = '\0';
  5634. IPADBG("user input string %s\n", dbg_buff);
  5635. /* Prevent consequent calls from trying to load the FW again. */
  5636. if (ipa3_is_ready())
  5637. return count;
  5638. /*Ignore empty ipa_config file*/
  5639. for (i = 0 ; i < count ; ++i) {
  5640. if (!isspace(dbg_buff[i]))
  5641. break;
  5642. }
  5643. if (i == count) {
  5644. IPADBG("Empty ipa_config file\n");
  5645. return count;
  5646. }
  5647. /* Check MHI configuration on MDM devices */
  5648. if (ipa3_ctx->platform_type == IPA_PLAT_TYPE_MDM) {
  5649. if (strnstr(dbg_buff, "vlan", strlen(dbg_buff))) {
  5650. if (strnstr(dbg_buff, "eth", strlen(dbg_buff)))
  5651. ipa3_ctx->vlan_mode_iface[IPA_VLAN_IF_EMAC] =
  5652. true;
  5653. if (strnstr(dbg_buff, "rndis", strlen(dbg_buff)))
  5654. ipa3_ctx->vlan_mode_iface[IPA_VLAN_IF_RNDIS] =
  5655. true;
  5656. if (strnstr(dbg_buff, "ecm", strlen(dbg_buff)))
  5657. ipa3_ctx->vlan_mode_iface[IPA_VLAN_IF_ECM] =
  5658. true;
  5659. /*
  5660. * when vlan mode is passed to our dev we expect
  5661. * another write
  5662. */
  5663. return count;
  5664. }
  5665. /* trim ending newline character if any */
  5666. if (count && (dbg_buff[count - 1] == '\n'))
  5667. dbg_buff[count - 1] = '\0';
  5668. /*
  5669. * This logic enforeces MHI mode based on userspace input.
  5670. * Note that MHI mode could be already determined due
  5671. * to previous logic.
  5672. */
  5673. if (!strcasecmp(dbg_buff, "MHI")) {
  5674. ipa3_ctx->ipa_config_is_mhi = true;
  5675. } else if (strcmp(dbg_buff, "1")) {
  5676. IPAERR("got invalid string %s not loading FW\n",
  5677. dbg_buff);
  5678. return count;
  5679. }
  5680. pr_info("IPA is loading with %sMHI configuration\n",
  5681. ipa3_ctx->ipa_config_is_mhi ? "" : "non ");
  5682. }
  5683. ipa_fw_load_sm_handle_event(IPA_FW_LOAD_EVNT_FWFILE_READY);
  5684. return count;
  5685. }
  5686. /**
  5687. * ipa3_tz_unlock_reg - Unlocks memory regions so that they become accessible
  5688. * from AP.
  5689. * @reg_info - Pointer to array of memory regions to unlock
  5690. * @num_regs - Number of elements in the array
  5691. *
  5692. * Converts the input array of regions to a struct that TZ understands and
  5693. * issues an SCM call.
  5694. * Also flushes the memory cache to DDR in order to make sure that TZ sees the
  5695. * correct data structure.
  5696. *
  5697. * Returns: 0 on success, negative on failure
  5698. */
  5699. int ipa3_tz_unlock_reg(struct ipa_tz_unlock_reg_info *reg_info, u16 num_regs)
  5700. {
  5701. int i, ret;
  5702. compat_size_t size;
  5703. struct tz_smmu_ipa_protect_region_iovec_s *ipa_tz_unlock_vec;
  5704. struct tz_smmu_ipa_protect_region_s cmd_buf;
  5705. if (reg_info == NULL || num_regs == 0) {
  5706. IPAERR("Bad parameters\n");
  5707. return -EFAULT;
  5708. }
  5709. size = num_regs * sizeof(struct tz_smmu_ipa_protect_region_iovec_s);
  5710. ipa_tz_unlock_vec = kzalloc(PAGE_ALIGN(size), GFP_KERNEL);
  5711. if (ipa_tz_unlock_vec == NULL)
  5712. return -ENOMEM;
  5713. for (i = 0; i < num_regs; i++) {
  5714. ipa_tz_unlock_vec[i].input_addr = reg_info[i].reg_addr ^
  5715. (reg_info[i].reg_addr & 0xFFF);
  5716. ipa_tz_unlock_vec[i].output_addr = reg_info[i].reg_addr ^
  5717. (reg_info[i].reg_addr & 0xFFF);
  5718. ipa_tz_unlock_vec[i].size = reg_info[i].size;
  5719. ipa_tz_unlock_vec[i].attr = IPA_TZ_UNLOCK_ATTRIBUTE;
  5720. }
  5721. /* pass physical address of command buffer */
  5722. cmd_buf.iovec_buf = virt_to_phys((void *)ipa_tz_unlock_vec);
  5723. cmd_buf.size_bytes = size;
  5724. ret = qcom_scm_mem_protect_region_id(
  5725. virt_to_phys((void *)ipa_tz_unlock_vec),
  5726. size);
  5727. if (ret) {
  5728. IPAERR("scm call SCM_SVC_MP failed: %d\n", ret);
  5729. kfree(ipa_tz_unlock_vec);
  5730. return -EFAULT;
  5731. }
  5732. kfree(ipa_tz_unlock_vec);
  5733. return 0;
  5734. }
  5735. static int ipa3_alloc_pkt_init(void)
  5736. {
  5737. struct ipa_mem_buffer mem;
  5738. struct ipahal_imm_cmd_pyld *cmd_pyld;
  5739. struct ipahal_imm_cmd_ip_packet_init cmd = {0};
  5740. int i;
  5741. cmd_pyld = ipahal_construct_imm_cmd(IPA_IMM_CMD_IP_PACKET_INIT,
  5742. &cmd, false);
  5743. if (!cmd_pyld) {
  5744. IPAERR("failed to construct IMM cmd\n");
  5745. return -ENOMEM;
  5746. }
  5747. ipa3_ctx->pkt_init_imm_opcode = cmd_pyld->opcode;
  5748. mem.size = cmd_pyld->len * ipa3_ctx->ipa_num_pipes;
  5749. mem.base = dma_alloc_coherent(ipa3_ctx->pdev, mem.size,
  5750. &mem.phys_base, GFP_KERNEL);
  5751. if (!mem.base) {
  5752. IPAERR("failed to alloc DMA buff of size %d\n", mem.size);
  5753. ipahal_destroy_imm_cmd(cmd_pyld);
  5754. return -ENOMEM;
  5755. }
  5756. ipahal_destroy_imm_cmd(cmd_pyld);
  5757. memset(mem.base, 0, mem.size);
  5758. for (i = 0; i < ipa3_ctx->ipa_num_pipes; i++) {
  5759. cmd.destination_pipe_index = i;
  5760. cmd_pyld = ipahal_construct_imm_cmd(IPA_IMM_CMD_IP_PACKET_INIT,
  5761. &cmd, false);
  5762. if (!cmd_pyld) {
  5763. IPAERR("failed to construct IMM cmd\n");
  5764. dma_free_coherent(ipa3_ctx->pdev,
  5765. mem.size,
  5766. mem.base,
  5767. mem.phys_base);
  5768. return -ENOMEM;
  5769. }
  5770. memcpy(mem.base + i * cmd_pyld->len, cmd_pyld->data,
  5771. cmd_pyld->len);
  5772. ipa3_ctx->pkt_init_imm[i] = mem.phys_base + i * cmd_pyld->len;
  5773. ipahal_destroy_imm_cmd(cmd_pyld);
  5774. }
  5775. return 0;
  5776. }
  5777. /*
  5778. * SCM call to check if secure dump is allowed.
  5779. *
  5780. * Returns true in secure dump allowed.
  5781. * Return false when secure dump not allowed.
  5782. */
  5783. static bool ipa_is_mem_dump_allowed(void)
  5784. {
  5785. int ret;
  5786. u32 dump_state;
  5787. ret = qcom_scm_get_sec_dump_state(&dump_state);
  5788. if (ret) {
  5789. IPAERR("SCM DUMP_STATE call failed\n");
  5790. return false;
  5791. }
  5792. return (dump_state == 1);
  5793. }
  5794. static int ipa3_lan_poll(struct napi_struct *napi, int budget)
  5795. {
  5796. int rcvd_pkts = 0;
  5797. rcvd_pkts = ipa3_lan_rx_poll(ipa3_ctx->clnt_hdl_data_in,
  5798. NAPI_WEIGHT);
  5799. return rcvd_pkts;
  5800. }
  5801. static inline void ipa3_enable_napi_netdev(void)
  5802. {
  5803. if (ipa3_ctx->lan_rx_napi_enable) {
  5804. init_dummy_netdev(&ipa3_ctx->lan_ndev);
  5805. netif_napi_add(&ipa3_ctx->lan_ndev, &ipa3_ctx->napi_lan_rx,
  5806. ipa3_lan_poll, NAPI_WEIGHT);
  5807. }
  5808. }
  5809. /**
  5810. * ipa3_pre_init() - Initialize the IPA Driver.
  5811. * This part contains all initialization which doesn't require IPA HW, such
  5812. * as structure allocations and initializations, register writes, etc.
  5813. *
  5814. * @resource_p: contain platform specific values from DST file
  5815. * @pdev: The platform device structure representing the IPA driver
  5816. *
  5817. * Function initialization process:
  5818. * Allocate memory for the driver context data struct
  5819. * Initializing the ipa3_ctx with :
  5820. * 1)parsed values from the dts file
  5821. * 2)parameters passed to the module initialization
  5822. * 3)read HW values(such as core memory size)
  5823. * Map IPA core registers to CPU memory
  5824. * Restart IPA core(HW reset)
  5825. * Initialize the look-aside caches(kmem_cache/slab) for filter,
  5826. * routing and IPA-tree
  5827. * Create memory pool with 4 objects for DMA operations(each object
  5828. * is 512Bytes long), this object will be use for tx(A5->IPA)
  5829. * Initialize lists head(routing, hdr, system pipes)
  5830. * Initialize mutexes (for ipa_ctx and NAT memory mutexes)
  5831. * Initialize spinlocks (for list related to A5<->IPA pipes)
  5832. * Initialize 2 single-threaded work-queue named "ipa rx wq" and "ipa tx wq"
  5833. * Initialize Red-Black-Tree(s) for handles of header,routing rule,
  5834. * routing table ,filtering rule
  5835. * Initialize the filter block by committing IPV4 and IPV6 default rules
  5836. * Create empty routing table in system memory(no committing)
  5837. * Create a char-device for IPA
  5838. * Initialize IPA PM (power manager)
  5839. * Configure GSI registers (in GSI case)
  5840. */
  5841. static int ipa3_pre_init(const struct ipa3_plat_drv_res *resource_p,
  5842. struct platform_device *ipa_pdev)
  5843. {
  5844. int result = 0;
  5845. int i, j;
  5846. struct ipa3_rt_tbl_set *rset;
  5847. struct ipa_active_client_logging_info log_info;
  5848. struct cdev *cdev;
  5849. IPADBG("IPA Driver initialization started\n");
  5850. ipa3_ctx = kzalloc(sizeof(*ipa3_ctx), GFP_KERNEL);
  5851. if (!ipa3_ctx) {
  5852. result = -ENOMEM;
  5853. goto fail_mem_ctx;
  5854. }
  5855. ipa3_ctx->fw_load_data.state = IPA_FW_LOAD_STATE_INIT;
  5856. mutex_init(&ipa3_ctx->fw_load_data.lock);
  5857. ipa3_ctx->logbuf = ipc_log_context_create(IPA_IPC_LOG_PAGES, "ipa", 0);
  5858. if (ipa3_ctx->logbuf == NULL)
  5859. IPADBG("failed to create IPC log, continue...\n");
  5860. /* ipa3_ctx->pdev and ipa3_ctx->uc_pdev will be set in the smmu probes*/
  5861. ipa3_ctx->master_pdev = ipa_pdev;
  5862. for (i = 0; i < IPA_SMMU_CB_MAX; i++)
  5863. ipa3_ctx->s1_bypass_arr[i] = true;
  5864. /* initialize the gsi protocol info for uC debug stats */
  5865. for (i = 0; i < IPA_HW_PROTOCOL_MAX; i++) {
  5866. ipa3_ctx->gsi_info[i].protocol = i;
  5867. /* initialize all to be not started */
  5868. for (j = 0; j < IPA_MAX_CH_STATS_SUPPORTED; j++)
  5869. ipa3_ctx->gsi_info[i].ch_id_info[j].ch_id =
  5870. 0xFF;
  5871. }
  5872. ipa3_ctx->ipa_wrapper_base = resource_p->ipa_mem_base;
  5873. ipa3_ctx->ipa_wrapper_size = resource_p->ipa_mem_size;
  5874. ipa3_ctx->ipa_hw_type = resource_p->ipa_hw_type;
  5875. ipa3_ctx->ipa3_hw_mode = resource_p->ipa3_hw_mode;
  5876. ipa3_ctx->platform_type = resource_p->platform_type;
  5877. ipa3_ctx->use_ipa_teth_bridge = resource_p->use_ipa_teth_bridge;
  5878. ipa3_ctx->modem_cfg_emb_pipe_flt = resource_p->modem_cfg_emb_pipe_flt;
  5879. ipa3_ctx->ipa_wdi2 = resource_p->ipa_wdi2;
  5880. ipa3_ctx->ipa_wdi2_over_gsi = resource_p->ipa_wdi2_over_gsi;
  5881. ipa3_ctx->ipa_wdi3_over_gsi = resource_p->ipa_wdi3_over_gsi;
  5882. ipa3_ctx->ipa_fltrt_not_hashable = resource_p->ipa_fltrt_not_hashable;
  5883. ipa3_ctx->use_64_bit_dma_mask = resource_p->use_64_bit_dma_mask;
  5884. ipa3_ctx->wan_rx_ring_size = resource_p->wan_rx_ring_size;
  5885. ipa3_ctx->lan_rx_ring_size = resource_p->lan_rx_ring_size;
  5886. ipa3_ctx->ipa_wan_skb_page = resource_p->ipa_wan_skb_page;
  5887. ipa3_ctx->stats.page_recycle_stats[0].total_replenished = 0;
  5888. ipa3_ctx->stats.page_recycle_stats[0].tmp_alloc = 0;
  5889. ipa3_ctx->stats.page_recycle_stats[1].total_replenished = 0;
  5890. ipa3_ctx->stats.page_recycle_stats[1].tmp_alloc = 0;
  5891. ipa3_ctx->skip_uc_pipe_reset = resource_p->skip_uc_pipe_reset;
  5892. ipa3_ctx->tethered_flow_control = resource_p->tethered_flow_control;
  5893. ipa3_ctx->ee = resource_p->ee;
  5894. ipa3_ctx->gsi_ch20_wa = resource_p->gsi_ch20_wa;
  5895. ipa3_ctx->wdi_over_pcie = resource_p->wdi_over_pcie;
  5896. ipa3_ctx->ipa3_active_clients_logging.log_rdy = false;
  5897. ipa3_ctx->ipa_config_is_mhi = resource_p->ipa_mhi_dynamic_config;
  5898. ipa3_ctx->mhi_evid_limits[0] = resource_p->mhi_evid_limits[0];
  5899. ipa3_ctx->mhi_evid_limits[1] = resource_p->mhi_evid_limits[1];
  5900. ipa3_ctx->entire_ipa_block_size = resource_p->entire_ipa_block_size;
  5901. ipa3_ctx->do_register_collection_on_crash =
  5902. resource_p->do_register_collection_on_crash;
  5903. ipa3_ctx->do_testbus_collection_on_crash =
  5904. resource_p->do_testbus_collection_on_crash;
  5905. ipa3_ctx->do_non_tn_collection_on_crash =
  5906. resource_p->do_non_tn_collection_on_crash;
  5907. ipa3_ctx->secure_debug_check_action =
  5908. resource_p->secure_debug_check_action;
  5909. ipa3_ctx->do_ram_collection_on_crash =
  5910. resource_p->do_ram_collection_on_crash;
  5911. ipa3_ctx->lan_rx_napi_enable = resource_p->lan_rx_napi_enable;
  5912. if (ipa3_ctx->secure_debug_check_action == USE_SCM) {
  5913. if (ipa_is_mem_dump_allowed())
  5914. ipa3_ctx->sd_state = SD_ENABLED;
  5915. else
  5916. ipa3_ctx->sd_state = SD_DISABLED;
  5917. } else {
  5918. if (ipa3_ctx->secure_debug_check_action == OVERRIDE_SCM_TRUE)
  5919. ipa3_ctx->sd_state = SD_ENABLED;
  5920. else
  5921. /* secure_debug_check_action == OVERRIDE_SCM_FALSE */
  5922. ipa3_ctx->sd_state = SD_DISABLED;
  5923. }
  5924. if (ipa3_ctx->sd_state == SD_ENABLED) {
  5925. /* secure debug is enabled. */
  5926. IPADBG("secure debug enabled\n");
  5927. } else {
  5928. /* secure debug is disabled. */
  5929. IPADBG("secure debug disabled\n");
  5930. ipa3_ctx->do_testbus_collection_on_crash = false;
  5931. }
  5932. ipa3_ctx->ipa_endp_delay_wa = resource_p->ipa_endp_delay_wa;
  5933. WARN(ipa3_ctx->ipa3_hw_mode != IPA_HW_MODE_NORMAL,
  5934. "Non NORMAL IPA HW mode, is this emulation platform ?");
  5935. if (resource_p->ipa_tz_unlock_reg) {
  5936. ipa3_ctx->ipa_tz_unlock_reg_num =
  5937. resource_p->ipa_tz_unlock_reg_num;
  5938. ipa3_ctx->ipa_tz_unlock_reg = kcalloc(
  5939. ipa3_ctx->ipa_tz_unlock_reg_num,
  5940. sizeof(*ipa3_ctx->ipa_tz_unlock_reg),
  5941. GFP_KERNEL);
  5942. if (ipa3_ctx->ipa_tz_unlock_reg == NULL) {
  5943. result = -ENOMEM;
  5944. goto fail_tz_unlock_reg;
  5945. }
  5946. for (i = 0; i < ipa3_ctx->ipa_tz_unlock_reg_num; i++) {
  5947. ipa3_ctx->ipa_tz_unlock_reg[i].reg_addr =
  5948. resource_p->ipa_tz_unlock_reg[i].reg_addr;
  5949. ipa3_ctx->ipa_tz_unlock_reg[i].size =
  5950. resource_p->ipa_tz_unlock_reg[i].size;
  5951. }
  5952. /* unlock registers for uc */
  5953. result = ipa3_tz_unlock_reg(ipa3_ctx->ipa_tz_unlock_reg,
  5954. ipa3_ctx->ipa_tz_unlock_reg_num);
  5955. if (result)
  5956. IPAERR("Failed to unlock memory region using TZ\n");
  5957. }
  5958. /* default aggregation parameters */
  5959. ipa3_ctx->aggregation_type = IPA_MBIM_16;
  5960. ipa3_ctx->aggregation_byte_limit = 1;
  5961. ipa3_ctx->aggregation_time_limit = 0;
  5962. /* configure interconnect parameters */
  5963. ipa3_ctx->icc_num_cases = resource_p->icc_num_cases;
  5964. ipa3_ctx->icc_num_paths = resource_p->icc_num_paths;
  5965. for (i = 0; i < ipa3_ctx->icc_num_cases; i++) {
  5966. for (j = 0; j < ipa3_ctx->icc_num_paths; j++) {
  5967. ipa3_ctx->icc_clk[i][j][IPA_ICC_AB] =
  5968. resource_p->icc_clk_val[i][j*IPA_ICC_TYPE_MAX];
  5969. ipa3_ctx->icc_clk[i][j][IPA_ICC_IB] =
  5970. resource_p->icc_clk_val[i][j*IPA_ICC_TYPE_MAX+1];
  5971. }
  5972. }
  5973. ipa3_ctx->ctrl = kzalloc(sizeof(*ipa3_ctx->ctrl), GFP_KERNEL);
  5974. if (!ipa3_ctx->ctrl) {
  5975. result = -ENOMEM;
  5976. goto fail_mem_ctrl;
  5977. }
  5978. result = ipa3_controller_static_bind(ipa3_ctx->ctrl,
  5979. ipa3_ctx->ipa_hw_type);
  5980. if (result) {
  5981. IPAERR("fail to static bind IPA ctrl\n");
  5982. result = -EFAULT;
  5983. goto fail_bind;
  5984. }
  5985. result = ipa3_init_mem_partition(ipa3_ctx->ipa_hw_type);
  5986. if (result) {
  5987. IPAERR(":ipa3_init_mem_partition failed\n");
  5988. result = -ENODEV;
  5989. goto fail_init_mem_partition;
  5990. }
  5991. if (ipa3_ctx->ipa3_hw_mode != IPA_HW_MODE_VIRTUAL &&
  5992. ipa3_ctx->ipa3_hw_mode != IPA_HW_MODE_EMULATION) {
  5993. /* get BUS handle */
  5994. for (i = 0; i < ipa3_ctx->icc_num_paths; i++) {
  5995. ipa3_ctx->ctrl->icc_path[i] = of_icc_get(
  5996. &ipa3_ctx->master_pdev->dev,
  5997. resource_p->icc_path_name[i]);
  5998. if (IS_ERR(ipa3_ctx->ctrl->icc_path[i])) {
  5999. IPAERR("fail to register with bus mgr!\n");
  6000. result = PTR_ERR(ipa3_ctx->ctrl->icc_path[i]);
  6001. if (result != -EPROBE_DEFER) {
  6002. IPAERR("Failed to get path %s\n",
  6003. ipa3_ctx->master_pdev->name);
  6004. }
  6005. goto fail_bus_reg;
  6006. }
  6007. }
  6008. }
  6009. /* get IPA clocks */
  6010. result = ipa3_get_clks(&ipa3_ctx->master_pdev->dev);
  6011. if (result)
  6012. goto fail_bus_reg;
  6013. /* init active_clients_log after getting ipa-clk */
  6014. result = ipa3_active_clients_log_init();
  6015. if (result)
  6016. goto fail_init_active_client;
  6017. /* Enable ipa3_ctx->enable_clock_scaling */
  6018. ipa3_ctx->enable_clock_scaling = 1;
  6019. /* vote for svs2 on bootup */
  6020. ipa3_ctx->curr_ipa_clk_rate = ipa3_ctx->ctrl->ipa_clk_rate_svs2;
  6021. /* Enable ipa3_ctx->enable_napi_chain */
  6022. ipa3_ctx->enable_napi_chain = 1;
  6023. /* assume clock is on in virtual/emulation mode */
  6024. if (ipa3_ctx->ipa3_hw_mode == IPA_HW_MODE_VIRTUAL ||
  6025. ipa3_ctx->ipa3_hw_mode == IPA_HW_MODE_EMULATION)
  6026. atomic_set(&ipa3_ctx->ipa_clk_vote, 1);
  6027. /* enable IPA clocks explicitly to allow the initialization */
  6028. ipa3_enable_clks();
  6029. /* setup IPA register access */
  6030. IPADBG("Mapping 0x%x\n", resource_p->ipa_mem_base +
  6031. ipa3_ctx->ctrl->ipa_reg_base_ofst);
  6032. ipa3_ctx->mmio = ioremap(resource_p->ipa_mem_base +
  6033. ipa3_ctx->ctrl->ipa_reg_base_ofst,
  6034. resource_p->ipa_mem_size);
  6035. if (!ipa3_ctx->mmio) {
  6036. IPAERR(":ipa-base ioremap err\n");
  6037. result = -EFAULT;
  6038. goto fail_remap;
  6039. }
  6040. IPADBG(
  6041. "base(0x%x)+offset(0x%x)=(0x%x) mapped to (%pK) with len (0x%x)\n",
  6042. resource_p->ipa_mem_base,
  6043. ipa3_ctx->ctrl->ipa_reg_base_ofst,
  6044. resource_p->ipa_mem_base + ipa3_ctx->ctrl->ipa_reg_base_ofst,
  6045. ipa3_ctx->mmio,
  6046. resource_p->ipa_mem_size);
  6047. /*
  6048. * Setup access for register collection/dump on crash
  6049. */
  6050. if (ipa_reg_save_init(IPA_MEM_INIT_VAL) != 0) {
  6051. result = -EFAULT;
  6052. goto fail_gsi_map;
  6053. }
  6054. /*
  6055. * Since we now know where the transport's registers live,
  6056. * let's set up access to them. This is done since subseqent
  6057. * functions, that deal with the transport, require the
  6058. * access.
  6059. */
  6060. if (gsi_map_base(
  6061. ipa3_res.transport_mem_base,
  6062. ipa3_res.transport_mem_size) != 0) {
  6063. IPAERR("Allocation of gsi base failed\n");
  6064. result = -EFAULT;
  6065. goto fail_gsi_map;
  6066. }
  6067. mutex_init(&ipa3_ctx->ipa3_active_clients.mutex);
  6068. IPA_ACTIVE_CLIENTS_PREP_SPECIAL(log_info, "PROXY_CLK_VOTE");
  6069. ipa3_active_clients_log_inc(&log_info, false);
  6070. ipa3_ctx->q6_proxy_clk_vote_valid = true;
  6071. ipa3_ctx->q6_proxy_clk_vote_cnt = 1;
  6072. /*Updating the proxy vote cnt 1 */
  6073. atomic_set(&ipa3_ctx->ipa3_active_clients.cnt, 1);
  6074. /* Create workqueues for power management */
  6075. ipa3_ctx->power_mgmt_wq =
  6076. create_singlethread_workqueue("ipa_power_mgmt");
  6077. if (!ipa3_ctx->power_mgmt_wq) {
  6078. IPAERR("failed to create power mgmt wq\n");
  6079. result = -ENOMEM;
  6080. goto fail_init_hw;
  6081. }
  6082. ipa3_ctx->transport_power_mgmt_wq =
  6083. create_singlethread_workqueue("transport_power_mgmt");
  6084. if (!ipa3_ctx->transport_power_mgmt_wq) {
  6085. IPAERR("failed to create transport power mgmt wq\n");
  6086. result = -ENOMEM;
  6087. goto fail_create_transport_wq;
  6088. }
  6089. mutex_init(&ipa3_ctx->transport_pm.transport_pm_mutex);
  6090. /* init the lookaside cache */
  6091. ipa3_ctx->flt_rule_cache = kmem_cache_create("IPA_FLT",
  6092. sizeof(struct ipa3_flt_entry), 0, 0, NULL);
  6093. if (!ipa3_ctx->flt_rule_cache) {
  6094. IPAERR(":ipa flt cache create failed\n");
  6095. result = -ENOMEM;
  6096. goto fail_flt_rule_cache;
  6097. }
  6098. ipa3_ctx->rt_rule_cache = kmem_cache_create("IPA_RT",
  6099. sizeof(struct ipa3_rt_entry), 0, 0, NULL);
  6100. if (!ipa3_ctx->rt_rule_cache) {
  6101. IPAERR(":ipa rt cache create failed\n");
  6102. result = -ENOMEM;
  6103. goto fail_rt_rule_cache;
  6104. }
  6105. ipa3_ctx->hdr_cache = kmem_cache_create("IPA_HDR",
  6106. sizeof(struct ipa3_hdr_entry), 0, 0, NULL);
  6107. if (!ipa3_ctx->hdr_cache) {
  6108. IPAERR(":ipa hdr cache create failed\n");
  6109. result = -ENOMEM;
  6110. goto fail_hdr_cache;
  6111. }
  6112. ipa3_ctx->hdr_offset_cache =
  6113. kmem_cache_create("IPA_HDR_OFFSET",
  6114. sizeof(struct ipa_hdr_offset_entry), 0, 0, NULL);
  6115. if (!ipa3_ctx->hdr_offset_cache) {
  6116. IPAERR(":ipa hdr off cache create failed\n");
  6117. result = -ENOMEM;
  6118. goto fail_hdr_offset_cache;
  6119. }
  6120. ipa3_ctx->hdr_proc_ctx_cache = kmem_cache_create("IPA_HDR_PROC_CTX",
  6121. sizeof(struct ipa3_hdr_proc_ctx_entry), 0, 0, NULL);
  6122. if (!ipa3_ctx->hdr_proc_ctx_cache) {
  6123. IPAERR(":ipa hdr proc ctx cache create failed\n");
  6124. result = -ENOMEM;
  6125. goto fail_hdr_proc_ctx_cache;
  6126. }
  6127. ipa3_ctx->hdr_proc_ctx_offset_cache =
  6128. kmem_cache_create("IPA_HDR_PROC_CTX_OFFSET",
  6129. sizeof(struct ipa3_hdr_proc_ctx_offset_entry), 0, 0, NULL);
  6130. if (!ipa3_ctx->hdr_proc_ctx_offset_cache) {
  6131. IPAERR(":ipa hdr proc ctx off cache create failed\n");
  6132. result = -ENOMEM;
  6133. goto fail_hdr_proc_ctx_offset_cache;
  6134. }
  6135. ipa3_ctx->rt_tbl_cache = kmem_cache_create("IPA_RT_TBL",
  6136. sizeof(struct ipa3_rt_tbl), 0, 0, NULL);
  6137. if (!ipa3_ctx->rt_tbl_cache) {
  6138. IPAERR(":ipa rt tbl cache create failed\n");
  6139. result = -ENOMEM;
  6140. goto fail_rt_tbl_cache;
  6141. }
  6142. ipa3_ctx->tx_pkt_wrapper_cache =
  6143. kmem_cache_create("IPA_TX_PKT_WRAPPER",
  6144. sizeof(struct ipa3_tx_pkt_wrapper), 0, 0, NULL);
  6145. if (!ipa3_ctx->tx_pkt_wrapper_cache) {
  6146. IPAERR(":ipa tx pkt wrapper cache create failed\n");
  6147. result = -ENOMEM;
  6148. goto fail_tx_pkt_wrapper_cache;
  6149. }
  6150. ipa3_ctx->rx_pkt_wrapper_cache =
  6151. kmem_cache_create("IPA_RX_PKT_WRAPPER",
  6152. sizeof(struct ipa3_rx_pkt_wrapper), 0, 0, NULL);
  6153. if (!ipa3_ctx->rx_pkt_wrapper_cache) {
  6154. IPAERR(":ipa rx pkt wrapper cache create failed\n");
  6155. result = -ENOMEM;
  6156. goto fail_rx_pkt_wrapper_cache;
  6157. }
  6158. /* init the various list heads */
  6159. INIT_LIST_HEAD(&ipa3_ctx->hdr_tbl.head_hdr_entry_list);
  6160. for (i = 0; i < IPA_HDR_BIN_MAX; i++) {
  6161. INIT_LIST_HEAD(&ipa3_ctx->hdr_tbl.head_offset_list[i]);
  6162. INIT_LIST_HEAD(&ipa3_ctx->hdr_tbl.head_free_offset_list[i]);
  6163. }
  6164. INIT_LIST_HEAD(&ipa3_ctx->hdr_proc_ctx_tbl.head_proc_ctx_entry_list);
  6165. for (i = 0; i < IPA_HDR_PROC_CTX_BIN_MAX; i++) {
  6166. INIT_LIST_HEAD(
  6167. &ipa3_ctx->hdr_proc_ctx_tbl.head_offset_list[i]);
  6168. INIT_LIST_HEAD(
  6169. &ipa3_ctx->hdr_proc_ctx_tbl.head_free_offset_list[i]);
  6170. }
  6171. INIT_LIST_HEAD(&ipa3_ctx->rt_tbl_set[IPA_IP_v4].head_rt_tbl_list);
  6172. idr_init(&ipa3_ctx->rt_tbl_set[IPA_IP_v4].rule_ids);
  6173. INIT_LIST_HEAD(&ipa3_ctx->rt_tbl_set[IPA_IP_v6].head_rt_tbl_list);
  6174. idr_init(&ipa3_ctx->rt_tbl_set[IPA_IP_v6].rule_ids);
  6175. rset = &ipa3_ctx->reap_rt_tbl_set[IPA_IP_v4];
  6176. INIT_LIST_HEAD(&rset->head_rt_tbl_list);
  6177. idr_init(&rset->rule_ids);
  6178. rset = &ipa3_ctx->reap_rt_tbl_set[IPA_IP_v6];
  6179. INIT_LIST_HEAD(&rset->head_rt_tbl_list);
  6180. idr_init(&rset->rule_ids);
  6181. idr_init(&ipa3_ctx->flt_rt_counters.hdl);
  6182. spin_lock_init(&ipa3_ctx->flt_rt_counters.hdl_lock);
  6183. memset(&ipa3_ctx->flt_rt_counters.used_hw, 0,
  6184. sizeof(ipa3_ctx->flt_rt_counters.used_hw));
  6185. memset(&ipa3_ctx->flt_rt_counters.used_sw, 0,
  6186. sizeof(ipa3_ctx->flt_rt_counters.used_sw));
  6187. INIT_LIST_HEAD(&ipa3_ctx->intf_list);
  6188. INIT_LIST_HEAD(&ipa3_ctx->msg_list);
  6189. INIT_LIST_HEAD(&ipa3_ctx->pull_msg_list);
  6190. init_waitqueue_head(&ipa3_ctx->msg_waitq);
  6191. mutex_init(&ipa3_ctx->msg_lock);
  6192. /* store wlan client-connect-msg-list */
  6193. INIT_LIST_HEAD(&ipa3_ctx->msg_wlan_client_list);
  6194. mutex_init(&ipa3_ctx->msg_wlan_client_lock);
  6195. mutex_init(&ipa3_ctx->lock);
  6196. mutex_init(&ipa3_ctx->q6_proxy_clk_vote_mutex);
  6197. mutex_init(&ipa3_ctx->ipa_cne_evt_lock);
  6198. idr_init(&ipa3_ctx->ipa_idr);
  6199. spin_lock_init(&ipa3_ctx->idr_lock);
  6200. /* wlan related member */
  6201. memset(&ipa3_ctx->wc_memb, 0, sizeof(ipa3_ctx->wc_memb));
  6202. spin_lock_init(&ipa3_ctx->wc_memb.wlan_spinlock);
  6203. spin_lock_init(&ipa3_ctx->wc_memb.ipa_tx_mul_spinlock);
  6204. INIT_LIST_HEAD(&ipa3_ctx->wc_memb.wlan_comm_desc_list);
  6205. ipa3_ctx->cdev.class = class_create(THIS_MODULE, DRV_NAME);
  6206. result = alloc_chrdev_region(&ipa3_ctx->cdev.dev_num, 0, 1, DRV_NAME);
  6207. if (result) {
  6208. IPAERR("alloc_chrdev_region err\n");
  6209. result = -ENODEV;
  6210. goto fail_alloc_chrdev_region;
  6211. }
  6212. ipa3_ctx->cdev.dev = device_create(ipa3_ctx->cdev.class, NULL,
  6213. ipa3_ctx->cdev.dev_num, ipa3_ctx, DRV_NAME);
  6214. if (IS_ERR(ipa3_ctx->cdev.dev)) {
  6215. IPAERR(":device_create err.\n");
  6216. result = -ENODEV;
  6217. goto fail_device_create;
  6218. }
  6219. /* Register a wakeup source. */
  6220. ipa3_ctx->w_lock =
  6221. wakeup_source_register(&ipa_pdev->dev, "IPA_WS");
  6222. if (!ipa3_ctx->w_lock) {
  6223. IPAERR("IPA wakeup source register failed\n");
  6224. result = -ENOMEM;
  6225. goto fail_w_source_register;
  6226. }
  6227. spin_lock_init(&ipa3_ctx->wakelock_ref_cnt.spinlock);
  6228. /* Initialize Power Management framework */
  6229. result = ipa_pm_init(&ipa3_res.pm_init);
  6230. if (result) {
  6231. IPAERR("IPA PM initialization failed (%d)\n", -result);
  6232. result = -ENODEV;
  6233. goto fail_ipa_pm_init;
  6234. }
  6235. IPADBG("IPA power manager initialized\n");
  6236. INIT_LIST_HEAD(&ipa3_ctx->ipa_ready_cb_list);
  6237. init_completion(&ipa3_ctx->init_completion_obj);
  6238. init_completion(&ipa3_ctx->uc_loaded_completion_obj);
  6239. result = ipa3_dma_setup();
  6240. if (result) {
  6241. IPAERR("Failed to setup IPA DMA\n");
  6242. result = -ENODEV;
  6243. goto fail_ipa_dma_setup;
  6244. }
  6245. /*
  6246. * We can't register the GSI driver yet, as it expects
  6247. * the GSI FW to be up and running before the registration.
  6248. *
  6249. * For IPA3.0 and the emulation system, the GSI configuration
  6250. * is done by the GSI driver.
  6251. *
  6252. * For IPA3.1 (and on), the GSI configuration is done by TZ.
  6253. */
  6254. if (ipa3_ctx->ipa_hw_type == IPA_HW_v3_0 ||
  6255. ipa3_ctx->ipa3_hw_mode == IPA_HW_MODE_EMULATION) {
  6256. result = ipa3_gsi_pre_fw_load_init();
  6257. if (result) {
  6258. IPAERR("gsi pre FW loading config failed\n");
  6259. result = -ENODEV;
  6260. goto fail_gsi_pre_fw_load_init;
  6261. }
  6262. }
  6263. cdev = &ipa3_ctx->cdev.cdev;
  6264. cdev_init(cdev, &ipa3_drv_fops);
  6265. cdev->owner = THIS_MODULE;
  6266. cdev->ops = &ipa3_drv_fops; /* from LDD3 */
  6267. result = cdev_add(cdev, ipa3_ctx->cdev.dev_num, 1);
  6268. if (result) {
  6269. IPAERR(":cdev_add err=%d\n", -result);
  6270. result = -ENODEV;
  6271. goto fail_cdev_add;
  6272. }
  6273. IPADBG("ipa cdev added successful. major:%d minor:%d\n",
  6274. MAJOR(ipa3_ctx->cdev.dev_num),
  6275. MINOR(ipa3_ctx->cdev.dev_num));
  6276. if (ipa3_ctx->ipa_hw_type >= IPA_HW_v4_1) {
  6277. result = ipa_odl_init();
  6278. if (result) {
  6279. IPADBG("Error: ODL init fialed\n");
  6280. result = -ENODEV;
  6281. goto fail_cdev_add;
  6282. }
  6283. }
  6284. /*
  6285. * for IPA 4.0 offline charge is not needed and we need to prevent
  6286. * power collapse until IPA uC is loaded.
  6287. */
  6288. /* proxy vote for modem is added in ipa3_post_init() phase */
  6289. if (ipa3_ctx->ipa_hw_type != IPA_HW_v4_0)
  6290. ipa3_proxy_clk_unvote();
  6291. /* Create the dummy netdev for LAN RX NAPI*/
  6292. ipa3_enable_napi_netdev();
  6293. ipa3_wwan_init();
  6294. mutex_init(&ipa3_ctx->app_clock_vote.mutex);
  6295. return 0;
  6296. fail_cdev_add:
  6297. fail_gsi_pre_fw_load_init:
  6298. ipa3_dma_shutdown();
  6299. fail_ipa_dma_setup:
  6300. ipa_pm_destroy();
  6301. fail_w_source_register:
  6302. device_destroy(ipa3_ctx->cdev.class, ipa3_ctx->cdev.dev_num);
  6303. fail_ipa_pm_init:
  6304. wakeup_source_unregister(ipa3_ctx->w_lock);
  6305. ipa3_ctx->w_lock = NULL;
  6306. fail_device_create:
  6307. unregister_chrdev_region(ipa3_ctx->cdev.dev_num, 1);
  6308. fail_alloc_chrdev_region:
  6309. idr_destroy(&ipa3_ctx->ipa_idr);
  6310. rset = &ipa3_ctx->reap_rt_tbl_set[IPA_IP_v6];
  6311. idr_destroy(&rset->rule_ids);
  6312. rset = &ipa3_ctx->reap_rt_tbl_set[IPA_IP_v4];
  6313. idr_destroy(&rset->rule_ids);
  6314. idr_destroy(&ipa3_ctx->rt_tbl_set[IPA_IP_v6].rule_ids);
  6315. idr_destroy(&ipa3_ctx->rt_tbl_set[IPA_IP_v4].rule_ids);
  6316. kmem_cache_destroy(ipa3_ctx->rx_pkt_wrapper_cache);
  6317. fail_rx_pkt_wrapper_cache:
  6318. kmem_cache_destroy(ipa3_ctx->tx_pkt_wrapper_cache);
  6319. fail_tx_pkt_wrapper_cache:
  6320. kmem_cache_destroy(ipa3_ctx->rt_tbl_cache);
  6321. fail_rt_tbl_cache:
  6322. kmem_cache_destroy(ipa3_ctx->hdr_proc_ctx_offset_cache);
  6323. fail_hdr_proc_ctx_offset_cache:
  6324. kmem_cache_destroy(ipa3_ctx->hdr_proc_ctx_cache);
  6325. fail_hdr_proc_ctx_cache:
  6326. kmem_cache_destroy(ipa3_ctx->hdr_offset_cache);
  6327. fail_hdr_offset_cache:
  6328. kmem_cache_destroy(ipa3_ctx->hdr_cache);
  6329. fail_hdr_cache:
  6330. kmem_cache_destroy(ipa3_ctx->rt_rule_cache);
  6331. fail_rt_rule_cache:
  6332. kmem_cache_destroy(ipa3_ctx->flt_rule_cache);
  6333. fail_flt_rule_cache:
  6334. destroy_workqueue(ipa3_ctx->transport_power_mgmt_wq);
  6335. fail_create_transport_wq:
  6336. destroy_workqueue(ipa3_ctx->power_mgmt_wq);
  6337. fail_init_hw:
  6338. gsi_unmap_base();
  6339. fail_gsi_map:
  6340. if (ipa3_ctx->reg_collection_base)
  6341. iounmap(ipa3_ctx->reg_collection_base);
  6342. iounmap(ipa3_ctx->mmio);
  6343. fail_remap:
  6344. ipa3_disable_clks();
  6345. ipa3_active_clients_log_destroy();
  6346. fail_init_active_client:
  6347. if (ipa3_clk)
  6348. clk_put(ipa3_clk);
  6349. ipa3_clk = NULL;
  6350. fail_bus_reg:
  6351. for (i = 0; i < ipa3_ctx->icc_num_paths; i++)
  6352. if (ipa3_ctx->ctrl->icc_path[i]) {
  6353. icc_put(ipa3_ctx->ctrl->icc_path[i]);
  6354. ipa3_ctx->ctrl->icc_path[i] = NULL;
  6355. }
  6356. fail_init_mem_partition:
  6357. fail_bind:
  6358. kfree(ipa3_ctx->ctrl);
  6359. fail_mem_ctrl:
  6360. kfree(ipa3_ctx->ipa_tz_unlock_reg);
  6361. fail_tz_unlock_reg:
  6362. if (ipa3_ctx->logbuf)
  6363. ipc_log_context_destroy(ipa3_ctx->logbuf);
  6364. kfree(ipa3_ctx);
  6365. ipa3_ctx = NULL;
  6366. fail_mem_ctx:
  6367. return result;
  6368. }
  6369. static int get_ipa_dts_pm_info(struct platform_device *pdev,
  6370. struct ipa3_plat_drv_res *ipa_drv_res)
  6371. {
  6372. int result;
  6373. int i, j;
  6374. /* this interconnects entry must be presented */
  6375. if (!of_find_property(pdev->dev.of_node,
  6376. "interconnects", NULL)) {
  6377. IPAERR("No interconnect info\n");
  6378. return -EFAULT;
  6379. }
  6380. result = of_property_read_u32(pdev->dev.of_node,
  6381. "qcom,interconnect,num-cases",
  6382. &ipa_drv_res->icc_num_cases);
  6383. /* No vote is ignored */
  6384. ipa_drv_res->pm_init.threshold_size =
  6385. ipa_drv_res->icc_num_cases - 2;
  6386. if (result || ipa_drv_res->pm_init.threshold_size >
  6387. IPA_PM_THRESHOLD_MAX) {
  6388. IPAERR("invalid qcom,interconnect,num-cases %d\n",
  6389. ipa_drv_res->pm_init.threshold_size);
  6390. return -EFAULT;
  6391. }
  6392. result = of_property_read_u32(pdev->dev.of_node,
  6393. "qcom,interconnect,num-paths",
  6394. &ipa_drv_res->icc_num_paths);
  6395. if (result || ipa_drv_res->icc_num_paths >
  6396. IPA_ICC_PATH_MAX) {
  6397. IPAERR("invalid qcom,interconnect,num-paths %d\n",
  6398. ipa_drv_res->icc_num_paths);
  6399. return -EFAULT;
  6400. }
  6401. for (i = 0; i < ipa_drv_res->icc_num_paths; i++) {
  6402. result = of_property_read_string_index(pdev->dev.of_node,
  6403. "interconnect-names",
  6404. i,
  6405. &ipa_drv_res->icc_path_name[i]);
  6406. if (result) {
  6407. IPAERR("invalid interconnect-names %d\n", i);
  6408. return -EFAULT;
  6409. }
  6410. }
  6411. /* read no-vote AB IB value */
  6412. result = of_property_read_u32_array(pdev->dev.of_node,
  6413. "qcom,no-vote",
  6414. ipa_drv_res->icc_clk_val[IPA_ICC_NONE],
  6415. ipa_drv_res->icc_num_paths *
  6416. IPA_ICC_TYPE_MAX);
  6417. if (result) {
  6418. IPAERR("invalid property qcom,no-vote\n");
  6419. return -EFAULT;
  6420. }
  6421. /* read svs2 AB IB value */
  6422. result = of_property_read_u32_array(pdev->dev.of_node,
  6423. "qcom,svs2",
  6424. ipa_drv_res->icc_clk_val[IPA_ICC_SVS2],
  6425. ipa_drv_res->icc_num_paths *
  6426. IPA_ICC_TYPE_MAX);
  6427. if (result) {
  6428. IPAERR("invalid property qcom,svs2\n");
  6429. return -EFAULT;
  6430. }
  6431. /* read svs AB IB value */
  6432. result = of_property_read_u32_array(pdev->dev.of_node,
  6433. "qcom,svs",
  6434. ipa_drv_res->icc_clk_val[IPA_ICC_SVS],
  6435. ipa_drv_res->icc_num_paths *
  6436. IPA_ICC_TYPE_MAX);
  6437. if (result) {
  6438. IPAERR("invalid property qcom,svs\n");
  6439. return -EFAULT;
  6440. }
  6441. /* read nominal AB IB value */
  6442. result = of_property_read_u32_array(pdev->dev.of_node,
  6443. "qcom,nominal",
  6444. ipa_drv_res->icc_clk_val[IPA_ICC_NOMINAL],
  6445. ipa_drv_res->icc_num_paths *
  6446. IPA_ICC_TYPE_MAX);
  6447. if (result) {
  6448. IPAERR("invalid property qcom,nominal\n");
  6449. return -EFAULT;
  6450. }
  6451. /* read turbo AB IB value */
  6452. result = of_property_read_u32_array(pdev->dev.of_node,
  6453. "qcom,turbo",
  6454. ipa_drv_res->icc_clk_val[IPA_ICC_TURBO],
  6455. ipa_drv_res->icc_num_paths *
  6456. IPA_ICC_TYPE_MAX);
  6457. if (result) {
  6458. IPAERR("invalid property qcom,turbo\n");
  6459. return -EFAULT;
  6460. }
  6461. result = of_property_read_u32_array(pdev->dev.of_node,
  6462. "qcom,throughput-threshold",
  6463. ipa_drv_res->pm_init.default_threshold,
  6464. ipa_drv_res->pm_init.threshold_size);
  6465. if (result) {
  6466. IPAERR("failed to read qcom,throughput-thresholds\n");
  6467. return -EFAULT;
  6468. }
  6469. result = of_property_count_strings(pdev->dev.of_node,
  6470. "qcom,scaling-exceptions");
  6471. if (result < 0) {
  6472. IPADBG("no exception list for ipa pm\n");
  6473. result = 0;
  6474. }
  6475. if (result % (ipa_drv_res->pm_init.threshold_size + 1)) {
  6476. IPAERR("failed to read qcom,scaling-exceptions\n");
  6477. return -EFAULT;
  6478. }
  6479. ipa_drv_res->pm_init.exception_size = result /
  6480. (ipa_drv_res->pm_init.threshold_size + 1);
  6481. if (ipa_drv_res->pm_init.exception_size >=
  6482. IPA_PM_EXCEPTION_MAX) {
  6483. IPAERR("exception list larger then max %d\n",
  6484. ipa_drv_res->pm_init.exception_size);
  6485. return -EFAULT;
  6486. }
  6487. for (i = 0; i < ipa_drv_res->pm_init.exception_size; i++) {
  6488. struct ipa_pm_exception *ex = ipa_drv_res->pm_init.exceptions;
  6489. result = of_property_read_string_index(pdev->dev.of_node,
  6490. "qcom,scaling-exceptions",
  6491. i * (ipa_drv_res->pm_init.threshold_size + 1),
  6492. &ex[i].usecase);
  6493. if (result) {
  6494. IPAERR("failed to read qcom,scaling-exceptions");
  6495. return -EFAULT;
  6496. }
  6497. for (j = 0; j < ipa_drv_res->pm_init.threshold_size; j++) {
  6498. const char *str;
  6499. result = of_property_read_string_index(
  6500. pdev->dev.of_node,
  6501. "qcom,scaling-exceptions",
  6502. i * (ipa_drv_res->pm_init.threshold_size + 1)
  6503. + j + 1,
  6504. &str);
  6505. if (result) {
  6506. IPAERR("failed to read qcom,scaling-exceptions"
  6507. );
  6508. return -EFAULT;
  6509. }
  6510. if (kstrtou32(str, 0, &ex[i].threshold[j])) {
  6511. IPAERR("error str=%s\n", str);
  6512. return -EFAULT;
  6513. }
  6514. }
  6515. }
  6516. return 0;
  6517. }
  6518. static int get_ipa_dts_configuration(struct platform_device *pdev,
  6519. struct ipa3_plat_drv_res *ipa_drv_res)
  6520. {
  6521. int i, result, pos;
  6522. struct resource *resource;
  6523. u32 *ipa_tz_unlock_reg;
  6524. int elem_num;
  6525. u32 mhi_evid_limits[2];
  6526. /* initialize ipa3_res */
  6527. ipa_drv_res->ipa_pipe_mem_start_ofst = IPA_PIPE_MEM_START_OFST;
  6528. ipa_drv_res->ipa_pipe_mem_size = IPA_PIPE_MEM_SIZE;
  6529. ipa_drv_res->ipa_hw_type = 0;
  6530. ipa_drv_res->ipa3_hw_mode = 0;
  6531. ipa_drv_res->platform_type = 0;
  6532. ipa_drv_res->modem_cfg_emb_pipe_flt = false;
  6533. ipa_drv_res->ipa_wdi2 = false;
  6534. ipa_drv_res->ipa_wan_skb_page = false;
  6535. ipa_drv_res->ipa_wdi2_over_gsi = false;
  6536. ipa_drv_res->ipa_wdi3_over_gsi = false;
  6537. ipa_drv_res->ipa_mhi_dynamic_config = false;
  6538. ipa_drv_res->use_64_bit_dma_mask = false;
  6539. ipa_drv_res->use_bw_vote = false;
  6540. ipa_drv_res->wan_rx_ring_size = IPA_GENERIC_RX_POOL_SZ;
  6541. ipa_drv_res->lan_rx_ring_size = IPA_GENERIC_RX_POOL_SZ;
  6542. ipa_drv_res->apply_rg10_wa = false;
  6543. ipa_drv_res->gsi_ch20_wa = false;
  6544. ipa_drv_res->ipa_tz_unlock_reg_num = 0;
  6545. ipa_drv_res->ipa_tz_unlock_reg = NULL;
  6546. ipa_drv_res->mhi_evid_limits[0] = IPA_MHI_GSI_EVENT_RING_ID_START;
  6547. ipa_drv_res->mhi_evid_limits[1] = IPA_MHI_GSI_EVENT_RING_ID_END;
  6548. ipa_drv_res->ipa_fltrt_not_hashable = false;
  6549. ipa_drv_res->ipa_endp_delay_wa = false;
  6550. ipa_drv_res->skip_ieob_mask_wa = false;
  6551. /* Get IPA HW Version */
  6552. result = of_property_read_u32(pdev->dev.of_node, "qcom,ipa-hw-ver",
  6553. &ipa_drv_res->ipa_hw_type);
  6554. if ((result) || (ipa_drv_res->ipa_hw_type == 0)) {
  6555. IPAERR(":get resource failed for ipa-hw-ver\n");
  6556. return -ENODEV;
  6557. }
  6558. IPADBG(": ipa_hw_type = %d", ipa_drv_res->ipa_hw_type);
  6559. if (ipa_drv_res->ipa_hw_type < IPA_HW_v3_0) {
  6560. IPAERR(":IPA version below 3.0 not supported\n");
  6561. return -ENODEV;
  6562. }
  6563. if (ipa_drv_res->ipa_hw_type >= IPA_HW_MAX) {
  6564. IPAERR(":IPA version is greater than the MAX\n");
  6565. return -ENODEV;
  6566. }
  6567. /* Get IPA HW mode */
  6568. result = of_property_read_u32(pdev->dev.of_node, "qcom,ipa-hw-mode",
  6569. &ipa_drv_res->ipa3_hw_mode);
  6570. if (result)
  6571. IPADBG("using default (IPA_MODE_NORMAL) for ipa-hw-mode\n");
  6572. else
  6573. IPADBG(": found ipa_drv_res->ipa3_hw_mode = %d",
  6574. ipa_drv_res->ipa3_hw_mode);
  6575. /* Get Platform Type */
  6576. result = of_property_read_u32(pdev->dev.of_node, "qcom,platform-type",
  6577. &ipa_drv_res->platform_type);
  6578. if (result)
  6579. IPADBG("using default (IPA_PLAT_TYPE_MDM) for platform-type\n");
  6580. else
  6581. IPADBG(": found ipa_drv_res->platform_type = %d",
  6582. ipa_drv_res->platform_type);
  6583. /* Get IPA WAN / LAN RX pool size */
  6584. result = of_property_read_u32(pdev->dev.of_node,
  6585. "qcom,wan-rx-ring-size",
  6586. &ipa_drv_res->wan_rx_ring_size);
  6587. if (result)
  6588. IPADBG("using default for wan-rx-ring-size = %u\n",
  6589. ipa_drv_res->wan_rx_ring_size);
  6590. else
  6591. IPADBG(": found ipa_drv_res->wan-rx-ring-size = %u",
  6592. ipa_drv_res->wan_rx_ring_size);
  6593. result = of_property_read_u32(pdev->dev.of_node,
  6594. "qcom,lan-rx-ring-size",
  6595. &ipa_drv_res->lan_rx_ring_size);
  6596. if (result)
  6597. IPADBG("using default for lan-rx-ring-size = %u\n",
  6598. ipa_drv_res->lan_rx_ring_size);
  6599. else
  6600. IPADBG(": found ipa_drv_res->lan-rx-ring-size = %u",
  6601. ipa_drv_res->lan_rx_ring_size);
  6602. ipa_drv_res->use_ipa_teth_bridge =
  6603. of_property_read_bool(pdev->dev.of_node,
  6604. "qcom,use-ipa-tethering-bridge");
  6605. IPADBG(": using ipa teth bridge = %s",
  6606. ipa_drv_res->use_ipa_teth_bridge
  6607. ? "True" : "False");
  6608. ipa_drv_res->ipa_mhi_dynamic_config =
  6609. of_property_read_bool(pdev->dev.of_node,
  6610. "qcom,use-ipa-in-mhi-mode");
  6611. IPADBG(": ipa_mhi_dynamic_config (%s)\n",
  6612. ipa_drv_res->ipa_mhi_dynamic_config
  6613. ? "True" : "False");
  6614. ipa_drv_res->modem_cfg_emb_pipe_flt =
  6615. of_property_read_bool(pdev->dev.of_node,
  6616. "qcom,modem-cfg-emb-pipe-flt");
  6617. IPADBG(": modem configure embedded pipe filtering = %s\n",
  6618. ipa_drv_res->modem_cfg_emb_pipe_flt
  6619. ? "True" : "False");
  6620. ipa_drv_res->ipa_wdi2_over_gsi =
  6621. of_property_read_bool(pdev->dev.of_node,
  6622. "qcom,ipa-wdi2_over_gsi");
  6623. IPADBG(": WDI-2.0 over gsi= %s\n",
  6624. ipa_drv_res->ipa_wdi2_over_gsi
  6625. ? "True" : "False");
  6626. ipa_drv_res->ipa_endp_delay_wa =
  6627. of_property_read_bool(pdev->dev.of_node,
  6628. "qcom,ipa-endp-delay-wa");
  6629. IPADBG(": endppoint delay wa = %s\n",
  6630. ipa_drv_res->ipa_endp_delay_wa
  6631. ? "True" : "False");
  6632. ipa_drv_res->ipa_wdi3_over_gsi =
  6633. of_property_read_bool(pdev->dev.of_node,
  6634. "qcom,ipa-wdi3-over-gsi");
  6635. IPADBG(": WDI-3.0 over gsi= %s\n",
  6636. ipa_drv_res->ipa_wdi3_over_gsi
  6637. ? "True" : "False");
  6638. ipa_drv_res->ipa_wdi2 =
  6639. of_property_read_bool(pdev->dev.of_node,
  6640. "qcom,ipa-wdi2");
  6641. IPADBG(": WDI-2.0 = %s\n",
  6642. ipa_drv_res->ipa_wdi2
  6643. ? "True" : "False");
  6644. ipa_drv_res->ipa_wan_skb_page =
  6645. of_property_read_bool(pdev->dev.of_node,
  6646. "qcom,wan-use-skb-page");
  6647. IPADBG(": Use skb page = %s\n",
  6648. ipa_drv_res->ipa_wan_skb_page
  6649. ? "True" : "False");
  6650. ipa_drv_res->ipa_fltrt_not_hashable =
  6651. of_property_read_bool(pdev->dev.of_node,
  6652. "qcom,ipa-fltrt-not-hashable");
  6653. IPADBG(": IPA filter/route rule hashable = %s\n",
  6654. ipa_drv_res->ipa_fltrt_not_hashable
  6655. ? "True" : "False");
  6656. ipa_drv_res->use_64_bit_dma_mask =
  6657. of_property_read_bool(pdev->dev.of_node,
  6658. "qcom,use-64-bit-dma-mask");
  6659. IPADBG(": use_64_bit_dma_mask = %s\n",
  6660. ipa_drv_res->use_64_bit_dma_mask
  6661. ? "True" : "False");
  6662. ipa_drv_res->use_bw_vote =
  6663. of_property_read_bool(pdev->dev.of_node,
  6664. "qcom,bandwidth-vote-for-ipa");
  6665. IPADBG(": use_bw_vote = %s\n",
  6666. ipa_drv_res->use_bw_vote
  6667. ? "True" : "False");
  6668. ipa_drv_res->skip_ieob_mask_wa =
  6669. of_property_read_bool(pdev->dev.of_node,
  6670. "qcom,skip-ieob-mask-wa");
  6671. IPADBG(": skip ieob mask wa = %s\n",
  6672. ipa_drv_res->skip_ieob_mask_wa
  6673. ? "True" : "False");
  6674. ipa_drv_res->skip_uc_pipe_reset =
  6675. of_property_read_bool(pdev->dev.of_node,
  6676. "qcom,skip-uc-pipe-reset");
  6677. IPADBG(": skip uC pipe reset = %s\n",
  6678. ipa_drv_res->skip_uc_pipe_reset
  6679. ? "True" : "False");
  6680. ipa_drv_res->tethered_flow_control =
  6681. of_property_read_bool(pdev->dev.of_node,
  6682. "qcom,tethered-flow-control");
  6683. IPADBG(": Use apps based flow control = %s\n",
  6684. ipa_drv_res->tethered_flow_control
  6685. ? "True" : "False");
  6686. ipa_drv_res->lan_rx_napi_enable =
  6687. of_property_read_bool(pdev->dev.of_node,
  6688. "qcom,lan-rx-napi");
  6689. IPADBG(": Enable LAN rx NAPI = %s\n",
  6690. ipa_drv_res->lan_rx_napi_enable
  6691. ? "True" : "False");
  6692. /* Get IPA wrapper address */
  6693. resource = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  6694. "ipa-base");
  6695. if (!resource) {
  6696. IPAERR(":get resource failed for ipa-base!\n");
  6697. return -ENODEV;
  6698. }
  6699. ipa_drv_res->ipa_mem_base = resource->start;
  6700. ipa_drv_res->ipa_mem_size = resource_size(resource);
  6701. IPADBG(": ipa-base = 0x%x, size = 0x%x\n",
  6702. ipa_drv_res->ipa_mem_base,
  6703. ipa_drv_res->ipa_mem_size);
  6704. smmu_info.ipa_base = ipa_drv_res->ipa_mem_base;
  6705. smmu_info.ipa_size = ipa_drv_res->ipa_mem_size;
  6706. /* Get IPA GSI address */
  6707. resource = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  6708. "gsi-base");
  6709. if (!resource) {
  6710. IPAERR(":get resource failed for gsi-base\n");
  6711. return -ENODEV;
  6712. }
  6713. ipa_drv_res->transport_mem_base = resource->start;
  6714. ipa_drv_res->transport_mem_size = resource_size(resource);
  6715. IPADBG(": gsi-base = 0x%x, size = 0x%x\n",
  6716. ipa_drv_res->transport_mem_base,
  6717. ipa_drv_res->transport_mem_size);
  6718. /* Get IPA GSI IRQ number */
  6719. resource = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
  6720. "gsi-irq");
  6721. if (!resource) {
  6722. IPAERR(":get resource failed for gsi-irq\n");
  6723. return -ENODEV;
  6724. }
  6725. ipa_drv_res->transport_irq = resource->start;
  6726. IPADBG(": gsi-irq = %d\n", ipa_drv_res->transport_irq);
  6727. /* Get IPA pipe mem start ofst */
  6728. resource = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  6729. "ipa-pipe-mem");
  6730. if (!resource) {
  6731. IPADBG(":not using pipe memory - resource nonexisting\n");
  6732. } else {
  6733. ipa_drv_res->ipa_pipe_mem_start_ofst = resource->start;
  6734. ipa_drv_res->ipa_pipe_mem_size = resource_size(resource);
  6735. IPADBG(":using pipe memory - at 0x%x of size 0x%x\n",
  6736. ipa_drv_res->ipa_pipe_mem_start_ofst,
  6737. ipa_drv_res->ipa_pipe_mem_size);
  6738. }
  6739. /* Get IPA IRQ number */
  6740. resource = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
  6741. "ipa-irq");
  6742. if (!resource) {
  6743. IPAERR(":get resource failed for ipa-irq\n");
  6744. return -ENODEV;
  6745. }
  6746. ipa_drv_res->ipa_irq = resource->start;
  6747. IPADBG(":ipa-irq = %d\n", ipa_drv_res->ipa_irq);
  6748. result = of_property_read_u32(pdev->dev.of_node, "qcom,ee",
  6749. &ipa_drv_res->ee);
  6750. if (result)
  6751. ipa_drv_res->ee = 0;
  6752. IPADBG(":ee = %u\n", ipa_drv_res->ee);
  6753. ipa_drv_res->apply_rg10_wa =
  6754. of_property_read_bool(pdev->dev.of_node,
  6755. "qcom,use-rg10-limitation-mitigation");
  6756. IPADBG(": Use Register Group 10 limitation mitigation = %s\n",
  6757. ipa_drv_res->apply_rg10_wa
  6758. ? "True" : "False");
  6759. ipa_drv_res->gsi_ch20_wa =
  6760. of_property_read_bool(pdev->dev.of_node,
  6761. "qcom,do-not-use-ch-gsi-20");
  6762. IPADBG(": GSI CH 20 WA is = %s\n",
  6763. ipa_drv_res->gsi_ch20_wa
  6764. ? "Needed" : "Not needed");
  6765. elem_num = of_property_count_elems_of_size(pdev->dev.of_node,
  6766. "qcom,mhi-event-ring-id-limits", sizeof(u32));
  6767. if (elem_num == 2) {
  6768. if (of_property_read_u32_array(pdev->dev.of_node,
  6769. "qcom,mhi-event-ring-id-limits", mhi_evid_limits, 2)) {
  6770. IPAERR("failed to read mhi event ring id limits\n");
  6771. return -EFAULT;
  6772. }
  6773. if (mhi_evid_limits[0] > mhi_evid_limits[1]) {
  6774. IPAERR("mhi event ring id low limit > high limit\n");
  6775. return -EFAULT;
  6776. }
  6777. ipa_drv_res->mhi_evid_limits[0] = mhi_evid_limits[0];
  6778. ipa_drv_res->mhi_evid_limits[1] = mhi_evid_limits[1];
  6779. IPADBG(": mhi-event-ring-id-limits start=%u end=%u\n",
  6780. mhi_evid_limits[0], mhi_evid_limits[1]);
  6781. } else {
  6782. if (elem_num > 0) {
  6783. IPAERR("Invalid mhi event ring id limits number %d\n",
  6784. elem_num);
  6785. return -EINVAL;
  6786. }
  6787. IPADBG("use default mhi evt ring id limits start=%u end=%u\n",
  6788. ipa_drv_res->mhi_evid_limits[0],
  6789. ipa_drv_res->mhi_evid_limits[1]);
  6790. }
  6791. elem_num = of_property_count_elems_of_size(pdev->dev.of_node,
  6792. "qcom,ipa-tz-unlock-reg", sizeof(u32));
  6793. if (elem_num > 0 && elem_num % 2 == 0) {
  6794. ipa_drv_res->ipa_tz_unlock_reg_num = elem_num / 2;
  6795. ipa_tz_unlock_reg = kcalloc(elem_num, sizeof(u32), GFP_KERNEL);
  6796. if (ipa_tz_unlock_reg == NULL)
  6797. return -ENOMEM;
  6798. ipa_drv_res->ipa_tz_unlock_reg = kcalloc(
  6799. ipa_drv_res->ipa_tz_unlock_reg_num,
  6800. sizeof(*ipa_drv_res->ipa_tz_unlock_reg),
  6801. GFP_KERNEL);
  6802. if (ipa_drv_res->ipa_tz_unlock_reg == NULL) {
  6803. kfree(ipa_tz_unlock_reg);
  6804. return -ENOMEM;
  6805. }
  6806. if (of_property_read_u32_array(pdev->dev.of_node,
  6807. "qcom,ipa-tz-unlock-reg", ipa_tz_unlock_reg,
  6808. elem_num)) {
  6809. IPAERR("failed to read register addresses\n");
  6810. kfree(ipa_tz_unlock_reg);
  6811. kfree(ipa_drv_res->ipa_tz_unlock_reg);
  6812. return -EFAULT;
  6813. }
  6814. pos = 0;
  6815. for (i = 0; i < ipa_drv_res->ipa_tz_unlock_reg_num; i++) {
  6816. ipa_drv_res->ipa_tz_unlock_reg[i].reg_addr =
  6817. ipa_tz_unlock_reg[pos++];
  6818. ipa_drv_res->ipa_tz_unlock_reg[i].size =
  6819. ipa_tz_unlock_reg[pos++];
  6820. IPADBG("tz unlock reg %d: addr 0x%pa size %llu\n", i,
  6821. &ipa_drv_res->ipa_tz_unlock_reg[i].reg_addr,
  6822. ipa_drv_res->ipa_tz_unlock_reg[i].size);
  6823. }
  6824. kfree(ipa_tz_unlock_reg);
  6825. }
  6826. /* get IPA PM related information */
  6827. result = get_ipa_dts_pm_info(pdev, ipa_drv_res);
  6828. if (result) {
  6829. IPAERR("failed to get pm info from dts %d\n", result);
  6830. return result;
  6831. }
  6832. ipa_drv_res->wdi_over_pcie =
  6833. of_property_read_bool(pdev->dev.of_node,
  6834. "qcom,wlan-ce-db-over-pcie");
  6835. IPADBG("Is wdi_over_pcie ? (%s)\n",
  6836. ipa_drv_res->wdi_over_pcie ? "Yes":"No");
  6837. /*
  6838. * If we're on emulator, get its interrupt controller's mem
  6839. * start and size
  6840. */
  6841. if (ipa_drv_res->ipa3_hw_mode == IPA_HW_MODE_EMULATION) {
  6842. resource = platform_get_resource_byname(
  6843. pdev, IORESOURCE_MEM, "intctrl-base");
  6844. if (!resource) {
  6845. IPAERR(":Can't find intctrl-base resource\n");
  6846. return -ENODEV;
  6847. }
  6848. ipa_drv_res->emulator_intcntrlr_mem_base =
  6849. resource->start;
  6850. ipa_drv_res->emulator_intcntrlr_mem_size =
  6851. resource_size(resource);
  6852. IPADBG(":using intctrl-base at 0x%x of size 0x%x\n",
  6853. ipa_drv_res->emulator_intcntrlr_mem_base,
  6854. ipa_drv_res->emulator_intcntrlr_mem_size);
  6855. }
  6856. ipa_drv_res->entire_ipa_block_size = 0x100000;
  6857. result = of_property_read_u32(pdev->dev.of_node,
  6858. "qcom,entire-ipa-block-size",
  6859. &ipa_drv_res->entire_ipa_block_size);
  6860. IPADBG(": entire_ipa_block_size = %d\n",
  6861. ipa_drv_res->entire_ipa_block_size);
  6862. /*
  6863. * We'll read register-collection-on-crash here, but log it
  6864. * later below because its value may change based on other
  6865. * subsequent dtsi reads......
  6866. */
  6867. ipa_drv_res->do_register_collection_on_crash =
  6868. of_property_read_bool(pdev->dev.of_node,
  6869. "qcom,register-collection-on-crash");
  6870. /*
  6871. * We'll read testbus-collection-on-crash here...
  6872. */
  6873. ipa_drv_res->do_testbus_collection_on_crash =
  6874. of_property_read_bool(pdev->dev.of_node,
  6875. "qcom,testbus-collection-on-crash");
  6876. IPADBG(": doing testbus collection on crash = %u\n",
  6877. ipa_drv_res->do_testbus_collection_on_crash);
  6878. /*
  6879. * We'll read non-tn-collection-on-crash here...
  6880. */
  6881. ipa_drv_res->do_non_tn_collection_on_crash =
  6882. of_property_read_bool(pdev->dev.of_node,
  6883. "qcom,non-tn-collection-on-crash");
  6884. IPADBG(": doing non-tn collection on crash = %u\n",
  6885. ipa_drv_res->do_non_tn_collection_on_crash);
  6886. /*
  6887. * We'll read ram-collection-on-crash here...
  6888. */
  6889. ipa_drv_res->do_ram_collection_on_crash =
  6890. of_property_read_bool(
  6891. pdev->dev.of_node,
  6892. "qcom,ram-collection-on-crash");
  6893. IPADBG(": doing ram collection on crash = %u\n",
  6894. ipa_drv_res->do_ram_collection_on_crash);
  6895. if (ipa_drv_res->do_testbus_collection_on_crash ||
  6896. ipa_drv_res->do_non_tn_collection_on_crash ||
  6897. ipa_drv_res->do_ram_collection_on_crash)
  6898. ipa_drv_res->do_register_collection_on_crash = true;
  6899. IPADBG(": doing register collection on crash = %u\n",
  6900. ipa_drv_res->do_register_collection_on_crash);
  6901. result = of_property_read_u32(
  6902. pdev->dev.of_node,
  6903. "qcom,secure-debug-check-action",
  6904. &ipa_drv_res->secure_debug_check_action);
  6905. if (result ||
  6906. (ipa_drv_res->secure_debug_check_action != 0 &&
  6907. ipa_drv_res->secure_debug_check_action != 1 &&
  6908. ipa_drv_res->secure_debug_check_action != 2))
  6909. ipa_drv_res->secure_debug_check_action = USE_SCM;
  6910. IPADBG(": secure-debug-check-action = %d\n",
  6911. ipa_drv_res->secure_debug_check_action);
  6912. return 0;
  6913. }
  6914. static int ipa_smmu_wlan_cb_probe(struct device *dev)
  6915. {
  6916. struct ipa_smmu_cb_ctx *cb = ipa3_get_smmu_ctx(IPA_SMMU_CB_WLAN);
  6917. int fast = 0;
  6918. int bypass = 0;
  6919. u32 add_map_size;
  6920. const u32 *add_map;
  6921. int i;
  6922. u32 iova_ap_mapping[2];
  6923. IPADBG("WLAN CB PROBE dev=%pK\n", dev);
  6924. if (!smmu_info.present[IPA_SMMU_CB_WLAN]) {
  6925. IPAERR("WLAN SMMU is disabled\n");
  6926. return 0;
  6927. }
  6928. IPADBG("WLAN CB PROBE dev=%pK retrieving IOMMU mapping\n", dev);
  6929. cb->iommu_domain = iommu_get_domain_for_dev(dev);
  6930. if (IS_ERR_OR_NULL(cb->iommu_domain)) {
  6931. IPAERR("could not get iommu domain\n");
  6932. return -EINVAL;
  6933. }
  6934. IPADBG("WLAN CB PROBE mapping retrieved\n");
  6935. cb->is_cache_coherent = of_property_read_bool(dev->of_node,
  6936. "dma-coherent");
  6937. cb->dev = dev;
  6938. cb->valid = true;
  6939. cb->va_start = cb->va_end = cb->va_size = 0;
  6940. if (of_property_read_u32_array(
  6941. dev->of_node, "qcom,iommu-dma-addr-pool",
  6942. iova_ap_mapping, 2) == 0) {
  6943. cb->va_start = iova_ap_mapping[0];
  6944. cb->va_size = iova_ap_mapping[1];
  6945. cb->va_end = cb->va_start + cb->va_size;
  6946. }
  6947. IPADBG("WLAN CB PROBE dev=%pK va_start=0x%x va_size=0x%x\n",
  6948. dev, cb->va_start, cb->va_size);
  6949. /*
  6950. * Prior to these calls to iommu_domain_get_attr(), these
  6951. * attributes were set in this function relative to dtsi values
  6952. * defined for this driver. In other words, if corresponding ipa
  6953. * driver owned values were found in the dtsi, they were read and
  6954. * set here.
  6955. *
  6956. * In this new world, the developer will use iommu owned dtsi
  6957. * settings to set them there. This new logic below, simply
  6958. * checks to see if they've been set in dtsi. If so, the logic
  6959. * further below acts accordingly...
  6960. */
  6961. iommu_domain_get_attr(cb->iommu_domain, DOMAIN_ATTR_S1_BYPASS, &bypass);
  6962. iommu_domain_get_attr(cb->iommu_domain, DOMAIN_ATTR_FAST, &fast);
  6963. IPADBG(
  6964. "WLAN CB PROBE dev=%pK DOMAIN ATTRS bypass=%d fast=%d\n",
  6965. dev, bypass, fast);
  6966. ipa3_ctx->s1_bypass_arr[IPA_SMMU_CB_WLAN] = (bypass != 0);
  6967. /* MAP ipa-uc ram */
  6968. add_map = of_get_property(dev->of_node,
  6969. "qcom,additional-mapping", &add_map_size);
  6970. if (add_map) {
  6971. /* mapping size is an array of 3-tuple of u32 */
  6972. if (add_map_size % (3 * sizeof(u32))) {
  6973. IPAERR("wrong additional mapping format\n");
  6974. cb->valid = false;
  6975. return -EFAULT;
  6976. }
  6977. /* iterate of each entry of the additional mapping array */
  6978. for (i = 0; i < add_map_size / sizeof(u32); i += 3) {
  6979. u32 iova = be32_to_cpu(add_map[i]);
  6980. u32 pa = be32_to_cpu(add_map[i + 1]);
  6981. u32 size = be32_to_cpu(add_map[i + 2]);
  6982. unsigned long iova_p;
  6983. phys_addr_t pa_p;
  6984. u32 size_p;
  6985. IPA_SMMU_ROUND_TO_PAGE(iova, pa, size,
  6986. iova_p, pa_p, size_p);
  6987. IPADBG_LOW("mapping 0x%lx to 0x%pa size %d\n",
  6988. iova_p, &pa_p, size_p);
  6989. ipa3_iommu_map(cb->iommu_domain,
  6990. iova_p, pa_p, size_p,
  6991. IOMMU_READ | IOMMU_WRITE | IOMMU_MMIO);
  6992. }
  6993. }
  6994. return 0;
  6995. }
  6996. static int ipa_smmu_uc_cb_probe(struct device *dev)
  6997. {
  6998. struct ipa_smmu_cb_ctx *cb = ipa3_get_smmu_ctx(IPA_SMMU_CB_UC);
  6999. int bypass = 0;
  7000. int fast = 0;
  7001. u32 iova_ap_mapping[2];
  7002. IPADBG("UC CB PROBE dev=%pK\n", dev);
  7003. if (!smmu_info.present[IPA_SMMU_CB_UC]) {
  7004. IPAERR("UC SMMU is disabled\n");
  7005. return 0;
  7006. }
  7007. if (smmu_info.use_64_bit_dma_mask) {
  7008. if (dma_set_mask(dev, DMA_BIT_MASK(64)) ||
  7009. dma_set_coherent_mask(dev, DMA_BIT_MASK(64))) {
  7010. IPAERR("DMA set 64bit mask failed\n");
  7011. return -EOPNOTSUPP;
  7012. }
  7013. } else {
  7014. if (dma_set_mask(dev, DMA_BIT_MASK(32)) ||
  7015. dma_set_coherent_mask(dev, DMA_BIT_MASK(32))) {
  7016. IPAERR("DMA set 32bit mask failed\n");
  7017. return -EOPNOTSUPP;
  7018. }
  7019. }
  7020. IPADBG("UC CB PROBE dev=%pK retrieving IOMMU mapping\n", dev);
  7021. cb->iommu_domain = iommu_get_domain_for_dev(dev);
  7022. if (IS_ERR_OR_NULL(cb->iommu_domain)) {
  7023. IPAERR("could not get iommu domain\n");
  7024. return -EINVAL;
  7025. }
  7026. IPADBG("UC CB PROBE mapping retrieved\n");
  7027. cb->is_cache_coherent = of_property_read_bool(dev->of_node,
  7028. "dma-coherent");
  7029. cb->dev = dev;
  7030. cb->valid = true;
  7031. cb->va_start = cb->va_end = cb->va_size = 0;
  7032. if (of_property_read_u32_array(
  7033. dev->of_node, "qcom,iommu-dma-addr-pool",
  7034. iova_ap_mapping, 2) == 0) {
  7035. cb->va_start = iova_ap_mapping[0];
  7036. cb->va_size = iova_ap_mapping[1];
  7037. cb->va_end = cb->va_start + cb->va_size;
  7038. }
  7039. IPADBG("UC CB PROBE dev=%pK va_start=0x%x va_size=0x%x\n",
  7040. dev, cb->va_start, cb->va_size);
  7041. /*
  7042. * Prior to these calls to iommu_domain_get_attr(), these
  7043. * attributes were set in this function relative to dtsi values
  7044. * defined for this driver. In other words, if corresponding ipa
  7045. * driver owned values were found in the dtsi, they were read and
  7046. * set here.
  7047. *
  7048. * In this new world, the developer will use iommu owned dtsi
  7049. * settings to set them there. This new logic below, simply
  7050. * checks to see if they've been set in dtsi. If so, the logic
  7051. * further below acts accordingly...
  7052. */
  7053. iommu_domain_get_attr(cb->iommu_domain, DOMAIN_ATTR_S1_BYPASS, &bypass);
  7054. iommu_domain_get_attr(cb->iommu_domain, DOMAIN_ATTR_FAST, &fast);
  7055. IPADBG("UC CB PROBE dev=%pK DOMAIN ATTRS bypass=%d fast=%d\n",
  7056. dev, bypass, fast);
  7057. ipa3_ctx->s1_bypass_arr[IPA_SMMU_CB_UC] = (bypass != 0);
  7058. ipa3_ctx->uc_pdev = dev;
  7059. return 0;
  7060. }
  7061. static int ipa_smmu_ap_cb_probe(struct device *dev)
  7062. {
  7063. struct ipa_smmu_cb_ctx *cb = ipa3_get_smmu_ctx(IPA_SMMU_CB_AP);
  7064. int fast = 0;
  7065. int bypass = 0;
  7066. u32 add_map_size;
  7067. const u32 *add_map;
  7068. void *smem_addr;
  7069. size_t smem_size;
  7070. u32 ipa_smem_size = 0;
  7071. int ret;
  7072. int i;
  7073. unsigned long iova_p;
  7074. phys_addr_t pa_p;
  7075. u32 size_p;
  7076. phys_addr_t iova;
  7077. phys_addr_t pa;
  7078. u32 iova_ap_mapping[2];
  7079. IPADBG("AP CB PROBE dev=%pK\n", dev);
  7080. if (!smmu_info.present[IPA_SMMU_CB_AP]) {
  7081. IPAERR("AP SMMU is disabled");
  7082. return 0;
  7083. }
  7084. if (smmu_info.use_64_bit_dma_mask) {
  7085. if (dma_set_mask(dev, DMA_BIT_MASK(64)) ||
  7086. dma_set_coherent_mask(dev, DMA_BIT_MASK(64))) {
  7087. IPAERR("DMA set 64bit mask failed\n");
  7088. return -EOPNOTSUPP;
  7089. }
  7090. } else {
  7091. if (dma_set_mask(dev, DMA_BIT_MASK(32)) ||
  7092. dma_set_coherent_mask(dev, DMA_BIT_MASK(32))) {
  7093. IPAERR("DMA set 32bit mask failed\n");
  7094. return -EOPNOTSUPP;
  7095. }
  7096. }
  7097. IPADBG("AP CB PROBE dev=%pK retrieving IOMMU mapping\n", dev);
  7098. cb->iommu_domain = iommu_get_domain_for_dev(dev);
  7099. if (IS_ERR_OR_NULL(cb->iommu_domain)) {
  7100. IPAERR("could not get iommu domain\n");
  7101. return -EINVAL;
  7102. }
  7103. IPADBG("AP CB PROBE mapping retrieved\n");
  7104. cb->is_cache_coherent = of_property_read_bool(dev->of_node,
  7105. "dma-coherent");
  7106. cb->dev = dev;
  7107. cb->valid = true;
  7108. cb->va_start = cb->va_end = cb->va_size = 0;
  7109. if (of_property_read_u32_array(
  7110. dev->of_node, "qcom,iommu-dma-addr-pool",
  7111. iova_ap_mapping, 2) == 0) {
  7112. cb->va_start = iova_ap_mapping[0];
  7113. cb->va_size = iova_ap_mapping[1];
  7114. cb->va_end = cb->va_start + cb->va_size;
  7115. }
  7116. IPADBG("AP CB PROBE dev=%pK va_start=0x%x va_size=0x%x\n",
  7117. dev, cb->va_start, cb->va_size);
  7118. /*
  7119. * Prior to these calls to iommu_domain_get_attr(), these
  7120. * attributes were set in this function relative to dtsi values
  7121. * defined for this driver. In other words, if corresponding ipa
  7122. * driver owned values were found in the dtsi, they were read and
  7123. * set here.
  7124. *
  7125. * In this new world, the developer will use iommu owned dtsi
  7126. * settings to set them there. This new logic below, simply
  7127. * checks to see if they've been set in dtsi. If so, the logic
  7128. * further below acts accordingly...
  7129. */
  7130. iommu_domain_get_attr(cb->iommu_domain, DOMAIN_ATTR_S1_BYPASS, &bypass);
  7131. iommu_domain_get_attr(cb->iommu_domain, DOMAIN_ATTR_FAST, &fast);
  7132. IPADBG("AP CB PROBE dev=%pK DOMAIN ATTRS bypass=%d fast=%d\n",
  7133. dev, bypass, fast);
  7134. ipa3_ctx->s1_bypass_arr[IPA_SMMU_CB_AP] = (bypass != 0);
  7135. add_map = of_get_property(dev->of_node,
  7136. "qcom,additional-mapping", &add_map_size);
  7137. if (add_map) {
  7138. /* mapping size is an array of 3-tuple of u32 */
  7139. if (add_map_size % (3 * sizeof(u32))) {
  7140. IPAERR("wrong additional mapping format\n");
  7141. cb->valid = false;
  7142. return -EFAULT;
  7143. }
  7144. /* iterate of each entry of the additional mapping array */
  7145. for (i = 0; i < add_map_size / sizeof(u32); i += 3) {
  7146. u32 iova = be32_to_cpu(add_map[i]);
  7147. u32 pa = be32_to_cpu(add_map[i + 1]);
  7148. u32 size = be32_to_cpu(add_map[i + 2]);
  7149. unsigned long iova_p;
  7150. phys_addr_t pa_p;
  7151. u32 size_p;
  7152. IPA_SMMU_ROUND_TO_PAGE(iova, pa, size,
  7153. iova_p, pa_p, size_p);
  7154. IPADBG_LOW("mapping 0x%lx to 0x%pa size %d\n",
  7155. iova_p, &pa_p, size_p);
  7156. ipa3_iommu_map(cb->iommu_domain,
  7157. iova_p, pa_p, size_p,
  7158. IOMMU_READ | IOMMU_WRITE | IOMMU_MMIO);
  7159. }
  7160. }
  7161. ret = of_property_read_u32(dev->of_node, "qcom,ipa-q6-smem-size",
  7162. &ipa_smem_size);
  7163. if (ret) {
  7164. IPADBG("ipa q6 smem size (default) = %u\n", IPA_SMEM_SIZE);
  7165. ipa_smem_size = IPA_SMEM_SIZE;
  7166. } else {
  7167. IPADBG("ipa q6 smem size = %u\n", ipa_smem_size);
  7168. }
  7169. if (ipa3_ctx->platform_type != IPA_PLAT_TYPE_APQ) {
  7170. /* map SMEM memory for IPA table accesses */
  7171. ret = qcom_smem_alloc(SMEM_MODEM,
  7172. SMEM_IPA_FILTER_TABLE,
  7173. ipa_smem_size);
  7174. if (ret < 0 && ret != -EEXIST) {
  7175. IPAERR("unable to allocate smem MODEM entry\n");
  7176. cb->valid = false;
  7177. return -EFAULT;
  7178. }
  7179. smem_addr = qcom_smem_get(SMEM_MODEM,
  7180. SMEM_IPA_FILTER_TABLE,
  7181. &smem_size);
  7182. if (IS_ERR(smem_addr)) {
  7183. IPAERR("unable to acquire smem MODEM entry\n");
  7184. cb->valid = false;
  7185. return -EFAULT;
  7186. }
  7187. if (smem_size != ipa_smem_size)
  7188. IPAERR("unexpected read q6 smem size %zu %u\n",
  7189. smem_size, ipa_smem_size);
  7190. iova = qcom_smem_virt_to_phys(smem_addr);
  7191. pa = iova;
  7192. IPA_SMMU_ROUND_TO_PAGE(iova, pa, ipa_smem_size,
  7193. iova_p, pa_p, size_p);
  7194. IPADBG("mapping 0x%lx to 0x%pa size %d\n",
  7195. iova_p, &pa_p, size_p);
  7196. ipa3_iommu_map(cb->iommu_domain,
  7197. iova_p, pa_p, size_p,
  7198. IOMMU_READ | IOMMU_WRITE);
  7199. }
  7200. smmu_info.present[IPA_SMMU_CB_AP] = true;
  7201. ipa3_ctx->pdev = dev;
  7202. cb->next_addr = cb->va_end;
  7203. return 0;
  7204. }
  7205. static int ipa_smmu_11ad_cb_probe(struct device *dev)
  7206. {
  7207. int bypass = 0;
  7208. struct ipa_smmu_cb_ctx *cb = ipa3_get_smmu_ctx(IPA_SMMU_CB_11AD);
  7209. u32 iova_ap_mapping[2];
  7210. IPADBG("11AD CB probe: dev=%pK\n", dev);
  7211. if (!smmu_info.present[IPA_SMMU_CB_11AD]) {
  7212. IPAERR("11AD SMMU is disabled");
  7213. return 0;
  7214. }
  7215. cb->iommu_domain = iommu_get_domain_for_dev(dev);
  7216. if (IS_ERR_OR_NULL(cb->iommu_domain)) {
  7217. IPAERR("could not get iommu domain\n");
  7218. return -EINVAL;
  7219. }
  7220. cb->is_cache_coherent = of_property_read_bool(dev->of_node,
  7221. "dma-coherent");
  7222. cb->dev = dev;
  7223. cb->valid = true;
  7224. cb->va_start = cb->va_end = cb->va_size = 0;
  7225. if (of_property_read_u32_array(
  7226. dev->of_node, "qcom,iommu-dma-addr-pool",
  7227. iova_ap_mapping, 2) == 0) {
  7228. cb->va_start = iova_ap_mapping[0];
  7229. cb->va_size = iova_ap_mapping[1];
  7230. cb->va_end = cb->va_start + cb->va_size;
  7231. }
  7232. IPADBG("11AD CB PROBE dev=%pK va_start=0x%x va_size=0x%x\n",
  7233. dev, cb->va_start, cb->va_size);
  7234. iommu_domain_get_attr(cb->iommu_domain, DOMAIN_ATTR_S1_BYPASS, &bypass);
  7235. IPADBG("11AD CB PROBE dev=%pK DOMAIN ATTRS bypass=%d\n",
  7236. dev, bypass);
  7237. ipa3_ctx->s1_bypass_arr[IPA_SMMU_CB_11AD] = (bypass != 0);
  7238. if (of_property_read_bool(dev->of_node, "qcom,shared-cb")) {
  7239. IPADBG("11AD using shared CB\n");
  7240. cb->shared = true;
  7241. }
  7242. return 0;
  7243. }
  7244. static int ipa_smmu_cb_probe(struct device *dev, enum ipa_smmu_cb_type cb_type)
  7245. {
  7246. switch (cb_type) {
  7247. case IPA_SMMU_CB_AP:
  7248. return ipa_smmu_ap_cb_probe(dev);
  7249. case IPA_SMMU_CB_WLAN:
  7250. return ipa_smmu_wlan_cb_probe(dev);
  7251. case IPA_SMMU_CB_UC:
  7252. return ipa_smmu_uc_cb_probe(dev);
  7253. case IPA_SMMU_CB_11AD:
  7254. return ipa_smmu_11ad_cb_probe(dev);
  7255. case IPA_SMMU_CB_MAX:
  7256. IPAERR("Invalid cb_type\n");
  7257. }
  7258. return 0;
  7259. }
  7260. static int ipa3_attach_to_smmu(void)
  7261. {
  7262. struct ipa_smmu_cb_ctx *cb;
  7263. int i, result;
  7264. ipa3_ctx->pdev = &ipa3_ctx->master_pdev->dev;
  7265. ipa3_ctx->uc_pdev = &ipa3_ctx->master_pdev->dev;
  7266. if (smmu_info.arm_smmu) {
  7267. IPADBG("smmu is enabled\n");
  7268. for (i = 0; i < IPA_SMMU_CB_MAX; i++) {
  7269. cb = ipa3_get_smmu_ctx(i);
  7270. result = ipa_smmu_cb_probe(cb->dev, i);
  7271. if (result)
  7272. IPAERR("probe failed for cb %d\n", i);
  7273. }
  7274. } else {
  7275. IPADBG("smmu is disabled\n");
  7276. }
  7277. return 0;
  7278. }
  7279. static irqreturn_t ipa3_smp2p_modem_clk_query_isr(int irq, void *ctxt)
  7280. {
  7281. ipa3_freeze_clock_vote_and_notify_modem();
  7282. return IRQ_HANDLED;
  7283. }
  7284. static int ipa3_smp2p_probe(struct device *dev)
  7285. {
  7286. struct device_node *node = dev->of_node;
  7287. int res;
  7288. int irq = 0;
  7289. if (ipa3_ctx == NULL) {
  7290. IPAERR("ipa3_ctx was not initialized\n");
  7291. return -EPROBE_DEFER;
  7292. }
  7293. IPADBG("node->name=%s\n", node->name);
  7294. if (ipa3_ctx->platform_type == IPA_PLAT_TYPE_APQ) {
  7295. IPADBG("Ignore smp2p on APQ platform\n");
  7296. return 0;
  7297. }
  7298. if (strcmp("qcom,smp2p_map_ipa_1_out", node->name) == 0) {
  7299. if (of_find_property(node, "qcom,smem-states", NULL)) {
  7300. ipa3_ctx->smp2p_info.smem_state =
  7301. qcom_smem_state_get(dev, "ipa-smp2p-out",
  7302. &ipa3_ctx->smp2p_info.smem_bit);
  7303. if (IS_ERR(ipa3_ctx->smp2p_info.smem_state)) {
  7304. IPAERR("fail to get smp2p clk resp bit %ld\n",
  7305. PTR_ERR(ipa3_ctx->smp2p_info.smem_state));
  7306. return PTR_ERR(ipa3_ctx->smp2p_info.smem_state);
  7307. }
  7308. IPADBG("smem_bit=%d\n", ipa3_ctx->smp2p_info.smem_bit);
  7309. }
  7310. } else if (strcmp("qcom,smp2p_map_ipa_1_in", node->name) == 0) {
  7311. res = irq = of_irq_get_byname(node, "ipa-smp2p-in");
  7312. if (res < 0) {
  7313. IPADBG("of_irq_get_byname returned %d\n", irq);
  7314. return res;
  7315. }
  7316. ipa3_ctx->smp2p_info.in_base_id = irq;
  7317. IPADBG("smp2p irq#=%d\n", irq);
  7318. res = devm_request_threaded_irq(dev, irq, NULL,
  7319. (irq_handler_t)ipa3_smp2p_modem_clk_query_isr,
  7320. IRQF_TRIGGER_RISING | IRQF_ONESHOT,
  7321. "ipa_smp2p_clk_vote", dev);
  7322. if (res) {
  7323. IPAERR("fail to register smp2p irq=%d\n", irq);
  7324. return -ENODEV;
  7325. }
  7326. }
  7327. return 0;
  7328. }
  7329. static void ipa_smmu_update_fw_loader(void)
  7330. {
  7331. int i;
  7332. if (smmu_info.arm_smmu) {
  7333. IPADBG("smmu is enabled\n");
  7334. for (i = 0; i < IPA_SMMU_CB_MAX; i++) {
  7335. if (!smmu_info.present[i]) {
  7336. IPADBG("CB %d not probed yet\n", i);
  7337. break;
  7338. }
  7339. }
  7340. if (i == IPA_SMMU_CB_MAX) {
  7341. IPADBG("All %d CBs probed\n", IPA_SMMU_CB_MAX);
  7342. ipa_fw_load_sm_handle_event(IPA_FW_LOAD_EVNT_SMMU_DONE);
  7343. }
  7344. } else {
  7345. IPADBG("smmu is disabled\n");
  7346. }
  7347. }
  7348. int ipa3_plat_drv_probe(struct platform_device *pdev_p,
  7349. struct ipa_api_controller *api_ctrl,
  7350. const struct of_device_id *pdrv_match)
  7351. {
  7352. int result;
  7353. struct device *dev = &pdev_p->dev;
  7354. struct ipa_smmu_cb_ctx *cb;
  7355. IPADBG("IPA driver probing started\n");
  7356. IPADBG("dev->of_node->name = %s\n", dev->of_node->name);
  7357. if (of_device_is_compatible(dev->of_node, "qcom,ipa-smmu-ap-cb")) {
  7358. if (ipa3_ctx == NULL) {
  7359. IPAERR("ipa3_ctx was not initialized\n");
  7360. return -EPROBE_DEFER;
  7361. }
  7362. cb = ipa3_get_smmu_ctx(IPA_SMMU_CB_AP);
  7363. cb->dev = dev;
  7364. smmu_info.present[IPA_SMMU_CB_AP] = true;
  7365. ipa_smmu_update_fw_loader();
  7366. return 0;
  7367. }
  7368. if (of_device_is_compatible(dev->of_node, "qcom,ipa-smmu-wlan-cb")) {
  7369. if (ipa3_ctx == NULL) {
  7370. IPAERR("ipa3_ctx was not initialized\n");
  7371. return -EPROBE_DEFER;
  7372. }
  7373. cb = ipa3_get_smmu_ctx(IPA_SMMU_CB_WLAN);
  7374. cb->dev = dev;
  7375. smmu_info.present[IPA_SMMU_CB_WLAN] = true;
  7376. ipa_smmu_update_fw_loader();
  7377. return 0;
  7378. }
  7379. if (of_device_is_compatible(dev->of_node, "qcom,ipa-smmu-uc-cb")) {
  7380. if (ipa3_ctx == NULL) {
  7381. IPAERR("ipa3_ctx was not initialized\n");
  7382. return -EPROBE_DEFER;
  7383. }
  7384. cb = ipa3_get_smmu_ctx(IPA_SMMU_CB_UC);
  7385. cb->dev = dev;
  7386. smmu_info.present[IPA_SMMU_CB_UC] = true;
  7387. ipa_smmu_update_fw_loader();
  7388. return 0;
  7389. }
  7390. if (of_device_is_compatible(dev->of_node, "qcom,ipa-smmu-11ad-cb")) {
  7391. if (ipa3_ctx == NULL) {
  7392. IPAERR("ipa3_ctx was not initialized\n");
  7393. return -EPROBE_DEFER;
  7394. }
  7395. cb = ipa3_get_smmu_ctx(IPA_SMMU_CB_11AD);
  7396. cb->dev = dev;
  7397. smmu_info.present[IPA_SMMU_CB_11AD] = true;
  7398. ipa_smmu_update_fw_loader();
  7399. return 0;
  7400. }
  7401. if (of_device_is_compatible(dev->of_node,
  7402. "qcom,smp2p-map-ipa-1-out"))
  7403. return ipa3_smp2p_probe(dev);
  7404. if (of_device_is_compatible(dev->of_node,
  7405. "qcom,smp2p-map-ipa-1-in"))
  7406. return ipa3_smp2p_probe(dev);
  7407. result = get_ipa_dts_configuration(pdev_p, &ipa3_res);
  7408. if (result) {
  7409. IPAERR("IPA dts parsing failed\n");
  7410. return result;
  7411. }
  7412. result = ipa3_bind_api_controller(ipa3_res.ipa_hw_type, api_ctrl);
  7413. if (result) {
  7414. IPAERR("IPA API binding failed\n");
  7415. return result;
  7416. }
  7417. if (of_property_read_bool(pdev_p->dev.of_node, "qcom,arm-smmu")) {
  7418. if (of_property_read_bool(pdev_p->dev.of_node,
  7419. "qcom,use-64-bit-dma-mask"))
  7420. smmu_info.use_64_bit_dma_mask = true;
  7421. smmu_info.arm_smmu = true;
  7422. } else {
  7423. if (of_property_read_bool(pdev_p->dev.of_node,
  7424. "qcom,use-64-bit-dma-mask")) {
  7425. if (dma_set_mask(&pdev_p->dev, DMA_BIT_MASK(64)) ||
  7426. dma_set_coherent_mask(&pdev_p->dev,
  7427. DMA_BIT_MASK(64))) {
  7428. IPAERR("DMA set 64bit mask failed\n");
  7429. return -EOPNOTSUPP;
  7430. }
  7431. } else {
  7432. if (dma_set_mask(&pdev_p->dev, DMA_BIT_MASK(32)) ||
  7433. dma_set_coherent_mask(&pdev_p->dev,
  7434. DMA_BIT_MASK(32))) {
  7435. IPAERR("DMA set 32bit mask failed\n");
  7436. return -EOPNOTSUPP;
  7437. }
  7438. }
  7439. ipa_fw_load_sm_handle_event(IPA_FW_LOAD_EVNT_SMMU_DONE);
  7440. }
  7441. /* Proceed to real initialization */
  7442. result = ipa3_pre_init(&ipa3_res, pdev_p);
  7443. if (result) {
  7444. IPAERR("ipa3_init failed\n");
  7445. return result;
  7446. }
  7447. result = of_platform_populate(pdev_p->dev.of_node,
  7448. pdrv_match, NULL, &pdev_p->dev);
  7449. if (result) {
  7450. IPAERR("failed to populate platform\n");
  7451. return result;
  7452. }
  7453. return result;
  7454. }
  7455. /**
  7456. * ipa3_ap_suspend() - suspend callback for runtime_pm
  7457. * @dev: pointer to device
  7458. *
  7459. * This callback will be invoked by the runtime_pm framework when an AP suspend
  7460. * operation is invoked, usually by pressing a suspend button.
  7461. *
  7462. * Returns -EAGAIN to runtime_pm framework in case IPA is in use by AP.
  7463. * This will postpone the suspend operation until IPA is no longer used by AP.
  7464. */
  7465. int ipa3_ap_suspend(struct device *dev)
  7466. {
  7467. int i;
  7468. IPADBG("Enter...\n");
  7469. /* In case there is a tx/rx handler in polling mode fail to suspend */
  7470. for (i = 0; i < ipa3_ctx->ipa_num_pipes; i++) {
  7471. if (ipa3_ctx->ep[i].sys &&
  7472. atomic_read(&ipa3_ctx->ep[i].sys->curr_polling_state)) {
  7473. IPAERR("EP %d is in polling state, do not suspend\n",
  7474. i);
  7475. return -EAGAIN;
  7476. }
  7477. }
  7478. ipa_pm_deactivate_all_deferred();
  7479. IPADBG("Exit\n");
  7480. return 0;
  7481. }
  7482. /**
  7483. * ipa3_ap_resume() - resume callback for runtime_pm
  7484. * @dev: pointer to device
  7485. *
  7486. * This callback will be invoked by the runtime_pm framework when an AP resume
  7487. * operation is invoked.
  7488. *
  7489. * Always returns 0 since resume should always succeed.
  7490. */
  7491. int ipa3_ap_resume(struct device *dev)
  7492. {
  7493. return 0;
  7494. }
  7495. struct ipa3_context *ipa3_get_ctx(void)
  7496. {
  7497. return ipa3_ctx;
  7498. }
  7499. bool ipa3_get_lan_rx_napi(void)
  7500. {
  7501. return ipa3_ctx->lan_rx_napi_enable;
  7502. }
  7503. static void ipa_gsi_notify_cb(struct gsi_per_notify *notify)
  7504. {
  7505. /*
  7506. * These values are reported by hardware. Any error indicates
  7507. * hardware unexpected state.
  7508. */
  7509. switch (notify->evt_id) {
  7510. case GSI_PER_EVT_GLOB_ERROR:
  7511. IPAERR("Got GSI_PER_EVT_GLOB_ERROR\n");
  7512. IPAERR("Err_desc = 0x%04x\n", notify->data.err_desc);
  7513. break;
  7514. case GSI_PER_EVT_GLOB_GP1:
  7515. IPAERR("Got GSI_PER_EVT_GLOB_GP1\n");
  7516. ipa_assert();
  7517. break;
  7518. case GSI_PER_EVT_GLOB_GP2:
  7519. IPAERR("Got GSI_PER_EVT_GLOB_GP2\n");
  7520. ipa_assert();
  7521. break;
  7522. case GSI_PER_EVT_GLOB_GP3:
  7523. IPAERR("Got GSI_PER_EVT_GLOB_GP3\n");
  7524. ipa_assert();
  7525. break;
  7526. case GSI_PER_EVT_GENERAL_BREAK_POINT:
  7527. IPAERR("Got GSI_PER_EVT_GENERAL_BREAK_POINT\n");
  7528. break;
  7529. case GSI_PER_EVT_GENERAL_BUS_ERROR:
  7530. IPAERR("Got GSI_PER_EVT_GENERAL_BUS_ERROR\n");
  7531. ipa_assert();
  7532. break;
  7533. case GSI_PER_EVT_GENERAL_CMD_FIFO_OVERFLOW:
  7534. IPAERR("Got GSI_PER_EVT_GENERAL_CMD_FIFO_OVERFLOW\n");
  7535. ipa_assert();
  7536. break;
  7537. case GSI_PER_EVT_GENERAL_MCS_STACK_OVERFLOW:
  7538. IPAERR("Got GSI_PER_EVT_GENERAL_MCS_STACK_OVERFLOW\n");
  7539. ipa_assert();
  7540. break;
  7541. default:
  7542. IPAERR("Received unexpected evt: %d\n",
  7543. notify->evt_id);
  7544. ipa_assert();
  7545. }
  7546. }
  7547. int ipa3_register_ipa_ready_cb(void (*ipa_ready_cb)(void *), void *user_data)
  7548. {
  7549. struct ipa3_ready_cb_info *cb_info = NULL;
  7550. /* check ipa3_ctx existed or not */
  7551. if (!ipa3_ctx) {
  7552. IPADBG("IPA driver haven't initialized\n");
  7553. return -ENXIO;
  7554. }
  7555. mutex_lock(&ipa3_ctx->lock);
  7556. if (ipa3_ctx->ipa_initialization_complete) {
  7557. mutex_unlock(&ipa3_ctx->lock);
  7558. IPADBG("IPA driver finished initialization already\n");
  7559. return -EEXIST;
  7560. }
  7561. cb_info = kmalloc(sizeof(struct ipa3_ready_cb_info), GFP_KERNEL);
  7562. if (!cb_info) {
  7563. mutex_unlock(&ipa3_ctx->lock);
  7564. return -ENOMEM;
  7565. }
  7566. cb_info->ready_cb = ipa_ready_cb;
  7567. cb_info->user_data = user_data;
  7568. list_add_tail(&cb_info->link, &ipa3_ctx->ipa_ready_cb_list);
  7569. mutex_unlock(&ipa3_ctx->lock);
  7570. return 0;
  7571. }
  7572. int ipa3_iommu_map(struct iommu_domain *domain,
  7573. unsigned long iova, phys_addr_t paddr, size_t size, int prot)
  7574. {
  7575. struct ipa_smmu_cb_ctx *cb = NULL;
  7576. IPADBG_LOW("domain =0x%pK iova 0x%lx\n", domain, iova);
  7577. IPADBG_LOW("paddr =0x%pa size 0x%x\n", &paddr, (u32)size);
  7578. /* make sure no overlapping */
  7579. if (domain == ipa3_get_smmu_domain()) {
  7580. cb = ipa3_get_smmu_ctx(IPA_SMMU_CB_AP);
  7581. if (iova >= cb->va_start && iova < cb->va_end) {
  7582. IPAERR("iommu AP overlap addr 0x%lx\n", iova);
  7583. ipa_assert();
  7584. return -EFAULT;
  7585. }
  7586. } else if (domain == ipa3_get_wlan_smmu_domain()) {
  7587. /* wlan is one time map */
  7588. cb = ipa3_get_smmu_ctx(IPA_SMMU_CB_WLAN);
  7589. } else if (domain == ipa3_get_11ad_smmu_domain()) {
  7590. /* 11ad is one time map */
  7591. cb = ipa3_get_smmu_ctx(IPA_SMMU_CB_11AD);
  7592. } else if (domain == ipa3_get_uc_smmu_domain()) {
  7593. cb = ipa3_get_smmu_ctx(IPA_SMMU_CB_UC);
  7594. if (iova >= cb->va_start && iova < cb->va_end) {
  7595. IPAERR("iommu uC overlap addr 0x%lx\n", iova);
  7596. ipa_assert();
  7597. return -EFAULT;
  7598. }
  7599. } else {
  7600. IPAERR("Unexpected domain 0x%pK\n", domain);
  7601. ipa_assert();
  7602. return -EFAULT;
  7603. }
  7604. if (cb == NULL) {
  7605. IPAERR("Unexpected cb turning NULL for domain 0x%pK\n", domain);
  7606. ipa_assert();
  7607. }
  7608. /*
  7609. * IOMMU_CACHE is needed to make the entries cachable
  7610. * if cache coherency is enabled in dtsi.
  7611. */
  7612. if (cb->is_cache_coherent)
  7613. prot |= IOMMU_CACHE;
  7614. return iommu_map(domain, iova, paddr, size, prot);
  7615. }
  7616. /**
  7617. * ipa3_get_smmu_params()- Return the ipa3 smmu related params.
  7618. */
  7619. int ipa3_get_smmu_params(struct ipa_smmu_in_params *in,
  7620. struct ipa_smmu_out_params *out)
  7621. {
  7622. bool is_smmu_enable = false;
  7623. if (out == NULL || in == NULL) {
  7624. IPAERR("bad parms for Client SMMU out params\n");
  7625. return -EINVAL;
  7626. }
  7627. if (!ipa3_ctx) {
  7628. IPAERR("IPA not yet initialized\n");
  7629. return -EINVAL;
  7630. }
  7631. out->shared_cb = false;
  7632. switch (in->smmu_client) {
  7633. case IPA_SMMU_WLAN_CLIENT:
  7634. if (ipa3_ctx->ipa_wdi3_over_gsi ||
  7635. ipa3_ctx->ipa_wdi2_over_gsi)
  7636. is_smmu_enable =
  7637. !(ipa3_ctx->s1_bypass_arr[IPA_SMMU_CB_AP] ||
  7638. ipa3_ctx->s1_bypass_arr[IPA_SMMU_CB_WLAN]);
  7639. else
  7640. is_smmu_enable =
  7641. !(ipa3_ctx->s1_bypass_arr[IPA_SMMU_CB_UC] ||
  7642. ipa3_ctx->s1_bypass_arr[IPA_SMMU_CB_WLAN]);
  7643. break;
  7644. case IPA_SMMU_WIGIG_CLIENT:
  7645. is_smmu_enable = !(ipa3_ctx->s1_bypass_arr[IPA_SMMU_CB_UC] ||
  7646. ipa3_ctx->s1_bypass_arr[IPA_SMMU_CB_11AD] ||
  7647. ipa3_ctx->s1_bypass_arr[IPA_SMMU_CB_AP]);
  7648. if (is_smmu_enable) {
  7649. if (ipa3_ctx->s1_bypass_arr[IPA_SMMU_CB_UC] ||
  7650. ipa3_ctx->s1_bypass_arr[IPA_SMMU_CB_11AD] ||
  7651. ipa3_ctx->s1_bypass_arr[IPA_SMMU_CB_AP]) {
  7652. IPAERR("11AD SMMU Discrepancy (%d %d %d)\n",
  7653. ipa3_ctx->s1_bypass_arr[IPA_SMMU_CB_UC],
  7654. ipa3_ctx->s1_bypass_arr[IPA_SMMU_CB_AP],
  7655. ipa3_ctx->s1_bypass_arr[IPA_SMMU_CB_11AD]);
  7656. WARN_ON(1);
  7657. return -EINVAL;
  7658. }
  7659. } else {
  7660. if (!ipa3_ctx->s1_bypass_arr[IPA_SMMU_CB_UC] ||
  7661. !ipa3_ctx->s1_bypass_arr[IPA_SMMU_CB_11AD] ||
  7662. !ipa3_ctx->s1_bypass_arr[IPA_SMMU_CB_AP]) {
  7663. IPAERR("11AD SMMU Discrepancy (%d %d %d)\n",
  7664. ipa3_ctx->s1_bypass_arr[IPA_SMMU_CB_UC],
  7665. ipa3_ctx->s1_bypass_arr[IPA_SMMU_CB_AP],
  7666. ipa3_ctx->s1_bypass_arr[IPA_SMMU_CB_11AD]);
  7667. WARN_ON(1);
  7668. return -EINVAL;
  7669. }
  7670. }
  7671. out->shared_cb = (ipa3_get_smmu_ctx(IPA_SMMU_CB_11AD))->shared;
  7672. break;
  7673. case IPA_SMMU_AP_CLIENT:
  7674. is_smmu_enable =
  7675. !(ipa3_ctx->s1_bypass_arr[IPA_SMMU_CB_AP]);
  7676. break;
  7677. default:
  7678. is_smmu_enable = false;
  7679. IPAERR("Trying to get illegal clients SMMU status");
  7680. return -EINVAL;
  7681. }
  7682. out->smmu_enable = is_smmu_enable;
  7683. return 0;
  7684. }
  7685. #define MAX_LEN 96
  7686. void ipa_pc_qmp_enable(void)
  7687. {
  7688. char buf[MAX_LEN] = "{class: bcm, res: ipa_pc, val: 1}";
  7689. struct qmp_pkt pkt;
  7690. int ret = 0;
  7691. struct ipa3_pc_mbox_data *mbox_data = &ipa3_ctx->pc_mbox;
  7692. IPADBG("Enter\n");
  7693. /* prepare the mailbox struct */
  7694. mbox_data->mbox_client.dev = &ipa3_ctx->master_pdev->dev;
  7695. mbox_data->mbox_client.tx_block = true;
  7696. mbox_data->mbox_client.tx_tout = MBOX_TOUT_MS;
  7697. mbox_data->mbox_client.knows_txdone = false;
  7698. mbox_data->mbox = mbox_request_channel(&mbox_data->mbox_client, 0);
  7699. if (IS_ERR(mbox_data->mbox)) {
  7700. ret = PTR_ERR(mbox_data->mbox);
  7701. if (ret != -EPROBE_DEFER)
  7702. IPAERR("mailbox channel request failed, ret=%d\n", ret);
  7703. return;
  7704. }
  7705. /* prepare the QMP packet to send */
  7706. pkt.size = MAX_LEN;
  7707. pkt.data = buf;
  7708. /* send the QMP packet to AOP */
  7709. ret = mbox_send_message(mbox_data->mbox, &pkt);
  7710. if (ret < 0)
  7711. IPAERR("qmp message send failed, ret=%d\n", ret);
  7712. if (mbox_data->mbox) {
  7713. mbox_free_channel(mbox_data->mbox);
  7714. mbox_data->mbox = NULL;
  7715. }
  7716. }
  7717. /**************************************************************
  7718. * PCIe Version
  7719. *************************************************************/
  7720. int ipa3_pci_drv_probe(
  7721. struct pci_dev *pci_dev,
  7722. struct ipa_api_controller *api_ctrl,
  7723. const struct of_device_id *pdrv_match)
  7724. {
  7725. int result;
  7726. struct ipa3_plat_drv_res *ipa_drv_res;
  7727. u32 bar0_offset;
  7728. u32 mem_start;
  7729. u32 mem_end;
  7730. uint32_t bits;
  7731. uint32_t ipa_start, gsi_start, intctrl_start;
  7732. struct device *dev;
  7733. static struct platform_device platform_dev;
  7734. if (!pci_dev || !api_ctrl || !pdrv_match) {
  7735. IPAERR(
  7736. "Bad arg: pci_dev (%pK) and/or api_ctrl (%pK) and/or pdrv_match (%pK)\n",
  7737. pci_dev, api_ctrl, pdrv_match);
  7738. return -EOPNOTSUPP;
  7739. }
  7740. dev = &(pci_dev->dev);
  7741. IPADBG("IPA PCI driver probing started\n");
  7742. /*
  7743. * Follow PCI driver flow here.
  7744. * pci_enable_device: Enables device and assigns resources
  7745. * pci_request_region: Makes BAR0 address region usable
  7746. */
  7747. result = pci_enable_device(pci_dev);
  7748. if (result < 0) {
  7749. IPAERR("pci_enable_device() failed\n");
  7750. return -EOPNOTSUPP;
  7751. }
  7752. result = pci_request_region(pci_dev, 0, "IPA Memory");
  7753. if (result < 0) {
  7754. IPAERR("pci_request_region() failed\n");
  7755. pci_disable_device(pci_dev);
  7756. return -EOPNOTSUPP;
  7757. }
  7758. /*
  7759. * When in the PCI/emulation environment, &platform_dev is
  7760. * passed to get_ipa_dts_configuration(), but is unused, since
  7761. * all usages of it in the function are replaced by CPP
  7762. * relative to definitions in ipa_emulation_stubs.h. Passing
  7763. * &platform_dev makes code validity tools happy.
  7764. */
  7765. if (get_ipa_dts_configuration(&platform_dev, &ipa3_res) != 0) {
  7766. IPAERR("get_ipa_dts_configuration() failed\n");
  7767. pci_release_region(pci_dev, 0);
  7768. pci_disable_device(pci_dev);
  7769. return -EOPNOTSUPP;
  7770. }
  7771. ipa_drv_res = &ipa3_res;
  7772. result =
  7773. of_property_read_u32(NULL, "emulator-bar0-offset",
  7774. &bar0_offset);
  7775. if (result) {
  7776. IPAERR(":get resource failed for emulator-bar0-offset!\n");
  7777. pci_release_region(pci_dev, 0);
  7778. pci_disable_device(pci_dev);
  7779. return -ENODEV;
  7780. }
  7781. IPADBG(":using emulator-bar0-offset 0x%08X\n", bar0_offset);
  7782. ipa_start = ipa_drv_res->ipa_mem_base;
  7783. gsi_start = ipa_drv_res->transport_mem_base;
  7784. intctrl_start = ipa_drv_res->emulator_intcntrlr_mem_base;
  7785. /*
  7786. * Where will we be inerrupted at?
  7787. */
  7788. ipa_drv_res->emulator_irq = pci_dev->irq;
  7789. IPADBG(
  7790. "EMULATION PCI_INTERRUPT_PIN(%u)\n",
  7791. ipa_drv_res->emulator_irq);
  7792. /*
  7793. * Set the ipa_mem_base to the PCI base address of BAR0
  7794. */
  7795. mem_start = pci_resource_start(pci_dev, 0);
  7796. mem_end = pci_resource_end(pci_dev, 0);
  7797. IPADBG("PCI START = 0x%x\n", mem_start);
  7798. IPADBG("PCI END = 0x%x\n", mem_end);
  7799. ipa_drv_res->ipa_mem_base = mem_start + bar0_offset;
  7800. smmu_info.ipa_base = ipa_drv_res->ipa_mem_base;
  7801. smmu_info.ipa_size = ipa_drv_res->ipa_mem_size;
  7802. ipa_drv_res->transport_mem_base =
  7803. ipa_drv_res->ipa_mem_base + (gsi_start - ipa_start);
  7804. ipa_drv_res->emulator_intcntrlr_mem_base =
  7805. ipa_drv_res->ipa_mem_base + (intctrl_start - ipa_start);
  7806. IPADBG("ipa_mem_base = 0x%x\n",
  7807. ipa_drv_res->ipa_mem_base);
  7808. IPADBG("ipa_mem_size = 0x%x\n",
  7809. ipa_drv_res->ipa_mem_size);
  7810. IPADBG("transport_mem_base = 0x%x\n",
  7811. ipa_drv_res->transport_mem_base);
  7812. IPADBG("transport_mem_size = 0x%x\n",
  7813. ipa_drv_res->transport_mem_size);
  7814. IPADBG("emulator_intcntrlr_mem_base = 0x%x\n",
  7815. ipa_drv_res->emulator_intcntrlr_mem_base);
  7816. IPADBG("emulator_intcntrlr_mem_size = 0x%x\n",
  7817. ipa_drv_res->emulator_intcntrlr_mem_size);
  7818. result = ipa3_bind_api_controller(ipa_drv_res->ipa_hw_type, api_ctrl);
  7819. if (result != 0) {
  7820. IPAERR("ipa3_bind_api_controller() failed\n");
  7821. pci_release_region(pci_dev, 0);
  7822. pci_disable_device(pci_dev);
  7823. return result;
  7824. }
  7825. bits = (ipa_drv_res->use_64_bit_dma_mask) ? 64 : 32;
  7826. if (dma_set_mask(dev, DMA_BIT_MASK(bits)) != 0) {
  7827. IPAERR("dma_set_mask(%pK, %u) failed\n", dev, bits);
  7828. pci_release_region(pci_dev, 0);
  7829. pci_disable_device(pci_dev);
  7830. return -EOPNOTSUPP;
  7831. }
  7832. if (dma_set_coherent_mask(dev, DMA_BIT_MASK(bits)) != 0) {
  7833. IPAERR("dma_set_coherent_mask(%pK, %u) failed\n", dev, bits);
  7834. pci_release_region(pci_dev, 0);
  7835. pci_disable_device(pci_dev);
  7836. return -EOPNOTSUPP;
  7837. }
  7838. pci_set_master(pci_dev);
  7839. memset(&platform_dev, 0, sizeof(platform_dev));
  7840. platform_dev.dev = *dev;
  7841. /* Proceed to real initialization */
  7842. result = ipa3_pre_init(&ipa3_res, &platform_dev);
  7843. if (result) {
  7844. IPAERR("ipa3_init failed\n");
  7845. pci_clear_master(pci_dev);
  7846. pci_release_region(pci_dev, 0);
  7847. pci_disable_device(pci_dev);
  7848. return result;
  7849. }
  7850. return result;
  7851. }
  7852. /*
  7853. * The following returns transport register memory location and
  7854. * size...
  7855. */
  7856. int ipa3_get_transport_info(
  7857. phys_addr_t *phys_addr_ptr,
  7858. unsigned long *size_ptr)
  7859. {
  7860. if (!phys_addr_ptr || !size_ptr) {
  7861. IPAERR("Bad arg: phys_addr_ptr(%pK) and/or size_ptr(%pK)\n",
  7862. phys_addr_ptr, size_ptr);
  7863. return -EINVAL;
  7864. }
  7865. *phys_addr_ptr = ipa3_res.transport_mem_base;
  7866. *size_ptr = ipa3_res.transport_mem_size;
  7867. return 0;
  7868. }
  7869. EXPORT_SYMBOL(ipa3_get_transport_info);
  7870. static uint emulation_type = IPA_HW_v4_0;
  7871. /*
  7872. * The following returns emulation type...
  7873. */
  7874. uint ipa3_get_emulation_type(void)
  7875. {
  7876. return emulation_type;
  7877. }
  7878. MODULE_LICENSE("GPL v2");
  7879. MODULE_DESCRIPTION("IPA HW device driver");
  7880. /*
  7881. * Module parameter. Invoke as follows:
  7882. * insmod ipat.ko emulation_type=[13|14|17|...|N]
  7883. * Examples:
  7884. * insmod ipat.ko emulation_type=13 # for IPA 3.5.1
  7885. * insmod ipat.ko emulation_type=14 # for IPA 4.0
  7886. * insmod ipat.ko emulation_type=17 # for IPA 4.5
  7887. *
  7888. * NOTE: The emulation_type values need to come from: enum ipa_hw_type
  7889. *
  7890. */
  7891. module_param(emulation_type, uint, 0000);
  7892. MODULE_PARM_DESC(
  7893. emulation_type,
  7894. "emulation_type=N N can be 13 for IPA 3.5.1, 14 for IPA 4.0, 17 for IPA 4.5");