ipa.c 223 KB

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