qce50.c 179 KB

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