caamalg_qi2.c 147 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471
  1. // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
  2. /*
  3. * Copyright 2015-2016 Freescale Semiconductor Inc.
  4. * Copyright 2017-2019 NXP
  5. */
  6. #include "compat.h"
  7. #include "regs.h"
  8. #include "caamalg_qi2.h"
  9. #include "dpseci_cmd.h"
  10. #include "desc_constr.h"
  11. #include "error.h"
  12. #include "sg_sw_sec4.h"
  13. #include "sg_sw_qm2.h"
  14. #include "key_gen.h"
  15. #include "caamalg_desc.h"
  16. #include "caamhash_desc.h"
  17. #include "dpseci-debugfs.h"
  18. #include <linux/fsl/mc.h>
  19. #include <soc/fsl/dpaa2-io.h>
  20. #include <soc/fsl/dpaa2-fd.h>
  21. #define CAAM_CRA_PRIORITY 2000
  22. /* max key is sum of AES_MAX_KEY_SIZE, max split key size */
  23. #define CAAM_MAX_KEY_SIZE (AES_MAX_KEY_SIZE + CTR_RFC3686_NONCE_SIZE + \
  24. SHA512_DIGEST_SIZE * 2)
  25. /*
  26. * This is a a cache of buffers, from which the users of CAAM QI driver
  27. * can allocate short buffers. It's speedier than doing kmalloc on the hotpath.
  28. * NOTE: A more elegant solution would be to have some headroom in the frames
  29. * being processed. This can be added by the dpaa2-eth driver. This would
  30. * pose a problem for userspace application processing which cannot
  31. * know of this limitation. So for now, this will work.
  32. * NOTE: The memcache is SMP-safe. No need to handle spinlocks in-here
  33. */
  34. static struct kmem_cache *qi_cache;
  35. struct caam_alg_entry {
  36. struct device *dev;
  37. int class1_alg_type;
  38. int class2_alg_type;
  39. bool rfc3686;
  40. bool geniv;
  41. bool nodkp;
  42. };
  43. struct caam_aead_alg {
  44. struct aead_alg aead;
  45. struct caam_alg_entry caam;
  46. bool registered;
  47. };
  48. struct caam_skcipher_alg {
  49. struct skcipher_alg skcipher;
  50. struct caam_alg_entry caam;
  51. bool registered;
  52. };
  53. /**
  54. * caam_ctx - per-session context
  55. * @flc: Flow Contexts array
  56. * @key: [authentication key], encryption key
  57. * @flc_dma: I/O virtual addresses of the Flow Contexts
  58. * @key_dma: I/O virtual address of the key
  59. * @dir: DMA direction for mapping key and Flow Contexts
  60. * @dev: dpseci device
  61. * @adata: authentication algorithm details
  62. * @cdata: encryption algorithm details
  63. * @authsize: authentication tag (a.k.a. ICV / MAC) size
  64. */
  65. struct caam_ctx {
  66. struct caam_flc flc[NUM_OP];
  67. u8 key[CAAM_MAX_KEY_SIZE];
  68. dma_addr_t flc_dma[NUM_OP];
  69. dma_addr_t key_dma;
  70. enum dma_data_direction dir;
  71. struct device *dev;
  72. struct alginfo adata;
  73. struct alginfo cdata;
  74. unsigned int authsize;
  75. };
  76. static void *dpaa2_caam_iova_to_virt(struct dpaa2_caam_priv *priv,
  77. dma_addr_t iova_addr)
  78. {
  79. phys_addr_t phys_addr;
  80. phys_addr = priv->domain ? iommu_iova_to_phys(priv->domain, iova_addr) :
  81. iova_addr;
  82. return phys_to_virt(phys_addr);
  83. }
  84. /*
  85. * qi_cache_zalloc - Allocate buffers from CAAM-QI cache
  86. *
  87. * Allocate data on the hotpath. Instead of using kzalloc, one can use the
  88. * services of the CAAM QI memory cache (backed by kmem_cache). The buffers
  89. * will have a size of CAAM_QI_MEMCACHE_SIZE, which should be sufficient for
  90. * hosting 16 SG entries.
  91. *
  92. * @flags - flags that would be used for the equivalent kmalloc(..) call
  93. *
  94. * Returns a pointer to a retrieved buffer on success or NULL on failure.
  95. */
  96. static inline void *qi_cache_zalloc(gfp_t flags)
  97. {
  98. return kmem_cache_zalloc(qi_cache, flags);
  99. }
  100. /*
  101. * qi_cache_free - Frees buffers allocated from CAAM-QI cache
  102. *
  103. * @obj - buffer previously allocated by qi_cache_zalloc
  104. *
  105. * No checking is being done, the call is a passthrough call to
  106. * kmem_cache_free(...)
  107. */
  108. static inline void qi_cache_free(void *obj)
  109. {
  110. kmem_cache_free(qi_cache, obj);
  111. }
  112. static struct caam_request *to_caam_req(struct crypto_async_request *areq)
  113. {
  114. switch (crypto_tfm_alg_type(areq->tfm)) {
  115. case CRYPTO_ALG_TYPE_SKCIPHER:
  116. return skcipher_request_ctx(skcipher_request_cast(areq));
  117. case CRYPTO_ALG_TYPE_AEAD:
  118. return aead_request_ctx(container_of(areq, struct aead_request,
  119. base));
  120. case CRYPTO_ALG_TYPE_AHASH:
  121. return ahash_request_ctx(ahash_request_cast(areq));
  122. default:
  123. return ERR_PTR(-EINVAL);
  124. }
  125. }
  126. static void caam_unmap(struct device *dev, struct scatterlist *src,
  127. struct scatterlist *dst, int src_nents,
  128. int dst_nents, dma_addr_t iv_dma, int ivsize,
  129. enum dma_data_direction iv_dir, dma_addr_t qm_sg_dma,
  130. int qm_sg_bytes)
  131. {
  132. if (dst != src) {
  133. if (src_nents)
  134. dma_unmap_sg(dev, src, src_nents, DMA_TO_DEVICE);
  135. if (dst_nents)
  136. dma_unmap_sg(dev, dst, dst_nents, DMA_FROM_DEVICE);
  137. } else {
  138. dma_unmap_sg(dev, src, src_nents, DMA_BIDIRECTIONAL);
  139. }
  140. if (iv_dma)
  141. dma_unmap_single(dev, iv_dma, ivsize, iv_dir);
  142. if (qm_sg_bytes)
  143. dma_unmap_single(dev, qm_sg_dma, qm_sg_bytes, DMA_TO_DEVICE);
  144. }
  145. static int aead_set_sh_desc(struct crypto_aead *aead)
  146. {
  147. struct caam_aead_alg *alg = container_of(crypto_aead_alg(aead),
  148. typeof(*alg), aead);
  149. struct caam_ctx *ctx = crypto_aead_ctx(aead);
  150. unsigned int ivsize = crypto_aead_ivsize(aead);
  151. struct device *dev = ctx->dev;
  152. struct dpaa2_caam_priv *priv = dev_get_drvdata(dev);
  153. struct caam_flc *flc;
  154. u32 *desc;
  155. u32 ctx1_iv_off = 0;
  156. u32 *nonce = NULL;
  157. unsigned int data_len[2];
  158. u32 inl_mask;
  159. const bool ctr_mode = ((ctx->cdata.algtype & OP_ALG_AAI_MASK) ==
  160. OP_ALG_AAI_CTR_MOD128);
  161. const bool is_rfc3686 = alg->caam.rfc3686;
  162. if (!ctx->cdata.keylen || !ctx->authsize)
  163. return 0;
  164. /*
  165. * AES-CTR needs to load IV in CONTEXT1 reg
  166. * at an offset of 128bits (16bytes)
  167. * CONTEXT1[255:128] = IV
  168. */
  169. if (ctr_mode)
  170. ctx1_iv_off = 16;
  171. /*
  172. * RFC3686 specific:
  173. * CONTEXT1[255:128] = {NONCE, IV, COUNTER}
  174. */
  175. if (is_rfc3686) {
  176. ctx1_iv_off = 16 + CTR_RFC3686_NONCE_SIZE;
  177. nonce = (u32 *)((void *)ctx->key + ctx->adata.keylen_pad +
  178. ctx->cdata.keylen - CTR_RFC3686_NONCE_SIZE);
  179. }
  180. /*
  181. * In case |user key| > |derived key|, using DKP<imm,imm> would result
  182. * in invalid opcodes (last bytes of user key) in the resulting
  183. * descriptor. Use DKP<ptr,imm> instead => both virtual and dma key
  184. * addresses are needed.
  185. */
  186. ctx->adata.key_virt = ctx->key;
  187. ctx->adata.key_dma = ctx->key_dma;
  188. ctx->cdata.key_virt = ctx->key + ctx->adata.keylen_pad;
  189. ctx->cdata.key_dma = ctx->key_dma + ctx->adata.keylen_pad;
  190. data_len[0] = ctx->adata.keylen_pad;
  191. data_len[1] = ctx->cdata.keylen;
  192. /* aead_encrypt shared descriptor */
  193. if (desc_inline_query((alg->caam.geniv ? DESC_QI_AEAD_GIVENC_LEN :
  194. DESC_QI_AEAD_ENC_LEN) +
  195. (is_rfc3686 ? DESC_AEAD_CTR_RFC3686_LEN : 0),
  196. DESC_JOB_IO_LEN, data_len, &inl_mask,
  197. ARRAY_SIZE(data_len)) < 0)
  198. return -EINVAL;
  199. ctx->adata.key_inline = !!(inl_mask & 1);
  200. ctx->cdata.key_inline = !!(inl_mask & 2);
  201. flc = &ctx->flc[ENCRYPT];
  202. desc = flc->sh_desc;
  203. if (alg->caam.geniv)
  204. cnstr_shdsc_aead_givencap(desc, &ctx->cdata, &ctx->adata,
  205. ivsize, ctx->authsize, is_rfc3686,
  206. nonce, ctx1_iv_off, true,
  207. priv->sec_attr.era);
  208. else
  209. cnstr_shdsc_aead_encap(desc, &ctx->cdata, &ctx->adata,
  210. ivsize, ctx->authsize, is_rfc3686, nonce,
  211. ctx1_iv_off, true, priv->sec_attr.era);
  212. flc->flc[1] = cpu_to_caam32(desc_len(desc)); /* SDL */
  213. dma_sync_single_for_device(dev, ctx->flc_dma[ENCRYPT],
  214. sizeof(flc->flc) + desc_bytes(desc),
  215. ctx->dir);
  216. /* aead_decrypt shared descriptor */
  217. if (desc_inline_query(DESC_QI_AEAD_DEC_LEN +
  218. (is_rfc3686 ? DESC_AEAD_CTR_RFC3686_LEN : 0),
  219. DESC_JOB_IO_LEN, data_len, &inl_mask,
  220. ARRAY_SIZE(data_len)) < 0)
  221. return -EINVAL;
  222. ctx->adata.key_inline = !!(inl_mask & 1);
  223. ctx->cdata.key_inline = !!(inl_mask & 2);
  224. flc = &ctx->flc[DECRYPT];
  225. desc = flc->sh_desc;
  226. cnstr_shdsc_aead_decap(desc, &ctx->cdata, &ctx->adata,
  227. ivsize, ctx->authsize, alg->caam.geniv,
  228. is_rfc3686, nonce, ctx1_iv_off, true,
  229. priv->sec_attr.era);
  230. flc->flc[1] = cpu_to_caam32(desc_len(desc)); /* SDL */
  231. dma_sync_single_for_device(dev, ctx->flc_dma[DECRYPT],
  232. sizeof(flc->flc) + desc_bytes(desc),
  233. ctx->dir);
  234. return 0;
  235. }
  236. static int aead_setauthsize(struct crypto_aead *authenc, unsigned int authsize)
  237. {
  238. struct caam_ctx *ctx = crypto_aead_ctx(authenc);
  239. ctx->authsize = authsize;
  240. aead_set_sh_desc(authenc);
  241. return 0;
  242. }
  243. static int aead_setkey(struct crypto_aead *aead, const u8 *key,
  244. unsigned int keylen)
  245. {
  246. struct caam_ctx *ctx = crypto_aead_ctx(aead);
  247. struct device *dev = ctx->dev;
  248. struct crypto_authenc_keys keys;
  249. if (crypto_authenc_extractkeys(&keys, key, keylen) != 0)
  250. goto badkey;
  251. dev_dbg(dev, "keylen %d enckeylen %d authkeylen %d\n",
  252. keys.authkeylen + keys.enckeylen, keys.enckeylen,
  253. keys.authkeylen);
  254. print_hex_dump_debug("key in @" __stringify(__LINE__)": ",
  255. DUMP_PREFIX_ADDRESS, 16, 4, key, keylen, 1);
  256. ctx->adata.keylen = keys.authkeylen;
  257. ctx->adata.keylen_pad = split_key_len(ctx->adata.algtype &
  258. OP_ALG_ALGSEL_MASK);
  259. if (ctx->adata.keylen_pad + keys.enckeylen > CAAM_MAX_KEY_SIZE)
  260. goto badkey;
  261. memcpy(ctx->key, keys.authkey, keys.authkeylen);
  262. memcpy(ctx->key + ctx->adata.keylen_pad, keys.enckey, keys.enckeylen);
  263. dma_sync_single_for_device(dev, ctx->key_dma, ctx->adata.keylen_pad +
  264. keys.enckeylen, ctx->dir);
  265. print_hex_dump_debug("ctx.key@" __stringify(__LINE__)": ",
  266. DUMP_PREFIX_ADDRESS, 16, 4, ctx->key,
  267. ctx->adata.keylen_pad + keys.enckeylen, 1);
  268. ctx->cdata.keylen = keys.enckeylen;
  269. memzero_explicit(&keys, sizeof(keys));
  270. return aead_set_sh_desc(aead);
  271. badkey:
  272. crypto_aead_set_flags(aead, CRYPTO_TFM_RES_BAD_KEY_LEN);
  273. memzero_explicit(&keys, sizeof(keys));
  274. return -EINVAL;
  275. }
  276. static int des3_aead_setkey(struct crypto_aead *aead, const u8 *key,
  277. unsigned int keylen)
  278. {
  279. struct crypto_authenc_keys keys;
  280. int err;
  281. err = crypto_authenc_extractkeys(&keys, key, keylen);
  282. if (unlikely(err))
  283. goto badkey;
  284. err = -EINVAL;
  285. if (keys.enckeylen != DES3_EDE_KEY_SIZE)
  286. goto badkey;
  287. err = crypto_des3_ede_verify_key(crypto_aead_tfm(aead), keys.enckey) ?:
  288. aead_setkey(aead, key, keylen);
  289. out:
  290. memzero_explicit(&keys, sizeof(keys));
  291. return err;
  292. badkey:
  293. crypto_aead_set_flags(aead, CRYPTO_TFM_RES_BAD_KEY_LEN);
  294. goto out;
  295. }
  296. static struct aead_edesc *aead_edesc_alloc(struct aead_request *req,
  297. bool encrypt)
  298. {
  299. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  300. struct caam_request *req_ctx = aead_request_ctx(req);
  301. struct dpaa2_fl_entry *in_fle = &req_ctx->fd_flt[1];
  302. struct dpaa2_fl_entry *out_fle = &req_ctx->fd_flt[0];
  303. struct caam_ctx *ctx = crypto_aead_ctx(aead);
  304. struct caam_aead_alg *alg = container_of(crypto_aead_alg(aead),
  305. typeof(*alg), aead);
  306. struct device *dev = ctx->dev;
  307. gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
  308. GFP_KERNEL : GFP_ATOMIC;
  309. int src_nents, mapped_src_nents, dst_nents = 0, mapped_dst_nents = 0;
  310. int src_len, dst_len = 0;
  311. struct aead_edesc *edesc;
  312. dma_addr_t qm_sg_dma, iv_dma = 0;
  313. int ivsize = 0;
  314. unsigned int authsize = ctx->authsize;
  315. int qm_sg_index = 0, qm_sg_nents = 0, qm_sg_bytes;
  316. int in_len, out_len;
  317. struct dpaa2_sg_entry *sg_table;
  318. /* allocate space for base edesc, link tables and IV */
  319. edesc = qi_cache_zalloc(GFP_DMA | flags);
  320. if (unlikely(!edesc)) {
  321. dev_err(dev, "could not allocate extended descriptor\n");
  322. return ERR_PTR(-ENOMEM);
  323. }
  324. if (unlikely(req->dst != req->src)) {
  325. src_len = req->assoclen + req->cryptlen;
  326. dst_len = src_len + (encrypt ? authsize : (-authsize));
  327. src_nents = sg_nents_for_len(req->src, src_len);
  328. if (unlikely(src_nents < 0)) {
  329. dev_err(dev, "Insufficient bytes (%d) in src S/G\n",
  330. src_len);
  331. qi_cache_free(edesc);
  332. return ERR_PTR(src_nents);
  333. }
  334. dst_nents = sg_nents_for_len(req->dst, dst_len);
  335. if (unlikely(dst_nents < 0)) {
  336. dev_err(dev, "Insufficient bytes (%d) in dst S/G\n",
  337. dst_len);
  338. qi_cache_free(edesc);
  339. return ERR_PTR(dst_nents);
  340. }
  341. if (src_nents) {
  342. mapped_src_nents = dma_map_sg(dev, req->src, src_nents,
  343. DMA_TO_DEVICE);
  344. if (unlikely(!mapped_src_nents)) {
  345. dev_err(dev, "unable to map source\n");
  346. qi_cache_free(edesc);
  347. return ERR_PTR(-ENOMEM);
  348. }
  349. } else {
  350. mapped_src_nents = 0;
  351. }
  352. if (dst_nents) {
  353. mapped_dst_nents = dma_map_sg(dev, req->dst, dst_nents,
  354. DMA_FROM_DEVICE);
  355. if (unlikely(!mapped_dst_nents)) {
  356. dev_err(dev, "unable to map destination\n");
  357. dma_unmap_sg(dev, req->src, src_nents,
  358. DMA_TO_DEVICE);
  359. qi_cache_free(edesc);
  360. return ERR_PTR(-ENOMEM);
  361. }
  362. } else {
  363. mapped_dst_nents = 0;
  364. }
  365. } else {
  366. src_len = req->assoclen + req->cryptlen +
  367. (encrypt ? authsize : 0);
  368. src_nents = sg_nents_for_len(req->src, src_len);
  369. if (unlikely(src_nents < 0)) {
  370. dev_err(dev, "Insufficient bytes (%d) in src S/G\n",
  371. src_len);
  372. qi_cache_free(edesc);
  373. return ERR_PTR(src_nents);
  374. }
  375. mapped_src_nents = dma_map_sg(dev, req->src, src_nents,
  376. DMA_BIDIRECTIONAL);
  377. if (unlikely(!mapped_src_nents)) {
  378. dev_err(dev, "unable to map source\n");
  379. qi_cache_free(edesc);
  380. return ERR_PTR(-ENOMEM);
  381. }
  382. }
  383. if ((alg->caam.rfc3686 && encrypt) || !alg->caam.geniv)
  384. ivsize = crypto_aead_ivsize(aead);
  385. /*
  386. * Create S/G table: req->assoclen, [IV,] req->src [, req->dst].
  387. * Input is not contiguous.
  388. * HW reads 4 S/G entries at a time; make sure the reads don't go beyond
  389. * the end of the table by allocating more S/G entries. Logic:
  390. * if (src != dst && output S/G)
  391. * pad output S/G, if needed
  392. * else if (src == dst && S/G)
  393. * overlapping S/Gs; pad one of them
  394. * else if (input S/G) ...
  395. * pad input S/G, if needed
  396. */
  397. qm_sg_nents = 1 + !!ivsize + mapped_src_nents;
  398. if (mapped_dst_nents > 1)
  399. qm_sg_nents += pad_sg_nents(mapped_dst_nents);
  400. else if ((req->src == req->dst) && (mapped_src_nents > 1))
  401. qm_sg_nents = max(pad_sg_nents(qm_sg_nents),
  402. 1 + !!ivsize +
  403. pad_sg_nents(mapped_src_nents));
  404. else
  405. qm_sg_nents = pad_sg_nents(qm_sg_nents);
  406. sg_table = &edesc->sgt[0];
  407. qm_sg_bytes = qm_sg_nents * sizeof(*sg_table);
  408. if (unlikely(offsetof(struct aead_edesc, sgt) + qm_sg_bytes + ivsize >
  409. CAAM_QI_MEMCACHE_SIZE)) {
  410. dev_err(dev, "No space for %d S/G entries and/or %dB IV\n",
  411. qm_sg_nents, ivsize);
  412. caam_unmap(dev, req->src, req->dst, src_nents, dst_nents, 0,
  413. 0, DMA_NONE, 0, 0);
  414. qi_cache_free(edesc);
  415. return ERR_PTR(-ENOMEM);
  416. }
  417. if (ivsize) {
  418. u8 *iv = (u8 *)(sg_table + qm_sg_nents);
  419. /* Make sure IV is located in a DMAable area */
  420. memcpy(iv, req->iv, ivsize);
  421. iv_dma = dma_map_single(dev, iv, ivsize, DMA_TO_DEVICE);
  422. if (dma_mapping_error(dev, iv_dma)) {
  423. dev_err(dev, "unable to map IV\n");
  424. caam_unmap(dev, req->src, req->dst, src_nents,
  425. dst_nents, 0, 0, DMA_NONE, 0, 0);
  426. qi_cache_free(edesc);
  427. return ERR_PTR(-ENOMEM);
  428. }
  429. }
  430. edesc->src_nents = src_nents;
  431. edesc->dst_nents = dst_nents;
  432. edesc->iv_dma = iv_dma;
  433. if ((alg->caam.class1_alg_type & OP_ALG_ALGSEL_MASK) ==
  434. OP_ALG_ALGSEL_CHACHA20 && ivsize != CHACHAPOLY_IV_SIZE)
  435. /*
  436. * The associated data comes already with the IV but we need
  437. * to skip it when we authenticate or encrypt...
  438. */
  439. edesc->assoclen = cpu_to_caam32(req->assoclen - ivsize);
  440. else
  441. edesc->assoclen = cpu_to_caam32(req->assoclen);
  442. edesc->assoclen_dma = dma_map_single(dev, &edesc->assoclen, 4,
  443. DMA_TO_DEVICE);
  444. if (dma_mapping_error(dev, edesc->assoclen_dma)) {
  445. dev_err(dev, "unable to map assoclen\n");
  446. caam_unmap(dev, req->src, req->dst, src_nents, dst_nents,
  447. iv_dma, ivsize, DMA_TO_DEVICE, 0, 0);
  448. qi_cache_free(edesc);
  449. return ERR_PTR(-ENOMEM);
  450. }
  451. dma_to_qm_sg_one(sg_table, edesc->assoclen_dma, 4, 0);
  452. qm_sg_index++;
  453. if (ivsize) {
  454. dma_to_qm_sg_one(sg_table + qm_sg_index, iv_dma, ivsize, 0);
  455. qm_sg_index++;
  456. }
  457. sg_to_qm_sg_last(req->src, src_len, sg_table + qm_sg_index, 0);
  458. qm_sg_index += mapped_src_nents;
  459. if (mapped_dst_nents > 1)
  460. sg_to_qm_sg_last(req->dst, dst_len, sg_table + qm_sg_index, 0);
  461. qm_sg_dma = dma_map_single(dev, sg_table, qm_sg_bytes, DMA_TO_DEVICE);
  462. if (dma_mapping_error(dev, qm_sg_dma)) {
  463. dev_err(dev, "unable to map S/G table\n");
  464. dma_unmap_single(dev, edesc->assoclen_dma, 4, DMA_TO_DEVICE);
  465. caam_unmap(dev, req->src, req->dst, src_nents, dst_nents,
  466. iv_dma, ivsize, DMA_TO_DEVICE, 0, 0);
  467. qi_cache_free(edesc);
  468. return ERR_PTR(-ENOMEM);
  469. }
  470. edesc->qm_sg_dma = qm_sg_dma;
  471. edesc->qm_sg_bytes = qm_sg_bytes;
  472. out_len = req->assoclen + req->cryptlen +
  473. (encrypt ? ctx->authsize : (-ctx->authsize));
  474. in_len = 4 + ivsize + req->assoclen + req->cryptlen;
  475. memset(&req_ctx->fd_flt, 0, sizeof(req_ctx->fd_flt));
  476. dpaa2_fl_set_final(in_fle, true);
  477. dpaa2_fl_set_format(in_fle, dpaa2_fl_sg);
  478. dpaa2_fl_set_addr(in_fle, qm_sg_dma);
  479. dpaa2_fl_set_len(in_fle, in_len);
  480. if (req->dst == req->src) {
  481. if (mapped_src_nents == 1) {
  482. dpaa2_fl_set_format(out_fle, dpaa2_fl_single);
  483. dpaa2_fl_set_addr(out_fle, sg_dma_address(req->src));
  484. } else {
  485. dpaa2_fl_set_format(out_fle, dpaa2_fl_sg);
  486. dpaa2_fl_set_addr(out_fle, qm_sg_dma +
  487. (1 + !!ivsize) * sizeof(*sg_table));
  488. }
  489. } else if (!mapped_dst_nents) {
  490. /*
  491. * crypto engine requires the output entry to be present when
  492. * "frame list" FD is used.
  493. * Since engine does not support FMT=2'b11 (unused entry type),
  494. * leaving out_fle zeroized is the best option.
  495. */
  496. goto skip_out_fle;
  497. } else if (mapped_dst_nents == 1) {
  498. dpaa2_fl_set_format(out_fle, dpaa2_fl_single);
  499. dpaa2_fl_set_addr(out_fle, sg_dma_address(req->dst));
  500. } else {
  501. dpaa2_fl_set_format(out_fle, dpaa2_fl_sg);
  502. dpaa2_fl_set_addr(out_fle, qm_sg_dma + qm_sg_index *
  503. sizeof(*sg_table));
  504. }
  505. dpaa2_fl_set_len(out_fle, out_len);
  506. skip_out_fle:
  507. return edesc;
  508. }
  509. static int chachapoly_set_sh_desc(struct crypto_aead *aead)
  510. {
  511. struct caam_ctx *ctx = crypto_aead_ctx(aead);
  512. unsigned int ivsize = crypto_aead_ivsize(aead);
  513. struct device *dev = ctx->dev;
  514. struct caam_flc *flc;
  515. u32 *desc;
  516. if (!ctx->cdata.keylen || !ctx->authsize)
  517. return 0;
  518. flc = &ctx->flc[ENCRYPT];
  519. desc = flc->sh_desc;
  520. cnstr_shdsc_chachapoly(desc, &ctx->cdata, &ctx->adata, ivsize,
  521. ctx->authsize, true, true);
  522. flc->flc[1] = cpu_to_caam32(desc_len(desc)); /* SDL */
  523. dma_sync_single_for_device(dev, ctx->flc_dma[ENCRYPT],
  524. sizeof(flc->flc) + desc_bytes(desc),
  525. ctx->dir);
  526. flc = &ctx->flc[DECRYPT];
  527. desc = flc->sh_desc;
  528. cnstr_shdsc_chachapoly(desc, &ctx->cdata, &ctx->adata, ivsize,
  529. ctx->authsize, false, true);
  530. flc->flc[1] = cpu_to_caam32(desc_len(desc)); /* SDL */
  531. dma_sync_single_for_device(dev, ctx->flc_dma[DECRYPT],
  532. sizeof(flc->flc) + desc_bytes(desc),
  533. ctx->dir);
  534. return 0;
  535. }
  536. static int chachapoly_setauthsize(struct crypto_aead *aead,
  537. unsigned int authsize)
  538. {
  539. struct caam_ctx *ctx = crypto_aead_ctx(aead);
  540. if (authsize != POLY1305_DIGEST_SIZE)
  541. return -EINVAL;
  542. ctx->authsize = authsize;
  543. return chachapoly_set_sh_desc(aead);
  544. }
  545. static int chachapoly_setkey(struct crypto_aead *aead, const u8 *key,
  546. unsigned int keylen)
  547. {
  548. struct caam_ctx *ctx = crypto_aead_ctx(aead);
  549. unsigned int ivsize = crypto_aead_ivsize(aead);
  550. unsigned int saltlen = CHACHAPOLY_IV_SIZE - ivsize;
  551. if (keylen != CHACHA_KEY_SIZE + saltlen) {
  552. crypto_aead_set_flags(aead, CRYPTO_TFM_RES_BAD_KEY_LEN);
  553. return -EINVAL;
  554. }
  555. ctx->cdata.key_virt = key;
  556. ctx->cdata.keylen = keylen - saltlen;
  557. return chachapoly_set_sh_desc(aead);
  558. }
  559. static int gcm_set_sh_desc(struct crypto_aead *aead)
  560. {
  561. struct caam_ctx *ctx = crypto_aead_ctx(aead);
  562. struct device *dev = ctx->dev;
  563. unsigned int ivsize = crypto_aead_ivsize(aead);
  564. struct caam_flc *flc;
  565. u32 *desc;
  566. int rem_bytes = CAAM_DESC_BYTES_MAX - DESC_JOB_IO_LEN -
  567. ctx->cdata.keylen;
  568. if (!ctx->cdata.keylen || !ctx->authsize)
  569. return 0;
  570. /*
  571. * AES GCM encrypt shared descriptor
  572. * Job Descriptor and Shared Descriptor
  573. * must fit into the 64-word Descriptor h/w Buffer
  574. */
  575. if (rem_bytes >= DESC_QI_GCM_ENC_LEN) {
  576. ctx->cdata.key_inline = true;
  577. ctx->cdata.key_virt = ctx->key;
  578. } else {
  579. ctx->cdata.key_inline = false;
  580. ctx->cdata.key_dma = ctx->key_dma;
  581. }
  582. flc = &ctx->flc[ENCRYPT];
  583. desc = flc->sh_desc;
  584. cnstr_shdsc_gcm_encap(desc, &ctx->cdata, ivsize, ctx->authsize, true);
  585. flc->flc[1] = cpu_to_caam32(desc_len(desc)); /* SDL */
  586. dma_sync_single_for_device(dev, ctx->flc_dma[ENCRYPT],
  587. sizeof(flc->flc) + desc_bytes(desc),
  588. ctx->dir);
  589. /*
  590. * Job Descriptor and Shared Descriptors
  591. * must all fit into the 64-word Descriptor h/w Buffer
  592. */
  593. if (rem_bytes >= DESC_QI_GCM_DEC_LEN) {
  594. ctx->cdata.key_inline = true;
  595. ctx->cdata.key_virt = ctx->key;
  596. } else {
  597. ctx->cdata.key_inline = false;
  598. ctx->cdata.key_dma = ctx->key_dma;
  599. }
  600. flc = &ctx->flc[DECRYPT];
  601. desc = flc->sh_desc;
  602. cnstr_shdsc_gcm_decap(desc, &ctx->cdata, ivsize, ctx->authsize, true);
  603. flc->flc[1] = cpu_to_caam32(desc_len(desc)); /* SDL */
  604. dma_sync_single_for_device(dev, ctx->flc_dma[DECRYPT],
  605. sizeof(flc->flc) + desc_bytes(desc),
  606. ctx->dir);
  607. return 0;
  608. }
  609. static int gcm_setauthsize(struct crypto_aead *authenc, unsigned int authsize)
  610. {
  611. struct caam_ctx *ctx = crypto_aead_ctx(authenc);
  612. int err;
  613. err = crypto_gcm_check_authsize(authsize);
  614. if (err)
  615. return err;
  616. ctx->authsize = authsize;
  617. gcm_set_sh_desc(authenc);
  618. return 0;
  619. }
  620. static int gcm_setkey(struct crypto_aead *aead,
  621. const u8 *key, unsigned int keylen)
  622. {
  623. struct caam_ctx *ctx = crypto_aead_ctx(aead);
  624. struct device *dev = ctx->dev;
  625. int ret;
  626. ret = aes_check_keylen(keylen);
  627. if (ret) {
  628. crypto_aead_set_flags(aead, CRYPTO_TFM_RES_BAD_KEY_LEN);
  629. return ret;
  630. }
  631. print_hex_dump_debug("key in @" __stringify(__LINE__)": ",
  632. DUMP_PREFIX_ADDRESS, 16, 4, key, keylen, 1);
  633. memcpy(ctx->key, key, keylen);
  634. dma_sync_single_for_device(dev, ctx->key_dma, keylen, ctx->dir);
  635. ctx->cdata.keylen = keylen;
  636. return gcm_set_sh_desc(aead);
  637. }
  638. static int rfc4106_set_sh_desc(struct crypto_aead *aead)
  639. {
  640. struct caam_ctx *ctx = crypto_aead_ctx(aead);
  641. struct device *dev = ctx->dev;
  642. unsigned int ivsize = crypto_aead_ivsize(aead);
  643. struct caam_flc *flc;
  644. u32 *desc;
  645. int rem_bytes = CAAM_DESC_BYTES_MAX - DESC_JOB_IO_LEN -
  646. ctx->cdata.keylen;
  647. if (!ctx->cdata.keylen || !ctx->authsize)
  648. return 0;
  649. ctx->cdata.key_virt = ctx->key;
  650. /*
  651. * RFC4106 encrypt shared descriptor
  652. * Job Descriptor and Shared Descriptor
  653. * must fit into the 64-word Descriptor h/w Buffer
  654. */
  655. if (rem_bytes >= DESC_QI_RFC4106_ENC_LEN) {
  656. ctx->cdata.key_inline = true;
  657. } else {
  658. ctx->cdata.key_inline = false;
  659. ctx->cdata.key_dma = ctx->key_dma;
  660. }
  661. flc = &ctx->flc[ENCRYPT];
  662. desc = flc->sh_desc;
  663. cnstr_shdsc_rfc4106_encap(desc, &ctx->cdata, ivsize, ctx->authsize,
  664. true);
  665. flc->flc[1] = cpu_to_caam32(desc_len(desc)); /* SDL */
  666. dma_sync_single_for_device(dev, ctx->flc_dma[ENCRYPT],
  667. sizeof(flc->flc) + desc_bytes(desc),
  668. ctx->dir);
  669. /*
  670. * Job Descriptor and Shared Descriptors
  671. * must all fit into the 64-word Descriptor h/w Buffer
  672. */
  673. if (rem_bytes >= DESC_QI_RFC4106_DEC_LEN) {
  674. ctx->cdata.key_inline = true;
  675. } else {
  676. ctx->cdata.key_inline = false;
  677. ctx->cdata.key_dma = ctx->key_dma;
  678. }
  679. flc = &ctx->flc[DECRYPT];
  680. desc = flc->sh_desc;
  681. cnstr_shdsc_rfc4106_decap(desc, &ctx->cdata, ivsize, ctx->authsize,
  682. true);
  683. flc->flc[1] = cpu_to_caam32(desc_len(desc)); /* SDL */
  684. dma_sync_single_for_device(dev, ctx->flc_dma[DECRYPT],
  685. sizeof(flc->flc) + desc_bytes(desc),
  686. ctx->dir);
  687. return 0;
  688. }
  689. static int rfc4106_setauthsize(struct crypto_aead *authenc,
  690. unsigned int authsize)
  691. {
  692. struct caam_ctx *ctx = crypto_aead_ctx(authenc);
  693. int err;
  694. err = crypto_rfc4106_check_authsize(authsize);
  695. if (err)
  696. return err;
  697. ctx->authsize = authsize;
  698. rfc4106_set_sh_desc(authenc);
  699. return 0;
  700. }
  701. static int rfc4106_setkey(struct crypto_aead *aead,
  702. const u8 *key, unsigned int keylen)
  703. {
  704. struct caam_ctx *ctx = crypto_aead_ctx(aead);
  705. struct device *dev = ctx->dev;
  706. int ret;
  707. ret = aes_check_keylen(keylen - 4);
  708. if (ret) {
  709. crypto_aead_set_flags(aead, CRYPTO_TFM_RES_BAD_KEY_LEN);
  710. return ret;
  711. }
  712. print_hex_dump_debug("key in @" __stringify(__LINE__)": ",
  713. DUMP_PREFIX_ADDRESS, 16, 4, key, keylen, 1);
  714. memcpy(ctx->key, key, keylen);
  715. /*
  716. * The last four bytes of the key material are used as the salt value
  717. * in the nonce. Update the AES key length.
  718. */
  719. ctx->cdata.keylen = keylen - 4;
  720. dma_sync_single_for_device(dev, ctx->key_dma, ctx->cdata.keylen,
  721. ctx->dir);
  722. return rfc4106_set_sh_desc(aead);
  723. }
  724. static int rfc4543_set_sh_desc(struct crypto_aead *aead)
  725. {
  726. struct caam_ctx *ctx = crypto_aead_ctx(aead);
  727. struct device *dev = ctx->dev;
  728. unsigned int ivsize = crypto_aead_ivsize(aead);
  729. struct caam_flc *flc;
  730. u32 *desc;
  731. int rem_bytes = CAAM_DESC_BYTES_MAX - DESC_JOB_IO_LEN -
  732. ctx->cdata.keylen;
  733. if (!ctx->cdata.keylen || !ctx->authsize)
  734. return 0;
  735. ctx->cdata.key_virt = ctx->key;
  736. /*
  737. * RFC4543 encrypt shared descriptor
  738. * Job Descriptor and Shared Descriptor
  739. * must fit into the 64-word Descriptor h/w Buffer
  740. */
  741. if (rem_bytes >= DESC_QI_RFC4543_ENC_LEN) {
  742. ctx->cdata.key_inline = true;
  743. } else {
  744. ctx->cdata.key_inline = false;
  745. ctx->cdata.key_dma = ctx->key_dma;
  746. }
  747. flc = &ctx->flc[ENCRYPT];
  748. desc = flc->sh_desc;
  749. cnstr_shdsc_rfc4543_encap(desc, &ctx->cdata, ivsize, ctx->authsize,
  750. true);
  751. flc->flc[1] = cpu_to_caam32(desc_len(desc)); /* SDL */
  752. dma_sync_single_for_device(dev, ctx->flc_dma[ENCRYPT],
  753. sizeof(flc->flc) + desc_bytes(desc),
  754. ctx->dir);
  755. /*
  756. * Job Descriptor and Shared Descriptors
  757. * must all fit into the 64-word Descriptor h/w Buffer
  758. */
  759. if (rem_bytes >= DESC_QI_RFC4543_DEC_LEN) {
  760. ctx->cdata.key_inline = true;
  761. } else {
  762. ctx->cdata.key_inline = false;
  763. ctx->cdata.key_dma = ctx->key_dma;
  764. }
  765. flc = &ctx->flc[DECRYPT];
  766. desc = flc->sh_desc;
  767. cnstr_shdsc_rfc4543_decap(desc, &ctx->cdata, ivsize, ctx->authsize,
  768. true);
  769. flc->flc[1] = cpu_to_caam32(desc_len(desc)); /* SDL */
  770. dma_sync_single_for_device(dev, ctx->flc_dma[DECRYPT],
  771. sizeof(flc->flc) + desc_bytes(desc),
  772. ctx->dir);
  773. return 0;
  774. }
  775. static int rfc4543_setauthsize(struct crypto_aead *authenc,
  776. unsigned int authsize)
  777. {
  778. struct caam_ctx *ctx = crypto_aead_ctx(authenc);
  779. if (authsize != 16)
  780. return -EINVAL;
  781. ctx->authsize = authsize;
  782. rfc4543_set_sh_desc(authenc);
  783. return 0;
  784. }
  785. static int rfc4543_setkey(struct crypto_aead *aead,
  786. const u8 *key, unsigned int keylen)
  787. {
  788. struct caam_ctx *ctx = crypto_aead_ctx(aead);
  789. struct device *dev = ctx->dev;
  790. int ret;
  791. ret = aes_check_keylen(keylen - 4);
  792. if (ret) {
  793. crypto_aead_set_flags(aead, CRYPTO_TFM_RES_BAD_KEY_LEN);
  794. return ret;
  795. }
  796. print_hex_dump_debug("key in @" __stringify(__LINE__)": ",
  797. DUMP_PREFIX_ADDRESS, 16, 4, key, keylen, 1);
  798. memcpy(ctx->key, key, keylen);
  799. /*
  800. * The last four bytes of the key material are used as the salt value
  801. * in the nonce. Update the AES key length.
  802. */
  803. ctx->cdata.keylen = keylen - 4;
  804. dma_sync_single_for_device(dev, ctx->key_dma, ctx->cdata.keylen,
  805. ctx->dir);
  806. return rfc4543_set_sh_desc(aead);
  807. }
  808. static int skcipher_setkey(struct crypto_skcipher *skcipher, const u8 *key,
  809. unsigned int keylen, const u32 ctx1_iv_off)
  810. {
  811. struct caam_ctx *ctx = crypto_skcipher_ctx(skcipher);
  812. struct caam_skcipher_alg *alg =
  813. container_of(crypto_skcipher_alg(skcipher),
  814. struct caam_skcipher_alg, skcipher);
  815. struct device *dev = ctx->dev;
  816. struct caam_flc *flc;
  817. unsigned int ivsize = crypto_skcipher_ivsize(skcipher);
  818. u32 *desc;
  819. const bool is_rfc3686 = alg->caam.rfc3686;
  820. print_hex_dump_debug("key in @" __stringify(__LINE__)": ",
  821. DUMP_PREFIX_ADDRESS, 16, 4, key, keylen, 1);
  822. ctx->cdata.keylen = keylen;
  823. ctx->cdata.key_virt = key;
  824. ctx->cdata.key_inline = true;
  825. /* skcipher_encrypt shared descriptor */
  826. flc = &ctx->flc[ENCRYPT];
  827. desc = flc->sh_desc;
  828. cnstr_shdsc_skcipher_encap(desc, &ctx->cdata, ivsize, is_rfc3686,
  829. ctx1_iv_off);
  830. flc->flc[1] = cpu_to_caam32(desc_len(desc)); /* SDL */
  831. dma_sync_single_for_device(dev, ctx->flc_dma[ENCRYPT],
  832. sizeof(flc->flc) + desc_bytes(desc),
  833. ctx->dir);
  834. /* skcipher_decrypt shared descriptor */
  835. flc = &ctx->flc[DECRYPT];
  836. desc = flc->sh_desc;
  837. cnstr_shdsc_skcipher_decap(desc, &ctx->cdata, ivsize, is_rfc3686,
  838. ctx1_iv_off);
  839. flc->flc[1] = cpu_to_caam32(desc_len(desc)); /* SDL */
  840. dma_sync_single_for_device(dev, ctx->flc_dma[DECRYPT],
  841. sizeof(flc->flc) + desc_bytes(desc),
  842. ctx->dir);
  843. return 0;
  844. }
  845. static int aes_skcipher_setkey(struct crypto_skcipher *skcipher,
  846. const u8 *key, unsigned int keylen)
  847. {
  848. int err;
  849. err = aes_check_keylen(keylen);
  850. if (err) {
  851. crypto_skcipher_set_flags(skcipher,
  852. CRYPTO_TFM_RES_BAD_KEY_LEN);
  853. return err;
  854. }
  855. return skcipher_setkey(skcipher, key, keylen, 0);
  856. }
  857. static int rfc3686_skcipher_setkey(struct crypto_skcipher *skcipher,
  858. const u8 *key, unsigned int keylen)
  859. {
  860. u32 ctx1_iv_off;
  861. int err;
  862. /*
  863. * RFC3686 specific:
  864. * | CONTEXT1[255:128] = {NONCE, IV, COUNTER}
  865. * | *key = {KEY, NONCE}
  866. */
  867. ctx1_iv_off = 16 + CTR_RFC3686_NONCE_SIZE;
  868. keylen -= CTR_RFC3686_NONCE_SIZE;
  869. err = aes_check_keylen(keylen);
  870. if (err) {
  871. crypto_skcipher_set_flags(skcipher,
  872. CRYPTO_TFM_RES_BAD_KEY_LEN);
  873. return err;
  874. }
  875. return skcipher_setkey(skcipher, key, keylen, ctx1_iv_off);
  876. }
  877. static int ctr_skcipher_setkey(struct crypto_skcipher *skcipher,
  878. const u8 *key, unsigned int keylen)
  879. {
  880. u32 ctx1_iv_off;
  881. int err;
  882. /*
  883. * AES-CTR needs to load IV in CONTEXT1 reg
  884. * at an offset of 128bits (16bytes)
  885. * CONTEXT1[255:128] = IV
  886. */
  887. ctx1_iv_off = 16;
  888. err = aes_check_keylen(keylen);
  889. if (err) {
  890. crypto_skcipher_set_flags(skcipher,
  891. CRYPTO_TFM_RES_BAD_KEY_LEN);
  892. return err;
  893. }
  894. return skcipher_setkey(skcipher, key, keylen, ctx1_iv_off);
  895. }
  896. static int chacha20_skcipher_setkey(struct crypto_skcipher *skcipher,
  897. const u8 *key, unsigned int keylen)
  898. {
  899. if (keylen != CHACHA_KEY_SIZE) {
  900. crypto_skcipher_set_flags(skcipher,
  901. CRYPTO_TFM_RES_BAD_KEY_LEN);
  902. return -EINVAL;
  903. }
  904. return skcipher_setkey(skcipher, key, keylen, 0);
  905. }
  906. static int des_skcipher_setkey(struct crypto_skcipher *skcipher,
  907. const u8 *key, unsigned int keylen)
  908. {
  909. return verify_skcipher_des_key(skcipher, key) ?:
  910. skcipher_setkey(skcipher, key, keylen, 0);
  911. }
  912. static int des3_skcipher_setkey(struct crypto_skcipher *skcipher,
  913. const u8 *key, unsigned int keylen)
  914. {
  915. return verify_skcipher_des3_key(skcipher, key) ?:
  916. skcipher_setkey(skcipher, key, keylen, 0);
  917. }
  918. static int xts_skcipher_setkey(struct crypto_skcipher *skcipher, const u8 *key,
  919. unsigned int keylen)
  920. {
  921. struct caam_ctx *ctx = crypto_skcipher_ctx(skcipher);
  922. struct device *dev = ctx->dev;
  923. struct caam_flc *flc;
  924. u32 *desc;
  925. if (keylen != 2 * AES_MIN_KEY_SIZE && keylen != 2 * AES_MAX_KEY_SIZE) {
  926. dev_err(dev, "key size mismatch\n");
  927. crypto_skcipher_set_flags(skcipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
  928. return -EINVAL;
  929. }
  930. ctx->cdata.keylen = keylen;
  931. ctx->cdata.key_virt = key;
  932. ctx->cdata.key_inline = true;
  933. /* xts_skcipher_encrypt shared descriptor */
  934. flc = &ctx->flc[ENCRYPT];
  935. desc = flc->sh_desc;
  936. cnstr_shdsc_xts_skcipher_encap(desc, &ctx->cdata);
  937. flc->flc[1] = cpu_to_caam32(desc_len(desc)); /* SDL */
  938. dma_sync_single_for_device(dev, ctx->flc_dma[ENCRYPT],
  939. sizeof(flc->flc) + desc_bytes(desc),
  940. ctx->dir);
  941. /* xts_skcipher_decrypt shared descriptor */
  942. flc = &ctx->flc[DECRYPT];
  943. desc = flc->sh_desc;
  944. cnstr_shdsc_xts_skcipher_decap(desc, &ctx->cdata);
  945. flc->flc[1] = cpu_to_caam32(desc_len(desc)); /* SDL */
  946. dma_sync_single_for_device(dev, ctx->flc_dma[DECRYPT],
  947. sizeof(flc->flc) + desc_bytes(desc),
  948. ctx->dir);
  949. return 0;
  950. }
  951. static struct skcipher_edesc *skcipher_edesc_alloc(struct skcipher_request *req)
  952. {
  953. struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
  954. struct caam_request *req_ctx = skcipher_request_ctx(req);
  955. struct dpaa2_fl_entry *in_fle = &req_ctx->fd_flt[1];
  956. struct dpaa2_fl_entry *out_fle = &req_ctx->fd_flt[0];
  957. struct caam_ctx *ctx = crypto_skcipher_ctx(skcipher);
  958. struct device *dev = ctx->dev;
  959. gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
  960. GFP_KERNEL : GFP_ATOMIC;
  961. int src_nents, mapped_src_nents, dst_nents = 0, mapped_dst_nents = 0;
  962. struct skcipher_edesc *edesc;
  963. dma_addr_t iv_dma;
  964. u8 *iv;
  965. int ivsize = crypto_skcipher_ivsize(skcipher);
  966. int dst_sg_idx, qm_sg_ents, qm_sg_bytes;
  967. struct dpaa2_sg_entry *sg_table;
  968. src_nents = sg_nents_for_len(req->src, req->cryptlen);
  969. if (unlikely(src_nents < 0)) {
  970. dev_err(dev, "Insufficient bytes (%d) in src S/G\n",
  971. req->cryptlen);
  972. return ERR_PTR(src_nents);
  973. }
  974. if (unlikely(req->dst != req->src)) {
  975. dst_nents = sg_nents_for_len(req->dst, req->cryptlen);
  976. if (unlikely(dst_nents < 0)) {
  977. dev_err(dev, "Insufficient bytes (%d) in dst S/G\n",
  978. req->cryptlen);
  979. return ERR_PTR(dst_nents);
  980. }
  981. mapped_src_nents = dma_map_sg(dev, req->src, src_nents,
  982. DMA_TO_DEVICE);
  983. if (unlikely(!mapped_src_nents)) {
  984. dev_err(dev, "unable to map source\n");
  985. return ERR_PTR(-ENOMEM);
  986. }
  987. mapped_dst_nents = dma_map_sg(dev, req->dst, dst_nents,
  988. DMA_FROM_DEVICE);
  989. if (unlikely(!mapped_dst_nents)) {
  990. dev_err(dev, "unable to map destination\n");
  991. dma_unmap_sg(dev, req->src, src_nents, DMA_TO_DEVICE);
  992. return ERR_PTR(-ENOMEM);
  993. }
  994. } else {
  995. mapped_src_nents = dma_map_sg(dev, req->src, src_nents,
  996. DMA_BIDIRECTIONAL);
  997. if (unlikely(!mapped_src_nents)) {
  998. dev_err(dev, "unable to map source\n");
  999. return ERR_PTR(-ENOMEM);
  1000. }
  1001. }
  1002. qm_sg_ents = 1 + mapped_src_nents;
  1003. dst_sg_idx = qm_sg_ents;
  1004. /*
  1005. * Input, output HW S/G tables: [IV, src][dst, IV]
  1006. * IV entries point to the same buffer
  1007. * If src == dst, S/G entries are reused (S/G tables overlap)
  1008. *
  1009. * HW reads 4 S/G entries at a time; make sure the reads don't go beyond
  1010. * the end of the table by allocating more S/G entries.
  1011. */
  1012. if (req->src != req->dst)
  1013. qm_sg_ents += pad_sg_nents(mapped_dst_nents + 1);
  1014. else
  1015. qm_sg_ents = 1 + pad_sg_nents(qm_sg_ents);
  1016. qm_sg_bytes = qm_sg_ents * sizeof(struct dpaa2_sg_entry);
  1017. if (unlikely(offsetof(struct skcipher_edesc, sgt) + qm_sg_bytes +
  1018. ivsize > CAAM_QI_MEMCACHE_SIZE)) {
  1019. dev_err(dev, "No space for %d S/G entries and/or %dB IV\n",
  1020. qm_sg_ents, ivsize);
  1021. caam_unmap(dev, req->src, req->dst, src_nents, dst_nents, 0,
  1022. 0, DMA_NONE, 0, 0);
  1023. return ERR_PTR(-ENOMEM);
  1024. }
  1025. /* allocate space for base edesc, link tables and IV */
  1026. edesc = qi_cache_zalloc(GFP_DMA | flags);
  1027. if (unlikely(!edesc)) {
  1028. dev_err(dev, "could not allocate extended descriptor\n");
  1029. caam_unmap(dev, req->src, req->dst, src_nents, dst_nents, 0,
  1030. 0, DMA_NONE, 0, 0);
  1031. return ERR_PTR(-ENOMEM);
  1032. }
  1033. /* Make sure IV is located in a DMAable area */
  1034. sg_table = &edesc->sgt[0];
  1035. iv = (u8 *)(sg_table + qm_sg_ents);
  1036. memcpy(iv, req->iv, ivsize);
  1037. iv_dma = dma_map_single(dev, iv, ivsize, DMA_BIDIRECTIONAL);
  1038. if (dma_mapping_error(dev, iv_dma)) {
  1039. dev_err(dev, "unable to map IV\n");
  1040. caam_unmap(dev, req->src, req->dst, src_nents, dst_nents, 0,
  1041. 0, DMA_NONE, 0, 0);
  1042. qi_cache_free(edesc);
  1043. return ERR_PTR(-ENOMEM);
  1044. }
  1045. edesc->src_nents = src_nents;
  1046. edesc->dst_nents = dst_nents;
  1047. edesc->iv_dma = iv_dma;
  1048. edesc->qm_sg_bytes = qm_sg_bytes;
  1049. dma_to_qm_sg_one(sg_table, iv_dma, ivsize, 0);
  1050. sg_to_qm_sg(req->src, req->cryptlen, sg_table + 1, 0);
  1051. if (req->src != req->dst)
  1052. sg_to_qm_sg(req->dst, req->cryptlen, sg_table + dst_sg_idx, 0);
  1053. dma_to_qm_sg_one(sg_table + dst_sg_idx + mapped_dst_nents, iv_dma,
  1054. ivsize, 0);
  1055. edesc->qm_sg_dma = dma_map_single(dev, sg_table, edesc->qm_sg_bytes,
  1056. DMA_TO_DEVICE);
  1057. if (dma_mapping_error(dev, edesc->qm_sg_dma)) {
  1058. dev_err(dev, "unable to map S/G table\n");
  1059. caam_unmap(dev, req->src, req->dst, src_nents, dst_nents,
  1060. iv_dma, ivsize, DMA_BIDIRECTIONAL, 0, 0);
  1061. qi_cache_free(edesc);
  1062. return ERR_PTR(-ENOMEM);
  1063. }
  1064. memset(&req_ctx->fd_flt, 0, sizeof(req_ctx->fd_flt));
  1065. dpaa2_fl_set_final(in_fle, true);
  1066. dpaa2_fl_set_len(in_fle, req->cryptlen + ivsize);
  1067. dpaa2_fl_set_len(out_fle, req->cryptlen + ivsize);
  1068. dpaa2_fl_set_format(in_fle, dpaa2_fl_sg);
  1069. dpaa2_fl_set_addr(in_fle, edesc->qm_sg_dma);
  1070. dpaa2_fl_set_format(out_fle, dpaa2_fl_sg);
  1071. if (req->src == req->dst)
  1072. dpaa2_fl_set_addr(out_fle, edesc->qm_sg_dma +
  1073. sizeof(*sg_table));
  1074. else
  1075. dpaa2_fl_set_addr(out_fle, edesc->qm_sg_dma + dst_sg_idx *
  1076. sizeof(*sg_table));
  1077. return edesc;
  1078. }
  1079. static void aead_unmap(struct device *dev, struct aead_edesc *edesc,
  1080. struct aead_request *req)
  1081. {
  1082. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  1083. int ivsize = crypto_aead_ivsize(aead);
  1084. caam_unmap(dev, req->src, req->dst, edesc->src_nents, edesc->dst_nents,
  1085. edesc->iv_dma, ivsize, DMA_TO_DEVICE, edesc->qm_sg_dma,
  1086. edesc->qm_sg_bytes);
  1087. dma_unmap_single(dev, edesc->assoclen_dma, 4, DMA_TO_DEVICE);
  1088. }
  1089. static void skcipher_unmap(struct device *dev, struct skcipher_edesc *edesc,
  1090. struct skcipher_request *req)
  1091. {
  1092. struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
  1093. int ivsize = crypto_skcipher_ivsize(skcipher);
  1094. caam_unmap(dev, req->src, req->dst, edesc->src_nents, edesc->dst_nents,
  1095. edesc->iv_dma, ivsize, DMA_BIDIRECTIONAL, edesc->qm_sg_dma,
  1096. edesc->qm_sg_bytes);
  1097. }
  1098. static void aead_encrypt_done(void *cbk_ctx, u32 status)
  1099. {
  1100. struct crypto_async_request *areq = cbk_ctx;
  1101. struct aead_request *req = container_of(areq, struct aead_request,
  1102. base);
  1103. struct caam_request *req_ctx = to_caam_req(areq);
  1104. struct aead_edesc *edesc = req_ctx->edesc;
  1105. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  1106. struct caam_ctx *ctx = crypto_aead_ctx(aead);
  1107. int ecode = 0;
  1108. dev_dbg(ctx->dev, "%s %d: err 0x%x\n", __func__, __LINE__, status);
  1109. if (unlikely(status))
  1110. ecode = caam_qi2_strstatus(ctx->dev, status);
  1111. aead_unmap(ctx->dev, edesc, req);
  1112. qi_cache_free(edesc);
  1113. aead_request_complete(req, ecode);
  1114. }
  1115. static void aead_decrypt_done(void *cbk_ctx, u32 status)
  1116. {
  1117. struct crypto_async_request *areq = cbk_ctx;
  1118. struct aead_request *req = container_of(areq, struct aead_request,
  1119. base);
  1120. struct caam_request *req_ctx = to_caam_req(areq);
  1121. struct aead_edesc *edesc = req_ctx->edesc;
  1122. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  1123. struct caam_ctx *ctx = crypto_aead_ctx(aead);
  1124. int ecode = 0;
  1125. dev_dbg(ctx->dev, "%s %d: err 0x%x\n", __func__, __LINE__, status);
  1126. if (unlikely(status))
  1127. ecode = caam_qi2_strstatus(ctx->dev, status);
  1128. aead_unmap(ctx->dev, edesc, req);
  1129. qi_cache_free(edesc);
  1130. aead_request_complete(req, ecode);
  1131. }
  1132. static int aead_encrypt(struct aead_request *req)
  1133. {
  1134. struct aead_edesc *edesc;
  1135. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  1136. struct caam_ctx *ctx = crypto_aead_ctx(aead);
  1137. struct caam_request *caam_req = aead_request_ctx(req);
  1138. int ret;
  1139. /* allocate extended descriptor */
  1140. edesc = aead_edesc_alloc(req, true);
  1141. if (IS_ERR(edesc))
  1142. return PTR_ERR(edesc);
  1143. caam_req->flc = &ctx->flc[ENCRYPT];
  1144. caam_req->flc_dma = ctx->flc_dma[ENCRYPT];
  1145. caam_req->cbk = aead_encrypt_done;
  1146. caam_req->ctx = &req->base;
  1147. caam_req->edesc = edesc;
  1148. ret = dpaa2_caam_enqueue(ctx->dev, caam_req);
  1149. if (ret != -EINPROGRESS &&
  1150. !(ret == -EBUSY && req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG)) {
  1151. aead_unmap(ctx->dev, edesc, req);
  1152. qi_cache_free(edesc);
  1153. }
  1154. return ret;
  1155. }
  1156. static int aead_decrypt(struct aead_request *req)
  1157. {
  1158. struct aead_edesc *edesc;
  1159. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  1160. struct caam_ctx *ctx = crypto_aead_ctx(aead);
  1161. struct caam_request *caam_req = aead_request_ctx(req);
  1162. int ret;
  1163. /* allocate extended descriptor */
  1164. edesc = aead_edesc_alloc(req, false);
  1165. if (IS_ERR(edesc))
  1166. return PTR_ERR(edesc);
  1167. caam_req->flc = &ctx->flc[DECRYPT];
  1168. caam_req->flc_dma = ctx->flc_dma[DECRYPT];
  1169. caam_req->cbk = aead_decrypt_done;
  1170. caam_req->ctx = &req->base;
  1171. caam_req->edesc = edesc;
  1172. ret = dpaa2_caam_enqueue(ctx->dev, caam_req);
  1173. if (ret != -EINPROGRESS &&
  1174. !(ret == -EBUSY && req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG)) {
  1175. aead_unmap(ctx->dev, edesc, req);
  1176. qi_cache_free(edesc);
  1177. }
  1178. return ret;
  1179. }
  1180. static int ipsec_gcm_encrypt(struct aead_request *req)
  1181. {
  1182. return crypto_ipsec_check_assoclen(req->assoclen) ? : aead_encrypt(req);
  1183. }
  1184. static int ipsec_gcm_decrypt(struct aead_request *req)
  1185. {
  1186. return crypto_ipsec_check_assoclen(req->assoclen) ? : aead_decrypt(req);
  1187. }
  1188. static void skcipher_encrypt_done(void *cbk_ctx, u32 status)
  1189. {
  1190. struct crypto_async_request *areq = cbk_ctx;
  1191. struct skcipher_request *req = skcipher_request_cast(areq);
  1192. struct caam_request *req_ctx = to_caam_req(areq);
  1193. struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
  1194. struct caam_ctx *ctx = crypto_skcipher_ctx(skcipher);
  1195. struct skcipher_edesc *edesc = req_ctx->edesc;
  1196. int ecode = 0;
  1197. int ivsize = crypto_skcipher_ivsize(skcipher);
  1198. dev_dbg(ctx->dev, "%s %d: err 0x%x\n", __func__, __LINE__, status);
  1199. if (unlikely(status))
  1200. ecode = caam_qi2_strstatus(ctx->dev, status);
  1201. print_hex_dump_debug("dstiv @" __stringify(__LINE__)": ",
  1202. DUMP_PREFIX_ADDRESS, 16, 4, req->iv,
  1203. edesc->src_nents > 1 ? 100 : ivsize, 1);
  1204. caam_dump_sg("dst @" __stringify(__LINE__)": ",
  1205. DUMP_PREFIX_ADDRESS, 16, 4, req->dst,
  1206. edesc->dst_nents > 1 ? 100 : req->cryptlen, 1);
  1207. skcipher_unmap(ctx->dev, edesc, req);
  1208. /*
  1209. * The crypto API expects us to set the IV (req->iv) to the last
  1210. * ciphertext block (CBC mode) or last counter (CTR mode).
  1211. * This is used e.g. by the CTS mode.
  1212. */
  1213. if (!ecode)
  1214. memcpy(req->iv, (u8 *)&edesc->sgt[0] + edesc->qm_sg_bytes,
  1215. ivsize);
  1216. qi_cache_free(edesc);
  1217. skcipher_request_complete(req, ecode);
  1218. }
  1219. static void skcipher_decrypt_done(void *cbk_ctx, u32 status)
  1220. {
  1221. struct crypto_async_request *areq = cbk_ctx;
  1222. struct skcipher_request *req = skcipher_request_cast(areq);
  1223. struct caam_request *req_ctx = to_caam_req(areq);
  1224. struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
  1225. struct caam_ctx *ctx = crypto_skcipher_ctx(skcipher);
  1226. struct skcipher_edesc *edesc = req_ctx->edesc;
  1227. int ecode = 0;
  1228. int ivsize = crypto_skcipher_ivsize(skcipher);
  1229. dev_dbg(ctx->dev, "%s %d: err 0x%x\n", __func__, __LINE__, status);
  1230. if (unlikely(status))
  1231. ecode = caam_qi2_strstatus(ctx->dev, status);
  1232. print_hex_dump_debug("dstiv @" __stringify(__LINE__)": ",
  1233. DUMP_PREFIX_ADDRESS, 16, 4, req->iv,
  1234. edesc->src_nents > 1 ? 100 : ivsize, 1);
  1235. caam_dump_sg("dst @" __stringify(__LINE__)": ",
  1236. DUMP_PREFIX_ADDRESS, 16, 4, req->dst,
  1237. edesc->dst_nents > 1 ? 100 : req->cryptlen, 1);
  1238. skcipher_unmap(ctx->dev, edesc, req);
  1239. /*
  1240. * The crypto API expects us to set the IV (req->iv) to the last
  1241. * ciphertext block (CBC mode) or last counter (CTR mode).
  1242. * This is used e.g. by the CTS mode.
  1243. */
  1244. if (!ecode)
  1245. memcpy(req->iv, (u8 *)&edesc->sgt[0] + edesc->qm_sg_bytes,
  1246. ivsize);
  1247. qi_cache_free(edesc);
  1248. skcipher_request_complete(req, ecode);
  1249. }
  1250. static int skcipher_encrypt(struct skcipher_request *req)
  1251. {
  1252. struct skcipher_edesc *edesc;
  1253. struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
  1254. struct caam_ctx *ctx = crypto_skcipher_ctx(skcipher);
  1255. struct caam_request *caam_req = skcipher_request_ctx(req);
  1256. int ret;
  1257. if (!req->cryptlen)
  1258. return 0;
  1259. /* allocate extended descriptor */
  1260. edesc = skcipher_edesc_alloc(req);
  1261. if (IS_ERR(edesc))
  1262. return PTR_ERR(edesc);
  1263. caam_req->flc = &ctx->flc[ENCRYPT];
  1264. caam_req->flc_dma = ctx->flc_dma[ENCRYPT];
  1265. caam_req->cbk = skcipher_encrypt_done;
  1266. caam_req->ctx = &req->base;
  1267. caam_req->edesc = edesc;
  1268. ret = dpaa2_caam_enqueue(ctx->dev, caam_req);
  1269. if (ret != -EINPROGRESS &&
  1270. !(ret == -EBUSY && req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG)) {
  1271. skcipher_unmap(ctx->dev, edesc, req);
  1272. qi_cache_free(edesc);
  1273. }
  1274. return ret;
  1275. }
  1276. static int skcipher_decrypt(struct skcipher_request *req)
  1277. {
  1278. struct skcipher_edesc *edesc;
  1279. struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
  1280. struct caam_ctx *ctx = crypto_skcipher_ctx(skcipher);
  1281. struct caam_request *caam_req = skcipher_request_ctx(req);
  1282. int ret;
  1283. if (!req->cryptlen)
  1284. return 0;
  1285. /* allocate extended descriptor */
  1286. edesc = skcipher_edesc_alloc(req);
  1287. if (IS_ERR(edesc))
  1288. return PTR_ERR(edesc);
  1289. caam_req->flc = &ctx->flc[DECRYPT];
  1290. caam_req->flc_dma = ctx->flc_dma[DECRYPT];
  1291. caam_req->cbk = skcipher_decrypt_done;
  1292. caam_req->ctx = &req->base;
  1293. caam_req->edesc = edesc;
  1294. ret = dpaa2_caam_enqueue(ctx->dev, caam_req);
  1295. if (ret != -EINPROGRESS &&
  1296. !(ret == -EBUSY && req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG)) {
  1297. skcipher_unmap(ctx->dev, edesc, req);
  1298. qi_cache_free(edesc);
  1299. }
  1300. return ret;
  1301. }
  1302. static int caam_cra_init(struct caam_ctx *ctx, struct caam_alg_entry *caam,
  1303. bool uses_dkp)
  1304. {
  1305. dma_addr_t dma_addr;
  1306. int i;
  1307. /* copy descriptor header template value */
  1308. ctx->cdata.algtype = OP_TYPE_CLASS1_ALG | caam->class1_alg_type;
  1309. ctx->adata.algtype = OP_TYPE_CLASS2_ALG | caam->class2_alg_type;
  1310. ctx->dev = caam->dev;
  1311. ctx->dir = uses_dkp ? DMA_BIDIRECTIONAL : DMA_TO_DEVICE;
  1312. dma_addr = dma_map_single_attrs(ctx->dev, ctx->flc,
  1313. offsetof(struct caam_ctx, flc_dma),
  1314. ctx->dir, DMA_ATTR_SKIP_CPU_SYNC);
  1315. if (dma_mapping_error(ctx->dev, dma_addr)) {
  1316. dev_err(ctx->dev, "unable to map key, shared descriptors\n");
  1317. return -ENOMEM;
  1318. }
  1319. for (i = 0; i < NUM_OP; i++)
  1320. ctx->flc_dma[i] = dma_addr + i * sizeof(ctx->flc[i]);
  1321. ctx->key_dma = dma_addr + NUM_OP * sizeof(ctx->flc[0]);
  1322. return 0;
  1323. }
  1324. static int caam_cra_init_skcipher(struct crypto_skcipher *tfm)
  1325. {
  1326. struct skcipher_alg *alg = crypto_skcipher_alg(tfm);
  1327. struct caam_skcipher_alg *caam_alg =
  1328. container_of(alg, typeof(*caam_alg), skcipher);
  1329. crypto_skcipher_set_reqsize(tfm, sizeof(struct caam_request));
  1330. return caam_cra_init(crypto_skcipher_ctx(tfm), &caam_alg->caam, false);
  1331. }
  1332. static int caam_cra_init_aead(struct crypto_aead *tfm)
  1333. {
  1334. struct aead_alg *alg = crypto_aead_alg(tfm);
  1335. struct caam_aead_alg *caam_alg = container_of(alg, typeof(*caam_alg),
  1336. aead);
  1337. crypto_aead_set_reqsize(tfm, sizeof(struct caam_request));
  1338. return caam_cra_init(crypto_aead_ctx(tfm), &caam_alg->caam,
  1339. !caam_alg->caam.nodkp);
  1340. }
  1341. static void caam_exit_common(struct caam_ctx *ctx)
  1342. {
  1343. dma_unmap_single_attrs(ctx->dev, ctx->flc_dma[0],
  1344. offsetof(struct caam_ctx, flc_dma), ctx->dir,
  1345. DMA_ATTR_SKIP_CPU_SYNC);
  1346. }
  1347. static void caam_cra_exit(struct crypto_skcipher *tfm)
  1348. {
  1349. caam_exit_common(crypto_skcipher_ctx(tfm));
  1350. }
  1351. static void caam_cra_exit_aead(struct crypto_aead *tfm)
  1352. {
  1353. caam_exit_common(crypto_aead_ctx(tfm));
  1354. }
  1355. static struct caam_skcipher_alg driver_algs[] = {
  1356. {
  1357. .skcipher = {
  1358. .base = {
  1359. .cra_name = "cbc(aes)",
  1360. .cra_driver_name = "cbc-aes-caam-qi2",
  1361. .cra_blocksize = AES_BLOCK_SIZE,
  1362. },
  1363. .setkey = aes_skcipher_setkey,
  1364. .encrypt = skcipher_encrypt,
  1365. .decrypt = skcipher_decrypt,
  1366. .min_keysize = AES_MIN_KEY_SIZE,
  1367. .max_keysize = AES_MAX_KEY_SIZE,
  1368. .ivsize = AES_BLOCK_SIZE,
  1369. },
  1370. .caam.class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
  1371. },
  1372. {
  1373. .skcipher = {
  1374. .base = {
  1375. .cra_name = "cbc(des3_ede)",
  1376. .cra_driver_name = "cbc-3des-caam-qi2",
  1377. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  1378. },
  1379. .setkey = des3_skcipher_setkey,
  1380. .encrypt = skcipher_encrypt,
  1381. .decrypt = skcipher_decrypt,
  1382. .min_keysize = DES3_EDE_KEY_SIZE,
  1383. .max_keysize = DES3_EDE_KEY_SIZE,
  1384. .ivsize = DES3_EDE_BLOCK_SIZE,
  1385. },
  1386. .caam.class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
  1387. },
  1388. {
  1389. .skcipher = {
  1390. .base = {
  1391. .cra_name = "cbc(des)",
  1392. .cra_driver_name = "cbc-des-caam-qi2",
  1393. .cra_blocksize = DES_BLOCK_SIZE,
  1394. },
  1395. .setkey = des_skcipher_setkey,
  1396. .encrypt = skcipher_encrypt,
  1397. .decrypt = skcipher_decrypt,
  1398. .min_keysize = DES_KEY_SIZE,
  1399. .max_keysize = DES_KEY_SIZE,
  1400. .ivsize = DES_BLOCK_SIZE,
  1401. },
  1402. .caam.class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
  1403. },
  1404. {
  1405. .skcipher = {
  1406. .base = {
  1407. .cra_name = "ctr(aes)",
  1408. .cra_driver_name = "ctr-aes-caam-qi2",
  1409. .cra_blocksize = 1,
  1410. },
  1411. .setkey = ctr_skcipher_setkey,
  1412. .encrypt = skcipher_encrypt,
  1413. .decrypt = skcipher_decrypt,
  1414. .min_keysize = AES_MIN_KEY_SIZE,
  1415. .max_keysize = AES_MAX_KEY_SIZE,
  1416. .ivsize = AES_BLOCK_SIZE,
  1417. .chunksize = AES_BLOCK_SIZE,
  1418. },
  1419. .caam.class1_alg_type = OP_ALG_ALGSEL_AES |
  1420. OP_ALG_AAI_CTR_MOD128,
  1421. },
  1422. {
  1423. .skcipher = {
  1424. .base = {
  1425. .cra_name = "rfc3686(ctr(aes))",
  1426. .cra_driver_name = "rfc3686-ctr-aes-caam-qi2",
  1427. .cra_blocksize = 1,
  1428. },
  1429. .setkey = rfc3686_skcipher_setkey,
  1430. .encrypt = skcipher_encrypt,
  1431. .decrypt = skcipher_decrypt,
  1432. .min_keysize = AES_MIN_KEY_SIZE +
  1433. CTR_RFC3686_NONCE_SIZE,
  1434. .max_keysize = AES_MAX_KEY_SIZE +
  1435. CTR_RFC3686_NONCE_SIZE,
  1436. .ivsize = CTR_RFC3686_IV_SIZE,
  1437. .chunksize = AES_BLOCK_SIZE,
  1438. },
  1439. .caam = {
  1440. .class1_alg_type = OP_ALG_ALGSEL_AES |
  1441. OP_ALG_AAI_CTR_MOD128,
  1442. .rfc3686 = true,
  1443. },
  1444. },
  1445. {
  1446. .skcipher = {
  1447. .base = {
  1448. .cra_name = "xts(aes)",
  1449. .cra_driver_name = "xts-aes-caam-qi2",
  1450. .cra_blocksize = AES_BLOCK_SIZE,
  1451. },
  1452. .setkey = xts_skcipher_setkey,
  1453. .encrypt = skcipher_encrypt,
  1454. .decrypt = skcipher_decrypt,
  1455. .min_keysize = 2 * AES_MIN_KEY_SIZE,
  1456. .max_keysize = 2 * AES_MAX_KEY_SIZE,
  1457. .ivsize = AES_BLOCK_SIZE,
  1458. },
  1459. .caam.class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_XTS,
  1460. },
  1461. {
  1462. .skcipher = {
  1463. .base = {
  1464. .cra_name = "chacha20",
  1465. .cra_driver_name = "chacha20-caam-qi2",
  1466. .cra_blocksize = 1,
  1467. },
  1468. .setkey = chacha20_skcipher_setkey,
  1469. .encrypt = skcipher_encrypt,
  1470. .decrypt = skcipher_decrypt,
  1471. .min_keysize = CHACHA_KEY_SIZE,
  1472. .max_keysize = CHACHA_KEY_SIZE,
  1473. .ivsize = CHACHA_IV_SIZE,
  1474. },
  1475. .caam.class1_alg_type = OP_ALG_ALGSEL_CHACHA20,
  1476. },
  1477. };
  1478. static struct caam_aead_alg driver_aeads[] = {
  1479. {
  1480. .aead = {
  1481. .base = {
  1482. .cra_name = "rfc4106(gcm(aes))",
  1483. .cra_driver_name = "rfc4106-gcm-aes-caam-qi2",
  1484. .cra_blocksize = 1,
  1485. },
  1486. .setkey = rfc4106_setkey,
  1487. .setauthsize = rfc4106_setauthsize,
  1488. .encrypt = ipsec_gcm_encrypt,
  1489. .decrypt = ipsec_gcm_decrypt,
  1490. .ivsize = 8,
  1491. .maxauthsize = AES_BLOCK_SIZE,
  1492. },
  1493. .caam = {
  1494. .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_GCM,
  1495. .nodkp = true,
  1496. },
  1497. },
  1498. {
  1499. .aead = {
  1500. .base = {
  1501. .cra_name = "rfc4543(gcm(aes))",
  1502. .cra_driver_name = "rfc4543-gcm-aes-caam-qi2",
  1503. .cra_blocksize = 1,
  1504. },
  1505. .setkey = rfc4543_setkey,
  1506. .setauthsize = rfc4543_setauthsize,
  1507. .encrypt = ipsec_gcm_encrypt,
  1508. .decrypt = ipsec_gcm_decrypt,
  1509. .ivsize = 8,
  1510. .maxauthsize = AES_BLOCK_SIZE,
  1511. },
  1512. .caam = {
  1513. .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_GCM,
  1514. .nodkp = true,
  1515. },
  1516. },
  1517. /* Galois Counter Mode */
  1518. {
  1519. .aead = {
  1520. .base = {
  1521. .cra_name = "gcm(aes)",
  1522. .cra_driver_name = "gcm-aes-caam-qi2",
  1523. .cra_blocksize = 1,
  1524. },
  1525. .setkey = gcm_setkey,
  1526. .setauthsize = gcm_setauthsize,
  1527. .encrypt = aead_encrypt,
  1528. .decrypt = aead_decrypt,
  1529. .ivsize = 12,
  1530. .maxauthsize = AES_BLOCK_SIZE,
  1531. },
  1532. .caam = {
  1533. .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_GCM,
  1534. .nodkp = true,
  1535. }
  1536. },
  1537. /* single-pass ipsec_esp descriptor */
  1538. {
  1539. .aead = {
  1540. .base = {
  1541. .cra_name = "authenc(hmac(md5),cbc(aes))",
  1542. .cra_driver_name = "authenc-hmac-md5-"
  1543. "cbc-aes-caam-qi2",
  1544. .cra_blocksize = AES_BLOCK_SIZE,
  1545. },
  1546. .setkey = aead_setkey,
  1547. .setauthsize = aead_setauthsize,
  1548. .encrypt = aead_encrypt,
  1549. .decrypt = aead_decrypt,
  1550. .ivsize = AES_BLOCK_SIZE,
  1551. .maxauthsize = MD5_DIGEST_SIZE,
  1552. },
  1553. .caam = {
  1554. .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
  1555. .class2_alg_type = OP_ALG_ALGSEL_MD5 |
  1556. OP_ALG_AAI_HMAC_PRECOMP,
  1557. }
  1558. },
  1559. {
  1560. .aead = {
  1561. .base = {
  1562. .cra_name = "echainiv(authenc(hmac(md5),"
  1563. "cbc(aes)))",
  1564. .cra_driver_name = "echainiv-authenc-hmac-md5-"
  1565. "cbc-aes-caam-qi2",
  1566. .cra_blocksize = AES_BLOCK_SIZE,
  1567. },
  1568. .setkey = aead_setkey,
  1569. .setauthsize = aead_setauthsize,
  1570. .encrypt = aead_encrypt,
  1571. .decrypt = aead_decrypt,
  1572. .ivsize = AES_BLOCK_SIZE,
  1573. .maxauthsize = MD5_DIGEST_SIZE,
  1574. },
  1575. .caam = {
  1576. .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
  1577. .class2_alg_type = OP_ALG_ALGSEL_MD5 |
  1578. OP_ALG_AAI_HMAC_PRECOMP,
  1579. .geniv = true,
  1580. }
  1581. },
  1582. {
  1583. .aead = {
  1584. .base = {
  1585. .cra_name = "authenc(hmac(sha1),cbc(aes))",
  1586. .cra_driver_name = "authenc-hmac-sha1-"
  1587. "cbc-aes-caam-qi2",
  1588. .cra_blocksize = AES_BLOCK_SIZE,
  1589. },
  1590. .setkey = aead_setkey,
  1591. .setauthsize = aead_setauthsize,
  1592. .encrypt = aead_encrypt,
  1593. .decrypt = aead_decrypt,
  1594. .ivsize = AES_BLOCK_SIZE,
  1595. .maxauthsize = SHA1_DIGEST_SIZE,
  1596. },
  1597. .caam = {
  1598. .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
  1599. .class2_alg_type = OP_ALG_ALGSEL_SHA1 |
  1600. OP_ALG_AAI_HMAC_PRECOMP,
  1601. }
  1602. },
  1603. {
  1604. .aead = {
  1605. .base = {
  1606. .cra_name = "echainiv(authenc(hmac(sha1),"
  1607. "cbc(aes)))",
  1608. .cra_driver_name = "echainiv-authenc-"
  1609. "hmac-sha1-cbc-aes-caam-qi2",
  1610. .cra_blocksize = AES_BLOCK_SIZE,
  1611. },
  1612. .setkey = aead_setkey,
  1613. .setauthsize = aead_setauthsize,
  1614. .encrypt = aead_encrypt,
  1615. .decrypt = aead_decrypt,
  1616. .ivsize = AES_BLOCK_SIZE,
  1617. .maxauthsize = SHA1_DIGEST_SIZE,
  1618. },
  1619. .caam = {
  1620. .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
  1621. .class2_alg_type = OP_ALG_ALGSEL_SHA1 |
  1622. OP_ALG_AAI_HMAC_PRECOMP,
  1623. .geniv = true,
  1624. },
  1625. },
  1626. {
  1627. .aead = {
  1628. .base = {
  1629. .cra_name = "authenc(hmac(sha224),cbc(aes))",
  1630. .cra_driver_name = "authenc-hmac-sha224-"
  1631. "cbc-aes-caam-qi2",
  1632. .cra_blocksize = AES_BLOCK_SIZE,
  1633. },
  1634. .setkey = aead_setkey,
  1635. .setauthsize = aead_setauthsize,
  1636. .encrypt = aead_encrypt,
  1637. .decrypt = aead_decrypt,
  1638. .ivsize = AES_BLOCK_SIZE,
  1639. .maxauthsize = SHA224_DIGEST_SIZE,
  1640. },
  1641. .caam = {
  1642. .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
  1643. .class2_alg_type = OP_ALG_ALGSEL_SHA224 |
  1644. OP_ALG_AAI_HMAC_PRECOMP,
  1645. }
  1646. },
  1647. {
  1648. .aead = {
  1649. .base = {
  1650. .cra_name = "echainiv(authenc(hmac(sha224),"
  1651. "cbc(aes)))",
  1652. .cra_driver_name = "echainiv-authenc-"
  1653. "hmac-sha224-cbc-aes-caam-qi2",
  1654. .cra_blocksize = AES_BLOCK_SIZE,
  1655. },
  1656. .setkey = aead_setkey,
  1657. .setauthsize = aead_setauthsize,
  1658. .encrypt = aead_encrypt,
  1659. .decrypt = aead_decrypt,
  1660. .ivsize = AES_BLOCK_SIZE,
  1661. .maxauthsize = SHA224_DIGEST_SIZE,
  1662. },
  1663. .caam = {
  1664. .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
  1665. .class2_alg_type = OP_ALG_ALGSEL_SHA224 |
  1666. OP_ALG_AAI_HMAC_PRECOMP,
  1667. .geniv = true,
  1668. }
  1669. },
  1670. {
  1671. .aead = {
  1672. .base = {
  1673. .cra_name = "authenc(hmac(sha256),cbc(aes))",
  1674. .cra_driver_name = "authenc-hmac-sha256-"
  1675. "cbc-aes-caam-qi2",
  1676. .cra_blocksize = AES_BLOCK_SIZE,
  1677. },
  1678. .setkey = aead_setkey,
  1679. .setauthsize = aead_setauthsize,
  1680. .encrypt = aead_encrypt,
  1681. .decrypt = aead_decrypt,
  1682. .ivsize = AES_BLOCK_SIZE,
  1683. .maxauthsize = SHA256_DIGEST_SIZE,
  1684. },
  1685. .caam = {
  1686. .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
  1687. .class2_alg_type = OP_ALG_ALGSEL_SHA256 |
  1688. OP_ALG_AAI_HMAC_PRECOMP,
  1689. }
  1690. },
  1691. {
  1692. .aead = {
  1693. .base = {
  1694. .cra_name = "echainiv(authenc(hmac(sha256),"
  1695. "cbc(aes)))",
  1696. .cra_driver_name = "echainiv-authenc-"
  1697. "hmac-sha256-cbc-aes-"
  1698. "caam-qi2",
  1699. .cra_blocksize = AES_BLOCK_SIZE,
  1700. },
  1701. .setkey = aead_setkey,
  1702. .setauthsize = aead_setauthsize,
  1703. .encrypt = aead_encrypt,
  1704. .decrypt = aead_decrypt,
  1705. .ivsize = AES_BLOCK_SIZE,
  1706. .maxauthsize = SHA256_DIGEST_SIZE,
  1707. },
  1708. .caam = {
  1709. .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
  1710. .class2_alg_type = OP_ALG_ALGSEL_SHA256 |
  1711. OP_ALG_AAI_HMAC_PRECOMP,
  1712. .geniv = true,
  1713. }
  1714. },
  1715. {
  1716. .aead = {
  1717. .base = {
  1718. .cra_name = "authenc(hmac(sha384),cbc(aes))",
  1719. .cra_driver_name = "authenc-hmac-sha384-"
  1720. "cbc-aes-caam-qi2",
  1721. .cra_blocksize = AES_BLOCK_SIZE,
  1722. },
  1723. .setkey = aead_setkey,
  1724. .setauthsize = aead_setauthsize,
  1725. .encrypt = aead_encrypt,
  1726. .decrypt = aead_decrypt,
  1727. .ivsize = AES_BLOCK_SIZE,
  1728. .maxauthsize = SHA384_DIGEST_SIZE,
  1729. },
  1730. .caam = {
  1731. .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
  1732. .class2_alg_type = OP_ALG_ALGSEL_SHA384 |
  1733. OP_ALG_AAI_HMAC_PRECOMP,
  1734. }
  1735. },
  1736. {
  1737. .aead = {
  1738. .base = {
  1739. .cra_name = "echainiv(authenc(hmac(sha384),"
  1740. "cbc(aes)))",
  1741. .cra_driver_name = "echainiv-authenc-"
  1742. "hmac-sha384-cbc-aes-"
  1743. "caam-qi2",
  1744. .cra_blocksize = AES_BLOCK_SIZE,
  1745. },
  1746. .setkey = aead_setkey,
  1747. .setauthsize = aead_setauthsize,
  1748. .encrypt = aead_encrypt,
  1749. .decrypt = aead_decrypt,
  1750. .ivsize = AES_BLOCK_SIZE,
  1751. .maxauthsize = SHA384_DIGEST_SIZE,
  1752. },
  1753. .caam = {
  1754. .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
  1755. .class2_alg_type = OP_ALG_ALGSEL_SHA384 |
  1756. OP_ALG_AAI_HMAC_PRECOMP,
  1757. .geniv = true,
  1758. }
  1759. },
  1760. {
  1761. .aead = {
  1762. .base = {
  1763. .cra_name = "authenc(hmac(sha512),cbc(aes))",
  1764. .cra_driver_name = "authenc-hmac-sha512-"
  1765. "cbc-aes-caam-qi2",
  1766. .cra_blocksize = AES_BLOCK_SIZE,
  1767. },
  1768. .setkey = aead_setkey,
  1769. .setauthsize = aead_setauthsize,
  1770. .encrypt = aead_encrypt,
  1771. .decrypt = aead_decrypt,
  1772. .ivsize = AES_BLOCK_SIZE,
  1773. .maxauthsize = SHA512_DIGEST_SIZE,
  1774. },
  1775. .caam = {
  1776. .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
  1777. .class2_alg_type = OP_ALG_ALGSEL_SHA512 |
  1778. OP_ALG_AAI_HMAC_PRECOMP,
  1779. }
  1780. },
  1781. {
  1782. .aead = {
  1783. .base = {
  1784. .cra_name = "echainiv(authenc(hmac(sha512),"
  1785. "cbc(aes)))",
  1786. .cra_driver_name = "echainiv-authenc-"
  1787. "hmac-sha512-cbc-aes-"
  1788. "caam-qi2",
  1789. .cra_blocksize = AES_BLOCK_SIZE,
  1790. },
  1791. .setkey = aead_setkey,
  1792. .setauthsize = aead_setauthsize,
  1793. .encrypt = aead_encrypt,
  1794. .decrypt = aead_decrypt,
  1795. .ivsize = AES_BLOCK_SIZE,
  1796. .maxauthsize = SHA512_DIGEST_SIZE,
  1797. },
  1798. .caam = {
  1799. .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
  1800. .class2_alg_type = OP_ALG_ALGSEL_SHA512 |
  1801. OP_ALG_AAI_HMAC_PRECOMP,
  1802. .geniv = true,
  1803. }
  1804. },
  1805. {
  1806. .aead = {
  1807. .base = {
  1808. .cra_name = "authenc(hmac(md5),cbc(des3_ede))",
  1809. .cra_driver_name = "authenc-hmac-md5-"
  1810. "cbc-des3_ede-caam-qi2",
  1811. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  1812. },
  1813. .setkey = des3_aead_setkey,
  1814. .setauthsize = aead_setauthsize,
  1815. .encrypt = aead_encrypt,
  1816. .decrypt = aead_decrypt,
  1817. .ivsize = DES3_EDE_BLOCK_SIZE,
  1818. .maxauthsize = MD5_DIGEST_SIZE,
  1819. },
  1820. .caam = {
  1821. .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
  1822. .class2_alg_type = OP_ALG_ALGSEL_MD5 |
  1823. OP_ALG_AAI_HMAC_PRECOMP,
  1824. }
  1825. },
  1826. {
  1827. .aead = {
  1828. .base = {
  1829. .cra_name = "echainiv(authenc(hmac(md5),"
  1830. "cbc(des3_ede)))",
  1831. .cra_driver_name = "echainiv-authenc-hmac-md5-"
  1832. "cbc-des3_ede-caam-qi2",
  1833. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  1834. },
  1835. .setkey = des3_aead_setkey,
  1836. .setauthsize = aead_setauthsize,
  1837. .encrypt = aead_encrypt,
  1838. .decrypt = aead_decrypt,
  1839. .ivsize = DES3_EDE_BLOCK_SIZE,
  1840. .maxauthsize = MD5_DIGEST_SIZE,
  1841. },
  1842. .caam = {
  1843. .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
  1844. .class2_alg_type = OP_ALG_ALGSEL_MD5 |
  1845. OP_ALG_AAI_HMAC_PRECOMP,
  1846. .geniv = true,
  1847. }
  1848. },
  1849. {
  1850. .aead = {
  1851. .base = {
  1852. .cra_name = "authenc(hmac(sha1),"
  1853. "cbc(des3_ede))",
  1854. .cra_driver_name = "authenc-hmac-sha1-"
  1855. "cbc-des3_ede-caam-qi2",
  1856. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  1857. },
  1858. .setkey = des3_aead_setkey,
  1859. .setauthsize = aead_setauthsize,
  1860. .encrypt = aead_encrypt,
  1861. .decrypt = aead_decrypt,
  1862. .ivsize = DES3_EDE_BLOCK_SIZE,
  1863. .maxauthsize = SHA1_DIGEST_SIZE,
  1864. },
  1865. .caam = {
  1866. .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
  1867. .class2_alg_type = OP_ALG_ALGSEL_SHA1 |
  1868. OP_ALG_AAI_HMAC_PRECOMP,
  1869. },
  1870. },
  1871. {
  1872. .aead = {
  1873. .base = {
  1874. .cra_name = "echainiv(authenc(hmac(sha1),"
  1875. "cbc(des3_ede)))",
  1876. .cra_driver_name = "echainiv-authenc-"
  1877. "hmac-sha1-"
  1878. "cbc-des3_ede-caam-qi2",
  1879. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  1880. },
  1881. .setkey = des3_aead_setkey,
  1882. .setauthsize = aead_setauthsize,
  1883. .encrypt = aead_encrypt,
  1884. .decrypt = aead_decrypt,
  1885. .ivsize = DES3_EDE_BLOCK_SIZE,
  1886. .maxauthsize = SHA1_DIGEST_SIZE,
  1887. },
  1888. .caam = {
  1889. .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
  1890. .class2_alg_type = OP_ALG_ALGSEL_SHA1 |
  1891. OP_ALG_AAI_HMAC_PRECOMP,
  1892. .geniv = true,
  1893. }
  1894. },
  1895. {
  1896. .aead = {
  1897. .base = {
  1898. .cra_name = "authenc(hmac(sha224),"
  1899. "cbc(des3_ede))",
  1900. .cra_driver_name = "authenc-hmac-sha224-"
  1901. "cbc-des3_ede-caam-qi2",
  1902. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  1903. },
  1904. .setkey = des3_aead_setkey,
  1905. .setauthsize = aead_setauthsize,
  1906. .encrypt = aead_encrypt,
  1907. .decrypt = aead_decrypt,
  1908. .ivsize = DES3_EDE_BLOCK_SIZE,
  1909. .maxauthsize = SHA224_DIGEST_SIZE,
  1910. },
  1911. .caam = {
  1912. .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
  1913. .class2_alg_type = OP_ALG_ALGSEL_SHA224 |
  1914. OP_ALG_AAI_HMAC_PRECOMP,
  1915. },
  1916. },
  1917. {
  1918. .aead = {
  1919. .base = {
  1920. .cra_name = "echainiv(authenc(hmac(sha224),"
  1921. "cbc(des3_ede)))",
  1922. .cra_driver_name = "echainiv-authenc-"
  1923. "hmac-sha224-"
  1924. "cbc-des3_ede-caam-qi2",
  1925. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  1926. },
  1927. .setkey = des3_aead_setkey,
  1928. .setauthsize = aead_setauthsize,
  1929. .encrypt = aead_encrypt,
  1930. .decrypt = aead_decrypt,
  1931. .ivsize = DES3_EDE_BLOCK_SIZE,
  1932. .maxauthsize = SHA224_DIGEST_SIZE,
  1933. },
  1934. .caam = {
  1935. .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
  1936. .class2_alg_type = OP_ALG_ALGSEL_SHA224 |
  1937. OP_ALG_AAI_HMAC_PRECOMP,
  1938. .geniv = true,
  1939. }
  1940. },
  1941. {
  1942. .aead = {
  1943. .base = {
  1944. .cra_name = "authenc(hmac(sha256),"
  1945. "cbc(des3_ede))",
  1946. .cra_driver_name = "authenc-hmac-sha256-"
  1947. "cbc-des3_ede-caam-qi2",
  1948. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  1949. },
  1950. .setkey = des3_aead_setkey,
  1951. .setauthsize = aead_setauthsize,
  1952. .encrypt = aead_encrypt,
  1953. .decrypt = aead_decrypt,
  1954. .ivsize = DES3_EDE_BLOCK_SIZE,
  1955. .maxauthsize = SHA256_DIGEST_SIZE,
  1956. },
  1957. .caam = {
  1958. .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
  1959. .class2_alg_type = OP_ALG_ALGSEL_SHA256 |
  1960. OP_ALG_AAI_HMAC_PRECOMP,
  1961. },
  1962. },
  1963. {
  1964. .aead = {
  1965. .base = {
  1966. .cra_name = "echainiv(authenc(hmac(sha256),"
  1967. "cbc(des3_ede)))",
  1968. .cra_driver_name = "echainiv-authenc-"
  1969. "hmac-sha256-"
  1970. "cbc-des3_ede-caam-qi2",
  1971. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  1972. },
  1973. .setkey = des3_aead_setkey,
  1974. .setauthsize = aead_setauthsize,
  1975. .encrypt = aead_encrypt,
  1976. .decrypt = aead_decrypt,
  1977. .ivsize = DES3_EDE_BLOCK_SIZE,
  1978. .maxauthsize = SHA256_DIGEST_SIZE,
  1979. },
  1980. .caam = {
  1981. .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
  1982. .class2_alg_type = OP_ALG_ALGSEL_SHA256 |
  1983. OP_ALG_AAI_HMAC_PRECOMP,
  1984. .geniv = true,
  1985. }
  1986. },
  1987. {
  1988. .aead = {
  1989. .base = {
  1990. .cra_name = "authenc(hmac(sha384),"
  1991. "cbc(des3_ede))",
  1992. .cra_driver_name = "authenc-hmac-sha384-"
  1993. "cbc-des3_ede-caam-qi2",
  1994. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  1995. },
  1996. .setkey = des3_aead_setkey,
  1997. .setauthsize = aead_setauthsize,
  1998. .encrypt = aead_encrypt,
  1999. .decrypt = aead_decrypt,
  2000. .ivsize = DES3_EDE_BLOCK_SIZE,
  2001. .maxauthsize = SHA384_DIGEST_SIZE,
  2002. },
  2003. .caam = {
  2004. .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
  2005. .class2_alg_type = OP_ALG_ALGSEL_SHA384 |
  2006. OP_ALG_AAI_HMAC_PRECOMP,
  2007. },
  2008. },
  2009. {
  2010. .aead = {
  2011. .base = {
  2012. .cra_name = "echainiv(authenc(hmac(sha384),"
  2013. "cbc(des3_ede)))",
  2014. .cra_driver_name = "echainiv-authenc-"
  2015. "hmac-sha384-"
  2016. "cbc-des3_ede-caam-qi2",
  2017. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  2018. },
  2019. .setkey = des3_aead_setkey,
  2020. .setauthsize = aead_setauthsize,
  2021. .encrypt = aead_encrypt,
  2022. .decrypt = aead_decrypt,
  2023. .ivsize = DES3_EDE_BLOCK_SIZE,
  2024. .maxauthsize = SHA384_DIGEST_SIZE,
  2025. },
  2026. .caam = {
  2027. .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
  2028. .class2_alg_type = OP_ALG_ALGSEL_SHA384 |
  2029. OP_ALG_AAI_HMAC_PRECOMP,
  2030. .geniv = true,
  2031. }
  2032. },
  2033. {
  2034. .aead = {
  2035. .base = {
  2036. .cra_name = "authenc(hmac(sha512),"
  2037. "cbc(des3_ede))",
  2038. .cra_driver_name = "authenc-hmac-sha512-"
  2039. "cbc-des3_ede-caam-qi2",
  2040. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  2041. },
  2042. .setkey = des3_aead_setkey,
  2043. .setauthsize = aead_setauthsize,
  2044. .encrypt = aead_encrypt,
  2045. .decrypt = aead_decrypt,
  2046. .ivsize = DES3_EDE_BLOCK_SIZE,
  2047. .maxauthsize = SHA512_DIGEST_SIZE,
  2048. },
  2049. .caam = {
  2050. .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
  2051. .class2_alg_type = OP_ALG_ALGSEL_SHA512 |
  2052. OP_ALG_AAI_HMAC_PRECOMP,
  2053. },
  2054. },
  2055. {
  2056. .aead = {
  2057. .base = {
  2058. .cra_name = "echainiv(authenc(hmac(sha512),"
  2059. "cbc(des3_ede)))",
  2060. .cra_driver_name = "echainiv-authenc-"
  2061. "hmac-sha512-"
  2062. "cbc-des3_ede-caam-qi2",
  2063. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  2064. },
  2065. .setkey = des3_aead_setkey,
  2066. .setauthsize = aead_setauthsize,
  2067. .encrypt = aead_encrypt,
  2068. .decrypt = aead_decrypt,
  2069. .ivsize = DES3_EDE_BLOCK_SIZE,
  2070. .maxauthsize = SHA512_DIGEST_SIZE,
  2071. },
  2072. .caam = {
  2073. .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
  2074. .class2_alg_type = OP_ALG_ALGSEL_SHA512 |
  2075. OP_ALG_AAI_HMAC_PRECOMP,
  2076. .geniv = true,
  2077. }
  2078. },
  2079. {
  2080. .aead = {
  2081. .base = {
  2082. .cra_name = "authenc(hmac(md5),cbc(des))",
  2083. .cra_driver_name = "authenc-hmac-md5-"
  2084. "cbc-des-caam-qi2",
  2085. .cra_blocksize = DES_BLOCK_SIZE,
  2086. },
  2087. .setkey = aead_setkey,
  2088. .setauthsize = aead_setauthsize,
  2089. .encrypt = aead_encrypt,
  2090. .decrypt = aead_decrypt,
  2091. .ivsize = DES_BLOCK_SIZE,
  2092. .maxauthsize = MD5_DIGEST_SIZE,
  2093. },
  2094. .caam = {
  2095. .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
  2096. .class2_alg_type = OP_ALG_ALGSEL_MD5 |
  2097. OP_ALG_AAI_HMAC_PRECOMP,
  2098. },
  2099. },
  2100. {
  2101. .aead = {
  2102. .base = {
  2103. .cra_name = "echainiv(authenc(hmac(md5),"
  2104. "cbc(des)))",
  2105. .cra_driver_name = "echainiv-authenc-hmac-md5-"
  2106. "cbc-des-caam-qi2",
  2107. .cra_blocksize = DES_BLOCK_SIZE,
  2108. },
  2109. .setkey = aead_setkey,
  2110. .setauthsize = aead_setauthsize,
  2111. .encrypt = aead_encrypt,
  2112. .decrypt = aead_decrypt,
  2113. .ivsize = DES_BLOCK_SIZE,
  2114. .maxauthsize = MD5_DIGEST_SIZE,
  2115. },
  2116. .caam = {
  2117. .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
  2118. .class2_alg_type = OP_ALG_ALGSEL_MD5 |
  2119. OP_ALG_AAI_HMAC_PRECOMP,
  2120. .geniv = true,
  2121. }
  2122. },
  2123. {
  2124. .aead = {
  2125. .base = {
  2126. .cra_name = "authenc(hmac(sha1),cbc(des))",
  2127. .cra_driver_name = "authenc-hmac-sha1-"
  2128. "cbc-des-caam-qi2",
  2129. .cra_blocksize = DES_BLOCK_SIZE,
  2130. },
  2131. .setkey = aead_setkey,
  2132. .setauthsize = aead_setauthsize,
  2133. .encrypt = aead_encrypt,
  2134. .decrypt = aead_decrypt,
  2135. .ivsize = DES_BLOCK_SIZE,
  2136. .maxauthsize = SHA1_DIGEST_SIZE,
  2137. },
  2138. .caam = {
  2139. .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
  2140. .class2_alg_type = OP_ALG_ALGSEL_SHA1 |
  2141. OP_ALG_AAI_HMAC_PRECOMP,
  2142. },
  2143. },
  2144. {
  2145. .aead = {
  2146. .base = {
  2147. .cra_name = "echainiv(authenc(hmac(sha1),"
  2148. "cbc(des)))",
  2149. .cra_driver_name = "echainiv-authenc-"
  2150. "hmac-sha1-cbc-des-caam-qi2",
  2151. .cra_blocksize = DES_BLOCK_SIZE,
  2152. },
  2153. .setkey = aead_setkey,
  2154. .setauthsize = aead_setauthsize,
  2155. .encrypt = aead_encrypt,
  2156. .decrypt = aead_decrypt,
  2157. .ivsize = DES_BLOCK_SIZE,
  2158. .maxauthsize = SHA1_DIGEST_SIZE,
  2159. },
  2160. .caam = {
  2161. .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
  2162. .class2_alg_type = OP_ALG_ALGSEL_SHA1 |
  2163. OP_ALG_AAI_HMAC_PRECOMP,
  2164. .geniv = true,
  2165. }
  2166. },
  2167. {
  2168. .aead = {
  2169. .base = {
  2170. .cra_name = "authenc(hmac(sha224),cbc(des))",
  2171. .cra_driver_name = "authenc-hmac-sha224-"
  2172. "cbc-des-caam-qi2",
  2173. .cra_blocksize = DES_BLOCK_SIZE,
  2174. },
  2175. .setkey = aead_setkey,
  2176. .setauthsize = aead_setauthsize,
  2177. .encrypt = aead_encrypt,
  2178. .decrypt = aead_decrypt,
  2179. .ivsize = DES_BLOCK_SIZE,
  2180. .maxauthsize = SHA224_DIGEST_SIZE,
  2181. },
  2182. .caam = {
  2183. .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
  2184. .class2_alg_type = OP_ALG_ALGSEL_SHA224 |
  2185. OP_ALG_AAI_HMAC_PRECOMP,
  2186. },
  2187. },
  2188. {
  2189. .aead = {
  2190. .base = {
  2191. .cra_name = "echainiv(authenc(hmac(sha224),"
  2192. "cbc(des)))",
  2193. .cra_driver_name = "echainiv-authenc-"
  2194. "hmac-sha224-cbc-des-"
  2195. "caam-qi2",
  2196. .cra_blocksize = DES_BLOCK_SIZE,
  2197. },
  2198. .setkey = aead_setkey,
  2199. .setauthsize = aead_setauthsize,
  2200. .encrypt = aead_encrypt,
  2201. .decrypt = aead_decrypt,
  2202. .ivsize = DES_BLOCK_SIZE,
  2203. .maxauthsize = SHA224_DIGEST_SIZE,
  2204. },
  2205. .caam = {
  2206. .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
  2207. .class2_alg_type = OP_ALG_ALGSEL_SHA224 |
  2208. OP_ALG_AAI_HMAC_PRECOMP,
  2209. .geniv = true,
  2210. }
  2211. },
  2212. {
  2213. .aead = {
  2214. .base = {
  2215. .cra_name = "authenc(hmac(sha256),cbc(des))",
  2216. .cra_driver_name = "authenc-hmac-sha256-"
  2217. "cbc-des-caam-qi2",
  2218. .cra_blocksize = DES_BLOCK_SIZE,
  2219. },
  2220. .setkey = aead_setkey,
  2221. .setauthsize = aead_setauthsize,
  2222. .encrypt = aead_encrypt,
  2223. .decrypt = aead_decrypt,
  2224. .ivsize = DES_BLOCK_SIZE,
  2225. .maxauthsize = SHA256_DIGEST_SIZE,
  2226. },
  2227. .caam = {
  2228. .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
  2229. .class2_alg_type = OP_ALG_ALGSEL_SHA256 |
  2230. OP_ALG_AAI_HMAC_PRECOMP,
  2231. },
  2232. },
  2233. {
  2234. .aead = {
  2235. .base = {
  2236. .cra_name = "echainiv(authenc(hmac(sha256),"
  2237. "cbc(des)))",
  2238. .cra_driver_name = "echainiv-authenc-"
  2239. "hmac-sha256-cbc-des-"
  2240. "caam-qi2",
  2241. .cra_blocksize = DES_BLOCK_SIZE,
  2242. },
  2243. .setkey = aead_setkey,
  2244. .setauthsize = aead_setauthsize,
  2245. .encrypt = aead_encrypt,
  2246. .decrypt = aead_decrypt,
  2247. .ivsize = DES_BLOCK_SIZE,
  2248. .maxauthsize = SHA256_DIGEST_SIZE,
  2249. },
  2250. .caam = {
  2251. .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
  2252. .class2_alg_type = OP_ALG_ALGSEL_SHA256 |
  2253. OP_ALG_AAI_HMAC_PRECOMP,
  2254. .geniv = true,
  2255. },
  2256. },
  2257. {
  2258. .aead = {
  2259. .base = {
  2260. .cra_name = "authenc(hmac(sha384),cbc(des))",
  2261. .cra_driver_name = "authenc-hmac-sha384-"
  2262. "cbc-des-caam-qi2",
  2263. .cra_blocksize = DES_BLOCK_SIZE,
  2264. },
  2265. .setkey = aead_setkey,
  2266. .setauthsize = aead_setauthsize,
  2267. .encrypt = aead_encrypt,
  2268. .decrypt = aead_decrypt,
  2269. .ivsize = DES_BLOCK_SIZE,
  2270. .maxauthsize = SHA384_DIGEST_SIZE,
  2271. },
  2272. .caam = {
  2273. .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
  2274. .class2_alg_type = OP_ALG_ALGSEL_SHA384 |
  2275. OP_ALG_AAI_HMAC_PRECOMP,
  2276. },
  2277. },
  2278. {
  2279. .aead = {
  2280. .base = {
  2281. .cra_name = "echainiv(authenc(hmac(sha384),"
  2282. "cbc(des)))",
  2283. .cra_driver_name = "echainiv-authenc-"
  2284. "hmac-sha384-cbc-des-"
  2285. "caam-qi2",
  2286. .cra_blocksize = DES_BLOCK_SIZE,
  2287. },
  2288. .setkey = aead_setkey,
  2289. .setauthsize = aead_setauthsize,
  2290. .encrypt = aead_encrypt,
  2291. .decrypt = aead_decrypt,
  2292. .ivsize = DES_BLOCK_SIZE,
  2293. .maxauthsize = SHA384_DIGEST_SIZE,
  2294. },
  2295. .caam = {
  2296. .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
  2297. .class2_alg_type = OP_ALG_ALGSEL_SHA384 |
  2298. OP_ALG_AAI_HMAC_PRECOMP,
  2299. .geniv = true,
  2300. }
  2301. },
  2302. {
  2303. .aead = {
  2304. .base = {
  2305. .cra_name = "authenc(hmac(sha512),cbc(des))",
  2306. .cra_driver_name = "authenc-hmac-sha512-"
  2307. "cbc-des-caam-qi2",
  2308. .cra_blocksize = DES_BLOCK_SIZE,
  2309. },
  2310. .setkey = aead_setkey,
  2311. .setauthsize = aead_setauthsize,
  2312. .encrypt = aead_encrypt,
  2313. .decrypt = aead_decrypt,
  2314. .ivsize = DES_BLOCK_SIZE,
  2315. .maxauthsize = SHA512_DIGEST_SIZE,
  2316. },
  2317. .caam = {
  2318. .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
  2319. .class2_alg_type = OP_ALG_ALGSEL_SHA512 |
  2320. OP_ALG_AAI_HMAC_PRECOMP,
  2321. }
  2322. },
  2323. {
  2324. .aead = {
  2325. .base = {
  2326. .cra_name = "echainiv(authenc(hmac(sha512),"
  2327. "cbc(des)))",
  2328. .cra_driver_name = "echainiv-authenc-"
  2329. "hmac-sha512-cbc-des-"
  2330. "caam-qi2",
  2331. .cra_blocksize = DES_BLOCK_SIZE,
  2332. },
  2333. .setkey = aead_setkey,
  2334. .setauthsize = aead_setauthsize,
  2335. .encrypt = aead_encrypt,
  2336. .decrypt = aead_decrypt,
  2337. .ivsize = DES_BLOCK_SIZE,
  2338. .maxauthsize = SHA512_DIGEST_SIZE,
  2339. },
  2340. .caam = {
  2341. .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
  2342. .class2_alg_type = OP_ALG_ALGSEL_SHA512 |
  2343. OP_ALG_AAI_HMAC_PRECOMP,
  2344. .geniv = true,
  2345. }
  2346. },
  2347. {
  2348. .aead = {
  2349. .base = {
  2350. .cra_name = "authenc(hmac(md5),"
  2351. "rfc3686(ctr(aes)))",
  2352. .cra_driver_name = "authenc-hmac-md5-"
  2353. "rfc3686-ctr-aes-caam-qi2",
  2354. .cra_blocksize = 1,
  2355. },
  2356. .setkey = aead_setkey,
  2357. .setauthsize = aead_setauthsize,
  2358. .encrypt = aead_encrypt,
  2359. .decrypt = aead_decrypt,
  2360. .ivsize = CTR_RFC3686_IV_SIZE,
  2361. .maxauthsize = MD5_DIGEST_SIZE,
  2362. },
  2363. .caam = {
  2364. .class1_alg_type = OP_ALG_ALGSEL_AES |
  2365. OP_ALG_AAI_CTR_MOD128,
  2366. .class2_alg_type = OP_ALG_ALGSEL_MD5 |
  2367. OP_ALG_AAI_HMAC_PRECOMP,
  2368. .rfc3686 = true,
  2369. },
  2370. },
  2371. {
  2372. .aead = {
  2373. .base = {
  2374. .cra_name = "seqiv(authenc("
  2375. "hmac(md5),rfc3686(ctr(aes))))",
  2376. .cra_driver_name = "seqiv-authenc-hmac-md5-"
  2377. "rfc3686-ctr-aes-caam-qi2",
  2378. .cra_blocksize = 1,
  2379. },
  2380. .setkey = aead_setkey,
  2381. .setauthsize = aead_setauthsize,
  2382. .encrypt = aead_encrypt,
  2383. .decrypt = aead_decrypt,
  2384. .ivsize = CTR_RFC3686_IV_SIZE,
  2385. .maxauthsize = MD5_DIGEST_SIZE,
  2386. },
  2387. .caam = {
  2388. .class1_alg_type = OP_ALG_ALGSEL_AES |
  2389. OP_ALG_AAI_CTR_MOD128,
  2390. .class2_alg_type = OP_ALG_ALGSEL_MD5 |
  2391. OP_ALG_AAI_HMAC_PRECOMP,
  2392. .rfc3686 = true,
  2393. .geniv = true,
  2394. },
  2395. },
  2396. {
  2397. .aead = {
  2398. .base = {
  2399. .cra_name = "authenc(hmac(sha1),"
  2400. "rfc3686(ctr(aes)))",
  2401. .cra_driver_name = "authenc-hmac-sha1-"
  2402. "rfc3686-ctr-aes-caam-qi2",
  2403. .cra_blocksize = 1,
  2404. },
  2405. .setkey = aead_setkey,
  2406. .setauthsize = aead_setauthsize,
  2407. .encrypt = aead_encrypt,
  2408. .decrypt = aead_decrypt,
  2409. .ivsize = CTR_RFC3686_IV_SIZE,
  2410. .maxauthsize = SHA1_DIGEST_SIZE,
  2411. },
  2412. .caam = {
  2413. .class1_alg_type = OP_ALG_ALGSEL_AES |
  2414. OP_ALG_AAI_CTR_MOD128,
  2415. .class2_alg_type = OP_ALG_ALGSEL_SHA1 |
  2416. OP_ALG_AAI_HMAC_PRECOMP,
  2417. .rfc3686 = true,
  2418. },
  2419. },
  2420. {
  2421. .aead = {
  2422. .base = {
  2423. .cra_name = "seqiv(authenc("
  2424. "hmac(sha1),rfc3686(ctr(aes))))",
  2425. .cra_driver_name = "seqiv-authenc-hmac-sha1-"
  2426. "rfc3686-ctr-aes-caam-qi2",
  2427. .cra_blocksize = 1,
  2428. },
  2429. .setkey = aead_setkey,
  2430. .setauthsize = aead_setauthsize,
  2431. .encrypt = aead_encrypt,
  2432. .decrypt = aead_decrypt,
  2433. .ivsize = CTR_RFC3686_IV_SIZE,
  2434. .maxauthsize = SHA1_DIGEST_SIZE,
  2435. },
  2436. .caam = {
  2437. .class1_alg_type = OP_ALG_ALGSEL_AES |
  2438. OP_ALG_AAI_CTR_MOD128,
  2439. .class2_alg_type = OP_ALG_ALGSEL_SHA1 |
  2440. OP_ALG_AAI_HMAC_PRECOMP,
  2441. .rfc3686 = true,
  2442. .geniv = true,
  2443. },
  2444. },
  2445. {
  2446. .aead = {
  2447. .base = {
  2448. .cra_name = "authenc(hmac(sha224),"
  2449. "rfc3686(ctr(aes)))",
  2450. .cra_driver_name = "authenc-hmac-sha224-"
  2451. "rfc3686-ctr-aes-caam-qi2",
  2452. .cra_blocksize = 1,
  2453. },
  2454. .setkey = aead_setkey,
  2455. .setauthsize = aead_setauthsize,
  2456. .encrypt = aead_encrypt,
  2457. .decrypt = aead_decrypt,
  2458. .ivsize = CTR_RFC3686_IV_SIZE,
  2459. .maxauthsize = SHA224_DIGEST_SIZE,
  2460. },
  2461. .caam = {
  2462. .class1_alg_type = OP_ALG_ALGSEL_AES |
  2463. OP_ALG_AAI_CTR_MOD128,
  2464. .class2_alg_type = OP_ALG_ALGSEL_SHA224 |
  2465. OP_ALG_AAI_HMAC_PRECOMP,
  2466. .rfc3686 = true,
  2467. },
  2468. },
  2469. {
  2470. .aead = {
  2471. .base = {
  2472. .cra_name = "seqiv(authenc("
  2473. "hmac(sha224),rfc3686(ctr(aes))))",
  2474. .cra_driver_name = "seqiv-authenc-hmac-sha224-"
  2475. "rfc3686-ctr-aes-caam-qi2",
  2476. .cra_blocksize = 1,
  2477. },
  2478. .setkey = aead_setkey,
  2479. .setauthsize = aead_setauthsize,
  2480. .encrypt = aead_encrypt,
  2481. .decrypt = aead_decrypt,
  2482. .ivsize = CTR_RFC3686_IV_SIZE,
  2483. .maxauthsize = SHA224_DIGEST_SIZE,
  2484. },
  2485. .caam = {
  2486. .class1_alg_type = OP_ALG_ALGSEL_AES |
  2487. OP_ALG_AAI_CTR_MOD128,
  2488. .class2_alg_type = OP_ALG_ALGSEL_SHA224 |
  2489. OP_ALG_AAI_HMAC_PRECOMP,
  2490. .rfc3686 = true,
  2491. .geniv = true,
  2492. },
  2493. },
  2494. {
  2495. .aead = {
  2496. .base = {
  2497. .cra_name = "authenc(hmac(sha256),"
  2498. "rfc3686(ctr(aes)))",
  2499. .cra_driver_name = "authenc-hmac-sha256-"
  2500. "rfc3686-ctr-aes-caam-qi2",
  2501. .cra_blocksize = 1,
  2502. },
  2503. .setkey = aead_setkey,
  2504. .setauthsize = aead_setauthsize,
  2505. .encrypt = aead_encrypt,
  2506. .decrypt = aead_decrypt,
  2507. .ivsize = CTR_RFC3686_IV_SIZE,
  2508. .maxauthsize = SHA256_DIGEST_SIZE,
  2509. },
  2510. .caam = {
  2511. .class1_alg_type = OP_ALG_ALGSEL_AES |
  2512. OP_ALG_AAI_CTR_MOD128,
  2513. .class2_alg_type = OP_ALG_ALGSEL_SHA256 |
  2514. OP_ALG_AAI_HMAC_PRECOMP,
  2515. .rfc3686 = true,
  2516. },
  2517. },
  2518. {
  2519. .aead = {
  2520. .base = {
  2521. .cra_name = "seqiv(authenc(hmac(sha256),"
  2522. "rfc3686(ctr(aes))))",
  2523. .cra_driver_name = "seqiv-authenc-hmac-sha256-"
  2524. "rfc3686-ctr-aes-caam-qi2",
  2525. .cra_blocksize = 1,
  2526. },
  2527. .setkey = aead_setkey,
  2528. .setauthsize = aead_setauthsize,
  2529. .encrypt = aead_encrypt,
  2530. .decrypt = aead_decrypt,
  2531. .ivsize = CTR_RFC3686_IV_SIZE,
  2532. .maxauthsize = SHA256_DIGEST_SIZE,
  2533. },
  2534. .caam = {
  2535. .class1_alg_type = OP_ALG_ALGSEL_AES |
  2536. OP_ALG_AAI_CTR_MOD128,
  2537. .class2_alg_type = OP_ALG_ALGSEL_SHA256 |
  2538. OP_ALG_AAI_HMAC_PRECOMP,
  2539. .rfc3686 = true,
  2540. .geniv = true,
  2541. },
  2542. },
  2543. {
  2544. .aead = {
  2545. .base = {
  2546. .cra_name = "authenc(hmac(sha384),"
  2547. "rfc3686(ctr(aes)))",
  2548. .cra_driver_name = "authenc-hmac-sha384-"
  2549. "rfc3686-ctr-aes-caam-qi2",
  2550. .cra_blocksize = 1,
  2551. },
  2552. .setkey = aead_setkey,
  2553. .setauthsize = aead_setauthsize,
  2554. .encrypt = aead_encrypt,
  2555. .decrypt = aead_decrypt,
  2556. .ivsize = CTR_RFC3686_IV_SIZE,
  2557. .maxauthsize = SHA384_DIGEST_SIZE,
  2558. },
  2559. .caam = {
  2560. .class1_alg_type = OP_ALG_ALGSEL_AES |
  2561. OP_ALG_AAI_CTR_MOD128,
  2562. .class2_alg_type = OP_ALG_ALGSEL_SHA384 |
  2563. OP_ALG_AAI_HMAC_PRECOMP,
  2564. .rfc3686 = true,
  2565. },
  2566. },
  2567. {
  2568. .aead = {
  2569. .base = {
  2570. .cra_name = "seqiv(authenc(hmac(sha384),"
  2571. "rfc3686(ctr(aes))))",
  2572. .cra_driver_name = "seqiv-authenc-hmac-sha384-"
  2573. "rfc3686-ctr-aes-caam-qi2",
  2574. .cra_blocksize = 1,
  2575. },
  2576. .setkey = aead_setkey,
  2577. .setauthsize = aead_setauthsize,
  2578. .encrypt = aead_encrypt,
  2579. .decrypt = aead_decrypt,
  2580. .ivsize = CTR_RFC3686_IV_SIZE,
  2581. .maxauthsize = SHA384_DIGEST_SIZE,
  2582. },
  2583. .caam = {
  2584. .class1_alg_type = OP_ALG_ALGSEL_AES |
  2585. OP_ALG_AAI_CTR_MOD128,
  2586. .class2_alg_type = OP_ALG_ALGSEL_SHA384 |
  2587. OP_ALG_AAI_HMAC_PRECOMP,
  2588. .rfc3686 = true,
  2589. .geniv = true,
  2590. },
  2591. },
  2592. {
  2593. .aead = {
  2594. .base = {
  2595. .cra_name = "rfc7539(chacha20,poly1305)",
  2596. .cra_driver_name = "rfc7539-chacha20-poly1305-"
  2597. "caam-qi2",
  2598. .cra_blocksize = 1,
  2599. },
  2600. .setkey = chachapoly_setkey,
  2601. .setauthsize = chachapoly_setauthsize,
  2602. .encrypt = aead_encrypt,
  2603. .decrypt = aead_decrypt,
  2604. .ivsize = CHACHAPOLY_IV_SIZE,
  2605. .maxauthsize = POLY1305_DIGEST_SIZE,
  2606. },
  2607. .caam = {
  2608. .class1_alg_type = OP_ALG_ALGSEL_CHACHA20 |
  2609. OP_ALG_AAI_AEAD,
  2610. .class2_alg_type = OP_ALG_ALGSEL_POLY1305 |
  2611. OP_ALG_AAI_AEAD,
  2612. .nodkp = true,
  2613. },
  2614. },
  2615. {
  2616. .aead = {
  2617. .base = {
  2618. .cra_name = "rfc7539esp(chacha20,poly1305)",
  2619. .cra_driver_name = "rfc7539esp-chacha20-"
  2620. "poly1305-caam-qi2",
  2621. .cra_blocksize = 1,
  2622. },
  2623. .setkey = chachapoly_setkey,
  2624. .setauthsize = chachapoly_setauthsize,
  2625. .encrypt = aead_encrypt,
  2626. .decrypt = aead_decrypt,
  2627. .ivsize = 8,
  2628. .maxauthsize = POLY1305_DIGEST_SIZE,
  2629. },
  2630. .caam = {
  2631. .class1_alg_type = OP_ALG_ALGSEL_CHACHA20 |
  2632. OP_ALG_AAI_AEAD,
  2633. .class2_alg_type = OP_ALG_ALGSEL_POLY1305 |
  2634. OP_ALG_AAI_AEAD,
  2635. .nodkp = true,
  2636. },
  2637. },
  2638. {
  2639. .aead = {
  2640. .base = {
  2641. .cra_name = "authenc(hmac(sha512),"
  2642. "rfc3686(ctr(aes)))",
  2643. .cra_driver_name = "authenc-hmac-sha512-"
  2644. "rfc3686-ctr-aes-caam-qi2",
  2645. .cra_blocksize = 1,
  2646. },
  2647. .setkey = aead_setkey,
  2648. .setauthsize = aead_setauthsize,
  2649. .encrypt = aead_encrypt,
  2650. .decrypt = aead_decrypt,
  2651. .ivsize = CTR_RFC3686_IV_SIZE,
  2652. .maxauthsize = SHA512_DIGEST_SIZE,
  2653. },
  2654. .caam = {
  2655. .class1_alg_type = OP_ALG_ALGSEL_AES |
  2656. OP_ALG_AAI_CTR_MOD128,
  2657. .class2_alg_type = OP_ALG_ALGSEL_SHA512 |
  2658. OP_ALG_AAI_HMAC_PRECOMP,
  2659. .rfc3686 = true,
  2660. },
  2661. },
  2662. {
  2663. .aead = {
  2664. .base = {
  2665. .cra_name = "seqiv(authenc(hmac(sha512),"
  2666. "rfc3686(ctr(aes))))",
  2667. .cra_driver_name = "seqiv-authenc-hmac-sha512-"
  2668. "rfc3686-ctr-aes-caam-qi2",
  2669. .cra_blocksize = 1,
  2670. },
  2671. .setkey = aead_setkey,
  2672. .setauthsize = aead_setauthsize,
  2673. .encrypt = aead_encrypt,
  2674. .decrypt = aead_decrypt,
  2675. .ivsize = CTR_RFC3686_IV_SIZE,
  2676. .maxauthsize = SHA512_DIGEST_SIZE,
  2677. },
  2678. .caam = {
  2679. .class1_alg_type = OP_ALG_ALGSEL_AES |
  2680. OP_ALG_AAI_CTR_MOD128,
  2681. .class2_alg_type = OP_ALG_ALGSEL_SHA512 |
  2682. OP_ALG_AAI_HMAC_PRECOMP,
  2683. .rfc3686 = true,
  2684. .geniv = true,
  2685. },
  2686. },
  2687. };
  2688. static void caam_skcipher_alg_init(struct caam_skcipher_alg *t_alg)
  2689. {
  2690. struct skcipher_alg *alg = &t_alg->skcipher;
  2691. alg->base.cra_module = THIS_MODULE;
  2692. alg->base.cra_priority = CAAM_CRA_PRIORITY;
  2693. alg->base.cra_ctxsize = sizeof(struct caam_ctx);
  2694. alg->base.cra_flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_KERN_DRIVER_ONLY;
  2695. alg->init = caam_cra_init_skcipher;
  2696. alg->exit = caam_cra_exit;
  2697. }
  2698. static void caam_aead_alg_init(struct caam_aead_alg *t_alg)
  2699. {
  2700. struct aead_alg *alg = &t_alg->aead;
  2701. alg->base.cra_module = THIS_MODULE;
  2702. alg->base.cra_priority = CAAM_CRA_PRIORITY;
  2703. alg->base.cra_ctxsize = sizeof(struct caam_ctx);
  2704. alg->base.cra_flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_KERN_DRIVER_ONLY;
  2705. alg->init = caam_cra_init_aead;
  2706. alg->exit = caam_cra_exit_aead;
  2707. }
  2708. /* max hash key is max split key size */
  2709. #define CAAM_MAX_HASH_KEY_SIZE (SHA512_DIGEST_SIZE * 2)
  2710. #define CAAM_MAX_HASH_BLOCK_SIZE SHA512_BLOCK_SIZE
  2711. /* caam context sizes for hashes: running digest + 8 */
  2712. #define HASH_MSG_LEN 8
  2713. #define MAX_CTX_LEN (HASH_MSG_LEN + SHA512_DIGEST_SIZE)
  2714. enum hash_optype {
  2715. UPDATE = 0,
  2716. UPDATE_FIRST,
  2717. FINALIZE,
  2718. DIGEST,
  2719. HASH_NUM_OP
  2720. };
  2721. /**
  2722. * caam_hash_ctx - ahash per-session context
  2723. * @flc: Flow Contexts array
  2724. * @key: authentication key
  2725. * @flc_dma: I/O virtual addresses of the Flow Contexts
  2726. * @dev: dpseci device
  2727. * @ctx_len: size of Context Register
  2728. * @adata: hashing algorithm details
  2729. */
  2730. struct caam_hash_ctx {
  2731. struct caam_flc flc[HASH_NUM_OP];
  2732. u8 key[CAAM_MAX_HASH_BLOCK_SIZE] ____cacheline_aligned;
  2733. dma_addr_t flc_dma[HASH_NUM_OP];
  2734. struct device *dev;
  2735. int ctx_len;
  2736. struct alginfo adata;
  2737. };
  2738. /* ahash state */
  2739. struct caam_hash_state {
  2740. struct caam_request caam_req;
  2741. dma_addr_t buf_dma;
  2742. dma_addr_t ctx_dma;
  2743. int ctx_dma_len;
  2744. u8 buf_0[CAAM_MAX_HASH_BLOCK_SIZE] ____cacheline_aligned;
  2745. int buflen_0;
  2746. u8 buf_1[CAAM_MAX_HASH_BLOCK_SIZE] ____cacheline_aligned;
  2747. int buflen_1;
  2748. u8 caam_ctx[MAX_CTX_LEN] ____cacheline_aligned;
  2749. int (*update)(struct ahash_request *req);
  2750. int (*final)(struct ahash_request *req);
  2751. int (*finup)(struct ahash_request *req);
  2752. int current_buf;
  2753. };
  2754. struct caam_export_state {
  2755. u8 buf[CAAM_MAX_HASH_BLOCK_SIZE];
  2756. u8 caam_ctx[MAX_CTX_LEN];
  2757. int buflen;
  2758. int (*update)(struct ahash_request *req);
  2759. int (*final)(struct ahash_request *req);
  2760. int (*finup)(struct ahash_request *req);
  2761. };
  2762. static inline void switch_buf(struct caam_hash_state *state)
  2763. {
  2764. state->current_buf ^= 1;
  2765. }
  2766. static inline u8 *current_buf(struct caam_hash_state *state)
  2767. {
  2768. return state->current_buf ? state->buf_1 : state->buf_0;
  2769. }
  2770. static inline u8 *alt_buf(struct caam_hash_state *state)
  2771. {
  2772. return state->current_buf ? state->buf_0 : state->buf_1;
  2773. }
  2774. static inline int *current_buflen(struct caam_hash_state *state)
  2775. {
  2776. return state->current_buf ? &state->buflen_1 : &state->buflen_0;
  2777. }
  2778. static inline int *alt_buflen(struct caam_hash_state *state)
  2779. {
  2780. return state->current_buf ? &state->buflen_0 : &state->buflen_1;
  2781. }
  2782. /* Map current buffer in state (if length > 0) and put it in link table */
  2783. static inline int buf_map_to_qm_sg(struct device *dev,
  2784. struct dpaa2_sg_entry *qm_sg,
  2785. struct caam_hash_state *state)
  2786. {
  2787. int buflen = *current_buflen(state);
  2788. if (!buflen)
  2789. return 0;
  2790. state->buf_dma = dma_map_single(dev, current_buf(state), buflen,
  2791. DMA_TO_DEVICE);
  2792. if (dma_mapping_error(dev, state->buf_dma)) {
  2793. dev_err(dev, "unable to map buf\n");
  2794. state->buf_dma = 0;
  2795. return -ENOMEM;
  2796. }
  2797. dma_to_qm_sg_one(qm_sg, state->buf_dma, buflen, 0);
  2798. return 0;
  2799. }
  2800. /* Map state->caam_ctx, and add it to link table */
  2801. static inline int ctx_map_to_qm_sg(struct device *dev,
  2802. struct caam_hash_state *state, int ctx_len,
  2803. struct dpaa2_sg_entry *qm_sg, u32 flag)
  2804. {
  2805. state->ctx_dma_len = ctx_len;
  2806. state->ctx_dma = dma_map_single(dev, state->caam_ctx, ctx_len, flag);
  2807. if (dma_mapping_error(dev, state->ctx_dma)) {
  2808. dev_err(dev, "unable to map ctx\n");
  2809. state->ctx_dma = 0;
  2810. return -ENOMEM;
  2811. }
  2812. dma_to_qm_sg_one(qm_sg, state->ctx_dma, ctx_len, 0);
  2813. return 0;
  2814. }
  2815. static int ahash_set_sh_desc(struct crypto_ahash *ahash)
  2816. {
  2817. struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
  2818. int digestsize = crypto_ahash_digestsize(ahash);
  2819. struct dpaa2_caam_priv *priv = dev_get_drvdata(ctx->dev);
  2820. struct caam_flc *flc;
  2821. u32 *desc;
  2822. /* ahash_update shared descriptor */
  2823. flc = &ctx->flc[UPDATE];
  2824. desc = flc->sh_desc;
  2825. cnstr_shdsc_ahash(desc, &ctx->adata, OP_ALG_AS_UPDATE, ctx->ctx_len,
  2826. ctx->ctx_len, true, priv->sec_attr.era);
  2827. flc->flc[1] = cpu_to_caam32(desc_len(desc)); /* SDL */
  2828. dma_sync_single_for_device(ctx->dev, ctx->flc_dma[UPDATE],
  2829. desc_bytes(desc), DMA_BIDIRECTIONAL);
  2830. print_hex_dump_debug("ahash update shdesc@" __stringify(__LINE__)": ",
  2831. DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc),
  2832. 1);
  2833. /* ahash_update_first shared descriptor */
  2834. flc = &ctx->flc[UPDATE_FIRST];
  2835. desc = flc->sh_desc;
  2836. cnstr_shdsc_ahash(desc, &ctx->adata, OP_ALG_AS_INIT, ctx->ctx_len,
  2837. ctx->ctx_len, false, priv->sec_attr.era);
  2838. flc->flc[1] = cpu_to_caam32(desc_len(desc)); /* SDL */
  2839. dma_sync_single_for_device(ctx->dev, ctx->flc_dma[UPDATE_FIRST],
  2840. desc_bytes(desc), DMA_BIDIRECTIONAL);
  2841. print_hex_dump_debug("ahash update first shdesc@" __stringify(__LINE__)": ",
  2842. DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc),
  2843. 1);
  2844. /* ahash_final shared descriptor */
  2845. flc = &ctx->flc[FINALIZE];
  2846. desc = flc->sh_desc;
  2847. cnstr_shdsc_ahash(desc, &ctx->adata, OP_ALG_AS_FINALIZE, digestsize,
  2848. ctx->ctx_len, true, priv->sec_attr.era);
  2849. flc->flc[1] = cpu_to_caam32(desc_len(desc)); /* SDL */
  2850. dma_sync_single_for_device(ctx->dev, ctx->flc_dma[FINALIZE],
  2851. desc_bytes(desc), DMA_BIDIRECTIONAL);
  2852. print_hex_dump_debug("ahash final shdesc@" __stringify(__LINE__)": ",
  2853. DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc),
  2854. 1);
  2855. /* ahash_digest shared descriptor */
  2856. flc = &ctx->flc[DIGEST];
  2857. desc = flc->sh_desc;
  2858. cnstr_shdsc_ahash(desc, &ctx->adata, OP_ALG_AS_INITFINAL, digestsize,
  2859. ctx->ctx_len, false, priv->sec_attr.era);
  2860. flc->flc[1] = cpu_to_caam32(desc_len(desc)); /* SDL */
  2861. dma_sync_single_for_device(ctx->dev, ctx->flc_dma[DIGEST],
  2862. desc_bytes(desc), DMA_BIDIRECTIONAL);
  2863. print_hex_dump_debug("ahash digest shdesc@" __stringify(__LINE__)": ",
  2864. DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc),
  2865. 1);
  2866. return 0;
  2867. }
  2868. struct split_key_sh_result {
  2869. struct completion completion;
  2870. int err;
  2871. struct device *dev;
  2872. };
  2873. static void split_key_sh_done(void *cbk_ctx, u32 err)
  2874. {
  2875. struct split_key_sh_result *res = cbk_ctx;
  2876. dev_dbg(res->dev, "%s %d: err 0x%x\n", __func__, __LINE__, err);
  2877. res->err = err ? caam_qi2_strstatus(res->dev, err) : 0;
  2878. complete(&res->completion);
  2879. }
  2880. /* Digest hash size if it is too large */
  2881. static int hash_digest_key(struct caam_hash_ctx *ctx, u32 *keylen, u8 *key,
  2882. u32 digestsize)
  2883. {
  2884. struct caam_request *req_ctx;
  2885. u32 *desc;
  2886. struct split_key_sh_result result;
  2887. dma_addr_t key_dma;
  2888. struct caam_flc *flc;
  2889. dma_addr_t flc_dma;
  2890. int ret = -ENOMEM;
  2891. struct dpaa2_fl_entry *in_fle, *out_fle;
  2892. req_ctx = kzalloc(sizeof(*req_ctx), GFP_KERNEL | GFP_DMA);
  2893. if (!req_ctx)
  2894. return -ENOMEM;
  2895. in_fle = &req_ctx->fd_flt[1];
  2896. out_fle = &req_ctx->fd_flt[0];
  2897. flc = kzalloc(sizeof(*flc), GFP_KERNEL | GFP_DMA);
  2898. if (!flc)
  2899. goto err_flc;
  2900. key_dma = dma_map_single(ctx->dev, key, *keylen, DMA_BIDIRECTIONAL);
  2901. if (dma_mapping_error(ctx->dev, key_dma)) {
  2902. dev_err(ctx->dev, "unable to map key memory\n");
  2903. goto err_key_dma;
  2904. }
  2905. desc = flc->sh_desc;
  2906. init_sh_desc(desc, 0);
  2907. /* descriptor to perform unkeyed hash on key_in */
  2908. append_operation(desc, ctx->adata.algtype | OP_ALG_ENCRYPT |
  2909. OP_ALG_AS_INITFINAL);
  2910. append_seq_fifo_load(desc, *keylen, FIFOLD_CLASS_CLASS2 |
  2911. FIFOLD_TYPE_LAST2 | FIFOLD_TYPE_MSG);
  2912. append_seq_store(desc, digestsize, LDST_CLASS_2_CCB |
  2913. LDST_SRCDST_BYTE_CONTEXT);
  2914. flc->flc[1] = cpu_to_caam32(desc_len(desc)); /* SDL */
  2915. flc_dma = dma_map_single(ctx->dev, flc, sizeof(flc->flc) +
  2916. desc_bytes(desc), DMA_TO_DEVICE);
  2917. if (dma_mapping_error(ctx->dev, flc_dma)) {
  2918. dev_err(ctx->dev, "unable to map shared descriptor\n");
  2919. goto err_flc_dma;
  2920. }
  2921. dpaa2_fl_set_final(in_fle, true);
  2922. dpaa2_fl_set_format(in_fle, dpaa2_fl_single);
  2923. dpaa2_fl_set_addr(in_fle, key_dma);
  2924. dpaa2_fl_set_len(in_fle, *keylen);
  2925. dpaa2_fl_set_format(out_fle, dpaa2_fl_single);
  2926. dpaa2_fl_set_addr(out_fle, key_dma);
  2927. dpaa2_fl_set_len(out_fle, digestsize);
  2928. print_hex_dump_debug("key_in@" __stringify(__LINE__)": ",
  2929. DUMP_PREFIX_ADDRESS, 16, 4, key, *keylen, 1);
  2930. print_hex_dump_debug("shdesc@" __stringify(__LINE__)": ",
  2931. DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc),
  2932. 1);
  2933. result.err = 0;
  2934. init_completion(&result.completion);
  2935. result.dev = ctx->dev;
  2936. req_ctx->flc = flc;
  2937. req_ctx->flc_dma = flc_dma;
  2938. req_ctx->cbk = split_key_sh_done;
  2939. req_ctx->ctx = &result;
  2940. ret = dpaa2_caam_enqueue(ctx->dev, req_ctx);
  2941. if (ret == -EINPROGRESS) {
  2942. /* in progress */
  2943. wait_for_completion(&result.completion);
  2944. ret = result.err;
  2945. print_hex_dump_debug("digested key@" __stringify(__LINE__)": ",
  2946. DUMP_PREFIX_ADDRESS, 16, 4, key,
  2947. digestsize, 1);
  2948. }
  2949. dma_unmap_single(ctx->dev, flc_dma, sizeof(flc->flc) + desc_bytes(desc),
  2950. DMA_TO_DEVICE);
  2951. err_flc_dma:
  2952. dma_unmap_single(ctx->dev, key_dma, *keylen, DMA_BIDIRECTIONAL);
  2953. err_key_dma:
  2954. kfree(flc);
  2955. err_flc:
  2956. kfree(req_ctx);
  2957. *keylen = digestsize;
  2958. return ret;
  2959. }
  2960. static int ahash_setkey(struct crypto_ahash *ahash, const u8 *key,
  2961. unsigned int keylen)
  2962. {
  2963. struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
  2964. unsigned int blocksize = crypto_tfm_alg_blocksize(&ahash->base);
  2965. unsigned int digestsize = crypto_ahash_digestsize(ahash);
  2966. int ret;
  2967. u8 *hashed_key = NULL;
  2968. dev_dbg(ctx->dev, "keylen %d blocksize %d\n", keylen, blocksize);
  2969. if (keylen > blocksize) {
  2970. hashed_key = kmemdup(key, keylen, GFP_KERNEL | GFP_DMA);
  2971. if (!hashed_key)
  2972. return -ENOMEM;
  2973. ret = hash_digest_key(ctx, &keylen, hashed_key, digestsize);
  2974. if (ret)
  2975. goto bad_free_key;
  2976. key = hashed_key;
  2977. }
  2978. ctx->adata.keylen = keylen;
  2979. ctx->adata.keylen_pad = split_key_len(ctx->adata.algtype &
  2980. OP_ALG_ALGSEL_MASK);
  2981. if (ctx->adata.keylen_pad > CAAM_MAX_HASH_KEY_SIZE)
  2982. goto bad_free_key;
  2983. ctx->adata.key_virt = key;
  2984. ctx->adata.key_inline = true;
  2985. /*
  2986. * In case |user key| > |derived key|, using DKP<imm,imm> would result
  2987. * in invalid opcodes (last bytes of user key) in the resulting
  2988. * descriptor. Use DKP<ptr,imm> instead => both virtual and dma key
  2989. * addresses are needed.
  2990. */
  2991. if (keylen > ctx->adata.keylen_pad) {
  2992. memcpy(ctx->key, key, keylen);
  2993. dma_sync_single_for_device(ctx->dev, ctx->adata.key_dma,
  2994. ctx->adata.keylen_pad,
  2995. DMA_TO_DEVICE);
  2996. }
  2997. ret = ahash_set_sh_desc(ahash);
  2998. kfree(hashed_key);
  2999. return ret;
  3000. bad_free_key:
  3001. kfree(hashed_key);
  3002. crypto_ahash_set_flags(ahash, CRYPTO_TFM_RES_BAD_KEY_LEN);
  3003. return -EINVAL;
  3004. }
  3005. static inline void ahash_unmap(struct device *dev, struct ahash_edesc *edesc,
  3006. struct ahash_request *req)
  3007. {
  3008. struct caam_hash_state *state = ahash_request_ctx(req);
  3009. if (edesc->src_nents)
  3010. dma_unmap_sg(dev, req->src, edesc->src_nents, DMA_TO_DEVICE);
  3011. if (edesc->qm_sg_bytes)
  3012. dma_unmap_single(dev, edesc->qm_sg_dma, edesc->qm_sg_bytes,
  3013. DMA_TO_DEVICE);
  3014. if (state->buf_dma) {
  3015. dma_unmap_single(dev, state->buf_dma, *current_buflen(state),
  3016. DMA_TO_DEVICE);
  3017. state->buf_dma = 0;
  3018. }
  3019. }
  3020. static inline void ahash_unmap_ctx(struct device *dev,
  3021. struct ahash_edesc *edesc,
  3022. struct ahash_request *req, u32 flag)
  3023. {
  3024. struct caam_hash_state *state = ahash_request_ctx(req);
  3025. if (state->ctx_dma) {
  3026. dma_unmap_single(dev, state->ctx_dma, state->ctx_dma_len, flag);
  3027. state->ctx_dma = 0;
  3028. }
  3029. ahash_unmap(dev, edesc, req);
  3030. }
  3031. static void ahash_done(void *cbk_ctx, u32 status)
  3032. {
  3033. struct crypto_async_request *areq = cbk_ctx;
  3034. struct ahash_request *req = ahash_request_cast(areq);
  3035. struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
  3036. struct caam_hash_state *state = ahash_request_ctx(req);
  3037. struct ahash_edesc *edesc = state->caam_req.edesc;
  3038. struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
  3039. int digestsize = crypto_ahash_digestsize(ahash);
  3040. int ecode = 0;
  3041. dev_dbg(ctx->dev, "%s %d: err 0x%x\n", __func__, __LINE__, status);
  3042. if (unlikely(status))
  3043. ecode = caam_qi2_strstatus(ctx->dev, status);
  3044. ahash_unmap_ctx(ctx->dev, edesc, req, DMA_FROM_DEVICE);
  3045. memcpy(req->result, state->caam_ctx, digestsize);
  3046. qi_cache_free(edesc);
  3047. print_hex_dump_debug("ctx@" __stringify(__LINE__)": ",
  3048. DUMP_PREFIX_ADDRESS, 16, 4, state->caam_ctx,
  3049. ctx->ctx_len, 1);
  3050. req->base.complete(&req->base, ecode);
  3051. }
  3052. static void ahash_done_bi(void *cbk_ctx, u32 status)
  3053. {
  3054. struct crypto_async_request *areq = cbk_ctx;
  3055. struct ahash_request *req = ahash_request_cast(areq);
  3056. struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
  3057. struct caam_hash_state *state = ahash_request_ctx(req);
  3058. struct ahash_edesc *edesc = state->caam_req.edesc;
  3059. struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
  3060. int ecode = 0;
  3061. dev_dbg(ctx->dev, "%s %d: err 0x%x\n", __func__, __LINE__, status);
  3062. if (unlikely(status))
  3063. ecode = caam_qi2_strstatus(ctx->dev, status);
  3064. ahash_unmap_ctx(ctx->dev, edesc, req, DMA_BIDIRECTIONAL);
  3065. switch_buf(state);
  3066. qi_cache_free(edesc);
  3067. print_hex_dump_debug("ctx@" __stringify(__LINE__)": ",
  3068. DUMP_PREFIX_ADDRESS, 16, 4, state->caam_ctx,
  3069. ctx->ctx_len, 1);
  3070. if (req->result)
  3071. print_hex_dump_debug("result@" __stringify(__LINE__)": ",
  3072. DUMP_PREFIX_ADDRESS, 16, 4, req->result,
  3073. crypto_ahash_digestsize(ahash), 1);
  3074. req->base.complete(&req->base, ecode);
  3075. }
  3076. static void ahash_done_ctx_src(void *cbk_ctx, u32 status)
  3077. {
  3078. struct crypto_async_request *areq = cbk_ctx;
  3079. struct ahash_request *req = ahash_request_cast(areq);
  3080. struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
  3081. struct caam_hash_state *state = ahash_request_ctx(req);
  3082. struct ahash_edesc *edesc = state->caam_req.edesc;
  3083. struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
  3084. int digestsize = crypto_ahash_digestsize(ahash);
  3085. int ecode = 0;
  3086. dev_dbg(ctx->dev, "%s %d: err 0x%x\n", __func__, __LINE__, status);
  3087. if (unlikely(status))
  3088. ecode = caam_qi2_strstatus(ctx->dev, status);
  3089. ahash_unmap_ctx(ctx->dev, edesc, req, DMA_BIDIRECTIONAL);
  3090. memcpy(req->result, state->caam_ctx, digestsize);
  3091. qi_cache_free(edesc);
  3092. print_hex_dump_debug("ctx@" __stringify(__LINE__)": ",
  3093. DUMP_PREFIX_ADDRESS, 16, 4, state->caam_ctx,
  3094. ctx->ctx_len, 1);
  3095. req->base.complete(&req->base, ecode);
  3096. }
  3097. static void ahash_done_ctx_dst(void *cbk_ctx, u32 status)
  3098. {
  3099. struct crypto_async_request *areq = cbk_ctx;
  3100. struct ahash_request *req = ahash_request_cast(areq);
  3101. struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
  3102. struct caam_hash_state *state = ahash_request_ctx(req);
  3103. struct ahash_edesc *edesc = state->caam_req.edesc;
  3104. struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
  3105. int ecode = 0;
  3106. dev_dbg(ctx->dev, "%s %d: err 0x%x\n", __func__, __LINE__, status);
  3107. if (unlikely(status))
  3108. ecode = caam_qi2_strstatus(ctx->dev, status);
  3109. ahash_unmap_ctx(ctx->dev, edesc, req, DMA_FROM_DEVICE);
  3110. switch_buf(state);
  3111. qi_cache_free(edesc);
  3112. print_hex_dump_debug("ctx@" __stringify(__LINE__)": ",
  3113. DUMP_PREFIX_ADDRESS, 16, 4, state->caam_ctx,
  3114. ctx->ctx_len, 1);
  3115. if (req->result)
  3116. print_hex_dump_debug("result@" __stringify(__LINE__)": ",
  3117. DUMP_PREFIX_ADDRESS, 16, 4, req->result,
  3118. crypto_ahash_digestsize(ahash), 1);
  3119. req->base.complete(&req->base, ecode);
  3120. }
  3121. static int ahash_update_ctx(struct ahash_request *req)
  3122. {
  3123. struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
  3124. struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
  3125. struct caam_hash_state *state = ahash_request_ctx(req);
  3126. struct caam_request *req_ctx = &state->caam_req;
  3127. struct dpaa2_fl_entry *in_fle = &req_ctx->fd_flt[1];
  3128. struct dpaa2_fl_entry *out_fle = &req_ctx->fd_flt[0];
  3129. gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
  3130. GFP_KERNEL : GFP_ATOMIC;
  3131. u8 *buf = current_buf(state);
  3132. int *buflen = current_buflen(state);
  3133. u8 *next_buf = alt_buf(state);
  3134. int *next_buflen = alt_buflen(state), last_buflen;
  3135. int in_len = *buflen + req->nbytes, to_hash;
  3136. int src_nents, mapped_nents, qm_sg_bytes, qm_sg_src_index;
  3137. struct ahash_edesc *edesc;
  3138. int ret = 0;
  3139. last_buflen = *next_buflen;
  3140. *next_buflen = in_len & (crypto_tfm_alg_blocksize(&ahash->base) - 1);
  3141. to_hash = in_len - *next_buflen;
  3142. if (to_hash) {
  3143. struct dpaa2_sg_entry *sg_table;
  3144. int src_len = req->nbytes - *next_buflen;
  3145. src_nents = sg_nents_for_len(req->src, src_len);
  3146. if (src_nents < 0) {
  3147. dev_err(ctx->dev, "Invalid number of src SG.\n");
  3148. return src_nents;
  3149. }
  3150. if (src_nents) {
  3151. mapped_nents = dma_map_sg(ctx->dev, req->src, src_nents,
  3152. DMA_TO_DEVICE);
  3153. if (!mapped_nents) {
  3154. dev_err(ctx->dev, "unable to DMA map source\n");
  3155. return -ENOMEM;
  3156. }
  3157. } else {
  3158. mapped_nents = 0;
  3159. }
  3160. /* allocate space for base edesc and link tables */
  3161. edesc = qi_cache_zalloc(GFP_DMA | flags);
  3162. if (!edesc) {
  3163. dma_unmap_sg(ctx->dev, req->src, src_nents,
  3164. DMA_TO_DEVICE);
  3165. return -ENOMEM;
  3166. }
  3167. edesc->src_nents = src_nents;
  3168. qm_sg_src_index = 1 + (*buflen ? 1 : 0);
  3169. qm_sg_bytes = pad_sg_nents(qm_sg_src_index + mapped_nents) *
  3170. sizeof(*sg_table);
  3171. sg_table = &edesc->sgt[0];
  3172. ret = ctx_map_to_qm_sg(ctx->dev, state, ctx->ctx_len, sg_table,
  3173. DMA_BIDIRECTIONAL);
  3174. if (ret)
  3175. goto unmap_ctx;
  3176. ret = buf_map_to_qm_sg(ctx->dev, sg_table + 1, state);
  3177. if (ret)
  3178. goto unmap_ctx;
  3179. if (mapped_nents) {
  3180. sg_to_qm_sg_last(req->src, src_len,
  3181. sg_table + qm_sg_src_index, 0);
  3182. if (*next_buflen)
  3183. scatterwalk_map_and_copy(next_buf, req->src,
  3184. to_hash - *buflen,
  3185. *next_buflen, 0);
  3186. } else {
  3187. dpaa2_sg_set_final(sg_table + qm_sg_src_index - 1,
  3188. true);
  3189. }
  3190. edesc->qm_sg_dma = dma_map_single(ctx->dev, sg_table,
  3191. qm_sg_bytes, DMA_TO_DEVICE);
  3192. if (dma_mapping_error(ctx->dev, edesc->qm_sg_dma)) {
  3193. dev_err(ctx->dev, "unable to map S/G table\n");
  3194. ret = -ENOMEM;
  3195. goto unmap_ctx;
  3196. }
  3197. edesc->qm_sg_bytes = qm_sg_bytes;
  3198. memset(&req_ctx->fd_flt, 0, sizeof(req_ctx->fd_flt));
  3199. dpaa2_fl_set_final(in_fle, true);
  3200. dpaa2_fl_set_format(in_fle, dpaa2_fl_sg);
  3201. dpaa2_fl_set_addr(in_fle, edesc->qm_sg_dma);
  3202. dpaa2_fl_set_len(in_fle, ctx->ctx_len + to_hash);
  3203. dpaa2_fl_set_format(out_fle, dpaa2_fl_single);
  3204. dpaa2_fl_set_addr(out_fle, state->ctx_dma);
  3205. dpaa2_fl_set_len(out_fle, ctx->ctx_len);
  3206. req_ctx->flc = &ctx->flc[UPDATE];
  3207. req_ctx->flc_dma = ctx->flc_dma[UPDATE];
  3208. req_ctx->cbk = ahash_done_bi;
  3209. req_ctx->ctx = &req->base;
  3210. req_ctx->edesc = edesc;
  3211. ret = dpaa2_caam_enqueue(ctx->dev, req_ctx);
  3212. if (ret != -EINPROGRESS &&
  3213. !(ret == -EBUSY &&
  3214. req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG))
  3215. goto unmap_ctx;
  3216. } else if (*next_buflen) {
  3217. scatterwalk_map_and_copy(buf + *buflen, req->src, 0,
  3218. req->nbytes, 0);
  3219. *buflen = *next_buflen;
  3220. *next_buflen = last_buflen;
  3221. }
  3222. print_hex_dump_debug("buf@" __stringify(__LINE__)": ",
  3223. DUMP_PREFIX_ADDRESS, 16, 4, buf, *buflen, 1);
  3224. print_hex_dump_debug("next buf@" __stringify(__LINE__)": ",
  3225. DUMP_PREFIX_ADDRESS, 16, 4, next_buf, *next_buflen,
  3226. 1);
  3227. return ret;
  3228. unmap_ctx:
  3229. ahash_unmap_ctx(ctx->dev, edesc, req, DMA_BIDIRECTIONAL);
  3230. qi_cache_free(edesc);
  3231. return ret;
  3232. }
  3233. static int ahash_final_ctx(struct ahash_request *req)
  3234. {
  3235. struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
  3236. struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
  3237. struct caam_hash_state *state = ahash_request_ctx(req);
  3238. struct caam_request *req_ctx = &state->caam_req;
  3239. struct dpaa2_fl_entry *in_fle = &req_ctx->fd_flt[1];
  3240. struct dpaa2_fl_entry *out_fle = &req_ctx->fd_flt[0];
  3241. gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
  3242. GFP_KERNEL : GFP_ATOMIC;
  3243. int buflen = *current_buflen(state);
  3244. int qm_sg_bytes;
  3245. int digestsize = crypto_ahash_digestsize(ahash);
  3246. struct ahash_edesc *edesc;
  3247. struct dpaa2_sg_entry *sg_table;
  3248. int ret;
  3249. /* allocate space for base edesc and link tables */
  3250. edesc = qi_cache_zalloc(GFP_DMA | flags);
  3251. if (!edesc)
  3252. return -ENOMEM;
  3253. qm_sg_bytes = pad_sg_nents(1 + (buflen ? 1 : 0)) * sizeof(*sg_table);
  3254. sg_table = &edesc->sgt[0];
  3255. ret = ctx_map_to_qm_sg(ctx->dev, state, ctx->ctx_len, sg_table,
  3256. DMA_BIDIRECTIONAL);
  3257. if (ret)
  3258. goto unmap_ctx;
  3259. ret = buf_map_to_qm_sg(ctx->dev, sg_table + 1, state);
  3260. if (ret)
  3261. goto unmap_ctx;
  3262. dpaa2_sg_set_final(sg_table + (buflen ? 1 : 0), true);
  3263. edesc->qm_sg_dma = dma_map_single(ctx->dev, sg_table, qm_sg_bytes,
  3264. DMA_TO_DEVICE);
  3265. if (dma_mapping_error(ctx->dev, edesc->qm_sg_dma)) {
  3266. dev_err(ctx->dev, "unable to map S/G table\n");
  3267. ret = -ENOMEM;
  3268. goto unmap_ctx;
  3269. }
  3270. edesc->qm_sg_bytes = qm_sg_bytes;
  3271. memset(&req_ctx->fd_flt, 0, sizeof(req_ctx->fd_flt));
  3272. dpaa2_fl_set_final(in_fle, true);
  3273. dpaa2_fl_set_format(in_fle, dpaa2_fl_sg);
  3274. dpaa2_fl_set_addr(in_fle, edesc->qm_sg_dma);
  3275. dpaa2_fl_set_len(in_fle, ctx->ctx_len + buflen);
  3276. dpaa2_fl_set_format(out_fle, dpaa2_fl_single);
  3277. dpaa2_fl_set_addr(out_fle, state->ctx_dma);
  3278. dpaa2_fl_set_len(out_fle, digestsize);
  3279. req_ctx->flc = &ctx->flc[FINALIZE];
  3280. req_ctx->flc_dma = ctx->flc_dma[FINALIZE];
  3281. req_ctx->cbk = ahash_done_ctx_src;
  3282. req_ctx->ctx = &req->base;
  3283. req_ctx->edesc = edesc;
  3284. ret = dpaa2_caam_enqueue(ctx->dev, req_ctx);
  3285. if (ret == -EINPROGRESS ||
  3286. (ret == -EBUSY && req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG))
  3287. return ret;
  3288. unmap_ctx:
  3289. ahash_unmap_ctx(ctx->dev, edesc, req, DMA_BIDIRECTIONAL);
  3290. qi_cache_free(edesc);
  3291. return ret;
  3292. }
  3293. static int ahash_finup_ctx(struct ahash_request *req)
  3294. {
  3295. struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
  3296. struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
  3297. struct caam_hash_state *state = ahash_request_ctx(req);
  3298. struct caam_request *req_ctx = &state->caam_req;
  3299. struct dpaa2_fl_entry *in_fle = &req_ctx->fd_flt[1];
  3300. struct dpaa2_fl_entry *out_fle = &req_ctx->fd_flt[0];
  3301. gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
  3302. GFP_KERNEL : GFP_ATOMIC;
  3303. int buflen = *current_buflen(state);
  3304. int qm_sg_bytes, qm_sg_src_index;
  3305. int src_nents, mapped_nents;
  3306. int digestsize = crypto_ahash_digestsize(ahash);
  3307. struct ahash_edesc *edesc;
  3308. struct dpaa2_sg_entry *sg_table;
  3309. int ret;
  3310. src_nents = sg_nents_for_len(req->src, req->nbytes);
  3311. if (src_nents < 0) {
  3312. dev_err(ctx->dev, "Invalid number of src SG.\n");
  3313. return src_nents;
  3314. }
  3315. if (src_nents) {
  3316. mapped_nents = dma_map_sg(ctx->dev, req->src, src_nents,
  3317. DMA_TO_DEVICE);
  3318. if (!mapped_nents) {
  3319. dev_err(ctx->dev, "unable to DMA map source\n");
  3320. return -ENOMEM;
  3321. }
  3322. } else {
  3323. mapped_nents = 0;
  3324. }
  3325. /* allocate space for base edesc and link tables */
  3326. edesc = qi_cache_zalloc(GFP_DMA | flags);
  3327. if (!edesc) {
  3328. dma_unmap_sg(ctx->dev, req->src, src_nents, DMA_TO_DEVICE);
  3329. return -ENOMEM;
  3330. }
  3331. edesc->src_nents = src_nents;
  3332. qm_sg_src_index = 1 + (buflen ? 1 : 0);
  3333. qm_sg_bytes = pad_sg_nents(qm_sg_src_index + mapped_nents) *
  3334. sizeof(*sg_table);
  3335. sg_table = &edesc->sgt[0];
  3336. ret = ctx_map_to_qm_sg(ctx->dev, state, ctx->ctx_len, sg_table,
  3337. DMA_BIDIRECTIONAL);
  3338. if (ret)
  3339. goto unmap_ctx;
  3340. ret = buf_map_to_qm_sg(ctx->dev, sg_table + 1, state);
  3341. if (ret)
  3342. goto unmap_ctx;
  3343. sg_to_qm_sg_last(req->src, req->nbytes, sg_table + qm_sg_src_index, 0);
  3344. edesc->qm_sg_dma = dma_map_single(ctx->dev, sg_table, qm_sg_bytes,
  3345. DMA_TO_DEVICE);
  3346. if (dma_mapping_error(ctx->dev, edesc->qm_sg_dma)) {
  3347. dev_err(ctx->dev, "unable to map S/G table\n");
  3348. ret = -ENOMEM;
  3349. goto unmap_ctx;
  3350. }
  3351. edesc->qm_sg_bytes = qm_sg_bytes;
  3352. memset(&req_ctx->fd_flt, 0, sizeof(req_ctx->fd_flt));
  3353. dpaa2_fl_set_final(in_fle, true);
  3354. dpaa2_fl_set_format(in_fle, dpaa2_fl_sg);
  3355. dpaa2_fl_set_addr(in_fle, edesc->qm_sg_dma);
  3356. dpaa2_fl_set_len(in_fle, ctx->ctx_len + buflen + req->nbytes);
  3357. dpaa2_fl_set_format(out_fle, dpaa2_fl_single);
  3358. dpaa2_fl_set_addr(out_fle, state->ctx_dma);
  3359. dpaa2_fl_set_len(out_fle, digestsize);
  3360. req_ctx->flc = &ctx->flc[FINALIZE];
  3361. req_ctx->flc_dma = ctx->flc_dma[FINALIZE];
  3362. req_ctx->cbk = ahash_done_ctx_src;
  3363. req_ctx->ctx = &req->base;
  3364. req_ctx->edesc = edesc;
  3365. ret = dpaa2_caam_enqueue(ctx->dev, req_ctx);
  3366. if (ret == -EINPROGRESS ||
  3367. (ret == -EBUSY && req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG))
  3368. return ret;
  3369. unmap_ctx:
  3370. ahash_unmap_ctx(ctx->dev, edesc, req, DMA_BIDIRECTIONAL);
  3371. qi_cache_free(edesc);
  3372. return ret;
  3373. }
  3374. static int ahash_digest(struct ahash_request *req)
  3375. {
  3376. struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
  3377. struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
  3378. struct caam_hash_state *state = ahash_request_ctx(req);
  3379. struct caam_request *req_ctx = &state->caam_req;
  3380. struct dpaa2_fl_entry *in_fle = &req_ctx->fd_flt[1];
  3381. struct dpaa2_fl_entry *out_fle = &req_ctx->fd_flt[0];
  3382. gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
  3383. GFP_KERNEL : GFP_ATOMIC;
  3384. int digestsize = crypto_ahash_digestsize(ahash);
  3385. int src_nents, mapped_nents;
  3386. struct ahash_edesc *edesc;
  3387. int ret = -ENOMEM;
  3388. state->buf_dma = 0;
  3389. src_nents = sg_nents_for_len(req->src, req->nbytes);
  3390. if (src_nents < 0) {
  3391. dev_err(ctx->dev, "Invalid number of src SG.\n");
  3392. return src_nents;
  3393. }
  3394. if (src_nents) {
  3395. mapped_nents = dma_map_sg(ctx->dev, req->src, src_nents,
  3396. DMA_TO_DEVICE);
  3397. if (!mapped_nents) {
  3398. dev_err(ctx->dev, "unable to map source for DMA\n");
  3399. return ret;
  3400. }
  3401. } else {
  3402. mapped_nents = 0;
  3403. }
  3404. /* allocate space for base edesc and link tables */
  3405. edesc = qi_cache_zalloc(GFP_DMA | flags);
  3406. if (!edesc) {
  3407. dma_unmap_sg(ctx->dev, req->src, src_nents, DMA_TO_DEVICE);
  3408. return ret;
  3409. }
  3410. edesc->src_nents = src_nents;
  3411. memset(&req_ctx->fd_flt, 0, sizeof(req_ctx->fd_flt));
  3412. if (mapped_nents > 1) {
  3413. int qm_sg_bytes;
  3414. struct dpaa2_sg_entry *sg_table = &edesc->sgt[0];
  3415. qm_sg_bytes = pad_sg_nents(mapped_nents) * sizeof(*sg_table);
  3416. sg_to_qm_sg_last(req->src, req->nbytes, sg_table, 0);
  3417. edesc->qm_sg_dma = dma_map_single(ctx->dev, sg_table,
  3418. qm_sg_bytes, DMA_TO_DEVICE);
  3419. if (dma_mapping_error(ctx->dev, edesc->qm_sg_dma)) {
  3420. dev_err(ctx->dev, "unable to map S/G table\n");
  3421. goto unmap;
  3422. }
  3423. edesc->qm_sg_bytes = qm_sg_bytes;
  3424. dpaa2_fl_set_format(in_fle, dpaa2_fl_sg);
  3425. dpaa2_fl_set_addr(in_fle, edesc->qm_sg_dma);
  3426. } else {
  3427. dpaa2_fl_set_format(in_fle, dpaa2_fl_single);
  3428. dpaa2_fl_set_addr(in_fle, sg_dma_address(req->src));
  3429. }
  3430. state->ctx_dma_len = digestsize;
  3431. state->ctx_dma = dma_map_single(ctx->dev, state->caam_ctx, digestsize,
  3432. DMA_FROM_DEVICE);
  3433. if (dma_mapping_error(ctx->dev, state->ctx_dma)) {
  3434. dev_err(ctx->dev, "unable to map ctx\n");
  3435. state->ctx_dma = 0;
  3436. goto unmap;
  3437. }
  3438. dpaa2_fl_set_final(in_fle, true);
  3439. dpaa2_fl_set_len(in_fle, req->nbytes);
  3440. dpaa2_fl_set_format(out_fle, dpaa2_fl_single);
  3441. dpaa2_fl_set_addr(out_fle, state->ctx_dma);
  3442. dpaa2_fl_set_len(out_fle, digestsize);
  3443. req_ctx->flc = &ctx->flc[DIGEST];
  3444. req_ctx->flc_dma = ctx->flc_dma[DIGEST];
  3445. req_ctx->cbk = ahash_done;
  3446. req_ctx->ctx = &req->base;
  3447. req_ctx->edesc = edesc;
  3448. ret = dpaa2_caam_enqueue(ctx->dev, req_ctx);
  3449. if (ret == -EINPROGRESS ||
  3450. (ret == -EBUSY && req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG))
  3451. return ret;
  3452. unmap:
  3453. ahash_unmap_ctx(ctx->dev, edesc, req, DMA_FROM_DEVICE);
  3454. qi_cache_free(edesc);
  3455. return ret;
  3456. }
  3457. static int ahash_final_no_ctx(struct ahash_request *req)
  3458. {
  3459. struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
  3460. struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
  3461. struct caam_hash_state *state = ahash_request_ctx(req);
  3462. struct caam_request *req_ctx = &state->caam_req;
  3463. struct dpaa2_fl_entry *in_fle = &req_ctx->fd_flt[1];
  3464. struct dpaa2_fl_entry *out_fle = &req_ctx->fd_flt[0];
  3465. gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
  3466. GFP_KERNEL : GFP_ATOMIC;
  3467. u8 *buf = current_buf(state);
  3468. int buflen = *current_buflen(state);
  3469. int digestsize = crypto_ahash_digestsize(ahash);
  3470. struct ahash_edesc *edesc;
  3471. int ret = -ENOMEM;
  3472. /* allocate space for base edesc and link tables */
  3473. edesc = qi_cache_zalloc(GFP_DMA | flags);
  3474. if (!edesc)
  3475. return ret;
  3476. if (buflen) {
  3477. state->buf_dma = dma_map_single(ctx->dev, buf, buflen,
  3478. DMA_TO_DEVICE);
  3479. if (dma_mapping_error(ctx->dev, state->buf_dma)) {
  3480. dev_err(ctx->dev, "unable to map src\n");
  3481. goto unmap;
  3482. }
  3483. }
  3484. state->ctx_dma_len = digestsize;
  3485. state->ctx_dma = dma_map_single(ctx->dev, state->caam_ctx, digestsize,
  3486. DMA_FROM_DEVICE);
  3487. if (dma_mapping_error(ctx->dev, state->ctx_dma)) {
  3488. dev_err(ctx->dev, "unable to map ctx\n");
  3489. state->ctx_dma = 0;
  3490. goto unmap;
  3491. }
  3492. memset(&req_ctx->fd_flt, 0, sizeof(req_ctx->fd_flt));
  3493. dpaa2_fl_set_final(in_fle, true);
  3494. /*
  3495. * crypto engine requires the input entry to be present when
  3496. * "frame list" FD is used.
  3497. * Since engine does not support FMT=2'b11 (unused entry type), leaving
  3498. * in_fle zeroized (except for "Final" flag) is the best option.
  3499. */
  3500. if (buflen) {
  3501. dpaa2_fl_set_format(in_fle, dpaa2_fl_single);
  3502. dpaa2_fl_set_addr(in_fle, state->buf_dma);
  3503. dpaa2_fl_set_len(in_fle, buflen);
  3504. }
  3505. dpaa2_fl_set_format(out_fle, dpaa2_fl_single);
  3506. dpaa2_fl_set_addr(out_fle, state->ctx_dma);
  3507. dpaa2_fl_set_len(out_fle, digestsize);
  3508. req_ctx->flc = &ctx->flc[DIGEST];
  3509. req_ctx->flc_dma = ctx->flc_dma[DIGEST];
  3510. req_ctx->cbk = ahash_done;
  3511. req_ctx->ctx = &req->base;
  3512. req_ctx->edesc = edesc;
  3513. ret = dpaa2_caam_enqueue(ctx->dev, req_ctx);
  3514. if (ret == -EINPROGRESS ||
  3515. (ret == -EBUSY && req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG))
  3516. return ret;
  3517. unmap:
  3518. ahash_unmap_ctx(ctx->dev, edesc, req, DMA_FROM_DEVICE);
  3519. qi_cache_free(edesc);
  3520. return ret;
  3521. }
  3522. static int ahash_update_no_ctx(struct ahash_request *req)
  3523. {
  3524. struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
  3525. struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
  3526. struct caam_hash_state *state = ahash_request_ctx(req);
  3527. struct caam_request *req_ctx = &state->caam_req;
  3528. struct dpaa2_fl_entry *in_fle = &req_ctx->fd_flt[1];
  3529. struct dpaa2_fl_entry *out_fle = &req_ctx->fd_flt[0];
  3530. gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
  3531. GFP_KERNEL : GFP_ATOMIC;
  3532. u8 *buf = current_buf(state);
  3533. int *buflen = current_buflen(state);
  3534. u8 *next_buf = alt_buf(state);
  3535. int *next_buflen = alt_buflen(state);
  3536. int in_len = *buflen + req->nbytes, to_hash;
  3537. int qm_sg_bytes, src_nents, mapped_nents;
  3538. struct ahash_edesc *edesc;
  3539. int ret = 0;
  3540. *next_buflen = in_len & (crypto_tfm_alg_blocksize(&ahash->base) - 1);
  3541. to_hash = in_len - *next_buflen;
  3542. if (to_hash) {
  3543. struct dpaa2_sg_entry *sg_table;
  3544. int src_len = req->nbytes - *next_buflen;
  3545. src_nents = sg_nents_for_len(req->src, src_len);
  3546. if (src_nents < 0) {
  3547. dev_err(ctx->dev, "Invalid number of src SG.\n");
  3548. return src_nents;
  3549. }
  3550. if (src_nents) {
  3551. mapped_nents = dma_map_sg(ctx->dev, req->src, src_nents,
  3552. DMA_TO_DEVICE);
  3553. if (!mapped_nents) {
  3554. dev_err(ctx->dev, "unable to DMA map source\n");
  3555. return -ENOMEM;
  3556. }
  3557. } else {
  3558. mapped_nents = 0;
  3559. }
  3560. /* allocate space for base edesc and link tables */
  3561. edesc = qi_cache_zalloc(GFP_DMA | flags);
  3562. if (!edesc) {
  3563. dma_unmap_sg(ctx->dev, req->src, src_nents,
  3564. DMA_TO_DEVICE);
  3565. return -ENOMEM;
  3566. }
  3567. edesc->src_nents = src_nents;
  3568. qm_sg_bytes = pad_sg_nents(1 + mapped_nents) *
  3569. sizeof(*sg_table);
  3570. sg_table = &edesc->sgt[0];
  3571. ret = buf_map_to_qm_sg(ctx->dev, sg_table, state);
  3572. if (ret)
  3573. goto unmap_ctx;
  3574. sg_to_qm_sg_last(req->src, src_len, sg_table + 1, 0);
  3575. if (*next_buflen)
  3576. scatterwalk_map_and_copy(next_buf, req->src,
  3577. to_hash - *buflen,
  3578. *next_buflen, 0);
  3579. edesc->qm_sg_dma = dma_map_single(ctx->dev, sg_table,
  3580. qm_sg_bytes, DMA_TO_DEVICE);
  3581. if (dma_mapping_error(ctx->dev, edesc->qm_sg_dma)) {
  3582. dev_err(ctx->dev, "unable to map S/G table\n");
  3583. ret = -ENOMEM;
  3584. goto unmap_ctx;
  3585. }
  3586. edesc->qm_sg_bytes = qm_sg_bytes;
  3587. state->ctx_dma_len = ctx->ctx_len;
  3588. state->ctx_dma = dma_map_single(ctx->dev, state->caam_ctx,
  3589. ctx->ctx_len, DMA_FROM_DEVICE);
  3590. if (dma_mapping_error(ctx->dev, state->ctx_dma)) {
  3591. dev_err(ctx->dev, "unable to map ctx\n");
  3592. state->ctx_dma = 0;
  3593. ret = -ENOMEM;
  3594. goto unmap_ctx;
  3595. }
  3596. memset(&req_ctx->fd_flt, 0, sizeof(req_ctx->fd_flt));
  3597. dpaa2_fl_set_final(in_fle, true);
  3598. dpaa2_fl_set_format(in_fle, dpaa2_fl_sg);
  3599. dpaa2_fl_set_addr(in_fle, edesc->qm_sg_dma);
  3600. dpaa2_fl_set_len(in_fle, to_hash);
  3601. dpaa2_fl_set_format(out_fle, dpaa2_fl_single);
  3602. dpaa2_fl_set_addr(out_fle, state->ctx_dma);
  3603. dpaa2_fl_set_len(out_fle, ctx->ctx_len);
  3604. req_ctx->flc = &ctx->flc[UPDATE_FIRST];
  3605. req_ctx->flc_dma = ctx->flc_dma[UPDATE_FIRST];
  3606. req_ctx->cbk = ahash_done_ctx_dst;
  3607. req_ctx->ctx = &req->base;
  3608. req_ctx->edesc = edesc;
  3609. ret = dpaa2_caam_enqueue(ctx->dev, req_ctx);
  3610. if (ret != -EINPROGRESS &&
  3611. !(ret == -EBUSY &&
  3612. req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG))
  3613. goto unmap_ctx;
  3614. state->update = ahash_update_ctx;
  3615. state->finup = ahash_finup_ctx;
  3616. state->final = ahash_final_ctx;
  3617. } else if (*next_buflen) {
  3618. scatterwalk_map_and_copy(buf + *buflen, req->src, 0,
  3619. req->nbytes, 0);
  3620. *buflen = *next_buflen;
  3621. *next_buflen = 0;
  3622. }
  3623. print_hex_dump_debug("buf@" __stringify(__LINE__)": ",
  3624. DUMP_PREFIX_ADDRESS, 16, 4, buf, *buflen, 1);
  3625. print_hex_dump_debug("next buf@" __stringify(__LINE__)": ",
  3626. DUMP_PREFIX_ADDRESS, 16, 4, next_buf, *next_buflen,
  3627. 1);
  3628. return ret;
  3629. unmap_ctx:
  3630. ahash_unmap_ctx(ctx->dev, edesc, req, DMA_TO_DEVICE);
  3631. qi_cache_free(edesc);
  3632. return ret;
  3633. }
  3634. static int ahash_finup_no_ctx(struct ahash_request *req)
  3635. {
  3636. struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
  3637. struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
  3638. struct caam_hash_state *state = ahash_request_ctx(req);
  3639. struct caam_request *req_ctx = &state->caam_req;
  3640. struct dpaa2_fl_entry *in_fle = &req_ctx->fd_flt[1];
  3641. struct dpaa2_fl_entry *out_fle = &req_ctx->fd_flt[0];
  3642. gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
  3643. GFP_KERNEL : GFP_ATOMIC;
  3644. int buflen = *current_buflen(state);
  3645. int qm_sg_bytes, src_nents, mapped_nents;
  3646. int digestsize = crypto_ahash_digestsize(ahash);
  3647. struct ahash_edesc *edesc;
  3648. struct dpaa2_sg_entry *sg_table;
  3649. int ret;
  3650. src_nents = sg_nents_for_len(req->src, req->nbytes);
  3651. if (src_nents < 0) {
  3652. dev_err(ctx->dev, "Invalid number of src SG.\n");
  3653. return src_nents;
  3654. }
  3655. if (src_nents) {
  3656. mapped_nents = dma_map_sg(ctx->dev, req->src, src_nents,
  3657. DMA_TO_DEVICE);
  3658. if (!mapped_nents) {
  3659. dev_err(ctx->dev, "unable to DMA map source\n");
  3660. return -ENOMEM;
  3661. }
  3662. } else {
  3663. mapped_nents = 0;
  3664. }
  3665. /* allocate space for base edesc and link tables */
  3666. edesc = qi_cache_zalloc(GFP_DMA | flags);
  3667. if (!edesc) {
  3668. dma_unmap_sg(ctx->dev, req->src, src_nents, DMA_TO_DEVICE);
  3669. return -ENOMEM;
  3670. }
  3671. edesc->src_nents = src_nents;
  3672. qm_sg_bytes = pad_sg_nents(2 + mapped_nents) * sizeof(*sg_table);
  3673. sg_table = &edesc->sgt[0];
  3674. ret = buf_map_to_qm_sg(ctx->dev, sg_table, state);
  3675. if (ret)
  3676. goto unmap;
  3677. sg_to_qm_sg_last(req->src, req->nbytes, sg_table + 1, 0);
  3678. edesc->qm_sg_dma = dma_map_single(ctx->dev, sg_table, qm_sg_bytes,
  3679. DMA_TO_DEVICE);
  3680. if (dma_mapping_error(ctx->dev, edesc->qm_sg_dma)) {
  3681. dev_err(ctx->dev, "unable to map S/G table\n");
  3682. ret = -ENOMEM;
  3683. goto unmap;
  3684. }
  3685. edesc->qm_sg_bytes = qm_sg_bytes;
  3686. state->ctx_dma_len = digestsize;
  3687. state->ctx_dma = dma_map_single(ctx->dev, state->caam_ctx, digestsize,
  3688. DMA_FROM_DEVICE);
  3689. if (dma_mapping_error(ctx->dev, state->ctx_dma)) {
  3690. dev_err(ctx->dev, "unable to map ctx\n");
  3691. state->ctx_dma = 0;
  3692. ret = -ENOMEM;
  3693. goto unmap;
  3694. }
  3695. memset(&req_ctx->fd_flt, 0, sizeof(req_ctx->fd_flt));
  3696. dpaa2_fl_set_final(in_fle, true);
  3697. dpaa2_fl_set_format(in_fle, dpaa2_fl_sg);
  3698. dpaa2_fl_set_addr(in_fle, edesc->qm_sg_dma);
  3699. dpaa2_fl_set_len(in_fle, buflen + req->nbytes);
  3700. dpaa2_fl_set_format(out_fle, dpaa2_fl_single);
  3701. dpaa2_fl_set_addr(out_fle, state->ctx_dma);
  3702. dpaa2_fl_set_len(out_fle, digestsize);
  3703. req_ctx->flc = &ctx->flc[DIGEST];
  3704. req_ctx->flc_dma = ctx->flc_dma[DIGEST];
  3705. req_ctx->cbk = ahash_done;
  3706. req_ctx->ctx = &req->base;
  3707. req_ctx->edesc = edesc;
  3708. ret = dpaa2_caam_enqueue(ctx->dev, req_ctx);
  3709. if (ret != -EINPROGRESS &&
  3710. !(ret == -EBUSY && req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG))
  3711. goto unmap;
  3712. return ret;
  3713. unmap:
  3714. ahash_unmap_ctx(ctx->dev, edesc, req, DMA_FROM_DEVICE);
  3715. qi_cache_free(edesc);
  3716. return -ENOMEM;
  3717. }
  3718. static int ahash_update_first(struct ahash_request *req)
  3719. {
  3720. struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
  3721. struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
  3722. struct caam_hash_state *state = ahash_request_ctx(req);
  3723. struct caam_request *req_ctx = &state->caam_req;
  3724. struct dpaa2_fl_entry *in_fle = &req_ctx->fd_flt[1];
  3725. struct dpaa2_fl_entry *out_fle = &req_ctx->fd_flt[0];
  3726. gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
  3727. GFP_KERNEL : GFP_ATOMIC;
  3728. u8 *next_buf = alt_buf(state);
  3729. int *next_buflen = alt_buflen(state);
  3730. int to_hash;
  3731. int src_nents, mapped_nents;
  3732. struct ahash_edesc *edesc;
  3733. int ret = 0;
  3734. *next_buflen = req->nbytes & (crypto_tfm_alg_blocksize(&ahash->base) -
  3735. 1);
  3736. to_hash = req->nbytes - *next_buflen;
  3737. if (to_hash) {
  3738. struct dpaa2_sg_entry *sg_table;
  3739. int src_len = req->nbytes - *next_buflen;
  3740. src_nents = sg_nents_for_len(req->src, src_len);
  3741. if (src_nents < 0) {
  3742. dev_err(ctx->dev, "Invalid number of src SG.\n");
  3743. return src_nents;
  3744. }
  3745. if (src_nents) {
  3746. mapped_nents = dma_map_sg(ctx->dev, req->src, src_nents,
  3747. DMA_TO_DEVICE);
  3748. if (!mapped_nents) {
  3749. dev_err(ctx->dev, "unable to map source for DMA\n");
  3750. return -ENOMEM;
  3751. }
  3752. } else {
  3753. mapped_nents = 0;
  3754. }
  3755. /* allocate space for base edesc and link tables */
  3756. edesc = qi_cache_zalloc(GFP_DMA | flags);
  3757. if (!edesc) {
  3758. dma_unmap_sg(ctx->dev, req->src, src_nents,
  3759. DMA_TO_DEVICE);
  3760. return -ENOMEM;
  3761. }
  3762. edesc->src_nents = src_nents;
  3763. sg_table = &edesc->sgt[0];
  3764. memset(&req_ctx->fd_flt, 0, sizeof(req_ctx->fd_flt));
  3765. dpaa2_fl_set_final(in_fle, true);
  3766. dpaa2_fl_set_len(in_fle, to_hash);
  3767. if (mapped_nents > 1) {
  3768. int qm_sg_bytes;
  3769. sg_to_qm_sg_last(req->src, src_len, sg_table, 0);
  3770. qm_sg_bytes = pad_sg_nents(mapped_nents) *
  3771. sizeof(*sg_table);
  3772. edesc->qm_sg_dma = dma_map_single(ctx->dev, sg_table,
  3773. qm_sg_bytes,
  3774. DMA_TO_DEVICE);
  3775. if (dma_mapping_error(ctx->dev, edesc->qm_sg_dma)) {
  3776. dev_err(ctx->dev, "unable to map S/G table\n");
  3777. ret = -ENOMEM;
  3778. goto unmap_ctx;
  3779. }
  3780. edesc->qm_sg_bytes = qm_sg_bytes;
  3781. dpaa2_fl_set_format(in_fle, dpaa2_fl_sg);
  3782. dpaa2_fl_set_addr(in_fle, edesc->qm_sg_dma);
  3783. } else {
  3784. dpaa2_fl_set_format(in_fle, dpaa2_fl_single);
  3785. dpaa2_fl_set_addr(in_fle, sg_dma_address(req->src));
  3786. }
  3787. if (*next_buflen)
  3788. scatterwalk_map_and_copy(next_buf, req->src, to_hash,
  3789. *next_buflen, 0);
  3790. state->ctx_dma_len = ctx->ctx_len;
  3791. state->ctx_dma = dma_map_single(ctx->dev, state->caam_ctx,
  3792. ctx->ctx_len, DMA_FROM_DEVICE);
  3793. if (dma_mapping_error(ctx->dev, state->ctx_dma)) {
  3794. dev_err(ctx->dev, "unable to map ctx\n");
  3795. state->ctx_dma = 0;
  3796. ret = -ENOMEM;
  3797. goto unmap_ctx;
  3798. }
  3799. dpaa2_fl_set_format(out_fle, dpaa2_fl_single);
  3800. dpaa2_fl_set_addr(out_fle, state->ctx_dma);
  3801. dpaa2_fl_set_len(out_fle, ctx->ctx_len);
  3802. req_ctx->flc = &ctx->flc[UPDATE_FIRST];
  3803. req_ctx->flc_dma = ctx->flc_dma[UPDATE_FIRST];
  3804. req_ctx->cbk = ahash_done_ctx_dst;
  3805. req_ctx->ctx = &req->base;
  3806. req_ctx->edesc = edesc;
  3807. ret = dpaa2_caam_enqueue(ctx->dev, req_ctx);
  3808. if (ret != -EINPROGRESS &&
  3809. !(ret == -EBUSY && req->base.flags &
  3810. CRYPTO_TFM_REQ_MAY_BACKLOG))
  3811. goto unmap_ctx;
  3812. state->update = ahash_update_ctx;
  3813. state->finup = ahash_finup_ctx;
  3814. state->final = ahash_final_ctx;
  3815. } else if (*next_buflen) {
  3816. state->update = ahash_update_no_ctx;
  3817. state->finup = ahash_finup_no_ctx;
  3818. state->final = ahash_final_no_ctx;
  3819. scatterwalk_map_and_copy(next_buf, req->src, 0,
  3820. req->nbytes, 0);
  3821. switch_buf(state);
  3822. }
  3823. print_hex_dump_debug("next buf@" __stringify(__LINE__)": ",
  3824. DUMP_PREFIX_ADDRESS, 16, 4, next_buf, *next_buflen,
  3825. 1);
  3826. return ret;
  3827. unmap_ctx:
  3828. ahash_unmap_ctx(ctx->dev, edesc, req, DMA_TO_DEVICE);
  3829. qi_cache_free(edesc);
  3830. return ret;
  3831. }
  3832. static int ahash_finup_first(struct ahash_request *req)
  3833. {
  3834. return ahash_digest(req);
  3835. }
  3836. static int ahash_init(struct ahash_request *req)
  3837. {
  3838. struct caam_hash_state *state = ahash_request_ctx(req);
  3839. state->update = ahash_update_first;
  3840. state->finup = ahash_finup_first;
  3841. state->final = ahash_final_no_ctx;
  3842. state->ctx_dma = 0;
  3843. state->ctx_dma_len = 0;
  3844. state->current_buf = 0;
  3845. state->buf_dma = 0;
  3846. state->buflen_0 = 0;
  3847. state->buflen_1 = 0;
  3848. return 0;
  3849. }
  3850. static int ahash_update(struct ahash_request *req)
  3851. {
  3852. struct caam_hash_state *state = ahash_request_ctx(req);
  3853. return state->update(req);
  3854. }
  3855. static int ahash_finup(struct ahash_request *req)
  3856. {
  3857. struct caam_hash_state *state = ahash_request_ctx(req);
  3858. return state->finup(req);
  3859. }
  3860. static int ahash_final(struct ahash_request *req)
  3861. {
  3862. struct caam_hash_state *state = ahash_request_ctx(req);
  3863. return state->final(req);
  3864. }
  3865. static int ahash_export(struct ahash_request *req, void *out)
  3866. {
  3867. struct caam_hash_state *state = ahash_request_ctx(req);
  3868. struct caam_export_state *export = out;
  3869. int len;
  3870. u8 *buf;
  3871. if (state->current_buf) {
  3872. buf = state->buf_1;
  3873. len = state->buflen_1;
  3874. } else {
  3875. buf = state->buf_0;
  3876. len = state->buflen_0;
  3877. }
  3878. memcpy(export->buf, buf, len);
  3879. memcpy(export->caam_ctx, state->caam_ctx, sizeof(export->caam_ctx));
  3880. export->buflen = len;
  3881. export->update = state->update;
  3882. export->final = state->final;
  3883. export->finup = state->finup;
  3884. return 0;
  3885. }
  3886. static int ahash_import(struct ahash_request *req, const void *in)
  3887. {
  3888. struct caam_hash_state *state = ahash_request_ctx(req);
  3889. const struct caam_export_state *export = in;
  3890. memset(state, 0, sizeof(*state));
  3891. memcpy(state->buf_0, export->buf, export->buflen);
  3892. memcpy(state->caam_ctx, export->caam_ctx, sizeof(state->caam_ctx));
  3893. state->buflen_0 = export->buflen;
  3894. state->update = export->update;
  3895. state->final = export->final;
  3896. state->finup = export->finup;
  3897. return 0;
  3898. }
  3899. struct caam_hash_template {
  3900. char name[CRYPTO_MAX_ALG_NAME];
  3901. char driver_name[CRYPTO_MAX_ALG_NAME];
  3902. char hmac_name[CRYPTO_MAX_ALG_NAME];
  3903. char hmac_driver_name[CRYPTO_MAX_ALG_NAME];
  3904. unsigned int blocksize;
  3905. struct ahash_alg template_ahash;
  3906. u32 alg_type;
  3907. };
  3908. /* ahash descriptors */
  3909. static struct caam_hash_template driver_hash[] = {
  3910. {
  3911. .name = "sha1",
  3912. .driver_name = "sha1-caam-qi2",
  3913. .hmac_name = "hmac(sha1)",
  3914. .hmac_driver_name = "hmac-sha1-caam-qi2",
  3915. .blocksize = SHA1_BLOCK_SIZE,
  3916. .template_ahash = {
  3917. .init = ahash_init,
  3918. .update = ahash_update,
  3919. .final = ahash_final,
  3920. .finup = ahash_finup,
  3921. .digest = ahash_digest,
  3922. .export = ahash_export,
  3923. .import = ahash_import,
  3924. .setkey = ahash_setkey,
  3925. .halg = {
  3926. .digestsize = SHA1_DIGEST_SIZE,
  3927. .statesize = sizeof(struct caam_export_state),
  3928. },
  3929. },
  3930. .alg_type = OP_ALG_ALGSEL_SHA1,
  3931. }, {
  3932. .name = "sha224",
  3933. .driver_name = "sha224-caam-qi2",
  3934. .hmac_name = "hmac(sha224)",
  3935. .hmac_driver_name = "hmac-sha224-caam-qi2",
  3936. .blocksize = SHA224_BLOCK_SIZE,
  3937. .template_ahash = {
  3938. .init = ahash_init,
  3939. .update = ahash_update,
  3940. .final = ahash_final,
  3941. .finup = ahash_finup,
  3942. .digest = ahash_digest,
  3943. .export = ahash_export,
  3944. .import = ahash_import,
  3945. .setkey = ahash_setkey,
  3946. .halg = {
  3947. .digestsize = SHA224_DIGEST_SIZE,
  3948. .statesize = sizeof(struct caam_export_state),
  3949. },
  3950. },
  3951. .alg_type = OP_ALG_ALGSEL_SHA224,
  3952. }, {
  3953. .name = "sha256",
  3954. .driver_name = "sha256-caam-qi2",
  3955. .hmac_name = "hmac(sha256)",
  3956. .hmac_driver_name = "hmac-sha256-caam-qi2",
  3957. .blocksize = SHA256_BLOCK_SIZE,
  3958. .template_ahash = {
  3959. .init = ahash_init,
  3960. .update = ahash_update,
  3961. .final = ahash_final,
  3962. .finup = ahash_finup,
  3963. .digest = ahash_digest,
  3964. .export = ahash_export,
  3965. .import = ahash_import,
  3966. .setkey = ahash_setkey,
  3967. .halg = {
  3968. .digestsize = SHA256_DIGEST_SIZE,
  3969. .statesize = sizeof(struct caam_export_state),
  3970. },
  3971. },
  3972. .alg_type = OP_ALG_ALGSEL_SHA256,
  3973. }, {
  3974. .name = "sha384",
  3975. .driver_name = "sha384-caam-qi2",
  3976. .hmac_name = "hmac(sha384)",
  3977. .hmac_driver_name = "hmac-sha384-caam-qi2",
  3978. .blocksize = SHA384_BLOCK_SIZE,
  3979. .template_ahash = {
  3980. .init = ahash_init,
  3981. .update = ahash_update,
  3982. .final = ahash_final,
  3983. .finup = ahash_finup,
  3984. .digest = ahash_digest,
  3985. .export = ahash_export,
  3986. .import = ahash_import,
  3987. .setkey = ahash_setkey,
  3988. .halg = {
  3989. .digestsize = SHA384_DIGEST_SIZE,
  3990. .statesize = sizeof(struct caam_export_state),
  3991. },
  3992. },
  3993. .alg_type = OP_ALG_ALGSEL_SHA384,
  3994. }, {
  3995. .name = "sha512",
  3996. .driver_name = "sha512-caam-qi2",
  3997. .hmac_name = "hmac(sha512)",
  3998. .hmac_driver_name = "hmac-sha512-caam-qi2",
  3999. .blocksize = SHA512_BLOCK_SIZE,
  4000. .template_ahash = {
  4001. .init = ahash_init,
  4002. .update = ahash_update,
  4003. .final = ahash_final,
  4004. .finup = ahash_finup,
  4005. .digest = ahash_digest,
  4006. .export = ahash_export,
  4007. .import = ahash_import,
  4008. .setkey = ahash_setkey,
  4009. .halg = {
  4010. .digestsize = SHA512_DIGEST_SIZE,
  4011. .statesize = sizeof(struct caam_export_state),
  4012. },
  4013. },
  4014. .alg_type = OP_ALG_ALGSEL_SHA512,
  4015. }, {
  4016. .name = "md5",
  4017. .driver_name = "md5-caam-qi2",
  4018. .hmac_name = "hmac(md5)",
  4019. .hmac_driver_name = "hmac-md5-caam-qi2",
  4020. .blocksize = MD5_BLOCK_WORDS * 4,
  4021. .template_ahash = {
  4022. .init = ahash_init,
  4023. .update = ahash_update,
  4024. .final = ahash_final,
  4025. .finup = ahash_finup,
  4026. .digest = ahash_digest,
  4027. .export = ahash_export,
  4028. .import = ahash_import,
  4029. .setkey = ahash_setkey,
  4030. .halg = {
  4031. .digestsize = MD5_DIGEST_SIZE,
  4032. .statesize = sizeof(struct caam_export_state),
  4033. },
  4034. },
  4035. .alg_type = OP_ALG_ALGSEL_MD5,
  4036. }
  4037. };
  4038. struct caam_hash_alg {
  4039. struct list_head entry;
  4040. struct device *dev;
  4041. int alg_type;
  4042. struct ahash_alg ahash_alg;
  4043. };
  4044. static int caam_hash_cra_init(struct crypto_tfm *tfm)
  4045. {
  4046. struct crypto_ahash *ahash = __crypto_ahash_cast(tfm);
  4047. struct crypto_alg *base = tfm->__crt_alg;
  4048. struct hash_alg_common *halg =
  4049. container_of(base, struct hash_alg_common, base);
  4050. struct ahash_alg *alg =
  4051. container_of(halg, struct ahash_alg, halg);
  4052. struct caam_hash_alg *caam_hash =
  4053. container_of(alg, struct caam_hash_alg, ahash_alg);
  4054. struct caam_hash_ctx *ctx = crypto_tfm_ctx(tfm);
  4055. /* Sizes for MDHA running digests: MD5, SHA1, 224, 256, 384, 512 */
  4056. static const u8 runninglen[] = { HASH_MSG_LEN + MD5_DIGEST_SIZE,
  4057. HASH_MSG_LEN + SHA1_DIGEST_SIZE,
  4058. HASH_MSG_LEN + 32,
  4059. HASH_MSG_LEN + SHA256_DIGEST_SIZE,
  4060. HASH_MSG_LEN + 64,
  4061. HASH_MSG_LEN + SHA512_DIGEST_SIZE };
  4062. dma_addr_t dma_addr;
  4063. int i;
  4064. ctx->dev = caam_hash->dev;
  4065. if (alg->setkey) {
  4066. ctx->adata.key_dma = dma_map_single_attrs(ctx->dev, ctx->key,
  4067. ARRAY_SIZE(ctx->key),
  4068. DMA_TO_DEVICE,
  4069. DMA_ATTR_SKIP_CPU_SYNC);
  4070. if (dma_mapping_error(ctx->dev, ctx->adata.key_dma)) {
  4071. dev_err(ctx->dev, "unable to map key\n");
  4072. return -ENOMEM;
  4073. }
  4074. }
  4075. dma_addr = dma_map_single_attrs(ctx->dev, ctx->flc, sizeof(ctx->flc),
  4076. DMA_BIDIRECTIONAL,
  4077. DMA_ATTR_SKIP_CPU_SYNC);
  4078. if (dma_mapping_error(ctx->dev, dma_addr)) {
  4079. dev_err(ctx->dev, "unable to map shared descriptors\n");
  4080. if (ctx->adata.key_dma)
  4081. dma_unmap_single_attrs(ctx->dev, ctx->adata.key_dma,
  4082. ARRAY_SIZE(ctx->key),
  4083. DMA_TO_DEVICE,
  4084. DMA_ATTR_SKIP_CPU_SYNC);
  4085. return -ENOMEM;
  4086. }
  4087. for (i = 0; i < HASH_NUM_OP; i++)
  4088. ctx->flc_dma[i] = dma_addr + i * sizeof(ctx->flc[i]);
  4089. /* copy descriptor header template value */
  4090. ctx->adata.algtype = OP_TYPE_CLASS2_ALG | caam_hash->alg_type;
  4091. ctx->ctx_len = runninglen[(ctx->adata.algtype &
  4092. OP_ALG_ALGSEL_SUBMASK) >>
  4093. OP_ALG_ALGSEL_SHIFT];
  4094. crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
  4095. sizeof(struct caam_hash_state));
  4096. return ahash_set_sh_desc(ahash);
  4097. }
  4098. static void caam_hash_cra_exit(struct crypto_tfm *tfm)
  4099. {
  4100. struct caam_hash_ctx *ctx = crypto_tfm_ctx(tfm);
  4101. dma_unmap_single_attrs(ctx->dev, ctx->flc_dma[0], sizeof(ctx->flc),
  4102. DMA_BIDIRECTIONAL, DMA_ATTR_SKIP_CPU_SYNC);
  4103. if (ctx->adata.key_dma)
  4104. dma_unmap_single_attrs(ctx->dev, ctx->adata.key_dma,
  4105. ARRAY_SIZE(ctx->key), DMA_TO_DEVICE,
  4106. DMA_ATTR_SKIP_CPU_SYNC);
  4107. }
  4108. static struct caam_hash_alg *caam_hash_alloc(struct device *dev,
  4109. struct caam_hash_template *template, bool keyed)
  4110. {
  4111. struct caam_hash_alg *t_alg;
  4112. struct ahash_alg *halg;
  4113. struct crypto_alg *alg;
  4114. t_alg = kzalloc(sizeof(*t_alg), GFP_KERNEL);
  4115. if (!t_alg)
  4116. return ERR_PTR(-ENOMEM);
  4117. t_alg->ahash_alg = template->template_ahash;
  4118. halg = &t_alg->ahash_alg;
  4119. alg = &halg->halg.base;
  4120. if (keyed) {
  4121. snprintf(alg->cra_name, CRYPTO_MAX_ALG_NAME, "%s",
  4122. template->hmac_name);
  4123. snprintf(alg->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
  4124. template->hmac_driver_name);
  4125. } else {
  4126. snprintf(alg->cra_name, CRYPTO_MAX_ALG_NAME, "%s",
  4127. template->name);
  4128. snprintf(alg->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
  4129. template->driver_name);
  4130. t_alg->ahash_alg.setkey = NULL;
  4131. }
  4132. alg->cra_module = THIS_MODULE;
  4133. alg->cra_init = caam_hash_cra_init;
  4134. alg->cra_exit = caam_hash_cra_exit;
  4135. alg->cra_ctxsize = sizeof(struct caam_hash_ctx);
  4136. alg->cra_priority = CAAM_CRA_PRIORITY;
  4137. alg->cra_blocksize = template->blocksize;
  4138. alg->cra_alignmask = 0;
  4139. alg->cra_flags = CRYPTO_ALG_ASYNC;
  4140. t_alg->alg_type = template->alg_type;
  4141. t_alg->dev = dev;
  4142. return t_alg;
  4143. }
  4144. static void dpaa2_caam_fqdan_cb(struct dpaa2_io_notification_ctx *nctx)
  4145. {
  4146. struct dpaa2_caam_priv_per_cpu *ppriv;
  4147. ppriv = container_of(nctx, struct dpaa2_caam_priv_per_cpu, nctx);
  4148. napi_schedule_irqoff(&ppriv->napi);
  4149. }
  4150. static int __cold dpaa2_dpseci_dpio_setup(struct dpaa2_caam_priv *priv)
  4151. {
  4152. struct device *dev = priv->dev;
  4153. struct dpaa2_io_notification_ctx *nctx;
  4154. struct dpaa2_caam_priv_per_cpu *ppriv;
  4155. int err, i = 0, cpu;
  4156. for_each_online_cpu(cpu) {
  4157. ppriv = per_cpu_ptr(priv->ppriv, cpu);
  4158. ppriv->priv = priv;
  4159. nctx = &ppriv->nctx;
  4160. nctx->is_cdan = 0;
  4161. nctx->id = ppriv->rsp_fqid;
  4162. nctx->desired_cpu = cpu;
  4163. nctx->cb = dpaa2_caam_fqdan_cb;
  4164. /* Register notification callbacks */
  4165. ppriv->dpio = dpaa2_io_service_select(cpu);
  4166. err = dpaa2_io_service_register(ppriv->dpio, nctx, dev);
  4167. if (unlikely(err)) {
  4168. dev_dbg(dev, "No affine DPIO for cpu %d\n", cpu);
  4169. nctx->cb = NULL;
  4170. /*
  4171. * If no affine DPIO for this core, there's probably
  4172. * none available for next cores either. Signal we want
  4173. * to retry later, in case the DPIO devices weren't
  4174. * probed yet.
  4175. */
  4176. err = -EPROBE_DEFER;
  4177. goto err;
  4178. }
  4179. ppriv->store = dpaa2_io_store_create(DPAA2_CAAM_STORE_SIZE,
  4180. dev);
  4181. if (unlikely(!ppriv->store)) {
  4182. dev_err(dev, "dpaa2_io_store_create() failed\n");
  4183. err = -ENOMEM;
  4184. goto err;
  4185. }
  4186. if (++i == priv->num_pairs)
  4187. break;
  4188. }
  4189. return 0;
  4190. err:
  4191. for_each_online_cpu(cpu) {
  4192. ppriv = per_cpu_ptr(priv->ppriv, cpu);
  4193. if (!ppriv->nctx.cb)
  4194. break;
  4195. dpaa2_io_service_deregister(ppriv->dpio, &ppriv->nctx, dev);
  4196. }
  4197. for_each_online_cpu(cpu) {
  4198. ppriv = per_cpu_ptr(priv->ppriv, cpu);
  4199. if (!ppriv->store)
  4200. break;
  4201. dpaa2_io_store_destroy(ppriv->store);
  4202. }
  4203. return err;
  4204. }
  4205. static void __cold dpaa2_dpseci_dpio_free(struct dpaa2_caam_priv *priv)
  4206. {
  4207. struct dpaa2_caam_priv_per_cpu *ppriv;
  4208. int i = 0, cpu;
  4209. for_each_online_cpu(cpu) {
  4210. ppriv = per_cpu_ptr(priv->ppriv, cpu);
  4211. dpaa2_io_service_deregister(ppriv->dpio, &ppriv->nctx,
  4212. priv->dev);
  4213. dpaa2_io_store_destroy(ppriv->store);
  4214. if (++i == priv->num_pairs)
  4215. return;
  4216. }
  4217. }
  4218. static int dpaa2_dpseci_bind(struct dpaa2_caam_priv *priv)
  4219. {
  4220. struct dpseci_rx_queue_cfg rx_queue_cfg;
  4221. struct device *dev = priv->dev;
  4222. struct fsl_mc_device *ls_dev = to_fsl_mc_device(dev);
  4223. struct dpaa2_caam_priv_per_cpu *ppriv;
  4224. int err = 0, i = 0, cpu;
  4225. /* Configure Rx queues */
  4226. for_each_online_cpu(cpu) {
  4227. ppriv = per_cpu_ptr(priv->ppriv, cpu);
  4228. rx_queue_cfg.options = DPSECI_QUEUE_OPT_DEST |
  4229. DPSECI_QUEUE_OPT_USER_CTX;
  4230. rx_queue_cfg.order_preservation_en = 0;
  4231. rx_queue_cfg.dest_cfg.dest_type = DPSECI_DEST_DPIO;
  4232. rx_queue_cfg.dest_cfg.dest_id = ppriv->nctx.dpio_id;
  4233. /*
  4234. * Rx priority (WQ) doesn't really matter, since we use
  4235. * pull mode, i.e. volatile dequeues from specific FQs
  4236. */
  4237. rx_queue_cfg.dest_cfg.priority = 0;
  4238. rx_queue_cfg.user_ctx = ppriv->nctx.qman64;
  4239. err = dpseci_set_rx_queue(priv->mc_io, 0, ls_dev->mc_handle, i,
  4240. &rx_queue_cfg);
  4241. if (err) {
  4242. dev_err(dev, "dpseci_set_rx_queue() failed with err %d\n",
  4243. err);
  4244. return err;
  4245. }
  4246. if (++i == priv->num_pairs)
  4247. break;
  4248. }
  4249. return err;
  4250. }
  4251. static void dpaa2_dpseci_congestion_free(struct dpaa2_caam_priv *priv)
  4252. {
  4253. struct device *dev = priv->dev;
  4254. if (!priv->cscn_mem)
  4255. return;
  4256. dma_unmap_single(dev, priv->cscn_dma, DPAA2_CSCN_SIZE, DMA_FROM_DEVICE);
  4257. kfree(priv->cscn_mem);
  4258. }
  4259. static void dpaa2_dpseci_free(struct dpaa2_caam_priv *priv)
  4260. {
  4261. struct device *dev = priv->dev;
  4262. struct fsl_mc_device *ls_dev = to_fsl_mc_device(dev);
  4263. dpaa2_dpseci_congestion_free(priv);
  4264. dpseci_close(priv->mc_io, 0, ls_dev->mc_handle);
  4265. }
  4266. static void dpaa2_caam_process_fd(struct dpaa2_caam_priv *priv,
  4267. const struct dpaa2_fd *fd)
  4268. {
  4269. struct caam_request *req;
  4270. u32 fd_err;
  4271. if (dpaa2_fd_get_format(fd) != dpaa2_fd_list) {
  4272. dev_err(priv->dev, "Only Frame List FD format is supported!\n");
  4273. return;
  4274. }
  4275. fd_err = dpaa2_fd_get_ctrl(fd) & FD_CTRL_ERR_MASK;
  4276. if (unlikely(fd_err))
  4277. dev_err_ratelimited(priv->dev, "FD error: %08x\n", fd_err);
  4278. /*
  4279. * FD[ADDR] is guaranteed to be valid, irrespective of errors reported
  4280. * in FD[ERR] or FD[FRC].
  4281. */
  4282. req = dpaa2_caam_iova_to_virt(priv, dpaa2_fd_get_addr(fd));
  4283. dma_unmap_single(priv->dev, req->fd_flt_dma, sizeof(req->fd_flt),
  4284. DMA_BIDIRECTIONAL);
  4285. req->cbk(req->ctx, dpaa2_fd_get_frc(fd));
  4286. }
  4287. static int dpaa2_caam_pull_fq(struct dpaa2_caam_priv_per_cpu *ppriv)
  4288. {
  4289. int err;
  4290. /* Retry while portal is busy */
  4291. do {
  4292. err = dpaa2_io_service_pull_fq(ppriv->dpio, ppriv->rsp_fqid,
  4293. ppriv->store);
  4294. } while (err == -EBUSY);
  4295. if (unlikely(err))
  4296. dev_err(ppriv->priv->dev, "dpaa2_io_service_pull err %d", err);
  4297. return err;
  4298. }
  4299. static int dpaa2_caam_store_consume(struct dpaa2_caam_priv_per_cpu *ppriv)
  4300. {
  4301. struct dpaa2_dq *dq;
  4302. int cleaned = 0, is_last;
  4303. do {
  4304. dq = dpaa2_io_store_next(ppriv->store, &is_last);
  4305. if (unlikely(!dq)) {
  4306. if (unlikely(!is_last)) {
  4307. dev_dbg(ppriv->priv->dev,
  4308. "FQ %d returned no valid frames\n",
  4309. ppriv->rsp_fqid);
  4310. /*
  4311. * MUST retry until we get some sort of
  4312. * valid response token (be it "empty dequeue"
  4313. * or a valid frame).
  4314. */
  4315. continue;
  4316. }
  4317. break;
  4318. }
  4319. /* Process FD */
  4320. dpaa2_caam_process_fd(ppriv->priv, dpaa2_dq_fd(dq));
  4321. cleaned++;
  4322. } while (!is_last);
  4323. return cleaned;
  4324. }
  4325. static int dpaa2_dpseci_poll(struct napi_struct *napi, int budget)
  4326. {
  4327. struct dpaa2_caam_priv_per_cpu *ppriv;
  4328. struct dpaa2_caam_priv *priv;
  4329. int err, cleaned = 0, store_cleaned;
  4330. ppriv = container_of(napi, struct dpaa2_caam_priv_per_cpu, napi);
  4331. priv = ppriv->priv;
  4332. if (unlikely(dpaa2_caam_pull_fq(ppriv)))
  4333. return 0;
  4334. do {
  4335. store_cleaned = dpaa2_caam_store_consume(ppriv);
  4336. cleaned += store_cleaned;
  4337. if (store_cleaned == 0 ||
  4338. cleaned > budget - DPAA2_CAAM_STORE_SIZE)
  4339. break;
  4340. /* Try to dequeue some more */
  4341. err = dpaa2_caam_pull_fq(ppriv);
  4342. if (unlikely(err))
  4343. break;
  4344. } while (1);
  4345. if (cleaned < budget) {
  4346. napi_complete_done(napi, cleaned);
  4347. err = dpaa2_io_service_rearm(ppriv->dpio, &ppriv->nctx);
  4348. if (unlikely(err))
  4349. dev_err(priv->dev, "Notification rearm failed: %d\n",
  4350. err);
  4351. }
  4352. return cleaned;
  4353. }
  4354. static int dpaa2_dpseci_congestion_setup(struct dpaa2_caam_priv *priv,
  4355. u16 token)
  4356. {
  4357. struct dpseci_congestion_notification_cfg cong_notif_cfg = { 0 };
  4358. struct device *dev = priv->dev;
  4359. int err;
  4360. /*
  4361. * Congestion group feature supported starting with DPSECI API v5.1
  4362. * and only when object has been created with this capability.
  4363. */
  4364. if ((DPSECI_VER(priv->major_ver, priv->minor_ver) < DPSECI_VER(5, 1)) ||
  4365. !(priv->dpseci_attr.options & DPSECI_OPT_HAS_CG))
  4366. return 0;
  4367. priv->cscn_mem = kzalloc(DPAA2_CSCN_SIZE + DPAA2_CSCN_ALIGN,
  4368. GFP_KERNEL | GFP_DMA);
  4369. if (!priv->cscn_mem)
  4370. return -ENOMEM;
  4371. priv->cscn_mem_aligned = PTR_ALIGN(priv->cscn_mem, DPAA2_CSCN_ALIGN);
  4372. priv->cscn_dma = dma_map_single(dev, priv->cscn_mem_aligned,
  4373. DPAA2_CSCN_SIZE, DMA_FROM_DEVICE);
  4374. if (dma_mapping_error(dev, priv->cscn_dma)) {
  4375. dev_err(dev, "Error mapping CSCN memory area\n");
  4376. err = -ENOMEM;
  4377. goto err_dma_map;
  4378. }
  4379. cong_notif_cfg.units = DPSECI_CONGESTION_UNIT_BYTES;
  4380. cong_notif_cfg.threshold_entry = DPAA2_SEC_CONG_ENTRY_THRESH;
  4381. cong_notif_cfg.threshold_exit = DPAA2_SEC_CONG_EXIT_THRESH;
  4382. cong_notif_cfg.message_ctx = (uintptr_t)priv;
  4383. cong_notif_cfg.message_iova = priv->cscn_dma;
  4384. cong_notif_cfg.notification_mode = DPSECI_CGN_MODE_WRITE_MEM_ON_ENTER |
  4385. DPSECI_CGN_MODE_WRITE_MEM_ON_EXIT |
  4386. DPSECI_CGN_MODE_COHERENT_WRITE;
  4387. err = dpseci_set_congestion_notification(priv->mc_io, 0, token,
  4388. &cong_notif_cfg);
  4389. if (err) {
  4390. dev_err(dev, "dpseci_set_congestion_notification failed\n");
  4391. goto err_set_cong;
  4392. }
  4393. return 0;
  4394. err_set_cong:
  4395. dma_unmap_single(dev, priv->cscn_dma, DPAA2_CSCN_SIZE, DMA_FROM_DEVICE);
  4396. err_dma_map:
  4397. kfree(priv->cscn_mem);
  4398. return err;
  4399. }
  4400. static int __cold dpaa2_dpseci_setup(struct fsl_mc_device *ls_dev)
  4401. {
  4402. struct device *dev = &ls_dev->dev;
  4403. struct dpaa2_caam_priv *priv;
  4404. struct dpaa2_caam_priv_per_cpu *ppriv;
  4405. int err, cpu;
  4406. u8 i;
  4407. priv = dev_get_drvdata(dev);
  4408. priv->dev = dev;
  4409. priv->dpsec_id = ls_dev->obj_desc.id;
  4410. /* Get a handle for the DPSECI this interface is associate with */
  4411. err = dpseci_open(priv->mc_io, 0, priv->dpsec_id, &ls_dev->mc_handle);
  4412. if (err) {
  4413. dev_err(dev, "dpseci_open() failed: %d\n", err);
  4414. goto err_open;
  4415. }
  4416. err = dpseci_get_api_version(priv->mc_io, 0, &priv->major_ver,
  4417. &priv->minor_ver);
  4418. if (err) {
  4419. dev_err(dev, "dpseci_get_api_version() failed\n");
  4420. goto err_get_vers;
  4421. }
  4422. dev_info(dev, "dpseci v%d.%d\n", priv->major_ver, priv->minor_ver);
  4423. err = dpseci_get_attributes(priv->mc_io, 0, ls_dev->mc_handle,
  4424. &priv->dpseci_attr);
  4425. if (err) {
  4426. dev_err(dev, "dpseci_get_attributes() failed\n");
  4427. goto err_get_vers;
  4428. }
  4429. err = dpseci_get_sec_attr(priv->mc_io, 0, ls_dev->mc_handle,
  4430. &priv->sec_attr);
  4431. if (err) {
  4432. dev_err(dev, "dpseci_get_sec_attr() failed\n");
  4433. goto err_get_vers;
  4434. }
  4435. err = dpaa2_dpseci_congestion_setup(priv, ls_dev->mc_handle);
  4436. if (err) {
  4437. dev_err(dev, "setup_congestion() failed\n");
  4438. goto err_get_vers;
  4439. }
  4440. priv->num_pairs = min(priv->dpseci_attr.num_rx_queues,
  4441. priv->dpseci_attr.num_tx_queues);
  4442. if (priv->num_pairs > num_online_cpus()) {
  4443. dev_warn(dev, "%d queues won't be used\n",
  4444. priv->num_pairs - num_online_cpus());
  4445. priv->num_pairs = num_online_cpus();
  4446. }
  4447. for (i = 0; i < priv->dpseci_attr.num_rx_queues; i++) {
  4448. err = dpseci_get_rx_queue(priv->mc_io, 0, ls_dev->mc_handle, i,
  4449. &priv->rx_queue_attr[i]);
  4450. if (err) {
  4451. dev_err(dev, "dpseci_get_rx_queue() failed\n");
  4452. goto err_get_rx_queue;
  4453. }
  4454. }
  4455. for (i = 0; i < priv->dpseci_attr.num_tx_queues; i++) {
  4456. err = dpseci_get_tx_queue(priv->mc_io, 0, ls_dev->mc_handle, i,
  4457. &priv->tx_queue_attr[i]);
  4458. if (err) {
  4459. dev_err(dev, "dpseci_get_tx_queue() failed\n");
  4460. goto err_get_rx_queue;
  4461. }
  4462. }
  4463. i = 0;
  4464. for_each_online_cpu(cpu) {
  4465. u8 j;
  4466. j = i % priv->num_pairs;
  4467. ppriv = per_cpu_ptr(priv->ppriv, cpu);
  4468. ppriv->req_fqid = priv->tx_queue_attr[j].fqid;
  4469. /*
  4470. * Allow all cores to enqueue, while only some of them
  4471. * will take part in dequeuing.
  4472. */
  4473. if (++i > priv->num_pairs)
  4474. continue;
  4475. ppriv->rsp_fqid = priv->rx_queue_attr[j].fqid;
  4476. ppriv->prio = j;
  4477. dev_dbg(dev, "pair %d: rx queue %d, tx queue %d\n", j,
  4478. priv->rx_queue_attr[j].fqid,
  4479. priv->tx_queue_attr[j].fqid);
  4480. ppriv->net_dev.dev = *dev;
  4481. INIT_LIST_HEAD(&ppriv->net_dev.napi_list);
  4482. netif_napi_add(&ppriv->net_dev, &ppriv->napi, dpaa2_dpseci_poll,
  4483. DPAA2_CAAM_NAPI_WEIGHT);
  4484. }
  4485. return 0;
  4486. err_get_rx_queue:
  4487. dpaa2_dpseci_congestion_free(priv);
  4488. err_get_vers:
  4489. dpseci_close(priv->mc_io, 0, ls_dev->mc_handle);
  4490. err_open:
  4491. return err;
  4492. }
  4493. static int dpaa2_dpseci_enable(struct dpaa2_caam_priv *priv)
  4494. {
  4495. struct device *dev = priv->dev;
  4496. struct fsl_mc_device *ls_dev = to_fsl_mc_device(dev);
  4497. struct dpaa2_caam_priv_per_cpu *ppriv;
  4498. int i;
  4499. for (i = 0; i < priv->num_pairs; i++) {
  4500. ppriv = per_cpu_ptr(priv->ppriv, i);
  4501. napi_enable(&ppriv->napi);
  4502. }
  4503. return dpseci_enable(priv->mc_io, 0, ls_dev->mc_handle);
  4504. }
  4505. static int __cold dpaa2_dpseci_disable(struct dpaa2_caam_priv *priv)
  4506. {
  4507. struct device *dev = priv->dev;
  4508. struct dpaa2_caam_priv_per_cpu *ppriv;
  4509. struct fsl_mc_device *ls_dev = to_fsl_mc_device(dev);
  4510. int i, err = 0, enabled;
  4511. err = dpseci_disable(priv->mc_io, 0, ls_dev->mc_handle);
  4512. if (err) {
  4513. dev_err(dev, "dpseci_disable() failed\n");
  4514. return err;
  4515. }
  4516. err = dpseci_is_enabled(priv->mc_io, 0, ls_dev->mc_handle, &enabled);
  4517. if (err) {
  4518. dev_err(dev, "dpseci_is_enabled() failed\n");
  4519. return err;
  4520. }
  4521. dev_dbg(dev, "disable: %s\n", enabled ? "false" : "true");
  4522. for (i = 0; i < priv->num_pairs; i++) {
  4523. ppriv = per_cpu_ptr(priv->ppriv, i);
  4524. napi_disable(&ppriv->napi);
  4525. netif_napi_del(&ppriv->napi);
  4526. }
  4527. return 0;
  4528. }
  4529. static struct list_head hash_list;
  4530. static int dpaa2_caam_probe(struct fsl_mc_device *dpseci_dev)
  4531. {
  4532. struct device *dev;
  4533. struct dpaa2_caam_priv *priv;
  4534. int i, err = 0;
  4535. bool registered = false;
  4536. /*
  4537. * There is no way to get CAAM endianness - there is no direct register
  4538. * space access and MC f/w does not provide this attribute.
  4539. * All DPAA2-based SoCs have little endian CAAM, thus hard-code this
  4540. * property.
  4541. */
  4542. caam_little_end = true;
  4543. caam_imx = false;
  4544. dev = &dpseci_dev->dev;
  4545. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  4546. if (!priv)
  4547. return -ENOMEM;
  4548. dev_set_drvdata(dev, priv);
  4549. priv->domain = iommu_get_domain_for_dev(dev);
  4550. qi_cache = kmem_cache_create("dpaa2_caamqicache", CAAM_QI_MEMCACHE_SIZE,
  4551. 0, SLAB_CACHE_DMA, NULL);
  4552. if (!qi_cache) {
  4553. dev_err(dev, "Can't allocate SEC cache\n");
  4554. return -ENOMEM;
  4555. }
  4556. err = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(49));
  4557. if (err) {
  4558. dev_err(dev, "dma_set_mask_and_coherent() failed\n");
  4559. goto err_dma_mask;
  4560. }
  4561. /* Obtain a MC portal */
  4562. err = fsl_mc_portal_allocate(dpseci_dev, 0, &priv->mc_io);
  4563. if (err) {
  4564. if (err == -ENXIO)
  4565. err = -EPROBE_DEFER;
  4566. else
  4567. dev_err(dev, "MC portal allocation failed\n");
  4568. goto err_dma_mask;
  4569. }
  4570. priv->ppriv = alloc_percpu(*priv->ppriv);
  4571. if (!priv->ppriv) {
  4572. dev_err(dev, "alloc_percpu() failed\n");
  4573. err = -ENOMEM;
  4574. goto err_alloc_ppriv;
  4575. }
  4576. /* DPSECI initialization */
  4577. err = dpaa2_dpseci_setup(dpseci_dev);
  4578. if (err) {
  4579. dev_err(dev, "dpaa2_dpseci_setup() failed\n");
  4580. goto err_dpseci_setup;
  4581. }
  4582. /* DPIO */
  4583. err = dpaa2_dpseci_dpio_setup(priv);
  4584. if (err) {
  4585. if (err != -EPROBE_DEFER)
  4586. dev_err(dev, "dpaa2_dpseci_dpio_setup() failed\n");
  4587. goto err_dpio_setup;
  4588. }
  4589. /* DPSECI binding to DPIO */
  4590. err = dpaa2_dpseci_bind(priv);
  4591. if (err) {
  4592. dev_err(dev, "dpaa2_dpseci_bind() failed\n");
  4593. goto err_bind;
  4594. }
  4595. /* DPSECI enable */
  4596. err = dpaa2_dpseci_enable(priv);
  4597. if (err) {
  4598. dev_err(dev, "dpaa2_dpseci_enable() failed\n");
  4599. goto err_bind;
  4600. }
  4601. dpaa2_dpseci_debugfs_init(priv);
  4602. /* register crypto algorithms the device supports */
  4603. for (i = 0; i < ARRAY_SIZE(driver_algs); i++) {
  4604. struct caam_skcipher_alg *t_alg = driver_algs + i;
  4605. u32 alg_sel = t_alg->caam.class1_alg_type & OP_ALG_ALGSEL_MASK;
  4606. /* Skip DES algorithms if not supported by device */
  4607. if (!priv->sec_attr.des_acc_num &&
  4608. (alg_sel == OP_ALG_ALGSEL_3DES ||
  4609. alg_sel == OP_ALG_ALGSEL_DES))
  4610. continue;
  4611. /* Skip AES algorithms if not supported by device */
  4612. if (!priv->sec_attr.aes_acc_num &&
  4613. alg_sel == OP_ALG_ALGSEL_AES)
  4614. continue;
  4615. /* Skip CHACHA20 algorithms if not supported by device */
  4616. if (alg_sel == OP_ALG_ALGSEL_CHACHA20 &&
  4617. !priv->sec_attr.ccha_acc_num)
  4618. continue;
  4619. t_alg->caam.dev = dev;
  4620. caam_skcipher_alg_init(t_alg);
  4621. err = crypto_register_skcipher(&t_alg->skcipher);
  4622. if (err) {
  4623. dev_warn(dev, "%s alg registration failed: %d\n",
  4624. t_alg->skcipher.base.cra_driver_name, err);
  4625. continue;
  4626. }
  4627. t_alg->registered = true;
  4628. registered = true;
  4629. }
  4630. for (i = 0; i < ARRAY_SIZE(driver_aeads); i++) {
  4631. struct caam_aead_alg *t_alg = driver_aeads + i;
  4632. u32 c1_alg_sel = t_alg->caam.class1_alg_type &
  4633. OP_ALG_ALGSEL_MASK;
  4634. u32 c2_alg_sel = t_alg->caam.class2_alg_type &
  4635. OP_ALG_ALGSEL_MASK;
  4636. /* Skip DES algorithms if not supported by device */
  4637. if (!priv->sec_attr.des_acc_num &&
  4638. (c1_alg_sel == OP_ALG_ALGSEL_3DES ||
  4639. c1_alg_sel == OP_ALG_ALGSEL_DES))
  4640. continue;
  4641. /* Skip AES algorithms if not supported by device */
  4642. if (!priv->sec_attr.aes_acc_num &&
  4643. c1_alg_sel == OP_ALG_ALGSEL_AES)
  4644. continue;
  4645. /* Skip CHACHA20 algorithms if not supported by device */
  4646. if (c1_alg_sel == OP_ALG_ALGSEL_CHACHA20 &&
  4647. !priv->sec_attr.ccha_acc_num)
  4648. continue;
  4649. /* Skip POLY1305 algorithms if not supported by device */
  4650. if (c2_alg_sel == OP_ALG_ALGSEL_POLY1305 &&
  4651. !priv->sec_attr.ptha_acc_num)
  4652. continue;
  4653. /*
  4654. * Skip algorithms requiring message digests
  4655. * if MD not supported by device.
  4656. */
  4657. if ((c2_alg_sel & ~OP_ALG_ALGSEL_SUBMASK) == 0x40 &&
  4658. !priv->sec_attr.md_acc_num)
  4659. continue;
  4660. t_alg->caam.dev = dev;
  4661. caam_aead_alg_init(t_alg);
  4662. err = crypto_register_aead(&t_alg->aead);
  4663. if (err) {
  4664. dev_warn(dev, "%s alg registration failed: %d\n",
  4665. t_alg->aead.base.cra_driver_name, err);
  4666. continue;
  4667. }
  4668. t_alg->registered = true;
  4669. registered = true;
  4670. }
  4671. if (registered)
  4672. dev_info(dev, "algorithms registered in /proc/crypto\n");
  4673. /* register hash algorithms the device supports */
  4674. INIT_LIST_HEAD(&hash_list);
  4675. /*
  4676. * Skip registration of any hashing algorithms if MD block
  4677. * is not present.
  4678. */
  4679. if (!priv->sec_attr.md_acc_num)
  4680. return 0;
  4681. for (i = 0; i < ARRAY_SIZE(driver_hash); i++) {
  4682. struct caam_hash_alg *t_alg;
  4683. struct caam_hash_template *alg = driver_hash + i;
  4684. /* register hmac version */
  4685. t_alg = caam_hash_alloc(dev, alg, true);
  4686. if (IS_ERR(t_alg)) {
  4687. err = PTR_ERR(t_alg);
  4688. dev_warn(dev, "%s hash alg allocation failed: %d\n",
  4689. alg->driver_name, err);
  4690. continue;
  4691. }
  4692. err = crypto_register_ahash(&t_alg->ahash_alg);
  4693. if (err) {
  4694. dev_warn(dev, "%s alg registration failed: %d\n",
  4695. t_alg->ahash_alg.halg.base.cra_driver_name,
  4696. err);
  4697. kfree(t_alg);
  4698. } else {
  4699. list_add_tail(&t_alg->entry, &hash_list);
  4700. }
  4701. /* register unkeyed version */
  4702. t_alg = caam_hash_alloc(dev, alg, false);
  4703. if (IS_ERR(t_alg)) {
  4704. err = PTR_ERR(t_alg);
  4705. dev_warn(dev, "%s alg allocation failed: %d\n",
  4706. alg->driver_name, err);
  4707. continue;
  4708. }
  4709. err = crypto_register_ahash(&t_alg->ahash_alg);
  4710. if (err) {
  4711. dev_warn(dev, "%s alg registration failed: %d\n",
  4712. t_alg->ahash_alg.halg.base.cra_driver_name,
  4713. err);
  4714. kfree(t_alg);
  4715. } else {
  4716. list_add_tail(&t_alg->entry, &hash_list);
  4717. }
  4718. }
  4719. if (!list_empty(&hash_list))
  4720. dev_info(dev, "hash algorithms registered in /proc/crypto\n");
  4721. return err;
  4722. err_bind:
  4723. dpaa2_dpseci_dpio_free(priv);
  4724. err_dpio_setup:
  4725. dpaa2_dpseci_free(priv);
  4726. err_dpseci_setup:
  4727. free_percpu(priv->ppriv);
  4728. err_alloc_ppriv:
  4729. fsl_mc_portal_free(priv->mc_io);
  4730. err_dma_mask:
  4731. kmem_cache_destroy(qi_cache);
  4732. return err;
  4733. }
  4734. static int __cold dpaa2_caam_remove(struct fsl_mc_device *ls_dev)
  4735. {
  4736. struct device *dev;
  4737. struct dpaa2_caam_priv *priv;
  4738. int i;
  4739. dev = &ls_dev->dev;
  4740. priv = dev_get_drvdata(dev);
  4741. dpaa2_dpseci_debugfs_exit(priv);
  4742. for (i = 0; i < ARRAY_SIZE(driver_aeads); i++) {
  4743. struct caam_aead_alg *t_alg = driver_aeads + i;
  4744. if (t_alg->registered)
  4745. crypto_unregister_aead(&t_alg->aead);
  4746. }
  4747. for (i = 0; i < ARRAY_SIZE(driver_algs); i++) {
  4748. struct caam_skcipher_alg *t_alg = driver_algs + i;
  4749. if (t_alg->registered)
  4750. crypto_unregister_skcipher(&t_alg->skcipher);
  4751. }
  4752. if (hash_list.next) {
  4753. struct caam_hash_alg *t_hash_alg, *p;
  4754. list_for_each_entry_safe(t_hash_alg, p, &hash_list, entry) {
  4755. crypto_unregister_ahash(&t_hash_alg->ahash_alg);
  4756. list_del(&t_hash_alg->entry);
  4757. kfree(t_hash_alg);
  4758. }
  4759. }
  4760. dpaa2_dpseci_disable(priv);
  4761. dpaa2_dpseci_dpio_free(priv);
  4762. dpaa2_dpseci_free(priv);
  4763. free_percpu(priv->ppriv);
  4764. fsl_mc_portal_free(priv->mc_io);
  4765. kmem_cache_destroy(qi_cache);
  4766. return 0;
  4767. }
  4768. int dpaa2_caam_enqueue(struct device *dev, struct caam_request *req)
  4769. {
  4770. struct dpaa2_fd fd;
  4771. struct dpaa2_caam_priv *priv = dev_get_drvdata(dev);
  4772. struct dpaa2_caam_priv_per_cpu *ppriv;
  4773. int err = 0, i;
  4774. if (IS_ERR(req))
  4775. return PTR_ERR(req);
  4776. if (priv->cscn_mem) {
  4777. dma_sync_single_for_cpu(priv->dev, priv->cscn_dma,
  4778. DPAA2_CSCN_SIZE,
  4779. DMA_FROM_DEVICE);
  4780. if (unlikely(dpaa2_cscn_state_congested(priv->cscn_mem_aligned))) {
  4781. dev_dbg_ratelimited(dev, "Dropping request\n");
  4782. return -EBUSY;
  4783. }
  4784. }
  4785. dpaa2_fl_set_flc(&req->fd_flt[1], req->flc_dma);
  4786. req->fd_flt_dma = dma_map_single(dev, req->fd_flt, sizeof(req->fd_flt),
  4787. DMA_BIDIRECTIONAL);
  4788. if (dma_mapping_error(dev, req->fd_flt_dma)) {
  4789. dev_err(dev, "DMA mapping error for QI enqueue request\n");
  4790. goto err_out;
  4791. }
  4792. memset(&fd, 0, sizeof(fd));
  4793. dpaa2_fd_set_format(&fd, dpaa2_fd_list);
  4794. dpaa2_fd_set_addr(&fd, req->fd_flt_dma);
  4795. dpaa2_fd_set_len(&fd, dpaa2_fl_get_len(&req->fd_flt[1]));
  4796. dpaa2_fd_set_flc(&fd, req->flc_dma);
  4797. ppriv = this_cpu_ptr(priv->ppriv);
  4798. for (i = 0; i < (priv->dpseci_attr.num_tx_queues << 1); i++) {
  4799. err = dpaa2_io_service_enqueue_fq(ppriv->dpio, ppriv->req_fqid,
  4800. &fd);
  4801. if (err != -EBUSY)
  4802. break;
  4803. cpu_relax();
  4804. }
  4805. if (unlikely(err)) {
  4806. dev_err_ratelimited(dev, "Error enqueuing frame: %d\n", err);
  4807. goto err_out;
  4808. }
  4809. return -EINPROGRESS;
  4810. err_out:
  4811. dma_unmap_single(dev, req->fd_flt_dma, sizeof(req->fd_flt),
  4812. DMA_BIDIRECTIONAL);
  4813. return -EIO;
  4814. }
  4815. EXPORT_SYMBOL(dpaa2_caam_enqueue);
  4816. static const struct fsl_mc_device_id dpaa2_caam_match_id_table[] = {
  4817. {
  4818. .vendor = FSL_MC_VENDOR_FREESCALE,
  4819. .obj_type = "dpseci",
  4820. },
  4821. { .vendor = 0x0 }
  4822. };
  4823. static struct fsl_mc_driver dpaa2_caam_driver = {
  4824. .driver = {
  4825. .name = KBUILD_MODNAME,
  4826. .owner = THIS_MODULE,
  4827. },
  4828. .probe = dpaa2_caam_probe,
  4829. .remove = dpaa2_caam_remove,
  4830. .match_id_table = dpaa2_caam_match_id_table
  4831. };
  4832. MODULE_LICENSE("Dual BSD/GPL");
  4833. MODULE_AUTHOR("Freescale Semiconductor, Inc");
  4834. MODULE_DESCRIPTION("Freescale DPAA2 CAAM Driver");
  4835. module_fsl_mc_driver(dpaa2_caam_driver);