qcrypto.c 149 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504
  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. else
  1214. return -EINVAL;
  1215. default:
  1216. //crypto_ablkcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
  1217. return -EINVAL;
  1218. }
  1219. return 0;
  1220. }
  1221. static int _qcrypto_setkey_aes_192_fallback(struct crypto_skcipher *tfm,
  1222. const u8 *key)
  1223. {
  1224. //struct crypto_tfm *tfm = crypto_ablkcipher_tfm(cipher);
  1225. struct qcrypto_cipher_ctx *ctx = crypto_skcipher_ctx(tfm);
  1226. int ret;
  1227. ctx->enc_key_len = AES_KEYSIZE_192;
  1228. crypto_sync_skcipher_clear_flags(ctx->cipher_aes192_fb,
  1229. CRYPTO_TFM_REQ_MASK);
  1230. crypto_sync_skcipher_set_flags(ctx->cipher_aes192_fb,
  1231. (crypto_skcipher_get_flags(tfm) & CRYPTO_TFM_REQ_MASK));
  1232. ret = crypto_sync_skcipher_setkey(ctx->cipher_aes192_fb, key,
  1233. AES_KEYSIZE_192);
  1234. /*
  1235. * TODO: delete or find equivalent in new crypto_skcipher api
  1236. if (ret) {
  1237. tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK;
  1238. tfm->crt_flags |=
  1239. (cipher->base.crt_flags & CRYPTO_TFM_RES_MASK);
  1240. }
  1241. */
  1242. return ret;
  1243. }
  1244. static int _qcrypto_setkey_aes(struct crypto_skcipher *tfm, const u8 *key,
  1245. unsigned int keylen)
  1246. {
  1247. struct qcrypto_cipher_ctx *ctx = crypto_skcipher_ctx(tfm);
  1248. struct crypto_priv *cp = ctx->cp;
  1249. if ((ctx->flags & QCRYPTO_CTX_USE_HW_KEY) == QCRYPTO_CTX_USE_HW_KEY)
  1250. return 0;
  1251. if ((keylen == AES_KEYSIZE_192) && (!cp->ce_support.aes_key_192)
  1252. && ctx->cipher_aes192_fb)
  1253. return _qcrypto_setkey_aes_192_fallback(tfm, key);
  1254. if (_qcrypto_check_aes_keylen(cp, keylen))
  1255. return -EINVAL;
  1256. ctx->enc_key_len = keylen;
  1257. if (!(ctx->flags & QCRYPTO_CTX_USE_PIPE_KEY)) {
  1258. if (key != NULL) {
  1259. memcpy(ctx->enc_key, key, keylen);
  1260. } else {
  1261. pr_err("%s Invalid key pointer\n", __func__);
  1262. return -EINVAL;
  1263. }
  1264. }
  1265. return 0;
  1266. }
  1267. static int _qcrypto_setkey_aes_xts(struct crypto_skcipher *tfm,
  1268. const u8 *key, unsigned int keylen)
  1269. {
  1270. struct qcrypto_cipher_ctx *ctx = crypto_skcipher_ctx(tfm);
  1271. struct crypto_priv *cp = ctx->cp;
  1272. if ((ctx->flags & QCRYPTO_CTX_USE_HW_KEY) == QCRYPTO_CTX_USE_HW_KEY)
  1273. return 0;
  1274. if (_qcrypto_check_aes_keylen(cp, keylen/2))
  1275. return -EINVAL;
  1276. ctx->enc_key_len = keylen;
  1277. if (!(ctx->flags & QCRYPTO_CTX_USE_PIPE_KEY)) {
  1278. if (key != NULL) {
  1279. memcpy(ctx->enc_key, key, keylen);
  1280. } else {
  1281. pr_err("%s Invalid key pointer\n", __func__);
  1282. return -EINVAL;
  1283. }
  1284. }
  1285. return 0;
  1286. }
  1287. static int _qcrypto_setkey_des(struct crypto_skcipher *tfm, const u8 *key,
  1288. unsigned int keylen)
  1289. {
  1290. struct qcrypto_cipher_ctx *ctx = crypto_skcipher_ctx(tfm);
  1291. struct des_ctx dctx;
  1292. if (!key) {
  1293. pr_err("%s Invalid key pointer\n", __func__);
  1294. return -EINVAL;
  1295. }
  1296. if ((ctx->flags & QCRYPTO_CTX_USE_HW_KEY) == QCRYPTO_CTX_USE_HW_KEY) {
  1297. pr_err("%s HW KEY usage not supported for DES algorithm\n", __func__);
  1298. return 0;
  1299. }
  1300. if (keylen != DES_KEY_SIZE) {
  1301. //crypto_ablkcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
  1302. return -EINVAL;
  1303. }
  1304. memset(&dctx, 0, sizeof(dctx));
  1305. /*Need to be fixed. Compilation error was seen with the below API.
  1306. Needs to be uncommented and enable
  1307. if (des_expand_key(&dctx, key, keylen) == -ENOKEY) {
  1308. if (crypto_skcipher_get_flags(tfm) & CRYPTO_TFM_REQ_FORBID_WEAK_KEYS)
  1309. return -EINVAL;
  1310. else
  1311. return 0;
  1312. }*/
  1313. /*
  1314. * TODO: delete of find equivalent in skcipher api
  1315. if (ret) {
  1316. tfm->crt_flags |= CRYPTO_TFM_RES_WEAK_KEY;
  1317. crypto_tfm_set_flags(tfm, CRYPTO_TFM_RES_WEAK_KEY);
  1318. return -EINVAL;
  1319. }
  1320. */
  1321. ctx->enc_key_len = keylen;
  1322. if (!(ctx->flags & QCRYPTO_CTX_USE_PIPE_KEY))
  1323. memcpy(ctx->enc_key, key, keylen);
  1324. return 0;
  1325. }
  1326. static int _qcrypto_setkey_3des(struct crypto_skcipher *tfm, const u8 *key,
  1327. unsigned int keylen)
  1328. {
  1329. struct qcrypto_cipher_ctx *ctx = crypto_skcipher_ctx(tfm);
  1330. if ((ctx->flags & QCRYPTO_CTX_USE_HW_KEY) == QCRYPTO_CTX_USE_HW_KEY) {
  1331. pr_err("%s HW KEY usage not supported for 3DES algorithm\n", __func__);
  1332. return 0;
  1333. }
  1334. if (keylen != DES3_EDE_KEY_SIZE) {
  1335. //crypto_ablkcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
  1336. return -EINVAL;
  1337. }
  1338. ctx->enc_key_len = keylen;
  1339. if (!(ctx->flags & QCRYPTO_CTX_USE_PIPE_KEY)) {
  1340. if (key != NULL) {
  1341. memcpy(ctx->enc_key, key, keylen);
  1342. } else {
  1343. pr_err("%s Invalid key pointer\n", __func__);
  1344. return -EINVAL;
  1345. }
  1346. }
  1347. return 0;
  1348. }
  1349. static void seq_response(struct work_struct *work)
  1350. {
  1351. struct crypto_priv *cp = container_of(work, struct crypto_priv,
  1352. resp_work);
  1353. struct llist_node *list;
  1354. struct llist_node *rev = NULL;
  1355. struct crypto_engine *pengine;
  1356. unsigned long flags;
  1357. int total_unit;
  1358. again:
  1359. list = llist_del_all(&cp->ordered_resp_list);
  1360. if (!list)
  1361. goto end;
  1362. while (list) {
  1363. struct llist_node *t = list;
  1364. list = llist_next(list);
  1365. t->next = rev;
  1366. rev = t;
  1367. }
  1368. while (rev) {
  1369. struct qcrypto_resp_ctx *arsp;
  1370. struct crypto_async_request *areq;
  1371. arsp = container_of(rev, struct qcrypto_resp_ctx, llist);
  1372. rev = llist_next(rev);
  1373. areq = arsp->async_req;
  1374. local_bh_disable();
  1375. areq->complete(areq, arsp->res);
  1376. local_bh_enable();
  1377. atomic_dec(&cp->resp_cnt);
  1378. }
  1379. if (atomic_read(&cp->resp_cnt) < COMPLETION_CB_BACKLOG_LENGTH_START &&
  1380. (cmpxchg(&cp->ce_req_proc_sts, STOPPED, IN_PROGRESS)
  1381. == STOPPED)) {
  1382. cp->resp_start++;
  1383. for (total_unit = cp->total_units; total_unit-- > 0;) {
  1384. spin_lock_irqsave(&cp->lock, flags);
  1385. pengine = _avail_eng(cp);
  1386. spin_unlock_irqrestore(&cp->lock, flags);
  1387. if (pengine)
  1388. _start_qcrypto_process(cp, pengine);
  1389. else
  1390. break;
  1391. }
  1392. }
  1393. end:
  1394. if (cmpxchg(&cp->sched_resp_workq_status, SCHEDULE_AGAIN,
  1395. IS_SCHEDULED) == SCHEDULE_AGAIN)
  1396. goto again;
  1397. else if (cmpxchg(&cp->sched_resp_workq_status, IS_SCHEDULED,
  1398. NOT_SCHEDULED) == SCHEDULE_AGAIN)
  1399. goto end;
  1400. }
  1401. #define SCHEUDLE_RSP_QLEN_THRESHOLD 64
  1402. static void _qcrypto_tfm_complete(struct crypto_engine *pengine, u32 type,
  1403. void *tfm_ctx,
  1404. struct qcrypto_resp_ctx *cur_arsp,
  1405. int res)
  1406. {
  1407. struct crypto_priv *cp = pengine->pcp;
  1408. unsigned long flags;
  1409. struct qcrypto_resp_ctx *arsp;
  1410. struct list_head *plist;
  1411. unsigned int resp_qlen;
  1412. unsigned int cnt = 0;
  1413. switch (type) {
  1414. case CRYPTO_ALG_TYPE_AHASH:
  1415. plist = &((struct qcrypto_sha_ctx *) tfm_ctx)->rsp_queue;
  1416. break;
  1417. case CRYPTO_ALG_TYPE_SKCIPHER:
  1418. case CRYPTO_ALG_TYPE_AEAD:
  1419. default:
  1420. plist = &((struct qcrypto_cipher_ctx *) tfm_ctx)->rsp_queue;
  1421. break;
  1422. }
  1423. spin_lock_irqsave(&cp->lock, flags);
  1424. cur_arsp->res = res;
  1425. while (!list_empty(plist)) {
  1426. arsp = list_first_entry(plist,
  1427. struct qcrypto_resp_ctx, list);
  1428. if (arsp->res == -EINPROGRESS)
  1429. break;
  1430. list_del(&arsp->list);
  1431. llist_add(&arsp->llist, &cp->ordered_resp_list);
  1432. atomic_inc(&cp->resp_cnt);
  1433. cnt++;
  1434. }
  1435. resp_qlen = atomic_read(&cp->resp_cnt);
  1436. if (resp_qlen > cp->max_resp_qlen)
  1437. cp->max_resp_qlen = resp_qlen;
  1438. if (cnt > cp->max_reorder_cnt)
  1439. cp->max_reorder_cnt = cnt;
  1440. if ((resp_qlen >= COMPLETION_CB_BACKLOG_LENGTH_STOP) &&
  1441. cmpxchg(&cp->ce_req_proc_sts, IN_PROGRESS,
  1442. STOPPED) == IN_PROGRESS) {
  1443. cp->resp_stop++;
  1444. }
  1445. spin_unlock_irqrestore(&cp->lock, flags);
  1446. retry:
  1447. if (!llist_empty(&cp->ordered_resp_list)) {
  1448. unsigned int cpu;
  1449. if (pengine->first_engine) {
  1450. cpu = WORK_CPU_UNBOUND;
  1451. cp->queue_work_eng3++;
  1452. } else {
  1453. cp->queue_work_not_eng3++;
  1454. cpu = cp->cpu_getting_irqs_frm_first_ce;
  1455. /*
  1456. * If source not the first engine, and there
  1457. * are outstanding requests going on first engine,
  1458. * skip scheduling of work queue to anticipate
  1459. * more may be coming. If the response queue
  1460. * length exceeds threshold, to avoid further
  1461. * delay, schedule work queue immediately.
  1462. */
  1463. if (cp->first_engine && atomic_read(
  1464. &cp->first_engine->req_count)) {
  1465. if (resp_qlen < SCHEUDLE_RSP_QLEN_THRESHOLD)
  1466. return;
  1467. cp->queue_work_not_eng3_nz++;
  1468. }
  1469. }
  1470. if (cmpxchg(&cp->sched_resp_workq_status, NOT_SCHEDULED,
  1471. IS_SCHEDULED) == NOT_SCHEDULED)
  1472. queue_work_on(cpu, cp->resp_wq, &cp->resp_work);
  1473. else if (cmpxchg(&cp->sched_resp_workq_status, IS_SCHEDULED,
  1474. SCHEDULE_AGAIN) == NOT_SCHEDULED)
  1475. goto retry;
  1476. }
  1477. }
  1478. static void req_done(struct qcrypto_req_control *pqcrypto_req_control)
  1479. {
  1480. struct crypto_engine *pengine;
  1481. struct crypto_async_request *areq;
  1482. struct crypto_priv *cp;
  1483. struct qcrypto_resp_ctx *arsp;
  1484. u32 type = 0;
  1485. void *tfm_ctx = NULL;
  1486. unsigned int cpu;
  1487. int res;
  1488. pengine = pqcrypto_req_control->pce;
  1489. cp = pengine->pcp;
  1490. areq = pqcrypto_req_control->req;
  1491. arsp = pqcrypto_req_control->arsp;
  1492. res = pqcrypto_req_control->res;
  1493. qcrypto_free_req_control(pengine, pqcrypto_req_control);
  1494. if (areq) {
  1495. type = crypto_tfm_alg_type(areq->tfm);
  1496. tfm_ctx = crypto_tfm_ctx(areq->tfm);
  1497. }
  1498. cpu = smp_processor_id();
  1499. pengine->irq_cpu = cpu;
  1500. if (pengine->first_engine) {
  1501. if (cpu != cp->cpu_getting_irqs_frm_first_ce)
  1502. cp->cpu_getting_irqs_frm_first_ce = cpu;
  1503. }
  1504. if (areq)
  1505. _qcrypto_tfm_complete(pengine, type, tfm_ctx, arsp, res);
  1506. if (READ_ONCE(cp->ce_req_proc_sts) == IN_PROGRESS)
  1507. _start_qcrypto_process(cp, pengine);
  1508. }
  1509. static void _qce_ahash_complete(void *cookie, unsigned char *digest,
  1510. unsigned char *authdata, int ret)
  1511. {
  1512. struct ahash_request *areq = (struct ahash_request *) cookie;
  1513. struct crypto_async_request *async_req;
  1514. struct crypto_ahash *ahash = crypto_ahash_reqtfm(areq);
  1515. struct qcrypto_sha_ctx *sha_ctx = crypto_tfm_ctx(areq->base.tfm);
  1516. struct qcrypto_sha_req_ctx *rctx = ahash_request_ctx(areq);
  1517. struct crypto_priv *cp = sha_ctx->cp;
  1518. struct crypto_stat *pstat;
  1519. uint32_t diglen = crypto_ahash_digestsize(ahash);
  1520. uint32_t *auth32 = (uint32_t *)authdata;
  1521. struct crypto_engine *pengine;
  1522. struct qcrypto_req_control *pqcrypto_req_control;
  1523. async_req = &areq->base;
  1524. pstat = &_qcrypto_stat;
  1525. pengine = rctx->pengine;
  1526. pqcrypto_req_control = find_req_control_for_areq(pengine,
  1527. async_req);
  1528. if (pqcrypto_req_control == NULL) {
  1529. pr_err("async request not found\n");
  1530. return;
  1531. }
  1532. #ifdef QCRYPTO_DEBUG
  1533. dev_info(&pengine->pdev->dev, "%s: %pK ret %d\n",
  1534. __func__, areq, ret);
  1535. #endif
  1536. if (digest) {
  1537. memcpy(rctx->digest, digest, diglen);
  1538. if (rctx->last_blk)
  1539. memcpy(areq->result, digest, diglen);
  1540. }
  1541. if (authdata) {
  1542. rctx->byte_count[0] = auth32[0];
  1543. rctx->byte_count[1] = auth32[1];
  1544. rctx->byte_count[2] = auth32[2];
  1545. rctx->byte_count[3] = auth32[3];
  1546. }
  1547. areq->src = rctx->src;
  1548. areq->nbytes = rctx->nbytes;
  1549. rctx->last_blk = 0;
  1550. rctx->first_blk = 0;
  1551. if (ret) {
  1552. pqcrypto_req_control->res = -ENXIO;
  1553. pstat->ahash_op_fail++;
  1554. } else {
  1555. pqcrypto_req_control->res = 0;
  1556. pstat->ahash_op_success++;
  1557. }
  1558. if (cp->ce_support.aligned_only) {
  1559. areq->src = rctx->orig_src;
  1560. kfree(rctx->data);
  1561. }
  1562. req_done(pqcrypto_req_control);
  1563. }
  1564. static void _qce_sk_cipher_complete(void *cookie, unsigned char *icb,
  1565. unsigned char *iv, int ret)
  1566. {
  1567. struct skcipher_request *areq = (struct skcipher_request *) cookie;
  1568. struct crypto_async_request *async_req;
  1569. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
  1570. struct qcrypto_cipher_ctx *ctx = crypto_skcipher_ctx(tfm);
  1571. struct crypto_priv *cp = ctx->cp;
  1572. struct crypto_stat *pstat;
  1573. struct qcrypto_cipher_req_ctx *rctx;
  1574. struct crypto_engine *pengine;
  1575. struct qcrypto_req_control *pqcrypto_req_control;
  1576. async_req = &areq->base;
  1577. pstat = &_qcrypto_stat;
  1578. rctx = skcipher_request_ctx(areq);
  1579. pengine = rctx->pengine;
  1580. pqcrypto_req_control = find_req_control_for_areq(pengine,
  1581. async_req);
  1582. if (pqcrypto_req_control == NULL) {
  1583. pr_err("async request not found\n");
  1584. return;
  1585. }
  1586. #ifdef QCRYPTO_DEBUG
  1587. dev_info(&pengine->pdev->dev, "%s: %pK ret %d\n",
  1588. __func__, areq, ret);
  1589. #endif
  1590. if (iv)
  1591. memcpy(ctx->iv, iv, crypto_skcipher_ivsize(tfm));
  1592. if (ret) {
  1593. pqcrypto_req_control->res = -ENXIO;
  1594. pstat->sk_cipher_op_fail++;
  1595. } else {
  1596. pqcrypto_req_control->res = 0;
  1597. pstat->sk_cipher_op_success++;
  1598. }
  1599. if (cp->ce_support.aligned_only) {
  1600. struct qcrypto_cipher_req_ctx *rctx;
  1601. uint32_t num_sg = 0;
  1602. uint32_t bytes = 0;
  1603. rctx = skcipher_request_ctx(areq);
  1604. areq->src = rctx->orig_src;
  1605. areq->dst = rctx->orig_dst;
  1606. num_sg = qcrypto_count_sg(areq->dst, areq->cryptlen);
  1607. bytes = qcrypto_sg_copy_from_buffer(areq->dst, num_sg,
  1608. rctx->data, areq->cryptlen);
  1609. if (bytes != areq->cryptlen)
  1610. pr_warn("bytes copied=0x%x bytes to copy= 0x%x\n",
  1611. bytes, areq->cryptlen);
  1612. kfree_sensitive(rctx->data);
  1613. }
  1614. req_done(pqcrypto_req_control);
  1615. }
  1616. static void _qce_aead_complete(void *cookie, unsigned char *icv,
  1617. unsigned char *iv, int ret)
  1618. {
  1619. struct aead_request *areq = (struct aead_request *) cookie;
  1620. struct crypto_async_request *async_req;
  1621. struct crypto_aead *aead = crypto_aead_reqtfm(areq);
  1622. struct qcrypto_cipher_ctx *ctx = crypto_tfm_ctx(areq->base.tfm);
  1623. struct qcrypto_cipher_req_ctx *rctx;
  1624. struct crypto_stat *pstat;
  1625. struct crypto_engine *pengine;
  1626. struct qcrypto_req_control *pqcrypto_req_control;
  1627. async_req = &areq->base;
  1628. pstat = &_qcrypto_stat;
  1629. rctx = aead_request_ctx(areq);
  1630. pengine = rctx->pengine;
  1631. pqcrypto_req_control = find_req_control_for_areq(pengine,
  1632. async_req);
  1633. if (pqcrypto_req_control == NULL) {
  1634. pr_err("async request not found\n");
  1635. return;
  1636. }
  1637. if (rctx->mode == QCE_MODE_CCM) {
  1638. kfree_sensitive(rctx->adata);
  1639. } else {
  1640. uint32_t ivsize = crypto_aead_ivsize(aead);
  1641. if (ret == 0) {
  1642. if (rctx->dir == QCE_ENCRYPT) {
  1643. /* copy the icv to dst */
  1644. scatterwalk_map_and_copy(icv, areq->dst,
  1645. areq->cryptlen + areq->assoclen,
  1646. ctx->authsize, 1);
  1647. } else {
  1648. unsigned char tmp[SHA256_DIGESTSIZE] = {0};
  1649. /* compare icv from src */
  1650. scatterwalk_map_and_copy(tmp,
  1651. areq->src, areq->assoclen +
  1652. areq->cryptlen - ctx->authsize,
  1653. ctx->authsize, 0);
  1654. ret = memcmp(icv, tmp, ctx->authsize);
  1655. if (ret != 0)
  1656. ret = -EBADMSG;
  1657. }
  1658. } else {
  1659. ret = -ENXIO;
  1660. }
  1661. if (iv)
  1662. memcpy(ctx->iv, iv, ivsize);
  1663. }
  1664. if (ret == (-EBADMSG))
  1665. pstat->aead_bad_msg++;
  1666. else if (ret)
  1667. pstat->aead_op_fail++;
  1668. else
  1669. pstat->aead_op_success++;
  1670. pqcrypto_req_control->res = ret;
  1671. req_done(pqcrypto_req_control);
  1672. }
  1673. static int aead_ccm_set_msg_len(u8 *block, unsigned int msglen, int csize)
  1674. {
  1675. __be32 data;
  1676. memset(block, 0, csize);
  1677. block += csize;
  1678. if (csize >= 4)
  1679. csize = 4;
  1680. else if (msglen > (1 << (8 * csize)))
  1681. return -EOVERFLOW;
  1682. data = cpu_to_be32(msglen);
  1683. memcpy(block - csize, (u8 *)&data + 4 - csize, csize);
  1684. return 0;
  1685. }
  1686. static int qccrypto_set_aead_ccm_nonce(struct qce_req *qreq, uint32_t assoclen)
  1687. {
  1688. unsigned int i = ((unsigned int)qreq->iv[0]) + 1;
  1689. memcpy(&qreq->nonce[0], qreq->iv, qreq->ivsize);
  1690. /*
  1691. * Format control info per RFC 3610 and
  1692. * NIST Special Publication 800-38C
  1693. */
  1694. qreq->nonce[0] |= (8 * ((qreq->authsize - 2) / 2));
  1695. if (assoclen)
  1696. qreq->nonce[0] |= 64;
  1697. if (i > MAX_NONCE)
  1698. return -EINVAL;
  1699. return aead_ccm_set_msg_len(qreq->nonce + 16 - i, qreq->cryptlen, i);
  1700. }
  1701. static int qcrypto_aead_ccm_format_adata(struct qce_req *qreq, uint32_t alen,
  1702. struct scatterlist *sg, unsigned char *adata)
  1703. {
  1704. uint32_t len;
  1705. uint32_t bytes = 0;
  1706. uint32_t num_sg = 0;
  1707. /*
  1708. * Add control info for associated data
  1709. * RFC 3610 and NIST Special Publication 800-38C
  1710. */
  1711. if (alen < 65280) {
  1712. *(__be16 *)adata = cpu_to_be16(alen);
  1713. len = 2;
  1714. } else {
  1715. if ((alen >= 65280) && (alen <= 0xffffffff)) {
  1716. *(__be16 *)adata = cpu_to_be16(0xfffe);
  1717. *(__be32 *)&adata[2] = cpu_to_be32(alen);
  1718. len = 6;
  1719. } else {
  1720. *(__be16 *)adata = cpu_to_be16(0xffff);
  1721. *(__be32 *)&adata[6] = cpu_to_be32(alen);
  1722. len = 10;
  1723. }
  1724. }
  1725. adata += len;
  1726. qreq->assoclen = ALIGN((alen + len), 16);
  1727. num_sg = qcrypto_count_sg(sg, alen);
  1728. bytes = qcrypto_sg_copy_to_buffer(sg, num_sg, adata, alen);
  1729. if (bytes != alen)
  1730. pr_warn("bytes copied=0x%x bytes to copy= 0x%x\n", bytes, alen);
  1731. return 0;
  1732. }
  1733. static int _qcrypto_process_skcipher(struct crypto_engine *pengine,
  1734. struct qcrypto_req_control *pqcrypto_req_control)
  1735. {
  1736. struct crypto_async_request *async_req;
  1737. struct qce_req qreq;
  1738. int ret;
  1739. struct qcrypto_cipher_req_ctx *rctx;
  1740. struct qcrypto_cipher_ctx *cipher_ctx;
  1741. struct skcipher_request *req;
  1742. struct crypto_skcipher *tfm;
  1743. async_req = pqcrypto_req_control->req;
  1744. req = container_of(async_req, struct skcipher_request, base);
  1745. cipher_ctx = crypto_tfm_ctx(async_req->tfm);
  1746. rctx = skcipher_request_ctx(req);
  1747. rctx->pengine = pengine;
  1748. tfm = crypto_skcipher_reqtfm(req);
  1749. if (pengine->pcp->ce_support.aligned_only) {
  1750. uint32_t bytes = 0;
  1751. uint32_t num_sg = 0;
  1752. rctx->orig_src = req->src;
  1753. rctx->orig_dst = req->dst;
  1754. rctx->data = kzalloc((req->cryptlen + 64), GFP_ATOMIC);
  1755. if (rctx->data == NULL)
  1756. return -ENOMEM;
  1757. num_sg = qcrypto_count_sg(req->src, req->cryptlen);
  1758. bytes = qcrypto_sg_copy_to_buffer(req->src, num_sg, rctx->data,
  1759. req->cryptlen);
  1760. if (bytes != req->cryptlen)
  1761. pr_warn("bytes copied=0x%x bytes to copy= 0x%x\n",
  1762. bytes, req->cryptlen);
  1763. sg_set_buf(&rctx->dsg, rctx->data, req->cryptlen);
  1764. sg_mark_end(&rctx->dsg);
  1765. rctx->iv = req->iv;
  1766. req->src = &rctx->dsg;
  1767. req->dst = &rctx->dsg;
  1768. }
  1769. qreq.op = QCE_REQ_ABLK_CIPHER; //TODO: change name in qcedev.h
  1770. qreq.qce_cb = _qce_sk_cipher_complete;
  1771. qreq.areq = req;
  1772. qreq.alg = rctx->alg;
  1773. qreq.dir = rctx->dir;
  1774. qreq.mode = rctx->mode;
  1775. qreq.enckey = cipher_ctx->enc_key;
  1776. qreq.encklen = cipher_ctx->enc_key_len;
  1777. qreq.iv = req->iv;
  1778. qreq.ivsize = crypto_skcipher_ivsize(tfm);
  1779. qreq.cryptlen = req->cryptlen;
  1780. qreq.use_pmem = 0;
  1781. qreq.flags = cipher_ctx->flags;
  1782. if ((cipher_ctx->enc_key_len == 0) &&
  1783. (pengine->pcp->platform_support.hw_key_support == 0))
  1784. ret = -EINVAL;
  1785. else
  1786. ret = qce_ablk_cipher_req(pengine->qce, &qreq); //maybe change name?
  1787. return ret;
  1788. }
  1789. static int _qcrypto_process_ahash(struct crypto_engine *pengine,
  1790. struct qcrypto_req_control *pqcrypto_req_control)
  1791. {
  1792. struct crypto_async_request *async_req;
  1793. struct ahash_request *req;
  1794. struct qce_sha_req sreq;
  1795. struct qcrypto_sha_req_ctx *rctx;
  1796. struct qcrypto_sha_ctx *sha_ctx;
  1797. int ret = 0;
  1798. async_req = pqcrypto_req_control->req;
  1799. req = container_of(async_req,
  1800. struct ahash_request, base);
  1801. rctx = ahash_request_ctx(req);
  1802. sha_ctx = crypto_tfm_ctx(async_req->tfm);
  1803. rctx->pengine = pengine;
  1804. sreq.qce_cb = _qce_ahash_complete;
  1805. sreq.digest = &rctx->digest[0];
  1806. sreq.src = req->src;
  1807. sreq.auth_data[0] = rctx->byte_count[0];
  1808. sreq.auth_data[1] = rctx->byte_count[1];
  1809. sreq.auth_data[2] = rctx->byte_count[2];
  1810. sreq.auth_data[3] = rctx->byte_count[3];
  1811. sreq.first_blk = rctx->first_blk;
  1812. sreq.last_blk = rctx->last_blk;
  1813. sreq.size = req->nbytes;
  1814. sreq.areq = req;
  1815. sreq.flags = sha_ctx->flags;
  1816. switch (sha_ctx->alg) {
  1817. case QCE_HASH_SHA1:
  1818. sreq.alg = QCE_HASH_SHA1;
  1819. sreq.authkey = NULL;
  1820. break;
  1821. case QCE_HASH_SHA256:
  1822. sreq.alg = QCE_HASH_SHA256;
  1823. sreq.authkey = NULL;
  1824. break;
  1825. case QCE_HASH_SHA1_HMAC:
  1826. sreq.alg = QCE_HASH_SHA1_HMAC;
  1827. sreq.authkey = &sha_ctx->authkey[0];
  1828. sreq.authklen = SHA_HMAC_KEY_SIZE;
  1829. break;
  1830. case QCE_HASH_SHA256_HMAC:
  1831. sreq.alg = QCE_HASH_SHA256_HMAC;
  1832. sreq.authkey = &sha_ctx->authkey[0];
  1833. sreq.authklen = SHA_HMAC_KEY_SIZE;
  1834. break;
  1835. default:
  1836. pr_err("Algorithm %d not supported, exiting\n", sha_ctx->alg);
  1837. ret = -1;
  1838. break;
  1839. }
  1840. ret = qce_process_sha_req(pengine->qce, &sreq);
  1841. return ret;
  1842. }
  1843. static int _qcrypto_process_aead(struct crypto_engine *pengine,
  1844. struct qcrypto_req_control *pqcrypto_req_control)
  1845. {
  1846. struct crypto_async_request *async_req;
  1847. struct qce_req qreq;
  1848. int ret = 0;
  1849. struct qcrypto_cipher_req_ctx *rctx;
  1850. struct qcrypto_cipher_ctx *cipher_ctx;
  1851. struct aead_request *req;
  1852. struct crypto_aead *aead;
  1853. async_req = pqcrypto_req_control->req;
  1854. req = container_of(async_req, struct aead_request, base);
  1855. aead = crypto_aead_reqtfm(req);
  1856. rctx = aead_request_ctx(req);
  1857. rctx->pengine = pengine;
  1858. cipher_ctx = crypto_tfm_ctx(async_req->tfm);
  1859. qreq.op = QCE_REQ_AEAD;
  1860. qreq.qce_cb = _qce_aead_complete;
  1861. qreq.areq = req;
  1862. qreq.alg = rctx->alg;
  1863. qreq.dir = rctx->dir;
  1864. qreq.mode = rctx->mode;
  1865. qreq.iv = rctx->iv;
  1866. qreq.enckey = cipher_ctx->enc_key;
  1867. qreq.encklen = cipher_ctx->enc_key_len;
  1868. qreq.authkey = cipher_ctx->auth_key;
  1869. qreq.authklen = cipher_ctx->auth_key_len;
  1870. qreq.authsize = crypto_aead_authsize(aead);
  1871. qreq.auth_alg = cipher_ctx->auth_alg;
  1872. if (qreq.mode == QCE_MODE_CCM)
  1873. qreq.ivsize = AES_BLOCK_SIZE;
  1874. else
  1875. qreq.ivsize = crypto_aead_ivsize(aead);
  1876. qreq.flags = cipher_ctx->flags;
  1877. if (qreq.mode == QCE_MODE_CCM) {
  1878. uint32_t assoclen;
  1879. if (qreq.dir == QCE_ENCRYPT)
  1880. qreq.cryptlen = req->cryptlen;
  1881. else
  1882. qreq.cryptlen = req->cryptlen -
  1883. qreq.authsize;
  1884. /* if rfc4309 ccm, adjust assoclen */
  1885. assoclen = req->assoclen;
  1886. if (rctx->ccmtype)
  1887. assoclen -= 8;
  1888. /* Get NONCE */
  1889. ret = qccrypto_set_aead_ccm_nonce(&qreq, assoclen);
  1890. if (ret)
  1891. return ret;
  1892. if (assoclen) {
  1893. rctx->adata = kzalloc((assoclen + 0x64),
  1894. GFP_ATOMIC);
  1895. if (!rctx->adata)
  1896. return -ENOMEM;
  1897. /* Format Associated data */
  1898. ret = qcrypto_aead_ccm_format_adata(&qreq,
  1899. assoclen,
  1900. req->src,
  1901. rctx->adata);
  1902. } else {
  1903. qreq.assoclen = 0;
  1904. rctx->adata = NULL;
  1905. }
  1906. if (ret) {
  1907. kfree_sensitive(rctx->adata);
  1908. return ret;
  1909. }
  1910. /*
  1911. * update req with new formatted associated
  1912. * data info
  1913. */
  1914. qreq.asg = &rctx->asg;
  1915. if (rctx->adata)
  1916. sg_set_buf(qreq.asg, rctx->adata,
  1917. qreq.assoclen);
  1918. sg_mark_end(qreq.asg);
  1919. }
  1920. ret = qce_aead_req(pengine->qce, &qreq);
  1921. return ret;
  1922. }
  1923. static struct crypto_engine *_qcrypto_static_assign_engine(
  1924. struct crypto_priv *cp)
  1925. {
  1926. struct crypto_engine *pengine;
  1927. unsigned long flags;
  1928. spin_lock_irqsave(&cp->lock, flags);
  1929. if (cp->next_engine)
  1930. pengine = cp->next_engine;
  1931. else
  1932. pengine = list_first_entry(&cp->engine_list,
  1933. struct crypto_engine, elist);
  1934. if (list_is_last(&pengine->elist, &cp->engine_list))
  1935. cp->next_engine = list_first_entry(
  1936. &cp->engine_list, struct crypto_engine, elist);
  1937. else
  1938. cp->next_engine = list_next_entry(pengine, elist);
  1939. spin_unlock_irqrestore(&cp->lock, flags);
  1940. return pengine;
  1941. }
  1942. static int _start_qcrypto_process(struct crypto_priv *cp,
  1943. struct crypto_engine *pengine)
  1944. {
  1945. struct crypto_async_request *async_req = NULL;
  1946. struct crypto_async_request *backlog_eng = NULL;
  1947. struct crypto_async_request *backlog_cp = NULL;
  1948. unsigned long flags;
  1949. u32 type;
  1950. int ret = 0;
  1951. struct crypto_stat *pstat;
  1952. void *tfm_ctx;
  1953. struct qcrypto_cipher_req_ctx *cipher_rctx;
  1954. struct qcrypto_sha_req_ctx *ahash_rctx;
  1955. struct skcipher_request *skcipher_req;
  1956. struct ahash_request *ahash_req;
  1957. struct aead_request *aead_req;
  1958. struct qcrypto_resp_ctx *arsp;
  1959. struct qcrypto_req_control *pqcrypto_req_control;
  1960. unsigned int cpu = MAX_SMP_CPU;
  1961. if (READ_ONCE(cp->ce_req_proc_sts) == STOPPED)
  1962. return 0;
  1963. if (in_interrupt()) {
  1964. cpu = smp_processor_id();
  1965. if (cpu >= MAX_SMP_CPU)
  1966. cpu = MAX_SMP_CPU - 1;
  1967. } else
  1968. cpu = MAX_SMP_CPU;
  1969. pstat = &_qcrypto_stat;
  1970. again:
  1971. spin_lock_irqsave(&cp->lock, flags);
  1972. if (pengine->issue_req ||
  1973. atomic_read(&pengine->req_count) >= (pengine->max_req)) {
  1974. spin_unlock_irqrestore(&cp->lock, flags);
  1975. return 0;
  1976. }
  1977. backlog_eng = crypto_get_backlog(&pengine->req_queue);
  1978. /* make sure it is in high bandwidth state */
  1979. if (pengine->bw_state != BUS_HAS_BANDWIDTH) {
  1980. spin_unlock_irqrestore(&cp->lock, flags);
  1981. return 0;
  1982. }
  1983. /* try to get request from request queue of the engine first */
  1984. async_req = crypto_dequeue_request(&pengine->req_queue);
  1985. if (!async_req) {
  1986. /*
  1987. * if no request from the engine,
  1988. * try to get from request queue of driver
  1989. */
  1990. backlog_cp = crypto_get_backlog(&cp->req_queue);
  1991. async_req = crypto_dequeue_request(&cp->req_queue);
  1992. if (!async_req) {
  1993. spin_unlock_irqrestore(&cp->lock, flags);
  1994. return 0;
  1995. }
  1996. }
  1997. pqcrypto_req_control = qcrypto_alloc_req_control(pengine);
  1998. if (pqcrypto_req_control == NULL) {
  1999. pr_err("Allocation of request failed\n");
  2000. spin_unlock_irqrestore(&cp->lock, flags);
  2001. return 0;
  2002. }
  2003. /* add associated rsp entry to tfm response queue */
  2004. type = crypto_tfm_alg_type(async_req->tfm);
  2005. tfm_ctx = crypto_tfm_ctx(async_req->tfm);
  2006. switch (type) {
  2007. case CRYPTO_ALG_TYPE_AHASH:
  2008. ahash_req = container_of(async_req,
  2009. struct ahash_request, base);
  2010. ahash_rctx = ahash_request_ctx(ahash_req);
  2011. arsp = &ahash_rctx->rsp_entry;
  2012. list_add_tail(
  2013. &arsp->list,
  2014. &((struct qcrypto_sha_ctx *)tfm_ctx)
  2015. ->rsp_queue);
  2016. break;
  2017. case CRYPTO_ALG_TYPE_SKCIPHER:
  2018. skcipher_req = container_of(async_req,
  2019. struct skcipher_request, base);
  2020. cipher_rctx = skcipher_request_ctx(skcipher_req);
  2021. arsp = &cipher_rctx->rsp_entry;
  2022. list_add_tail(
  2023. &arsp->list,
  2024. &((struct qcrypto_cipher_ctx *)tfm_ctx)
  2025. ->rsp_queue);
  2026. break;
  2027. case CRYPTO_ALG_TYPE_AEAD:
  2028. default:
  2029. aead_req = container_of(async_req,
  2030. struct aead_request, base);
  2031. cipher_rctx = aead_request_ctx(aead_req);
  2032. arsp = &cipher_rctx->rsp_entry;
  2033. list_add_tail(
  2034. &arsp->list,
  2035. &((struct qcrypto_cipher_ctx *)tfm_ctx)
  2036. ->rsp_queue);
  2037. break;
  2038. }
  2039. arsp->res = -EINPROGRESS;
  2040. arsp->async_req = async_req;
  2041. pqcrypto_req_control->pce = pengine;
  2042. pqcrypto_req_control->req = async_req;
  2043. pqcrypto_req_control->arsp = arsp;
  2044. pengine->active_seq++;
  2045. pengine->check_flag = true;
  2046. pengine->issue_req = true;
  2047. cp->cpu_req[cpu]++;
  2048. smp_mb(); /* make it visible */
  2049. spin_unlock_irqrestore(&cp->lock, flags);
  2050. if (backlog_eng)
  2051. backlog_eng->complete(backlog_eng, -EINPROGRESS);
  2052. if (backlog_cp)
  2053. backlog_cp->complete(backlog_cp, -EINPROGRESS);
  2054. switch (type) {
  2055. case CRYPTO_ALG_TYPE_SKCIPHER:
  2056. ret = _qcrypto_process_skcipher(pengine, pqcrypto_req_control);
  2057. break;
  2058. case CRYPTO_ALG_TYPE_AHASH:
  2059. ret = _qcrypto_process_ahash(pengine, pqcrypto_req_control);
  2060. break;
  2061. case CRYPTO_ALG_TYPE_AEAD:
  2062. ret = _qcrypto_process_aead(pengine, pqcrypto_req_control);
  2063. break;
  2064. default:
  2065. ret = -EINVAL;
  2066. }
  2067. pengine->issue_req = false;
  2068. smp_mb(); /* make it visible */
  2069. pengine->total_req++;
  2070. if (ret) {
  2071. pengine->err_req++;
  2072. qcrypto_free_req_control(pengine, pqcrypto_req_control);
  2073. if (type == CRYPTO_ALG_TYPE_SKCIPHER)
  2074. pstat->sk_cipher_op_fail++;
  2075. else
  2076. if (type == CRYPTO_ALG_TYPE_AHASH)
  2077. pstat->ahash_op_fail++;
  2078. else
  2079. pstat->aead_op_fail++;
  2080. _qcrypto_tfm_complete(pengine, type, tfm_ctx, arsp, ret);
  2081. goto again;
  2082. }
  2083. return ret;
  2084. }
  2085. static inline struct crypto_engine *_next_eng(struct crypto_priv *cp,
  2086. struct crypto_engine *p)
  2087. {
  2088. if (p == NULL || list_is_last(&p->elist, &cp->engine_list))
  2089. p = list_first_entry(&cp->engine_list, struct crypto_engine,
  2090. elist);
  2091. else
  2092. p = list_entry(p->elist.next, struct crypto_engine, elist);
  2093. return p;
  2094. }
  2095. static struct crypto_engine *_avail_eng(struct crypto_priv *cp)
  2096. {
  2097. /* call this function with spinlock set */
  2098. struct crypto_engine *q = NULL;
  2099. struct crypto_engine *p = cp->scheduled_eng;
  2100. struct crypto_engine *q1;
  2101. int eng_cnt = cp->total_units;
  2102. if (unlikely(list_empty(&cp->engine_list))) {
  2103. pr_err("%s: no valid ce to schedule\n", __func__);
  2104. return NULL;
  2105. }
  2106. p = _next_eng(cp, p);
  2107. q1 = p;
  2108. while (eng_cnt-- > 0) {
  2109. if (!p->issue_req && atomic_read(&p->req_count) < p->max_req) {
  2110. q = p;
  2111. break;
  2112. }
  2113. p = _next_eng(cp, p);
  2114. if (q1 == p)
  2115. break;
  2116. }
  2117. cp->scheduled_eng = q;
  2118. return q;
  2119. }
  2120. static int _qcrypto_queue_req(struct crypto_priv *cp,
  2121. struct crypto_engine *pengine,
  2122. struct crypto_async_request *req)
  2123. {
  2124. int ret;
  2125. unsigned long flags;
  2126. spin_lock_irqsave(&cp->lock, flags);
  2127. if (pengine) {
  2128. ret = crypto_enqueue_request(&pengine->req_queue, req);
  2129. } else {
  2130. ret = crypto_enqueue_request(&cp->req_queue, req);
  2131. pengine = _avail_eng(cp);
  2132. if (cp->req_queue.qlen > cp->max_qlen)
  2133. cp->max_qlen = cp->req_queue.qlen;
  2134. }
  2135. if (pengine) {
  2136. switch (pengine->bw_state) {
  2137. case BUS_NO_BANDWIDTH:
  2138. if (!pengine->high_bw_req) {
  2139. qcrypto_ce_bw_allocate_req(pengine);
  2140. pengine->high_bw_req = true;
  2141. }
  2142. pengine = NULL;
  2143. break;
  2144. case BUS_HAS_BANDWIDTH:
  2145. break;
  2146. case BUS_BANDWIDTH_RELEASING:
  2147. pengine->high_bw_req = true;
  2148. pengine = NULL;
  2149. break;
  2150. case BUS_BANDWIDTH_ALLOCATING:
  2151. pengine = NULL;
  2152. break;
  2153. case BUS_SUSPENDED:
  2154. case BUS_SUSPENDING:
  2155. default:
  2156. pengine = NULL;
  2157. break;
  2158. }
  2159. } else {
  2160. cp->no_avail++;
  2161. }
  2162. spin_unlock_irqrestore(&cp->lock, flags);
  2163. if (pengine && (READ_ONCE(cp->ce_req_proc_sts) == IN_PROGRESS))
  2164. _start_qcrypto_process(cp, pengine);
  2165. return ret;
  2166. }
  2167. static int _qcrypto_enc_aes_192_fallback(struct skcipher_request *req)
  2168. {
  2169. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  2170. struct qcrypto_cipher_ctx *ctx = crypto_skcipher_ctx(tfm);
  2171. int err;
  2172. SYNC_SKCIPHER_REQUEST_ON_STACK(subreq, ctx->cipher_aes192_fb);
  2173. skcipher_request_set_sync_tfm(subreq, ctx->cipher_aes192_fb);
  2174. skcipher_request_set_callback(subreq, req->base.flags, NULL, NULL);
  2175. skcipher_request_set_crypt(subreq, req->src, req->dst,
  2176. req->cryptlen, req->iv);
  2177. err = crypto_skcipher_encrypt(subreq);
  2178. skcipher_request_zero(subreq);
  2179. return err;
  2180. }
  2181. static int _qcrypto_dec_aes_192_fallback(struct skcipher_request *req)
  2182. {
  2183. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  2184. struct qcrypto_cipher_ctx *ctx = crypto_skcipher_ctx(tfm);
  2185. int err;
  2186. SYNC_SKCIPHER_REQUEST_ON_STACK(subreq, ctx->cipher_aes192_fb);
  2187. skcipher_request_set_sync_tfm(subreq, ctx->cipher_aes192_fb);
  2188. skcipher_request_set_callback(subreq, req->base.flags, NULL, NULL);
  2189. skcipher_request_set_crypt(subreq, req->src, req->dst,
  2190. req->cryptlen, req->iv);
  2191. err = crypto_skcipher_decrypt(subreq);
  2192. skcipher_request_zero(subreq);
  2193. return err;
  2194. }
  2195. static int _qcrypto_enc_aes_ecb(struct skcipher_request *req)
  2196. {
  2197. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  2198. struct qcrypto_cipher_ctx *ctx = crypto_skcipher_ctx(tfm);
  2199. struct qcrypto_cipher_req_ctx *rctx;
  2200. struct crypto_priv *cp = ctx->cp;
  2201. struct crypto_stat *pstat = &_qcrypto_stat;
  2202. WARN_ON(crypto_tfm_alg_type(&tfm->base) != CRYPTO_ALG_TYPE_SKCIPHER);
  2203. #ifdef QCRYPTO_DEBUG
  2204. dev_info(&ctx->pengine->pdev->dev, "%s: %pK\n", __func__, req);
  2205. #endif
  2206. if ((ctx->enc_key_len == AES_KEYSIZE_192) &&
  2207. (!cp->ce_support.aes_key_192) &&
  2208. ctx->cipher_aes192_fb)
  2209. return _qcrypto_enc_aes_192_fallback(req);
  2210. rctx = skcipher_request_ctx(req);
  2211. rctx->aead = 0;
  2212. rctx->alg = CIPHER_ALG_AES;
  2213. rctx->dir = QCE_ENCRYPT;
  2214. rctx->mode = QCE_MODE_ECB;
  2215. pstat->sk_cipher_aes_enc++;
  2216. return _qcrypto_queue_req(cp, ctx->pengine, &req->base);
  2217. }
  2218. static int _qcrypto_enc_aes_cbc(struct skcipher_request *req)
  2219. {
  2220. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  2221. struct qcrypto_cipher_ctx *ctx = crypto_skcipher_ctx(tfm);
  2222. struct qcrypto_cipher_req_ctx *rctx;
  2223. struct crypto_priv *cp = ctx->cp;
  2224. struct crypto_stat *pstat = &_qcrypto_stat;
  2225. WARN_ON(crypto_tfm_alg_type(&tfm->base) != CRYPTO_ALG_TYPE_SKCIPHER);
  2226. #ifdef QCRYPTO_DEBUG
  2227. dev_info(&ctx->pengine->pdev->dev, "%s: %pK\n", __func__, req);
  2228. #endif
  2229. if ((ctx->enc_key_len == AES_KEYSIZE_192) &&
  2230. (!cp->ce_support.aes_key_192) &&
  2231. ctx->cipher_aes192_fb)
  2232. return _qcrypto_enc_aes_192_fallback(req);
  2233. rctx = skcipher_request_ctx(req);
  2234. rctx->aead = 0;
  2235. rctx->alg = CIPHER_ALG_AES;
  2236. rctx->dir = QCE_ENCRYPT;
  2237. rctx->mode = QCE_MODE_CBC;
  2238. pstat->sk_cipher_aes_enc++;
  2239. return _qcrypto_queue_req(cp, ctx->pengine, &req->base);
  2240. }
  2241. static int _qcrypto_enc_aes_ctr(struct skcipher_request *req)
  2242. {
  2243. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  2244. struct qcrypto_cipher_ctx *ctx = crypto_skcipher_ctx(tfm);
  2245. struct qcrypto_cipher_req_ctx *rctx;
  2246. struct crypto_priv *cp = ctx->cp;
  2247. struct crypto_stat *pstat = &_qcrypto_stat;
  2248. WARN_ON(crypto_tfm_alg_type(&tfm->base) != CRYPTO_ALG_TYPE_SKCIPHER);
  2249. #ifdef QCRYPTO_DEBUG
  2250. dev_info(&ctx->pengine->pdev->dev, "%s: %pK\n", __func__, req);
  2251. #endif
  2252. if ((ctx->enc_key_len == AES_KEYSIZE_192) &&
  2253. (!cp->ce_support.aes_key_192) &&
  2254. ctx->cipher_aes192_fb)
  2255. return _qcrypto_enc_aes_192_fallback(req);
  2256. rctx = skcipher_request_ctx(req);
  2257. rctx->aead = 0;
  2258. rctx->alg = CIPHER_ALG_AES;
  2259. rctx->dir = QCE_ENCRYPT;
  2260. rctx->mode = QCE_MODE_CTR;
  2261. pstat->sk_cipher_aes_enc++;
  2262. return _qcrypto_queue_req(cp, ctx->pengine, &req->base);
  2263. }
  2264. static int _qcrypto_enc_aes_xts(struct skcipher_request *req)
  2265. {
  2266. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  2267. struct qcrypto_cipher_ctx *ctx = crypto_skcipher_ctx(tfm);
  2268. struct qcrypto_cipher_req_ctx *rctx;
  2269. struct crypto_stat *pstat = &_qcrypto_stat;
  2270. struct crypto_priv *cp = ctx->cp;
  2271. WARN_ON(crypto_tfm_alg_type(&tfm->base) != CRYPTO_ALG_TYPE_SKCIPHER);
  2272. rctx = skcipher_request_ctx(req);
  2273. rctx->aead = 0;
  2274. rctx->alg = CIPHER_ALG_AES;
  2275. rctx->dir = QCE_ENCRYPT;
  2276. rctx->mode = QCE_MODE_XTS;
  2277. pstat->sk_cipher_aes_enc++;
  2278. return _qcrypto_queue_req(cp, ctx->pengine, &req->base);
  2279. }
  2280. static int _qcrypto_aead_encrypt_aes_ccm(struct aead_request *req)
  2281. {
  2282. struct qcrypto_cipher_req_ctx *rctx;
  2283. struct qcrypto_cipher_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  2284. struct crypto_priv *cp = ctx->cp;
  2285. struct crypto_stat *pstat;
  2286. if ((ctx->authsize > 16) || (ctx->authsize < 4) || (ctx->authsize & 1))
  2287. return -EINVAL;
  2288. if ((ctx->auth_key_len != AES_KEYSIZE_128) &&
  2289. (ctx->auth_key_len != AES_KEYSIZE_256))
  2290. return -EINVAL;
  2291. pstat = &_qcrypto_stat;
  2292. rctx = aead_request_ctx(req);
  2293. rctx->aead = 1;
  2294. rctx->alg = CIPHER_ALG_AES;
  2295. rctx->dir = QCE_ENCRYPT;
  2296. rctx->mode = QCE_MODE_CCM;
  2297. rctx->iv = req->iv;
  2298. rctx->ccmtype = 0;
  2299. pstat->aead_ccm_aes_enc++;
  2300. return _qcrypto_queue_req(cp, ctx->pengine, &req->base);
  2301. }
  2302. static int _qcrypto_aead_rfc4309_enc_aes_ccm(struct aead_request *req)
  2303. {
  2304. struct qcrypto_cipher_req_ctx *rctx;
  2305. struct qcrypto_cipher_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  2306. struct crypto_priv *cp = ctx->cp;
  2307. struct crypto_stat *pstat;
  2308. pstat = &_qcrypto_stat;
  2309. if (req->assoclen != 16 && req->assoclen != 20)
  2310. return -EINVAL;
  2311. rctx = aead_request_ctx(req);
  2312. rctx->aead = 1;
  2313. rctx->alg = CIPHER_ALG_AES;
  2314. rctx->dir = QCE_ENCRYPT;
  2315. rctx->mode = QCE_MODE_CCM;
  2316. memset(rctx->rfc4309_iv, 0, sizeof(rctx->rfc4309_iv));
  2317. rctx->rfc4309_iv[0] = 3; /* L -1 */
  2318. memcpy(&rctx->rfc4309_iv[1], ctx->ccm4309_nonce, 3);
  2319. memcpy(&rctx->rfc4309_iv[4], req->iv, 8);
  2320. rctx->ccmtype = 1;
  2321. rctx->iv = rctx->rfc4309_iv;
  2322. pstat->aead_rfc4309_ccm_aes_enc++;
  2323. return _qcrypto_queue_req(cp, ctx->pengine, &req->base);
  2324. }
  2325. static int _qcrypto_enc_des_ecb(struct skcipher_request *req)
  2326. {
  2327. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  2328. struct qcrypto_cipher_ctx *ctx = crypto_skcipher_ctx(tfm);
  2329. struct qcrypto_cipher_req_ctx *rctx;
  2330. struct crypto_priv *cp = ctx->cp;
  2331. struct crypto_stat *pstat = &_qcrypto_stat;
  2332. WARN_ON(crypto_tfm_alg_type(&tfm->base) != CRYPTO_ALG_TYPE_SKCIPHER);
  2333. rctx = skcipher_request_ctx(req);
  2334. rctx->aead = 0;
  2335. rctx->alg = CIPHER_ALG_DES;
  2336. rctx->dir = QCE_ENCRYPT;
  2337. rctx->mode = QCE_MODE_ECB;
  2338. pstat->sk_cipher_des_enc++;
  2339. return _qcrypto_queue_req(cp, ctx->pengine, &req->base);
  2340. }
  2341. static int _qcrypto_enc_des_cbc(struct skcipher_request *req)
  2342. {
  2343. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  2344. struct qcrypto_cipher_ctx *ctx = crypto_skcipher_ctx(tfm);
  2345. struct qcrypto_cipher_req_ctx *rctx;
  2346. struct crypto_priv *cp = ctx->cp;
  2347. struct crypto_stat *pstat = &_qcrypto_stat;
  2348. WARN_ON(crypto_tfm_alg_type(&tfm->base) != CRYPTO_ALG_TYPE_SKCIPHER);
  2349. rctx = skcipher_request_ctx(req);
  2350. rctx->aead = 0;
  2351. rctx->alg = CIPHER_ALG_DES;
  2352. rctx->dir = QCE_ENCRYPT;
  2353. rctx->mode = QCE_MODE_CBC;
  2354. pstat->sk_cipher_des_enc++;
  2355. return _qcrypto_queue_req(cp, ctx->pengine, &req->base);
  2356. }
  2357. static int _qcrypto_enc_3des_ecb(struct skcipher_request *req)
  2358. {
  2359. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  2360. struct qcrypto_cipher_ctx *ctx = crypto_skcipher_ctx(tfm);
  2361. struct qcrypto_cipher_req_ctx *rctx;
  2362. struct crypto_priv *cp = ctx->cp;
  2363. struct crypto_stat *pstat = &_qcrypto_stat;
  2364. WARN_ON(crypto_tfm_alg_type(&tfm->base) != CRYPTO_ALG_TYPE_SKCIPHER);
  2365. rctx = skcipher_request_ctx(req);
  2366. rctx->aead = 0;
  2367. rctx->alg = CIPHER_ALG_3DES;
  2368. rctx->dir = QCE_ENCRYPT;
  2369. rctx->mode = QCE_MODE_ECB;
  2370. pstat->sk_cipher_3des_enc++;
  2371. return _qcrypto_queue_req(cp, ctx->pengine, &req->base);
  2372. }
  2373. static int _qcrypto_enc_3des_cbc(struct skcipher_request *req)
  2374. {
  2375. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  2376. struct qcrypto_cipher_ctx *ctx = crypto_skcipher_ctx(tfm);
  2377. struct qcrypto_cipher_req_ctx *rctx;
  2378. struct crypto_priv *cp = ctx->cp;
  2379. struct crypto_stat *pstat = &_qcrypto_stat;
  2380. WARN_ON(crypto_tfm_alg_type(&tfm->base) != CRYPTO_ALG_TYPE_SKCIPHER);
  2381. rctx = skcipher_request_ctx(req);
  2382. rctx->aead = 0;
  2383. rctx->alg = CIPHER_ALG_3DES;
  2384. rctx->dir = QCE_ENCRYPT;
  2385. rctx->mode = QCE_MODE_CBC;
  2386. pstat->sk_cipher_3des_enc++;
  2387. return _qcrypto_queue_req(cp, ctx->pengine, &req->base);
  2388. }
  2389. static int _qcrypto_dec_aes_ecb(struct skcipher_request *req)
  2390. {
  2391. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  2392. struct qcrypto_cipher_ctx *ctx = crypto_skcipher_ctx(tfm);
  2393. struct qcrypto_cipher_req_ctx *rctx;
  2394. struct crypto_priv *cp = ctx->cp;
  2395. struct crypto_stat *pstat = &_qcrypto_stat;
  2396. WARN_ON(crypto_tfm_alg_type(&tfm->base) != CRYPTO_ALG_TYPE_SKCIPHER);
  2397. #ifdef QCRYPTO_DEBUG
  2398. dev_info(&ctx->pengine->pdev->dev, "%s: %pK\n", __func__, req);
  2399. #endif
  2400. if ((ctx->enc_key_len == AES_KEYSIZE_192) &&
  2401. (!cp->ce_support.aes_key_192) &&
  2402. ctx->cipher_aes192_fb)
  2403. return _qcrypto_dec_aes_192_fallback(req);
  2404. rctx = skcipher_request_ctx(req);
  2405. rctx->aead = 0;
  2406. rctx->alg = CIPHER_ALG_AES;
  2407. rctx->dir = QCE_DECRYPT;
  2408. rctx->mode = QCE_MODE_ECB;
  2409. pstat->sk_cipher_aes_dec++;
  2410. return _qcrypto_queue_req(cp, ctx->pengine, &req->base);
  2411. }
  2412. static int _qcrypto_dec_aes_cbc(struct skcipher_request *req)
  2413. {
  2414. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  2415. struct qcrypto_cipher_ctx *ctx = crypto_skcipher_ctx(tfm);
  2416. struct qcrypto_cipher_req_ctx *rctx;
  2417. struct crypto_priv *cp = ctx->cp;
  2418. struct crypto_stat *pstat = &_qcrypto_stat;
  2419. WARN_ON(crypto_tfm_alg_type(&tfm->base) != CRYPTO_ALG_TYPE_SKCIPHER);
  2420. #ifdef QCRYPTO_DEBUG
  2421. dev_info(&ctx->pengine->pdev->dev, "%s: %pK\n", __func__, req);
  2422. #endif
  2423. if ((ctx->enc_key_len == AES_KEYSIZE_192) &&
  2424. (!cp->ce_support.aes_key_192) &&
  2425. ctx->cipher_aes192_fb)
  2426. return _qcrypto_dec_aes_192_fallback(req);
  2427. rctx = skcipher_request_ctx(req);
  2428. rctx->aead = 0;
  2429. rctx->alg = CIPHER_ALG_AES;
  2430. rctx->dir = QCE_DECRYPT;
  2431. rctx->mode = QCE_MODE_CBC;
  2432. pstat->sk_cipher_aes_dec++;
  2433. return _qcrypto_queue_req(cp, ctx->pengine, &req->base);
  2434. }
  2435. static int _qcrypto_dec_aes_ctr(struct skcipher_request *req)
  2436. {
  2437. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  2438. struct qcrypto_cipher_ctx *ctx = crypto_skcipher_ctx(tfm);
  2439. struct qcrypto_cipher_req_ctx *rctx;
  2440. struct crypto_priv *cp = ctx->cp;
  2441. struct crypto_stat *pstat = &_qcrypto_stat;
  2442. WARN_ON(crypto_tfm_alg_type(&tfm->base) != CRYPTO_ALG_TYPE_SKCIPHER);
  2443. #ifdef QCRYPTO_DEBUG
  2444. dev_info(&ctx->pengine->pdev->dev, "%s: %pK\n", __func__, req);
  2445. #endif
  2446. if ((ctx->enc_key_len == AES_KEYSIZE_192) &&
  2447. (!cp->ce_support.aes_key_192) &&
  2448. ctx->cipher_aes192_fb)
  2449. return _qcrypto_dec_aes_192_fallback(req);
  2450. rctx = skcipher_request_ctx(req);
  2451. rctx->aead = 0;
  2452. rctx->alg = CIPHER_ALG_AES;
  2453. rctx->mode = QCE_MODE_CTR;
  2454. /* Note. There is no such thing as aes/counter mode, decrypt */
  2455. rctx->dir = QCE_ENCRYPT;
  2456. pstat->sk_cipher_aes_dec++;
  2457. return _qcrypto_queue_req(cp, ctx->pengine, &req->base);
  2458. }
  2459. static int _qcrypto_dec_des_ecb(struct skcipher_request *req)
  2460. {
  2461. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  2462. struct qcrypto_cipher_ctx *ctx = crypto_skcipher_ctx(tfm);
  2463. struct qcrypto_cipher_req_ctx *rctx;
  2464. struct crypto_priv *cp = ctx->cp;
  2465. struct crypto_stat *pstat = &_qcrypto_stat;
  2466. WARN_ON(crypto_tfm_alg_type(&tfm->base) != CRYPTO_ALG_TYPE_SKCIPHER);
  2467. rctx = skcipher_request_ctx(req);
  2468. rctx->aead = 0;
  2469. rctx->alg = CIPHER_ALG_DES;
  2470. rctx->dir = QCE_DECRYPT;
  2471. rctx->mode = QCE_MODE_ECB;
  2472. pstat->sk_cipher_des_dec++;
  2473. return _qcrypto_queue_req(cp, ctx->pengine, &req->base);
  2474. }
  2475. static int _qcrypto_dec_des_cbc(struct skcipher_request *req)
  2476. {
  2477. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  2478. struct qcrypto_cipher_ctx *ctx = crypto_skcipher_ctx(tfm);
  2479. struct qcrypto_cipher_req_ctx *rctx;
  2480. struct crypto_priv *cp = ctx->cp;
  2481. struct crypto_stat *pstat = &_qcrypto_stat;
  2482. WARN_ON(crypto_tfm_alg_type(&tfm->base) != CRYPTO_ALG_TYPE_SKCIPHER);
  2483. rctx = skcipher_request_ctx(req);
  2484. rctx->aead = 0;
  2485. rctx->alg = CIPHER_ALG_DES;
  2486. rctx->dir = QCE_DECRYPT;
  2487. rctx->mode = QCE_MODE_CBC;
  2488. pstat->sk_cipher_des_dec++;
  2489. return _qcrypto_queue_req(cp, ctx->pengine, &req->base);
  2490. }
  2491. static int _qcrypto_dec_3des_ecb(struct skcipher_request *req)
  2492. {
  2493. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  2494. struct qcrypto_cipher_ctx *ctx = crypto_skcipher_ctx(tfm);
  2495. struct qcrypto_cipher_req_ctx *rctx;
  2496. struct crypto_priv *cp = ctx->cp;
  2497. struct crypto_stat *pstat = &_qcrypto_stat;
  2498. WARN_ON(crypto_tfm_alg_type(&tfm->base) != CRYPTO_ALG_TYPE_SKCIPHER);
  2499. rctx = skcipher_request_ctx(req);
  2500. rctx->aead = 0;
  2501. rctx->alg = CIPHER_ALG_3DES;
  2502. rctx->dir = QCE_DECRYPT;
  2503. rctx->mode = QCE_MODE_ECB;
  2504. pstat->sk_cipher_3des_dec++;
  2505. return _qcrypto_queue_req(cp, ctx->pengine, &req->base);
  2506. }
  2507. static int _qcrypto_dec_3des_cbc(struct skcipher_request *req)
  2508. {
  2509. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  2510. struct qcrypto_cipher_ctx *ctx = crypto_skcipher_ctx(tfm);
  2511. struct qcrypto_cipher_req_ctx *rctx;
  2512. struct crypto_priv *cp = ctx->cp;
  2513. struct crypto_stat *pstat = &_qcrypto_stat;
  2514. WARN_ON(crypto_tfm_alg_type(&tfm->base) != CRYPTO_ALG_TYPE_SKCIPHER);
  2515. rctx = skcipher_request_ctx(req);
  2516. rctx->aead = 0;
  2517. rctx->alg = CIPHER_ALG_3DES;
  2518. rctx->dir = QCE_DECRYPT;
  2519. rctx->mode = QCE_MODE_CBC;
  2520. pstat->sk_cipher_3des_dec++;
  2521. return _qcrypto_queue_req(cp, ctx->pengine, &req->base);
  2522. }
  2523. static int _qcrypto_dec_aes_xts(struct skcipher_request *req)
  2524. {
  2525. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  2526. struct qcrypto_cipher_ctx *ctx = crypto_skcipher_ctx(tfm);
  2527. struct qcrypto_cipher_req_ctx *rctx;
  2528. struct crypto_priv *cp = ctx->cp;
  2529. struct crypto_stat *pstat = &_qcrypto_stat;
  2530. WARN_ON(crypto_tfm_alg_type(&tfm->base) != CRYPTO_ALG_TYPE_SKCIPHER);
  2531. rctx = skcipher_request_ctx(req);
  2532. rctx->aead = 0;
  2533. rctx->alg = CIPHER_ALG_AES;
  2534. rctx->mode = QCE_MODE_XTS;
  2535. rctx->dir = QCE_DECRYPT;
  2536. pstat->sk_cipher_aes_dec++;
  2537. return _qcrypto_queue_req(cp, ctx->pengine, &req->base);
  2538. }
  2539. static int _qcrypto_aead_decrypt_aes_ccm(struct aead_request *req)
  2540. {
  2541. struct qcrypto_cipher_req_ctx *rctx;
  2542. struct qcrypto_cipher_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  2543. struct crypto_priv *cp = ctx->cp;
  2544. struct crypto_stat *pstat;
  2545. if ((ctx->authsize > 16) || (ctx->authsize < 4) || (ctx->authsize & 1))
  2546. return -EINVAL;
  2547. if ((ctx->auth_key_len != AES_KEYSIZE_128) &&
  2548. (ctx->auth_key_len != AES_KEYSIZE_256))
  2549. return -EINVAL;
  2550. pstat = &_qcrypto_stat;
  2551. rctx = aead_request_ctx(req);
  2552. rctx->aead = 1;
  2553. rctx->alg = CIPHER_ALG_AES;
  2554. rctx->dir = QCE_DECRYPT;
  2555. rctx->mode = QCE_MODE_CCM;
  2556. rctx->iv = req->iv;
  2557. rctx->ccmtype = 0;
  2558. pstat->aead_ccm_aes_dec++;
  2559. return _qcrypto_queue_req(cp, ctx->pengine, &req->base);
  2560. }
  2561. static int _qcrypto_aead_rfc4309_dec_aes_ccm(struct aead_request *req)
  2562. {
  2563. struct qcrypto_cipher_req_ctx *rctx;
  2564. struct qcrypto_cipher_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  2565. struct crypto_priv *cp = ctx->cp;
  2566. struct crypto_stat *pstat;
  2567. pstat = &_qcrypto_stat;
  2568. if (req->assoclen != 16 && req->assoclen != 20)
  2569. return -EINVAL;
  2570. rctx = aead_request_ctx(req);
  2571. rctx->aead = 1;
  2572. rctx->alg = CIPHER_ALG_AES;
  2573. rctx->dir = QCE_DECRYPT;
  2574. rctx->mode = QCE_MODE_CCM;
  2575. memset(rctx->rfc4309_iv, 0, sizeof(rctx->rfc4309_iv));
  2576. rctx->rfc4309_iv[0] = 3; /* L -1 */
  2577. memcpy(&rctx->rfc4309_iv[1], ctx->ccm4309_nonce, 3);
  2578. memcpy(&rctx->rfc4309_iv[4], req->iv, 8);
  2579. rctx->ccmtype = 1;
  2580. rctx->iv = rctx->rfc4309_iv;
  2581. pstat->aead_rfc4309_ccm_aes_dec++;
  2582. return _qcrypto_queue_req(cp, ctx->pengine, &req->base);
  2583. }
  2584. static int _qcrypto_aead_setauthsize(struct crypto_aead *authenc,
  2585. unsigned int authsize)
  2586. {
  2587. struct qcrypto_cipher_ctx *ctx = crypto_aead_ctx(authenc);
  2588. ctx->authsize = authsize;
  2589. return 0;
  2590. }
  2591. static int _qcrypto_aead_ccm_setauthsize(struct crypto_aead *authenc,
  2592. unsigned int authsize)
  2593. {
  2594. struct qcrypto_cipher_ctx *ctx = crypto_aead_ctx(authenc);
  2595. switch (authsize) {
  2596. case 4:
  2597. case 6:
  2598. case 8:
  2599. case 10:
  2600. case 12:
  2601. case 14:
  2602. case 16:
  2603. break;
  2604. default:
  2605. return -EINVAL;
  2606. }
  2607. ctx->authsize = authsize;
  2608. return 0;
  2609. }
  2610. static int _qcrypto_aead_rfc4309_ccm_setauthsize(struct crypto_aead *authenc,
  2611. unsigned int authsize)
  2612. {
  2613. struct qcrypto_cipher_ctx *ctx = crypto_aead_ctx(authenc);
  2614. switch (authsize) {
  2615. case 8:
  2616. case 12:
  2617. case 16:
  2618. break;
  2619. default:
  2620. return -EINVAL;
  2621. }
  2622. ctx->authsize = authsize;
  2623. return 0;
  2624. }
  2625. static int _qcrypto_aead_setkey(struct crypto_aead *tfm, const u8 *key,
  2626. unsigned int keylen)
  2627. {
  2628. struct qcrypto_cipher_ctx *ctx = crypto_aead_ctx(tfm);
  2629. struct rtattr *rta = (struct rtattr *)key;
  2630. struct crypto_authenc_key_param *param;
  2631. int ret;
  2632. if (!RTA_OK(rta, keylen))
  2633. goto badkey;
  2634. if (rta->rta_type != CRYPTO_AUTHENC_KEYA_PARAM)
  2635. goto badkey;
  2636. if (RTA_PAYLOAD(rta) < sizeof(*param))
  2637. goto badkey;
  2638. param = RTA_DATA(rta);
  2639. ctx->enc_key_len = be32_to_cpu(param->enckeylen);
  2640. key += RTA_ALIGN(rta->rta_len);
  2641. keylen -= RTA_ALIGN(rta->rta_len);
  2642. if (keylen < ctx->enc_key_len)
  2643. goto badkey;
  2644. ctx->auth_key_len = keylen - ctx->enc_key_len;
  2645. if (ctx->enc_key_len >= QCRYPTO_MAX_KEY_SIZE ||
  2646. ctx->auth_key_len >= QCRYPTO_MAX_KEY_SIZE)
  2647. goto badkey;
  2648. memset(ctx->auth_key, 0, QCRYPTO_MAX_KEY_SIZE);
  2649. memcpy(ctx->enc_key, key + ctx->auth_key_len, ctx->enc_key_len);
  2650. memcpy(ctx->auth_key, key, ctx->auth_key_len);
  2651. if (ctx->enc_key_len == AES_KEYSIZE_192 && ctx->cipher_aes192_fb &&
  2652. ctx->ahash_aead_aes192_fb) {
  2653. crypto_ahash_clear_flags(ctx->ahash_aead_aes192_fb, ~0);
  2654. ret = crypto_ahash_setkey(ctx->ahash_aead_aes192_fb,
  2655. ctx->auth_key, ctx->auth_key_len);
  2656. if (ret)
  2657. goto badkey;
  2658. crypto_sync_skcipher_clear_flags(ctx->cipher_aes192_fb, ~0);
  2659. ret = crypto_sync_skcipher_setkey(ctx->cipher_aes192_fb,
  2660. ctx->enc_key, ctx->enc_key_len);
  2661. if (ret)
  2662. goto badkey;
  2663. }
  2664. return 0;
  2665. badkey:
  2666. ctx->enc_key_len = 0;
  2667. //crypto_aead_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
  2668. return -EINVAL;
  2669. }
  2670. static int _qcrypto_aead_ccm_setkey(struct crypto_aead *aead, const u8 *key,
  2671. unsigned int keylen)
  2672. {
  2673. struct crypto_tfm *tfm = crypto_aead_tfm(aead);
  2674. struct qcrypto_cipher_ctx *ctx = crypto_tfm_ctx(tfm);
  2675. struct crypto_priv *cp = ctx->cp;
  2676. switch (keylen) {
  2677. case AES_KEYSIZE_128:
  2678. case AES_KEYSIZE_256:
  2679. break;
  2680. case AES_KEYSIZE_192:
  2681. if (cp->ce_support.aes_key_192) {
  2682. break;
  2683. }
  2684. else {
  2685. ctx->enc_key_len = 0;
  2686. return -EINVAL;
  2687. }
  2688. default:
  2689. ctx->enc_key_len = 0;
  2690. //crypto_aead_set_flags(aead, CRYPTO_TFM_RES_BAD_KEY_LEN);
  2691. return -EINVAL;
  2692. }
  2693. ctx->enc_key_len = keylen;
  2694. memcpy(ctx->enc_key, key, keylen);
  2695. ctx->auth_key_len = keylen;
  2696. memcpy(ctx->auth_key, key, keylen);
  2697. return 0;
  2698. }
  2699. static int _qcrypto_aead_rfc4309_ccm_setkey(struct crypto_aead *aead,
  2700. const u8 *key, unsigned int key_len)
  2701. {
  2702. struct crypto_tfm *tfm = crypto_aead_tfm(aead);
  2703. struct qcrypto_cipher_ctx *ctx = crypto_tfm_ctx(tfm);
  2704. int ret;
  2705. if (key_len < QCRYPTO_CCM4309_NONCE_LEN)
  2706. return -EINVAL;
  2707. key_len -= QCRYPTO_CCM4309_NONCE_LEN;
  2708. memcpy(ctx->ccm4309_nonce, key + key_len, QCRYPTO_CCM4309_NONCE_LEN);
  2709. ret = _qcrypto_aead_ccm_setkey(aead, key, key_len);
  2710. return ret;
  2711. }
  2712. static void _qcrypto_aead_aes_192_fb_a_cb(struct qcrypto_cipher_req_ctx *rctx,
  2713. int res)
  2714. {
  2715. struct aead_request *req;
  2716. struct crypto_async_request *areq;
  2717. req = rctx->aead_req;
  2718. areq = &req->base;
  2719. if (rctx->fb_aes_req)
  2720. skcipher_request_free(rctx->fb_aes_req);
  2721. if (rctx->fb_hash_req)
  2722. ahash_request_free(rctx->fb_hash_req);
  2723. rctx->fb_aes_req = NULL;
  2724. rctx->fb_hash_req = NULL;
  2725. kfree(rctx->fb_aes_iv);
  2726. areq->complete(areq, res);
  2727. }
  2728. static void _aead_aes_fb_stage2_ahash_complete(
  2729. struct crypto_async_request *base, int err)
  2730. {
  2731. struct qcrypto_cipher_req_ctx *rctx;
  2732. struct aead_request *req;
  2733. struct qcrypto_cipher_ctx *ctx;
  2734. rctx = base->data;
  2735. req = rctx->aead_req;
  2736. ctx = crypto_tfm_ctx(req->base.tfm);
  2737. /* copy icv */
  2738. if (err == 0)
  2739. scatterwalk_map_and_copy(rctx->fb_ahash_digest,
  2740. rctx->fb_aes_dst,
  2741. req->cryptlen,
  2742. ctx->authsize, 1);
  2743. _qcrypto_aead_aes_192_fb_a_cb(rctx, err);
  2744. }
  2745. static int _start_aead_aes_fb_stage2_hmac(struct qcrypto_cipher_req_ctx *rctx)
  2746. {
  2747. struct ahash_request *ahash_req;
  2748. ahash_req = rctx->fb_hash_req;
  2749. ahash_request_set_callback(ahash_req, CRYPTO_TFM_REQ_MAY_BACKLOG,
  2750. _aead_aes_fb_stage2_ahash_complete, rctx);
  2751. return crypto_ahash_digest(ahash_req);
  2752. }
  2753. static void _aead_aes_fb_stage2_decrypt_complete(
  2754. struct crypto_async_request *base, int err)
  2755. {
  2756. struct qcrypto_cipher_req_ctx *rctx;
  2757. rctx = base->data;
  2758. _qcrypto_aead_aes_192_fb_a_cb(rctx, err);
  2759. }
  2760. static int _start_aead_aes_fb_stage2_decrypt(
  2761. struct qcrypto_cipher_req_ctx *rctx)
  2762. {
  2763. struct skcipher_request *aes_req;
  2764. aes_req = rctx->fb_aes_req;
  2765. skcipher_request_set_callback(aes_req, CRYPTO_TFM_REQ_MAY_BACKLOG,
  2766. _aead_aes_fb_stage2_decrypt_complete, rctx);
  2767. return crypto_skcipher_decrypt(aes_req);
  2768. }
  2769. static void _aead_aes_fb_stage1_ahash_complete(
  2770. struct crypto_async_request *base, int err)
  2771. {
  2772. struct qcrypto_cipher_req_ctx *rctx;
  2773. struct aead_request *req;
  2774. struct qcrypto_cipher_ctx *ctx;
  2775. rctx = base->data;
  2776. req = rctx->aead_req;
  2777. ctx = crypto_tfm_ctx(req->base.tfm);
  2778. /* compare icv */
  2779. if (err == 0) {
  2780. unsigned char *tmp;
  2781. tmp = kmalloc(ctx->authsize, GFP_KERNEL);
  2782. if (!tmp) {
  2783. err = -ENOMEM;
  2784. goto ret;
  2785. }
  2786. scatterwalk_map_and_copy(tmp, rctx->fb_aes_src,
  2787. req->cryptlen - ctx->authsize, ctx->authsize, 0);
  2788. if (memcmp(rctx->fb_ahash_digest, tmp, ctx->authsize) != 0)
  2789. err = -EBADMSG;
  2790. kfree(tmp);
  2791. }
  2792. ret:
  2793. if (err)
  2794. _qcrypto_aead_aes_192_fb_a_cb(rctx, err);
  2795. else {
  2796. err = _start_aead_aes_fb_stage2_decrypt(rctx);
  2797. if (err != -EINPROGRESS && err != -EBUSY)
  2798. _qcrypto_aead_aes_192_fb_a_cb(rctx, err);
  2799. }
  2800. }
  2801. static void _aead_aes_fb_stage1_encrypt_complete(
  2802. struct crypto_async_request *base, int err)
  2803. {
  2804. struct qcrypto_cipher_req_ctx *rctx;
  2805. struct aead_request *req;
  2806. struct qcrypto_cipher_ctx *ctx;
  2807. rctx = base->data;
  2808. req = rctx->aead_req;
  2809. ctx = crypto_tfm_ctx(req->base.tfm);
  2810. memcpy(ctx->iv, rctx->fb_aes_iv, rctx->ivsize);
  2811. if (err) {
  2812. _qcrypto_aead_aes_192_fb_a_cb(rctx, err);
  2813. return;
  2814. }
  2815. err = _start_aead_aes_fb_stage2_hmac(rctx);
  2816. /* copy icv */
  2817. if (err == 0) {
  2818. scatterwalk_map_and_copy(rctx->fb_ahash_digest,
  2819. rctx->fb_aes_dst,
  2820. req->cryptlen,
  2821. ctx->authsize, 1);
  2822. }
  2823. if (err != -EINPROGRESS && err != -EBUSY)
  2824. _qcrypto_aead_aes_192_fb_a_cb(rctx, err);
  2825. }
  2826. static int _qcrypto_aead_aes_192_fallback(struct aead_request *req,
  2827. bool is_encrypt)
  2828. {
  2829. int rc = -EINVAL;
  2830. struct qcrypto_cipher_req_ctx *rctx = aead_request_ctx(req);
  2831. struct qcrypto_cipher_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  2832. struct crypto_aead *aead_tfm = crypto_aead_reqtfm(req);
  2833. struct skcipher_request *aes_req = NULL;
  2834. struct ahash_request *ahash_req = NULL;
  2835. int nbytes;
  2836. struct scatterlist *src, *dst;
  2837. rctx->fb_aes_iv = NULL;
  2838. aes_req = skcipher_request_alloc(&ctx->cipher_aes192_fb->base,
  2839. GFP_KERNEL);
  2840. if (!aes_req)
  2841. return -ENOMEM;
  2842. ahash_req = ahash_request_alloc(ctx->ahash_aead_aes192_fb, GFP_KERNEL);
  2843. if (!ahash_req)
  2844. goto ret;
  2845. rctx->fb_aes_req = aes_req;
  2846. rctx->fb_hash_req = ahash_req;
  2847. rctx->aead_req = req;
  2848. /* assoc and iv are sitting in the beginning of src sg list */
  2849. /* Similarly, assoc and iv are sitting in the beginning of dst list */
  2850. src = scatterwalk_ffwd(rctx->fb_ablkcipher_src_sg, req->src,
  2851. req->assoclen);
  2852. dst = scatterwalk_ffwd(rctx->fb_ablkcipher_dst_sg, req->dst,
  2853. req->assoclen);
  2854. nbytes = req->cryptlen;
  2855. if (!is_encrypt)
  2856. nbytes -= ctx->authsize;
  2857. rctx->fb_ahash_length = nbytes + req->assoclen;
  2858. rctx->fb_aes_src = src;
  2859. rctx->fb_aes_dst = dst;
  2860. rctx->fb_aes_cryptlen = nbytes;
  2861. rctx->ivsize = crypto_aead_ivsize(aead_tfm);
  2862. rctx->fb_aes_iv = kmemdup(req->iv, rctx->ivsize, GFP_ATOMIC);
  2863. if (!rctx->fb_aes_iv)
  2864. goto ret;
  2865. skcipher_request_set_crypt(aes_req, rctx->fb_aes_src,
  2866. rctx->fb_aes_dst,
  2867. rctx->fb_aes_cryptlen, rctx->fb_aes_iv);
  2868. if (is_encrypt)
  2869. ahash_request_set_crypt(ahash_req, req->dst,
  2870. rctx->fb_ahash_digest,
  2871. rctx->fb_ahash_length);
  2872. else
  2873. ahash_request_set_crypt(ahash_req, req->src,
  2874. rctx->fb_ahash_digest,
  2875. rctx->fb_ahash_length);
  2876. if (is_encrypt) {
  2877. skcipher_request_set_callback(aes_req,
  2878. CRYPTO_TFM_REQ_MAY_BACKLOG,
  2879. _aead_aes_fb_stage1_encrypt_complete, rctx);
  2880. rc = crypto_skcipher_encrypt(aes_req);
  2881. if (rc == 0) {
  2882. memcpy(ctx->iv, rctx->fb_aes_iv, rctx->ivsize);
  2883. rc = _start_aead_aes_fb_stage2_hmac(rctx);
  2884. if (rc == 0) {
  2885. /* copy icv */
  2886. scatterwalk_map_and_copy(rctx->fb_ahash_digest,
  2887. dst,
  2888. req->cryptlen,
  2889. ctx->authsize, 1);
  2890. }
  2891. }
  2892. if (rc == -EINPROGRESS || rc == -EBUSY)
  2893. return rc;
  2894. goto ret;
  2895. } else {
  2896. ahash_request_set_callback(ahash_req,
  2897. CRYPTO_TFM_REQ_MAY_BACKLOG,
  2898. _aead_aes_fb_stage1_ahash_complete, rctx);
  2899. rc = crypto_ahash_digest(ahash_req);
  2900. if (rc == 0) {
  2901. unsigned char *tmp;
  2902. tmp = kmalloc(ctx->authsize, GFP_KERNEL);
  2903. if (!tmp) {
  2904. rc = -ENOMEM;
  2905. goto ret;
  2906. }
  2907. /* compare icv */
  2908. scatterwalk_map_and_copy(tmp,
  2909. src, req->cryptlen - ctx->authsize,
  2910. ctx->authsize, 0);
  2911. if (memcmp(rctx->fb_ahash_digest, tmp,
  2912. ctx->authsize) != 0)
  2913. rc = -EBADMSG;
  2914. else
  2915. rc = _start_aead_aes_fb_stage2_decrypt(rctx);
  2916. kfree(tmp);
  2917. }
  2918. if (rc == -EINPROGRESS || rc == -EBUSY)
  2919. return rc;
  2920. goto ret;
  2921. }
  2922. ret:
  2923. if (aes_req)
  2924. skcipher_request_free(aes_req);
  2925. if (ahash_req)
  2926. ahash_request_free(ahash_req);
  2927. kfree(rctx->fb_aes_iv);
  2928. return rc;
  2929. }
  2930. static int _qcrypto_aead_encrypt_aes_cbc(struct aead_request *req)
  2931. {
  2932. struct qcrypto_cipher_req_ctx *rctx;
  2933. struct qcrypto_cipher_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  2934. struct crypto_priv *cp = ctx->cp;
  2935. struct crypto_stat *pstat;
  2936. pstat = &_qcrypto_stat;
  2937. #ifdef QCRYPTO_DEBUG
  2938. dev_info(&ctx->pengine->pdev->dev, "%s: %pK\n", __func__, req);
  2939. #endif
  2940. rctx = aead_request_ctx(req);
  2941. rctx->aead = 1;
  2942. rctx->alg = CIPHER_ALG_AES;
  2943. rctx->dir = QCE_ENCRYPT;
  2944. rctx->mode = QCE_MODE_CBC;
  2945. rctx->iv = req->iv;
  2946. rctx->aead_req = req;
  2947. if (ctx->auth_alg == QCE_HASH_SHA1_HMAC)
  2948. pstat->aead_sha1_aes_enc++;
  2949. else
  2950. pstat->aead_sha256_aes_enc++;
  2951. if (ctx->enc_key_len == AES_KEYSIZE_192 && ctx->cipher_aes192_fb &&
  2952. ctx->ahash_aead_aes192_fb)
  2953. return _qcrypto_aead_aes_192_fallback(req, true);
  2954. return _qcrypto_queue_req(cp, ctx->pengine, &req->base);
  2955. }
  2956. static int _qcrypto_aead_decrypt_aes_cbc(struct aead_request *req)
  2957. {
  2958. struct qcrypto_cipher_req_ctx *rctx;
  2959. struct qcrypto_cipher_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  2960. struct crypto_priv *cp = ctx->cp;
  2961. struct crypto_stat *pstat;
  2962. pstat = &_qcrypto_stat;
  2963. #ifdef QCRYPTO_DEBUG
  2964. dev_info(&ctx->pengine->pdev->dev, "%s: %pK\n", __func__, req);
  2965. #endif
  2966. rctx = aead_request_ctx(req);
  2967. rctx->aead = 1;
  2968. rctx->alg = CIPHER_ALG_AES;
  2969. rctx->dir = QCE_DECRYPT;
  2970. rctx->mode = QCE_MODE_CBC;
  2971. rctx->iv = req->iv;
  2972. rctx->aead_req = req;
  2973. if (ctx->auth_alg == QCE_HASH_SHA1_HMAC)
  2974. pstat->aead_sha1_aes_dec++;
  2975. else
  2976. pstat->aead_sha256_aes_dec++;
  2977. if (ctx->enc_key_len == AES_KEYSIZE_192 && ctx->cipher_aes192_fb &&
  2978. ctx->ahash_aead_aes192_fb)
  2979. return _qcrypto_aead_aes_192_fallback(req, false);
  2980. return _qcrypto_queue_req(cp, ctx->pengine, &req->base);
  2981. }
  2982. static int _qcrypto_aead_encrypt_des_cbc(struct aead_request *req)
  2983. {
  2984. struct qcrypto_cipher_req_ctx *rctx;
  2985. struct qcrypto_cipher_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  2986. struct crypto_priv *cp = ctx->cp;
  2987. struct crypto_stat *pstat;
  2988. pstat = &_qcrypto_stat;
  2989. rctx = aead_request_ctx(req);
  2990. rctx->aead = 1;
  2991. rctx->alg = CIPHER_ALG_DES;
  2992. rctx->dir = QCE_ENCRYPT;
  2993. rctx->mode = QCE_MODE_CBC;
  2994. rctx->iv = req->iv;
  2995. if (ctx->auth_alg == QCE_HASH_SHA1_HMAC)
  2996. pstat->aead_sha1_des_enc++;
  2997. else
  2998. pstat->aead_sha256_des_enc++;
  2999. return _qcrypto_queue_req(cp, ctx->pengine, &req->base);
  3000. }
  3001. static int _qcrypto_aead_decrypt_des_cbc(struct aead_request *req)
  3002. {
  3003. struct qcrypto_cipher_req_ctx *rctx;
  3004. struct qcrypto_cipher_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  3005. struct crypto_priv *cp = ctx->cp;
  3006. struct crypto_stat *pstat;
  3007. pstat = &_qcrypto_stat;
  3008. rctx = aead_request_ctx(req);
  3009. rctx->aead = 1;
  3010. rctx->alg = CIPHER_ALG_DES;
  3011. rctx->dir = QCE_DECRYPT;
  3012. rctx->mode = QCE_MODE_CBC;
  3013. rctx->iv = req->iv;
  3014. if (ctx->auth_alg == QCE_HASH_SHA1_HMAC)
  3015. pstat->aead_sha1_des_dec++;
  3016. else
  3017. pstat->aead_sha256_des_dec++;
  3018. return _qcrypto_queue_req(cp, ctx->pengine, &req->base);
  3019. }
  3020. static int _qcrypto_aead_encrypt_3des_cbc(struct aead_request *req)
  3021. {
  3022. struct qcrypto_cipher_req_ctx *rctx;
  3023. struct qcrypto_cipher_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  3024. struct crypto_priv *cp = ctx->cp;
  3025. struct crypto_stat *pstat;
  3026. pstat = &_qcrypto_stat;
  3027. rctx = aead_request_ctx(req);
  3028. rctx->aead = 1;
  3029. rctx->alg = CIPHER_ALG_3DES;
  3030. rctx->dir = QCE_ENCRYPT;
  3031. rctx->mode = QCE_MODE_CBC;
  3032. rctx->iv = req->iv;
  3033. if (ctx->auth_alg == QCE_HASH_SHA1_HMAC)
  3034. pstat->aead_sha1_3des_enc++;
  3035. else
  3036. pstat->aead_sha256_3des_enc++;
  3037. return _qcrypto_queue_req(cp, ctx->pengine, &req->base);
  3038. }
  3039. static int _qcrypto_aead_decrypt_3des_cbc(struct aead_request *req)
  3040. {
  3041. struct qcrypto_cipher_req_ctx *rctx;
  3042. struct qcrypto_cipher_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  3043. struct crypto_priv *cp = ctx->cp;
  3044. struct crypto_stat *pstat;
  3045. pstat = &_qcrypto_stat;
  3046. rctx = aead_request_ctx(req);
  3047. rctx->aead = 1;
  3048. rctx->alg = CIPHER_ALG_3DES;
  3049. rctx->dir = QCE_DECRYPT;
  3050. rctx->mode = QCE_MODE_CBC;
  3051. rctx->iv = req->iv;
  3052. if (ctx->auth_alg == QCE_HASH_SHA1_HMAC)
  3053. pstat->aead_sha1_3des_dec++;
  3054. else
  3055. pstat->aead_sha256_3des_dec++;
  3056. return _qcrypto_queue_req(cp, ctx->pengine, &req->base);
  3057. }
  3058. static int _sha_init(struct ahash_request *req)
  3059. {
  3060. struct qcrypto_sha_req_ctx *rctx = ahash_request_ctx(req);
  3061. rctx->first_blk = 1;
  3062. rctx->last_blk = 0;
  3063. rctx->byte_count[0] = 0;
  3064. rctx->byte_count[1] = 0;
  3065. rctx->byte_count[2] = 0;
  3066. rctx->byte_count[3] = 0;
  3067. rctx->trailing_buf_len = 0;
  3068. rctx->count = 0;
  3069. return 0;
  3070. }
  3071. static int _sha1_init(struct ahash_request *req)
  3072. {
  3073. struct qcrypto_sha_ctx *sha_ctx = crypto_tfm_ctx(req->base.tfm);
  3074. struct crypto_stat *pstat;
  3075. struct qcrypto_sha_req_ctx *rctx = ahash_request_ctx(req);
  3076. pstat = &_qcrypto_stat;
  3077. _sha_init(req);
  3078. sha_ctx->alg = QCE_HASH_SHA1;
  3079. memset(&rctx->trailing_buf[0], 0x00, SHA1_BLOCK_SIZE);
  3080. memcpy(&rctx->digest[0], &_std_init_vector_sha1_uint8[0],
  3081. SHA1_DIGEST_SIZE);
  3082. sha_ctx->diglen = SHA1_DIGEST_SIZE;
  3083. pstat->sha1_digest++;
  3084. return 0;
  3085. }
  3086. static int _sha256_init(struct ahash_request *req)
  3087. {
  3088. struct qcrypto_sha_ctx *sha_ctx = crypto_tfm_ctx(req->base.tfm);
  3089. struct crypto_stat *pstat;
  3090. struct qcrypto_sha_req_ctx *rctx = ahash_request_ctx(req);
  3091. pstat = &_qcrypto_stat;
  3092. _sha_init(req);
  3093. sha_ctx->alg = QCE_HASH_SHA256;
  3094. memset(&rctx->trailing_buf[0], 0x00, SHA256_BLOCK_SIZE);
  3095. memcpy(&rctx->digest[0], &_std_init_vector_sha256_uint8[0],
  3096. SHA256_DIGEST_SIZE);
  3097. sha_ctx->diglen = SHA256_DIGEST_SIZE;
  3098. pstat->sha256_digest++;
  3099. return 0;
  3100. }
  3101. static int _sha1_export(struct ahash_request *req, void *out)
  3102. {
  3103. struct qcrypto_sha_req_ctx *rctx = ahash_request_ctx(req);
  3104. struct sha1_state *out_ctx = (struct sha1_state *)out;
  3105. out_ctx->count = rctx->count;
  3106. _byte_stream_to_words(out_ctx->state, rctx->digest, SHA1_DIGEST_SIZE);
  3107. memcpy(out_ctx->buffer, rctx->trailing_buf, SHA1_BLOCK_SIZE);
  3108. return 0;
  3109. }
  3110. static int _sha1_hmac_export(struct ahash_request *req, void *out)
  3111. {
  3112. return _sha1_export(req, out);
  3113. }
  3114. /* crypto hw padding constant for hmac first operation */
  3115. #define HMAC_PADDING 64
  3116. static int __sha1_import_common(struct ahash_request *req, const void *in,
  3117. bool hmac)
  3118. {
  3119. struct qcrypto_sha_ctx *sha_ctx = crypto_tfm_ctx(req->base.tfm);
  3120. struct qcrypto_sha_req_ctx *rctx = ahash_request_ctx(req);
  3121. struct sha1_state *in_ctx = (struct sha1_state *)in;
  3122. u64 hw_count = in_ctx->count;
  3123. rctx->count = in_ctx->count;
  3124. memcpy(rctx->trailing_buf, in_ctx->buffer, SHA1_BLOCK_SIZE);
  3125. if (in_ctx->count <= SHA1_BLOCK_SIZE) {
  3126. rctx->first_blk = 1;
  3127. } else {
  3128. rctx->first_blk = 0;
  3129. /*
  3130. * For hmac, there is a hardware padding done
  3131. * when first is set. So the byte_count will be
  3132. * incremened by 64 after the operstion of first
  3133. */
  3134. if (hmac)
  3135. hw_count += HMAC_PADDING;
  3136. }
  3137. rctx->byte_count[0] = (uint32_t)(hw_count & 0xFFFFFFC0);
  3138. rctx->byte_count[1] = (uint32_t)(hw_count >> 32);
  3139. _words_to_byte_stream(in_ctx->state, rctx->digest, sha_ctx->diglen);
  3140. rctx->trailing_buf_len = (uint32_t)(in_ctx->count &
  3141. (SHA1_BLOCK_SIZE-1));
  3142. return 0;
  3143. }
  3144. static int _sha1_import(struct ahash_request *req, const void *in)
  3145. {
  3146. return __sha1_import_common(req, in, false);
  3147. }
  3148. static int _sha1_hmac_import(struct ahash_request *req, const void *in)
  3149. {
  3150. return __sha1_import_common(req, in, true);
  3151. }
  3152. static int _sha256_export(struct ahash_request *req, void *out)
  3153. {
  3154. struct qcrypto_sha_req_ctx *rctx = ahash_request_ctx(req);
  3155. struct sha256_state *out_ctx = (struct sha256_state *)out;
  3156. out_ctx->count = rctx->count;
  3157. _byte_stream_to_words(out_ctx->state, rctx->digest, SHA256_DIGEST_SIZE);
  3158. memcpy(out_ctx->buf, rctx->trailing_buf, SHA256_BLOCK_SIZE);
  3159. return 0;
  3160. }
  3161. static int _sha256_hmac_export(struct ahash_request *req, void *out)
  3162. {
  3163. return _sha256_export(req, out);
  3164. }
  3165. static int __sha256_import_common(struct ahash_request *req, const void *in,
  3166. bool hmac)
  3167. {
  3168. struct qcrypto_sha_ctx *sha_ctx = crypto_tfm_ctx(req->base.tfm);
  3169. struct qcrypto_sha_req_ctx *rctx = ahash_request_ctx(req);
  3170. struct sha256_state *in_ctx = (struct sha256_state *)in;
  3171. u64 hw_count = in_ctx->count;
  3172. rctx->count = in_ctx->count;
  3173. memcpy(rctx->trailing_buf, in_ctx->buf, SHA256_BLOCK_SIZE);
  3174. if (in_ctx->count <= SHA256_BLOCK_SIZE) {
  3175. rctx->first_blk = 1;
  3176. } else {
  3177. rctx->first_blk = 0;
  3178. /*
  3179. * for hmac, there is a hardware padding done
  3180. * when first is set. So the byte_count will be
  3181. * incremened by 64 after the operstion of first
  3182. */
  3183. if (hmac)
  3184. hw_count += HMAC_PADDING;
  3185. }
  3186. rctx->byte_count[0] = (uint32_t)(hw_count & 0xFFFFFFC0);
  3187. rctx->byte_count[1] = (uint32_t)(hw_count >> 32);
  3188. _words_to_byte_stream(in_ctx->state, rctx->digest, sha_ctx->diglen);
  3189. rctx->trailing_buf_len = (uint32_t)(in_ctx->count &
  3190. (SHA256_BLOCK_SIZE-1));
  3191. return 0;
  3192. }
  3193. static int _sha256_import(struct ahash_request *req, const void *in)
  3194. {
  3195. return __sha256_import_common(req, in, false);
  3196. }
  3197. static int _sha256_hmac_import(struct ahash_request *req, const void *in)
  3198. {
  3199. return __sha256_import_common(req, in, true);
  3200. }
  3201. static int _copy_source(struct ahash_request *req)
  3202. {
  3203. struct qcrypto_sha_req_ctx *srctx = NULL;
  3204. uint32_t bytes = 0;
  3205. uint32_t num_sg = 0;
  3206. srctx = ahash_request_ctx(req);
  3207. srctx->orig_src = req->src;
  3208. srctx->data = kzalloc((req->nbytes + 64), GFP_ATOMIC);
  3209. if (srctx->data == NULL) {
  3210. pr_err("Mem Alloc fail rctx->data, err %ld for 0x%x\n",
  3211. PTR_ERR(srctx->data), (req->nbytes + 64));
  3212. return -ENOMEM;
  3213. }
  3214. num_sg = qcrypto_count_sg(req->src, req->nbytes);
  3215. bytes = qcrypto_sg_copy_to_buffer(req->src, num_sg, srctx->data,
  3216. req->nbytes);
  3217. if (bytes != req->nbytes)
  3218. pr_warn("bytes copied=0x%x bytes to copy= 0x%x\n", bytes,
  3219. req->nbytes);
  3220. sg_set_buf(&srctx->dsg, srctx->data,
  3221. req->nbytes);
  3222. sg_mark_end(&srctx->dsg);
  3223. req->src = &srctx->dsg;
  3224. return 0;
  3225. }
  3226. static int _sha_update(struct ahash_request *req, uint32_t sha_block_size)
  3227. {
  3228. struct qcrypto_sha_ctx *sha_ctx = crypto_tfm_ctx(req->base.tfm);
  3229. struct crypto_priv *cp = sha_ctx->cp;
  3230. struct qcrypto_sha_req_ctx *rctx = ahash_request_ctx(req);
  3231. uint32_t total, len, num_sg;
  3232. struct scatterlist *sg_last;
  3233. uint8_t *k_src = NULL;
  3234. uint32_t sha_pad_len = 0;
  3235. uint32_t trailing_buf_len = 0;
  3236. uint32_t nbytes;
  3237. uint32_t offset = 0;
  3238. uint32_t bytes = 0;
  3239. uint8_t *staging;
  3240. int ret = 0;
  3241. /* check for trailing buffer from previous updates and append it */
  3242. total = req->nbytes + rctx->trailing_buf_len;
  3243. len = req->nbytes;
  3244. if (total <= sha_block_size) {
  3245. k_src = &rctx->trailing_buf[rctx->trailing_buf_len];
  3246. num_sg = qcrypto_count_sg(req->src, len);
  3247. bytes = qcrypto_sg_copy_to_buffer(req->src, num_sg, k_src, len);
  3248. rctx->trailing_buf_len = total;
  3249. return 0;
  3250. }
  3251. /* save the original req structure fields*/
  3252. rctx->src = req->src;
  3253. rctx->nbytes = req->nbytes;
  3254. staging = (uint8_t *)ALIGN(((uintptr_t)rctx->staging_dmabuf),
  3255. L1_CACHE_BYTES);
  3256. memcpy(staging, rctx->trailing_buf, rctx->trailing_buf_len);
  3257. k_src = &rctx->trailing_buf[0];
  3258. /* get new trailing buffer */
  3259. sha_pad_len = ALIGN(total, sha_block_size) - total;
  3260. trailing_buf_len = sha_block_size - sha_pad_len;
  3261. offset = req->nbytes - trailing_buf_len;
  3262. if (offset != req->nbytes)
  3263. scatterwalk_map_and_copy(k_src, req->src, offset,
  3264. trailing_buf_len, 0);
  3265. nbytes = total - trailing_buf_len;
  3266. num_sg = qcrypto_count_sg(req->src, req->nbytes);
  3267. len = rctx->trailing_buf_len;
  3268. sg_last = req->src;
  3269. while (len < nbytes) {
  3270. if ((len + sg_last->length) > nbytes)
  3271. break;
  3272. len += sg_last->length;
  3273. sg_last = sg_next(sg_last);
  3274. }
  3275. if (rctx->trailing_buf_len) {
  3276. if (cp->ce_support.aligned_only) {
  3277. rctx->data2 = kzalloc((req->nbytes + 64), GFP_ATOMIC);
  3278. if (rctx->data2 == NULL)
  3279. return -ENOMEM;
  3280. memcpy(rctx->data2, staging,
  3281. rctx->trailing_buf_len);
  3282. memcpy((rctx->data2 + rctx->trailing_buf_len),
  3283. rctx->data, req->src->length);
  3284. kfree_sensitive(rctx->data);
  3285. rctx->data = rctx->data2;
  3286. sg_set_buf(&rctx->sg[0], rctx->data,
  3287. (rctx->trailing_buf_len +
  3288. req->src->length));
  3289. req->src = rctx->sg;
  3290. sg_mark_end(&rctx->sg[0]);
  3291. } else {
  3292. sg_mark_end(sg_last);
  3293. memset(rctx->sg, 0, sizeof(rctx->sg));
  3294. sg_set_buf(&rctx->sg[0], staging,
  3295. rctx->trailing_buf_len);
  3296. sg_mark_end(&rctx->sg[1]);
  3297. sg_chain(rctx->sg, 2, req->src);
  3298. req->src = rctx->sg;
  3299. }
  3300. } else
  3301. sg_mark_end(sg_last);
  3302. req->nbytes = nbytes;
  3303. rctx->trailing_buf_len = trailing_buf_len;
  3304. ret = _qcrypto_queue_req(cp, sha_ctx->pengine, &req->base);
  3305. return ret;
  3306. }
  3307. static int _sha1_update(struct ahash_request *req)
  3308. {
  3309. struct qcrypto_sha_req_ctx *rctx = ahash_request_ctx(req);
  3310. struct qcrypto_sha_ctx *sha_ctx = crypto_tfm_ctx(req->base.tfm);
  3311. struct crypto_priv *cp = sha_ctx->cp;
  3312. if (cp->ce_support.aligned_only) {
  3313. if (_copy_source(req))
  3314. return -ENOMEM;
  3315. }
  3316. rctx->count += req->nbytes;
  3317. return _sha_update(req, SHA1_BLOCK_SIZE);
  3318. }
  3319. static int _sha256_update(struct ahash_request *req)
  3320. {
  3321. struct qcrypto_sha_req_ctx *rctx = ahash_request_ctx(req);
  3322. struct qcrypto_sha_ctx *sha_ctx = crypto_tfm_ctx(req->base.tfm);
  3323. struct crypto_priv *cp = sha_ctx->cp;
  3324. if (cp->ce_support.aligned_only) {
  3325. if (_copy_source(req))
  3326. return -ENOMEM;
  3327. }
  3328. rctx->count += req->nbytes;
  3329. return _sha_update(req, SHA256_BLOCK_SIZE);
  3330. }
  3331. static int _sha_final(struct ahash_request *req, uint32_t sha_block_size)
  3332. {
  3333. struct qcrypto_sha_ctx *sha_ctx = crypto_tfm_ctx(req->base.tfm);
  3334. struct crypto_priv *cp = sha_ctx->cp;
  3335. struct qcrypto_sha_req_ctx *rctx = ahash_request_ctx(req);
  3336. int ret = 0;
  3337. uint8_t *staging;
  3338. if (cp->ce_support.aligned_only) {
  3339. if (_copy_source(req))
  3340. return -ENOMEM;
  3341. }
  3342. rctx->last_blk = 1;
  3343. /* save the original req structure fields*/
  3344. rctx->src = req->src;
  3345. rctx->nbytes = req->nbytes;
  3346. staging = (uint8_t *)ALIGN(((uintptr_t)rctx->staging_dmabuf),
  3347. L1_CACHE_BYTES);
  3348. memcpy(staging, rctx->trailing_buf, rctx->trailing_buf_len);
  3349. sg_set_buf(&rctx->sg[0], staging, rctx->trailing_buf_len);
  3350. sg_mark_end(&rctx->sg[0]);
  3351. req->src = &rctx->sg[0];
  3352. req->nbytes = rctx->trailing_buf_len;
  3353. ret = _qcrypto_queue_req(cp, sha_ctx->pengine, &req->base);
  3354. return ret;
  3355. }
  3356. static int _sha1_final(struct ahash_request *req)
  3357. {
  3358. return _sha_final(req, SHA1_BLOCK_SIZE);
  3359. }
  3360. static int _sha256_final(struct ahash_request *req)
  3361. {
  3362. return _sha_final(req, SHA256_BLOCK_SIZE);
  3363. }
  3364. static int _sha_digest(struct ahash_request *req)
  3365. {
  3366. struct qcrypto_sha_ctx *sha_ctx = crypto_tfm_ctx(req->base.tfm);
  3367. struct qcrypto_sha_req_ctx *rctx = ahash_request_ctx(req);
  3368. struct crypto_priv *cp = sha_ctx->cp;
  3369. int ret = 0;
  3370. if (cp->ce_support.aligned_only) {
  3371. if (_copy_source(req))
  3372. return -ENOMEM;
  3373. }
  3374. /* save the original req structure fields*/
  3375. rctx->src = req->src;
  3376. rctx->nbytes = req->nbytes;
  3377. rctx->first_blk = 1;
  3378. rctx->last_blk = 1;
  3379. ret = _qcrypto_queue_req(cp, sha_ctx->pengine, &req->base);
  3380. return ret;
  3381. }
  3382. static int _sha1_digest(struct ahash_request *req)
  3383. {
  3384. _sha1_init(req);
  3385. return _sha_digest(req);
  3386. }
  3387. static int _sha256_digest(struct ahash_request *req)
  3388. {
  3389. _sha256_init(req);
  3390. return _sha_digest(req);
  3391. }
  3392. static void _crypto_sha_hmac_ahash_req_complete(
  3393. struct crypto_async_request *req, int err)
  3394. {
  3395. struct completion *ahash_req_complete = req->data;
  3396. if (err == -EINPROGRESS)
  3397. return;
  3398. complete(ahash_req_complete);
  3399. }
  3400. static int _sha_hmac_setkey(struct crypto_ahash *tfm, const u8 *key,
  3401. unsigned int len)
  3402. {
  3403. struct qcrypto_sha_ctx *sha_ctx = crypto_tfm_ctx(&tfm->base);
  3404. uint8_t *in_buf;
  3405. int ret = 0;
  3406. struct scatterlist sg = {0};
  3407. struct ahash_request *ahash_req;
  3408. struct completion ahash_req_complete;
  3409. ahash_req = ahash_request_alloc(tfm, GFP_KERNEL);
  3410. if (ahash_req == NULL)
  3411. return -ENOMEM;
  3412. init_completion(&ahash_req_complete);
  3413. ahash_request_set_callback(ahash_req,
  3414. CRYPTO_TFM_REQ_MAY_BACKLOG,
  3415. _crypto_sha_hmac_ahash_req_complete,
  3416. &ahash_req_complete);
  3417. crypto_ahash_clear_flags(tfm, ~0);
  3418. in_buf = kzalloc(len + 64, GFP_KERNEL);
  3419. if (in_buf == NULL) {
  3420. ahash_request_free(ahash_req);
  3421. return -ENOMEM;
  3422. }
  3423. memcpy(in_buf, key, len);
  3424. sg_set_buf(&sg, in_buf, len);
  3425. sg_mark_end(&sg);
  3426. ahash_request_set_crypt(ahash_req, &sg,
  3427. &sha_ctx->authkey[0], len);
  3428. if (sha_ctx->alg == QCE_HASH_SHA1)
  3429. ret = _sha1_digest(ahash_req);
  3430. else
  3431. ret = _sha256_digest(ahash_req);
  3432. if (ret == -EINPROGRESS || ret == -EBUSY) {
  3433. ret =
  3434. wait_for_completion_interruptible(
  3435. &ahash_req_complete);
  3436. reinit_completion(&sha_ctx->ahash_req_complete);
  3437. }
  3438. kfree_sensitive(in_buf);
  3439. ahash_request_free(ahash_req);
  3440. return ret;
  3441. }
  3442. static int _sha1_hmac_setkey(struct crypto_ahash *tfm, const u8 *key,
  3443. unsigned int len)
  3444. {
  3445. struct qcrypto_sha_ctx *sha_ctx = crypto_tfm_ctx(&tfm->base);
  3446. int ret = 0;
  3447. memset(&sha_ctx->authkey[0], 0, SHA1_BLOCK_SIZE);
  3448. if (len <= SHA1_BLOCK_SIZE) {
  3449. memcpy(&sha_ctx->authkey[0], key, len);
  3450. sha_ctx->authkey_in_len = len;
  3451. } else {
  3452. sha_ctx->alg = QCE_HASH_SHA1;
  3453. sha_ctx->diglen = SHA1_DIGEST_SIZE;
  3454. ret = _sha_hmac_setkey(tfm, key, len);
  3455. if (ret)
  3456. pr_err("SHA1 hmac setkey failed\n");
  3457. sha_ctx->authkey_in_len = SHA1_BLOCK_SIZE;
  3458. }
  3459. return ret;
  3460. }
  3461. static int _sha256_hmac_setkey(struct crypto_ahash *tfm, const u8 *key,
  3462. unsigned int len)
  3463. {
  3464. struct qcrypto_sha_ctx *sha_ctx = crypto_tfm_ctx(&tfm->base);
  3465. int ret = 0;
  3466. memset(&sha_ctx->authkey[0], 0, SHA256_BLOCK_SIZE);
  3467. if (len <= SHA256_BLOCK_SIZE) {
  3468. memcpy(&sha_ctx->authkey[0], key, len);
  3469. sha_ctx->authkey_in_len = len;
  3470. } else {
  3471. sha_ctx->alg = QCE_HASH_SHA256;
  3472. sha_ctx->diglen = SHA256_DIGEST_SIZE;
  3473. ret = _sha_hmac_setkey(tfm, key, len);
  3474. if (ret)
  3475. pr_err("SHA256 hmac setkey failed\n");
  3476. sha_ctx->authkey_in_len = SHA256_BLOCK_SIZE;
  3477. }
  3478. return ret;
  3479. }
  3480. static int _sha_hmac_init_ihash(struct ahash_request *req,
  3481. uint32_t sha_block_size)
  3482. {
  3483. struct qcrypto_sha_ctx *sha_ctx = crypto_tfm_ctx(req->base.tfm);
  3484. struct qcrypto_sha_req_ctx *rctx = ahash_request_ctx(req);
  3485. int i;
  3486. for (i = 0; i < sha_block_size; i++)
  3487. rctx->trailing_buf[i] = sha_ctx->authkey[i] ^ 0x36;
  3488. rctx->trailing_buf_len = sha_block_size;
  3489. return 0;
  3490. }
  3491. static int _sha1_hmac_init(struct ahash_request *req)
  3492. {
  3493. struct qcrypto_sha_ctx *sha_ctx = crypto_tfm_ctx(req->base.tfm);
  3494. struct crypto_priv *cp = sha_ctx->cp;
  3495. struct crypto_stat *pstat;
  3496. int ret = 0;
  3497. struct qcrypto_sha_req_ctx *rctx = ahash_request_ctx(req);
  3498. pstat = &_qcrypto_stat;
  3499. pstat->sha1_hmac_digest++;
  3500. _sha_init(req);
  3501. memset(&rctx->trailing_buf[0], 0x00, SHA1_BLOCK_SIZE);
  3502. memcpy(&rctx->digest[0], &_std_init_vector_sha1_uint8[0],
  3503. SHA1_DIGEST_SIZE);
  3504. sha_ctx->diglen = SHA1_DIGEST_SIZE;
  3505. if (cp->ce_support.sha_hmac)
  3506. sha_ctx->alg = QCE_HASH_SHA1_HMAC;
  3507. else {
  3508. sha_ctx->alg = QCE_HASH_SHA1;
  3509. ret = _sha_hmac_init_ihash(req, SHA1_BLOCK_SIZE);
  3510. }
  3511. return ret;
  3512. }
  3513. static int _sha256_hmac_init(struct ahash_request *req)
  3514. {
  3515. struct qcrypto_sha_ctx *sha_ctx = crypto_tfm_ctx(req->base.tfm);
  3516. struct crypto_priv *cp = sha_ctx->cp;
  3517. struct crypto_stat *pstat;
  3518. int ret = 0;
  3519. struct qcrypto_sha_req_ctx *rctx = ahash_request_ctx(req);
  3520. pstat = &_qcrypto_stat;
  3521. pstat->sha256_hmac_digest++;
  3522. _sha_init(req);
  3523. memset(&rctx->trailing_buf[0], 0x00, SHA256_BLOCK_SIZE);
  3524. memcpy(&rctx->digest[0], &_std_init_vector_sha256_uint8[0],
  3525. SHA256_DIGEST_SIZE);
  3526. sha_ctx->diglen = SHA256_DIGEST_SIZE;
  3527. if (cp->ce_support.sha_hmac)
  3528. sha_ctx->alg = QCE_HASH_SHA256_HMAC;
  3529. else {
  3530. sha_ctx->alg = QCE_HASH_SHA256;
  3531. ret = _sha_hmac_init_ihash(req, SHA256_BLOCK_SIZE);
  3532. }
  3533. return ret;
  3534. }
  3535. static int _sha1_hmac_update(struct ahash_request *req)
  3536. {
  3537. return _sha1_update(req);
  3538. }
  3539. static int _sha256_hmac_update(struct ahash_request *req)
  3540. {
  3541. return _sha256_update(req);
  3542. }
  3543. static int _sha_hmac_outer_hash(struct ahash_request *req,
  3544. uint32_t sha_digest_size, uint32_t sha_block_size)
  3545. {
  3546. struct qcrypto_sha_ctx *sha_ctx = crypto_tfm_ctx(req->base.tfm);
  3547. struct qcrypto_sha_req_ctx *rctx = ahash_request_ctx(req);
  3548. struct crypto_priv *cp = sha_ctx->cp;
  3549. int i;
  3550. uint8_t *staging;
  3551. uint8_t *p;
  3552. staging = (uint8_t *)ALIGN(((uintptr_t)rctx->staging_dmabuf),
  3553. L1_CACHE_BYTES);
  3554. p = staging;
  3555. for (i = 0; i < sha_block_size; i++)
  3556. *p++ = sha_ctx->authkey[i] ^ 0x5c;
  3557. memcpy(p, &rctx->digest[0], sha_digest_size);
  3558. sg_set_buf(&rctx->sg[0], staging, sha_block_size +
  3559. sha_digest_size);
  3560. sg_mark_end(&rctx->sg[0]);
  3561. /* save the original req structure fields*/
  3562. rctx->src = req->src;
  3563. rctx->nbytes = req->nbytes;
  3564. req->src = &rctx->sg[0];
  3565. req->nbytes = sha_block_size + sha_digest_size;
  3566. _sha_init(req);
  3567. if (sha_ctx->alg == QCE_HASH_SHA1) {
  3568. memcpy(&rctx->digest[0], &_std_init_vector_sha1_uint8[0],
  3569. SHA1_DIGEST_SIZE);
  3570. sha_ctx->diglen = SHA1_DIGEST_SIZE;
  3571. } else {
  3572. memcpy(&rctx->digest[0], &_std_init_vector_sha256_uint8[0],
  3573. SHA256_DIGEST_SIZE);
  3574. sha_ctx->diglen = SHA256_DIGEST_SIZE;
  3575. }
  3576. rctx->last_blk = 1;
  3577. return _qcrypto_queue_req(cp, sha_ctx->pengine, &req->base);
  3578. }
  3579. static int _sha_hmac_inner_hash(struct ahash_request *req,
  3580. uint32_t sha_digest_size, uint32_t sha_block_size)
  3581. {
  3582. struct qcrypto_sha_ctx *sha_ctx = crypto_tfm_ctx(req->base.tfm);
  3583. struct ahash_request *areq = sha_ctx->ahash_req;
  3584. struct crypto_priv *cp = sha_ctx->cp;
  3585. int ret = 0;
  3586. struct qcrypto_sha_req_ctx *rctx = ahash_request_ctx(req);
  3587. uint8_t *staging;
  3588. staging = (uint8_t *)ALIGN(((uintptr_t)rctx->staging_dmabuf),
  3589. L1_CACHE_BYTES);
  3590. memcpy(staging, rctx->trailing_buf, rctx->trailing_buf_len);
  3591. sg_set_buf(&rctx->sg[0], staging, rctx->trailing_buf_len);
  3592. sg_mark_end(&rctx->sg[0]);
  3593. ahash_request_set_crypt(areq, &rctx->sg[0], &rctx->digest[0],
  3594. rctx->trailing_buf_len);
  3595. rctx->last_blk = 1;
  3596. ret = _qcrypto_queue_req(cp, sha_ctx->pengine, &areq->base);
  3597. if (ret == -EINPROGRESS || ret == -EBUSY) {
  3598. ret =
  3599. wait_for_completion_interruptible(&sha_ctx->ahash_req_complete);
  3600. reinit_completion(&sha_ctx->ahash_req_complete);
  3601. }
  3602. return ret;
  3603. }
  3604. static int _sha1_hmac_final(struct ahash_request *req)
  3605. {
  3606. struct qcrypto_sha_ctx *sha_ctx = crypto_tfm_ctx(req->base.tfm);
  3607. struct crypto_priv *cp = sha_ctx->cp;
  3608. int ret = 0;
  3609. if (cp->ce_support.sha_hmac)
  3610. return _sha_final(req, SHA1_BLOCK_SIZE);
  3611. ret = _sha_hmac_inner_hash(req, SHA1_DIGEST_SIZE, SHA1_BLOCK_SIZE);
  3612. if (ret)
  3613. return ret;
  3614. return _sha_hmac_outer_hash(req, SHA1_DIGEST_SIZE, SHA1_BLOCK_SIZE);
  3615. }
  3616. static int _sha256_hmac_final(struct ahash_request *req)
  3617. {
  3618. struct qcrypto_sha_ctx *sha_ctx = crypto_tfm_ctx(req->base.tfm);
  3619. struct crypto_priv *cp = sha_ctx->cp;
  3620. int ret = 0;
  3621. if (cp->ce_support.sha_hmac)
  3622. return _sha_final(req, SHA256_BLOCK_SIZE);
  3623. ret = _sha_hmac_inner_hash(req, SHA256_DIGEST_SIZE, SHA256_BLOCK_SIZE);
  3624. if (ret)
  3625. return ret;
  3626. return _sha_hmac_outer_hash(req, SHA256_DIGEST_SIZE, SHA256_BLOCK_SIZE);
  3627. }
  3628. static int _sha1_hmac_digest(struct ahash_request *req)
  3629. {
  3630. struct qcrypto_sha_ctx *sha_ctx = crypto_tfm_ctx(req->base.tfm);
  3631. struct crypto_stat *pstat;
  3632. struct qcrypto_sha_req_ctx *rctx = ahash_request_ctx(req);
  3633. pstat = &_qcrypto_stat;
  3634. pstat->sha1_hmac_digest++;
  3635. _sha_init(req);
  3636. memcpy(&rctx->digest[0], &_std_init_vector_sha1_uint8[0],
  3637. SHA1_DIGEST_SIZE);
  3638. sha_ctx->diglen = SHA1_DIGEST_SIZE;
  3639. sha_ctx->alg = QCE_HASH_SHA1_HMAC;
  3640. return _sha_digest(req);
  3641. }
  3642. static int _sha256_hmac_digest(struct ahash_request *req)
  3643. {
  3644. struct qcrypto_sha_ctx *sha_ctx = crypto_tfm_ctx(req->base.tfm);
  3645. struct crypto_stat *pstat;
  3646. struct qcrypto_sha_req_ctx *rctx = ahash_request_ctx(req);
  3647. pstat = &_qcrypto_stat;
  3648. pstat->sha256_hmac_digest++;
  3649. _sha_init(req);
  3650. memcpy(&rctx->digest[0], &_std_init_vector_sha256_uint8[0],
  3651. SHA256_DIGEST_SIZE);
  3652. sha_ctx->diglen = SHA256_DIGEST_SIZE;
  3653. sha_ctx->alg = QCE_HASH_SHA256_HMAC;
  3654. return _sha_digest(req);
  3655. }
  3656. static int _qcrypto_prefix_alg_cra_name(char cra_name[], unsigned int size)
  3657. {
  3658. char new_cra_name[CRYPTO_MAX_ALG_NAME] = "qcom-";
  3659. if (size >= CRYPTO_MAX_ALG_NAME - strlen("qcom-"))
  3660. return -EINVAL;
  3661. strlcat(new_cra_name, cra_name, CRYPTO_MAX_ALG_NAME);
  3662. strlcpy(cra_name, new_cra_name, CRYPTO_MAX_ALG_NAME);
  3663. return 0;
  3664. }
  3665. int qcrypto_cipher_set_device(struct skcipher_request *req, unsigned int dev)
  3666. {
  3667. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  3668. struct qcrypto_cipher_ctx *ctx = crypto_skcipher_ctx(tfm);
  3669. struct crypto_priv *cp = ctx->cp;
  3670. struct crypto_engine *pengine = NULL;
  3671. pengine = _qrypto_find_pengine_device(cp, dev);
  3672. if (pengine == NULL)
  3673. return -ENODEV;
  3674. ctx->pengine = pengine;
  3675. return 0;
  3676. }
  3677. EXPORT_SYMBOL(qcrypto_cipher_set_device);
  3678. int qcrypto_cipher_set_device_hw(struct skcipher_request *req, u32 dev,
  3679. u32 hw_inst)
  3680. {
  3681. struct qcrypto_cipher_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  3682. struct crypto_priv *cp = ctx->cp;
  3683. struct crypto_engine *pengine = NULL;
  3684. pengine = _qrypto_find_pengine_device_hw(cp, dev, hw_inst);
  3685. if (pengine == NULL)
  3686. return -ENODEV;
  3687. ctx->pengine = pengine;
  3688. return 0;
  3689. }
  3690. EXPORT_SYMBOL(qcrypto_cipher_set_device_hw);
  3691. int qcrypto_aead_set_device(struct aead_request *req, unsigned int dev)
  3692. {
  3693. struct qcrypto_cipher_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  3694. struct crypto_priv *cp = ctx->cp;
  3695. struct crypto_engine *pengine = NULL;
  3696. pengine = _qrypto_find_pengine_device(cp, dev);
  3697. if (pengine == NULL)
  3698. return -ENODEV;
  3699. ctx->pengine = pengine;
  3700. return 0;
  3701. }
  3702. EXPORT_SYMBOL(qcrypto_aead_set_device);
  3703. int qcrypto_ahash_set_device(struct ahash_request *req, unsigned int dev)
  3704. {
  3705. struct qcrypto_sha_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  3706. struct crypto_priv *cp = ctx->cp;
  3707. struct crypto_engine *pengine = NULL;
  3708. pengine = _qrypto_find_pengine_device(cp, dev);
  3709. if (pengine == NULL)
  3710. return -ENODEV;
  3711. ctx->pengine = pengine;
  3712. return 0;
  3713. }
  3714. EXPORT_SYMBOL(qcrypto_ahash_set_device);
  3715. int qcrypto_cipher_set_flag(struct skcipher_request *req, unsigned int flags)
  3716. {
  3717. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  3718. struct qcrypto_cipher_ctx *ctx = crypto_skcipher_ctx(tfm);
  3719. struct crypto_priv *cp = ctx->cp;
  3720. if ((flags & QCRYPTO_CTX_USE_HW_KEY) &&
  3721. (!cp->platform_support.hw_key_support)) {
  3722. pr_err("%s HW key usage not supported\n", __func__);
  3723. return -EINVAL;
  3724. }
  3725. if (((flags | ctx->flags) & QCRYPTO_CTX_KEY_MASK) ==
  3726. QCRYPTO_CTX_KEY_MASK) {
  3727. pr_err("%s Cannot set all key flags\n", __func__);
  3728. return -EINVAL;
  3729. }
  3730. ctx->flags |= flags;
  3731. return 0;
  3732. }
  3733. EXPORT_SYMBOL(qcrypto_cipher_set_flag);
  3734. int qcrypto_aead_set_flag(struct aead_request *req, unsigned int flags)
  3735. {
  3736. struct qcrypto_cipher_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  3737. struct crypto_priv *cp = ctx->cp;
  3738. if ((flags & QCRYPTO_CTX_USE_HW_KEY) &&
  3739. (!cp->platform_support.hw_key_support)) {
  3740. pr_err("%s HW key usage not supported\n", __func__);
  3741. return -EINVAL;
  3742. }
  3743. if (((flags | ctx->flags) & QCRYPTO_CTX_KEY_MASK) ==
  3744. QCRYPTO_CTX_KEY_MASK) {
  3745. pr_err("%s Cannot set all key flags\n", __func__);
  3746. return -EINVAL;
  3747. }
  3748. ctx->flags |= flags;
  3749. return 0;
  3750. }
  3751. EXPORT_SYMBOL(qcrypto_aead_set_flag);
  3752. int qcrypto_ahash_set_flag(struct ahash_request *req, unsigned int flags)
  3753. {
  3754. struct qcrypto_sha_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  3755. struct crypto_priv *cp = ctx->cp;
  3756. if ((flags & QCRYPTO_CTX_USE_HW_KEY) &&
  3757. (!cp->platform_support.hw_key_support)) {
  3758. pr_err("%s HW key usage not supported\n", __func__);
  3759. return -EINVAL;
  3760. }
  3761. if (((flags | ctx->flags) & QCRYPTO_CTX_KEY_MASK) ==
  3762. QCRYPTO_CTX_KEY_MASK) {
  3763. pr_err("%s Cannot set all key flags\n", __func__);
  3764. return -EINVAL;
  3765. }
  3766. ctx->flags |= flags;
  3767. return 0;
  3768. }
  3769. EXPORT_SYMBOL(qcrypto_ahash_set_flag);
  3770. int qcrypto_cipher_clear_flag(struct skcipher_request *req,
  3771. unsigned int flags)
  3772. {
  3773. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  3774. struct qcrypto_cipher_ctx *ctx = crypto_skcipher_ctx(tfm);
  3775. ctx->flags &= ~flags;
  3776. return 0;
  3777. }
  3778. EXPORT_SYMBOL(qcrypto_cipher_clear_flag);
  3779. int qcrypto_aead_clear_flag(struct aead_request *req, unsigned int flags)
  3780. {
  3781. struct qcrypto_cipher_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  3782. ctx->flags &= ~flags;
  3783. return 0;
  3784. }
  3785. EXPORT_SYMBOL(qcrypto_aead_clear_flag);
  3786. int qcrypto_ahash_clear_flag(struct ahash_request *req, unsigned int flags)
  3787. {
  3788. struct qcrypto_sha_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  3789. ctx->flags &= ~flags;
  3790. return 0;
  3791. }
  3792. EXPORT_SYMBOL(qcrypto_ahash_clear_flag);
  3793. static struct ahash_alg _qcrypto_ahash_algos[] = {
  3794. {
  3795. .init = _sha1_init,
  3796. .update = _sha1_update,
  3797. .final = _sha1_final,
  3798. .digest = _sha1_digest,
  3799. .export = _sha1_export,
  3800. .import = _sha1_import,
  3801. .halg = {
  3802. .digestsize = SHA1_DIGEST_SIZE,
  3803. .statesize = sizeof(struct sha1_state),
  3804. .base = {
  3805. .cra_name = "sha1",
  3806. .cra_driver_name = "qcrypto-sha1",
  3807. .cra_priority = 300,
  3808. .cra_flags = CRYPTO_ALG_ASYNC,
  3809. .cra_blocksize = SHA1_BLOCK_SIZE,
  3810. .cra_ctxsize = sizeof(struct qcrypto_sha_ctx),
  3811. .cra_alignmask = 0,
  3812. .cra_module = THIS_MODULE,
  3813. .cra_init = _qcrypto_ahash_cra_init,
  3814. .cra_exit = _qcrypto_ahash_cra_exit,
  3815. },
  3816. },
  3817. },
  3818. {
  3819. .init = _sha256_init,
  3820. .update = _sha256_update,
  3821. .final = _sha256_final,
  3822. .digest = _sha256_digest,
  3823. .export = _sha256_export,
  3824. .import = _sha256_import,
  3825. .halg = {
  3826. .digestsize = SHA256_DIGEST_SIZE,
  3827. .statesize = sizeof(struct sha256_state),
  3828. .base = {
  3829. .cra_name = "sha256",
  3830. .cra_driver_name = "qcrypto-sha256",
  3831. .cra_priority = 300,
  3832. .cra_flags = CRYPTO_ALG_ASYNC,
  3833. .cra_blocksize = SHA256_BLOCK_SIZE,
  3834. .cra_ctxsize = sizeof(struct qcrypto_sha_ctx),
  3835. .cra_alignmask = 0,
  3836. .cra_module = THIS_MODULE,
  3837. .cra_init = _qcrypto_ahash_cra_init,
  3838. .cra_exit = _qcrypto_ahash_cra_exit,
  3839. },
  3840. },
  3841. },
  3842. };
  3843. static struct ahash_alg _qcrypto_sha_hmac_algos[] = {
  3844. {
  3845. .init = _sha1_hmac_init,
  3846. .update = _sha1_hmac_update,
  3847. .final = _sha1_hmac_final,
  3848. .export = _sha1_hmac_export,
  3849. .import = _sha1_hmac_import,
  3850. .digest = _sha1_hmac_digest,
  3851. .setkey = _sha1_hmac_setkey,
  3852. .halg = {
  3853. .digestsize = SHA1_DIGEST_SIZE,
  3854. .statesize = sizeof(struct sha1_state),
  3855. .base = {
  3856. .cra_name = "hmac(sha1)",
  3857. .cra_driver_name = "qcrypto-hmac-sha1",
  3858. .cra_priority = 300,
  3859. .cra_flags = CRYPTO_ALG_ASYNC,
  3860. .cra_blocksize = SHA1_BLOCK_SIZE,
  3861. .cra_ctxsize = sizeof(struct qcrypto_sha_ctx),
  3862. .cra_alignmask = 0,
  3863. .cra_module = THIS_MODULE,
  3864. .cra_init = _qcrypto_ahash_hmac_cra_init,
  3865. .cra_exit = _qcrypto_ahash_cra_exit,
  3866. },
  3867. },
  3868. },
  3869. {
  3870. .init = _sha256_hmac_init,
  3871. .update = _sha256_hmac_update,
  3872. .final = _sha256_hmac_final,
  3873. .export = _sha256_hmac_export,
  3874. .import = _sha256_hmac_import,
  3875. .digest = _sha256_hmac_digest,
  3876. .setkey = _sha256_hmac_setkey,
  3877. .halg = {
  3878. .digestsize = SHA256_DIGEST_SIZE,
  3879. .statesize = sizeof(struct sha256_state),
  3880. .base = {
  3881. .cra_name = "hmac(sha256)",
  3882. .cra_driver_name = "qcrypto-hmac-sha256",
  3883. .cra_priority = 300,
  3884. .cra_flags = CRYPTO_ALG_ASYNC,
  3885. .cra_blocksize = SHA256_BLOCK_SIZE,
  3886. .cra_ctxsize = sizeof(struct qcrypto_sha_ctx),
  3887. .cra_alignmask = 0,
  3888. .cra_module = THIS_MODULE,
  3889. .cra_init = _qcrypto_ahash_hmac_cra_init,
  3890. .cra_exit = _qcrypto_ahash_cra_exit,
  3891. },
  3892. },
  3893. },
  3894. };
  3895. static struct skcipher_alg _qcrypto_sk_cipher_algos[] = {
  3896. {
  3897. .setkey = _qcrypto_setkey_aes,
  3898. .encrypt = _qcrypto_enc_aes_ecb,
  3899. .decrypt = _qcrypto_dec_aes_ecb,
  3900. .init = _qcrypto_aes_skcipher_init,
  3901. .exit = _qcrypto_aes_skcipher_exit,
  3902. .min_keysize = AES_MIN_KEY_SIZE,
  3903. .max_keysize = AES_MAX_KEY_SIZE,
  3904. .base = {
  3905. .cra_name = "ecb(aes)",
  3906. .cra_driver_name = "qcrypto-ecb-aes",
  3907. .cra_priority = 300,
  3908. .cra_flags = CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC,
  3909. .cra_blocksize = AES_BLOCK_SIZE,
  3910. .cra_ctxsize = sizeof(struct qcrypto_cipher_ctx),
  3911. .cra_alignmask = 0,
  3912. .cra_module = THIS_MODULE,
  3913. },
  3914. },
  3915. {
  3916. .setkey = _qcrypto_setkey_aes,
  3917. .encrypt = _qcrypto_enc_aes_cbc,
  3918. .decrypt = _qcrypto_dec_aes_cbc,
  3919. .init = _qcrypto_aes_skcipher_init,
  3920. .exit = _qcrypto_aes_skcipher_exit,
  3921. .min_keysize = AES_MIN_KEY_SIZE,
  3922. .max_keysize = AES_MAX_KEY_SIZE,
  3923. .ivsize = AES_BLOCK_SIZE,
  3924. .base = {
  3925. .cra_name = "cbc(aes)",
  3926. .cra_driver_name = "qcrypto-cbc-aes",
  3927. .cra_priority = 300,
  3928. .cra_flags = CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC,
  3929. .cra_blocksize = AES_BLOCK_SIZE,
  3930. .cra_ctxsize = sizeof(struct qcrypto_cipher_ctx),
  3931. .cra_alignmask = 0,
  3932. .cra_module = THIS_MODULE,
  3933. },
  3934. },
  3935. {
  3936. .setkey = _qcrypto_setkey_aes,
  3937. .encrypt = _qcrypto_enc_aes_ctr,
  3938. .decrypt = _qcrypto_dec_aes_ctr,
  3939. .init = _qcrypto_aes_skcipher_init,
  3940. .exit = _qcrypto_aes_skcipher_exit,
  3941. .min_keysize = AES_MIN_KEY_SIZE,
  3942. .max_keysize = AES_MAX_KEY_SIZE,
  3943. .ivsize = AES_BLOCK_SIZE,
  3944. .base = {
  3945. .cra_name = "ctr(aes)",
  3946. .cra_driver_name = "qcrypto-ctr-aes",
  3947. .cra_priority = 300,
  3948. .cra_flags = CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC,
  3949. .cra_blocksize = AES_BLOCK_SIZE,
  3950. .cra_ctxsize = sizeof(struct qcrypto_cipher_ctx),
  3951. .cra_alignmask = 0,
  3952. .cra_module = THIS_MODULE,
  3953. },
  3954. },
  3955. {
  3956. .setkey = _qcrypto_setkey_des,
  3957. .encrypt = _qcrypto_enc_des_ecb,
  3958. .decrypt = _qcrypto_dec_des_ecb,
  3959. .init = _qcrypto_skcipher_init,
  3960. .exit = _qcrypto_skcipher_exit,
  3961. .min_keysize = DES_KEY_SIZE,
  3962. .max_keysize = DES_KEY_SIZE,
  3963. .base = {
  3964. .cra_name = "ecb(des)",
  3965. .cra_driver_name = "qcrypto-ecb-des",
  3966. .cra_priority = 300,
  3967. .cra_flags = CRYPTO_ALG_ASYNC,
  3968. .cra_blocksize = DES_BLOCK_SIZE,
  3969. .cra_ctxsize = sizeof(struct qcrypto_cipher_ctx),
  3970. .cra_alignmask = 0,
  3971. .cra_module = THIS_MODULE,
  3972. },
  3973. },
  3974. {
  3975. .setkey = _qcrypto_setkey_des,
  3976. .encrypt = _qcrypto_enc_des_cbc,
  3977. .decrypt = _qcrypto_dec_des_cbc,
  3978. .init = _qcrypto_skcipher_init,
  3979. .exit = _qcrypto_skcipher_exit,
  3980. .min_keysize = DES_KEY_SIZE,
  3981. .max_keysize = DES_KEY_SIZE,
  3982. .ivsize = DES_BLOCK_SIZE,
  3983. .base = {
  3984. .cra_name = "cbc(des)",
  3985. .cra_driver_name = "qcrypto-cbc-des",
  3986. .cra_priority = 300,
  3987. .cra_flags = CRYPTO_ALG_ASYNC,
  3988. .cra_blocksize = DES_BLOCK_SIZE,
  3989. .cra_ctxsize = sizeof(struct qcrypto_cipher_ctx),
  3990. .cra_alignmask = 0,
  3991. .cra_module = THIS_MODULE,
  3992. },
  3993. },
  3994. {
  3995. .setkey = _qcrypto_setkey_3des,
  3996. .encrypt = _qcrypto_enc_3des_ecb,
  3997. .decrypt = _qcrypto_dec_3des_ecb,
  3998. .init = _qcrypto_skcipher_init,
  3999. .exit = _qcrypto_skcipher_exit,
  4000. .min_keysize = DES3_EDE_KEY_SIZE,
  4001. .max_keysize = DES3_EDE_KEY_SIZE,
  4002. .base = {
  4003. .cra_name = "ecb(des3_ede)",
  4004. .cra_driver_name = "qcrypto-ecb-3des",
  4005. .cra_priority = 300,
  4006. .cra_flags = CRYPTO_ALG_ASYNC,
  4007. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  4008. .cra_ctxsize = sizeof(struct qcrypto_cipher_ctx),
  4009. .cra_alignmask = 0,
  4010. .cra_module = THIS_MODULE,
  4011. },
  4012. },
  4013. {
  4014. .setkey = _qcrypto_setkey_3des,
  4015. .encrypt = _qcrypto_enc_3des_cbc,
  4016. .decrypt = _qcrypto_dec_3des_cbc,
  4017. .init = _qcrypto_skcipher_init,
  4018. .exit = _qcrypto_skcipher_exit,
  4019. .min_keysize = DES3_EDE_KEY_SIZE,
  4020. .max_keysize = DES3_EDE_KEY_SIZE,
  4021. .ivsize = DES3_EDE_BLOCK_SIZE,
  4022. .base = {
  4023. .cra_name = "cbc(des3_ede)",
  4024. .cra_driver_name = "qcrypto-cbc-3des",
  4025. .cra_priority = 300,
  4026. .cra_flags = CRYPTO_ALG_ASYNC,
  4027. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  4028. .cra_ctxsize = sizeof(struct qcrypto_cipher_ctx),
  4029. .cra_alignmask = 0,
  4030. .cra_module = THIS_MODULE,
  4031. },
  4032. },
  4033. };
  4034. static struct skcipher_alg _qcrypto_sk_cipher_xts_algo = {
  4035. .setkey = _qcrypto_setkey_aes_xts,
  4036. .encrypt = _qcrypto_enc_aes_xts,
  4037. .decrypt = _qcrypto_dec_aes_xts,
  4038. .init = _qcrypto_skcipher_init,
  4039. .exit = _qcrypto_skcipher_exit,
  4040. .min_keysize = AES_MIN_KEY_SIZE,
  4041. .max_keysize = AES_MAX_KEY_SIZE,
  4042. .ivsize = AES_BLOCK_SIZE,
  4043. .base = {
  4044. .cra_name = "xts(aes)",
  4045. .cra_driver_name = "qcrypto-xts-aes",
  4046. .cra_priority = 300,
  4047. .cra_flags = CRYPTO_ALG_ASYNC,
  4048. .cra_blocksize = AES_BLOCK_SIZE,
  4049. .cra_ctxsize = sizeof(struct qcrypto_cipher_ctx),
  4050. .cra_alignmask = 0,
  4051. .cra_module = THIS_MODULE,
  4052. },
  4053. };
  4054. static struct aead_alg _qcrypto_aead_sha1_hmac_algos[] = {
  4055. {
  4056. .setkey = _qcrypto_aead_setkey,
  4057. .setauthsize = _qcrypto_aead_setauthsize,
  4058. .encrypt = _qcrypto_aead_encrypt_aes_cbc,
  4059. .decrypt = _qcrypto_aead_decrypt_aes_cbc,
  4060. .init = _qcrypto_cra_aead_aes_sha1_init,
  4061. .exit = _qcrypto_cra_aead_aes_exit,
  4062. .ivsize = AES_BLOCK_SIZE,
  4063. .maxauthsize = SHA1_DIGEST_SIZE,
  4064. .base = {
  4065. .cra_name = "authenc(hmac(sha1),cbc(aes))",
  4066. .cra_driver_name = "qcrypto-aead-hmac-sha1-cbc-aes",
  4067. .cra_priority = 300,
  4068. .cra_flags = CRYPTO_ALG_ASYNC,
  4069. .cra_blocksize = AES_BLOCK_SIZE,
  4070. .cra_ctxsize = sizeof(struct qcrypto_cipher_ctx),
  4071. .cra_alignmask = 0,
  4072. .cra_module = THIS_MODULE,
  4073. },
  4074. },
  4075. {
  4076. .setkey = _qcrypto_aead_setkey,
  4077. .setauthsize = _qcrypto_aead_setauthsize,
  4078. .encrypt = _qcrypto_aead_encrypt_des_cbc,
  4079. .decrypt = _qcrypto_aead_decrypt_des_cbc,
  4080. .init = _qcrypto_cra_aead_sha1_init,
  4081. .exit = _qcrypto_cra_aead_exit,
  4082. .ivsize = DES_BLOCK_SIZE,
  4083. .maxauthsize = SHA1_DIGEST_SIZE,
  4084. .base = {
  4085. .cra_name = "authenc(hmac(sha1),cbc(des))",
  4086. .cra_driver_name = "qcrypto-aead-hmac-sha1-cbc-des",
  4087. .cra_priority = 300,
  4088. .cra_flags = CRYPTO_ALG_ASYNC,
  4089. .cra_blocksize = DES_BLOCK_SIZE,
  4090. .cra_ctxsize = sizeof(struct qcrypto_cipher_ctx),
  4091. .cra_alignmask = 0,
  4092. .cra_module = THIS_MODULE,
  4093. },
  4094. },
  4095. {
  4096. .setkey = _qcrypto_aead_setkey,
  4097. .setauthsize = _qcrypto_aead_setauthsize,
  4098. .encrypt = _qcrypto_aead_encrypt_3des_cbc,
  4099. .decrypt = _qcrypto_aead_decrypt_3des_cbc,
  4100. .init = _qcrypto_cra_aead_sha1_init,
  4101. .exit = _qcrypto_cra_aead_exit,
  4102. .ivsize = DES3_EDE_BLOCK_SIZE,
  4103. .maxauthsize = SHA1_DIGEST_SIZE,
  4104. .base = {
  4105. .cra_name = "authenc(hmac(sha1),cbc(des3_ede))",
  4106. .cra_driver_name = "qcrypto-aead-hmac-sha1-cbc-3des",
  4107. .cra_priority = 300,
  4108. .cra_flags = CRYPTO_ALG_ASYNC,
  4109. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  4110. .cra_ctxsize = sizeof(struct qcrypto_cipher_ctx),
  4111. .cra_alignmask = 0,
  4112. .cra_module = THIS_MODULE,
  4113. },
  4114. },
  4115. };
  4116. static struct aead_alg _qcrypto_aead_sha256_hmac_algos[] = {
  4117. {
  4118. .setkey = _qcrypto_aead_setkey,
  4119. .setauthsize = _qcrypto_aead_setauthsize,
  4120. .encrypt = _qcrypto_aead_encrypt_aes_cbc,
  4121. .decrypt = _qcrypto_aead_decrypt_aes_cbc,
  4122. .init = _qcrypto_cra_aead_aes_sha256_init,
  4123. .exit = _qcrypto_cra_aead_aes_exit,
  4124. .ivsize = AES_BLOCK_SIZE,
  4125. .maxauthsize = SHA256_DIGEST_SIZE,
  4126. .base = {
  4127. .cra_name = "authenc(hmac(sha256),cbc(aes))",
  4128. .cra_driver_name = "qcrypto-aead-hmac-sha256-cbc-aes",
  4129. .cra_priority = 300,
  4130. .cra_flags = CRYPTO_ALG_ASYNC,
  4131. .cra_blocksize = AES_BLOCK_SIZE,
  4132. .cra_ctxsize = sizeof(struct qcrypto_cipher_ctx),
  4133. .cra_alignmask = 0,
  4134. .cra_module = THIS_MODULE,
  4135. },
  4136. },
  4137. {
  4138. .setkey = _qcrypto_aead_setkey,
  4139. .setauthsize = _qcrypto_aead_setauthsize,
  4140. .encrypt = _qcrypto_aead_encrypt_des_cbc,
  4141. .decrypt = _qcrypto_aead_decrypt_des_cbc,
  4142. .init = _qcrypto_cra_aead_sha256_init,
  4143. .exit = _qcrypto_cra_aead_exit,
  4144. .ivsize = DES_BLOCK_SIZE,
  4145. .maxauthsize = SHA256_DIGEST_SIZE,
  4146. .base = {
  4147. .cra_name = "authenc(hmac(sha256),cbc(des))",
  4148. .cra_driver_name = "qcrypto-aead-hmac-sha256-cbc-des",
  4149. .cra_priority = 300,
  4150. .cra_flags = CRYPTO_ALG_ASYNC,
  4151. .cra_blocksize = DES_BLOCK_SIZE,
  4152. .cra_ctxsize = sizeof(struct qcrypto_cipher_ctx),
  4153. .cra_alignmask = 0,
  4154. .cra_module = THIS_MODULE,
  4155. },
  4156. },
  4157. {
  4158. .setkey = _qcrypto_aead_setkey,
  4159. .setauthsize = _qcrypto_aead_setauthsize,
  4160. .encrypt = _qcrypto_aead_encrypt_3des_cbc,
  4161. .decrypt = _qcrypto_aead_decrypt_3des_cbc,
  4162. .init = _qcrypto_cra_aead_sha256_init,
  4163. .exit = _qcrypto_cra_aead_exit,
  4164. .ivsize = DES3_EDE_BLOCK_SIZE,
  4165. .maxauthsize = SHA256_DIGEST_SIZE,
  4166. .base = {
  4167. .cra_name = "authenc(hmac(sha256),cbc(des3_ede))",
  4168. .cra_driver_name = "qcrypto-aead-hmac-sha256-cbc-3des",
  4169. .cra_priority = 300,
  4170. .cra_flags = CRYPTO_ALG_ASYNC,
  4171. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  4172. .cra_ctxsize = sizeof(struct qcrypto_cipher_ctx),
  4173. .cra_alignmask = 0,
  4174. .cra_module = THIS_MODULE,
  4175. },
  4176. },
  4177. };
  4178. static struct aead_alg _qcrypto_aead_ccm_algo = {
  4179. .setkey = _qcrypto_aead_ccm_setkey,
  4180. .setauthsize = _qcrypto_aead_ccm_setauthsize,
  4181. .encrypt = _qcrypto_aead_encrypt_aes_ccm,
  4182. .decrypt = _qcrypto_aead_decrypt_aes_ccm,
  4183. .init = _qcrypto_cra_aead_ccm_init,
  4184. .exit = _qcrypto_cra_aead_exit,
  4185. .ivsize = AES_BLOCK_SIZE,
  4186. .maxauthsize = AES_BLOCK_SIZE,
  4187. .base = {
  4188. .cra_name = "ccm(aes)",
  4189. .cra_driver_name = "qcrypto-aes-ccm",
  4190. .cra_priority = 300,
  4191. .cra_flags = CRYPTO_ALG_ASYNC,
  4192. .cra_blocksize = AES_BLOCK_SIZE,
  4193. .cra_ctxsize = sizeof(struct qcrypto_cipher_ctx),
  4194. .cra_alignmask = 0,
  4195. .cra_module = THIS_MODULE,
  4196. },
  4197. };
  4198. static struct aead_alg _qcrypto_aead_rfc4309_ccm_algo = {
  4199. .setkey = _qcrypto_aead_rfc4309_ccm_setkey,
  4200. .setauthsize = _qcrypto_aead_rfc4309_ccm_setauthsize,
  4201. .encrypt = _qcrypto_aead_rfc4309_enc_aes_ccm,
  4202. .decrypt = _qcrypto_aead_rfc4309_dec_aes_ccm,
  4203. .init = _qcrypto_cra_aead_rfc4309_ccm_init,
  4204. .exit = _qcrypto_cra_aead_exit,
  4205. .ivsize = 8,
  4206. .maxauthsize = 16,
  4207. .base = {
  4208. .cra_name = "rfc4309(ccm(aes))",
  4209. .cra_driver_name = "qcrypto-rfc4309-aes-ccm",
  4210. .cra_priority = 300,
  4211. .cra_flags = CRYPTO_ALG_ASYNC,
  4212. .cra_blocksize = 1,
  4213. .cra_ctxsize = sizeof(struct qcrypto_cipher_ctx),
  4214. .cra_alignmask = 0,
  4215. .cra_module = THIS_MODULE,
  4216. },
  4217. };
  4218. static int _qcrypto_probe(struct platform_device *pdev)
  4219. {
  4220. int rc = 0;
  4221. void *handle;
  4222. struct crypto_priv *cp = &qcrypto_dev;
  4223. int i;
  4224. struct msm_ce_hw_support *platform_support;
  4225. struct crypto_engine *pengine;
  4226. unsigned long flags;
  4227. struct qcrypto_req_control *pqcrypto_req_control = NULL;
  4228. pengine = kzalloc(sizeof(*pengine), GFP_KERNEL);
  4229. if (!pengine)
  4230. return -ENOMEM;
  4231. pengine->icc_path = of_icc_get(&pdev->dev, "data_path");
  4232. if (IS_ERR(pengine->icc_path)) {
  4233. dev_err(&pdev->dev, "failed to get icc path\n");
  4234. rc = PTR_ERR(pengine->icc_path);
  4235. goto exit_kzfree;
  4236. }
  4237. pengine->bw_state = BUS_NO_BANDWIDTH;
  4238. rc = icc_set_bw(pengine->icc_path, CRYPTO_AVG_BW, CRYPTO_PEAK_BW);
  4239. if (rc) {
  4240. dev_err(&pdev->dev, "failed to set high bandwidth\n");
  4241. goto exit_kzfree;
  4242. }
  4243. handle = qce_open(pdev, &rc);
  4244. if (handle == NULL) {
  4245. rc = -ENODEV;
  4246. goto exit_free_pdata;
  4247. }
  4248. rc = icc_set_bw(pengine->icc_path, 0, 0);
  4249. if (rc) {
  4250. dev_err(&pdev->dev, "failed to set low bandwidth\n");
  4251. goto exit_qce_close;
  4252. }
  4253. platform_set_drvdata(pdev, pengine);
  4254. pengine->qce = handle;
  4255. pengine->pcp = cp;
  4256. pengine->pdev = pdev;
  4257. pengine->signature = 0xdeadbeef;
  4258. timer_setup(&(pengine->bw_reaper_timer),
  4259. qcrypto_bw_reaper_timer_callback, 0);
  4260. INIT_WORK(&pengine->bw_reaper_ws, qcrypto_bw_reaper_work);
  4261. INIT_WORK(&pengine->bw_allocate_ws, qcrypto_bw_allocate_work);
  4262. pengine->high_bw_req = false;
  4263. pengine->active_seq = 0;
  4264. pengine->last_active_seq = 0;
  4265. pengine->check_flag = false;
  4266. pengine->max_req_used = 0;
  4267. pengine->issue_req = false;
  4268. crypto_init_queue(&pengine->req_queue, MSM_QCRYPTO_REQ_QUEUE_LENGTH);
  4269. mutex_lock(&cp->engine_lock);
  4270. cp->total_units++;
  4271. pengine->unit = cp->total_units;
  4272. spin_lock_irqsave(&cp->lock, flags);
  4273. pengine->first_engine = list_empty(&cp->engine_list);
  4274. if (pengine->first_engine)
  4275. cp->first_engine = pengine;
  4276. list_add_tail(&pengine->elist, &cp->engine_list);
  4277. cp->next_engine = pengine;
  4278. spin_unlock_irqrestore(&cp->lock, flags);
  4279. qce_hw_support(pengine->qce, &cp->ce_support);
  4280. pengine->ce_hw_instance = cp->ce_support.ce_hw_instance;
  4281. pengine->max_req = cp->ce_support.max_request;
  4282. pqcrypto_req_control = kcalloc(pengine->max_req,
  4283. sizeof(struct qcrypto_req_control),
  4284. GFP_KERNEL);
  4285. if (pqcrypto_req_control == NULL) {
  4286. rc = -ENOMEM;
  4287. goto exit_unlock_mutex;
  4288. }
  4289. qcrypto_init_req_control(pengine, pqcrypto_req_control);
  4290. if (cp->ce_support.bam) {
  4291. cp->platform_support.ce_shared = cp->ce_support.is_shared;
  4292. cp->platform_support.shared_ce_resource = 0;
  4293. cp->platform_support.hw_key_support = cp->ce_support.hw_key;
  4294. cp->platform_support.sha_hmac = 1;
  4295. pengine->ce_device = cp->ce_support.ce_device;
  4296. } else {
  4297. platform_support =
  4298. (struct msm_ce_hw_support *)pdev->dev.platform_data;
  4299. cp->platform_support.ce_shared = platform_support->ce_shared;
  4300. cp->platform_support.shared_ce_resource =
  4301. platform_support->shared_ce_resource;
  4302. cp->platform_support.hw_key_support =
  4303. platform_support->hw_key_support;
  4304. cp->platform_support.sha_hmac = platform_support->sha_hmac;
  4305. }
  4306. if (cp->total_units != 1)
  4307. goto exit_unlock_mutex;
  4308. /* register crypto cipher algorithms the device supports */
  4309. for (i = 0; i < ARRAY_SIZE(_qcrypto_sk_cipher_algos); i++) {
  4310. struct qcrypto_alg *q_alg;
  4311. q_alg = _qcrypto_cipher_alg_alloc(cp,
  4312. &_qcrypto_sk_cipher_algos[i]);
  4313. if (IS_ERR(q_alg)) {
  4314. rc = PTR_ERR(q_alg);
  4315. goto err;
  4316. }
  4317. if (cp->ce_support.use_sw_aes_cbc_ecb_ctr_algo) {
  4318. rc = _qcrypto_prefix_alg_cra_name(
  4319. q_alg->cipher_alg.base.cra_name,
  4320. strlen(q_alg->cipher_alg.base.cra_name));
  4321. if (rc) {
  4322. dev_err(&pdev->dev,
  4323. "The algorithm name %s is too long.\n",
  4324. q_alg->cipher_alg.base.cra_name);
  4325. kfree(q_alg);
  4326. goto err;
  4327. }
  4328. }
  4329. rc = crypto_register_skcipher(&q_alg->cipher_alg);
  4330. if (rc) {
  4331. dev_err(&pdev->dev, "%s alg registration failed\n",
  4332. q_alg->cipher_alg.base.cra_driver_name);
  4333. kfree_sensitive(q_alg);
  4334. } else {
  4335. list_add_tail(&q_alg->entry, &cp->alg_list);
  4336. dev_info(&pdev->dev, "%s\n",
  4337. q_alg->cipher_alg.base.cra_driver_name);
  4338. }
  4339. }
  4340. /* register crypto cipher algorithms the device supports */
  4341. if (cp->ce_support.aes_xts) {
  4342. struct qcrypto_alg *q_alg;
  4343. q_alg = _qcrypto_cipher_alg_alloc(cp,
  4344. &_qcrypto_sk_cipher_xts_algo);
  4345. if (IS_ERR(q_alg)) {
  4346. rc = PTR_ERR(q_alg);
  4347. goto err;
  4348. }
  4349. if (cp->ce_support.use_sw_aes_xts_algo) {
  4350. rc = _qcrypto_prefix_alg_cra_name(
  4351. q_alg->cipher_alg.base.cra_name,
  4352. strlen(q_alg->cipher_alg.base.cra_name));
  4353. if (rc) {
  4354. dev_err(&pdev->dev,
  4355. "The algorithm name %s is too long.\n",
  4356. q_alg->cipher_alg.base.cra_name);
  4357. kfree(q_alg);
  4358. goto err;
  4359. }
  4360. }
  4361. rc = crypto_register_skcipher(&q_alg->cipher_alg);
  4362. if (rc) {
  4363. dev_err(&pdev->dev, "%s alg registration failed\n",
  4364. q_alg->cipher_alg.base.cra_driver_name);
  4365. kfree_sensitive(q_alg);
  4366. } else {
  4367. list_add_tail(&q_alg->entry, &cp->alg_list);
  4368. dev_info(&pdev->dev, "%s\n",
  4369. q_alg->cipher_alg.base.cra_driver_name);
  4370. }
  4371. }
  4372. /*
  4373. * Register crypto hash (sha1 and sha256) algorithms the
  4374. * device supports
  4375. */
  4376. for (i = 0; i < ARRAY_SIZE(_qcrypto_ahash_algos); i++) {
  4377. struct qcrypto_alg *q_alg = NULL;
  4378. q_alg = _qcrypto_sha_alg_alloc(cp, &_qcrypto_ahash_algos[i]);
  4379. if (IS_ERR(q_alg)) {
  4380. rc = PTR_ERR(q_alg);
  4381. goto err;
  4382. }
  4383. if (cp->ce_support.use_sw_ahash_algo) {
  4384. rc = _qcrypto_prefix_alg_cra_name(
  4385. q_alg->sha_alg.halg.base.cra_name,
  4386. strlen(q_alg->sha_alg.halg.base.cra_name));
  4387. if (rc) {
  4388. dev_err(&pdev->dev,
  4389. "The algorithm name %s is too long.\n",
  4390. q_alg->sha_alg.halg.base.cra_name);
  4391. kfree(q_alg);
  4392. goto err;
  4393. }
  4394. }
  4395. rc = crypto_register_ahash(&q_alg->sha_alg);
  4396. if (rc) {
  4397. dev_err(&pdev->dev, "%s alg registration failed\n",
  4398. q_alg->sha_alg.halg.base.cra_driver_name);
  4399. kfree_sensitive(q_alg);
  4400. } else {
  4401. list_add_tail(&q_alg->entry, &cp->alg_list);
  4402. dev_info(&pdev->dev, "%s\n",
  4403. q_alg->sha_alg.halg.base.cra_driver_name);
  4404. }
  4405. }
  4406. /* register crypto aead (hmac-sha1) algorithms the device supports */
  4407. if (cp->ce_support.sha1_hmac_20 || cp->ce_support.sha1_hmac
  4408. || cp->ce_support.sha_hmac) {
  4409. for (i = 0; i < ARRAY_SIZE(_qcrypto_aead_sha1_hmac_algos);
  4410. i++) {
  4411. struct qcrypto_alg *q_alg;
  4412. q_alg = _qcrypto_aead_alg_alloc(cp,
  4413. &_qcrypto_aead_sha1_hmac_algos[i]);
  4414. if (IS_ERR(q_alg)) {
  4415. rc = PTR_ERR(q_alg);
  4416. goto err;
  4417. }
  4418. if (cp->ce_support.use_sw_aead_algo) {
  4419. rc = _qcrypto_prefix_alg_cra_name(
  4420. q_alg->aead_alg.base.cra_name,
  4421. strlen(q_alg->aead_alg.base.cra_name));
  4422. if (rc) {
  4423. dev_err(&pdev->dev,
  4424. "The algorithm name %s is too long.\n",
  4425. q_alg->aead_alg.base.cra_name);
  4426. kfree(q_alg);
  4427. goto err;
  4428. }
  4429. }
  4430. rc = crypto_register_aead(&q_alg->aead_alg);
  4431. if (rc) {
  4432. dev_err(&pdev->dev,
  4433. "%s alg registration failed\n",
  4434. q_alg->aead_alg.base.cra_driver_name);
  4435. kfree(q_alg);
  4436. } else {
  4437. list_add_tail(&q_alg->entry, &cp->alg_list);
  4438. dev_info(&pdev->dev, "%s\n",
  4439. q_alg->aead_alg.base.cra_driver_name);
  4440. }
  4441. }
  4442. }
  4443. /* register crypto aead (hmac-sha256) algorithms the device supports */
  4444. if (cp->ce_support.sha_hmac) {
  4445. for (i = 0; i < ARRAY_SIZE(_qcrypto_aead_sha256_hmac_algos);
  4446. i++) {
  4447. struct qcrypto_alg *q_alg;
  4448. q_alg = _qcrypto_aead_alg_alloc(cp,
  4449. &_qcrypto_aead_sha256_hmac_algos[i]);
  4450. if (IS_ERR(q_alg)) {
  4451. rc = PTR_ERR(q_alg);
  4452. goto err;
  4453. }
  4454. if (cp->ce_support.use_sw_aead_algo) {
  4455. rc = _qcrypto_prefix_alg_cra_name(
  4456. q_alg->aead_alg.base.cra_name,
  4457. strlen(q_alg->aead_alg.base.cra_name));
  4458. if (rc) {
  4459. dev_err(&pdev->dev,
  4460. "The algorithm name %s is too long.\n",
  4461. q_alg->aead_alg.base.cra_name);
  4462. kfree(q_alg);
  4463. goto err;
  4464. }
  4465. }
  4466. rc = crypto_register_aead(&q_alg->aead_alg);
  4467. if (rc) {
  4468. dev_err(&pdev->dev,
  4469. "%s alg registration failed\n",
  4470. q_alg->aead_alg.base.cra_driver_name);
  4471. kfree(q_alg);
  4472. } else {
  4473. list_add_tail(&q_alg->entry, &cp->alg_list);
  4474. dev_info(&pdev->dev, "%s\n",
  4475. q_alg->aead_alg.base.cra_driver_name);
  4476. }
  4477. }
  4478. }
  4479. if ((cp->ce_support.sha_hmac) || (cp->platform_support.sha_hmac)) {
  4480. /* register crypto hmac algorithms the device supports */
  4481. for (i = 0; i < ARRAY_SIZE(_qcrypto_sha_hmac_algos); i++) {
  4482. struct qcrypto_alg *q_alg = NULL;
  4483. q_alg = _qcrypto_sha_alg_alloc(cp,
  4484. &_qcrypto_sha_hmac_algos[i]);
  4485. if (IS_ERR(q_alg)) {
  4486. rc = PTR_ERR(q_alg);
  4487. goto err;
  4488. }
  4489. if (cp->ce_support.use_sw_hmac_algo) {
  4490. rc = _qcrypto_prefix_alg_cra_name(
  4491. q_alg->sha_alg.halg.base.cra_name,
  4492. strlen(
  4493. q_alg->sha_alg.halg.base.cra_name));
  4494. if (rc) {
  4495. dev_err(&pdev->dev,
  4496. "The algorithm name %s is too long.\n",
  4497. q_alg->sha_alg.halg.base.cra_name);
  4498. kfree(q_alg);
  4499. goto err;
  4500. }
  4501. }
  4502. rc = crypto_register_ahash(&q_alg->sha_alg);
  4503. if (rc) {
  4504. dev_err(&pdev->dev,
  4505. "%s alg registration failed\n",
  4506. q_alg->sha_alg.halg.base.cra_driver_name);
  4507. kfree_sensitive(q_alg);
  4508. } else {
  4509. list_add_tail(&q_alg->entry, &cp->alg_list);
  4510. dev_info(&pdev->dev, "%s\n",
  4511. q_alg->sha_alg.halg.base.cra_driver_name);
  4512. }
  4513. }
  4514. }
  4515. /*
  4516. * Register crypto cipher (aes-ccm) algorithms the
  4517. * device supports
  4518. */
  4519. if (cp->ce_support.aes_ccm) {
  4520. struct qcrypto_alg *q_alg;
  4521. q_alg = _qcrypto_aead_alg_alloc(cp, &_qcrypto_aead_ccm_algo);
  4522. if (IS_ERR(q_alg)) {
  4523. rc = PTR_ERR(q_alg);
  4524. goto err;
  4525. }
  4526. if (cp->ce_support.use_sw_aes_ccm_algo) {
  4527. rc = _qcrypto_prefix_alg_cra_name(
  4528. q_alg->aead_alg.base.cra_name,
  4529. strlen(q_alg->aead_alg.base.cra_name));
  4530. if (rc) {
  4531. dev_err(&pdev->dev,
  4532. "The algorithm name %s is too long.\n",
  4533. q_alg->aead_alg.base.cra_name);
  4534. kfree(q_alg);
  4535. goto err;
  4536. }
  4537. }
  4538. rc = crypto_register_aead(&q_alg->aead_alg);
  4539. if (rc) {
  4540. dev_err(&pdev->dev, "%s alg registration failed\n",
  4541. q_alg->aead_alg.base.cra_driver_name);
  4542. kfree_sensitive(q_alg);
  4543. } else {
  4544. list_add_tail(&q_alg->entry, &cp->alg_list);
  4545. dev_info(&pdev->dev, "%s\n",
  4546. q_alg->aead_alg.base.cra_driver_name);
  4547. }
  4548. q_alg = _qcrypto_aead_alg_alloc(cp,
  4549. &_qcrypto_aead_rfc4309_ccm_algo);
  4550. if (IS_ERR(q_alg)) {
  4551. rc = PTR_ERR(q_alg);
  4552. goto err;
  4553. }
  4554. if (cp->ce_support.use_sw_aes_ccm_algo) {
  4555. rc = _qcrypto_prefix_alg_cra_name(
  4556. q_alg->aead_alg.base.cra_name,
  4557. strlen(q_alg->aead_alg.base.cra_name));
  4558. if (rc) {
  4559. dev_err(&pdev->dev,
  4560. "The algorithm name %s is too long.\n",
  4561. q_alg->aead_alg.base.cra_name);
  4562. kfree(q_alg);
  4563. goto err;
  4564. }
  4565. }
  4566. rc = crypto_register_aead(&q_alg->aead_alg);
  4567. if (rc) {
  4568. dev_err(&pdev->dev, "%s alg registration failed\n",
  4569. q_alg->aead_alg.base.cra_driver_name);
  4570. kfree(q_alg);
  4571. } else {
  4572. list_add_tail(&q_alg->entry, &cp->alg_list);
  4573. dev_info(&pdev->dev, "%s\n",
  4574. q_alg->aead_alg.base.cra_driver_name);
  4575. }
  4576. }
  4577. mutex_unlock(&cp->engine_lock);
  4578. return 0;
  4579. err:
  4580. _qcrypto_remove_engine(pengine);
  4581. kfree_sensitive(pqcrypto_req_control);
  4582. exit_unlock_mutex:
  4583. mutex_unlock(&cp->engine_lock);
  4584. exit_qce_close:
  4585. if (pengine->qce)
  4586. qce_close(pengine->qce);
  4587. exit_free_pdata:
  4588. icc_set_bw(pengine->icc_path, 0, 0);
  4589. platform_set_drvdata(pdev, NULL);
  4590. exit_kzfree:
  4591. memset(pengine, 0, ksize((void *)pengine));
  4592. kfree(pengine);
  4593. return rc;
  4594. }
  4595. static int _qcrypto_engine_in_use(struct crypto_engine *pengine)
  4596. {
  4597. struct crypto_priv *cp = pengine->pcp;
  4598. if ((atomic_read(&pengine->req_count) > 0) || pengine->req_queue.qlen
  4599. || cp->req_queue.qlen)
  4600. return 1;
  4601. return 0;
  4602. }
  4603. static void _qcrypto_do_suspending(struct crypto_engine *pengine)
  4604. {
  4605. del_timer_sync(&pengine->bw_reaper_timer);
  4606. qcrypto_ce_set_bus(pengine, false);
  4607. }
  4608. static int _qcrypto_suspend(struct platform_device *pdev, pm_message_t state)
  4609. {
  4610. int ret = 0;
  4611. struct crypto_engine *pengine;
  4612. struct crypto_priv *cp;
  4613. unsigned long flags;
  4614. pengine = platform_get_drvdata(pdev);
  4615. if (!pengine)
  4616. return -EINVAL;
  4617. /*
  4618. * Check if this platform supports clock management in suspend/resume
  4619. * If not, just simply return 0.
  4620. */
  4621. cp = pengine->pcp;
  4622. if (!cp->ce_support.clk_mgmt_sus_res)
  4623. return 0;
  4624. spin_lock_irqsave(&cp->lock, flags);
  4625. switch (pengine->bw_state) {
  4626. case BUS_NO_BANDWIDTH:
  4627. if (!pengine->high_bw_req)
  4628. pengine->bw_state = BUS_SUSPENDED;
  4629. else
  4630. ret = -EBUSY;
  4631. break;
  4632. case BUS_HAS_BANDWIDTH:
  4633. if (_qcrypto_engine_in_use(pengine)) {
  4634. ret = -EBUSY;
  4635. } else {
  4636. pengine->bw_state = BUS_SUSPENDING;
  4637. spin_unlock_irqrestore(&cp->lock, flags);
  4638. _qcrypto_do_suspending(pengine);
  4639. spin_lock_irqsave(&cp->lock, flags);
  4640. pengine->bw_state = BUS_SUSPENDED;
  4641. }
  4642. break;
  4643. case BUS_BANDWIDTH_RELEASING:
  4644. case BUS_BANDWIDTH_ALLOCATING:
  4645. case BUS_SUSPENDED:
  4646. case BUS_SUSPENDING:
  4647. default:
  4648. ret = -EBUSY;
  4649. break;
  4650. }
  4651. spin_unlock_irqrestore(&cp->lock, flags);
  4652. if (ret)
  4653. return ret;
  4654. if (qce_pm_table.suspend) {
  4655. qcrypto_ce_set_bus(pengine, true);
  4656. qce_pm_table.suspend(pengine->qce);
  4657. qcrypto_ce_set_bus(pengine, false);
  4658. }
  4659. return 0;
  4660. }
  4661. static int _qcrypto_resume(struct platform_device *pdev)
  4662. {
  4663. struct crypto_engine *pengine;
  4664. struct crypto_priv *cp;
  4665. unsigned long flags;
  4666. int ret = 0;
  4667. pengine = platform_get_drvdata(pdev);
  4668. if (!pengine)
  4669. return -EINVAL;
  4670. cp = pengine->pcp;
  4671. if (!cp->ce_support.clk_mgmt_sus_res)
  4672. return 0;
  4673. spin_lock_irqsave(&cp->lock, flags);
  4674. if (pengine->bw_state == BUS_SUSPENDED) {
  4675. spin_unlock_irqrestore(&cp->lock, flags);
  4676. if (qce_pm_table.resume) {
  4677. qcrypto_ce_set_bus(pengine, true);
  4678. qce_pm_table.resume(pengine->qce);
  4679. qcrypto_ce_set_bus(pengine, false);
  4680. }
  4681. spin_lock_irqsave(&cp->lock, flags);
  4682. pengine->bw_state = BUS_NO_BANDWIDTH;
  4683. pengine->active_seq++;
  4684. pengine->check_flag = false;
  4685. if (cp->req_queue.qlen || pengine->req_queue.qlen) {
  4686. if (!pengine->high_bw_req) {
  4687. qcrypto_ce_bw_allocate_req(pengine);
  4688. pengine->high_bw_req = true;
  4689. }
  4690. }
  4691. } else
  4692. ret = -EBUSY;
  4693. spin_unlock_irqrestore(&cp->lock, flags);
  4694. return ret;
  4695. }
  4696. static const struct of_device_id qcrypto_match[] = {
  4697. {.compatible = "qcom,qcrypto",},
  4698. {}
  4699. };
  4700. static struct platform_driver __qcrypto = {
  4701. .probe = _qcrypto_probe,
  4702. .remove = _qcrypto_remove,
  4703. .suspend = _qcrypto_suspend,
  4704. .resume = _qcrypto_resume,
  4705. .driver = {
  4706. .name = "qcrypto",
  4707. .of_match_table = qcrypto_match,
  4708. },
  4709. };
  4710. static int _debug_qcrypto;
  4711. static ssize_t _debug_stats_read(struct file *file, char __user *buf,
  4712. size_t count, loff_t *ppos)
  4713. {
  4714. int rc = -EINVAL;
  4715. int qcrypto = *((int *) file->private_data);
  4716. int len;
  4717. len = _disp_stats(qcrypto);
  4718. if (len <= count)
  4719. rc = simple_read_from_buffer((void __user *) buf, len,
  4720. ppos, (void *) _debug_read_buf, len);
  4721. return rc;
  4722. }
  4723. static ssize_t _debug_stats_write(struct file *file, const char __user *buf,
  4724. size_t count, loff_t *ppos)
  4725. {
  4726. unsigned long flags;
  4727. struct crypto_priv *cp = &qcrypto_dev;
  4728. struct crypto_engine *pe;
  4729. int i;
  4730. memset((char *)&_qcrypto_stat, 0, sizeof(struct crypto_stat));
  4731. spin_lock_irqsave(&cp->lock, flags);
  4732. list_for_each_entry(pe, &cp->engine_list, elist) {
  4733. pe->total_req = 0;
  4734. pe->err_req = 0;
  4735. qce_clear_driver_stats(pe->qce);
  4736. pe->max_req_used = 0;
  4737. }
  4738. cp->max_qlen = 0;
  4739. cp->resp_start = 0;
  4740. cp->resp_stop = 0;
  4741. cp->no_avail = 0;
  4742. cp->max_resp_qlen = 0;
  4743. cp->queue_work_eng3 = 0;
  4744. cp->queue_work_not_eng3 = 0;
  4745. cp->queue_work_not_eng3_nz = 0;
  4746. cp->max_reorder_cnt = 0;
  4747. for (i = 0; i < MAX_SMP_CPU + 1; i++)
  4748. cp->cpu_req[i] = 0;
  4749. spin_unlock_irqrestore(&cp->lock, flags);
  4750. return count;
  4751. }
  4752. static const struct file_operations _debug_stats_ops = {
  4753. .open = simple_open,
  4754. .read = _debug_stats_read,
  4755. .write = _debug_stats_write,
  4756. };
  4757. static int _qcrypto_debug_init(void)
  4758. {
  4759. int rc;
  4760. char name[DEBUG_MAX_FNAME];
  4761. struct dentry *dent;
  4762. _debug_dent = debugfs_create_dir("qcrypto", NULL);
  4763. if (IS_ERR(_debug_dent)) {
  4764. pr_debug("qcrypto debugfs_create_dir fail, error %ld\n",
  4765. PTR_ERR(_debug_dent));
  4766. return PTR_ERR(_debug_dent);
  4767. }
  4768. snprintf(name, DEBUG_MAX_FNAME-1, "stats-%d", 1);
  4769. _debug_qcrypto = 0;
  4770. dent = debugfs_create_file(name, 0644, _debug_dent,
  4771. &_debug_qcrypto, &_debug_stats_ops);
  4772. if (dent == NULL) {
  4773. pr_debug("qcrypto debugfs_create_file fail, error %ld\n",
  4774. PTR_ERR(dent));
  4775. rc = PTR_ERR(dent);
  4776. goto err;
  4777. }
  4778. return 0;
  4779. err:
  4780. debugfs_remove_recursive(_debug_dent);
  4781. return rc;
  4782. }
  4783. static int __init _qcrypto_init(void)
  4784. {
  4785. struct crypto_priv *pcp = &qcrypto_dev;
  4786. _qcrypto_debug_init();
  4787. INIT_LIST_HEAD(&pcp->alg_list);
  4788. INIT_LIST_HEAD(&pcp->engine_list);
  4789. init_llist_head(&pcp->ordered_resp_list);
  4790. spin_lock_init(&pcp->lock);
  4791. mutex_init(&pcp->engine_lock);
  4792. pcp->resp_wq = alloc_workqueue("qcrypto_seq_response_wq",
  4793. WQ_MEM_RECLAIM | WQ_HIGHPRI | WQ_CPU_INTENSIVE, 1);
  4794. if (!pcp->resp_wq) {
  4795. pr_err("Error allocating workqueue\n");
  4796. return -ENOMEM;
  4797. }
  4798. INIT_WORK(&pcp->resp_work, seq_response);
  4799. pcp->total_units = 0;
  4800. pcp->next_engine = NULL;
  4801. pcp->scheduled_eng = NULL;
  4802. pcp->ce_req_proc_sts = IN_PROGRESS;
  4803. crypto_init_queue(&pcp->req_queue, MSM_QCRYPTO_REQ_QUEUE_LENGTH);
  4804. return platform_driver_register(&__qcrypto);
  4805. }
  4806. static void __exit _qcrypto_exit(void)
  4807. {
  4808. pr_debug("%s Unregister QCRYPTO\n", __func__);
  4809. debugfs_remove_recursive(_debug_dent);
  4810. platform_driver_unregister(&__qcrypto);
  4811. }
  4812. module_init(_qcrypto_init);
  4813. module_exit(_qcrypto_exit);
  4814. MODULE_LICENSE("GPL v2");
  4815. MODULE_DESCRIPTION("QTI Crypto driver");