12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471 |
- // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
- /*
- * Copyright 2015-2016 Freescale Semiconductor Inc.
- * Copyright 2017-2019 NXP
- */
- #include "compat.h"
- #include "regs.h"
- #include "caamalg_qi2.h"
- #include "dpseci_cmd.h"
- #include "desc_constr.h"
- #include "error.h"
- #include "sg_sw_sec4.h"
- #include "sg_sw_qm2.h"
- #include "key_gen.h"
- #include "caamalg_desc.h"
- #include "caamhash_desc.h"
- #include "dpseci-debugfs.h"
- #include <linux/fsl/mc.h>
- #include <soc/fsl/dpaa2-io.h>
- #include <soc/fsl/dpaa2-fd.h>
- #define CAAM_CRA_PRIORITY 2000
- /* max key is sum of AES_MAX_KEY_SIZE, max split key size */
- #define CAAM_MAX_KEY_SIZE (AES_MAX_KEY_SIZE + CTR_RFC3686_NONCE_SIZE + \
- SHA512_DIGEST_SIZE * 2)
- /*
- * This is a a cache of buffers, from which the users of CAAM QI driver
- * can allocate short buffers. It's speedier than doing kmalloc on the hotpath.
- * NOTE: A more elegant solution would be to have some headroom in the frames
- * being processed. This can be added by the dpaa2-eth driver. This would
- * pose a problem for userspace application processing which cannot
- * know of this limitation. So for now, this will work.
- * NOTE: The memcache is SMP-safe. No need to handle spinlocks in-here
- */
- static struct kmem_cache *qi_cache;
- struct caam_alg_entry {
- struct device *dev;
- int class1_alg_type;
- int class2_alg_type;
- bool rfc3686;
- bool geniv;
- bool nodkp;
- };
- struct caam_aead_alg {
- struct aead_alg aead;
- struct caam_alg_entry caam;
- bool registered;
- };
- struct caam_skcipher_alg {
- struct skcipher_alg skcipher;
- struct caam_alg_entry caam;
- bool registered;
- };
- /**
- * caam_ctx - per-session context
- * @flc: Flow Contexts array
- * @key: [authentication key], encryption key
- * @flc_dma: I/O virtual addresses of the Flow Contexts
- * @key_dma: I/O virtual address of the key
- * @dir: DMA direction for mapping key and Flow Contexts
- * @dev: dpseci device
- * @adata: authentication algorithm details
- * @cdata: encryption algorithm details
- * @authsize: authentication tag (a.k.a. ICV / MAC) size
- */
- struct caam_ctx {
- struct caam_flc flc[NUM_OP];
- u8 key[CAAM_MAX_KEY_SIZE];
- dma_addr_t flc_dma[NUM_OP];
- dma_addr_t key_dma;
- enum dma_data_direction dir;
- struct device *dev;
- struct alginfo adata;
- struct alginfo cdata;
- unsigned int authsize;
- };
- static void *dpaa2_caam_iova_to_virt(struct dpaa2_caam_priv *priv,
- dma_addr_t iova_addr)
- {
- phys_addr_t phys_addr;
- phys_addr = priv->domain ? iommu_iova_to_phys(priv->domain, iova_addr) :
- iova_addr;
- return phys_to_virt(phys_addr);
- }
- /*
- * qi_cache_zalloc - Allocate buffers from CAAM-QI cache
- *
- * Allocate data on the hotpath. Instead of using kzalloc, one can use the
- * services of the CAAM QI memory cache (backed by kmem_cache). The buffers
- * will have a size of CAAM_QI_MEMCACHE_SIZE, which should be sufficient for
- * hosting 16 SG entries.
- *
- * @flags - flags that would be used for the equivalent kmalloc(..) call
- *
- * Returns a pointer to a retrieved buffer on success or NULL on failure.
- */
- static inline void *qi_cache_zalloc(gfp_t flags)
- {
- return kmem_cache_zalloc(qi_cache, flags);
- }
- /*
- * qi_cache_free - Frees buffers allocated from CAAM-QI cache
- *
- * @obj - buffer previously allocated by qi_cache_zalloc
- *
- * No checking is being done, the call is a passthrough call to
- * kmem_cache_free(...)
- */
- static inline void qi_cache_free(void *obj)
- {
- kmem_cache_free(qi_cache, obj);
- }
- static struct caam_request *to_caam_req(struct crypto_async_request *areq)
- {
- switch (crypto_tfm_alg_type(areq->tfm)) {
- case CRYPTO_ALG_TYPE_SKCIPHER:
- return skcipher_request_ctx(skcipher_request_cast(areq));
- case CRYPTO_ALG_TYPE_AEAD:
- return aead_request_ctx(container_of(areq, struct aead_request,
- base));
- case CRYPTO_ALG_TYPE_AHASH:
- return ahash_request_ctx(ahash_request_cast(areq));
- default:
- return ERR_PTR(-EINVAL);
- }
- }
- static void caam_unmap(struct device *dev, struct scatterlist *src,
- struct scatterlist *dst, int src_nents,
- int dst_nents, dma_addr_t iv_dma, int ivsize,
- enum dma_data_direction iv_dir, dma_addr_t qm_sg_dma,
- int qm_sg_bytes)
- {
- if (dst != src) {
- if (src_nents)
- dma_unmap_sg(dev, src, src_nents, DMA_TO_DEVICE);
- if (dst_nents)
- dma_unmap_sg(dev, dst, dst_nents, DMA_FROM_DEVICE);
- } else {
- dma_unmap_sg(dev, src, src_nents, DMA_BIDIRECTIONAL);
- }
- if (iv_dma)
- dma_unmap_single(dev, iv_dma, ivsize, iv_dir);
- if (qm_sg_bytes)
- dma_unmap_single(dev, qm_sg_dma, qm_sg_bytes, DMA_TO_DEVICE);
- }
- static int aead_set_sh_desc(struct crypto_aead *aead)
- {
- struct caam_aead_alg *alg = container_of(crypto_aead_alg(aead),
- typeof(*alg), aead);
- struct caam_ctx *ctx = crypto_aead_ctx(aead);
- unsigned int ivsize = crypto_aead_ivsize(aead);
- struct device *dev = ctx->dev;
- struct dpaa2_caam_priv *priv = dev_get_drvdata(dev);
- struct caam_flc *flc;
- u32 *desc;
- u32 ctx1_iv_off = 0;
- u32 *nonce = NULL;
- unsigned int data_len[2];
- u32 inl_mask;
- const bool ctr_mode = ((ctx->cdata.algtype & OP_ALG_AAI_MASK) ==
- OP_ALG_AAI_CTR_MOD128);
- const bool is_rfc3686 = alg->caam.rfc3686;
- if (!ctx->cdata.keylen || !ctx->authsize)
- return 0;
- /*
- * AES-CTR needs to load IV in CONTEXT1 reg
- * at an offset of 128bits (16bytes)
- * CONTEXT1[255:128] = IV
- */
- if (ctr_mode)
- ctx1_iv_off = 16;
- /*
- * RFC3686 specific:
- * CONTEXT1[255:128] = {NONCE, IV, COUNTER}
- */
- if (is_rfc3686) {
- ctx1_iv_off = 16 + CTR_RFC3686_NONCE_SIZE;
- nonce = (u32 *)((void *)ctx->key + ctx->adata.keylen_pad +
- ctx->cdata.keylen - CTR_RFC3686_NONCE_SIZE);
- }
- /*
- * In case |user key| > |derived key|, using DKP<imm,imm> would result
- * in invalid opcodes (last bytes of user key) in the resulting
- * descriptor. Use DKP<ptr,imm> instead => both virtual and dma key
- * addresses are needed.
- */
- ctx->adata.key_virt = ctx->key;
- ctx->adata.key_dma = ctx->key_dma;
- ctx->cdata.key_virt = ctx->key + ctx->adata.keylen_pad;
- ctx->cdata.key_dma = ctx->key_dma + ctx->adata.keylen_pad;
- data_len[0] = ctx->adata.keylen_pad;
- data_len[1] = ctx->cdata.keylen;
- /* aead_encrypt shared descriptor */
- if (desc_inline_query((alg->caam.geniv ? DESC_QI_AEAD_GIVENC_LEN :
- DESC_QI_AEAD_ENC_LEN) +
- (is_rfc3686 ? DESC_AEAD_CTR_RFC3686_LEN : 0),
- DESC_JOB_IO_LEN, data_len, &inl_mask,
- ARRAY_SIZE(data_len)) < 0)
- return -EINVAL;
- ctx->adata.key_inline = !!(inl_mask & 1);
- ctx->cdata.key_inline = !!(inl_mask & 2);
- flc = &ctx->flc[ENCRYPT];
- desc = flc->sh_desc;
- if (alg->caam.geniv)
- cnstr_shdsc_aead_givencap(desc, &ctx->cdata, &ctx->adata,
- ivsize, ctx->authsize, is_rfc3686,
- nonce, ctx1_iv_off, true,
- priv->sec_attr.era);
- else
- cnstr_shdsc_aead_encap(desc, &ctx->cdata, &ctx->adata,
- ivsize, ctx->authsize, is_rfc3686, nonce,
- ctx1_iv_off, true, priv->sec_attr.era);
- flc->flc[1] = cpu_to_caam32(desc_len(desc)); /* SDL */
- dma_sync_single_for_device(dev, ctx->flc_dma[ENCRYPT],
- sizeof(flc->flc) + desc_bytes(desc),
- ctx->dir);
- /* aead_decrypt shared descriptor */
- if (desc_inline_query(DESC_QI_AEAD_DEC_LEN +
- (is_rfc3686 ? DESC_AEAD_CTR_RFC3686_LEN : 0),
- DESC_JOB_IO_LEN, data_len, &inl_mask,
- ARRAY_SIZE(data_len)) < 0)
- return -EINVAL;
- ctx->adata.key_inline = !!(inl_mask & 1);
- ctx->cdata.key_inline = !!(inl_mask & 2);
- flc = &ctx->flc[DECRYPT];
- desc = flc->sh_desc;
- cnstr_shdsc_aead_decap(desc, &ctx->cdata, &ctx->adata,
- ivsize, ctx->authsize, alg->caam.geniv,
- is_rfc3686, nonce, ctx1_iv_off, true,
- priv->sec_attr.era);
- flc->flc[1] = cpu_to_caam32(desc_len(desc)); /* SDL */
- dma_sync_single_for_device(dev, ctx->flc_dma[DECRYPT],
- sizeof(flc->flc) + desc_bytes(desc),
- ctx->dir);
- return 0;
- }
- static int aead_setauthsize(struct crypto_aead *authenc, unsigned int authsize)
- {
- struct caam_ctx *ctx = crypto_aead_ctx(authenc);
- ctx->authsize = authsize;
- aead_set_sh_desc(authenc);
- return 0;
- }
- static int aead_setkey(struct crypto_aead *aead, const u8 *key,
- unsigned int keylen)
- {
- struct caam_ctx *ctx = crypto_aead_ctx(aead);
- struct device *dev = ctx->dev;
- struct crypto_authenc_keys keys;
- if (crypto_authenc_extractkeys(&keys, key, keylen) != 0)
- goto badkey;
- dev_dbg(dev, "keylen %d enckeylen %d authkeylen %d\n",
- keys.authkeylen + keys.enckeylen, keys.enckeylen,
- keys.authkeylen);
- print_hex_dump_debug("key in @" __stringify(__LINE__)": ",
- DUMP_PREFIX_ADDRESS, 16, 4, key, keylen, 1);
- ctx->adata.keylen = keys.authkeylen;
- ctx->adata.keylen_pad = split_key_len(ctx->adata.algtype &
- OP_ALG_ALGSEL_MASK);
- if (ctx->adata.keylen_pad + keys.enckeylen > CAAM_MAX_KEY_SIZE)
- goto badkey;
- memcpy(ctx->key, keys.authkey, keys.authkeylen);
- memcpy(ctx->key + ctx->adata.keylen_pad, keys.enckey, keys.enckeylen);
- dma_sync_single_for_device(dev, ctx->key_dma, ctx->adata.keylen_pad +
- keys.enckeylen, ctx->dir);
- print_hex_dump_debug("ctx.key@" __stringify(__LINE__)": ",
- DUMP_PREFIX_ADDRESS, 16, 4, ctx->key,
- ctx->adata.keylen_pad + keys.enckeylen, 1);
- ctx->cdata.keylen = keys.enckeylen;
- memzero_explicit(&keys, sizeof(keys));
- return aead_set_sh_desc(aead);
- badkey:
- crypto_aead_set_flags(aead, CRYPTO_TFM_RES_BAD_KEY_LEN);
- memzero_explicit(&keys, sizeof(keys));
- return -EINVAL;
- }
- static int des3_aead_setkey(struct crypto_aead *aead, const u8 *key,
- unsigned int keylen)
- {
- struct crypto_authenc_keys keys;
- int err;
- err = crypto_authenc_extractkeys(&keys, key, keylen);
- if (unlikely(err))
- goto badkey;
- err = -EINVAL;
- if (keys.enckeylen != DES3_EDE_KEY_SIZE)
- goto badkey;
- err = crypto_des3_ede_verify_key(crypto_aead_tfm(aead), keys.enckey) ?:
- aead_setkey(aead, key, keylen);
- out:
- memzero_explicit(&keys, sizeof(keys));
- return err;
- badkey:
- crypto_aead_set_flags(aead, CRYPTO_TFM_RES_BAD_KEY_LEN);
- goto out;
- }
- static struct aead_edesc *aead_edesc_alloc(struct aead_request *req,
- bool encrypt)
- {
- struct crypto_aead *aead = crypto_aead_reqtfm(req);
- struct caam_request *req_ctx = aead_request_ctx(req);
- struct dpaa2_fl_entry *in_fle = &req_ctx->fd_flt[1];
- struct dpaa2_fl_entry *out_fle = &req_ctx->fd_flt[0];
- struct caam_ctx *ctx = crypto_aead_ctx(aead);
- struct caam_aead_alg *alg = container_of(crypto_aead_alg(aead),
- typeof(*alg), aead);
- struct device *dev = ctx->dev;
- gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
- GFP_KERNEL : GFP_ATOMIC;
- int src_nents, mapped_src_nents, dst_nents = 0, mapped_dst_nents = 0;
- int src_len, dst_len = 0;
- struct aead_edesc *edesc;
- dma_addr_t qm_sg_dma, iv_dma = 0;
- int ivsize = 0;
- unsigned int authsize = ctx->authsize;
- int qm_sg_index = 0, qm_sg_nents = 0, qm_sg_bytes;
- int in_len, out_len;
- struct dpaa2_sg_entry *sg_table;
- /* allocate space for base edesc, link tables and IV */
- edesc = qi_cache_zalloc(GFP_DMA | flags);
- if (unlikely(!edesc)) {
- dev_err(dev, "could not allocate extended descriptor\n");
- return ERR_PTR(-ENOMEM);
- }
- if (unlikely(req->dst != req->src)) {
- src_len = req->assoclen + req->cryptlen;
- dst_len = src_len + (encrypt ? authsize : (-authsize));
- src_nents = sg_nents_for_len(req->src, src_len);
- if (unlikely(src_nents < 0)) {
- dev_err(dev, "Insufficient bytes (%d) in src S/G\n",
- src_len);
- qi_cache_free(edesc);
- return ERR_PTR(src_nents);
- }
- dst_nents = sg_nents_for_len(req->dst, dst_len);
- if (unlikely(dst_nents < 0)) {
- dev_err(dev, "Insufficient bytes (%d) in dst S/G\n",
- dst_len);
- qi_cache_free(edesc);
- return ERR_PTR(dst_nents);
- }
- if (src_nents) {
- mapped_src_nents = dma_map_sg(dev, req->src, src_nents,
- DMA_TO_DEVICE);
- if (unlikely(!mapped_src_nents)) {
- dev_err(dev, "unable to map source\n");
- qi_cache_free(edesc);
- return ERR_PTR(-ENOMEM);
- }
- } else {
- mapped_src_nents = 0;
- }
- if (dst_nents) {
- mapped_dst_nents = dma_map_sg(dev, req->dst, dst_nents,
- DMA_FROM_DEVICE);
- if (unlikely(!mapped_dst_nents)) {
- dev_err(dev, "unable to map destination\n");
- dma_unmap_sg(dev, req->src, src_nents,
- DMA_TO_DEVICE);
- qi_cache_free(edesc);
- return ERR_PTR(-ENOMEM);
- }
- } else {
- mapped_dst_nents = 0;
- }
- } else {
- src_len = req->assoclen + req->cryptlen +
- (encrypt ? authsize : 0);
- src_nents = sg_nents_for_len(req->src, src_len);
- if (unlikely(src_nents < 0)) {
- dev_err(dev, "Insufficient bytes (%d) in src S/G\n",
- src_len);
- qi_cache_free(edesc);
- return ERR_PTR(src_nents);
- }
- mapped_src_nents = dma_map_sg(dev, req->src, src_nents,
- DMA_BIDIRECTIONAL);
- if (unlikely(!mapped_src_nents)) {
- dev_err(dev, "unable to map source\n");
- qi_cache_free(edesc);
- return ERR_PTR(-ENOMEM);
- }
- }
- if ((alg->caam.rfc3686 && encrypt) || !alg->caam.geniv)
- ivsize = crypto_aead_ivsize(aead);
- /*
- * Create S/G table: req->assoclen, [IV,] req->src [, req->dst].
- * Input is not contiguous.
- * HW reads 4 S/G entries at a time; make sure the reads don't go beyond
- * the end of the table by allocating more S/G entries. Logic:
- * if (src != dst && output S/G)
- * pad output S/G, if needed
- * else if (src == dst && S/G)
- * overlapping S/Gs; pad one of them
- * else if (input S/G) ...
- * pad input S/G, if needed
- */
- qm_sg_nents = 1 + !!ivsize + mapped_src_nents;
- if (mapped_dst_nents > 1)
- qm_sg_nents += pad_sg_nents(mapped_dst_nents);
- else if ((req->src == req->dst) && (mapped_src_nents > 1))
- qm_sg_nents = max(pad_sg_nents(qm_sg_nents),
- 1 + !!ivsize +
- pad_sg_nents(mapped_src_nents));
- else
- qm_sg_nents = pad_sg_nents(qm_sg_nents);
- sg_table = &edesc->sgt[0];
- qm_sg_bytes = qm_sg_nents * sizeof(*sg_table);
- if (unlikely(offsetof(struct aead_edesc, sgt) + qm_sg_bytes + ivsize >
- CAAM_QI_MEMCACHE_SIZE)) {
- dev_err(dev, "No space for %d S/G entries and/or %dB IV\n",
- qm_sg_nents, ivsize);
- caam_unmap(dev, req->src, req->dst, src_nents, dst_nents, 0,
- 0, DMA_NONE, 0, 0);
- qi_cache_free(edesc);
- return ERR_PTR(-ENOMEM);
- }
- if (ivsize) {
- u8 *iv = (u8 *)(sg_table + qm_sg_nents);
- /* Make sure IV is located in a DMAable area */
- memcpy(iv, req->iv, ivsize);
- iv_dma = dma_map_single(dev, iv, ivsize, DMA_TO_DEVICE);
- if (dma_mapping_error(dev, iv_dma)) {
- dev_err(dev, "unable to map IV\n");
- caam_unmap(dev, req->src, req->dst, src_nents,
- dst_nents, 0, 0, DMA_NONE, 0, 0);
- qi_cache_free(edesc);
- return ERR_PTR(-ENOMEM);
- }
- }
- edesc->src_nents = src_nents;
- edesc->dst_nents = dst_nents;
- edesc->iv_dma = iv_dma;
- if ((alg->caam.class1_alg_type & OP_ALG_ALGSEL_MASK) ==
- OP_ALG_ALGSEL_CHACHA20 && ivsize != CHACHAPOLY_IV_SIZE)
- /*
- * The associated data comes already with the IV but we need
- * to skip it when we authenticate or encrypt...
- */
- edesc->assoclen = cpu_to_caam32(req->assoclen - ivsize);
- else
- edesc->assoclen = cpu_to_caam32(req->assoclen);
- edesc->assoclen_dma = dma_map_single(dev, &edesc->assoclen, 4,
- DMA_TO_DEVICE);
- if (dma_mapping_error(dev, edesc->assoclen_dma)) {
- dev_err(dev, "unable to map assoclen\n");
- caam_unmap(dev, req->src, req->dst, src_nents, dst_nents,
- iv_dma, ivsize, DMA_TO_DEVICE, 0, 0);
- qi_cache_free(edesc);
- return ERR_PTR(-ENOMEM);
- }
- dma_to_qm_sg_one(sg_table, edesc->assoclen_dma, 4, 0);
- qm_sg_index++;
- if (ivsize) {
- dma_to_qm_sg_one(sg_table + qm_sg_index, iv_dma, ivsize, 0);
- qm_sg_index++;
- }
- sg_to_qm_sg_last(req->src, src_len, sg_table + qm_sg_index, 0);
- qm_sg_index += mapped_src_nents;
- if (mapped_dst_nents > 1)
- sg_to_qm_sg_last(req->dst, dst_len, sg_table + qm_sg_index, 0);
- qm_sg_dma = dma_map_single(dev, sg_table, qm_sg_bytes, DMA_TO_DEVICE);
- if (dma_mapping_error(dev, qm_sg_dma)) {
- dev_err(dev, "unable to map S/G table\n");
- dma_unmap_single(dev, edesc->assoclen_dma, 4, DMA_TO_DEVICE);
- caam_unmap(dev, req->src, req->dst, src_nents, dst_nents,
- iv_dma, ivsize, DMA_TO_DEVICE, 0, 0);
- qi_cache_free(edesc);
- return ERR_PTR(-ENOMEM);
- }
- edesc->qm_sg_dma = qm_sg_dma;
- edesc->qm_sg_bytes = qm_sg_bytes;
- out_len = req->assoclen + req->cryptlen +
- (encrypt ? ctx->authsize : (-ctx->authsize));
- in_len = 4 + ivsize + req->assoclen + req->cryptlen;
- memset(&req_ctx->fd_flt, 0, sizeof(req_ctx->fd_flt));
- dpaa2_fl_set_final(in_fle, true);
- dpaa2_fl_set_format(in_fle, dpaa2_fl_sg);
- dpaa2_fl_set_addr(in_fle, qm_sg_dma);
- dpaa2_fl_set_len(in_fle, in_len);
- if (req->dst == req->src) {
- if (mapped_src_nents == 1) {
- dpaa2_fl_set_format(out_fle, dpaa2_fl_single);
- dpaa2_fl_set_addr(out_fle, sg_dma_address(req->src));
- } else {
- dpaa2_fl_set_format(out_fle, dpaa2_fl_sg);
- dpaa2_fl_set_addr(out_fle, qm_sg_dma +
- (1 + !!ivsize) * sizeof(*sg_table));
- }
- } else if (!mapped_dst_nents) {
- /*
- * crypto engine requires the output entry to be present when
- * "frame list" FD is used.
- * Since engine does not support FMT=2'b11 (unused entry type),
- * leaving out_fle zeroized is the best option.
- */
- goto skip_out_fle;
- } else if (mapped_dst_nents == 1) {
- dpaa2_fl_set_format(out_fle, dpaa2_fl_single);
- dpaa2_fl_set_addr(out_fle, sg_dma_address(req->dst));
- } else {
- dpaa2_fl_set_format(out_fle, dpaa2_fl_sg);
- dpaa2_fl_set_addr(out_fle, qm_sg_dma + qm_sg_index *
- sizeof(*sg_table));
- }
- dpaa2_fl_set_len(out_fle, out_len);
- skip_out_fle:
- return edesc;
- }
- static int chachapoly_set_sh_desc(struct crypto_aead *aead)
- {
- struct caam_ctx *ctx = crypto_aead_ctx(aead);
- unsigned int ivsize = crypto_aead_ivsize(aead);
- struct device *dev = ctx->dev;
- struct caam_flc *flc;
- u32 *desc;
- if (!ctx->cdata.keylen || !ctx->authsize)
- return 0;
- flc = &ctx->flc[ENCRYPT];
- desc = flc->sh_desc;
- cnstr_shdsc_chachapoly(desc, &ctx->cdata, &ctx->adata, ivsize,
- ctx->authsize, true, true);
- flc->flc[1] = cpu_to_caam32(desc_len(desc)); /* SDL */
- dma_sync_single_for_device(dev, ctx->flc_dma[ENCRYPT],
- sizeof(flc->flc) + desc_bytes(desc),
- ctx->dir);
- flc = &ctx->flc[DECRYPT];
- desc = flc->sh_desc;
- cnstr_shdsc_chachapoly(desc, &ctx->cdata, &ctx->adata, ivsize,
- ctx->authsize, false, true);
- flc->flc[1] = cpu_to_caam32(desc_len(desc)); /* SDL */
- dma_sync_single_for_device(dev, ctx->flc_dma[DECRYPT],
- sizeof(flc->flc) + desc_bytes(desc),
- ctx->dir);
- return 0;
- }
- static int chachapoly_setauthsize(struct crypto_aead *aead,
- unsigned int authsize)
- {
- struct caam_ctx *ctx = crypto_aead_ctx(aead);
- if (authsize != POLY1305_DIGEST_SIZE)
- return -EINVAL;
- ctx->authsize = authsize;
- return chachapoly_set_sh_desc(aead);
- }
- static int chachapoly_setkey(struct crypto_aead *aead, const u8 *key,
- unsigned int keylen)
- {
- struct caam_ctx *ctx = crypto_aead_ctx(aead);
- unsigned int ivsize = crypto_aead_ivsize(aead);
- unsigned int saltlen = CHACHAPOLY_IV_SIZE - ivsize;
- if (keylen != CHACHA_KEY_SIZE + saltlen) {
- crypto_aead_set_flags(aead, CRYPTO_TFM_RES_BAD_KEY_LEN);
- return -EINVAL;
- }
- ctx->cdata.key_virt = key;
- ctx->cdata.keylen = keylen - saltlen;
- return chachapoly_set_sh_desc(aead);
- }
- static int gcm_set_sh_desc(struct crypto_aead *aead)
- {
- struct caam_ctx *ctx = crypto_aead_ctx(aead);
- struct device *dev = ctx->dev;
- unsigned int ivsize = crypto_aead_ivsize(aead);
- struct caam_flc *flc;
- u32 *desc;
- int rem_bytes = CAAM_DESC_BYTES_MAX - DESC_JOB_IO_LEN -
- ctx->cdata.keylen;
- if (!ctx->cdata.keylen || !ctx->authsize)
- return 0;
- /*
- * AES GCM encrypt shared descriptor
- * Job Descriptor and Shared Descriptor
- * must fit into the 64-word Descriptor h/w Buffer
- */
- if (rem_bytes >= DESC_QI_GCM_ENC_LEN) {
- ctx->cdata.key_inline = true;
- ctx->cdata.key_virt = ctx->key;
- } else {
- ctx->cdata.key_inline = false;
- ctx->cdata.key_dma = ctx->key_dma;
- }
- flc = &ctx->flc[ENCRYPT];
- desc = flc->sh_desc;
- cnstr_shdsc_gcm_encap(desc, &ctx->cdata, ivsize, ctx->authsize, true);
- flc->flc[1] = cpu_to_caam32(desc_len(desc)); /* SDL */
- dma_sync_single_for_device(dev, ctx->flc_dma[ENCRYPT],
- sizeof(flc->flc) + desc_bytes(desc),
- ctx->dir);
- /*
- * Job Descriptor and Shared Descriptors
- * must all fit into the 64-word Descriptor h/w Buffer
- */
- if (rem_bytes >= DESC_QI_GCM_DEC_LEN) {
- ctx->cdata.key_inline = true;
- ctx->cdata.key_virt = ctx->key;
- } else {
- ctx->cdata.key_inline = false;
- ctx->cdata.key_dma = ctx->key_dma;
- }
- flc = &ctx->flc[DECRYPT];
- desc = flc->sh_desc;
- cnstr_shdsc_gcm_decap(desc, &ctx->cdata, ivsize, ctx->authsize, true);
- flc->flc[1] = cpu_to_caam32(desc_len(desc)); /* SDL */
- dma_sync_single_for_device(dev, ctx->flc_dma[DECRYPT],
- sizeof(flc->flc) + desc_bytes(desc),
- ctx->dir);
- return 0;
- }
- static int gcm_setauthsize(struct crypto_aead *authenc, unsigned int authsize)
- {
- struct caam_ctx *ctx = crypto_aead_ctx(authenc);
- int err;
- err = crypto_gcm_check_authsize(authsize);
- if (err)
- return err;
- ctx->authsize = authsize;
- gcm_set_sh_desc(authenc);
- return 0;
- }
- static int gcm_setkey(struct crypto_aead *aead,
- const u8 *key, unsigned int keylen)
- {
- struct caam_ctx *ctx = crypto_aead_ctx(aead);
- struct device *dev = ctx->dev;
- int ret;
- ret = aes_check_keylen(keylen);
- if (ret) {
- crypto_aead_set_flags(aead, CRYPTO_TFM_RES_BAD_KEY_LEN);
- return ret;
- }
- print_hex_dump_debug("key in @" __stringify(__LINE__)": ",
- DUMP_PREFIX_ADDRESS, 16, 4, key, keylen, 1);
- memcpy(ctx->key, key, keylen);
- dma_sync_single_for_device(dev, ctx->key_dma, keylen, ctx->dir);
- ctx->cdata.keylen = keylen;
- return gcm_set_sh_desc(aead);
- }
- static int rfc4106_set_sh_desc(struct crypto_aead *aead)
- {
- struct caam_ctx *ctx = crypto_aead_ctx(aead);
- struct device *dev = ctx->dev;
- unsigned int ivsize = crypto_aead_ivsize(aead);
- struct caam_flc *flc;
- u32 *desc;
- int rem_bytes = CAAM_DESC_BYTES_MAX - DESC_JOB_IO_LEN -
- ctx->cdata.keylen;
- if (!ctx->cdata.keylen || !ctx->authsize)
- return 0;
- ctx->cdata.key_virt = ctx->key;
- /*
- * RFC4106 encrypt shared descriptor
- * Job Descriptor and Shared Descriptor
- * must fit into the 64-word Descriptor h/w Buffer
- */
- if (rem_bytes >= DESC_QI_RFC4106_ENC_LEN) {
- ctx->cdata.key_inline = true;
- } else {
- ctx->cdata.key_inline = false;
- ctx->cdata.key_dma = ctx->key_dma;
- }
- flc = &ctx->flc[ENCRYPT];
- desc = flc->sh_desc;
- cnstr_shdsc_rfc4106_encap(desc, &ctx->cdata, ivsize, ctx->authsize,
- true);
- flc->flc[1] = cpu_to_caam32(desc_len(desc)); /* SDL */
- dma_sync_single_for_device(dev, ctx->flc_dma[ENCRYPT],
- sizeof(flc->flc) + desc_bytes(desc),
- ctx->dir);
- /*
- * Job Descriptor and Shared Descriptors
- * must all fit into the 64-word Descriptor h/w Buffer
- */
- if (rem_bytes >= DESC_QI_RFC4106_DEC_LEN) {
- ctx->cdata.key_inline = true;
- } else {
- ctx->cdata.key_inline = false;
- ctx->cdata.key_dma = ctx->key_dma;
- }
- flc = &ctx->flc[DECRYPT];
- desc = flc->sh_desc;
- cnstr_shdsc_rfc4106_decap(desc, &ctx->cdata, ivsize, ctx->authsize,
- true);
- flc->flc[1] = cpu_to_caam32(desc_len(desc)); /* SDL */
- dma_sync_single_for_device(dev, ctx->flc_dma[DECRYPT],
- sizeof(flc->flc) + desc_bytes(desc),
- ctx->dir);
- return 0;
- }
- static int rfc4106_setauthsize(struct crypto_aead *authenc,
- unsigned int authsize)
- {
- struct caam_ctx *ctx = crypto_aead_ctx(authenc);
- int err;
- err = crypto_rfc4106_check_authsize(authsize);
- if (err)
- return err;
- ctx->authsize = authsize;
- rfc4106_set_sh_desc(authenc);
- return 0;
- }
- static int rfc4106_setkey(struct crypto_aead *aead,
- const u8 *key, unsigned int keylen)
- {
- struct caam_ctx *ctx = crypto_aead_ctx(aead);
- struct device *dev = ctx->dev;
- int ret;
- ret = aes_check_keylen(keylen - 4);
- if (ret) {
- crypto_aead_set_flags(aead, CRYPTO_TFM_RES_BAD_KEY_LEN);
- return ret;
- }
- print_hex_dump_debug("key in @" __stringify(__LINE__)": ",
- DUMP_PREFIX_ADDRESS, 16, 4, key, keylen, 1);
- memcpy(ctx->key, key, keylen);
- /*
- * The last four bytes of the key material are used as the salt value
- * in the nonce. Update the AES key length.
- */
- ctx->cdata.keylen = keylen - 4;
- dma_sync_single_for_device(dev, ctx->key_dma, ctx->cdata.keylen,
- ctx->dir);
- return rfc4106_set_sh_desc(aead);
- }
- static int rfc4543_set_sh_desc(struct crypto_aead *aead)
- {
- struct caam_ctx *ctx = crypto_aead_ctx(aead);
- struct device *dev = ctx->dev;
- unsigned int ivsize = crypto_aead_ivsize(aead);
- struct caam_flc *flc;
- u32 *desc;
- int rem_bytes = CAAM_DESC_BYTES_MAX - DESC_JOB_IO_LEN -
- ctx->cdata.keylen;
- if (!ctx->cdata.keylen || !ctx->authsize)
- return 0;
- ctx->cdata.key_virt = ctx->key;
- /*
- * RFC4543 encrypt shared descriptor
- * Job Descriptor and Shared Descriptor
- * must fit into the 64-word Descriptor h/w Buffer
- */
- if (rem_bytes >= DESC_QI_RFC4543_ENC_LEN) {
- ctx->cdata.key_inline = true;
- } else {
- ctx->cdata.key_inline = false;
- ctx->cdata.key_dma = ctx->key_dma;
- }
- flc = &ctx->flc[ENCRYPT];
- desc = flc->sh_desc;
- cnstr_shdsc_rfc4543_encap(desc, &ctx->cdata, ivsize, ctx->authsize,
- true);
- flc->flc[1] = cpu_to_caam32(desc_len(desc)); /* SDL */
- dma_sync_single_for_device(dev, ctx->flc_dma[ENCRYPT],
- sizeof(flc->flc) + desc_bytes(desc),
- ctx->dir);
- /*
- * Job Descriptor and Shared Descriptors
- * must all fit into the 64-word Descriptor h/w Buffer
- */
- if (rem_bytes >= DESC_QI_RFC4543_DEC_LEN) {
- ctx->cdata.key_inline = true;
- } else {
- ctx->cdata.key_inline = false;
- ctx->cdata.key_dma = ctx->key_dma;
- }
- flc = &ctx->flc[DECRYPT];
- desc = flc->sh_desc;
- cnstr_shdsc_rfc4543_decap(desc, &ctx->cdata, ivsize, ctx->authsize,
- true);
- flc->flc[1] = cpu_to_caam32(desc_len(desc)); /* SDL */
- dma_sync_single_for_device(dev, ctx->flc_dma[DECRYPT],
- sizeof(flc->flc) + desc_bytes(desc),
- ctx->dir);
- return 0;
- }
- static int rfc4543_setauthsize(struct crypto_aead *authenc,
- unsigned int authsize)
- {
- struct caam_ctx *ctx = crypto_aead_ctx(authenc);
- if (authsize != 16)
- return -EINVAL;
- ctx->authsize = authsize;
- rfc4543_set_sh_desc(authenc);
- return 0;
- }
- static int rfc4543_setkey(struct crypto_aead *aead,
- const u8 *key, unsigned int keylen)
- {
- struct caam_ctx *ctx = crypto_aead_ctx(aead);
- struct device *dev = ctx->dev;
- int ret;
- ret = aes_check_keylen(keylen - 4);
- if (ret) {
- crypto_aead_set_flags(aead, CRYPTO_TFM_RES_BAD_KEY_LEN);
- return ret;
- }
- print_hex_dump_debug("key in @" __stringify(__LINE__)": ",
- DUMP_PREFIX_ADDRESS, 16, 4, key, keylen, 1);
- memcpy(ctx->key, key, keylen);
- /*
- * The last four bytes of the key material are used as the salt value
- * in the nonce. Update the AES key length.
- */
- ctx->cdata.keylen = keylen - 4;
- dma_sync_single_for_device(dev, ctx->key_dma, ctx->cdata.keylen,
- ctx->dir);
- return rfc4543_set_sh_desc(aead);
- }
- static int skcipher_setkey(struct crypto_skcipher *skcipher, const u8 *key,
- unsigned int keylen, const u32 ctx1_iv_off)
- {
- struct caam_ctx *ctx = crypto_skcipher_ctx(skcipher);
- struct caam_skcipher_alg *alg =
- container_of(crypto_skcipher_alg(skcipher),
- struct caam_skcipher_alg, skcipher);
- struct device *dev = ctx->dev;
- struct caam_flc *flc;
- unsigned int ivsize = crypto_skcipher_ivsize(skcipher);
- u32 *desc;
- const bool is_rfc3686 = alg->caam.rfc3686;
- print_hex_dump_debug("key in @" __stringify(__LINE__)": ",
- DUMP_PREFIX_ADDRESS, 16, 4, key, keylen, 1);
- ctx->cdata.keylen = keylen;
- ctx->cdata.key_virt = key;
- ctx->cdata.key_inline = true;
- /* skcipher_encrypt shared descriptor */
- flc = &ctx->flc[ENCRYPT];
- desc = flc->sh_desc;
- cnstr_shdsc_skcipher_encap(desc, &ctx->cdata, ivsize, is_rfc3686,
- ctx1_iv_off);
- flc->flc[1] = cpu_to_caam32(desc_len(desc)); /* SDL */
- dma_sync_single_for_device(dev, ctx->flc_dma[ENCRYPT],
- sizeof(flc->flc) + desc_bytes(desc),
- ctx->dir);
- /* skcipher_decrypt shared descriptor */
- flc = &ctx->flc[DECRYPT];
- desc = flc->sh_desc;
- cnstr_shdsc_skcipher_decap(desc, &ctx->cdata, ivsize, is_rfc3686,
- ctx1_iv_off);
- flc->flc[1] = cpu_to_caam32(desc_len(desc)); /* SDL */
- dma_sync_single_for_device(dev, ctx->flc_dma[DECRYPT],
- sizeof(flc->flc) + desc_bytes(desc),
- ctx->dir);
- return 0;
- }
- static int aes_skcipher_setkey(struct crypto_skcipher *skcipher,
- const u8 *key, unsigned int keylen)
- {
- int err;
- err = aes_check_keylen(keylen);
- if (err) {
- crypto_skcipher_set_flags(skcipher,
- CRYPTO_TFM_RES_BAD_KEY_LEN);
- return err;
- }
- return skcipher_setkey(skcipher, key, keylen, 0);
- }
- static int rfc3686_skcipher_setkey(struct crypto_skcipher *skcipher,
- const u8 *key, unsigned int keylen)
- {
- u32 ctx1_iv_off;
- int err;
- /*
- * RFC3686 specific:
- * | CONTEXT1[255:128] = {NONCE, IV, COUNTER}
- * | *key = {KEY, NONCE}
- */
- ctx1_iv_off = 16 + CTR_RFC3686_NONCE_SIZE;
- keylen -= CTR_RFC3686_NONCE_SIZE;
- err = aes_check_keylen(keylen);
- if (err) {
- crypto_skcipher_set_flags(skcipher,
- CRYPTO_TFM_RES_BAD_KEY_LEN);
- return err;
- }
- return skcipher_setkey(skcipher, key, keylen, ctx1_iv_off);
- }
- static int ctr_skcipher_setkey(struct crypto_skcipher *skcipher,
- const u8 *key, unsigned int keylen)
- {
- u32 ctx1_iv_off;
- int err;
- /*
- * AES-CTR needs to load IV in CONTEXT1 reg
- * at an offset of 128bits (16bytes)
- * CONTEXT1[255:128] = IV
- */
- ctx1_iv_off = 16;
- err = aes_check_keylen(keylen);
- if (err) {
- crypto_skcipher_set_flags(skcipher,
- CRYPTO_TFM_RES_BAD_KEY_LEN);
- return err;
- }
- return skcipher_setkey(skcipher, key, keylen, ctx1_iv_off);
- }
- static int chacha20_skcipher_setkey(struct crypto_skcipher *skcipher,
- const u8 *key, unsigned int keylen)
- {
- if (keylen != CHACHA_KEY_SIZE) {
- crypto_skcipher_set_flags(skcipher,
- CRYPTO_TFM_RES_BAD_KEY_LEN);
- return -EINVAL;
- }
- return skcipher_setkey(skcipher, key, keylen, 0);
- }
- static int des_skcipher_setkey(struct crypto_skcipher *skcipher,
- const u8 *key, unsigned int keylen)
- {
- return verify_skcipher_des_key(skcipher, key) ?:
- skcipher_setkey(skcipher, key, keylen, 0);
- }
- static int des3_skcipher_setkey(struct crypto_skcipher *skcipher,
- const u8 *key, unsigned int keylen)
- {
- return verify_skcipher_des3_key(skcipher, key) ?:
- skcipher_setkey(skcipher, key, keylen, 0);
- }
- static int xts_skcipher_setkey(struct crypto_skcipher *skcipher, const u8 *key,
- unsigned int keylen)
- {
- struct caam_ctx *ctx = crypto_skcipher_ctx(skcipher);
- struct device *dev = ctx->dev;
- struct caam_flc *flc;
- u32 *desc;
- if (keylen != 2 * AES_MIN_KEY_SIZE && keylen != 2 * AES_MAX_KEY_SIZE) {
- dev_err(dev, "key size mismatch\n");
- crypto_skcipher_set_flags(skcipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
- return -EINVAL;
- }
- ctx->cdata.keylen = keylen;
- ctx->cdata.key_virt = key;
- ctx->cdata.key_inline = true;
- /* xts_skcipher_encrypt shared descriptor */
- flc = &ctx->flc[ENCRYPT];
- desc = flc->sh_desc;
- cnstr_shdsc_xts_skcipher_encap(desc, &ctx->cdata);
- flc->flc[1] = cpu_to_caam32(desc_len(desc)); /* SDL */
- dma_sync_single_for_device(dev, ctx->flc_dma[ENCRYPT],
- sizeof(flc->flc) + desc_bytes(desc),
- ctx->dir);
- /* xts_skcipher_decrypt shared descriptor */
- flc = &ctx->flc[DECRYPT];
- desc = flc->sh_desc;
- cnstr_shdsc_xts_skcipher_decap(desc, &ctx->cdata);
- flc->flc[1] = cpu_to_caam32(desc_len(desc)); /* SDL */
- dma_sync_single_for_device(dev, ctx->flc_dma[DECRYPT],
- sizeof(flc->flc) + desc_bytes(desc),
- ctx->dir);
- return 0;
- }
- static struct skcipher_edesc *skcipher_edesc_alloc(struct skcipher_request *req)
- {
- struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
- struct caam_request *req_ctx = skcipher_request_ctx(req);
- struct dpaa2_fl_entry *in_fle = &req_ctx->fd_flt[1];
- struct dpaa2_fl_entry *out_fle = &req_ctx->fd_flt[0];
- struct caam_ctx *ctx = crypto_skcipher_ctx(skcipher);
- struct device *dev = ctx->dev;
- gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
- GFP_KERNEL : GFP_ATOMIC;
- int src_nents, mapped_src_nents, dst_nents = 0, mapped_dst_nents = 0;
- struct skcipher_edesc *edesc;
- dma_addr_t iv_dma;
- u8 *iv;
- int ivsize = crypto_skcipher_ivsize(skcipher);
- int dst_sg_idx, qm_sg_ents, qm_sg_bytes;
- struct dpaa2_sg_entry *sg_table;
- src_nents = sg_nents_for_len(req->src, req->cryptlen);
- if (unlikely(src_nents < 0)) {
- dev_err(dev, "Insufficient bytes (%d) in src S/G\n",
- req->cryptlen);
- return ERR_PTR(src_nents);
- }
- if (unlikely(req->dst != req->src)) {
- dst_nents = sg_nents_for_len(req->dst, req->cryptlen);
- if (unlikely(dst_nents < 0)) {
- dev_err(dev, "Insufficient bytes (%d) in dst S/G\n",
- req->cryptlen);
- return ERR_PTR(dst_nents);
- }
- mapped_src_nents = dma_map_sg(dev, req->src, src_nents,
- DMA_TO_DEVICE);
- if (unlikely(!mapped_src_nents)) {
- dev_err(dev, "unable to map source\n");
- return ERR_PTR(-ENOMEM);
- }
- mapped_dst_nents = dma_map_sg(dev, req->dst, dst_nents,
- DMA_FROM_DEVICE);
- if (unlikely(!mapped_dst_nents)) {
- dev_err(dev, "unable to map destination\n");
- dma_unmap_sg(dev, req->src, src_nents, DMA_TO_DEVICE);
- return ERR_PTR(-ENOMEM);
- }
- } else {
- mapped_src_nents = dma_map_sg(dev, req->src, src_nents,
- DMA_BIDIRECTIONAL);
- if (unlikely(!mapped_src_nents)) {
- dev_err(dev, "unable to map source\n");
- return ERR_PTR(-ENOMEM);
- }
- }
- qm_sg_ents = 1 + mapped_src_nents;
- dst_sg_idx = qm_sg_ents;
- /*
- * Input, output HW S/G tables: [IV, src][dst, IV]
- * IV entries point to the same buffer
- * If src == dst, S/G entries are reused (S/G tables overlap)
- *
- * HW reads 4 S/G entries at a time; make sure the reads don't go beyond
- * the end of the table by allocating more S/G entries.
- */
- if (req->src != req->dst)
- qm_sg_ents += pad_sg_nents(mapped_dst_nents + 1);
- else
- qm_sg_ents = 1 + pad_sg_nents(qm_sg_ents);
- qm_sg_bytes = qm_sg_ents * sizeof(struct dpaa2_sg_entry);
- if (unlikely(offsetof(struct skcipher_edesc, sgt) + qm_sg_bytes +
- ivsize > CAAM_QI_MEMCACHE_SIZE)) {
- dev_err(dev, "No space for %d S/G entries and/or %dB IV\n",
- qm_sg_ents, ivsize);
- caam_unmap(dev, req->src, req->dst, src_nents, dst_nents, 0,
- 0, DMA_NONE, 0, 0);
- return ERR_PTR(-ENOMEM);
- }
- /* allocate space for base edesc, link tables and IV */
- edesc = qi_cache_zalloc(GFP_DMA | flags);
- if (unlikely(!edesc)) {
- dev_err(dev, "could not allocate extended descriptor\n");
- caam_unmap(dev, req->src, req->dst, src_nents, dst_nents, 0,
- 0, DMA_NONE, 0, 0);
- return ERR_PTR(-ENOMEM);
- }
- /* Make sure IV is located in a DMAable area */
- sg_table = &edesc->sgt[0];
- iv = (u8 *)(sg_table + qm_sg_ents);
- memcpy(iv, req->iv, ivsize);
- iv_dma = dma_map_single(dev, iv, ivsize, DMA_BIDIRECTIONAL);
- if (dma_mapping_error(dev, iv_dma)) {
- dev_err(dev, "unable to map IV\n");
- caam_unmap(dev, req->src, req->dst, src_nents, dst_nents, 0,
- 0, DMA_NONE, 0, 0);
- qi_cache_free(edesc);
- return ERR_PTR(-ENOMEM);
- }
- edesc->src_nents = src_nents;
- edesc->dst_nents = dst_nents;
- edesc->iv_dma = iv_dma;
- edesc->qm_sg_bytes = qm_sg_bytes;
- dma_to_qm_sg_one(sg_table, iv_dma, ivsize, 0);
- sg_to_qm_sg(req->src, req->cryptlen, sg_table + 1, 0);
- if (req->src != req->dst)
- sg_to_qm_sg(req->dst, req->cryptlen, sg_table + dst_sg_idx, 0);
- dma_to_qm_sg_one(sg_table + dst_sg_idx + mapped_dst_nents, iv_dma,
- ivsize, 0);
- edesc->qm_sg_dma = dma_map_single(dev, sg_table, edesc->qm_sg_bytes,
- DMA_TO_DEVICE);
- if (dma_mapping_error(dev, edesc->qm_sg_dma)) {
- dev_err(dev, "unable to map S/G table\n");
- caam_unmap(dev, req->src, req->dst, src_nents, dst_nents,
- iv_dma, ivsize, DMA_BIDIRECTIONAL, 0, 0);
- qi_cache_free(edesc);
- return ERR_PTR(-ENOMEM);
- }
- memset(&req_ctx->fd_flt, 0, sizeof(req_ctx->fd_flt));
- dpaa2_fl_set_final(in_fle, true);
- dpaa2_fl_set_len(in_fle, req->cryptlen + ivsize);
- dpaa2_fl_set_len(out_fle, req->cryptlen + ivsize);
- dpaa2_fl_set_format(in_fle, dpaa2_fl_sg);
- dpaa2_fl_set_addr(in_fle, edesc->qm_sg_dma);
- dpaa2_fl_set_format(out_fle, dpaa2_fl_sg);
- if (req->src == req->dst)
- dpaa2_fl_set_addr(out_fle, edesc->qm_sg_dma +
- sizeof(*sg_table));
- else
- dpaa2_fl_set_addr(out_fle, edesc->qm_sg_dma + dst_sg_idx *
- sizeof(*sg_table));
- return edesc;
- }
- static void aead_unmap(struct device *dev, struct aead_edesc *edesc,
- struct aead_request *req)
- {
- struct crypto_aead *aead = crypto_aead_reqtfm(req);
- int ivsize = crypto_aead_ivsize(aead);
- caam_unmap(dev, req->src, req->dst, edesc->src_nents, edesc->dst_nents,
- edesc->iv_dma, ivsize, DMA_TO_DEVICE, edesc->qm_sg_dma,
- edesc->qm_sg_bytes);
- dma_unmap_single(dev, edesc->assoclen_dma, 4, DMA_TO_DEVICE);
- }
- static void skcipher_unmap(struct device *dev, struct skcipher_edesc *edesc,
- struct skcipher_request *req)
- {
- struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
- int ivsize = crypto_skcipher_ivsize(skcipher);
- caam_unmap(dev, req->src, req->dst, edesc->src_nents, edesc->dst_nents,
- edesc->iv_dma, ivsize, DMA_BIDIRECTIONAL, edesc->qm_sg_dma,
- edesc->qm_sg_bytes);
- }
- static void aead_encrypt_done(void *cbk_ctx, u32 status)
- {
- struct crypto_async_request *areq = cbk_ctx;
- struct aead_request *req = container_of(areq, struct aead_request,
- base);
- struct caam_request *req_ctx = to_caam_req(areq);
- struct aead_edesc *edesc = req_ctx->edesc;
- struct crypto_aead *aead = crypto_aead_reqtfm(req);
- struct caam_ctx *ctx = crypto_aead_ctx(aead);
- int ecode = 0;
- dev_dbg(ctx->dev, "%s %d: err 0x%x\n", __func__, __LINE__, status);
- if (unlikely(status))
- ecode = caam_qi2_strstatus(ctx->dev, status);
- aead_unmap(ctx->dev, edesc, req);
- qi_cache_free(edesc);
- aead_request_complete(req, ecode);
- }
- static void aead_decrypt_done(void *cbk_ctx, u32 status)
- {
- struct crypto_async_request *areq = cbk_ctx;
- struct aead_request *req = container_of(areq, struct aead_request,
- base);
- struct caam_request *req_ctx = to_caam_req(areq);
- struct aead_edesc *edesc = req_ctx->edesc;
- struct crypto_aead *aead = crypto_aead_reqtfm(req);
- struct caam_ctx *ctx = crypto_aead_ctx(aead);
- int ecode = 0;
- dev_dbg(ctx->dev, "%s %d: err 0x%x\n", __func__, __LINE__, status);
- if (unlikely(status))
- ecode = caam_qi2_strstatus(ctx->dev, status);
- aead_unmap(ctx->dev, edesc, req);
- qi_cache_free(edesc);
- aead_request_complete(req, ecode);
- }
- static int aead_encrypt(struct aead_request *req)
- {
- struct aead_edesc *edesc;
- struct crypto_aead *aead = crypto_aead_reqtfm(req);
- struct caam_ctx *ctx = crypto_aead_ctx(aead);
- struct caam_request *caam_req = aead_request_ctx(req);
- int ret;
- /* allocate extended descriptor */
- edesc = aead_edesc_alloc(req, true);
- if (IS_ERR(edesc))
- return PTR_ERR(edesc);
- caam_req->flc = &ctx->flc[ENCRYPT];
- caam_req->flc_dma = ctx->flc_dma[ENCRYPT];
- caam_req->cbk = aead_encrypt_done;
- caam_req->ctx = &req->base;
- caam_req->edesc = edesc;
- ret = dpaa2_caam_enqueue(ctx->dev, caam_req);
- if (ret != -EINPROGRESS &&
- !(ret == -EBUSY && req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG)) {
- aead_unmap(ctx->dev, edesc, req);
- qi_cache_free(edesc);
- }
- return ret;
- }
- static int aead_decrypt(struct aead_request *req)
- {
- struct aead_edesc *edesc;
- struct crypto_aead *aead = crypto_aead_reqtfm(req);
- struct caam_ctx *ctx = crypto_aead_ctx(aead);
- struct caam_request *caam_req = aead_request_ctx(req);
- int ret;
- /* allocate extended descriptor */
- edesc = aead_edesc_alloc(req, false);
- if (IS_ERR(edesc))
- return PTR_ERR(edesc);
- caam_req->flc = &ctx->flc[DECRYPT];
- caam_req->flc_dma = ctx->flc_dma[DECRYPT];
- caam_req->cbk = aead_decrypt_done;
- caam_req->ctx = &req->base;
- caam_req->edesc = edesc;
- ret = dpaa2_caam_enqueue(ctx->dev, caam_req);
- if (ret != -EINPROGRESS &&
- !(ret == -EBUSY && req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG)) {
- aead_unmap(ctx->dev, edesc, req);
- qi_cache_free(edesc);
- }
- return ret;
- }
- static int ipsec_gcm_encrypt(struct aead_request *req)
- {
- return crypto_ipsec_check_assoclen(req->assoclen) ? : aead_encrypt(req);
- }
- static int ipsec_gcm_decrypt(struct aead_request *req)
- {
- return crypto_ipsec_check_assoclen(req->assoclen) ? : aead_decrypt(req);
- }
- static void skcipher_encrypt_done(void *cbk_ctx, u32 status)
- {
- struct crypto_async_request *areq = cbk_ctx;
- struct skcipher_request *req = skcipher_request_cast(areq);
- struct caam_request *req_ctx = to_caam_req(areq);
- struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
- struct caam_ctx *ctx = crypto_skcipher_ctx(skcipher);
- struct skcipher_edesc *edesc = req_ctx->edesc;
- int ecode = 0;
- int ivsize = crypto_skcipher_ivsize(skcipher);
- dev_dbg(ctx->dev, "%s %d: err 0x%x\n", __func__, __LINE__, status);
- if (unlikely(status))
- ecode = caam_qi2_strstatus(ctx->dev, status);
- print_hex_dump_debug("dstiv @" __stringify(__LINE__)": ",
- DUMP_PREFIX_ADDRESS, 16, 4, req->iv,
- edesc->src_nents > 1 ? 100 : ivsize, 1);
- caam_dump_sg("dst @" __stringify(__LINE__)": ",
- DUMP_PREFIX_ADDRESS, 16, 4, req->dst,
- edesc->dst_nents > 1 ? 100 : req->cryptlen, 1);
- skcipher_unmap(ctx->dev, edesc, req);
- /*
- * The crypto API expects us to set the IV (req->iv) to the last
- * ciphertext block (CBC mode) or last counter (CTR mode).
- * This is used e.g. by the CTS mode.
- */
- if (!ecode)
- memcpy(req->iv, (u8 *)&edesc->sgt[0] + edesc->qm_sg_bytes,
- ivsize);
- qi_cache_free(edesc);
- skcipher_request_complete(req, ecode);
- }
- static void skcipher_decrypt_done(void *cbk_ctx, u32 status)
- {
- struct crypto_async_request *areq = cbk_ctx;
- struct skcipher_request *req = skcipher_request_cast(areq);
- struct caam_request *req_ctx = to_caam_req(areq);
- struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
- struct caam_ctx *ctx = crypto_skcipher_ctx(skcipher);
- struct skcipher_edesc *edesc = req_ctx->edesc;
- int ecode = 0;
- int ivsize = crypto_skcipher_ivsize(skcipher);
- dev_dbg(ctx->dev, "%s %d: err 0x%x\n", __func__, __LINE__, status);
- if (unlikely(status))
- ecode = caam_qi2_strstatus(ctx->dev, status);
- print_hex_dump_debug("dstiv @" __stringify(__LINE__)": ",
- DUMP_PREFIX_ADDRESS, 16, 4, req->iv,
- edesc->src_nents > 1 ? 100 : ivsize, 1);
- caam_dump_sg("dst @" __stringify(__LINE__)": ",
- DUMP_PREFIX_ADDRESS, 16, 4, req->dst,
- edesc->dst_nents > 1 ? 100 : req->cryptlen, 1);
- skcipher_unmap(ctx->dev, edesc, req);
- /*
- * The crypto API expects us to set the IV (req->iv) to the last
- * ciphertext block (CBC mode) or last counter (CTR mode).
- * This is used e.g. by the CTS mode.
- */
- if (!ecode)
- memcpy(req->iv, (u8 *)&edesc->sgt[0] + edesc->qm_sg_bytes,
- ivsize);
- qi_cache_free(edesc);
- skcipher_request_complete(req, ecode);
- }
- static int skcipher_encrypt(struct skcipher_request *req)
- {
- struct skcipher_edesc *edesc;
- struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
- struct caam_ctx *ctx = crypto_skcipher_ctx(skcipher);
- struct caam_request *caam_req = skcipher_request_ctx(req);
- int ret;
- if (!req->cryptlen)
- return 0;
- /* allocate extended descriptor */
- edesc = skcipher_edesc_alloc(req);
- if (IS_ERR(edesc))
- return PTR_ERR(edesc);
- caam_req->flc = &ctx->flc[ENCRYPT];
- caam_req->flc_dma = ctx->flc_dma[ENCRYPT];
- caam_req->cbk = skcipher_encrypt_done;
- caam_req->ctx = &req->base;
- caam_req->edesc = edesc;
- ret = dpaa2_caam_enqueue(ctx->dev, caam_req);
- if (ret != -EINPROGRESS &&
- !(ret == -EBUSY && req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG)) {
- skcipher_unmap(ctx->dev, edesc, req);
- qi_cache_free(edesc);
- }
- return ret;
- }
- static int skcipher_decrypt(struct skcipher_request *req)
- {
- struct skcipher_edesc *edesc;
- struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
- struct caam_ctx *ctx = crypto_skcipher_ctx(skcipher);
- struct caam_request *caam_req = skcipher_request_ctx(req);
- int ret;
- if (!req->cryptlen)
- return 0;
- /* allocate extended descriptor */
- edesc = skcipher_edesc_alloc(req);
- if (IS_ERR(edesc))
- return PTR_ERR(edesc);
- caam_req->flc = &ctx->flc[DECRYPT];
- caam_req->flc_dma = ctx->flc_dma[DECRYPT];
- caam_req->cbk = skcipher_decrypt_done;
- caam_req->ctx = &req->base;
- caam_req->edesc = edesc;
- ret = dpaa2_caam_enqueue(ctx->dev, caam_req);
- if (ret != -EINPROGRESS &&
- !(ret == -EBUSY && req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG)) {
- skcipher_unmap(ctx->dev, edesc, req);
- qi_cache_free(edesc);
- }
- return ret;
- }
- static int caam_cra_init(struct caam_ctx *ctx, struct caam_alg_entry *caam,
- bool uses_dkp)
- {
- dma_addr_t dma_addr;
- int i;
- /* copy descriptor header template value */
- ctx->cdata.algtype = OP_TYPE_CLASS1_ALG | caam->class1_alg_type;
- ctx->adata.algtype = OP_TYPE_CLASS2_ALG | caam->class2_alg_type;
- ctx->dev = caam->dev;
- ctx->dir = uses_dkp ? DMA_BIDIRECTIONAL : DMA_TO_DEVICE;
- dma_addr = dma_map_single_attrs(ctx->dev, ctx->flc,
- offsetof(struct caam_ctx, flc_dma),
- ctx->dir, DMA_ATTR_SKIP_CPU_SYNC);
- if (dma_mapping_error(ctx->dev, dma_addr)) {
- dev_err(ctx->dev, "unable to map key, shared descriptors\n");
- return -ENOMEM;
- }
- for (i = 0; i < NUM_OP; i++)
- ctx->flc_dma[i] = dma_addr + i * sizeof(ctx->flc[i]);
- ctx->key_dma = dma_addr + NUM_OP * sizeof(ctx->flc[0]);
- return 0;
- }
- static int caam_cra_init_skcipher(struct crypto_skcipher *tfm)
- {
- struct skcipher_alg *alg = crypto_skcipher_alg(tfm);
- struct caam_skcipher_alg *caam_alg =
- container_of(alg, typeof(*caam_alg), skcipher);
- crypto_skcipher_set_reqsize(tfm, sizeof(struct caam_request));
- return caam_cra_init(crypto_skcipher_ctx(tfm), &caam_alg->caam, false);
- }
- static int caam_cra_init_aead(struct crypto_aead *tfm)
- {
- struct aead_alg *alg = crypto_aead_alg(tfm);
- struct caam_aead_alg *caam_alg = container_of(alg, typeof(*caam_alg),
- aead);
- crypto_aead_set_reqsize(tfm, sizeof(struct caam_request));
- return caam_cra_init(crypto_aead_ctx(tfm), &caam_alg->caam,
- !caam_alg->caam.nodkp);
- }
- static void caam_exit_common(struct caam_ctx *ctx)
- {
- dma_unmap_single_attrs(ctx->dev, ctx->flc_dma[0],
- offsetof(struct caam_ctx, flc_dma), ctx->dir,
- DMA_ATTR_SKIP_CPU_SYNC);
- }
- static void caam_cra_exit(struct crypto_skcipher *tfm)
- {
- caam_exit_common(crypto_skcipher_ctx(tfm));
- }
- static void caam_cra_exit_aead(struct crypto_aead *tfm)
- {
- caam_exit_common(crypto_aead_ctx(tfm));
- }
- static struct caam_skcipher_alg driver_algs[] = {
- {
- .skcipher = {
- .base = {
- .cra_name = "cbc(aes)",
- .cra_driver_name = "cbc-aes-caam-qi2",
- .cra_blocksize = AES_BLOCK_SIZE,
- },
- .setkey = aes_skcipher_setkey,
- .encrypt = skcipher_encrypt,
- .decrypt = skcipher_decrypt,
- .min_keysize = AES_MIN_KEY_SIZE,
- .max_keysize = AES_MAX_KEY_SIZE,
- .ivsize = AES_BLOCK_SIZE,
- },
- .caam.class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
- },
- {
- .skcipher = {
- .base = {
- .cra_name = "cbc(des3_ede)",
- .cra_driver_name = "cbc-3des-caam-qi2",
- .cra_blocksize = DES3_EDE_BLOCK_SIZE,
- },
- .setkey = des3_skcipher_setkey,
- .encrypt = skcipher_encrypt,
- .decrypt = skcipher_decrypt,
- .min_keysize = DES3_EDE_KEY_SIZE,
- .max_keysize = DES3_EDE_KEY_SIZE,
- .ivsize = DES3_EDE_BLOCK_SIZE,
- },
- .caam.class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
- },
- {
- .skcipher = {
- .base = {
- .cra_name = "cbc(des)",
- .cra_driver_name = "cbc-des-caam-qi2",
- .cra_blocksize = DES_BLOCK_SIZE,
- },
- .setkey = des_skcipher_setkey,
- .encrypt = skcipher_encrypt,
- .decrypt = skcipher_decrypt,
- .min_keysize = DES_KEY_SIZE,
- .max_keysize = DES_KEY_SIZE,
- .ivsize = DES_BLOCK_SIZE,
- },
- .caam.class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
- },
- {
- .skcipher = {
- .base = {
- .cra_name = "ctr(aes)",
- .cra_driver_name = "ctr-aes-caam-qi2",
- .cra_blocksize = 1,
- },
- .setkey = ctr_skcipher_setkey,
- .encrypt = skcipher_encrypt,
- .decrypt = skcipher_decrypt,
- .min_keysize = AES_MIN_KEY_SIZE,
- .max_keysize = AES_MAX_KEY_SIZE,
- .ivsize = AES_BLOCK_SIZE,
- .chunksize = AES_BLOCK_SIZE,
- },
- .caam.class1_alg_type = OP_ALG_ALGSEL_AES |
- OP_ALG_AAI_CTR_MOD128,
- },
- {
- .skcipher = {
- .base = {
- .cra_name = "rfc3686(ctr(aes))",
- .cra_driver_name = "rfc3686-ctr-aes-caam-qi2",
- .cra_blocksize = 1,
- },
- .setkey = rfc3686_skcipher_setkey,
- .encrypt = skcipher_encrypt,
- .decrypt = skcipher_decrypt,
- .min_keysize = AES_MIN_KEY_SIZE +
- CTR_RFC3686_NONCE_SIZE,
- .max_keysize = AES_MAX_KEY_SIZE +
- CTR_RFC3686_NONCE_SIZE,
- .ivsize = CTR_RFC3686_IV_SIZE,
- .chunksize = AES_BLOCK_SIZE,
- },
- .caam = {
- .class1_alg_type = OP_ALG_ALGSEL_AES |
- OP_ALG_AAI_CTR_MOD128,
- .rfc3686 = true,
- },
- },
- {
- .skcipher = {
- .base = {
- .cra_name = "xts(aes)",
- .cra_driver_name = "xts-aes-caam-qi2",
- .cra_blocksize = AES_BLOCK_SIZE,
- },
- .setkey = xts_skcipher_setkey,
- .encrypt = skcipher_encrypt,
- .decrypt = skcipher_decrypt,
- .min_keysize = 2 * AES_MIN_KEY_SIZE,
- .max_keysize = 2 * AES_MAX_KEY_SIZE,
- .ivsize = AES_BLOCK_SIZE,
- },
- .caam.class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_XTS,
- },
- {
- .skcipher = {
- .base = {
- .cra_name = "chacha20",
- .cra_driver_name = "chacha20-caam-qi2",
- .cra_blocksize = 1,
- },
- .setkey = chacha20_skcipher_setkey,
- .encrypt = skcipher_encrypt,
- .decrypt = skcipher_decrypt,
- .min_keysize = CHACHA_KEY_SIZE,
- .max_keysize = CHACHA_KEY_SIZE,
- .ivsize = CHACHA_IV_SIZE,
- },
- .caam.class1_alg_type = OP_ALG_ALGSEL_CHACHA20,
- },
- };
- static struct caam_aead_alg driver_aeads[] = {
- {
- .aead = {
- .base = {
- .cra_name = "rfc4106(gcm(aes))",
- .cra_driver_name = "rfc4106-gcm-aes-caam-qi2",
- .cra_blocksize = 1,
- },
- .setkey = rfc4106_setkey,
- .setauthsize = rfc4106_setauthsize,
- .encrypt = ipsec_gcm_encrypt,
- .decrypt = ipsec_gcm_decrypt,
- .ivsize = 8,
- .maxauthsize = AES_BLOCK_SIZE,
- },
- .caam = {
- .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_GCM,
- .nodkp = true,
- },
- },
- {
- .aead = {
- .base = {
- .cra_name = "rfc4543(gcm(aes))",
- .cra_driver_name = "rfc4543-gcm-aes-caam-qi2",
- .cra_blocksize = 1,
- },
- .setkey = rfc4543_setkey,
- .setauthsize = rfc4543_setauthsize,
- .encrypt = ipsec_gcm_encrypt,
- .decrypt = ipsec_gcm_decrypt,
- .ivsize = 8,
- .maxauthsize = AES_BLOCK_SIZE,
- },
- .caam = {
- .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_GCM,
- .nodkp = true,
- },
- },
- /* Galois Counter Mode */
- {
- .aead = {
- .base = {
- .cra_name = "gcm(aes)",
- .cra_driver_name = "gcm-aes-caam-qi2",
- .cra_blocksize = 1,
- },
- .setkey = gcm_setkey,
- .setauthsize = gcm_setauthsize,
- .encrypt = aead_encrypt,
- .decrypt = aead_decrypt,
- .ivsize = 12,
- .maxauthsize = AES_BLOCK_SIZE,
- },
- .caam = {
- .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_GCM,
- .nodkp = true,
- }
- },
- /* single-pass ipsec_esp descriptor */
- {
- .aead = {
- .base = {
- .cra_name = "authenc(hmac(md5),cbc(aes))",
- .cra_driver_name = "authenc-hmac-md5-"
- "cbc-aes-caam-qi2",
- .cra_blocksize = AES_BLOCK_SIZE,
- },
- .setkey = aead_setkey,
- .setauthsize = aead_setauthsize,
- .encrypt = aead_encrypt,
- .decrypt = aead_decrypt,
- .ivsize = AES_BLOCK_SIZE,
- .maxauthsize = MD5_DIGEST_SIZE,
- },
- .caam = {
- .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
- .class2_alg_type = OP_ALG_ALGSEL_MD5 |
- OP_ALG_AAI_HMAC_PRECOMP,
- }
- },
- {
- .aead = {
- .base = {
- .cra_name = "echainiv(authenc(hmac(md5),"
- "cbc(aes)))",
- .cra_driver_name = "echainiv-authenc-hmac-md5-"
- "cbc-aes-caam-qi2",
- .cra_blocksize = AES_BLOCK_SIZE,
- },
- .setkey = aead_setkey,
- .setauthsize = aead_setauthsize,
- .encrypt = aead_encrypt,
- .decrypt = aead_decrypt,
- .ivsize = AES_BLOCK_SIZE,
- .maxauthsize = MD5_DIGEST_SIZE,
- },
- .caam = {
- .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
- .class2_alg_type = OP_ALG_ALGSEL_MD5 |
- OP_ALG_AAI_HMAC_PRECOMP,
- .geniv = true,
- }
- },
- {
- .aead = {
- .base = {
- .cra_name = "authenc(hmac(sha1),cbc(aes))",
- .cra_driver_name = "authenc-hmac-sha1-"
- "cbc-aes-caam-qi2",
- .cra_blocksize = AES_BLOCK_SIZE,
- },
- .setkey = aead_setkey,
- .setauthsize = aead_setauthsize,
- .encrypt = aead_encrypt,
- .decrypt = aead_decrypt,
- .ivsize = AES_BLOCK_SIZE,
- .maxauthsize = SHA1_DIGEST_SIZE,
- },
- .caam = {
- .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
- .class2_alg_type = OP_ALG_ALGSEL_SHA1 |
- OP_ALG_AAI_HMAC_PRECOMP,
- }
- },
- {
- .aead = {
- .base = {
- .cra_name = "echainiv(authenc(hmac(sha1),"
- "cbc(aes)))",
- .cra_driver_name = "echainiv-authenc-"
- "hmac-sha1-cbc-aes-caam-qi2",
- .cra_blocksize = AES_BLOCK_SIZE,
- },
- .setkey = aead_setkey,
- .setauthsize = aead_setauthsize,
- .encrypt = aead_encrypt,
- .decrypt = aead_decrypt,
- .ivsize = AES_BLOCK_SIZE,
- .maxauthsize = SHA1_DIGEST_SIZE,
- },
- .caam = {
- .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
- .class2_alg_type = OP_ALG_ALGSEL_SHA1 |
- OP_ALG_AAI_HMAC_PRECOMP,
- .geniv = true,
- },
- },
- {
- .aead = {
- .base = {
- .cra_name = "authenc(hmac(sha224),cbc(aes))",
- .cra_driver_name = "authenc-hmac-sha224-"
- "cbc-aes-caam-qi2",
- .cra_blocksize = AES_BLOCK_SIZE,
- },
- .setkey = aead_setkey,
- .setauthsize = aead_setauthsize,
- .encrypt = aead_encrypt,
- .decrypt = aead_decrypt,
- .ivsize = AES_BLOCK_SIZE,
- .maxauthsize = SHA224_DIGEST_SIZE,
- },
- .caam = {
- .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
- .class2_alg_type = OP_ALG_ALGSEL_SHA224 |
- OP_ALG_AAI_HMAC_PRECOMP,
- }
- },
- {
- .aead = {
- .base = {
- .cra_name = "echainiv(authenc(hmac(sha224),"
- "cbc(aes)))",
- .cra_driver_name = "echainiv-authenc-"
- "hmac-sha224-cbc-aes-caam-qi2",
- .cra_blocksize = AES_BLOCK_SIZE,
- },
- .setkey = aead_setkey,
- .setauthsize = aead_setauthsize,
- .encrypt = aead_encrypt,
- .decrypt = aead_decrypt,
- .ivsize = AES_BLOCK_SIZE,
- .maxauthsize = SHA224_DIGEST_SIZE,
- },
- .caam = {
- .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
- .class2_alg_type = OP_ALG_ALGSEL_SHA224 |
- OP_ALG_AAI_HMAC_PRECOMP,
- .geniv = true,
- }
- },
- {
- .aead = {
- .base = {
- .cra_name = "authenc(hmac(sha256),cbc(aes))",
- .cra_driver_name = "authenc-hmac-sha256-"
- "cbc-aes-caam-qi2",
- .cra_blocksize = AES_BLOCK_SIZE,
- },
- .setkey = aead_setkey,
- .setauthsize = aead_setauthsize,
- .encrypt = aead_encrypt,
- .decrypt = aead_decrypt,
- .ivsize = AES_BLOCK_SIZE,
- .maxauthsize = SHA256_DIGEST_SIZE,
- },
- .caam = {
- .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
- .class2_alg_type = OP_ALG_ALGSEL_SHA256 |
- OP_ALG_AAI_HMAC_PRECOMP,
- }
- },
- {
- .aead = {
- .base = {
- .cra_name = "echainiv(authenc(hmac(sha256),"
- "cbc(aes)))",
- .cra_driver_name = "echainiv-authenc-"
- "hmac-sha256-cbc-aes-"
- "caam-qi2",
- .cra_blocksize = AES_BLOCK_SIZE,
- },
- .setkey = aead_setkey,
- .setauthsize = aead_setauthsize,
- .encrypt = aead_encrypt,
- .decrypt = aead_decrypt,
- .ivsize = AES_BLOCK_SIZE,
- .maxauthsize = SHA256_DIGEST_SIZE,
- },
- .caam = {
- .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
- .class2_alg_type = OP_ALG_ALGSEL_SHA256 |
- OP_ALG_AAI_HMAC_PRECOMP,
- .geniv = true,
- }
- },
- {
- .aead = {
- .base = {
- .cra_name = "authenc(hmac(sha384),cbc(aes))",
- .cra_driver_name = "authenc-hmac-sha384-"
- "cbc-aes-caam-qi2",
- .cra_blocksize = AES_BLOCK_SIZE,
- },
- .setkey = aead_setkey,
- .setauthsize = aead_setauthsize,
- .encrypt = aead_encrypt,
- .decrypt = aead_decrypt,
- .ivsize = AES_BLOCK_SIZE,
- .maxauthsize = SHA384_DIGEST_SIZE,
- },
- .caam = {
- .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
- .class2_alg_type = OP_ALG_ALGSEL_SHA384 |
- OP_ALG_AAI_HMAC_PRECOMP,
- }
- },
- {
- .aead = {
- .base = {
- .cra_name = "echainiv(authenc(hmac(sha384),"
- "cbc(aes)))",
- .cra_driver_name = "echainiv-authenc-"
- "hmac-sha384-cbc-aes-"
- "caam-qi2",
- .cra_blocksize = AES_BLOCK_SIZE,
- },
- .setkey = aead_setkey,
- .setauthsize = aead_setauthsize,
- .encrypt = aead_encrypt,
- .decrypt = aead_decrypt,
- .ivsize = AES_BLOCK_SIZE,
- .maxauthsize = SHA384_DIGEST_SIZE,
- },
- .caam = {
- .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
- .class2_alg_type = OP_ALG_ALGSEL_SHA384 |
- OP_ALG_AAI_HMAC_PRECOMP,
- .geniv = true,
- }
- },
- {
- .aead = {
- .base = {
- .cra_name = "authenc(hmac(sha512),cbc(aes))",
- .cra_driver_name = "authenc-hmac-sha512-"
- "cbc-aes-caam-qi2",
- .cra_blocksize = AES_BLOCK_SIZE,
- },
- .setkey = aead_setkey,
- .setauthsize = aead_setauthsize,
- .encrypt = aead_encrypt,
- .decrypt = aead_decrypt,
- .ivsize = AES_BLOCK_SIZE,
- .maxauthsize = SHA512_DIGEST_SIZE,
- },
- .caam = {
- .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
- .class2_alg_type = OP_ALG_ALGSEL_SHA512 |
- OP_ALG_AAI_HMAC_PRECOMP,
- }
- },
- {
- .aead = {
- .base = {
- .cra_name = "echainiv(authenc(hmac(sha512),"
- "cbc(aes)))",
- .cra_driver_name = "echainiv-authenc-"
- "hmac-sha512-cbc-aes-"
- "caam-qi2",
- .cra_blocksize = AES_BLOCK_SIZE,
- },
- .setkey = aead_setkey,
- .setauthsize = aead_setauthsize,
- .encrypt = aead_encrypt,
- .decrypt = aead_decrypt,
- .ivsize = AES_BLOCK_SIZE,
- .maxauthsize = SHA512_DIGEST_SIZE,
- },
- .caam = {
- .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
- .class2_alg_type = OP_ALG_ALGSEL_SHA512 |
- OP_ALG_AAI_HMAC_PRECOMP,
- .geniv = true,
- }
- },
- {
- .aead = {
- .base = {
- .cra_name = "authenc(hmac(md5),cbc(des3_ede))",
- .cra_driver_name = "authenc-hmac-md5-"
- "cbc-des3_ede-caam-qi2",
- .cra_blocksize = DES3_EDE_BLOCK_SIZE,
- },
- .setkey = des3_aead_setkey,
- .setauthsize = aead_setauthsize,
- .encrypt = aead_encrypt,
- .decrypt = aead_decrypt,
- .ivsize = DES3_EDE_BLOCK_SIZE,
- .maxauthsize = MD5_DIGEST_SIZE,
- },
- .caam = {
- .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
- .class2_alg_type = OP_ALG_ALGSEL_MD5 |
- OP_ALG_AAI_HMAC_PRECOMP,
- }
- },
- {
- .aead = {
- .base = {
- .cra_name = "echainiv(authenc(hmac(md5),"
- "cbc(des3_ede)))",
- .cra_driver_name = "echainiv-authenc-hmac-md5-"
- "cbc-des3_ede-caam-qi2",
- .cra_blocksize = DES3_EDE_BLOCK_SIZE,
- },
- .setkey = des3_aead_setkey,
- .setauthsize = aead_setauthsize,
- .encrypt = aead_encrypt,
- .decrypt = aead_decrypt,
- .ivsize = DES3_EDE_BLOCK_SIZE,
- .maxauthsize = MD5_DIGEST_SIZE,
- },
- .caam = {
- .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
- .class2_alg_type = OP_ALG_ALGSEL_MD5 |
- OP_ALG_AAI_HMAC_PRECOMP,
- .geniv = true,
- }
- },
- {
- .aead = {
- .base = {
- .cra_name = "authenc(hmac(sha1),"
- "cbc(des3_ede))",
- .cra_driver_name = "authenc-hmac-sha1-"
- "cbc-des3_ede-caam-qi2",
- .cra_blocksize = DES3_EDE_BLOCK_SIZE,
- },
- .setkey = des3_aead_setkey,
- .setauthsize = aead_setauthsize,
- .encrypt = aead_encrypt,
- .decrypt = aead_decrypt,
- .ivsize = DES3_EDE_BLOCK_SIZE,
- .maxauthsize = SHA1_DIGEST_SIZE,
- },
- .caam = {
- .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
- .class2_alg_type = OP_ALG_ALGSEL_SHA1 |
- OP_ALG_AAI_HMAC_PRECOMP,
- },
- },
- {
- .aead = {
- .base = {
- .cra_name = "echainiv(authenc(hmac(sha1),"
- "cbc(des3_ede)))",
- .cra_driver_name = "echainiv-authenc-"
- "hmac-sha1-"
- "cbc-des3_ede-caam-qi2",
- .cra_blocksize = DES3_EDE_BLOCK_SIZE,
- },
- .setkey = des3_aead_setkey,
- .setauthsize = aead_setauthsize,
- .encrypt = aead_encrypt,
- .decrypt = aead_decrypt,
- .ivsize = DES3_EDE_BLOCK_SIZE,
- .maxauthsize = SHA1_DIGEST_SIZE,
- },
- .caam = {
- .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
- .class2_alg_type = OP_ALG_ALGSEL_SHA1 |
- OP_ALG_AAI_HMAC_PRECOMP,
- .geniv = true,
- }
- },
- {
- .aead = {
- .base = {
- .cra_name = "authenc(hmac(sha224),"
- "cbc(des3_ede))",
- .cra_driver_name = "authenc-hmac-sha224-"
- "cbc-des3_ede-caam-qi2",
- .cra_blocksize = DES3_EDE_BLOCK_SIZE,
- },
- .setkey = des3_aead_setkey,
- .setauthsize = aead_setauthsize,
- .encrypt = aead_encrypt,
- .decrypt = aead_decrypt,
- .ivsize = DES3_EDE_BLOCK_SIZE,
- .maxauthsize = SHA224_DIGEST_SIZE,
- },
- .caam = {
- .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
- .class2_alg_type = OP_ALG_ALGSEL_SHA224 |
- OP_ALG_AAI_HMAC_PRECOMP,
- },
- },
- {
- .aead = {
- .base = {
- .cra_name = "echainiv(authenc(hmac(sha224),"
- "cbc(des3_ede)))",
- .cra_driver_name = "echainiv-authenc-"
- "hmac-sha224-"
- "cbc-des3_ede-caam-qi2",
- .cra_blocksize = DES3_EDE_BLOCK_SIZE,
- },
- .setkey = des3_aead_setkey,
- .setauthsize = aead_setauthsize,
- .encrypt = aead_encrypt,
- .decrypt = aead_decrypt,
- .ivsize = DES3_EDE_BLOCK_SIZE,
- .maxauthsize = SHA224_DIGEST_SIZE,
- },
- .caam = {
- .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
- .class2_alg_type = OP_ALG_ALGSEL_SHA224 |
- OP_ALG_AAI_HMAC_PRECOMP,
- .geniv = true,
- }
- },
- {
- .aead = {
- .base = {
- .cra_name = "authenc(hmac(sha256),"
- "cbc(des3_ede))",
- .cra_driver_name = "authenc-hmac-sha256-"
- "cbc-des3_ede-caam-qi2",
- .cra_blocksize = DES3_EDE_BLOCK_SIZE,
- },
- .setkey = des3_aead_setkey,
- .setauthsize = aead_setauthsize,
- .encrypt = aead_encrypt,
- .decrypt = aead_decrypt,
- .ivsize = DES3_EDE_BLOCK_SIZE,
- .maxauthsize = SHA256_DIGEST_SIZE,
- },
- .caam = {
- .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
- .class2_alg_type = OP_ALG_ALGSEL_SHA256 |
- OP_ALG_AAI_HMAC_PRECOMP,
- },
- },
- {
- .aead = {
- .base = {
- .cra_name = "echainiv(authenc(hmac(sha256),"
- "cbc(des3_ede)))",
- .cra_driver_name = "echainiv-authenc-"
- "hmac-sha256-"
- "cbc-des3_ede-caam-qi2",
- .cra_blocksize = DES3_EDE_BLOCK_SIZE,
- },
- .setkey = des3_aead_setkey,
- .setauthsize = aead_setauthsize,
- .encrypt = aead_encrypt,
- .decrypt = aead_decrypt,
- .ivsize = DES3_EDE_BLOCK_SIZE,
- .maxauthsize = SHA256_DIGEST_SIZE,
- },
- .caam = {
- .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
- .class2_alg_type = OP_ALG_ALGSEL_SHA256 |
- OP_ALG_AAI_HMAC_PRECOMP,
- .geniv = true,
- }
- },
- {
- .aead = {
- .base = {
- .cra_name = "authenc(hmac(sha384),"
- "cbc(des3_ede))",
- .cra_driver_name = "authenc-hmac-sha384-"
- "cbc-des3_ede-caam-qi2",
- .cra_blocksize = DES3_EDE_BLOCK_SIZE,
- },
- .setkey = des3_aead_setkey,
- .setauthsize = aead_setauthsize,
- .encrypt = aead_encrypt,
- .decrypt = aead_decrypt,
- .ivsize = DES3_EDE_BLOCK_SIZE,
- .maxauthsize = SHA384_DIGEST_SIZE,
- },
- .caam = {
- .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
- .class2_alg_type = OP_ALG_ALGSEL_SHA384 |
- OP_ALG_AAI_HMAC_PRECOMP,
- },
- },
- {
- .aead = {
- .base = {
- .cra_name = "echainiv(authenc(hmac(sha384),"
- "cbc(des3_ede)))",
- .cra_driver_name = "echainiv-authenc-"
- "hmac-sha384-"
- "cbc-des3_ede-caam-qi2",
- .cra_blocksize = DES3_EDE_BLOCK_SIZE,
- },
- .setkey = des3_aead_setkey,
- .setauthsize = aead_setauthsize,
- .encrypt = aead_encrypt,
- .decrypt = aead_decrypt,
- .ivsize = DES3_EDE_BLOCK_SIZE,
- .maxauthsize = SHA384_DIGEST_SIZE,
- },
- .caam = {
- .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
- .class2_alg_type = OP_ALG_ALGSEL_SHA384 |
- OP_ALG_AAI_HMAC_PRECOMP,
- .geniv = true,
- }
- },
- {
- .aead = {
- .base = {
- .cra_name = "authenc(hmac(sha512),"
- "cbc(des3_ede))",
- .cra_driver_name = "authenc-hmac-sha512-"
- "cbc-des3_ede-caam-qi2",
- .cra_blocksize = DES3_EDE_BLOCK_SIZE,
- },
- .setkey = des3_aead_setkey,
- .setauthsize = aead_setauthsize,
- .encrypt = aead_encrypt,
- .decrypt = aead_decrypt,
- .ivsize = DES3_EDE_BLOCK_SIZE,
- .maxauthsize = SHA512_DIGEST_SIZE,
- },
- .caam = {
- .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
- .class2_alg_type = OP_ALG_ALGSEL_SHA512 |
- OP_ALG_AAI_HMAC_PRECOMP,
- },
- },
- {
- .aead = {
- .base = {
- .cra_name = "echainiv(authenc(hmac(sha512),"
- "cbc(des3_ede)))",
- .cra_driver_name = "echainiv-authenc-"
- "hmac-sha512-"
- "cbc-des3_ede-caam-qi2",
- .cra_blocksize = DES3_EDE_BLOCK_SIZE,
- },
- .setkey = des3_aead_setkey,
- .setauthsize = aead_setauthsize,
- .encrypt = aead_encrypt,
- .decrypt = aead_decrypt,
- .ivsize = DES3_EDE_BLOCK_SIZE,
- .maxauthsize = SHA512_DIGEST_SIZE,
- },
- .caam = {
- .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
- .class2_alg_type = OP_ALG_ALGSEL_SHA512 |
- OP_ALG_AAI_HMAC_PRECOMP,
- .geniv = true,
- }
- },
- {
- .aead = {
- .base = {
- .cra_name = "authenc(hmac(md5),cbc(des))",
- .cra_driver_name = "authenc-hmac-md5-"
- "cbc-des-caam-qi2",
- .cra_blocksize = DES_BLOCK_SIZE,
- },
- .setkey = aead_setkey,
- .setauthsize = aead_setauthsize,
- .encrypt = aead_encrypt,
- .decrypt = aead_decrypt,
- .ivsize = DES_BLOCK_SIZE,
- .maxauthsize = MD5_DIGEST_SIZE,
- },
- .caam = {
- .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
- .class2_alg_type = OP_ALG_ALGSEL_MD5 |
- OP_ALG_AAI_HMAC_PRECOMP,
- },
- },
- {
- .aead = {
- .base = {
- .cra_name = "echainiv(authenc(hmac(md5),"
- "cbc(des)))",
- .cra_driver_name = "echainiv-authenc-hmac-md5-"
- "cbc-des-caam-qi2",
- .cra_blocksize = DES_BLOCK_SIZE,
- },
- .setkey = aead_setkey,
- .setauthsize = aead_setauthsize,
- .encrypt = aead_encrypt,
- .decrypt = aead_decrypt,
- .ivsize = DES_BLOCK_SIZE,
- .maxauthsize = MD5_DIGEST_SIZE,
- },
- .caam = {
- .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
- .class2_alg_type = OP_ALG_ALGSEL_MD5 |
- OP_ALG_AAI_HMAC_PRECOMP,
- .geniv = true,
- }
- },
- {
- .aead = {
- .base = {
- .cra_name = "authenc(hmac(sha1),cbc(des))",
- .cra_driver_name = "authenc-hmac-sha1-"
- "cbc-des-caam-qi2",
- .cra_blocksize = DES_BLOCK_SIZE,
- },
- .setkey = aead_setkey,
- .setauthsize = aead_setauthsize,
- .encrypt = aead_encrypt,
- .decrypt = aead_decrypt,
- .ivsize = DES_BLOCK_SIZE,
- .maxauthsize = SHA1_DIGEST_SIZE,
- },
- .caam = {
- .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
- .class2_alg_type = OP_ALG_ALGSEL_SHA1 |
- OP_ALG_AAI_HMAC_PRECOMP,
- },
- },
- {
- .aead = {
- .base = {
- .cra_name = "echainiv(authenc(hmac(sha1),"
- "cbc(des)))",
- .cra_driver_name = "echainiv-authenc-"
- "hmac-sha1-cbc-des-caam-qi2",
- .cra_blocksize = DES_BLOCK_SIZE,
- },
- .setkey = aead_setkey,
- .setauthsize = aead_setauthsize,
- .encrypt = aead_encrypt,
- .decrypt = aead_decrypt,
- .ivsize = DES_BLOCK_SIZE,
- .maxauthsize = SHA1_DIGEST_SIZE,
- },
- .caam = {
- .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
- .class2_alg_type = OP_ALG_ALGSEL_SHA1 |
- OP_ALG_AAI_HMAC_PRECOMP,
- .geniv = true,
- }
- },
- {
- .aead = {
- .base = {
- .cra_name = "authenc(hmac(sha224),cbc(des))",
- .cra_driver_name = "authenc-hmac-sha224-"
- "cbc-des-caam-qi2",
- .cra_blocksize = DES_BLOCK_SIZE,
- },
- .setkey = aead_setkey,
- .setauthsize = aead_setauthsize,
- .encrypt = aead_encrypt,
- .decrypt = aead_decrypt,
- .ivsize = DES_BLOCK_SIZE,
- .maxauthsize = SHA224_DIGEST_SIZE,
- },
- .caam = {
- .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
- .class2_alg_type = OP_ALG_ALGSEL_SHA224 |
- OP_ALG_AAI_HMAC_PRECOMP,
- },
- },
- {
- .aead = {
- .base = {
- .cra_name = "echainiv(authenc(hmac(sha224),"
- "cbc(des)))",
- .cra_driver_name = "echainiv-authenc-"
- "hmac-sha224-cbc-des-"
- "caam-qi2",
- .cra_blocksize = DES_BLOCK_SIZE,
- },
- .setkey = aead_setkey,
- .setauthsize = aead_setauthsize,
- .encrypt = aead_encrypt,
- .decrypt = aead_decrypt,
- .ivsize = DES_BLOCK_SIZE,
- .maxauthsize = SHA224_DIGEST_SIZE,
- },
- .caam = {
- .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
- .class2_alg_type = OP_ALG_ALGSEL_SHA224 |
- OP_ALG_AAI_HMAC_PRECOMP,
- .geniv = true,
- }
- },
- {
- .aead = {
- .base = {
- .cra_name = "authenc(hmac(sha256),cbc(des))",
- .cra_driver_name = "authenc-hmac-sha256-"
- "cbc-des-caam-qi2",
- .cra_blocksize = DES_BLOCK_SIZE,
- },
- .setkey = aead_setkey,
- .setauthsize = aead_setauthsize,
- .encrypt = aead_encrypt,
- .decrypt = aead_decrypt,
- .ivsize = DES_BLOCK_SIZE,
- .maxauthsize = SHA256_DIGEST_SIZE,
- },
- .caam = {
- .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
- .class2_alg_type = OP_ALG_ALGSEL_SHA256 |
- OP_ALG_AAI_HMAC_PRECOMP,
- },
- },
- {
- .aead = {
- .base = {
- .cra_name = "echainiv(authenc(hmac(sha256),"
- "cbc(des)))",
- .cra_driver_name = "echainiv-authenc-"
- "hmac-sha256-cbc-des-"
- "caam-qi2",
- .cra_blocksize = DES_BLOCK_SIZE,
- },
- .setkey = aead_setkey,
- .setauthsize = aead_setauthsize,
- .encrypt = aead_encrypt,
- .decrypt = aead_decrypt,
- .ivsize = DES_BLOCK_SIZE,
- .maxauthsize = SHA256_DIGEST_SIZE,
- },
- .caam = {
- .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
- .class2_alg_type = OP_ALG_ALGSEL_SHA256 |
- OP_ALG_AAI_HMAC_PRECOMP,
- .geniv = true,
- },
- },
- {
- .aead = {
- .base = {
- .cra_name = "authenc(hmac(sha384),cbc(des))",
- .cra_driver_name = "authenc-hmac-sha384-"
- "cbc-des-caam-qi2",
- .cra_blocksize = DES_BLOCK_SIZE,
- },
- .setkey = aead_setkey,
- .setauthsize = aead_setauthsize,
- .encrypt = aead_encrypt,
- .decrypt = aead_decrypt,
- .ivsize = DES_BLOCK_SIZE,
- .maxauthsize = SHA384_DIGEST_SIZE,
- },
- .caam = {
- .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
- .class2_alg_type = OP_ALG_ALGSEL_SHA384 |
- OP_ALG_AAI_HMAC_PRECOMP,
- },
- },
- {
- .aead = {
- .base = {
- .cra_name = "echainiv(authenc(hmac(sha384),"
- "cbc(des)))",
- .cra_driver_name = "echainiv-authenc-"
- "hmac-sha384-cbc-des-"
- "caam-qi2",
- .cra_blocksize = DES_BLOCK_SIZE,
- },
- .setkey = aead_setkey,
- .setauthsize = aead_setauthsize,
- .encrypt = aead_encrypt,
- .decrypt = aead_decrypt,
- .ivsize = DES_BLOCK_SIZE,
- .maxauthsize = SHA384_DIGEST_SIZE,
- },
- .caam = {
- .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
- .class2_alg_type = OP_ALG_ALGSEL_SHA384 |
- OP_ALG_AAI_HMAC_PRECOMP,
- .geniv = true,
- }
- },
- {
- .aead = {
- .base = {
- .cra_name = "authenc(hmac(sha512),cbc(des))",
- .cra_driver_name = "authenc-hmac-sha512-"
- "cbc-des-caam-qi2",
- .cra_blocksize = DES_BLOCK_SIZE,
- },
- .setkey = aead_setkey,
- .setauthsize = aead_setauthsize,
- .encrypt = aead_encrypt,
- .decrypt = aead_decrypt,
- .ivsize = DES_BLOCK_SIZE,
- .maxauthsize = SHA512_DIGEST_SIZE,
- },
- .caam = {
- .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
- .class2_alg_type = OP_ALG_ALGSEL_SHA512 |
- OP_ALG_AAI_HMAC_PRECOMP,
- }
- },
- {
- .aead = {
- .base = {
- .cra_name = "echainiv(authenc(hmac(sha512),"
- "cbc(des)))",
- .cra_driver_name = "echainiv-authenc-"
- "hmac-sha512-cbc-des-"
- "caam-qi2",
- .cra_blocksize = DES_BLOCK_SIZE,
- },
- .setkey = aead_setkey,
- .setauthsize = aead_setauthsize,
- .encrypt = aead_encrypt,
- .decrypt = aead_decrypt,
- .ivsize = DES_BLOCK_SIZE,
- .maxauthsize = SHA512_DIGEST_SIZE,
- },
- .caam = {
- .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
- .class2_alg_type = OP_ALG_ALGSEL_SHA512 |
- OP_ALG_AAI_HMAC_PRECOMP,
- .geniv = true,
- }
- },
- {
- .aead = {
- .base = {
- .cra_name = "authenc(hmac(md5),"
- "rfc3686(ctr(aes)))",
- .cra_driver_name = "authenc-hmac-md5-"
- "rfc3686-ctr-aes-caam-qi2",
- .cra_blocksize = 1,
- },
- .setkey = aead_setkey,
- .setauthsize = aead_setauthsize,
- .encrypt = aead_encrypt,
- .decrypt = aead_decrypt,
- .ivsize = CTR_RFC3686_IV_SIZE,
- .maxauthsize = MD5_DIGEST_SIZE,
- },
- .caam = {
- .class1_alg_type = OP_ALG_ALGSEL_AES |
- OP_ALG_AAI_CTR_MOD128,
- .class2_alg_type = OP_ALG_ALGSEL_MD5 |
- OP_ALG_AAI_HMAC_PRECOMP,
- .rfc3686 = true,
- },
- },
- {
- .aead = {
- .base = {
- .cra_name = "seqiv(authenc("
- "hmac(md5),rfc3686(ctr(aes))))",
- .cra_driver_name = "seqiv-authenc-hmac-md5-"
- "rfc3686-ctr-aes-caam-qi2",
- .cra_blocksize = 1,
- },
- .setkey = aead_setkey,
- .setauthsize = aead_setauthsize,
- .encrypt = aead_encrypt,
- .decrypt = aead_decrypt,
- .ivsize = CTR_RFC3686_IV_SIZE,
- .maxauthsize = MD5_DIGEST_SIZE,
- },
- .caam = {
- .class1_alg_type = OP_ALG_ALGSEL_AES |
- OP_ALG_AAI_CTR_MOD128,
- .class2_alg_type = OP_ALG_ALGSEL_MD5 |
- OP_ALG_AAI_HMAC_PRECOMP,
- .rfc3686 = true,
- .geniv = true,
- },
- },
- {
- .aead = {
- .base = {
- .cra_name = "authenc(hmac(sha1),"
- "rfc3686(ctr(aes)))",
- .cra_driver_name = "authenc-hmac-sha1-"
- "rfc3686-ctr-aes-caam-qi2",
- .cra_blocksize = 1,
- },
- .setkey = aead_setkey,
- .setauthsize = aead_setauthsize,
- .encrypt = aead_encrypt,
- .decrypt = aead_decrypt,
- .ivsize = CTR_RFC3686_IV_SIZE,
- .maxauthsize = SHA1_DIGEST_SIZE,
- },
- .caam = {
- .class1_alg_type = OP_ALG_ALGSEL_AES |
- OP_ALG_AAI_CTR_MOD128,
- .class2_alg_type = OP_ALG_ALGSEL_SHA1 |
- OP_ALG_AAI_HMAC_PRECOMP,
- .rfc3686 = true,
- },
- },
- {
- .aead = {
- .base = {
- .cra_name = "seqiv(authenc("
- "hmac(sha1),rfc3686(ctr(aes))))",
- .cra_driver_name = "seqiv-authenc-hmac-sha1-"
- "rfc3686-ctr-aes-caam-qi2",
- .cra_blocksize = 1,
- },
- .setkey = aead_setkey,
- .setauthsize = aead_setauthsize,
- .encrypt = aead_encrypt,
- .decrypt = aead_decrypt,
- .ivsize = CTR_RFC3686_IV_SIZE,
- .maxauthsize = SHA1_DIGEST_SIZE,
- },
- .caam = {
- .class1_alg_type = OP_ALG_ALGSEL_AES |
- OP_ALG_AAI_CTR_MOD128,
- .class2_alg_type = OP_ALG_ALGSEL_SHA1 |
- OP_ALG_AAI_HMAC_PRECOMP,
- .rfc3686 = true,
- .geniv = true,
- },
- },
- {
- .aead = {
- .base = {
- .cra_name = "authenc(hmac(sha224),"
- "rfc3686(ctr(aes)))",
- .cra_driver_name = "authenc-hmac-sha224-"
- "rfc3686-ctr-aes-caam-qi2",
- .cra_blocksize = 1,
- },
- .setkey = aead_setkey,
- .setauthsize = aead_setauthsize,
- .encrypt = aead_encrypt,
- .decrypt = aead_decrypt,
- .ivsize = CTR_RFC3686_IV_SIZE,
- .maxauthsize = SHA224_DIGEST_SIZE,
- },
- .caam = {
- .class1_alg_type = OP_ALG_ALGSEL_AES |
- OP_ALG_AAI_CTR_MOD128,
- .class2_alg_type = OP_ALG_ALGSEL_SHA224 |
- OP_ALG_AAI_HMAC_PRECOMP,
- .rfc3686 = true,
- },
- },
- {
- .aead = {
- .base = {
- .cra_name = "seqiv(authenc("
- "hmac(sha224),rfc3686(ctr(aes))))",
- .cra_driver_name = "seqiv-authenc-hmac-sha224-"
- "rfc3686-ctr-aes-caam-qi2",
- .cra_blocksize = 1,
- },
- .setkey = aead_setkey,
- .setauthsize = aead_setauthsize,
- .encrypt = aead_encrypt,
- .decrypt = aead_decrypt,
- .ivsize = CTR_RFC3686_IV_SIZE,
- .maxauthsize = SHA224_DIGEST_SIZE,
- },
- .caam = {
- .class1_alg_type = OP_ALG_ALGSEL_AES |
- OP_ALG_AAI_CTR_MOD128,
- .class2_alg_type = OP_ALG_ALGSEL_SHA224 |
- OP_ALG_AAI_HMAC_PRECOMP,
- .rfc3686 = true,
- .geniv = true,
- },
- },
- {
- .aead = {
- .base = {
- .cra_name = "authenc(hmac(sha256),"
- "rfc3686(ctr(aes)))",
- .cra_driver_name = "authenc-hmac-sha256-"
- "rfc3686-ctr-aes-caam-qi2",
- .cra_blocksize = 1,
- },
- .setkey = aead_setkey,
- .setauthsize = aead_setauthsize,
- .encrypt = aead_encrypt,
- .decrypt = aead_decrypt,
- .ivsize = CTR_RFC3686_IV_SIZE,
- .maxauthsize = SHA256_DIGEST_SIZE,
- },
- .caam = {
- .class1_alg_type = OP_ALG_ALGSEL_AES |
- OP_ALG_AAI_CTR_MOD128,
- .class2_alg_type = OP_ALG_ALGSEL_SHA256 |
- OP_ALG_AAI_HMAC_PRECOMP,
- .rfc3686 = true,
- },
- },
- {
- .aead = {
- .base = {
- .cra_name = "seqiv(authenc(hmac(sha256),"
- "rfc3686(ctr(aes))))",
- .cra_driver_name = "seqiv-authenc-hmac-sha256-"
- "rfc3686-ctr-aes-caam-qi2",
- .cra_blocksize = 1,
- },
- .setkey = aead_setkey,
- .setauthsize = aead_setauthsize,
- .encrypt = aead_encrypt,
- .decrypt = aead_decrypt,
- .ivsize = CTR_RFC3686_IV_SIZE,
- .maxauthsize = SHA256_DIGEST_SIZE,
- },
- .caam = {
- .class1_alg_type = OP_ALG_ALGSEL_AES |
- OP_ALG_AAI_CTR_MOD128,
- .class2_alg_type = OP_ALG_ALGSEL_SHA256 |
- OP_ALG_AAI_HMAC_PRECOMP,
- .rfc3686 = true,
- .geniv = true,
- },
- },
- {
- .aead = {
- .base = {
- .cra_name = "authenc(hmac(sha384),"
- "rfc3686(ctr(aes)))",
- .cra_driver_name = "authenc-hmac-sha384-"
- "rfc3686-ctr-aes-caam-qi2",
- .cra_blocksize = 1,
- },
- .setkey = aead_setkey,
- .setauthsize = aead_setauthsize,
- .encrypt = aead_encrypt,
- .decrypt = aead_decrypt,
- .ivsize = CTR_RFC3686_IV_SIZE,
- .maxauthsize = SHA384_DIGEST_SIZE,
- },
- .caam = {
- .class1_alg_type = OP_ALG_ALGSEL_AES |
- OP_ALG_AAI_CTR_MOD128,
- .class2_alg_type = OP_ALG_ALGSEL_SHA384 |
- OP_ALG_AAI_HMAC_PRECOMP,
- .rfc3686 = true,
- },
- },
- {
- .aead = {
- .base = {
- .cra_name = "seqiv(authenc(hmac(sha384),"
- "rfc3686(ctr(aes))))",
- .cra_driver_name = "seqiv-authenc-hmac-sha384-"
- "rfc3686-ctr-aes-caam-qi2",
- .cra_blocksize = 1,
- },
- .setkey = aead_setkey,
- .setauthsize = aead_setauthsize,
- .encrypt = aead_encrypt,
- .decrypt = aead_decrypt,
- .ivsize = CTR_RFC3686_IV_SIZE,
- .maxauthsize = SHA384_DIGEST_SIZE,
- },
- .caam = {
- .class1_alg_type = OP_ALG_ALGSEL_AES |
- OP_ALG_AAI_CTR_MOD128,
- .class2_alg_type = OP_ALG_ALGSEL_SHA384 |
- OP_ALG_AAI_HMAC_PRECOMP,
- .rfc3686 = true,
- .geniv = true,
- },
- },
- {
- .aead = {
- .base = {
- .cra_name = "rfc7539(chacha20,poly1305)",
- .cra_driver_name = "rfc7539-chacha20-poly1305-"
- "caam-qi2",
- .cra_blocksize = 1,
- },
- .setkey = chachapoly_setkey,
- .setauthsize = chachapoly_setauthsize,
- .encrypt = aead_encrypt,
- .decrypt = aead_decrypt,
- .ivsize = CHACHAPOLY_IV_SIZE,
- .maxauthsize = POLY1305_DIGEST_SIZE,
- },
- .caam = {
- .class1_alg_type = OP_ALG_ALGSEL_CHACHA20 |
- OP_ALG_AAI_AEAD,
- .class2_alg_type = OP_ALG_ALGSEL_POLY1305 |
- OP_ALG_AAI_AEAD,
- .nodkp = true,
- },
- },
- {
- .aead = {
- .base = {
- .cra_name = "rfc7539esp(chacha20,poly1305)",
- .cra_driver_name = "rfc7539esp-chacha20-"
- "poly1305-caam-qi2",
- .cra_blocksize = 1,
- },
- .setkey = chachapoly_setkey,
- .setauthsize = chachapoly_setauthsize,
- .encrypt = aead_encrypt,
- .decrypt = aead_decrypt,
- .ivsize = 8,
- .maxauthsize = POLY1305_DIGEST_SIZE,
- },
- .caam = {
- .class1_alg_type = OP_ALG_ALGSEL_CHACHA20 |
- OP_ALG_AAI_AEAD,
- .class2_alg_type = OP_ALG_ALGSEL_POLY1305 |
- OP_ALG_AAI_AEAD,
- .nodkp = true,
- },
- },
- {
- .aead = {
- .base = {
- .cra_name = "authenc(hmac(sha512),"
- "rfc3686(ctr(aes)))",
- .cra_driver_name = "authenc-hmac-sha512-"
- "rfc3686-ctr-aes-caam-qi2",
- .cra_blocksize = 1,
- },
- .setkey = aead_setkey,
- .setauthsize = aead_setauthsize,
- .encrypt = aead_encrypt,
- .decrypt = aead_decrypt,
- .ivsize = CTR_RFC3686_IV_SIZE,
- .maxauthsize = SHA512_DIGEST_SIZE,
- },
- .caam = {
- .class1_alg_type = OP_ALG_ALGSEL_AES |
- OP_ALG_AAI_CTR_MOD128,
- .class2_alg_type = OP_ALG_ALGSEL_SHA512 |
- OP_ALG_AAI_HMAC_PRECOMP,
- .rfc3686 = true,
- },
- },
- {
- .aead = {
- .base = {
- .cra_name = "seqiv(authenc(hmac(sha512),"
- "rfc3686(ctr(aes))))",
- .cra_driver_name = "seqiv-authenc-hmac-sha512-"
- "rfc3686-ctr-aes-caam-qi2",
- .cra_blocksize = 1,
- },
- .setkey = aead_setkey,
- .setauthsize = aead_setauthsize,
- .encrypt = aead_encrypt,
- .decrypt = aead_decrypt,
- .ivsize = CTR_RFC3686_IV_SIZE,
- .maxauthsize = SHA512_DIGEST_SIZE,
- },
- .caam = {
- .class1_alg_type = OP_ALG_ALGSEL_AES |
- OP_ALG_AAI_CTR_MOD128,
- .class2_alg_type = OP_ALG_ALGSEL_SHA512 |
- OP_ALG_AAI_HMAC_PRECOMP,
- .rfc3686 = true,
- .geniv = true,
- },
- },
- };
- static void caam_skcipher_alg_init(struct caam_skcipher_alg *t_alg)
- {
- struct skcipher_alg *alg = &t_alg->skcipher;
- alg->base.cra_module = THIS_MODULE;
- alg->base.cra_priority = CAAM_CRA_PRIORITY;
- alg->base.cra_ctxsize = sizeof(struct caam_ctx);
- alg->base.cra_flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_KERN_DRIVER_ONLY;
- alg->init = caam_cra_init_skcipher;
- alg->exit = caam_cra_exit;
- }
- static void caam_aead_alg_init(struct caam_aead_alg *t_alg)
- {
- struct aead_alg *alg = &t_alg->aead;
- alg->base.cra_module = THIS_MODULE;
- alg->base.cra_priority = CAAM_CRA_PRIORITY;
- alg->base.cra_ctxsize = sizeof(struct caam_ctx);
- alg->base.cra_flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_KERN_DRIVER_ONLY;
- alg->init = caam_cra_init_aead;
- alg->exit = caam_cra_exit_aead;
- }
- /* max hash key is max split key size */
- #define CAAM_MAX_HASH_KEY_SIZE (SHA512_DIGEST_SIZE * 2)
- #define CAAM_MAX_HASH_BLOCK_SIZE SHA512_BLOCK_SIZE
- /* caam context sizes for hashes: running digest + 8 */
- #define HASH_MSG_LEN 8
- #define MAX_CTX_LEN (HASH_MSG_LEN + SHA512_DIGEST_SIZE)
- enum hash_optype {
- UPDATE = 0,
- UPDATE_FIRST,
- FINALIZE,
- DIGEST,
- HASH_NUM_OP
- };
- /**
- * caam_hash_ctx - ahash per-session context
- * @flc: Flow Contexts array
- * @key: authentication key
- * @flc_dma: I/O virtual addresses of the Flow Contexts
- * @dev: dpseci device
- * @ctx_len: size of Context Register
- * @adata: hashing algorithm details
- */
- struct caam_hash_ctx {
- struct caam_flc flc[HASH_NUM_OP];
- u8 key[CAAM_MAX_HASH_BLOCK_SIZE] ____cacheline_aligned;
- dma_addr_t flc_dma[HASH_NUM_OP];
- struct device *dev;
- int ctx_len;
- struct alginfo adata;
- };
- /* ahash state */
- struct caam_hash_state {
- struct caam_request caam_req;
- dma_addr_t buf_dma;
- dma_addr_t ctx_dma;
- int ctx_dma_len;
- u8 buf_0[CAAM_MAX_HASH_BLOCK_SIZE] ____cacheline_aligned;
- int buflen_0;
- u8 buf_1[CAAM_MAX_HASH_BLOCK_SIZE] ____cacheline_aligned;
- int buflen_1;
- u8 caam_ctx[MAX_CTX_LEN] ____cacheline_aligned;
- int (*update)(struct ahash_request *req);
- int (*final)(struct ahash_request *req);
- int (*finup)(struct ahash_request *req);
- int current_buf;
- };
- struct caam_export_state {
- u8 buf[CAAM_MAX_HASH_BLOCK_SIZE];
- u8 caam_ctx[MAX_CTX_LEN];
- int buflen;
- int (*update)(struct ahash_request *req);
- int (*final)(struct ahash_request *req);
- int (*finup)(struct ahash_request *req);
- };
- static inline void switch_buf(struct caam_hash_state *state)
- {
- state->current_buf ^= 1;
- }
- static inline u8 *current_buf(struct caam_hash_state *state)
- {
- return state->current_buf ? state->buf_1 : state->buf_0;
- }
- static inline u8 *alt_buf(struct caam_hash_state *state)
- {
- return state->current_buf ? state->buf_0 : state->buf_1;
- }
- static inline int *current_buflen(struct caam_hash_state *state)
- {
- return state->current_buf ? &state->buflen_1 : &state->buflen_0;
- }
- static inline int *alt_buflen(struct caam_hash_state *state)
- {
- return state->current_buf ? &state->buflen_0 : &state->buflen_1;
- }
- /* Map current buffer in state (if length > 0) and put it in link table */
- static inline int buf_map_to_qm_sg(struct device *dev,
- struct dpaa2_sg_entry *qm_sg,
- struct caam_hash_state *state)
- {
- int buflen = *current_buflen(state);
- if (!buflen)
- return 0;
- state->buf_dma = dma_map_single(dev, current_buf(state), buflen,
- DMA_TO_DEVICE);
- if (dma_mapping_error(dev, state->buf_dma)) {
- dev_err(dev, "unable to map buf\n");
- state->buf_dma = 0;
- return -ENOMEM;
- }
- dma_to_qm_sg_one(qm_sg, state->buf_dma, buflen, 0);
- return 0;
- }
- /* Map state->caam_ctx, and add it to link table */
- static inline int ctx_map_to_qm_sg(struct device *dev,
- struct caam_hash_state *state, int ctx_len,
- struct dpaa2_sg_entry *qm_sg, u32 flag)
- {
- state->ctx_dma_len = ctx_len;
- state->ctx_dma = dma_map_single(dev, state->caam_ctx, ctx_len, flag);
- if (dma_mapping_error(dev, state->ctx_dma)) {
- dev_err(dev, "unable to map ctx\n");
- state->ctx_dma = 0;
- return -ENOMEM;
- }
- dma_to_qm_sg_one(qm_sg, state->ctx_dma, ctx_len, 0);
- return 0;
- }
- static int ahash_set_sh_desc(struct crypto_ahash *ahash)
- {
- struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
- int digestsize = crypto_ahash_digestsize(ahash);
- struct dpaa2_caam_priv *priv = dev_get_drvdata(ctx->dev);
- struct caam_flc *flc;
- u32 *desc;
- /* ahash_update shared descriptor */
- flc = &ctx->flc[UPDATE];
- desc = flc->sh_desc;
- cnstr_shdsc_ahash(desc, &ctx->adata, OP_ALG_AS_UPDATE, ctx->ctx_len,
- ctx->ctx_len, true, priv->sec_attr.era);
- flc->flc[1] = cpu_to_caam32(desc_len(desc)); /* SDL */
- dma_sync_single_for_device(ctx->dev, ctx->flc_dma[UPDATE],
- desc_bytes(desc), DMA_BIDIRECTIONAL);
- print_hex_dump_debug("ahash update shdesc@" __stringify(__LINE__)": ",
- DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc),
- 1);
- /* ahash_update_first shared descriptor */
- flc = &ctx->flc[UPDATE_FIRST];
- desc = flc->sh_desc;
- cnstr_shdsc_ahash(desc, &ctx->adata, OP_ALG_AS_INIT, ctx->ctx_len,
- ctx->ctx_len, false, priv->sec_attr.era);
- flc->flc[1] = cpu_to_caam32(desc_len(desc)); /* SDL */
- dma_sync_single_for_device(ctx->dev, ctx->flc_dma[UPDATE_FIRST],
- desc_bytes(desc), DMA_BIDIRECTIONAL);
- print_hex_dump_debug("ahash update first shdesc@" __stringify(__LINE__)": ",
- DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc),
- 1);
- /* ahash_final shared descriptor */
- flc = &ctx->flc[FINALIZE];
- desc = flc->sh_desc;
- cnstr_shdsc_ahash(desc, &ctx->adata, OP_ALG_AS_FINALIZE, digestsize,
- ctx->ctx_len, true, priv->sec_attr.era);
- flc->flc[1] = cpu_to_caam32(desc_len(desc)); /* SDL */
- dma_sync_single_for_device(ctx->dev, ctx->flc_dma[FINALIZE],
- desc_bytes(desc), DMA_BIDIRECTIONAL);
- print_hex_dump_debug("ahash final shdesc@" __stringify(__LINE__)": ",
- DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc),
- 1);
- /* ahash_digest shared descriptor */
- flc = &ctx->flc[DIGEST];
- desc = flc->sh_desc;
- cnstr_shdsc_ahash(desc, &ctx->adata, OP_ALG_AS_INITFINAL, digestsize,
- ctx->ctx_len, false, priv->sec_attr.era);
- flc->flc[1] = cpu_to_caam32(desc_len(desc)); /* SDL */
- dma_sync_single_for_device(ctx->dev, ctx->flc_dma[DIGEST],
- desc_bytes(desc), DMA_BIDIRECTIONAL);
- print_hex_dump_debug("ahash digest shdesc@" __stringify(__LINE__)": ",
- DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc),
- 1);
- return 0;
- }
- struct split_key_sh_result {
- struct completion completion;
- int err;
- struct device *dev;
- };
- static void split_key_sh_done(void *cbk_ctx, u32 err)
- {
- struct split_key_sh_result *res = cbk_ctx;
- dev_dbg(res->dev, "%s %d: err 0x%x\n", __func__, __LINE__, err);
- res->err = err ? caam_qi2_strstatus(res->dev, err) : 0;
- complete(&res->completion);
- }
- /* Digest hash size if it is too large */
- static int hash_digest_key(struct caam_hash_ctx *ctx, u32 *keylen, u8 *key,
- u32 digestsize)
- {
- struct caam_request *req_ctx;
- u32 *desc;
- struct split_key_sh_result result;
- dma_addr_t key_dma;
- struct caam_flc *flc;
- dma_addr_t flc_dma;
- int ret = -ENOMEM;
- struct dpaa2_fl_entry *in_fle, *out_fle;
- req_ctx = kzalloc(sizeof(*req_ctx), GFP_KERNEL | GFP_DMA);
- if (!req_ctx)
- return -ENOMEM;
- in_fle = &req_ctx->fd_flt[1];
- out_fle = &req_ctx->fd_flt[0];
- flc = kzalloc(sizeof(*flc), GFP_KERNEL | GFP_DMA);
- if (!flc)
- goto err_flc;
- key_dma = dma_map_single(ctx->dev, key, *keylen, DMA_BIDIRECTIONAL);
- if (dma_mapping_error(ctx->dev, key_dma)) {
- dev_err(ctx->dev, "unable to map key memory\n");
- goto err_key_dma;
- }
- desc = flc->sh_desc;
- init_sh_desc(desc, 0);
- /* descriptor to perform unkeyed hash on key_in */
- append_operation(desc, ctx->adata.algtype | OP_ALG_ENCRYPT |
- OP_ALG_AS_INITFINAL);
- append_seq_fifo_load(desc, *keylen, FIFOLD_CLASS_CLASS2 |
- FIFOLD_TYPE_LAST2 | FIFOLD_TYPE_MSG);
- append_seq_store(desc, digestsize, LDST_CLASS_2_CCB |
- LDST_SRCDST_BYTE_CONTEXT);
- flc->flc[1] = cpu_to_caam32(desc_len(desc)); /* SDL */
- flc_dma = dma_map_single(ctx->dev, flc, sizeof(flc->flc) +
- desc_bytes(desc), DMA_TO_DEVICE);
- if (dma_mapping_error(ctx->dev, flc_dma)) {
- dev_err(ctx->dev, "unable to map shared descriptor\n");
- goto err_flc_dma;
- }
- dpaa2_fl_set_final(in_fle, true);
- dpaa2_fl_set_format(in_fle, dpaa2_fl_single);
- dpaa2_fl_set_addr(in_fle, key_dma);
- dpaa2_fl_set_len(in_fle, *keylen);
- dpaa2_fl_set_format(out_fle, dpaa2_fl_single);
- dpaa2_fl_set_addr(out_fle, key_dma);
- dpaa2_fl_set_len(out_fle, digestsize);
- print_hex_dump_debug("key_in@" __stringify(__LINE__)": ",
- DUMP_PREFIX_ADDRESS, 16, 4, key, *keylen, 1);
- print_hex_dump_debug("shdesc@" __stringify(__LINE__)": ",
- DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc),
- 1);
- result.err = 0;
- init_completion(&result.completion);
- result.dev = ctx->dev;
- req_ctx->flc = flc;
- req_ctx->flc_dma = flc_dma;
- req_ctx->cbk = split_key_sh_done;
- req_ctx->ctx = &result;
- ret = dpaa2_caam_enqueue(ctx->dev, req_ctx);
- if (ret == -EINPROGRESS) {
- /* in progress */
- wait_for_completion(&result.completion);
- ret = result.err;
- print_hex_dump_debug("digested key@" __stringify(__LINE__)": ",
- DUMP_PREFIX_ADDRESS, 16, 4, key,
- digestsize, 1);
- }
- dma_unmap_single(ctx->dev, flc_dma, sizeof(flc->flc) + desc_bytes(desc),
- DMA_TO_DEVICE);
- err_flc_dma:
- dma_unmap_single(ctx->dev, key_dma, *keylen, DMA_BIDIRECTIONAL);
- err_key_dma:
- kfree(flc);
- err_flc:
- kfree(req_ctx);
- *keylen = digestsize;
- return ret;
- }
- static int ahash_setkey(struct crypto_ahash *ahash, const u8 *key,
- unsigned int keylen)
- {
- struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
- unsigned int blocksize = crypto_tfm_alg_blocksize(&ahash->base);
- unsigned int digestsize = crypto_ahash_digestsize(ahash);
- int ret;
- u8 *hashed_key = NULL;
- dev_dbg(ctx->dev, "keylen %d blocksize %d\n", keylen, blocksize);
- if (keylen > blocksize) {
- hashed_key = kmemdup(key, keylen, GFP_KERNEL | GFP_DMA);
- if (!hashed_key)
- return -ENOMEM;
- ret = hash_digest_key(ctx, &keylen, hashed_key, digestsize);
- if (ret)
- goto bad_free_key;
- key = hashed_key;
- }
- ctx->adata.keylen = keylen;
- ctx->adata.keylen_pad = split_key_len(ctx->adata.algtype &
- OP_ALG_ALGSEL_MASK);
- if (ctx->adata.keylen_pad > CAAM_MAX_HASH_KEY_SIZE)
- goto bad_free_key;
- ctx->adata.key_virt = key;
- ctx->adata.key_inline = true;
- /*
- * In case |user key| > |derived key|, using DKP<imm,imm> would result
- * in invalid opcodes (last bytes of user key) in the resulting
- * descriptor. Use DKP<ptr,imm> instead => both virtual and dma key
- * addresses are needed.
- */
- if (keylen > ctx->adata.keylen_pad) {
- memcpy(ctx->key, key, keylen);
- dma_sync_single_for_device(ctx->dev, ctx->adata.key_dma,
- ctx->adata.keylen_pad,
- DMA_TO_DEVICE);
- }
- ret = ahash_set_sh_desc(ahash);
- kfree(hashed_key);
- return ret;
- bad_free_key:
- kfree(hashed_key);
- crypto_ahash_set_flags(ahash, CRYPTO_TFM_RES_BAD_KEY_LEN);
- return -EINVAL;
- }
- static inline void ahash_unmap(struct device *dev, struct ahash_edesc *edesc,
- struct ahash_request *req)
- {
- struct caam_hash_state *state = ahash_request_ctx(req);
- if (edesc->src_nents)
- dma_unmap_sg(dev, req->src, edesc->src_nents, DMA_TO_DEVICE);
- if (edesc->qm_sg_bytes)
- dma_unmap_single(dev, edesc->qm_sg_dma, edesc->qm_sg_bytes,
- DMA_TO_DEVICE);
- if (state->buf_dma) {
- dma_unmap_single(dev, state->buf_dma, *current_buflen(state),
- DMA_TO_DEVICE);
- state->buf_dma = 0;
- }
- }
- static inline void ahash_unmap_ctx(struct device *dev,
- struct ahash_edesc *edesc,
- struct ahash_request *req, u32 flag)
- {
- struct caam_hash_state *state = ahash_request_ctx(req);
- if (state->ctx_dma) {
- dma_unmap_single(dev, state->ctx_dma, state->ctx_dma_len, flag);
- state->ctx_dma = 0;
- }
- ahash_unmap(dev, edesc, req);
- }
- static void ahash_done(void *cbk_ctx, u32 status)
- {
- struct crypto_async_request *areq = cbk_ctx;
- struct ahash_request *req = ahash_request_cast(areq);
- struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
- struct caam_hash_state *state = ahash_request_ctx(req);
- struct ahash_edesc *edesc = state->caam_req.edesc;
- struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
- int digestsize = crypto_ahash_digestsize(ahash);
- int ecode = 0;
- dev_dbg(ctx->dev, "%s %d: err 0x%x\n", __func__, __LINE__, status);
- if (unlikely(status))
- ecode = caam_qi2_strstatus(ctx->dev, status);
- ahash_unmap_ctx(ctx->dev, edesc, req, DMA_FROM_DEVICE);
- memcpy(req->result, state->caam_ctx, digestsize);
- qi_cache_free(edesc);
- print_hex_dump_debug("ctx@" __stringify(__LINE__)": ",
- DUMP_PREFIX_ADDRESS, 16, 4, state->caam_ctx,
- ctx->ctx_len, 1);
- req->base.complete(&req->base, ecode);
- }
- static void ahash_done_bi(void *cbk_ctx, u32 status)
- {
- struct crypto_async_request *areq = cbk_ctx;
- struct ahash_request *req = ahash_request_cast(areq);
- struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
- struct caam_hash_state *state = ahash_request_ctx(req);
- struct ahash_edesc *edesc = state->caam_req.edesc;
- struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
- int ecode = 0;
- dev_dbg(ctx->dev, "%s %d: err 0x%x\n", __func__, __LINE__, status);
- if (unlikely(status))
- ecode = caam_qi2_strstatus(ctx->dev, status);
- ahash_unmap_ctx(ctx->dev, edesc, req, DMA_BIDIRECTIONAL);
- switch_buf(state);
- qi_cache_free(edesc);
- print_hex_dump_debug("ctx@" __stringify(__LINE__)": ",
- DUMP_PREFIX_ADDRESS, 16, 4, state->caam_ctx,
- ctx->ctx_len, 1);
- if (req->result)
- print_hex_dump_debug("result@" __stringify(__LINE__)": ",
- DUMP_PREFIX_ADDRESS, 16, 4, req->result,
- crypto_ahash_digestsize(ahash), 1);
- req->base.complete(&req->base, ecode);
- }
- static void ahash_done_ctx_src(void *cbk_ctx, u32 status)
- {
- struct crypto_async_request *areq = cbk_ctx;
- struct ahash_request *req = ahash_request_cast(areq);
- struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
- struct caam_hash_state *state = ahash_request_ctx(req);
- struct ahash_edesc *edesc = state->caam_req.edesc;
- struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
- int digestsize = crypto_ahash_digestsize(ahash);
- int ecode = 0;
- dev_dbg(ctx->dev, "%s %d: err 0x%x\n", __func__, __LINE__, status);
- if (unlikely(status))
- ecode = caam_qi2_strstatus(ctx->dev, status);
- ahash_unmap_ctx(ctx->dev, edesc, req, DMA_BIDIRECTIONAL);
- memcpy(req->result, state->caam_ctx, digestsize);
- qi_cache_free(edesc);
- print_hex_dump_debug("ctx@" __stringify(__LINE__)": ",
- DUMP_PREFIX_ADDRESS, 16, 4, state->caam_ctx,
- ctx->ctx_len, 1);
- req->base.complete(&req->base, ecode);
- }
- static void ahash_done_ctx_dst(void *cbk_ctx, u32 status)
- {
- struct crypto_async_request *areq = cbk_ctx;
- struct ahash_request *req = ahash_request_cast(areq);
- struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
- struct caam_hash_state *state = ahash_request_ctx(req);
- struct ahash_edesc *edesc = state->caam_req.edesc;
- struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
- int ecode = 0;
- dev_dbg(ctx->dev, "%s %d: err 0x%x\n", __func__, __LINE__, status);
- if (unlikely(status))
- ecode = caam_qi2_strstatus(ctx->dev, status);
- ahash_unmap_ctx(ctx->dev, edesc, req, DMA_FROM_DEVICE);
- switch_buf(state);
- qi_cache_free(edesc);
- print_hex_dump_debug("ctx@" __stringify(__LINE__)": ",
- DUMP_PREFIX_ADDRESS, 16, 4, state->caam_ctx,
- ctx->ctx_len, 1);
- if (req->result)
- print_hex_dump_debug("result@" __stringify(__LINE__)": ",
- DUMP_PREFIX_ADDRESS, 16, 4, req->result,
- crypto_ahash_digestsize(ahash), 1);
- req->base.complete(&req->base, ecode);
- }
- static int ahash_update_ctx(struct ahash_request *req)
- {
- struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
- struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
- struct caam_hash_state *state = ahash_request_ctx(req);
- struct caam_request *req_ctx = &state->caam_req;
- struct dpaa2_fl_entry *in_fle = &req_ctx->fd_flt[1];
- struct dpaa2_fl_entry *out_fle = &req_ctx->fd_flt[0];
- gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
- GFP_KERNEL : GFP_ATOMIC;
- u8 *buf = current_buf(state);
- int *buflen = current_buflen(state);
- u8 *next_buf = alt_buf(state);
- int *next_buflen = alt_buflen(state), last_buflen;
- int in_len = *buflen + req->nbytes, to_hash;
- int src_nents, mapped_nents, qm_sg_bytes, qm_sg_src_index;
- struct ahash_edesc *edesc;
- int ret = 0;
- last_buflen = *next_buflen;
- *next_buflen = in_len & (crypto_tfm_alg_blocksize(&ahash->base) - 1);
- to_hash = in_len - *next_buflen;
- if (to_hash) {
- struct dpaa2_sg_entry *sg_table;
- int src_len = req->nbytes - *next_buflen;
- src_nents = sg_nents_for_len(req->src, src_len);
- if (src_nents < 0) {
- dev_err(ctx->dev, "Invalid number of src SG.\n");
- return src_nents;
- }
- if (src_nents) {
- mapped_nents = dma_map_sg(ctx->dev, req->src, src_nents,
- DMA_TO_DEVICE);
- if (!mapped_nents) {
- dev_err(ctx->dev, "unable to DMA map source\n");
- return -ENOMEM;
- }
- } else {
- mapped_nents = 0;
- }
- /* allocate space for base edesc and link tables */
- edesc = qi_cache_zalloc(GFP_DMA | flags);
- if (!edesc) {
- dma_unmap_sg(ctx->dev, req->src, src_nents,
- DMA_TO_DEVICE);
- return -ENOMEM;
- }
- edesc->src_nents = src_nents;
- qm_sg_src_index = 1 + (*buflen ? 1 : 0);
- qm_sg_bytes = pad_sg_nents(qm_sg_src_index + mapped_nents) *
- sizeof(*sg_table);
- sg_table = &edesc->sgt[0];
- ret = ctx_map_to_qm_sg(ctx->dev, state, ctx->ctx_len, sg_table,
- DMA_BIDIRECTIONAL);
- if (ret)
- goto unmap_ctx;
- ret = buf_map_to_qm_sg(ctx->dev, sg_table + 1, state);
- if (ret)
- goto unmap_ctx;
- if (mapped_nents) {
- sg_to_qm_sg_last(req->src, src_len,
- sg_table + qm_sg_src_index, 0);
- if (*next_buflen)
- scatterwalk_map_and_copy(next_buf, req->src,
- to_hash - *buflen,
- *next_buflen, 0);
- } else {
- dpaa2_sg_set_final(sg_table + qm_sg_src_index - 1,
- true);
- }
- edesc->qm_sg_dma = dma_map_single(ctx->dev, sg_table,
- qm_sg_bytes, DMA_TO_DEVICE);
- if (dma_mapping_error(ctx->dev, edesc->qm_sg_dma)) {
- dev_err(ctx->dev, "unable to map S/G table\n");
- ret = -ENOMEM;
- goto unmap_ctx;
- }
- edesc->qm_sg_bytes = qm_sg_bytes;
- memset(&req_ctx->fd_flt, 0, sizeof(req_ctx->fd_flt));
- dpaa2_fl_set_final(in_fle, true);
- dpaa2_fl_set_format(in_fle, dpaa2_fl_sg);
- dpaa2_fl_set_addr(in_fle, edesc->qm_sg_dma);
- dpaa2_fl_set_len(in_fle, ctx->ctx_len + to_hash);
- dpaa2_fl_set_format(out_fle, dpaa2_fl_single);
- dpaa2_fl_set_addr(out_fle, state->ctx_dma);
- dpaa2_fl_set_len(out_fle, ctx->ctx_len);
- req_ctx->flc = &ctx->flc[UPDATE];
- req_ctx->flc_dma = ctx->flc_dma[UPDATE];
- req_ctx->cbk = ahash_done_bi;
- req_ctx->ctx = &req->base;
- req_ctx->edesc = edesc;
- ret = dpaa2_caam_enqueue(ctx->dev, req_ctx);
- if (ret != -EINPROGRESS &&
- !(ret == -EBUSY &&
- req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG))
- goto unmap_ctx;
- } else if (*next_buflen) {
- scatterwalk_map_and_copy(buf + *buflen, req->src, 0,
- req->nbytes, 0);
- *buflen = *next_buflen;
- *next_buflen = last_buflen;
- }
- print_hex_dump_debug("buf@" __stringify(__LINE__)": ",
- DUMP_PREFIX_ADDRESS, 16, 4, buf, *buflen, 1);
- print_hex_dump_debug("next buf@" __stringify(__LINE__)": ",
- DUMP_PREFIX_ADDRESS, 16, 4, next_buf, *next_buflen,
- 1);
- return ret;
- unmap_ctx:
- ahash_unmap_ctx(ctx->dev, edesc, req, DMA_BIDIRECTIONAL);
- qi_cache_free(edesc);
- return ret;
- }
- static int ahash_final_ctx(struct ahash_request *req)
- {
- struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
- struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
- struct caam_hash_state *state = ahash_request_ctx(req);
- struct caam_request *req_ctx = &state->caam_req;
- struct dpaa2_fl_entry *in_fle = &req_ctx->fd_flt[1];
- struct dpaa2_fl_entry *out_fle = &req_ctx->fd_flt[0];
- gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
- GFP_KERNEL : GFP_ATOMIC;
- int buflen = *current_buflen(state);
- int qm_sg_bytes;
- int digestsize = crypto_ahash_digestsize(ahash);
- struct ahash_edesc *edesc;
- struct dpaa2_sg_entry *sg_table;
- int ret;
- /* allocate space for base edesc and link tables */
- edesc = qi_cache_zalloc(GFP_DMA | flags);
- if (!edesc)
- return -ENOMEM;
- qm_sg_bytes = pad_sg_nents(1 + (buflen ? 1 : 0)) * sizeof(*sg_table);
- sg_table = &edesc->sgt[0];
- ret = ctx_map_to_qm_sg(ctx->dev, state, ctx->ctx_len, sg_table,
- DMA_BIDIRECTIONAL);
- if (ret)
- goto unmap_ctx;
- ret = buf_map_to_qm_sg(ctx->dev, sg_table + 1, state);
- if (ret)
- goto unmap_ctx;
- dpaa2_sg_set_final(sg_table + (buflen ? 1 : 0), true);
- edesc->qm_sg_dma = dma_map_single(ctx->dev, sg_table, qm_sg_bytes,
- DMA_TO_DEVICE);
- if (dma_mapping_error(ctx->dev, edesc->qm_sg_dma)) {
- dev_err(ctx->dev, "unable to map S/G table\n");
- ret = -ENOMEM;
- goto unmap_ctx;
- }
- edesc->qm_sg_bytes = qm_sg_bytes;
- memset(&req_ctx->fd_flt, 0, sizeof(req_ctx->fd_flt));
- dpaa2_fl_set_final(in_fle, true);
- dpaa2_fl_set_format(in_fle, dpaa2_fl_sg);
- dpaa2_fl_set_addr(in_fle, edesc->qm_sg_dma);
- dpaa2_fl_set_len(in_fle, ctx->ctx_len + buflen);
- dpaa2_fl_set_format(out_fle, dpaa2_fl_single);
- dpaa2_fl_set_addr(out_fle, state->ctx_dma);
- dpaa2_fl_set_len(out_fle, digestsize);
- req_ctx->flc = &ctx->flc[FINALIZE];
- req_ctx->flc_dma = ctx->flc_dma[FINALIZE];
- req_ctx->cbk = ahash_done_ctx_src;
- req_ctx->ctx = &req->base;
- req_ctx->edesc = edesc;
- ret = dpaa2_caam_enqueue(ctx->dev, req_ctx);
- if (ret == -EINPROGRESS ||
- (ret == -EBUSY && req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG))
- return ret;
- unmap_ctx:
- ahash_unmap_ctx(ctx->dev, edesc, req, DMA_BIDIRECTIONAL);
- qi_cache_free(edesc);
- return ret;
- }
- static int ahash_finup_ctx(struct ahash_request *req)
- {
- struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
- struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
- struct caam_hash_state *state = ahash_request_ctx(req);
- struct caam_request *req_ctx = &state->caam_req;
- struct dpaa2_fl_entry *in_fle = &req_ctx->fd_flt[1];
- struct dpaa2_fl_entry *out_fle = &req_ctx->fd_flt[0];
- gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
- GFP_KERNEL : GFP_ATOMIC;
- int buflen = *current_buflen(state);
- int qm_sg_bytes, qm_sg_src_index;
- int src_nents, mapped_nents;
- int digestsize = crypto_ahash_digestsize(ahash);
- struct ahash_edesc *edesc;
- struct dpaa2_sg_entry *sg_table;
- int ret;
- src_nents = sg_nents_for_len(req->src, req->nbytes);
- if (src_nents < 0) {
- dev_err(ctx->dev, "Invalid number of src SG.\n");
- return src_nents;
- }
- if (src_nents) {
- mapped_nents = dma_map_sg(ctx->dev, req->src, src_nents,
- DMA_TO_DEVICE);
- if (!mapped_nents) {
- dev_err(ctx->dev, "unable to DMA map source\n");
- return -ENOMEM;
- }
- } else {
- mapped_nents = 0;
- }
- /* allocate space for base edesc and link tables */
- edesc = qi_cache_zalloc(GFP_DMA | flags);
- if (!edesc) {
- dma_unmap_sg(ctx->dev, req->src, src_nents, DMA_TO_DEVICE);
- return -ENOMEM;
- }
- edesc->src_nents = src_nents;
- qm_sg_src_index = 1 + (buflen ? 1 : 0);
- qm_sg_bytes = pad_sg_nents(qm_sg_src_index + mapped_nents) *
- sizeof(*sg_table);
- sg_table = &edesc->sgt[0];
- ret = ctx_map_to_qm_sg(ctx->dev, state, ctx->ctx_len, sg_table,
- DMA_BIDIRECTIONAL);
- if (ret)
- goto unmap_ctx;
- ret = buf_map_to_qm_sg(ctx->dev, sg_table + 1, state);
- if (ret)
- goto unmap_ctx;
- sg_to_qm_sg_last(req->src, req->nbytes, sg_table + qm_sg_src_index, 0);
- edesc->qm_sg_dma = dma_map_single(ctx->dev, sg_table, qm_sg_bytes,
- DMA_TO_DEVICE);
- if (dma_mapping_error(ctx->dev, edesc->qm_sg_dma)) {
- dev_err(ctx->dev, "unable to map S/G table\n");
- ret = -ENOMEM;
- goto unmap_ctx;
- }
- edesc->qm_sg_bytes = qm_sg_bytes;
- memset(&req_ctx->fd_flt, 0, sizeof(req_ctx->fd_flt));
- dpaa2_fl_set_final(in_fle, true);
- dpaa2_fl_set_format(in_fle, dpaa2_fl_sg);
- dpaa2_fl_set_addr(in_fle, edesc->qm_sg_dma);
- dpaa2_fl_set_len(in_fle, ctx->ctx_len + buflen + req->nbytes);
- dpaa2_fl_set_format(out_fle, dpaa2_fl_single);
- dpaa2_fl_set_addr(out_fle, state->ctx_dma);
- dpaa2_fl_set_len(out_fle, digestsize);
- req_ctx->flc = &ctx->flc[FINALIZE];
- req_ctx->flc_dma = ctx->flc_dma[FINALIZE];
- req_ctx->cbk = ahash_done_ctx_src;
- req_ctx->ctx = &req->base;
- req_ctx->edesc = edesc;
- ret = dpaa2_caam_enqueue(ctx->dev, req_ctx);
- if (ret == -EINPROGRESS ||
- (ret == -EBUSY && req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG))
- return ret;
- unmap_ctx:
- ahash_unmap_ctx(ctx->dev, edesc, req, DMA_BIDIRECTIONAL);
- qi_cache_free(edesc);
- return ret;
- }
- static int ahash_digest(struct ahash_request *req)
- {
- struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
- struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
- struct caam_hash_state *state = ahash_request_ctx(req);
- struct caam_request *req_ctx = &state->caam_req;
- struct dpaa2_fl_entry *in_fle = &req_ctx->fd_flt[1];
- struct dpaa2_fl_entry *out_fle = &req_ctx->fd_flt[0];
- gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
- GFP_KERNEL : GFP_ATOMIC;
- int digestsize = crypto_ahash_digestsize(ahash);
- int src_nents, mapped_nents;
- struct ahash_edesc *edesc;
- int ret = -ENOMEM;
- state->buf_dma = 0;
- src_nents = sg_nents_for_len(req->src, req->nbytes);
- if (src_nents < 0) {
- dev_err(ctx->dev, "Invalid number of src SG.\n");
- return src_nents;
- }
- if (src_nents) {
- mapped_nents = dma_map_sg(ctx->dev, req->src, src_nents,
- DMA_TO_DEVICE);
- if (!mapped_nents) {
- dev_err(ctx->dev, "unable to map source for DMA\n");
- return ret;
- }
- } else {
- mapped_nents = 0;
- }
- /* allocate space for base edesc and link tables */
- edesc = qi_cache_zalloc(GFP_DMA | flags);
- if (!edesc) {
- dma_unmap_sg(ctx->dev, req->src, src_nents, DMA_TO_DEVICE);
- return ret;
- }
- edesc->src_nents = src_nents;
- memset(&req_ctx->fd_flt, 0, sizeof(req_ctx->fd_flt));
- if (mapped_nents > 1) {
- int qm_sg_bytes;
- struct dpaa2_sg_entry *sg_table = &edesc->sgt[0];
- qm_sg_bytes = pad_sg_nents(mapped_nents) * sizeof(*sg_table);
- sg_to_qm_sg_last(req->src, req->nbytes, sg_table, 0);
- edesc->qm_sg_dma = dma_map_single(ctx->dev, sg_table,
- qm_sg_bytes, DMA_TO_DEVICE);
- if (dma_mapping_error(ctx->dev, edesc->qm_sg_dma)) {
- dev_err(ctx->dev, "unable to map S/G table\n");
- goto unmap;
- }
- edesc->qm_sg_bytes = qm_sg_bytes;
- dpaa2_fl_set_format(in_fle, dpaa2_fl_sg);
- dpaa2_fl_set_addr(in_fle, edesc->qm_sg_dma);
- } else {
- dpaa2_fl_set_format(in_fle, dpaa2_fl_single);
- dpaa2_fl_set_addr(in_fle, sg_dma_address(req->src));
- }
- state->ctx_dma_len = digestsize;
- state->ctx_dma = dma_map_single(ctx->dev, state->caam_ctx, digestsize,
- DMA_FROM_DEVICE);
- if (dma_mapping_error(ctx->dev, state->ctx_dma)) {
- dev_err(ctx->dev, "unable to map ctx\n");
- state->ctx_dma = 0;
- goto unmap;
- }
- dpaa2_fl_set_final(in_fle, true);
- dpaa2_fl_set_len(in_fle, req->nbytes);
- dpaa2_fl_set_format(out_fle, dpaa2_fl_single);
- dpaa2_fl_set_addr(out_fle, state->ctx_dma);
- dpaa2_fl_set_len(out_fle, digestsize);
- req_ctx->flc = &ctx->flc[DIGEST];
- req_ctx->flc_dma = ctx->flc_dma[DIGEST];
- req_ctx->cbk = ahash_done;
- req_ctx->ctx = &req->base;
- req_ctx->edesc = edesc;
- ret = dpaa2_caam_enqueue(ctx->dev, req_ctx);
- if (ret == -EINPROGRESS ||
- (ret == -EBUSY && req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG))
- return ret;
- unmap:
- ahash_unmap_ctx(ctx->dev, edesc, req, DMA_FROM_DEVICE);
- qi_cache_free(edesc);
- return ret;
- }
- static int ahash_final_no_ctx(struct ahash_request *req)
- {
- struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
- struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
- struct caam_hash_state *state = ahash_request_ctx(req);
- struct caam_request *req_ctx = &state->caam_req;
- struct dpaa2_fl_entry *in_fle = &req_ctx->fd_flt[1];
- struct dpaa2_fl_entry *out_fle = &req_ctx->fd_flt[0];
- gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
- GFP_KERNEL : GFP_ATOMIC;
- u8 *buf = current_buf(state);
- int buflen = *current_buflen(state);
- int digestsize = crypto_ahash_digestsize(ahash);
- struct ahash_edesc *edesc;
- int ret = -ENOMEM;
- /* allocate space for base edesc and link tables */
- edesc = qi_cache_zalloc(GFP_DMA | flags);
- if (!edesc)
- return ret;
- if (buflen) {
- state->buf_dma = dma_map_single(ctx->dev, buf, buflen,
- DMA_TO_DEVICE);
- if (dma_mapping_error(ctx->dev, state->buf_dma)) {
- dev_err(ctx->dev, "unable to map src\n");
- goto unmap;
- }
- }
- state->ctx_dma_len = digestsize;
- state->ctx_dma = dma_map_single(ctx->dev, state->caam_ctx, digestsize,
- DMA_FROM_DEVICE);
- if (dma_mapping_error(ctx->dev, state->ctx_dma)) {
- dev_err(ctx->dev, "unable to map ctx\n");
- state->ctx_dma = 0;
- goto unmap;
- }
- memset(&req_ctx->fd_flt, 0, sizeof(req_ctx->fd_flt));
- dpaa2_fl_set_final(in_fle, true);
- /*
- * crypto engine requires the input entry to be present when
- * "frame list" FD is used.
- * Since engine does not support FMT=2'b11 (unused entry type), leaving
- * in_fle zeroized (except for "Final" flag) is the best option.
- */
- if (buflen) {
- dpaa2_fl_set_format(in_fle, dpaa2_fl_single);
- dpaa2_fl_set_addr(in_fle, state->buf_dma);
- dpaa2_fl_set_len(in_fle, buflen);
- }
- dpaa2_fl_set_format(out_fle, dpaa2_fl_single);
- dpaa2_fl_set_addr(out_fle, state->ctx_dma);
- dpaa2_fl_set_len(out_fle, digestsize);
- req_ctx->flc = &ctx->flc[DIGEST];
- req_ctx->flc_dma = ctx->flc_dma[DIGEST];
- req_ctx->cbk = ahash_done;
- req_ctx->ctx = &req->base;
- req_ctx->edesc = edesc;
- ret = dpaa2_caam_enqueue(ctx->dev, req_ctx);
- if (ret == -EINPROGRESS ||
- (ret == -EBUSY && req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG))
- return ret;
- unmap:
- ahash_unmap_ctx(ctx->dev, edesc, req, DMA_FROM_DEVICE);
- qi_cache_free(edesc);
- return ret;
- }
- static int ahash_update_no_ctx(struct ahash_request *req)
- {
- struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
- struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
- struct caam_hash_state *state = ahash_request_ctx(req);
- struct caam_request *req_ctx = &state->caam_req;
- struct dpaa2_fl_entry *in_fle = &req_ctx->fd_flt[1];
- struct dpaa2_fl_entry *out_fle = &req_ctx->fd_flt[0];
- gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
- GFP_KERNEL : GFP_ATOMIC;
- u8 *buf = current_buf(state);
- int *buflen = current_buflen(state);
- u8 *next_buf = alt_buf(state);
- int *next_buflen = alt_buflen(state);
- int in_len = *buflen + req->nbytes, to_hash;
- int qm_sg_bytes, src_nents, mapped_nents;
- struct ahash_edesc *edesc;
- int ret = 0;
- *next_buflen = in_len & (crypto_tfm_alg_blocksize(&ahash->base) - 1);
- to_hash = in_len - *next_buflen;
- if (to_hash) {
- struct dpaa2_sg_entry *sg_table;
- int src_len = req->nbytes - *next_buflen;
- src_nents = sg_nents_for_len(req->src, src_len);
- if (src_nents < 0) {
- dev_err(ctx->dev, "Invalid number of src SG.\n");
- return src_nents;
- }
- if (src_nents) {
- mapped_nents = dma_map_sg(ctx->dev, req->src, src_nents,
- DMA_TO_DEVICE);
- if (!mapped_nents) {
- dev_err(ctx->dev, "unable to DMA map source\n");
- return -ENOMEM;
- }
- } else {
- mapped_nents = 0;
- }
- /* allocate space for base edesc and link tables */
- edesc = qi_cache_zalloc(GFP_DMA | flags);
- if (!edesc) {
- dma_unmap_sg(ctx->dev, req->src, src_nents,
- DMA_TO_DEVICE);
- return -ENOMEM;
- }
- edesc->src_nents = src_nents;
- qm_sg_bytes = pad_sg_nents(1 + mapped_nents) *
- sizeof(*sg_table);
- sg_table = &edesc->sgt[0];
- ret = buf_map_to_qm_sg(ctx->dev, sg_table, state);
- if (ret)
- goto unmap_ctx;
- sg_to_qm_sg_last(req->src, src_len, sg_table + 1, 0);
- if (*next_buflen)
- scatterwalk_map_and_copy(next_buf, req->src,
- to_hash - *buflen,
- *next_buflen, 0);
- edesc->qm_sg_dma = dma_map_single(ctx->dev, sg_table,
- qm_sg_bytes, DMA_TO_DEVICE);
- if (dma_mapping_error(ctx->dev, edesc->qm_sg_dma)) {
- dev_err(ctx->dev, "unable to map S/G table\n");
- ret = -ENOMEM;
- goto unmap_ctx;
- }
- edesc->qm_sg_bytes = qm_sg_bytes;
- state->ctx_dma_len = ctx->ctx_len;
- state->ctx_dma = dma_map_single(ctx->dev, state->caam_ctx,
- ctx->ctx_len, DMA_FROM_DEVICE);
- if (dma_mapping_error(ctx->dev, state->ctx_dma)) {
- dev_err(ctx->dev, "unable to map ctx\n");
- state->ctx_dma = 0;
- ret = -ENOMEM;
- goto unmap_ctx;
- }
- memset(&req_ctx->fd_flt, 0, sizeof(req_ctx->fd_flt));
- dpaa2_fl_set_final(in_fle, true);
- dpaa2_fl_set_format(in_fle, dpaa2_fl_sg);
- dpaa2_fl_set_addr(in_fle, edesc->qm_sg_dma);
- dpaa2_fl_set_len(in_fle, to_hash);
- dpaa2_fl_set_format(out_fle, dpaa2_fl_single);
- dpaa2_fl_set_addr(out_fle, state->ctx_dma);
- dpaa2_fl_set_len(out_fle, ctx->ctx_len);
- req_ctx->flc = &ctx->flc[UPDATE_FIRST];
- req_ctx->flc_dma = ctx->flc_dma[UPDATE_FIRST];
- req_ctx->cbk = ahash_done_ctx_dst;
- req_ctx->ctx = &req->base;
- req_ctx->edesc = edesc;
- ret = dpaa2_caam_enqueue(ctx->dev, req_ctx);
- if (ret != -EINPROGRESS &&
- !(ret == -EBUSY &&
- req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG))
- goto unmap_ctx;
- state->update = ahash_update_ctx;
- state->finup = ahash_finup_ctx;
- state->final = ahash_final_ctx;
- } else if (*next_buflen) {
- scatterwalk_map_and_copy(buf + *buflen, req->src, 0,
- req->nbytes, 0);
- *buflen = *next_buflen;
- *next_buflen = 0;
- }
- print_hex_dump_debug("buf@" __stringify(__LINE__)": ",
- DUMP_PREFIX_ADDRESS, 16, 4, buf, *buflen, 1);
- print_hex_dump_debug("next buf@" __stringify(__LINE__)": ",
- DUMP_PREFIX_ADDRESS, 16, 4, next_buf, *next_buflen,
- 1);
- return ret;
- unmap_ctx:
- ahash_unmap_ctx(ctx->dev, edesc, req, DMA_TO_DEVICE);
- qi_cache_free(edesc);
- return ret;
- }
- static int ahash_finup_no_ctx(struct ahash_request *req)
- {
- struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
- struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
- struct caam_hash_state *state = ahash_request_ctx(req);
- struct caam_request *req_ctx = &state->caam_req;
- struct dpaa2_fl_entry *in_fle = &req_ctx->fd_flt[1];
- struct dpaa2_fl_entry *out_fle = &req_ctx->fd_flt[0];
- gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
- GFP_KERNEL : GFP_ATOMIC;
- int buflen = *current_buflen(state);
- int qm_sg_bytes, src_nents, mapped_nents;
- int digestsize = crypto_ahash_digestsize(ahash);
- struct ahash_edesc *edesc;
- struct dpaa2_sg_entry *sg_table;
- int ret;
- src_nents = sg_nents_for_len(req->src, req->nbytes);
- if (src_nents < 0) {
- dev_err(ctx->dev, "Invalid number of src SG.\n");
- return src_nents;
- }
- if (src_nents) {
- mapped_nents = dma_map_sg(ctx->dev, req->src, src_nents,
- DMA_TO_DEVICE);
- if (!mapped_nents) {
- dev_err(ctx->dev, "unable to DMA map source\n");
- return -ENOMEM;
- }
- } else {
- mapped_nents = 0;
- }
- /* allocate space for base edesc and link tables */
- edesc = qi_cache_zalloc(GFP_DMA | flags);
- if (!edesc) {
- dma_unmap_sg(ctx->dev, req->src, src_nents, DMA_TO_DEVICE);
- return -ENOMEM;
- }
- edesc->src_nents = src_nents;
- qm_sg_bytes = pad_sg_nents(2 + mapped_nents) * sizeof(*sg_table);
- sg_table = &edesc->sgt[0];
- ret = buf_map_to_qm_sg(ctx->dev, sg_table, state);
- if (ret)
- goto unmap;
- sg_to_qm_sg_last(req->src, req->nbytes, sg_table + 1, 0);
- edesc->qm_sg_dma = dma_map_single(ctx->dev, sg_table, qm_sg_bytes,
- DMA_TO_DEVICE);
- if (dma_mapping_error(ctx->dev, edesc->qm_sg_dma)) {
- dev_err(ctx->dev, "unable to map S/G table\n");
- ret = -ENOMEM;
- goto unmap;
- }
- edesc->qm_sg_bytes = qm_sg_bytes;
- state->ctx_dma_len = digestsize;
- state->ctx_dma = dma_map_single(ctx->dev, state->caam_ctx, digestsize,
- DMA_FROM_DEVICE);
- if (dma_mapping_error(ctx->dev, state->ctx_dma)) {
- dev_err(ctx->dev, "unable to map ctx\n");
- state->ctx_dma = 0;
- ret = -ENOMEM;
- goto unmap;
- }
- memset(&req_ctx->fd_flt, 0, sizeof(req_ctx->fd_flt));
- dpaa2_fl_set_final(in_fle, true);
- dpaa2_fl_set_format(in_fle, dpaa2_fl_sg);
- dpaa2_fl_set_addr(in_fle, edesc->qm_sg_dma);
- dpaa2_fl_set_len(in_fle, buflen + req->nbytes);
- dpaa2_fl_set_format(out_fle, dpaa2_fl_single);
- dpaa2_fl_set_addr(out_fle, state->ctx_dma);
- dpaa2_fl_set_len(out_fle, digestsize);
- req_ctx->flc = &ctx->flc[DIGEST];
- req_ctx->flc_dma = ctx->flc_dma[DIGEST];
- req_ctx->cbk = ahash_done;
- req_ctx->ctx = &req->base;
- req_ctx->edesc = edesc;
- ret = dpaa2_caam_enqueue(ctx->dev, req_ctx);
- if (ret != -EINPROGRESS &&
- !(ret == -EBUSY && req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG))
- goto unmap;
- return ret;
- unmap:
- ahash_unmap_ctx(ctx->dev, edesc, req, DMA_FROM_DEVICE);
- qi_cache_free(edesc);
- return -ENOMEM;
- }
- static int ahash_update_first(struct ahash_request *req)
- {
- struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
- struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
- struct caam_hash_state *state = ahash_request_ctx(req);
- struct caam_request *req_ctx = &state->caam_req;
- struct dpaa2_fl_entry *in_fle = &req_ctx->fd_flt[1];
- struct dpaa2_fl_entry *out_fle = &req_ctx->fd_flt[0];
- gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
- GFP_KERNEL : GFP_ATOMIC;
- u8 *next_buf = alt_buf(state);
- int *next_buflen = alt_buflen(state);
- int to_hash;
- int src_nents, mapped_nents;
- struct ahash_edesc *edesc;
- int ret = 0;
- *next_buflen = req->nbytes & (crypto_tfm_alg_blocksize(&ahash->base) -
- 1);
- to_hash = req->nbytes - *next_buflen;
- if (to_hash) {
- struct dpaa2_sg_entry *sg_table;
- int src_len = req->nbytes - *next_buflen;
- src_nents = sg_nents_for_len(req->src, src_len);
- if (src_nents < 0) {
- dev_err(ctx->dev, "Invalid number of src SG.\n");
- return src_nents;
- }
- if (src_nents) {
- mapped_nents = dma_map_sg(ctx->dev, req->src, src_nents,
- DMA_TO_DEVICE);
- if (!mapped_nents) {
- dev_err(ctx->dev, "unable to map source for DMA\n");
- return -ENOMEM;
- }
- } else {
- mapped_nents = 0;
- }
- /* allocate space for base edesc and link tables */
- edesc = qi_cache_zalloc(GFP_DMA | flags);
- if (!edesc) {
- dma_unmap_sg(ctx->dev, req->src, src_nents,
- DMA_TO_DEVICE);
- return -ENOMEM;
- }
- edesc->src_nents = src_nents;
- sg_table = &edesc->sgt[0];
- memset(&req_ctx->fd_flt, 0, sizeof(req_ctx->fd_flt));
- dpaa2_fl_set_final(in_fle, true);
- dpaa2_fl_set_len(in_fle, to_hash);
- if (mapped_nents > 1) {
- int qm_sg_bytes;
- sg_to_qm_sg_last(req->src, src_len, sg_table, 0);
- qm_sg_bytes = pad_sg_nents(mapped_nents) *
- sizeof(*sg_table);
- edesc->qm_sg_dma = dma_map_single(ctx->dev, sg_table,
- qm_sg_bytes,
- DMA_TO_DEVICE);
- if (dma_mapping_error(ctx->dev, edesc->qm_sg_dma)) {
- dev_err(ctx->dev, "unable to map S/G table\n");
- ret = -ENOMEM;
- goto unmap_ctx;
- }
- edesc->qm_sg_bytes = qm_sg_bytes;
- dpaa2_fl_set_format(in_fle, dpaa2_fl_sg);
- dpaa2_fl_set_addr(in_fle, edesc->qm_sg_dma);
- } else {
- dpaa2_fl_set_format(in_fle, dpaa2_fl_single);
- dpaa2_fl_set_addr(in_fle, sg_dma_address(req->src));
- }
- if (*next_buflen)
- scatterwalk_map_and_copy(next_buf, req->src, to_hash,
- *next_buflen, 0);
- state->ctx_dma_len = ctx->ctx_len;
- state->ctx_dma = dma_map_single(ctx->dev, state->caam_ctx,
- ctx->ctx_len, DMA_FROM_DEVICE);
- if (dma_mapping_error(ctx->dev, state->ctx_dma)) {
- dev_err(ctx->dev, "unable to map ctx\n");
- state->ctx_dma = 0;
- ret = -ENOMEM;
- goto unmap_ctx;
- }
- dpaa2_fl_set_format(out_fle, dpaa2_fl_single);
- dpaa2_fl_set_addr(out_fle, state->ctx_dma);
- dpaa2_fl_set_len(out_fle, ctx->ctx_len);
- req_ctx->flc = &ctx->flc[UPDATE_FIRST];
- req_ctx->flc_dma = ctx->flc_dma[UPDATE_FIRST];
- req_ctx->cbk = ahash_done_ctx_dst;
- req_ctx->ctx = &req->base;
- req_ctx->edesc = edesc;
- ret = dpaa2_caam_enqueue(ctx->dev, req_ctx);
- if (ret != -EINPROGRESS &&
- !(ret == -EBUSY && req->base.flags &
- CRYPTO_TFM_REQ_MAY_BACKLOG))
- goto unmap_ctx;
- state->update = ahash_update_ctx;
- state->finup = ahash_finup_ctx;
- state->final = ahash_final_ctx;
- } else if (*next_buflen) {
- state->update = ahash_update_no_ctx;
- state->finup = ahash_finup_no_ctx;
- state->final = ahash_final_no_ctx;
- scatterwalk_map_and_copy(next_buf, req->src, 0,
- req->nbytes, 0);
- switch_buf(state);
- }
- print_hex_dump_debug("next buf@" __stringify(__LINE__)": ",
- DUMP_PREFIX_ADDRESS, 16, 4, next_buf, *next_buflen,
- 1);
- return ret;
- unmap_ctx:
- ahash_unmap_ctx(ctx->dev, edesc, req, DMA_TO_DEVICE);
- qi_cache_free(edesc);
- return ret;
- }
- static int ahash_finup_first(struct ahash_request *req)
- {
- return ahash_digest(req);
- }
- static int ahash_init(struct ahash_request *req)
- {
- struct caam_hash_state *state = ahash_request_ctx(req);
- state->update = ahash_update_first;
- state->finup = ahash_finup_first;
- state->final = ahash_final_no_ctx;
- state->ctx_dma = 0;
- state->ctx_dma_len = 0;
- state->current_buf = 0;
- state->buf_dma = 0;
- state->buflen_0 = 0;
- state->buflen_1 = 0;
- return 0;
- }
- static int ahash_update(struct ahash_request *req)
- {
- struct caam_hash_state *state = ahash_request_ctx(req);
- return state->update(req);
- }
- static int ahash_finup(struct ahash_request *req)
- {
- struct caam_hash_state *state = ahash_request_ctx(req);
- return state->finup(req);
- }
- static int ahash_final(struct ahash_request *req)
- {
- struct caam_hash_state *state = ahash_request_ctx(req);
- return state->final(req);
- }
- static int ahash_export(struct ahash_request *req, void *out)
- {
- struct caam_hash_state *state = ahash_request_ctx(req);
- struct caam_export_state *export = out;
- int len;
- u8 *buf;
- if (state->current_buf) {
- buf = state->buf_1;
- len = state->buflen_1;
- } else {
- buf = state->buf_0;
- len = state->buflen_0;
- }
- memcpy(export->buf, buf, len);
- memcpy(export->caam_ctx, state->caam_ctx, sizeof(export->caam_ctx));
- export->buflen = len;
- export->update = state->update;
- export->final = state->final;
- export->finup = state->finup;
- return 0;
- }
- static int ahash_import(struct ahash_request *req, const void *in)
- {
- struct caam_hash_state *state = ahash_request_ctx(req);
- const struct caam_export_state *export = in;
- memset(state, 0, sizeof(*state));
- memcpy(state->buf_0, export->buf, export->buflen);
- memcpy(state->caam_ctx, export->caam_ctx, sizeof(state->caam_ctx));
- state->buflen_0 = export->buflen;
- state->update = export->update;
- state->final = export->final;
- state->finup = export->finup;
- return 0;
- }
- struct caam_hash_template {
- char name[CRYPTO_MAX_ALG_NAME];
- char driver_name[CRYPTO_MAX_ALG_NAME];
- char hmac_name[CRYPTO_MAX_ALG_NAME];
- char hmac_driver_name[CRYPTO_MAX_ALG_NAME];
- unsigned int blocksize;
- struct ahash_alg template_ahash;
- u32 alg_type;
- };
- /* ahash descriptors */
- static struct caam_hash_template driver_hash[] = {
- {
- .name = "sha1",
- .driver_name = "sha1-caam-qi2",
- .hmac_name = "hmac(sha1)",
- .hmac_driver_name = "hmac-sha1-caam-qi2",
- .blocksize = SHA1_BLOCK_SIZE,
- .template_ahash = {
- .init = ahash_init,
- .update = ahash_update,
- .final = ahash_final,
- .finup = ahash_finup,
- .digest = ahash_digest,
- .export = ahash_export,
- .import = ahash_import,
- .setkey = ahash_setkey,
- .halg = {
- .digestsize = SHA1_DIGEST_SIZE,
- .statesize = sizeof(struct caam_export_state),
- },
- },
- .alg_type = OP_ALG_ALGSEL_SHA1,
- }, {
- .name = "sha224",
- .driver_name = "sha224-caam-qi2",
- .hmac_name = "hmac(sha224)",
- .hmac_driver_name = "hmac-sha224-caam-qi2",
- .blocksize = SHA224_BLOCK_SIZE,
- .template_ahash = {
- .init = ahash_init,
- .update = ahash_update,
- .final = ahash_final,
- .finup = ahash_finup,
- .digest = ahash_digest,
- .export = ahash_export,
- .import = ahash_import,
- .setkey = ahash_setkey,
- .halg = {
- .digestsize = SHA224_DIGEST_SIZE,
- .statesize = sizeof(struct caam_export_state),
- },
- },
- .alg_type = OP_ALG_ALGSEL_SHA224,
- }, {
- .name = "sha256",
- .driver_name = "sha256-caam-qi2",
- .hmac_name = "hmac(sha256)",
- .hmac_driver_name = "hmac-sha256-caam-qi2",
- .blocksize = SHA256_BLOCK_SIZE,
- .template_ahash = {
- .init = ahash_init,
- .update = ahash_update,
- .final = ahash_final,
- .finup = ahash_finup,
- .digest = ahash_digest,
- .export = ahash_export,
- .import = ahash_import,
- .setkey = ahash_setkey,
- .halg = {
- .digestsize = SHA256_DIGEST_SIZE,
- .statesize = sizeof(struct caam_export_state),
- },
- },
- .alg_type = OP_ALG_ALGSEL_SHA256,
- }, {
- .name = "sha384",
- .driver_name = "sha384-caam-qi2",
- .hmac_name = "hmac(sha384)",
- .hmac_driver_name = "hmac-sha384-caam-qi2",
- .blocksize = SHA384_BLOCK_SIZE,
- .template_ahash = {
- .init = ahash_init,
- .update = ahash_update,
- .final = ahash_final,
- .finup = ahash_finup,
- .digest = ahash_digest,
- .export = ahash_export,
- .import = ahash_import,
- .setkey = ahash_setkey,
- .halg = {
- .digestsize = SHA384_DIGEST_SIZE,
- .statesize = sizeof(struct caam_export_state),
- },
- },
- .alg_type = OP_ALG_ALGSEL_SHA384,
- }, {
- .name = "sha512",
- .driver_name = "sha512-caam-qi2",
- .hmac_name = "hmac(sha512)",
- .hmac_driver_name = "hmac-sha512-caam-qi2",
- .blocksize = SHA512_BLOCK_SIZE,
- .template_ahash = {
- .init = ahash_init,
- .update = ahash_update,
- .final = ahash_final,
- .finup = ahash_finup,
- .digest = ahash_digest,
- .export = ahash_export,
- .import = ahash_import,
- .setkey = ahash_setkey,
- .halg = {
- .digestsize = SHA512_DIGEST_SIZE,
- .statesize = sizeof(struct caam_export_state),
- },
- },
- .alg_type = OP_ALG_ALGSEL_SHA512,
- }, {
- .name = "md5",
- .driver_name = "md5-caam-qi2",
- .hmac_name = "hmac(md5)",
- .hmac_driver_name = "hmac-md5-caam-qi2",
- .blocksize = MD5_BLOCK_WORDS * 4,
- .template_ahash = {
- .init = ahash_init,
- .update = ahash_update,
- .final = ahash_final,
- .finup = ahash_finup,
- .digest = ahash_digest,
- .export = ahash_export,
- .import = ahash_import,
- .setkey = ahash_setkey,
- .halg = {
- .digestsize = MD5_DIGEST_SIZE,
- .statesize = sizeof(struct caam_export_state),
- },
- },
- .alg_type = OP_ALG_ALGSEL_MD5,
- }
- };
- struct caam_hash_alg {
- struct list_head entry;
- struct device *dev;
- int alg_type;
- struct ahash_alg ahash_alg;
- };
- static int caam_hash_cra_init(struct crypto_tfm *tfm)
- {
- struct crypto_ahash *ahash = __crypto_ahash_cast(tfm);
- struct crypto_alg *base = tfm->__crt_alg;
- struct hash_alg_common *halg =
- container_of(base, struct hash_alg_common, base);
- struct ahash_alg *alg =
- container_of(halg, struct ahash_alg, halg);
- struct caam_hash_alg *caam_hash =
- container_of(alg, struct caam_hash_alg, ahash_alg);
- struct caam_hash_ctx *ctx = crypto_tfm_ctx(tfm);
- /* Sizes for MDHA running digests: MD5, SHA1, 224, 256, 384, 512 */
- static const u8 runninglen[] = { HASH_MSG_LEN + MD5_DIGEST_SIZE,
- HASH_MSG_LEN + SHA1_DIGEST_SIZE,
- HASH_MSG_LEN + 32,
- HASH_MSG_LEN + SHA256_DIGEST_SIZE,
- HASH_MSG_LEN + 64,
- HASH_MSG_LEN + SHA512_DIGEST_SIZE };
- dma_addr_t dma_addr;
- int i;
- ctx->dev = caam_hash->dev;
- if (alg->setkey) {
- ctx->adata.key_dma = dma_map_single_attrs(ctx->dev, ctx->key,
- ARRAY_SIZE(ctx->key),
- DMA_TO_DEVICE,
- DMA_ATTR_SKIP_CPU_SYNC);
- if (dma_mapping_error(ctx->dev, ctx->adata.key_dma)) {
- dev_err(ctx->dev, "unable to map key\n");
- return -ENOMEM;
- }
- }
- dma_addr = dma_map_single_attrs(ctx->dev, ctx->flc, sizeof(ctx->flc),
- DMA_BIDIRECTIONAL,
- DMA_ATTR_SKIP_CPU_SYNC);
- if (dma_mapping_error(ctx->dev, dma_addr)) {
- dev_err(ctx->dev, "unable to map shared descriptors\n");
- if (ctx->adata.key_dma)
- dma_unmap_single_attrs(ctx->dev, ctx->adata.key_dma,
- ARRAY_SIZE(ctx->key),
- DMA_TO_DEVICE,
- DMA_ATTR_SKIP_CPU_SYNC);
- return -ENOMEM;
- }
- for (i = 0; i < HASH_NUM_OP; i++)
- ctx->flc_dma[i] = dma_addr + i * sizeof(ctx->flc[i]);
- /* copy descriptor header template value */
- ctx->adata.algtype = OP_TYPE_CLASS2_ALG | caam_hash->alg_type;
- ctx->ctx_len = runninglen[(ctx->adata.algtype &
- OP_ALG_ALGSEL_SUBMASK) >>
- OP_ALG_ALGSEL_SHIFT];
- crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
- sizeof(struct caam_hash_state));
- return ahash_set_sh_desc(ahash);
- }
- static void caam_hash_cra_exit(struct crypto_tfm *tfm)
- {
- struct caam_hash_ctx *ctx = crypto_tfm_ctx(tfm);
- dma_unmap_single_attrs(ctx->dev, ctx->flc_dma[0], sizeof(ctx->flc),
- DMA_BIDIRECTIONAL, DMA_ATTR_SKIP_CPU_SYNC);
- if (ctx->adata.key_dma)
- dma_unmap_single_attrs(ctx->dev, ctx->adata.key_dma,
- ARRAY_SIZE(ctx->key), DMA_TO_DEVICE,
- DMA_ATTR_SKIP_CPU_SYNC);
- }
- static struct caam_hash_alg *caam_hash_alloc(struct device *dev,
- struct caam_hash_template *template, bool keyed)
- {
- struct caam_hash_alg *t_alg;
- struct ahash_alg *halg;
- struct crypto_alg *alg;
- t_alg = kzalloc(sizeof(*t_alg), GFP_KERNEL);
- if (!t_alg)
- return ERR_PTR(-ENOMEM);
- t_alg->ahash_alg = template->template_ahash;
- halg = &t_alg->ahash_alg;
- alg = &halg->halg.base;
- if (keyed) {
- snprintf(alg->cra_name, CRYPTO_MAX_ALG_NAME, "%s",
- template->hmac_name);
- snprintf(alg->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
- template->hmac_driver_name);
- } else {
- snprintf(alg->cra_name, CRYPTO_MAX_ALG_NAME, "%s",
- template->name);
- snprintf(alg->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
- template->driver_name);
- t_alg->ahash_alg.setkey = NULL;
- }
- alg->cra_module = THIS_MODULE;
- alg->cra_init = caam_hash_cra_init;
- alg->cra_exit = caam_hash_cra_exit;
- alg->cra_ctxsize = sizeof(struct caam_hash_ctx);
- alg->cra_priority = CAAM_CRA_PRIORITY;
- alg->cra_blocksize = template->blocksize;
- alg->cra_alignmask = 0;
- alg->cra_flags = CRYPTO_ALG_ASYNC;
- t_alg->alg_type = template->alg_type;
- t_alg->dev = dev;
- return t_alg;
- }
- static void dpaa2_caam_fqdan_cb(struct dpaa2_io_notification_ctx *nctx)
- {
- struct dpaa2_caam_priv_per_cpu *ppriv;
- ppriv = container_of(nctx, struct dpaa2_caam_priv_per_cpu, nctx);
- napi_schedule_irqoff(&ppriv->napi);
- }
- static int __cold dpaa2_dpseci_dpio_setup(struct dpaa2_caam_priv *priv)
- {
- struct device *dev = priv->dev;
- struct dpaa2_io_notification_ctx *nctx;
- struct dpaa2_caam_priv_per_cpu *ppriv;
- int err, i = 0, cpu;
- for_each_online_cpu(cpu) {
- ppriv = per_cpu_ptr(priv->ppriv, cpu);
- ppriv->priv = priv;
- nctx = &ppriv->nctx;
- nctx->is_cdan = 0;
- nctx->id = ppriv->rsp_fqid;
- nctx->desired_cpu = cpu;
- nctx->cb = dpaa2_caam_fqdan_cb;
- /* Register notification callbacks */
- ppriv->dpio = dpaa2_io_service_select(cpu);
- err = dpaa2_io_service_register(ppriv->dpio, nctx, dev);
- if (unlikely(err)) {
- dev_dbg(dev, "No affine DPIO for cpu %d\n", cpu);
- nctx->cb = NULL;
- /*
- * If no affine DPIO for this core, there's probably
- * none available for next cores either. Signal we want
- * to retry later, in case the DPIO devices weren't
- * probed yet.
- */
- err = -EPROBE_DEFER;
- goto err;
- }
- ppriv->store = dpaa2_io_store_create(DPAA2_CAAM_STORE_SIZE,
- dev);
- if (unlikely(!ppriv->store)) {
- dev_err(dev, "dpaa2_io_store_create() failed\n");
- err = -ENOMEM;
- goto err;
- }
- if (++i == priv->num_pairs)
- break;
- }
- return 0;
- err:
- for_each_online_cpu(cpu) {
- ppriv = per_cpu_ptr(priv->ppriv, cpu);
- if (!ppriv->nctx.cb)
- break;
- dpaa2_io_service_deregister(ppriv->dpio, &ppriv->nctx, dev);
- }
- for_each_online_cpu(cpu) {
- ppriv = per_cpu_ptr(priv->ppriv, cpu);
- if (!ppriv->store)
- break;
- dpaa2_io_store_destroy(ppriv->store);
- }
- return err;
- }
- static void __cold dpaa2_dpseci_dpio_free(struct dpaa2_caam_priv *priv)
- {
- struct dpaa2_caam_priv_per_cpu *ppriv;
- int i = 0, cpu;
- for_each_online_cpu(cpu) {
- ppriv = per_cpu_ptr(priv->ppriv, cpu);
- dpaa2_io_service_deregister(ppriv->dpio, &ppriv->nctx,
- priv->dev);
- dpaa2_io_store_destroy(ppriv->store);
- if (++i == priv->num_pairs)
- return;
- }
- }
- static int dpaa2_dpseci_bind(struct dpaa2_caam_priv *priv)
- {
- struct dpseci_rx_queue_cfg rx_queue_cfg;
- struct device *dev = priv->dev;
- struct fsl_mc_device *ls_dev = to_fsl_mc_device(dev);
- struct dpaa2_caam_priv_per_cpu *ppriv;
- int err = 0, i = 0, cpu;
- /* Configure Rx queues */
- for_each_online_cpu(cpu) {
- ppriv = per_cpu_ptr(priv->ppriv, cpu);
- rx_queue_cfg.options = DPSECI_QUEUE_OPT_DEST |
- DPSECI_QUEUE_OPT_USER_CTX;
- rx_queue_cfg.order_preservation_en = 0;
- rx_queue_cfg.dest_cfg.dest_type = DPSECI_DEST_DPIO;
- rx_queue_cfg.dest_cfg.dest_id = ppriv->nctx.dpio_id;
- /*
- * Rx priority (WQ) doesn't really matter, since we use
- * pull mode, i.e. volatile dequeues from specific FQs
- */
- rx_queue_cfg.dest_cfg.priority = 0;
- rx_queue_cfg.user_ctx = ppriv->nctx.qman64;
- err = dpseci_set_rx_queue(priv->mc_io, 0, ls_dev->mc_handle, i,
- &rx_queue_cfg);
- if (err) {
- dev_err(dev, "dpseci_set_rx_queue() failed with err %d\n",
- err);
- return err;
- }
- if (++i == priv->num_pairs)
- break;
- }
- return err;
- }
- static void dpaa2_dpseci_congestion_free(struct dpaa2_caam_priv *priv)
- {
- struct device *dev = priv->dev;
- if (!priv->cscn_mem)
- return;
- dma_unmap_single(dev, priv->cscn_dma, DPAA2_CSCN_SIZE, DMA_FROM_DEVICE);
- kfree(priv->cscn_mem);
- }
- static void dpaa2_dpseci_free(struct dpaa2_caam_priv *priv)
- {
- struct device *dev = priv->dev;
- struct fsl_mc_device *ls_dev = to_fsl_mc_device(dev);
- dpaa2_dpseci_congestion_free(priv);
- dpseci_close(priv->mc_io, 0, ls_dev->mc_handle);
- }
- static void dpaa2_caam_process_fd(struct dpaa2_caam_priv *priv,
- const struct dpaa2_fd *fd)
- {
- struct caam_request *req;
- u32 fd_err;
- if (dpaa2_fd_get_format(fd) != dpaa2_fd_list) {
- dev_err(priv->dev, "Only Frame List FD format is supported!\n");
- return;
- }
- fd_err = dpaa2_fd_get_ctrl(fd) & FD_CTRL_ERR_MASK;
- if (unlikely(fd_err))
- dev_err_ratelimited(priv->dev, "FD error: %08x\n", fd_err);
- /*
- * FD[ADDR] is guaranteed to be valid, irrespective of errors reported
- * in FD[ERR] or FD[FRC].
- */
- req = dpaa2_caam_iova_to_virt(priv, dpaa2_fd_get_addr(fd));
- dma_unmap_single(priv->dev, req->fd_flt_dma, sizeof(req->fd_flt),
- DMA_BIDIRECTIONAL);
- req->cbk(req->ctx, dpaa2_fd_get_frc(fd));
- }
- static int dpaa2_caam_pull_fq(struct dpaa2_caam_priv_per_cpu *ppriv)
- {
- int err;
- /* Retry while portal is busy */
- do {
- err = dpaa2_io_service_pull_fq(ppriv->dpio, ppriv->rsp_fqid,
- ppriv->store);
- } while (err == -EBUSY);
- if (unlikely(err))
- dev_err(ppriv->priv->dev, "dpaa2_io_service_pull err %d", err);
- return err;
- }
- static int dpaa2_caam_store_consume(struct dpaa2_caam_priv_per_cpu *ppriv)
- {
- struct dpaa2_dq *dq;
- int cleaned = 0, is_last;
- do {
- dq = dpaa2_io_store_next(ppriv->store, &is_last);
- if (unlikely(!dq)) {
- if (unlikely(!is_last)) {
- dev_dbg(ppriv->priv->dev,
- "FQ %d returned no valid frames\n",
- ppriv->rsp_fqid);
- /*
- * MUST retry until we get some sort of
- * valid response token (be it "empty dequeue"
- * or a valid frame).
- */
- continue;
- }
- break;
- }
- /* Process FD */
- dpaa2_caam_process_fd(ppriv->priv, dpaa2_dq_fd(dq));
- cleaned++;
- } while (!is_last);
- return cleaned;
- }
- static int dpaa2_dpseci_poll(struct napi_struct *napi, int budget)
- {
- struct dpaa2_caam_priv_per_cpu *ppriv;
- struct dpaa2_caam_priv *priv;
- int err, cleaned = 0, store_cleaned;
- ppriv = container_of(napi, struct dpaa2_caam_priv_per_cpu, napi);
- priv = ppriv->priv;
- if (unlikely(dpaa2_caam_pull_fq(ppriv)))
- return 0;
- do {
- store_cleaned = dpaa2_caam_store_consume(ppriv);
- cleaned += store_cleaned;
- if (store_cleaned == 0 ||
- cleaned > budget - DPAA2_CAAM_STORE_SIZE)
- break;
- /* Try to dequeue some more */
- err = dpaa2_caam_pull_fq(ppriv);
- if (unlikely(err))
- break;
- } while (1);
- if (cleaned < budget) {
- napi_complete_done(napi, cleaned);
- err = dpaa2_io_service_rearm(ppriv->dpio, &ppriv->nctx);
- if (unlikely(err))
- dev_err(priv->dev, "Notification rearm failed: %d\n",
- err);
- }
- return cleaned;
- }
- static int dpaa2_dpseci_congestion_setup(struct dpaa2_caam_priv *priv,
- u16 token)
- {
- struct dpseci_congestion_notification_cfg cong_notif_cfg = { 0 };
- struct device *dev = priv->dev;
- int err;
- /*
- * Congestion group feature supported starting with DPSECI API v5.1
- * and only when object has been created with this capability.
- */
- if ((DPSECI_VER(priv->major_ver, priv->minor_ver) < DPSECI_VER(5, 1)) ||
- !(priv->dpseci_attr.options & DPSECI_OPT_HAS_CG))
- return 0;
- priv->cscn_mem = kzalloc(DPAA2_CSCN_SIZE + DPAA2_CSCN_ALIGN,
- GFP_KERNEL | GFP_DMA);
- if (!priv->cscn_mem)
- return -ENOMEM;
- priv->cscn_mem_aligned = PTR_ALIGN(priv->cscn_mem, DPAA2_CSCN_ALIGN);
- priv->cscn_dma = dma_map_single(dev, priv->cscn_mem_aligned,
- DPAA2_CSCN_SIZE, DMA_FROM_DEVICE);
- if (dma_mapping_error(dev, priv->cscn_dma)) {
- dev_err(dev, "Error mapping CSCN memory area\n");
- err = -ENOMEM;
- goto err_dma_map;
- }
- cong_notif_cfg.units = DPSECI_CONGESTION_UNIT_BYTES;
- cong_notif_cfg.threshold_entry = DPAA2_SEC_CONG_ENTRY_THRESH;
- cong_notif_cfg.threshold_exit = DPAA2_SEC_CONG_EXIT_THRESH;
- cong_notif_cfg.message_ctx = (uintptr_t)priv;
- cong_notif_cfg.message_iova = priv->cscn_dma;
- cong_notif_cfg.notification_mode = DPSECI_CGN_MODE_WRITE_MEM_ON_ENTER |
- DPSECI_CGN_MODE_WRITE_MEM_ON_EXIT |
- DPSECI_CGN_MODE_COHERENT_WRITE;
- err = dpseci_set_congestion_notification(priv->mc_io, 0, token,
- &cong_notif_cfg);
- if (err) {
- dev_err(dev, "dpseci_set_congestion_notification failed\n");
- goto err_set_cong;
- }
- return 0;
- err_set_cong:
- dma_unmap_single(dev, priv->cscn_dma, DPAA2_CSCN_SIZE, DMA_FROM_DEVICE);
- err_dma_map:
- kfree(priv->cscn_mem);
- return err;
- }
- static int __cold dpaa2_dpseci_setup(struct fsl_mc_device *ls_dev)
- {
- struct device *dev = &ls_dev->dev;
- struct dpaa2_caam_priv *priv;
- struct dpaa2_caam_priv_per_cpu *ppriv;
- int err, cpu;
- u8 i;
- priv = dev_get_drvdata(dev);
- priv->dev = dev;
- priv->dpsec_id = ls_dev->obj_desc.id;
- /* Get a handle for the DPSECI this interface is associate with */
- err = dpseci_open(priv->mc_io, 0, priv->dpsec_id, &ls_dev->mc_handle);
- if (err) {
- dev_err(dev, "dpseci_open() failed: %d\n", err);
- goto err_open;
- }
- err = dpseci_get_api_version(priv->mc_io, 0, &priv->major_ver,
- &priv->minor_ver);
- if (err) {
- dev_err(dev, "dpseci_get_api_version() failed\n");
- goto err_get_vers;
- }
- dev_info(dev, "dpseci v%d.%d\n", priv->major_ver, priv->minor_ver);
- err = dpseci_get_attributes(priv->mc_io, 0, ls_dev->mc_handle,
- &priv->dpseci_attr);
- if (err) {
- dev_err(dev, "dpseci_get_attributes() failed\n");
- goto err_get_vers;
- }
- err = dpseci_get_sec_attr(priv->mc_io, 0, ls_dev->mc_handle,
- &priv->sec_attr);
- if (err) {
- dev_err(dev, "dpseci_get_sec_attr() failed\n");
- goto err_get_vers;
- }
- err = dpaa2_dpseci_congestion_setup(priv, ls_dev->mc_handle);
- if (err) {
- dev_err(dev, "setup_congestion() failed\n");
- goto err_get_vers;
- }
- priv->num_pairs = min(priv->dpseci_attr.num_rx_queues,
- priv->dpseci_attr.num_tx_queues);
- if (priv->num_pairs > num_online_cpus()) {
- dev_warn(dev, "%d queues won't be used\n",
- priv->num_pairs - num_online_cpus());
- priv->num_pairs = num_online_cpus();
- }
- for (i = 0; i < priv->dpseci_attr.num_rx_queues; i++) {
- err = dpseci_get_rx_queue(priv->mc_io, 0, ls_dev->mc_handle, i,
- &priv->rx_queue_attr[i]);
- if (err) {
- dev_err(dev, "dpseci_get_rx_queue() failed\n");
- goto err_get_rx_queue;
- }
- }
- for (i = 0; i < priv->dpseci_attr.num_tx_queues; i++) {
- err = dpseci_get_tx_queue(priv->mc_io, 0, ls_dev->mc_handle, i,
- &priv->tx_queue_attr[i]);
- if (err) {
- dev_err(dev, "dpseci_get_tx_queue() failed\n");
- goto err_get_rx_queue;
- }
- }
- i = 0;
- for_each_online_cpu(cpu) {
- u8 j;
- j = i % priv->num_pairs;
- ppriv = per_cpu_ptr(priv->ppriv, cpu);
- ppriv->req_fqid = priv->tx_queue_attr[j].fqid;
- /*
- * Allow all cores to enqueue, while only some of them
- * will take part in dequeuing.
- */
- if (++i > priv->num_pairs)
- continue;
- ppriv->rsp_fqid = priv->rx_queue_attr[j].fqid;
- ppriv->prio = j;
- dev_dbg(dev, "pair %d: rx queue %d, tx queue %d\n", j,
- priv->rx_queue_attr[j].fqid,
- priv->tx_queue_attr[j].fqid);
- ppriv->net_dev.dev = *dev;
- INIT_LIST_HEAD(&ppriv->net_dev.napi_list);
- netif_napi_add(&ppriv->net_dev, &ppriv->napi, dpaa2_dpseci_poll,
- DPAA2_CAAM_NAPI_WEIGHT);
- }
- return 0;
- err_get_rx_queue:
- dpaa2_dpseci_congestion_free(priv);
- err_get_vers:
- dpseci_close(priv->mc_io, 0, ls_dev->mc_handle);
- err_open:
- return err;
- }
- static int dpaa2_dpseci_enable(struct dpaa2_caam_priv *priv)
- {
- struct device *dev = priv->dev;
- struct fsl_mc_device *ls_dev = to_fsl_mc_device(dev);
- struct dpaa2_caam_priv_per_cpu *ppriv;
- int i;
- for (i = 0; i < priv->num_pairs; i++) {
- ppriv = per_cpu_ptr(priv->ppriv, i);
- napi_enable(&ppriv->napi);
- }
- return dpseci_enable(priv->mc_io, 0, ls_dev->mc_handle);
- }
- static int __cold dpaa2_dpseci_disable(struct dpaa2_caam_priv *priv)
- {
- struct device *dev = priv->dev;
- struct dpaa2_caam_priv_per_cpu *ppriv;
- struct fsl_mc_device *ls_dev = to_fsl_mc_device(dev);
- int i, err = 0, enabled;
- err = dpseci_disable(priv->mc_io, 0, ls_dev->mc_handle);
- if (err) {
- dev_err(dev, "dpseci_disable() failed\n");
- return err;
- }
- err = dpseci_is_enabled(priv->mc_io, 0, ls_dev->mc_handle, &enabled);
- if (err) {
- dev_err(dev, "dpseci_is_enabled() failed\n");
- return err;
- }
- dev_dbg(dev, "disable: %s\n", enabled ? "false" : "true");
- for (i = 0; i < priv->num_pairs; i++) {
- ppriv = per_cpu_ptr(priv->ppriv, i);
- napi_disable(&ppriv->napi);
- netif_napi_del(&ppriv->napi);
- }
- return 0;
- }
- static struct list_head hash_list;
- static int dpaa2_caam_probe(struct fsl_mc_device *dpseci_dev)
- {
- struct device *dev;
- struct dpaa2_caam_priv *priv;
- int i, err = 0;
- bool registered = false;
- /*
- * There is no way to get CAAM endianness - there is no direct register
- * space access and MC f/w does not provide this attribute.
- * All DPAA2-based SoCs have little endian CAAM, thus hard-code this
- * property.
- */
- caam_little_end = true;
- caam_imx = false;
- dev = &dpseci_dev->dev;
- priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
- if (!priv)
- return -ENOMEM;
- dev_set_drvdata(dev, priv);
- priv->domain = iommu_get_domain_for_dev(dev);
- qi_cache = kmem_cache_create("dpaa2_caamqicache", CAAM_QI_MEMCACHE_SIZE,
- 0, SLAB_CACHE_DMA, NULL);
- if (!qi_cache) {
- dev_err(dev, "Can't allocate SEC cache\n");
- return -ENOMEM;
- }
- err = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(49));
- if (err) {
- dev_err(dev, "dma_set_mask_and_coherent() failed\n");
- goto err_dma_mask;
- }
- /* Obtain a MC portal */
- err = fsl_mc_portal_allocate(dpseci_dev, 0, &priv->mc_io);
- if (err) {
- if (err == -ENXIO)
- err = -EPROBE_DEFER;
- else
- dev_err(dev, "MC portal allocation failed\n");
- goto err_dma_mask;
- }
- priv->ppriv = alloc_percpu(*priv->ppriv);
- if (!priv->ppriv) {
- dev_err(dev, "alloc_percpu() failed\n");
- err = -ENOMEM;
- goto err_alloc_ppriv;
- }
- /* DPSECI initialization */
- err = dpaa2_dpseci_setup(dpseci_dev);
- if (err) {
- dev_err(dev, "dpaa2_dpseci_setup() failed\n");
- goto err_dpseci_setup;
- }
- /* DPIO */
- err = dpaa2_dpseci_dpio_setup(priv);
- if (err) {
- if (err != -EPROBE_DEFER)
- dev_err(dev, "dpaa2_dpseci_dpio_setup() failed\n");
- goto err_dpio_setup;
- }
- /* DPSECI binding to DPIO */
- err = dpaa2_dpseci_bind(priv);
- if (err) {
- dev_err(dev, "dpaa2_dpseci_bind() failed\n");
- goto err_bind;
- }
- /* DPSECI enable */
- err = dpaa2_dpseci_enable(priv);
- if (err) {
- dev_err(dev, "dpaa2_dpseci_enable() failed\n");
- goto err_bind;
- }
- dpaa2_dpseci_debugfs_init(priv);
- /* register crypto algorithms the device supports */
- for (i = 0; i < ARRAY_SIZE(driver_algs); i++) {
- struct caam_skcipher_alg *t_alg = driver_algs + i;
- u32 alg_sel = t_alg->caam.class1_alg_type & OP_ALG_ALGSEL_MASK;
- /* Skip DES algorithms if not supported by device */
- if (!priv->sec_attr.des_acc_num &&
- (alg_sel == OP_ALG_ALGSEL_3DES ||
- alg_sel == OP_ALG_ALGSEL_DES))
- continue;
- /* Skip AES algorithms if not supported by device */
- if (!priv->sec_attr.aes_acc_num &&
- alg_sel == OP_ALG_ALGSEL_AES)
- continue;
- /* Skip CHACHA20 algorithms if not supported by device */
- if (alg_sel == OP_ALG_ALGSEL_CHACHA20 &&
- !priv->sec_attr.ccha_acc_num)
- continue;
- t_alg->caam.dev = dev;
- caam_skcipher_alg_init(t_alg);
- err = crypto_register_skcipher(&t_alg->skcipher);
- if (err) {
- dev_warn(dev, "%s alg registration failed: %d\n",
- t_alg->skcipher.base.cra_driver_name, err);
- continue;
- }
- t_alg->registered = true;
- registered = true;
- }
- for (i = 0; i < ARRAY_SIZE(driver_aeads); i++) {
- struct caam_aead_alg *t_alg = driver_aeads + i;
- u32 c1_alg_sel = t_alg->caam.class1_alg_type &
- OP_ALG_ALGSEL_MASK;
- u32 c2_alg_sel = t_alg->caam.class2_alg_type &
- OP_ALG_ALGSEL_MASK;
- /* Skip DES algorithms if not supported by device */
- if (!priv->sec_attr.des_acc_num &&
- (c1_alg_sel == OP_ALG_ALGSEL_3DES ||
- c1_alg_sel == OP_ALG_ALGSEL_DES))
- continue;
- /* Skip AES algorithms if not supported by device */
- if (!priv->sec_attr.aes_acc_num &&
- c1_alg_sel == OP_ALG_ALGSEL_AES)
- continue;
- /* Skip CHACHA20 algorithms if not supported by device */
- if (c1_alg_sel == OP_ALG_ALGSEL_CHACHA20 &&
- !priv->sec_attr.ccha_acc_num)
- continue;
- /* Skip POLY1305 algorithms if not supported by device */
- if (c2_alg_sel == OP_ALG_ALGSEL_POLY1305 &&
- !priv->sec_attr.ptha_acc_num)
- continue;
- /*
- * Skip algorithms requiring message digests
- * if MD not supported by device.
- */
- if ((c2_alg_sel & ~OP_ALG_ALGSEL_SUBMASK) == 0x40 &&
- !priv->sec_attr.md_acc_num)
- continue;
- t_alg->caam.dev = dev;
- caam_aead_alg_init(t_alg);
- err = crypto_register_aead(&t_alg->aead);
- if (err) {
- dev_warn(dev, "%s alg registration failed: %d\n",
- t_alg->aead.base.cra_driver_name, err);
- continue;
- }
- t_alg->registered = true;
- registered = true;
- }
- if (registered)
- dev_info(dev, "algorithms registered in /proc/crypto\n");
- /* register hash algorithms the device supports */
- INIT_LIST_HEAD(&hash_list);
- /*
- * Skip registration of any hashing algorithms if MD block
- * is not present.
- */
- if (!priv->sec_attr.md_acc_num)
- return 0;
- for (i = 0; i < ARRAY_SIZE(driver_hash); i++) {
- struct caam_hash_alg *t_alg;
- struct caam_hash_template *alg = driver_hash + i;
- /* register hmac version */
- t_alg = caam_hash_alloc(dev, alg, true);
- if (IS_ERR(t_alg)) {
- err = PTR_ERR(t_alg);
- dev_warn(dev, "%s hash alg allocation failed: %d\n",
- alg->driver_name, err);
- continue;
- }
- err = crypto_register_ahash(&t_alg->ahash_alg);
- if (err) {
- dev_warn(dev, "%s alg registration failed: %d\n",
- t_alg->ahash_alg.halg.base.cra_driver_name,
- err);
- kfree(t_alg);
- } else {
- list_add_tail(&t_alg->entry, &hash_list);
- }
- /* register unkeyed version */
- t_alg = caam_hash_alloc(dev, alg, false);
- if (IS_ERR(t_alg)) {
- err = PTR_ERR(t_alg);
- dev_warn(dev, "%s alg allocation failed: %d\n",
- alg->driver_name, err);
- continue;
- }
- err = crypto_register_ahash(&t_alg->ahash_alg);
- if (err) {
- dev_warn(dev, "%s alg registration failed: %d\n",
- t_alg->ahash_alg.halg.base.cra_driver_name,
- err);
- kfree(t_alg);
- } else {
- list_add_tail(&t_alg->entry, &hash_list);
- }
- }
- if (!list_empty(&hash_list))
- dev_info(dev, "hash algorithms registered in /proc/crypto\n");
- return err;
- err_bind:
- dpaa2_dpseci_dpio_free(priv);
- err_dpio_setup:
- dpaa2_dpseci_free(priv);
- err_dpseci_setup:
- free_percpu(priv->ppriv);
- err_alloc_ppriv:
- fsl_mc_portal_free(priv->mc_io);
- err_dma_mask:
- kmem_cache_destroy(qi_cache);
- return err;
- }
- static int __cold dpaa2_caam_remove(struct fsl_mc_device *ls_dev)
- {
- struct device *dev;
- struct dpaa2_caam_priv *priv;
- int i;
- dev = &ls_dev->dev;
- priv = dev_get_drvdata(dev);
- dpaa2_dpseci_debugfs_exit(priv);
- for (i = 0; i < ARRAY_SIZE(driver_aeads); i++) {
- struct caam_aead_alg *t_alg = driver_aeads + i;
- if (t_alg->registered)
- crypto_unregister_aead(&t_alg->aead);
- }
- for (i = 0; i < ARRAY_SIZE(driver_algs); i++) {
- struct caam_skcipher_alg *t_alg = driver_algs + i;
- if (t_alg->registered)
- crypto_unregister_skcipher(&t_alg->skcipher);
- }
- if (hash_list.next) {
- struct caam_hash_alg *t_hash_alg, *p;
- list_for_each_entry_safe(t_hash_alg, p, &hash_list, entry) {
- crypto_unregister_ahash(&t_hash_alg->ahash_alg);
- list_del(&t_hash_alg->entry);
- kfree(t_hash_alg);
- }
- }
- dpaa2_dpseci_disable(priv);
- dpaa2_dpseci_dpio_free(priv);
- dpaa2_dpseci_free(priv);
- free_percpu(priv->ppriv);
- fsl_mc_portal_free(priv->mc_io);
- kmem_cache_destroy(qi_cache);
- return 0;
- }
- int dpaa2_caam_enqueue(struct device *dev, struct caam_request *req)
- {
- struct dpaa2_fd fd;
- struct dpaa2_caam_priv *priv = dev_get_drvdata(dev);
- struct dpaa2_caam_priv_per_cpu *ppriv;
- int err = 0, i;
- if (IS_ERR(req))
- return PTR_ERR(req);
- if (priv->cscn_mem) {
- dma_sync_single_for_cpu(priv->dev, priv->cscn_dma,
- DPAA2_CSCN_SIZE,
- DMA_FROM_DEVICE);
- if (unlikely(dpaa2_cscn_state_congested(priv->cscn_mem_aligned))) {
- dev_dbg_ratelimited(dev, "Dropping request\n");
- return -EBUSY;
- }
- }
- dpaa2_fl_set_flc(&req->fd_flt[1], req->flc_dma);
- req->fd_flt_dma = dma_map_single(dev, req->fd_flt, sizeof(req->fd_flt),
- DMA_BIDIRECTIONAL);
- if (dma_mapping_error(dev, req->fd_flt_dma)) {
- dev_err(dev, "DMA mapping error for QI enqueue request\n");
- goto err_out;
- }
- memset(&fd, 0, sizeof(fd));
- dpaa2_fd_set_format(&fd, dpaa2_fd_list);
- dpaa2_fd_set_addr(&fd, req->fd_flt_dma);
- dpaa2_fd_set_len(&fd, dpaa2_fl_get_len(&req->fd_flt[1]));
- dpaa2_fd_set_flc(&fd, req->flc_dma);
- ppriv = this_cpu_ptr(priv->ppriv);
- for (i = 0; i < (priv->dpseci_attr.num_tx_queues << 1); i++) {
- err = dpaa2_io_service_enqueue_fq(ppriv->dpio, ppriv->req_fqid,
- &fd);
- if (err != -EBUSY)
- break;
- cpu_relax();
- }
- if (unlikely(err)) {
- dev_err_ratelimited(dev, "Error enqueuing frame: %d\n", err);
- goto err_out;
- }
- return -EINPROGRESS;
- err_out:
- dma_unmap_single(dev, req->fd_flt_dma, sizeof(req->fd_flt),
- DMA_BIDIRECTIONAL);
- return -EIO;
- }
- EXPORT_SYMBOL(dpaa2_caam_enqueue);
- static const struct fsl_mc_device_id dpaa2_caam_match_id_table[] = {
- {
- .vendor = FSL_MC_VENDOR_FREESCALE,
- .obj_type = "dpseci",
- },
- { .vendor = 0x0 }
- };
- static struct fsl_mc_driver dpaa2_caam_driver = {
- .driver = {
- .name = KBUILD_MODNAME,
- .owner = THIS_MODULE,
- },
- .probe = dpaa2_caam_probe,
- .remove = dpaa2_caam_remove,
- .match_id_table = dpaa2_caam_match_id_table
- };
- MODULE_LICENSE("Dual BSD/GPL");
- MODULE_AUTHOR("Freescale Semiconductor, Inc");
- MODULE_DESCRIPTION("Freescale DPAA2 CAAM Driver");
- module_fsl_mc_driver(dpaa2_caam_driver);
|