qcrypto.c 148 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * QTI Crypto driver
  4. *
  5. * Copyright (c) 2010-2021, The Linux Foundation. All rights reserved.
  6. */
  7. #include <linux/module.h>
  8. #include <linux/device.h>
  9. #include <linux/mod_devicetable.h>
  10. #include <linux/clk.h>
  11. #include <linux/cpu.h>
  12. #include <linux/types.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/dma-mapping.h>
  15. #include <linux/dmapool.h>
  16. #include <linux/crypto.h>
  17. #include <linux/kernel.h>
  18. #include <linux/rtnetlink.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/spinlock.h>
  21. #include <linux/llist.h>
  22. #include <linux/debugfs.h>
  23. #include <linux/workqueue.h>
  24. #include <linux/sched.h>
  25. #include <linux/init.h>
  26. #include <linux/cache.h>
  27. #include "linux/platform_data/qcom_crypto_device.h"
  28. #include <linux/interconnect.h>
  29. #include <linux/hardirq.h>
  30. #include "linux/qcrypto.h"
  31. #include <crypto/ctr.h>
  32. #include <crypto/des.h>
  33. #include <crypto/aes.h>
  34. #include <crypto/sha1.h>
  35. #include <crypto/sha2.h>
  36. #include <crypto/hash.h>
  37. #include <crypto/algapi.h>
  38. #include <crypto/aead.h>
  39. #include <crypto/authenc.h>
  40. #include <crypto/scatterwalk.h>
  41. #include <crypto/skcipher.h>
  42. #include <crypto/internal/skcipher.h>
  43. #include <crypto/internal/hash.h>
  44. #include <crypto/internal/aead.h>
  45. #include "linux/fips_status.h"
  46. #include "qce.h"
  47. #define DEBUG_MAX_FNAME 16
  48. #define DEBUG_MAX_RW_BUF 4096
  49. #define QCRYPTO_BIG_NUMBER 9999999 /* a big number */
  50. /*
  51. * For crypto 5.0 which has burst size alignment requirement.
  52. */
  53. #define MAX_ALIGN_SIZE 0x40
  54. #define QCRYPTO_HIGH_BANDWIDTH_TIMEOUT 1000
  55. /* Status of response workq */
  56. enum resp_workq_sts {
  57. NOT_SCHEDULED = 0,
  58. IS_SCHEDULED = 1,
  59. SCHEDULE_AGAIN = 2
  60. };
  61. /* Status of req processing by CEs */
  62. enum req_processing_sts {
  63. STOPPED = 0,
  64. IN_PROGRESS = 1
  65. };
  66. enum qcrypto_bus_state {
  67. BUS_NO_BANDWIDTH = 0,
  68. BUS_HAS_BANDWIDTH,
  69. BUS_BANDWIDTH_RELEASING,
  70. BUS_BANDWIDTH_ALLOCATING,
  71. BUS_SUSPENDED,
  72. BUS_SUSPENDING,
  73. };
  74. struct crypto_stat {
  75. u64 aead_sha1_aes_enc;
  76. u64 aead_sha1_aes_dec;
  77. u64 aead_sha1_des_enc;
  78. u64 aead_sha1_des_dec;
  79. u64 aead_sha1_3des_enc;
  80. u64 aead_sha1_3des_dec;
  81. u64 aead_sha256_aes_enc;
  82. u64 aead_sha256_aes_dec;
  83. u64 aead_sha256_des_enc;
  84. u64 aead_sha256_des_dec;
  85. u64 aead_sha256_3des_enc;
  86. u64 aead_sha256_3des_dec;
  87. u64 aead_ccm_aes_enc;
  88. u64 aead_ccm_aes_dec;
  89. u64 aead_rfc4309_ccm_aes_enc;
  90. u64 aead_rfc4309_ccm_aes_dec;
  91. u64 aead_op_success;
  92. u64 aead_op_fail;
  93. u64 aead_bad_msg;
  94. u64 sk_cipher_aes_enc;
  95. u64 sk_cipher_aes_dec;
  96. u64 sk_cipher_des_enc;
  97. u64 sk_cipher_des_dec;
  98. u64 sk_cipher_3des_enc;
  99. u64 sk_cipher_3des_dec;
  100. u64 sk_cipher_op_success;
  101. u64 sk_cipher_op_fail;
  102. u64 sha1_digest;
  103. u64 sha256_digest;
  104. u64 sha1_hmac_digest;
  105. u64 sha256_hmac_digest;
  106. u64 ahash_op_success;
  107. u64 ahash_op_fail;
  108. };
  109. static struct crypto_stat _qcrypto_stat;
  110. static struct dentry *_debug_dent;
  111. static char _debug_read_buf[DEBUG_MAX_RW_BUF];
  112. static bool _qcrypto_init_assign;
  113. struct crypto_priv;
  114. struct qcrypto_req_control {
  115. unsigned int index;
  116. bool in_use;
  117. struct crypto_engine *pce;
  118. struct crypto_async_request *req;
  119. struct qcrypto_resp_ctx *arsp;
  120. int res; /* execution result */
  121. };
  122. struct crypto_engine {
  123. struct list_head elist;
  124. void *qce; /* qce handle */
  125. struct platform_device *pdev; /* platform device */
  126. struct crypto_priv *pcp;
  127. struct icc_path *icc_path;
  128. struct crypto_queue req_queue; /*
  129. * request queue for those requests
  130. * that have this engine assigned
  131. * waiting to be executed
  132. */
  133. u64 total_req;
  134. u64 err_req;
  135. u32 unit;
  136. u32 ce_device;
  137. u32 ce_hw_instance;
  138. unsigned int signature;
  139. enum qcrypto_bus_state bw_state;
  140. bool high_bw_req;
  141. struct timer_list bw_reaper_timer;
  142. struct work_struct bw_reaper_ws;
  143. struct work_struct bw_allocate_ws;
  144. /* engine execution sequence number */
  145. u32 active_seq;
  146. /* last QCRYPTO_HIGH_BANDWIDTH_TIMEOUT active_seq */
  147. u32 last_active_seq;
  148. bool check_flag;
  149. /*Added to support multi-requests*/
  150. unsigned int max_req;
  151. struct qcrypto_req_control *preq_pool;
  152. atomic_t req_count;
  153. bool issue_req; /* an request is being issued to qce */
  154. bool first_engine; /* this engine is the first engine or not */
  155. unsigned int irq_cpu; /* the cpu running the irq of this engine */
  156. unsigned int max_req_used; /* debug stats */
  157. };
  158. #define MAX_SMP_CPU 8
  159. struct crypto_priv {
  160. /* CE features supported by target device*/
  161. struct msm_ce_hw_support platform_support;
  162. /* CE features/algorithms supported by HW engine*/
  163. struct ce_hw_support ce_support;
  164. /* the lock protects crypto queue and req */
  165. spinlock_t lock;
  166. /* list of registered algorithms */
  167. struct list_head alg_list;
  168. /* current active request */
  169. struct crypto_async_request *req;
  170. struct work_struct unlock_ce_ws;
  171. struct list_head engine_list; /* list of qcrypto engines */
  172. int32_t total_units; /* total units of engines */
  173. struct mutex engine_lock;
  174. struct crypto_engine *next_engine; /* next assign engine */
  175. struct crypto_queue req_queue; /*
  176. * request queue for those requests
  177. * that waiting for an available
  178. * engine.
  179. */
  180. struct llist_head ordered_resp_list; /* Queue to maintain
  181. * responses in sequence.
  182. */
  183. atomic_t resp_cnt;
  184. struct workqueue_struct *resp_wq;
  185. struct work_struct resp_work; /*
  186. * Workq to send responses
  187. * in sequence.
  188. */
  189. enum resp_workq_sts sched_resp_workq_status;
  190. enum req_processing_sts ce_req_proc_sts;
  191. int cpu_getting_irqs_frm_first_ce;
  192. struct crypto_engine *first_engine;
  193. struct crypto_engine *scheduled_eng; /* last engine scheduled */
  194. /* debug stats */
  195. unsigned int no_avail;
  196. unsigned int resp_stop;
  197. unsigned int resp_start;
  198. unsigned int max_qlen;
  199. unsigned int queue_work_eng3;
  200. unsigned int queue_work_not_eng3;
  201. unsigned int queue_work_not_eng3_nz;
  202. unsigned int max_resp_qlen;
  203. unsigned int max_reorder_cnt;
  204. unsigned int cpu_req[MAX_SMP_CPU+1];
  205. };
  206. static struct crypto_priv qcrypto_dev;
  207. static struct crypto_engine *_qcrypto_static_assign_engine(
  208. struct crypto_priv *cp);
  209. static struct crypto_engine *_avail_eng(struct crypto_priv *cp);
  210. static struct qcrypto_req_control *qcrypto_alloc_req_control(
  211. struct crypto_engine *pce)
  212. {
  213. int i;
  214. struct qcrypto_req_control *pqcrypto_req_control = pce->preq_pool;
  215. unsigned int req_count;
  216. for (i = 0; i < pce->max_req; i++) {
  217. if (!xchg(&pqcrypto_req_control->in_use, true)) {
  218. req_count = atomic_inc_return(&pce->req_count);
  219. if (req_count > pce->max_req_used)
  220. pce->max_req_used = req_count;
  221. return pqcrypto_req_control;
  222. }
  223. pqcrypto_req_control++;
  224. }
  225. return NULL;
  226. }
  227. static void qcrypto_free_req_control(struct crypto_engine *pce,
  228. struct qcrypto_req_control *preq)
  229. {
  230. /* do this before free req */
  231. preq->req = NULL;
  232. preq->arsp = NULL;
  233. /* free req */
  234. if (!xchg(&preq->in_use, false))
  235. pr_warn("request info %pK free already\n", preq);
  236. else
  237. atomic_dec(&pce->req_count);
  238. }
  239. static struct qcrypto_req_control *find_req_control_for_areq(
  240. struct crypto_engine *pce,
  241. struct crypto_async_request *areq)
  242. {
  243. int i;
  244. struct qcrypto_req_control *pqcrypto_req_control = pce->preq_pool;
  245. for (i = 0; i < pce->max_req; i++) {
  246. if (pqcrypto_req_control->req == areq)
  247. return pqcrypto_req_control;
  248. pqcrypto_req_control++;
  249. }
  250. return NULL;
  251. }
  252. static void qcrypto_init_req_control(struct crypto_engine *pce,
  253. struct qcrypto_req_control *pqcrypto_req_control)
  254. {
  255. int i;
  256. pce->preq_pool = pqcrypto_req_control;
  257. atomic_set(&pce->req_count, 0);
  258. for (i = 0; i < pce->max_req; i++) {
  259. pqcrypto_req_control->index = i;
  260. pqcrypto_req_control->in_use = false;
  261. pqcrypto_req_control->pce = pce;
  262. pqcrypto_req_control++;
  263. }
  264. }
  265. static struct crypto_engine *_qrypto_find_pengine_device(struct crypto_priv *cp,
  266. unsigned int device)
  267. {
  268. struct crypto_engine *entry = NULL;
  269. unsigned long flags;
  270. spin_lock_irqsave(&cp->lock, flags);
  271. list_for_each_entry(entry, &cp->engine_list, elist) {
  272. if (entry->ce_device == device)
  273. break;
  274. }
  275. spin_unlock_irqrestore(&cp->lock, flags);
  276. if (((entry != NULL) && (entry->ce_device != device)) ||
  277. (entry == NULL)) {
  278. pr_err("Device node for CE device %d NOT FOUND!!\n",
  279. device);
  280. return NULL;
  281. }
  282. return entry;
  283. }
  284. static struct crypto_engine *_qrypto_find_pengine_device_hw
  285. (struct crypto_priv *cp,
  286. u32 device,
  287. u32 hw_instance)
  288. {
  289. struct crypto_engine *entry = NULL;
  290. unsigned long flags;
  291. spin_lock_irqsave(&cp->lock, flags);
  292. list_for_each_entry(entry, &cp->engine_list, elist) {
  293. if ((entry->ce_device == device) &&
  294. (entry->ce_hw_instance == hw_instance))
  295. break;
  296. }
  297. spin_unlock_irqrestore(&cp->lock, flags);
  298. if (((entry != NULL) &&
  299. ((entry->ce_device != device)
  300. || (entry->ce_hw_instance != hw_instance)))
  301. || (entry == NULL)) {
  302. pr_err("Device node for CE device %d NOT FOUND!!\n",
  303. device);
  304. return NULL;
  305. }
  306. return entry;
  307. }
  308. int qcrypto_get_num_engines(void)
  309. {
  310. struct crypto_priv *cp = &qcrypto_dev;
  311. struct crypto_engine *entry = NULL;
  312. int count = 0;
  313. list_for_each_entry(entry, &cp->engine_list, elist) {
  314. count++;
  315. }
  316. return count;
  317. }
  318. EXPORT_SYMBOL(qcrypto_get_num_engines);
  319. void qcrypto_get_engine_list(size_t num_engines,
  320. struct crypto_engine_entry *arr)
  321. {
  322. struct crypto_priv *cp = &qcrypto_dev;
  323. struct crypto_engine *entry = NULL;
  324. size_t arr_index = 0;
  325. list_for_each_entry(entry, &cp->engine_list, elist) {
  326. arr[arr_index].ce_device = entry->ce_device;
  327. arr[arr_index].hw_instance = entry->ce_hw_instance;
  328. arr_index++;
  329. if (arr_index >= num_engines)
  330. break;
  331. }
  332. }
  333. EXPORT_SYMBOL(qcrypto_get_engine_list);
  334. enum qcrypto_alg_type {
  335. QCRYPTO_ALG_CIPHER = 0,
  336. QCRYPTO_ALG_SHA = 1,
  337. QCRYPTO_ALG_AEAD = 2,
  338. QCRYPTO_ALG_LAST
  339. };
  340. struct qcrypto_alg {
  341. struct list_head entry;
  342. struct skcipher_alg cipher_alg;
  343. struct ahash_alg sha_alg;
  344. struct aead_alg aead_alg;
  345. enum qcrypto_alg_type alg_type;
  346. struct crypto_priv *cp;
  347. };
  348. #define QCRYPTO_MAX_KEY_SIZE 64
  349. /* max of AES_BLOCK_SIZE, DES3_EDE_BLOCK_SIZE */
  350. #define QCRYPTO_MAX_IV_LENGTH 16
  351. #define QCRYPTO_CCM4309_NONCE_LEN 3
  352. struct qcrypto_cipher_ctx {
  353. struct list_head rsp_queue; /* response queue */
  354. struct crypto_engine *pengine; /* fixed engine assigned to this tfm */
  355. struct crypto_priv *cp;
  356. unsigned int flags;
  357. enum qce_hash_alg_enum auth_alg; /* for aead */
  358. u8 auth_key[QCRYPTO_MAX_KEY_SIZE];
  359. u8 iv[QCRYPTO_MAX_IV_LENGTH];
  360. u8 enc_key[QCRYPTO_MAX_KEY_SIZE];
  361. unsigned int enc_key_len;
  362. unsigned int authsize;
  363. unsigned int auth_key_len;
  364. u8 ccm4309_nonce[QCRYPTO_CCM4309_NONCE_LEN];
  365. struct crypto_sync_skcipher *cipher_aes192_fb;
  366. struct crypto_ahash *ahash_aead_aes192_fb;
  367. };
  368. struct qcrypto_resp_ctx {
  369. struct list_head list;
  370. struct llist_node llist;
  371. struct crypto_async_request *async_req; /* async req */
  372. int res; /* execution result */
  373. };
  374. struct qcrypto_cipher_req_ctx {
  375. struct qcrypto_resp_ctx rsp_entry;/* rsp entry. */
  376. struct crypto_engine *pengine; /* engine assigned to this request */
  377. u8 *iv;
  378. u8 rfc4309_iv[QCRYPTO_MAX_IV_LENGTH];
  379. unsigned int ivsize;
  380. int aead;
  381. int ccmtype; /* default: 0, rfc4309: 1 */
  382. struct scatterlist asg; /* Formatted associated data sg */
  383. unsigned char *adata; /* Pointer to formatted assoc data */
  384. enum qce_cipher_alg_enum alg;
  385. enum qce_cipher_dir_enum dir;
  386. enum qce_cipher_mode_enum mode;
  387. struct scatterlist *orig_src; /* Original src sg ptr */
  388. struct scatterlist *orig_dst; /* Original dst sg ptr */
  389. struct scatterlist dsg; /* Dest Data sg */
  390. struct scatterlist ssg; /* Source Data sg */
  391. unsigned char *data; /* Incoming data pointer*/
  392. struct aead_request *aead_req;
  393. struct ahash_request *fb_hash_req;
  394. uint8_t fb_ahash_digest[SHA256_DIGEST_SIZE];
  395. struct scatterlist fb_ablkcipher_src_sg[2];
  396. struct scatterlist fb_ablkcipher_dst_sg[2];
  397. char *fb_aes_iv;
  398. unsigned int fb_ahash_length;
  399. struct skcipher_request *fb_aes_req;
  400. struct scatterlist *fb_aes_src;
  401. struct scatterlist *fb_aes_dst;
  402. unsigned int fb_aes_cryptlen;
  403. };
  404. #define SHA_MAX_BLOCK_SIZE SHA256_BLOCK_SIZE
  405. #define SHA_MAX_STATE_SIZE (SHA256_DIGEST_SIZE / sizeof(u32))
  406. #define SHA_MAX_DIGEST_SIZE SHA256_DIGEST_SIZE
  407. #define MSM_QCRYPTO_REQ_QUEUE_LENGTH 768
  408. #define COMPLETION_CB_BACKLOG_LENGTH_STOP 400
  409. #define COMPLETION_CB_BACKLOG_LENGTH_START \
  410. (COMPLETION_CB_BACKLOG_LENGTH_STOP / 2)
  411. static uint8_t _std_init_vector_sha1_uint8[] = {
  412. 0x67, 0x45, 0x23, 0x01, 0xEF, 0xCD, 0xAB, 0x89,
  413. 0x98, 0xBA, 0xDC, 0xFE, 0x10, 0x32, 0x54, 0x76,
  414. 0xC3, 0xD2, 0xE1, 0xF0
  415. };
  416. /* standard initialization vector for SHA-256, source: FIPS 180-2 */
  417. static uint8_t _std_init_vector_sha256_uint8[] = {
  418. 0x6A, 0x09, 0xE6, 0x67, 0xBB, 0x67, 0xAE, 0x85,
  419. 0x3C, 0x6E, 0xF3, 0x72, 0xA5, 0x4F, 0xF5, 0x3A,
  420. 0x51, 0x0E, 0x52, 0x7F, 0x9B, 0x05, 0x68, 0x8C,
  421. 0x1F, 0x83, 0xD9, 0xAB, 0x5B, 0xE0, 0xCD, 0x19
  422. };
  423. struct qcrypto_sha_ctx {
  424. struct list_head rsp_queue; /* response queue */
  425. struct crypto_engine *pengine; /* fixed engine assigned to this tfm */
  426. struct crypto_priv *cp;
  427. unsigned int flags;
  428. enum qce_hash_alg_enum alg;
  429. uint32_t diglen;
  430. uint32_t authkey_in_len;
  431. uint8_t authkey[SHA_MAX_BLOCK_SIZE];
  432. struct ahash_request *ahash_req;
  433. struct completion ahash_req_complete;
  434. };
  435. struct qcrypto_sha_req_ctx {
  436. struct qcrypto_resp_ctx rsp_entry;/* rsp entry. */
  437. struct crypto_engine *pengine; /* engine assigned to this request */
  438. struct scatterlist *src;
  439. uint32_t nbytes;
  440. struct scatterlist *orig_src; /* Original src sg ptr */
  441. struct scatterlist dsg; /* Data sg */
  442. unsigned char *data; /* Incoming data pointer*/
  443. unsigned char *data2; /* Updated data pointer*/
  444. uint32_t byte_count[4];
  445. u64 count;
  446. uint8_t first_blk;
  447. uint8_t last_blk;
  448. uint8_t trailing_buf[SHA_MAX_BLOCK_SIZE];
  449. uint32_t trailing_buf_len;
  450. /* dma buffer, Internal use */
  451. uint8_t staging_dmabuf
  452. [SHA_MAX_BLOCK_SIZE+SHA_MAX_DIGEST_SIZE+MAX_ALIGN_SIZE];
  453. uint8_t digest[SHA_MAX_DIGEST_SIZE];
  454. struct scatterlist sg[2];
  455. };
  456. static void _byte_stream_to_words(uint32_t *iv, unsigned char *b,
  457. unsigned int len)
  458. {
  459. unsigned int n;
  460. n = len / sizeof(uint32_t);
  461. for (; n > 0; n--) {
  462. *iv = ((*b << 24) & 0xff000000) |
  463. (((*(b+1)) << 16) & 0xff0000) |
  464. (((*(b+2)) << 8) & 0xff00) |
  465. (*(b+3) & 0xff);
  466. b += sizeof(uint32_t);
  467. iv++;
  468. }
  469. n = len % sizeof(uint32_t);
  470. if (n == 3) {
  471. *iv = ((*b << 24) & 0xff000000) |
  472. (((*(b+1)) << 16) & 0xff0000) |
  473. (((*(b+2)) << 8) & 0xff00);
  474. } else if (n == 2) {
  475. *iv = ((*b << 24) & 0xff000000) |
  476. (((*(b+1)) << 16) & 0xff0000);
  477. } else if (n == 1) {
  478. *iv = ((*b << 24) & 0xff000000);
  479. }
  480. }
  481. static void _words_to_byte_stream(uint32_t *iv, unsigned char *b,
  482. unsigned int len)
  483. {
  484. unsigned int n = len / sizeof(uint32_t);
  485. for (; n > 0; n--) {
  486. *b++ = (unsigned char) ((*iv >> 24) & 0xff);
  487. *b++ = (unsigned char) ((*iv >> 16) & 0xff);
  488. *b++ = (unsigned char) ((*iv >> 8) & 0xff);
  489. *b++ = (unsigned char) (*iv & 0xff);
  490. iv++;
  491. }
  492. n = len % sizeof(uint32_t);
  493. if (n == 3) {
  494. *b++ = (unsigned char) ((*iv >> 24) & 0xff);
  495. *b++ = (unsigned char) ((*iv >> 16) & 0xff);
  496. *b = (unsigned char) ((*iv >> 8) & 0xff);
  497. } else if (n == 2) {
  498. *b++ = (unsigned char) ((*iv >> 24) & 0xff);
  499. *b = (unsigned char) ((*iv >> 16) & 0xff);
  500. } else if (n == 1) {
  501. *b = (unsigned char) ((*iv >> 24) & 0xff);
  502. }
  503. }
  504. static void qcrypto_ce_set_bus(struct crypto_engine *pengine,
  505. bool high_bw_req)
  506. {
  507. struct crypto_priv *cp = pengine->pcp;
  508. unsigned int control_flag;
  509. int ret = 0;
  510. if (cp->ce_support.req_bw_before_clk) {
  511. if (high_bw_req)
  512. control_flag = QCE_BW_REQUEST_FIRST;
  513. else
  514. control_flag = QCE_CLK_DISABLE_FIRST;
  515. } else {
  516. if (high_bw_req)
  517. control_flag = QCE_CLK_ENABLE_FIRST;
  518. else
  519. control_flag = QCE_BW_REQUEST_RESET_FIRST;
  520. }
  521. switch (control_flag) {
  522. case QCE_CLK_ENABLE_FIRST:
  523. ret = qce_enable_clk(pengine->qce);
  524. if (ret) {
  525. pr_err("%s Unable enable clk\n", __func__);
  526. return;
  527. }
  528. ret = icc_set_bw(pengine->icc_path,
  529. CRYPTO_AVG_BW, CRYPTO_PEAK_BW);
  530. if (ret) {
  531. pr_err("%s Unable to set high bw\n", __func__);
  532. ret = qce_disable_clk(pengine->qce);
  533. if (ret)
  534. pr_err("%s Unable disable clk\n", __func__);
  535. return;
  536. }
  537. break;
  538. case QCE_BW_REQUEST_FIRST:
  539. ret = icc_set_bw(pengine->icc_path,
  540. CRYPTO_AVG_BW, CRYPTO_PEAK_BW);
  541. if (ret) {
  542. pr_err("%s Unable to set high bw\n", __func__);
  543. return;
  544. }
  545. ret = qce_enable_clk(pengine->qce);
  546. if (ret) {
  547. pr_err("%s Unable enable clk\n", __func__);
  548. ret = icc_set_bw(pengine->icc_path, 0, 0);
  549. if (ret)
  550. pr_err("%s Unable to set low bw\n", __func__);
  551. return;
  552. }
  553. break;
  554. case QCE_CLK_DISABLE_FIRST:
  555. ret = qce_disable_clk(pengine->qce);
  556. if (ret) {
  557. pr_err("%s Unable to disable clk\n", __func__);
  558. return;
  559. }
  560. ret = icc_set_bw(pengine->icc_path, 0, 0);
  561. if (ret) {
  562. pr_err("%s Unable to set low bw\n", __func__);
  563. ret = qce_enable_clk(pengine->qce);
  564. if (ret)
  565. pr_err("%s Unable enable clk\n", __func__);
  566. return;
  567. }
  568. break;
  569. case QCE_BW_REQUEST_RESET_FIRST:
  570. ret = icc_set_bw(pengine->icc_path, 0, 0);
  571. if (ret) {
  572. pr_err("%s Unable to set low bw\n", __func__);
  573. return;
  574. }
  575. ret = qce_disable_clk(pengine->qce);
  576. if (ret) {
  577. pr_err("%s Unable to disable clk\n", __func__);
  578. ret = icc_set_bw(pengine->icc_path,
  579. CRYPTO_AVG_BW, CRYPTO_PEAK_BW);
  580. if (ret)
  581. pr_err("%s Unable to set high bw\n", __func__);
  582. return;
  583. }
  584. break;
  585. default:
  586. return;
  587. }
  588. }
  589. static void qcrypto_bw_reaper_timer_callback(struct timer_list *data)
  590. {
  591. struct crypto_engine *pengine = from_timer(pengine, data,
  592. bw_reaper_timer);
  593. schedule_work(&pengine->bw_reaper_ws);
  594. }
  595. static void qcrypto_bw_set_timeout(struct crypto_engine *pengine)
  596. {
  597. pengine->bw_reaper_timer.expires = jiffies +
  598. msecs_to_jiffies(QCRYPTO_HIGH_BANDWIDTH_TIMEOUT);
  599. mod_timer(&(pengine->bw_reaper_timer),
  600. pengine->bw_reaper_timer.expires);
  601. }
  602. static void qcrypto_ce_bw_allocate_req(struct crypto_engine *pengine)
  603. {
  604. schedule_work(&pengine->bw_allocate_ws);
  605. }
  606. static int _start_qcrypto_process(struct crypto_priv *cp,
  607. struct crypto_engine *pengine);
  608. static void qcrypto_bw_allocate_work(struct work_struct *work)
  609. {
  610. struct crypto_engine *pengine = container_of(work,
  611. struct crypto_engine, bw_allocate_ws);
  612. unsigned long flags;
  613. struct crypto_priv *cp = pengine->pcp;
  614. spin_lock_irqsave(&cp->lock, flags);
  615. pengine->bw_state = BUS_BANDWIDTH_ALLOCATING;
  616. spin_unlock_irqrestore(&cp->lock, flags);
  617. qcrypto_ce_set_bus(pengine, true);
  618. qcrypto_bw_set_timeout(pengine);
  619. spin_lock_irqsave(&cp->lock, flags);
  620. pengine->bw_state = BUS_HAS_BANDWIDTH;
  621. pengine->high_bw_req = false;
  622. pengine->active_seq++;
  623. pengine->check_flag = true;
  624. spin_unlock_irqrestore(&cp->lock, flags);
  625. _start_qcrypto_process(cp, pengine);
  626. };
  627. static void qcrypto_bw_reaper_work(struct work_struct *work)
  628. {
  629. struct crypto_engine *pengine = container_of(work,
  630. struct crypto_engine, bw_reaper_ws);
  631. struct crypto_priv *cp = pengine->pcp;
  632. unsigned long flags;
  633. u32 active_seq;
  634. bool restart = false;
  635. spin_lock_irqsave(&cp->lock, flags);
  636. active_seq = pengine->active_seq;
  637. if (pengine->bw_state == BUS_HAS_BANDWIDTH &&
  638. (active_seq == pengine->last_active_seq)) {
  639. /* check if engine is stuck */
  640. if (atomic_read(&pengine->req_count) > 0) {
  641. if (pengine->check_flag)
  642. dev_warn(&pengine->pdev->dev,
  643. "The engine appears to be stuck seq %d.\n",
  644. active_seq);
  645. pengine->check_flag = false;
  646. goto ret;
  647. }
  648. pengine->bw_state = BUS_BANDWIDTH_RELEASING;
  649. spin_unlock_irqrestore(&cp->lock, flags);
  650. qcrypto_ce_set_bus(pengine, false);
  651. spin_lock_irqsave(&cp->lock, flags);
  652. if (pengine->high_bw_req) {
  653. /* we got request while we are disabling clock */
  654. pengine->bw_state = BUS_BANDWIDTH_ALLOCATING;
  655. spin_unlock_irqrestore(&cp->lock, flags);
  656. qcrypto_ce_set_bus(pengine, true);
  657. spin_lock_irqsave(&cp->lock, flags);
  658. pengine->bw_state = BUS_HAS_BANDWIDTH;
  659. pengine->high_bw_req = false;
  660. restart = true;
  661. } else
  662. pengine->bw_state = BUS_NO_BANDWIDTH;
  663. }
  664. ret:
  665. pengine->last_active_seq = active_seq;
  666. spin_unlock_irqrestore(&cp->lock, flags);
  667. if (restart)
  668. _start_qcrypto_process(cp, pengine);
  669. if (pengine->bw_state != BUS_NO_BANDWIDTH)
  670. qcrypto_bw_set_timeout(pengine);
  671. }
  672. static int qcrypto_count_sg(struct scatterlist *sg, int nbytes)
  673. {
  674. int i;
  675. for (i = 0; nbytes > 0 && sg != NULL; i++, sg = sg_next(sg))
  676. nbytes -= sg->length;
  677. return i;
  678. }
  679. static size_t qcrypto_sg_copy_from_buffer(struct scatterlist *sgl,
  680. unsigned int nents, void *buf, size_t buflen)
  681. {
  682. int i;
  683. size_t offset, len;
  684. for (i = 0, offset = 0; i < nents; ++i) {
  685. len = sg_copy_from_buffer(sgl, 1, buf, buflen);
  686. buf += len;
  687. buflen -= len;
  688. offset += len;
  689. sgl = sg_next(sgl);
  690. }
  691. return offset;
  692. }
  693. static size_t qcrypto_sg_copy_to_buffer(struct scatterlist *sgl,
  694. unsigned int nents, void *buf, size_t buflen)
  695. {
  696. int i;
  697. size_t offset, len;
  698. for (i = 0, offset = 0; i < nents; ++i) {
  699. len = sg_copy_to_buffer(sgl, 1, buf, buflen);
  700. buf += len;
  701. buflen -= len;
  702. offset += len;
  703. sgl = sg_next(sgl);
  704. }
  705. return offset;
  706. }
  707. static struct qcrypto_alg *_qcrypto_sha_alg_alloc(struct crypto_priv *cp,
  708. struct ahash_alg *template)
  709. {
  710. struct qcrypto_alg *q_alg;
  711. q_alg = kzalloc(sizeof(struct qcrypto_alg), GFP_KERNEL);
  712. if (!q_alg)
  713. return ERR_PTR(-ENOMEM);
  714. q_alg->alg_type = QCRYPTO_ALG_SHA;
  715. q_alg->sha_alg = *template;
  716. q_alg->cp = cp;
  717. return q_alg;
  718. }
  719. static struct qcrypto_alg *_qcrypto_cipher_alg_alloc(struct crypto_priv *cp,
  720. struct skcipher_alg *template)
  721. {
  722. struct qcrypto_alg *q_alg;
  723. q_alg = kzalloc(sizeof(struct qcrypto_alg), GFP_KERNEL);
  724. if (!q_alg)
  725. return ERR_PTR(-ENOMEM);
  726. q_alg->alg_type = QCRYPTO_ALG_CIPHER;
  727. q_alg->cipher_alg = *template;
  728. q_alg->cp = cp;
  729. return q_alg;
  730. }
  731. static struct qcrypto_alg *_qcrypto_aead_alg_alloc(struct crypto_priv *cp,
  732. struct aead_alg *template)
  733. {
  734. struct qcrypto_alg *q_alg;
  735. q_alg = kzalloc(sizeof(struct qcrypto_alg), GFP_KERNEL);
  736. if (!q_alg)
  737. return ERR_PTR(-ENOMEM);
  738. q_alg->alg_type = QCRYPTO_ALG_AEAD;
  739. q_alg->aead_alg = *template;
  740. q_alg->cp = cp;
  741. return q_alg;
  742. }
  743. static int _qcrypto_cipher_ctx_init(struct qcrypto_cipher_ctx *ctx,
  744. struct qcrypto_alg *q_alg)
  745. {
  746. if (!ctx || !q_alg) {
  747. pr_err("ctx or q_alg is NULL\n");
  748. return -EINVAL;
  749. }
  750. ctx->flags = 0;
  751. /* update context with ptr to cp */
  752. ctx->cp = q_alg->cp;
  753. /* random first IV */
  754. get_random_bytes(ctx->iv, QCRYPTO_MAX_IV_LENGTH);
  755. if (_qcrypto_init_assign) {
  756. ctx->pengine = _qcrypto_static_assign_engine(ctx->cp);
  757. if (ctx->pengine == NULL)
  758. return -ENODEV;
  759. } else
  760. ctx->pengine = NULL;
  761. INIT_LIST_HEAD(&ctx->rsp_queue);
  762. ctx->auth_alg = QCE_HASH_LAST;
  763. return 0;
  764. }
  765. static int _qcrypto_ahash_cra_init(struct crypto_tfm *tfm)
  766. {
  767. struct crypto_ahash *ahash = __crypto_ahash_cast(tfm);
  768. struct qcrypto_sha_ctx *sha_ctx = crypto_tfm_ctx(tfm);
  769. struct ahash_alg *alg = container_of(crypto_hash_alg_common(ahash),
  770. struct ahash_alg, halg);
  771. struct qcrypto_alg *q_alg = container_of(alg, struct qcrypto_alg,
  772. sha_alg);
  773. crypto_ahash_set_reqsize(ahash, sizeof(struct qcrypto_sha_req_ctx));
  774. /* update context with ptr to cp */
  775. sha_ctx->cp = q_alg->cp;
  776. sha_ctx->flags = 0;
  777. sha_ctx->ahash_req = NULL;
  778. if (_qcrypto_init_assign) {
  779. sha_ctx->pengine = _qcrypto_static_assign_engine(sha_ctx->cp);
  780. if (sha_ctx->pengine == NULL)
  781. return -ENODEV;
  782. } else
  783. sha_ctx->pengine = NULL;
  784. INIT_LIST_HEAD(&sha_ctx->rsp_queue);
  785. return 0;
  786. }
  787. static void _qcrypto_ahash_cra_exit(struct crypto_tfm *tfm)
  788. {
  789. struct qcrypto_sha_ctx *sha_ctx = crypto_tfm_ctx(tfm);
  790. if (!list_empty(&sha_ctx->rsp_queue))
  791. pr_err("%s: requests still outstanding\n", __func__);
  792. if (sha_ctx->ahash_req != NULL) {
  793. ahash_request_free(sha_ctx->ahash_req);
  794. sha_ctx->ahash_req = NULL;
  795. }
  796. }
  797. static void _crypto_sha_hmac_ahash_req_complete(
  798. struct crypto_async_request *req, int err);
  799. static int _qcrypto_ahash_hmac_cra_init(struct crypto_tfm *tfm)
  800. {
  801. struct crypto_ahash *ahash = __crypto_ahash_cast(tfm);
  802. struct qcrypto_sha_ctx *sha_ctx = crypto_tfm_ctx(tfm);
  803. int ret = 0;
  804. ret = _qcrypto_ahash_cra_init(tfm);
  805. if (ret)
  806. return ret;
  807. sha_ctx->ahash_req = ahash_request_alloc(ahash, GFP_KERNEL);
  808. if (sha_ctx->ahash_req == NULL) {
  809. _qcrypto_ahash_cra_exit(tfm);
  810. return -ENOMEM;
  811. }
  812. init_completion(&sha_ctx->ahash_req_complete);
  813. ahash_request_set_callback(sha_ctx->ahash_req,
  814. CRYPTO_TFM_REQ_MAY_BACKLOG,
  815. _crypto_sha_hmac_ahash_req_complete,
  816. &sha_ctx->ahash_req_complete);
  817. crypto_ahash_clear_flags(ahash, ~0);
  818. return 0;
  819. }
  820. static int _qcrypto_skcipher_init(struct crypto_skcipher *tfm)
  821. {
  822. struct qcrypto_cipher_ctx *ctx = crypto_skcipher_ctx(tfm);
  823. struct skcipher_alg *alg = crypto_skcipher_alg(tfm);
  824. struct qcrypto_alg *q_alg;
  825. q_alg = container_of(alg, struct qcrypto_alg, cipher_alg);
  826. crypto_skcipher_set_reqsize(tfm, sizeof(struct qcrypto_cipher_req_ctx));
  827. return _qcrypto_cipher_ctx_init(ctx, q_alg);
  828. }
  829. static int _qcrypto_aes_skcipher_init(struct crypto_skcipher *tfm)
  830. {
  831. const char *name = crypto_tfm_alg_name(&tfm->base);
  832. struct qcrypto_cipher_ctx *ctx = crypto_skcipher_ctx(tfm);
  833. int ret;
  834. struct crypto_priv *cp = &qcrypto_dev;
  835. if (cp->ce_support.use_sw_aes_cbc_ecb_ctr_algo) {
  836. ctx->cipher_aes192_fb = NULL;
  837. return _qcrypto_skcipher_init(tfm);
  838. }
  839. ctx->cipher_aes192_fb = crypto_alloc_sync_skcipher(name, 0,
  840. CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK);
  841. if (IS_ERR(ctx->cipher_aes192_fb)) {
  842. pr_err("Error allocating fallback algo %s\n", name);
  843. ret = PTR_ERR(ctx->cipher_aes192_fb);
  844. ctx->cipher_aes192_fb = NULL;
  845. return ret;
  846. }
  847. return _qcrypto_skcipher_init(tfm);
  848. }
  849. static int _qcrypto_aead_cra_init(struct crypto_aead *tfm)
  850. {
  851. struct qcrypto_cipher_ctx *ctx = crypto_aead_ctx(tfm);
  852. struct aead_alg *aeadalg = crypto_aead_alg(tfm);
  853. struct qcrypto_alg *q_alg = container_of(aeadalg, struct qcrypto_alg,
  854. aead_alg);
  855. return _qcrypto_cipher_ctx_init(ctx, q_alg);
  856. }
  857. static int _qcrypto_cra_aead_sha1_init(struct crypto_aead *tfm)
  858. {
  859. int rc;
  860. struct qcrypto_cipher_ctx *ctx = crypto_aead_ctx(tfm);
  861. crypto_aead_set_reqsize(tfm, sizeof(struct qcrypto_cipher_req_ctx));
  862. rc = _qcrypto_aead_cra_init(tfm);
  863. ctx->auth_alg = QCE_HASH_SHA1_HMAC;
  864. return rc;
  865. }
  866. static int _qcrypto_cra_aead_sha256_init(struct crypto_aead *tfm)
  867. {
  868. int rc;
  869. struct qcrypto_cipher_ctx *ctx = crypto_aead_ctx(tfm);
  870. crypto_aead_set_reqsize(tfm, sizeof(struct qcrypto_cipher_req_ctx));
  871. rc = _qcrypto_aead_cra_init(tfm);
  872. ctx->auth_alg = QCE_HASH_SHA256_HMAC;
  873. return rc;
  874. }
  875. static int _qcrypto_cra_aead_ccm_init(struct crypto_aead *tfm)
  876. {
  877. int rc;
  878. struct qcrypto_cipher_ctx *ctx = crypto_aead_ctx(tfm);
  879. crypto_aead_set_reqsize(tfm, sizeof(struct qcrypto_cipher_req_ctx));
  880. rc = _qcrypto_aead_cra_init(tfm);
  881. ctx->auth_alg = QCE_HASH_AES_CMAC;
  882. return rc;
  883. }
  884. static int _qcrypto_cra_aead_rfc4309_ccm_init(struct crypto_aead *tfm)
  885. {
  886. int rc;
  887. struct qcrypto_cipher_ctx *ctx = crypto_aead_ctx(tfm);
  888. crypto_aead_set_reqsize(tfm, sizeof(struct qcrypto_cipher_req_ctx));
  889. rc = _qcrypto_aead_cra_init(tfm);
  890. ctx->auth_alg = QCE_HASH_AES_CMAC;
  891. return rc;
  892. }
  893. static int _qcrypto_cra_aead_aes_sha1_init(struct crypto_aead *tfm)
  894. {
  895. int rc;
  896. struct qcrypto_cipher_ctx *ctx = crypto_aead_ctx(tfm);
  897. struct crypto_priv *cp = &qcrypto_dev;
  898. crypto_aead_set_reqsize(tfm, sizeof(struct qcrypto_cipher_req_ctx));
  899. rc = _qcrypto_aead_cra_init(tfm);
  900. if (rc)
  901. return rc;
  902. ctx->cipher_aes192_fb = NULL;
  903. ctx->ahash_aead_aes192_fb = NULL;
  904. if (!cp->ce_support.aes_key_192) {
  905. ctx->cipher_aes192_fb = crypto_alloc_sync_skcipher(
  906. "cbc(aes)", 0, 0);
  907. if (IS_ERR(ctx->cipher_aes192_fb)) {
  908. ctx->cipher_aes192_fb = NULL;
  909. } else {
  910. ctx->ahash_aead_aes192_fb = crypto_alloc_ahash(
  911. "hmac(sha1)", 0, 0);
  912. if (IS_ERR(ctx->ahash_aead_aes192_fb)) {
  913. ctx->ahash_aead_aes192_fb = NULL;
  914. crypto_free_sync_skcipher(
  915. ctx->cipher_aes192_fb);
  916. ctx->cipher_aes192_fb = NULL;
  917. }
  918. }
  919. }
  920. ctx->auth_alg = QCE_HASH_SHA1_HMAC;
  921. return 0;
  922. }
  923. static int _qcrypto_cra_aead_aes_sha256_init(struct crypto_aead *tfm)
  924. {
  925. int rc;
  926. struct qcrypto_cipher_ctx *ctx = crypto_aead_ctx(tfm);
  927. struct crypto_priv *cp = &qcrypto_dev;
  928. crypto_aead_set_reqsize(tfm, sizeof(struct qcrypto_cipher_req_ctx));
  929. rc = _qcrypto_aead_cra_init(tfm);
  930. if (rc)
  931. return rc;
  932. ctx->cipher_aes192_fb = NULL;
  933. ctx->ahash_aead_aes192_fb = NULL;
  934. if (!cp->ce_support.aes_key_192) {
  935. ctx->cipher_aes192_fb = crypto_alloc_sync_skcipher(
  936. "cbc(aes)", 0, 0);
  937. if (IS_ERR(ctx->cipher_aes192_fb)) {
  938. ctx->cipher_aes192_fb = NULL;
  939. } else {
  940. ctx->ahash_aead_aes192_fb = crypto_alloc_ahash(
  941. "hmac(sha256)", 0, 0);
  942. if (IS_ERR(ctx->ahash_aead_aes192_fb)) {
  943. ctx->ahash_aead_aes192_fb = NULL;
  944. crypto_free_sync_skcipher(
  945. ctx->cipher_aes192_fb);
  946. ctx->cipher_aes192_fb = NULL;
  947. }
  948. }
  949. }
  950. ctx->auth_alg = QCE_HASH_SHA256_HMAC;
  951. return 0;
  952. }
  953. static void _qcrypto_skcipher_exit(struct crypto_skcipher *tfm)
  954. {
  955. struct qcrypto_cipher_ctx *ctx = crypto_skcipher_ctx(tfm);
  956. if (!list_empty(&ctx->rsp_queue))
  957. pr_err("_qcrypto__cra_skcipher_exit: requests still outstanding\n");
  958. }
  959. static void _qcrypto_aes_skcipher_exit(struct crypto_skcipher *tfm)
  960. {
  961. struct qcrypto_cipher_ctx *ctx = crypto_skcipher_ctx(tfm);
  962. _qcrypto_skcipher_exit(tfm);
  963. if (ctx->cipher_aes192_fb)
  964. crypto_free_sync_skcipher(ctx->cipher_aes192_fb);
  965. ctx->cipher_aes192_fb = NULL;
  966. }
  967. static void _qcrypto_cra_aead_exit(struct crypto_aead *tfm)
  968. {
  969. struct qcrypto_cipher_ctx *ctx = crypto_aead_ctx(tfm);
  970. if (!list_empty(&ctx->rsp_queue))
  971. pr_err("_qcrypto__cra_aead_exit: requests still outstanding\n");
  972. }
  973. static void _qcrypto_cra_aead_aes_exit(struct crypto_aead *tfm)
  974. {
  975. struct qcrypto_cipher_ctx *ctx = crypto_aead_ctx(tfm);
  976. if (!list_empty(&ctx->rsp_queue))
  977. pr_err("_qcrypto__cra_aead_exit: requests still outstanding\n");
  978. if (ctx->cipher_aes192_fb)
  979. crypto_free_sync_skcipher(ctx->cipher_aes192_fb);
  980. if (ctx->ahash_aead_aes192_fb)
  981. crypto_free_ahash(ctx->ahash_aead_aes192_fb);
  982. ctx->cipher_aes192_fb = NULL;
  983. ctx->ahash_aead_aes192_fb = NULL;
  984. }
  985. static int _disp_stats(int id)
  986. {
  987. struct crypto_stat *pstat;
  988. int len = 0;
  989. unsigned long flags;
  990. struct crypto_priv *cp = &qcrypto_dev;
  991. struct crypto_engine *pe;
  992. int i;
  993. pstat = &_qcrypto_stat;
  994. len = scnprintf(_debug_read_buf, DEBUG_MAX_RW_BUF - 1,
  995. "\nQTI crypto accelerator %d Statistics\n",
  996. id + 1);
  997. len += scnprintf(_debug_read_buf + len, DEBUG_MAX_RW_BUF - len - 1,
  998. " SK CIPHER AES encryption : %llu\n",
  999. pstat->sk_cipher_aes_enc);
  1000. len += scnprintf(_debug_read_buf + len, DEBUG_MAX_RW_BUF - len - 1,
  1001. " SK CIPHER AES decryption : %llu\n",
  1002. pstat->sk_cipher_aes_dec);
  1003. len += scnprintf(_debug_read_buf + len, DEBUG_MAX_RW_BUF - len - 1,
  1004. " SK CIPHER DES encryption : %llu\n",
  1005. pstat->sk_cipher_des_enc);
  1006. len += scnprintf(_debug_read_buf + len, DEBUG_MAX_RW_BUF - len - 1,
  1007. " SK CIPHER DES decryption : %llu\n",
  1008. pstat->sk_cipher_des_dec);
  1009. len += scnprintf(_debug_read_buf + len, DEBUG_MAX_RW_BUF - len - 1,
  1010. " SK CIPHER 3DES encryption : %llu\n",
  1011. pstat->sk_cipher_3des_enc);
  1012. len += scnprintf(_debug_read_buf + len, DEBUG_MAX_RW_BUF - len - 1,
  1013. " SK CIPHER 3DES decryption : %llu\n",
  1014. pstat->sk_cipher_3des_dec);
  1015. len += scnprintf(_debug_read_buf + len, DEBUG_MAX_RW_BUF - len - 1,
  1016. " SK CIPHER operation success : %llu\n",
  1017. pstat->sk_cipher_op_success);
  1018. len += scnprintf(_debug_read_buf + len, DEBUG_MAX_RW_BUF - len - 1,
  1019. " SK CIPHER operation fail : %llu\n",
  1020. pstat->sk_cipher_op_fail);
  1021. len += scnprintf(_debug_read_buf + len, DEBUG_MAX_RW_BUF - len - 1,
  1022. "\n");
  1023. len += scnprintf(_debug_read_buf + len, DEBUG_MAX_RW_BUF - len - 1,
  1024. " AEAD SHA1-AES encryption : %llu\n",
  1025. pstat->aead_sha1_aes_enc);
  1026. len += scnprintf(_debug_read_buf + len, DEBUG_MAX_RW_BUF - len - 1,
  1027. " AEAD SHA1-AES decryption : %llu\n",
  1028. pstat->aead_sha1_aes_dec);
  1029. len += scnprintf(_debug_read_buf + len, DEBUG_MAX_RW_BUF - len - 1,
  1030. " AEAD SHA1-DES encryption : %llu\n",
  1031. pstat->aead_sha1_des_enc);
  1032. len += scnprintf(_debug_read_buf + len, DEBUG_MAX_RW_BUF - len - 1,
  1033. " AEAD SHA1-DES decryption : %llu\n",
  1034. pstat->aead_sha1_des_dec);
  1035. len += scnprintf(_debug_read_buf + len, DEBUG_MAX_RW_BUF - len - 1,
  1036. " AEAD SHA1-3DES encryption : %llu\n",
  1037. pstat->aead_sha1_3des_enc);
  1038. len += scnprintf(_debug_read_buf + len, DEBUG_MAX_RW_BUF - len - 1,
  1039. " AEAD SHA1-3DES decryption : %llu\n",
  1040. pstat->aead_sha1_3des_dec);
  1041. len += scnprintf(_debug_read_buf + len, DEBUG_MAX_RW_BUF - len - 1,
  1042. " AEAD SHA256-AES encryption : %llu\n",
  1043. pstat->aead_sha256_aes_enc);
  1044. len += scnprintf(_debug_read_buf + len, DEBUG_MAX_RW_BUF - len - 1,
  1045. " AEAD SHA256-AES decryption : %llu\n",
  1046. pstat->aead_sha256_aes_dec);
  1047. len += scnprintf(_debug_read_buf + len, DEBUG_MAX_RW_BUF - len - 1,
  1048. " AEAD SHA256-DES encryption : %llu\n",
  1049. pstat->aead_sha256_des_enc);
  1050. len += scnprintf(_debug_read_buf + len, DEBUG_MAX_RW_BUF - len - 1,
  1051. " AEAD SHA256-DES decryption : %llu\n",
  1052. pstat->aead_sha256_des_dec);
  1053. len += scnprintf(_debug_read_buf + len, DEBUG_MAX_RW_BUF - len - 1,
  1054. " AEAD SHA256-3DES encryption : %llu\n",
  1055. pstat->aead_sha256_3des_enc);
  1056. len += scnprintf(_debug_read_buf + len, DEBUG_MAX_RW_BUF - len - 1,
  1057. " AEAD SHA256-3DES decryption : %llu\n",
  1058. pstat->aead_sha256_3des_dec);
  1059. len += scnprintf(_debug_read_buf + len, DEBUG_MAX_RW_BUF - len - 1,
  1060. " AEAD CCM-AES encryption : %llu\n",
  1061. pstat->aead_ccm_aes_enc);
  1062. len += scnprintf(_debug_read_buf + len, DEBUG_MAX_RW_BUF - len - 1,
  1063. " AEAD CCM-AES decryption : %llu\n",
  1064. pstat->aead_ccm_aes_dec);
  1065. len += scnprintf(_debug_read_buf + len, DEBUG_MAX_RW_BUF - len - 1,
  1066. " AEAD RFC4309-CCM-AES encryption : %llu\n",
  1067. pstat->aead_rfc4309_ccm_aes_enc);
  1068. len += scnprintf(_debug_read_buf + len, DEBUG_MAX_RW_BUF - len - 1,
  1069. " AEAD RFC4309-CCM-AES decryption : %llu\n",
  1070. pstat->aead_rfc4309_ccm_aes_dec);
  1071. len += scnprintf(_debug_read_buf + len, DEBUG_MAX_RW_BUF - len - 1,
  1072. " AEAD operation success : %llu\n",
  1073. pstat->aead_op_success);
  1074. len += scnprintf(_debug_read_buf + len, DEBUG_MAX_RW_BUF - len - 1,
  1075. " AEAD operation fail : %llu\n",
  1076. pstat->aead_op_fail);
  1077. len += scnprintf(_debug_read_buf + len, DEBUG_MAX_RW_BUF - len - 1,
  1078. " AEAD bad message : %llu\n",
  1079. pstat->aead_bad_msg);
  1080. len += scnprintf(_debug_read_buf + len, DEBUG_MAX_RW_BUF - len - 1,
  1081. "\n");
  1082. len += scnprintf(_debug_read_buf + len, DEBUG_MAX_RW_BUF - len - 1,
  1083. " AHASH SHA1 digest : %llu\n",
  1084. pstat->sha1_digest);
  1085. len += scnprintf(_debug_read_buf + len, DEBUG_MAX_RW_BUF - len - 1,
  1086. " AHASH SHA256 digest : %llu\n",
  1087. pstat->sha256_digest);
  1088. len += scnprintf(_debug_read_buf + len, DEBUG_MAX_RW_BUF - len - 1,
  1089. " AHASH SHA1 HMAC digest : %llu\n",
  1090. pstat->sha1_hmac_digest);
  1091. len += scnprintf(_debug_read_buf + len, DEBUG_MAX_RW_BUF - len - 1,
  1092. " AHASH SHA256 HMAC digest : %llu\n",
  1093. pstat->sha256_hmac_digest);
  1094. len += scnprintf(_debug_read_buf + len, DEBUG_MAX_RW_BUF - len - 1,
  1095. " AHASH operation success : %llu\n",
  1096. pstat->ahash_op_success);
  1097. len += scnprintf(_debug_read_buf + len, DEBUG_MAX_RW_BUF - len - 1,
  1098. " AHASH operation fail : %llu\n",
  1099. pstat->ahash_op_fail);
  1100. len += scnprintf(_debug_read_buf + len, DEBUG_MAX_RW_BUF - len - 1,
  1101. " resp start, resp stop, max rsp queue reorder-cnt : %u %u %u %u\n",
  1102. cp->resp_start, cp->resp_stop,
  1103. cp->max_resp_qlen, cp->max_reorder_cnt);
  1104. len += scnprintf(_debug_read_buf + len, DEBUG_MAX_RW_BUF - len - 1,
  1105. " max queue length, no avail : %u %u\n",
  1106. cp->max_qlen, cp->no_avail);
  1107. len += scnprintf(_debug_read_buf + len, DEBUG_MAX_RW_BUF - len - 1,
  1108. " work queue : %u %u %u\n",
  1109. cp->queue_work_eng3,
  1110. cp->queue_work_not_eng3,
  1111. cp->queue_work_not_eng3_nz);
  1112. len += scnprintf(_debug_read_buf + len, DEBUG_MAX_RW_BUF - len - 1,
  1113. "\n");
  1114. spin_lock_irqsave(&cp->lock, flags);
  1115. list_for_each_entry(pe, &cp->engine_list, elist) {
  1116. len += scnprintf(
  1117. _debug_read_buf + len,
  1118. DEBUG_MAX_RW_BUF - len - 1,
  1119. " Engine %4d Req max %d : %llu\n",
  1120. pe->unit,
  1121. pe->max_req_used,
  1122. pe->total_req
  1123. );
  1124. len += scnprintf(
  1125. _debug_read_buf + len,
  1126. DEBUG_MAX_RW_BUF - len - 1,
  1127. " Engine %4d Req Error : %llu\n",
  1128. pe->unit,
  1129. pe->err_req
  1130. );
  1131. qce_get_driver_stats(pe->qce);
  1132. }
  1133. spin_unlock_irqrestore(&cp->lock, flags);
  1134. for (i = 0; i < MAX_SMP_CPU+1; i++)
  1135. if (cp->cpu_req[i])
  1136. len += scnprintf(
  1137. _debug_read_buf + len,
  1138. DEBUG_MAX_RW_BUF - len - 1,
  1139. "CPU %d Issue Req : %d\n",
  1140. i, cp->cpu_req[i]);
  1141. return len;
  1142. }
  1143. static void _qcrypto_remove_engine(struct crypto_engine *pengine)
  1144. {
  1145. struct crypto_priv *cp;
  1146. struct qcrypto_alg *q_alg;
  1147. struct qcrypto_alg *n;
  1148. unsigned long flags;
  1149. struct crypto_engine *pe;
  1150. cp = pengine->pcp;
  1151. spin_lock_irqsave(&cp->lock, flags);
  1152. list_del(&pengine->elist);
  1153. if (pengine->first_engine) {
  1154. cp->first_engine = NULL;
  1155. pe = list_first_entry(&cp->engine_list, struct crypto_engine,
  1156. elist);
  1157. if (pe) {
  1158. pe->first_engine = true;
  1159. cp->first_engine = pe;
  1160. }
  1161. }
  1162. if (cp->next_engine == pengine)
  1163. cp->next_engine = NULL;
  1164. if (cp->scheduled_eng == pengine)
  1165. cp->scheduled_eng = NULL;
  1166. spin_unlock_irqrestore(&cp->lock, flags);
  1167. cp->total_units--;
  1168. cancel_work_sync(&pengine->bw_reaper_ws);
  1169. cancel_work_sync(&pengine->bw_allocate_ws);
  1170. del_timer_sync(&pengine->bw_reaper_timer);
  1171. if (pengine->icc_path)
  1172. icc_put(pengine->icc_path);
  1173. pengine->icc_path = NULL;
  1174. kfree_sensitive(pengine->preq_pool);
  1175. if (cp->total_units)
  1176. return;
  1177. list_for_each_entry_safe(q_alg, n, &cp->alg_list, entry) {
  1178. if (q_alg->alg_type == QCRYPTO_ALG_CIPHER)
  1179. crypto_unregister_skcipher(&q_alg->cipher_alg);
  1180. if (q_alg->alg_type == QCRYPTO_ALG_SHA)
  1181. crypto_unregister_ahash(&q_alg->sha_alg);
  1182. if (q_alg->alg_type == QCRYPTO_ALG_AEAD)
  1183. crypto_unregister_aead(&q_alg->aead_alg);
  1184. list_del(&q_alg->entry);
  1185. kfree_sensitive(q_alg);
  1186. }
  1187. }
  1188. static int _qcrypto_remove(struct platform_device *pdev)
  1189. {
  1190. struct crypto_engine *pengine;
  1191. struct crypto_priv *cp;
  1192. pengine = platform_get_drvdata(pdev);
  1193. if (!pengine)
  1194. return 0;
  1195. cp = pengine->pcp;
  1196. mutex_lock(&cp->engine_lock);
  1197. _qcrypto_remove_engine(pengine);
  1198. mutex_unlock(&cp->engine_lock);
  1199. if (pengine->qce)
  1200. qce_close(pengine->qce);
  1201. kfree_sensitive(pengine);
  1202. return 0;
  1203. }
  1204. static int _qcrypto_check_aes_keylen(struct crypto_priv *cp, unsigned int len)
  1205. {
  1206. switch (len) {
  1207. case AES_KEYSIZE_128:
  1208. case AES_KEYSIZE_256:
  1209. break;
  1210. case AES_KEYSIZE_192:
  1211. if (cp->ce_support.aes_key_192)
  1212. break;
  1213. default:
  1214. //crypto_ablkcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
  1215. return -EINVAL;
  1216. }
  1217. return 0;
  1218. }
  1219. static int _qcrypto_setkey_aes_192_fallback(struct crypto_skcipher *tfm,
  1220. const u8 *key)
  1221. {
  1222. //struct crypto_tfm *tfm = crypto_ablkcipher_tfm(cipher);
  1223. struct qcrypto_cipher_ctx *ctx = crypto_skcipher_ctx(tfm);
  1224. int ret;
  1225. ctx->enc_key_len = AES_KEYSIZE_192;
  1226. crypto_sync_skcipher_clear_flags(ctx->cipher_aes192_fb,
  1227. CRYPTO_TFM_REQ_MASK);
  1228. crypto_sync_skcipher_set_flags(ctx->cipher_aes192_fb,
  1229. (crypto_skcipher_get_flags(tfm) & CRYPTO_TFM_REQ_MASK));
  1230. ret = crypto_sync_skcipher_setkey(ctx->cipher_aes192_fb, key,
  1231. AES_KEYSIZE_192);
  1232. /*
  1233. * TODO: delete or find equivalent in new crypto_skcipher api
  1234. if (ret) {
  1235. tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK;
  1236. tfm->crt_flags |=
  1237. (cipher->base.crt_flags & CRYPTO_TFM_RES_MASK);
  1238. }
  1239. */
  1240. return ret;
  1241. }
  1242. static int _qcrypto_setkey_aes(struct crypto_skcipher *tfm, const u8 *key,
  1243. unsigned int keylen)
  1244. {
  1245. struct qcrypto_cipher_ctx *ctx = crypto_skcipher_ctx(tfm);
  1246. struct crypto_priv *cp = ctx->cp;
  1247. if ((ctx->flags & QCRYPTO_CTX_USE_HW_KEY) == QCRYPTO_CTX_USE_HW_KEY)
  1248. return 0;
  1249. if ((keylen == AES_KEYSIZE_192) && (!cp->ce_support.aes_key_192)
  1250. && ctx->cipher_aes192_fb)
  1251. return _qcrypto_setkey_aes_192_fallback(tfm, key);
  1252. if (_qcrypto_check_aes_keylen(cp, keylen))
  1253. return -EINVAL;
  1254. ctx->enc_key_len = keylen;
  1255. if (!(ctx->flags & QCRYPTO_CTX_USE_PIPE_KEY)) {
  1256. if (key != NULL) {
  1257. memcpy(ctx->enc_key, key, keylen);
  1258. } else {
  1259. pr_err("%s Invalid key pointer\n", __func__);
  1260. return -EINVAL;
  1261. }
  1262. }
  1263. return 0;
  1264. }
  1265. static int _qcrypto_setkey_aes_xts(struct crypto_skcipher *tfm,
  1266. const u8 *key, unsigned int keylen)
  1267. {
  1268. struct qcrypto_cipher_ctx *ctx = crypto_skcipher_ctx(tfm);
  1269. struct crypto_priv *cp = ctx->cp;
  1270. if ((ctx->flags & QCRYPTO_CTX_USE_HW_KEY) == QCRYPTO_CTX_USE_HW_KEY)
  1271. return 0;
  1272. if (_qcrypto_check_aes_keylen(cp, keylen/2))
  1273. return -EINVAL;
  1274. ctx->enc_key_len = keylen;
  1275. if (!(ctx->flags & QCRYPTO_CTX_USE_PIPE_KEY)) {
  1276. if (key != NULL) {
  1277. memcpy(ctx->enc_key, key, keylen);
  1278. } else {
  1279. pr_err("%s Invalid key pointer\n", __func__);
  1280. return -EINVAL;
  1281. }
  1282. }
  1283. return 0;
  1284. }
  1285. static int _qcrypto_setkey_des(struct crypto_skcipher *tfm, const u8 *key,
  1286. unsigned int keylen)
  1287. {
  1288. struct qcrypto_cipher_ctx *ctx = crypto_skcipher_ctx(tfm);
  1289. struct des_ctx dctx;
  1290. if (!key) {
  1291. pr_err("%s Invalid key pointer\n", __func__);
  1292. return -EINVAL;
  1293. }
  1294. if ((ctx->flags & QCRYPTO_CTX_USE_HW_KEY) == QCRYPTO_CTX_USE_HW_KEY) {
  1295. pr_err("%s HW KEY usage not supported for DES algorithm\n", __func__);
  1296. return 0;
  1297. }
  1298. if (keylen != DES_KEY_SIZE) {
  1299. //crypto_ablkcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
  1300. return -EINVAL;
  1301. }
  1302. memset(&dctx, 0, sizeof(dctx));
  1303. if (des_expand_key(&dctx, key, keylen) == -ENOKEY) {
  1304. if (crypto_skcipher_get_flags(tfm) & CRYPTO_TFM_REQ_FORBID_WEAK_KEYS)
  1305. return -EINVAL;
  1306. else
  1307. return 0;
  1308. }
  1309. /*
  1310. * TODO: delete of find equivalent in skcipher api
  1311. if (ret) {
  1312. tfm->crt_flags |= CRYPTO_TFM_RES_WEAK_KEY;
  1313. crypto_tfm_set_flags(tfm, CRYPTO_TFM_RES_WEAK_KEY);
  1314. return -EINVAL;
  1315. }
  1316. */
  1317. ctx->enc_key_len = keylen;
  1318. if (!(ctx->flags & QCRYPTO_CTX_USE_PIPE_KEY))
  1319. memcpy(ctx->enc_key, key, keylen);
  1320. return 0;
  1321. }
  1322. static int _qcrypto_setkey_3des(struct crypto_skcipher *tfm, const u8 *key,
  1323. unsigned int keylen)
  1324. {
  1325. struct qcrypto_cipher_ctx *ctx = crypto_skcipher_ctx(tfm);
  1326. if ((ctx->flags & QCRYPTO_CTX_USE_HW_KEY) == QCRYPTO_CTX_USE_HW_KEY) {
  1327. pr_err("%s HW KEY usage not supported for 3DES algorithm\n", __func__);
  1328. return 0;
  1329. }
  1330. if (keylen != DES3_EDE_KEY_SIZE) {
  1331. //crypto_ablkcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
  1332. return -EINVAL;
  1333. }
  1334. ctx->enc_key_len = keylen;
  1335. if (!(ctx->flags & QCRYPTO_CTX_USE_PIPE_KEY)) {
  1336. if (key != NULL) {
  1337. memcpy(ctx->enc_key, key, keylen);
  1338. } else {
  1339. pr_err("%s Invalid key pointer\n", __func__);
  1340. return -EINVAL;
  1341. }
  1342. }
  1343. return 0;
  1344. }
  1345. static void seq_response(struct work_struct *work)
  1346. {
  1347. struct crypto_priv *cp = container_of(work, struct crypto_priv,
  1348. resp_work);
  1349. struct llist_node *list;
  1350. struct llist_node *rev = NULL;
  1351. struct crypto_engine *pengine;
  1352. unsigned long flags;
  1353. int total_unit;
  1354. again:
  1355. list = llist_del_all(&cp->ordered_resp_list);
  1356. if (!list)
  1357. goto end;
  1358. while (list) {
  1359. struct llist_node *t = list;
  1360. list = llist_next(list);
  1361. t->next = rev;
  1362. rev = t;
  1363. }
  1364. while (rev) {
  1365. struct qcrypto_resp_ctx *arsp;
  1366. struct crypto_async_request *areq;
  1367. arsp = container_of(rev, struct qcrypto_resp_ctx, llist);
  1368. rev = llist_next(rev);
  1369. areq = arsp->async_req;
  1370. local_bh_disable();
  1371. areq->complete(areq, arsp->res);
  1372. local_bh_enable();
  1373. atomic_dec(&cp->resp_cnt);
  1374. }
  1375. if (atomic_read(&cp->resp_cnt) < COMPLETION_CB_BACKLOG_LENGTH_START &&
  1376. (cmpxchg(&cp->ce_req_proc_sts, STOPPED, IN_PROGRESS)
  1377. == STOPPED)) {
  1378. cp->resp_start++;
  1379. for (total_unit = cp->total_units; total_unit-- > 0;) {
  1380. spin_lock_irqsave(&cp->lock, flags);
  1381. pengine = _avail_eng(cp);
  1382. spin_unlock_irqrestore(&cp->lock, flags);
  1383. if (pengine)
  1384. _start_qcrypto_process(cp, pengine);
  1385. else
  1386. break;
  1387. }
  1388. }
  1389. end:
  1390. if (cmpxchg(&cp->sched_resp_workq_status, SCHEDULE_AGAIN,
  1391. IS_SCHEDULED) == SCHEDULE_AGAIN)
  1392. goto again;
  1393. else if (cmpxchg(&cp->sched_resp_workq_status, IS_SCHEDULED,
  1394. NOT_SCHEDULED) == SCHEDULE_AGAIN)
  1395. goto end;
  1396. }
  1397. #define SCHEUDLE_RSP_QLEN_THRESHOLD 64
  1398. static void _qcrypto_tfm_complete(struct crypto_engine *pengine, u32 type,
  1399. void *tfm_ctx,
  1400. struct qcrypto_resp_ctx *cur_arsp,
  1401. int res)
  1402. {
  1403. struct crypto_priv *cp = pengine->pcp;
  1404. unsigned long flags;
  1405. struct qcrypto_resp_ctx *arsp;
  1406. struct list_head *plist;
  1407. unsigned int resp_qlen;
  1408. unsigned int cnt = 0;
  1409. switch (type) {
  1410. case CRYPTO_ALG_TYPE_AHASH:
  1411. plist = &((struct qcrypto_sha_ctx *) tfm_ctx)->rsp_queue;
  1412. break;
  1413. case CRYPTO_ALG_TYPE_SKCIPHER:
  1414. case CRYPTO_ALG_TYPE_AEAD:
  1415. default:
  1416. plist = &((struct qcrypto_cipher_ctx *) tfm_ctx)->rsp_queue;
  1417. break;
  1418. }
  1419. spin_lock_irqsave(&cp->lock, flags);
  1420. cur_arsp->res = res;
  1421. while (!list_empty(plist)) {
  1422. arsp = list_first_entry(plist,
  1423. struct qcrypto_resp_ctx, list);
  1424. if (arsp->res == -EINPROGRESS)
  1425. break;
  1426. list_del(&arsp->list);
  1427. llist_add(&arsp->llist, &cp->ordered_resp_list);
  1428. atomic_inc(&cp->resp_cnt);
  1429. cnt++;
  1430. }
  1431. resp_qlen = atomic_read(&cp->resp_cnt);
  1432. if (resp_qlen > cp->max_resp_qlen)
  1433. cp->max_resp_qlen = resp_qlen;
  1434. if (cnt > cp->max_reorder_cnt)
  1435. cp->max_reorder_cnt = cnt;
  1436. if ((resp_qlen >= COMPLETION_CB_BACKLOG_LENGTH_STOP) &&
  1437. cmpxchg(&cp->ce_req_proc_sts, IN_PROGRESS,
  1438. STOPPED) == IN_PROGRESS) {
  1439. cp->resp_stop++;
  1440. }
  1441. spin_unlock_irqrestore(&cp->lock, flags);
  1442. retry:
  1443. if (!llist_empty(&cp->ordered_resp_list)) {
  1444. unsigned int cpu;
  1445. if (pengine->first_engine) {
  1446. cpu = WORK_CPU_UNBOUND;
  1447. cp->queue_work_eng3++;
  1448. } else {
  1449. cp->queue_work_not_eng3++;
  1450. cpu = cp->cpu_getting_irqs_frm_first_ce;
  1451. /*
  1452. * If source not the first engine, and there
  1453. * are outstanding requests going on first engine,
  1454. * skip scheduling of work queue to anticipate
  1455. * more may be coming. If the response queue
  1456. * length exceeds threshold, to avoid further
  1457. * delay, schedule work queue immediately.
  1458. */
  1459. if (cp->first_engine && atomic_read(
  1460. &cp->first_engine->req_count)) {
  1461. if (resp_qlen < SCHEUDLE_RSP_QLEN_THRESHOLD)
  1462. return;
  1463. cp->queue_work_not_eng3_nz++;
  1464. }
  1465. }
  1466. if (cmpxchg(&cp->sched_resp_workq_status, NOT_SCHEDULED,
  1467. IS_SCHEDULED) == NOT_SCHEDULED)
  1468. queue_work_on(cpu, cp->resp_wq, &cp->resp_work);
  1469. else if (cmpxchg(&cp->sched_resp_workq_status, IS_SCHEDULED,
  1470. SCHEDULE_AGAIN) == NOT_SCHEDULED)
  1471. goto retry;
  1472. }
  1473. }
  1474. static void req_done(struct qcrypto_req_control *pqcrypto_req_control)
  1475. {
  1476. struct crypto_engine *pengine;
  1477. struct crypto_async_request *areq;
  1478. struct crypto_priv *cp;
  1479. struct qcrypto_resp_ctx *arsp;
  1480. u32 type = 0;
  1481. void *tfm_ctx = NULL;
  1482. unsigned int cpu;
  1483. int res;
  1484. pengine = pqcrypto_req_control->pce;
  1485. cp = pengine->pcp;
  1486. areq = pqcrypto_req_control->req;
  1487. arsp = pqcrypto_req_control->arsp;
  1488. res = pqcrypto_req_control->res;
  1489. qcrypto_free_req_control(pengine, pqcrypto_req_control);
  1490. if (areq) {
  1491. type = crypto_tfm_alg_type(areq->tfm);
  1492. tfm_ctx = crypto_tfm_ctx(areq->tfm);
  1493. }
  1494. cpu = smp_processor_id();
  1495. pengine->irq_cpu = cpu;
  1496. if (pengine->first_engine) {
  1497. if (cpu != cp->cpu_getting_irqs_frm_first_ce)
  1498. cp->cpu_getting_irqs_frm_first_ce = cpu;
  1499. }
  1500. if (areq)
  1501. _qcrypto_tfm_complete(pengine, type, tfm_ctx, arsp, res);
  1502. if (READ_ONCE(cp->ce_req_proc_sts) == IN_PROGRESS)
  1503. _start_qcrypto_process(cp, pengine);
  1504. }
  1505. static void _qce_ahash_complete(void *cookie, unsigned char *digest,
  1506. unsigned char *authdata, int ret)
  1507. {
  1508. struct ahash_request *areq = (struct ahash_request *) cookie;
  1509. struct crypto_async_request *async_req;
  1510. struct crypto_ahash *ahash = crypto_ahash_reqtfm(areq);
  1511. struct qcrypto_sha_ctx *sha_ctx = crypto_tfm_ctx(areq->base.tfm);
  1512. struct qcrypto_sha_req_ctx *rctx = ahash_request_ctx(areq);
  1513. struct crypto_priv *cp = sha_ctx->cp;
  1514. struct crypto_stat *pstat;
  1515. uint32_t diglen = crypto_ahash_digestsize(ahash);
  1516. uint32_t *auth32 = (uint32_t *)authdata;
  1517. struct crypto_engine *pengine;
  1518. struct qcrypto_req_control *pqcrypto_req_control;
  1519. async_req = &areq->base;
  1520. pstat = &_qcrypto_stat;
  1521. pengine = rctx->pengine;
  1522. pqcrypto_req_control = find_req_control_for_areq(pengine,
  1523. async_req);
  1524. if (pqcrypto_req_control == NULL) {
  1525. pr_err("async request not found\n");
  1526. return;
  1527. }
  1528. #ifdef QCRYPTO_DEBUG
  1529. dev_info(&pengine->pdev->dev, "%s: %pK ret %d\n",
  1530. __func__, areq, ret);
  1531. #endif
  1532. if (digest) {
  1533. memcpy(rctx->digest, digest, diglen);
  1534. if (rctx->last_blk)
  1535. memcpy(areq->result, digest, diglen);
  1536. }
  1537. if (authdata) {
  1538. rctx->byte_count[0] = auth32[0];
  1539. rctx->byte_count[1] = auth32[1];
  1540. rctx->byte_count[2] = auth32[2];
  1541. rctx->byte_count[3] = auth32[3];
  1542. }
  1543. areq->src = rctx->src;
  1544. areq->nbytes = rctx->nbytes;
  1545. rctx->last_blk = 0;
  1546. rctx->first_blk = 0;
  1547. if (ret) {
  1548. pqcrypto_req_control->res = -ENXIO;
  1549. pstat->ahash_op_fail++;
  1550. } else {
  1551. pqcrypto_req_control->res = 0;
  1552. pstat->ahash_op_success++;
  1553. }
  1554. if (cp->ce_support.aligned_only) {
  1555. areq->src = rctx->orig_src;
  1556. kfree(rctx->data);
  1557. }
  1558. req_done(pqcrypto_req_control);
  1559. }
  1560. static void _qce_sk_cipher_complete(void *cookie, unsigned char *icb,
  1561. unsigned char *iv, int ret)
  1562. {
  1563. struct skcipher_request *areq = (struct skcipher_request *) cookie;
  1564. struct crypto_async_request *async_req;
  1565. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
  1566. struct qcrypto_cipher_ctx *ctx = crypto_skcipher_ctx(tfm);
  1567. struct crypto_priv *cp = ctx->cp;
  1568. struct crypto_stat *pstat;
  1569. struct qcrypto_cipher_req_ctx *rctx;
  1570. struct crypto_engine *pengine;
  1571. struct qcrypto_req_control *pqcrypto_req_control;
  1572. async_req = &areq->base;
  1573. pstat = &_qcrypto_stat;
  1574. rctx = skcipher_request_ctx(areq);
  1575. pengine = rctx->pengine;
  1576. pqcrypto_req_control = find_req_control_for_areq(pengine,
  1577. async_req);
  1578. if (pqcrypto_req_control == NULL) {
  1579. pr_err("async request not found\n");
  1580. return;
  1581. }
  1582. #ifdef QCRYPTO_DEBUG
  1583. dev_info(&pengine->pdev->dev, "%s: %pK ret %d\n",
  1584. __func__, areq, ret);
  1585. #endif
  1586. if (iv)
  1587. memcpy(ctx->iv, iv, crypto_skcipher_ivsize(tfm));
  1588. if (ret) {
  1589. pqcrypto_req_control->res = -ENXIO;
  1590. pstat->sk_cipher_op_fail++;
  1591. } else {
  1592. pqcrypto_req_control->res = 0;
  1593. pstat->sk_cipher_op_success++;
  1594. }
  1595. if (cp->ce_support.aligned_only) {
  1596. struct qcrypto_cipher_req_ctx *rctx;
  1597. uint32_t num_sg = 0;
  1598. uint32_t bytes = 0;
  1599. rctx = skcipher_request_ctx(areq);
  1600. areq->src = rctx->orig_src;
  1601. areq->dst = rctx->orig_dst;
  1602. num_sg = qcrypto_count_sg(areq->dst, areq->cryptlen);
  1603. bytes = qcrypto_sg_copy_from_buffer(areq->dst, num_sg,
  1604. rctx->data, areq->cryptlen);
  1605. if (bytes != areq->cryptlen)
  1606. pr_warn("bytes copied=0x%x bytes to copy= 0x%x\n",
  1607. bytes, areq->cryptlen);
  1608. kfree_sensitive(rctx->data);
  1609. }
  1610. req_done(pqcrypto_req_control);
  1611. }
  1612. static void _qce_aead_complete(void *cookie, unsigned char *icv,
  1613. unsigned char *iv, int ret)
  1614. {
  1615. struct aead_request *areq = (struct aead_request *) cookie;
  1616. struct crypto_async_request *async_req;
  1617. struct crypto_aead *aead = crypto_aead_reqtfm(areq);
  1618. struct qcrypto_cipher_ctx *ctx = crypto_tfm_ctx(areq->base.tfm);
  1619. struct qcrypto_cipher_req_ctx *rctx;
  1620. struct crypto_stat *pstat;
  1621. struct crypto_engine *pengine;
  1622. struct qcrypto_req_control *pqcrypto_req_control;
  1623. async_req = &areq->base;
  1624. pstat = &_qcrypto_stat;
  1625. rctx = aead_request_ctx(areq);
  1626. pengine = rctx->pengine;
  1627. pqcrypto_req_control = find_req_control_for_areq(pengine,
  1628. async_req);
  1629. if (pqcrypto_req_control == NULL) {
  1630. pr_err("async request not found\n");
  1631. return;
  1632. }
  1633. if (rctx->mode == QCE_MODE_CCM) {
  1634. kfree_sensitive(rctx->adata);
  1635. } else {
  1636. uint32_t ivsize = crypto_aead_ivsize(aead);
  1637. if (ret == 0) {
  1638. if (rctx->dir == QCE_ENCRYPT) {
  1639. /* copy the icv to dst */
  1640. scatterwalk_map_and_copy(icv, areq->dst,
  1641. areq->cryptlen + areq->assoclen,
  1642. ctx->authsize, 1);
  1643. } else {
  1644. unsigned char tmp[SHA256_DIGESTSIZE] = {0};
  1645. /* compare icv from src */
  1646. scatterwalk_map_and_copy(tmp,
  1647. areq->src, areq->assoclen +
  1648. areq->cryptlen - ctx->authsize,
  1649. ctx->authsize, 0);
  1650. ret = memcmp(icv, tmp, ctx->authsize);
  1651. if (ret != 0)
  1652. ret = -EBADMSG;
  1653. }
  1654. } else {
  1655. ret = -ENXIO;
  1656. }
  1657. if (iv)
  1658. memcpy(ctx->iv, iv, ivsize);
  1659. }
  1660. if (ret == (-EBADMSG))
  1661. pstat->aead_bad_msg++;
  1662. else if (ret)
  1663. pstat->aead_op_fail++;
  1664. else
  1665. pstat->aead_op_success++;
  1666. pqcrypto_req_control->res = ret;
  1667. req_done(pqcrypto_req_control);
  1668. }
  1669. static int aead_ccm_set_msg_len(u8 *block, unsigned int msglen, int csize)
  1670. {
  1671. __be32 data;
  1672. memset(block, 0, csize);
  1673. block += csize;
  1674. if (csize >= 4)
  1675. csize = 4;
  1676. else if (msglen > (1 << (8 * csize)))
  1677. return -EOVERFLOW;
  1678. data = cpu_to_be32(msglen);
  1679. memcpy(block - csize, (u8 *)&data + 4 - csize, csize);
  1680. return 0;
  1681. }
  1682. static int qccrypto_set_aead_ccm_nonce(struct qce_req *qreq, uint32_t assoclen)
  1683. {
  1684. unsigned int i = ((unsigned int)qreq->iv[0]) + 1;
  1685. memcpy(&qreq->nonce[0], qreq->iv, qreq->ivsize);
  1686. /*
  1687. * Format control info per RFC 3610 and
  1688. * NIST Special Publication 800-38C
  1689. */
  1690. qreq->nonce[0] |= (8 * ((qreq->authsize - 2) / 2));
  1691. if (assoclen)
  1692. qreq->nonce[0] |= 64;
  1693. if (i > MAX_NONCE)
  1694. return -EINVAL;
  1695. return aead_ccm_set_msg_len(qreq->nonce + 16 - i, qreq->cryptlen, i);
  1696. }
  1697. static int qcrypto_aead_ccm_format_adata(struct qce_req *qreq, uint32_t alen,
  1698. struct scatterlist *sg, unsigned char *adata)
  1699. {
  1700. uint32_t len;
  1701. uint32_t bytes = 0;
  1702. uint32_t num_sg = 0;
  1703. /*
  1704. * Add control info for associated data
  1705. * RFC 3610 and NIST Special Publication 800-38C
  1706. */
  1707. if (alen < 65280) {
  1708. *(__be16 *)adata = cpu_to_be16(alen);
  1709. len = 2;
  1710. } else {
  1711. if ((alen >= 65280) && (alen <= 0xffffffff)) {
  1712. *(__be16 *)adata = cpu_to_be16(0xfffe);
  1713. *(__be32 *)&adata[2] = cpu_to_be32(alen);
  1714. len = 6;
  1715. } else {
  1716. *(__be16 *)adata = cpu_to_be16(0xffff);
  1717. *(__be32 *)&adata[6] = cpu_to_be32(alen);
  1718. len = 10;
  1719. }
  1720. }
  1721. adata += len;
  1722. qreq->assoclen = ALIGN((alen + len), 16);
  1723. num_sg = qcrypto_count_sg(sg, alen);
  1724. bytes = qcrypto_sg_copy_to_buffer(sg, num_sg, adata, alen);
  1725. if (bytes != alen)
  1726. pr_warn("bytes copied=0x%x bytes to copy= 0x%x\n", bytes, alen);
  1727. return 0;
  1728. }
  1729. static int _qcrypto_process_skcipher(struct crypto_engine *pengine,
  1730. struct qcrypto_req_control *pqcrypto_req_control)
  1731. {
  1732. struct crypto_async_request *async_req;
  1733. struct qce_req qreq;
  1734. int ret;
  1735. struct qcrypto_cipher_req_ctx *rctx;
  1736. struct qcrypto_cipher_ctx *cipher_ctx;
  1737. struct skcipher_request *req;
  1738. struct crypto_skcipher *tfm;
  1739. async_req = pqcrypto_req_control->req;
  1740. req = container_of(async_req, struct skcipher_request, base);
  1741. cipher_ctx = crypto_tfm_ctx(async_req->tfm);
  1742. rctx = skcipher_request_ctx(req);
  1743. rctx->pengine = pengine;
  1744. tfm = crypto_skcipher_reqtfm(req);
  1745. if (pengine->pcp->ce_support.aligned_only) {
  1746. uint32_t bytes = 0;
  1747. uint32_t num_sg = 0;
  1748. rctx->orig_src = req->src;
  1749. rctx->orig_dst = req->dst;
  1750. rctx->data = kzalloc((req->cryptlen + 64), GFP_ATOMIC);
  1751. if (rctx->data == NULL)
  1752. return -ENOMEM;
  1753. num_sg = qcrypto_count_sg(req->src, req->cryptlen);
  1754. bytes = qcrypto_sg_copy_to_buffer(req->src, num_sg, rctx->data,
  1755. req->cryptlen);
  1756. if (bytes != req->cryptlen)
  1757. pr_warn("bytes copied=0x%x bytes to copy= 0x%x\n",
  1758. bytes, req->cryptlen);
  1759. sg_set_buf(&rctx->dsg, rctx->data, req->cryptlen);
  1760. sg_mark_end(&rctx->dsg);
  1761. rctx->iv = req->iv;
  1762. req->src = &rctx->dsg;
  1763. req->dst = &rctx->dsg;
  1764. }
  1765. qreq.op = QCE_REQ_ABLK_CIPHER; //TODO: change name in qcedev.h
  1766. qreq.qce_cb = _qce_sk_cipher_complete;
  1767. qreq.areq = req;
  1768. qreq.alg = rctx->alg;
  1769. qreq.dir = rctx->dir;
  1770. qreq.mode = rctx->mode;
  1771. qreq.enckey = cipher_ctx->enc_key;
  1772. qreq.encklen = cipher_ctx->enc_key_len;
  1773. qreq.iv = req->iv;
  1774. qreq.ivsize = crypto_skcipher_ivsize(tfm);
  1775. qreq.cryptlen = req->cryptlen;
  1776. qreq.use_pmem = 0;
  1777. qreq.flags = cipher_ctx->flags;
  1778. if ((cipher_ctx->enc_key_len == 0) &&
  1779. (pengine->pcp->platform_support.hw_key_support == 0))
  1780. ret = -EINVAL;
  1781. else
  1782. ret = qce_ablk_cipher_req(pengine->qce, &qreq); //maybe change name?
  1783. return ret;
  1784. }
  1785. static int _qcrypto_process_ahash(struct crypto_engine *pengine,
  1786. struct qcrypto_req_control *pqcrypto_req_control)
  1787. {
  1788. struct crypto_async_request *async_req;
  1789. struct ahash_request *req;
  1790. struct qce_sha_req sreq;
  1791. struct qcrypto_sha_req_ctx *rctx;
  1792. struct qcrypto_sha_ctx *sha_ctx;
  1793. int ret = 0;
  1794. async_req = pqcrypto_req_control->req;
  1795. req = container_of(async_req,
  1796. struct ahash_request, base);
  1797. rctx = ahash_request_ctx(req);
  1798. sha_ctx = crypto_tfm_ctx(async_req->tfm);
  1799. rctx->pengine = pengine;
  1800. sreq.qce_cb = _qce_ahash_complete;
  1801. sreq.digest = &rctx->digest[0];
  1802. sreq.src = req->src;
  1803. sreq.auth_data[0] = rctx->byte_count[0];
  1804. sreq.auth_data[1] = rctx->byte_count[1];
  1805. sreq.auth_data[2] = rctx->byte_count[2];
  1806. sreq.auth_data[3] = rctx->byte_count[3];
  1807. sreq.first_blk = rctx->first_blk;
  1808. sreq.last_blk = rctx->last_blk;
  1809. sreq.size = req->nbytes;
  1810. sreq.areq = req;
  1811. sreq.flags = sha_ctx->flags;
  1812. switch (sha_ctx->alg) {
  1813. case QCE_HASH_SHA1:
  1814. sreq.alg = QCE_HASH_SHA1;
  1815. sreq.authkey = NULL;
  1816. break;
  1817. case QCE_HASH_SHA256:
  1818. sreq.alg = QCE_HASH_SHA256;
  1819. sreq.authkey = NULL;
  1820. break;
  1821. case QCE_HASH_SHA1_HMAC:
  1822. sreq.alg = QCE_HASH_SHA1_HMAC;
  1823. sreq.authkey = &sha_ctx->authkey[0];
  1824. sreq.authklen = SHA_HMAC_KEY_SIZE;
  1825. break;
  1826. case QCE_HASH_SHA256_HMAC:
  1827. sreq.alg = QCE_HASH_SHA256_HMAC;
  1828. sreq.authkey = &sha_ctx->authkey[0];
  1829. sreq.authklen = SHA_HMAC_KEY_SIZE;
  1830. break;
  1831. default:
  1832. pr_err("Algorithm %d not supported, exiting\n", sha_ctx->alg);
  1833. ret = -1;
  1834. break;
  1835. }
  1836. ret = qce_process_sha_req(pengine->qce, &sreq);
  1837. return ret;
  1838. }
  1839. static int _qcrypto_process_aead(struct crypto_engine *pengine,
  1840. struct qcrypto_req_control *pqcrypto_req_control)
  1841. {
  1842. struct crypto_async_request *async_req;
  1843. struct qce_req qreq;
  1844. int ret = 0;
  1845. struct qcrypto_cipher_req_ctx *rctx;
  1846. struct qcrypto_cipher_ctx *cipher_ctx;
  1847. struct aead_request *req;
  1848. struct crypto_aead *aead;
  1849. async_req = pqcrypto_req_control->req;
  1850. req = container_of(async_req, struct aead_request, base);
  1851. aead = crypto_aead_reqtfm(req);
  1852. rctx = aead_request_ctx(req);
  1853. rctx->pengine = pengine;
  1854. cipher_ctx = crypto_tfm_ctx(async_req->tfm);
  1855. qreq.op = QCE_REQ_AEAD;
  1856. qreq.qce_cb = _qce_aead_complete;
  1857. qreq.areq = req;
  1858. qreq.alg = rctx->alg;
  1859. qreq.dir = rctx->dir;
  1860. qreq.mode = rctx->mode;
  1861. qreq.iv = rctx->iv;
  1862. qreq.enckey = cipher_ctx->enc_key;
  1863. qreq.encklen = cipher_ctx->enc_key_len;
  1864. qreq.authkey = cipher_ctx->auth_key;
  1865. qreq.authklen = cipher_ctx->auth_key_len;
  1866. qreq.authsize = crypto_aead_authsize(aead);
  1867. qreq.auth_alg = cipher_ctx->auth_alg;
  1868. if (qreq.mode == QCE_MODE_CCM)
  1869. qreq.ivsize = AES_BLOCK_SIZE;
  1870. else
  1871. qreq.ivsize = crypto_aead_ivsize(aead);
  1872. qreq.flags = cipher_ctx->flags;
  1873. if (qreq.mode == QCE_MODE_CCM) {
  1874. uint32_t assoclen;
  1875. if (qreq.dir == QCE_ENCRYPT)
  1876. qreq.cryptlen = req->cryptlen;
  1877. else
  1878. qreq.cryptlen = req->cryptlen -
  1879. qreq.authsize;
  1880. /* if rfc4309 ccm, adjust assoclen */
  1881. assoclen = req->assoclen;
  1882. if (rctx->ccmtype)
  1883. assoclen -= 8;
  1884. /* Get NONCE */
  1885. ret = qccrypto_set_aead_ccm_nonce(&qreq, assoclen);
  1886. if (ret)
  1887. return ret;
  1888. if (assoclen) {
  1889. rctx->adata = kzalloc((assoclen + 0x64),
  1890. GFP_ATOMIC);
  1891. if (!rctx->adata)
  1892. return -ENOMEM;
  1893. /* Format Associated data */
  1894. ret = qcrypto_aead_ccm_format_adata(&qreq,
  1895. assoclen,
  1896. req->src,
  1897. rctx->adata);
  1898. } else {
  1899. qreq.assoclen = 0;
  1900. rctx->adata = NULL;
  1901. }
  1902. if (ret) {
  1903. kfree_sensitive(rctx->adata);
  1904. return ret;
  1905. }
  1906. /*
  1907. * update req with new formatted associated
  1908. * data info
  1909. */
  1910. qreq.asg = &rctx->asg;
  1911. if (rctx->adata)
  1912. sg_set_buf(qreq.asg, rctx->adata,
  1913. qreq.assoclen);
  1914. sg_mark_end(qreq.asg);
  1915. }
  1916. ret = qce_aead_req(pengine->qce, &qreq);
  1917. return ret;
  1918. }
  1919. static struct crypto_engine *_qcrypto_static_assign_engine(
  1920. struct crypto_priv *cp)
  1921. {
  1922. struct crypto_engine *pengine;
  1923. unsigned long flags;
  1924. spin_lock_irqsave(&cp->lock, flags);
  1925. if (cp->next_engine)
  1926. pengine = cp->next_engine;
  1927. else
  1928. pengine = list_first_entry(&cp->engine_list,
  1929. struct crypto_engine, elist);
  1930. if (list_is_last(&pengine->elist, &cp->engine_list))
  1931. cp->next_engine = list_first_entry(
  1932. &cp->engine_list, struct crypto_engine, elist);
  1933. else
  1934. cp->next_engine = list_next_entry(pengine, elist);
  1935. spin_unlock_irqrestore(&cp->lock, flags);
  1936. return pengine;
  1937. }
  1938. static int _start_qcrypto_process(struct crypto_priv *cp,
  1939. struct crypto_engine *pengine)
  1940. {
  1941. struct crypto_async_request *async_req = NULL;
  1942. struct crypto_async_request *backlog_eng = NULL;
  1943. struct crypto_async_request *backlog_cp = NULL;
  1944. unsigned long flags;
  1945. u32 type;
  1946. int ret = 0;
  1947. struct crypto_stat *pstat;
  1948. void *tfm_ctx;
  1949. struct qcrypto_cipher_req_ctx *cipher_rctx;
  1950. struct qcrypto_sha_req_ctx *ahash_rctx;
  1951. struct skcipher_request *skcipher_req;
  1952. struct ahash_request *ahash_req;
  1953. struct aead_request *aead_req;
  1954. struct qcrypto_resp_ctx *arsp;
  1955. struct qcrypto_req_control *pqcrypto_req_control;
  1956. unsigned int cpu = MAX_SMP_CPU;
  1957. if (READ_ONCE(cp->ce_req_proc_sts) == STOPPED)
  1958. return 0;
  1959. if (in_interrupt()) {
  1960. cpu = smp_processor_id();
  1961. if (cpu >= MAX_SMP_CPU)
  1962. cpu = MAX_SMP_CPU - 1;
  1963. } else
  1964. cpu = MAX_SMP_CPU;
  1965. pstat = &_qcrypto_stat;
  1966. again:
  1967. spin_lock_irqsave(&cp->lock, flags);
  1968. if (pengine->issue_req ||
  1969. atomic_read(&pengine->req_count) >= (pengine->max_req)) {
  1970. spin_unlock_irqrestore(&cp->lock, flags);
  1971. return 0;
  1972. }
  1973. backlog_eng = crypto_get_backlog(&pengine->req_queue);
  1974. /* make sure it is in high bandwidth state */
  1975. if (pengine->bw_state != BUS_HAS_BANDWIDTH) {
  1976. spin_unlock_irqrestore(&cp->lock, flags);
  1977. return 0;
  1978. }
  1979. /* try to get request from request queue of the engine first */
  1980. async_req = crypto_dequeue_request(&pengine->req_queue);
  1981. if (!async_req) {
  1982. /*
  1983. * if no request from the engine,
  1984. * try to get from request queue of driver
  1985. */
  1986. backlog_cp = crypto_get_backlog(&cp->req_queue);
  1987. async_req = crypto_dequeue_request(&cp->req_queue);
  1988. if (!async_req) {
  1989. spin_unlock_irqrestore(&cp->lock, flags);
  1990. return 0;
  1991. }
  1992. }
  1993. pqcrypto_req_control = qcrypto_alloc_req_control(pengine);
  1994. if (pqcrypto_req_control == NULL) {
  1995. pr_err("Allocation of request failed\n");
  1996. spin_unlock_irqrestore(&cp->lock, flags);
  1997. return 0;
  1998. }
  1999. /* add associated rsp entry to tfm response queue */
  2000. type = crypto_tfm_alg_type(async_req->tfm);
  2001. tfm_ctx = crypto_tfm_ctx(async_req->tfm);
  2002. switch (type) {
  2003. case CRYPTO_ALG_TYPE_AHASH:
  2004. ahash_req = container_of(async_req,
  2005. struct ahash_request, base);
  2006. ahash_rctx = ahash_request_ctx(ahash_req);
  2007. arsp = &ahash_rctx->rsp_entry;
  2008. list_add_tail(
  2009. &arsp->list,
  2010. &((struct qcrypto_sha_ctx *)tfm_ctx)
  2011. ->rsp_queue);
  2012. break;
  2013. case CRYPTO_ALG_TYPE_SKCIPHER:
  2014. skcipher_req = container_of(async_req,
  2015. struct skcipher_request, base);
  2016. cipher_rctx = skcipher_request_ctx(skcipher_req);
  2017. arsp = &cipher_rctx->rsp_entry;
  2018. list_add_tail(
  2019. &arsp->list,
  2020. &((struct qcrypto_cipher_ctx *)tfm_ctx)
  2021. ->rsp_queue);
  2022. break;
  2023. case CRYPTO_ALG_TYPE_AEAD:
  2024. default:
  2025. aead_req = container_of(async_req,
  2026. struct aead_request, base);
  2027. cipher_rctx = aead_request_ctx(aead_req);
  2028. arsp = &cipher_rctx->rsp_entry;
  2029. list_add_tail(
  2030. &arsp->list,
  2031. &((struct qcrypto_cipher_ctx *)tfm_ctx)
  2032. ->rsp_queue);
  2033. break;
  2034. }
  2035. arsp->res = -EINPROGRESS;
  2036. arsp->async_req = async_req;
  2037. pqcrypto_req_control->pce = pengine;
  2038. pqcrypto_req_control->req = async_req;
  2039. pqcrypto_req_control->arsp = arsp;
  2040. pengine->active_seq++;
  2041. pengine->check_flag = true;
  2042. pengine->issue_req = true;
  2043. cp->cpu_req[cpu]++;
  2044. smp_mb(); /* make it visible */
  2045. spin_unlock_irqrestore(&cp->lock, flags);
  2046. if (backlog_eng)
  2047. backlog_eng->complete(backlog_eng, -EINPROGRESS);
  2048. if (backlog_cp)
  2049. backlog_cp->complete(backlog_cp, -EINPROGRESS);
  2050. switch (type) {
  2051. case CRYPTO_ALG_TYPE_SKCIPHER:
  2052. ret = _qcrypto_process_skcipher(pengine, pqcrypto_req_control);
  2053. break;
  2054. case CRYPTO_ALG_TYPE_AHASH:
  2055. ret = _qcrypto_process_ahash(pengine, pqcrypto_req_control);
  2056. break;
  2057. case CRYPTO_ALG_TYPE_AEAD:
  2058. ret = _qcrypto_process_aead(pengine, pqcrypto_req_control);
  2059. break;
  2060. default:
  2061. ret = -EINVAL;
  2062. }
  2063. pengine->issue_req = false;
  2064. smp_mb(); /* make it visible */
  2065. pengine->total_req++;
  2066. if (ret) {
  2067. pengine->err_req++;
  2068. qcrypto_free_req_control(pengine, pqcrypto_req_control);
  2069. if (type == CRYPTO_ALG_TYPE_SKCIPHER)
  2070. pstat->sk_cipher_op_fail++;
  2071. else
  2072. if (type == CRYPTO_ALG_TYPE_AHASH)
  2073. pstat->ahash_op_fail++;
  2074. else
  2075. pstat->aead_op_fail++;
  2076. _qcrypto_tfm_complete(pengine, type, tfm_ctx, arsp, ret);
  2077. goto again;
  2078. }
  2079. return ret;
  2080. }
  2081. static inline struct crypto_engine *_next_eng(struct crypto_priv *cp,
  2082. struct crypto_engine *p)
  2083. {
  2084. if (p == NULL || list_is_last(&p->elist, &cp->engine_list))
  2085. p = list_first_entry(&cp->engine_list, struct crypto_engine,
  2086. elist);
  2087. else
  2088. p = list_entry(p->elist.next, struct crypto_engine, elist);
  2089. return p;
  2090. }
  2091. static struct crypto_engine *_avail_eng(struct crypto_priv *cp)
  2092. {
  2093. /* call this function with spinlock set */
  2094. struct crypto_engine *q = NULL;
  2095. struct crypto_engine *p = cp->scheduled_eng;
  2096. struct crypto_engine *q1;
  2097. int eng_cnt = cp->total_units;
  2098. if (unlikely(list_empty(&cp->engine_list))) {
  2099. pr_err("%s: no valid ce to schedule\n", __func__);
  2100. return NULL;
  2101. }
  2102. p = _next_eng(cp, p);
  2103. q1 = p;
  2104. while (eng_cnt-- > 0) {
  2105. if (!p->issue_req && atomic_read(&p->req_count) < p->max_req) {
  2106. q = p;
  2107. break;
  2108. }
  2109. p = _next_eng(cp, p);
  2110. if (q1 == p)
  2111. break;
  2112. }
  2113. cp->scheduled_eng = q;
  2114. return q;
  2115. }
  2116. static int _qcrypto_queue_req(struct crypto_priv *cp,
  2117. struct crypto_engine *pengine,
  2118. struct crypto_async_request *req)
  2119. {
  2120. int ret;
  2121. unsigned long flags;
  2122. spin_lock_irqsave(&cp->lock, flags);
  2123. if (pengine) {
  2124. ret = crypto_enqueue_request(&pengine->req_queue, req);
  2125. } else {
  2126. ret = crypto_enqueue_request(&cp->req_queue, req);
  2127. pengine = _avail_eng(cp);
  2128. if (cp->req_queue.qlen > cp->max_qlen)
  2129. cp->max_qlen = cp->req_queue.qlen;
  2130. }
  2131. if (pengine) {
  2132. switch (pengine->bw_state) {
  2133. case BUS_NO_BANDWIDTH:
  2134. if (!pengine->high_bw_req) {
  2135. qcrypto_ce_bw_allocate_req(pengine);
  2136. pengine->high_bw_req = true;
  2137. }
  2138. pengine = NULL;
  2139. break;
  2140. case BUS_HAS_BANDWIDTH:
  2141. break;
  2142. case BUS_BANDWIDTH_RELEASING:
  2143. pengine->high_bw_req = true;
  2144. pengine = NULL;
  2145. break;
  2146. case BUS_BANDWIDTH_ALLOCATING:
  2147. pengine = NULL;
  2148. break;
  2149. case BUS_SUSPENDED:
  2150. case BUS_SUSPENDING:
  2151. default:
  2152. pengine = NULL;
  2153. break;
  2154. }
  2155. } else {
  2156. cp->no_avail++;
  2157. }
  2158. spin_unlock_irqrestore(&cp->lock, flags);
  2159. if (pengine && (READ_ONCE(cp->ce_req_proc_sts) == IN_PROGRESS))
  2160. _start_qcrypto_process(cp, pengine);
  2161. return ret;
  2162. }
  2163. static int _qcrypto_enc_aes_192_fallback(struct skcipher_request *req)
  2164. {
  2165. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  2166. struct qcrypto_cipher_ctx *ctx = crypto_skcipher_ctx(tfm);
  2167. int err;
  2168. SYNC_SKCIPHER_REQUEST_ON_STACK(subreq, ctx->cipher_aes192_fb);
  2169. skcipher_request_set_sync_tfm(subreq, ctx->cipher_aes192_fb);
  2170. skcipher_request_set_callback(subreq, req->base.flags, NULL, NULL);
  2171. skcipher_request_set_crypt(subreq, req->src, req->dst,
  2172. req->cryptlen, req->iv);
  2173. err = crypto_skcipher_encrypt(subreq);
  2174. skcipher_request_zero(subreq);
  2175. return err;
  2176. }
  2177. static int _qcrypto_dec_aes_192_fallback(struct skcipher_request *req)
  2178. {
  2179. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  2180. struct qcrypto_cipher_ctx *ctx = crypto_skcipher_ctx(tfm);
  2181. int err;
  2182. SYNC_SKCIPHER_REQUEST_ON_STACK(subreq, ctx->cipher_aes192_fb);
  2183. skcipher_request_set_sync_tfm(subreq, ctx->cipher_aes192_fb);
  2184. skcipher_request_set_callback(subreq, req->base.flags, NULL, NULL);
  2185. skcipher_request_set_crypt(subreq, req->src, req->dst,
  2186. req->cryptlen, req->iv);
  2187. err = crypto_skcipher_decrypt(subreq);
  2188. skcipher_request_zero(subreq);
  2189. return err;
  2190. }
  2191. static int _qcrypto_enc_aes_ecb(struct skcipher_request *req)
  2192. {
  2193. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  2194. struct qcrypto_cipher_ctx *ctx = crypto_skcipher_ctx(tfm);
  2195. struct qcrypto_cipher_req_ctx *rctx;
  2196. struct crypto_priv *cp = ctx->cp;
  2197. struct crypto_stat *pstat = &_qcrypto_stat;
  2198. WARN_ON(crypto_tfm_alg_type(&tfm->base) != CRYPTO_ALG_TYPE_SKCIPHER);
  2199. #ifdef QCRYPTO_DEBUG
  2200. dev_info(&ctx->pengine->pdev->dev, "%s: %pK\n", __func__, req);
  2201. #endif
  2202. if ((ctx->enc_key_len == AES_KEYSIZE_192) &&
  2203. (!cp->ce_support.aes_key_192) &&
  2204. ctx->cipher_aes192_fb)
  2205. return _qcrypto_enc_aes_192_fallback(req);
  2206. rctx = skcipher_request_ctx(req);
  2207. rctx->aead = 0;
  2208. rctx->alg = CIPHER_ALG_AES;
  2209. rctx->dir = QCE_ENCRYPT;
  2210. rctx->mode = QCE_MODE_ECB;
  2211. pstat->sk_cipher_aes_enc++;
  2212. return _qcrypto_queue_req(cp, ctx->pengine, &req->base);
  2213. }
  2214. static int _qcrypto_enc_aes_cbc(struct skcipher_request *req)
  2215. {
  2216. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  2217. struct qcrypto_cipher_ctx *ctx = crypto_skcipher_ctx(tfm);
  2218. struct qcrypto_cipher_req_ctx *rctx;
  2219. struct crypto_priv *cp = ctx->cp;
  2220. struct crypto_stat *pstat = &_qcrypto_stat;
  2221. WARN_ON(crypto_tfm_alg_type(&tfm->base) != CRYPTO_ALG_TYPE_SKCIPHER);
  2222. #ifdef QCRYPTO_DEBUG
  2223. dev_info(&ctx->pengine->pdev->dev, "%s: %pK\n", __func__, req);
  2224. #endif
  2225. if ((ctx->enc_key_len == AES_KEYSIZE_192) &&
  2226. (!cp->ce_support.aes_key_192) &&
  2227. ctx->cipher_aes192_fb)
  2228. return _qcrypto_enc_aes_192_fallback(req);
  2229. rctx = skcipher_request_ctx(req);
  2230. rctx->aead = 0;
  2231. rctx->alg = CIPHER_ALG_AES;
  2232. rctx->dir = QCE_ENCRYPT;
  2233. rctx->mode = QCE_MODE_CBC;
  2234. pstat->sk_cipher_aes_enc++;
  2235. return _qcrypto_queue_req(cp, ctx->pengine, &req->base);
  2236. }
  2237. static int _qcrypto_enc_aes_ctr(struct skcipher_request *req)
  2238. {
  2239. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  2240. struct qcrypto_cipher_ctx *ctx = crypto_skcipher_ctx(tfm);
  2241. struct qcrypto_cipher_req_ctx *rctx;
  2242. struct crypto_priv *cp = ctx->cp;
  2243. struct crypto_stat *pstat = &_qcrypto_stat;
  2244. WARN_ON(crypto_tfm_alg_type(&tfm->base) != CRYPTO_ALG_TYPE_SKCIPHER);
  2245. #ifdef QCRYPTO_DEBUG
  2246. dev_info(&ctx->pengine->pdev->dev, "%s: %pK\n", __func__, req);
  2247. #endif
  2248. if ((ctx->enc_key_len == AES_KEYSIZE_192) &&
  2249. (!cp->ce_support.aes_key_192) &&
  2250. ctx->cipher_aes192_fb)
  2251. return _qcrypto_enc_aes_192_fallback(req);
  2252. rctx = skcipher_request_ctx(req);
  2253. rctx->aead = 0;
  2254. rctx->alg = CIPHER_ALG_AES;
  2255. rctx->dir = QCE_ENCRYPT;
  2256. rctx->mode = QCE_MODE_CTR;
  2257. pstat->sk_cipher_aes_enc++;
  2258. return _qcrypto_queue_req(cp, ctx->pengine, &req->base);
  2259. }
  2260. static int _qcrypto_enc_aes_xts(struct skcipher_request *req)
  2261. {
  2262. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  2263. struct qcrypto_cipher_ctx *ctx = crypto_skcipher_ctx(tfm);
  2264. struct qcrypto_cipher_req_ctx *rctx;
  2265. struct crypto_stat *pstat = &_qcrypto_stat;
  2266. struct crypto_priv *cp = ctx->cp;
  2267. WARN_ON(crypto_tfm_alg_type(&tfm->base) != CRYPTO_ALG_TYPE_SKCIPHER);
  2268. rctx = skcipher_request_ctx(req);
  2269. rctx->aead = 0;
  2270. rctx->alg = CIPHER_ALG_AES;
  2271. rctx->dir = QCE_ENCRYPT;
  2272. rctx->mode = QCE_MODE_XTS;
  2273. pstat->sk_cipher_aes_enc++;
  2274. return _qcrypto_queue_req(cp, ctx->pengine, &req->base);
  2275. }
  2276. static int _qcrypto_aead_encrypt_aes_ccm(struct aead_request *req)
  2277. {
  2278. struct qcrypto_cipher_req_ctx *rctx;
  2279. struct qcrypto_cipher_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  2280. struct crypto_priv *cp = ctx->cp;
  2281. struct crypto_stat *pstat;
  2282. if ((ctx->authsize > 16) || (ctx->authsize < 4) || (ctx->authsize & 1))
  2283. return -EINVAL;
  2284. if ((ctx->auth_key_len != AES_KEYSIZE_128) &&
  2285. (ctx->auth_key_len != AES_KEYSIZE_256))
  2286. return -EINVAL;
  2287. pstat = &_qcrypto_stat;
  2288. rctx = aead_request_ctx(req);
  2289. rctx->aead = 1;
  2290. rctx->alg = CIPHER_ALG_AES;
  2291. rctx->dir = QCE_ENCRYPT;
  2292. rctx->mode = QCE_MODE_CCM;
  2293. rctx->iv = req->iv;
  2294. rctx->ccmtype = 0;
  2295. pstat->aead_ccm_aes_enc++;
  2296. return _qcrypto_queue_req(cp, ctx->pengine, &req->base);
  2297. }
  2298. static int _qcrypto_aead_rfc4309_enc_aes_ccm(struct aead_request *req)
  2299. {
  2300. struct qcrypto_cipher_req_ctx *rctx;
  2301. struct qcrypto_cipher_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  2302. struct crypto_priv *cp = ctx->cp;
  2303. struct crypto_stat *pstat;
  2304. pstat = &_qcrypto_stat;
  2305. if (req->assoclen != 16 && req->assoclen != 20)
  2306. return -EINVAL;
  2307. rctx = aead_request_ctx(req);
  2308. rctx->aead = 1;
  2309. rctx->alg = CIPHER_ALG_AES;
  2310. rctx->dir = QCE_ENCRYPT;
  2311. rctx->mode = QCE_MODE_CCM;
  2312. memset(rctx->rfc4309_iv, 0, sizeof(rctx->rfc4309_iv));
  2313. rctx->rfc4309_iv[0] = 3; /* L -1 */
  2314. memcpy(&rctx->rfc4309_iv[1], ctx->ccm4309_nonce, 3);
  2315. memcpy(&rctx->rfc4309_iv[4], req->iv, 8);
  2316. rctx->ccmtype = 1;
  2317. rctx->iv = rctx->rfc4309_iv;
  2318. pstat->aead_rfc4309_ccm_aes_enc++;
  2319. return _qcrypto_queue_req(cp, ctx->pengine, &req->base);
  2320. }
  2321. static int _qcrypto_enc_des_ecb(struct skcipher_request *req)
  2322. {
  2323. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  2324. struct qcrypto_cipher_ctx *ctx = crypto_skcipher_ctx(tfm);
  2325. struct qcrypto_cipher_req_ctx *rctx;
  2326. struct crypto_priv *cp = ctx->cp;
  2327. struct crypto_stat *pstat = &_qcrypto_stat;
  2328. WARN_ON(crypto_tfm_alg_type(&tfm->base) != CRYPTO_ALG_TYPE_SKCIPHER);
  2329. rctx = skcipher_request_ctx(req);
  2330. rctx->aead = 0;
  2331. rctx->alg = CIPHER_ALG_DES;
  2332. rctx->dir = QCE_ENCRYPT;
  2333. rctx->mode = QCE_MODE_ECB;
  2334. pstat->sk_cipher_des_enc++;
  2335. return _qcrypto_queue_req(cp, ctx->pengine, &req->base);
  2336. }
  2337. static int _qcrypto_enc_des_cbc(struct skcipher_request *req)
  2338. {
  2339. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  2340. struct qcrypto_cipher_ctx *ctx = crypto_skcipher_ctx(tfm);
  2341. struct qcrypto_cipher_req_ctx *rctx;
  2342. struct crypto_priv *cp = ctx->cp;
  2343. struct crypto_stat *pstat = &_qcrypto_stat;
  2344. WARN_ON(crypto_tfm_alg_type(&tfm->base) != CRYPTO_ALG_TYPE_SKCIPHER);
  2345. rctx = skcipher_request_ctx(req);
  2346. rctx->aead = 0;
  2347. rctx->alg = CIPHER_ALG_DES;
  2348. rctx->dir = QCE_ENCRYPT;
  2349. rctx->mode = QCE_MODE_CBC;
  2350. pstat->sk_cipher_des_enc++;
  2351. return _qcrypto_queue_req(cp, ctx->pengine, &req->base);
  2352. }
  2353. static int _qcrypto_enc_3des_ecb(struct skcipher_request *req)
  2354. {
  2355. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  2356. struct qcrypto_cipher_ctx *ctx = crypto_skcipher_ctx(tfm);
  2357. struct qcrypto_cipher_req_ctx *rctx;
  2358. struct crypto_priv *cp = ctx->cp;
  2359. struct crypto_stat *pstat = &_qcrypto_stat;
  2360. WARN_ON(crypto_tfm_alg_type(&tfm->base) != CRYPTO_ALG_TYPE_SKCIPHER);
  2361. rctx = skcipher_request_ctx(req);
  2362. rctx->aead = 0;
  2363. rctx->alg = CIPHER_ALG_3DES;
  2364. rctx->dir = QCE_ENCRYPT;
  2365. rctx->mode = QCE_MODE_ECB;
  2366. pstat->sk_cipher_3des_enc++;
  2367. return _qcrypto_queue_req(cp, ctx->pengine, &req->base);
  2368. }
  2369. static int _qcrypto_enc_3des_cbc(struct skcipher_request *req)
  2370. {
  2371. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  2372. struct qcrypto_cipher_ctx *ctx = crypto_skcipher_ctx(tfm);
  2373. struct qcrypto_cipher_req_ctx *rctx;
  2374. struct crypto_priv *cp = ctx->cp;
  2375. struct crypto_stat *pstat = &_qcrypto_stat;
  2376. WARN_ON(crypto_tfm_alg_type(&tfm->base) != CRYPTO_ALG_TYPE_SKCIPHER);
  2377. rctx = skcipher_request_ctx(req);
  2378. rctx->aead = 0;
  2379. rctx->alg = CIPHER_ALG_3DES;
  2380. rctx->dir = QCE_ENCRYPT;
  2381. rctx->mode = QCE_MODE_CBC;
  2382. pstat->sk_cipher_3des_enc++;
  2383. return _qcrypto_queue_req(cp, ctx->pengine, &req->base);
  2384. }
  2385. static int _qcrypto_dec_aes_ecb(struct skcipher_request *req)
  2386. {
  2387. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  2388. struct qcrypto_cipher_ctx *ctx = crypto_skcipher_ctx(tfm);
  2389. struct qcrypto_cipher_req_ctx *rctx;
  2390. struct crypto_priv *cp = ctx->cp;
  2391. struct crypto_stat *pstat = &_qcrypto_stat;
  2392. WARN_ON(crypto_tfm_alg_type(&tfm->base) != CRYPTO_ALG_TYPE_SKCIPHER);
  2393. #ifdef QCRYPTO_DEBUG
  2394. dev_info(&ctx->pengine->pdev->dev, "%s: %pK\n", __func__, req);
  2395. #endif
  2396. if ((ctx->enc_key_len == AES_KEYSIZE_192) &&
  2397. (!cp->ce_support.aes_key_192) &&
  2398. ctx->cipher_aes192_fb)
  2399. return _qcrypto_dec_aes_192_fallback(req);
  2400. rctx = skcipher_request_ctx(req);
  2401. rctx->aead = 0;
  2402. rctx->alg = CIPHER_ALG_AES;
  2403. rctx->dir = QCE_DECRYPT;
  2404. rctx->mode = QCE_MODE_ECB;
  2405. pstat->sk_cipher_aes_dec++;
  2406. return _qcrypto_queue_req(cp, ctx->pengine, &req->base);
  2407. }
  2408. static int _qcrypto_dec_aes_cbc(struct skcipher_request *req)
  2409. {
  2410. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  2411. struct qcrypto_cipher_ctx *ctx = crypto_skcipher_ctx(tfm);
  2412. struct qcrypto_cipher_req_ctx *rctx;
  2413. struct crypto_priv *cp = ctx->cp;
  2414. struct crypto_stat *pstat = &_qcrypto_stat;
  2415. WARN_ON(crypto_tfm_alg_type(&tfm->base) != CRYPTO_ALG_TYPE_SKCIPHER);
  2416. #ifdef QCRYPTO_DEBUG
  2417. dev_info(&ctx->pengine->pdev->dev, "%s: %pK\n", __func__, req);
  2418. #endif
  2419. if ((ctx->enc_key_len == AES_KEYSIZE_192) &&
  2420. (!cp->ce_support.aes_key_192) &&
  2421. ctx->cipher_aes192_fb)
  2422. return _qcrypto_dec_aes_192_fallback(req);
  2423. rctx = skcipher_request_ctx(req);
  2424. rctx->aead = 0;
  2425. rctx->alg = CIPHER_ALG_AES;
  2426. rctx->dir = QCE_DECRYPT;
  2427. rctx->mode = QCE_MODE_CBC;
  2428. pstat->sk_cipher_aes_dec++;
  2429. return _qcrypto_queue_req(cp, ctx->pengine, &req->base);
  2430. }
  2431. static int _qcrypto_dec_aes_ctr(struct skcipher_request *req)
  2432. {
  2433. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  2434. struct qcrypto_cipher_ctx *ctx = crypto_skcipher_ctx(tfm);
  2435. struct qcrypto_cipher_req_ctx *rctx;
  2436. struct crypto_priv *cp = ctx->cp;
  2437. struct crypto_stat *pstat = &_qcrypto_stat;
  2438. WARN_ON(crypto_tfm_alg_type(&tfm->base) != CRYPTO_ALG_TYPE_SKCIPHER);
  2439. #ifdef QCRYPTO_DEBUG
  2440. dev_info(&ctx->pengine->pdev->dev, "%s: %pK\n", __func__, req);
  2441. #endif
  2442. if ((ctx->enc_key_len == AES_KEYSIZE_192) &&
  2443. (!cp->ce_support.aes_key_192) &&
  2444. ctx->cipher_aes192_fb)
  2445. return _qcrypto_dec_aes_192_fallback(req);
  2446. rctx = skcipher_request_ctx(req);
  2447. rctx->aead = 0;
  2448. rctx->alg = CIPHER_ALG_AES;
  2449. rctx->mode = QCE_MODE_CTR;
  2450. /* Note. There is no such thing as aes/counter mode, decrypt */
  2451. rctx->dir = QCE_ENCRYPT;
  2452. pstat->sk_cipher_aes_dec++;
  2453. return _qcrypto_queue_req(cp, ctx->pengine, &req->base);
  2454. }
  2455. static int _qcrypto_dec_des_ecb(struct skcipher_request *req)
  2456. {
  2457. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  2458. struct qcrypto_cipher_ctx *ctx = crypto_skcipher_ctx(tfm);
  2459. struct qcrypto_cipher_req_ctx *rctx;
  2460. struct crypto_priv *cp = ctx->cp;
  2461. struct crypto_stat *pstat = &_qcrypto_stat;
  2462. WARN_ON(crypto_tfm_alg_type(&tfm->base) != CRYPTO_ALG_TYPE_SKCIPHER);
  2463. rctx = skcipher_request_ctx(req);
  2464. rctx->aead = 0;
  2465. rctx->alg = CIPHER_ALG_DES;
  2466. rctx->dir = QCE_DECRYPT;
  2467. rctx->mode = QCE_MODE_ECB;
  2468. pstat->sk_cipher_des_dec++;
  2469. return _qcrypto_queue_req(cp, ctx->pengine, &req->base);
  2470. }
  2471. static int _qcrypto_dec_des_cbc(struct skcipher_request *req)
  2472. {
  2473. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  2474. struct qcrypto_cipher_ctx *ctx = crypto_skcipher_ctx(tfm);
  2475. struct qcrypto_cipher_req_ctx *rctx;
  2476. struct crypto_priv *cp = ctx->cp;
  2477. struct crypto_stat *pstat = &_qcrypto_stat;
  2478. WARN_ON(crypto_tfm_alg_type(&tfm->base) != CRYPTO_ALG_TYPE_SKCIPHER);
  2479. rctx = skcipher_request_ctx(req);
  2480. rctx->aead = 0;
  2481. rctx->alg = CIPHER_ALG_DES;
  2482. rctx->dir = QCE_DECRYPT;
  2483. rctx->mode = QCE_MODE_CBC;
  2484. pstat->sk_cipher_des_dec++;
  2485. return _qcrypto_queue_req(cp, ctx->pengine, &req->base);
  2486. }
  2487. static int _qcrypto_dec_3des_ecb(struct skcipher_request *req)
  2488. {
  2489. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  2490. struct qcrypto_cipher_ctx *ctx = crypto_skcipher_ctx(tfm);
  2491. struct qcrypto_cipher_req_ctx *rctx;
  2492. struct crypto_priv *cp = ctx->cp;
  2493. struct crypto_stat *pstat = &_qcrypto_stat;
  2494. WARN_ON(crypto_tfm_alg_type(&tfm->base) != CRYPTO_ALG_TYPE_SKCIPHER);
  2495. rctx = skcipher_request_ctx(req);
  2496. rctx->aead = 0;
  2497. rctx->alg = CIPHER_ALG_3DES;
  2498. rctx->dir = QCE_DECRYPT;
  2499. rctx->mode = QCE_MODE_ECB;
  2500. pstat->sk_cipher_3des_dec++;
  2501. return _qcrypto_queue_req(cp, ctx->pengine, &req->base);
  2502. }
  2503. static int _qcrypto_dec_3des_cbc(struct skcipher_request *req)
  2504. {
  2505. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  2506. struct qcrypto_cipher_ctx *ctx = crypto_skcipher_ctx(tfm);
  2507. struct qcrypto_cipher_req_ctx *rctx;
  2508. struct crypto_priv *cp = ctx->cp;
  2509. struct crypto_stat *pstat = &_qcrypto_stat;
  2510. WARN_ON(crypto_tfm_alg_type(&tfm->base) != CRYPTO_ALG_TYPE_SKCIPHER);
  2511. rctx = skcipher_request_ctx(req);
  2512. rctx->aead = 0;
  2513. rctx->alg = CIPHER_ALG_3DES;
  2514. rctx->dir = QCE_DECRYPT;
  2515. rctx->mode = QCE_MODE_CBC;
  2516. pstat->sk_cipher_3des_dec++;
  2517. return _qcrypto_queue_req(cp, ctx->pengine, &req->base);
  2518. }
  2519. static int _qcrypto_dec_aes_xts(struct skcipher_request *req)
  2520. {
  2521. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  2522. struct qcrypto_cipher_ctx *ctx = crypto_skcipher_ctx(tfm);
  2523. struct qcrypto_cipher_req_ctx *rctx;
  2524. struct crypto_priv *cp = ctx->cp;
  2525. struct crypto_stat *pstat = &_qcrypto_stat;
  2526. WARN_ON(crypto_tfm_alg_type(&tfm->base) != CRYPTO_ALG_TYPE_SKCIPHER);
  2527. rctx = skcipher_request_ctx(req);
  2528. rctx->aead = 0;
  2529. rctx->alg = CIPHER_ALG_AES;
  2530. rctx->mode = QCE_MODE_XTS;
  2531. rctx->dir = QCE_DECRYPT;
  2532. pstat->sk_cipher_aes_dec++;
  2533. return _qcrypto_queue_req(cp, ctx->pengine, &req->base);
  2534. }
  2535. static int _qcrypto_aead_decrypt_aes_ccm(struct aead_request *req)
  2536. {
  2537. struct qcrypto_cipher_req_ctx *rctx;
  2538. struct qcrypto_cipher_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  2539. struct crypto_priv *cp = ctx->cp;
  2540. struct crypto_stat *pstat;
  2541. if ((ctx->authsize > 16) || (ctx->authsize < 4) || (ctx->authsize & 1))
  2542. return -EINVAL;
  2543. if ((ctx->auth_key_len != AES_KEYSIZE_128) &&
  2544. (ctx->auth_key_len != AES_KEYSIZE_256))
  2545. return -EINVAL;
  2546. pstat = &_qcrypto_stat;
  2547. rctx = aead_request_ctx(req);
  2548. rctx->aead = 1;
  2549. rctx->alg = CIPHER_ALG_AES;
  2550. rctx->dir = QCE_DECRYPT;
  2551. rctx->mode = QCE_MODE_CCM;
  2552. rctx->iv = req->iv;
  2553. rctx->ccmtype = 0;
  2554. pstat->aead_ccm_aes_dec++;
  2555. return _qcrypto_queue_req(cp, ctx->pengine, &req->base);
  2556. }
  2557. static int _qcrypto_aead_rfc4309_dec_aes_ccm(struct aead_request *req)
  2558. {
  2559. struct qcrypto_cipher_req_ctx *rctx;
  2560. struct qcrypto_cipher_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  2561. struct crypto_priv *cp = ctx->cp;
  2562. struct crypto_stat *pstat;
  2563. pstat = &_qcrypto_stat;
  2564. if (req->assoclen != 16 && req->assoclen != 20)
  2565. return -EINVAL;
  2566. rctx = aead_request_ctx(req);
  2567. rctx->aead = 1;
  2568. rctx->alg = CIPHER_ALG_AES;
  2569. rctx->dir = QCE_DECRYPT;
  2570. rctx->mode = QCE_MODE_CCM;
  2571. memset(rctx->rfc4309_iv, 0, sizeof(rctx->rfc4309_iv));
  2572. rctx->rfc4309_iv[0] = 3; /* L -1 */
  2573. memcpy(&rctx->rfc4309_iv[1], ctx->ccm4309_nonce, 3);
  2574. memcpy(&rctx->rfc4309_iv[4], req->iv, 8);
  2575. rctx->ccmtype = 1;
  2576. rctx->iv = rctx->rfc4309_iv;
  2577. pstat->aead_rfc4309_ccm_aes_dec++;
  2578. return _qcrypto_queue_req(cp, ctx->pengine, &req->base);
  2579. }
  2580. static int _qcrypto_aead_setauthsize(struct crypto_aead *authenc,
  2581. unsigned int authsize)
  2582. {
  2583. struct qcrypto_cipher_ctx *ctx = crypto_aead_ctx(authenc);
  2584. ctx->authsize = authsize;
  2585. return 0;
  2586. }
  2587. static int _qcrypto_aead_ccm_setauthsize(struct crypto_aead *authenc,
  2588. unsigned int authsize)
  2589. {
  2590. struct qcrypto_cipher_ctx *ctx = crypto_aead_ctx(authenc);
  2591. switch (authsize) {
  2592. case 4:
  2593. case 6:
  2594. case 8:
  2595. case 10:
  2596. case 12:
  2597. case 14:
  2598. case 16:
  2599. break;
  2600. default:
  2601. return -EINVAL;
  2602. }
  2603. ctx->authsize = authsize;
  2604. return 0;
  2605. }
  2606. static int _qcrypto_aead_rfc4309_ccm_setauthsize(struct crypto_aead *authenc,
  2607. unsigned int authsize)
  2608. {
  2609. struct qcrypto_cipher_ctx *ctx = crypto_aead_ctx(authenc);
  2610. switch (authsize) {
  2611. case 8:
  2612. case 12:
  2613. case 16:
  2614. break;
  2615. default:
  2616. return -EINVAL;
  2617. }
  2618. ctx->authsize = authsize;
  2619. return 0;
  2620. }
  2621. static int _qcrypto_aead_setkey(struct crypto_aead *tfm, const u8 *key,
  2622. unsigned int keylen)
  2623. {
  2624. struct qcrypto_cipher_ctx *ctx = crypto_aead_ctx(tfm);
  2625. struct rtattr *rta = (struct rtattr *)key;
  2626. struct crypto_authenc_key_param *param;
  2627. int ret;
  2628. if (!RTA_OK(rta, keylen))
  2629. goto badkey;
  2630. if (rta->rta_type != CRYPTO_AUTHENC_KEYA_PARAM)
  2631. goto badkey;
  2632. if (RTA_PAYLOAD(rta) < sizeof(*param))
  2633. goto badkey;
  2634. param = RTA_DATA(rta);
  2635. ctx->enc_key_len = be32_to_cpu(param->enckeylen);
  2636. key += RTA_ALIGN(rta->rta_len);
  2637. keylen -= RTA_ALIGN(rta->rta_len);
  2638. if (keylen < ctx->enc_key_len)
  2639. goto badkey;
  2640. ctx->auth_key_len = keylen - ctx->enc_key_len;
  2641. if (ctx->enc_key_len >= QCRYPTO_MAX_KEY_SIZE ||
  2642. ctx->auth_key_len >= QCRYPTO_MAX_KEY_SIZE)
  2643. goto badkey;
  2644. memset(ctx->auth_key, 0, QCRYPTO_MAX_KEY_SIZE);
  2645. memcpy(ctx->enc_key, key + ctx->auth_key_len, ctx->enc_key_len);
  2646. memcpy(ctx->auth_key, key, ctx->auth_key_len);
  2647. if (ctx->enc_key_len == AES_KEYSIZE_192 && ctx->cipher_aes192_fb &&
  2648. ctx->ahash_aead_aes192_fb) {
  2649. crypto_ahash_clear_flags(ctx->ahash_aead_aes192_fb, ~0);
  2650. ret = crypto_ahash_setkey(ctx->ahash_aead_aes192_fb,
  2651. ctx->auth_key, ctx->auth_key_len);
  2652. if (ret)
  2653. goto badkey;
  2654. crypto_sync_skcipher_clear_flags(ctx->cipher_aes192_fb, ~0);
  2655. ret = crypto_sync_skcipher_setkey(ctx->cipher_aes192_fb,
  2656. ctx->enc_key, ctx->enc_key_len);
  2657. if (ret)
  2658. goto badkey;
  2659. }
  2660. return 0;
  2661. badkey:
  2662. ctx->enc_key_len = 0;
  2663. //crypto_aead_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
  2664. return -EINVAL;
  2665. }
  2666. static int _qcrypto_aead_ccm_setkey(struct crypto_aead *aead, const u8 *key,
  2667. unsigned int keylen)
  2668. {
  2669. struct crypto_tfm *tfm = crypto_aead_tfm(aead);
  2670. struct qcrypto_cipher_ctx *ctx = crypto_tfm_ctx(tfm);
  2671. struct crypto_priv *cp = ctx->cp;
  2672. switch (keylen) {
  2673. case AES_KEYSIZE_128:
  2674. case AES_KEYSIZE_256:
  2675. break;
  2676. case AES_KEYSIZE_192:
  2677. if (cp->ce_support.aes_key_192)
  2678. break;
  2679. default:
  2680. ctx->enc_key_len = 0;
  2681. //crypto_aead_set_flags(aead, CRYPTO_TFM_RES_BAD_KEY_LEN);
  2682. return -EINVAL;
  2683. }
  2684. ctx->enc_key_len = keylen;
  2685. memcpy(ctx->enc_key, key, keylen);
  2686. ctx->auth_key_len = keylen;
  2687. memcpy(ctx->auth_key, key, keylen);
  2688. return 0;
  2689. }
  2690. static int _qcrypto_aead_rfc4309_ccm_setkey(struct crypto_aead *aead,
  2691. const u8 *key, unsigned int key_len)
  2692. {
  2693. struct crypto_tfm *tfm = crypto_aead_tfm(aead);
  2694. struct qcrypto_cipher_ctx *ctx = crypto_tfm_ctx(tfm);
  2695. int ret;
  2696. if (key_len < QCRYPTO_CCM4309_NONCE_LEN)
  2697. return -EINVAL;
  2698. key_len -= QCRYPTO_CCM4309_NONCE_LEN;
  2699. memcpy(ctx->ccm4309_nonce, key + key_len, QCRYPTO_CCM4309_NONCE_LEN);
  2700. ret = _qcrypto_aead_ccm_setkey(aead, key, key_len);
  2701. return ret;
  2702. }
  2703. static void _qcrypto_aead_aes_192_fb_a_cb(struct qcrypto_cipher_req_ctx *rctx,
  2704. int res)
  2705. {
  2706. struct aead_request *req;
  2707. struct crypto_async_request *areq;
  2708. req = rctx->aead_req;
  2709. areq = &req->base;
  2710. if (rctx->fb_aes_req)
  2711. skcipher_request_free(rctx->fb_aes_req);
  2712. if (rctx->fb_hash_req)
  2713. ahash_request_free(rctx->fb_hash_req);
  2714. rctx->fb_aes_req = NULL;
  2715. rctx->fb_hash_req = NULL;
  2716. kfree(rctx->fb_aes_iv);
  2717. areq->complete(areq, res);
  2718. }
  2719. static void _aead_aes_fb_stage2_ahash_complete(
  2720. struct crypto_async_request *base, int err)
  2721. {
  2722. struct qcrypto_cipher_req_ctx *rctx;
  2723. struct aead_request *req;
  2724. struct qcrypto_cipher_ctx *ctx;
  2725. rctx = base->data;
  2726. req = rctx->aead_req;
  2727. ctx = crypto_tfm_ctx(req->base.tfm);
  2728. /* copy icv */
  2729. if (err == 0)
  2730. scatterwalk_map_and_copy(rctx->fb_ahash_digest,
  2731. rctx->fb_aes_dst,
  2732. req->cryptlen,
  2733. ctx->authsize, 1);
  2734. _qcrypto_aead_aes_192_fb_a_cb(rctx, err);
  2735. }
  2736. static int _start_aead_aes_fb_stage2_hmac(struct qcrypto_cipher_req_ctx *rctx)
  2737. {
  2738. struct ahash_request *ahash_req;
  2739. ahash_req = rctx->fb_hash_req;
  2740. ahash_request_set_callback(ahash_req, CRYPTO_TFM_REQ_MAY_BACKLOG,
  2741. _aead_aes_fb_stage2_ahash_complete, rctx);
  2742. return crypto_ahash_digest(ahash_req);
  2743. }
  2744. static void _aead_aes_fb_stage2_decrypt_complete(
  2745. struct crypto_async_request *base, int err)
  2746. {
  2747. struct qcrypto_cipher_req_ctx *rctx;
  2748. rctx = base->data;
  2749. _qcrypto_aead_aes_192_fb_a_cb(rctx, err);
  2750. }
  2751. static int _start_aead_aes_fb_stage2_decrypt(
  2752. struct qcrypto_cipher_req_ctx *rctx)
  2753. {
  2754. struct skcipher_request *aes_req;
  2755. aes_req = rctx->fb_aes_req;
  2756. skcipher_request_set_callback(aes_req, CRYPTO_TFM_REQ_MAY_BACKLOG,
  2757. _aead_aes_fb_stage2_decrypt_complete, rctx);
  2758. return crypto_skcipher_decrypt(aes_req);
  2759. }
  2760. static void _aead_aes_fb_stage1_ahash_complete(
  2761. struct crypto_async_request *base, int err)
  2762. {
  2763. struct qcrypto_cipher_req_ctx *rctx;
  2764. struct aead_request *req;
  2765. struct qcrypto_cipher_ctx *ctx;
  2766. rctx = base->data;
  2767. req = rctx->aead_req;
  2768. ctx = crypto_tfm_ctx(req->base.tfm);
  2769. /* compare icv */
  2770. if (err == 0) {
  2771. unsigned char *tmp;
  2772. tmp = kmalloc(ctx->authsize, GFP_KERNEL);
  2773. if (!tmp) {
  2774. err = -ENOMEM;
  2775. goto ret;
  2776. }
  2777. scatterwalk_map_and_copy(tmp, rctx->fb_aes_src,
  2778. req->cryptlen - ctx->authsize, ctx->authsize, 0);
  2779. if (memcmp(rctx->fb_ahash_digest, tmp, ctx->authsize) != 0)
  2780. err = -EBADMSG;
  2781. kfree(tmp);
  2782. }
  2783. ret:
  2784. if (err)
  2785. _qcrypto_aead_aes_192_fb_a_cb(rctx, err);
  2786. else {
  2787. err = _start_aead_aes_fb_stage2_decrypt(rctx);
  2788. if (err != -EINPROGRESS && err != -EBUSY)
  2789. _qcrypto_aead_aes_192_fb_a_cb(rctx, err);
  2790. }
  2791. }
  2792. static void _aead_aes_fb_stage1_encrypt_complete(
  2793. struct crypto_async_request *base, int err)
  2794. {
  2795. struct qcrypto_cipher_req_ctx *rctx;
  2796. struct aead_request *req;
  2797. struct qcrypto_cipher_ctx *ctx;
  2798. rctx = base->data;
  2799. req = rctx->aead_req;
  2800. ctx = crypto_tfm_ctx(req->base.tfm);
  2801. memcpy(ctx->iv, rctx->fb_aes_iv, rctx->ivsize);
  2802. if (err) {
  2803. _qcrypto_aead_aes_192_fb_a_cb(rctx, err);
  2804. return;
  2805. }
  2806. err = _start_aead_aes_fb_stage2_hmac(rctx);
  2807. /* copy icv */
  2808. if (err == 0) {
  2809. scatterwalk_map_and_copy(rctx->fb_ahash_digest,
  2810. rctx->fb_aes_dst,
  2811. req->cryptlen,
  2812. ctx->authsize, 1);
  2813. }
  2814. if (err != -EINPROGRESS && err != -EBUSY)
  2815. _qcrypto_aead_aes_192_fb_a_cb(rctx, err);
  2816. }
  2817. static int _qcrypto_aead_aes_192_fallback(struct aead_request *req,
  2818. bool is_encrypt)
  2819. {
  2820. int rc = -EINVAL;
  2821. struct qcrypto_cipher_req_ctx *rctx = aead_request_ctx(req);
  2822. struct qcrypto_cipher_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  2823. struct crypto_aead *aead_tfm = crypto_aead_reqtfm(req);
  2824. struct skcipher_request *aes_req = NULL;
  2825. struct ahash_request *ahash_req = NULL;
  2826. int nbytes;
  2827. struct scatterlist *src, *dst;
  2828. rctx->fb_aes_iv = NULL;
  2829. aes_req = skcipher_request_alloc(&ctx->cipher_aes192_fb->base,
  2830. GFP_KERNEL);
  2831. if (!aes_req)
  2832. return -ENOMEM;
  2833. ahash_req = ahash_request_alloc(ctx->ahash_aead_aes192_fb, GFP_KERNEL);
  2834. if (!ahash_req)
  2835. goto ret;
  2836. rctx->fb_aes_req = aes_req;
  2837. rctx->fb_hash_req = ahash_req;
  2838. rctx->aead_req = req;
  2839. /* assoc and iv are sitting in the beginning of src sg list */
  2840. /* Similarly, assoc and iv are sitting in the beginning of dst list */
  2841. src = scatterwalk_ffwd(rctx->fb_ablkcipher_src_sg, req->src,
  2842. req->assoclen);
  2843. dst = scatterwalk_ffwd(rctx->fb_ablkcipher_dst_sg, req->dst,
  2844. req->assoclen);
  2845. nbytes = req->cryptlen;
  2846. if (!is_encrypt)
  2847. nbytes -= ctx->authsize;
  2848. rctx->fb_ahash_length = nbytes + req->assoclen;
  2849. rctx->fb_aes_src = src;
  2850. rctx->fb_aes_dst = dst;
  2851. rctx->fb_aes_cryptlen = nbytes;
  2852. rctx->ivsize = crypto_aead_ivsize(aead_tfm);
  2853. rctx->fb_aes_iv = kmemdup(req->iv, rctx->ivsize, GFP_ATOMIC);
  2854. if (!rctx->fb_aes_iv)
  2855. goto ret;
  2856. skcipher_request_set_crypt(aes_req, rctx->fb_aes_src,
  2857. rctx->fb_aes_dst,
  2858. rctx->fb_aes_cryptlen, rctx->fb_aes_iv);
  2859. if (is_encrypt)
  2860. ahash_request_set_crypt(ahash_req, req->dst,
  2861. rctx->fb_ahash_digest,
  2862. rctx->fb_ahash_length);
  2863. else
  2864. ahash_request_set_crypt(ahash_req, req->src,
  2865. rctx->fb_ahash_digest,
  2866. rctx->fb_ahash_length);
  2867. if (is_encrypt) {
  2868. skcipher_request_set_callback(aes_req,
  2869. CRYPTO_TFM_REQ_MAY_BACKLOG,
  2870. _aead_aes_fb_stage1_encrypt_complete, rctx);
  2871. rc = crypto_skcipher_encrypt(aes_req);
  2872. if (rc == 0) {
  2873. memcpy(ctx->iv, rctx->fb_aes_iv, rctx->ivsize);
  2874. rc = _start_aead_aes_fb_stage2_hmac(rctx);
  2875. if (rc == 0) {
  2876. /* copy icv */
  2877. scatterwalk_map_and_copy(rctx->fb_ahash_digest,
  2878. dst,
  2879. req->cryptlen,
  2880. ctx->authsize, 1);
  2881. }
  2882. }
  2883. if (rc == -EINPROGRESS || rc == -EBUSY)
  2884. return rc;
  2885. goto ret;
  2886. } else {
  2887. ahash_request_set_callback(ahash_req,
  2888. CRYPTO_TFM_REQ_MAY_BACKLOG,
  2889. _aead_aes_fb_stage1_ahash_complete, rctx);
  2890. rc = crypto_ahash_digest(ahash_req);
  2891. if (rc == 0) {
  2892. unsigned char *tmp;
  2893. tmp = kmalloc(ctx->authsize, GFP_KERNEL);
  2894. if (!tmp) {
  2895. rc = -ENOMEM;
  2896. goto ret;
  2897. }
  2898. /* compare icv */
  2899. scatterwalk_map_and_copy(tmp,
  2900. src, req->cryptlen - ctx->authsize,
  2901. ctx->authsize, 0);
  2902. if (memcmp(rctx->fb_ahash_digest, tmp,
  2903. ctx->authsize) != 0)
  2904. rc = -EBADMSG;
  2905. else
  2906. rc = _start_aead_aes_fb_stage2_decrypt(rctx);
  2907. kfree(tmp);
  2908. }
  2909. if (rc == -EINPROGRESS || rc == -EBUSY)
  2910. return rc;
  2911. goto ret;
  2912. }
  2913. ret:
  2914. if (aes_req)
  2915. skcipher_request_free(aes_req);
  2916. if (ahash_req)
  2917. ahash_request_free(ahash_req);
  2918. kfree(rctx->fb_aes_iv);
  2919. return rc;
  2920. }
  2921. static int _qcrypto_aead_encrypt_aes_cbc(struct aead_request *req)
  2922. {
  2923. struct qcrypto_cipher_req_ctx *rctx;
  2924. struct qcrypto_cipher_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  2925. struct crypto_priv *cp = ctx->cp;
  2926. struct crypto_stat *pstat;
  2927. pstat = &_qcrypto_stat;
  2928. #ifdef QCRYPTO_DEBUG
  2929. dev_info(&ctx->pengine->pdev->dev, "%s: %pK\n", __func__, req);
  2930. #endif
  2931. rctx = aead_request_ctx(req);
  2932. rctx->aead = 1;
  2933. rctx->alg = CIPHER_ALG_AES;
  2934. rctx->dir = QCE_ENCRYPT;
  2935. rctx->mode = QCE_MODE_CBC;
  2936. rctx->iv = req->iv;
  2937. rctx->aead_req = req;
  2938. if (ctx->auth_alg == QCE_HASH_SHA1_HMAC)
  2939. pstat->aead_sha1_aes_enc++;
  2940. else
  2941. pstat->aead_sha256_aes_enc++;
  2942. if (ctx->enc_key_len == AES_KEYSIZE_192 && ctx->cipher_aes192_fb &&
  2943. ctx->ahash_aead_aes192_fb)
  2944. return _qcrypto_aead_aes_192_fallback(req, true);
  2945. return _qcrypto_queue_req(cp, ctx->pengine, &req->base);
  2946. }
  2947. static int _qcrypto_aead_decrypt_aes_cbc(struct aead_request *req)
  2948. {
  2949. struct qcrypto_cipher_req_ctx *rctx;
  2950. struct qcrypto_cipher_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  2951. struct crypto_priv *cp = ctx->cp;
  2952. struct crypto_stat *pstat;
  2953. pstat = &_qcrypto_stat;
  2954. #ifdef QCRYPTO_DEBUG
  2955. dev_info(&ctx->pengine->pdev->dev, "%s: %pK\n", __func__, req);
  2956. #endif
  2957. rctx = aead_request_ctx(req);
  2958. rctx->aead = 1;
  2959. rctx->alg = CIPHER_ALG_AES;
  2960. rctx->dir = QCE_DECRYPT;
  2961. rctx->mode = QCE_MODE_CBC;
  2962. rctx->iv = req->iv;
  2963. rctx->aead_req = req;
  2964. if (ctx->auth_alg == QCE_HASH_SHA1_HMAC)
  2965. pstat->aead_sha1_aes_dec++;
  2966. else
  2967. pstat->aead_sha256_aes_dec++;
  2968. if (ctx->enc_key_len == AES_KEYSIZE_192 && ctx->cipher_aes192_fb &&
  2969. ctx->ahash_aead_aes192_fb)
  2970. return _qcrypto_aead_aes_192_fallback(req, false);
  2971. return _qcrypto_queue_req(cp, ctx->pengine, &req->base);
  2972. }
  2973. static int _qcrypto_aead_encrypt_des_cbc(struct aead_request *req)
  2974. {
  2975. struct qcrypto_cipher_req_ctx *rctx;
  2976. struct qcrypto_cipher_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  2977. struct crypto_priv *cp = ctx->cp;
  2978. struct crypto_stat *pstat;
  2979. pstat = &_qcrypto_stat;
  2980. rctx = aead_request_ctx(req);
  2981. rctx->aead = 1;
  2982. rctx->alg = CIPHER_ALG_DES;
  2983. rctx->dir = QCE_ENCRYPT;
  2984. rctx->mode = QCE_MODE_CBC;
  2985. rctx->iv = req->iv;
  2986. if (ctx->auth_alg == QCE_HASH_SHA1_HMAC)
  2987. pstat->aead_sha1_des_enc++;
  2988. else
  2989. pstat->aead_sha256_des_enc++;
  2990. return _qcrypto_queue_req(cp, ctx->pengine, &req->base);
  2991. }
  2992. static int _qcrypto_aead_decrypt_des_cbc(struct aead_request *req)
  2993. {
  2994. struct qcrypto_cipher_req_ctx *rctx;
  2995. struct qcrypto_cipher_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  2996. struct crypto_priv *cp = ctx->cp;
  2997. struct crypto_stat *pstat;
  2998. pstat = &_qcrypto_stat;
  2999. rctx = aead_request_ctx(req);
  3000. rctx->aead = 1;
  3001. rctx->alg = CIPHER_ALG_DES;
  3002. rctx->dir = QCE_DECRYPT;
  3003. rctx->mode = QCE_MODE_CBC;
  3004. rctx->iv = req->iv;
  3005. if (ctx->auth_alg == QCE_HASH_SHA1_HMAC)
  3006. pstat->aead_sha1_des_dec++;
  3007. else
  3008. pstat->aead_sha256_des_dec++;
  3009. return _qcrypto_queue_req(cp, ctx->pengine, &req->base);
  3010. }
  3011. static int _qcrypto_aead_encrypt_3des_cbc(struct aead_request *req)
  3012. {
  3013. struct qcrypto_cipher_req_ctx *rctx;
  3014. struct qcrypto_cipher_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  3015. struct crypto_priv *cp = ctx->cp;
  3016. struct crypto_stat *pstat;
  3017. pstat = &_qcrypto_stat;
  3018. rctx = aead_request_ctx(req);
  3019. rctx->aead = 1;
  3020. rctx->alg = CIPHER_ALG_3DES;
  3021. rctx->dir = QCE_ENCRYPT;
  3022. rctx->mode = QCE_MODE_CBC;
  3023. rctx->iv = req->iv;
  3024. if (ctx->auth_alg == QCE_HASH_SHA1_HMAC)
  3025. pstat->aead_sha1_3des_enc++;
  3026. else
  3027. pstat->aead_sha256_3des_enc++;
  3028. return _qcrypto_queue_req(cp, ctx->pengine, &req->base);
  3029. }
  3030. static int _qcrypto_aead_decrypt_3des_cbc(struct aead_request *req)
  3031. {
  3032. struct qcrypto_cipher_req_ctx *rctx;
  3033. struct qcrypto_cipher_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  3034. struct crypto_priv *cp = ctx->cp;
  3035. struct crypto_stat *pstat;
  3036. pstat = &_qcrypto_stat;
  3037. rctx = aead_request_ctx(req);
  3038. rctx->aead = 1;
  3039. rctx->alg = CIPHER_ALG_3DES;
  3040. rctx->dir = QCE_DECRYPT;
  3041. rctx->mode = QCE_MODE_CBC;
  3042. rctx->iv = req->iv;
  3043. if (ctx->auth_alg == QCE_HASH_SHA1_HMAC)
  3044. pstat->aead_sha1_3des_dec++;
  3045. else
  3046. pstat->aead_sha256_3des_dec++;
  3047. return _qcrypto_queue_req(cp, ctx->pengine, &req->base);
  3048. }
  3049. static int _sha_init(struct ahash_request *req)
  3050. {
  3051. struct qcrypto_sha_req_ctx *rctx = ahash_request_ctx(req);
  3052. rctx->first_blk = 1;
  3053. rctx->last_blk = 0;
  3054. rctx->byte_count[0] = 0;
  3055. rctx->byte_count[1] = 0;
  3056. rctx->byte_count[2] = 0;
  3057. rctx->byte_count[3] = 0;
  3058. rctx->trailing_buf_len = 0;
  3059. rctx->count = 0;
  3060. return 0;
  3061. }
  3062. static int _sha1_init(struct ahash_request *req)
  3063. {
  3064. struct qcrypto_sha_ctx *sha_ctx = crypto_tfm_ctx(req->base.tfm);
  3065. struct crypto_stat *pstat;
  3066. struct qcrypto_sha_req_ctx *rctx = ahash_request_ctx(req);
  3067. pstat = &_qcrypto_stat;
  3068. _sha_init(req);
  3069. sha_ctx->alg = QCE_HASH_SHA1;
  3070. memset(&rctx->trailing_buf[0], 0x00, SHA1_BLOCK_SIZE);
  3071. memcpy(&rctx->digest[0], &_std_init_vector_sha1_uint8[0],
  3072. SHA1_DIGEST_SIZE);
  3073. sha_ctx->diglen = SHA1_DIGEST_SIZE;
  3074. pstat->sha1_digest++;
  3075. return 0;
  3076. }
  3077. static int _sha256_init(struct ahash_request *req)
  3078. {
  3079. struct qcrypto_sha_ctx *sha_ctx = crypto_tfm_ctx(req->base.tfm);
  3080. struct crypto_stat *pstat;
  3081. struct qcrypto_sha_req_ctx *rctx = ahash_request_ctx(req);
  3082. pstat = &_qcrypto_stat;
  3083. _sha_init(req);
  3084. sha_ctx->alg = QCE_HASH_SHA256;
  3085. memset(&rctx->trailing_buf[0], 0x00, SHA256_BLOCK_SIZE);
  3086. memcpy(&rctx->digest[0], &_std_init_vector_sha256_uint8[0],
  3087. SHA256_DIGEST_SIZE);
  3088. sha_ctx->diglen = SHA256_DIGEST_SIZE;
  3089. pstat->sha256_digest++;
  3090. return 0;
  3091. }
  3092. static int _sha1_export(struct ahash_request *req, void *out)
  3093. {
  3094. struct qcrypto_sha_req_ctx *rctx = ahash_request_ctx(req);
  3095. struct sha1_state *out_ctx = (struct sha1_state *)out;
  3096. out_ctx->count = rctx->count;
  3097. _byte_stream_to_words(out_ctx->state, rctx->digest, SHA1_DIGEST_SIZE);
  3098. memcpy(out_ctx->buffer, rctx->trailing_buf, SHA1_BLOCK_SIZE);
  3099. return 0;
  3100. }
  3101. static int _sha1_hmac_export(struct ahash_request *req, void *out)
  3102. {
  3103. return _sha1_export(req, out);
  3104. }
  3105. /* crypto hw padding constant for hmac first operation */
  3106. #define HMAC_PADDING 64
  3107. static int __sha1_import_common(struct ahash_request *req, const void *in,
  3108. bool hmac)
  3109. {
  3110. struct qcrypto_sha_ctx *sha_ctx = crypto_tfm_ctx(req->base.tfm);
  3111. struct qcrypto_sha_req_ctx *rctx = ahash_request_ctx(req);
  3112. struct sha1_state *in_ctx = (struct sha1_state *)in;
  3113. u64 hw_count = in_ctx->count;
  3114. rctx->count = in_ctx->count;
  3115. memcpy(rctx->trailing_buf, in_ctx->buffer, SHA1_BLOCK_SIZE);
  3116. if (in_ctx->count <= SHA1_BLOCK_SIZE) {
  3117. rctx->first_blk = 1;
  3118. } else {
  3119. rctx->first_blk = 0;
  3120. /*
  3121. * For hmac, there is a hardware padding done
  3122. * when first is set. So the byte_count will be
  3123. * incremened by 64 after the operstion of first
  3124. */
  3125. if (hmac)
  3126. hw_count += HMAC_PADDING;
  3127. }
  3128. rctx->byte_count[0] = (uint32_t)(hw_count & 0xFFFFFFC0);
  3129. rctx->byte_count[1] = (uint32_t)(hw_count >> 32);
  3130. _words_to_byte_stream(in_ctx->state, rctx->digest, sha_ctx->diglen);
  3131. rctx->trailing_buf_len = (uint32_t)(in_ctx->count &
  3132. (SHA1_BLOCK_SIZE-1));
  3133. return 0;
  3134. }
  3135. static int _sha1_import(struct ahash_request *req, const void *in)
  3136. {
  3137. return __sha1_import_common(req, in, false);
  3138. }
  3139. static int _sha1_hmac_import(struct ahash_request *req, const void *in)
  3140. {
  3141. return __sha1_import_common(req, in, true);
  3142. }
  3143. static int _sha256_export(struct ahash_request *req, void *out)
  3144. {
  3145. struct qcrypto_sha_req_ctx *rctx = ahash_request_ctx(req);
  3146. struct sha256_state *out_ctx = (struct sha256_state *)out;
  3147. out_ctx->count = rctx->count;
  3148. _byte_stream_to_words(out_ctx->state, rctx->digest, SHA256_DIGEST_SIZE);
  3149. memcpy(out_ctx->buf, rctx->trailing_buf, SHA256_BLOCK_SIZE);
  3150. return 0;
  3151. }
  3152. static int _sha256_hmac_export(struct ahash_request *req, void *out)
  3153. {
  3154. return _sha256_export(req, out);
  3155. }
  3156. static int __sha256_import_common(struct ahash_request *req, const void *in,
  3157. bool hmac)
  3158. {
  3159. struct qcrypto_sha_ctx *sha_ctx = crypto_tfm_ctx(req->base.tfm);
  3160. struct qcrypto_sha_req_ctx *rctx = ahash_request_ctx(req);
  3161. struct sha256_state *in_ctx = (struct sha256_state *)in;
  3162. u64 hw_count = in_ctx->count;
  3163. rctx->count = in_ctx->count;
  3164. memcpy(rctx->trailing_buf, in_ctx->buf, SHA256_BLOCK_SIZE);
  3165. if (in_ctx->count <= SHA256_BLOCK_SIZE) {
  3166. rctx->first_blk = 1;
  3167. } else {
  3168. rctx->first_blk = 0;
  3169. /*
  3170. * for hmac, there is a hardware padding done
  3171. * when first is set. So the byte_count will be
  3172. * incremened by 64 after the operstion of first
  3173. */
  3174. if (hmac)
  3175. hw_count += HMAC_PADDING;
  3176. }
  3177. rctx->byte_count[0] = (uint32_t)(hw_count & 0xFFFFFFC0);
  3178. rctx->byte_count[1] = (uint32_t)(hw_count >> 32);
  3179. _words_to_byte_stream(in_ctx->state, rctx->digest, sha_ctx->diglen);
  3180. rctx->trailing_buf_len = (uint32_t)(in_ctx->count &
  3181. (SHA256_BLOCK_SIZE-1));
  3182. return 0;
  3183. }
  3184. static int _sha256_import(struct ahash_request *req, const void *in)
  3185. {
  3186. return __sha256_import_common(req, in, false);
  3187. }
  3188. static int _sha256_hmac_import(struct ahash_request *req, const void *in)
  3189. {
  3190. return __sha256_import_common(req, in, true);
  3191. }
  3192. static int _copy_source(struct ahash_request *req)
  3193. {
  3194. struct qcrypto_sha_req_ctx *srctx = NULL;
  3195. uint32_t bytes = 0;
  3196. uint32_t num_sg = 0;
  3197. srctx = ahash_request_ctx(req);
  3198. srctx->orig_src = req->src;
  3199. srctx->data = kzalloc((req->nbytes + 64), GFP_ATOMIC);
  3200. if (srctx->data == NULL) {
  3201. pr_err("Mem Alloc fail rctx->data, err %ld for 0x%x\n",
  3202. PTR_ERR(srctx->data), (req->nbytes + 64));
  3203. return -ENOMEM;
  3204. }
  3205. num_sg = qcrypto_count_sg(req->src, req->nbytes);
  3206. bytes = qcrypto_sg_copy_to_buffer(req->src, num_sg, srctx->data,
  3207. req->nbytes);
  3208. if (bytes != req->nbytes)
  3209. pr_warn("bytes copied=0x%x bytes to copy= 0x%x\n", bytes,
  3210. req->nbytes);
  3211. sg_set_buf(&srctx->dsg, srctx->data,
  3212. req->nbytes);
  3213. sg_mark_end(&srctx->dsg);
  3214. req->src = &srctx->dsg;
  3215. return 0;
  3216. }
  3217. static int _sha_update(struct ahash_request *req, uint32_t sha_block_size)
  3218. {
  3219. struct qcrypto_sha_ctx *sha_ctx = crypto_tfm_ctx(req->base.tfm);
  3220. struct crypto_priv *cp = sha_ctx->cp;
  3221. struct qcrypto_sha_req_ctx *rctx = ahash_request_ctx(req);
  3222. uint32_t total, len, num_sg;
  3223. struct scatterlist *sg_last;
  3224. uint8_t *k_src = NULL;
  3225. uint32_t sha_pad_len = 0;
  3226. uint32_t trailing_buf_len = 0;
  3227. uint32_t nbytes;
  3228. uint32_t offset = 0;
  3229. uint32_t bytes = 0;
  3230. uint8_t *staging;
  3231. int ret = 0;
  3232. /* check for trailing buffer from previous updates and append it */
  3233. total = req->nbytes + rctx->trailing_buf_len;
  3234. len = req->nbytes;
  3235. if (total <= sha_block_size) {
  3236. k_src = &rctx->trailing_buf[rctx->trailing_buf_len];
  3237. num_sg = qcrypto_count_sg(req->src, len);
  3238. bytes = qcrypto_sg_copy_to_buffer(req->src, num_sg, k_src, len);
  3239. rctx->trailing_buf_len = total;
  3240. return 0;
  3241. }
  3242. /* save the original req structure fields*/
  3243. rctx->src = req->src;
  3244. rctx->nbytes = req->nbytes;
  3245. staging = (uint8_t *)ALIGN(((uintptr_t)rctx->staging_dmabuf),
  3246. L1_CACHE_BYTES);
  3247. memcpy(staging, rctx->trailing_buf, rctx->trailing_buf_len);
  3248. k_src = &rctx->trailing_buf[0];
  3249. /* get new trailing buffer */
  3250. sha_pad_len = ALIGN(total, sha_block_size) - total;
  3251. trailing_buf_len = sha_block_size - sha_pad_len;
  3252. offset = req->nbytes - trailing_buf_len;
  3253. if (offset != req->nbytes)
  3254. scatterwalk_map_and_copy(k_src, req->src, offset,
  3255. trailing_buf_len, 0);
  3256. nbytes = total - trailing_buf_len;
  3257. num_sg = qcrypto_count_sg(req->src, req->nbytes);
  3258. len = rctx->trailing_buf_len;
  3259. sg_last = req->src;
  3260. while (len < nbytes) {
  3261. if ((len + sg_last->length) > nbytes)
  3262. break;
  3263. len += sg_last->length;
  3264. sg_last = sg_next(sg_last);
  3265. }
  3266. if (rctx->trailing_buf_len) {
  3267. if (cp->ce_support.aligned_only) {
  3268. rctx->data2 = kzalloc((req->nbytes + 64), GFP_ATOMIC);
  3269. if (rctx->data2 == NULL)
  3270. return -ENOMEM;
  3271. memcpy(rctx->data2, staging,
  3272. rctx->trailing_buf_len);
  3273. memcpy((rctx->data2 + rctx->trailing_buf_len),
  3274. rctx->data, req->src->length);
  3275. kfree_sensitive(rctx->data);
  3276. rctx->data = rctx->data2;
  3277. sg_set_buf(&rctx->sg[0], rctx->data,
  3278. (rctx->trailing_buf_len +
  3279. req->src->length));
  3280. req->src = rctx->sg;
  3281. sg_mark_end(&rctx->sg[0]);
  3282. } else {
  3283. sg_mark_end(sg_last);
  3284. memset(rctx->sg, 0, sizeof(rctx->sg));
  3285. sg_set_buf(&rctx->sg[0], staging,
  3286. rctx->trailing_buf_len);
  3287. sg_mark_end(&rctx->sg[1]);
  3288. sg_chain(rctx->sg, 2, req->src);
  3289. req->src = rctx->sg;
  3290. }
  3291. } else
  3292. sg_mark_end(sg_last);
  3293. req->nbytes = nbytes;
  3294. rctx->trailing_buf_len = trailing_buf_len;
  3295. ret = _qcrypto_queue_req(cp, sha_ctx->pengine, &req->base);
  3296. return ret;
  3297. }
  3298. static int _sha1_update(struct ahash_request *req)
  3299. {
  3300. struct qcrypto_sha_req_ctx *rctx = ahash_request_ctx(req);
  3301. struct qcrypto_sha_ctx *sha_ctx = crypto_tfm_ctx(req->base.tfm);
  3302. struct crypto_priv *cp = sha_ctx->cp;
  3303. if (cp->ce_support.aligned_only) {
  3304. if (_copy_source(req))
  3305. return -ENOMEM;
  3306. }
  3307. rctx->count += req->nbytes;
  3308. return _sha_update(req, SHA1_BLOCK_SIZE);
  3309. }
  3310. static int _sha256_update(struct ahash_request *req)
  3311. {
  3312. struct qcrypto_sha_req_ctx *rctx = ahash_request_ctx(req);
  3313. struct qcrypto_sha_ctx *sha_ctx = crypto_tfm_ctx(req->base.tfm);
  3314. struct crypto_priv *cp = sha_ctx->cp;
  3315. if (cp->ce_support.aligned_only) {
  3316. if (_copy_source(req))
  3317. return -ENOMEM;
  3318. }
  3319. rctx->count += req->nbytes;
  3320. return _sha_update(req, SHA256_BLOCK_SIZE);
  3321. }
  3322. static int _sha_final(struct ahash_request *req, uint32_t sha_block_size)
  3323. {
  3324. struct qcrypto_sha_ctx *sha_ctx = crypto_tfm_ctx(req->base.tfm);
  3325. struct crypto_priv *cp = sha_ctx->cp;
  3326. struct qcrypto_sha_req_ctx *rctx = ahash_request_ctx(req);
  3327. int ret = 0;
  3328. uint8_t *staging;
  3329. if (cp->ce_support.aligned_only) {
  3330. if (_copy_source(req))
  3331. return -ENOMEM;
  3332. }
  3333. rctx->last_blk = 1;
  3334. /* save the original req structure fields*/
  3335. rctx->src = req->src;
  3336. rctx->nbytes = req->nbytes;
  3337. staging = (uint8_t *)ALIGN(((uintptr_t)rctx->staging_dmabuf),
  3338. L1_CACHE_BYTES);
  3339. memcpy(staging, rctx->trailing_buf, rctx->trailing_buf_len);
  3340. sg_set_buf(&rctx->sg[0], staging, rctx->trailing_buf_len);
  3341. sg_mark_end(&rctx->sg[0]);
  3342. req->src = &rctx->sg[0];
  3343. req->nbytes = rctx->trailing_buf_len;
  3344. ret = _qcrypto_queue_req(cp, sha_ctx->pengine, &req->base);
  3345. return ret;
  3346. }
  3347. static int _sha1_final(struct ahash_request *req)
  3348. {
  3349. return _sha_final(req, SHA1_BLOCK_SIZE);
  3350. }
  3351. static int _sha256_final(struct ahash_request *req)
  3352. {
  3353. return _sha_final(req, SHA256_BLOCK_SIZE);
  3354. }
  3355. static int _sha_digest(struct ahash_request *req)
  3356. {
  3357. struct qcrypto_sha_ctx *sha_ctx = crypto_tfm_ctx(req->base.tfm);
  3358. struct qcrypto_sha_req_ctx *rctx = ahash_request_ctx(req);
  3359. struct crypto_priv *cp = sha_ctx->cp;
  3360. int ret = 0;
  3361. if (cp->ce_support.aligned_only) {
  3362. if (_copy_source(req))
  3363. return -ENOMEM;
  3364. }
  3365. /* save the original req structure fields*/
  3366. rctx->src = req->src;
  3367. rctx->nbytes = req->nbytes;
  3368. rctx->first_blk = 1;
  3369. rctx->last_blk = 1;
  3370. ret = _qcrypto_queue_req(cp, sha_ctx->pengine, &req->base);
  3371. return ret;
  3372. }
  3373. static int _sha1_digest(struct ahash_request *req)
  3374. {
  3375. _sha1_init(req);
  3376. return _sha_digest(req);
  3377. }
  3378. static int _sha256_digest(struct ahash_request *req)
  3379. {
  3380. _sha256_init(req);
  3381. return _sha_digest(req);
  3382. }
  3383. static void _crypto_sha_hmac_ahash_req_complete(
  3384. struct crypto_async_request *req, int err)
  3385. {
  3386. struct completion *ahash_req_complete = req->data;
  3387. if (err == -EINPROGRESS)
  3388. return;
  3389. complete(ahash_req_complete);
  3390. }
  3391. static int _sha_hmac_setkey(struct crypto_ahash *tfm, const u8 *key,
  3392. unsigned int len)
  3393. {
  3394. struct qcrypto_sha_ctx *sha_ctx = crypto_tfm_ctx(&tfm->base);
  3395. uint8_t *in_buf;
  3396. int ret = 0;
  3397. struct scatterlist sg = {0};
  3398. struct ahash_request *ahash_req;
  3399. struct completion ahash_req_complete;
  3400. ahash_req = ahash_request_alloc(tfm, GFP_KERNEL);
  3401. if (ahash_req == NULL)
  3402. return -ENOMEM;
  3403. init_completion(&ahash_req_complete);
  3404. ahash_request_set_callback(ahash_req,
  3405. CRYPTO_TFM_REQ_MAY_BACKLOG,
  3406. _crypto_sha_hmac_ahash_req_complete,
  3407. &ahash_req_complete);
  3408. crypto_ahash_clear_flags(tfm, ~0);
  3409. in_buf = kzalloc(len + 64, GFP_KERNEL);
  3410. if (in_buf == NULL) {
  3411. ahash_request_free(ahash_req);
  3412. return -ENOMEM;
  3413. }
  3414. memcpy(in_buf, key, len);
  3415. sg_set_buf(&sg, in_buf, len);
  3416. sg_mark_end(&sg);
  3417. ahash_request_set_crypt(ahash_req, &sg,
  3418. &sha_ctx->authkey[0], len);
  3419. if (sha_ctx->alg == QCE_HASH_SHA1)
  3420. ret = _sha1_digest(ahash_req);
  3421. else
  3422. ret = _sha256_digest(ahash_req);
  3423. if (ret == -EINPROGRESS || ret == -EBUSY) {
  3424. ret =
  3425. wait_for_completion_interruptible(
  3426. &ahash_req_complete);
  3427. reinit_completion(&sha_ctx->ahash_req_complete);
  3428. }
  3429. kfree_sensitive(in_buf);
  3430. ahash_request_free(ahash_req);
  3431. return ret;
  3432. }
  3433. static int _sha1_hmac_setkey(struct crypto_ahash *tfm, const u8 *key,
  3434. unsigned int len)
  3435. {
  3436. struct qcrypto_sha_ctx *sha_ctx = crypto_tfm_ctx(&tfm->base);
  3437. int ret = 0;
  3438. memset(&sha_ctx->authkey[0], 0, SHA1_BLOCK_SIZE);
  3439. if (len <= SHA1_BLOCK_SIZE) {
  3440. memcpy(&sha_ctx->authkey[0], key, len);
  3441. sha_ctx->authkey_in_len = len;
  3442. } else {
  3443. sha_ctx->alg = QCE_HASH_SHA1;
  3444. sha_ctx->diglen = SHA1_DIGEST_SIZE;
  3445. ret = _sha_hmac_setkey(tfm, key, len);
  3446. if (ret)
  3447. pr_err("SHA1 hmac setkey failed\n");
  3448. sha_ctx->authkey_in_len = SHA1_BLOCK_SIZE;
  3449. }
  3450. return ret;
  3451. }
  3452. static int _sha256_hmac_setkey(struct crypto_ahash *tfm, const u8 *key,
  3453. unsigned int len)
  3454. {
  3455. struct qcrypto_sha_ctx *sha_ctx = crypto_tfm_ctx(&tfm->base);
  3456. int ret = 0;
  3457. memset(&sha_ctx->authkey[0], 0, SHA256_BLOCK_SIZE);
  3458. if (len <= SHA256_BLOCK_SIZE) {
  3459. memcpy(&sha_ctx->authkey[0], key, len);
  3460. sha_ctx->authkey_in_len = len;
  3461. } else {
  3462. sha_ctx->alg = QCE_HASH_SHA256;
  3463. sha_ctx->diglen = SHA256_DIGEST_SIZE;
  3464. ret = _sha_hmac_setkey(tfm, key, len);
  3465. if (ret)
  3466. pr_err("SHA256 hmac setkey failed\n");
  3467. sha_ctx->authkey_in_len = SHA256_BLOCK_SIZE;
  3468. }
  3469. return ret;
  3470. }
  3471. static int _sha_hmac_init_ihash(struct ahash_request *req,
  3472. uint32_t sha_block_size)
  3473. {
  3474. struct qcrypto_sha_ctx *sha_ctx = crypto_tfm_ctx(req->base.tfm);
  3475. struct qcrypto_sha_req_ctx *rctx = ahash_request_ctx(req);
  3476. int i;
  3477. for (i = 0; i < sha_block_size; i++)
  3478. rctx->trailing_buf[i] = sha_ctx->authkey[i] ^ 0x36;
  3479. rctx->trailing_buf_len = sha_block_size;
  3480. return 0;
  3481. }
  3482. static int _sha1_hmac_init(struct ahash_request *req)
  3483. {
  3484. struct qcrypto_sha_ctx *sha_ctx = crypto_tfm_ctx(req->base.tfm);
  3485. struct crypto_priv *cp = sha_ctx->cp;
  3486. struct crypto_stat *pstat;
  3487. int ret = 0;
  3488. struct qcrypto_sha_req_ctx *rctx = ahash_request_ctx(req);
  3489. pstat = &_qcrypto_stat;
  3490. pstat->sha1_hmac_digest++;
  3491. _sha_init(req);
  3492. memset(&rctx->trailing_buf[0], 0x00, SHA1_BLOCK_SIZE);
  3493. memcpy(&rctx->digest[0], &_std_init_vector_sha1_uint8[0],
  3494. SHA1_DIGEST_SIZE);
  3495. sha_ctx->diglen = SHA1_DIGEST_SIZE;
  3496. if (cp->ce_support.sha_hmac)
  3497. sha_ctx->alg = QCE_HASH_SHA1_HMAC;
  3498. else {
  3499. sha_ctx->alg = QCE_HASH_SHA1;
  3500. ret = _sha_hmac_init_ihash(req, SHA1_BLOCK_SIZE);
  3501. }
  3502. return ret;
  3503. }
  3504. static int _sha256_hmac_init(struct ahash_request *req)
  3505. {
  3506. struct qcrypto_sha_ctx *sha_ctx = crypto_tfm_ctx(req->base.tfm);
  3507. struct crypto_priv *cp = sha_ctx->cp;
  3508. struct crypto_stat *pstat;
  3509. int ret = 0;
  3510. struct qcrypto_sha_req_ctx *rctx = ahash_request_ctx(req);
  3511. pstat = &_qcrypto_stat;
  3512. pstat->sha256_hmac_digest++;
  3513. _sha_init(req);
  3514. memset(&rctx->trailing_buf[0], 0x00, SHA256_BLOCK_SIZE);
  3515. memcpy(&rctx->digest[0], &_std_init_vector_sha256_uint8[0],
  3516. SHA256_DIGEST_SIZE);
  3517. sha_ctx->diglen = SHA256_DIGEST_SIZE;
  3518. if (cp->ce_support.sha_hmac)
  3519. sha_ctx->alg = QCE_HASH_SHA256_HMAC;
  3520. else {
  3521. sha_ctx->alg = QCE_HASH_SHA256;
  3522. ret = _sha_hmac_init_ihash(req, SHA256_BLOCK_SIZE);
  3523. }
  3524. return ret;
  3525. }
  3526. static int _sha1_hmac_update(struct ahash_request *req)
  3527. {
  3528. return _sha1_update(req);
  3529. }
  3530. static int _sha256_hmac_update(struct ahash_request *req)
  3531. {
  3532. return _sha256_update(req);
  3533. }
  3534. static int _sha_hmac_outer_hash(struct ahash_request *req,
  3535. uint32_t sha_digest_size, uint32_t sha_block_size)
  3536. {
  3537. struct qcrypto_sha_ctx *sha_ctx = crypto_tfm_ctx(req->base.tfm);
  3538. struct qcrypto_sha_req_ctx *rctx = ahash_request_ctx(req);
  3539. struct crypto_priv *cp = sha_ctx->cp;
  3540. int i;
  3541. uint8_t *staging;
  3542. uint8_t *p;
  3543. staging = (uint8_t *)ALIGN(((uintptr_t)rctx->staging_dmabuf),
  3544. L1_CACHE_BYTES);
  3545. p = staging;
  3546. for (i = 0; i < sha_block_size; i++)
  3547. *p++ = sha_ctx->authkey[i] ^ 0x5c;
  3548. memcpy(p, &rctx->digest[0], sha_digest_size);
  3549. sg_set_buf(&rctx->sg[0], staging, sha_block_size +
  3550. sha_digest_size);
  3551. sg_mark_end(&rctx->sg[0]);
  3552. /* save the original req structure fields*/
  3553. rctx->src = req->src;
  3554. rctx->nbytes = req->nbytes;
  3555. req->src = &rctx->sg[0];
  3556. req->nbytes = sha_block_size + sha_digest_size;
  3557. _sha_init(req);
  3558. if (sha_ctx->alg == QCE_HASH_SHA1) {
  3559. memcpy(&rctx->digest[0], &_std_init_vector_sha1_uint8[0],
  3560. SHA1_DIGEST_SIZE);
  3561. sha_ctx->diglen = SHA1_DIGEST_SIZE;
  3562. } else {
  3563. memcpy(&rctx->digest[0], &_std_init_vector_sha256_uint8[0],
  3564. SHA256_DIGEST_SIZE);
  3565. sha_ctx->diglen = SHA256_DIGEST_SIZE;
  3566. }
  3567. rctx->last_blk = 1;
  3568. return _qcrypto_queue_req(cp, sha_ctx->pengine, &req->base);
  3569. }
  3570. static int _sha_hmac_inner_hash(struct ahash_request *req,
  3571. uint32_t sha_digest_size, uint32_t sha_block_size)
  3572. {
  3573. struct qcrypto_sha_ctx *sha_ctx = crypto_tfm_ctx(req->base.tfm);
  3574. struct ahash_request *areq = sha_ctx->ahash_req;
  3575. struct crypto_priv *cp = sha_ctx->cp;
  3576. int ret = 0;
  3577. struct qcrypto_sha_req_ctx *rctx = ahash_request_ctx(req);
  3578. uint8_t *staging;
  3579. staging = (uint8_t *)ALIGN(((uintptr_t)rctx->staging_dmabuf),
  3580. L1_CACHE_BYTES);
  3581. memcpy(staging, rctx->trailing_buf, rctx->trailing_buf_len);
  3582. sg_set_buf(&rctx->sg[0], staging, rctx->trailing_buf_len);
  3583. sg_mark_end(&rctx->sg[0]);
  3584. ahash_request_set_crypt(areq, &rctx->sg[0], &rctx->digest[0],
  3585. rctx->trailing_buf_len);
  3586. rctx->last_blk = 1;
  3587. ret = _qcrypto_queue_req(cp, sha_ctx->pengine, &areq->base);
  3588. if (ret == -EINPROGRESS || ret == -EBUSY) {
  3589. ret =
  3590. wait_for_completion_interruptible(&sha_ctx->ahash_req_complete);
  3591. reinit_completion(&sha_ctx->ahash_req_complete);
  3592. }
  3593. return ret;
  3594. }
  3595. static int _sha1_hmac_final(struct ahash_request *req)
  3596. {
  3597. struct qcrypto_sha_ctx *sha_ctx = crypto_tfm_ctx(req->base.tfm);
  3598. struct crypto_priv *cp = sha_ctx->cp;
  3599. int ret = 0;
  3600. if (cp->ce_support.sha_hmac)
  3601. return _sha_final(req, SHA1_BLOCK_SIZE);
  3602. ret = _sha_hmac_inner_hash(req, SHA1_DIGEST_SIZE, SHA1_BLOCK_SIZE);
  3603. if (ret)
  3604. return ret;
  3605. return _sha_hmac_outer_hash(req, SHA1_DIGEST_SIZE, SHA1_BLOCK_SIZE);
  3606. }
  3607. static int _sha256_hmac_final(struct ahash_request *req)
  3608. {
  3609. struct qcrypto_sha_ctx *sha_ctx = crypto_tfm_ctx(req->base.tfm);
  3610. struct crypto_priv *cp = sha_ctx->cp;
  3611. int ret = 0;
  3612. if (cp->ce_support.sha_hmac)
  3613. return _sha_final(req, SHA256_BLOCK_SIZE);
  3614. ret = _sha_hmac_inner_hash(req, SHA256_DIGEST_SIZE, SHA256_BLOCK_SIZE);
  3615. if (ret)
  3616. return ret;
  3617. return _sha_hmac_outer_hash(req, SHA256_DIGEST_SIZE, SHA256_BLOCK_SIZE);
  3618. }
  3619. static int _sha1_hmac_digest(struct ahash_request *req)
  3620. {
  3621. struct qcrypto_sha_ctx *sha_ctx = crypto_tfm_ctx(req->base.tfm);
  3622. struct crypto_stat *pstat;
  3623. struct qcrypto_sha_req_ctx *rctx = ahash_request_ctx(req);
  3624. pstat = &_qcrypto_stat;
  3625. pstat->sha1_hmac_digest++;
  3626. _sha_init(req);
  3627. memcpy(&rctx->digest[0], &_std_init_vector_sha1_uint8[0],
  3628. SHA1_DIGEST_SIZE);
  3629. sha_ctx->diglen = SHA1_DIGEST_SIZE;
  3630. sha_ctx->alg = QCE_HASH_SHA1_HMAC;
  3631. return _sha_digest(req);
  3632. }
  3633. static int _sha256_hmac_digest(struct ahash_request *req)
  3634. {
  3635. struct qcrypto_sha_ctx *sha_ctx = crypto_tfm_ctx(req->base.tfm);
  3636. struct crypto_stat *pstat;
  3637. struct qcrypto_sha_req_ctx *rctx = ahash_request_ctx(req);
  3638. pstat = &_qcrypto_stat;
  3639. pstat->sha256_hmac_digest++;
  3640. _sha_init(req);
  3641. memcpy(&rctx->digest[0], &_std_init_vector_sha256_uint8[0],
  3642. SHA256_DIGEST_SIZE);
  3643. sha_ctx->diglen = SHA256_DIGEST_SIZE;
  3644. sha_ctx->alg = QCE_HASH_SHA256_HMAC;
  3645. return _sha_digest(req);
  3646. }
  3647. static int _qcrypto_prefix_alg_cra_name(char cra_name[], unsigned int size)
  3648. {
  3649. char new_cra_name[CRYPTO_MAX_ALG_NAME] = "qcom-";
  3650. if (size >= CRYPTO_MAX_ALG_NAME - strlen("qcom-"))
  3651. return -EINVAL;
  3652. strlcat(new_cra_name, cra_name, CRYPTO_MAX_ALG_NAME);
  3653. strlcpy(cra_name, new_cra_name, CRYPTO_MAX_ALG_NAME);
  3654. return 0;
  3655. }
  3656. int qcrypto_cipher_set_device(struct skcipher_request *req, unsigned int dev)
  3657. {
  3658. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  3659. struct qcrypto_cipher_ctx *ctx = crypto_skcipher_ctx(tfm);
  3660. struct crypto_priv *cp = ctx->cp;
  3661. struct crypto_engine *pengine = NULL;
  3662. pengine = _qrypto_find_pengine_device(cp, dev);
  3663. if (pengine == NULL)
  3664. return -ENODEV;
  3665. ctx->pengine = pengine;
  3666. return 0;
  3667. }
  3668. EXPORT_SYMBOL(qcrypto_cipher_set_device);
  3669. int qcrypto_cipher_set_device_hw(struct skcipher_request *req, u32 dev,
  3670. u32 hw_inst)
  3671. {
  3672. struct qcrypto_cipher_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  3673. struct crypto_priv *cp = ctx->cp;
  3674. struct crypto_engine *pengine = NULL;
  3675. pengine = _qrypto_find_pengine_device_hw(cp, dev, hw_inst);
  3676. if (pengine == NULL)
  3677. return -ENODEV;
  3678. ctx->pengine = pengine;
  3679. return 0;
  3680. }
  3681. EXPORT_SYMBOL(qcrypto_cipher_set_device_hw);
  3682. int qcrypto_aead_set_device(struct aead_request *req, unsigned int dev)
  3683. {
  3684. struct qcrypto_cipher_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  3685. struct crypto_priv *cp = ctx->cp;
  3686. struct crypto_engine *pengine = NULL;
  3687. pengine = _qrypto_find_pengine_device(cp, dev);
  3688. if (pengine == NULL)
  3689. return -ENODEV;
  3690. ctx->pengine = pengine;
  3691. return 0;
  3692. }
  3693. EXPORT_SYMBOL(qcrypto_aead_set_device);
  3694. int qcrypto_ahash_set_device(struct ahash_request *req, unsigned int dev)
  3695. {
  3696. struct qcrypto_sha_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  3697. struct crypto_priv *cp = ctx->cp;
  3698. struct crypto_engine *pengine = NULL;
  3699. pengine = _qrypto_find_pengine_device(cp, dev);
  3700. if (pengine == NULL)
  3701. return -ENODEV;
  3702. ctx->pengine = pengine;
  3703. return 0;
  3704. }
  3705. EXPORT_SYMBOL(qcrypto_ahash_set_device);
  3706. int qcrypto_cipher_set_flag(struct skcipher_request *req, unsigned int flags)
  3707. {
  3708. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  3709. struct qcrypto_cipher_ctx *ctx = crypto_skcipher_ctx(tfm);
  3710. struct crypto_priv *cp = ctx->cp;
  3711. if ((flags & QCRYPTO_CTX_USE_HW_KEY) &&
  3712. (!cp->platform_support.hw_key_support)) {
  3713. pr_err("%s HW key usage not supported\n", __func__);
  3714. return -EINVAL;
  3715. }
  3716. if (((flags | ctx->flags) & QCRYPTO_CTX_KEY_MASK) ==
  3717. QCRYPTO_CTX_KEY_MASK) {
  3718. pr_err("%s Cannot set all key flags\n", __func__);
  3719. return -EINVAL;
  3720. }
  3721. ctx->flags |= flags;
  3722. return 0;
  3723. }
  3724. EXPORT_SYMBOL(qcrypto_cipher_set_flag);
  3725. int qcrypto_aead_set_flag(struct aead_request *req, unsigned int flags)
  3726. {
  3727. struct qcrypto_cipher_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  3728. struct crypto_priv *cp = ctx->cp;
  3729. if ((flags & QCRYPTO_CTX_USE_HW_KEY) &&
  3730. (!cp->platform_support.hw_key_support)) {
  3731. pr_err("%s HW key usage not supported\n", __func__);
  3732. return -EINVAL;
  3733. }
  3734. if (((flags | ctx->flags) & QCRYPTO_CTX_KEY_MASK) ==
  3735. QCRYPTO_CTX_KEY_MASK) {
  3736. pr_err("%s Cannot set all key flags\n", __func__);
  3737. return -EINVAL;
  3738. }
  3739. ctx->flags |= flags;
  3740. return 0;
  3741. }
  3742. EXPORT_SYMBOL(qcrypto_aead_set_flag);
  3743. int qcrypto_ahash_set_flag(struct ahash_request *req, unsigned int flags)
  3744. {
  3745. struct qcrypto_sha_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  3746. struct crypto_priv *cp = ctx->cp;
  3747. if ((flags & QCRYPTO_CTX_USE_HW_KEY) &&
  3748. (!cp->platform_support.hw_key_support)) {
  3749. pr_err("%s HW key usage not supported\n", __func__);
  3750. return -EINVAL;
  3751. }
  3752. if (((flags | ctx->flags) & QCRYPTO_CTX_KEY_MASK) ==
  3753. QCRYPTO_CTX_KEY_MASK) {
  3754. pr_err("%s Cannot set all key flags\n", __func__);
  3755. return -EINVAL;
  3756. }
  3757. ctx->flags |= flags;
  3758. return 0;
  3759. }
  3760. EXPORT_SYMBOL(qcrypto_ahash_set_flag);
  3761. int qcrypto_cipher_clear_flag(struct skcipher_request *req,
  3762. unsigned int flags)
  3763. {
  3764. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  3765. struct qcrypto_cipher_ctx *ctx = crypto_skcipher_ctx(tfm);
  3766. ctx->flags &= ~flags;
  3767. return 0;
  3768. }
  3769. EXPORT_SYMBOL(qcrypto_cipher_clear_flag);
  3770. int qcrypto_aead_clear_flag(struct aead_request *req, unsigned int flags)
  3771. {
  3772. struct qcrypto_cipher_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  3773. ctx->flags &= ~flags;
  3774. return 0;
  3775. }
  3776. EXPORT_SYMBOL(qcrypto_aead_clear_flag);
  3777. int qcrypto_ahash_clear_flag(struct ahash_request *req, unsigned int flags)
  3778. {
  3779. struct qcrypto_sha_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  3780. ctx->flags &= ~flags;
  3781. return 0;
  3782. }
  3783. EXPORT_SYMBOL(qcrypto_ahash_clear_flag);
  3784. static struct ahash_alg _qcrypto_ahash_algos[] = {
  3785. {
  3786. .init = _sha1_init,
  3787. .update = _sha1_update,
  3788. .final = _sha1_final,
  3789. .digest = _sha1_digest,
  3790. .export = _sha1_export,
  3791. .import = _sha1_import,
  3792. .halg = {
  3793. .digestsize = SHA1_DIGEST_SIZE,
  3794. .statesize = sizeof(struct sha1_state),
  3795. .base = {
  3796. .cra_name = "sha1",
  3797. .cra_driver_name = "qcrypto-sha1",
  3798. .cra_priority = 300,
  3799. .cra_flags = CRYPTO_ALG_ASYNC,
  3800. .cra_blocksize = SHA1_BLOCK_SIZE,
  3801. .cra_ctxsize = sizeof(struct qcrypto_sha_ctx),
  3802. .cra_alignmask = 0,
  3803. .cra_module = THIS_MODULE,
  3804. .cra_init = _qcrypto_ahash_cra_init,
  3805. .cra_exit = _qcrypto_ahash_cra_exit,
  3806. },
  3807. },
  3808. },
  3809. {
  3810. .init = _sha256_init,
  3811. .update = _sha256_update,
  3812. .final = _sha256_final,
  3813. .digest = _sha256_digest,
  3814. .export = _sha256_export,
  3815. .import = _sha256_import,
  3816. .halg = {
  3817. .digestsize = SHA256_DIGEST_SIZE,
  3818. .statesize = sizeof(struct sha256_state),
  3819. .base = {
  3820. .cra_name = "sha256",
  3821. .cra_driver_name = "qcrypto-sha256",
  3822. .cra_priority = 300,
  3823. .cra_flags = CRYPTO_ALG_ASYNC,
  3824. .cra_blocksize = SHA256_BLOCK_SIZE,
  3825. .cra_ctxsize = sizeof(struct qcrypto_sha_ctx),
  3826. .cra_alignmask = 0,
  3827. .cra_module = THIS_MODULE,
  3828. .cra_init = _qcrypto_ahash_cra_init,
  3829. .cra_exit = _qcrypto_ahash_cra_exit,
  3830. },
  3831. },
  3832. },
  3833. };
  3834. static struct ahash_alg _qcrypto_sha_hmac_algos[] = {
  3835. {
  3836. .init = _sha1_hmac_init,
  3837. .update = _sha1_hmac_update,
  3838. .final = _sha1_hmac_final,
  3839. .export = _sha1_hmac_export,
  3840. .import = _sha1_hmac_import,
  3841. .digest = _sha1_hmac_digest,
  3842. .setkey = _sha1_hmac_setkey,
  3843. .halg = {
  3844. .digestsize = SHA1_DIGEST_SIZE,
  3845. .statesize = sizeof(struct sha1_state),
  3846. .base = {
  3847. .cra_name = "hmac(sha1)",
  3848. .cra_driver_name = "qcrypto-hmac-sha1",
  3849. .cra_priority = 300,
  3850. .cra_flags = CRYPTO_ALG_ASYNC,
  3851. .cra_blocksize = SHA1_BLOCK_SIZE,
  3852. .cra_ctxsize = sizeof(struct qcrypto_sha_ctx),
  3853. .cra_alignmask = 0,
  3854. .cra_module = THIS_MODULE,
  3855. .cra_init = _qcrypto_ahash_hmac_cra_init,
  3856. .cra_exit = _qcrypto_ahash_cra_exit,
  3857. },
  3858. },
  3859. },
  3860. {
  3861. .init = _sha256_hmac_init,
  3862. .update = _sha256_hmac_update,
  3863. .final = _sha256_hmac_final,
  3864. .export = _sha256_hmac_export,
  3865. .import = _sha256_hmac_import,
  3866. .digest = _sha256_hmac_digest,
  3867. .setkey = _sha256_hmac_setkey,
  3868. .halg = {
  3869. .digestsize = SHA256_DIGEST_SIZE,
  3870. .statesize = sizeof(struct sha256_state),
  3871. .base = {
  3872. .cra_name = "hmac(sha256)",
  3873. .cra_driver_name = "qcrypto-hmac-sha256",
  3874. .cra_priority = 300,
  3875. .cra_flags = CRYPTO_ALG_ASYNC,
  3876. .cra_blocksize = SHA256_BLOCK_SIZE,
  3877. .cra_ctxsize = sizeof(struct qcrypto_sha_ctx),
  3878. .cra_alignmask = 0,
  3879. .cra_module = THIS_MODULE,
  3880. .cra_init = _qcrypto_ahash_hmac_cra_init,
  3881. .cra_exit = _qcrypto_ahash_cra_exit,
  3882. },
  3883. },
  3884. },
  3885. };
  3886. static struct skcipher_alg _qcrypto_sk_cipher_algos[] = {
  3887. {
  3888. .setkey = _qcrypto_setkey_aes,
  3889. .encrypt = _qcrypto_enc_aes_ecb,
  3890. .decrypt = _qcrypto_dec_aes_ecb,
  3891. .init = _qcrypto_aes_skcipher_init,
  3892. .exit = _qcrypto_aes_skcipher_exit,
  3893. .min_keysize = AES_MIN_KEY_SIZE,
  3894. .max_keysize = AES_MAX_KEY_SIZE,
  3895. .base = {
  3896. .cra_name = "ecb(aes)",
  3897. .cra_driver_name = "qcrypto-ecb-aes",
  3898. .cra_priority = 300,
  3899. .cra_flags = CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC,
  3900. .cra_blocksize = AES_BLOCK_SIZE,
  3901. .cra_ctxsize = sizeof(struct qcrypto_cipher_ctx),
  3902. .cra_alignmask = 0,
  3903. .cra_module = THIS_MODULE,
  3904. },
  3905. },
  3906. {
  3907. .setkey = _qcrypto_setkey_aes,
  3908. .encrypt = _qcrypto_enc_aes_cbc,
  3909. .decrypt = _qcrypto_dec_aes_cbc,
  3910. .init = _qcrypto_aes_skcipher_init,
  3911. .exit = _qcrypto_aes_skcipher_exit,
  3912. .min_keysize = AES_MIN_KEY_SIZE,
  3913. .max_keysize = AES_MAX_KEY_SIZE,
  3914. .ivsize = AES_BLOCK_SIZE,
  3915. .base = {
  3916. .cra_name = "cbc(aes)",
  3917. .cra_driver_name = "qcrypto-cbc-aes",
  3918. .cra_priority = 300,
  3919. .cra_flags = CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC,
  3920. .cra_blocksize = AES_BLOCK_SIZE,
  3921. .cra_ctxsize = sizeof(struct qcrypto_cipher_ctx),
  3922. .cra_alignmask = 0,
  3923. .cra_module = THIS_MODULE,
  3924. },
  3925. },
  3926. {
  3927. .setkey = _qcrypto_setkey_aes,
  3928. .encrypt = _qcrypto_enc_aes_ctr,
  3929. .decrypt = _qcrypto_dec_aes_ctr,
  3930. .init = _qcrypto_aes_skcipher_init,
  3931. .exit = _qcrypto_aes_skcipher_exit,
  3932. .min_keysize = AES_MIN_KEY_SIZE,
  3933. .max_keysize = AES_MAX_KEY_SIZE,
  3934. .ivsize = AES_BLOCK_SIZE,
  3935. .base = {
  3936. .cra_name = "ctr(aes)",
  3937. .cra_driver_name = "qcrypto-ctr-aes",
  3938. .cra_priority = 300,
  3939. .cra_flags = CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC,
  3940. .cra_blocksize = AES_BLOCK_SIZE,
  3941. .cra_ctxsize = sizeof(struct qcrypto_cipher_ctx),
  3942. .cra_alignmask = 0,
  3943. .cra_module = THIS_MODULE,
  3944. },
  3945. },
  3946. {
  3947. .setkey = _qcrypto_setkey_des,
  3948. .encrypt = _qcrypto_enc_des_ecb,
  3949. .decrypt = _qcrypto_dec_des_ecb,
  3950. .init = _qcrypto_skcipher_init,
  3951. .exit = _qcrypto_skcipher_exit,
  3952. .min_keysize = DES_KEY_SIZE,
  3953. .max_keysize = DES_KEY_SIZE,
  3954. .base = {
  3955. .cra_name = "ecb(des)",
  3956. .cra_driver_name = "qcrypto-ecb-des",
  3957. .cra_priority = 300,
  3958. .cra_flags = CRYPTO_ALG_ASYNC,
  3959. .cra_blocksize = DES_BLOCK_SIZE,
  3960. .cra_ctxsize = sizeof(struct qcrypto_cipher_ctx),
  3961. .cra_alignmask = 0,
  3962. .cra_module = THIS_MODULE,
  3963. },
  3964. },
  3965. {
  3966. .setkey = _qcrypto_setkey_des,
  3967. .encrypt = _qcrypto_enc_des_cbc,
  3968. .decrypt = _qcrypto_dec_des_cbc,
  3969. .init = _qcrypto_skcipher_init,
  3970. .exit = _qcrypto_skcipher_exit,
  3971. .min_keysize = DES_KEY_SIZE,
  3972. .max_keysize = DES_KEY_SIZE,
  3973. .ivsize = DES_BLOCK_SIZE,
  3974. .base = {
  3975. .cra_name = "cbc(des)",
  3976. .cra_driver_name = "qcrypto-cbc-des",
  3977. .cra_priority = 300,
  3978. .cra_flags = CRYPTO_ALG_ASYNC,
  3979. .cra_blocksize = DES_BLOCK_SIZE,
  3980. .cra_ctxsize = sizeof(struct qcrypto_cipher_ctx),
  3981. .cra_alignmask = 0,
  3982. .cra_module = THIS_MODULE,
  3983. },
  3984. },
  3985. {
  3986. .setkey = _qcrypto_setkey_3des,
  3987. .encrypt = _qcrypto_enc_3des_ecb,
  3988. .decrypt = _qcrypto_dec_3des_ecb,
  3989. .init = _qcrypto_skcipher_init,
  3990. .exit = _qcrypto_skcipher_exit,
  3991. .min_keysize = DES3_EDE_KEY_SIZE,
  3992. .max_keysize = DES3_EDE_KEY_SIZE,
  3993. .base = {
  3994. .cra_name = "ecb(des3_ede)",
  3995. .cra_driver_name = "qcrypto-ecb-3des",
  3996. .cra_priority = 300,
  3997. .cra_flags = CRYPTO_ALG_ASYNC,
  3998. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  3999. .cra_ctxsize = sizeof(struct qcrypto_cipher_ctx),
  4000. .cra_alignmask = 0,
  4001. .cra_module = THIS_MODULE,
  4002. },
  4003. },
  4004. {
  4005. .setkey = _qcrypto_setkey_3des,
  4006. .encrypt = _qcrypto_enc_3des_cbc,
  4007. .decrypt = _qcrypto_dec_3des_cbc,
  4008. .init = _qcrypto_skcipher_init,
  4009. .exit = _qcrypto_skcipher_exit,
  4010. .min_keysize = DES3_EDE_KEY_SIZE,
  4011. .max_keysize = DES3_EDE_KEY_SIZE,
  4012. .ivsize = DES3_EDE_BLOCK_SIZE,
  4013. .base = {
  4014. .cra_name = "cbc(des3_ede)",
  4015. .cra_driver_name = "qcrypto-cbc-3des",
  4016. .cra_priority = 300,
  4017. .cra_flags = CRYPTO_ALG_ASYNC,
  4018. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  4019. .cra_ctxsize = sizeof(struct qcrypto_cipher_ctx),
  4020. .cra_alignmask = 0,
  4021. .cra_module = THIS_MODULE,
  4022. },
  4023. },
  4024. };
  4025. static struct skcipher_alg _qcrypto_sk_cipher_xts_algo = {
  4026. .setkey = _qcrypto_setkey_aes_xts,
  4027. .encrypt = _qcrypto_enc_aes_xts,
  4028. .decrypt = _qcrypto_dec_aes_xts,
  4029. .init = _qcrypto_skcipher_init,
  4030. .exit = _qcrypto_skcipher_exit,
  4031. .min_keysize = AES_MIN_KEY_SIZE,
  4032. .max_keysize = AES_MAX_KEY_SIZE,
  4033. .ivsize = AES_BLOCK_SIZE,
  4034. .base = {
  4035. .cra_name = "xts(aes)",
  4036. .cra_driver_name = "qcrypto-xts-aes",
  4037. .cra_priority = 300,
  4038. .cra_flags = CRYPTO_ALG_ASYNC,
  4039. .cra_blocksize = AES_BLOCK_SIZE,
  4040. .cra_ctxsize = sizeof(struct qcrypto_cipher_ctx),
  4041. .cra_alignmask = 0,
  4042. .cra_module = THIS_MODULE,
  4043. },
  4044. };
  4045. static struct aead_alg _qcrypto_aead_sha1_hmac_algos[] = {
  4046. {
  4047. .setkey = _qcrypto_aead_setkey,
  4048. .setauthsize = _qcrypto_aead_setauthsize,
  4049. .encrypt = _qcrypto_aead_encrypt_aes_cbc,
  4050. .decrypt = _qcrypto_aead_decrypt_aes_cbc,
  4051. .init = _qcrypto_cra_aead_aes_sha1_init,
  4052. .exit = _qcrypto_cra_aead_aes_exit,
  4053. .ivsize = AES_BLOCK_SIZE,
  4054. .maxauthsize = SHA1_DIGEST_SIZE,
  4055. .base = {
  4056. .cra_name = "authenc(hmac(sha1),cbc(aes))",
  4057. .cra_driver_name = "qcrypto-aead-hmac-sha1-cbc-aes",
  4058. .cra_priority = 300,
  4059. .cra_flags = CRYPTO_ALG_ASYNC,
  4060. .cra_blocksize = AES_BLOCK_SIZE,
  4061. .cra_ctxsize = sizeof(struct qcrypto_cipher_ctx),
  4062. .cra_alignmask = 0,
  4063. .cra_module = THIS_MODULE,
  4064. },
  4065. },
  4066. {
  4067. .setkey = _qcrypto_aead_setkey,
  4068. .setauthsize = _qcrypto_aead_setauthsize,
  4069. .encrypt = _qcrypto_aead_encrypt_des_cbc,
  4070. .decrypt = _qcrypto_aead_decrypt_des_cbc,
  4071. .init = _qcrypto_cra_aead_sha1_init,
  4072. .exit = _qcrypto_cra_aead_exit,
  4073. .ivsize = DES_BLOCK_SIZE,
  4074. .maxauthsize = SHA1_DIGEST_SIZE,
  4075. .base = {
  4076. .cra_name = "authenc(hmac(sha1),cbc(des))",
  4077. .cra_driver_name = "qcrypto-aead-hmac-sha1-cbc-des",
  4078. .cra_priority = 300,
  4079. .cra_flags = CRYPTO_ALG_ASYNC,
  4080. .cra_blocksize = DES_BLOCK_SIZE,
  4081. .cra_ctxsize = sizeof(struct qcrypto_cipher_ctx),
  4082. .cra_alignmask = 0,
  4083. .cra_module = THIS_MODULE,
  4084. },
  4085. },
  4086. {
  4087. .setkey = _qcrypto_aead_setkey,
  4088. .setauthsize = _qcrypto_aead_setauthsize,
  4089. .encrypt = _qcrypto_aead_encrypt_3des_cbc,
  4090. .decrypt = _qcrypto_aead_decrypt_3des_cbc,
  4091. .init = _qcrypto_cra_aead_sha1_init,
  4092. .exit = _qcrypto_cra_aead_exit,
  4093. .ivsize = DES3_EDE_BLOCK_SIZE,
  4094. .maxauthsize = SHA1_DIGEST_SIZE,
  4095. .base = {
  4096. .cra_name = "authenc(hmac(sha1),cbc(des3_ede))",
  4097. .cra_driver_name = "qcrypto-aead-hmac-sha1-cbc-3des",
  4098. .cra_priority = 300,
  4099. .cra_flags = CRYPTO_ALG_ASYNC,
  4100. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  4101. .cra_ctxsize = sizeof(struct qcrypto_cipher_ctx),
  4102. .cra_alignmask = 0,
  4103. .cra_module = THIS_MODULE,
  4104. },
  4105. },
  4106. };
  4107. static struct aead_alg _qcrypto_aead_sha256_hmac_algos[] = {
  4108. {
  4109. .setkey = _qcrypto_aead_setkey,
  4110. .setauthsize = _qcrypto_aead_setauthsize,
  4111. .encrypt = _qcrypto_aead_encrypt_aes_cbc,
  4112. .decrypt = _qcrypto_aead_decrypt_aes_cbc,
  4113. .init = _qcrypto_cra_aead_aes_sha256_init,
  4114. .exit = _qcrypto_cra_aead_aes_exit,
  4115. .ivsize = AES_BLOCK_SIZE,
  4116. .maxauthsize = SHA256_DIGEST_SIZE,
  4117. .base = {
  4118. .cra_name = "authenc(hmac(sha256),cbc(aes))",
  4119. .cra_driver_name = "qcrypto-aead-hmac-sha256-cbc-aes",
  4120. .cra_priority = 300,
  4121. .cra_flags = CRYPTO_ALG_ASYNC,
  4122. .cra_blocksize = AES_BLOCK_SIZE,
  4123. .cra_ctxsize = sizeof(struct qcrypto_cipher_ctx),
  4124. .cra_alignmask = 0,
  4125. .cra_module = THIS_MODULE,
  4126. },
  4127. },
  4128. {
  4129. .setkey = _qcrypto_aead_setkey,
  4130. .setauthsize = _qcrypto_aead_setauthsize,
  4131. .encrypt = _qcrypto_aead_encrypt_des_cbc,
  4132. .decrypt = _qcrypto_aead_decrypt_des_cbc,
  4133. .init = _qcrypto_cra_aead_sha256_init,
  4134. .exit = _qcrypto_cra_aead_exit,
  4135. .ivsize = DES_BLOCK_SIZE,
  4136. .maxauthsize = SHA256_DIGEST_SIZE,
  4137. .base = {
  4138. .cra_name = "authenc(hmac(sha256),cbc(des))",
  4139. .cra_driver_name = "qcrypto-aead-hmac-sha256-cbc-des",
  4140. .cra_priority = 300,
  4141. .cra_flags = CRYPTO_ALG_ASYNC,
  4142. .cra_blocksize = DES_BLOCK_SIZE,
  4143. .cra_ctxsize = sizeof(struct qcrypto_cipher_ctx),
  4144. .cra_alignmask = 0,
  4145. .cra_module = THIS_MODULE,
  4146. },
  4147. },
  4148. {
  4149. .setkey = _qcrypto_aead_setkey,
  4150. .setauthsize = _qcrypto_aead_setauthsize,
  4151. .encrypt = _qcrypto_aead_encrypt_3des_cbc,
  4152. .decrypt = _qcrypto_aead_decrypt_3des_cbc,
  4153. .init = _qcrypto_cra_aead_sha256_init,
  4154. .exit = _qcrypto_cra_aead_exit,
  4155. .ivsize = DES3_EDE_BLOCK_SIZE,
  4156. .maxauthsize = SHA256_DIGEST_SIZE,
  4157. .base = {
  4158. .cra_name = "authenc(hmac(sha256),cbc(des3_ede))",
  4159. .cra_driver_name = "qcrypto-aead-hmac-sha256-cbc-3des",
  4160. .cra_priority = 300,
  4161. .cra_flags = CRYPTO_ALG_ASYNC,
  4162. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  4163. .cra_ctxsize = sizeof(struct qcrypto_cipher_ctx),
  4164. .cra_alignmask = 0,
  4165. .cra_module = THIS_MODULE,
  4166. },
  4167. },
  4168. };
  4169. static struct aead_alg _qcrypto_aead_ccm_algo = {
  4170. .setkey = _qcrypto_aead_ccm_setkey,
  4171. .setauthsize = _qcrypto_aead_ccm_setauthsize,
  4172. .encrypt = _qcrypto_aead_encrypt_aes_ccm,
  4173. .decrypt = _qcrypto_aead_decrypt_aes_ccm,
  4174. .init = _qcrypto_cra_aead_ccm_init,
  4175. .exit = _qcrypto_cra_aead_exit,
  4176. .ivsize = AES_BLOCK_SIZE,
  4177. .maxauthsize = AES_BLOCK_SIZE,
  4178. .base = {
  4179. .cra_name = "ccm(aes)",
  4180. .cra_driver_name = "qcrypto-aes-ccm",
  4181. .cra_priority = 300,
  4182. .cra_flags = CRYPTO_ALG_ASYNC,
  4183. .cra_blocksize = AES_BLOCK_SIZE,
  4184. .cra_ctxsize = sizeof(struct qcrypto_cipher_ctx),
  4185. .cra_alignmask = 0,
  4186. .cra_module = THIS_MODULE,
  4187. },
  4188. };
  4189. static struct aead_alg _qcrypto_aead_rfc4309_ccm_algo = {
  4190. .setkey = _qcrypto_aead_rfc4309_ccm_setkey,
  4191. .setauthsize = _qcrypto_aead_rfc4309_ccm_setauthsize,
  4192. .encrypt = _qcrypto_aead_rfc4309_enc_aes_ccm,
  4193. .decrypt = _qcrypto_aead_rfc4309_dec_aes_ccm,
  4194. .init = _qcrypto_cra_aead_rfc4309_ccm_init,
  4195. .exit = _qcrypto_cra_aead_exit,
  4196. .ivsize = 8,
  4197. .maxauthsize = 16,
  4198. .base = {
  4199. .cra_name = "rfc4309(ccm(aes))",
  4200. .cra_driver_name = "qcrypto-rfc4309-aes-ccm",
  4201. .cra_priority = 300,
  4202. .cra_flags = CRYPTO_ALG_ASYNC,
  4203. .cra_blocksize = 1,
  4204. .cra_ctxsize = sizeof(struct qcrypto_cipher_ctx),
  4205. .cra_alignmask = 0,
  4206. .cra_module = THIS_MODULE,
  4207. },
  4208. };
  4209. static int _qcrypto_probe(struct platform_device *pdev)
  4210. {
  4211. int rc = 0;
  4212. void *handle;
  4213. struct crypto_priv *cp = &qcrypto_dev;
  4214. int i;
  4215. struct msm_ce_hw_support *platform_support;
  4216. struct crypto_engine *pengine;
  4217. unsigned long flags;
  4218. struct qcrypto_req_control *pqcrypto_req_control = NULL;
  4219. pengine = kzalloc(sizeof(*pengine), GFP_KERNEL);
  4220. if (!pengine)
  4221. return -ENOMEM;
  4222. pengine->icc_path = of_icc_get(&pdev->dev, "data_path");
  4223. if (IS_ERR(pengine->icc_path)) {
  4224. dev_err(&pdev->dev, "failed to get icc path\n");
  4225. rc = PTR_ERR(pengine->icc_path);
  4226. goto exit_kzfree;
  4227. }
  4228. pengine->bw_state = BUS_NO_BANDWIDTH;
  4229. rc = icc_set_bw(pengine->icc_path, CRYPTO_AVG_BW, CRYPTO_PEAK_BW);
  4230. if (rc) {
  4231. dev_err(&pdev->dev, "failed to set high bandwidth\n");
  4232. goto exit_kzfree;
  4233. }
  4234. handle = qce_open(pdev, &rc);
  4235. if (handle == NULL) {
  4236. rc = -ENODEV;
  4237. goto exit_free_pdata;
  4238. }
  4239. rc = icc_set_bw(pengine->icc_path, 0, 0);
  4240. if (rc) {
  4241. dev_err(&pdev->dev, "failed to set low bandwidth\n");
  4242. goto exit_qce_close;
  4243. }
  4244. platform_set_drvdata(pdev, pengine);
  4245. pengine->qce = handle;
  4246. pengine->pcp = cp;
  4247. pengine->pdev = pdev;
  4248. pengine->signature = 0xdeadbeef;
  4249. timer_setup(&(pengine->bw_reaper_timer),
  4250. qcrypto_bw_reaper_timer_callback, 0);
  4251. INIT_WORK(&pengine->bw_reaper_ws, qcrypto_bw_reaper_work);
  4252. INIT_WORK(&pengine->bw_allocate_ws, qcrypto_bw_allocate_work);
  4253. pengine->high_bw_req = false;
  4254. pengine->active_seq = 0;
  4255. pengine->last_active_seq = 0;
  4256. pengine->check_flag = false;
  4257. pengine->max_req_used = 0;
  4258. pengine->issue_req = false;
  4259. crypto_init_queue(&pengine->req_queue, MSM_QCRYPTO_REQ_QUEUE_LENGTH);
  4260. mutex_lock(&cp->engine_lock);
  4261. cp->total_units++;
  4262. pengine->unit = cp->total_units;
  4263. spin_lock_irqsave(&cp->lock, flags);
  4264. pengine->first_engine = list_empty(&cp->engine_list);
  4265. if (pengine->first_engine)
  4266. cp->first_engine = pengine;
  4267. list_add_tail(&pengine->elist, &cp->engine_list);
  4268. cp->next_engine = pengine;
  4269. spin_unlock_irqrestore(&cp->lock, flags);
  4270. qce_hw_support(pengine->qce, &cp->ce_support);
  4271. pengine->ce_hw_instance = cp->ce_support.ce_hw_instance;
  4272. pengine->max_req = cp->ce_support.max_request;
  4273. pqcrypto_req_control = kcalloc(pengine->max_req,
  4274. sizeof(struct qcrypto_req_control),
  4275. GFP_KERNEL);
  4276. if (pqcrypto_req_control == NULL) {
  4277. rc = -ENOMEM;
  4278. goto exit_unlock_mutex;
  4279. }
  4280. qcrypto_init_req_control(pengine, pqcrypto_req_control);
  4281. if (cp->ce_support.bam) {
  4282. cp->platform_support.ce_shared = cp->ce_support.is_shared;
  4283. cp->platform_support.shared_ce_resource = 0;
  4284. cp->platform_support.hw_key_support = cp->ce_support.hw_key;
  4285. cp->platform_support.sha_hmac = 1;
  4286. pengine->ce_device = cp->ce_support.ce_device;
  4287. } else {
  4288. platform_support =
  4289. (struct msm_ce_hw_support *)pdev->dev.platform_data;
  4290. cp->platform_support.ce_shared = platform_support->ce_shared;
  4291. cp->platform_support.shared_ce_resource =
  4292. platform_support->shared_ce_resource;
  4293. cp->platform_support.hw_key_support =
  4294. platform_support->hw_key_support;
  4295. cp->platform_support.sha_hmac = platform_support->sha_hmac;
  4296. }
  4297. if (cp->total_units != 1)
  4298. goto exit_unlock_mutex;
  4299. /* register crypto cipher algorithms the device supports */
  4300. for (i = 0; i < ARRAY_SIZE(_qcrypto_sk_cipher_algos); i++) {
  4301. struct qcrypto_alg *q_alg;
  4302. q_alg = _qcrypto_cipher_alg_alloc(cp,
  4303. &_qcrypto_sk_cipher_algos[i]);
  4304. if (IS_ERR(q_alg)) {
  4305. rc = PTR_ERR(q_alg);
  4306. goto err;
  4307. }
  4308. if (cp->ce_support.use_sw_aes_cbc_ecb_ctr_algo) {
  4309. rc = _qcrypto_prefix_alg_cra_name(
  4310. q_alg->cipher_alg.base.cra_name,
  4311. strlen(q_alg->cipher_alg.base.cra_name));
  4312. if (rc) {
  4313. dev_err(&pdev->dev,
  4314. "The algorithm name %s is too long.\n",
  4315. q_alg->cipher_alg.base.cra_name);
  4316. kfree(q_alg);
  4317. goto err;
  4318. }
  4319. }
  4320. rc = crypto_register_skcipher(&q_alg->cipher_alg);
  4321. if (rc) {
  4322. dev_err(&pdev->dev, "%s alg registration failed\n",
  4323. q_alg->cipher_alg.base.cra_driver_name);
  4324. kfree_sensitive(q_alg);
  4325. } else {
  4326. list_add_tail(&q_alg->entry, &cp->alg_list);
  4327. dev_info(&pdev->dev, "%s\n",
  4328. q_alg->cipher_alg.base.cra_driver_name);
  4329. }
  4330. }
  4331. /* register crypto cipher algorithms the device supports */
  4332. if (cp->ce_support.aes_xts) {
  4333. struct qcrypto_alg *q_alg;
  4334. q_alg = _qcrypto_cipher_alg_alloc(cp,
  4335. &_qcrypto_sk_cipher_xts_algo);
  4336. if (IS_ERR(q_alg)) {
  4337. rc = PTR_ERR(q_alg);
  4338. goto err;
  4339. }
  4340. if (cp->ce_support.use_sw_aes_xts_algo) {
  4341. rc = _qcrypto_prefix_alg_cra_name(
  4342. q_alg->cipher_alg.base.cra_name,
  4343. strlen(q_alg->cipher_alg.base.cra_name));
  4344. if (rc) {
  4345. dev_err(&pdev->dev,
  4346. "The algorithm name %s is too long.\n",
  4347. q_alg->cipher_alg.base.cra_name);
  4348. kfree(q_alg);
  4349. goto err;
  4350. }
  4351. }
  4352. rc = crypto_register_skcipher(&q_alg->cipher_alg);
  4353. if (rc) {
  4354. dev_err(&pdev->dev, "%s alg registration failed\n",
  4355. q_alg->cipher_alg.base.cra_driver_name);
  4356. kfree_sensitive(q_alg);
  4357. } else {
  4358. list_add_tail(&q_alg->entry, &cp->alg_list);
  4359. dev_info(&pdev->dev, "%s\n",
  4360. q_alg->cipher_alg.base.cra_driver_name);
  4361. }
  4362. }
  4363. /*
  4364. * Register crypto hash (sha1 and sha256) algorithms the
  4365. * device supports
  4366. */
  4367. for (i = 0; i < ARRAY_SIZE(_qcrypto_ahash_algos); i++) {
  4368. struct qcrypto_alg *q_alg = NULL;
  4369. q_alg = _qcrypto_sha_alg_alloc(cp, &_qcrypto_ahash_algos[i]);
  4370. if (IS_ERR(q_alg)) {
  4371. rc = PTR_ERR(q_alg);
  4372. goto err;
  4373. }
  4374. if (cp->ce_support.use_sw_ahash_algo) {
  4375. rc = _qcrypto_prefix_alg_cra_name(
  4376. q_alg->sha_alg.halg.base.cra_name,
  4377. strlen(q_alg->sha_alg.halg.base.cra_name));
  4378. if (rc) {
  4379. dev_err(&pdev->dev,
  4380. "The algorithm name %s is too long.\n",
  4381. q_alg->sha_alg.halg.base.cra_name);
  4382. kfree(q_alg);
  4383. goto err;
  4384. }
  4385. }
  4386. rc = crypto_register_ahash(&q_alg->sha_alg);
  4387. if (rc) {
  4388. dev_err(&pdev->dev, "%s alg registration failed\n",
  4389. q_alg->sha_alg.halg.base.cra_driver_name);
  4390. kfree_sensitive(q_alg);
  4391. } else {
  4392. list_add_tail(&q_alg->entry, &cp->alg_list);
  4393. dev_info(&pdev->dev, "%s\n",
  4394. q_alg->sha_alg.halg.base.cra_driver_name);
  4395. }
  4396. }
  4397. /* register crypto aead (hmac-sha1) algorithms the device supports */
  4398. if (cp->ce_support.sha1_hmac_20 || cp->ce_support.sha1_hmac
  4399. || cp->ce_support.sha_hmac) {
  4400. for (i = 0; i < ARRAY_SIZE(_qcrypto_aead_sha1_hmac_algos);
  4401. i++) {
  4402. struct qcrypto_alg *q_alg;
  4403. q_alg = _qcrypto_aead_alg_alloc(cp,
  4404. &_qcrypto_aead_sha1_hmac_algos[i]);
  4405. if (IS_ERR(q_alg)) {
  4406. rc = PTR_ERR(q_alg);
  4407. goto err;
  4408. }
  4409. if (cp->ce_support.use_sw_aead_algo) {
  4410. rc = _qcrypto_prefix_alg_cra_name(
  4411. q_alg->aead_alg.base.cra_name,
  4412. strlen(q_alg->aead_alg.base.cra_name));
  4413. if (rc) {
  4414. dev_err(&pdev->dev,
  4415. "The algorithm name %s is too long.\n",
  4416. q_alg->aead_alg.base.cra_name);
  4417. kfree(q_alg);
  4418. goto err;
  4419. }
  4420. }
  4421. rc = crypto_register_aead(&q_alg->aead_alg);
  4422. if (rc) {
  4423. dev_err(&pdev->dev,
  4424. "%s alg registration failed\n",
  4425. q_alg->aead_alg.base.cra_driver_name);
  4426. kfree(q_alg);
  4427. } else {
  4428. list_add_tail(&q_alg->entry, &cp->alg_list);
  4429. dev_info(&pdev->dev, "%s\n",
  4430. q_alg->aead_alg.base.cra_driver_name);
  4431. }
  4432. }
  4433. }
  4434. /* register crypto aead (hmac-sha256) algorithms the device supports */
  4435. if (cp->ce_support.sha_hmac) {
  4436. for (i = 0; i < ARRAY_SIZE(_qcrypto_aead_sha256_hmac_algos);
  4437. i++) {
  4438. struct qcrypto_alg *q_alg;
  4439. q_alg = _qcrypto_aead_alg_alloc(cp,
  4440. &_qcrypto_aead_sha256_hmac_algos[i]);
  4441. if (IS_ERR(q_alg)) {
  4442. rc = PTR_ERR(q_alg);
  4443. goto err;
  4444. }
  4445. if (cp->ce_support.use_sw_aead_algo) {
  4446. rc = _qcrypto_prefix_alg_cra_name(
  4447. q_alg->aead_alg.base.cra_name,
  4448. strlen(q_alg->aead_alg.base.cra_name));
  4449. if (rc) {
  4450. dev_err(&pdev->dev,
  4451. "The algorithm name %s is too long.\n",
  4452. q_alg->aead_alg.base.cra_name);
  4453. kfree(q_alg);
  4454. goto err;
  4455. }
  4456. }
  4457. rc = crypto_register_aead(&q_alg->aead_alg);
  4458. if (rc) {
  4459. dev_err(&pdev->dev,
  4460. "%s alg registration failed\n",
  4461. q_alg->aead_alg.base.cra_driver_name);
  4462. kfree(q_alg);
  4463. } else {
  4464. list_add_tail(&q_alg->entry, &cp->alg_list);
  4465. dev_info(&pdev->dev, "%s\n",
  4466. q_alg->aead_alg.base.cra_driver_name);
  4467. }
  4468. }
  4469. }
  4470. if ((cp->ce_support.sha_hmac) || (cp->platform_support.sha_hmac)) {
  4471. /* register crypto hmac algorithms the device supports */
  4472. for (i = 0; i < ARRAY_SIZE(_qcrypto_sha_hmac_algos); i++) {
  4473. struct qcrypto_alg *q_alg = NULL;
  4474. q_alg = _qcrypto_sha_alg_alloc(cp,
  4475. &_qcrypto_sha_hmac_algos[i]);
  4476. if (IS_ERR(q_alg)) {
  4477. rc = PTR_ERR(q_alg);
  4478. goto err;
  4479. }
  4480. if (cp->ce_support.use_sw_hmac_algo) {
  4481. rc = _qcrypto_prefix_alg_cra_name(
  4482. q_alg->sha_alg.halg.base.cra_name,
  4483. strlen(
  4484. q_alg->sha_alg.halg.base.cra_name));
  4485. if (rc) {
  4486. dev_err(&pdev->dev,
  4487. "The algorithm name %s is too long.\n",
  4488. q_alg->sha_alg.halg.base.cra_name);
  4489. kfree(q_alg);
  4490. goto err;
  4491. }
  4492. }
  4493. rc = crypto_register_ahash(&q_alg->sha_alg);
  4494. if (rc) {
  4495. dev_err(&pdev->dev,
  4496. "%s alg registration failed\n",
  4497. q_alg->sha_alg.halg.base.cra_driver_name);
  4498. kfree_sensitive(q_alg);
  4499. } else {
  4500. list_add_tail(&q_alg->entry, &cp->alg_list);
  4501. dev_info(&pdev->dev, "%s\n",
  4502. q_alg->sha_alg.halg.base.cra_driver_name);
  4503. }
  4504. }
  4505. }
  4506. /*
  4507. * Register crypto cipher (aes-ccm) algorithms the
  4508. * device supports
  4509. */
  4510. if (cp->ce_support.aes_ccm) {
  4511. struct qcrypto_alg *q_alg;
  4512. q_alg = _qcrypto_aead_alg_alloc(cp, &_qcrypto_aead_ccm_algo);
  4513. if (IS_ERR(q_alg)) {
  4514. rc = PTR_ERR(q_alg);
  4515. goto err;
  4516. }
  4517. if (cp->ce_support.use_sw_aes_ccm_algo) {
  4518. rc = _qcrypto_prefix_alg_cra_name(
  4519. q_alg->aead_alg.base.cra_name,
  4520. strlen(q_alg->aead_alg.base.cra_name));
  4521. if (rc) {
  4522. dev_err(&pdev->dev,
  4523. "The algorithm name %s is too long.\n",
  4524. q_alg->aead_alg.base.cra_name);
  4525. kfree(q_alg);
  4526. goto err;
  4527. }
  4528. }
  4529. rc = crypto_register_aead(&q_alg->aead_alg);
  4530. if (rc) {
  4531. dev_err(&pdev->dev, "%s alg registration failed\n",
  4532. q_alg->aead_alg.base.cra_driver_name);
  4533. kfree_sensitive(q_alg);
  4534. } else {
  4535. list_add_tail(&q_alg->entry, &cp->alg_list);
  4536. dev_info(&pdev->dev, "%s\n",
  4537. q_alg->aead_alg.base.cra_driver_name);
  4538. }
  4539. q_alg = _qcrypto_aead_alg_alloc(cp,
  4540. &_qcrypto_aead_rfc4309_ccm_algo);
  4541. if (IS_ERR(q_alg)) {
  4542. rc = PTR_ERR(q_alg);
  4543. goto err;
  4544. }
  4545. if (cp->ce_support.use_sw_aes_ccm_algo) {
  4546. rc = _qcrypto_prefix_alg_cra_name(
  4547. q_alg->aead_alg.base.cra_name,
  4548. strlen(q_alg->aead_alg.base.cra_name));
  4549. if (rc) {
  4550. dev_err(&pdev->dev,
  4551. "The algorithm name %s is too long.\n",
  4552. q_alg->aead_alg.base.cra_name);
  4553. kfree(q_alg);
  4554. goto err;
  4555. }
  4556. }
  4557. rc = crypto_register_aead(&q_alg->aead_alg);
  4558. if (rc) {
  4559. dev_err(&pdev->dev, "%s alg registration failed\n",
  4560. q_alg->aead_alg.base.cra_driver_name);
  4561. kfree(q_alg);
  4562. } else {
  4563. list_add_tail(&q_alg->entry, &cp->alg_list);
  4564. dev_info(&pdev->dev, "%s\n",
  4565. q_alg->aead_alg.base.cra_driver_name);
  4566. }
  4567. }
  4568. mutex_unlock(&cp->engine_lock);
  4569. return 0;
  4570. err:
  4571. _qcrypto_remove_engine(pengine);
  4572. kfree_sensitive(pqcrypto_req_control);
  4573. exit_unlock_mutex:
  4574. mutex_unlock(&cp->engine_lock);
  4575. exit_qce_close:
  4576. if (pengine->qce)
  4577. qce_close(pengine->qce);
  4578. exit_free_pdata:
  4579. icc_set_bw(pengine->icc_path, 0, 0);
  4580. platform_set_drvdata(pdev, NULL);
  4581. exit_kzfree:
  4582. memset(pengine, 0, ksize((void *)pengine));
  4583. kfree(pengine);
  4584. return rc;
  4585. }
  4586. static int _qcrypto_engine_in_use(struct crypto_engine *pengine)
  4587. {
  4588. struct crypto_priv *cp = pengine->pcp;
  4589. if ((atomic_read(&pengine->req_count) > 0) || pengine->req_queue.qlen
  4590. || cp->req_queue.qlen)
  4591. return 1;
  4592. return 0;
  4593. }
  4594. static void _qcrypto_do_suspending(struct crypto_engine *pengine)
  4595. {
  4596. del_timer_sync(&pengine->bw_reaper_timer);
  4597. qcrypto_ce_set_bus(pengine, false);
  4598. }
  4599. static int _qcrypto_suspend(struct platform_device *pdev, pm_message_t state)
  4600. {
  4601. int ret = 0;
  4602. struct crypto_engine *pengine;
  4603. struct crypto_priv *cp;
  4604. unsigned long flags;
  4605. pengine = platform_get_drvdata(pdev);
  4606. if (!pengine)
  4607. return -EINVAL;
  4608. /*
  4609. * Check if this platform supports clock management in suspend/resume
  4610. * If not, just simply return 0.
  4611. */
  4612. cp = pengine->pcp;
  4613. if (!cp->ce_support.clk_mgmt_sus_res)
  4614. return 0;
  4615. spin_lock_irqsave(&cp->lock, flags);
  4616. switch (pengine->bw_state) {
  4617. case BUS_NO_BANDWIDTH:
  4618. if (!pengine->high_bw_req)
  4619. pengine->bw_state = BUS_SUSPENDED;
  4620. else
  4621. ret = -EBUSY;
  4622. break;
  4623. case BUS_HAS_BANDWIDTH:
  4624. if (_qcrypto_engine_in_use(pengine)) {
  4625. ret = -EBUSY;
  4626. } else {
  4627. pengine->bw_state = BUS_SUSPENDING;
  4628. spin_unlock_irqrestore(&cp->lock, flags);
  4629. _qcrypto_do_suspending(pengine);
  4630. spin_lock_irqsave(&cp->lock, flags);
  4631. pengine->bw_state = BUS_SUSPENDED;
  4632. }
  4633. break;
  4634. case BUS_BANDWIDTH_RELEASING:
  4635. case BUS_BANDWIDTH_ALLOCATING:
  4636. case BUS_SUSPENDED:
  4637. case BUS_SUSPENDING:
  4638. default:
  4639. ret = -EBUSY;
  4640. break;
  4641. }
  4642. spin_unlock_irqrestore(&cp->lock, flags);
  4643. if (ret)
  4644. return ret;
  4645. if (qce_pm_table.suspend) {
  4646. qcrypto_ce_set_bus(pengine, true);
  4647. qce_pm_table.suspend(pengine->qce);
  4648. qcrypto_ce_set_bus(pengine, false);
  4649. }
  4650. return 0;
  4651. }
  4652. static int _qcrypto_resume(struct platform_device *pdev)
  4653. {
  4654. struct crypto_engine *pengine;
  4655. struct crypto_priv *cp;
  4656. unsigned long flags;
  4657. int ret = 0;
  4658. pengine = platform_get_drvdata(pdev);
  4659. if (!pengine)
  4660. return -EINVAL;
  4661. cp = pengine->pcp;
  4662. if (!cp->ce_support.clk_mgmt_sus_res)
  4663. return 0;
  4664. spin_lock_irqsave(&cp->lock, flags);
  4665. if (pengine->bw_state == BUS_SUSPENDED) {
  4666. spin_unlock_irqrestore(&cp->lock, flags);
  4667. if (qce_pm_table.resume) {
  4668. qcrypto_ce_set_bus(pengine, true);
  4669. qce_pm_table.resume(pengine->qce);
  4670. qcrypto_ce_set_bus(pengine, false);
  4671. }
  4672. spin_lock_irqsave(&cp->lock, flags);
  4673. pengine->bw_state = BUS_NO_BANDWIDTH;
  4674. pengine->active_seq++;
  4675. pengine->check_flag = false;
  4676. if (cp->req_queue.qlen || pengine->req_queue.qlen) {
  4677. if (!pengine->high_bw_req) {
  4678. qcrypto_ce_bw_allocate_req(pengine);
  4679. pengine->high_bw_req = true;
  4680. }
  4681. }
  4682. } else
  4683. ret = -EBUSY;
  4684. spin_unlock_irqrestore(&cp->lock, flags);
  4685. return ret;
  4686. }
  4687. static const struct of_device_id qcrypto_match[] = {
  4688. {.compatible = "qcom,qcrypto",},
  4689. {}
  4690. };
  4691. static struct platform_driver __qcrypto = {
  4692. .probe = _qcrypto_probe,
  4693. .remove = _qcrypto_remove,
  4694. .suspend = _qcrypto_suspend,
  4695. .resume = _qcrypto_resume,
  4696. .driver = {
  4697. .name = "qcrypto",
  4698. .of_match_table = qcrypto_match,
  4699. },
  4700. };
  4701. static int _debug_qcrypto;
  4702. static ssize_t _debug_stats_read(struct file *file, char __user *buf,
  4703. size_t count, loff_t *ppos)
  4704. {
  4705. int rc = -EINVAL;
  4706. int qcrypto = *((int *) file->private_data);
  4707. int len;
  4708. len = _disp_stats(qcrypto);
  4709. if (len <= count)
  4710. rc = simple_read_from_buffer((void __user *) buf, len,
  4711. ppos, (void *) _debug_read_buf, len);
  4712. return rc;
  4713. }
  4714. static ssize_t _debug_stats_write(struct file *file, const char __user *buf,
  4715. size_t count, loff_t *ppos)
  4716. {
  4717. unsigned long flags;
  4718. struct crypto_priv *cp = &qcrypto_dev;
  4719. struct crypto_engine *pe;
  4720. int i;
  4721. memset((char *)&_qcrypto_stat, 0, sizeof(struct crypto_stat));
  4722. spin_lock_irqsave(&cp->lock, flags);
  4723. list_for_each_entry(pe, &cp->engine_list, elist) {
  4724. pe->total_req = 0;
  4725. pe->err_req = 0;
  4726. qce_clear_driver_stats(pe->qce);
  4727. pe->max_req_used = 0;
  4728. }
  4729. cp->max_qlen = 0;
  4730. cp->resp_start = 0;
  4731. cp->resp_stop = 0;
  4732. cp->no_avail = 0;
  4733. cp->max_resp_qlen = 0;
  4734. cp->queue_work_eng3 = 0;
  4735. cp->queue_work_not_eng3 = 0;
  4736. cp->queue_work_not_eng3_nz = 0;
  4737. cp->max_reorder_cnt = 0;
  4738. for (i = 0; i < MAX_SMP_CPU + 1; i++)
  4739. cp->cpu_req[i] = 0;
  4740. spin_unlock_irqrestore(&cp->lock, flags);
  4741. return count;
  4742. }
  4743. static const struct file_operations _debug_stats_ops = {
  4744. .open = simple_open,
  4745. .read = _debug_stats_read,
  4746. .write = _debug_stats_write,
  4747. };
  4748. static int _qcrypto_debug_init(void)
  4749. {
  4750. int rc;
  4751. char name[DEBUG_MAX_FNAME];
  4752. struct dentry *dent;
  4753. _debug_dent = debugfs_create_dir("qcrypto", NULL);
  4754. if (IS_ERR(_debug_dent)) {
  4755. pr_debug("qcrypto debugfs_create_dir fail, error %ld\n",
  4756. PTR_ERR(_debug_dent));
  4757. return PTR_ERR(_debug_dent);
  4758. }
  4759. snprintf(name, DEBUG_MAX_FNAME-1, "stats-%d", 1);
  4760. _debug_qcrypto = 0;
  4761. dent = debugfs_create_file(name, 0644, _debug_dent,
  4762. &_debug_qcrypto, &_debug_stats_ops);
  4763. if (dent == NULL) {
  4764. pr_debug("qcrypto debugfs_create_file fail, error %ld\n",
  4765. PTR_ERR(dent));
  4766. rc = PTR_ERR(dent);
  4767. goto err;
  4768. }
  4769. return 0;
  4770. err:
  4771. debugfs_remove_recursive(_debug_dent);
  4772. return rc;
  4773. }
  4774. static int __init _qcrypto_init(void)
  4775. {
  4776. struct crypto_priv *pcp = &qcrypto_dev;
  4777. _qcrypto_debug_init();
  4778. INIT_LIST_HEAD(&pcp->alg_list);
  4779. INIT_LIST_HEAD(&pcp->engine_list);
  4780. init_llist_head(&pcp->ordered_resp_list);
  4781. spin_lock_init(&pcp->lock);
  4782. mutex_init(&pcp->engine_lock);
  4783. pcp->resp_wq = alloc_workqueue("qcrypto_seq_response_wq",
  4784. WQ_MEM_RECLAIM | WQ_HIGHPRI | WQ_CPU_INTENSIVE, 1);
  4785. if (!pcp->resp_wq) {
  4786. pr_err("Error allocating workqueue\n");
  4787. return -ENOMEM;
  4788. }
  4789. INIT_WORK(&pcp->resp_work, seq_response);
  4790. pcp->total_units = 0;
  4791. pcp->next_engine = NULL;
  4792. pcp->scheduled_eng = NULL;
  4793. pcp->ce_req_proc_sts = IN_PROGRESS;
  4794. crypto_init_queue(&pcp->req_queue, MSM_QCRYPTO_REQ_QUEUE_LENGTH);
  4795. return platform_driver_register(&__qcrypto);
  4796. }
  4797. static void __exit _qcrypto_exit(void)
  4798. {
  4799. pr_debug("%s Unregister QCRYPTO\n", __func__);
  4800. debugfs_remove_recursive(_debug_dent);
  4801. platform_driver_unregister(&__qcrypto);
  4802. }
  4803. module_init(_qcrypto_init);
  4804. module_exit(_qcrypto_exit);
  4805. MODULE_LICENSE("GPL v2");
  4806. MODULE_DESCRIPTION("QTI Crypto driver");