qce50.c 194 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * QTI Crypto Engine driver.
  4. *
  5. * Copyright (c) 2012-2021, The Linux Foundation. All rights reserved.
  6. */
  7. #define pr_fmt(fmt) "QCE50: %s: " fmt, __func__
  8. #include <linux/types.h>
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/mod_devicetable.h>
  12. #include <linux/device.h>
  13. #include <linux/clk.h>
  14. #include <linux/err.h>
  15. #include <linux/dma-mapping.h>
  16. #include <linux/io.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/spinlock.h>
  19. #include <linux/delay.h>
  20. #include <linux/crypto.h>
  21. #include <linux/bitops.h>
  22. #include "linux/qcrypto.h"
  23. #include <crypto/hash.h>
  24. #include <crypto/sha1.h>
  25. #include <soc/qcom/socinfo.h>
  26. #include <linux/iommu.h>
  27. #include "qce.h"
  28. #include "qce50.h"
  29. #include "qcryptohw_50.h"
  30. #include "qce_ota.h"
  31. #define CRYPTO_SMMU_IOVA_START 0x10000000
  32. #define CRYPTO_SMMU_IOVA_SIZE 0x40000000
  33. #define CRYPTO_CONFIG_RESET 0xE01EF
  34. #define MAX_SPS_DESC_FIFO_SIZE 0xfff0
  35. #define QCE_MAX_NUM_DSCR 0x200
  36. #define QCE_SECTOR_SIZE 0x200
  37. #define CE_CLK_100MHZ 100000000
  38. #define CE_CLK_DIV 1000000
  39. #define CRYPTO_CORE_MAJOR_VER_NUM 0x05
  40. #define CRYPTO_CORE_MINOR_VER_NUM 0x03
  41. #define CRYPTO_CORE_STEP_VER_NUM 0x1
  42. #define CRYPTO_REQ_USER_PAT 0xdead0000
  43. static DEFINE_MUTEX(bam_register_lock);
  44. static DEFINE_MUTEX(qce_iomap_mutex);
  45. struct bam_registration_info {
  46. struct list_head qlist;
  47. unsigned long handle;
  48. uint32_t cnt;
  49. uint32_t bam_mem;
  50. void __iomem *bam_iobase;
  51. bool support_cmd_dscr;
  52. };
  53. static LIST_HEAD(qce50_bam_list);
  54. /* Used to determine the mode */
  55. #define MAX_BUNCH_MODE_REQ 2
  56. /* Max number of request supported */
  57. #define MAX_QCE_BAM_REQ 8
  58. /* Interrupt flag will be set for every SET_INTR_AT_REQ request */
  59. #define SET_INTR_AT_REQ (MAX_QCE_BAM_REQ / 2)
  60. /* To create extra request space to hold dummy request */
  61. #define MAX_QCE_BAM_REQ_WITH_DUMMY_REQ (MAX_QCE_BAM_REQ + 1)
  62. /* Allocate the memory for MAX_QCE_BAM_REQ + 1 (for dummy request) */
  63. #define MAX_QCE_ALLOC_BAM_REQ MAX_QCE_BAM_REQ_WITH_DUMMY_REQ
  64. /* QCE driver modes */
  65. #define IN_INTERRUPT_MODE 0
  66. #define IN_BUNCH_MODE 1
  67. /* Dummy request data length */
  68. #define DUMMY_REQ_DATA_LEN 64
  69. /* Delay timer to expire when in bunch mode */
  70. #define DELAY_IN_JIFFIES 5
  71. /* Index to point the dummy request */
  72. #define DUMMY_REQ_INDEX MAX_QCE_BAM_REQ
  73. #define TOTAL_IOVEC_SPACE_PER_PIPE (QCE_MAX_NUM_DSCR * sizeof(struct sps_iovec))
  74. #define AES_CTR_IV_CTR_SIZE 64
  75. #define QCE_STATUS1_NO_ERROR 0x2000006
  76. // Crypto Engines 5.7 and below
  77. // Key timer expiry for pipes 1-15 (Status3)
  78. #define CRYPTO5_LEGACY_TIMER_EXPIRED_STATUS3 0x0000FF00
  79. // Key timer expiry for pipes 16-19 (Status6)
  80. #define CRYPTO5_LEGACY_TIMER_EXPIRED_STATUS6 0x00000300
  81. // Key pause for pipes 1-15 (Status3)
  82. #define CRYPTO5_LEGACY_KEY_PAUSE_STATUS3 0xFF000000
  83. // Key pause for pipes 16-19 (Status6)
  84. #define CRYPTO5_LEGACY_KEY_PAUSE_STATUS6 0x3000000
  85. // Crypto Engines 5.8 and above
  86. // Key timer expiry for all pipes (Status3)
  87. #define CRYPTO58_TIMER_EXPIRED 0x00000010
  88. // Key pause for all pipes (Status3)
  89. #define CRYPTO58_KEY_PAUSE 0x00001000
  90. // Key index for Status3 (Timer and Key Pause)
  91. #define KEY_INDEX_SHIFT 16
  92. enum qce_owner {
  93. QCE_OWNER_NONE = 0,
  94. QCE_OWNER_CLIENT = 1,
  95. QCE_OWNER_TIMEOUT = 2
  96. };
  97. struct dummy_request {
  98. struct qce_sha_req sreq;
  99. struct scatterlist sg;
  100. struct ahash_request areq;
  101. };
  102. /*
  103. * CE HW device structure.
  104. * Each engine has an instance of the structure.
  105. * Each engine can only handle one crypto operation at one time. It is up to
  106. * the sw above to ensure single threading of operation on an engine.
  107. */
  108. struct qce_device {
  109. struct device *pdev; /* Handle to platform_device structure */
  110. struct bam_registration_info *pbam;
  111. unsigned char *coh_vmem; /* Allocated coherent virtual memory */
  112. dma_addr_t coh_pmem; /* Allocated coherent physical memory */
  113. int memsize; /* Memory allocated */
  114. unsigned char *iovec_vmem; /* Allocate iovec virtual memory */
  115. int iovec_memsize; /* Memory allocated */
  116. uint32_t bam_mem; /* bam physical address, from DT */
  117. uint32_t bam_mem_size; /* bam io size, from DT */
  118. int is_shared; /* CE HW is shared */
  119. bool support_cmd_dscr;
  120. bool support_hw_key;
  121. bool support_clk_mgmt_sus_res;
  122. bool support_only_core_src_clk;
  123. bool request_bw_before_clk;
  124. void __iomem *iobase; /* Virtual io base of CE HW */
  125. unsigned int phy_iobase; /* Physical io base of CE HW */
  126. struct clk *ce_core_src_clk; /* Handle to CE src clk*/
  127. struct clk *ce_core_clk; /* Handle to CE clk */
  128. struct clk *ce_clk; /* Handle to CE clk */
  129. struct clk *ce_bus_clk; /* Handle to CE AXI clk*/
  130. bool no_get_around;
  131. bool no_ccm_mac_status_get_around;
  132. unsigned int ce_opp_freq_hz;
  133. bool use_sw_aes_cbc_ecb_ctr_algo;
  134. bool use_sw_aead_algo;
  135. bool use_sw_aes_xts_algo;
  136. bool use_sw_ahash_algo;
  137. bool use_sw_hmac_algo;
  138. bool use_sw_aes_ccm_algo;
  139. uint32_t engines_avail;
  140. struct qce_ce_cfg_reg_setting reg;
  141. struct ce_bam_info ce_bam_info;
  142. struct ce_request_info ce_request_info[MAX_QCE_ALLOC_BAM_REQ];
  143. unsigned int ce_request_index;
  144. enum qce_owner owner;
  145. atomic_t no_of_queued_req;
  146. struct timer_list timer;
  147. struct dummy_request dummyreq;
  148. unsigned int mode;
  149. unsigned int intr_cadence;
  150. unsigned int dev_no;
  151. struct qce_driver_stats qce_stats;
  152. atomic_t bunch_cmd_seq;
  153. atomic_t last_intr_seq;
  154. bool cadence_flag;
  155. uint8_t *dummyreq_in_buf;
  156. struct dma_iommu_mapping *smmu_mapping;
  157. bool enable_s1_smmu;
  158. bool no_clock_support;
  159. bool kernel_pipes_support;
  160. bool offload_pipes_support;
  161. };
  162. static void print_notify_debug(struct sps_event_notify *notify);
  163. static void _sps_producer_callback(struct sps_event_notify *notify);
  164. static int qce_dummy_req(struct qce_device *pce_dev);
  165. static int _qce50_disp_stats;
  166. /* Standard initialization vector for SHA-1, source: FIPS 180-2 */
  167. static uint32_t _std_init_vector_sha1[] = {
  168. 0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0
  169. };
  170. /* Standard initialization vector for SHA-256, source: FIPS 180-2 */
  171. static uint32_t _std_init_vector_sha256[] = {
  172. 0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A,
  173. 0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19
  174. };
  175. /*
  176. * Requests for offload operations do not require explicit dma operations
  177. * as they already have SMMU mapped source/destination buffers.
  178. */
  179. static bool is_offload_op(int op)
  180. {
  181. return (op == QCE_OFFLOAD_HLOS_HLOS || op == QCE_OFFLOAD_HLOS_CPB ||
  182. op == QCE_OFFLOAD_CPB_HLOS);
  183. }
  184. static uint32_t qce_get_config_be(struct qce_device *pce_dev,
  185. uint32_t pipe_pair)
  186. {
  187. uint32_t beats = (pce_dev->ce_bam_info.ce_burst_size >> 3) - 1;
  188. return (beats << CRYPTO_REQ_SIZE |
  189. BIT(CRYPTO_MASK_DOUT_INTR) | BIT(CRYPTO_MASK_DIN_INTR) |
  190. BIT(CRYPTO_MASK_OP_DONE_INTR) | 0 << CRYPTO_HIGH_SPD_EN_N |
  191. pipe_pair << CRYPTO_PIPE_SET_SELECT);
  192. }
  193. static void dump_status_regs(unsigned int *status)
  194. {
  195. pr_info("%s: CRYPTO_STATUS_REG = 0x%x\n", __func__, status[0]);
  196. pr_info("%s: CRYPTO_STATUS2_REG = 0x%x\n", __func__, status[1]);
  197. pr_info("%s: CRYPTO_STATUS3_REG = 0x%x\n", __func__, status[2]);
  198. pr_info("%s: CRYPTO_STATUS4_REG = 0x%x\n", __func__, status[3]);
  199. pr_info("%s: CRYPTO_STATUS5_REG = 0x%x\n", __func__, status[4]);
  200. pr_info("%s: CRYPTO_STATUS6_REG = 0x%x\n", __func__, status[5]);
  201. }
  202. void qce_get_crypto_status(void *handle, struct qce_error *error)
  203. {
  204. struct qce_device *pce_dev = (struct qce_device *) handle;
  205. unsigned int status[6] = {0};
  206. status[0] = readl_relaxed(pce_dev->iobase + CRYPTO_STATUS_REG);
  207. status[1] = readl_relaxed(pce_dev->iobase + CRYPTO_STATUS2_REG);
  208. status[2] = readl_relaxed(pce_dev->iobase + CRYPTO_STATUS3_REG);
  209. status[3] = readl_relaxed(pce_dev->iobase + CRYPTO_STATUS4_REG);
  210. status[4] = readl_relaxed(pce_dev->iobase + CRYPTO_STATUS5_REG);
  211. status[5] = readl_relaxed(pce_dev->iobase + CRYPTO_STATUS6_REG);
  212. #ifdef QCE_DEBUG
  213. dump_status_regs(status);
  214. #endif
  215. if (status[0] != QCE_STATUS1_NO_ERROR) {
  216. if (pce_dev->ce_bam_info.minor_version >= 8) {
  217. if (status[2] & CRYPTO58_TIMER_EXPIRED) {
  218. error->timer_error = true;
  219. pr_err("%s: timer expired, index = 0x%x\n",
  220. __func__, (status[2] >> KEY_INDEX_SHIFT));
  221. } else if (status[2] & CRYPTO58_KEY_PAUSE) {
  222. error->key_paused = true;
  223. pr_err("%s: key paused, index = 0x%x\n",
  224. __func__, (status[2] >> KEY_INDEX_SHIFT));
  225. } else {
  226. pr_err("%s: generic error, refer all status\n",
  227. __func__);
  228. error->generic_error = true;
  229. }
  230. } else {
  231. if ((status[2] & CRYPTO5_LEGACY_TIMER_EXPIRED_STATUS3) ||
  232. (status[5] & CRYPTO5_LEGACY_TIMER_EXPIRED_STATUS6)) {
  233. error->timer_error = true;
  234. pr_err("%s: timer expired, refer status 3 and 6\n",
  235. __func__);
  236. }
  237. else if ((status[2] & CRYPTO5_LEGACY_KEY_PAUSE_STATUS3) ||
  238. (status[5] & CRYPTO5_LEGACY_KEY_PAUSE_STATUS6)) {
  239. error->key_paused = true;
  240. pr_err("%s: key paused, reder status 3 and 6\n",
  241. __func__);
  242. } else {
  243. pr_err("%s: generic error, refer all status\n",
  244. __func__);
  245. error->generic_error = true;
  246. }
  247. }
  248. dump_status_regs(status);
  249. return;
  250. }
  251. error->no_error = true;
  252. pr_err("%s: No crypto error, status1 = 0x%x\n",
  253. __func__, status[0]);
  254. return;
  255. }
  256. EXPORT_SYMBOL(qce_get_crypto_status);
  257. static int qce_crypto_config(struct qce_device *pce_dev,
  258. enum qce_offload_op_enum offload_op)
  259. {
  260. uint32_t config_be = 0;
  261. switch (offload_op) {
  262. case QCE_OFFLOAD_NONE:
  263. config_be = qce_get_config_be(pce_dev,
  264. pce_dev->ce_bam_info.pipe_pair_index[QCE_OFFLOAD_NONE]);
  265. break;
  266. case QCE_OFFLOAD_HLOS_HLOS:
  267. config_be = qce_get_config_be(pce_dev,
  268. pce_dev->ce_bam_info.pipe_pair_index[QCE_OFFLOAD_HLOS_HLOS]);
  269. break;
  270. case QCE_OFFLOAD_HLOS_CPB:
  271. config_be = qce_get_config_be(pce_dev,
  272. pce_dev->ce_bam_info.pipe_pair_index[QCE_OFFLOAD_HLOS_CPB]);
  273. break;
  274. case QCE_OFFLOAD_CPB_HLOS:
  275. config_be = qce_get_config_be(pce_dev,
  276. pce_dev->ce_bam_info.pipe_pair_index[QCE_OFFLOAD_CPB_HLOS]);
  277. break;
  278. default:
  279. pr_err("%s: Valid pipe config not set, offload op = %d\n",
  280. __func__, offload_op);
  281. return -EINVAL;
  282. }
  283. pce_dev->reg.crypto_cfg_be = config_be;
  284. pce_dev->reg.crypto_cfg_le = (config_be |
  285. CRYPTO_LITTLE_ENDIAN_MASK);
  286. return 0;
  287. }
  288. static void qce_enable_clock_gating(struct qce_device *pce_dev)
  289. {
  290. /* This feature might cause some HW issues, noop till resolved. */
  291. return;
  292. }
  293. /*
  294. * IV counter mask is be set based on the values sent through the offload ioctl
  295. * calls. Currently for offload operations, it is 64 bytes of mask for AES CTR,
  296. * and 128 bytes of mask for AES CBC.
  297. */
  298. static void qce_set_iv_ctr_mask(struct qce_device *pce_dev,
  299. struct qce_req *creq)
  300. {
  301. if (creq->iv_ctr_size == AES_CTR_IV_CTR_SIZE) {
  302. pce_dev->reg.encr_cntr_mask_0 = 0x0;
  303. pce_dev->reg.encr_cntr_mask_1 = 0x0;
  304. pce_dev->reg.encr_cntr_mask_2 = 0xFFFFFFFF;
  305. pce_dev->reg.encr_cntr_mask_3 = 0xFFFFFFFF;
  306. } else {
  307. pce_dev->reg.encr_cntr_mask_0 = 0xFFFFFFFF;
  308. pce_dev->reg.encr_cntr_mask_1 = 0xFFFFFFFF;
  309. pce_dev->reg.encr_cntr_mask_2 = 0xFFFFFFFF;
  310. pce_dev->reg.encr_cntr_mask_3 = 0xFFFFFFFF;
  311. }
  312. return;
  313. }
  314. static void _byte_stream_to_net_words(uint32_t *iv, unsigned char *b,
  315. unsigned int len)
  316. {
  317. unsigned int n;
  318. n = len / sizeof(uint32_t);
  319. for (; n > 0; n--) {
  320. *iv = ((*b << 24) & 0xff000000) |
  321. (((*(b+1)) << 16) & 0xff0000) |
  322. (((*(b+2)) << 8) & 0xff00) |
  323. (*(b+3) & 0xff);
  324. b += sizeof(uint32_t);
  325. iv++;
  326. }
  327. n = len % sizeof(uint32_t);
  328. if (n == 3) {
  329. *iv = ((*b << 24) & 0xff000000) |
  330. (((*(b+1)) << 16) & 0xff0000) |
  331. (((*(b+2)) << 8) & 0xff00);
  332. } else if (n == 2) {
  333. *iv = ((*b << 24) & 0xff000000) |
  334. (((*(b+1)) << 16) & 0xff0000);
  335. } else if (n == 1) {
  336. *iv = ((*b << 24) & 0xff000000);
  337. }
  338. }
  339. static void _byte_stream_swap_to_net_words(uint32_t *iv, unsigned char *b,
  340. unsigned int len)
  341. {
  342. unsigned int i, j;
  343. unsigned char swap_iv[AES_IV_LENGTH];
  344. memset(swap_iv, 0, AES_IV_LENGTH);
  345. for (i = (AES_IV_LENGTH-len), j = len-1; i < AES_IV_LENGTH; i++, j--)
  346. swap_iv[i] = b[j];
  347. _byte_stream_to_net_words(iv, swap_iv, AES_IV_LENGTH);
  348. }
  349. static int count_sg(struct scatterlist *sg, int nbytes)
  350. {
  351. int i;
  352. for (i = 0; nbytes > 0; i++, sg = sg_next(sg))
  353. nbytes -= sg->length;
  354. return i;
  355. }
  356. static int qce_dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
  357. enum dma_data_direction direction)
  358. {
  359. int i;
  360. for (i = 0; i < nents; ++i) {
  361. dma_map_sg(dev, sg, 1, direction);
  362. sg = sg_next(sg);
  363. }
  364. return nents;
  365. }
  366. static int qce_dma_unmap_sg(struct device *dev, struct scatterlist *sg,
  367. int nents, enum dma_data_direction direction)
  368. {
  369. int i;
  370. for (i = 0; i < nents; ++i) {
  371. dma_unmap_sg(dev, sg, 1, direction);
  372. sg = sg_next(sg);
  373. }
  374. return nents;
  375. }
  376. static int _probe_ce_engine(struct qce_device *pce_dev)
  377. {
  378. unsigned int rev;
  379. unsigned int maj_rev, min_rev, step_rev;
  380. rev = readl_relaxed(pce_dev->iobase + CRYPTO_VERSION_REG);
  381. /*
  382. * Ensure previous instructions (setting the GO register)
  383. * was completed before checking the version.
  384. */
  385. mb();
  386. maj_rev = (rev & CRYPTO_CORE_MAJOR_REV_MASK) >> CRYPTO_CORE_MAJOR_REV;
  387. min_rev = (rev & CRYPTO_CORE_MINOR_REV_MASK) >> CRYPTO_CORE_MINOR_REV;
  388. step_rev = (rev & CRYPTO_CORE_STEP_REV_MASK) >> CRYPTO_CORE_STEP_REV;
  389. if (maj_rev != CRYPTO_CORE_MAJOR_VER_NUM) {
  390. pr_err("Unsupported QTI crypto device at 0x%x, rev %d.%d.%d\n",
  391. pce_dev->phy_iobase, maj_rev, min_rev, step_rev);
  392. return -EIO;
  393. }
  394. /*
  395. * The majority of crypto HW bugs have been fixed in 5.3.0 and
  396. * above. That allows a single sps transfer of consumer
  397. * pipe, and a single sps transfer of producer pipe
  398. * for a crypto request. no_get_around flag indicates this.
  399. *
  400. * In 5.3.1, the CCM MAC_FAILED in result dump issue is
  401. * fixed. no_ccm_mac_status_get_around flag indicates this.
  402. */
  403. pce_dev->no_get_around = (min_rev >=
  404. CRYPTO_CORE_MINOR_VER_NUM) ? true : false;
  405. if (min_rev > CRYPTO_CORE_MINOR_VER_NUM)
  406. pce_dev->no_ccm_mac_status_get_around = true;
  407. else if ((min_rev == CRYPTO_CORE_MINOR_VER_NUM) &&
  408. (step_rev >= CRYPTO_CORE_STEP_VER_NUM))
  409. pce_dev->no_ccm_mac_status_get_around = true;
  410. else
  411. pce_dev->no_ccm_mac_status_get_around = false;
  412. pce_dev->ce_bam_info.minor_version = min_rev;
  413. pce_dev->ce_bam_info.major_version = maj_rev;
  414. pce_dev->engines_avail = readl_relaxed(pce_dev->iobase +
  415. CRYPTO_ENGINES_AVAIL);
  416. dev_info(pce_dev->pdev, "QTI Crypto %d.%d.%d device found @0x%x\n",
  417. maj_rev, min_rev, step_rev, pce_dev->phy_iobase);
  418. pce_dev->ce_bam_info.ce_burst_size = MAX_CE_BAM_BURST_SIZE;
  419. dev_dbg(pce_dev->pdev, "CE device = %#x IO base, CE = %pK Consumer (IN) PIPE %d,\nProducer (OUT) PIPE %d IO base BAM = %pK\nBAM IRQ %d Engines Availability = %#x\n",
  420. pce_dev->ce_bam_info.ce_device, pce_dev->iobase,
  421. pce_dev->ce_bam_info.dest_pipe_index,
  422. pce_dev->ce_bam_info.src_pipe_index,
  423. pce_dev->ce_bam_info.bam_iobase,
  424. pce_dev->ce_bam_info.bam_irq, pce_dev->engines_avail);
  425. return 0;
  426. };
  427. static struct qce_cmdlist_info *_ce_get_hash_cmdlistinfo(
  428. struct qce_device *pce_dev,
  429. int req_info, struct qce_sha_req *sreq)
  430. {
  431. struct ce_sps_data *pce_sps_data;
  432. struct qce_cmdlistptr_ops *cmdlistptr;
  433. pce_sps_data = &pce_dev->ce_request_info[req_info].ce_sps;
  434. cmdlistptr = &pce_sps_data->cmdlistptr;
  435. switch (sreq->alg) {
  436. case QCE_HASH_SHA1:
  437. return &cmdlistptr->auth_sha1;
  438. case QCE_HASH_SHA256:
  439. return &cmdlistptr->auth_sha256;
  440. case QCE_HASH_SHA1_HMAC:
  441. return &cmdlistptr->auth_sha1_hmac;
  442. case QCE_HASH_SHA256_HMAC:
  443. return &cmdlistptr->auth_sha256_hmac;
  444. case QCE_HASH_AES_CMAC:
  445. if (sreq->authklen == AES128_KEY_SIZE)
  446. return &cmdlistptr->auth_aes_128_cmac;
  447. return &cmdlistptr->auth_aes_256_cmac;
  448. default:
  449. return NULL;
  450. }
  451. return NULL;
  452. }
  453. static int _ce_setup_hash(struct qce_device *pce_dev,
  454. struct qce_sha_req *sreq,
  455. struct qce_cmdlist_info *cmdlistinfo)
  456. {
  457. uint32_t auth32[SHA256_DIGEST_SIZE / sizeof(uint32_t)];
  458. uint32_t diglen;
  459. int i;
  460. uint32_t mackey32[SHA_HMAC_KEY_SIZE/sizeof(uint32_t)] = {
  461. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  462. bool sha1 = false;
  463. struct sps_command_element *pce = NULL;
  464. bool use_hw_key = false;
  465. bool use_pipe_key = false;
  466. uint32_t authk_size_in_word = sreq->authklen/sizeof(uint32_t);
  467. uint32_t auth_cfg;
  468. if (qce_crypto_config(pce_dev, QCE_OFFLOAD_NONE))
  469. return -EINVAL;
  470. pce = cmdlistinfo->crypto_cfg;
  471. pce->data = pce_dev->reg.crypto_cfg_be;
  472. pce = cmdlistinfo->crypto_cfg_le;
  473. pce->data = pce_dev->reg.crypto_cfg_le;
  474. if ((sreq->alg == QCE_HASH_SHA1_HMAC) ||
  475. (sreq->alg == QCE_HASH_SHA256_HMAC) ||
  476. (sreq->alg == QCE_HASH_AES_CMAC)) {
  477. /* no more check for null key. use flag */
  478. if ((sreq->flags & QCRYPTO_CTX_USE_HW_KEY)
  479. == QCRYPTO_CTX_USE_HW_KEY)
  480. use_hw_key = true;
  481. else if ((sreq->flags & QCRYPTO_CTX_USE_PIPE_KEY) ==
  482. QCRYPTO_CTX_USE_PIPE_KEY)
  483. use_pipe_key = true;
  484. pce = cmdlistinfo->go_proc;
  485. if (use_hw_key) {
  486. pce->addr = (uint32_t)(CRYPTO_GOPROC_QC_KEY_REG +
  487. pce_dev->phy_iobase);
  488. } else {
  489. pce->addr = (uint32_t)(CRYPTO_GOPROC_REG +
  490. pce_dev->phy_iobase);
  491. pce = cmdlistinfo->auth_key;
  492. if (!use_pipe_key) {
  493. _byte_stream_to_net_words(mackey32,
  494. sreq->authkey,
  495. sreq->authklen);
  496. for (i = 0; i < authk_size_in_word; i++, pce++)
  497. pce->data = mackey32[i];
  498. }
  499. }
  500. }
  501. if (sreq->alg == QCE_HASH_AES_CMAC)
  502. goto go_proc;
  503. /* if not the last, the size has to be on the block boundary */
  504. if (!sreq->last_blk && (sreq->size % SHA256_BLOCK_SIZE))
  505. return -EIO;
  506. switch (sreq->alg) {
  507. case QCE_HASH_SHA1:
  508. case QCE_HASH_SHA1_HMAC:
  509. diglen = SHA1_DIGEST_SIZE;
  510. sha1 = true;
  511. break;
  512. case QCE_HASH_SHA256:
  513. case QCE_HASH_SHA256_HMAC:
  514. diglen = SHA256_DIGEST_SIZE;
  515. break;
  516. default:
  517. return -EINVAL;
  518. }
  519. /* write 20/32 bytes, 5/8 words into auth_iv for SHA1/SHA256 */
  520. if (sreq->first_blk) {
  521. if (sha1) {
  522. for (i = 0; i < 5; i++)
  523. auth32[i] = _std_init_vector_sha1[i];
  524. } else {
  525. for (i = 0; i < 8; i++)
  526. auth32[i] = _std_init_vector_sha256[i];
  527. }
  528. } else {
  529. _byte_stream_to_net_words(auth32, sreq->digest, diglen);
  530. }
  531. pce = cmdlistinfo->auth_iv;
  532. for (i = 0; i < 5; i++, pce++)
  533. pce->data = auth32[i];
  534. if ((sreq->alg == QCE_HASH_SHA256) ||
  535. (sreq->alg == QCE_HASH_SHA256_HMAC)) {
  536. for (i = 5; i < 8; i++, pce++)
  537. pce->data = auth32[i];
  538. }
  539. /* write auth_bytecnt 0/1, start with 0 */
  540. pce = cmdlistinfo->auth_bytecount;
  541. for (i = 0; i < 2; i++, pce++)
  542. pce->data = sreq->auth_data[i];
  543. /* Set/reset last bit in CFG register */
  544. pce = cmdlistinfo->auth_seg_cfg;
  545. auth_cfg = pce->data & ~(1 << CRYPTO_LAST |
  546. 1 << CRYPTO_FIRST |
  547. 1 << CRYPTO_USE_PIPE_KEY_AUTH |
  548. 1 << CRYPTO_USE_HW_KEY_AUTH);
  549. if (sreq->last_blk)
  550. auth_cfg |= 1 << CRYPTO_LAST;
  551. if (sreq->first_blk)
  552. auth_cfg |= 1 << CRYPTO_FIRST;
  553. if (use_hw_key)
  554. auth_cfg |= 1 << CRYPTO_USE_HW_KEY_AUTH;
  555. if (use_pipe_key)
  556. auth_cfg |= 1 << CRYPTO_USE_PIPE_KEY_AUTH;
  557. pce->data = auth_cfg;
  558. go_proc:
  559. /* write auth seg size */
  560. pce = cmdlistinfo->auth_seg_size;
  561. pce->data = sreq->size;
  562. pce = cmdlistinfo->encr_seg_cfg;
  563. pce->data = 0;
  564. /* write auth seg size start*/
  565. pce = cmdlistinfo->auth_seg_start;
  566. pce->data = 0;
  567. /* write seg size */
  568. pce = cmdlistinfo->seg_size;
  569. /* always ensure there is input data. ZLT does not work for bam-ndp */
  570. if (sreq->size)
  571. pce->data = sreq->size;
  572. else
  573. pce->data = pce_dev->ce_bam_info.ce_burst_size;
  574. return 0;
  575. }
  576. static struct qce_cmdlist_info *_ce_get_aead_cmdlistinfo(
  577. struct qce_device *pce_dev,
  578. int req_info, struct qce_req *creq)
  579. {
  580. struct ce_sps_data *pce_sps_data;
  581. struct qce_cmdlistptr_ops *cmdlistptr;
  582. pce_sps_data = &pce_dev->ce_request_info[req_info].ce_sps;
  583. cmdlistptr = &pce_sps_data->cmdlistptr;
  584. switch (creq->alg) {
  585. case CIPHER_ALG_DES:
  586. switch (creq->mode) {
  587. case QCE_MODE_CBC:
  588. if (creq->auth_alg == QCE_HASH_SHA1_HMAC)
  589. return &cmdlistptr->aead_hmac_sha1_cbc_des;
  590. else if (creq->auth_alg == QCE_HASH_SHA256_HMAC)
  591. return &cmdlistptr->aead_hmac_sha256_cbc_des;
  592. else
  593. return NULL;
  594. break;
  595. default:
  596. return NULL;
  597. }
  598. break;
  599. case CIPHER_ALG_3DES:
  600. switch (creq->mode) {
  601. case QCE_MODE_CBC:
  602. if (creq->auth_alg == QCE_HASH_SHA1_HMAC)
  603. return &cmdlistptr->aead_hmac_sha1_cbc_3des;
  604. else if (creq->auth_alg == QCE_HASH_SHA256_HMAC)
  605. return &cmdlistptr->aead_hmac_sha256_cbc_3des;
  606. else
  607. return NULL;
  608. break;
  609. default:
  610. return NULL;
  611. }
  612. break;
  613. case CIPHER_ALG_AES:
  614. switch (creq->mode) {
  615. case QCE_MODE_CBC:
  616. if (creq->encklen == AES128_KEY_SIZE) {
  617. if (creq->auth_alg == QCE_HASH_SHA1_HMAC)
  618. return
  619. &cmdlistptr->aead_hmac_sha1_cbc_aes_128;
  620. else if (creq->auth_alg == QCE_HASH_SHA256_HMAC)
  621. return
  622. &cmdlistptr->aead_hmac_sha256_cbc_aes_128;
  623. else
  624. return NULL;
  625. } else if (creq->encklen == AES256_KEY_SIZE) {
  626. if (creq->auth_alg == QCE_HASH_SHA1_HMAC)
  627. return &cmdlistptr->aead_hmac_sha1_cbc_aes_256;
  628. else if (creq->auth_alg == QCE_HASH_SHA256_HMAC)
  629. return
  630. &cmdlistptr->aead_hmac_sha256_cbc_aes_256;
  631. else
  632. return NULL;
  633. } else
  634. return NULL;
  635. break;
  636. default:
  637. return NULL;
  638. }
  639. break;
  640. default:
  641. return NULL;
  642. }
  643. return NULL;
  644. }
  645. static int _ce_setup_aead(struct qce_device *pce_dev, struct qce_req *q_req,
  646. uint32_t totallen_in, uint32_t coffset,
  647. struct qce_cmdlist_info *cmdlistinfo)
  648. {
  649. int32_t authk_size_in_word = SHA_HMAC_KEY_SIZE/sizeof(uint32_t);
  650. int i;
  651. uint32_t mackey32[SHA_HMAC_KEY_SIZE/sizeof(uint32_t)] = {0};
  652. struct sps_command_element *pce;
  653. uint32_t a_cfg;
  654. uint32_t enckey32[(MAX_CIPHER_KEY_SIZE*2)/sizeof(uint32_t)] = {0};
  655. uint32_t enciv32[MAX_IV_LENGTH/sizeof(uint32_t)] = {0};
  656. uint32_t enck_size_in_word = 0;
  657. uint32_t enciv_in_word;
  658. uint32_t key_size;
  659. uint32_t encr_cfg = 0;
  660. uint32_t ivsize = q_req->ivsize;
  661. key_size = q_req->encklen;
  662. enck_size_in_word = key_size/sizeof(uint32_t);
  663. if (qce_crypto_config(pce_dev, q_req->offload_op))
  664. return -EINVAL;
  665. pce = cmdlistinfo->crypto_cfg;
  666. pce->data = pce_dev->reg.crypto_cfg_be;
  667. pce = cmdlistinfo->crypto_cfg_le;
  668. pce->data = pce_dev->reg.crypto_cfg_le;
  669. switch (q_req->alg) {
  670. case CIPHER_ALG_DES:
  671. enciv_in_word = 2;
  672. break;
  673. case CIPHER_ALG_3DES:
  674. enciv_in_word = 2;
  675. break;
  676. case CIPHER_ALG_AES:
  677. if ((key_size != AES128_KEY_SIZE) &&
  678. (key_size != AES256_KEY_SIZE))
  679. return -EINVAL;
  680. enciv_in_word = 4;
  681. break;
  682. default:
  683. return -EINVAL;
  684. }
  685. /* only support cbc mode */
  686. if (q_req->mode != QCE_MODE_CBC)
  687. return -EINVAL;
  688. _byte_stream_to_net_words(enciv32, q_req->iv, ivsize);
  689. pce = cmdlistinfo->encr_cntr_iv;
  690. for (i = 0; i < enciv_in_word; i++, pce++)
  691. pce->data = enciv32[i];
  692. /*
  693. * write encr key
  694. * do not use hw key or pipe key
  695. */
  696. _byte_stream_to_net_words(enckey32, q_req->enckey, key_size);
  697. pce = cmdlistinfo->encr_key;
  698. for (i = 0; i < enck_size_in_word; i++, pce++)
  699. pce->data = enckey32[i];
  700. /* write encr seg cfg */
  701. pce = cmdlistinfo->encr_seg_cfg;
  702. encr_cfg = pce->data;
  703. if (q_req->dir == QCE_ENCRYPT)
  704. encr_cfg |= (1 << CRYPTO_ENCODE);
  705. else
  706. encr_cfg &= ~(1 << CRYPTO_ENCODE);
  707. pce->data = encr_cfg;
  708. /* we only support sha1-hmac and sha256-hmac at this point */
  709. _byte_stream_to_net_words(mackey32, q_req->authkey,
  710. q_req->authklen);
  711. pce = cmdlistinfo->auth_key;
  712. for (i = 0; i < authk_size_in_word; i++, pce++)
  713. pce->data = mackey32[i];
  714. pce = cmdlistinfo->auth_iv;
  715. if (q_req->auth_alg == QCE_HASH_SHA1_HMAC)
  716. for (i = 0; i < 5; i++, pce++)
  717. pce->data = _std_init_vector_sha1[i];
  718. else
  719. for (i = 0; i < 8; i++, pce++)
  720. pce->data = _std_init_vector_sha256[i];
  721. /* write auth_bytecnt 0/1, start with 0 */
  722. pce = cmdlistinfo->auth_bytecount;
  723. for (i = 0; i < 2; i++, pce++)
  724. pce->data = 0;
  725. pce = cmdlistinfo->auth_seg_cfg;
  726. a_cfg = pce->data;
  727. a_cfg &= ~(CRYPTO_AUTH_POS_MASK);
  728. if (q_req->dir == QCE_ENCRYPT)
  729. a_cfg |= (CRYPTO_AUTH_POS_AFTER << CRYPTO_AUTH_POS);
  730. else
  731. a_cfg |= (CRYPTO_AUTH_POS_BEFORE << CRYPTO_AUTH_POS);
  732. pce->data = a_cfg;
  733. /* write auth seg size */
  734. pce = cmdlistinfo->auth_seg_size;
  735. pce->data = totallen_in;
  736. /* write auth seg size start*/
  737. pce = cmdlistinfo->auth_seg_start;
  738. pce->data = 0;
  739. /* write seg size */
  740. pce = cmdlistinfo->seg_size;
  741. pce->data = totallen_in;
  742. /* write encr seg size */
  743. pce = cmdlistinfo->encr_seg_size;
  744. pce->data = q_req->cryptlen;
  745. /* write encr seg start */
  746. pce = cmdlistinfo->encr_seg_start;
  747. pce->data = (coffset & 0xffff);
  748. return 0;
  749. }
  750. static struct qce_cmdlist_info *_ce_get_cipher_cmdlistinfo(
  751. struct qce_device *pce_dev,
  752. int req_info, struct qce_req *creq)
  753. {
  754. struct ce_request_info *preq_info;
  755. struct ce_sps_data *pce_sps_data;
  756. struct qce_cmdlistptr_ops *cmdlistptr;
  757. preq_info = &pce_dev->ce_request_info[req_info];
  758. pce_sps_data = &preq_info->ce_sps;
  759. cmdlistptr = &pce_sps_data->cmdlistptr;
  760. if (creq->alg != CIPHER_ALG_AES) {
  761. switch (creq->alg) {
  762. case CIPHER_ALG_DES:
  763. if (creq->mode == QCE_MODE_ECB)
  764. return &cmdlistptr->cipher_des_ecb;
  765. return &cmdlistptr->cipher_des_cbc;
  766. case CIPHER_ALG_3DES:
  767. if (creq->mode == QCE_MODE_ECB)
  768. return &cmdlistptr->cipher_3des_ecb;
  769. return &cmdlistptr->cipher_3des_cbc;
  770. default:
  771. return NULL;
  772. }
  773. } else {
  774. switch (creq->mode) {
  775. case QCE_MODE_ECB:
  776. if (creq->encklen == AES128_KEY_SIZE)
  777. return &cmdlistptr->cipher_aes_128_ecb;
  778. return &cmdlistptr->cipher_aes_256_ecb;
  779. case QCE_MODE_CBC:
  780. case QCE_MODE_CTR:
  781. if (creq->encklen == AES128_KEY_SIZE)
  782. return &cmdlistptr->cipher_aes_128_cbc_ctr;
  783. return &cmdlistptr->cipher_aes_256_cbc_ctr;
  784. case QCE_MODE_XTS:
  785. if (creq->encklen/2 == AES128_KEY_SIZE)
  786. return &cmdlistptr->cipher_aes_128_xts;
  787. return &cmdlistptr->cipher_aes_256_xts;
  788. case QCE_MODE_CCM:
  789. if (creq->encklen == AES128_KEY_SIZE)
  790. return &cmdlistptr->aead_aes_128_ccm;
  791. return &cmdlistptr->aead_aes_256_ccm;
  792. default:
  793. return NULL;
  794. }
  795. }
  796. return NULL;
  797. }
  798. static int _ce_setup_cipher(struct qce_device *pce_dev, struct qce_req *creq,
  799. uint32_t totallen_in, uint32_t coffset,
  800. struct qce_cmdlist_info *cmdlistinfo)
  801. {
  802. uint32_t enckey32[(MAX_CIPHER_KEY_SIZE * 2)/sizeof(uint32_t)] = {
  803. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  804. uint32_t enciv32[MAX_IV_LENGTH / sizeof(uint32_t)] = {
  805. 0, 0, 0, 0};
  806. uint32_t enck_size_in_word = 0;
  807. uint32_t key_size;
  808. bool use_hw_key = false;
  809. bool use_pipe_key = false;
  810. uint32_t encr_cfg = 0;
  811. uint32_t ivsize = creq->ivsize;
  812. int i;
  813. struct sps_command_element *pce = NULL;
  814. bool is_des_cipher = false;
  815. if (creq->mode == QCE_MODE_XTS)
  816. key_size = creq->encklen/2;
  817. else
  818. key_size = creq->encklen;
  819. if (qce_crypto_config(pce_dev, creq->offload_op))
  820. return -EINVAL;
  821. pce = cmdlistinfo->crypto_cfg;
  822. pce->data = pce_dev->reg.crypto_cfg_be;
  823. pce = cmdlistinfo->crypto_cfg_le;
  824. pce->data = pce_dev->reg.crypto_cfg_le;
  825. pce = cmdlistinfo->go_proc;
  826. if ((creq->flags & QCRYPTO_CTX_USE_HW_KEY) == QCRYPTO_CTX_USE_HW_KEY) {
  827. use_hw_key = true;
  828. } else {
  829. if ((creq->flags & QCRYPTO_CTX_USE_PIPE_KEY) ==
  830. QCRYPTO_CTX_USE_PIPE_KEY)
  831. use_pipe_key = true;
  832. }
  833. if (use_hw_key)
  834. pce->addr = (uint32_t)(CRYPTO_GOPROC_QC_KEY_REG +
  835. pce_dev->phy_iobase);
  836. else
  837. pce->addr = (uint32_t)(CRYPTO_GOPROC_REG +
  838. pce_dev->phy_iobase);
  839. if (!use_pipe_key && !use_hw_key) {
  840. _byte_stream_to_net_words(enckey32, creq->enckey, key_size);
  841. enck_size_in_word = key_size/sizeof(uint32_t);
  842. }
  843. if ((creq->op == QCE_REQ_AEAD) && (creq->mode == QCE_MODE_CCM)) {
  844. uint32_t authklen32 = creq->encklen/sizeof(uint32_t);
  845. uint32_t noncelen32 = MAX_NONCE/sizeof(uint32_t);
  846. uint32_t nonce32[MAX_NONCE/sizeof(uint32_t)] = {0, 0, 0, 0};
  847. uint32_t auth_cfg = 0;
  848. /* write nonce */
  849. _byte_stream_to_net_words(nonce32, creq->nonce, MAX_NONCE);
  850. pce = cmdlistinfo->auth_nonce_info;
  851. for (i = 0; i < noncelen32; i++, pce++)
  852. pce->data = nonce32[i];
  853. if (creq->authklen == AES128_KEY_SIZE)
  854. auth_cfg = pce_dev->reg.auth_cfg_aes_ccm_128;
  855. else {
  856. if (creq->authklen == AES256_KEY_SIZE)
  857. auth_cfg = pce_dev->reg.auth_cfg_aes_ccm_256;
  858. }
  859. if (creq->dir == QCE_ENCRYPT)
  860. auth_cfg |= (CRYPTO_AUTH_POS_BEFORE << CRYPTO_AUTH_POS);
  861. else
  862. auth_cfg |= (CRYPTO_AUTH_POS_AFTER << CRYPTO_AUTH_POS);
  863. auth_cfg |= ((creq->authsize - 1) << CRYPTO_AUTH_SIZE);
  864. if (use_hw_key) {
  865. auth_cfg |= (1 << CRYPTO_USE_HW_KEY_AUTH);
  866. } else {
  867. auth_cfg &= ~(1 << CRYPTO_USE_HW_KEY_AUTH);
  868. /* write auth key */
  869. pce = cmdlistinfo->auth_key;
  870. for (i = 0; i < authklen32; i++, pce++)
  871. pce->data = enckey32[i];
  872. }
  873. pce = cmdlistinfo->auth_seg_cfg;
  874. pce->data = auth_cfg;
  875. pce = cmdlistinfo->auth_seg_size;
  876. if (creq->dir == QCE_ENCRYPT)
  877. pce->data = totallen_in;
  878. else
  879. pce->data = totallen_in - creq->authsize;
  880. pce = cmdlistinfo->auth_seg_start;
  881. pce->data = 0;
  882. } else {
  883. if (creq->op != QCE_REQ_AEAD) {
  884. pce = cmdlistinfo->auth_seg_cfg;
  885. pce->data = 0;
  886. }
  887. }
  888. switch (creq->mode) {
  889. case QCE_MODE_ECB:
  890. if (key_size == AES128_KEY_SIZE)
  891. encr_cfg = pce_dev->reg.encr_cfg_aes_ecb_128;
  892. else
  893. encr_cfg = pce_dev->reg.encr_cfg_aes_ecb_256;
  894. break;
  895. case QCE_MODE_CBC:
  896. if (key_size == AES128_KEY_SIZE)
  897. encr_cfg = pce_dev->reg.encr_cfg_aes_cbc_128;
  898. else
  899. encr_cfg = pce_dev->reg.encr_cfg_aes_cbc_256;
  900. break;
  901. case QCE_MODE_XTS:
  902. if (key_size == AES128_KEY_SIZE)
  903. encr_cfg = pce_dev->reg.encr_cfg_aes_xts_128;
  904. else
  905. encr_cfg = pce_dev->reg.encr_cfg_aes_xts_256;
  906. break;
  907. case QCE_MODE_CCM:
  908. if (key_size == AES128_KEY_SIZE)
  909. encr_cfg = pce_dev->reg.encr_cfg_aes_ccm_128;
  910. else
  911. encr_cfg = pce_dev->reg.encr_cfg_aes_ccm_256;
  912. encr_cfg |= (CRYPTO_ENCR_MODE_CCM << CRYPTO_ENCR_MODE) |
  913. (CRYPTO_LAST_CCM_XFR << CRYPTO_LAST_CCM);
  914. break;
  915. case QCE_MODE_CTR:
  916. default:
  917. if (key_size == AES128_KEY_SIZE)
  918. encr_cfg = pce_dev->reg.encr_cfg_aes_ctr_128;
  919. else
  920. encr_cfg = pce_dev->reg.encr_cfg_aes_ctr_256;
  921. break;
  922. }
  923. switch (creq->alg) {
  924. case CIPHER_ALG_DES:
  925. if (creq->mode != QCE_MODE_ECB) {
  926. if (ivsize > MAX_IV_LENGTH) {
  927. pr_err("%s: error: Invalid length parameter\n",
  928. __func__);
  929. return -EINVAL;
  930. }
  931. _byte_stream_to_net_words(enciv32, creq->iv, ivsize);
  932. pce = cmdlistinfo->encr_cntr_iv;
  933. pce->data = enciv32[0];
  934. pce++;
  935. pce->data = enciv32[1];
  936. }
  937. if (!use_hw_key) {
  938. pce = cmdlistinfo->encr_key;
  939. pce->data = enckey32[0];
  940. pce++;
  941. pce->data = enckey32[1];
  942. }
  943. is_des_cipher = true;
  944. break;
  945. case CIPHER_ALG_3DES:
  946. if (creq->mode != QCE_MODE_ECB) {
  947. if (ivsize > MAX_IV_LENGTH) {
  948. pr_err("%s: error: Invalid length parameter\n",
  949. __func__);
  950. return -EINVAL;
  951. }
  952. _byte_stream_to_net_words(enciv32, creq->iv, ivsize);
  953. pce = cmdlistinfo->encr_cntr_iv;
  954. pce->data = enciv32[0];
  955. pce++;
  956. pce->data = enciv32[1];
  957. }
  958. if (!use_hw_key) {
  959. /* write encr key */
  960. pce = cmdlistinfo->encr_key;
  961. for (i = 0; i < 6; i++, pce++)
  962. pce->data = enckey32[i];
  963. }
  964. is_des_cipher = true;
  965. break;
  966. case CIPHER_ALG_AES:
  967. default:
  968. if (creq->mode == QCE_MODE_XTS) {
  969. uint32_t xtskey32[MAX_CIPHER_KEY_SIZE/sizeof(uint32_t)]
  970. = {0, 0, 0, 0, 0, 0, 0, 0};
  971. uint32_t xtsklen =
  972. creq->encklen/(2 * sizeof(uint32_t));
  973. if (!use_hw_key && !use_pipe_key) {
  974. _byte_stream_to_net_words(xtskey32,
  975. (creq->enckey + creq->encklen/2),
  976. creq->encklen/2);
  977. /* write xts encr key */
  978. pce = cmdlistinfo->encr_xts_key;
  979. for (i = 0; i < xtsklen; i++, pce++)
  980. pce->data = xtskey32[i];
  981. }
  982. /* write xts du size */
  983. pce = cmdlistinfo->encr_xts_du_size;
  984. switch (creq->flags & QCRYPTO_CTX_XTS_MASK) {
  985. case QCRYPTO_CTX_XTS_DU_SIZE_512B:
  986. pce->data = min((unsigned int)QCE_SECTOR_SIZE,
  987. creq->cryptlen);
  988. break;
  989. case QCRYPTO_CTX_XTS_DU_SIZE_1KB:
  990. pce->data =
  991. min((unsigned int)QCE_SECTOR_SIZE * 2,
  992. creq->cryptlen);
  993. break;
  994. default:
  995. pce->data = creq->cryptlen;
  996. break;
  997. }
  998. }
  999. if (creq->mode != QCE_MODE_ECB) {
  1000. if (ivsize > MAX_IV_LENGTH) {
  1001. pr_err("%s: error: Invalid length parameter\n",
  1002. __func__);
  1003. return -EINVAL;
  1004. }
  1005. if (creq->mode == QCE_MODE_XTS)
  1006. _byte_stream_swap_to_net_words(enciv32,
  1007. creq->iv, ivsize);
  1008. else
  1009. _byte_stream_to_net_words(enciv32, creq->iv,
  1010. ivsize);
  1011. /* write encr cntr iv */
  1012. pce = cmdlistinfo->encr_cntr_iv;
  1013. for (i = 0; i < 4; i++, pce++)
  1014. pce->data = enciv32[i];
  1015. if (creq->mode == QCE_MODE_CCM) {
  1016. /* write cntr iv for ccm */
  1017. pce = cmdlistinfo->encr_ccm_cntr_iv;
  1018. for (i = 0; i < 4; i++, pce++)
  1019. pce->data = enciv32[i];
  1020. /* update cntr_iv[3] by one */
  1021. pce = cmdlistinfo->encr_cntr_iv;
  1022. pce += 3;
  1023. pce->data += 1;
  1024. }
  1025. }
  1026. if (creq->op == QCE_REQ_ABLK_CIPHER_NO_KEY) {
  1027. encr_cfg |= (CRYPTO_ENCR_KEY_SZ_AES128 <<
  1028. CRYPTO_ENCR_KEY_SZ);
  1029. } else {
  1030. if (!use_hw_key) {
  1031. /* write encr key */
  1032. pce = cmdlistinfo->encr_key;
  1033. for (i = 0; i < enck_size_in_word; i++, pce++)
  1034. pce->data = enckey32[i];
  1035. }
  1036. } /* else of if (creq->op == QCE_REQ_ABLK_CIPHER_NO_KEY) */
  1037. break;
  1038. } /* end of switch (creq->mode) */
  1039. if (use_pipe_key)
  1040. encr_cfg |= (CRYPTO_USE_PIPE_KEY_ENCR_ENABLED
  1041. << CRYPTO_USE_PIPE_KEY_ENCR);
  1042. /* write encr seg cfg */
  1043. pce = cmdlistinfo->encr_seg_cfg;
  1044. if ((creq->alg == CIPHER_ALG_DES) || (creq->alg == CIPHER_ALG_3DES)) {
  1045. if (creq->dir == QCE_ENCRYPT)
  1046. pce->data |= (1 << CRYPTO_ENCODE);
  1047. else
  1048. pce->data &= ~(1 << CRYPTO_ENCODE);
  1049. encr_cfg = pce->data;
  1050. } else {
  1051. encr_cfg |=
  1052. ((creq->dir == QCE_ENCRYPT) ? 1 : 0) << CRYPTO_ENCODE;
  1053. }
  1054. if (use_hw_key)
  1055. encr_cfg |= (CRYPTO_USE_HW_KEY << CRYPTO_USE_HW_KEY_ENCR);
  1056. else
  1057. encr_cfg &= ~(CRYPTO_USE_HW_KEY << CRYPTO_USE_HW_KEY_ENCR);
  1058. pce->data = encr_cfg;
  1059. /* write encr seg size */
  1060. pce = cmdlistinfo->encr_seg_size;
  1061. if (creq->is_copy_op) {
  1062. pce->data = 0;
  1063. } else {
  1064. if ((creq->mode == QCE_MODE_CCM) && (creq->dir == QCE_DECRYPT))
  1065. pce->data = (creq->cryptlen + creq->authsize);
  1066. else
  1067. pce->data = creq->cryptlen;
  1068. }
  1069. /* write encr seg start */
  1070. pce = cmdlistinfo->encr_seg_start;
  1071. pce->data = (coffset & 0xffff);
  1072. /* write seg size */
  1073. pce = cmdlistinfo->seg_size;
  1074. pce->data = totallen_in;
  1075. if (!is_des_cipher) {
  1076. /* pattern info */
  1077. pce = cmdlistinfo->pattern_info;
  1078. pce->data = creq->pattern_info;
  1079. /* block offset */
  1080. pce = cmdlistinfo->block_offset;
  1081. pce->data = (creq->block_offset << 4) |
  1082. (creq->block_offset ? 1: 0);
  1083. /* IV counter size */
  1084. qce_set_iv_ctr_mask(pce_dev, creq);
  1085. pce = cmdlistinfo->encr_mask_3;
  1086. pce->data = pce_dev->reg.encr_cntr_mask_3;
  1087. pce = cmdlistinfo->encr_mask_2;
  1088. pce->data = pce_dev->reg.encr_cntr_mask_2;
  1089. pce = cmdlistinfo->encr_mask_1;
  1090. pce->data = pce_dev->reg.encr_cntr_mask_1;
  1091. pce = cmdlistinfo->encr_mask_0;
  1092. pce->data = pce_dev->reg.encr_cntr_mask_0;
  1093. }
  1094. pce = cmdlistinfo->go_proc;
  1095. pce->data = 0;
  1096. if (is_offload_op(creq->offload_op))
  1097. pce->data = ((1 << CRYPTO_GO) | (1 << CRYPTO_CLR_CNTXT));
  1098. else
  1099. pce->data = ((1 << CRYPTO_GO) | (1 << CRYPTO_CLR_CNTXT) |
  1100. (1 << CRYPTO_RESULTS_DUMP));
  1101. return 0;
  1102. }
  1103. static int _ce_f9_setup(struct qce_device *pce_dev, struct qce_f9_req *req,
  1104. struct qce_cmdlist_info *cmdlistinfo)
  1105. {
  1106. uint32_t ikey32[OTA_KEY_SIZE/sizeof(uint32_t)];
  1107. uint32_t key_size_in_word = OTA_KEY_SIZE/sizeof(uint32_t);
  1108. uint32_t cfg;
  1109. struct sps_command_element *pce;
  1110. int i;
  1111. switch (req->algorithm) {
  1112. case QCE_OTA_ALGO_KASUMI:
  1113. cfg = pce_dev->reg.auth_cfg_kasumi;
  1114. break;
  1115. case QCE_OTA_ALGO_SNOW3G:
  1116. default:
  1117. cfg = pce_dev->reg.auth_cfg_snow3g;
  1118. break;
  1119. }
  1120. if (qce_crypto_config(pce_dev, QCE_OFFLOAD_NONE))
  1121. return -EINVAL;
  1122. pce = cmdlistinfo->crypto_cfg;
  1123. pce->data = pce_dev->reg.crypto_cfg_be;
  1124. pce = cmdlistinfo->crypto_cfg_le;
  1125. pce->data = pce_dev->reg.crypto_cfg_le;
  1126. /* write key in CRYPTO_AUTH_IV0-3_REG */
  1127. _byte_stream_to_net_words(ikey32, &req->ikey[0], OTA_KEY_SIZE);
  1128. pce = cmdlistinfo->auth_iv;
  1129. for (i = 0; i < key_size_in_word; i++, pce++)
  1130. pce->data = ikey32[i];
  1131. /* write last bits in CRYPTO_AUTH_IV4_REG */
  1132. pce->data = req->last_bits;
  1133. /* write fresh to CRYPTO_AUTH_BYTECNT0_REG */
  1134. pce = cmdlistinfo->auth_bytecount;
  1135. pce->data = req->fresh;
  1136. /* write count-i to CRYPTO_AUTH_BYTECNT1_REG */
  1137. pce++;
  1138. pce->data = req->count_i;
  1139. /* write auth seg cfg */
  1140. pce = cmdlistinfo->auth_seg_cfg;
  1141. if (req->direction == QCE_OTA_DIR_DOWNLINK)
  1142. cfg |= BIT(CRYPTO_F9_DIRECTION);
  1143. pce->data = cfg;
  1144. /* write auth seg size */
  1145. pce = cmdlistinfo->auth_seg_size;
  1146. pce->data = req->msize;
  1147. /* write auth seg start*/
  1148. pce = cmdlistinfo->auth_seg_start;
  1149. pce->data = 0;
  1150. /* write seg size */
  1151. pce = cmdlistinfo->seg_size;
  1152. pce->data = req->msize;
  1153. /* write go */
  1154. pce = cmdlistinfo->go_proc;
  1155. pce->addr = (uint32_t)(CRYPTO_GOPROC_REG + pce_dev->phy_iobase);
  1156. return 0;
  1157. }
  1158. static int _ce_f8_setup(struct qce_device *pce_dev, struct qce_f8_req *req,
  1159. bool key_stream_mode, uint16_t npkts, uint16_t cipher_offset,
  1160. uint16_t cipher_size,
  1161. struct qce_cmdlist_info *cmdlistinfo)
  1162. {
  1163. uint32_t ckey32[OTA_KEY_SIZE/sizeof(uint32_t)];
  1164. uint32_t key_size_in_word = OTA_KEY_SIZE/sizeof(uint32_t);
  1165. uint32_t cfg;
  1166. struct sps_command_element *pce;
  1167. int i;
  1168. switch (req->algorithm) {
  1169. case QCE_OTA_ALGO_KASUMI:
  1170. cfg = pce_dev->reg.encr_cfg_kasumi;
  1171. break;
  1172. case QCE_OTA_ALGO_SNOW3G:
  1173. default:
  1174. cfg = pce_dev->reg.encr_cfg_snow3g;
  1175. break;
  1176. }
  1177. if (qce_crypto_config(pce_dev, QCE_OFFLOAD_NONE))
  1178. return -EINVAL;
  1179. pce = cmdlistinfo->crypto_cfg;
  1180. pce->data = pce_dev->reg.crypto_cfg_be;
  1181. pce = cmdlistinfo->crypto_cfg_le;
  1182. pce->data = pce_dev->reg.crypto_cfg_le;
  1183. /* write key */
  1184. _byte_stream_to_net_words(ckey32, &req->ckey[0], OTA_KEY_SIZE);
  1185. pce = cmdlistinfo->encr_key;
  1186. for (i = 0; i < key_size_in_word; i++, pce++)
  1187. pce->data = ckey32[i];
  1188. /* write encr seg cfg */
  1189. pce = cmdlistinfo->encr_seg_cfg;
  1190. if (key_stream_mode)
  1191. cfg |= BIT(CRYPTO_F8_KEYSTREAM_ENABLE);
  1192. if (req->direction == QCE_OTA_DIR_DOWNLINK)
  1193. cfg |= BIT(CRYPTO_F8_DIRECTION);
  1194. pce->data = cfg;
  1195. /* write encr seg start */
  1196. pce = cmdlistinfo->encr_seg_start;
  1197. pce->data = (cipher_offset & 0xffff);
  1198. /* write encr seg size */
  1199. pce = cmdlistinfo->encr_seg_size;
  1200. pce->data = cipher_size;
  1201. /* write seg size */
  1202. pce = cmdlistinfo->seg_size;
  1203. pce->data = req->data_len;
  1204. /* write cntr0_iv0 for countC */
  1205. pce = cmdlistinfo->encr_cntr_iv;
  1206. pce->data = req->count_c;
  1207. /* write cntr1_iv1 for nPkts, and bearer */
  1208. pce++;
  1209. if (npkts == 1)
  1210. npkts = 0;
  1211. pce->data = req->bearer << CRYPTO_CNTR1_IV1_REG_F8_BEARER |
  1212. npkts << CRYPTO_CNTR1_IV1_REG_F8_PKT_CNT;
  1213. /* write go */
  1214. pce = cmdlistinfo->go_proc;
  1215. pce->addr = (uint32_t)(CRYPTO_GOPROC_REG + pce_dev->phy_iobase);
  1216. return 0;
  1217. }
  1218. static void _qce_dump_descr_fifos(struct qce_device *pce_dev, int req_info)
  1219. {
  1220. int i, j, ents;
  1221. struct ce_sps_data *pce_sps_data;
  1222. struct sps_iovec *iovec;
  1223. uint32_t cmd_flags = SPS_IOVEC_FLAG_CMD;
  1224. pce_sps_data = &pce_dev->ce_request_info[req_info].ce_sps;
  1225. iovec = pce_sps_data->in_transfer.iovec;
  1226. pr_info("==============================================\n");
  1227. pr_info("CONSUMER (TX/IN/DEST) PIPE DESCRIPTOR\n");
  1228. pr_info("==============================================\n");
  1229. for (i = 0; i < pce_sps_data->in_transfer.iovec_count; i++) {
  1230. pr_info(" [%d] addr=0x%x size=0x%x flags=0x%x\n", i,
  1231. iovec->addr, iovec->size, iovec->flags);
  1232. if (iovec->flags & cmd_flags) {
  1233. struct sps_command_element *pced;
  1234. pced = (struct sps_command_element *)
  1235. (GET_VIRT_ADDR(iovec->addr));
  1236. ents = iovec->size/(sizeof(struct sps_command_element));
  1237. for (j = 0; j < ents; j++) {
  1238. pr_info(" [%d] [0x%x] 0x%x\n", j,
  1239. pced->addr, pced->data);
  1240. pced++;
  1241. }
  1242. }
  1243. iovec++;
  1244. }
  1245. pr_info("==============================================\n");
  1246. pr_info("PRODUCER (RX/OUT/SRC) PIPE DESCRIPTOR\n");
  1247. pr_info("==============================================\n");
  1248. iovec = pce_sps_data->out_transfer.iovec;
  1249. for (i = 0; i < pce_sps_data->out_transfer.iovec_count; i++) {
  1250. pr_info(" [%d] addr=0x%x size=0x%x flags=0x%x\n", i,
  1251. iovec->addr, iovec->size, iovec->flags);
  1252. iovec++;
  1253. }
  1254. }
  1255. #ifdef QCE_DEBUG
  1256. static void _qce_dump_descr_fifos_dbg(struct qce_device *pce_dev, int req_info)
  1257. {
  1258. _qce_dump_descr_fifos(pce_dev, req_info);
  1259. }
  1260. #define QCE_WRITE_REG(val, addr) \
  1261. { \
  1262. pr_info(" [0x%pK] 0x%x\n", addr, (uint32_t)val); \
  1263. writel_relaxed(val, addr); \
  1264. }
  1265. #else
  1266. static void _qce_dump_descr_fifos_dbg(struct qce_device *pce_dev, int req_info)
  1267. {
  1268. }
  1269. #define QCE_WRITE_REG(val, addr) \
  1270. writel_relaxed(val, addr)
  1271. #endif
  1272. static int _ce_setup_hash_direct(struct qce_device *pce_dev,
  1273. struct qce_sha_req *sreq)
  1274. {
  1275. uint32_t auth32[SHA256_DIGEST_SIZE / sizeof(uint32_t)];
  1276. uint32_t diglen;
  1277. bool use_hw_key = false;
  1278. bool use_pipe_key = false;
  1279. int i;
  1280. uint32_t mackey32[SHA_HMAC_KEY_SIZE/sizeof(uint32_t)] = {
  1281. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  1282. uint32_t authk_size_in_word = sreq->authklen/sizeof(uint32_t);
  1283. bool sha1 = false;
  1284. uint32_t auth_cfg = 0;
  1285. /* clear status */
  1286. QCE_WRITE_REG(0, pce_dev->iobase + CRYPTO_STATUS_REG);
  1287. if (qce_crypto_config(pce_dev, QCE_OFFLOAD_NONE))
  1288. return -EINVAL;
  1289. QCE_WRITE_REG(pce_dev->reg.crypto_cfg_be, (pce_dev->iobase +
  1290. CRYPTO_CONFIG_REG));
  1291. /*
  1292. * Ensure previous instructions (setting the CONFIG register)
  1293. * was completed before issuing starting to set other config register
  1294. * This is to ensure the configurations are done in correct endian-ness
  1295. * as set in the CONFIG registers
  1296. */
  1297. mb();
  1298. if (sreq->alg == QCE_HASH_AES_CMAC) {
  1299. /* write seg_cfg */
  1300. QCE_WRITE_REG(0, pce_dev->iobase + CRYPTO_AUTH_SEG_CFG_REG);
  1301. /* write seg_cfg */
  1302. QCE_WRITE_REG(0, pce_dev->iobase + CRYPTO_ENCR_SEG_CFG_REG);
  1303. /* write seg_cfg */
  1304. QCE_WRITE_REG(0, pce_dev->iobase + CRYPTO_ENCR_SEG_SIZE_REG);
  1305. /* Clear auth_ivn, auth_keyn registers */
  1306. for (i = 0; i < 16; i++) {
  1307. QCE_WRITE_REG(0, (pce_dev->iobase +
  1308. (CRYPTO_AUTH_IV0_REG + i*sizeof(uint32_t))));
  1309. QCE_WRITE_REG(0, (pce_dev->iobase +
  1310. (CRYPTO_AUTH_KEY0_REG + i*sizeof(uint32_t))));
  1311. }
  1312. /* write auth_bytecnt 0/1/2/3, start with 0 */
  1313. for (i = 0; i < 4; i++)
  1314. QCE_WRITE_REG(0, pce_dev->iobase +
  1315. CRYPTO_AUTH_BYTECNT0_REG +
  1316. i * sizeof(uint32_t));
  1317. if (sreq->authklen == AES128_KEY_SIZE)
  1318. auth_cfg = pce_dev->reg.auth_cfg_cmac_128;
  1319. else
  1320. auth_cfg = pce_dev->reg.auth_cfg_cmac_256;
  1321. }
  1322. if ((sreq->alg == QCE_HASH_SHA1_HMAC) ||
  1323. (sreq->alg == QCE_HASH_SHA256_HMAC) ||
  1324. (sreq->alg == QCE_HASH_AES_CMAC)) {
  1325. _byte_stream_to_net_words(mackey32, sreq->authkey,
  1326. sreq->authklen);
  1327. /* no more check for null key. use flag to check*/
  1328. if ((sreq->flags & QCRYPTO_CTX_USE_HW_KEY) ==
  1329. QCRYPTO_CTX_USE_HW_KEY) {
  1330. use_hw_key = true;
  1331. } else if ((sreq->flags & QCRYPTO_CTX_USE_PIPE_KEY) ==
  1332. QCRYPTO_CTX_USE_PIPE_KEY) {
  1333. use_pipe_key = true;
  1334. } else {
  1335. /* setup key */
  1336. for (i = 0; i < authk_size_in_word; i++)
  1337. QCE_WRITE_REG(mackey32[i], (pce_dev->iobase +
  1338. (CRYPTO_AUTH_KEY0_REG +
  1339. i*sizeof(uint32_t))));
  1340. }
  1341. }
  1342. if (sreq->alg == QCE_HASH_AES_CMAC)
  1343. goto go_proc;
  1344. /* if not the last, the size has to be on the block boundary */
  1345. if (!sreq->last_blk && (sreq->size % SHA256_BLOCK_SIZE))
  1346. return -EIO;
  1347. switch (sreq->alg) {
  1348. case QCE_HASH_SHA1:
  1349. auth_cfg = pce_dev->reg.auth_cfg_sha1;
  1350. diglen = SHA1_DIGEST_SIZE;
  1351. sha1 = true;
  1352. break;
  1353. case QCE_HASH_SHA1_HMAC:
  1354. auth_cfg = pce_dev->reg.auth_cfg_hmac_sha1;
  1355. diglen = SHA1_DIGEST_SIZE;
  1356. sha1 = true;
  1357. break;
  1358. case QCE_HASH_SHA256:
  1359. auth_cfg = pce_dev->reg.auth_cfg_sha256;
  1360. diglen = SHA256_DIGEST_SIZE;
  1361. break;
  1362. case QCE_HASH_SHA256_HMAC:
  1363. auth_cfg = pce_dev->reg.auth_cfg_hmac_sha256;
  1364. diglen = SHA256_DIGEST_SIZE;
  1365. break;
  1366. default:
  1367. return -EINVAL;
  1368. }
  1369. /* write 20/32 bytes, 5/8 words into auth_iv for SHA1/SHA256 */
  1370. if (sreq->first_blk) {
  1371. if (sha1) {
  1372. for (i = 0; i < 5; i++)
  1373. auth32[i] = _std_init_vector_sha1[i];
  1374. } else {
  1375. for (i = 0; i < 8; i++)
  1376. auth32[i] = _std_init_vector_sha256[i];
  1377. }
  1378. } else {
  1379. _byte_stream_to_net_words(auth32, sreq->digest, diglen);
  1380. }
  1381. /* Set auth_ivn, auth_keyn registers */
  1382. for (i = 0; i < 5; i++)
  1383. QCE_WRITE_REG(auth32[i], (pce_dev->iobase +
  1384. (CRYPTO_AUTH_IV0_REG + i*sizeof(uint32_t))));
  1385. if ((sreq->alg == QCE_HASH_SHA256) ||
  1386. (sreq->alg == QCE_HASH_SHA256_HMAC)) {
  1387. for (i = 5; i < 8; i++)
  1388. QCE_WRITE_REG(auth32[i], (pce_dev->iobase +
  1389. (CRYPTO_AUTH_IV0_REG + i*sizeof(uint32_t))));
  1390. }
  1391. /* write auth_bytecnt 0/1/2/3, start with 0 */
  1392. for (i = 0; i < 2; i++)
  1393. QCE_WRITE_REG(sreq->auth_data[i], pce_dev->iobase +
  1394. CRYPTO_AUTH_BYTECNT0_REG +
  1395. i * sizeof(uint32_t));
  1396. /* Set/reset last bit in CFG register */
  1397. if (sreq->last_blk)
  1398. auth_cfg |= 1 << CRYPTO_LAST;
  1399. else
  1400. auth_cfg &= ~(1 << CRYPTO_LAST);
  1401. if (sreq->first_blk)
  1402. auth_cfg |= 1 << CRYPTO_FIRST;
  1403. else
  1404. auth_cfg &= ~(1 << CRYPTO_FIRST);
  1405. if (use_hw_key)
  1406. auth_cfg |= 1 << CRYPTO_USE_HW_KEY_AUTH;
  1407. if (use_pipe_key)
  1408. auth_cfg |= 1 << CRYPTO_USE_PIPE_KEY_AUTH;
  1409. go_proc:
  1410. /* write seg_cfg */
  1411. QCE_WRITE_REG(auth_cfg, pce_dev->iobase + CRYPTO_AUTH_SEG_CFG_REG);
  1412. /* write auth seg_size */
  1413. QCE_WRITE_REG(sreq->size, pce_dev->iobase + CRYPTO_AUTH_SEG_SIZE_REG);
  1414. /* write auth_seg_start */
  1415. QCE_WRITE_REG(0, pce_dev->iobase + CRYPTO_AUTH_SEG_START_REG);
  1416. /* reset encr seg_cfg */
  1417. QCE_WRITE_REG(0, pce_dev->iobase + CRYPTO_ENCR_SEG_CFG_REG);
  1418. /* write seg_size */
  1419. QCE_WRITE_REG(sreq->size, pce_dev->iobase + CRYPTO_SEG_SIZE_REG);
  1420. QCE_WRITE_REG(pce_dev->reg.crypto_cfg_le, (pce_dev->iobase +
  1421. CRYPTO_CONFIG_REG));
  1422. /* issue go to crypto */
  1423. if (!use_hw_key) {
  1424. QCE_WRITE_REG(((1 << CRYPTO_GO) | (1 << CRYPTO_RESULTS_DUMP) |
  1425. (1 << CRYPTO_CLR_CNTXT)),
  1426. pce_dev->iobase + CRYPTO_GOPROC_REG);
  1427. } else {
  1428. QCE_WRITE_REG(((1 << CRYPTO_GO) | (1 << CRYPTO_RESULTS_DUMP)),
  1429. pce_dev->iobase + CRYPTO_GOPROC_QC_KEY_REG);
  1430. }
  1431. /*
  1432. * Ensure previous instructions (setting the GO register)
  1433. * was completed before issuing a DMA transfer request
  1434. */
  1435. mb();
  1436. return 0;
  1437. }
  1438. static int _ce_setup_aead_direct(struct qce_device *pce_dev,
  1439. struct qce_req *q_req, uint32_t totallen_in, uint32_t coffset)
  1440. {
  1441. int32_t authk_size_in_word = SHA_HMAC_KEY_SIZE/sizeof(uint32_t);
  1442. int i;
  1443. uint32_t mackey32[SHA_HMAC_KEY_SIZE/sizeof(uint32_t)] = {0};
  1444. uint32_t a_cfg;
  1445. uint32_t enckey32[(MAX_CIPHER_KEY_SIZE*2)/sizeof(uint32_t)] = {0};
  1446. uint32_t enciv32[MAX_IV_LENGTH/sizeof(uint32_t)] = {0};
  1447. uint32_t enck_size_in_word = 0;
  1448. uint32_t enciv_in_word;
  1449. uint32_t key_size;
  1450. uint32_t ivsize = q_req->ivsize;
  1451. uint32_t encr_cfg;
  1452. /* clear status */
  1453. QCE_WRITE_REG(0, pce_dev->iobase + CRYPTO_STATUS_REG);
  1454. if (qce_crypto_config(pce_dev, q_req->offload_op))
  1455. return -EINVAL;
  1456. QCE_WRITE_REG(pce_dev->reg.crypto_cfg_be, (pce_dev->iobase +
  1457. CRYPTO_CONFIG_REG));
  1458. /*
  1459. * Ensure previous instructions (setting the CONFIG register)
  1460. * was completed before issuing starting to set other config register
  1461. * This is to ensure the configurations are done in correct endian-ness
  1462. * as set in the CONFIG registers
  1463. */
  1464. mb();
  1465. key_size = q_req->encklen;
  1466. enck_size_in_word = key_size/sizeof(uint32_t);
  1467. switch (q_req->alg) {
  1468. case CIPHER_ALG_DES:
  1469. switch (q_req->mode) {
  1470. case QCE_MODE_CBC:
  1471. encr_cfg = pce_dev->reg.encr_cfg_des_cbc;
  1472. break;
  1473. default:
  1474. return -EINVAL;
  1475. }
  1476. enciv_in_word = 2;
  1477. break;
  1478. case CIPHER_ALG_3DES:
  1479. switch (q_req->mode) {
  1480. case QCE_MODE_CBC:
  1481. encr_cfg = pce_dev->reg.encr_cfg_3des_cbc;
  1482. break;
  1483. default:
  1484. return -EINVAL;
  1485. }
  1486. enciv_in_word = 2;
  1487. break;
  1488. case CIPHER_ALG_AES:
  1489. switch (q_req->mode) {
  1490. case QCE_MODE_CBC:
  1491. if (key_size == AES128_KEY_SIZE)
  1492. encr_cfg = pce_dev->reg.encr_cfg_aes_cbc_128;
  1493. else if (key_size == AES256_KEY_SIZE)
  1494. encr_cfg = pce_dev->reg.encr_cfg_aes_cbc_256;
  1495. else
  1496. return -EINVAL;
  1497. break;
  1498. default:
  1499. return -EINVAL;
  1500. }
  1501. enciv_in_word = 4;
  1502. break;
  1503. default:
  1504. return -EINVAL;
  1505. }
  1506. /* write CNTR0_IV0_REG */
  1507. if (q_req->mode != QCE_MODE_ECB) {
  1508. _byte_stream_to_net_words(enciv32, q_req->iv, ivsize);
  1509. for (i = 0; i < enciv_in_word; i++)
  1510. QCE_WRITE_REG(enciv32[i], pce_dev->iobase +
  1511. (CRYPTO_CNTR0_IV0_REG + i * sizeof(uint32_t)));
  1512. }
  1513. /*
  1514. * write encr key
  1515. * do not use hw key or pipe key
  1516. */
  1517. _byte_stream_to_net_words(enckey32, q_req->enckey, key_size);
  1518. for (i = 0; i < enck_size_in_word; i++)
  1519. QCE_WRITE_REG(enckey32[i], pce_dev->iobase +
  1520. (CRYPTO_ENCR_KEY0_REG + i * sizeof(uint32_t)));
  1521. /* write encr seg cfg */
  1522. if (q_req->dir == QCE_ENCRYPT)
  1523. encr_cfg |= (1 << CRYPTO_ENCODE);
  1524. QCE_WRITE_REG(encr_cfg, pce_dev->iobase + CRYPTO_ENCR_SEG_CFG_REG);
  1525. /* we only support sha1-hmac and sha256-hmac at this point */
  1526. _byte_stream_to_net_words(mackey32, q_req->authkey,
  1527. q_req->authklen);
  1528. for (i = 0; i < authk_size_in_word; i++)
  1529. QCE_WRITE_REG(mackey32[i], pce_dev->iobase +
  1530. (CRYPTO_AUTH_KEY0_REG + i * sizeof(uint32_t)));
  1531. if (q_req->auth_alg == QCE_HASH_SHA1_HMAC) {
  1532. for (i = 0; i < 5; i++)
  1533. QCE_WRITE_REG(_std_init_vector_sha1[i],
  1534. pce_dev->iobase +
  1535. (CRYPTO_AUTH_IV0_REG + i * sizeof(uint32_t)));
  1536. } else {
  1537. for (i = 0; i < 8; i++)
  1538. QCE_WRITE_REG(_std_init_vector_sha256[i],
  1539. pce_dev->iobase +
  1540. (CRYPTO_AUTH_IV0_REG + i * sizeof(uint32_t)));
  1541. }
  1542. /* write auth_bytecnt 0/1, start with 0 */
  1543. QCE_WRITE_REG(0, pce_dev->iobase + CRYPTO_AUTH_BYTECNT0_REG);
  1544. QCE_WRITE_REG(0, pce_dev->iobase + CRYPTO_AUTH_BYTECNT1_REG);
  1545. /* write encr seg size */
  1546. QCE_WRITE_REG(q_req->cryptlen, pce_dev->iobase +
  1547. CRYPTO_ENCR_SEG_SIZE_REG);
  1548. /* write encr start */
  1549. QCE_WRITE_REG(coffset & 0xffff, pce_dev->iobase +
  1550. CRYPTO_ENCR_SEG_START_REG);
  1551. if (q_req->auth_alg == QCE_HASH_SHA1_HMAC)
  1552. a_cfg = pce_dev->reg.auth_cfg_aead_sha1_hmac;
  1553. else
  1554. a_cfg = pce_dev->reg.auth_cfg_aead_sha256_hmac;
  1555. if (q_req->dir == QCE_ENCRYPT)
  1556. a_cfg |= (CRYPTO_AUTH_POS_AFTER << CRYPTO_AUTH_POS);
  1557. else
  1558. a_cfg |= (CRYPTO_AUTH_POS_BEFORE << CRYPTO_AUTH_POS);
  1559. /* write auth seg_cfg */
  1560. QCE_WRITE_REG(a_cfg, pce_dev->iobase + CRYPTO_AUTH_SEG_CFG_REG);
  1561. /* write auth seg_size */
  1562. QCE_WRITE_REG(totallen_in, pce_dev->iobase + CRYPTO_AUTH_SEG_SIZE_REG);
  1563. /* write auth_seg_start */
  1564. QCE_WRITE_REG(0, pce_dev->iobase + CRYPTO_AUTH_SEG_START_REG);
  1565. /* write seg_size */
  1566. QCE_WRITE_REG(totallen_in, pce_dev->iobase + CRYPTO_SEG_SIZE_REG);
  1567. QCE_WRITE_REG(pce_dev->reg.crypto_cfg_le, (pce_dev->iobase +
  1568. CRYPTO_CONFIG_REG));
  1569. /* issue go to crypto */
  1570. QCE_WRITE_REG(((1 << CRYPTO_GO) | (1 << CRYPTO_RESULTS_DUMP) |
  1571. (1 << CRYPTO_CLR_CNTXT)),
  1572. pce_dev->iobase + CRYPTO_GOPROC_REG);
  1573. /*
  1574. * Ensure previous instructions (setting the GO register)
  1575. * was completed before issuing a DMA transfer request
  1576. */
  1577. mb();
  1578. return 0;
  1579. }
  1580. static int _ce_setup_cipher_direct(struct qce_device *pce_dev,
  1581. struct qce_req *creq, uint32_t totallen_in, uint32_t coffset)
  1582. {
  1583. uint32_t enckey32[(MAX_CIPHER_KEY_SIZE * 2)/sizeof(uint32_t)] = {
  1584. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  1585. uint32_t enciv32[MAX_IV_LENGTH / sizeof(uint32_t)] = {
  1586. 0, 0, 0, 0};
  1587. uint32_t enck_size_in_word = 0;
  1588. uint32_t key_size;
  1589. bool use_hw_key = false;
  1590. bool use_pipe_key = false;
  1591. uint32_t encr_cfg = 0;
  1592. uint32_t ivsize = creq->ivsize;
  1593. int i;
  1594. /* clear status */
  1595. QCE_WRITE_REG(0, pce_dev->iobase + CRYPTO_STATUS_REG);
  1596. if (qce_crypto_config(pce_dev, creq->offload_op))
  1597. return -EINVAL;
  1598. QCE_WRITE_REG(pce_dev->reg.crypto_cfg_be,
  1599. (pce_dev->iobase + CRYPTO_CONFIG_REG));
  1600. QCE_WRITE_REG(pce_dev->reg.crypto_cfg_le,
  1601. (pce_dev->iobase + CRYPTO_CONFIG_REG));
  1602. /*
  1603. * Ensure previous instructions (setting the CONFIG register)
  1604. * was completed before issuing starting to set other config register
  1605. * This is to ensure the configurations are done in correct endian-ness
  1606. * as set in the CONFIG registers
  1607. */
  1608. mb();
  1609. if (creq->mode == QCE_MODE_XTS)
  1610. key_size = creq->encklen/2;
  1611. else
  1612. key_size = creq->encklen;
  1613. if ((creq->flags & QCRYPTO_CTX_USE_HW_KEY) == QCRYPTO_CTX_USE_HW_KEY) {
  1614. use_hw_key = true;
  1615. } else {
  1616. if ((creq->flags & QCRYPTO_CTX_USE_PIPE_KEY) ==
  1617. QCRYPTO_CTX_USE_PIPE_KEY)
  1618. use_pipe_key = true;
  1619. }
  1620. if (!use_pipe_key && !use_hw_key) {
  1621. _byte_stream_to_net_words(enckey32, creq->enckey, key_size);
  1622. enck_size_in_word = key_size/sizeof(uint32_t);
  1623. }
  1624. if ((creq->op == QCE_REQ_AEAD) && (creq->mode == QCE_MODE_CCM)) {
  1625. uint32_t authklen32 = creq->encklen/sizeof(uint32_t);
  1626. uint32_t noncelen32 = MAX_NONCE/sizeof(uint32_t);
  1627. uint32_t nonce32[MAX_NONCE/sizeof(uint32_t)] = {0, 0, 0, 0};
  1628. uint32_t auth_cfg = 0;
  1629. /* Clear auth_ivn, auth_keyn registers */
  1630. for (i = 0; i < 16; i++) {
  1631. QCE_WRITE_REG(0, (pce_dev->iobase +
  1632. (CRYPTO_AUTH_IV0_REG + i*sizeof(uint32_t))));
  1633. QCE_WRITE_REG(0, (pce_dev->iobase +
  1634. (CRYPTO_AUTH_KEY0_REG + i*sizeof(uint32_t))));
  1635. }
  1636. /* write auth_bytecnt 0/1/2/3, start with 0 */
  1637. for (i = 0; i < 4; i++)
  1638. QCE_WRITE_REG(0, pce_dev->iobase +
  1639. CRYPTO_AUTH_BYTECNT0_REG +
  1640. i * sizeof(uint32_t));
  1641. /* write nonce */
  1642. _byte_stream_to_net_words(nonce32, creq->nonce, MAX_NONCE);
  1643. for (i = 0; i < noncelen32; i++)
  1644. QCE_WRITE_REG(nonce32[i], pce_dev->iobase +
  1645. CRYPTO_AUTH_INFO_NONCE0_REG +
  1646. (i*sizeof(uint32_t)));
  1647. if (creq->authklen == AES128_KEY_SIZE)
  1648. auth_cfg = pce_dev->reg.auth_cfg_aes_ccm_128;
  1649. else {
  1650. if (creq->authklen == AES256_KEY_SIZE)
  1651. auth_cfg = pce_dev->reg.auth_cfg_aes_ccm_256;
  1652. }
  1653. if (creq->dir == QCE_ENCRYPT)
  1654. auth_cfg |= (CRYPTO_AUTH_POS_BEFORE << CRYPTO_AUTH_POS);
  1655. else
  1656. auth_cfg |= (CRYPTO_AUTH_POS_AFTER << CRYPTO_AUTH_POS);
  1657. auth_cfg |= ((creq->authsize - 1) << CRYPTO_AUTH_SIZE);
  1658. if (use_hw_key) {
  1659. auth_cfg |= (1 << CRYPTO_USE_HW_KEY_AUTH);
  1660. } else {
  1661. auth_cfg &= ~(1 << CRYPTO_USE_HW_KEY_AUTH);
  1662. /* write auth key */
  1663. for (i = 0; i < authklen32; i++)
  1664. QCE_WRITE_REG(enckey32[i], pce_dev->iobase +
  1665. CRYPTO_AUTH_KEY0_REG + (i*sizeof(uint32_t)));
  1666. }
  1667. QCE_WRITE_REG(auth_cfg, pce_dev->iobase +
  1668. CRYPTO_AUTH_SEG_CFG_REG);
  1669. if (creq->dir == QCE_ENCRYPT) {
  1670. QCE_WRITE_REG(totallen_in, pce_dev->iobase +
  1671. CRYPTO_AUTH_SEG_SIZE_REG);
  1672. } else {
  1673. QCE_WRITE_REG((totallen_in - creq->authsize),
  1674. pce_dev->iobase + CRYPTO_AUTH_SEG_SIZE_REG);
  1675. }
  1676. QCE_WRITE_REG(0, pce_dev->iobase + CRYPTO_AUTH_SEG_START_REG);
  1677. } else {
  1678. if (creq->op != QCE_REQ_AEAD)
  1679. QCE_WRITE_REG(0, pce_dev->iobase +
  1680. CRYPTO_AUTH_SEG_CFG_REG);
  1681. }
  1682. /*
  1683. * Ensure previous instructions (write to all AUTH registers)
  1684. * was completed before accessing a register that is not in
  1685. * in the same 1K range.
  1686. */
  1687. mb();
  1688. switch (creq->mode) {
  1689. case QCE_MODE_ECB:
  1690. if (key_size == AES128_KEY_SIZE)
  1691. encr_cfg = pce_dev->reg.encr_cfg_aes_ecb_128;
  1692. else
  1693. encr_cfg = pce_dev->reg.encr_cfg_aes_ecb_256;
  1694. break;
  1695. case QCE_MODE_CBC:
  1696. if (key_size == AES128_KEY_SIZE)
  1697. encr_cfg = pce_dev->reg.encr_cfg_aes_cbc_128;
  1698. else
  1699. encr_cfg = pce_dev->reg.encr_cfg_aes_cbc_256;
  1700. break;
  1701. case QCE_MODE_XTS:
  1702. if (key_size == AES128_KEY_SIZE)
  1703. encr_cfg = pce_dev->reg.encr_cfg_aes_xts_128;
  1704. else
  1705. encr_cfg = pce_dev->reg.encr_cfg_aes_xts_256;
  1706. break;
  1707. case QCE_MODE_CCM:
  1708. if (key_size == AES128_KEY_SIZE)
  1709. encr_cfg = pce_dev->reg.encr_cfg_aes_ccm_128;
  1710. else
  1711. encr_cfg = pce_dev->reg.encr_cfg_aes_ccm_256;
  1712. break;
  1713. case QCE_MODE_CTR:
  1714. default:
  1715. if (key_size == AES128_KEY_SIZE)
  1716. encr_cfg = pce_dev->reg.encr_cfg_aes_ctr_128;
  1717. else
  1718. encr_cfg = pce_dev->reg.encr_cfg_aes_ctr_256;
  1719. break;
  1720. }
  1721. switch (creq->alg) {
  1722. case CIPHER_ALG_DES:
  1723. if (creq->mode != QCE_MODE_ECB) {
  1724. encr_cfg = pce_dev->reg.encr_cfg_des_cbc;
  1725. _byte_stream_to_net_words(enciv32, creq->iv, ivsize);
  1726. QCE_WRITE_REG(enciv32[0], pce_dev->iobase +
  1727. CRYPTO_CNTR0_IV0_REG);
  1728. QCE_WRITE_REG(enciv32[1], pce_dev->iobase +
  1729. CRYPTO_CNTR1_IV1_REG);
  1730. } else {
  1731. encr_cfg = pce_dev->reg.encr_cfg_des_ecb;
  1732. }
  1733. if (!use_hw_key) {
  1734. QCE_WRITE_REG(enckey32[0], pce_dev->iobase +
  1735. CRYPTO_ENCR_KEY0_REG);
  1736. QCE_WRITE_REG(enckey32[1], pce_dev->iobase +
  1737. CRYPTO_ENCR_KEY1_REG);
  1738. }
  1739. break;
  1740. case CIPHER_ALG_3DES:
  1741. if (creq->mode != QCE_MODE_ECB) {
  1742. _byte_stream_to_net_words(enciv32, creq->iv, ivsize);
  1743. QCE_WRITE_REG(enciv32[0], pce_dev->iobase +
  1744. CRYPTO_CNTR0_IV0_REG);
  1745. QCE_WRITE_REG(enciv32[1], pce_dev->iobase +
  1746. CRYPTO_CNTR1_IV1_REG);
  1747. encr_cfg = pce_dev->reg.encr_cfg_3des_cbc;
  1748. } else {
  1749. encr_cfg = pce_dev->reg.encr_cfg_3des_ecb;
  1750. }
  1751. if (!use_hw_key) {
  1752. /* write encr key */
  1753. for (i = 0; i < 6; i++)
  1754. QCE_WRITE_REG(enckey32[0], (pce_dev->iobase +
  1755. (CRYPTO_ENCR_KEY0_REG + i * sizeof(uint32_t))));
  1756. }
  1757. break;
  1758. case CIPHER_ALG_AES:
  1759. default:
  1760. if (creq->mode == QCE_MODE_XTS) {
  1761. uint32_t xtskey32[MAX_CIPHER_KEY_SIZE/sizeof(uint32_t)]
  1762. = {0, 0, 0, 0, 0, 0, 0, 0};
  1763. uint32_t xtsklen =
  1764. creq->encklen/(2 * sizeof(uint32_t));
  1765. if (!use_hw_key && !use_pipe_key) {
  1766. _byte_stream_to_net_words(xtskey32,
  1767. (creq->enckey + creq->encklen/2),
  1768. creq->encklen/2);
  1769. /* write xts encr key */
  1770. for (i = 0; i < xtsklen; i++)
  1771. QCE_WRITE_REG(xtskey32[i],
  1772. pce_dev->iobase +
  1773. CRYPTO_ENCR_XTS_KEY0_REG +
  1774. (i * sizeof(uint32_t)));
  1775. }
  1776. /* write xts du size */
  1777. switch (creq->flags & QCRYPTO_CTX_XTS_MASK) {
  1778. case QCRYPTO_CTX_XTS_DU_SIZE_512B:
  1779. QCE_WRITE_REG(
  1780. min((uint32_t)QCE_SECTOR_SIZE,
  1781. creq->cryptlen), pce_dev->iobase +
  1782. CRYPTO_ENCR_XTS_DU_SIZE_REG);
  1783. break;
  1784. case QCRYPTO_CTX_XTS_DU_SIZE_1KB:
  1785. QCE_WRITE_REG(
  1786. min((uint32_t)(QCE_SECTOR_SIZE * 2),
  1787. creq->cryptlen), pce_dev->iobase +
  1788. CRYPTO_ENCR_XTS_DU_SIZE_REG);
  1789. break;
  1790. default:
  1791. QCE_WRITE_REG(creq->cryptlen,
  1792. pce_dev->iobase +
  1793. CRYPTO_ENCR_XTS_DU_SIZE_REG);
  1794. break;
  1795. }
  1796. }
  1797. if (creq->mode != QCE_MODE_ECB) {
  1798. if (creq->mode == QCE_MODE_XTS)
  1799. _byte_stream_swap_to_net_words(enciv32,
  1800. creq->iv, ivsize);
  1801. else
  1802. _byte_stream_to_net_words(enciv32, creq->iv,
  1803. ivsize);
  1804. /* write encr cntr iv */
  1805. for (i = 0; i <= 3; i++)
  1806. QCE_WRITE_REG(enciv32[i], pce_dev->iobase +
  1807. CRYPTO_CNTR0_IV0_REG +
  1808. (i * sizeof(uint32_t)));
  1809. if (creq->mode == QCE_MODE_CCM) {
  1810. /* write cntr iv for ccm */
  1811. for (i = 0; i <= 3; i++)
  1812. QCE_WRITE_REG(enciv32[i],
  1813. pce_dev->iobase +
  1814. CRYPTO_ENCR_CCM_INT_CNTR0_REG +
  1815. (i * sizeof(uint32_t)));
  1816. /* update cntr_iv[3] by one */
  1817. QCE_WRITE_REG((enciv32[3] + 1),
  1818. pce_dev->iobase +
  1819. CRYPTO_CNTR0_IV0_REG +
  1820. (3 * sizeof(uint32_t)));
  1821. }
  1822. }
  1823. if (creq->op == QCE_REQ_ABLK_CIPHER_NO_KEY) {
  1824. encr_cfg |= (CRYPTO_ENCR_KEY_SZ_AES128 <<
  1825. CRYPTO_ENCR_KEY_SZ);
  1826. } else {
  1827. if (!use_hw_key && !use_pipe_key) {
  1828. for (i = 0; i < enck_size_in_word; i++)
  1829. QCE_WRITE_REG(enckey32[i],
  1830. pce_dev->iobase +
  1831. CRYPTO_ENCR_KEY0_REG +
  1832. (i * sizeof(uint32_t)));
  1833. }
  1834. } /* else of if (creq->op == QCE_REQ_ABLK_CIPHER_NO_KEY) */
  1835. break;
  1836. } /* end of switch (creq->mode) */
  1837. if (use_pipe_key)
  1838. encr_cfg |= (CRYPTO_USE_PIPE_KEY_ENCR_ENABLED
  1839. << CRYPTO_USE_PIPE_KEY_ENCR);
  1840. /* write encr seg cfg */
  1841. encr_cfg |= ((creq->dir == QCE_ENCRYPT) ? 1 : 0) << CRYPTO_ENCODE;
  1842. if (use_hw_key)
  1843. encr_cfg |= (CRYPTO_USE_HW_KEY << CRYPTO_USE_HW_KEY_ENCR);
  1844. else
  1845. encr_cfg &= ~(CRYPTO_USE_HW_KEY << CRYPTO_USE_HW_KEY_ENCR);
  1846. /* write encr seg cfg */
  1847. QCE_WRITE_REG(encr_cfg, pce_dev->iobase + CRYPTO_ENCR_SEG_CFG_REG);
  1848. /* write encr seg size */
  1849. if ((creq->mode == QCE_MODE_CCM) && (creq->dir == QCE_DECRYPT)) {
  1850. QCE_WRITE_REG((creq->cryptlen + creq->authsize),
  1851. pce_dev->iobase + CRYPTO_ENCR_SEG_SIZE_REG);
  1852. } else {
  1853. QCE_WRITE_REG(creq->cryptlen,
  1854. pce_dev->iobase + CRYPTO_ENCR_SEG_SIZE_REG);
  1855. }
  1856. /* write pattern */
  1857. if (creq->is_pattern_valid)
  1858. QCE_WRITE_REG(creq->pattern_info, pce_dev->iobase +
  1859. CRYPTO_DATA_PATT_PROC_CFG_REG);
  1860. /* write block offset to CRYPTO_DATA_PARTIAL_BLOCK_PROC_CFG? */
  1861. QCE_WRITE_REG(((creq->block_offset << 4) |
  1862. (creq->block_offset ? 1 : 0)),
  1863. pce_dev->iobase + CRYPTO_DATA_PARTIAL_BLOCK_PROC_CFG_REG);
  1864. /* write encr seg start */
  1865. QCE_WRITE_REG((coffset & 0xffff),
  1866. pce_dev->iobase + CRYPTO_ENCR_SEG_START_REG);
  1867. /* write encr counter mask */
  1868. qce_set_iv_ctr_mask(pce_dev, creq);
  1869. QCE_WRITE_REG(pce_dev->reg.encr_cntr_mask_3,
  1870. pce_dev->iobase + CRYPTO_CNTR_MASK_REG);
  1871. QCE_WRITE_REG(pce_dev->reg.encr_cntr_mask_2,
  1872. pce_dev->iobase + CRYPTO_CNTR_MASK_REG2);
  1873. QCE_WRITE_REG(pce_dev->reg.encr_cntr_mask_1,
  1874. pce_dev->iobase + CRYPTO_CNTR_MASK_REG1);
  1875. QCE_WRITE_REG(pce_dev->reg.encr_cntr_mask_0,
  1876. pce_dev->iobase + CRYPTO_CNTR_MASK_REG0);
  1877. /* write seg size */
  1878. QCE_WRITE_REG(totallen_in, pce_dev->iobase + CRYPTO_SEG_SIZE_REG);
  1879. /* issue go to crypto */
  1880. if (!use_hw_key) {
  1881. QCE_WRITE_REG(((1 << CRYPTO_GO) | (1 << CRYPTO_RESULTS_DUMP) |
  1882. (1 << CRYPTO_CLR_CNTXT)),
  1883. pce_dev->iobase + CRYPTO_GOPROC_REG);
  1884. } else {
  1885. QCE_WRITE_REG(((1 << CRYPTO_GO) | (1 << CRYPTO_RESULTS_DUMP)),
  1886. pce_dev->iobase + CRYPTO_GOPROC_QC_KEY_REG);
  1887. }
  1888. /*
  1889. * Ensure previous instructions (setting the GO register)
  1890. * was completed before issuing a DMA transfer request
  1891. */
  1892. mb();
  1893. return 0;
  1894. }
  1895. static int _ce_f9_setup_direct(struct qce_device *pce_dev,
  1896. struct qce_f9_req *req)
  1897. {
  1898. uint32_t ikey32[OTA_KEY_SIZE/sizeof(uint32_t)];
  1899. uint32_t key_size_in_word = OTA_KEY_SIZE/sizeof(uint32_t);
  1900. uint32_t auth_cfg;
  1901. int i;
  1902. switch (req->algorithm) {
  1903. case QCE_OTA_ALGO_KASUMI:
  1904. auth_cfg = pce_dev->reg.auth_cfg_kasumi;
  1905. break;
  1906. case QCE_OTA_ALGO_SNOW3G:
  1907. default:
  1908. auth_cfg = pce_dev->reg.auth_cfg_snow3g;
  1909. break;
  1910. }
  1911. if (qce_crypto_config(pce_dev, QCE_OFFLOAD_NONE))
  1912. return -EINVAL;
  1913. /* clear status */
  1914. QCE_WRITE_REG(0, pce_dev->iobase + CRYPTO_STATUS_REG);
  1915. /* set big endian configuration */
  1916. QCE_WRITE_REG(pce_dev->reg.crypto_cfg_be, (pce_dev->iobase +
  1917. CRYPTO_CONFIG_REG));
  1918. /*
  1919. * Ensure previous instructions (setting the CONFIG register)
  1920. * was completed before issuing starting to set other config register
  1921. * This is to ensure the configurations are done in correct endian-ness
  1922. * as set in the CONFIG registers
  1923. */
  1924. mb();
  1925. /* write enc_seg_cfg */
  1926. QCE_WRITE_REG(0, pce_dev->iobase + CRYPTO_ENCR_SEG_CFG_REG);
  1927. /* write ecn_seg_size */
  1928. QCE_WRITE_REG(0, pce_dev->iobase + CRYPTO_ENCR_SEG_SIZE_REG);
  1929. /* write key in CRYPTO_AUTH_IV0-3_REG */
  1930. _byte_stream_to_net_words(ikey32, &req->ikey[0], OTA_KEY_SIZE);
  1931. for (i = 0; i < key_size_in_word; i++)
  1932. QCE_WRITE_REG(ikey32[i], (pce_dev->iobase +
  1933. (CRYPTO_AUTH_IV0_REG + i*sizeof(uint32_t))));
  1934. /* write last bits in CRYPTO_AUTH_IV4_REG */
  1935. QCE_WRITE_REG(req->last_bits, (pce_dev->iobase +
  1936. CRYPTO_AUTH_IV4_REG));
  1937. /* write fresh to CRYPTO_AUTH_BYTECNT0_REG */
  1938. QCE_WRITE_REG(req->fresh, (pce_dev->iobase +
  1939. CRYPTO_AUTH_BYTECNT0_REG));
  1940. /* write count-i to CRYPTO_AUTH_BYTECNT1_REG */
  1941. QCE_WRITE_REG(req->count_i, (pce_dev->iobase +
  1942. CRYPTO_AUTH_BYTECNT1_REG));
  1943. /* write auth seg cfg */
  1944. if (req->direction == QCE_OTA_DIR_DOWNLINK)
  1945. auth_cfg |= BIT(CRYPTO_F9_DIRECTION);
  1946. QCE_WRITE_REG(auth_cfg, pce_dev->iobase + CRYPTO_AUTH_SEG_CFG_REG);
  1947. /* write auth seg size */
  1948. QCE_WRITE_REG(req->msize, pce_dev->iobase + CRYPTO_AUTH_SEG_SIZE_REG);
  1949. /* write auth seg start*/
  1950. QCE_WRITE_REG(0, pce_dev->iobase + CRYPTO_AUTH_SEG_START_REG);
  1951. /* write seg size */
  1952. QCE_WRITE_REG(req->msize, pce_dev->iobase + CRYPTO_SEG_SIZE_REG);
  1953. /* set little endian configuration before go*/
  1954. QCE_WRITE_REG(pce_dev->reg.crypto_cfg_le, (pce_dev->iobase +
  1955. CRYPTO_CONFIG_REG));
  1956. /* write go */
  1957. QCE_WRITE_REG(((1 << CRYPTO_GO) | (1 << CRYPTO_RESULTS_DUMP) |
  1958. (1 << CRYPTO_CLR_CNTXT)),
  1959. pce_dev->iobase + CRYPTO_GOPROC_REG);
  1960. /*
  1961. * Ensure previous instructions (setting the GO register)
  1962. * was completed before issuing a DMA transfer request
  1963. */
  1964. mb();
  1965. return 0;
  1966. }
  1967. static int _ce_f8_setup_direct(struct qce_device *pce_dev,
  1968. struct qce_f8_req *req, bool key_stream_mode,
  1969. uint16_t npkts, uint16_t cipher_offset, uint16_t cipher_size)
  1970. {
  1971. int i = 0;
  1972. uint32_t encr_cfg = 0;
  1973. uint32_t ckey32[OTA_KEY_SIZE/sizeof(uint32_t)];
  1974. uint32_t key_size_in_word = OTA_KEY_SIZE/sizeof(uint32_t);
  1975. switch (req->algorithm) {
  1976. case QCE_OTA_ALGO_KASUMI:
  1977. encr_cfg = pce_dev->reg.encr_cfg_kasumi;
  1978. break;
  1979. case QCE_OTA_ALGO_SNOW3G:
  1980. default:
  1981. encr_cfg = pce_dev->reg.encr_cfg_snow3g;
  1982. break;
  1983. }
  1984. /* clear status */
  1985. QCE_WRITE_REG(0, pce_dev->iobase + CRYPTO_STATUS_REG);
  1986. /* set big endian configuration */
  1987. if (qce_crypto_config(pce_dev, QCE_OFFLOAD_NONE))
  1988. return -EINVAL;
  1989. QCE_WRITE_REG(pce_dev->reg.crypto_cfg_be, (pce_dev->iobase +
  1990. CRYPTO_CONFIG_REG));
  1991. /* write auth seg configuration */
  1992. QCE_WRITE_REG(0, pce_dev->iobase + CRYPTO_AUTH_SEG_CFG_REG);
  1993. /* write auth seg size */
  1994. QCE_WRITE_REG(0, pce_dev->iobase + CRYPTO_AUTH_SEG_SIZE_REG);
  1995. /* write key */
  1996. _byte_stream_to_net_words(ckey32, &req->ckey[0], OTA_KEY_SIZE);
  1997. for (i = 0; i < key_size_in_word; i++)
  1998. QCE_WRITE_REG(ckey32[i], (pce_dev->iobase +
  1999. (CRYPTO_ENCR_KEY0_REG + i*sizeof(uint32_t))));
  2000. /* write encr seg cfg */
  2001. if (key_stream_mode)
  2002. encr_cfg |= BIT(CRYPTO_F8_KEYSTREAM_ENABLE);
  2003. if (req->direction == QCE_OTA_DIR_DOWNLINK)
  2004. encr_cfg |= BIT(CRYPTO_F8_DIRECTION);
  2005. QCE_WRITE_REG(encr_cfg, pce_dev->iobase +
  2006. CRYPTO_ENCR_SEG_CFG_REG);
  2007. /* write encr seg start */
  2008. QCE_WRITE_REG((cipher_offset & 0xffff), pce_dev->iobase +
  2009. CRYPTO_ENCR_SEG_START_REG);
  2010. /* write encr seg size */
  2011. QCE_WRITE_REG(cipher_size, pce_dev->iobase +
  2012. CRYPTO_ENCR_SEG_SIZE_REG);
  2013. /* write seg size */
  2014. QCE_WRITE_REG(req->data_len, pce_dev->iobase +
  2015. CRYPTO_SEG_SIZE_REG);
  2016. /* write cntr0_iv0 for countC */
  2017. QCE_WRITE_REG(req->count_c, pce_dev->iobase +
  2018. CRYPTO_CNTR0_IV0_REG);
  2019. /* write cntr1_iv1 for nPkts, and bearer */
  2020. if (npkts == 1)
  2021. npkts = 0;
  2022. QCE_WRITE_REG(req->bearer << CRYPTO_CNTR1_IV1_REG_F8_BEARER |
  2023. npkts << CRYPTO_CNTR1_IV1_REG_F8_PKT_CNT,
  2024. pce_dev->iobase + CRYPTO_CNTR1_IV1_REG);
  2025. /* set little endian configuration before go*/
  2026. QCE_WRITE_REG(pce_dev->reg.crypto_cfg_le, (pce_dev->iobase +
  2027. CRYPTO_CONFIG_REG));
  2028. /* write go */
  2029. QCE_WRITE_REG(((1 << CRYPTO_GO) | (1 << CRYPTO_RESULTS_DUMP) |
  2030. (1 << CRYPTO_CLR_CNTXT)),
  2031. pce_dev->iobase + CRYPTO_GOPROC_REG);
  2032. /*
  2033. * Ensure previous instructions (setting the GO register)
  2034. * was completed before issuing a DMA transfer request
  2035. */
  2036. mb();
  2037. return 0;
  2038. }
  2039. static int _qce_unlock_other_pipes(struct qce_device *pce_dev, int req_info)
  2040. {
  2041. int rc = 0;
  2042. struct ce_sps_data *pce_sps_data = &pce_dev->ce_request_info
  2043. [req_info].ce_sps;
  2044. uint16_t op = pce_dev->ce_request_info[req_info].offload_op;
  2045. if (pce_dev->no_get_around || !pce_dev->support_cmd_dscr)
  2046. return rc;
  2047. rc = sps_transfer_one(pce_dev->ce_bam_info.consumer[op].pipe,
  2048. GET_PHYS_ADDR(
  2049. pce_sps_data->cmdlistptr.unlock_all_pipes.cmdlist),
  2050. 0, NULL, (SPS_IOVEC_FLAG_CMD | SPS_IOVEC_FLAG_UNLOCK));
  2051. if (rc) {
  2052. pr_err("sps_xfr_one() fail rc=%d\n", rc);
  2053. rc = -EINVAL;
  2054. }
  2055. return rc;
  2056. }
  2057. static inline void qce_free_req_info(struct qce_device *pce_dev, int req_info,
  2058. bool is_complete);
  2059. static int qce_sps_pipe_reset(struct qce_device *pce_dev, int op)
  2060. {
  2061. int rc = -1;
  2062. struct sps_pipe *sps_pipe_info = NULL;
  2063. struct sps_connect *sps_connect_info = NULL;
  2064. /* Reset both the pipe sets in the pipe group */
  2065. sps_pipe_reset(pce_dev->ce_bam_info.bam_handle,
  2066. pce_dev->ce_bam_info.dest_pipe_index[op]);
  2067. sps_pipe_reset(pce_dev->ce_bam_info.bam_handle,
  2068. pce_dev->ce_bam_info.src_pipe_index[op]);
  2069. /* Reconnect to consumer pipe */
  2070. sps_pipe_info = pce_dev->ce_bam_info.consumer[op].pipe;
  2071. sps_connect_info = &pce_dev->ce_bam_info.consumer[op].connect;
  2072. rc = sps_disconnect(sps_pipe_info);
  2073. if (rc) {
  2074. pr_err("sps_disconnect() fail pipe=0x%lx, rc = %d\n",
  2075. (uintptr_t)sps_pipe_info, rc);
  2076. goto exit;
  2077. }
  2078. memset(sps_connect_info->desc.base, 0x00,
  2079. sps_connect_info->desc.size);
  2080. rc = sps_connect(sps_pipe_info, sps_connect_info);
  2081. if (rc) {
  2082. pr_err("sps_connect() fail pipe=0x%lx, rc = %d\n",
  2083. (uintptr_t)sps_pipe_info, rc);
  2084. goto exit;
  2085. }
  2086. /* Reconnect to producer pipe */
  2087. sps_pipe_info = pce_dev->ce_bam_info.producer[op].pipe;
  2088. sps_connect_info = &pce_dev->ce_bam_info.producer[op].connect;
  2089. rc = sps_disconnect(sps_pipe_info);
  2090. if (rc) {
  2091. pr_err("sps_connect() fail pipe=0x%lx, rc = %d\n",
  2092. (uintptr_t)sps_pipe_info, rc);
  2093. goto exit;
  2094. }
  2095. memset(sps_connect_info->desc.base, 0x00,
  2096. sps_connect_info->desc.size);
  2097. rc = sps_connect(sps_pipe_info, sps_connect_info);
  2098. if (rc) {
  2099. pr_err("sps_connect() fail pipe=0x%lx, rc = %d\n",
  2100. (uintptr_t)sps_pipe_info, rc);
  2101. goto exit;
  2102. }
  2103. /* Register producer callback */
  2104. rc = sps_register_event(sps_pipe_info,
  2105. &pce_dev->ce_bam_info.producer[op].event);
  2106. if (rc)
  2107. pr_err("Producer cb registration failed rc = %d\n",
  2108. rc);
  2109. exit:
  2110. return rc;
  2111. }
  2112. int qce_manage_timeout(void *handle, int req_info)
  2113. {
  2114. struct qce_device *pce_dev = (struct qce_device *) handle;
  2115. struct skcipher_request *areq;
  2116. struct ce_request_info *preq_info;
  2117. qce_comp_func_ptr_t qce_callback;
  2118. uint16_t op = pce_dev->ce_request_info[req_info].offload_op;
  2119. preq_info = &pce_dev->ce_request_info[req_info];
  2120. qce_callback = preq_info->qce_cb;
  2121. areq = (struct skcipher_request *) preq_info->areq;
  2122. pr_info("%s: req info = %d, offload op = %d\n", __func__, req_info, op);
  2123. if (qce_sps_pipe_reset(pce_dev, op))
  2124. pr_err("%s: pipe reset failed\n", __func__);
  2125. qce_enable_clock_gating(pce_dev);
  2126. if (_qce_unlock_other_pipes(pce_dev, req_info))
  2127. pr_err("%s: fail unlock other pipes\n", __func__);
  2128. if (!atomic_read(&preq_info->in_use)) {
  2129. pr_err("request information %d already done\n", req_info);
  2130. return -ENXIO;
  2131. }
  2132. qce_free_req_info(pce_dev, req_info, true);
  2133. return 0;
  2134. }
  2135. EXPORT_SYMBOL(qce_manage_timeout);
  2136. static int _aead_complete(struct qce_device *pce_dev, int req_info)
  2137. {
  2138. struct aead_request *areq;
  2139. unsigned char mac[SHA256_DIGEST_SIZE];
  2140. uint32_t ccm_fail_status = 0;
  2141. uint32_t result_dump_status = 0;
  2142. int32_t result_status = 0;
  2143. struct ce_request_info *preq_info;
  2144. struct ce_sps_data *pce_sps_data;
  2145. qce_comp_func_ptr_t qce_callback;
  2146. preq_info = &pce_dev->ce_request_info[req_info];
  2147. pce_sps_data = &preq_info->ce_sps;
  2148. qce_callback = preq_info->qce_cb;
  2149. areq = (struct aead_request *) preq_info->areq;
  2150. if (areq->src != areq->dst) {
  2151. qce_dma_unmap_sg(pce_dev->pdev, areq->dst, preq_info->dst_nents,
  2152. DMA_FROM_DEVICE);
  2153. }
  2154. qce_dma_unmap_sg(pce_dev->pdev, areq->src, preq_info->src_nents,
  2155. (areq->src == areq->dst) ? DMA_BIDIRECTIONAL :
  2156. DMA_TO_DEVICE);
  2157. if (preq_info->asg)
  2158. qce_dma_unmap_sg(pce_dev->pdev, preq_info->asg,
  2159. preq_info->assoc_nents, DMA_TO_DEVICE);
  2160. /* check MAC */
  2161. memcpy(mac, (char *)(&pce_sps_data->result->auth_iv[0]),
  2162. SHA256_DIGEST_SIZE);
  2163. /* read status before unlock */
  2164. if (preq_info->dir == QCE_DECRYPT) {
  2165. if (pce_dev->no_get_around)
  2166. if (pce_dev->no_ccm_mac_status_get_around)
  2167. ccm_fail_status =
  2168. be32_to_cpu(pce_sps_data->result->status);
  2169. else
  2170. ccm_fail_status =
  2171. be32_to_cpu(pce_sps_data->result_null->status);
  2172. else
  2173. ccm_fail_status = readl_relaxed(pce_dev->iobase +
  2174. CRYPTO_STATUS_REG);
  2175. }
  2176. if (_qce_unlock_other_pipes(pce_dev, req_info)) {
  2177. qce_free_req_info(pce_dev, req_info, true);
  2178. qce_callback(areq, mac, NULL, -ENXIO);
  2179. return -ENXIO;
  2180. }
  2181. result_dump_status = be32_to_cpu(pce_sps_data->result->status);
  2182. pce_sps_data->result->status = 0;
  2183. if (result_dump_status & ((1 << CRYPTO_SW_ERR) | (1 << CRYPTO_AXI_ERR)
  2184. | (1 << CRYPTO_HSD_ERR))) {
  2185. pr_err("aead operation error. Status %x\n", result_dump_status);
  2186. result_status = -ENXIO;
  2187. } else if (pce_sps_data->consumer_status |
  2188. pce_sps_data->producer_status) {
  2189. pr_err("aead sps operation error. sps status %x %x\n",
  2190. pce_sps_data->consumer_status,
  2191. pce_sps_data->producer_status);
  2192. result_status = -ENXIO;
  2193. }
  2194. if (!atomic_read(&preq_info->in_use)) {
  2195. pr_err("request information %d already done\n", req_info);
  2196. return -ENXIO;
  2197. }
  2198. if (preq_info->mode == QCE_MODE_CCM) {
  2199. /*
  2200. * Not from result dump, instead, use the status we just
  2201. * read of device for MAC_FAILED.
  2202. */
  2203. if (result_status == 0 && (preq_info->dir == QCE_DECRYPT) &&
  2204. (ccm_fail_status & (1 << CRYPTO_MAC_FAILED)))
  2205. result_status = -EBADMSG;
  2206. qce_free_req_info(pce_dev, req_info, true);
  2207. qce_callback(areq, mac, NULL, result_status);
  2208. } else {
  2209. uint32_t ivsize = 0;
  2210. struct crypto_aead *aead;
  2211. unsigned char iv[NUM_OF_CRYPTO_CNTR_IV_REG * CRYPTO_REG_SIZE];
  2212. aead = crypto_aead_reqtfm(areq);
  2213. ivsize = crypto_aead_ivsize(aead);
  2214. memcpy(iv, (char *)(pce_sps_data->result->encr_cntr_iv),
  2215. sizeof(iv));
  2216. qce_free_req_info(pce_dev, req_info, true);
  2217. qce_callback(areq, mac, iv, result_status);
  2218. }
  2219. return 0;
  2220. }
  2221. static int _sha_complete(struct qce_device *pce_dev, int req_info)
  2222. {
  2223. struct ahash_request *areq;
  2224. unsigned char digest[SHA256_DIGEST_SIZE];
  2225. uint32_t bytecount32[2];
  2226. int32_t result_status = 0;
  2227. uint32_t result_dump_status;
  2228. struct ce_request_info *preq_info;
  2229. struct ce_sps_data *pce_sps_data;
  2230. qce_comp_func_ptr_t qce_callback;
  2231. preq_info = &pce_dev->ce_request_info[req_info];
  2232. pce_sps_data = &preq_info->ce_sps;
  2233. qce_callback = preq_info->qce_cb;
  2234. areq = (struct ahash_request *) preq_info->areq;
  2235. if (!areq) {
  2236. pr_err("sha operation error. areq is NULL\n");
  2237. return -ENXIO;
  2238. }
  2239. qce_dma_unmap_sg(pce_dev->pdev, areq->src, preq_info->src_nents,
  2240. DMA_TO_DEVICE);
  2241. memcpy(digest, (char *)(&pce_sps_data->result->auth_iv[0]),
  2242. SHA256_DIGEST_SIZE);
  2243. _byte_stream_to_net_words(bytecount32,
  2244. (unsigned char *)pce_sps_data->result->auth_byte_count,
  2245. 2 * CRYPTO_REG_SIZE);
  2246. if (_qce_unlock_other_pipes(pce_dev, req_info)) {
  2247. qce_free_req_info(pce_dev, req_info, true);
  2248. qce_callback(areq, digest, (char *)bytecount32,
  2249. -ENXIO);
  2250. return -ENXIO;
  2251. }
  2252. result_dump_status = be32_to_cpu(pce_sps_data->result->status);
  2253. pce_sps_data->result->status = 0;
  2254. if (result_dump_status & ((1 << CRYPTO_SW_ERR) | (1 << CRYPTO_AXI_ERR)
  2255. | (1 << CRYPTO_HSD_ERR))) {
  2256. pr_err("sha operation error. Status %x\n", result_dump_status);
  2257. result_status = -ENXIO;
  2258. } else if (pce_sps_data->consumer_status) {
  2259. pr_err("sha sps operation error. sps status %x\n",
  2260. pce_sps_data->consumer_status);
  2261. result_status = -ENXIO;
  2262. }
  2263. if (!atomic_read(&preq_info->in_use)) {
  2264. pr_err("request information %d already done\n", req_info);
  2265. return -ENXIO;
  2266. }
  2267. qce_free_req_info(pce_dev, req_info, true);
  2268. qce_callback(areq, digest, (char *)bytecount32, result_status);
  2269. return 0;
  2270. }
  2271. static int _f9_complete(struct qce_device *pce_dev, int req_info)
  2272. {
  2273. uint32_t mac_i;
  2274. int32_t result_status = 0;
  2275. uint32_t result_dump_status;
  2276. struct ce_request_info *preq_info;
  2277. struct ce_sps_data *pce_sps_data;
  2278. qce_comp_func_ptr_t qce_callback;
  2279. void *areq;
  2280. preq_info = &pce_dev->ce_request_info[req_info];
  2281. pce_sps_data = &preq_info->ce_sps;
  2282. qce_callback = preq_info->qce_cb;
  2283. areq = preq_info->areq;
  2284. dma_unmap_single(pce_dev->pdev, preq_info->phy_ota_src,
  2285. preq_info->ota_size, DMA_TO_DEVICE);
  2286. _byte_stream_to_net_words(&mac_i,
  2287. (char *)(&pce_sps_data->result->auth_iv[0]),
  2288. CRYPTO_REG_SIZE);
  2289. if (_qce_unlock_other_pipes(pce_dev, req_info)) {
  2290. qce_free_req_info(pce_dev, req_info, true);
  2291. qce_callback(areq, NULL, NULL, -ENXIO);
  2292. return -ENXIO;
  2293. }
  2294. result_dump_status = be32_to_cpu(pce_sps_data->result->status);
  2295. pce_sps_data->result->status = 0;
  2296. if (result_dump_status & ((1 << CRYPTO_SW_ERR) | (1 << CRYPTO_AXI_ERR)
  2297. | (1 << CRYPTO_HSD_ERR))) {
  2298. pr_err("f9 operation error. Status %x\n", result_dump_status);
  2299. result_status = -ENXIO;
  2300. } else if (pce_sps_data->consumer_status |
  2301. pce_sps_data->producer_status) {
  2302. pr_err("f9 sps operation error. sps status %x %x\n",
  2303. pce_sps_data->consumer_status,
  2304. pce_sps_data->producer_status);
  2305. result_status = -ENXIO;
  2306. }
  2307. qce_free_req_info(pce_dev, req_info, true);
  2308. qce_callback(areq, (char *)&mac_i, NULL, result_status);
  2309. return 0;
  2310. }
  2311. static int _ablk_cipher_complete(struct qce_device *pce_dev, int req_info)
  2312. {
  2313. struct skcipher_request *areq;
  2314. unsigned char iv[NUM_OF_CRYPTO_CNTR_IV_REG * CRYPTO_REG_SIZE];
  2315. int32_t result_status = 0;
  2316. uint32_t result_dump_status;
  2317. struct ce_request_info *preq_info;
  2318. struct ce_sps_data *pce_sps_data;
  2319. qce_comp_func_ptr_t qce_callback;
  2320. preq_info = &pce_dev->ce_request_info[req_info];
  2321. pce_sps_data = &preq_info->ce_sps;
  2322. qce_callback = preq_info->qce_cb;
  2323. areq = (struct skcipher_request *) preq_info->areq;
  2324. if (!is_offload_op(preq_info->offload_op)) {
  2325. if (areq->src != areq->dst)
  2326. qce_dma_unmap_sg(pce_dev->pdev, areq->dst,
  2327. preq_info->dst_nents, DMA_FROM_DEVICE);
  2328. qce_dma_unmap_sg(pce_dev->pdev, areq->src,
  2329. preq_info->src_nents,
  2330. (areq->src == areq->dst) ? DMA_BIDIRECTIONAL :
  2331. DMA_TO_DEVICE);
  2332. }
  2333. if (_qce_unlock_other_pipes(pce_dev, req_info)) {
  2334. qce_free_req_info(pce_dev, req_info, true);
  2335. qce_callback(areq, NULL, NULL, -ENXIO);
  2336. return -ENXIO;
  2337. }
  2338. result_dump_status = be32_to_cpu(pce_sps_data->result->status);
  2339. pce_sps_data->result->status = 0;
  2340. if (!is_offload_op(preq_info->offload_op)) {
  2341. if (result_dump_status & ((1 << CRYPTO_SW_ERR) |
  2342. (1 << CRYPTO_AXI_ERR) | (1 << CRYPTO_HSD_ERR))) {
  2343. pr_err("ablk_cipher operation error. Status %x\n",
  2344. result_dump_status);
  2345. result_status = -ENXIO;
  2346. }
  2347. }
  2348. if (pce_sps_data->consumer_status |
  2349. pce_sps_data->producer_status) {
  2350. pr_err("ablk_cipher sps operation error. sps status %x %x\n",
  2351. pce_sps_data->consumer_status,
  2352. pce_sps_data->producer_status);
  2353. result_status = -ENXIO;
  2354. }
  2355. if (preq_info->mode == QCE_MODE_ECB) {
  2356. qce_free_req_info(pce_dev, req_info, true);
  2357. qce_callback(areq, NULL, NULL, pce_sps_data->consumer_status |
  2358. result_status);
  2359. } else {
  2360. if (pce_dev->ce_bam_info.minor_version == 0) {
  2361. if (preq_info->mode == QCE_MODE_CBC) {
  2362. if (preq_info->dir == QCE_DECRYPT)
  2363. memcpy(iv, (char *)preq_info->dec_iv,
  2364. sizeof(iv));
  2365. else
  2366. memcpy(iv, (unsigned char *)
  2367. (sg_virt(areq->src) +
  2368. areq->src->length - 16),
  2369. sizeof(iv));
  2370. }
  2371. if ((preq_info->mode == QCE_MODE_CTR) ||
  2372. (preq_info->mode == QCE_MODE_XTS)) {
  2373. uint32_t num_blk = 0;
  2374. uint32_t cntr_iv3 = 0;
  2375. unsigned long long cntr_iv64 = 0;
  2376. unsigned char *b = (unsigned char *)(&cntr_iv3);
  2377. memcpy(iv, areq->iv, sizeof(iv));
  2378. if (preq_info->mode != QCE_MODE_XTS)
  2379. num_blk = areq->cryptlen/16;
  2380. else
  2381. num_blk = 1;
  2382. cntr_iv3 = ((*(iv + 12) << 24) & 0xff000000) |
  2383. (((*(iv + 13)) << 16) & 0xff0000) |
  2384. (((*(iv + 14)) << 8) & 0xff00) |
  2385. (*(iv + 15) & 0xff);
  2386. cntr_iv64 =
  2387. (((unsigned long long)cntr_iv3 &
  2388. 0xFFFFFFFFULL) +
  2389. (unsigned long long)num_blk) %
  2390. (unsigned long long)(0x100000000ULL);
  2391. cntr_iv3 = (u32)(cntr_iv64 & 0xFFFFFFFF);
  2392. *(iv + 15) = (char)(*b);
  2393. *(iv + 14) = (char)(*(b + 1));
  2394. *(iv + 13) = (char)(*(b + 2));
  2395. *(iv + 12) = (char)(*(b + 3));
  2396. }
  2397. } else {
  2398. memcpy(iv,
  2399. (char *)(pce_sps_data->result->encr_cntr_iv),
  2400. sizeof(iv));
  2401. }
  2402. if (!atomic_read(&preq_info->in_use)) {
  2403. pr_err("request information %d already done\n", req_info);
  2404. return -ENXIO;
  2405. }
  2406. qce_free_req_info(pce_dev, req_info, true);
  2407. qce_callback(areq, NULL, iv, result_status);
  2408. }
  2409. return 0;
  2410. }
  2411. static int _f8_complete(struct qce_device *pce_dev, int req_info)
  2412. {
  2413. int32_t result_status = 0;
  2414. uint32_t result_dump_status;
  2415. uint32_t result_dump_status2;
  2416. struct ce_request_info *preq_info;
  2417. struct ce_sps_data *pce_sps_data;
  2418. qce_comp_func_ptr_t qce_callback;
  2419. void *areq;
  2420. preq_info = &pce_dev->ce_request_info[req_info];
  2421. pce_sps_data = &preq_info->ce_sps;
  2422. qce_callback = preq_info->qce_cb;
  2423. areq = preq_info->areq;
  2424. if (preq_info->phy_ota_dst)
  2425. dma_unmap_single(pce_dev->pdev, preq_info->phy_ota_dst,
  2426. preq_info->ota_size, DMA_FROM_DEVICE);
  2427. if (preq_info->phy_ota_src)
  2428. dma_unmap_single(pce_dev->pdev, preq_info->phy_ota_src,
  2429. preq_info->ota_size, (preq_info->phy_ota_dst) ?
  2430. DMA_TO_DEVICE : DMA_BIDIRECTIONAL);
  2431. if (_qce_unlock_other_pipes(pce_dev, req_info)) {
  2432. qce_free_req_info(pce_dev, req_info, true);
  2433. qce_callback(areq, NULL, NULL, -ENXIO);
  2434. return -ENXIO;
  2435. }
  2436. result_dump_status = be32_to_cpu(pce_sps_data->result->status);
  2437. result_dump_status2 = be32_to_cpu(pce_sps_data->result->status2);
  2438. if ((result_dump_status & ((1 << CRYPTO_SW_ERR) | (1 << CRYPTO_AXI_ERR)
  2439. | (1 << CRYPTO_HSD_ERR)))) {
  2440. pr_err(
  2441. "f8 oper error. Dump Sta %x Sta2 %x req %d\n",
  2442. result_dump_status, result_dump_status2, req_info);
  2443. result_status = -ENXIO;
  2444. } else if (pce_sps_data->consumer_status |
  2445. pce_sps_data->producer_status) {
  2446. pr_err("f8 sps operation error. sps status %x %x\n",
  2447. pce_sps_data->consumer_status,
  2448. pce_sps_data->producer_status);
  2449. result_status = -ENXIO;
  2450. }
  2451. pce_sps_data->result->status = 0;
  2452. pce_sps_data->result->status2 = 0;
  2453. qce_free_req_info(pce_dev, req_info, true);
  2454. qce_callback(areq, NULL, NULL, result_status);
  2455. return 0;
  2456. }
  2457. static void _qce_sps_iovec_count_init(struct qce_device *pce_dev, int req_info)
  2458. {
  2459. struct ce_sps_data *pce_sps_data = &pce_dev->ce_request_info[req_info]
  2460. .ce_sps;
  2461. pce_sps_data->in_transfer.iovec_count = 0;
  2462. pce_sps_data->out_transfer.iovec_count = 0;
  2463. }
  2464. static void _qce_set_flag(struct sps_transfer *sps_bam_pipe, uint32_t flag)
  2465. {
  2466. struct sps_iovec *iovec;
  2467. if (sps_bam_pipe->iovec_count == 0)
  2468. return;
  2469. iovec = sps_bam_pipe->iovec + (sps_bam_pipe->iovec_count - 1);
  2470. iovec->flags |= flag;
  2471. }
  2472. static int _qce_sps_add_data(dma_addr_t paddr, uint32_t len,
  2473. struct sps_transfer *sps_bam_pipe)
  2474. {
  2475. struct sps_iovec *iovec = sps_bam_pipe->iovec +
  2476. sps_bam_pipe->iovec_count;
  2477. uint32_t data_cnt;
  2478. while (len > 0) {
  2479. if (sps_bam_pipe->iovec_count == QCE_MAX_NUM_DSCR) {
  2480. pr_err("Num of descrptor %d exceed max (%d)\n",
  2481. sps_bam_pipe->iovec_count,
  2482. (uint32_t)QCE_MAX_NUM_DSCR);
  2483. return -ENOMEM;
  2484. }
  2485. if (len > SPS_MAX_PKT_SIZE)
  2486. data_cnt = SPS_MAX_PKT_SIZE;
  2487. else
  2488. data_cnt = len;
  2489. iovec->size = data_cnt;
  2490. iovec->addr = SPS_GET_LOWER_ADDR(paddr);
  2491. iovec->flags = SPS_GET_UPPER_ADDR(paddr);
  2492. sps_bam_pipe->iovec_count++;
  2493. iovec++;
  2494. paddr += data_cnt;
  2495. len -= data_cnt;
  2496. }
  2497. return 0;
  2498. }
  2499. static int _qce_sps_add_sg_data(struct qce_device *pce_dev,
  2500. struct scatterlist *sg_src, uint32_t nbytes,
  2501. struct sps_transfer *sps_bam_pipe)
  2502. {
  2503. uint32_t data_cnt, len;
  2504. dma_addr_t addr;
  2505. struct sps_iovec *iovec = sps_bam_pipe->iovec +
  2506. sps_bam_pipe->iovec_count;
  2507. while (nbytes > 0 && sg_src) {
  2508. len = min(nbytes, sg_dma_len(sg_src));
  2509. nbytes -= len;
  2510. addr = sg_dma_address(sg_src);
  2511. if (pce_dev->ce_bam_info.minor_version == 0)
  2512. len = ALIGN(len, pce_dev->ce_bam_info.ce_burst_size);
  2513. while (len > 0) {
  2514. if (sps_bam_pipe->iovec_count == QCE_MAX_NUM_DSCR) {
  2515. pr_err("Num of descrptor %d exceed max (%d)\n",
  2516. sps_bam_pipe->iovec_count,
  2517. (uint32_t)QCE_MAX_NUM_DSCR);
  2518. return -ENOMEM;
  2519. }
  2520. if (len > SPS_MAX_PKT_SIZE) {
  2521. data_cnt = SPS_MAX_PKT_SIZE;
  2522. iovec->size = data_cnt;
  2523. iovec->addr = SPS_GET_LOWER_ADDR(addr);
  2524. iovec->flags = SPS_GET_UPPER_ADDR(addr);
  2525. } else {
  2526. data_cnt = len;
  2527. iovec->size = data_cnt;
  2528. iovec->addr = SPS_GET_LOWER_ADDR(addr);
  2529. iovec->flags = SPS_GET_UPPER_ADDR(addr);
  2530. }
  2531. iovec++;
  2532. sps_bam_pipe->iovec_count++;
  2533. addr += data_cnt;
  2534. len -= data_cnt;
  2535. }
  2536. sg_src = sg_next(sg_src);
  2537. }
  2538. return 0;
  2539. }
  2540. static int _qce_sps_add_sg_data_off(struct qce_device *pce_dev,
  2541. struct scatterlist *sg_src, uint32_t nbytes, uint32_t off,
  2542. struct sps_transfer *sps_bam_pipe)
  2543. {
  2544. uint32_t data_cnt, len;
  2545. dma_addr_t addr;
  2546. struct sps_iovec *iovec = sps_bam_pipe->iovec +
  2547. sps_bam_pipe->iovec_count;
  2548. unsigned int res_within_sg;
  2549. if (!sg_src)
  2550. return -ENOENT;
  2551. res_within_sg = sg_dma_len(sg_src);
  2552. while (off > 0) {
  2553. if (!sg_src) {
  2554. pr_err("broken sg list off %d nbytes %d\n",
  2555. off, nbytes);
  2556. return -ENOENT;
  2557. }
  2558. len = sg_dma_len(sg_src);
  2559. if (off < len) {
  2560. res_within_sg = len - off;
  2561. break;
  2562. }
  2563. off -= len;
  2564. sg_src = sg_next(sg_src);
  2565. if (sg_src)
  2566. res_within_sg = sg_dma_len(sg_src);
  2567. }
  2568. while (nbytes > 0 && sg_src) {
  2569. len = min(nbytes, res_within_sg);
  2570. nbytes -= len;
  2571. addr = sg_dma_address(sg_src) + off;
  2572. if (pce_dev->ce_bam_info.minor_version == 0)
  2573. len = ALIGN(len, pce_dev->ce_bam_info.ce_burst_size);
  2574. while (len > 0) {
  2575. if (sps_bam_pipe->iovec_count == QCE_MAX_NUM_DSCR) {
  2576. pr_err("Num of descrptor %d exceed max (%d)\n",
  2577. sps_bam_pipe->iovec_count,
  2578. (uint32_t)QCE_MAX_NUM_DSCR);
  2579. return -ENOMEM;
  2580. }
  2581. if (len > SPS_MAX_PKT_SIZE) {
  2582. data_cnt = SPS_MAX_PKT_SIZE;
  2583. iovec->size = data_cnt;
  2584. iovec->addr = SPS_GET_LOWER_ADDR(addr);
  2585. iovec->flags = SPS_GET_UPPER_ADDR(addr);
  2586. } else {
  2587. data_cnt = len;
  2588. iovec->size = data_cnt;
  2589. iovec->addr = SPS_GET_LOWER_ADDR(addr);
  2590. iovec->flags = SPS_GET_UPPER_ADDR(addr);
  2591. }
  2592. iovec++;
  2593. sps_bam_pipe->iovec_count++;
  2594. addr += data_cnt;
  2595. len -= data_cnt;
  2596. }
  2597. if (nbytes) {
  2598. sg_src = sg_next(sg_src);
  2599. if (!sg_src) {
  2600. pr_err("more data bytes %d\n", nbytes);
  2601. return -ENOMEM;
  2602. }
  2603. res_within_sg = sg_dma_len(sg_src);
  2604. off = 0;
  2605. }
  2606. }
  2607. return 0;
  2608. }
  2609. static int _qce_sps_add_cmd(struct qce_device *pce_dev, uint32_t flag,
  2610. struct qce_cmdlist_info *cmdptr,
  2611. struct sps_transfer *sps_bam_pipe)
  2612. {
  2613. dma_addr_t paddr = GET_PHYS_ADDR(cmdptr->cmdlist);
  2614. struct sps_iovec *iovec = sps_bam_pipe->iovec +
  2615. sps_bam_pipe->iovec_count;
  2616. iovec->size = cmdptr->size;
  2617. iovec->addr = SPS_GET_LOWER_ADDR(paddr);
  2618. iovec->flags = SPS_GET_UPPER_ADDR(paddr) | SPS_IOVEC_FLAG_CMD | flag;
  2619. sps_bam_pipe->iovec_count++;
  2620. if (sps_bam_pipe->iovec_count >= QCE_MAX_NUM_DSCR) {
  2621. pr_err("Num of descrptor %d exceed max (%d)\n",
  2622. sps_bam_pipe->iovec_count, (uint32_t)QCE_MAX_NUM_DSCR);
  2623. return -ENOMEM;
  2624. }
  2625. return 0;
  2626. }
  2627. static int _qce_sps_transfer(struct qce_device *pce_dev, int req_info)
  2628. {
  2629. int rc = 0;
  2630. struct ce_sps_data *pce_sps_data;
  2631. uint16_t op = pce_dev->ce_request_info[req_info].offload_op;
  2632. pce_sps_data = &pce_dev->ce_request_info[req_info].ce_sps;
  2633. pce_sps_data->out_transfer.user =
  2634. (void *)((uintptr_t)(CRYPTO_REQ_USER_PAT |
  2635. (unsigned int) req_info));
  2636. pce_sps_data->in_transfer.user =
  2637. (void *)((uintptr_t)(CRYPTO_REQ_USER_PAT |
  2638. (unsigned int) req_info));
  2639. _qce_dump_descr_fifos_dbg(pce_dev, req_info);
  2640. if (pce_sps_data->in_transfer.iovec_count) {
  2641. rc = sps_transfer(pce_dev->ce_bam_info.consumer[op].pipe,
  2642. &pce_sps_data->in_transfer);
  2643. if (rc) {
  2644. pr_err("sps_xfr() fail (cons pipe=0x%lx) rc = %d\n",
  2645. (uintptr_t)pce_dev->ce_bam_info.consumer[op].pipe,
  2646. rc);
  2647. goto ret;
  2648. }
  2649. }
  2650. rc = sps_transfer(pce_dev->ce_bam_info.producer[op].pipe,
  2651. &pce_sps_data->out_transfer);
  2652. if (rc)
  2653. pr_err("sps_xfr() fail (producer pipe=0x%lx) rc = %d\n",
  2654. (uintptr_t)pce_dev->ce_bam_info.producer[op].pipe, rc);
  2655. ret:
  2656. if (rc)
  2657. _qce_dump_descr_fifos(pce_dev, req_info);
  2658. return rc;
  2659. }
  2660. /**
  2661. * Allocate and Connect a CE peripheral's SPS endpoint
  2662. *
  2663. * This function allocates endpoint context and
  2664. * connect it with memory endpoint by calling
  2665. * appropriate SPS driver APIs.
  2666. *
  2667. * Also registers a SPS callback function with
  2668. * SPS driver
  2669. *
  2670. * This function should only be called once typically
  2671. * during driver probe.
  2672. *
  2673. * @pce_dev - Pointer to qce_device structure
  2674. * @ep - Pointer to sps endpoint data structure
  2675. * @index - Points to crypto use case
  2676. * @is_produce - 1 means Producer endpoint
  2677. * 0 means Consumer endpoint
  2678. *
  2679. * @return - 0 if successful else negative value.
  2680. *
  2681. */
  2682. static int qce_sps_init_ep_conn(struct qce_device *pce_dev,
  2683. struct qce_sps_ep_conn_data *ep,
  2684. int index,
  2685. bool is_producer)
  2686. {
  2687. int rc = 0;
  2688. struct sps_pipe *sps_pipe_info;
  2689. struct sps_connect *sps_connect_info = &ep->connect;
  2690. struct sps_register_event *sps_event = &ep->event;
  2691. /* Allocate endpoint context */
  2692. sps_pipe_info = sps_alloc_endpoint();
  2693. if (!sps_pipe_info) {
  2694. pr_err("sps_alloc_endpoint() failed!!! is_producer=%d\n",
  2695. is_producer);
  2696. rc = -ENOMEM;
  2697. goto out;
  2698. }
  2699. /* Now save the sps pipe handle */
  2700. ep->pipe = sps_pipe_info;
  2701. /* Get default connection configuration for an endpoint */
  2702. rc = sps_get_config(sps_pipe_info, sps_connect_info);
  2703. if (rc) {
  2704. pr_err("sps_get_config() fail pipe_handle=0x%lx, rc = %d\n",
  2705. (uintptr_t)sps_pipe_info, rc);
  2706. goto get_config_err;
  2707. }
  2708. /* Modify the default connection configuration */
  2709. if (is_producer) {
  2710. /*
  2711. * For CE producer transfer, source should be
  2712. * CE peripheral where as destination should
  2713. * be system memory.
  2714. */
  2715. sps_connect_info->source = pce_dev->ce_bam_info.bam_handle;
  2716. sps_connect_info->destination = SPS_DEV_HANDLE_MEM;
  2717. /* Producer pipe will handle this connection */
  2718. sps_connect_info->mode = SPS_MODE_SRC;
  2719. sps_connect_info->options =
  2720. SPS_O_AUTO_ENABLE | SPS_O_DESC_DONE;
  2721. } else {
  2722. /* For CE consumer transfer, source should be
  2723. * system memory where as destination should
  2724. * CE peripheral
  2725. */
  2726. sps_connect_info->source = SPS_DEV_HANDLE_MEM;
  2727. sps_connect_info->destination = pce_dev->ce_bam_info.bam_handle;
  2728. sps_connect_info->mode = SPS_MODE_DEST;
  2729. sps_connect_info->options =
  2730. SPS_O_AUTO_ENABLE;
  2731. }
  2732. /* Producer pipe index */
  2733. sps_connect_info->src_pipe_index =
  2734. pce_dev->ce_bam_info.src_pipe_index[index];
  2735. /* Consumer pipe index */
  2736. sps_connect_info->dest_pipe_index =
  2737. pce_dev->ce_bam_info.dest_pipe_index[index];
  2738. /* Set pipe group */
  2739. sps_connect_info->lock_group =
  2740. pce_dev->ce_bam_info.pipe_pair_index[index];
  2741. sps_connect_info->event_thresh = 0x10;
  2742. /*
  2743. * Max. no of scatter/gather buffers that can
  2744. * be passed by block layer = 32 (NR_SG).
  2745. * Each BAM descritor needs 64 bits (8 bytes).
  2746. * One BAM descriptor is required per buffer transfer.
  2747. * So we would require total 256 (32 * 8) bytes of descriptor FIFO.
  2748. * But due to HW limitation we need to allocate atleast one extra
  2749. * descriptor memory (256 bytes + 8 bytes). But in order to be
  2750. * in power of 2, we are allocating 512 bytes of memory.
  2751. */
  2752. sps_connect_info->desc.size = QCE_MAX_NUM_DSCR * MAX_QCE_ALLOC_BAM_REQ *
  2753. sizeof(struct sps_iovec);
  2754. if (sps_connect_info->desc.size > MAX_SPS_DESC_FIFO_SIZE)
  2755. sps_connect_info->desc.size = MAX_SPS_DESC_FIFO_SIZE;
  2756. sps_connect_info->desc.base = dma_alloc_coherent(pce_dev->pdev,
  2757. sps_connect_info->desc.size,
  2758. &sps_connect_info->desc.phys_base,
  2759. GFP_KERNEL | __GFP_ZERO);
  2760. if (sps_connect_info->desc.base == NULL) {
  2761. rc = -ENOMEM;
  2762. pr_err("Can not allocate coherent memory for sps data\n");
  2763. goto get_config_err;
  2764. }
  2765. /* Establish connection between peripheral and memory endpoint */
  2766. rc = sps_connect(sps_pipe_info, sps_connect_info);
  2767. if (rc) {
  2768. pr_err("sps_connect() fail pipe_handle=0x%lx, rc = %d\n",
  2769. (uintptr_t)sps_pipe_info, rc);
  2770. goto sps_connect_err;
  2771. }
  2772. sps_event->mode = SPS_TRIGGER_CALLBACK;
  2773. sps_event->xfer_done = NULL;
  2774. sps_event->user = (void *)pce_dev;
  2775. if (is_producer) {
  2776. sps_event->options = SPS_O_EOT | SPS_O_DESC_DONE;
  2777. sps_event->callback = _sps_producer_callback;
  2778. rc = sps_register_event(ep->pipe, sps_event);
  2779. if (rc) {
  2780. pr_err("Producer callback registration failed rc=%d\n",
  2781. rc);
  2782. goto sps_connect_err;
  2783. }
  2784. } else {
  2785. sps_event->options = SPS_O_EOT;
  2786. sps_event->callback = NULL;
  2787. }
  2788. pr_debug("success, %s : pipe_handle=0x%lx, desc fifo base (phy) = 0x%pK\n",
  2789. is_producer ? "PRODUCER(RX/OUT)" : "CONSUMER(TX/IN)",
  2790. (uintptr_t)sps_pipe_info, &sps_connect_info->desc.phys_base);
  2791. goto out;
  2792. sps_connect_err:
  2793. dma_free_coherent(pce_dev->pdev,
  2794. sps_connect_info->desc.size,
  2795. sps_connect_info->desc.base,
  2796. sps_connect_info->desc.phys_base);
  2797. get_config_err:
  2798. sps_free_endpoint(sps_pipe_info);
  2799. out:
  2800. return rc;
  2801. }
  2802. /**
  2803. * Disconnect and Deallocate a CE peripheral's SPS endpoint
  2804. *
  2805. * This function disconnect endpoint and deallocates
  2806. * endpoint context.
  2807. *
  2808. * This function should only be called once typically
  2809. * during driver remove.
  2810. *
  2811. * @pce_dev - Pointer to qce_device structure
  2812. * @ep - Pointer to sps endpoint data structure
  2813. *
  2814. */
  2815. static void qce_sps_exit_ep_conn(struct qce_device *pce_dev,
  2816. struct qce_sps_ep_conn_data *ep)
  2817. {
  2818. struct sps_pipe *sps_pipe_info = ep->pipe;
  2819. struct sps_connect *sps_connect_info = &ep->connect;
  2820. sps_disconnect(sps_pipe_info);
  2821. dma_free_coherent(pce_dev->pdev,
  2822. sps_connect_info->desc.size,
  2823. sps_connect_info->desc.base,
  2824. sps_connect_info->desc.phys_base);
  2825. sps_free_endpoint(sps_pipe_info);
  2826. }
  2827. static void qce_sps_release_bam(struct qce_device *pce_dev)
  2828. {
  2829. struct bam_registration_info *pbam;
  2830. mutex_lock(&bam_register_lock);
  2831. pbam = pce_dev->pbam;
  2832. if (pbam == NULL)
  2833. goto ret;
  2834. pbam->cnt--;
  2835. if (pbam->cnt > 0)
  2836. goto ret;
  2837. if (pce_dev->ce_bam_info.bam_handle) {
  2838. sps_deregister_bam_device(pce_dev->ce_bam_info.bam_handle);
  2839. pr_debug("deregister bam handle 0x%lx\n",
  2840. pce_dev->ce_bam_info.bam_handle);
  2841. pce_dev->ce_bam_info.bam_handle = 0;
  2842. }
  2843. iounmap(pbam->bam_iobase);
  2844. pr_debug("delete bam 0x%x\n", pbam->bam_mem);
  2845. list_del(&pbam->qlist);
  2846. kfree(pbam);
  2847. ret:
  2848. pce_dev->pbam = NULL;
  2849. mutex_unlock(&bam_register_lock);
  2850. }
  2851. static int qce_sps_get_bam(struct qce_device *pce_dev)
  2852. {
  2853. int rc = 0;
  2854. struct sps_bam_props bam = {0};
  2855. struct bam_registration_info *pbam = NULL;
  2856. struct bam_registration_info *p;
  2857. uint32_t bam_cfg = 0;
  2858. mutex_lock(&bam_register_lock);
  2859. list_for_each_entry(p, &qce50_bam_list, qlist) {
  2860. if (p->bam_mem == pce_dev->bam_mem) {
  2861. pbam = p; /* found */
  2862. break;
  2863. }
  2864. }
  2865. if (pbam) {
  2866. pr_debug("found bam 0x%x\n", pbam->bam_mem);
  2867. pbam->cnt++;
  2868. pce_dev->ce_bam_info.bam_handle = pbam->handle;
  2869. pce_dev->ce_bam_info.bam_mem = pbam->bam_mem;
  2870. pce_dev->ce_bam_info.bam_iobase = pbam->bam_iobase;
  2871. pce_dev->pbam = pbam;
  2872. pce_dev->support_cmd_dscr = pbam->support_cmd_dscr;
  2873. goto ret;
  2874. }
  2875. pbam = kzalloc(sizeof(struct bam_registration_info), GFP_KERNEL);
  2876. if (!pbam) {
  2877. rc = -ENOMEM;
  2878. goto ret;
  2879. }
  2880. pbam->cnt = 1;
  2881. pbam->bam_mem = pce_dev->bam_mem;
  2882. pbam->bam_iobase = ioremap(pce_dev->bam_mem,
  2883. pce_dev->bam_mem_size);
  2884. if (!pbam->bam_iobase) {
  2885. kfree(pbam);
  2886. rc = -ENOMEM;
  2887. pr_err("Can not map BAM io memory\n");
  2888. goto ret;
  2889. }
  2890. pce_dev->ce_bam_info.bam_mem = pbam->bam_mem;
  2891. pce_dev->ce_bam_info.bam_iobase = pbam->bam_iobase;
  2892. pbam->handle = 0;
  2893. pr_debug("allocate bam 0x%x\n", pbam->bam_mem);
  2894. bam_cfg = readl_relaxed(pce_dev->ce_bam_info.bam_iobase +
  2895. CRYPTO_BAM_CNFG_BITS_REG);
  2896. pbam->support_cmd_dscr = (bam_cfg & CRYPTO_BAM_CD_ENABLE_MASK) ?
  2897. true : false;
  2898. if (!pbam->support_cmd_dscr) {
  2899. pr_info("qce50 don't support command descriptor. bam_cfg%x\n",
  2900. bam_cfg);
  2901. pce_dev->no_get_around = false;
  2902. }
  2903. pce_dev->support_cmd_dscr = pbam->support_cmd_dscr;
  2904. bam.phys_addr = pce_dev->ce_bam_info.bam_mem;
  2905. bam.virt_addr = pce_dev->ce_bam_info.bam_iobase;
  2906. /*
  2907. * This event threshold value is only significant for BAM-to-BAM
  2908. * transfer. It's ignored for BAM-to-System mode transfer.
  2909. */
  2910. bam.event_threshold = 0x10; /* Pipe event threshold */
  2911. /*
  2912. * This threshold controls when the BAM publish
  2913. * the descriptor size on the sideband interface.
  2914. * SPS HW will only be used when
  2915. * data transfer size > 64 bytes.
  2916. */
  2917. bam.summing_threshold = 64;
  2918. /* SPS driver wll handle the crypto BAM IRQ */
  2919. bam.irq = (u32)pce_dev->ce_bam_info.bam_irq;
  2920. /*
  2921. * Set flag to indicate BAM global device control is managed
  2922. * remotely.
  2923. */
  2924. if (!pce_dev->support_cmd_dscr || pce_dev->is_shared)
  2925. bam.manage = SPS_BAM_MGR_DEVICE_REMOTE;
  2926. else
  2927. bam.manage = SPS_BAM_MGR_LOCAL;
  2928. bam.ee = pce_dev->ce_bam_info.bam_ee;
  2929. bam.ipc_loglevel = QCE_BAM_DEFAULT_IPC_LOGLVL;
  2930. bam.options |= SPS_BAM_CACHED_WP;
  2931. pr_debug("bam physical base=0x%lx\n", (uintptr_t)bam.phys_addr);
  2932. pr_debug("bam virtual base=0x%pK\n", bam.virt_addr);
  2933. /* Register CE Peripheral BAM device to SPS driver */
  2934. rc = sps_register_bam_device(&bam, &pbam->handle);
  2935. if (rc) {
  2936. pr_err("sps_register_bam_device() failed! err=%d\n", rc);
  2937. rc = -EIO;
  2938. iounmap(pbam->bam_iobase);
  2939. kfree(pbam);
  2940. goto ret;
  2941. }
  2942. pce_dev->pbam = pbam;
  2943. list_add_tail(&pbam->qlist, &qce50_bam_list);
  2944. pce_dev->ce_bam_info.bam_handle = pbam->handle;
  2945. ret:
  2946. mutex_unlock(&bam_register_lock);
  2947. return rc;
  2948. }
  2949. /**
  2950. * Initialize SPS HW connected with CE core
  2951. *
  2952. * This function register BAM HW resources with
  2953. * SPS driver and then initialize 2 SPS endpoints
  2954. *
  2955. * This function should only be called once typically
  2956. * during driver probe.
  2957. *
  2958. * @pce_dev - Pointer to qce_device structure
  2959. *
  2960. * @return - 0 if successful else negative value.
  2961. *
  2962. */
  2963. static int qce_sps_init(struct qce_device *pce_dev)
  2964. {
  2965. int rc = 0, i = 0;
  2966. rc = qce_sps_get_bam(pce_dev);
  2967. if (rc)
  2968. return rc;
  2969. pr_debug("BAM device registered. bam_handle=0x%lx\n",
  2970. pce_dev->ce_bam_info.bam_handle);
  2971. for (i = 0; i < QCE_OFFLOAD_OPER_LAST; i++) {
  2972. if (i == QCE_OFFLOAD_NONE && !(pce_dev->kernel_pipes_support))
  2973. continue;
  2974. else if ((i > 0) && !(pce_dev->offload_pipes_support))
  2975. break;
  2976. rc = qce_sps_init_ep_conn(pce_dev,
  2977. &pce_dev->ce_bam_info.producer[i], i, true);
  2978. if (rc)
  2979. goto sps_connect_producer_err;
  2980. rc = qce_sps_init_ep_conn(pce_dev,
  2981. &pce_dev->ce_bam_info.consumer[i], i, false);
  2982. if (rc)
  2983. goto sps_connect_consumer_err;
  2984. }
  2985. pr_info(" QTI MSM CE-BAM at 0x%016llx irq %d\n",
  2986. (unsigned long long)pce_dev->ce_bam_info.bam_mem,
  2987. (unsigned int)pce_dev->ce_bam_info.bam_irq);
  2988. return rc;
  2989. sps_connect_consumer_err:
  2990. qce_sps_exit_ep_conn(pce_dev, &pce_dev->ce_bam_info.producer[i]);
  2991. sps_connect_producer_err:
  2992. qce_sps_release_bam(pce_dev);
  2993. return rc;
  2994. }
  2995. static inline int qce_alloc_req_info(struct qce_device *pce_dev)
  2996. {
  2997. int i;
  2998. int request_index = pce_dev->ce_request_index;
  2999. for (i = 0; i < MAX_QCE_BAM_REQ; i++) {
  3000. request_index++;
  3001. if (request_index >= MAX_QCE_BAM_REQ)
  3002. request_index = 0;
  3003. if (!atomic_xchg(
  3004. &pce_dev->ce_request_info[request_index].in_use,
  3005. true)) {
  3006. pce_dev->ce_request_index = request_index;
  3007. return request_index;
  3008. }
  3009. }
  3010. pr_warn("pcedev %d no reqs available no_of_queued_req %d\n",
  3011. pce_dev->dev_no, atomic_read(
  3012. &pce_dev->no_of_queued_req));
  3013. return -EBUSY;
  3014. }
  3015. static inline void qce_free_req_info(struct qce_device *pce_dev, int req_info,
  3016. bool is_complete)
  3017. {
  3018. pce_dev->ce_request_info[req_info].xfer_type = QCE_XFER_TYPE_LAST;
  3019. if (atomic_xchg(&pce_dev->ce_request_info[req_info].in_use,
  3020. false)) {
  3021. if (req_info < MAX_QCE_BAM_REQ && is_complete)
  3022. atomic_dec(&pce_dev->no_of_queued_req);
  3023. } else
  3024. pr_warn("request info %d free already\n", req_info);
  3025. }
  3026. static void print_notify_debug(struct sps_event_notify *notify)
  3027. {
  3028. phys_addr_t addr =
  3029. DESC_FULL_ADDR((phys_addr_t) notify->data.transfer.iovec.flags,
  3030. notify->data.transfer.iovec.addr);
  3031. pr_debug("sps ev_id=%d, addr=0x%pa, size=0x%x, flags=0x%x user=0x%pK\n",
  3032. notify->event_id, &addr,
  3033. notify->data.transfer.iovec.size,
  3034. notify->data.transfer.iovec.flags,
  3035. notify->data.transfer.user);
  3036. }
  3037. static void _qce_req_complete(struct qce_device *pce_dev, unsigned int req_info)
  3038. {
  3039. struct ce_request_info *preq_info;
  3040. preq_info = &pce_dev->ce_request_info[req_info];
  3041. switch (preq_info->xfer_type) {
  3042. case QCE_XFER_CIPHERING:
  3043. _ablk_cipher_complete(pce_dev, req_info);
  3044. break;
  3045. case QCE_XFER_HASHING:
  3046. _sha_complete(pce_dev, req_info);
  3047. break;
  3048. case QCE_XFER_AEAD:
  3049. _aead_complete(pce_dev, req_info);
  3050. break;
  3051. case QCE_XFER_F8:
  3052. _f8_complete(pce_dev, req_info);
  3053. break;
  3054. case QCE_XFER_F9:
  3055. _f9_complete(pce_dev, req_info);
  3056. break;
  3057. default:
  3058. qce_free_req_info(pce_dev, req_info, true);
  3059. break;
  3060. }
  3061. }
  3062. static void qce_multireq_timeout(struct timer_list *data)
  3063. {
  3064. struct qce_device *pce_dev = from_timer(pce_dev, data, timer);
  3065. int ret = 0;
  3066. int last_seq;
  3067. unsigned long flags;
  3068. last_seq = atomic_read(&pce_dev->bunch_cmd_seq);
  3069. if (last_seq == 0 ||
  3070. last_seq != atomic_read(&pce_dev->last_intr_seq)) {
  3071. atomic_set(&pce_dev->last_intr_seq, last_seq);
  3072. mod_timer(&(pce_dev->timer), (jiffies + DELAY_IN_JIFFIES));
  3073. return;
  3074. }
  3075. /* last bunch mode command time out */
  3076. /*
  3077. * From here to dummy request finish sps request and set owner back
  3078. * to none, we disable interrupt.
  3079. * So it won't get preempted or interrupted. If bam inerrupts happen
  3080. * between, and completion callback gets called from BAM, a new
  3081. * request may be issued by the client driver. Deadlock may happen.
  3082. */
  3083. local_irq_save(flags);
  3084. if (cmpxchg(&pce_dev->owner, QCE_OWNER_NONE, QCE_OWNER_TIMEOUT)
  3085. != QCE_OWNER_NONE) {
  3086. local_irq_restore(flags);
  3087. mod_timer(&(pce_dev->timer), (jiffies + DELAY_IN_JIFFIES));
  3088. return;
  3089. }
  3090. ret = qce_dummy_req(pce_dev);
  3091. if (ret)
  3092. pr_warn("pcedev %d: Failed to insert dummy req\n",
  3093. pce_dev->dev_no);
  3094. cmpxchg(&pce_dev->owner, QCE_OWNER_TIMEOUT, QCE_OWNER_NONE);
  3095. pce_dev->mode = IN_INTERRUPT_MODE;
  3096. local_irq_restore(flags);
  3097. del_timer(&(pce_dev->timer));
  3098. pce_dev->qce_stats.no_of_timeouts++;
  3099. pr_debug("pcedev %d mode switch to INTR\n", pce_dev->dev_no);
  3100. }
  3101. void qce_get_driver_stats(void *handle)
  3102. {
  3103. struct qce_device *pce_dev = (struct qce_device *) handle;
  3104. if (!_qce50_disp_stats)
  3105. return;
  3106. pr_info("Engine %d timeout occuured %d\n", pce_dev->dev_no,
  3107. pce_dev->qce_stats.no_of_timeouts);
  3108. pr_info("Engine %d dummy request inserted %d\n", pce_dev->dev_no,
  3109. pce_dev->qce_stats.no_of_dummy_reqs);
  3110. if (pce_dev->mode)
  3111. pr_info("Engine %d is in BUNCH MODE\n", pce_dev->dev_no);
  3112. else
  3113. pr_info("Engine %d is in INTERRUPT MODE\n", pce_dev->dev_no);
  3114. pr_info("Engine %d outstanding request %d\n", pce_dev->dev_no,
  3115. atomic_read(&pce_dev->no_of_queued_req));
  3116. }
  3117. EXPORT_SYMBOL(qce_get_driver_stats);
  3118. void qce_clear_driver_stats(void *handle)
  3119. {
  3120. struct qce_device *pce_dev = (struct qce_device *) handle;
  3121. pce_dev->qce_stats.no_of_timeouts = 0;
  3122. pce_dev->qce_stats.no_of_dummy_reqs = 0;
  3123. }
  3124. EXPORT_SYMBOL(qce_clear_driver_stats);
  3125. static void _sps_producer_callback(struct sps_event_notify *notify)
  3126. {
  3127. struct qce_device *pce_dev = (struct qce_device *)
  3128. ((struct sps_event_notify *)notify)->user;
  3129. int rc = 0;
  3130. unsigned int req_info;
  3131. struct ce_sps_data *pce_sps_data;
  3132. struct ce_request_info *preq_info;
  3133. uint16_t op;
  3134. print_notify_debug(notify);
  3135. req_info = (unsigned int)((uintptr_t)notify->data.transfer.user);
  3136. if ((req_info & 0xffff0000) != CRYPTO_REQ_USER_PAT) {
  3137. pr_warn("request information %d out of range\n", req_info);
  3138. return;
  3139. }
  3140. req_info = req_info & 0x00ff;
  3141. if (req_info < 0 || req_info >= MAX_QCE_ALLOC_BAM_REQ) {
  3142. pr_warn("request information %d out of range\n", req_info);
  3143. return;
  3144. }
  3145. preq_info = &pce_dev->ce_request_info[req_info];
  3146. if (!atomic_read(&preq_info->in_use)) {
  3147. pr_err("request information %d already done\n", req_info);
  3148. return;
  3149. }
  3150. op = pce_dev->ce_request_info[req_info].offload_op;
  3151. pce_sps_data = &preq_info->ce_sps;
  3152. if ((preq_info->xfer_type == QCE_XFER_CIPHERING ||
  3153. preq_info->xfer_type == QCE_XFER_AEAD) &&
  3154. pce_sps_data->producer_state == QCE_PIPE_STATE_IDLE) {
  3155. pce_sps_data->producer_state = QCE_PIPE_STATE_COMP;
  3156. if (!is_offload_op(op)) {
  3157. pce_sps_data->out_transfer.iovec_count = 0;
  3158. _qce_sps_add_data(GET_PHYS_ADDR(
  3159. pce_sps_data->result_dump),
  3160. CRYPTO_RESULT_DUMP_SIZE,
  3161. &pce_sps_data->out_transfer);
  3162. _qce_set_flag(&pce_sps_data->out_transfer,
  3163. SPS_IOVEC_FLAG_INT);
  3164. rc = sps_transfer(
  3165. pce_dev->ce_bam_info.producer[op].pipe,
  3166. &pce_sps_data->out_transfer);
  3167. if (rc) {
  3168. pr_err("sps_xfr fail (prod pipe=0x%lx) rc = %d\n",
  3169. (uintptr_t)pce_dev->ce_bam_info.producer[op].pipe,
  3170. rc);
  3171. }
  3172. }
  3173. return;
  3174. }
  3175. _qce_req_complete(pce_dev, req_info);
  3176. }
  3177. /**
  3178. * De-initialize SPS HW connected with CE core
  3179. *
  3180. * This function deinitialize SPS endpoints and then
  3181. * deregisters BAM resources from SPS driver.
  3182. *
  3183. * This function should only be called once typically
  3184. * during driver remove.
  3185. *
  3186. * @pce_dev - Pointer to qce_device structure
  3187. *
  3188. */
  3189. static void qce_sps_exit(struct qce_device *pce_dev)
  3190. {
  3191. int i = 0;
  3192. for (i = 0; i < QCE_OFFLOAD_OPER_LAST; i++) {
  3193. if (i == QCE_OFFLOAD_NONE && !(pce_dev->kernel_pipes_support))
  3194. continue;
  3195. else if ((i > 0) && !(pce_dev->offload_pipes_support))
  3196. break;
  3197. qce_sps_exit_ep_conn(pce_dev,
  3198. &pce_dev->ce_bam_info.consumer[i]);
  3199. qce_sps_exit_ep_conn(pce_dev,
  3200. &pce_dev->ce_bam_info.producer[i]);
  3201. }
  3202. qce_sps_release_bam(pce_dev);
  3203. }
  3204. static void qce_add_cmd_element(struct qce_device *pdev,
  3205. struct sps_command_element **cmd_ptr, u32 addr,
  3206. u32 data, struct sps_command_element **populate)
  3207. {
  3208. (*cmd_ptr)->addr = (uint32_t)(addr + pdev->phy_iobase);
  3209. (*cmd_ptr)->command = 0;
  3210. (*cmd_ptr)->data = data;
  3211. (*cmd_ptr)->mask = 0xFFFFFFFF;
  3212. (*cmd_ptr)->reserved = 0;
  3213. if (populate != NULL)
  3214. *populate = *cmd_ptr;
  3215. (*cmd_ptr)++;
  3216. }
  3217. static int _setup_cipher_aes_cmdlistptrs(struct qce_device *pdev, int cri_index,
  3218. unsigned char **pvaddr, enum qce_cipher_mode_enum mode,
  3219. bool key_128)
  3220. {
  3221. struct sps_command_element *ce_vaddr;
  3222. uintptr_t ce_vaddr_start;
  3223. struct qce_cmdlistptr_ops *cmdlistptr;
  3224. struct qce_cmdlist_info *pcl_info = NULL;
  3225. int i = 0;
  3226. uint32_t encr_cfg = 0;
  3227. uint32_t key_reg = 0;
  3228. uint32_t xts_key_reg = 0;
  3229. uint32_t iv_reg = 0;
  3230. cmdlistptr = &pdev->ce_request_info[cri_index].ce_sps.cmdlistptr;
  3231. *pvaddr = (unsigned char *)ALIGN(((uintptr_t)(*pvaddr)),
  3232. pdev->ce_bam_info.ce_burst_size);
  3233. ce_vaddr = (struct sps_command_element *)(*pvaddr);
  3234. ce_vaddr_start = (uintptr_t)(*pvaddr);
  3235. /*
  3236. * Designate chunks of the allocated memory to various
  3237. * command list pointers related to AES cipher operations defined
  3238. * in ce_cmdlistptrs_ops structure.
  3239. */
  3240. switch (mode) {
  3241. case QCE_MODE_CBC:
  3242. case QCE_MODE_CTR:
  3243. if (key_128) {
  3244. cmdlistptr->cipher_aes_128_cbc_ctr.cmdlist =
  3245. (uintptr_t)ce_vaddr;
  3246. pcl_info = &(cmdlistptr->cipher_aes_128_cbc_ctr);
  3247. if (mode == QCE_MODE_CBC)
  3248. encr_cfg = pdev->reg.encr_cfg_aes_cbc_128;
  3249. else
  3250. encr_cfg = pdev->reg.encr_cfg_aes_ctr_128;
  3251. iv_reg = 4;
  3252. key_reg = 4;
  3253. xts_key_reg = 0;
  3254. } else {
  3255. cmdlistptr->cipher_aes_256_cbc_ctr.cmdlist =
  3256. (uintptr_t)ce_vaddr;
  3257. pcl_info = &(cmdlistptr->cipher_aes_256_cbc_ctr);
  3258. if (mode == QCE_MODE_CBC)
  3259. encr_cfg = pdev->reg.encr_cfg_aes_cbc_256;
  3260. else
  3261. encr_cfg = pdev->reg.encr_cfg_aes_ctr_256;
  3262. iv_reg = 4;
  3263. key_reg = 8;
  3264. xts_key_reg = 0;
  3265. }
  3266. break;
  3267. case QCE_MODE_ECB:
  3268. if (key_128) {
  3269. cmdlistptr->cipher_aes_128_ecb.cmdlist =
  3270. (uintptr_t)ce_vaddr;
  3271. pcl_info = &(cmdlistptr->cipher_aes_128_ecb);
  3272. encr_cfg = pdev->reg.encr_cfg_aes_ecb_128;
  3273. iv_reg = 0;
  3274. key_reg = 4;
  3275. xts_key_reg = 0;
  3276. } else {
  3277. cmdlistptr->cipher_aes_256_ecb.cmdlist =
  3278. (uintptr_t)ce_vaddr;
  3279. pcl_info = &(cmdlistptr->cipher_aes_256_ecb);
  3280. encr_cfg = pdev->reg.encr_cfg_aes_ecb_256;
  3281. iv_reg = 0;
  3282. key_reg = 8;
  3283. xts_key_reg = 0;
  3284. }
  3285. break;
  3286. case QCE_MODE_XTS:
  3287. if (key_128) {
  3288. cmdlistptr->cipher_aes_128_xts.cmdlist =
  3289. (uintptr_t)ce_vaddr;
  3290. pcl_info = &(cmdlistptr->cipher_aes_128_xts);
  3291. encr_cfg = pdev->reg.encr_cfg_aes_xts_128;
  3292. iv_reg = 4;
  3293. key_reg = 4;
  3294. xts_key_reg = 4;
  3295. } else {
  3296. cmdlistptr->cipher_aes_256_xts.cmdlist =
  3297. (uintptr_t)ce_vaddr;
  3298. pcl_info = &(cmdlistptr->cipher_aes_256_xts);
  3299. encr_cfg = pdev->reg.encr_cfg_aes_xts_256;
  3300. iv_reg = 4;
  3301. key_reg = 8;
  3302. xts_key_reg = 8;
  3303. }
  3304. break;
  3305. default:
  3306. pr_err("Unknown mode of operation %d received, exiting now\n",
  3307. mode);
  3308. return -EINVAL;
  3309. break;
  3310. }
  3311. /* clear status register */
  3312. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_STATUS_REG, 0, NULL);
  3313. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_STATUS2_REG, 0, NULL);
  3314. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_STATUS3_REG, 0, NULL);
  3315. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_STATUS4_REG, 0, NULL);
  3316. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_STATUS5_REG, 0, NULL);
  3317. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_STATUS6_REG, 0, NULL);
  3318. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CONFIG_REG,
  3319. pdev->reg.crypto_cfg_be, &pcl_info->crypto_cfg);
  3320. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_SEG_SIZE_REG, 0,
  3321. &pcl_info->seg_size);
  3322. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_SEG_CFG_REG, encr_cfg,
  3323. &pcl_info->encr_seg_cfg);
  3324. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_SEG_SIZE_REG, 0,
  3325. &pcl_info->encr_seg_size);
  3326. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_SEG_START_REG, 0,
  3327. &pcl_info->encr_seg_start);
  3328. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CNTR_MASK_REG,
  3329. pdev->reg.encr_cntr_mask_3, &pcl_info->encr_mask_3);
  3330. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CNTR_MASK_REG2,
  3331. pdev->reg.encr_cntr_mask_2, &pcl_info->encr_mask_2);
  3332. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CNTR_MASK_REG1,
  3333. pdev->reg.encr_cntr_mask_1, &pcl_info->encr_mask_1);
  3334. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CNTR_MASK_REG0,
  3335. pdev->reg.encr_cntr_mask_0, &pcl_info->encr_mask_0);
  3336. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_SEG_CFG_REG, 0,
  3337. &pcl_info->auth_seg_cfg);
  3338. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_DATA_PATT_PROC_CFG_REG, 0,
  3339. &pcl_info->pattern_info);
  3340. qce_add_cmd_element(pdev, &ce_vaddr,
  3341. CRYPTO_DATA_PARTIAL_BLOCK_PROC_CFG_REG, 0,
  3342. &pcl_info->block_offset);
  3343. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_KEY0_REG, 0,
  3344. &pcl_info->encr_key);
  3345. for (i = 1; i < key_reg; i++)
  3346. qce_add_cmd_element(pdev, &ce_vaddr,
  3347. (CRYPTO_ENCR_KEY0_REG + i * sizeof(uint32_t)),
  3348. 0, NULL);
  3349. if (xts_key_reg) {
  3350. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_XTS_KEY0_REG,
  3351. 0, &pcl_info->encr_xts_key);
  3352. for (i = 1; i < xts_key_reg; i++)
  3353. qce_add_cmd_element(pdev, &ce_vaddr,
  3354. (CRYPTO_ENCR_XTS_KEY0_REG +
  3355. i * sizeof(uint32_t)), 0, NULL);
  3356. qce_add_cmd_element(pdev, &ce_vaddr,
  3357. CRYPTO_ENCR_XTS_DU_SIZE_REG, 0,
  3358. &pcl_info->encr_xts_du_size);
  3359. }
  3360. if (iv_reg) {
  3361. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CNTR0_IV0_REG, 0,
  3362. &pcl_info->encr_cntr_iv);
  3363. for (i = 1; i < iv_reg; i++)
  3364. qce_add_cmd_element(pdev, &ce_vaddr,
  3365. (CRYPTO_CNTR0_IV0_REG + i * sizeof(uint32_t)),
  3366. 0, NULL);
  3367. }
  3368. /* Add dummy to align size to burst-size multiple */
  3369. if (mode == QCE_MODE_XTS) {
  3370. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_SEG_SIZE_REG,
  3371. 0, &pcl_info->auth_seg_size);
  3372. } else {
  3373. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_SEG_SIZE_REG,
  3374. 0, &pcl_info->auth_seg_size);
  3375. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_SEG_START_REG,
  3376. 0, &pcl_info->auth_seg_size);
  3377. }
  3378. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CONFIG_REG,
  3379. pdev->reg.crypto_cfg_le, &pcl_info->crypto_cfg_le);
  3380. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_GOPROC_REG,
  3381. ((1 << CRYPTO_GO) | (1 << CRYPTO_RESULTS_DUMP) |
  3382. (1 << CRYPTO_CLR_CNTXT)), &pcl_info->go_proc);
  3383. pcl_info->size = (uintptr_t)ce_vaddr - (uintptr_t)ce_vaddr_start;
  3384. *pvaddr = (unsigned char *) ce_vaddr;
  3385. return 0;
  3386. }
  3387. static int _setup_cipher_des_cmdlistptrs(struct qce_device *pdev, int cri_index,
  3388. unsigned char **pvaddr, enum qce_cipher_alg_enum alg,
  3389. bool mode_cbc)
  3390. {
  3391. struct sps_command_element *ce_vaddr;
  3392. uintptr_t ce_vaddr_start;
  3393. struct qce_cmdlistptr_ops *cmdlistptr;
  3394. struct qce_cmdlist_info *pcl_info = NULL;
  3395. int i = 0;
  3396. uint32_t encr_cfg = 0;
  3397. uint32_t key_reg = 0;
  3398. uint32_t iv_reg = 0;
  3399. cmdlistptr = &pdev->ce_request_info[cri_index].ce_sps.cmdlistptr;
  3400. *pvaddr = (unsigned char *)ALIGN(((uintptr_t)(*pvaddr)),
  3401. pdev->ce_bam_info.ce_burst_size);
  3402. ce_vaddr = (struct sps_command_element *)(*pvaddr);
  3403. ce_vaddr_start = (uintptr_t)(*pvaddr);
  3404. /*
  3405. * Designate chunks of the allocated memory to various
  3406. * command list pointers related to cipher operations defined
  3407. * in ce_cmdlistptrs_ops structure.
  3408. */
  3409. switch (alg) {
  3410. case CIPHER_ALG_DES:
  3411. if (mode_cbc) {
  3412. cmdlistptr->cipher_des_cbc.cmdlist =
  3413. (uintptr_t)ce_vaddr;
  3414. pcl_info = &(cmdlistptr->cipher_des_cbc);
  3415. encr_cfg = pdev->reg.encr_cfg_des_cbc;
  3416. iv_reg = 2;
  3417. key_reg = 2;
  3418. } else {
  3419. cmdlistptr->cipher_des_ecb.cmdlist =
  3420. (uintptr_t)ce_vaddr;
  3421. pcl_info = &(cmdlistptr->cipher_des_ecb);
  3422. encr_cfg = pdev->reg.encr_cfg_des_ecb;
  3423. iv_reg = 0;
  3424. key_reg = 2;
  3425. }
  3426. break;
  3427. case CIPHER_ALG_3DES:
  3428. if (mode_cbc) {
  3429. cmdlistptr->cipher_3des_cbc.cmdlist =
  3430. (uintptr_t)ce_vaddr;
  3431. pcl_info = &(cmdlistptr->cipher_3des_cbc);
  3432. encr_cfg = pdev->reg.encr_cfg_3des_cbc;
  3433. iv_reg = 2;
  3434. key_reg = 6;
  3435. } else {
  3436. cmdlistptr->cipher_3des_ecb.cmdlist =
  3437. (uintptr_t)ce_vaddr;
  3438. pcl_info = &(cmdlistptr->cipher_3des_ecb);
  3439. encr_cfg = pdev->reg.encr_cfg_3des_ecb;
  3440. iv_reg = 0;
  3441. key_reg = 6;
  3442. }
  3443. break;
  3444. default:
  3445. pr_err("Unknown algorithms %d received, exiting now\n", alg);
  3446. return -EINVAL;
  3447. break;
  3448. }
  3449. /* clear status register */
  3450. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_STATUS_REG, 0, NULL);
  3451. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CONFIG_REG,
  3452. pdev->reg.crypto_cfg_be, &pcl_info->crypto_cfg);
  3453. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_SEG_SIZE_REG, 0,
  3454. &pcl_info->seg_size);
  3455. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_SEG_CFG_REG, encr_cfg,
  3456. &pcl_info->encr_seg_cfg);
  3457. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_SEG_SIZE_REG, 0,
  3458. &pcl_info->encr_seg_size);
  3459. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_SEG_START_REG, 0,
  3460. &pcl_info->encr_seg_start);
  3461. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_SEG_CFG_REG, 0,
  3462. &pcl_info->auth_seg_cfg);
  3463. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_KEY0_REG, 0,
  3464. &pcl_info->encr_key);
  3465. for (i = 1; i < key_reg; i++)
  3466. qce_add_cmd_element(pdev, &ce_vaddr,
  3467. (CRYPTO_ENCR_KEY0_REG + i * sizeof(uint32_t)),
  3468. 0, NULL);
  3469. if (iv_reg) {
  3470. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CNTR0_IV0_REG, 0,
  3471. &pcl_info->encr_cntr_iv);
  3472. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CNTR1_IV1_REG, 0,
  3473. NULL);
  3474. }
  3475. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CONFIG_REG,
  3476. pdev->reg.crypto_cfg_le, &pcl_info->crypto_cfg_le);
  3477. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_GOPROC_REG,
  3478. ((1 << CRYPTO_GO) | (1 << CRYPTO_RESULTS_DUMP) |
  3479. (1 << CRYPTO_CLR_CNTXT)), &pcl_info->go_proc);
  3480. pcl_info->size = (uintptr_t)ce_vaddr - (uintptr_t)ce_vaddr_start;
  3481. *pvaddr = (unsigned char *) ce_vaddr;
  3482. return 0;
  3483. }
  3484. static int _setup_cipher_null_cmdlistptrs(struct qce_device *pdev,
  3485. int cri_index, unsigned char **pvaddr)
  3486. {
  3487. struct sps_command_element *ce_vaddr;
  3488. uintptr_t ce_vaddr_start;
  3489. struct qce_cmdlistptr_ops *cmdlistptr = &pdev->ce_request_info
  3490. [cri_index].ce_sps.cmdlistptr;
  3491. struct qce_cmdlist_info *pcl_info = NULL;
  3492. *pvaddr = (unsigned char *)ALIGN(((uintptr_t)(*pvaddr)),
  3493. pdev->ce_bam_info.ce_burst_size);
  3494. ce_vaddr_start = (uintptr_t)(*pvaddr);
  3495. ce_vaddr = (struct sps_command_element *)(*pvaddr);
  3496. cmdlistptr->cipher_null.cmdlist = (uintptr_t)ce_vaddr;
  3497. pcl_info = &(cmdlistptr->cipher_null);
  3498. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_SEG_SIZE_REG,
  3499. pdev->ce_bam_info.ce_burst_size, NULL);
  3500. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_SEG_CFG_REG,
  3501. pdev->reg.encr_cfg_aes_ecb_128, NULL);
  3502. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_SEG_SIZE_REG, 0,
  3503. NULL);
  3504. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_SEG_START_REG, 0,
  3505. NULL);
  3506. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_SEG_CFG_REG,
  3507. 0, NULL);
  3508. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_SEG_SIZE_REG,
  3509. 0, NULL);
  3510. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_SEG_START_REG, 0,
  3511. NULL);
  3512. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_GOPROC_REG,
  3513. ((1 << CRYPTO_GO) | (1 << CRYPTO_RESULTS_DUMP) |
  3514. (1 << CRYPTO_CLR_CNTXT)), &pcl_info->go_proc);
  3515. pcl_info->size = (uintptr_t)ce_vaddr - (uintptr_t)ce_vaddr_start;
  3516. *pvaddr = (unsigned char *) ce_vaddr;
  3517. return 0;
  3518. }
  3519. static int _setup_auth_cmdlistptrs(struct qce_device *pdev, int cri_index,
  3520. unsigned char **pvaddr, enum qce_hash_alg_enum alg,
  3521. bool key_128)
  3522. {
  3523. struct sps_command_element *ce_vaddr;
  3524. uintptr_t ce_vaddr_start;
  3525. struct qce_cmdlistptr_ops *cmdlistptr;
  3526. struct qce_cmdlist_info *pcl_info = NULL;
  3527. int i = 0;
  3528. uint32_t key_reg = 0;
  3529. uint32_t auth_cfg = 0;
  3530. uint32_t iv_reg = 0;
  3531. cmdlistptr = &pdev->ce_request_info[cri_index].ce_sps.cmdlistptr;
  3532. *pvaddr = (unsigned char *)ALIGN(((uintptr_t)(*pvaddr)),
  3533. pdev->ce_bam_info.ce_burst_size);
  3534. ce_vaddr_start = (uintptr_t)(*pvaddr);
  3535. ce_vaddr = (struct sps_command_element *)(*pvaddr);
  3536. /*
  3537. * Designate chunks of the allocated memory to various
  3538. * command list pointers related to authentication operations
  3539. * defined in ce_cmdlistptrs_ops structure.
  3540. */
  3541. switch (alg) {
  3542. case QCE_HASH_SHA1:
  3543. cmdlistptr->auth_sha1.cmdlist = (uintptr_t)ce_vaddr;
  3544. pcl_info = &(cmdlistptr->auth_sha1);
  3545. auth_cfg = pdev->reg.auth_cfg_sha1;
  3546. iv_reg = 5;
  3547. /* clear status register */
  3548. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_STATUS_REG,
  3549. 0, NULL);
  3550. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CONFIG_REG,
  3551. pdev->reg.crypto_cfg_be, &pcl_info->crypto_cfg);
  3552. break;
  3553. case QCE_HASH_SHA256:
  3554. cmdlistptr->auth_sha256.cmdlist = (uintptr_t)ce_vaddr;
  3555. pcl_info = &(cmdlistptr->auth_sha256);
  3556. auth_cfg = pdev->reg.auth_cfg_sha256;
  3557. iv_reg = 8;
  3558. /* clear status register */
  3559. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_STATUS_REG,
  3560. 0, NULL);
  3561. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CONFIG_REG,
  3562. pdev->reg.crypto_cfg_be, &pcl_info->crypto_cfg);
  3563. /* 1 dummy write */
  3564. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_SEG_SIZE_REG,
  3565. 0, NULL);
  3566. break;
  3567. case QCE_HASH_SHA1_HMAC:
  3568. cmdlistptr->auth_sha1_hmac.cmdlist = (uintptr_t)ce_vaddr;
  3569. pcl_info = &(cmdlistptr->auth_sha1_hmac);
  3570. auth_cfg = pdev->reg.auth_cfg_hmac_sha1;
  3571. key_reg = 16;
  3572. iv_reg = 5;
  3573. /* clear status register */
  3574. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_STATUS_REG,
  3575. 0, NULL);
  3576. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CONFIG_REG,
  3577. pdev->reg.crypto_cfg_be, &pcl_info->crypto_cfg);
  3578. break;
  3579. case QCE_HASH_SHA256_HMAC:
  3580. cmdlistptr->auth_sha256_hmac.cmdlist = (uintptr_t)ce_vaddr;
  3581. pcl_info = &(cmdlistptr->auth_sha256_hmac);
  3582. auth_cfg = pdev->reg.auth_cfg_hmac_sha256;
  3583. key_reg = 16;
  3584. iv_reg = 8;
  3585. /* clear status register */
  3586. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_STATUS_REG, 0,
  3587. NULL);
  3588. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CONFIG_REG,
  3589. pdev->reg.crypto_cfg_be, &pcl_info->crypto_cfg);
  3590. /* 1 dummy write */
  3591. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_SEG_SIZE_REG,
  3592. 0, NULL);
  3593. break;
  3594. case QCE_HASH_AES_CMAC:
  3595. if (key_128) {
  3596. cmdlistptr->auth_aes_128_cmac.cmdlist =
  3597. (uintptr_t)ce_vaddr;
  3598. pcl_info = &(cmdlistptr->auth_aes_128_cmac);
  3599. auth_cfg = pdev->reg.auth_cfg_cmac_128;
  3600. key_reg = 4;
  3601. } else {
  3602. cmdlistptr->auth_aes_256_cmac.cmdlist =
  3603. (uintptr_t)ce_vaddr;
  3604. pcl_info = &(cmdlistptr->auth_aes_256_cmac);
  3605. auth_cfg = pdev->reg.auth_cfg_cmac_256;
  3606. key_reg = 8;
  3607. }
  3608. /* clear status register */
  3609. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_STATUS_REG, 0,
  3610. NULL);
  3611. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CONFIG_REG,
  3612. pdev->reg.crypto_cfg_be, &pcl_info->crypto_cfg);
  3613. /* 1 dummy write */
  3614. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_SEG_SIZE_REG,
  3615. 0, NULL);
  3616. break;
  3617. default:
  3618. pr_err("Unknown algorithms %d received, exiting now\n", alg);
  3619. return -EINVAL;
  3620. break;
  3621. }
  3622. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_SEG_SIZE_REG, 0,
  3623. &pcl_info->seg_size);
  3624. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_SEG_CFG_REG, 0,
  3625. &pcl_info->encr_seg_cfg);
  3626. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_SEG_CFG_REG,
  3627. auth_cfg, &pcl_info->auth_seg_cfg);
  3628. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_SEG_SIZE_REG, 0,
  3629. &pcl_info->auth_seg_size);
  3630. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_SEG_START_REG, 0,
  3631. &pcl_info->auth_seg_start);
  3632. if (alg == QCE_HASH_AES_CMAC) {
  3633. /* reset auth iv, bytecount and key registers */
  3634. for (i = 0; i < 16; i++)
  3635. qce_add_cmd_element(pdev, &ce_vaddr,
  3636. (CRYPTO_AUTH_IV0_REG + i * sizeof(uint32_t)),
  3637. 0, NULL);
  3638. for (i = 0; i < 16; i++)
  3639. qce_add_cmd_element(pdev, &ce_vaddr,
  3640. (CRYPTO_AUTH_KEY0_REG + i*sizeof(uint32_t)),
  3641. 0, NULL);
  3642. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_BYTECNT0_REG,
  3643. 0, NULL);
  3644. } else {
  3645. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_IV0_REG, 0,
  3646. &pcl_info->auth_iv);
  3647. for (i = 1; i < iv_reg; i++)
  3648. qce_add_cmd_element(pdev, &ce_vaddr,
  3649. (CRYPTO_AUTH_IV0_REG + i*sizeof(uint32_t)),
  3650. 0, NULL);
  3651. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_BYTECNT0_REG,
  3652. 0, &pcl_info->auth_bytecount);
  3653. }
  3654. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_BYTECNT1_REG, 0, NULL);
  3655. if (key_reg) {
  3656. qce_add_cmd_element(pdev, &ce_vaddr,
  3657. CRYPTO_AUTH_KEY0_REG, 0, &pcl_info->auth_key);
  3658. for (i = 1; i < key_reg; i++)
  3659. qce_add_cmd_element(pdev, &ce_vaddr,
  3660. (CRYPTO_AUTH_KEY0_REG + i*sizeof(uint32_t)),
  3661. 0, NULL);
  3662. }
  3663. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CONFIG_REG,
  3664. pdev->reg.crypto_cfg_le, &pcl_info->crypto_cfg_le);
  3665. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_GOPROC_REG,
  3666. ((1 << CRYPTO_GO) | (1 << CRYPTO_RESULTS_DUMP) |
  3667. (1 << CRYPTO_CLR_CNTXT)), &pcl_info->go_proc);
  3668. pcl_info->size = (uintptr_t)ce_vaddr - (uintptr_t)ce_vaddr_start;
  3669. *pvaddr = (unsigned char *) ce_vaddr;
  3670. return 0;
  3671. }
  3672. static int _setup_aead_cmdlistptrs(struct qce_device *pdev,
  3673. int cri_index,
  3674. unsigned char **pvaddr,
  3675. uint32_t alg,
  3676. uint32_t mode,
  3677. uint32_t key_size,
  3678. bool sha1)
  3679. {
  3680. struct sps_command_element *ce_vaddr;
  3681. uintptr_t ce_vaddr_start;
  3682. struct qce_cmdlistptr_ops *cmd;
  3683. struct qce_cmdlist_info *pcl_info = NULL;
  3684. uint32_t key_reg;
  3685. uint32_t iv_reg;
  3686. uint32_t i;
  3687. uint32_t enciv_in_word;
  3688. uint32_t encr_cfg;
  3689. cmd = &pdev->ce_request_info[cri_index].ce_sps.cmdlistptr;
  3690. *pvaddr = (unsigned char *)ALIGN(((uintptr_t)(*pvaddr)),
  3691. pdev->ce_bam_info.ce_burst_size);
  3692. ce_vaddr_start = (uintptr_t)(*pvaddr);
  3693. ce_vaddr = (struct sps_command_element *)(*pvaddr);
  3694. switch (alg) {
  3695. case CIPHER_ALG_DES:
  3696. switch (mode) {
  3697. case QCE_MODE_CBC:
  3698. if (sha1) {
  3699. cmd->aead_hmac_sha1_cbc_des.cmdlist =
  3700. (uintptr_t)ce_vaddr;
  3701. pcl_info =
  3702. &(cmd->aead_hmac_sha1_cbc_des);
  3703. } else {
  3704. cmd->aead_hmac_sha256_cbc_des.cmdlist =
  3705. (uintptr_t)ce_vaddr;
  3706. pcl_info =
  3707. &(cmd->aead_hmac_sha256_cbc_des);
  3708. }
  3709. encr_cfg = pdev->reg.encr_cfg_des_cbc;
  3710. break;
  3711. default:
  3712. return -EINVAL;
  3713. }
  3714. enciv_in_word = 2;
  3715. break;
  3716. case CIPHER_ALG_3DES:
  3717. switch (mode) {
  3718. case QCE_MODE_CBC:
  3719. if (sha1) {
  3720. cmd->aead_hmac_sha1_cbc_3des.cmdlist =
  3721. (uintptr_t)ce_vaddr;
  3722. pcl_info =
  3723. &(cmd->aead_hmac_sha1_cbc_3des);
  3724. } else {
  3725. cmd->aead_hmac_sha256_cbc_3des.cmdlist =
  3726. (uintptr_t)ce_vaddr;
  3727. pcl_info =
  3728. &(cmd->aead_hmac_sha256_cbc_3des);
  3729. }
  3730. encr_cfg = pdev->reg.encr_cfg_3des_cbc;
  3731. break;
  3732. default:
  3733. return -EINVAL;
  3734. }
  3735. enciv_in_word = 2;
  3736. break;
  3737. case CIPHER_ALG_AES:
  3738. switch (mode) {
  3739. case QCE_MODE_CBC:
  3740. if (key_size == AES128_KEY_SIZE) {
  3741. if (sha1) {
  3742. cmd->aead_hmac_sha1_cbc_aes_128.cmdlist =
  3743. (uintptr_t)ce_vaddr;
  3744. pcl_info =
  3745. &(cmd->aead_hmac_sha1_cbc_aes_128);
  3746. } else {
  3747. cmd->aead_hmac_sha256_cbc_aes_128.cmdlist
  3748. = (uintptr_t)ce_vaddr;
  3749. pcl_info =
  3750. &(cmd->aead_hmac_sha256_cbc_aes_128);
  3751. }
  3752. encr_cfg = pdev->reg.encr_cfg_aes_cbc_128;
  3753. } else if (key_size == AES256_KEY_SIZE) {
  3754. if (sha1) {
  3755. cmd->aead_hmac_sha1_cbc_aes_256.cmdlist =
  3756. (uintptr_t)ce_vaddr;
  3757. pcl_info =
  3758. &(cmd->aead_hmac_sha1_cbc_aes_256);
  3759. } else {
  3760. cmd->aead_hmac_sha256_cbc_aes_256.cmdlist =
  3761. (uintptr_t)ce_vaddr;
  3762. pcl_info =
  3763. &(cmd->aead_hmac_sha256_cbc_aes_256);
  3764. }
  3765. encr_cfg = pdev->reg.encr_cfg_aes_cbc_256;
  3766. } else {
  3767. return -EINVAL;
  3768. }
  3769. break;
  3770. default:
  3771. return -EINVAL;
  3772. }
  3773. enciv_in_word = 4;
  3774. break;
  3775. default:
  3776. return -EINVAL;
  3777. }
  3778. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_STATUS_REG, 0, NULL);
  3779. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CONFIG_REG,
  3780. pdev->reg.crypto_cfg_be, &pcl_info->crypto_cfg);
  3781. key_reg = key_size/sizeof(uint32_t);
  3782. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_KEY0_REG, 0,
  3783. &pcl_info->encr_key);
  3784. for (i = 1; i < key_reg; i++)
  3785. qce_add_cmd_element(pdev, &ce_vaddr,
  3786. (CRYPTO_ENCR_KEY0_REG + i * sizeof(uint32_t)),
  3787. 0, NULL);
  3788. if (mode != QCE_MODE_ECB) {
  3789. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CNTR0_IV0_REG, 0,
  3790. &pcl_info->encr_cntr_iv);
  3791. for (i = 1; i < enciv_in_word; i++)
  3792. qce_add_cmd_element(pdev, &ce_vaddr,
  3793. (CRYPTO_CNTR0_IV0_REG + i * sizeof(uint32_t)),
  3794. 0, NULL);
  3795. }
  3796. if (sha1)
  3797. iv_reg = 5;
  3798. else
  3799. iv_reg = 8;
  3800. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_IV0_REG, 0,
  3801. &pcl_info->auth_iv);
  3802. for (i = 1; i < iv_reg; i++)
  3803. qce_add_cmd_element(pdev, &ce_vaddr,
  3804. (CRYPTO_AUTH_IV0_REG + i*sizeof(uint32_t)),
  3805. 0, NULL);
  3806. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_BYTECNT0_REG,
  3807. 0, &pcl_info->auth_bytecount);
  3808. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_BYTECNT1_REG, 0, NULL);
  3809. key_reg = SHA_HMAC_KEY_SIZE/sizeof(uint32_t);
  3810. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_KEY0_REG, 0,
  3811. &pcl_info->auth_key);
  3812. for (i = 1; i < key_reg; i++)
  3813. qce_add_cmd_element(pdev, &ce_vaddr,
  3814. (CRYPTO_AUTH_KEY0_REG + i*sizeof(uint32_t)), 0, NULL);
  3815. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_SEG_SIZE_REG, 0,
  3816. &pcl_info->seg_size);
  3817. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_SEG_CFG_REG, encr_cfg,
  3818. &pcl_info->encr_seg_cfg);
  3819. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_SEG_SIZE_REG, 0,
  3820. &pcl_info->encr_seg_size);
  3821. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_SEG_START_REG, 0,
  3822. &pcl_info->encr_seg_start);
  3823. if (sha1)
  3824. qce_add_cmd_element(
  3825. pdev,
  3826. &ce_vaddr,
  3827. CRYPTO_AUTH_SEG_CFG_REG,
  3828. pdev->reg.auth_cfg_aead_sha1_hmac,
  3829. &pcl_info->auth_seg_cfg);
  3830. else
  3831. qce_add_cmd_element(
  3832. pdev,
  3833. &ce_vaddr,
  3834. CRYPTO_AUTH_SEG_CFG_REG,
  3835. pdev->reg.auth_cfg_aead_sha256_hmac,
  3836. &pcl_info->auth_seg_cfg);
  3837. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_SEG_SIZE_REG, 0,
  3838. &pcl_info->auth_seg_size);
  3839. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_SEG_START_REG, 0,
  3840. &pcl_info->auth_seg_start);
  3841. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CONFIG_REG,
  3842. pdev->reg.crypto_cfg_le, &pcl_info->crypto_cfg_le);
  3843. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_GOPROC_REG,
  3844. ((1 << CRYPTO_GO) | (1 << CRYPTO_RESULTS_DUMP) |
  3845. (1 << CRYPTO_CLR_CNTXT)), &pcl_info->go_proc);
  3846. pcl_info->size = (uintptr_t)ce_vaddr - (uintptr_t)ce_vaddr_start;
  3847. *pvaddr = (unsigned char *) ce_vaddr;
  3848. return 0;
  3849. }
  3850. static int _setup_aead_ccm_cmdlistptrs(struct qce_device *pdev, int cri_index,
  3851. unsigned char **pvaddr, bool key_128)
  3852. {
  3853. struct sps_command_element *ce_vaddr;
  3854. uintptr_t ce_vaddr_start;
  3855. struct qce_cmdlistptr_ops *cmdlistptr = &pdev->ce_request_info
  3856. [cri_index].ce_sps.cmdlistptr;
  3857. struct qce_cmdlist_info *pcl_info = NULL;
  3858. int i = 0;
  3859. uint32_t encr_cfg = 0;
  3860. uint32_t auth_cfg = 0;
  3861. uint32_t key_reg = 0;
  3862. *pvaddr = (unsigned char *)ALIGN(((uintptr_t)(*pvaddr)),
  3863. pdev->ce_bam_info.ce_burst_size);
  3864. ce_vaddr_start = (uintptr_t)(*pvaddr);
  3865. ce_vaddr = (struct sps_command_element *)(*pvaddr);
  3866. /*
  3867. * Designate chunks of the allocated memory to various
  3868. * command list pointers related to aead operations
  3869. * defined in ce_cmdlistptrs_ops structure.
  3870. */
  3871. if (key_128) {
  3872. cmdlistptr->aead_aes_128_ccm.cmdlist =
  3873. (uintptr_t)ce_vaddr;
  3874. pcl_info = &(cmdlistptr->aead_aes_128_ccm);
  3875. auth_cfg = pdev->reg.auth_cfg_aes_ccm_128;
  3876. encr_cfg = pdev->reg.encr_cfg_aes_ccm_128;
  3877. key_reg = 4;
  3878. } else {
  3879. cmdlistptr->aead_aes_256_ccm.cmdlist =
  3880. (uintptr_t)ce_vaddr;
  3881. pcl_info = &(cmdlistptr->aead_aes_256_ccm);
  3882. auth_cfg = pdev->reg.auth_cfg_aes_ccm_256;
  3883. encr_cfg = pdev->reg.encr_cfg_aes_ccm_256;
  3884. key_reg = 8;
  3885. }
  3886. /* clear status register */
  3887. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_STATUS_REG, 0, NULL);
  3888. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CONFIG_REG,
  3889. pdev->reg.crypto_cfg_be, &pcl_info->crypto_cfg);
  3890. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_SEG_CFG_REG, 0, NULL);
  3891. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_SEG_START_REG, 0,
  3892. NULL);
  3893. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_SEG_SIZE_REG, 0,
  3894. &pcl_info->seg_size);
  3895. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_SEG_CFG_REG,
  3896. encr_cfg, &pcl_info->encr_seg_cfg);
  3897. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_SEG_SIZE_REG, 0,
  3898. &pcl_info->encr_seg_size);
  3899. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_SEG_START_REG, 0,
  3900. &pcl_info->encr_seg_start);
  3901. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CNTR_MASK_REG,
  3902. pdev->reg.encr_cntr_mask_3, &pcl_info->encr_mask_3);
  3903. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CNTR_MASK_REG0,
  3904. pdev->reg.encr_cntr_mask_2, &pcl_info->encr_mask_2);
  3905. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CNTR_MASK_REG1,
  3906. pdev->reg.encr_cntr_mask_1, &pcl_info->encr_mask_1);
  3907. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CNTR_MASK_REG2,
  3908. pdev->reg.encr_cntr_mask_0, &pcl_info->encr_mask_0);
  3909. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_SEG_CFG_REG,
  3910. auth_cfg, &pcl_info->auth_seg_cfg);
  3911. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_SEG_SIZE_REG, 0,
  3912. &pcl_info->auth_seg_size);
  3913. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_SEG_START_REG, 0,
  3914. &pcl_info->auth_seg_start);
  3915. /* reset auth iv, bytecount and key registers */
  3916. for (i = 0; i < 8; i++)
  3917. qce_add_cmd_element(pdev, &ce_vaddr,
  3918. (CRYPTO_AUTH_IV0_REG + i * sizeof(uint32_t)),
  3919. 0, NULL);
  3920. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_BYTECNT0_REG,
  3921. 0, NULL);
  3922. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_BYTECNT1_REG,
  3923. 0, NULL);
  3924. for (i = 0; i < 16; i++)
  3925. qce_add_cmd_element(pdev, &ce_vaddr,
  3926. (CRYPTO_AUTH_KEY0_REG + i * sizeof(uint32_t)),
  3927. 0, NULL);
  3928. /* set auth key */
  3929. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_KEY0_REG, 0,
  3930. &pcl_info->auth_key);
  3931. for (i = 1; i < key_reg; i++)
  3932. qce_add_cmd_element(pdev, &ce_vaddr,
  3933. (CRYPTO_AUTH_KEY0_REG + i * sizeof(uint32_t)),
  3934. 0, NULL);
  3935. /* set NONCE info */
  3936. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_INFO_NONCE0_REG, 0,
  3937. &pcl_info->auth_nonce_info);
  3938. for (i = 1; i < 4; i++)
  3939. qce_add_cmd_element(pdev, &ce_vaddr,
  3940. (CRYPTO_AUTH_INFO_NONCE0_REG +
  3941. i * sizeof(uint32_t)), 0, NULL);
  3942. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_KEY0_REG, 0,
  3943. &pcl_info->encr_key);
  3944. for (i = 1; i < key_reg; i++)
  3945. qce_add_cmd_element(pdev, &ce_vaddr,
  3946. (CRYPTO_ENCR_KEY0_REG + i * sizeof(uint32_t)),
  3947. 0, NULL);
  3948. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CNTR0_IV0_REG, 0,
  3949. &pcl_info->encr_cntr_iv);
  3950. for (i = 1; i < 4; i++)
  3951. qce_add_cmd_element(pdev, &ce_vaddr,
  3952. (CRYPTO_CNTR0_IV0_REG + i * sizeof(uint32_t)),
  3953. 0, NULL);
  3954. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_CCM_INT_CNTR0_REG, 0,
  3955. &pcl_info->encr_ccm_cntr_iv);
  3956. for (i = 1; i < 4; i++)
  3957. qce_add_cmd_element(pdev, &ce_vaddr,
  3958. (CRYPTO_ENCR_CCM_INT_CNTR0_REG + i * sizeof(uint32_t)),
  3959. 0, NULL);
  3960. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CONFIG_REG,
  3961. pdev->reg.crypto_cfg_le, &pcl_info->crypto_cfg_le);
  3962. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_GOPROC_REG,
  3963. ((1 << CRYPTO_GO) | (1 << CRYPTO_RESULTS_DUMP) |
  3964. (1 << CRYPTO_CLR_CNTXT)), &pcl_info->go_proc);
  3965. pcl_info->size = (uintptr_t)ce_vaddr - (uintptr_t)ce_vaddr_start;
  3966. *pvaddr = (unsigned char *) ce_vaddr;
  3967. return 0;
  3968. }
  3969. static int _setup_f8_cmdlistptrs(struct qce_device *pdev, int cri_index,
  3970. unsigned char **pvaddr, enum qce_ota_algo_enum alg)
  3971. {
  3972. struct sps_command_element *ce_vaddr;
  3973. uintptr_t ce_vaddr_start;
  3974. struct qce_cmdlistptr_ops *cmdlistptr;
  3975. struct qce_cmdlist_info *pcl_info = NULL;
  3976. int i = 0;
  3977. uint32_t encr_cfg = 0;
  3978. uint32_t key_reg = 4;
  3979. cmdlistptr = &pdev->ce_request_info[cri_index].ce_sps.cmdlistptr;
  3980. *pvaddr = (unsigned char *)ALIGN(((uintptr_t)(*pvaddr)),
  3981. pdev->ce_bam_info.ce_burst_size);
  3982. ce_vaddr = (struct sps_command_element *)(*pvaddr);
  3983. ce_vaddr_start = (uintptr_t)(*pvaddr);
  3984. /*
  3985. * Designate chunks of the allocated memory to various
  3986. * command list pointers related to f8 cipher algorithm defined
  3987. * in ce_cmdlistptrs_ops structure.
  3988. */
  3989. switch (alg) {
  3990. case QCE_OTA_ALGO_KASUMI:
  3991. cmdlistptr->f8_kasumi.cmdlist = (uintptr_t)ce_vaddr;
  3992. pcl_info = &(cmdlistptr->f8_kasumi);
  3993. encr_cfg = pdev->reg.encr_cfg_kasumi;
  3994. break;
  3995. case QCE_OTA_ALGO_SNOW3G:
  3996. default:
  3997. cmdlistptr->f8_snow3g.cmdlist = (uintptr_t)ce_vaddr;
  3998. pcl_info = &(cmdlistptr->f8_snow3g);
  3999. encr_cfg = pdev->reg.encr_cfg_snow3g;
  4000. break;
  4001. }
  4002. /* clear status register */
  4003. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_STATUS_REG,
  4004. 0, NULL);
  4005. /* set config to big endian */
  4006. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CONFIG_REG,
  4007. pdev->reg.crypto_cfg_be, &pcl_info->crypto_cfg);
  4008. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_SEG_SIZE_REG, 0,
  4009. &pcl_info->seg_size);
  4010. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_SEG_CFG_REG, encr_cfg,
  4011. &pcl_info->encr_seg_cfg);
  4012. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_SEG_SIZE_REG, 0,
  4013. &pcl_info->encr_seg_size);
  4014. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_SEG_START_REG, 0,
  4015. &pcl_info->encr_seg_start);
  4016. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_SEG_CFG_REG, 0,
  4017. &pcl_info->auth_seg_cfg);
  4018. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_SEG_SIZE_REG,
  4019. 0, &pcl_info->auth_seg_size);
  4020. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_SEG_START_REG,
  4021. 0, &pcl_info->auth_seg_start);
  4022. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_KEY0_REG, 0,
  4023. &pcl_info->encr_key);
  4024. for (i = 1; i < key_reg; i++)
  4025. qce_add_cmd_element(pdev, &ce_vaddr,
  4026. (CRYPTO_ENCR_KEY0_REG + i * sizeof(uint32_t)),
  4027. 0, NULL);
  4028. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CNTR0_IV0_REG, 0,
  4029. &pcl_info->encr_cntr_iv);
  4030. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CNTR1_IV1_REG, 0,
  4031. NULL);
  4032. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CONFIG_REG,
  4033. pdev->reg.crypto_cfg_le, &pcl_info->crypto_cfg_le);
  4034. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_GOPROC_REG,
  4035. ((1 << CRYPTO_GO) | (1 << CRYPTO_RESULTS_DUMP) |
  4036. (1 << CRYPTO_CLR_CNTXT)), &pcl_info->go_proc);
  4037. pcl_info->size = (uintptr_t)ce_vaddr - (uintptr_t)ce_vaddr_start;
  4038. *pvaddr = (unsigned char *) ce_vaddr;
  4039. return 0;
  4040. }
  4041. static int _setup_f9_cmdlistptrs(struct qce_device *pdev, int cri_index,
  4042. unsigned char **pvaddr, enum qce_ota_algo_enum alg)
  4043. {
  4044. struct sps_command_element *ce_vaddr;
  4045. uintptr_t ce_vaddr_start;
  4046. struct qce_cmdlistptr_ops *cmdlistptr;
  4047. struct qce_cmdlist_info *pcl_info = NULL;
  4048. int i = 0;
  4049. uint32_t auth_cfg = 0;
  4050. uint32_t iv_reg = 0;
  4051. cmdlistptr = &pdev->ce_request_info[cri_index].ce_sps.cmdlistptr;
  4052. *pvaddr = (unsigned char *)ALIGN(((uintptr_t)(*pvaddr)),
  4053. pdev->ce_bam_info.ce_burst_size);
  4054. ce_vaddr_start = (uintptr_t)(*pvaddr);
  4055. ce_vaddr = (struct sps_command_element *)(*pvaddr);
  4056. /*
  4057. * Designate chunks of the allocated memory to various
  4058. * command list pointers related to authentication operations
  4059. * defined in ce_cmdlistptrs_ops structure.
  4060. */
  4061. switch (alg) {
  4062. case QCE_OTA_ALGO_KASUMI:
  4063. cmdlistptr->f9_kasumi.cmdlist = (uintptr_t)ce_vaddr;
  4064. pcl_info = &(cmdlistptr->f9_kasumi);
  4065. auth_cfg = pdev->reg.auth_cfg_kasumi;
  4066. break;
  4067. case QCE_OTA_ALGO_SNOW3G:
  4068. default:
  4069. cmdlistptr->f9_snow3g.cmdlist = (uintptr_t)ce_vaddr;
  4070. pcl_info = &(cmdlistptr->f9_snow3g);
  4071. auth_cfg = pdev->reg.auth_cfg_snow3g;
  4072. }
  4073. /* clear status register */
  4074. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_STATUS_REG,
  4075. 0, NULL);
  4076. /* set config to big endian */
  4077. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CONFIG_REG,
  4078. pdev->reg.crypto_cfg_be, &pcl_info->crypto_cfg);
  4079. iv_reg = 5;
  4080. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_SEG_SIZE_REG, 0,
  4081. &pcl_info->seg_size);
  4082. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_SEG_CFG_REG, 0,
  4083. &pcl_info->encr_seg_cfg);
  4084. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_SEG_CFG_REG,
  4085. auth_cfg, &pcl_info->auth_seg_cfg);
  4086. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_SEG_SIZE_REG, 0,
  4087. &pcl_info->auth_seg_size);
  4088. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_SEG_START_REG, 0,
  4089. &pcl_info->auth_seg_start);
  4090. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_IV0_REG, 0,
  4091. &pcl_info->auth_iv);
  4092. for (i = 1; i < iv_reg; i++) {
  4093. qce_add_cmd_element(pdev, &ce_vaddr,
  4094. (CRYPTO_AUTH_IV0_REG + i*sizeof(uint32_t)),
  4095. 0, NULL);
  4096. }
  4097. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_BYTECNT0_REG,
  4098. 0, &pcl_info->auth_bytecount);
  4099. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_BYTECNT1_REG, 0, NULL);
  4100. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CONFIG_REG,
  4101. pdev->reg.crypto_cfg_le, &pcl_info->crypto_cfg_le);
  4102. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_GOPROC_REG,
  4103. ((1 << CRYPTO_GO) | (1 << CRYPTO_RESULTS_DUMP) |
  4104. (1 << CRYPTO_CLR_CNTXT)), &pcl_info->go_proc);
  4105. pcl_info->size = (uintptr_t)ce_vaddr - (uintptr_t)ce_vaddr_start;
  4106. *pvaddr = (unsigned char *) ce_vaddr;
  4107. return 0;
  4108. }
  4109. static int _setup_unlock_pipe_cmdlistptrs(struct qce_device *pdev,
  4110. int cri_index, unsigned char **pvaddr)
  4111. {
  4112. struct sps_command_element *ce_vaddr;
  4113. uintptr_t ce_vaddr_start = (uintptr_t)(*pvaddr);
  4114. struct qce_cmdlistptr_ops *cmdlistptr;
  4115. struct qce_cmdlist_info *pcl_info = NULL;
  4116. cmdlistptr = &pdev->ce_request_info[cri_index].ce_sps.cmdlistptr;
  4117. *pvaddr = (unsigned char *)ALIGN(((uintptr_t)(*pvaddr)),
  4118. pdev->ce_bam_info.ce_burst_size);
  4119. ce_vaddr = (struct sps_command_element *)(*pvaddr);
  4120. cmdlistptr->unlock_all_pipes.cmdlist = (uintptr_t)ce_vaddr;
  4121. pcl_info = &(cmdlistptr->unlock_all_pipes);
  4122. /*
  4123. * Designate chunks of the allocated memory to command list
  4124. * to unlock pipes.
  4125. */
  4126. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CONFIG_REG,
  4127. CRYPTO_CONFIG_RESET, NULL);
  4128. pcl_info->size = (uintptr_t)ce_vaddr - (uintptr_t)ce_vaddr_start;
  4129. *pvaddr = (unsigned char *) ce_vaddr;
  4130. return 0;
  4131. }
  4132. static int qce_setup_cmdlistptrs(struct qce_device *pdev, int cri_index,
  4133. unsigned char **pvaddr)
  4134. {
  4135. struct sps_command_element *ce_vaddr =
  4136. (struct sps_command_element *)(*pvaddr);
  4137. /*
  4138. * Designate chunks of the allocated memory to various
  4139. * command list pointers related to operations defined
  4140. * in ce_cmdlistptrs_ops structure.
  4141. */
  4142. ce_vaddr =
  4143. (struct sps_command_element *)ALIGN(((uintptr_t) ce_vaddr),
  4144. pdev->ce_bam_info.ce_burst_size);
  4145. *pvaddr = (unsigned char *) ce_vaddr;
  4146. _setup_cipher_aes_cmdlistptrs(pdev, cri_index, pvaddr, QCE_MODE_CBC,
  4147. true);
  4148. _setup_cipher_aes_cmdlistptrs(pdev, cri_index, pvaddr, QCE_MODE_CTR,
  4149. true);
  4150. _setup_cipher_aes_cmdlistptrs(pdev, cri_index, pvaddr, QCE_MODE_ECB,
  4151. true);
  4152. _setup_cipher_aes_cmdlistptrs(pdev, cri_index, pvaddr, QCE_MODE_XTS,
  4153. true);
  4154. _setup_cipher_aes_cmdlistptrs(pdev, cri_index, pvaddr, QCE_MODE_CBC,
  4155. false);
  4156. _setup_cipher_aes_cmdlistptrs(pdev, cri_index, pvaddr, QCE_MODE_CTR,
  4157. false);
  4158. _setup_cipher_aes_cmdlistptrs(pdev, cri_index, pvaddr, QCE_MODE_ECB,
  4159. false);
  4160. _setup_cipher_aes_cmdlistptrs(pdev, cri_index, pvaddr, QCE_MODE_XTS,
  4161. false);
  4162. _setup_cipher_des_cmdlistptrs(pdev, cri_index, pvaddr, CIPHER_ALG_DES,
  4163. true);
  4164. _setup_cipher_des_cmdlistptrs(pdev, cri_index, pvaddr, CIPHER_ALG_DES,
  4165. false);
  4166. _setup_cipher_des_cmdlistptrs(pdev, cri_index, pvaddr, CIPHER_ALG_3DES,
  4167. true);
  4168. _setup_cipher_des_cmdlistptrs(pdev, cri_index, pvaddr, CIPHER_ALG_3DES,
  4169. false);
  4170. _setup_auth_cmdlistptrs(pdev, cri_index, pvaddr, QCE_HASH_SHA1,
  4171. false);
  4172. _setup_auth_cmdlistptrs(pdev, cri_index, pvaddr, QCE_HASH_SHA256,
  4173. false);
  4174. _setup_auth_cmdlistptrs(pdev, cri_index, pvaddr, QCE_HASH_SHA1_HMAC,
  4175. false);
  4176. _setup_auth_cmdlistptrs(pdev, cri_index, pvaddr, QCE_HASH_SHA256_HMAC,
  4177. false);
  4178. _setup_auth_cmdlistptrs(pdev, cri_index, pvaddr, QCE_HASH_AES_CMAC,
  4179. true);
  4180. _setup_auth_cmdlistptrs(pdev, cri_index, pvaddr, QCE_HASH_AES_CMAC,
  4181. false);
  4182. _setup_aead_cmdlistptrs(pdev, cri_index, pvaddr, CIPHER_ALG_DES,
  4183. QCE_MODE_CBC, DES_KEY_SIZE, true);
  4184. _setup_aead_cmdlistptrs(pdev, cri_index, pvaddr, CIPHER_ALG_3DES,
  4185. QCE_MODE_CBC, DES3_EDE_KEY_SIZE, true);
  4186. _setup_aead_cmdlistptrs(pdev, cri_index, pvaddr, CIPHER_ALG_AES,
  4187. QCE_MODE_CBC, AES128_KEY_SIZE, true);
  4188. _setup_aead_cmdlistptrs(pdev, cri_index, pvaddr, CIPHER_ALG_AES,
  4189. QCE_MODE_CBC, AES256_KEY_SIZE, true);
  4190. _setup_aead_cmdlistptrs(pdev, cri_index, pvaddr, CIPHER_ALG_DES,
  4191. QCE_MODE_CBC, DES_KEY_SIZE, false);
  4192. _setup_aead_cmdlistptrs(pdev, cri_index, pvaddr, CIPHER_ALG_3DES,
  4193. QCE_MODE_CBC, DES3_EDE_KEY_SIZE, false);
  4194. _setup_aead_cmdlistptrs(pdev, cri_index, pvaddr, CIPHER_ALG_AES,
  4195. QCE_MODE_CBC, AES128_KEY_SIZE, false);
  4196. _setup_aead_cmdlistptrs(pdev, cri_index, pvaddr, CIPHER_ALG_AES,
  4197. QCE_MODE_CBC, AES256_KEY_SIZE, false);
  4198. _setup_cipher_null_cmdlistptrs(pdev, cri_index, pvaddr);
  4199. _setup_aead_ccm_cmdlistptrs(pdev, cri_index, pvaddr, true);
  4200. _setup_aead_ccm_cmdlistptrs(pdev, cri_index, pvaddr, false);
  4201. _setup_f8_cmdlistptrs(pdev, cri_index, pvaddr, QCE_OTA_ALGO_KASUMI);
  4202. _setup_f8_cmdlistptrs(pdev, cri_index, pvaddr, QCE_OTA_ALGO_SNOW3G);
  4203. _setup_f9_cmdlistptrs(pdev, cri_index, pvaddr, QCE_OTA_ALGO_KASUMI);
  4204. _setup_f9_cmdlistptrs(pdev, cri_index, pvaddr, QCE_OTA_ALGO_SNOW3G);
  4205. _setup_unlock_pipe_cmdlistptrs(pdev, cri_index, pvaddr);
  4206. return 0;
  4207. }
  4208. static int qce_setup_ce_sps_data(struct qce_device *pce_dev)
  4209. {
  4210. unsigned char *vaddr;
  4211. int i;
  4212. unsigned char *iovec_vaddr;
  4213. int iovec_memsize;
  4214. vaddr = pce_dev->coh_vmem;
  4215. vaddr = (unsigned char *)ALIGN(((uintptr_t)vaddr),
  4216. pce_dev->ce_bam_info.ce_burst_size);
  4217. iovec_vaddr = pce_dev->iovec_vmem;
  4218. iovec_memsize = pce_dev->iovec_memsize;
  4219. for (i = 0; i < MAX_QCE_ALLOC_BAM_REQ; i++) {
  4220. /* Allow for 256 descriptor (cmd and data) entries per pipe */
  4221. pce_dev->ce_request_info[i].ce_sps.in_transfer.iovec =
  4222. (struct sps_iovec *)iovec_vaddr;
  4223. pce_dev->ce_request_info[i].ce_sps.in_transfer.iovec_phys =
  4224. virt_to_phys(
  4225. pce_dev->ce_request_info[i].ce_sps.in_transfer.iovec);
  4226. iovec_vaddr += TOTAL_IOVEC_SPACE_PER_PIPE;
  4227. iovec_memsize -= TOTAL_IOVEC_SPACE_PER_PIPE;
  4228. pce_dev->ce_request_info[i].ce_sps.out_transfer.iovec =
  4229. (struct sps_iovec *)iovec_vaddr;
  4230. pce_dev->ce_request_info[i].ce_sps.out_transfer.iovec_phys =
  4231. virt_to_phys(
  4232. pce_dev->ce_request_info[i].ce_sps.out_transfer.iovec);
  4233. iovec_vaddr += TOTAL_IOVEC_SPACE_PER_PIPE;
  4234. iovec_memsize -= TOTAL_IOVEC_SPACE_PER_PIPE;
  4235. if (pce_dev->support_cmd_dscr)
  4236. qce_setup_cmdlistptrs(pce_dev, i, &vaddr);
  4237. vaddr = (unsigned char *)ALIGN(((uintptr_t)vaddr),
  4238. pce_dev->ce_bam_info.ce_burst_size);
  4239. pce_dev->ce_request_info[i].ce_sps.result_dump =
  4240. (uintptr_t)vaddr;
  4241. pce_dev->ce_request_info[i].ce_sps.result_dump_phy =
  4242. GET_PHYS_ADDR((uintptr_t)vaddr);
  4243. pce_dev->ce_request_info[i].ce_sps.result =
  4244. (struct ce_result_dump_format *)vaddr;
  4245. vaddr += CRYPTO_RESULT_DUMP_SIZE;
  4246. pce_dev->ce_request_info[i].ce_sps.result_dump_null =
  4247. (uintptr_t)vaddr;
  4248. pce_dev->ce_request_info[i].ce_sps.result_dump_null_phy =
  4249. GET_PHYS_ADDR((uintptr_t)vaddr);
  4250. pce_dev->ce_request_info[i].ce_sps.result_null =
  4251. (struct ce_result_dump_format *)vaddr;
  4252. vaddr += CRYPTO_RESULT_DUMP_SIZE;
  4253. pce_dev->ce_request_info[i].ce_sps.ignore_buffer =
  4254. (uintptr_t)vaddr;
  4255. vaddr += pce_dev->ce_bam_info.ce_burst_size * 2;
  4256. }
  4257. if ((vaddr - pce_dev->coh_vmem) > pce_dev->memsize ||
  4258. iovec_memsize < 0)
  4259. panic("qce50: Not enough coherent memory. Allocate %x , need %lx\n",
  4260. pce_dev->memsize, (uintptr_t)vaddr -
  4261. (uintptr_t)pce_dev->coh_vmem);
  4262. return 0;
  4263. }
  4264. static int qce_init_ce_cfg_val(struct qce_device *pce_dev)
  4265. {
  4266. uint32_t pipe_pair =
  4267. pce_dev->ce_bam_info.pipe_pair_index[QCE_OFFLOAD_NONE];
  4268. pce_dev->reg.crypto_cfg_be = qce_get_config_be(pce_dev, pipe_pair);
  4269. pce_dev->reg.crypto_cfg_le =
  4270. (pce_dev->reg.crypto_cfg_be | CRYPTO_LITTLE_ENDIAN_MASK);
  4271. /* Initialize encr_cfg register for AES alg */
  4272. pce_dev->reg.encr_cfg_aes_cbc_128 =
  4273. (CRYPTO_ENCR_KEY_SZ_AES128 << CRYPTO_ENCR_KEY_SZ) |
  4274. (CRYPTO_ENCR_ALG_AES << CRYPTO_ENCR_ALG) |
  4275. (CRYPTO_ENCR_MODE_CBC << CRYPTO_ENCR_MODE);
  4276. pce_dev->reg.encr_cfg_aes_cbc_256 =
  4277. (CRYPTO_ENCR_KEY_SZ_AES256 << CRYPTO_ENCR_KEY_SZ) |
  4278. (CRYPTO_ENCR_ALG_AES << CRYPTO_ENCR_ALG) |
  4279. (CRYPTO_ENCR_MODE_CBC << CRYPTO_ENCR_MODE);
  4280. pce_dev->reg.encr_cfg_aes_ctr_128 =
  4281. (CRYPTO_ENCR_KEY_SZ_AES128 << CRYPTO_ENCR_KEY_SZ) |
  4282. (CRYPTO_ENCR_ALG_AES << CRYPTO_ENCR_ALG) |
  4283. (CRYPTO_ENCR_MODE_CTR << CRYPTO_ENCR_MODE);
  4284. pce_dev->reg.encr_cfg_aes_ctr_256 =
  4285. (CRYPTO_ENCR_KEY_SZ_AES256 << CRYPTO_ENCR_KEY_SZ) |
  4286. (CRYPTO_ENCR_ALG_AES << CRYPTO_ENCR_ALG) |
  4287. (CRYPTO_ENCR_MODE_CTR << CRYPTO_ENCR_MODE);
  4288. pce_dev->reg.encr_cfg_aes_xts_128 =
  4289. (CRYPTO_ENCR_KEY_SZ_AES128 << CRYPTO_ENCR_KEY_SZ) |
  4290. (CRYPTO_ENCR_ALG_AES << CRYPTO_ENCR_ALG) |
  4291. (CRYPTO_ENCR_MODE_XTS << CRYPTO_ENCR_MODE);
  4292. pce_dev->reg.encr_cfg_aes_xts_256 =
  4293. (CRYPTO_ENCR_KEY_SZ_AES256 << CRYPTO_ENCR_KEY_SZ) |
  4294. (CRYPTO_ENCR_ALG_AES << CRYPTO_ENCR_ALG) |
  4295. (CRYPTO_ENCR_MODE_XTS << CRYPTO_ENCR_MODE);
  4296. pce_dev->reg.encr_cfg_aes_ecb_128 =
  4297. (CRYPTO_ENCR_KEY_SZ_AES128 << CRYPTO_ENCR_KEY_SZ) |
  4298. (CRYPTO_ENCR_ALG_AES << CRYPTO_ENCR_ALG) |
  4299. (CRYPTO_ENCR_MODE_ECB << CRYPTO_ENCR_MODE);
  4300. pce_dev->reg.encr_cfg_aes_ecb_256 =
  4301. (CRYPTO_ENCR_KEY_SZ_AES256 << CRYPTO_ENCR_KEY_SZ) |
  4302. (CRYPTO_ENCR_ALG_AES << CRYPTO_ENCR_ALG) |
  4303. (CRYPTO_ENCR_MODE_ECB << CRYPTO_ENCR_MODE);
  4304. pce_dev->reg.encr_cfg_aes_ccm_128 =
  4305. (CRYPTO_ENCR_KEY_SZ_AES128 << CRYPTO_ENCR_KEY_SZ) |
  4306. (CRYPTO_ENCR_ALG_AES << CRYPTO_ENCR_ALG) |
  4307. (CRYPTO_ENCR_MODE_CCM << CRYPTO_ENCR_MODE)|
  4308. (CRYPTO_LAST_CCM_XFR << CRYPTO_LAST_CCM);
  4309. pce_dev->reg.encr_cfg_aes_ccm_256 =
  4310. (CRYPTO_ENCR_KEY_SZ_AES256 << CRYPTO_ENCR_KEY_SZ) |
  4311. (CRYPTO_ENCR_ALG_AES << CRYPTO_ENCR_ALG) |
  4312. (CRYPTO_ENCR_MODE_CCM << CRYPTO_ENCR_MODE) |
  4313. (CRYPTO_LAST_CCM_XFR << CRYPTO_LAST_CCM);
  4314. /* Initialize encr_cfg register for DES alg */
  4315. pce_dev->reg.encr_cfg_des_ecb =
  4316. (CRYPTO_ENCR_KEY_SZ_DES << CRYPTO_ENCR_KEY_SZ) |
  4317. (CRYPTO_ENCR_ALG_DES << CRYPTO_ENCR_ALG) |
  4318. (CRYPTO_ENCR_MODE_ECB << CRYPTO_ENCR_MODE);
  4319. pce_dev->reg.encr_cfg_des_cbc =
  4320. (CRYPTO_ENCR_KEY_SZ_DES << CRYPTO_ENCR_KEY_SZ) |
  4321. (CRYPTO_ENCR_ALG_DES << CRYPTO_ENCR_ALG) |
  4322. (CRYPTO_ENCR_MODE_CBC << CRYPTO_ENCR_MODE);
  4323. pce_dev->reg.encr_cfg_3des_ecb =
  4324. (CRYPTO_ENCR_KEY_SZ_3DES << CRYPTO_ENCR_KEY_SZ) |
  4325. (CRYPTO_ENCR_ALG_DES << CRYPTO_ENCR_ALG) |
  4326. (CRYPTO_ENCR_MODE_ECB << CRYPTO_ENCR_MODE);
  4327. pce_dev->reg.encr_cfg_3des_cbc =
  4328. (CRYPTO_ENCR_KEY_SZ_3DES << CRYPTO_ENCR_KEY_SZ) |
  4329. (CRYPTO_ENCR_ALG_DES << CRYPTO_ENCR_ALG) |
  4330. (CRYPTO_ENCR_MODE_CBC << CRYPTO_ENCR_MODE);
  4331. /* Initialize encr_cfg register for kasumi/snow3g alg */
  4332. pce_dev->reg.encr_cfg_kasumi =
  4333. (CRYPTO_ENCR_ALG_KASUMI << CRYPTO_ENCR_ALG);
  4334. pce_dev->reg.encr_cfg_snow3g =
  4335. (CRYPTO_ENCR_ALG_SNOW_3G << CRYPTO_ENCR_ALG);
  4336. /* Initialize auth_cfg register for CMAC alg */
  4337. pce_dev->reg.auth_cfg_cmac_128 =
  4338. (1 << CRYPTO_LAST) | (1 << CRYPTO_FIRST) |
  4339. (CRYPTO_AUTH_MODE_CMAC << CRYPTO_AUTH_MODE)|
  4340. (CRYPTO_AUTH_SIZE_ENUM_16_BYTES << CRYPTO_AUTH_SIZE) |
  4341. (CRYPTO_AUTH_ALG_AES << CRYPTO_AUTH_ALG) |
  4342. (CRYPTO_AUTH_KEY_SZ_AES128 << CRYPTO_AUTH_KEY_SIZE);
  4343. pce_dev->reg.auth_cfg_cmac_256 =
  4344. (1 << CRYPTO_LAST) | (1 << CRYPTO_FIRST) |
  4345. (CRYPTO_AUTH_MODE_CMAC << CRYPTO_AUTH_MODE)|
  4346. (CRYPTO_AUTH_SIZE_ENUM_16_BYTES << CRYPTO_AUTH_SIZE) |
  4347. (CRYPTO_AUTH_ALG_AES << CRYPTO_AUTH_ALG) |
  4348. (CRYPTO_AUTH_KEY_SZ_AES256 << CRYPTO_AUTH_KEY_SIZE);
  4349. /* Initialize auth_cfg register for HMAC alg */
  4350. pce_dev->reg.auth_cfg_hmac_sha1 =
  4351. (CRYPTO_AUTH_MODE_HMAC << CRYPTO_AUTH_MODE)|
  4352. (CRYPTO_AUTH_SIZE_SHA1 << CRYPTO_AUTH_SIZE) |
  4353. (CRYPTO_AUTH_ALG_SHA << CRYPTO_AUTH_ALG) |
  4354. (CRYPTO_AUTH_POS_BEFORE << CRYPTO_AUTH_POS);
  4355. pce_dev->reg.auth_cfg_hmac_sha256 =
  4356. (CRYPTO_AUTH_MODE_HMAC << CRYPTO_AUTH_MODE)|
  4357. (CRYPTO_AUTH_SIZE_SHA256 << CRYPTO_AUTH_SIZE) |
  4358. (CRYPTO_AUTH_ALG_SHA << CRYPTO_AUTH_ALG) |
  4359. (CRYPTO_AUTH_POS_BEFORE << CRYPTO_AUTH_POS);
  4360. /* Initialize auth_cfg register for SHA1/256 alg */
  4361. pce_dev->reg.auth_cfg_sha1 =
  4362. (CRYPTO_AUTH_MODE_HASH << CRYPTO_AUTH_MODE)|
  4363. (CRYPTO_AUTH_SIZE_SHA1 << CRYPTO_AUTH_SIZE) |
  4364. (CRYPTO_AUTH_ALG_SHA << CRYPTO_AUTH_ALG) |
  4365. (CRYPTO_AUTH_POS_BEFORE << CRYPTO_AUTH_POS);
  4366. pce_dev->reg.auth_cfg_sha256 =
  4367. (CRYPTO_AUTH_MODE_HASH << CRYPTO_AUTH_MODE)|
  4368. (CRYPTO_AUTH_SIZE_SHA256 << CRYPTO_AUTH_SIZE) |
  4369. (CRYPTO_AUTH_ALG_SHA << CRYPTO_AUTH_ALG) |
  4370. (CRYPTO_AUTH_POS_BEFORE << CRYPTO_AUTH_POS);
  4371. /* Initialize auth_cfg register for AEAD alg */
  4372. pce_dev->reg.auth_cfg_aead_sha1_hmac =
  4373. (CRYPTO_AUTH_MODE_HMAC << CRYPTO_AUTH_MODE)|
  4374. (CRYPTO_AUTH_SIZE_SHA1 << CRYPTO_AUTH_SIZE) |
  4375. (CRYPTO_AUTH_ALG_SHA << CRYPTO_AUTH_ALG) |
  4376. (1 << CRYPTO_LAST) | (1 << CRYPTO_FIRST);
  4377. pce_dev->reg.auth_cfg_aead_sha256_hmac =
  4378. (CRYPTO_AUTH_MODE_HMAC << CRYPTO_AUTH_MODE)|
  4379. (CRYPTO_AUTH_SIZE_SHA256 << CRYPTO_AUTH_SIZE) |
  4380. (CRYPTO_AUTH_ALG_SHA << CRYPTO_AUTH_ALG) |
  4381. (1 << CRYPTO_LAST) | (1 << CRYPTO_FIRST);
  4382. pce_dev->reg.auth_cfg_aes_ccm_128 =
  4383. (1 << CRYPTO_LAST) | (1 << CRYPTO_FIRST) |
  4384. (CRYPTO_AUTH_MODE_CCM << CRYPTO_AUTH_MODE)|
  4385. (CRYPTO_AUTH_ALG_AES << CRYPTO_AUTH_ALG) |
  4386. (CRYPTO_AUTH_KEY_SZ_AES128 << CRYPTO_AUTH_KEY_SIZE) |
  4387. ((MAX_NONCE/sizeof(uint32_t)) << CRYPTO_AUTH_NONCE_NUM_WORDS);
  4388. pce_dev->reg.auth_cfg_aes_ccm_128 &= ~(1 << CRYPTO_USE_HW_KEY_AUTH);
  4389. pce_dev->reg.auth_cfg_aes_ccm_256 =
  4390. (1 << CRYPTO_LAST) | (1 << CRYPTO_FIRST) |
  4391. (CRYPTO_AUTH_MODE_CCM << CRYPTO_AUTH_MODE)|
  4392. (CRYPTO_AUTH_ALG_AES << CRYPTO_AUTH_ALG) |
  4393. (CRYPTO_AUTH_KEY_SZ_AES256 << CRYPTO_AUTH_KEY_SIZE) |
  4394. ((MAX_NONCE/sizeof(uint32_t)) << CRYPTO_AUTH_NONCE_NUM_WORDS);
  4395. pce_dev->reg.auth_cfg_aes_ccm_256 &= ~(1 << CRYPTO_USE_HW_KEY_AUTH);
  4396. /* Initialize auth_cfg register for kasumi/snow3g */
  4397. pce_dev->reg.auth_cfg_kasumi =
  4398. (CRYPTO_AUTH_ALG_KASUMI << CRYPTO_AUTH_ALG) |
  4399. BIT(CRYPTO_FIRST) | BIT(CRYPTO_LAST);
  4400. pce_dev->reg.auth_cfg_snow3g =
  4401. (CRYPTO_AUTH_ALG_SNOW3G << CRYPTO_AUTH_ALG) |
  4402. BIT(CRYPTO_FIRST) | BIT(CRYPTO_LAST);
  4403. /* Initialize IV counter mask values */
  4404. pce_dev->reg.encr_cntr_mask_3 = 0xFFFFFFFF;
  4405. pce_dev->reg.encr_cntr_mask_2 = 0xFFFFFFFF;
  4406. pce_dev->reg.encr_cntr_mask_1 = 0xFFFFFFFF;
  4407. pce_dev->reg.encr_cntr_mask_0 = 0xFFFFFFFF;
  4408. return 0;
  4409. }
  4410. static void _qce_ccm_get_around_input(struct qce_device *pce_dev,
  4411. struct ce_request_info *preq_info, enum qce_cipher_dir_enum dir)
  4412. {
  4413. struct qce_cmdlist_info *cmdlistinfo;
  4414. struct ce_sps_data *pce_sps_data;
  4415. pce_sps_data = &preq_info->ce_sps;
  4416. if ((dir == QCE_DECRYPT) && pce_dev->no_get_around &&
  4417. !(pce_dev->no_ccm_mac_status_get_around)) {
  4418. cmdlistinfo = &pce_sps_data->cmdlistptr.cipher_null;
  4419. _qce_sps_add_cmd(pce_dev, 0, cmdlistinfo,
  4420. &pce_sps_data->in_transfer);
  4421. _qce_sps_add_data(GET_PHYS_ADDR(pce_sps_data->ignore_buffer),
  4422. pce_dev->ce_bam_info.ce_burst_size,
  4423. &pce_sps_data->in_transfer);
  4424. _qce_set_flag(&pce_sps_data->in_transfer,
  4425. SPS_IOVEC_FLAG_EOT | SPS_IOVEC_FLAG_NWD);
  4426. }
  4427. }
  4428. static void _qce_ccm_get_around_output(struct qce_device *pce_dev,
  4429. struct ce_request_info *preq_info, enum qce_cipher_dir_enum dir)
  4430. {
  4431. struct ce_sps_data *pce_sps_data;
  4432. pce_sps_data = &preq_info->ce_sps;
  4433. if ((dir == QCE_DECRYPT) && pce_dev->no_get_around &&
  4434. !(pce_dev->no_ccm_mac_status_get_around)) {
  4435. _qce_sps_add_data(GET_PHYS_ADDR(pce_sps_data->ignore_buffer),
  4436. pce_dev->ce_bam_info.ce_burst_size,
  4437. &pce_sps_data->out_transfer);
  4438. _qce_sps_add_data(GET_PHYS_ADDR(pce_sps_data->result_dump_null),
  4439. CRYPTO_RESULT_DUMP_SIZE, &pce_sps_data->out_transfer);
  4440. }
  4441. }
  4442. /* QCE_DUMMY_REQ */
  4443. static void qce_dummy_complete(void *cookie, unsigned char *digest,
  4444. unsigned char *authdata, int ret)
  4445. {
  4446. if (!cookie)
  4447. pr_err("invalid cookie\n");
  4448. }
  4449. static int qce_dummy_req(struct qce_device *pce_dev)
  4450. {
  4451. int ret = 0;
  4452. if (atomic_xchg(
  4453. &pce_dev->ce_request_info[DUMMY_REQ_INDEX].in_use, true))
  4454. return -EBUSY;
  4455. ret = qce_process_sha_req(pce_dev, NULL);
  4456. pce_dev->qce_stats.no_of_dummy_reqs++;
  4457. return ret;
  4458. }
  4459. static int select_mode(struct qce_device *pce_dev,
  4460. struct ce_request_info *preq_info)
  4461. {
  4462. struct ce_sps_data *pce_sps_data = &preq_info->ce_sps;
  4463. unsigned int no_of_queued_req;
  4464. unsigned int cadence;
  4465. if (!pce_dev->no_get_around) {
  4466. _qce_set_flag(&pce_sps_data->out_transfer, SPS_IOVEC_FLAG_INT);
  4467. return 0;
  4468. }
  4469. /*
  4470. * claim ownership of device
  4471. */
  4472. again:
  4473. if (cmpxchg(&pce_dev->owner, QCE_OWNER_NONE, QCE_OWNER_CLIENT)
  4474. != QCE_OWNER_NONE) {
  4475. ndelay(40);
  4476. goto again;
  4477. }
  4478. no_of_queued_req = atomic_inc_return(&pce_dev->no_of_queued_req);
  4479. if (pce_dev->mode == IN_INTERRUPT_MODE) {
  4480. if (no_of_queued_req >= MAX_BUNCH_MODE_REQ) {
  4481. pce_dev->mode = IN_BUNCH_MODE;
  4482. pr_debug("pcedev %d mode switch to BUNCH\n",
  4483. pce_dev->dev_no);
  4484. _qce_set_flag(&pce_sps_data->out_transfer,
  4485. SPS_IOVEC_FLAG_INT);
  4486. pce_dev->intr_cadence = 0;
  4487. atomic_set(&pce_dev->bunch_cmd_seq, 1);
  4488. atomic_set(&pce_dev->last_intr_seq, 1);
  4489. mod_timer(&(pce_dev->timer),
  4490. (jiffies + DELAY_IN_JIFFIES));
  4491. } else {
  4492. _qce_set_flag(&pce_sps_data->out_transfer,
  4493. SPS_IOVEC_FLAG_INT);
  4494. }
  4495. } else {
  4496. pce_dev->intr_cadence++;
  4497. cadence = (preq_info->req_len >> 7) + 1;
  4498. if (cadence > SET_INTR_AT_REQ)
  4499. cadence = SET_INTR_AT_REQ;
  4500. if (pce_dev->intr_cadence < cadence || ((pce_dev->intr_cadence
  4501. == cadence) && pce_dev->cadence_flag))
  4502. atomic_inc(&pce_dev->bunch_cmd_seq);
  4503. else {
  4504. _qce_set_flag(&pce_sps_data->out_transfer,
  4505. SPS_IOVEC_FLAG_INT);
  4506. pce_dev->intr_cadence = 0;
  4507. atomic_set(&pce_dev->bunch_cmd_seq, 0);
  4508. atomic_set(&pce_dev->last_intr_seq, 0);
  4509. pce_dev->cadence_flag = !pce_dev->cadence_flag;
  4510. }
  4511. }
  4512. return 0;
  4513. }
  4514. static int _qce_aead_ccm_req(void *handle, struct qce_req *q_req)
  4515. {
  4516. int rc = 0;
  4517. struct qce_device *pce_dev = (struct qce_device *) handle;
  4518. struct aead_request *areq = (struct aead_request *) q_req->areq;
  4519. uint32_t authsize = q_req->authsize;
  4520. uint32_t totallen_in, out_len;
  4521. uint32_t hw_pad_out = 0;
  4522. int ce_burst_size;
  4523. struct qce_cmdlist_info *cmdlistinfo = NULL;
  4524. int req_info = -1;
  4525. struct ce_request_info *preq_info;
  4526. struct ce_sps_data *pce_sps_data;
  4527. req_info = qce_alloc_req_info(pce_dev);
  4528. if (req_info < 0)
  4529. return -EBUSY;
  4530. q_req->current_req_info = req_info;
  4531. preq_info = &pce_dev->ce_request_info[req_info];
  4532. pce_sps_data = &preq_info->ce_sps;
  4533. ce_burst_size = pce_dev->ce_bam_info.ce_burst_size;
  4534. totallen_in = areq->cryptlen + q_req->assoclen;
  4535. if (q_req->dir == QCE_ENCRYPT) {
  4536. q_req->cryptlen = areq->cryptlen;
  4537. out_len = areq->cryptlen + authsize;
  4538. hw_pad_out = ALIGN(authsize, ce_burst_size) - authsize;
  4539. } else {
  4540. q_req->cryptlen = areq->cryptlen - authsize;
  4541. out_len = q_req->cryptlen;
  4542. hw_pad_out = authsize;
  4543. }
  4544. /*
  4545. * For crypto 5.0 that has burst size alignment requirement
  4546. * for data descritpor,
  4547. * the agent above(qcrypto) prepares the src scatter list with
  4548. * memory starting with associated data, followed by
  4549. * data stream to be ciphered.
  4550. * The destination scatter list is pointing to the same
  4551. * data area as source.
  4552. */
  4553. if (pce_dev->ce_bam_info.minor_version == 0)
  4554. preq_info->src_nents = count_sg(areq->src, totallen_in);
  4555. else
  4556. preq_info->src_nents = count_sg(areq->src, areq->cryptlen +
  4557. areq->assoclen);
  4558. if (q_req->assoclen) {
  4559. preq_info->assoc_nents = count_sg(q_req->asg, q_req->assoclen);
  4560. /* formatted associated data input */
  4561. qce_dma_map_sg(pce_dev->pdev, q_req->asg,
  4562. preq_info->assoc_nents, DMA_TO_DEVICE);
  4563. preq_info->asg = q_req->asg;
  4564. } else {
  4565. preq_info->assoc_nents = 0;
  4566. preq_info->asg = NULL;
  4567. }
  4568. /* cipher input */
  4569. qce_dma_map_sg(pce_dev->pdev, areq->src, preq_info->src_nents,
  4570. (areq->src == areq->dst) ? DMA_BIDIRECTIONAL :
  4571. DMA_TO_DEVICE);
  4572. /* cipher + mac output for encryption */
  4573. if (areq->src != areq->dst) {
  4574. /*
  4575. * The destination scatter list is pointing to the same
  4576. * data area as src.
  4577. * Note, the associated data will be pass-through
  4578. * at the beginning of destination area.
  4579. */
  4580. preq_info->dst_nents = count_sg(areq->dst,
  4581. out_len + areq->assoclen);
  4582. qce_dma_map_sg(pce_dev->pdev, areq->dst, preq_info->dst_nents,
  4583. DMA_FROM_DEVICE);
  4584. } else {
  4585. preq_info->dst_nents = preq_info->src_nents;
  4586. }
  4587. if (pce_dev->support_cmd_dscr) {
  4588. cmdlistinfo = _ce_get_cipher_cmdlistinfo(pce_dev, req_info,
  4589. q_req);
  4590. if (cmdlistinfo == NULL) {
  4591. pr_err("Unsupported cipher algorithm %d, mode %d\n",
  4592. q_req->alg, q_req->mode);
  4593. qce_free_req_info(pce_dev, req_info, false);
  4594. return -EINVAL;
  4595. }
  4596. /* set up crypto device */
  4597. rc = _ce_setup_cipher(pce_dev, q_req, totallen_in,
  4598. q_req->assoclen, cmdlistinfo);
  4599. } else {
  4600. /* set up crypto device */
  4601. rc = _ce_setup_cipher_direct(pce_dev, q_req, totallen_in,
  4602. q_req->assoclen);
  4603. }
  4604. if (rc < 0)
  4605. goto bad;
  4606. preq_info->mode = q_req->mode;
  4607. /* setup for callback, and issue command to bam */
  4608. preq_info->areq = q_req->areq;
  4609. preq_info->qce_cb = q_req->qce_cb;
  4610. preq_info->dir = q_req->dir;
  4611. /* setup xfer type for producer callback handling */
  4612. preq_info->xfer_type = QCE_XFER_AEAD;
  4613. preq_info->req_len = totallen_in;
  4614. _qce_sps_iovec_count_init(pce_dev, req_info);
  4615. if (pce_dev->support_cmd_dscr && cmdlistinfo) {
  4616. rc = _qce_sps_add_cmd(pce_dev, SPS_IOVEC_FLAG_LOCK,
  4617. cmdlistinfo, &pce_sps_data->in_transfer);
  4618. if (rc)
  4619. goto bad;
  4620. }
  4621. if (pce_dev->ce_bam_info.minor_version == 0) {
  4622. goto bad;
  4623. } else {
  4624. if (q_req->assoclen) {
  4625. rc = _qce_sps_add_sg_data(pce_dev, q_req->asg,
  4626. q_req->assoclen, &pce_sps_data->in_transfer);
  4627. if (rc)
  4628. goto bad;
  4629. }
  4630. rc = _qce_sps_add_sg_data_off(pce_dev, areq->src, areq->cryptlen,
  4631. areq->assoclen,
  4632. &pce_sps_data->in_transfer);
  4633. if (rc)
  4634. goto bad;
  4635. _qce_set_flag(&pce_sps_data->in_transfer,
  4636. SPS_IOVEC_FLAG_EOT|SPS_IOVEC_FLAG_NWD);
  4637. _qce_ccm_get_around_input(pce_dev, preq_info, q_req->dir);
  4638. if (pce_dev->no_get_around) {
  4639. rc = _qce_sps_add_cmd(pce_dev, SPS_IOVEC_FLAG_UNLOCK,
  4640. &pce_sps_data->cmdlistptr.unlock_all_pipes,
  4641. &pce_sps_data->in_transfer);
  4642. if (rc)
  4643. goto bad;
  4644. }
  4645. /* Pass through to ignore associated data*/
  4646. rc = _qce_sps_add_data(
  4647. GET_PHYS_ADDR(pce_sps_data->ignore_buffer),
  4648. q_req->assoclen,
  4649. &pce_sps_data->out_transfer);
  4650. if (rc)
  4651. goto bad;
  4652. rc = _qce_sps_add_sg_data_off(pce_dev, areq->dst, out_len,
  4653. areq->assoclen,
  4654. &pce_sps_data->out_transfer);
  4655. if (rc)
  4656. goto bad;
  4657. /* Pass through to ignore hw_pad (padding of the MAC data) */
  4658. rc = _qce_sps_add_data(
  4659. GET_PHYS_ADDR(pce_sps_data->ignore_buffer),
  4660. hw_pad_out, &pce_sps_data->out_transfer);
  4661. if (rc)
  4662. goto bad;
  4663. if (pce_dev->no_get_around ||
  4664. totallen_in <= SPS_MAX_PKT_SIZE) {
  4665. rc = _qce_sps_add_data(
  4666. GET_PHYS_ADDR(pce_sps_data->result_dump),
  4667. CRYPTO_RESULT_DUMP_SIZE,
  4668. &pce_sps_data->out_transfer);
  4669. if (rc)
  4670. goto bad;
  4671. pce_sps_data->producer_state = QCE_PIPE_STATE_COMP;
  4672. } else {
  4673. pce_sps_data->producer_state = QCE_PIPE_STATE_IDLE;
  4674. }
  4675. _qce_ccm_get_around_output(pce_dev, preq_info, q_req->dir);
  4676. select_mode(pce_dev, preq_info);
  4677. rc = _qce_sps_transfer(pce_dev, req_info);
  4678. cmpxchg(&pce_dev->owner, QCE_OWNER_CLIENT, QCE_OWNER_NONE);
  4679. }
  4680. if (rc)
  4681. goto bad;
  4682. return 0;
  4683. bad:
  4684. if (preq_info->assoc_nents) {
  4685. qce_dma_unmap_sg(pce_dev->pdev, q_req->asg,
  4686. preq_info->assoc_nents, DMA_TO_DEVICE);
  4687. }
  4688. if (preq_info->src_nents) {
  4689. qce_dma_unmap_sg(pce_dev->pdev, areq->src, preq_info->src_nents,
  4690. (areq->src == areq->dst) ? DMA_BIDIRECTIONAL :
  4691. DMA_TO_DEVICE);
  4692. }
  4693. if (areq->src != areq->dst) {
  4694. qce_dma_unmap_sg(pce_dev->pdev, areq->dst, preq_info->dst_nents,
  4695. DMA_FROM_DEVICE);
  4696. }
  4697. qce_free_req_info(pce_dev, req_info, false);
  4698. return rc;
  4699. }
  4700. static int _qce_suspend(void *handle)
  4701. {
  4702. struct qce_device *pce_dev = (struct qce_device *)handle;
  4703. struct sps_pipe *sps_pipe_info;
  4704. int i = 0;
  4705. if (handle == NULL)
  4706. return -ENODEV;
  4707. for (i = 0; i < QCE_OFFLOAD_OPER_LAST; i++) {
  4708. if (i == QCE_OFFLOAD_NONE && !(pce_dev->kernel_pipes_support))
  4709. continue;
  4710. else if ((i > 0) && !(pce_dev->offload_pipes_support))
  4711. break;
  4712. sps_pipe_info = pce_dev->ce_bam_info.consumer[i].pipe;
  4713. sps_disconnect(sps_pipe_info);
  4714. sps_pipe_info = pce_dev->ce_bam_info.producer[i].pipe;
  4715. sps_disconnect(sps_pipe_info);
  4716. }
  4717. return 0;
  4718. }
  4719. static int _qce_resume(void *handle)
  4720. {
  4721. struct qce_device *pce_dev = (struct qce_device *)handle;
  4722. struct sps_pipe *sps_pipe_info;
  4723. struct sps_connect *sps_connect_info;
  4724. int rc, i;
  4725. if (handle == NULL)
  4726. return -ENODEV;
  4727. for (i = 0; i < QCE_OFFLOAD_OPER_LAST; i++) {
  4728. if (i == QCE_OFFLOAD_NONE && !(pce_dev->kernel_pipes_support))
  4729. continue;
  4730. else if ((i > 0) && !(pce_dev->offload_pipes_support))
  4731. break;
  4732. sps_pipe_info = pce_dev->ce_bam_info.consumer[i].pipe;
  4733. sps_connect_info = &pce_dev->ce_bam_info.consumer[i].connect;
  4734. memset(sps_connect_info->desc.base, 0x00,
  4735. sps_connect_info->desc.size);
  4736. rc = sps_connect(sps_pipe_info, sps_connect_info);
  4737. if (rc) {
  4738. pr_err("sps_connect() fail pipe=0x%lx, rc = %d\n",
  4739. (uintptr_t)sps_pipe_info, rc);
  4740. return rc;
  4741. }
  4742. sps_pipe_info = pce_dev->ce_bam_info.producer[i].pipe;
  4743. sps_connect_info = &pce_dev->ce_bam_info.producer[i].connect;
  4744. memset(sps_connect_info->desc.base, 0x00,
  4745. sps_connect_info->desc.size);
  4746. rc = sps_connect(sps_pipe_info, sps_connect_info);
  4747. if (rc)
  4748. pr_err("sps_connect() fail pipe=0x%lx, rc = %d\n",
  4749. (uintptr_t)sps_pipe_info, rc);
  4750. rc = sps_register_event(sps_pipe_info,
  4751. &pce_dev->ce_bam_info.producer[i].event);
  4752. if (rc)
  4753. pr_err("Producer cb registration failed rc = %d\n",
  4754. rc);
  4755. }
  4756. qce_enable_clock_gating(pce_dev);
  4757. return rc;
  4758. }
  4759. struct qce_pm_table qce_pm_table = {_qce_suspend, _qce_resume};
  4760. EXPORT_SYMBOL(qce_pm_table);
  4761. int qce_aead_req(void *handle, struct qce_req *q_req)
  4762. {
  4763. struct qce_device *pce_dev = (struct qce_device *)handle;
  4764. struct aead_request *areq;
  4765. uint32_t authsize;
  4766. struct crypto_aead *aead;
  4767. uint32_t ivsize;
  4768. uint32_t totallen;
  4769. int rc = 0;
  4770. struct qce_cmdlist_info *cmdlistinfo = NULL;
  4771. int req_info = -1;
  4772. struct ce_sps_data *pce_sps_data;
  4773. struct ce_request_info *preq_info;
  4774. if (q_req->mode == QCE_MODE_CCM)
  4775. return _qce_aead_ccm_req(handle, q_req);
  4776. req_info = qce_alloc_req_info(pce_dev);
  4777. if (req_info < 0)
  4778. return -EBUSY;
  4779. q_req->current_req_info = req_info;
  4780. preq_info = &pce_dev->ce_request_info[req_info];
  4781. pce_sps_data = &preq_info->ce_sps;
  4782. areq = (struct aead_request *) q_req->areq;
  4783. aead = crypto_aead_reqtfm(areq);
  4784. ivsize = crypto_aead_ivsize(aead);
  4785. q_req->ivsize = ivsize;
  4786. authsize = q_req->authsize;
  4787. if (q_req->dir == QCE_ENCRYPT)
  4788. q_req->cryptlen = areq->cryptlen;
  4789. else
  4790. q_req->cryptlen = areq->cryptlen - authsize;
  4791. if (q_req->cryptlen > UINT_MAX - areq->assoclen) {
  4792. pr_err("Integer overflow on total aead req length.\n");
  4793. return -EINVAL;
  4794. }
  4795. totallen = q_req->cryptlen + areq->assoclen;
  4796. if (pce_dev->support_cmd_dscr) {
  4797. cmdlistinfo = _ce_get_aead_cmdlistinfo(pce_dev,
  4798. req_info, q_req);
  4799. if (cmdlistinfo == NULL) {
  4800. pr_err("Unsupported aead ciphering algorithm %d, mode %d, ciphering key length %d, auth digest size %d\n",
  4801. q_req->alg, q_req->mode, q_req->encklen,
  4802. q_req->authsize);
  4803. qce_free_req_info(pce_dev, req_info, false);
  4804. return -EINVAL;
  4805. }
  4806. /* set up crypto device */
  4807. rc = _ce_setup_aead(pce_dev, q_req, totallen,
  4808. areq->assoclen, cmdlistinfo);
  4809. if (rc < 0) {
  4810. qce_free_req_info(pce_dev, req_info, false);
  4811. return -EINVAL;
  4812. }
  4813. }
  4814. /*
  4815. * For crypto 5.0 that has burst size alignment requirement
  4816. * for data descritpor,
  4817. * the agent above(qcrypto) prepares the src scatter list with
  4818. * memory starting with associated data, followed by
  4819. * iv, and data stream to be ciphered.
  4820. */
  4821. preq_info->src_nents = count_sg(areq->src, totallen);
  4822. /* cipher input */
  4823. qce_dma_map_sg(pce_dev->pdev, areq->src, preq_info->src_nents,
  4824. (areq->src == areq->dst) ? DMA_BIDIRECTIONAL :
  4825. DMA_TO_DEVICE);
  4826. /* cipher output for encryption */
  4827. if (areq->src != areq->dst) {
  4828. preq_info->dst_nents = count_sg(areq->dst, totallen);
  4829. qce_dma_map_sg(pce_dev->pdev, areq->dst, preq_info->dst_nents,
  4830. DMA_FROM_DEVICE);
  4831. }
  4832. /* setup for callback, and issue command to bam */
  4833. preq_info->areq = q_req->areq;
  4834. preq_info->qce_cb = q_req->qce_cb;
  4835. preq_info->dir = q_req->dir;
  4836. preq_info->asg = NULL;
  4837. preq_info->offload_op = QCE_OFFLOAD_NONE;
  4838. /* setup xfer type for producer callback handling */
  4839. preq_info->xfer_type = QCE_XFER_AEAD;
  4840. preq_info->req_len = totallen;
  4841. _qce_sps_iovec_count_init(pce_dev, req_info);
  4842. if (pce_dev->support_cmd_dscr) {
  4843. rc = _qce_sps_add_cmd(pce_dev, SPS_IOVEC_FLAG_LOCK,
  4844. cmdlistinfo, &pce_sps_data->in_transfer);
  4845. if (rc)
  4846. goto bad;
  4847. } else {
  4848. rc = _ce_setup_aead_direct(pce_dev, q_req, totallen,
  4849. areq->assoclen);
  4850. if (rc)
  4851. goto bad;
  4852. }
  4853. preq_info->mode = q_req->mode;
  4854. if (pce_dev->ce_bam_info.minor_version == 0) {
  4855. rc = _qce_sps_add_sg_data(pce_dev, areq->src, totallen,
  4856. &pce_sps_data->in_transfer);
  4857. if (rc)
  4858. goto bad;
  4859. _qce_set_flag(&pce_sps_data->in_transfer,
  4860. SPS_IOVEC_FLAG_EOT|SPS_IOVEC_FLAG_NWD);
  4861. rc = _qce_sps_add_sg_data(pce_dev, areq->dst, totallen,
  4862. &pce_sps_data->out_transfer);
  4863. if (rc)
  4864. goto bad;
  4865. if (totallen > SPS_MAX_PKT_SIZE) {
  4866. _qce_set_flag(&pce_sps_data->out_transfer,
  4867. SPS_IOVEC_FLAG_INT);
  4868. pce_sps_data->producer_state = QCE_PIPE_STATE_IDLE;
  4869. } else {
  4870. rc = _qce_sps_add_data(GET_PHYS_ADDR(
  4871. pce_sps_data->result_dump),
  4872. CRYPTO_RESULT_DUMP_SIZE,
  4873. &pce_sps_data->out_transfer);
  4874. if (rc)
  4875. goto bad;
  4876. _qce_set_flag(&pce_sps_data->out_transfer,
  4877. SPS_IOVEC_FLAG_INT);
  4878. pce_sps_data->producer_state = QCE_PIPE_STATE_COMP;
  4879. }
  4880. rc = _qce_sps_transfer(pce_dev, req_info);
  4881. } else {
  4882. rc = _qce_sps_add_sg_data(pce_dev, areq->src, totallen,
  4883. &pce_sps_data->in_transfer);
  4884. if (rc)
  4885. goto bad;
  4886. _qce_set_flag(&pce_sps_data->in_transfer,
  4887. SPS_IOVEC_FLAG_EOT|SPS_IOVEC_FLAG_NWD);
  4888. if (pce_dev->no_get_around) {
  4889. rc = _qce_sps_add_cmd(pce_dev, SPS_IOVEC_FLAG_UNLOCK,
  4890. &pce_sps_data->cmdlistptr.unlock_all_pipes,
  4891. &pce_sps_data->in_transfer);
  4892. if (rc)
  4893. goto bad;
  4894. }
  4895. rc = _qce_sps_add_sg_data(pce_dev, areq->dst, totallen,
  4896. &pce_sps_data->out_transfer);
  4897. if (rc)
  4898. goto bad;
  4899. if (pce_dev->no_get_around || totallen <= SPS_MAX_PKT_SIZE) {
  4900. rc = _qce_sps_add_data(
  4901. GET_PHYS_ADDR(pce_sps_data->result_dump),
  4902. CRYPTO_RESULT_DUMP_SIZE,
  4903. &pce_sps_data->out_transfer);
  4904. if (rc)
  4905. goto bad;
  4906. pce_sps_data->producer_state = QCE_PIPE_STATE_COMP;
  4907. } else {
  4908. pce_sps_data->producer_state = QCE_PIPE_STATE_IDLE;
  4909. }
  4910. select_mode(pce_dev, preq_info);
  4911. rc = _qce_sps_transfer(pce_dev, req_info);
  4912. cmpxchg(&pce_dev->owner, QCE_OWNER_CLIENT, QCE_OWNER_NONE);
  4913. }
  4914. if (rc)
  4915. goto bad;
  4916. return 0;
  4917. bad:
  4918. if (preq_info->src_nents)
  4919. qce_dma_unmap_sg(pce_dev->pdev, areq->src, preq_info->src_nents,
  4920. (areq->src == areq->dst) ? DMA_BIDIRECTIONAL :
  4921. DMA_TO_DEVICE);
  4922. if (areq->src != areq->dst)
  4923. qce_dma_unmap_sg(pce_dev->pdev, areq->dst, preq_info->dst_nents,
  4924. DMA_FROM_DEVICE);
  4925. qce_free_req_info(pce_dev, req_info, false);
  4926. return rc;
  4927. }
  4928. EXPORT_SYMBOL(qce_aead_req);
  4929. int qce_ablk_cipher_req(void *handle, struct qce_req *c_req)
  4930. {
  4931. int rc = 0;
  4932. struct qce_device *pce_dev = (struct qce_device *) handle;
  4933. struct skcipher_request *areq = (struct skcipher_request *)
  4934. c_req->areq;
  4935. struct qce_cmdlist_info *cmdlistinfo = NULL;
  4936. int req_info = -1;
  4937. struct ce_sps_data *pce_sps_data;
  4938. struct ce_request_info *preq_info;
  4939. req_info = qce_alloc_req_info(pce_dev);
  4940. if (req_info < 0)
  4941. return -EBUSY;
  4942. c_req->current_req_info = req_info;
  4943. preq_info = &pce_dev->ce_request_info[req_info];
  4944. pce_sps_data = &preq_info->ce_sps;
  4945. preq_info->src_nents = 0;
  4946. preq_info->dst_nents = 0;
  4947. /* cipher input */
  4948. preq_info->src_nents = count_sg(areq->src, areq->cryptlen);
  4949. if (!is_offload_op(c_req->offload_op))
  4950. qce_dma_map_sg(pce_dev->pdev, areq->src,
  4951. preq_info->src_nents,
  4952. (areq->src == areq->dst) ? DMA_BIDIRECTIONAL :
  4953. DMA_TO_DEVICE);
  4954. /* cipher output */
  4955. if (areq->src != areq->dst) {
  4956. preq_info->dst_nents = count_sg(areq->dst, areq->cryptlen);
  4957. if (!is_offload_op(c_req->offload_op))
  4958. qce_dma_map_sg(pce_dev->pdev, areq->dst,
  4959. preq_info->dst_nents, DMA_FROM_DEVICE);
  4960. } else {
  4961. preq_info->dst_nents = preq_info->src_nents;
  4962. }
  4963. preq_info->dir = c_req->dir;
  4964. if ((pce_dev->ce_bam_info.minor_version == 0) &&
  4965. (preq_info->dir == QCE_DECRYPT) &&
  4966. (c_req->mode == QCE_MODE_CBC)) {
  4967. memcpy(preq_info->dec_iv, (unsigned char *)
  4968. sg_virt(areq->src) + areq->src->length - 16,
  4969. NUM_OF_CRYPTO_CNTR_IV_REG * CRYPTO_REG_SIZE);
  4970. }
  4971. /* set up crypto device */
  4972. if (pce_dev->support_cmd_dscr) {
  4973. cmdlistinfo = _ce_get_cipher_cmdlistinfo(pce_dev,
  4974. req_info, c_req);
  4975. if (cmdlistinfo == NULL) {
  4976. pr_err("Unsupported cipher algorithm %d, mode %d\n",
  4977. c_req->alg, c_req->mode);
  4978. qce_free_req_info(pce_dev, req_info, false);
  4979. return -EINVAL;
  4980. }
  4981. rc = _ce_setup_cipher(pce_dev, c_req, areq->cryptlen, 0,
  4982. cmdlistinfo);
  4983. } else {
  4984. rc = _ce_setup_cipher_direct(pce_dev, c_req, areq->cryptlen, 0);
  4985. }
  4986. if (rc < 0)
  4987. goto bad;
  4988. preq_info->mode = c_req->mode;
  4989. preq_info->offload_op = c_req->offload_op;
  4990. /* setup for client callback, and issue command to BAM */
  4991. preq_info->areq = areq;
  4992. preq_info->qce_cb = c_req->qce_cb;
  4993. /* setup xfer type for producer callback handling */
  4994. preq_info->xfer_type = QCE_XFER_CIPHERING;
  4995. preq_info->req_len = areq->cryptlen;
  4996. _qce_sps_iovec_count_init(pce_dev, req_info);
  4997. if (pce_dev->support_cmd_dscr && cmdlistinfo) {
  4998. rc = _qce_sps_add_cmd(pce_dev, SPS_IOVEC_FLAG_LOCK,
  4999. cmdlistinfo, &pce_sps_data->in_transfer);
  5000. if (rc)
  5001. goto bad;
  5002. }
  5003. rc = _qce_sps_add_data(areq->src->dma_address, areq->cryptlen,
  5004. &pce_sps_data->in_transfer);
  5005. if (rc)
  5006. goto bad;
  5007. _qce_set_flag(&pce_sps_data->in_transfer,
  5008. SPS_IOVEC_FLAG_EOT|SPS_IOVEC_FLAG_NWD);
  5009. if (pce_dev->no_get_around) {
  5010. rc = _qce_sps_add_cmd(pce_dev, SPS_IOVEC_FLAG_UNLOCK,
  5011. &pce_sps_data->cmdlistptr.unlock_all_pipes,
  5012. &pce_sps_data->in_transfer);
  5013. if (rc)
  5014. goto bad;
  5015. }
  5016. rc = _qce_sps_add_data(areq->dst->dma_address, areq->cryptlen,
  5017. &pce_sps_data->out_transfer);
  5018. if (rc)
  5019. goto bad;
  5020. if (pce_dev->no_get_around || areq->cryptlen <= SPS_MAX_PKT_SIZE) {
  5021. pce_sps_data->producer_state = QCE_PIPE_STATE_COMP;
  5022. if (!is_offload_op(c_req->offload_op)) {
  5023. rc = _qce_sps_add_data(
  5024. GET_PHYS_ADDR(pce_sps_data->result_dump),
  5025. CRYPTO_RESULT_DUMP_SIZE,
  5026. &pce_sps_data->out_transfer);
  5027. if (rc)
  5028. goto bad;
  5029. }
  5030. } else {
  5031. pce_sps_data->producer_state = QCE_PIPE_STATE_IDLE;
  5032. }
  5033. select_mode(pce_dev, preq_info);
  5034. rc = _qce_sps_transfer(pce_dev, req_info);
  5035. cmpxchg(&pce_dev->owner, QCE_OWNER_CLIENT, QCE_OWNER_NONE);
  5036. if (rc)
  5037. goto bad;
  5038. return 0;
  5039. bad:
  5040. if (!is_offload_op(c_req->offload_op)) {
  5041. if (areq->src != areq->dst)
  5042. if (preq_info->dst_nents)
  5043. qce_dma_unmap_sg(pce_dev->pdev, areq->dst,
  5044. preq_info->dst_nents, DMA_FROM_DEVICE);
  5045. if (preq_info->src_nents)
  5046. qce_dma_unmap_sg(pce_dev->pdev, areq->src,
  5047. preq_info->src_nents,
  5048. (areq->src == areq->dst) ?
  5049. DMA_BIDIRECTIONAL : DMA_TO_DEVICE);
  5050. }
  5051. qce_free_req_info(pce_dev, req_info, false);
  5052. return rc;
  5053. }
  5054. EXPORT_SYMBOL(qce_ablk_cipher_req);
  5055. int qce_process_sha_req(void *handle, struct qce_sha_req *sreq)
  5056. {
  5057. struct qce_device *pce_dev = (struct qce_device *) handle;
  5058. int rc;
  5059. struct ahash_request *areq;
  5060. struct qce_cmdlist_info *cmdlistinfo = NULL;
  5061. int req_info = -1;
  5062. struct ce_sps_data *pce_sps_data;
  5063. struct ce_request_info *preq_info;
  5064. bool is_dummy = false;
  5065. if (!sreq) {
  5066. sreq = &(pce_dev->dummyreq.sreq);
  5067. req_info = DUMMY_REQ_INDEX;
  5068. is_dummy = true;
  5069. } else {
  5070. req_info = qce_alloc_req_info(pce_dev);
  5071. if (req_info < 0)
  5072. return -EBUSY;
  5073. }
  5074. sreq->current_req_info = req_info;
  5075. areq = (struct ahash_request *)sreq->areq;
  5076. preq_info = &pce_dev->ce_request_info[req_info];
  5077. pce_sps_data = &preq_info->ce_sps;
  5078. preq_info->src_nents = count_sg(sreq->src, sreq->size);
  5079. qce_dma_map_sg(pce_dev->pdev, sreq->src, preq_info->src_nents,
  5080. DMA_TO_DEVICE);
  5081. if (pce_dev->support_cmd_dscr) {
  5082. cmdlistinfo = _ce_get_hash_cmdlistinfo(pce_dev, req_info, sreq);
  5083. if (cmdlistinfo == NULL) {
  5084. pr_err("Unsupported hash algorithm %d\n", sreq->alg);
  5085. qce_free_req_info(pce_dev, req_info, false);
  5086. return -EINVAL;
  5087. }
  5088. rc = _ce_setup_hash(pce_dev, sreq, cmdlistinfo);
  5089. } else {
  5090. rc = _ce_setup_hash_direct(pce_dev, sreq);
  5091. }
  5092. if (rc < 0)
  5093. goto bad;
  5094. preq_info->areq = areq;
  5095. preq_info->qce_cb = sreq->qce_cb;
  5096. preq_info->offload_op = QCE_OFFLOAD_NONE;
  5097. /* setup xfer type for producer callback handling */
  5098. preq_info->xfer_type = QCE_XFER_HASHING;
  5099. preq_info->req_len = sreq->size;
  5100. _qce_sps_iovec_count_init(pce_dev, req_info);
  5101. if (pce_dev->support_cmd_dscr && cmdlistinfo) {
  5102. rc = _qce_sps_add_cmd(pce_dev, SPS_IOVEC_FLAG_LOCK,
  5103. cmdlistinfo, &pce_sps_data->in_transfer);
  5104. if (rc)
  5105. goto bad;
  5106. }
  5107. rc = _qce_sps_add_sg_data(pce_dev, areq->src, areq->nbytes,
  5108. &pce_sps_data->in_transfer);
  5109. if (rc)
  5110. goto bad;
  5111. /* always ensure there is input data. ZLT does not work for bam-ndp */
  5112. if (!areq->nbytes) {
  5113. rc = _qce_sps_add_data(
  5114. GET_PHYS_ADDR(pce_sps_data->ignore_buffer),
  5115. pce_dev->ce_bam_info.ce_burst_size,
  5116. &pce_sps_data->in_transfer);
  5117. if (rc)
  5118. goto bad;
  5119. }
  5120. _qce_set_flag(&pce_sps_data->in_transfer,
  5121. SPS_IOVEC_FLAG_EOT|SPS_IOVEC_FLAG_NWD);
  5122. if (pce_dev->no_get_around) {
  5123. rc = _qce_sps_add_cmd(pce_dev, SPS_IOVEC_FLAG_UNLOCK,
  5124. &pce_sps_data->cmdlistptr.unlock_all_pipes,
  5125. &pce_sps_data->in_transfer);
  5126. if (rc)
  5127. goto bad;
  5128. }
  5129. rc = _qce_sps_add_data(GET_PHYS_ADDR(pce_sps_data->result_dump),
  5130. CRYPTO_RESULT_DUMP_SIZE,
  5131. &pce_sps_data->out_transfer);
  5132. if (rc)
  5133. goto bad;
  5134. if (is_dummy) {
  5135. _qce_set_flag(&pce_sps_data->out_transfer, SPS_IOVEC_FLAG_INT);
  5136. rc = _qce_sps_transfer(pce_dev, req_info);
  5137. } else {
  5138. select_mode(pce_dev, preq_info);
  5139. rc = _qce_sps_transfer(pce_dev, req_info);
  5140. cmpxchg(&pce_dev->owner, QCE_OWNER_CLIENT, QCE_OWNER_NONE);
  5141. }
  5142. if (rc)
  5143. goto bad;
  5144. return 0;
  5145. bad:
  5146. if (preq_info->src_nents) {
  5147. qce_dma_unmap_sg(pce_dev->pdev, sreq->src,
  5148. preq_info->src_nents, DMA_TO_DEVICE);
  5149. }
  5150. qce_free_req_info(pce_dev, req_info, false);
  5151. return rc;
  5152. }
  5153. EXPORT_SYMBOL(qce_process_sha_req);
  5154. int qce_f8_req(void *handle, struct qce_f8_req *req,
  5155. void *cookie, qce_comp_func_ptr_t qce_cb)
  5156. {
  5157. struct qce_device *pce_dev = (struct qce_device *) handle;
  5158. bool key_stream_mode;
  5159. dma_addr_t dst;
  5160. int rc;
  5161. struct qce_cmdlist_info *cmdlistinfo;
  5162. int req_info = -1;
  5163. struct ce_request_info *preq_info;
  5164. struct ce_sps_data *pce_sps_data;
  5165. req_info = qce_alloc_req_info(pce_dev);
  5166. if (req_info < 0)
  5167. return -EBUSY;
  5168. req->current_req_info = req_info;
  5169. preq_info = &pce_dev->ce_request_info[req_info];
  5170. pce_sps_data = &preq_info->ce_sps;
  5171. switch (req->algorithm) {
  5172. case QCE_OTA_ALGO_KASUMI:
  5173. cmdlistinfo = &pce_sps_data->cmdlistptr.f8_kasumi;
  5174. break;
  5175. case QCE_OTA_ALGO_SNOW3G:
  5176. cmdlistinfo = &pce_sps_data->cmdlistptr.f8_snow3g;
  5177. break;
  5178. default:
  5179. qce_free_req_info(pce_dev, req_info, false);
  5180. return -EINVAL;
  5181. }
  5182. key_stream_mode = (req->data_in == NULL);
  5183. /* don't support key stream mode */
  5184. if (key_stream_mode || (req->bearer >= QCE_OTA_MAX_BEARER)) {
  5185. qce_free_req_info(pce_dev, req_info, false);
  5186. return -EINVAL;
  5187. }
  5188. /* F8 cipher input */
  5189. preq_info->phy_ota_src = dma_map_single(pce_dev->pdev,
  5190. req->data_in, req->data_len,
  5191. (req->data_in == req->data_out) ?
  5192. DMA_BIDIRECTIONAL : DMA_TO_DEVICE);
  5193. /* F8 cipher output */
  5194. if (req->data_in != req->data_out) {
  5195. dst = dma_map_single(pce_dev->pdev, req->data_out,
  5196. req->data_len, DMA_FROM_DEVICE);
  5197. preq_info->phy_ota_dst = dst;
  5198. } else {
  5199. /* in place ciphering */
  5200. dst = preq_info->phy_ota_src;
  5201. preq_info->phy_ota_dst = 0;
  5202. }
  5203. preq_info->ota_size = req->data_len;
  5204. /* set up crypto device */
  5205. if (pce_dev->support_cmd_dscr)
  5206. rc = _ce_f8_setup(pce_dev, req, key_stream_mode, 1, 0,
  5207. req->data_len, cmdlistinfo);
  5208. else
  5209. rc = _ce_f8_setup_direct(pce_dev, req, key_stream_mode, 1, 0,
  5210. req->data_len);
  5211. if (rc < 0)
  5212. goto bad;
  5213. /* setup for callback, and issue command to sps */
  5214. preq_info->areq = cookie;
  5215. preq_info->qce_cb = qce_cb;
  5216. preq_info->offload_op = QCE_OFFLOAD_NONE;
  5217. /* setup xfer type for producer callback handling */
  5218. preq_info->xfer_type = QCE_XFER_F8;
  5219. preq_info->req_len = req->data_len;
  5220. _qce_sps_iovec_count_init(pce_dev, req_info);
  5221. if (pce_dev->support_cmd_dscr) {
  5222. rc = _qce_sps_add_cmd(pce_dev, SPS_IOVEC_FLAG_LOCK,
  5223. cmdlistinfo, &pce_sps_data->in_transfer);
  5224. if (rc)
  5225. goto bad;
  5226. }
  5227. rc = _qce_sps_add_data((uint32_t)preq_info->phy_ota_src, req->data_len,
  5228. &pce_sps_data->in_transfer);
  5229. if (rc)
  5230. goto bad;
  5231. _qce_set_flag(&pce_sps_data->in_transfer,
  5232. SPS_IOVEC_FLAG_EOT|SPS_IOVEC_FLAG_NWD);
  5233. rc = _qce_sps_add_cmd(pce_dev, SPS_IOVEC_FLAG_UNLOCK,
  5234. &pce_sps_data->cmdlistptr.unlock_all_pipes,
  5235. &pce_sps_data->in_transfer);
  5236. if (rc)
  5237. goto bad;
  5238. rc = _qce_sps_add_data((uint32_t)dst, req->data_len,
  5239. &pce_sps_data->out_transfer);
  5240. if (rc)
  5241. goto bad;
  5242. rc = _qce_sps_add_data(GET_PHYS_ADDR(pce_sps_data->result_dump),
  5243. CRYPTO_RESULT_DUMP_SIZE,
  5244. &pce_sps_data->out_transfer);
  5245. if (rc)
  5246. goto bad;
  5247. select_mode(pce_dev, preq_info);
  5248. rc = _qce_sps_transfer(pce_dev, req_info);
  5249. cmpxchg(&pce_dev->owner, QCE_OWNER_CLIENT, QCE_OWNER_NONE);
  5250. if (rc)
  5251. goto bad;
  5252. return 0;
  5253. bad:
  5254. if (preq_info->phy_ota_dst != 0)
  5255. dma_unmap_single(pce_dev->pdev, preq_info->phy_ota_dst,
  5256. req->data_len, DMA_FROM_DEVICE);
  5257. if (preq_info->phy_ota_src != 0)
  5258. dma_unmap_single(pce_dev->pdev, preq_info->phy_ota_src,
  5259. req->data_len,
  5260. (req->data_in == req->data_out) ?
  5261. DMA_BIDIRECTIONAL : DMA_TO_DEVICE);
  5262. qce_free_req_info(pce_dev, req_info, false);
  5263. return rc;
  5264. }
  5265. EXPORT_SYMBOL(qce_f8_req);
  5266. int qce_f8_multi_pkt_req(void *handle, struct qce_f8_multi_pkt_req *mreq,
  5267. void *cookie, qce_comp_func_ptr_t qce_cb)
  5268. {
  5269. struct qce_device *pce_dev = (struct qce_device *) handle;
  5270. uint16_t num_pkt = mreq->num_pkt;
  5271. uint16_t cipher_start = mreq->cipher_start;
  5272. uint16_t cipher_size = mreq->cipher_size;
  5273. struct qce_f8_req *req = &mreq->qce_f8_req;
  5274. uint32_t total;
  5275. dma_addr_t dst = 0;
  5276. int rc = 0;
  5277. struct qce_cmdlist_info *cmdlistinfo;
  5278. int req_info = -1;
  5279. struct ce_request_info *preq_info;
  5280. struct ce_sps_data *pce_sps_data;
  5281. req_info = qce_alloc_req_info(pce_dev);
  5282. if (req_info < 0)
  5283. return -EBUSY;
  5284. req->current_req_info = req_info;
  5285. preq_info = &pce_dev->ce_request_info[req_info];
  5286. pce_sps_data = &preq_info->ce_sps;
  5287. switch (req->algorithm) {
  5288. case QCE_OTA_ALGO_KASUMI:
  5289. cmdlistinfo = &pce_sps_data->cmdlistptr.f8_kasumi;
  5290. break;
  5291. case QCE_OTA_ALGO_SNOW3G:
  5292. cmdlistinfo = &pce_sps_data->cmdlistptr.f8_snow3g;
  5293. break;
  5294. default:
  5295. qce_free_req_info(pce_dev, req_info, false);
  5296. return -EINVAL;
  5297. }
  5298. total = num_pkt * req->data_len;
  5299. /* F8 cipher input */
  5300. preq_info->phy_ota_src = dma_map_single(pce_dev->pdev,
  5301. req->data_in, total,
  5302. (req->data_in == req->data_out) ?
  5303. DMA_BIDIRECTIONAL : DMA_TO_DEVICE);
  5304. /* F8 cipher output */
  5305. if (req->data_in != req->data_out) {
  5306. dst = dma_map_single(pce_dev->pdev, req->data_out, total,
  5307. DMA_FROM_DEVICE);
  5308. preq_info->phy_ota_dst = dst;
  5309. } else {
  5310. /* in place ciphering */
  5311. dst = preq_info->phy_ota_src;
  5312. preq_info->phy_ota_dst = 0;
  5313. }
  5314. preq_info->ota_size = total;
  5315. /* set up crypto device */
  5316. if (pce_dev->support_cmd_dscr)
  5317. rc = _ce_f8_setup(pce_dev, req, false, num_pkt, cipher_start,
  5318. cipher_size, cmdlistinfo);
  5319. else
  5320. rc = _ce_f8_setup_direct(pce_dev, req, false, num_pkt,
  5321. cipher_start, cipher_size);
  5322. if (rc)
  5323. goto bad;
  5324. /* setup for callback, and issue command to sps */
  5325. preq_info->areq = cookie;
  5326. preq_info->qce_cb = qce_cb;
  5327. preq_info->offload_op = QCE_OFFLOAD_NONE;
  5328. /* setup xfer type for producer callback handling */
  5329. preq_info->xfer_type = QCE_XFER_F8;
  5330. preq_info->req_len = total;
  5331. _qce_sps_iovec_count_init(pce_dev, req_info);
  5332. if (pce_dev->support_cmd_dscr) {
  5333. rc = _qce_sps_add_cmd(pce_dev, SPS_IOVEC_FLAG_LOCK,
  5334. cmdlistinfo, &pce_sps_data->in_transfer);
  5335. goto bad;
  5336. }
  5337. rc = _qce_sps_add_data((uint32_t)preq_info->phy_ota_src, total,
  5338. &pce_sps_data->in_transfer);
  5339. if (rc)
  5340. goto bad;
  5341. _qce_set_flag(&pce_sps_data->in_transfer,
  5342. SPS_IOVEC_FLAG_EOT|SPS_IOVEC_FLAG_NWD);
  5343. rc = _qce_sps_add_cmd(pce_dev, SPS_IOVEC_FLAG_UNLOCK,
  5344. &pce_sps_data->cmdlistptr.unlock_all_pipes,
  5345. &pce_sps_data->in_transfer);
  5346. if (rc)
  5347. goto bad;
  5348. rc = _qce_sps_add_data((uint32_t)dst, total,
  5349. &pce_sps_data->out_transfer);
  5350. if (rc)
  5351. goto bad;
  5352. rc = _qce_sps_add_data(GET_PHYS_ADDR(pce_sps_data->result_dump),
  5353. CRYPTO_RESULT_DUMP_SIZE,
  5354. &pce_sps_data->out_transfer);
  5355. if (rc)
  5356. goto bad;
  5357. select_mode(pce_dev, preq_info);
  5358. rc = _qce_sps_transfer(pce_dev, req_info);
  5359. cmpxchg(&pce_dev->owner, QCE_OWNER_CLIENT, QCE_OWNER_NONE);
  5360. if (rc == 0)
  5361. return 0;
  5362. bad:
  5363. if (preq_info->phy_ota_dst)
  5364. dma_unmap_single(pce_dev->pdev, preq_info->phy_ota_dst, total,
  5365. DMA_FROM_DEVICE);
  5366. dma_unmap_single(pce_dev->pdev, preq_info->phy_ota_src, total,
  5367. (req->data_in == req->data_out) ?
  5368. DMA_BIDIRECTIONAL : DMA_TO_DEVICE);
  5369. qce_free_req_info(pce_dev, req_info, false);
  5370. return rc;
  5371. }
  5372. EXPORT_SYMBOL(qce_f8_multi_pkt_req);
  5373. int qce_f9_req(void *handle, struct qce_f9_req *req, void *cookie,
  5374. qce_comp_func_ptr_t qce_cb)
  5375. {
  5376. struct qce_device *pce_dev = (struct qce_device *) handle;
  5377. int rc;
  5378. struct qce_cmdlist_info *cmdlistinfo;
  5379. int req_info = -1;
  5380. struct ce_sps_data *pce_sps_data;
  5381. struct ce_request_info *preq_info;
  5382. req_info = qce_alloc_req_info(pce_dev);
  5383. if (req_info < 0)
  5384. return -EBUSY;
  5385. req->current_req_info = req_info;
  5386. preq_info = &pce_dev->ce_request_info[req_info];
  5387. pce_sps_data = &preq_info->ce_sps;
  5388. switch (req->algorithm) {
  5389. case QCE_OTA_ALGO_KASUMI:
  5390. cmdlistinfo = &pce_sps_data->cmdlistptr.f9_kasumi;
  5391. break;
  5392. case QCE_OTA_ALGO_SNOW3G:
  5393. cmdlistinfo = &pce_sps_data->cmdlistptr.f9_snow3g;
  5394. break;
  5395. default:
  5396. qce_free_req_info(pce_dev, req_info, false);
  5397. return -EINVAL;
  5398. }
  5399. preq_info->phy_ota_src = dma_map_single(pce_dev->pdev, req->message,
  5400. req->msize, DMA_TO_DEVICE);
  5401. preq_info->ota_size = req->msize;
  5402. if (pce_dev->support_cmd_dscr)
  5403. rc = _ce_f9_setup(pce_dev, req, cmdlistinfo);
  5404. else
  5405. rc = _ce_f9_setup_direct(pce_dev, req);
  5406. if (rc < 0)
  5407. goto bad;
  5408. /* setup for callback, and issue command to sps */
  5409. preq_info->areq = cookie;
  5410. preq_info->qce_cb = qce_cb;
  5411. preq_info->offload_op = QCE_OFFLOAD_NONE;
  5412. /* setup xfer type for producer callback handling */
  5413. preq_info->xfer_type = QCE_XFER_F9;
  5414. preq_info->req_len = req->msize;
  5415. _qce_sps_iovec_count_init(pce_dev, req_info);
  5416. if (pce_dev->support_cmd_dscr) {
  5417. rc = _qce_sps_add_cmd(pce_dev, SPS_IOVEC_FLAG_LOCK,
  5418. cmdlistinfo, &pce_sps_data->in_transfer);
  5419. if (rc)
  5420. goto bad;
  5421. }
  5422. rc = _qce_sps_add_data((uint32_t)preq_info->phy_ota_src, req->msize,
  5423. &pce_sps_data->in_transfer);
  5424. if (rc)
  5425. goto bad;
  5426. _qce_set_flag(&pce_sps_data->in_transfer,
  5427. SPS_IOVEC_FLAG_EOT|SPS_IOVEC_FLAG_NWD);
  5428. rc = _qce_sps_add_cmd(pce_dev, SPS_IOVEC_FLAG_UNLOCK,
  5429. &pce_sps_data->cmdlistptr.unlock_all_pipes,
  5430. &pce_sps_data->in_transfer);
  5431. if (rc)
  5432. goto bad;
  5433. rc = _qce_sps_add_data(GET_PHYS_ADDR(pce_sps_data->result_dump),
  5434. CRYPTO_RESULT_DUMP_SIZE,
  5435. &pce_sps_data->out_transfer);
  5436. if (rc)
  5437. goto bad;
  5438. select_mode(pce_dev, preq_info);
  5439. rc = _qce_sps_transfer(pce_dev, req_info);
  5440. cmpxchg(&pce_dev->owner, QCE_OWNER_CLIENT, QCE_OWNER_NONE);
  5441. if (rc)
  5442. goto bad;
  5443. return 0;
  5444. bad:
  5445. dma_unmap_single(pce_dev->pdev, preq_info->phy_ota_src,
  5446. req->msize, DMA_TO_DEVICE);
  5447. qce_free_req_info(pce_dev, req_info, false);
  5448. return rc;
  5449. }
  5450. EXPORT_SYMBOL(qce_f9_req);
  5451. static int __qce_get_device_tree_data(struct platform_device *pdev,
  5452. struct qce_device *pce_dev)
  5453. {
  5454. struct resource *resource;
  5455. int rc = 0, i = 0;
  5456. pce_dev->is_shared = of_property_read_bool((&pdev->dev)->of_node,
  5457. "qcom,ce-hw-shared");
  5458. pce_dev->support_hw_key = of_property_read_bool((&pdev->dev)->of_node,
  5459. "qcom,ce-hw-key");
  5460. pce_dev->use_sw_aes_cbc_ecb_ctr_algo =
  5461. of_property_read_bool((&pdev->dev)->of_node,
  5462. "qcom,use-sw-aes-cbc-ecb-ctr-algo");
  5463. pce_dev->use_sw_aead_algo =
  5464. of_property_read_bool((&pdev->dev)->of_node,
  5465. "qcom,use-sw-aead-algo");
  5466. pce_dev->use_sw_aes_xts_algo =
  5467. of_property_read_bool((&pdev->dev)->of_node,
  5468. "qcom,use-sw-aes-xts-algo");
  5469. pce_dev->use_sw_ahash_algo =
  5470. of_property_read_bool((&pdev->dev)->of_node,
  5471. "qcom,use-sw-ahash-algo");
  5472. pce_dev->use_sw_hmac_algo =
  5473. of_property_read_bool((&pdev->dev)->of_node,
  5474. "qcom,use-sw-hmac-algo");
  5475. pce_dev->use_sw_aes_ccm_algo =
  5476. of_property_read_bool((&pdev->dev)->of_node,
  5477. "qcom,use-sw-aes-ccm-algo");
  5478. pce_dev->support_clk_mgmt_sus_res = of_property_read_bool(
  5479. (&pdev->dev)->of_node, "qcom,clk-mgmt-sus-res");
  5480. pce_dev->support_only_core_src_clk = of_property_read_bool(
  5481. (&pdev->dev)->of_node, "qcom,support-core-clk-only");
  5482. pce_dev->request_bw_before_clk = of_property_read_bool(
  5483. (&pdev->dev)->of_node, "qcom,request-bw-before-clk");
  5484. pce_dev->kernel_pipes_support = true;
  5485. if (of_property_read_u32((&pdev->dev)->of_node,
  5486. "qcom,bam-pipe-pair",
  5487. &pce_dev->ce_bam_info.pipe_pair_index[QCE_OFFLOAD_NONE])) {
  5488. pr_warn("Kernel pipes not supported.\n");
  5489. //Unused pipe, just as failsafe.
  5490. pce_dev->ce_bam_info.pipe_pair_index[QCE_OFFLOAD_NONE] = 2;
  5491. pce_dev->kernel_pipes_support = false;
  5492. }
  5493. if (of_property_read_bool((&pdev->dev)->of_node,
  5494. "qcom,offload-ops-support")) {
  5495. pce_dev->offload_pipes_support = true;
  5496. if (of_property_read_u32((&pdev->dev)->of_node,
  5497. "qcom,bam-pipe-offload-cpb-hlos",
  5498. &pce_dev->ce_bam_info.pipe_pair_index[QCE_OFFLOAD_CPB_HLOS])) {
  5499. pr_err("Fail to get bam offload cpb-hlos pipe pair info.\n");
  5500. return -EINVAL;
  5501. }
  5502. if (of_property_read_u32((&pdev->dev)->of_node,
  5503. "qcom,bam-pipe-offload-hlos-hlos",
  5504. &pce_dev->ce_bam_info.pipe_pair_index[QCE_OFFLOAD_HLOS_HLOS])) {
  5505. pr_err("Fail to get bam offload hlos-hlos info.\n");
  5506. return -EINVAL;
  5507. }
  5508. if (of_property_read_u32((&pdev->dev)->of_node,
  5509. "qcom,bam-pipe-offload-hlos-cpb",
  5510. &pce_dev->ce_bam_info.pipe_pair_index[QCE_OFFLOAD_HLOS_CPB])) {
  5511. pr_err("Fail to get bam offload hlos-cpb info\n");
  5512. return -EINVAL;
  5513. }
  5514. }
  5515. if (of_property_read_u32((&pdev->dev)->of_node,
  5516. "qcom,ce-device",
  5517. &pce_dev->ce_bam_info.ce_device)) {
  5518. pr_err("Fail to get CE device information.\n");
  5519. return -EINVAL;
  5520. }
  5521. if (of_property_read_u32((&pdev->dev)->of_node,
  5522. "qcom,ce-hw-instance",
  5523. &pce_dev->ce_bam_info.ce_hw_instance)) {
  5524. pr_err("Fail to get CE hw instance information.\n");
  5525. return -EINVAL;
  5526. }
  5527. if (of_property_read_u32((&pdev->dev)->of_node,
  5528. "qcom,bam-ee",
  5529. &pce_dev->ce_bam_info.bam_ee)) {
  5530. pr_info("BAM Apps EE is not defined, setting to default 1\n");
  5531. pce_dev->ce_bam_info.bam_ee = 1;
  5532. }
  5533. if (of_property_read_u32((&pdev->dev)->of_node,
  5534. "qcom,ce-opp-freq",
  5535. &pce_dev->ce_opp_freq_hz)) {
  5536. pr_info("CE operating frequency is not defined, setting to default 100MHZ\n");
  5537. pce_dev->ce_opp_freq_hz = CE_CLK_100MHZ;
  5538. }
  5539. if (of_property_read_bool((&pdev->dev)->of_node, "qcom,smmu-s1-enable"))
  5540. pce_dev->enable_s1_smmu = true;
  5541. pce_dev->no_clock_support = of_property_read_bool((&pdev->dev)->of_node,
  5542. "qcom,no-clock-support");
  5543. for (i = 0; i < QCE_OFFLOAD_OPER_LAST; i++) {
  5544. /* Source/destination pipes for all usecases */
  5545. pce_dev->ce_bam_info.dest_pipe_index[i] =
  5546. 2 * pce_dev->ce_bam_info.pipe_pair_index[i];
  5547. pce_dev->ce_bam_info.src_pipe_index[i] =
  5548. pce_dev->ce_bam_info.dest_pipe_index[i] + 1;
  5549. }
  5550. resource = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  5551. "crypto-base");
  5552. if (resource) {
  5553. pce_dev->phy_iobase = resource->start;
  5554. pce_dev->iobase = ioremap(resource->start,
  5555. resource_size(resource));
  5556. if (!pce_dev->iobase) {
  5557. pr_err("Can not map CRYPTO io memory\n");
  5558. return -ENOMEM;
  5559. }
  5560. } else {
  5561. pr_err("CRYPTO HW mem unavailable.\n");
  5562. return -ENODEV;
  5563. }
  5564. resource = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  5565. "crypto-bam-base");
  5566. if (resource) {
  5567. pce_dev->bam_mem = resource->start;
  5568. pce_dev->bam_mem_size = resource_size(resource);
  5569. } else {
  5570. pr_err("CRYPTO BAM mem unavailable.\n");
  5571. rc = -ENODEV;
  5572. goto err_getting_bam_info;
  5573. }
  5574. pce_dev->ce_bam_info.bam_irq = platform_get_irq(pdev,0);
  5575. if (pce_dev->ce_bam_info.bam_irq < 0) {
  5576. pr_err("CRYPTO BAM IRQ unavailable.\n");
  5577. goto err_dev;
  5578. }
  5579. return rc;
  5580. err_dev:
  5581. if (pce_dev->ce_bam_info.bam_iobase)
  5582. iounmap(pce_dev->ce_bam_info.bam_iobase);
  5583. err_getting_bam_info:
  5584. if (pce_dev->iobase)
  5585. iounmap(pce_dev->iobase);
  5586. return rc;
  5587. }
  5588. static int __qce_init_clk(struct qce_device *pce_dev)
  5589. {
  5590. int rc = 0;
  5591. if (pce_dev->no_clock_support) {
  5592. pr_debug("No clock support defined in dts\n");
  5593. return rc;
  5594. }
  5595. pce_dev->ce_core_src_clk = clk_get(pce_dev->pdev, "core_clk_src");
  5596. if (!IS_ERR(pce_dev->ce_core_src_clk)) {
  5597. if (pce_dev->request_bw_before_clk)
  5598. goto skip_set_rate;
  5599. rc = clk_set_rate(pce_dev->ce_core_src_clk,
  5600. pce_dev->ce_opp_freq_hz);
  5601. if (rc) {
  5602. pr_err("Unable to set the core src clk @%uMhz.\n",
  5603. pce_dev->ce_opp_freq_hz/CE_CLK_DIV);
  5604. goto exit_put_core_src_clk;
  5605. }
  5606. } else {
  5607. if (pce_dev->support_only_core_src_clk) {
  5608. rc = PTR_ERR(pce_dev->ce_core_src_clk);
  5609. pce_dev->ce_core_src_clk = NULL;
  5610. pr_err("Unable to get CE core src clk\n");
  5611. return rc;
  5612. }
  5613. pr_warn("Unable to get CE core src clk, set to NULL\n");
  5614. pce_dev->ce_core_src_clk = NULL;
  5615. }
  5616. skip_set_rate:
  5617. if (pce_dev->support_only_core_src_clk) {
  5618. pce_dev->ce_core_clk = NULL;
  5619. pce_dev->ce_clk = NULL;
  5620. pce_dev->ce_bus_clk = NULL;
  5621. } else {
  5622. pce_dev->ce_core_clk = clk_get(pce_dev->pdev, "core_clk");
  5623. if (IS_ERR(pce_dev->ce_core_clk)) {
  5624. rc = PTR_ERR(pce_dev->ce_core_clk);
  5625. pr_err("Unable to get CE core clk\n");
  5626. goto exit_put_core_src_clk;
  5627. }
  5628. pce_dev->ce_clk = clk_get(pce_dev->pdev, "iface_clk");
  5629. if (IS_ERR(pce_dev->ce_clk)) {
  5630. rc = PTR_ERR(pce_dev->ce_clk);
  5631. pr_err("Unable to get CE interface clk\n");
  5632. goto exit_put_core_clk;
  5633. }
  5634. pce_dev->ce_bus_clk = clk_get(pce_dev->pdev, "bus_clk");
  5635. if (IS_ERR(pce_dev->ce_bus_clk)) {
  5636. rc = PTR_ERR(pce_dev->ce_bus_clk);
  5637. pr_err("Unable to get CE BUS interface clk\n");
  5638. goto exit_put_iface_clk;
  5639. }
  5640. }
  5641. return rc;
  5642. exit_put_iface_clk:
  5643. if (pce_dev->ce_clk)
  5644. clk_put(pce_dev->ce_clk);
  5645. exit_put_core_clk:
  5646. if (pce_dev->ce_core_clk)
  5647. clk_put(pce_dev->ce_core_clk);
  5648. exit_put_core_src_clk:
  5649. if (pce_dev->ce_core_src_clk)
  5650. clk_put(pce_dev->ce_core_src_clk);
  5651. pr_err("Unable to init CE clks, rc = %d\n", rc);
  5652. return rc;
  5653. }
  5654. static void __qce_deinit_clk(struct qce_device *pce_dev)
  5655. {
  5656. if (pce_dev->no_clock_support) {
  5657. pr_debug("No clock support defined in dts\n");
  5658. return;
  5659. }
  5660. if (pce_dev->ce_bus_clk)
  5661. clk_put(pce_dev->ce_bus_clk);
  5662. if (pce_dev->ce_clk)
  5663. clk_put(pce_dev->ce_clk);
  5664. if (pce_dev->ce_core_clk)
  5665. clk_put(pce_dev->ce_core_clk);
  5666. if (pce_dev->ce_core_src_clk)
  5667. clk_put(pce_dev->ce_core_src_clk);
  5668. }
  5669. int qce_enable_clk(void *handle)
  5670. {
  5671. struct qce_device *pce_dev = (struct qce_device *)handle;
  5672. int rc = 0;
  5673. if (pce_dev->no_clock_support) {
  5674. pr_debug("No clock support defined in dts\n");
  5675. return rc;
  5676. }
  5677. if (pce_dev->ce_core_src_clk) {
  5678. rc = clk_prepare_enable(pce_dev->ce_core_src_clk);
  5679. if (rc) {
  5680. pr_err("Unable to enable/prepare CE core src clk\n");
  5681. return rc;
  5682. }
  5683. }
  5684. if (pce_dev->support_only_core_src_clk)
  5685. return rc;
  5686. if (pce_dev->ce_core_clk) {
  5687. rc = clk_prepare_enable(pce_dev->ce_core_clk);
  5688. if (rc) {
  5689. pr_err("Unable to enable/prepare CE core clk\n");
  5690. goto exit_disable_core_src_clk;
  5691. }
  5692. }
  5693. if (pce_dev->ce_clk) {
  5694. rc = clk_prepare_enable(pce_dev->ce_clk);
  5695. if (rc) {
  5696. pr_err("Unable to enable/prepare CE iface clk\n");
  5697. goto exit_disable_core_clk;
  5698. }
  5699. }
  5700. if (pce_dev->ce_bus_clk) {
  5701. rc = clk_prepare_enable(pce_dev->ce_bus_clk);
  5702. if (rc) {
  5703. pr_err("Unable to enable/prepare CE BUS clk\n");
  5704. goto exit_disable_ce_clk;
  5705. }
  5706. }
  5707. return rc;
  5708. exit_disable_ce_clk:
  5709. if (pce_dev->ce_clk)
  5710. clk_disable_unprepare(pce_dev->ce_clk);
  5711. exit_disable_core_clk:
  5712. if (pce_dev->ce_core_clk)
  5713. clk_disable_unprepare(pce_dev->ce_core_clk);
  5714. exit_disable_core_src_clk:
  5715. if (pce_dev->ce_core_src_clk)
  5716. clk_disable_unprepare(pce_dev->ce_core_src_clk);
  5717. return rc;
  5718. }
  5719. EXPORT_SYMBOL(qce_enable_clk);
  5720. int qce_disable_clk(void *handle)
  5721. {
  5722. struct qce_device *pce_dev = (struct qce_device *) handle;
  5723. if (pce_dev->no_clock_support) {
  5724. pr_debug("No clock support defined in dts\n");
  5725. return 0;
  5726. }
  5727. if (pce_dev->ce_bus_clk)
  5728. clk_disable_unprepare(pce_dev->ce_bus_clk);
  5729. if (pce_dev->ce_clk)
  5730. clk_disable_unprepare(pce_dev->ce_clk);
  5731. if (pce_dev->ce_core_clk)
  5732. clk_disable_unprepare(pce_dev->ce_core_clk);
  5733. if (pce_dev->ce_core_src_clk)
  5734. clk_disable_unprepare(pce_dev->ce_core_src_clk);
  5735. return 0;
  5736. }
  5737. EXPORT_SYMBOL(qce_disable_clk);
  5738. /* dummy req setup */
  5739. static int setup_dummy_req(struct qce_device *pce_dev)
  5740. {
  5741. char *input =
  5742. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopqopqrpqrs";
  5743. int len = DUMMY_REQ_DATA_LEN;
  5744. memcpy(pce_dev->dummyreq_in_buf, input, len);
  5745. sg_init_one(&pce_dev->dummyreq.sg, pce_dev->dummyreq_in_buf, len);
  5746. pce_dev->dummyreq.sreq.alg = QCE_HASH_SHA1;
  5747. pce_dev->dummyreq.sreq.qce_cb = qce_dummy_complete;
  5748. pce_dev->dummyreq.sreq.src = &pce_dev->dummyreq.sg;
  5749. pce_dev->dummyreq.sreq.auth_data[0] = 0;
  5750. pce_dev->dummyreq.sreq.auth_data[1] = 0;
  5751. pce_dev->dummyreq.sreq.auth_data[2] = 0;
  5752. pce_dev->dummyreq.sreq.auth_data[3] = 0;
  5753. pce_dev->dummyreq.sreq.first_blk = true;
  5754. pce_dev->dummyreq.sreq.last_blk = true;
  5755. pce_dev->dummyreq.sreq.size = len;
  5756. pce_dev->dummyreq.sreq.areq = &pce_dev->dummyreq.areq;
  5757. pce_dev->dummyreq.sreq.flags = 0;
  5758. pce_dev->dummyreq.sreq.authkey = NULL;
  5759. pce_dev->dummyreq.areq.src = pce_dev->dummyreq.sreq.src;
  5760. pce_dev->dummyreq.areq.nbytes = pce_dev->dummyreq.sreq.size;
  5761. return 0;
  5762. }
  5763. static int qce_smmu_init(struct qce_device *pce_dev)
  5764. {
  5765. struct device *dev = pce_dev->pdev;
  5766. if (!dev->dma_parms) {
  5767. dev->dma_parms = devm_kzalloc(dev,
  5768. sizeof(*dev->dma_parms), GFP_KERNEL);
  5769. if (!dev->dma_parms)
  5770. return -ENOMEM;
  5771. }
  5772. dma_set_max_seg_size(dev, DMA_BIT_MASK(32));
  5773. dma_set_seg_boundary(dev, (unsigned long)DMA_BIT_MASK(64));
  5774. return 0;
  5775. }
  5776. /* crypto engine open function. */
  5777. void *qce_open(struct platform_device *pdev, int *rc)
  5778. {
  5779. struct qce_device *pce_dev;
  5780. int i;
  5781. static int pcedev_no = 1;
  5782. pce_dev = kzalloc(sizeof(struct qce_device), GFP_KERNEL);
  5783. if (!pce_dev) {
  5784. *rc = -ENOMEM;
  5785. pr_err("Can not allocate memory: %d\n", *rc);
  5786. return NULL;
  5787. }
  5788. pce_dev->pdev = &pdev->dev;
  5789. mutex_lock(&qce_iomap_mutex);
  5790. if (pdev->dev.of_node) {
  5791. *rc = __qce_get_device_tree_data(pdev, pce_dev);
  5792. if (*rc)
  5793. goto err_pce_dev;
  5794. } else {
  5795. *rc = -EINVAL;
  5796. pr_err("Device Node not found.\n");
  5797. goto err_pce_dev;
  5798. }
  5799. if (pce_dev->enable_s1_smmu) {
  5800. if (qce_smmu_init(pce_dev)) {
  5801. *rc = -EIO;
  5802. goto err_pce_dev;
  5803. }
  5804. }
  5805. for (i = 0; i < MAX_QCE_ALLOC_BAM_REQ; i++)
  5806. atomic_set(&pce_dev->ce_request_info[i].in_use, false);
  5807. pce_dev->ce_request_index = 0;
  5808. pce_dev->memsize = 10 * PAGE_SIZE * MAX_QCE_ALLOC_BAM_REQ;
  5809. pce_dev->coh_vmem = dma_alloc_coherent(pce_dev->pdev,
  5810. pce_dev->memsize, &pce_dev->coh_pmem, GFP_KERNEL);
  5811. if (pce_dev->coh_vmem == NULL) {
  5812. *rc = -ENOMEM;
  5813. pr_err("Can not allocate coherent memory for sps data\n");
  5814. goto err_iobase;
  5815. }
  5816. pce_dev->iovec_memsize = TOTAL_IOVEC_SPACE_PER_PIPE *
  5817. MAX_QCE_ALLOC_BAM_REQ * 2;
  5818. pce_dev->iovec_vmem = kzalloc(pce_dev->iovec_memsize, GFP_KERNEL);
  5819. if (pce_dev->iovec_vmem == NULL)
  5820. goto err_mem;
  5821. pce_dev->dummyreq_in_buf = kzalloc(DUMMY_REQ_DATA_LEN, GFP_KERNEL);
  5822. if (pce_dev->dummyreq_in_buf == NULL)
  5823. goto err_mem;
  5824. *rc = __qce_init_clk(pce_dev);
  5825. if (*rc)
  5826. goto err_mem;
  5827. *rc = qce_enable_clk(pce_dev);
  5828. if (*rc)
  5829. goto err_enable_clk;
  5830. if (_probe_ce_engine(pce_dev)) {
  5831. *rc = -ENXIO;
  5832. goto err;
  5833. }
  5834. *rc = 0;
  5835. qce_init_ce_cfg_val(pce_dev);
  5836. *rc = qce_sps_init(pce_dev);
  5837. if (*rc)
  5838. goto err;
  5839. qce_setup_ce_sps_data(pce_dev);
  5840. qce_disable_clk(pce_dev);
  5841. setup_dummy_req(pce_dev);
  5842. atomic_set(&pce_dev->no_of_queued_req, 0);
  5843. pce_dev->mode = IN_INTERRUPT_MODE;
  5844. timer_setup(&(pce_dev->timer), qce_multireq_timeout, 0);
  5845. //pce_dev->timer.function = qce_multireq_timeout;
  5846. //pce_dev->timer.data = (unsigned long)pce_dev;
  5847. pce_dev->timer.expires = jiffies + DELAY_IN_JIFFIES;
  5848. pce_dev->intr_cadence = 0;
  5849. pce_dev->dev_no = pcedev_no;
  5850. pcedev_no++;
  5851. pce_dev->owner = QCE_OWNER_NONE;
  5852. qce_enable_clock_gating(pce_dev);
  5853. mutex_unlock(&qce_iomap_mutex);
  5854. return pce_dev;
  5855. err:
  5856. qce_disable_clk(pce_dev);
  5857. err_enable_clk:
  5858. __qce_deinit_clk(pce_dev);
  5859. err_mem:
  5860. kfree(pce_dev->dummyreq_in_buf);
  5861. kfree(pce_dev->iovec_vmem);
  5862. if (pce_dev->coh_vmem)
  5863. dma_free_coherent(pce_dev->pdev, pce_dev->memsize,
  5864. pce_dev->coh_vmem, pce_dev->coh_pmem);
  5865. err_iobase:
  5866. if (pce_dev->iobase)
  5867. iounmap(pce_dev->iobase);
  5868. err_pce_dev:
  5869. mutex_unlock(&qce_iomap_mutex);
  5870. kfree(pce_dev);
  5871. return NULL;
  5872. }
  5873. EXPORT_SYMBOL(qce_open);
  5874. /* crypto engine close function. */
  5875. int qce_close(void *handle)
  5876. {
  5877. struct qce_device *pce_dev = (struct qce_device *) handle;
  5878. if (handle == NULL)
  5879. return -ENODEV;
  5880. mutex_lock(&qce_iomap_mutex);
  5881. qce_enable_clk(pce_dev);
  5882. qce_sps_exit(pce_dev);
  5883. if (pce_dev->iobase)
  5884. iounmap(pce_dev->iobase);
  5885. if (pce_dev->coh_vmem)
  5886. dma_free_coherent(pce_dev->pdev, pce_dev->memsize,
  5887. pce_dev->coh_vmem, pce_dev->coh_pmem);
  5888. kfree(pce_dev->dummyreq_in_buf);
  5889. kfree(pce_dev->iovec_vmem);
  5890. qce_disable_clk(pce_dev);
  5891. __qce_deinit_clk(pce_dev);
  5892. mutex_unlock(&qce_iomap_mutex);
  5893. kfree(handle);
  5894. return 0;
  5895. }
  5896. EXPORT_SYMBOL(qce_close);
  5897. #define OTA_SUPPORT_MASK (1 << CRYPTO_ENCR_SNOW3G_SEL |\
  5898. 1 << CRYPTO_ENCR_KASUMI_SEL |\
  5899. 1 << CRYPTO_AUTH_SNOW3G_SEL |\
  5900. 1 << CRYPTO_AUTH_KASUMI_SEL)
  5901. int qce_hw_support(void *handle, struct ce_hw_support *ce_support)
  5902. {
  5903. struct qce_device *pce_dev = (struct qce_device *)handle;
  5904. if (ce_support == NULL)
  5905. return -EINVAL;
  5906. ce_support->sha1_hmac_20 = false;
  5907. ce_support->sha1_hmac = false;
  5908. ce_support->sha256_hmac = false;
  5909. ce_support->sha_hmac = true;
  5910. ce_support->cmac = true;
  5911. ce_support->aes_key_192 = false;
  5912. ce_support->aes_xts = true;
  5913. if ((pce_dev->engines_avail & OTA_SUPPORT_MASK) == OTA_SUPPORT_MASK)
  5914. ce_support->ota = true;
  5915. else
  5916. ce_support->ota = false;
  5917. ce_support->bam = true;
  5918. ce_support->is_shared = (pce_dev->is_shared == 1) ? true : false;
  5919. ce_support->hw_key = pce_dev->support_hw_key;
  5920. ce_support->aes_ccm = true;
  5921. ce_support->clk_mgmt_sus_res = pce_dev->support_clk_mgmt_sus_res;
  5922. ce_support->req_bw_before_clk = pce_dev->request_bw_before_clk;
  5923. if (pce_dev->ce_bam_info.minor_version)
  5924. ce_support->aligned_only = false;
  5925. else
  5926. ce_support->aligned_only = true;
  5927. ce_support->use_sw_aes_cbc_ecb_ctr_algo =
  5928. pce_dev->use_sw_aes_cbc_ecb_ctr_algo;
  5929. ce_support->use_sw_aead_algo =
  5930. pce_dev->use_sw_aead_algo;
  5931. ce_support->use_sw_aes_xts_algo =
  5932. pce_dev->use_sw_aes_xts_algo;
  5933. ce_support->use_sw_ahash_algo =
  5934. pce_dev->use_sw_ahash_algo;
  5935. ce_support->use_sw_hmac_algo =
  5936. pce_dev->use_sw_hmac_algo;
  5937. ce_support->use_sw_aes_ccm_algo =
  5938. pce_dev->use_sw_aes_ccm_algo;
  5939. ce_support->ce_device = pce_dev->ce_bam_info.ce_device;
  5940. ce_support->ce_hw_instance = pce_dev->ce_bam_info.ce_hw_instance;
  5941. if (pce_dev->no_get_around)
  5942. ce_support->max_request = MAX_QCE_BAM_REQ;
  5943. else
  5944. ce_support->max_request = 1;
  5945. return 0;
  5946. }
  5947. EXPORT_SYMBOL(qce_hw_support);
  5948. void qce_dump_req(void *handle)
  5949. {
  5950. int i;
  5951. bool req_in_use;
  5952. struct qce_device *pce_dev = (struct qce_device *)handle;
  5953. for (i = 0; i < MAX_QCE_BAM_REQ; i++) {
  5954. req_in_use = atomic_read(&pce_dev->ce_request_info[i].in_use);
  5955. pr_info("%s: %d %d\n", __func__, i, req_in_use);
  5956. if (req_in_use)
  5957. _qce_dump_descr_fifos(pce_dev, i);
  5958. }
  5959. }
  5960. EXPORT_SYMBOL(qce_dump_req);
  5961. MODULE_LICENSE("GPL v2");
  5962. MODULE_DESCRIPTION("Crypto Engine driver");