qce50.c 195 KB

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