ipa.c 237 KB

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