qce50.c 159 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549
  1. /* Qualcomm Crypto Engine driver.
  2. *
  3. * Copyright (c) 2012-2014, The Linux Foundation. All rights reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 and
  7. * only version 2 as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #define pr_fmt(fmt) "QCE50: %s: " fmt, __func__
  15. #include <linux/types.h>
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/mod_devicetable.h>
  19. #include <linux/device.h>
  20. #include <linux/clk.h>
  21. #include <linux/err.h>
  22. #include <linux/dma-mapping.h>
  23. #include <linux/io.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/spinlock.h>
  26. #include <linux/delay.h>
  27. #include <linux/crypto.h>
  28. #include <linux/qcedev.h>
  29. #include <linux/bitops.h>
  30. #include <crypto/hash.h>
  31. #include <crypto/sha.h>
  32. #include <mach/dma.h>
  33. #include <mach/clk.h>
  34. #include <mach/socinfo.h>
  35. #include <mach/qcrypto.h>
  36. #include "qce.h"
  37. #include "qce50.h"
  38. #include "qcryptohw_50.h"
  39. #include "qce_ota.h"
  40. #define CRYPTO_CONFIG_RESET 0xE001F
  41. #define QCE_MAX_NUM_DSCR 0x500
  42. #define QCE_SECTOR_SIZE 0x200
  43. static DEFINE_MUTEX(bam_register_lock);
  44. struct bam_registration_info {
  45. struct list_head qlist;
  46. uint32_t handle;
  47. uint32_t cnt;
  48. uint32_t bam_mem;
  49. void __iomem *bam_iobase;
  50. bool support_cmd_dscr;
  51. };
  52. static LIST_HEAD(qce50_bam_list);
  53. /*
  54. * CE HW device structure.
  55. * Each engine has an instance of the structure.
  56. * Each engine can only handle one crypto operation at one time. It is up to
  57. * the sw above to ensure single threading of operation on an engine.
  58. */
  59. struct qce_device {
  60. struct device *pdev; /* Handle to platform_device structure */
  61. struct bam_registration_info *pbam;
  62. unsigned char *coh_vmem; /* Allocated coherent virtual memory */
  63. dma_addr_t coh_pmem; /* Allocated coherent physical memory */
  64. int memsize; /* Memory allocated */
  65. uint32_t bam_mem; /* bam physical address, from DT */
  66. uint32_t bam_mem_size; /* bam io size, from DT */
  67. int is_shared; /* CE HW is shared */
  68. bool support_cmd_dscr;
  69. bool support_hw_key;
  70. bool support_clk_mgmt_sus_res;
  71. void __iomem *iobase; /* Virtual io base of CE HW */
  72. unsigned int phy_iobase; /* Physical io base of CE HW */
  73. struct clk *ce_core_src_clk; /* Handle to CE src clk*/
  74. struct clk *ce_core_clk; /* Handle to CE clk */
  75. struct clk *ce_clk; /* Handle to CE clk */
  76. struct clk *ce_bus_clk; /* Handle to CE AXI clk*/
  77. qce_comp_func_ptr_t qce_cb; /* qce callback function pointer */
  78. int assoc_nents;
  79. int ivsize;
  80. int authsize;
  81. int src_nents;
  82. int dst_nents;
  83. dma_addr_t phy_iv_in;
  84. unsigned char dec_iv[16];
  85. int dir;
  86. void *areq;
  87. enum qce_cipher_mode_enum mode;
  88. struct qce_ce_cfg_reg_setting reg;
  89. struct ce_sps_data ce_sps;
  90. uint32_t engines_avail;
  91. dma_addr_t phy_ota_src;
  92. dma_addr_t phy_ota_dst;
  93. unsigned int ota_size;
  94. bool use_sw_aes_cbc_ecb_ctr_algo;
  95. bool use_sw_aead_algo;
  96. bool use_sw_aes_xts_algo;
  97. bool use_sw_ahash_algo;
  98. bool use_sw_hmac_algo;
  99. bool use_sw_aes_ccm_algo;
  100. };
  101. /* Standard initialization vector for SHA-1, source: FIPS 180-2 */
  102. static uint32_t _std_init_vector_sha1[] = {
  103. 0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0
  104. };
  105. /* Standard initialization vector for SHA-256, source: FIPS 180-2 */
  106. static uint32_t _std_init_vector_sha256[] = {
  107. 0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A,
  108. 0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19
  109. };
  110. static void _byte_stream_to_net_words(uint32_t *iv, unsigned char *b,
  111. unsigned int len)
  112. {
  113. unsigned n;
  114. n = len / sizeof(uint32_t);
  115. for (; n > 0; n--) {
  116. *iv = ((*b << 24) & 0xff000000) |
  117. (((*(b+1)) << 16) & 0xff0000) |
  118. (((*(b+2)) << 8) & 0xff00) |
  119. (*(b+3) & 0xff);
  120. b += sizeof(uint32_t);
  121. iv++;
  122. }
  123. n = len % sizeof(uint32_t);
  124. if (n == 3) {
  125. *iv = ((*b << 24) & 0xff000000) |
  126. (((*(b+1)) << 16) & 0xff0000) |
  127. (((*(b+2)) << 8) & 0xff00);
  128. } else if (n == 2) {
  129. *iv = ((*b << 24) & 0xff000000) |
  130. (((*(b+1)) << 16) & 0xff0000);
  131. } else if (n == 1) {
  132. *iv = ((*b << 24) & 0xff000000);
  133. }
  134. }
  135. static void _byte_stream_swap_to_net_words(uint32_t *iv, unsigned char *b,
  136. unsigned int len)
  137. {
  138. unsigned i, j;
  139. unsigned char swap_iv[AES_IV_LENGTH];
  140. memset(swap_iv, 0, AES_IV_LENGTH);
  141. for (i = (AES_IV_LENGTH-len), j = len-1; i < AES_IV_LENGTH; i++, j--)
  142. swap_iv[i] = b[j];
  143. _byte_stream_to_net_words(iv, swap_iv, AES_IV_LENGTH);
  144. }
  145. static int count_sg(struct scatterlist *sg, int nbytes)
  146. {
  147. int i;
  148. for (i = 0; nbytes > 0; i++, sg = scatterwalk_sg_next(sg))
  149. nbytes -= sg->length;
  150. return i;
  151. }
  152. static int qce_dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
  153. enum dma_data_direction direction)
  154. {
  155. int i;
  156. for (i = 0; i < nents; ++i) {
  157. dma_map_sg(dev, sg, 1, direction);
  158. sg = scatterwalk_sg_next(sg);
  159. }
  160. return nents;
  161. }
  162. static int qce_dma_unmap_sg(struct device *dev, struct scatterlist *sg,
  163. int nents, enum dma_data_direction direction)
  164. {
  165. int i;
  166. for (i = 0; i < nents; ++i) {
  167. dma_unmap_sg(dev, sg, 1, direction);
  168. sg = scatterwalk_sg_next(sg);
  169. }
  170. return nents;
  171. }
  172. static int _probe_ce_engine(struct qce_device *pce_dev)
  173. {
  174. unsigned int rev;
  175. unsigned int maj_rev, min_rev, step_rev;
  176. rev = readl_relaxed(pce_dev->iobase + CRYPTO_VERSION_REG);
  177. mb();
  178. maj_rev = (rev & CRYPTO_CORE_MAJOR_REV_MASK) >> CRYPTO_CORE_MAJOR_REV;
  179. min_rev = (rev & CRYPTO_CORE_MINOR_REV_MASK) >> CRYPTO_CORE_MINOR_REV;
  180. step_rev = (rev & CRYPTO_CORE_STEP_REV_MASK) >> CRYPTO_CORE_STEP_REV;
  181. if (maj_rev != 0x05) {
  182. pr_err("Unknown Qualcomm crypto device at 0x%x, rev %d.%d.%d\n",
  183. pce_dev->phy_iobase, maj_rev, min_rev, step_rev);
  184. return -EIO;
  185. };
  186. pce_dev->ce_sps.minor_version = min_rev;
  187. pce_dev->engines_avail = readl_relaxed(pce_dev->iobase +
  188. CRYPTO_ENGINES_AVAIL);
  189. dev_info(pce_dev->pdev, "Qualcomm Crypto %d.%d.%d device found @0x%x\n",
  190. maj_rev, min_rev, step_rev, pce_dev->phy_iobase);
  191. pce_dev->ce_sps.ce_burst_size = MAX_CE_BAM_BURST_SIZE;
  192. dev_info(pce_dev->pdev,
  193. "CE device = 0x%x\n, "
  194. "IO base, CE = 0x%x\n, "
  195. "Consumer (IN) PIPE %d, "
  196. "Producer (OUT) PIPE %d\n"
  197. "IO base BAM = 0x%x\n"
  198. "BAM IRQ %d\n"
  199. "Engines Availability = 0x%x\n",
  200. (uint32_t) pce_dev->ce_sps.ce_device,
  201. (uint32_t) pce_dev->iobase,
  202. pce_dev->ce_sps.dest_pipe_index,
  203. pce_dev->ce_sps.src_pipe_index,
  204. (uint32_t)pce_dev->ce_sps.bam_iobase,
  205. pce_dev->ce_sps.bam_irq,
  206. pce_dev->engines_avail);
  207. return 0;
  208. };
  209. static int _ce_get_hash_cmdlistinfo(struct qce_device *pce_dev,
  210. struct qce_sha_req *sreq,
  211. struct qce_cmdlist_info **cmdplistinfo)
  212. {
  213. struct qce_cmdlistptr_ops *cmdlistptr = &pce_dev->ce_sps.cmdlistptr;
  214. switch (sreq->alg) {
  215. case QCE_HASH_SHA1:
  216. *cmdplistinfo = &cmdlistptr->auth_sha1;
  217. break;
  218. case QCE_HASH_SHA256:
  219. *cmdplistinfo = &cmdlistptr->auth_sha256;
  220. break;
  221. case QCE_HASH_SHA1_HMAC:
  222. *cmdplistinfo = &cmdlistptr->auth_sha1_hmac;
  223. break;
  224. case QCE_HASH_SHA256_HMAC:
  225. *cmdplistinfo = &cmdlistptr->auth_sha256_hmac;
  226. break;
  227. case QCE_HASH_AES_CMAC:
  228. if (sreq->authklen == AES128_KEY_SIZE)
  229. *cmdplistinfo = &cmdlistptr->auth_aes_128_cmac;
  230. else
  231. *cmdplistinfo = &cmdlistptr->auth_aes_256_cmac;
  232. break;
  233. default:
  234. break;
  235. }
  236. return 0;
  237. }
  238. static int _ce_setup_hash(struct qce_device *pce_dev,
  239. struct qce_sha_req *sreq,
  240. struct qce_cmdlist_info *cmdlistinfo)
  241. {
  242. uint32_t auth32[SHA256_DIGEST_SIZE / sizeof(uint32_t)];
  243. uint32_t diglen;
  244. int i;
  245. uint32_t mackey32[SHA_HMAC_KEY_SIZE/sizeof(uint32_t)] = {
  246. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  247. bool sha1 = false;
  248. struct sps_command_element *pce = NULL;
  249. bool use_hw_key = false;
  250. bool use_pipe_key = false;
  251. uint32_t authk_size_in_word = sreq->authklen/sizeof(uint32_t);
  252. uint32_t auth_cfg;
  253. if ((sreq->alg == QCE_HASH_SHA1_HMAC) ||
  254. (sreq->alg == QCE_HASH_SHA256_HMAC) ||
  255. (sreq->alg == QCE_HASH_AES_CMAC)) {
  256. /* no more check for null key. use flag */
  257. if ((sreq->flags & QCRYPTO_CTX_USE_HW_KEY)
  258. == QCRYPTO_CTX_USE_HW_KEY)
  259. use_hw_key = true;
  260. else if ((sreq->flags & QCRYPTO_CTX_USE_PIPE_KEY) ==
  261. QCRYPTO_CTX_USE_PIPE_KEY)
  262. use_pipe_key = true;
  263. pce = cmdlistinfo->go_proc;
  264. if (use_hw_key == true) {
  265. pce->addr = (uint32_t)(CRYPTO_GOPROC_QC_KEY_REG +
  266. pce_dev->phy_iobase);
  267. } else {
  268. pce->addr = (uint32_t)(CRYPTO_GOPROC_REG +
  269. pce_dev->phy_iobase);
  270. pce = cmdlistinfo->auth_key;
  271. if (use_pipe_key == false) {
  272. _byte_stream_to_net_words(mackey32,
  273. sreq->authkey,
  274. sreq->authklen);
  275. for (i = 0; i < authk_size_in_word; i++, pce++)
  276. pce->data = mackey32[i];
  277. }
  278. }
  279. }
  280. if (sreq->alg == QCE_HASH_AES_CMAC)
  281. goto go_proc;
  282. /* if not the last, the size has to be on the block boundary */
  283. if (sreq->last_blk == 0 && (sreq->size % SHA256_BLOCK_SIZE))
  284. return -EIO;
  285. switch (sreq->alg) {
  286. case QCE_HASH_SHA1:
  287. case QCE_HASH_SHA1_HMAC:
  288. diglen = SHA1_DIGEST_SIZE;
  289. sha1 = true;
  290. break;
  291. case QCE_HASH_SHA256:
  292. case QCE_HASH_SHA256_HMAC:
  293. diglen = SHA256_DIGEST_SIZE;
  294. break;
  295. default:
  296. return -EINVAL;
  297. }
  298. /* write 20/32 bytes, 5/8 words into auth_iv for SHA1/SHA256 */
  299. if (sreq->first_blk) {
  300. if (sha1) {
  301. for (i = 0; i < 5; i++)
  302. auth32[i] = _std_init_vector_sha1[i];
  303. } else {
  304. for (i = 0; i < 8; i++)
  305. auth32[i] = _std_init_vector_sha256[i];
  306. }
  307. } else {
  308. _byte_stream_to_net_words(auth32, sreq->digest, diglen);
  309. }
  310. pce = cmdlistinfo->auth_iv;
  311. for (i = 0; i < 5; i++, pce++)
  312. pce->data = auth32[i];
  313. if ((sreq->alg == QCE_HASH_SHA256) ||
  314. (sreq->alg == QCE_HASH_SHA256_HMAC)) {
  315. for (i = 5; i < 8; i++, pce++)
  316. pce->data = auth32[i];
  317. }
  318. /* write auth_bytecnt 0/1, start with 0 */
  319. pce = cmdlistinfo->auth_bytecount;
  320. for (i = 0; i < 2; i++, pce++)
  321. pce->data = sreq->auth_data[i];
  322. /* Set/reset last bit in CFG register */
  323. pce = cmdlistinfo->auth_seg_cfg;
  324. auth_cfg = pce->data & ~(1 << CRYPTO_LAST |
  325. 1 << CRYPTO_FIRST |
  326. 1 << CRYPTO_USE_PIPE_KEY_AUTH |
  327. 1 << CRYPTO_USE_HW_KEY_AUTH);
  328. if (sreq->last_blk)
  329. auth_cfg |= 1 << CRYPTO_LAST;
  330. if (sreq->first_blk)
  331. auth_cfg |= 1 << CRYPTO_FIRST;
  332. if (use_hw_key)
  333. auth_cfg |= 1 << CRYPTO_USE_HW_KEY_AUTH;
  334. if (use_pipe_key)
  335. auth_cfg |= 1 << CRYPTO_USE_PIPE_KEY_AUTH;
  336. pce->data = auth_cfg;
  337. go_proc:
  338. /* write auth seg size */
  339. pce = cmdlistinfo->auth_seg_size;
  340. pce->data = sreq->size;
  341. pce = cmdlistinfo->encr_seg_cfg;
  342. pce->data = 0;
  343. /* write auth seg size start*/
  344. pce = cmdlistinfo->auth_seg_start;
  345. pce->data = 0;
  346. /* write seg size */
  347. pce = cmdlistinfo->seg_size;
  348. pce->data = sreq->size;
  349. return 0;
  350. }
  351. static struct qce_cmdlist_info *_ce_get_aead_cmdlistinfo(
  352. struct qce_device *pce_dev, struct qce_req *creq)
  353. {
  354. switch (creq->alg) {
  355. case CIPHER_ALG_DES:
  356. switch (creq->mode) {
  357. case QCE_MODE_ECB:
  358. return &pce_dev->ce_sps.
  359. cmdlistptr.aead_hmac_sha1_ecb_des;
  360. break;
  361. case QCE_MODE_CBC:
  362. return &pce_dev->ce_sps.
  363. cmdlistptr.aead_hmac_sha1_cbc_des;
  364. break;
  365. default:
  366. return NULL;
  367. }
  368. break;
  369. case CIPHER_ALG_3DES:
  370. switch (creq->mode) {
  371. case QCE_MODE_ECB:
  372. return &pce_dev->ce_sps.
  373. cmdlistptr.aead_hmac_sha1_ecb_3des;
  374. break;
  375. case QCE_MODE_CBC:
  376. return &pce_dev->ce_sps.
  377. cmdlistptr.aead_hmac_sha1_cbc_3des;
  378. break;
  379. default:
  380. return NULL;
  381. }
  382. break;
  383. case CIPHER_ALG_AES:
  384. switch (creq->mode) {
  385. case QCE_MODE_ECB:
  386. if (creq->encklen == AES128_KEY_SIZE)
  387. return &pce_dev->ce_sps.
  388. cmdlistptr.aead_hmac_sha1_ecb_aes_128;
  389. else if (creq->encklen == AES256_KEY_SIZE)
  390. return &pce_dev->ce_sps.
  391. cmdlistptr.aead_hmac_sha1_ecb_aes_256;
  392. else
  393. return NULL;
  394. break;
  395. case QCE_MODE_CBC:
  396. if (creq->encklen == AES128_KEY_SIZE)
  397. return &pce_dev->ce_sps.
  398. cmdlistptr.aead_hmac_sha1_cbc_aes_128;
  399. else if (creq->encklen == AES256_KEY_SIZE)
  400. return &pce_dev->ce_sps.
  401. cmdlistptr.aead_hmac_sha1_cbc_aes_256;
  402. else
  403. return NULL;
  404. break;
  405. default:
  406. return NULL;
  407. }
  408. break;
  409. default:
  410. return NULL;
  411. }
  412. return NULL;
  413. }
  414. static int _ce_setup_aead(struct qce_device *pce_dev, struct qce_req *q_req,
  415. uint32_t totallen_in, uint32_t coffset,
  416. struct qce_cmdlist_info *cmdlistinfo)
  417. {
  418. int32_t authk_size_in_word = SHA_HMAC_KEY_SIZE/sizeof(uint32_t);
  419. int i;
  420. uint32_t mackey32[SHA_HMAC_KEY_SIZE/sizeof(uint32_t)] = {0};
  421. struct sps_command_element *pce;
  422. uint32_t a_cfg;
  423. uint32_t enckey32[(MAX_CIPHER_KEY_SIZE*2)/sizeof(uint32_t)] = {0};
  424. uint32_t enciv32[MAX_IV_LENGTH/sizeof(uint32_t)] = {0};
  425. uint32_t enck_size_in_word = 0;
  426. uint32_t enciv_in_word;
  427. uint32_t key_size;
  428. uint32_t encr_cfg = 0;
  429. uint32_t ivsize = q_req->ivsize;
  430. key_size = q_req->encklen;
  431. enck_size_in_word = key_size/sizeof(uint32_t);
  432. switch (q_req->alg) {
  433. case CIPHER_ALG_DES:
  434. enciv_in_word = 2;
  435. break;
  436. case CIPHER_ALG_3DES:
  437. enciv_in_word = 2;
  438. break;
  439. case CIPHER_ALG_AES:
  440. if ((key_size != AES128_KEY_SIZE) &&
  441. (key_size != AES256_KEY_SIZE))
  442. return -EINVAL;
  443. enciv_in_word = 4;
  444. break;
  445. default:
  446. return -EINVAL;
  447. }
  448. switch (q_req->mode) {
  449. case QCE_MODE_ECB:
  450. case QCE_MODE_CBC:
  451. case QCE_MODE_CTR:
  452. pce_dev->mode = q_req->mode;
  453. break;
  454. default:
  455. return -EINVAL;
  456. }
  457. if (q_req->mode != QCE_MODE_ECB) {
  458. _byte_stream_to_net_words(enciv32, q_req->iv, ivsize);
  459. pce = cmdlistinfo->encr_cntr_iv;
  460. for (i = 0; i < enciv_in_word; i++, pce++)
  461. pce->data = enciv32[i];
  462. }
  463. /*
  464. * write encr key
  465. * do not use hw key or pipe key
  466. */
  467. _byte_stream_to_net_words(enckey32, q_req->enckey, key_size);
  468. pce = cmdlistinfo->encr_key;
  469. for (i = 0; i < enck_size_in_word; i++, pce++)
  470. pce->data = enckey32[i];
  471. /* write encr seg cfg */
  472. pce = cmdlistinfo->encr_seg_cfg;
  473. encr_cfg = pce->data;
  474. if (q_req->dir == QCE_ENCRYPT)
  475. encr_cfg |= (1 << CRYPTO_ENCODE);
  476. else
  477. encr_cfg &= ~(1 << CRYPTO_ENCODE);
  478. pce->data = encr_cfg;
  479. /* we only support sha1-hmac at this point */
  480. _byte_stream_to_net_words(mackey32, q_req->authkey,
  481. q_req->authklen);
  482. pce = cmdlistinfo->auth_key;
  483. for (i = 0; i < authk_size_in_word; i++, pce++)
  484. pce->data = mackey32[i];
  485. pce = cmdlistinfo->auth_iv;
  486. for (i = 0; i < 5; i++, pce++)
  487. pce->data = _std_init_vector_sha1[i];
  488. /* write auth_bytecnt 0/1, start with 0 */
  489. pce = cmdlistinfo->auth_bytecount;
  490. for (i = 0; i < 2; i++, pce++)
  491. pce->data = 0;
  492. pce = cmdlistinfo->auth_seg_cfg;
  493. a_cfg = pce->data;
  494. a_cfg &= ~(CRYPTO_AUTH_POS_MASK);
  495. if (q_req->dir == QCE_ENCRYPT)
  496. a_cfg |= (CRYPTO_AUTH_POS_AFTER << CRYPTO_AUTH_POS);
  497. else
  498. a_cfg |= (CRYPTO_AUTH_POS_BEFORE << CRYPTO_AUTH_POS);
  499. pce->data = a_cfg;
  500. /* write auth seg size */
  501. pce = cmdlistinfo->auth_seg_size;
  502. pce->data = totallen_in;
  503. /* write auth seg size start*/
  504. pce = cmdlistinfo->auth_seg_start;
  505. pce->data = 0;
  506. /* write seg size */
  507. pce = cmdlistinfo->seg_size;
  508. pce->data = totallen_in;
  509. /* write encr seg size */
  510. pce = cmdlistinfo->encr_seg_size;
  511. pce->data = q_req->cryptlen;
  512. /* write encr seg start */
  513. pce = cmdlistinfo->encr_seg_start;
  514. pce->data = (coffset & 0xffff);
  515. return 0;
  516. };
  517. static int _ce_get_cipher_cmdlistinfo(struct qce_device *pce_dev,
  518. struct qce_req *creq,
  519. struct qce_cmdlist_info **cmdlistinfo)
  520. {
  521. struct qce_cmdlistptr_ops *cmdlistptr = &pce_dev->ce_sps.cmdlistptr;
  522. if (creq->alg != CIPHER_ALG_AES) {
  523. switch (creq->alg) {
  524. case CIPHER_ALG_DES:
  525. if (creq->mode == QCE_MODE_ECB)
  526. *cmdlistinfo = &cmdlistptr->cipher_des_ecb;
  527. else
  528. *cmdlistinfo = &cmdlistptr->cipher_des_cbc;
  529. break;
  530. case CIPHER_ALG_3DES:
  531. if (creq->mode == QCE_MODE_ECB)
  532. *cmdlistinfo =
  533. &cmdlistptr->cipher_3des_ecb;
  534. else
  535. *cmdlistinfo =
  536. &cmdlistptr->cipher_3des_cbc;
  537. break;
  538. default:
  539. break;
  540. }
  541. } else {
  542. switch (creq->mode) {
  543. case QCE_MODE_ECB:
  544. if (creq->encklen == AES128_KEY_SIZE)
  545. *cmdlistinfo = &cmdlistptr->cipher_aes_128_ecb;
  546. else
  547. *cmdlistinfo = &cmdlistptr->cipher_aes_256_ecb;
  548. break;
  549. case QCE_MODE_CBC:
  550. case QCE_MODE_CTR:
  551. if (creq->encklen == AES128_KEY_SIZE)
  552. *cmdlistinfo =
  553. &cmdlistptr->cipher_aes_128_cbc_ctr;
  554. else
  555. *cmdlistinfo =
  556. &cmdlistptr->cipher_aes_256_cbc_ctr;
  557. break;
  558. case QCE_MODE_XTS:
  559. if (creq->encklen/2 == AES128_KEY_SIZE)
  560. *cmdlistinfo = &cmdlistptr->cipher_aes_128_xts;
  561. else
  562. *cmdlistinfo = &cmdlistptr->cipher_aes_256_xts;
  563. break;
  564. case QCE_MODE_CCM:
  565. if (creq->encklen == AES128_KEY_SIZE)
  566. *cmdlistinfo = &cmdlistptr->aead_aes_128_ccm;
  567. else
  568. *cmdlistinfo = &cmdlistptr->aead_aes_256_ccm;
  569. break;
  570. default:
  571. break;
  572. }
  573. }
  574. return 0;
  575. }
  576. static int _ce_setup_cipher(struct qce_device *pce_dev, struct qce_req *creq,
  577. uint32_t totallen_in, uint32_t coffset,
  578. struct qce_cmdlist_info *cmdlistinfo)
  579. {
  580. uint32_t enckey32[(MAX_CIPHER_KEY_SIZE * 2)/sizeof(uint32_t)] = {
  581. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  582. uint32_t enciv32[MAX_IV_LENGTH / sizeof(uint32_t)] = {
  583. 0, 0, 0, 0};
  584. uint32_t enck_size_in_word = 0;
  585. uint32_t key_size;
  586. bool use_hw_key = false;
  587. bool use_pipe_key = false;
  588. uint32_t encr_cfg = 0;
  589. uint32_t ivsize = creq->ivsize;
  590. int i;
  591. struct sps_command_element *pce = NULL;
  592. if (creq->mode == QCE_MODE_XTS)
  593. key_size = creq->encklen/2;
  594. else
  595. key_size = creq->encklen;
  596. pce = cmdlistinfo->go_proc;
  597. if ((creq->flags & QCRYPTO_CTX_USE_HW_KEY) == QCRYPTO_CTX_USE_HW_KEY) {
  598. use_hw_key = true;
  599. } else {
  600. if ((creq->flags & QCRYPTO_CTX_USE_PIPE_KEY) ==
  601. QCRYPTO_CTX_USE_PIPE_KEY)
  602. use_pipe_key = true;
  603. }
  604. pce = cmdlistinfo->go_proc;
  605. if (use_hw_key == true)
  606. pce->addr = (uint32_t)(CRYPTO_GOPROC_QC_KEY_REG +
  607. pce_dev->phy_iobase);
  608. else
  609. pce->addr = (uint32_t)(CRYPTO_GOPROC_REG +
  610. pce_dev->phy_iobase);
  611. if ((use_pipe_key == false) && (use_hw_key == false)) {
  612. _byte_stream_to_net_words(enckey32, creq->enckey, key_size);
  613. enck_size_in_word = key_size/sizeof(uint32_t);
  614. }
  615. if ((creq->op == QCE_REQ_AEAD) && (creq->mode == QCE_MODE_CCM)) {
  616. uint32_t authklen32 = creq->encklen/sizeof(uint32_t);
  617. uint32_t noncelen32 = MAX_NONCE/sizeof(uint32_t);
  618. uint32_t nonce32[MAX_NONCE/sizeof(uint32_t)] = {0, 0, 0, 0};
  619. uint32_t auth_cfg = 0;
  620. /* write nonce */
  621. _byte_stream_to_net_words(nonce32, creq->nonce, MAX_NONCE);
  622. pce = cmdlistinfo->auth_nonce_info;
  623. for (i = 0; i < noncelen32; i++, pce++)
  624. pce->data = nonce32[i];
  625. if (creq->authklen == AES128_KEY_SIZE)
  626. auth_cfg = pce_dev->reg.auth_cfg_aes_ccm_128;
  627. else {
  628. if (creq->authklen == AES256_KEY_SIZE)
  629. auth_cfg = pce_dev->reg.auth_cfg_aes_ccm_256;
  630. }
  631. if (creq->dir == QCE_ENCRYPT)
  632. auth_cfg |= (CRYPTO_AUTH_POS_BEFORE << CRYPTO_AUTH_POS);
  633. else
  634. auth_cfg |= (CRYPTO_AUTH_POS_AFTER << CRYPTO_AUTH_POS);
  635. auth_cfg |= ((creq->authsize - 1) << CRYPTO_AUTH_SIZE);
  636. if (use_hw_key == true) {
  637. auth_cfg |= (1 << CRYPTO_USE_HW_KEY_AUTH);
  638. } else {
  639. auth_cfg &= ~(1 << CRYPTO_USE_HW_KEY_AUTH);
  640. /* write auth key */
  641. pce = cmdlistinfo->auth_key;
  642. for (i = 0; i < authklen32; i++, pce++)
  643. pce->data = enckey32[i];
  644. }
  645. pce = cmdlistinfo->auth_seg_cfg;
  646. pce->data = auth_cfg;
  647. pce = cmdlistinfo->auth_seg_size;
  648. if (creq->dir == QCE_ENCRYPT)
  649. pce->data = totallen_in;
  650. else
  651. pce->data = totallen_in - creq->authsize;
  652. pce = cmdlistinfo->auth_seg_start;
  653. pce->data = 0;
  654. } else {
  655. if (creq->op != QCE_REQ_AEAD) {
  656. pce = cmdlistinfo->auth_seg_cfg;
  657. pce->data = 0;
  658. }
  659. }
  660. switch (creq->mode) {
  661. case QCE_MODE_ECB:
  662. if (key_size == AES128_KEY_SIZE)
  663. encr_cfg = pce_dev->reg.encr_cfg_aes_ecb_128;
  664. else
  665. encr_cfg = pce_dev->reg.encr_cfg_aes_ecb_256;
  666. break;
  667. case QCE_MODE_CBC:
  668. if (key_size == AES128_KEY_SIZE)
  669. encr_cfg = pce_dev->reg.encr_cfg_aes_cbc_128;
  670. else
  671. encr_cfg = pce_dev->reg.encr_cfg_aes_cbc_256;
  672. break;
  673. case QCE_MODE_XTS:
  674. if (key_size == AES128_KEY_SIZE)
  675. encr_cfg = pce_dev->reg.encr_cfg_aes_xts_128;
  676. else
  677. encr_cfg = pce_dev->reg.encr_cfg_aes_xts_256;
  678. break;
  679. case QCE_MODE_CCM:
  680. if (key_size == AES128_KEY_SIZE)
  681. encr_cfg = pce_dev->reg.encr_cfg_aes_ccm_128;
  682. else
  683. encr_cfg = pce_dev->reg.encr_cfg_aes_ccm_256;
  684. encr_cfg |= (CRYPTO_ENCR_MODE_CCM << CRYPTO_ENCR_MODE) |
  685. (CRYPTO_LAST_CCM_XFR << CRYPTO_LAST_CCM);
  686. break;
  687. case QCE_MODE_CTR:
  688. default:
  689. if (key_size == AES128_KEY_SIZE)
  690. encr_cfg = pce_dev->reg.encr_cfg_aes_ctr_128;
  691. else
  692. encr_cfg = pce_dev->reg.encr_cfg_aes_ctr_256;
  693. break;
  694. }
  695. pce_dev->mode = creq->mode;
  696. switch (creq->alg) {
  697. case CIPHER_ALG_DES:
  698. if (creq->mode != QCE_MODE_ECB) {
  699. _byte_stream_to_net_words(enciv32, creq->iv, ivsize);
  700. pce = cmdlistinfo->encr_cntr_iv;
  701. pce->data = enciv32[0];
  702. pce++;
  703. pce->data = enciv32[1];
  704. }
  705. if (use_hw_key == false) {
  706. pce = cmdlistinfo->encr_key;
  707. pce->data = enckey32[0];
  708. pce++;
  709. pce->data = enckey32[1];
  710. }
  711. break;
  712. case CIPHER_ALG_3DES:
  713. if (creq->mode != QCE_MODE_ECB) {
  714. _byte_stream_to_net_words(enciv32, creq->iv, ivsize);
  715. pce = cmdlistinfo->encr_cntr_iv;
  716. pce->data = enciv32[0];
  717. pce++;
  718. pce->data = enciv32[1];
  719. }
  720. if (use_hw_key == false) {
  721. /* write encr key */
  722. pce = cmdlistinfo->encr_key;
  723. for (i = 0; i < 6; i++, pce++)
  724. pce->data = enckey32[i];
  725. }
  726. break;
  727. case CIPHER_ALG_AES:
  728. default:
  729. if (creq->mode == QCE_MODE_XTS) {
  730. uint32_t xtskey32[MAX_CIPHER_KEY_SIZE/sizeof(uint32_t)]
  731. = {0, 0, 0, 0, 0, 0, 0, 0};
  732. uint32_t xtsklen =
  733. creq->encklen/(2 * sizeof(uint32_t));
  734. if ((use_hw_key == false) && (use_pipe_key == false)) {
  735. _byte_stream_to_net_words(xtskey32,
  736. (creq->enckey + creq->encklen/2),
  737. creq->encklen/2);
  738. /* write xts encr key */
  739. pce = cmdlistinfo->encr_xts_key;
  740. for (i = 0; i < xtsklen; i++, pce++)
  741. pce->data = xtskey32[i];
  742. }
  743. /* write xts du size */
  744. pce = cmdlistinfo->encr_xts_du_size;
  745. switch (creq->flags & QCRYPTO_CTX_XTS_MASK) {
  746. case QCRYPTO_CTX_XTS_DU_SIZE_512B:
  747. pce->data = min((unsigned int)QCE_SECTOR_SIZE,
  748. creq->cryptlen);
  749. break;
  750. case QCRYPTO_CTX_XTS_DU_SIZE_1KB:
  751. pce->data =
  752. min((unsigned int)QCE_SECTOR_SIZE * 2,
  753. creq->cryptlen);
  754. break;
  755. default:
  756. pce->data = creq->cryptlen;
  757. break;
  758. }
  759. }
  760. if (creq->mode != QCE_MODE_ECB) {
  761. if (creq->mode == QCE_MODE_XTS)
  762. _byte_stream_swap_to_net_words(enciv32,
  763. creq->iv, ivsize);
  764. else
  765. _byte_stream_to_net_words(enciv32, creq->iv,
  766. ivsize);
  767. /* write encr cntr iv */
  768. pce = cmdlistinfo->encr_cntr_iv;
  769. for (i = 0; i < 4; i++, pce++)
  770. pce->data = enciv32[i];
  771. if (creq->mode == QCE_MODE_CCM) {
  772. /* write cntr iv for ccm */
  773. pce = cmdlistinfo->encr_ccm_cntr_iv;
  774. for (i = 0; i < 4; i++, pce++)
  775. pce->data = enciv32[i];
  776. /* update cntr_iv[3] by one */
  777. pce = cmdlistinfo->encr_cntr_iv;
  778. pce += 3;
  779. pce->data += 1;
  780. }
  781. }
  782. if (creq->op == QCE_REQ_ABLK_CIPHER_NO_KEY) {
  783. encr_cfg |= (CRYPTO_ENCR_KEY_SZ_AES128 <<
  784. CRYPTO_ENCR_KEY_SZ);
  785. } else {
  786. if (use_hw_key == false) {
  787. /* write encr key */
  788. pce = cmdlistinfo->encr_key;
  789. for (i = 0; i < enck_size_in_word; i++, pce++)
  790. pce->data = enckey32[i];
  791. }
  792. } /* else of if (creq->op == QCE_REQ_ABLK_CIPHER_NO_KEY) */
  793. break;
  794. } /* end of switch (creq->mode) */
  795. if (use_pipe_key)
  796. encr_cfg |= (CRYPTO_USE_PIPE_KEY_ENCR_ENABLED
  797. << CRYPTO_USE_PIPE_KEY_ENCR);
  798. /* write encr seg cfg */
  799. pce = cmdlistinfo->encr_seg_cfg;
  800. if ((creq->alg == CIPHER_ALG_DES) || (creq->alg == CIPHER_ALG_3DES)) {
  801. if (creq->dir == QCE_ENCRYPT)
  802. pce->data |= (1 << CRYPTO_ENCODE);
  803. else
  804. pce->data &= ~(1 << CRYPTO_ENCODE);
  805. encr_cfg = pce->data;
  806. } else {
  807. encr_cfg |=
  808. ((creq->dir == QCE_ENCRYPT) ? 1 : 0) << CRYPTO_ENCODE;
  809. }
  810. if (use_hw_key == true)
  811. encr_cfg |= (CRYPTO_USE_HW_KEY << CRYPTO_USE_HW_KEY_ENCR);
  812. else
  813. encr_cfg &= ~(CRYPTO_USE_HW_KEY << CRYPTO_USE_HW_KEY_ENCR);
  814. pce->data = encr_cfg;
  815. /* write encr seg size */
  816. pce = cmdlistinfo->encr_seg_size;
  817. if ((creq->mode == QCE_MODE_CCM) && (creq->dir == QCE_DECRYPT))
  818. pce->data = (creq->cryptlen + creq->authsize);
  819. else
  820. pce->data = creq->cryptlen;
  821. /* write encr seg start */
  822. pce = cmdlistinfo->encr_seg_start;
  823. pce->data = (coffset & 0xffff);
  824. /* write seg size */
  825. pce = cmdlistinfo->seg_size;
  826. pce->data = totallen_in;
  827. return 0;
  828. };
  829. static int _ce_f9_setup(struct qce_device *pce_dev, struct qce_f9_req *req,
  830. struct qce_cmdlist_info *cmdlistinfo)
  831. {
  832. uint32_t ikey32[OTA_KEY_SIZE/sizeof(uint32_t)];
  833. uint32_t key_size_in_word = OTA_KEY_SIZE/sizeof(uint32_t);
  834. uint32_t cfg;
  835. struct sps_command_element *pce;
  836. int i;
  837. switch (req->algorithm) {
  838. case QCE_OTA_ALGO_KASUMI:
  839. cfg = pce_dev->reg.auth_cfg_kasumi;
  840. break;
  841. case QCE_OTA_ALGO_SNOW3G:
  842. default:
  843. cfg = pce_dev->reg.auth_cfg_snow3g;
  844. break;
  845. };
  846. /* write key in CRYPTO_AUTH_IV0-3_REG */
  847. _byte_stream_to_net_words(ikey32, &req->ikey[0], OTA_KEY_SIZE);
  848. pce = cmdlistinfo->auth_iv;
  849. for (i = 0; i < key_size_in_word; i++, pce++)
  850. pce->data = ikey32[i];
  851. /* write last bits in CRYPTO_AUTH_IV4_REG */
  852. pce->data = req->last_bits;
  853. /* write fresh to CRYPTO_AUTH_BYTECNT0_REG */
  854. pce = cmdlistinfo->auth_bytecount;
  855. pce->data = req->fresh;
  856. /* write count-i to CRYPTO_AUTH_BYTECNT1_REG */
  857. pce++;
  858. pce->data = req->count_i;
  859. /* write auth seg cfg */
  860. pce = cmdlistinfo->auth_seg_cfg;
  861. if (req->direction == QCE_OTA_DIR_DOWNLINK)
  862. cfg |= BIT(CRYPTO_F9_DIRECTION);
  863. pce->data = cfg;
  864. /* write auth seg size */
  865. pce = cmdlistinfo->auth_seg_size;
  866. pce->data = req->msize;
  867. /* write auth seg start*/
  868. pce = cmdlistinfo->auth_seg_start;
  869. pce->data = 0;
  870. /* write seg size */
  871. pce = cmdlistinfo->seg_size;
  872. pce->data = req->msize;
  873. /* write go */
  874. pce = cmdlistinfo->go_proc;
  875. pce->addr = (uint32_t)(CRYPTO_GOPROC_REG + pce_dev->phy_iobase);
  876. return 0;
  877. }
  878. static int _ce_f8_setup(struct qce_device *pce_dev, struct qce_f8_req *req,
  879. bool key_stream_mode, uint16_t npkts, uint16_t cipher_offset,
  880. uint16_t cipher_size,
  881. struct qce_cmdlist_info *cmdlistinfo)
  882. {
  883. uint32_t ckey32[OTA_KEY_SIZE/sizeof(uint32_t)];
  884. uint32_t key_size_in_word = OTA_KEY_SIZE/sizeof(uint32_t);
  885. uint32_t cfg;
  886. struct sps_command_element *pce;
  887. int i;
  888. switch (req->algorithm) {
  889. case QCE_OTA_ALGO_KASUMI:
  890. cfg = pce_dev->reg.encr_cfg_kasumi;
  891. break;
  892. case QCE_OTA_ALGO_SNOW3G:
  893. default:
  894. cfg = pce_dev->reg.encr_cfg_snow3g;
  895. break;
  896. };
  897. /* write key */
  898. _byte_stream_to_net_words(ckey32, &req->ckey[0], OTA_KEY_SIZE);
  899. pce = cmdlistinfo->encr_key;
  900. for (i = 0; i < key_size_in_word; i++, pce++)
  901. pce->data = ckey32[i];
  902. /* write encr seg cfg */
  903. pce = cmdlistinfo->encr_seg_cfg;
  904. if (key_stream_mode)
  905. cfg |= BIT(CRYPTO_F8_KEYSTREAM_ENABLE);
  906. if (req->direction == QCE_OTA_DIR_DOWNLINK)
  907. cfg |= BIT(CRYPTO_F8_DIRECTION);
  908. pce->data = cfg;
  909. /* write encr seg start */
  910. pce = cmdlistinfo->encr_seg_start;
  911. pce->data = (cipher_offset & 0xffff);
  912. /* write encr seg size */
  913. pce = cmdlistinfo->encr_seg_size;
  914. pce->data = cipher_size;
  915. /* write seg size */
  916. pce = cmdlistinfo->seg_size;
  917. pce->data = req->data_len;
  918. /* write cntr0_iv0 for countC */
  919. pce = cmdlistinfo->encr_cntr_iv;
  920. pce->data = req->count_c;
  921. /* write cntr1_iv1 for nPkts, and bearer */
  922. pce++;
  923. if (npkts == 1)
  924. npkts = 0;
  925. pce->data = req->bearer << CRYPTO_CNTR1_IV1_REG_F8_BEARER |
  926. npkts << CRYPTO_CNTR1_IV1_REG_F8_PKT_CNT;
  927. /* write go */
  928. pce = cmdlistinfo->go_proc;
  929. pce->addr = (uint32_t)(CRYPTO_GOPROC_REG + pce_dev->phy_iobase);
  930. return 0;
  931. }
  932. static int _ce_setup_hash_direct(struct qce_device *pce_dev,
  933. struct qce_sha_req *sreq)
  934. {
  935. uint32_t auth32[SHA256_DIGEST_SIZE / sizeof(uint32_t)];
  936. uint32_t diglen;
  937. bool use_hw_key = false;
  938. bool use_pipe_key = false;
  939. int i;
  940. uint32_t mackey32[SHA_HMAC_KEY_SIZE/sizeof(uint32_t)] = {
  941. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  942. uint32_t authk_size_in_word = sreq->authklen/sizeof(uint32_t);
  943. bool sha1 = false;
  944. uint32_t auth_cfg = 0;
  945. /* clear status */
  946. writel_relaxed(0, pce_dev->iobase + CRYPTO_STATUS_REG);
  947. writel_relaxed(pce_dev->reg.crypto_cfg_be, (pce_dev->iobase +
  948. CRYPTO_CONFIG_REG));
  949. /*
  950. * Ensure previous instructions (setting the CONFIG register)
  951. * was completed before issuing starting to set other config register
  952. * This is to ensure the configurations are done in correct endian-ness
  953. * as set in the CONFIG registers
  954. */
  955. mb();
  956. if (sreq->alg == QCE_HASH_AES_CMAC) {
  957. /* write seg_cfg */
  958. writel_relaxed(0, pce_dev->iobase + CRYPTO_AUTH_SEG_CFG_REG);
  959. /* write seg_cfg */
  960. writel_relaxed(0, pce_dev->iobase + CRYPTO_ENCR_SEG_CFG_REG);
  961. /* write seg_cfg */
  962. writel_relaxed(0, pce_dev->iobase + CRYPTO_ENCR_SEG_SIZE_REG);
  963. /* Clear auth_ivn, auth_keyn registers */
  964. for (i = 0; i < 16; i++) {
  965. writel_relaxed(0, (pce_dev->iobase +
  966. (CRYPTO_AUTH_IV0_REG + i*sizeof(uint32_t))));
  967. writel_relaxed(0, (pce_dev->iobase +
  968. (CRYPTO_AUTH_KEY0_REG + i*sizeof(uint32_t))));
  969. }
  970. /* write auth_bytecnt 0/1/2/3, start with 0 */
  971. for (i = 0; i < 4; i++)
  972. writel_relaxed(0, pce_dev->iobase +
  973. CRYPTO_AUTH_BYTECNT0_REG +
  974. i * sizeof(uint32_t));
  975. if (sreq->authklen == AES128_KEY_SIZE)
  976. auth_cfg = pce_dev->reg.auth_cfg_cmac_128;
  977. else
  978. auth_cfg = pce_dev->reg.auth_cfg_cmac_256;
  979. }
  980. if ((sreq->alg == QCE_HASH_SHA1_HMAC) ||
  981. (sreq->alg == QCE_HASH_SHA256_HMAC) ||
  982. (sreq->alg == QCE_HASH_AES_CMAC)) {
  983. _byte_stream_to_net_words(mackey32, sreq->authkey,
  984. sreq->authklen);
  985. /* no more check for null key. use flag to check*/
  986. if ((sreq->flags & QCRYPTO_CTX_USE_HW_KEY) ==
  987. QCRYPTO_CTX_USE_HW_KEY) {
  988. use_hw_key = true;
  989. } else if ((sreq->flags & QCRYPTO_CTX_USE_PIPE_KEY) ==
  990. QCRYPTO_CTX_USE_PIPE_KEY) {
  991. use_pipe_key = true;
  992. } else {
  993. /* setup key */
  994. for (i = 0; i < authk_size_in_word; i++)
  995. writel_relaxed(mackey32[i], (pce_dev->iobase +
  996. (CRYPTO_AUTH_KEY0_REG +
  997. i*sizeof(uint32_t))));
  998. }
  999. }
  1000. if (sreq->alg == QCE_HASH_AES_CMAC)
  1001. goto go_proc;
  1002. /* if not the last, the size has to be on the block boundary */
  1003. if (sreq->last_blk == 0 && (sreq->size % SHA256_BLOCK_SIZE))
  1004. return -EIO;
  1005. switch (sreq->alg) {
  1006. case QCE_HASH_SHA1:
  1007. auth_cfg = pce_dev->reg.auth_cfg_sha1;
  1008. diglen = SHA1_DIGEST_SIZE;
  1009. sha1 = true;
  1010. break;
  1011. case QCE_HASH_SHA1_HMAC:
  1012. auth_cfg = pce_dev->reg.auth_cfg_hmac_sha1;
  1013. diglen = SHA1_DIGEST_SIZE;
  1014. sha1 = true;
  1015. break;
  1016. case QCE_HASH_SHA256:
  1017. auth_cfg = pce_dev->reg.auth_cfg_sha256;
  1018. diglen = SHA256_DIGEST_SIZE;
  1019. break;
  1020. case QCE_HASH_SHA256_HMAC:
  1021. auth_cfg = pce_dev->reg.auth_cfg_hmac_sha256;
  1022. diglen = SHA256_DIGEST_SIZE;
  1023. break;
  1024. default:
  1025. return -EINVAL;
  1026. }
  1027. /* write 20/32 bytes, 5/8 words into auth_iv for SHA1/SHA256 */
  1028. if (sreq->first_blk) {
  1029. if (sha1) {
  1030. for (i = 0; i < 5; i++)
  1031. auth32[i] = _std_init_vector_sha1[i];
  1032. } else {
  1033. for (i = 0; i < 8; i++)
  1034. auth32[i] = _std_init_vector_sha256[i];
  1035. }
  1036. } else {
  1037. _byte_stream_to_net_words(auth32, sreq->digest, diglen);
  1038. }
  1039. /* Set auth_ivn, auth_keyn registers */
  1040. for (i = 0; i < 5; i++)
  1041. writel_relaxed(auth32[i], (pce_dev->iobase +
  1042. (CRYPTO_AUTH_IV0_REG + i*sizeof(uint32_t))));
  1043. if ((sreq->alg == QCE_HASH_SHA256) ||
  1044. (sreq->alg == QCE_HASH_SHA256_HMAC)) {
  1045. for (i = 5; i < 8; i++)
  1046. writel_relaxed(auth32[i], (pce_dev->iobase +
  1047. (CRYPTO_AUTH_IV0_REG + i*sizeof(uint32_t))));
  1048. }
  1049. /* write auth_bytecnt 0/1/2/3, start with 0 */
  1050. for (i = 0; i < 2; i++)
  1051. writel_relaxed(sreq->auth_data[i], pce_dev->iobase +
  1052. CRYPTO_AUTH_BYTECNT0_REG +
  1053. i * sizeof(uint32_t));
  1054. /* Set/reset last bit in CFG register */
  1055. if (sreq->last_blk)
  1056. auth_cfg |= 1 << CRYPTO_LAST;
  1057. else
  1058. auth_cfg &= ~(1 << CRYPTO_LAST);
  1059. if (sreq->first_blk)
  1060. auth_cfg |= 1 << CRYPTO_FIRST;
  1061. else
  1062. auth_cfg &= ~(1 << CRYPTO_FIRST);
  1063. if (use_hw_key)
  1064. auth_cfg |= 1 << CRYPTO_USE_HW_KEY_AUTH;
  1065. if (use_pipe_key)
  1066. auth_cfg |= 1 << CRYPTO_USE_PIPE_KEY_AUTH;
  1067. go_proc:
  1068. /* write seg_cfg */
  1069. writel_relaxed(auth_cfg, pce_dev->iobase + CRYPTO_AUTH_SEG_CFG_REG);
  1070. /* write auth seg_size */
  1071. writel_relaxed(sreq->size, pce_dev->iobase + CRYPTO_AUTH_SEG_SIZE_REG);
  1072. /* write auth_seg_start */
  1073. writel_relaxed(0, pce_dev->iobase + CRYPTO_AUTH_SEG_START_REG);
  1074. /* reset encr seg_cfg */
  1075. writel_relaxed(0, pce_dev->iobase + CRYPTO_ENCR_SEG_CFG_REG);
  1076. /* write seg_size */
  1077. writel_relaxed(sreq->size, pce_dev->iobase + CRYPTO_SEG_SIZE_REG);
  1078. writel_relaxed(pce_dev->reg.crypto_cfg_le, (pce_dev->iobase +
  1079. CRYPTO_CONFIG_REG));
  1080. /* issue go to crypto */
  1081. if (use_hw_key == false)
  1082. writel_relaxed(((1 << CRYPTO_GO) | (1 << CRYPTO_RESULTS_DUMP) |
  1083. (1 << CRYPTO_CLR_CNTXT)),
  1084. pce_dev->iobase + CRYPTO_GOPROC_REG);
  1085. else
  1086. writel_relaxed(((1 << CRYPTO_GO) | (1 << CRYPTO_RESULTS_DUMP)),
  1087. pce_dev->iobase + CRYPTO_GOPROC_QC_KEY_REG);
  1088. /*
  1089. * Ensure previous instructions (setting the GO register)
  1090. * was completed before issuing a DMA transfer request
  1091. */
  1092. mb();
  1093. return 0;
  1094. }
  1095. static int _ce_setup_aead_direct(struct qce_device *pce_dev,
  1096. struct qce_req *q_req, uint32_t totallen_in, uint32_t coffset)
  1097. {
  1098. int32_t authk_size_in_word = SHA_HMAC_KEY_SIZE/sizeof(uint32_t);
  1099. int i;
  1100. uint32_t mackey32[SHA_HMAC_KEY_SIZE/sizeof(uint32_t)] = {0};
  1101. uint32_t a_cfg;
  1102. uint32_t enckey32[(MAX_CIPHER_KEY_SIZE*2)/sizeof(uint32_t)] = {0};
  1103. uint32_t enciv32[MAX_IV_LENGTH/sizeof(uint32_t)] = {0};
  1104. uint32_t enck_size_in_word = 0;
  1105. uint32_t enciv_in_word;
  1106. uint32_t key_size;
  1107. uint32_t ivsize = q_req->ivsize;
  1108. uint32_t encr_cfg;
  1109. /* clear status */
  1110. writel_relaxed(0, pce_dev->iobase + CRYPTO_STATUS_REG);
  1111. writel_relaxed(pce_dev->reg.crypto_cfg_be, (pce_dev->iobase +
  1112. CRYPTO_CONFIG_REG));
  1113. /*
  1114. * Ensure previous instructions (setting the CONFIG register)
  1115. * was completed before issuing starting to set other config register
  1116. * This is to ensure the configurations are done in correct endian-ness
  1117. * as set in the CONFIG registers
  1118. */
  1119. mb();
  1120. key_size = q_req->encklen;
  1121. enck_size_in_word = key_size/sizeof(uint32_t);
  1122. switch (q_req->alg) {
  1123. case CIPHER_ALG_DES:
  1124. switch (q_req->mode) {
  1125. case QCE_MODE_ECB:
  1126. encr_cfg = pce_dev->reg.encr_cfg_des_ecb;
  1127. break;
  1128. case QCE_MODE_CBC:
  1129. encr_cfg = pce_dev->reg.encr_cfg_des_cbc;
  1130. break;
  1131. default:
  1132. return -EINVAL;
  1133. }
  1134. enciv_in_word = 2;
  1135. break;
  1136. case CIPHER_ALG_3DES:
  1137. switch (q_req->mode) {
  1138. case QCE_MODE_ECB:
  1139. encr_cfg = pce_dev->reg.encr_cfg_3des_ecb;
  1140. break;
  1141. case QCE_MODE_CBC:
  1142. encr_cfg = pce_dev->reg.encr_cfg_3des_cbc;
  1143. break;
  1144. default:
  1145. return -EINVAL;
  1146. }
  1147. enciv_in_word = 2;
  1148. break;
  1149. case CIPHER_ALG_AES:
  1150. switch (q_req->mode) {
  1151. case QCE_MODE_ECB:
  1152. if (key_size == AES128_KEY_SIZE)
  1153. encr_cfg = pce_dev->reg.encr_cfg_aes_ecb_128;
  1154. else if (key_size == AES256_KEY_SIZE)
  1155. encr_cfg = pce_dev->reg.encr_cfg_aes_ecb_256;
  1156. else
  1157. return -EINVAL;
  1158. break;
  1159. case QCE_MODE_CBC:
  1160. if (key_size == AES128_KEY_SIZE)
  1161. encr_cfg = pce_dev->reg.encr_cfg_aes_cbc_128;
  1162. else if (key_size == AES256_KEY_SIZE)
  1163. encr_cfg = pce_dev->reg.encr_cfg_aes_cbc_256;
  1164. else
  1165. return -EINVAL;
  1166. break;
  1167. default:
  1168. return -EINVAL;
  1169. }
  1170. enciv_in_word = 4;
  1171. break;
  1172. default:
  1173. return -EINVAL;
  1174. }
  1175. pce_dev->mode = q_req->mode;
  1176. /* write CNTR0_IV0_REG */
  1177. if (q_req->mode != QCE_MODE_ECB) {
  1178. _byte_stream_to_net_words(enciv32, q_req->iv, ivsize);
  1179. for (i = 0; i < enciv_in_word; i++)
  1180. writel_relaxed(enciv32[i], pce_dev->iobase +
  1181. (CRYPTO_CNTR0_IV0_REG + i * sizeof(uint32_t)));
  1182. }
  1183. /*
  1184. * write encr key
  1185. * do not use hw key or pipe key
  1186. */
  1187. _byte_stream_to_net_words(enckey32, q_req->enckey, key_size);
  1188. for (i = 0; i < enck_size_in_word; i++)
  1189. writel_relaxed(enckey32[i], pce_dev->iobase +
  1190. (CRYPTO_ENCR_KEY0_REG + i * sizeof(uint32_t)));
  1191. /* write encr seg cfg */
  1192. if (q_req->dir == QCE_ENCRYPT)
  1193. encr_cfg |= (1 << CRYPTO_ENCODE);
  1194. writel_relaxed(encr_cfg, pce_dev->iobase + CRYPTO_ENCR_SEG_CFG_REG);
  1195. /* we only support sha1-hmac at this point */
  1196. _byte_stream_to_net_words(mackey32, q_req->authkey,
  1197. q_req->authklen);
  1198. for (i = 0; i < authk_size_in_word; i++)
  1199. writel_relaxed(mackey32[i], pce_dev->iobase +
  1200. (CRYPTO_AUTH_KEY0_REG + i * sizeof(uint32_t)));
  1201. for (i = 0; i < 5; i++)
  1202. writel_relaxed(_std_init_vector_sha1[i], pce_dev->iobase +
  1203. (CRYPTO_AUTH_IV0_REG + i * sizeof(uint32_t)));
  1204. /* write auth_bytecnt 0/1, start with 0 */
  1205. writel_relaxed(0, pce_dev->iobase + CRYPTO_AUTH_BYTECNT0_REG);
  1206. writel_relaxed(0, pce_dev->iobase + CRYPTO_AUTH_BYTECNT1_REG);
  1207. /* write encr seg size */
  1208. writel_relaxed(q_req->cryptlen, pce_dev->iobase +
  1209. CRYPTO_ENCR_SEG_SIZE_REG);
  1210. /* write encr start */
  1211. writel_relaxed(coffset & 0xffff, pce_dev->iobase +
  1212. CRYPTO_ENCR_SEG_START_REG);
  1213. a_cfg = (CRYPTO_AUTH_MODE_HMAC << CRYPTO_AUTH_MODE) |
  1214. (CRYPTO_AUTH_SIZE_SHA1 << CRYPTO_AUTH_SIZE) |
  1215. (1 << CRYPTO_LAST) | (1 << CRYPTO_FIRST) |
  1216. (CRYPTO_AUTH_ALG_SHA << CRYPTO_AUTH_ALG);
  1217. if (q_req->dir == QCE_ENCRYPT)
  1218. a_cfg |= (CRYPTO_AUTH_POS_AFTER << CRYPTO_AUTH_POS);
  1219. else
  1220. a_cfg |= (CRYPTO_AUTH_POS_BEFORE << CRYPTO_AUTH_POS);
  1221. /* write auth seg_cfg */
  1222. writel_relaxed(a_cfg, pce_dev->iobase + CRYPTO_AUTH_SEG_CFG_REG);
  1223. /* write auth seg_size */
  1224. writel_relaxed(totallen_in, pce_dev->iobase + CRYPTO_AUTH_SEG_SIZE_REG);
  1225. /* write auth_seg_start */
  1226. writel_relaxed(0, pce_dev->iobase + CRYPTO_AUTH_SEG_START_REG);
  1227. /* write seg_size */
  1228. writel_relaxed(totallen_in, pce_dev->iobase + CRYPTO_SEG_SIZE_REG);
  1229. writel_relaxed(pce_dev->reg.crypto_cfg_le, (pce_dev->iobase +
  1230. CRYPTO_CONFIG_REG));
  1231. /* issue go to crypto */
  1232. writel_relaxed(((1 << CRYPTO_GO) | (1 << CRYPTO_RESULTS_DUMP) |
  1233. (1 << CRYPTO_CLR_CNTXT)),
  1234. pce_dev->iobase + CRYPTO_GOPROC_REG);
  1235. /*
  1236. * Ensure previous instructions (setting the GO register)
  1237. * was completed before issuing a DMA transfer request
  1238. */
  1239. mb();
  1240. return 0;
  1241. };
  1242. static int _ce_setup_cipher_direct(struct qce_device *pce_dev,
  1243. struct qce_req *creq, uint32_t totallen_in, uint32_t coffset)
  1244. {
  1245. uint32_t enckey32[(MAX_CIPHER_KEY_SIZE * 2)/sizeof(uint32_t)] = {
  1246. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  1247. uint32_t enciv32[MAX_IV_LENGTH / sizeof(uint32_t)] = {
  1248. 0, 0, 0, 0};
  1249. uint32_t enck_size_in_word = 0;
  1250. uint32_t key_size;
  1251. bool use_hw_key = false;
  1252. bool use_pipe_key = false;
  1253. uint32_t encr_cfg = 0;
  1254. uint32_t ivsize = creq->ivsize;
  1255. int i;
  1256. /* clear status */
  1257. writel_relaxed(0, pce_dev->iobase + CRYPTO_STATUS_REG);
  1258. writel_relaxed(pce_dev->reg.crypto_cfg_be, (pce_dev->iobase +
  1259. CRYPTO_CONFIG_REG));
  1260. /*
  1261. * Ensure previous instructions (setting the CONFIG register)
  1262. * was completed before issuing starting to set other config register
  1263. * This is to ensure the configurations are done in correct endian-ness
  1264. * as set in the CONFIG registers
  1265. */
  1266. mb();
  1267. if (creq->mode == QCE_MODE_XTS)
  1268. key_size = creq->encklen/2;
  1269. else
  1270. key_size = creq->encklen;
  1271. if ((creq->flags & QCRYPTO_CTX_USE_HW_KEY) == QCRYPTO_CTX_USE_HW_KEY) {
  1272. use_hw_key = true;
  1273. } else {
  1274. if ((creq->flags & QCRYPTO_CTX_USE_PIPE_KEY) ==
  1275. QCRYPTO_CTX_USE_PIPE_KEY)
  1276. use_pipe_key = true;
  1277. }
  1278. if ((use_pipe_key == false) && (use_hw_key == false)) {
  1279. _byte_stream_to_net_words(enckey32, creq->enckey, key_size);
  1280. enck_size_in_word = key_size/sizeof(uint32_t);
  1281. }
  1282. if ((creq->op == QCE_REQ_AEAD) && (creq->mode == QCE_MODE_CCM)) {
  1283. uint32_t authklen32 = creq->encklen/sizeof(uint32_t);
  1284. uint32_t noncelen32 = MAX_NONCE/sizeof(uint32_t);
  1285. uint32_t nonce32[MAX_NONCE/sizeof(uint32_t)] = {0, 0, 0, 0};
  1286. uint32_t auth_cfg = 0;
  1287. /* Clear auth_ivn, auth_keyn registers */
  1288. for (i = 0; i < 16; i++) {
  1289. writel_relaxed(0, (pce_dev->iobase +
  1290. (CRYPTO_AUTH_IV0_REG + i*sizeof(uint32_t))));
  1291. writel_relaxed(0, (pce_dev->iobase +
  1292. (CRYPTO_AUTH_KEY0_REG + i*sizeof(uint32_t))));
  1293. }
  1294. /* write auth_bytecnt 0/1/2/3, start with 0 */
  1295. for (i = 0; i < 4; i++)
  1296. writel_relaxed(0, pce_dev->iobase +
  1297. CRYPTO_AUTH_BYTECNT0_REG +
  1298. i * sizeof(uint32_t));
  1299. /* write nonce */
  1300. _byte_stream_to_net_words(nonce32, creq->nonce, MAX_NONCE);
  1301. for (i = 0; i < noncelen32; i++)
  1302. writel_relaxed(nonce32[i], pce_dev->iobase +
  1303. CRYPTO_AUTH_INFO_NONCE0_REG +
  1304. (i*sizeof(uint32_t)));
  1305. if (creq->authklen == AES128_KEY_SIZE)
  1306. auth_cfg = pce_dev->reg.auth_cfg_aes_ccm_128;
  1307. else {
  1308. if (creq->authklen == AES256_KEY_SIZE)
  1309. auth_cfg = pce_dev->reg.auth_cfg_aes_ccm_256;
  1310. }
  1311. if (creq->dir == QCE_ENCRYPT)
  1312. auth_cfg |= (CRYPTO_AUTH_POS_BEFORE << CRYPTO_AUTH_POS);
  1313. else
  1314. auth_cfg |= (CRYPTO_AUTH_POS_AFTER << CRYPTO_AUTH_POS);
  1315. auth_cfg |= ((creq->authsize - 1) << CRYPTO_AUTH_SIZE);
  1316. if (use_hw_key == true) {
  1317. auth_cfg |= (1 << CRYPTO_USE_HW_KEY_AUTH);
  1318. } else {
  1319. auth_cfg &= ~(1 << CRYPTO_USE_HW_KEY_AUTH);
  1320. /* write auth key */
  1321. for (i = 0; i < authklen32; i++)
  1322. writel_relaxed(enckey32[i], pce_dev->iobase +
  1323. CRYPTO_AUTH_KEY0_REG + (i*sizeof(uint32_t)));
  1324. }
  1325. writel_relaxed(auth_cfg, pce_dev->iobase +
  1326. CRYPTO_AUTH_SEG_CFG_REG);
  1327. if (creq->dir == QCE_ENCRYPT)
  1328. writel_relaxed(totallen_in, pce_dev->iobase +
  1329. CRYPTO_AUTH_SEG_SIZE_REG);
  1330. else
  1331. writel_relaxed((totallen_in - creq->authsize),
  1332. pce_dev->iobase + CRYPTO_AUTH_SEG_SIZE_REG);
  1333. writel_relaxed(0, pce_dev->iobase + CRYPTO_AUTH_SEG_START_REG);
  1334. } else {
  1335. if (creq->op != QCE_REQ_AEAD)
  1336. writel_relaxed(0, pce_dev->iobase +
  1337. CRYPTO_AUTH_SEG_CFG_REG);
  1338. }
  1339. /*
  1340. * Ensure previous instructions (write to all AUTH registers)
  1341. * was completed before accessing a register that is not in
  1342. * in the same 1K range.
  1343. */
  1344. mb();
  1345. switch (creq->mode) {
  1346. case QCE_MODE_ECB:
  1347. if (key_size == AES128_KEY_SIZE)
  1348. encr_cfg = pce_dev->reg.encr_cfg_aes_ecb_128;
  1349. else
  1350. encr_cfg = pce_dev->reg.encr_cfg_aes_ecb_256;
  1351. break;
  1352. case QCE_MODE_CBC:
  1353. if (key_size == AES128_KEY_SIZE)
  1354. encr_cfg = pce_dev->reg.encr_cfg_aes_cbc_128;
  1355. else
  1356. encr_cfg = pce_dev->reg.encr_cfg_aes_cbc_256;
  1357. break;
  1358. case QCE_MODE_XTS:
  1359. if (key_size == AES128_KEY_SIZE)
  1360. encr_cfg = pce_dev->reg.encr_cfg_aes_xts_128;
  1361. else
  1362. encr_cfg = pce_dev->reg.encr_cfg_aes_xts_256;
  1363. break;
  1364. case QCE_MODE_CCM:
  1365. if (key_size == AES128_KEY_SIZE)
  1366. encr_cfg = pce_dev->reg.encr_cfg_aes_ccm_128;
  1367. else
  1368. encr_cfg = pce_dev->reg.encr_cfg_aes_ccm_256;
  1369. break;
  1370. case QCE_MODE_CTR:
  1371. default:
  1372. if (key_size == AES128_KEY_SIZE)
  1373. encr_cfg = pce_dev->reg.encr_cfg_aes_ctr_128;
  1374. else
  1375. encr_cfg = pce_dev->reg.encr_cfg_aes_ctr_256;
  1376. break;
  1377. }
  1378. pce_dev->mode = creq->mode;
  1379. switch (creq->alg) {
  1380. case CIPHER_ALG_DES:
  1381. if (creq->mode != QCE_MODE_ECB) {
  1382. encr_cfg = pce_dev->reg.encr_cfg_des_cbc;
  1383. _byte_stream_to_net_words(enciv32, creq->iv, ivsize);
  1384. writel_relaxed(enciv32[0], pce_dev->iobase +
  1385. CRYPTO_CNTR0_IV0_REG);
  1386. writel_relaxed(enciv32[1], pce_dev->iobase +
  1387. CRYPTO_CNTR1_IV1_REG);
  1388. } else {
  1389. encr_cfg = pce_dev->reg.encr_cfg_des_ecb;
  1390. }
  1391. if (use_hw_key == false) {
  1392. writel_relaxed(enckey32[0], pce_dev->iobase +
  1393. CRYPTO_ENCR_KEY0_REG);
  1394. writel_relaxed(enckey32[1], pce_dev->iobase +
  1395. CRYPTO_ENCR_KEY1_REG);
  1396. }
  1397. break;
  1398. case CIPHER_ALG_3DES:
  1399. if (creq->mode != QCE_MODE_ECB) {
  1400. _byte_stream_to_net_words(enciv32, creq->iv, ivsize);
  1401. writel_relaxed(enciv32[0], pce_dev->iobase +
  1402. CRYPTO_CNTR0_IV0_REG);
  1403. writel_relaxed(enciv32[1], pce_dev->iobase +
  1404. CRYPTO_CNTR1_IV1_REG);
  1405. encr_cfg = pce_dev->reg.encr_cfg_3des_cbc;
  1406. } else {
  1407. encr_cfg = pce_dev->reg.encr_cfg_3des_ecb;
  1408. }
  1409. if (use_hw_key == false) {
  1410. /* write encr key */
  1411. for (i = 0; i < 6; i++)
  1412. writel_relaxed(enckey32[0], (pce_dev->iobase +
  1413. (CRYPTO_ENCR_KEY0_REG + i * sizeof(uint32_t))));
  1414. }
  1415. break;
  1416. case CIPHER_ALG_AES:
  1417. default:
  1418. if (creq->mode == QCE_MODE_XTS) {
  1419. uint32_t xtskey32[MAX_CIPHER_KEY_SIZE/sizeof(uint32_t)]
  1420. = {0, 0, 0, 0, 0, 0, 0, 0};
  1421. uint32_t xtsklen =
  1422. creq->encklen/(2 * sizeof(uint32_t));
  1423. if ((use_hw_key == false) && (use_pipe_key == false)) {
  1424. _byte_stream_to_net_words(xtskey32,
  1425. (creq->enckey + creq->encklen/2),
  1426. creq->encklen/2);
  1427. /* write xts encr key */
  1428. for (i = 0; i < xtsklen; i++)
  1429. writel_relaxed(xtskey32[i],
  1430. pce_dev->iobase +
  1431. CRYPTO_ENCR_XTS_KEY0_REG +
  1432. (i * sizeof(uint32_t)));
  1433. }
  1434. /* write xts du size */
  1435. switch (creq->flags & QCRYPTO_CTX_XTS_MASK) {
  1436. case QCRYPTO_CTX_XTS_DU_SIZE_512B:
  1437. writel_relaxed(
  1438. min((uint32_t)QCE_SECTOR_SIZE,
  1439. creq->cryptlen), pce_dev->iobase +
  1440. CRYPTO_ENCR_XTS_DU_SIZE_REG);
  1441. break;
  1442. case QCRYPTO_CTX_XTS_DU_SIZE_1KB:
  1443. writel_relaxed(
  1444. min((uint32_t)(QCE_SECTOR_SIZE * 2),
  1445. creq->cryptlen), pce_dev->iobase +
  1446. CRYPTO_ENCR_XTS_DU_SIZE_REG);
  1447. break;
  1448. default:
  1449. writel_relaxed(creq->cryptlen,
  1450. pce_dev->iobase +
  1451. CRYPTO_ENCR_XTS_DU_SIZE_REG);
  1452. break;
  1453. }
  1454. }
  1455. if (creq->mode != QCE_MODE_ECB) {
  1456. if (creq->mode == QCE_MODE_XTS)
  1457. _byte_stream_swap_to_net_words(enciv32,
  1458. creq->iv, ivsize);
  1459. else
  1460. _byte_stream_to_net_words(enciv32, creq->iv,
  1461. ivsize);
  1462. /* write encr cntr iv */
  1463. for (i = 0; i <= 3; i++)
  1464. writel_relaxed(enciv32[i], pce_dev->iobase +
  1465. CRYPTO_CNTR0_IV0_REG +
  1466. (i * sizeof(uint32_t)));
  1467. if (creq->mode == QCE_MODE_CCM) {
  1468. /* write cntr iv for ccm */
  1469. for (i = 0; i <= 3; i++)
  1470. writel_relaxed(enciv32[i],
  1471. pce_dev->iobase +
  1472. CRYPTO_ENCR_CCM_INT_CNTR0_REG +
  1473. (i * sizeof(uint32_t)));
  1474. /* update cntr_iv[3] by one */
  1475. writel_relaxed((enciv32[3] + 1),
  1476. pce_dev->iobase +
  1477. CRYPTO_CNTR0_IV0_REG +
  1478. (3 * sizeof(uint32_t)));
  1479. }
  1480. }
  1481. if (creq->op == QCE_REQ_ABLK_CIPHER_NO_KEY) {
  1482. encr_cfg |= (CRYPTO_ENCR_KEY_SZ_AES128 <<
  1483. CRYPTO_ENCR_KEY_SZ);
  1484. } else {
  1485. if ((use_hw_key == false) && (use_pipe_key == false)) {
  1486. for (i = 0; i < enck_size_in_word; i++)
  1487. writel_relaxed(enckey32[i],
  1488. pce_dev->iobase +
  1489. CRYPTO_ENCR_KEY0_REG +
  1490. (i * sizeof(uint32_t)));
  1491. }
  1492. } /* else of if (creq->op == QCE_REQ_ABLK_CIPHER_NO_KEY) */
  1493. break;
  1494. } /* end of switch (creq->mode) */
  1495. if (use_pipe_key)
  1496. encr_cfg |= (CRYPTO_USE_PIPE_KEY_ENCR_ENABLED
  1497. << CRYPTO_USE_PIPE_KEY_ENCR);
  1498. /* write encr seg cfg */
  1499. encr_cfg |= ((creq->dir == QCE_ENCRYPT) ? 1 : 0) << CRYPTO_ENCODE;
  1500. if (use_hw_key == true)
  1501. encr_cfg |= (CRYPTO_USE_HW_KEY << CRYPTO_USE_HW_KEY_ENCR);
  1502. else
  1503. encr_cfg &= ~(CRYPTO_USE_HW_KEY << CRYPTO_USE_HW_KEY_ENCR);
  1504. /* write encr seg cfg */
  1505. writel_relaxed(encr_cfg, pce_dev->iobase + CRYPTO_ENCR_SEG_CFG_REG);
  1506. /* write encr seg size */
  1507. if ((creq->mode == QCE_MODE_CCM) && (creq->dir == QCE_DECRYPT))
  1508. writel_relaxed((creq->cryptlen + creq->authsize),
  1509. pce_dev->iobase + CRYPTO_ENCR_SEG_SIZE_REG);
  1510. else
  1511. writel_relaxed(creq->cryptlen,
  1512. pce_dev->iobase + CRYPTO_ENCR_SEG_SIZE_REG);
  1513. /* write encr seg start */
  1514. writel_relaxed((coffset & 0xffff),
  1515. pce_dev->iobase + CRYPTO_ENCR_SEG_START_REG);
  1516. /* write encr seg start */
  1517. writel_relaxed(0xffffffff,
  1518. pce_dev->iobase + CRYPTO_CNTR_MASK_REG);
  1519. /* write seg size */
  1520. writel_relaxed(totallen_in, pce_dev->iobase + CRYPTO_SEG_SIZE_REG);
  1521. writel_relaxed(pce_dev->reg.crypto_cfg_le, (pce_dev->iobase +
  1522. CRYPTO_CONFIG_REG));
  1523. /* issue go to crypto */
  1524. if (use_hw_key == false)
  1525. writel_relaxed(((1 << CRYPTO_GO) | (1 << CRYPTO_RESULTS_DUMP) |
  1526. (1 << CRYPTO_CLR_CNTXT)),
  1527. pce_dev->iobase + CRYPTO_GOPROC_REG);
  1528. else
  1529. writel_relaxed(((1 << CRYPTO_GO) | (1 << CRYPTO_RESULTS_DUMP)),
  1530. pce_dev->iobase + CRYPTO_GOPROC_QC_KEY_REG);
  1531. /*
  1532. * Ensure previous instructions (setting the GO register)
  1533. * was completed before issuing a DMA transfer request
  1534. */
  1535. mb();
  1536. return 0;
  1537. };
  1538. static int _ce_f9_setup_direct(struct qce_device *pce_dev,
  1539. struct qce_f9_req *req)
  1540. {
  1541. uint32_t ikey32[OTA_KEY_SIZE/sizeof(uint32_t)];
  1542. uint32_t key_size_in_word = OTA_KEY_SIZE/sizeof(uint32_t);
  1543. uint32_t auth_cfg;
  1544. int i;
  1545. switch (req->algorithm) {
  1546. case QCE_OTA_ALGO_KASUMI:
  1547. auth_cfg = pce_dev->reg.auth_cfg_kasumi;
  1548. break;
  1549. case QCE_OTA_ALGO_SNOW3G:
  1550. default:
  1551. auth_cfg = pce_dev->reg.auth_cfg_snow3g;
  1552. break;
  1553. };
  1554. /* clear status */
  1555. writel_relaxed(0, pce_dev->iobase + CRYPTO_STATUS_REG);
  1556. /* set big endian configuration */
  1557. writel_relaxed(pce_dev->reg.crypto_cfg_be, (pce_dev->iobase +
  1558. CRYPTO_CONFIG_REG));
  1559. /*
  1560. * Ensure previous instructions (setting the CONFIG register)
  1561. * was completed before issuing starting to set other config register
  1562. * This is to ensure the configurations are done in correct endian-ness
  1563. * as set in the CONFIG registers
  1564. */
  1565. mb();
  1566. /* write enc_seg_cfg */
  1567. writel_relaxed(0, pce_dev->iobase + CRYPTO_ENCR_SEG_CFG_REG);
  1568. /* write ecn_seg_size */
  1569. writel_relaxed(0, pce_dev->iobase + CRYPTO_ENCR_SEG_SIZE_REG);
  1570. /* write key in CRYPTO_AUTH_IV0-3_REG */
  1571. _byte_stream_to_net_words(ikey32, &req->ikey[0], OTA_KEY_SIZE);
  1572. for (i = 0; i < key_size_in_word; i++)
  1573. writel_relaxed(ikey32[i], (pce_dev->iobase +
  1574. (CRYPTO_AUTH_IV0_REG + i*sizeof(uint32_t))));
  1575. /* write last bits in CRYPTO_AUTH_IV4_REG */
  1576. writel_relaxed(req->last_bits, (pce_dev->iobase +
  1577. CRYPTO_AUTH_IV4_REG));
  1578. /* write fresh to CRYPTO_AUTH_BYTECNT0_REG */
  1579. writel_relaxed(req->fresh, (pce_dev->iobase +
  1580. CRYPTO_AUTH_BYTECNT0_REG));
  1581. /* write count-i to CRYPTO_AUTH_BYTECNT1_REG */
  1582. writel_relaxed(req->count_i, (pce_dev->iobase +
  1583. CRYPTO_AUTH_BYTECNT1_REG));
  1584. /* write auth seg cfg */
  1585. if (req->direction == QCE_OTA_DIR_DOWNLINK)
  1586. auth_cfg |= BIT(CRYPTO_F9_DIRECTION);
  1587. writel_relaxed(auth_cfg, pce_dev->iobase + CRYPTO_AUTH_SEG_CFG_REG);
  1588. /* write auth seg size */
  1589. writel_relaxed(req->msize, pce_dev->iobase + CRYPTO_AUTH_SEG_SIZE_REG);
  1590. /* write auth seg start*/
  1591. writel_relaxed(0, pce_dev->iobase + CRYPTO_AUTH_SEG_START_REG);
  1592. /* write seg size */
  1593. writel_relaxed(req->msize, pce_dev->iobase + CRYPTO_SEG_SIZE_REG);
  1594. /* set little endian configuration before go*/
  1595. writel_relaxed(pce_dev->reg.crypto_cfg_le, (pce_dev->iobase +
  1596. CRYPTO_CONFIG_REG));
  1597. /* write go */
  1598. writel_relaxed(((1 << CRYPTO_GO) | (1 << CRYPTO_RESULTS_DUMP) |
  1599. (1 << CRYPTO_CLR_CNTXT)),
  1600. pce_dev->iobase + CRYPTO_GOPROC_REG);
  1601. /*
  1602. * Ensure previous instructions (setting the GO register)
  1603. * was completed before issuing a DMA transfer request
  1604. */
  1605. mb();
  1606. return 0;
  1607. }
  1608. static int _ce_f8_setup_direct(struct qce_device *pce_dev,
  1609. struct qce_f8_req *req, bool key_stream_mode,
  1610. uint16_t npkts, uint16_t cipher_offset, uint16_t cipher_size)
  1611. {
  1612. int i = 0;
  1613. uint32_t encr_cfg = 0;
  1614. uint32_t ckey32[OTA_KEY_SIZE/sizeof(uint32_t)];
  1615. uint32_t key_size_in_word = OTA_KEY_SIZE/sizeof(uint32_t);
  1616. switch (req->algorithm) {
  1617. case QCE_OTA_ALGO_KASUMI:
  1618. encr_cfg = pce_dev->reg.encr_cfg_kasumi;
  1619. break;
  1620. case QCE_OTA_ALGO_SNOW3G:
  1621. default:
  1622. encr_cfg = pce_dev->reg.encr_cfg_snow3g;
  1623. break;
  1624. };
  1625. /* clear status */
  1626. writel_relaxed(0, pce_dev->iobase + CRYPTO_STATUS_REG);
  1627. /* set big endian configuration */
  1628. writel_relaxed(pce_dev->reg.crypto_cfg_be, (pce_dev->iobase +
  1629. CRYPTO_CONFIG_REG));
  1630. /* write auth seg configuration */
  1631. writel_relaxed(0, pce_dev->iobase + CRYPTO_AUTH_SEG_CFG_REG);
  1632. /* write auth seg size */
  1633. writel_relaxed(0, pce_dev->iobase + CRYPTO_AUTH_SEG_SIZE_REG);
  1634. /* write key */
  1635. _byte_stream_to_net_words(ckey32, &req->ckey[0], OTA_KEY_SIZE);
  1636. for (i = 0; i < key_size_in_word; i++)
  1637. writel_relaxed(ckey32[i], (pce_dev->iobase +
  1638. (CRYPTO_ENCR_KEY0_REG + i*sizeof(uint32_t))));
  1639. /* write encr seg cfg */
  1640. if (key_stream_mode)
  1641. encr_cfg |= BIT(CRYPTO_F8_KEYSTREAM_ENABLE);
  1642. if (req->direction == QCE_OTA_DIR_DOWNLINK)
  1643. encr_cfg |= BIT(CRYPTO_F8_DIRECTION);
  1644. writel_relaxed(encr_cfg, pce_dev->iobase +
  1645. CRYPTO_ENCR_SEG_CFG_REG);
  1646. /* write encr seg start */
  1647. writel_relaxed((cipher_offset & 0xffff), pce_dev->iobase +
  1648. CRYPTO_ENCR_SEG_START_REG);
  1649. /* write encr seg size */
  1650. writel_relaxed(cipher_size, pce_dev->iobase +
  1651. CRYPTO_ENCR_SEG_SIZE_REG);
  1652. /* write seg size */
  1653. writel_relaxed(req->data_len, pce_dev->iobase +
  1654. CRYPTO_SEG_SIZE_REG);
  1655. /* write cntr0_iv0 for countC */
  1656. writel_relaxed(req->count_c, pce_dev->iobase +
  1657. CRYPTO_CNTR0_IV0_REG);
  1658. /* write cntr1_iv1 for nPkts, and bearer */
  1659. if (npkts == 1)
  1660. npkts = 0;
  1661. writel_relaxed(req->bearer << CRYPTO_CNTR1_IV1_REG_F8_BEARER |
  1662. npkts << CRYPTO_CNTR1_IV1_REG_F8_PKT_CNT,
  1663. pce_dev->iobase + CRYPTO_CNTR1_IV1_REG);
  1664. /* set little endian configuration before go*/
  1665. writel_relaxed(pce_dev->reg.crypto_cfg_le, (pce_dev->iobase +
  1666. CRYPTO_CONFIG_REG));
  1667. /* write go */
  1668. writel_relaxed(((1 << CRYPTO_GO) | (1 << CRYPTO_RESULTS_DUMP) |
  1669. (1 << CRYPTO_CLR_CNTXT)),
  1670. pce_dev->iobase + CRYPTO_GOPROC_REG);
  1671. /*
  1672. * Ensure previous instructions (setting the GO register)
  1673. * was completed before issuing a DMA transfer request
  1674. */
  1675. mb();
  1676. return 0;
  1677. }
  1678. static int _qce_unlock_other_pipes(struct qce_device *pce_dev)
  1679. {
  1680. int rc = 0;
  1681. if (pce_dev->support_cmd_dscr == false)
  1682. return rc;
  1683. pce_dev->ce_sps.consumer.event.callback = NULL;
  1684. rc = sps_transfer_one(pce_dev->ce_sps.consumer.pipe,
  1685. GET_PHYS_ADDR(pce_dev->ce_sps.cmdlistptr.unlock_all_pipes.cmdlist),
  1686. 0, NULL, (SPS_IOVEC_FLAG_CMD | SPS_IOVEC_FLAG_UNLOCK));
  1687. if (rc) {
  1688. pr_err("sps_xfr_one() fail rc=%d", rc);
  1689. rc = -EINVAL;
  1690. }
  1691. return rc;
  1692. }
  1693. static int _aead_complete(struct qce_device *pce_dev)
  1694. {
  1695. struct aead_request *areq;
  1696. unsigned char mac[SHA256_DIGEST_SIZE];
  1697. uint32_t status;
  1698. int32_t result_status;
  1699. areq = (struct aead_request *) pce_dev->areq;
  1700. if (areq->src != areq->dst) {
  1701. qce_dma_unmap_sg(pce_dev->pdev, areq->dst, pce_dev->dst_nents,
  1702. DMA_FROM_DEVICE);
  1703. }
  1704. qce_dma_unmap_sg(pce_dev->pdev, areq->src, pce_dev->src_nents,
  1705. (areq->src == areq->dst) ? DMA_BIDIRECTIONAL :
  1706. DMA_TO_DEVICE);
  1707. qce_dma_unmap_sg(pce_dev->pdev, areq->assoc, pce_dev->assoc_nents,
  1708. DMA_TO_DEVICE);
  1709. /* check MAC */
  1710. memcpy(mac, (char *)(&pce_dev->ce_sps.result->auth_iv[0]),
  1711. SHA256_DIGEST_SIZE);
  1712. /* read status before unlock */
  1713. status = readl_relaxed(pce_dev->iobase + CRYPTO_STATUS_REG);
  1714. if (_qce_unlock_other_pipes(pce_dev))
  1715. return -EINVAL;
  1716. /*
  1717. * Don't use result dump status. The operation may not
  1718. * be complete.
  1719. * Instead, use the status we just read of device.
  1720. * In case, we need to use result_status from result
  1721. * dump the result_status needs to be byte swapped,
  1722. * since we set the device to little endian.
  1723. */
  1724. result_status = 0;
  1725. pce_dev->ce_sps.result->status = 0;
  1726. if (status & ((1 << CRYPTO_SW_ERR) | (1 << CRYPTO_AXI_ERR)
  1727. | (1 << CRYPTO_HSD_ERR))) {
  1728. pr_err("aead operation error. Status %x\n", status);
  1729. result_status = -ENXIO;
  1730. } else if (pce_dev->ce_sps.consumer_status |
  1731. pce_dev->ce_sps.producer_status) {
  1732. pr_err("aead sps operation error. sps status %x %x\n",
  1733. pce_dev->ce_sps.consumer_status,
  1734. pce_dev->ce_sps.producer_status);
  1735. result_status = -ENXIO;
  1736. } else if ((status & (1 << CRYPTO_OPERATION_DONE)) == 0) {
  1737. pr_err("aead operation not done? Status %x, sps status %x %x\n",
  1738. status,
  1739. pce_dev->ce_sps.consumer_status,
  1740. pce_dev->ce_sps.producer_status);
  1741. result_status = -ENXIO;
  1742. }
  1743. if (pce_dev->mode == QCE_MODE_CCM) {
  1744. if (result_status == 0 && (status & (1 << CRYPTO_MAC_FAILED)))
  1745. result_status = -EBADMSG;
  1746. pce_dev->qce_cb(areq, mac, NULL, result_status);
  1747. } else {
  1748. uint32_t ivsize = 0;
  1749. struct crypto_aead *aead;
  1750. unsigned char iv[NUM_OF_CRYPTO_CNTR_IV_REG * CRYPTO_REG_SIZE];
  1751. aead = crypto_aead_reqtfm(areq);
  1752. ivsize = crypto_aead_ivsize(aead);
  1753. if (pce_dev->ce_sps.minor_version != 0)
  1754. dma_unmap_single(pce_dev->pdev, pce_dev->phy_iv_in,
  1755. ivsize, DMA_TO_DEVICE);
  1756. memcpy(iv, (char *)(pce_dev->ce_sps.result->encr_cntr_iv),
  1757. sizeof(iv));
  1758. pce_dev->qce_cb(areq, mac, iv, result_status);
  1759. }
  1760. return 0;
  1761. };
  1762. static int _sha_complete(struct qce_device *pce_dev)
  1763. {
  1764. struct ahash_request *areq;
  1765. unsigned char digest[SHA256_DIGEST_SIZE];
  1766. uint32_t bytecount32[2];
  1767. int32_t result_status = pce_dev->ce_sps.result->status;
  1768. uint32_t status;
  1769. areq = (struct ahash_request *) pce_dev->areq;
  1770. if (!areq) {
  1771. pr_err("sha operation error. areq is NULL\n");
  1772. return -ENXIO;
  1773. }
  1774. qce_dma_unmap_sg(pce_dev->pdev, areq->src, pce_dev->src_nents,
  1775. DMA_TO_DEVICE);
  1776. memcpy(digest, (char *)(&pce_dev->ce_sps.result->auth_iv[0]),
  1777. SHA256_DIGEST_SIZE);
  1778. _byte_stream_to_net_words(bytecount32,
  1779. (unsigned char *)pce_dev->ce_sps.result->auth_byte_count,
  1780. 2 * CRYPTO_REG_SIZE);
  1781. /* read status before unlock */
  1782. status = readl_relaxed(pce_dev->iobase + CRYPTO_STATUS_REG);
  1783. if (_qce_unlock_other_pipes(pce_dev))
  1784. return -EINVAL;
  1785. /*
  1786. * Don't use result dump status. The operation may not be complete.
  1787. * Instead, use the status we just read of device.
  1788. * In case, we need to use result_status from result
  1789. * dump the result_status needs to be byte swapped,
  1790. * since we set the device to little endian.
  1791. */
  1792. if (status & ((1 << CRYPTO_SW_ERR) | (1 << CRYPTO_AXI_ERR)
  1793. | (1 << CRYPTO_HSD_ERR))) {
  1794. pr_err("sha operation error. Status %x\n", status);
  1795. result_status = -ENXIO;
  1796. } else if (pce_dev->ce_sps.consumer_status) {
  1797. pr_err("sha sps operation error. sps status %x\n",
  1798. pce_dev->ce_sps.consumer_status);
  1799. result_status = -ENXIO;
  1800. } else if ((status & (1 << CRYPTO_OPERATION_DONE)) == 0) {
  1801. pr_err("sha operation not done? Status %x, sps status %x\n",
  1802. status, pce_dev->ce_sps.consumer_status);
  1803. result_status = -ENXIO;
  1804. } else {
  1805. result_status = 0;
  1806. }
  1807. pce_dev->qce_cb(areq, digest, (char *)bytecount32,
  1808. result_status);
  1809. return 0;
  1810. };
  1811. static int _f9_complete(struct qce_device *pce_dev)
  1812. {
  1813. uint32_t mac_i;
  1814. uint32_t status;
  1815. int32_t result_status;
  1816. dma_unmap_single(pce_dev->pdev, pce_dev->phy_ota_src,
  1817. pce_dev->ota_size, DMA_TO_DEVICE);
  1818. _byte_stream_to_net_words(&mac_i,
  1819. (char *)(&pce_dev->ce_sps.result->auth_iv[0]),
  1820. CRYPTO_REG_SIZE);
  1821. /* read status before unlock */
  1822. status = readl_relaxed(pce_dev->iobase + CRYPTO_STATUS_REG);
  1823. if (_qce_unlock_other_pipes(pce_dev)) {
  1824. pce_dev->qce_cb(pce_dev->areq, NULL, NULL, -ENXIO);
  1825. return -ENXIO;
  1826. }
  1827. if (status & ((1 << CRYPTO_SW_ERR) | (1 << CRYPTO_AXI_ERR)
  1828. | (1 << CRYPTO_HSD_ERR))) {
  1829. pr_err("f9 operation error. Status %x\n", status);
  1830. result_status = -ENXIO;
  1831. } else if (pce_dev->ce_sps.consumer_status |
  1832. pce_dev->ce_sps.producer_status) {
  1833. pr_err("f9 sps operation error. sps status %x %x\n",
  1834. pce_dev->ce_sps.consumer_status,
  1835. pce_dev->ce_sps.producer_status);
  1836. result_status = -ENXIO;
  1837. } else if ((status & (1 << CRYPTO_OPERATION_DONE)) == 0) {
  1838. pr_err("f9 operation not done? Status %x, sps status %x %x\n",
  1839. status,
  1840. pce_dev->ce_sps.consumer_status,
  1841. pce_dev->ce_sps.producer_status);
  1842. result_status = -ENXIO;
  1843. } else {
  1844. result_status = 0;
  1845. }
  1846. pce_dev->qce_cb(pce_dev->areq, (void *) mac_i, NULL,
  1847. result_status);
  1848. return 0;
  1849. }
  1850. static int _ablk_cipher_complete(struct qce_device *pce_dev)
  1851. {
  1852. struct ablkcipher_request *areq;
  1853. unsigned char iv[NUM_OF_CRYPTO_CNTR_IV_REG * CRYPTO_REG_SIZE];
  1854. uint32_t status;
  1855. int32_t result_status;
  1856. areq = (struct ablkcipher_request *) pce_dev->areq;
  1857. if (areq->src != areq->dst) {
  1858. qce_dma_unmap_sg(pce_dev->pdev, areq->dst,
  1859. pce_dev->dst_nents, DMA_FROM_DEVICE);
  1860. }
  1861. qce_dma_unmap_sg(pce_dev->pdev, areq->src, pce_dev->src_nents,
  1862. (areq->src == areq->dst) ? DMA_BIDIRECTIONAL :
  1863. DMA_TO_DEVICE);
  1864. /* read status before unlock */
  1865. status = readl_relaxed(pce_dev->iobase + CRYPTO_STATUS_REG);
  1866. if (_qce_unlock_other_pipes(pce_dev))
  1867. return -EINVAL;
  1868. /*
  1869. * Don't use result dump status. The operation may not be complete.
  1870. * Instead, use the status we just read of device.
  1871. * In case, we need to use result_status from result
  1872. * dump the result_status needs to be byte swapped,
  1873. * since we set the device to little endian.
  1874. */
  1875. if (status & ((1 << CRYPTO_SW_ERR) | (1 << CRYPTO_AXI_ERR)
  1876. | (1 << CRYPTO_HSD_ERR))) {
  1877. pr_err("ablk_cipher operation error. Status %x\n",
  1878. status);
  1879. result_status = -ENXIO;
  1880. } else if (pce_dev->ce_sps.consumer_status |
  1881. pce_dev->ce_sps.producer_status) {
  1882. pr_err("ablk_cipher sps operation error. sps status %x %x\n",
  1883. pce_dev->ce_sps.consumer_status,
  1884. pce_dev->ce_sps.producer_status);
  1885. result_status = -ENXIO;
  1886. } else if ((status & (1 << CRYPTO_OPERATION_DONE)) == 0) {
  1887. pr_err("ablk_cipher operation not done? Status %x, sps status %x %x\n",
  1888. status,
  1889. pce_dev->ce_sps.consumer_status,
  1890. pce_dev->ce_sps.producer_status);
  1891. result_status = -ENXIO;
  1892. } else {
  1893. result_status = 0;
  1894. }
  1895. if (pce_dev->mode == QCE_MODE_ECB) {
  1896. pce_dev->qce_cb(areq, NULL, NULL,
  1897. pce_dev->ce_sps.consumer_status |
  1898. result_status);
  1899. } else {
  1900. if (pce_dev->ce_sps.minor_version == 0) {
  1901. if (pce_dev->mode == QCE_MODE_CBC) {
  1902. if (pce_dev->dir == QCE_DECRYPT)
  1903. memcpy(iv, (char *)pce_dev->dec_iv,
  1904. sizeof(iv));
  1905. else
  1906. memcpy(iv, (unsigned char *)
  1907. (sg_virt(areq->src) +
  1908. areq->src->length - 16),
  1909. sizeof(iv));
  1910. }
  1911. if ((pce_dev->mode == QCE_MODE_CTR) ||
  1912. (pce_dev->mode == QCE_MODE_XTS)) {
  1913. uint32_t num_blk = 0;
  1914. uint32_t cntr_iv3 = 0;
  1915. unsigned long long cntr_iv64 = 0;
  1916. unsigned char *b = (unsigned char *)(&cntr_iv3);
  1917. memcpy(iv, areq->info, sizeof(iv));
  1918. if (pce_dev->mode != QCE_MODE_XTS)
  1919. num_blk = areq->nbytes/16;
  1920. else
  1921. num_blk = 1;
  1922. cntr_iv3 = ((*(iv + 12) << 24) & 0xff000000) |
  1923. (((*(iv + 13)) << 16) & 0xff0000) |
  1924. (((*(iv + 14)) << 8) & 0xff00) |
  1925. (*(iv + 15) & 0xff);
  1926. cntr_iv64 =
  1927. (((unsigned long long)cntr_iv3 &
  1928. (unsigned long long)0xFFFFFFFFULL) +
  1929. (unsigned long long)num_blk) %
  1930. (unsigned long long)(0x100000000ULL);
  1931. cntr_iv3 = (u32)(cntr_iv64 & 0xFFFFFFFF);
  1932. *(iv + 15) = (char)(*b);
  1933. *(iv + 14) = (char)(*(b + 1));
  1934. *(iv + 13) = (char)(*(b + 2));
  1935. *(iv + 12) = (char)(*(b + 3));
  1936. }
  1937. } else {
  1938. memcpy(iv,
  1939. (char *)(pce_dev->ce_sps.result->encr_cntr_iv),
  1940. sizeof(iv));
  1941. }
  1942. pce_dev->qce_cb(areq, NULL, iv, result_status);
  1943. }
  1944. return 0;
  1945. };
  1946. static int _f8_complete(struct qce_device *pce_dev)
  1947. {
  1948. uint32_t status;
  1949. int32_t result_status;
  1950. if (pce_dev->phy_ota_dst != 0)
  1951. dma_unmap_single(pce_dev->pdev, pce_dev->phy_ota_dst,
  1952. pce_dev->ota_size, DMA_FROM_DEVICE);
  1953. if (pce_dev->phy_ota_src != 0)
  1954. dma_unmap_single(pce_dev->pdev, pce_dev->phy_ota_src,
  1955. pce_dev->ota_size, (pce_dev->phy_ota_dst) ?
  1956. DMA_TO_DEVICE : DMA_BIDIRECTIONAL);
  1957. /* read status before unlock */
  1958. status = readl_relaxed(pce_dev->iobase + CRYPTO_STATUS_REG);
  1959. if (_qce_unlock_other_pipes(pce_dev)) {
  1960. pce_dev->qce_cb(pce_dev->areq, NULL, NULL, -ENXIO);
  1961. return -ENXIO;
  1962. }
  1963. if (status & ((1 << CRYPTO_SW_ERR) | (1 << CRYPTO_AXI_ERR)
  1964. | (1 << CRYPTO_HSD_ERR))) {
  1965. pr_err("f8 operation error. Status %x\n", status);
  1966. result_status = -ENXIO;
  1967. } else if (pce_dev->ce_sps.consumer_status |
  1968. pce_dev->ce_sps.producer_status) {
  1969. pr_err("f8 sps operation error. sps status %x %x\n",
  1970. pce_dev->ce_sps.consumer_status,
  1971. pce_dev->ce_sps.producer_status);
  1972. result_status = -ENXIO;
  1973. } else if ((status & (1 << CRYPTO_OPERATION_DONE)) == 0) {
  1974. pr_err("f8 operation not done? Status %x, sps status %x %x\n",
  1975. status,
  1976. pce_dev->ce_sps.consumer_status,
  1977. pce_dev->ce_sps.producer_status);
  1978. result_status = -ENXIO;
  1979. } else {
  1980. result_status = 0;
  1981. }
  1982. pce_dev->qce_cb(pce_dev->areq, NULL, NULL, result_status);
  1983. return 0;
  1984. }
  1985. #ifdef QCE_DEBUG
  1986. static void _qce_dump_descr_fifos(struct qce_device *pce_dev)
  1987. {
  1988. int i, j, ents;
  1989. struct sps_iovec *iovec = pce_dev->ce_sps.in_transfer.iovec;
  1990. uint32_t cmd_flags = SPS_IOVEC_FLAG_CMD;
  1991. printk(KERN_INFO "==============================================\n");
  1992. printk(KERN_INFO "CONSUMER (TX/IN/DEST) PIPE DESCRIPTOR\n");
  1993. printk(KERN_INFO "==============================================\n");
  1994. for (i = 0; i < pce_dev->ce_sps.in_transfer.iovec_count; i++) {
  1995. printk(KERN_INFO " [%d] addr=0x%x size=0x%x flags=0x%x\n", i,
  1996. iovec->addr, iovec->size, iovec->flags);
  1997. if (iovec->flags & cmd_flags) {
  1998. struct sps_command_element *pced;
  1999. pced = (struct sps_command_element *)
  2000. (GET_VIRT_ADDR(iovec->addr));
  2001. ents = iovec->size/(sizeof(struct sps_command_element));
  2002. for (j = 0; j < ents; j++) {
  2003. printk(KERN_INFO " [%d] [0x%x] 0x%x\n", j,
  2004. pced->addr, pced->data);
  2005. pced++;
  2006. }
  2007. }
  2008. iovec++;
  2009. }
  2010. printk(KERN_INFO "==============================================\n");
  2011. printk(KERN_INFO "PRODUCER (RX/OUT/SRC) PIPE DESCRIPTOR\n");
  2012. printk(KERN_INFO "==============================================\n");
  2013. iovec = pce_dev->ce_sps.out_transfer.iovec;
  2014. for (i = 0; i < pce_dev->ce_sps.out_transfer.iovec_count; i++) {
  2015. printk(KERN_INFO " [%d] addr=0x%x size=0x%x flags=0x%x\n", i,
  2016. iovec->addr, iovec->size, iovec->flags);
  2017. iovec++;
  2018. }
  2019. }
  2020. #else
  2021. static void _qce_dump_descr_fifos(struct qce_device *pce_dev)
  2022. {
  2023. }
  2024. #endif
  2025. static void _qce_dump_descr_fifos_fail(struct qce_device *pce_dev)
  2026. {
  2027. int i, j, ents;
  2028. struct sps_iovec *iovec = pce_dev->ce_sps.in_transfer.iovec;
  2029. uint32_t cmd_flags = SPS_IOVEC_FLAG_CMD;
  2030. printk(KERN_INFO "==============================================\n");
  2031. printk(KERN_INFO "CONSUMER (TX/IN/DEST) PIPE DESCRIPTOR\n");
  2032. printk(KERN_INFO "==============================================\n");
  2033. for (i = 0; i < pce_dev->ce_sps.in_transfer.iovec_count; i++) {
  2034. printk(KERN_INFO " [%d] addr=0x%x size=0x%x flags=0x%x\n", i,
  2035. iovec->addr, iovec->size, iovec->flags);
  2036. if (iovec->flags & cmd_flags) {
  2037. struct sps_command_element *pced;
  2038. pced = (struct sps_command_element *)
  2039. (GET_VIRT_ADDR(iovec->addr));
  2040. ents = iovec->size/(sizeof(struct sps_command_element));
  2041. for (j = 0; j < ents; j++) {
  2042. printk(KERN_INFO " [%d] [0x%x] 0x%x\n", j,
  2043. pced->addr, pced->data);
  2044. pced++;
  2045. }
  2046. }
  2047. iovec++;
  2048. }
  2049. printk(KERN_INFO "==============================================\n");
  2050. printk(KERN_INFO "PRODUCER (RX/OUT/SRC) PIPE DESCRIPTOR\n");
  2051. printk(KERN_INFO "==============================================\n");
  2052. iovec = pce_dev->ce_sps.out_transfer.iovec;
  2053. for (i = 0; i < pce_dev->ce_sps.out_transfer.iovec_count; i++) {
  2054. printk(KERN_INFO " [%d] addr=0x%x size=0x%x flags=0x%x\n", i,
  2055. iovec->addr, iovec->size, iovec->flags);
  2056. iovec++;
  2057. }
  2058. }
  2059. static void _qce_sps_iovec_count_init(struct qce_device *pce_dev)
  2060. {
  2061. pce_dev->ce_sps.in_transfer.iovec_count = 0;
  2062. pce_dev->ce_sps.out_transfer.iovec_count = 0;
  2063. }
  2064. static void _qce_set_flag(struct sps_transfer *sps_bam_pipe, uint32_t flag)
  2065. {
  2066. struct sps_iovec *iovec;
  2067. if (sps_bam_pipe->iovec_count == 0)
  2068. return;
  2069. iovec = sps_bam_pipe->iovec + (sps_bam_pipe->iovec_count - 1);
  2070. iovec->flags |= flag;
  2071. }
  2072. static int _qce_sps_add_data(uint32_t addr, uint32_t len,
  2073. struct sps_transfer *sps_bam_pipe)
  2074. {
  2075. struct sps_iovec *iovec = sps_bam_pipe->iovec +
  2076. sps_bam_pipe->iovec_count;
  2077. uint32_t data_cnt;
  2078. while (len > 0) {
  2079. if (sps_bam_pipe->iovec_count == QCE_MAX_NUM_DSCR) {
  2080. pr_err("Num of descrptor %d exceed max (%d)",
  2081. sps_bam_pipe->iovec_count,
  2082. (uint32_t)QCE_MAX_NUM_DSCR);
  2083. return -ENOMEM;
  2084. }
  2085. if (len > SPS_MAX_PKT_SIZE)
  2086. data_cnt = SPS_MAX_PKT_SIZE;
  2087. else
  2088. data_cnt = len;
  2089. iovec->size = data_cnt;
  2090. iovec->addr = addr;
  2091. iovec->flags = 0;
  2092. sps_bam_pipe->iovec_count++;
  2093. iovec++;
  2094. addr += data_cnt;
  2095. len -= data_cnt;
  2096. }
  2097. return 0;
  2098. }
  2099. static int _qce_sps_add_sg_data(struct qce_device *pce_dev,
  2100. struct scatterlist *sg_src, uint32_t nbytes,
  2101. struct sps_transfer *sps_bam_pipe)
  2102. {
  2103. uint32_t addr, data_cnt, len;
  2104. struct sps_iovec *iovec = sps_bam_pipe->iovec +
  2105. sps_bam_pipe->iovec_count;
  2106. while (nbytes > 0) {
  2107. len = min(nbytes, sg_dma_len(sg_src));
  2108. nbytes -= len;
  2109. addr = sg_dma_address(sg_src);
  2110. if (pce_dev->ce_sps.minor_version == 0)
  2111. len = ALIGN(len, pce_dev->ce_sps.ce_burst_size);
  2112. while (len > 0) {
  2113. if (sps_bam_pipe->iovec_count == QCE_MAX_NUM_DSCR) {
  2114. pr_err("Num of descrptor %d exceed max (%d)",
  2115. sps_bam_pipe->iovec_count,
  2116. (uint32_t)QCE_MAX_NUM_DSCR);
  2117. return -ENOMEM;
  2118. }
  2119. if (len > SPS_MAX_PKT_SIZE) {
  2120. data_cnt = SPS_MAX_PKT_SIZE;
  2121. iovec->size = data_cnt;
  2122. iovec->addr = addr;
  2123. iovec->flags = 0;
  2124. } else {
  2125. data_cnt = len;
  2126. iovec->size = data_cnt;
  2127. iovec->addr = addr;
  2128. iovec->flags = 0;
  2129. }
  2130. iovec++;
  2131. sps_bam_pipe->iovec_count++;
  2132. addr += data_cnt;
  2133. len -= data_cnt;
  2134. }
  2135. sg_src = scatterwalk_sg_next(sg_src);
  2136. }
  2137. return 0;
  2138. }
  2139. static int _qce_sps_add_cmd(struct qce_device *pce_dev, uint32_t flag,
  2140. struct qce_cmdlist_info *cmdptr,
  2141. struct sps_transfer *sps_bam_pipe)
  2142. {
  2143. struct sps_iovec *iovec = sps_bam_pipe->iovec +
  2144. sps_bam_pipe->iovec_count;
  2145. iovec->size = cmdptr->size;
  2146. iovec->addr = GET_PHYS_ADDR(cmdptr->cmdlist);
  2147. iovec->flags = SPS_IOVEC_FLAG_CMD | flag;
  2148. sps_bam_pipe->iovec_count++;
  2149. return 0;
  2150. }
  2151. static int _qce_sps_transfer(struct qce_device *pce_dev)
  2152. {
  2153. int rc = 0;
  2154. _qce_dump_descr_fifos(pce_dev);
  2155. if (pce_dev->ce_sps.in_transfer.iovec_count) {
  2156. rc = sps_transfer(pce_dev->ce_sps.consumer.pipe,
  2157. &pce_dev->ce_sps.in_transfer);
  2158. if (rc) {
  2159. pr_err("sps_xfr() fail (consumer pipe=0x%x) rc = %d,",
  2160. (u32)pce_dev->ce_sps.consumer.pipe, rc);
  2161. _qce_dump_descr_fifos_fail(pce_dev);
  2162. return rc;
  2163. }
  2164. }
  2165. rc = sps_transfer(pce_dev->ce_sps.producer.pipe,
  2166. &pce_dev->ce_sps.out_transfer);
  2167. if (rc) {
  2168. pr_err("sps_xfr() fail (producer pipe=0x%x) rc = %d,",
  2169. (u32)pce_dev->ce_sps.producer.pipe, rc);
  2170. return rc;
  2171. }
  2172. return rc;
  2173. }
  2174. /**
  2175. * Allocate and Connect a CE peripheral's SPS endpoint
  2176. *
  2177. * This function allocates endpoint context and
  2178. * connect it with memory endpoint by calling
  2179. * appropriate SPS driver APIs.
  2180. *
  2181. * Also registers a SPS callback function with
  2182. * SPS driver
  2183. *
  2184. * This function should only be called once typically
  2185. * during driver probe.
  2186. *
  2187. * @pce_dev - Pointer to qce_device structure
  2188. * @ep - Pointer to sps endpoint data structure
  2189. * @is_produce - 1 means Producer endpoint
  2190. * 0 means Consumer endpoint
  2191. *
  2192. * @return - 0 if successful else negative value.
  2193. *
  2194. */
  2195. static int qce_sps_init_ep_conn(struct qce_device *pce_dev,
  2196. struct qce_sps_ep_conn_data *ep,
  2197. bool is_producer)
  2198. {
  2199. int rc = 0;
  2200. struct sps_pipe *sps_pipe_info;
  2201. struct sps_connect *sps_connect_info = &ep->connect;
  2202. struct sps_register_event *sps_event = &ep->event;
  2203. /* Allocate endpoint context */
  2204. sps_pipe_info = sps_alloc_endpoint();
  2205. if (!sps_pipe_info) {
  2206. pr_err("sps_alloc_endpoint() failed!!! is_producer=%d",
  2207. is_producer);
  2208. rc = -ENOMEM;
  2209. goto out;
  2210. }
  2211. /* Now save the sps pipe handle */
  2212. ep->pipe = sps_pipe_info;
  2213. /* Get default connection configuration for an endpoint */
  2214. rc = sps_get_config(sps_pipe_info, sps_connect_info);
  2215. if (rc) {
  2216. pr_err("sps_get_config() fail pipe_handle=0x%x, rc = %d\n",
  2217. (u32)sps_pipe_info, rc);
  2218. goto get_config_err;
  2219. }
  2220. /* Modify the default connection configuration */
  2221. if (is_producer) {
  2222. /*
  2223. * For CE producer transfer, source should be
  2224. * CE peripheral where as destination should
  2225. * be system memory.
  2226. */
  2227. sps_connect_info->source = pce_dev->ce_sps.bam_handle;
  2228. sps_connect_info->destination = SPS_DEV_HANDLE_MEM;
  2229. /* Producer pipe will handle this connection */
  2230. sps_connect_info->mode = SPS_MODE_SRC;
  2231. sps_connect_info->options =
  2232. SPS_O_AUTO_ENABLE | SPS_O_DESC_DONE;
  2233. } else {
  2234. /* For CE consumer transfer, source should be
  2235. * system memory where as destination should
  2236. * CE peripheral
  2237. */
  2238. sps_connect_info->source = SPS_DEV_HANDLE_MEM;
  2239. sps_connect_info->destination = pce_dev->ce_sps.bam_handle;
  2240. sps_connect_info->mode = SPS_MODE_DEST;
  2241. sps_connect_info->options =
  2242. SPS_O_AUTO_ENABLE | SPS_O_EOT;
  2243. }
  2244. /* Producer pipe index */
  2245. sps_connect_info->src_pipe_index = pce_dev->ce_sps.src_pipe_index;
  2246. /* Consumer pipe index */
  2247. sps_connect_info->dest_pipe_index = pce_dev->ce_sps.dest_pipe_index;
  2248. /* Set pipe group */
  2249. sps_connect_info->lock_group = pce_dev->ce_sps.pipe_pair_index;
  2250. sps_connect_info->event_thresh = 0x10;
  2251. /*
  2252. * Max. no of scatter/gather buffers that can
  2253. * be passed by block layer = 32 (NR_SG).
  2254. * Each BAM descritor needs 64 bits (8 bytes).
  2255. * One BAM descriptor is required per buffer transfer.
  2256. * So we would require total 256 (32 * 8) bytes of descriptor FIFO.
  2257. * But due to HW limitation we need to allocate atleast one extra
  2258. * descriptor memory (256 bytes + 8 bytes). But in order to be
  2259. * in power of 2, we are allocating 512 bytes of memory.
  2260. */
  2261. sps_connect_info->desc.size = QCE_MAX_NUM_DSCR *
  2262. sizeof(struct sps_iovec);
  2263. sps_connect_info->desc.base = dma_alloc_coherent(pce_dev->pdev,
  2264. sps_connect_info->desc.size,
  2265. &sps_connect_info->desc.phys_base,
  2266. GFP_KERNEL);
  2267. if (sps_connect_info->desc.base == NULL) {
  2268. rc = -ENOMEM;
  2269. pr_err("Can not allocate coherent memory for sps data\n");
  2270. goto get_config_err;
  2271. }
  2272. memset(sps_connect_info->desc.base, 0x00, sps_connect_info->desc.size);
  2273. /* Establish connection between peripheral and memory endpoint */
  2274. rc = sps_connect(sps_pipe_info, sps_connect_info);
  2275. if (rc) {
  2276. pr_err("sps_connect() fail pipe_handle=0x%x, rc = %d\n",
  2277. (u32)sps_pipe_info, rc);
  2278. goto sps_connect_err;
  2279. }
  2280. sps_event->mode = SPS_TRIGGER_CALLBACK;
  2281. if (is_producer)
  2282. sps_event->options = SPS_O_EOT | SPS_O_DESC_DONE;
  2283. else
  2284. sps_event->options = SPS_O_EOT;
  2285. sps_event->xfer_done = NULL;
  2286. sps_event->user = (void *)pce_dev;
  2287. pr_debug("success, %s : pipe_handle=0x%x, desc fifo base (phy) = 0x%x\n",
  2288. is_producer ? "PRODUCER(RX/OUT)" : "CONSUMER(TX/IN)",
  2289. (u32)sps_pipe_info, sps_connect_info->desc.phys_base);
  2290. goto out;
  2291. sps_connect_err:
  2292. dma_free_coherent(pce_dev->pdev,
  2293. sps_connect_info->desc.size,
  2294. sps_connect_info->desc.base,
  2295. sps_connect_info->desc.phys_base);
  2296. get_config_err:
  2297. sps_free_endpoint(sps_pipe_info);
  2298. out:
  2299. return rc;
  2300. }
  2301. /**
  2302. * Disconnect and Deallocate a CE peripheral's SPS endpoint
  2303. *
  2304. * This function disconnect endpoint and deallocates
  2305. * endpoint context.
  2306. *
  2307. * This function should only be called once typically
  2308. * during driver remove.
  2309. *
  2310. * @pce_dev - Pointer to qce_device structure
  2311. * @ep - Pointer to sps endpoint data structure
  2312. *
  2313. */
  2314. static void qce_sps_exit_ep_conn(struct qce_device *pce_dev,
  2315. struct qce_sps_ep_conn_data *ep)
  2316. {
  2317. struct sps_pipe *sps_pipe_info = ep->pipe;
  2318. struct sps_connect *sps_connect_info = &ep->connect;
  2319. sps_disconnect(sps_pipe_info);
  2320. dma_free_coherent(pce_dev->pdev,
  2321. sps_connect_info->desc.size,
  2322. sps_connect_info->desc.base,
  2323. sps_connect_info->desc.phys_base);
  2324. sps_free_endpoint(sps_pipe_info);
  2325. }
  2326. static void qce_sps_release_bam(struct qce_device *pce_dev)
  2327. {
  2328. struct bam_registration_info *pbam;
  2329. mutex_lock(&bam_register_lock);
  2330. pbam = pce_dev->pbam;
  2331. if (pbam == NULL)
  2332. goto ret;
  2333. pbam->cnt--;
  2334. if (pbam->cnt > 0)
  2335. goto ret;
  2336. if (pce_dev->ce_sps.bam_handle) {
  2337. sps_deregister_bam_device(pce_dev->ce_sps.bam_handle);
  2338. pr_debug("deregister bam handle %x\n",
  2339. pce_dev->ce_sps.bam_handle);
  2340. pce_dev->ce_sps.bam_handle = 0;
  2341. }
  2342. iounmap(pbam->bam_iobase);
  2343. pr_debug("delete bam 0x%x\n", pbam->bam_mem);
  2344. list_del(&pbam->qlist);
  2345. kfree(pbam);
  2346. ret:
  2347. pce_dev->pbam = NULL;
  2348. mutex_unlock(&bam_register_lock);
  2349. }
  2350. static int qce_sps_get_bam(struct qce_device *pce_dev)
  2351. {
  2352. int rc = 0;
  2353. struct sps_bam_props bam = {0};
  2354. struct bam_registration_info *pbam = NULL;
  2355. struct bam_registration_info *p;
  2356. uint32_t bam_cfg = 0;
  2357. mutex_lock(&bam_register_lock);
  2358. list_for_each_entry(p, &qce50_bam_list, qlist) {
  2359. if (p->bam_mem == pce_dev->bam_mem) {
  2360. pbam = p; /* found */
  2361. break;
  2362. }
  2363. }
  2364. if (pbam) {
  2365. pr_debug("found bam 0x%x\n", pbam->bam_mem);
  2366. pbam->cnt++;
  2367. pce_dev->ce_sps.bam_handle = pbam->handle;
  2368. pce_dev->ce_sps.bam_mem = pbam->bam_mem;
  2369. pce_dev->ce_sps.bam_iobase = pbam->bam_iobase;
  2370. pce_dev->pbam = pbam;
  2371. pce_dev->support_cmd_dscr = pbam->support_cmd_dscr;
  2372. goto ret;
  2373. }
  2374. pbam = kzalloc(sizeof(struct bam_registration_info), GFP_KERNEL);
  2375. if (!pbam) {
  2376. pr_err("qce50 Memory allocation of bam FAIL, error %ld\n",
  2377. PTR_ERR(pbam));
  2378. rc = -ENOMEM;
  2379. goto ret;
  2380. }
  2381. pbam->cnt = 1;
  2382. pbam->bam_mem = pce_dev->bam_mem;
  2383. pbam->bam_iobase = ioremap_nocache(pce_dev->bam_mem,
  2384. pce_dev->bam_mem_size);
  2385. if (!pbam->bam_iobase) {
  2386. kfree(pbam);
  2387. rc = -ENOMEM;
  2388. pr_err("Can not map BAM io memory\n");
  2389. goto ret;
  2390. }
  2391. pce_dev->ce_sps.bam_mem = pbam->bam_mem;
  2392. pce_dev->ce_sps.bam_iobase = pbam->bam_iobase;
  2393. pbam->handle = 0;
  2394. pr_debug("allocate bam 0x%x\n", pbam->bam_mem);
  2395. bam_cfg = readl_relaxed(pce_dev->ce_sps.bam_iobase +
  2396. CRYPTO_BAM_CNFG_BITS_REG);
  2397. pbam->support_cmd_dscr = (bam_cfg & CRYPTO_BAM_CD_ENABLE_MASK) ?
  2398. true : false;
  2399. if (pbam->support_cmd_dscr == false) {
  2400. pr_info("qce50 don't support command descriptor. bam_cfg%x\n",
  2401. bam_cfg);
  2402. }
  2403. pce_dev->support_cmd_dscr = pbam->support_cmd_dscr;
  2404. bam.phys_addr = pce_dev->ce_sps.bam_mem;
  2405. bam.virt_addr = pce_dev->ce_sps.bam_iobase;
  2406. /*
  2407. * This event thresold value is only significant for BAM-to-BAM
  2408. * transfer. It's ignored for BAM-to-System mode transfer.
  2409. */
  2410. bam.event_threshold = 0x10; /* Pipe event threshold */
  2411. /*
  2412. * This threshold controls when the BAM publish
  2413. * the descriptor size on the sideband interface.
  2414. * SPS HW will only be used when
  2415. * data transfer size > 64 bytes.
  2416. */
  2417. bam.summing_threshold = 64;
  2418. /* SPS driver wll handle the crypto BAM IRQ */
  2419. bam.irq = (u32)pce_dev->ce_sps.bam_irq;
  2420. /*
  2421. * Set flag to indicate BAM global device control is managed
  2422. * remotely.
  2423. */
  2424. if ((pce_dev->support_cmd_dscr == false) || (pce_dev->is_shared))
  2425. bam.manage = SPS_BAM_MGR_DEVICE_REMOTE;
  2426. else
  2427. bam.manage = SPS_BAM_MGR_LOCAL;
  2428. bam.ee = 1;
  2429. pr_debug("bam physical base=0x%x\n", (u32)bam.phys_addr);
  2430. pr_debug("bam virtual base=0x%x\n", (u32)bam.virt_addr);
  2431. /* Register CE Peripheral BAM device to SPS driver */
  2432. rc = sps_register_bam_device(&bam, &pbam->handle);
  2433. if (rc) {
  2434. pr_err("sps_register_bam_device() failed! err=%d", rc);
  2435. rc = -EIO;
  2436. iounmap(pbam->bam_iobase);
  2437. kfree(pbam);
  2438. goto ret;
  2439. }
  2440. pce_dev->pbam = pbam;
  2441. list_add_tail(&pbam->qlist, &qce50_bam_list);
  2442. pce_dev->ce_sps.bam_handle = pbam->handle;
  2443. ret:
  2444. mutex_unlock(&bam_register_lock);
  2445. return rc;
  2446. }
  2447. /**
  2448. * Initialize SPS HW connected with CE core
  2449. *
  2450. * This function register BAM HW resources with
  2451. * SPS driver and then initialize 2 SPS endpoints
  2452. *
  2453. * This function should only be called once typically
  2454. * during driver probe.
  2455. *
  2456. * @pce_dev - Pointer to qce_device structure
  2457. *
  2458. * @return - 0 if successful else negative value.
  2459. *
  2460. */
  2461. static int qce_sps_init(struct qce_device *pce_dev)
  2462. {
  2463. int rc = 0;
  2464. rc = qce_sps_get_bam(pce_dev);
  2465. if (rc)
  2466. return rc;
  2467. pr_debug("BAM device registered. bam_handle=0x%x",
  2468. pce_dev->ce_sps.bam_handle);
  2469. rc = qce_sps_init_ep_conn(pce_dev, &pce_dev->ce_sps.producer, true);
  2470. if (rc)
  2471. goto sps_connect_producer_err;
  2472. rc = qce_sps_init_ep_conn(pce_dev, &pce_dev->ce_sps.consumer, false);
  2473. if (rc)
  2474. goto sps_connect_consumer_err;
  2475. pce_dev->ce_sps.out_transfer.user = pce_dev->ce_sps.producer.pipe;
  2476. pce_dev->ce_sps.in_transfer.user = pce_dev->ce_sps.consumer.pipe;
  2477. pr_info(" Qualcomm MSM CE-BAM at 0x%016llx irq %d\n",
  2478. (unsigned long long)pce_dev->ce_sps.bam_mem,
  2479. (unsigned int)pce_dev->ce_sps.bam_irq);
  2480. return rc;
  2481. sps_connect_consumer_err:
  2482. qce_sps_exit_ep_conn(pce_dev, &pce_dev->ce_sps.producer);
  2483. sps_connect_producer_err:
  2484. qce_sps_release_bam(pce_dev);
  2485. return rc;
  2486. }
  2487. /**
  2488. * De-initialize SPS HW connected with CE core
  2489. *
  2490. * This function deinitialize SPS endpoints and then
  2491. * deregisters BAM resources from SPS driver.
  2492. *
  2493. * This function should only be called once typically
  2494. * during driver remove.
  2495. *
  2496. * @pce_dev - Pointer to qce_device structure
  2497. *
  2498. */
  2499. static void qce_sps_exit(struct qce_device *pce_dev)
  2500. {
  2501. qce_sps_exit_ep_conn(pce_dev, &pce_dev->ce_sps.consumer);
  2502. qce_sps_exit_ep_conn(pce_dev, &pce_dev->ce_sps.producer);
  2503. qce_sps_release_bam(pce_dev);
  2504. }
  2505. static void _aead_sps_producer_callback(struct sps_event_notify *notify)
  2506. {
  2507. struct qce_device *pce_dev = (struct qce_device *)
  2508. ((struct sps_event_notify *)notify)->user;
  2509. pce_dev->ce_sps.notify = *notify;
  2510. pr_debug("sps ev_id=%d, addr=0x%x, size=0x%x, flags=0x%x\n",
  2511. notify->event_id,
  2512. notify->data.transfer.iovec.addr,
  2513. notify->data.transfer.iovec.size,
  2514. notify->data.transfer.iovec.flags);
  2515. if (pce_dev->ce_sps.producer_state == QCE_PIPE_STATE_COMP) {
  2516. pce_dev->ce_sps.producer_state = QCE_PIPE_STATE_IDLE;
  2517. /* done */
  2518. _aead_complete(pce_dev);
  2519. } else {
  2520. int rc = 0;
  2521. pce_dev->ce_sps.producer_state = QCE_PIPE_STATE_COMP;
  2522. pce_dev->ce_sps.out_transfer.iovec_count = 0;
  2523. _qce_sps_add_data(GET_PHYS_ADDR(pce_dev->ce_sps.result_dump),
  2524. CRYPTO_RESULT_DUMP_SIZE,
  2525. &pce_dev->ce_sps.out_transfer);
  2526. _qce_set_flag(&pce_dev->ce_sps.out_transfer,
  2527. SPS_IOVEC_FLAG_INT);
  2528. rc = sps_transfer(pce_dev->ce_sps.producer.pipe,
  2529. &pce_dev->ce_sps.out_transfer);
  2530. if (rc) {
  2531. pr_err("sps_xfr() fail (producer pipe=0x%x) rc = %d,",
  2532. (u32)pce_dev->ce_sps.producer.pipe, rc);
  2533. }
  2534. }
  2535. };
  2536. static void _sha_sps_producer_callback(struct sps_event_notify *notify)
  2537. {
  2538. struct qce_device *pce_dev = (struct qce_device *)
  2539. ((struct sps_event_notify *)notify)->user;
  2540. pce_dev->ce_sps.notify = *notify;
  2541. pr_debug("sps ev_id=%d, addr=0x%x, size=0x%x, flags=0x%x\n",
  2542. notify->event_id,
  2543. notify->data.transfer.iovec.addr,
  2544. notify->data.transfer.iovec.size,
  2545. notify->data.transfer.iovec.flags);
  2546. /* done */
  2547. _sha_complete(pce_dev);
  2548. };
  2549. static void _f9_sps_producer_callback(struct sps_event_notify *notify)
  2550. {
  2551. struct qce_device *pce_dev = (struct qce_device *)
  2552. ((struct sps_event_notify *)notify)->user;
  2553. pce_dev->ce_sps.notify = *notify;
  2554. pr_debug("sps ev_id=%d, addr=0x%x, size=0x%x, flags=0x%x\n",
  2555. notify->event_id,
  2556. notify->data.transfer.iovec.addr,
  2557. notify->data.transfer.iovec.size,
  2558. notify->data.transfer.iovec.flags);
  2559. /* done */
  2560. _f9_complete(pce_dev);
  2561. }
  2562. static void _f8_sps_producer_callback(struct sps_event_notify *notify)
  2563. {
  2564. struct qce_device *pce_dev = (struct qce_device *)
  2565. ((struct sps_event_notify *)notify)->user;
  2566. pce_dev->ce_sps.notify = *notify;
  2567. pr_debug("sps ev_id=%d, addr=0x%x, size=0x%x, flags=0x%x\n",
  2568. notify->event_id,
  2569. notify->data.transfer.iovec.addr,
  2570. notify->data.transfer.iovec.size,
  2571. notify->data.transfer.iovec.flags);
  2572. if (pce_dev->ce_sps.producer_state == QCE_PIPE_STATE_COMP) {
  2573. pce_dev->ce_sps.producer_state = QCE_PIPE_STATE_IDLE;
  2574. /* done */
  2575. _f8_complete(pce_dev);
  2576. } else {
  2577. int rc = 0;
  2578. pce_dev->ce_sps.producer_state = QCE_PIPE_STATE_COMP;
  2579. pce_dev->ce_sps.out_transfer.iovec_count = 0;
  2580. _qce_sps_add_data(GET_PHYS_ADDR(pce_dev->ce_sps.result_dump),
  2581. CRYPTO_RESULT_DUMP_SIZE,
  2582. &pce_dev->ce_sps.out_transfer);
  2583. _qce_set_flag(&pce_dev->ce_sps.out_transfer,
  2584. SPS_IOVEC_FLAG_EOT|SPS_IOVEC_FLAG_INT);
  2585. rc = sps_transfer(pce_dev->ce_sps.producer.pipe,
  2586. &pce_dev->ce_sps.out_transfer);
  2587. if (rc) {
  2588. pr_err("sps_xfr() fail (producer pipe=0x%x) rc = %d,",
  2589. (u32)pce_dev->ce_sps.producer.pipe, rc);
  2590. }
  2591. }
  2592. }
  2593. static void _ablk_cipher_sps_producer_callback(struct sps_event_notify *notify)
  2594. {
  2595. struct qce_device *pce_dev = (struct qce_device *)
  2596. ((struct sps_event_notify *)notify)->user;
  2597. pce_dev->ce_sps.notify = *notify;
  2598. pr_debug("sps ev_id=%d, addr=0x%x, size=0x%x, flags=0x%x\n",
  2599. notify->event_id,
  2600. notify->data.transfer.iovec.addr,
  2601. notify->data.transfer.iovec.size,
  2602. notify->data.transfer.iovec.flags);
  2603. if (pce_dev->ce_sps.producer_state == QCE_PIPE_STATE_COMP) {
  2604. pce_dev->ce_sps.producer_state = QCE_PIPE_STATE_IDLE;
  2605. /* done */
  2606. _ablk_cipher_complete(pce_dev);
  2607. } else {
  2608. int rc = 0;
  2609. pce_dev->ce_sps.producer_state = QCE_PIPE_STATE_COMP;
  2610. pce_dev->ce_sps.out_transfer.iovec_count = 0;
  2611. _qce_sps_add_data(GET_PHYS_ADDR(pce_dev->ce_sps.result_dump),
  2612. CRYPTO_RESULT_DUMP_SIZE,
  2613. &pce_dev->ce_sps.out_transfer);
  2614. _qce_set_flag(&pce_dev->ce_sps.out_transfer,
  2615. SPS_IOVEC_FLAG_INT);
  2616. rc = sps_transfer(pce_dev->ce_sps.producer.pipe,
  2617. &pce_dev->ce_sps.out_transfer);
  2618. if (rc) {
  2619. pr_err("sps_xfr() fail (producer pipe=0x%x) rc = %d,",
  2620. (u32)pce_dev->ce_sps.producer.pipe, rc);
  2621. }
  2622. }
  2623. };
  2624. static void qce_add_cmd_element(struct qce_device *pdev,
  2625. struct sps_command_element **cmd_ptr, u32 addr,
  2626. u32 data, struct sps_command_element **populate)
  2627. {
  2628. (*cmd_ptr)->addr = (uint32_t)(addr + pdev->phy_iobase);
  2629. (*cmd_ptr)->data = data;
  2630. (*cmd_ptr)->mask = 0xFFFFFFFF;
  2631. if (populate != NULL)
  2632. *populate = *cmd_ptr;
  2633. (*cmd_ptr)++;
  2634. }
  2635. static int _setup_cipher_aes_cmdlistptrs(struct qce_device *pdev,
  2636. unsigned char **pvaddr, enum qce_cipher_mode_enum mode,
  2637. bool key_128)
  2638. {
  2639. struct sps_command_element *ce_vaddr;
  2640. uint32_t ce_vaddr_start;
  2641. struct qce_cmdlistptr_ops *cmdlistptr = &pdev->ce_sps.cmdlistptr;
  2642. struct qce_cmdlist_info *pcl_info = NULL;
  2643. int i = 0;
  2644. uint32_t encr_cfg = 0;
  2645. uint32_t key_reg = 0;
  2646. uint32_t xts_key_reg = 0;
  2647. uint32_t iv_reg = 0;
  2648. *pvaddr = (unsigned char *) ALIGN(((unsigned int)(*pvaddr)),
  2649. pdev->ce_sps.ce_burst_size);
  2650. ce_vaddr = (struct sps_command_element *)(*pvaddr);
  2651. ce_vaddr_start = (uint32_t)(*pvaddr);
  2652. /*
  2653. * Designate chunks of the allocated memory to various
  2654. * command list pointers related to AES cipher operations defined
  2655. * in ce_cmdlistptrs_ops structure.
  2656. */
  2657. switch (mode) {
  2658. case QCE_MODE_CBC:
  2659. case QCE_MODE_CTR:
  2660. if (key_128 == true) {
  2661. cmdlistptr->cipher_aes_128_cbc_ctr.cmdlist =
  2662. (uint32_t)ce_vaddr;
  2663. pcl_info = &(cmdlistptr->cipher_aes_128_cbc_ctr);
  2664. if (mode == QCE_MODE_CBC)
  2665. encr_cfg = pdev->reg.encr_cfg_aes_cbc_128;
  2666. else
  2667. encr_cfg = pdev->reg.encr_cfg_aes_ctr_128;
  2668. iv_reg = 4;
  2669. key_reg = 4;
  2670. xts_key_reg = 0;
  2671. } else {
  2672. cmdlistptr->cipher_aes_256_cbc_ctr.cmdlist =
  2673. (uint32_t)ce_vaddr;
  2674. pcl_info = &(cmdlistptr->cipher_aes_256_cbc_ctr);
  2675. if (mode == QCE_MODE_CBC)
  2676. encr_cfg = pdev->reg.encr_cfg_aes_cbc_256;
  2677. else
  2678. encr_cfg = pdev->reg.encr_cfg_aes_ctr_256;
  2679. iv_reg = 4;
  2680. key_reg = 8;
  2681. xts_key_reg = 0;
  2682. }
  2683. break;
  2684. case QCE_MODE_ECB:
  2685. if (key_128 == true) {
  2686. cmdlistptr->cipher_aes_128_ecb.cmdlist =
  2687. (uint32_t)ce_vaddr;
  2688. pcl_info = &(cmdlistptr->cipher_aes_128_ecb);
  2689. encr_cfg = pdev->reg.encr_cfg_aes_ecb_128;
  2690. iv_reg = 0;
  2691. key_reg = 4;
  2692. xts_key_reg = 0;
  2693. } else {
  2694. cmdlistptr->cipher_aes_256_ecb.cmdlist =
  2695. (uint32_t)ce_vaddr;
  2696. pcl_info = &(cmdlistptr->cipher_aes_256_ecb);
  2697. encr_cfg = pdev->reg.encr_cfg_aes_ecb_256;
  2698. iv_reg = 0;
  2699. key_reg = 8;
  2700. xts_key_reg = 0;
  2701. }
  2702. break;
  2703. case QCE_MODE_XTS:
  2704. if (key_128 == true) {
  2705. cmdlistptr->cipher_aes_128_xts.cmdlist =
  2706. (uint32_t)ce_vaddr;
  2707. pcl_info = &(cmdlistptr->cipher_aes_128_xts);
  2708. encr_cfg = pdev->reg.encr_cfg_aes_xts_128;
  2709. iv_reg = 4;
  2710. key_reg = 4;
  2711. xts_key_reg = 4;
  2712. } else {
  2713. cmdlistptr->cipher_aes_256_xts.cmdlist =
  2714. (uint32_t)ce_vaddr;
  2715. pcl_info = &(cmdlistptr->cipher_aes_256_xts);
  2716. encr_cfg = pdev->reg.encr_cfg_aes_xts_256;
  2717. iv_reg = 4;
  2718. key_reg = 8;
  2719. xts_key_reg = 8;
  2720. }
  2721. break;
  2722. default:
  2723. pr_err("Unknown mode of operation %d received, exiting now\n",
  2724. mode);
  2725. return -EINVAL;
  2726. break;
  2727. }
  2728. /* clear status register */
  2729. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_STATUS_REG, 0, NULL);
  2730. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CONFIG_REG,
  2731. pdev->reg.crypto_cfg_be, &pcl_info->crypto_cfg);
  2732. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_SEG_SIZE_REG, 0,
  2733. &pcl_info->seg_size);
  2734. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_SEG_CFG_REG, encr_cfg,
  2735. &pcl_info->encr_seg_cfg);
  2736. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_SEG_SIZE_REG, 0,
  2737. &pcl_info->encr_seg_size);
  2738. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_SEG_START_REG, 0,
  2739. &pcl_info->encr_seg_start);
  2740. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CNTR_MASK_REG,
  2741. (uint32_t)0xffffffff, &pcl_info->encr_mask);
  2742. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_SEG_CFG_REG, 0,
  2743. &pcl_info->auth_seg_cfg);
  2744. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_KEY0_REG, 0,
  2745. &pcl_info->encr_key);
  2746. for (i = 1; i < key_reg; i++)
  2747. qce_add_cmd_element(pdev, &ce_vaddr,
  2748. (CRYPTO_ENCR_KEY0_REG + i * sizeof(uint32_t)),
  2749. 0, NULL);
  2750. if (xts_key_reg) {
  2751. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_XTS_KEY0_REG,
  2752. 0, &pcl_info->encr_xts_key);
  2753. for (i = 1; i < xts_key_reg; i++)
  2754. qce_add_cmd_element(pdev, &ce_vaddr,
  2755. (CRYPTO_ENCR_XTS_KEY0_REG +
  2756. i * sizeof(uint32_t)), 0, NULL);
  2757. qce_add_cmd_element(pdev, &ce_vaddr,
  2758. CRYPTO_ENCR_XTS_DU_SIZE_REG, 0,
  2759. &pcl_info->encr_xts_du_size);
  2760. }
  2761. if (iv_reg) {
  2762. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CNTR0_IV0_REG, 0,
  2763. &pcl_info->encr_cntr_iv);
  2764. for (i = 1; i < iv_reg; i++)
  2765. qce_add_cmd_element(pdev, &ce_vaddr,
  2766. (CRYPTO_CNTR0_IV0_REG + i * sizeof(uint32_t)),
  2767. 0, NULL);
  2768. }
  2769. /* Add dummy to align size to burst-size multiple */
  2770. if (mode == QCE_MODE_XTS) {
  2771. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_SEG_SIZE_REG,
  2772. 0, &pcl_info->auth_seg_size);
  2773. } else {
  2774. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_SEG_SIZE_REG,
  2775. 0, &pcl_info->auth_seg_size);
  2776. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_SEG_START_REG,
  2777. 0, &pcl_info->auth_seg_size);
  2778. }
  2779. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CONFIG_REG,
  2780. pdev->reg.crypto_cfg_le, NULL);
  2781. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_GOPROC_REG,
  2782. ((1 << CRYPTO_GO) | (1 << CRYPTO_RESULTS_DUMP) |
  2783. (1 << CRYPTO_CLR_CNTXT)), &pcl_info->go_proc);
  2784. pcl_info->size = (uint32_t)ce_vaddr - (uint32_t)ce_vaddr_start;
  2785. *pvaddr = (unsigned char *) ce_vaddr;
  2786. return 0;
  2787. }
  2788. static int _setup_cipher_des_cmdlistptrs(struct qce_device *pdev,
  2789. unsigned char **pvaddr, enum qce_cipher_alg_enum alg,
  2790. bool mode_cbc)
  2791. {
  2792. struct sps_command_element *ce_vaddr;
  2793. uint32_t ce_vaddr_start;
  2794. struct qce_cmdlistptr_ops *cmdlistptr = &pdev->ce_sps.cmdlistptr;
  2795. struct qce_cmdlist_info *pcl_info = NULL;
  2796. int i = 0;
  2797. uint32_t encr_cfg = 0;
  2798. uint32_t key_reg = 0;
  2799. uint32_t iv_reg = 0;
  2800. *pvaddr = (unsigned char *) ALIGN(((unsigned int)(*pvaddr)),
  2801. pdev->ce_sps.ce_burst_size);
  2802. ce_vaddr = (struct sps_command_element *)(*pvaddr);
  2803. ce_vaddr_start = (uint32_t)(*pvaddr);
  2804. /*
  2805. * Designate chunks of the allocated memory to various
  2806. * command list pointers related to cipher operations defined
  2807. * in ce_cmdlistptrs_ops structure.
  2808. */
  2809. switch (alg) {
  2810. case CIPHER_ALG_DES:
  2811. if (mode_cbc) {
  2812. cmdlistptr->cipher_des_cbc.cmdlist =
  2813. (uint32_t)ce_vaddr;
  2814. pcl_info = &(cmdlistptr->cipher_des_cbc);
  2815. encr_cfg = pdev->reg.encr_cfg_des_cbc;
  2816. iv_reg = 2;
  2817. key_reg = 2;
  2818. } else {
  2819. cmdlistptr->cipher_des_ecb.cmdlist =
  2820. (uint32_t)ce_vaddr;
  2821. pcl_info = &(cmdlistptr->cipher_des_ecb);
  2822. encr_cfg = pdev->reg.encr_cfg_des_ecb;
  2823. iv_reg = 0;
  2824. key_reg = 2;
  2825. }
  2826. break;
  2827. case CIPHER_ALG_3DES:
  2828. if (mode_cbc) {
  2829. cmdlistptr->cipher_3des_cbc.cmdlist =
  2830. (uint32_t)ce_vaddr;
  2831. pcl_info = &(cmdlistptr->cipher_3des_cbc);
  2832. encr_cfg = pdev->reg.encr_cfg_3des_cbc;
  2833. iv_reg = 2;
  2834. key_reg = 6;
  2835. } else {
  2836. cmdlistptr->cipher_3des_ecb.cmdlist =
  2837. (uint32_t)ce_vaddr;
  2838. pcl_info = &(cmdlistptr->cipher_3des_ecb);
  2839. encr_cfg = pdev->reg.encr_cfg_3des_ecb;
  2840. iv_reg = 0;
  2841. key_reg = 6;
  2842. }
  2843. break;
  2844. default:
  2845. pr_err("Unknown algorithms %d received, exiting now\n", alg);
  2846. return -EINVAL;
  2847. break;
  2848. }
  2849. /* clear status register */
  2850. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_STATUS_REG, 0, NULL);
  2851. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CONFIG_REG,
  2852. pdev->reg.crypto_cfg_be, &pcl_info->crypto_cfg);
  2853. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_SEG_SIZE_REG, 0,
  2854. &pcl_info->seg_size);
  2855. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_SEG_CFG_REG, encr_cfg,
  2856. &pcl_info->encr_seg_cfg);
  2857. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_SEG_SIZE_REG, 0,
  2858. &pcl_info->encr_seg_size);
  2859. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_SEG_START_REG, 0,
  2860. &pcl_info->encr_seg_start);
  2861. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_SEG_CFG_REG, 0,
  2862. &pcl_info->auth_seg_cfg);
  2863. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_KEY0_REG, 0,
  2864. &pcl_info->encr_key);
  2865. for (i = 1; i < key_reg; i++)
  2866. qce_add_cmd_element(pdev, &ce_vaddr,
  2867. (CRYPTO_ENCR_KEY0_REG + i * sizeof(uint32_t)),
  2868. 0, NULL);
  2869. if (iv_reg) {
  2870. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CNTR0_IV0_REG, 0,
  2871. &pcl_info->encr_cntr_iv);
  2872. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CNTR1_IV1_REG, 0,
  2873. NULL);
  2874. }
  2875. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CONFIG_REG,
  2876. pdev->reg.crypto_cfg_le, NULL);
  2877. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_GOPROC_REG,
  2878. ((1 << CRYPTO_GO) | (1 << CRYPTO_RESULTS_DUMP) |
  2879. (1 << CRYPTO_CLR_CNTXT)), &pcl_info->go_proc);
  2880. pcl_info->size = (uint32_t)ce_vaddr - (uint32_t)ce_vaddr_start;
  2881. *pvaddr = (unsigned char *) ce_vaddr;
  2882. return 0;
  2883. }
  2884. static int _setup_auth_cmdlistptrs(struct qce_device *pdev,
  2885. unsigned char **pvaddr, enum qce_hash_alg_enum alg,
  2886. bool key_128)
  2887. {
  2888. struct sps_command_element *ce_vaddr;
  2889. uint32_t ce_vaddr_start;
  2890. struct qce_cmdlistptr_ops *cmdlistptr = &pdev->ce_sps.cmdlistptr;
  2891. struct qce_cmdlist_info *pcl_info = NULL;
  2892. int i = 0;
  2893. uint32_t key_reg = 0;
  2894. uint32_t auth_cfg = 0;
  2895. uint32_t iv_reg = 0;
  2896. *pvaddr = (unsigned char *) ALIGN(((unsigned int)(*pvaddr)),
  2897. pdev->ce_sps.ce_burst_size);
  2898. ce_vaddr_start = (uint32_t)(*pvaddr);
  2899. ce_vaddr = (struct sps_command_element *)(*pvaddr);
  2900. /*
  2901. * Designate chunks of the allocated memory to various
  2902. * command list pointers related to authentication operations
  2903. * defined in ce_cmdlistptrs_ops structure.
  2904. */
  2905. switch (alg) {
  2906. case QCE_HASH_SHA1:
  2907. cmdlistptr->auth_sha1.cmdlist = (uint32_t)ce_vaddr;
  2908. pcl_info = &(cmdlistptr->auth_sha1);
  2909. auth_cfg = pdev->reg.auth_cfg_sha1;
  2910. iv_reg = 5;
  2911. /* clear status register */
  2912. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_STATUS_REG,
  2913. 0, NULL);
  2914. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CONFIG_REG,
  2915. pdev->reg.crypto_cfg_be, &pcl_info->crypto_cfg);
  2916. break;
  2917. case QCE_HASH_SHA256:
  2918. cmdlistptr->auth_sha256.cmdlist = (uint32_t)ce_vaddr;
  2919. pcl_info = &(cmdlistptr->auth_sha256);
  2920. auth_cfg = pdev->reg.auth_cfg_sha256;
  2921. iv_reg = 8;
  2922. /* clear status register */
  2923. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_STATUS_REG,
  2924. 0, NULL);
  2925. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CONFIG_REG,
  2926. pdev->reg.crypto_cfg_be, &pcl_info->crypto_cfg);
  2927. /* 1 dummy write */
  2928. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_SEG_SIZE_REG,
  2929. 0, NULL);
  2930. break;
  2931. case QCE_HASH_SHA1_HMAC:
  2932. cmdlistptr->auth_sha1_hmac.cmdlist = (uint32_t)ce_vaddr;
  2933. pcl_info = &(cmdlistptr->auth_sha1_hmac);
  2934. auth_cfg = pdev->reg.auth_cfg_hmac_sha1;
  2935. key_reg = 16;
  2936. iv_reg = 5;
  2937. /* clear status register */
  2938. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_STATUS_REG,
  2939. 0, NULL);
  2940. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CONFIG_REG,
  2941. pdev->reg.crypto_cfg_be, &pcl_info->crypto_cfg);
  2942. break;
  2943. case QCE_HASH_SHA256_HMAC:
  2944. cmdlistptr->auth_sha256_hmac.cmdlist = (uint32_t)ce_vaddr;
  2945. pcl_info = &(cmdlistptr->auth_sha256_hmac);
  2946. auth_cfg = pdev->reg.auth_cfg_hmac_sha256;
  2947. key_reg = 16;
  2948. iv_reg = 8;
  2949. /* clear status register */
  2950. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_STATUS_REG, 0,
  2951. NULL);
  2952. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CONFIG_REG,
  2953. pdev->reg.crypto_cfg_be, &pcl_info->crypto_cfg);
  2954. /* 1 dummy write */
  2955. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_SEG_SIZE_REG,
  2956. 0, NULL);
  2957. break;
  2958. case QCE_HASH_AES_CMAC:
  2959. if (key_128 == true) {
  2960. cmdlistptr->auth_aes_128_cmac.cmdlist =
  2961. (uint32_t)ce_vaddr;
  2962. pcl_info = &(cmdlistptr->auth_aes_128_cmac);
  2963. auth_cfg = pdev->reg.auth_cfg_cmac_128;
  2964. key_reg = 4;
  2965. } else {
  2966. cmdlistptr->auth_aes_256_cmac.cmdlist =
  2967. (uint32_t)ce_vaddr;
  2968. pcl_info = &(cmdlistptr->auth_aes_256_cmac);
  2969. auth_cfg = pdev->reg.auth_cfg_cmac_256;
  2970. key_reg = 8;
  2971. }
  2972. /* clear status register */
  2973. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_STATUS_REG, 0,
  2974. NULL);
  2975. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CONFIG_REG,
  2976. pdev->reg.crypto_cfg_be, &pcl_info->crypto_cfg);
  2977. /* 1 dummy write */
  2978. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_SEG_SIZE_REG,
  2979. 0, NULL);
  2980. break;
  2981. default:
  2982. pr_err("Unknown algorithms %d received, exiting now\n", alg);
  2983. return -EINVAL;
  2984. break;
  2985. }
  2986. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_SEG_SIZE_REG, 0,
  2987. &pcl_info->seg_size);
  2988. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_SEG_CFG_REG, 0,
  2989. &pcl_info->encr_seg_cfg);
  2990. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_SEG_CFG_REG,
  2991. auth_cfg, &pcl_info->auth_seg_cfg);
  2992. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_SEG_SIZE_REG, 0,
  2993. &pcl_info->auth_seg_size);
  2994. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_SEG_START_REG, 0,
  2995. &pcl_info->auth_seg_start);
  2996. if (alg == QCE_HASH_AES_CMAC) {
  2997. /* reset auth iv, bytecount and key registers */
  2998. for (i = 0; i < 16; i++)
  2999. qce_add_cmd_element(pdev, &ce_vaddr,
  3000. (CRYPTO_AUTH_IV0_REG + i * sizeof(uint32_t)),
  3001. 0, NULL);
  3002. for (i = 0; i < 16; i++)
  3003. qce_add_cmd_element(pdev, &ce_vaddr,
  3004. (CRYPTO_AUTH_KEY0_REG + i*sizeof(uint32_t)),
  3005. 0, NULL);
  3006. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_BYTECNT0_REG,
  3007. 0, NULL);
  3008. } else {
  3009. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_IV0_REG, 0,
  3010. &pcl_info->auth_iv);
  3011. for (i = 1; i < iv_reg; i++)
  3012. qce_add_cmd_element(pdev, &ce_vaddr,
  3013. (CRYPTO_AUTH_IV0_REG + i*sizeof(uint32_t)),
  3014. 0, NULL);
  3015. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_BYTECNT0_REG,
  3016. 0, &pcl_info->auth_bytecount);
  3017. }
  3018. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_BYTECNT1_REG, 0, NULL);
  3019. if (key_reg) {
  3020. qce_add_cmd_element(pdev, &ce_vaddr,
  3021. CRYPTO_AUTH_KEY0_REG, 0, &pcl_info->auth_key);
  3022. for (i = 1; i < key_reg; i++)
  3023. qce_add_cmd_element(pdev, &ce_vaddr,
  3024. (CRYPTO_AUTH_KEY0_REG + i*sizeof(uint32_t)),
  3025. 0, NULL);
  3026. }
  3027. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CONFIG_REG,
  3028. pdev->reg.crypto_cfg_le, NULL);
  3029. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_GOPROC_REG,
  3030. ((1 << CRYPTO_GO) | (1 << CRYPTO_RESULTS_DUMP) |
  3031. (1 << CRYPTO_CLR_CNTXT)), &pcl_info->go_proc);
  3032. pcl_info->size = (uint32_t)ce_vaddr - (uint32_t)ce_vaddr_start;
  3033. *pvaddr = (unsigned char *) ce_vaddr;
  3034. return 0;
  3035. }
  3036. static int _setup_aead_cmdlistptrs(struct qce_device *pdev,
  3037. unsigned char **pvaddr,
  3038. uint32_t alg,
  3039. uint32_t mode,
  3040. uint32_t key_size)
  3041. {
  3042. struct sps_command_element *ce_vaddr;
  3043. uint32_t ce_vaddr_start;
  3044. struct qce_cmdlistptr_ops *cmdlistptr = &pdev->ce_sps.cmdlistptr;
  3045. struct qce_cmdlist_info *pcl_info = NULL;
  3046. uint32_t key_reg;
  3047. uint32_t iv_reg;
  3048. uint32_t i;
  3049. uint32_t enciv_in_word;
  3050. uint32_t encr_cfg;
  3051. *pvaddr = (unsigned char *) ALIGN(((unsigned int)(*pvaddr)),
  3052. pdev->ce_sps.ce_burst_size);
  3053. ce_vaddr_start = (uint32_t)(*pvaddr);
  3054. ce_vaddr = (struct sps_command_element *)(*pvaddr);
  3055. switch (alg) {
  3056. case CIPHER_ALG_DES:
  3057. switch (mode) {
  3058. case QCE_MODE_ECB:
  3059. cmdlistptr->aead_hmac_sha1_ecb_des.cmdlist =
  3060. (uint32_t)ce_vaddr;
  3061. pcl_info = &(cmdlistptr->aead_hmac_sha1_ecb_des);
  3062. encr_cfg = pdev->reg.encr_cfg_des_ecb;
  3063. break;
  3064. case QCE_MODE_CBC:
  3065. cmdlistptr->aead_hmac_sha1_cbc_des.cmdlist =
  3066. (uint32_t)ce_vaddr;
  3067. pcl_info = &(cmdlistptr->aead_hmac_sha1_cbc_des);
  3068. encr_cfg = pdev->reg.encr_cfg_des_cbc;
  3069. break;
  3070. default:
  3071. return -EINVAL;
  3072. };
  3073. enciv_in_word = 2;
  3074. break;
  3075. case CIPHER_ALG_3DES:
  3076. switch (mode) {
  3077. case QCE_MODE_ECB:
  3078. cmdlistptr->aead_hmac_sha1_ecb_3des.cmdlist =
  3079. (uint32_t)ce_vaddr;
  3080. pcl_info = &(cmdlistptr->aead_hmac_sha1_ecb_3des);
  3081. encr_cfg = pdev->reg.encr_cfg_3des_ecb;
  3082. break;
  3083. case QCE_MODE_CBC:
  3084. cmdlistptr->aead_hmac_sha1_cbc_3des.cmdlist =
  3085. (uint32_t)ce_vaddr;
  3086. pcl_info = &(cmdlistptr->aead_hmac_sha1_cbc_3des);
  3087. encr_cfg = pdev->reg.encr_cfg_3des_cbc;
  3088. break;
  3089. default:
  3090. return -EINVAL;
  3091. };
  3092. enciv_in_word = 2;
  3093. break;
  3094. case CIPHER_ALG_AES:
  3095. switch (mode) {
  3096. case QCE_MODE_ECB:
  3097. if (key_size == AES128_KEY_SIZE) {
  3098. cmdlistptr->aead_hmac_sha1_ecb_aes_128.cmdlist =
  3099. (uint32_t)ce_vaddr;
  3100. pcl_info = &(cmdlistptr->
  3101. aead_hmac_sha1_ecb_aes_128);
  3102. encr_cfg = pdev->reg.encr_cfg_aes_ecb_128;
  3103. } else if (key_size == AES256_KEY_SIZE) {
  3104. cmdlistptr->aead_hmac_sha1_ecb_aes_256.cmdlist =
  3105. (uint32_t)ce_vaddr;
  3106. pcl_info = &(cmdlistptr->
  3107. aead_hmac_sha1_ecb_aes_256);
  3108. encr_cfg = pdev->reg.encr_cfg_aes_ecb_256;
  3109. } else {
  3110. return -EINVAL;
  3111. }
  3112. break;
  3113. case QCE_MODE_CBC:
  3114. if (key_size == AES128_KEY_SIZE) {
  3115. cmdlistptr->aead_hmac_sha1_cbc_aes_128.cmdlist =
  3116. (uint32_t)ce_vaddr;
  3117. pcl_info = &(cmdlistptr->
  3118. aead_hmac_sha1_cbc_aes_128);
  3119. encr_cfg = pdev->reg.encr_cfg_aes_cbc_128;
  3120. } else if (key_size == AES256_KEY_SIZE) {
  3121. cmdlistptr->aead_hmac_sha1_cbc_aes_256.cmdlist =
  3122. (uint32_t)ce_vaddr;
  3123. pcl_info = &(cmdlistptr->
  3124. aead_hmac_sha1_cbc_aes_256);
  3125. encr_cfg = pdev->reg.encr_cfg_aes_cbc_256;
  3126. } else {
  3127. return -EINVAL;
  3128. }
  3129. break;
  3130. default:
  3131. return -EINVAL;
  3132. };
  3133. enciv_in_word = 4;
  3134. break;
  3135. default:
  3136. return -EINVAL;
  3137. };
  3138. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_STATUS_REG, 0, NULL);
  3139. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CONFIG_REG,
  3140. pdev->reg.crypto_cfg_be, &pcl_info->crypto_cfg);
  3141. key_reg = key_size/sizeof(uint32_t);
  3142. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_KEY0_REG, 0,
  3143. &pcl_info->encr_key);
  3144. for (i = 1; i < key_reg; i++)
  3145. qce_add_cmd_element(pdev, &ce_vaddr,
  3146. (CRYPTO_ENCR_KEY0_REG + i * sizeof(uint32_t)),
  3147. 0, NULL);
  3148. if (mode != QCE_MODE_ECB) {
  3149. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CNTR0_IV0_REG, 0,
  3150. &pcl_info->encr_cntr_iv);
  3151. for (i = 1; i < enciv_in_word; i++)
  3152. qce_add_cmd_element(pdev, &ce_vaddr,
  3153. (CRYPTO_CNTR0_IV0_REG + i * sizeof(uint32_t)),
  3154. 0, NULL);
  3155. };
  3156. iv_reg = 5;
  3157. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_IV0_REG, 0,
  3158. &pcl_info->auth_iv);
  3159. for (i = 1; i < iv_reg; i++)
  3160. qce_add_cmd_element(pdev, &ce_vaddr,
  3161. (CRYPTO_AUTH_IV0_REG + i*sizeof(uint32_t)),
  3162. 0, NULL);
  3163. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_BYTECNT0_REG,
  3164. 0, &pcl_info->auth_bytecount);
  3165. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_BYTECNT1_REG, 0, NULL);
  3166. key_reg = SHA_HMAC_KEY_SIZE/sizeof(uint32_t);
  3167. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_KEY0_REG, 0,
  3168. &pcl_info->auth_key);
  3169. for (i = 1; i < key_reg; i++)
  3170. qce_add_cmd_element(pdev, &ce_vaddr,
  3171. (CRYPTO_AUTH_KEY0_REG + i*sizeof(uint32_t)), 0, NULL);
  3172. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_SEG_SIZE_REG, 0,
  3173. &pcl_info->seg_size);
  3174. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_SEG_CFG_REG, encr_cfg,
  3175. &pcl_info->encr_seg_cfg);
  3176. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_SEG_SIZE_REG, 0,
  3177. &pcl_info->encr_seg_size);
  3178. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_SEG_START_REG, 0,
  3179. &pcl_info->encr_seg_start);
  3180. qce_add_cmd_element(
  3181. pdev,
  3182. &ce_vaddr,
  3183. CRYPTO_AUTH_SEG_CFG_REG,
  3184. pdev->reg.auth_cfg_aead_sha1_hmac,
  3185. &pcl_info->auth_seg_cfg);
  3186. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_SEG_SIZE_REG, 0,
  3187. &pcl_info->auth_seg_size);
  3188. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_SEG_START_REG, 0,
  3189. &pcl_info->auth_seg_start);
  3190. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CONFIG_REG,
  3191. pdev->reg.crypto_cfg_le, NULL);
  3192. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_GOPROC_REG,
  3193. ((1 << CRYPTO_GO) | (1 << CRYPTO_RESULTS_DUMP) |
  3194. (1 << CRYPTO_CLR_CNTXT)), &pcl_info->go_proc);
  3195. pcl_info->size = (uint32_t)ce_vaddr - (uint32_t)ce_vaddr_start;
  3196. *pvaddr = (unsigned char *) ce_vaddr;
  3197. return 0;
  3198. }
  3199. static int _setup_aead_ccm_cmdlistptrs(struct qce_device *pdev,
  3200. unsigned char **pvaddr, bool key_128)
  3201. {
  3202. struct sps_command_element *ce_vaddr;
  3203. uint32_t ce_vaddr_start;
  3204. struct qce_cmdlistptr_ops *cmdlistptr = &pdev->ce_sps.cmdlistptr;
  3205. struct qce_cmdlist_info *pcl_info = NULL;
  3206. int i = 0;
  3207. uint32_t encr_cfg = 0;
  3208. uint32_t auth_cfg = 0;
  3209. uint32_t key_reg = 0;
  3210. *pvaddr = (unsigned char *) ALIGN(((unsigned int)(*pvaddr)),
  3211. pdev->ce_sps.ce_burst_size);
  3212. ce_vaddr_start = (uint32_t)(*pvaddr);
  3213. ce_vaddr = (struct sps_command_element *)(*pvaddr);
  3214. /*
  3215. * Designate chunks of the allocated memory to various
  3216. * command list pointers related to aead operations
  3217. * defined in ce_cmdlistptrs_ops structure.
  3218. */
  3219. if (key_128 == true) {
  3220. cmdlistptr->aead_aes_128_ccm.cmdlist = (uint32_t)ce_vaddr;
  3221. pcl_info = &(cmdlistptr->aead_aes_128_ccm);
  3222. auth_cfg = pdev->reg.auth_cfg_aes_ccm_128;
  3223. encr_cfg = pdev->reg.encr_cfg_aes_ccm_128;
  3224. key_reg = 4;
  3225. } else {
  3226. cmdlistptr->aead_aes_256_ccm.cmdlist = (uint32_t)ce_vaddr;
  3227. pcl_info = &(cmdlistptr->aead_aes_256_ccm);
  3228. auth_cfg = pdev->reg.auth_cfg_aes_ccm_256;
  3229. encr_cfg = pdev->reg.encr_cfg_aes_ccm_256;
  3230. key_reg = 8;
  3231. }
  3232. /* clear status register */
  3233. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_STATUS_REG, 0, NULL);
  3234. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CONFIG_REG,
  3235. pdev->reg.crypto_cfg_be, &pcl_info->crypto_cfg);
  3236. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_SEG_CFG_REG, 0, NULL);
  3237. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_SEG_START_REG, 0,
  3238. NULL);
  3239. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_SEG_SIZE_REG, 0,
  3240. &pcl_info->seg_size);
  3241. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_SEG_CFG_REG,
  3242. encr_cfg, &pcl_info->encr_seg_cfg);
  3243. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_SEG_SIZE_REG, 0,
  3244. &pcl_info->encr_seg_size);
  3245. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_SEG_START_REG, 0,
  3246. &pcl_info->encr_seg_start);
  3247. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CNTR_MASK_REG,
  3248. (uint32_t)0xffffffff, &pcl_info->encr_mask);
  3249. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_SEG_CFG_REG,
  3250. auth_cfg, &pcl_info->auth_seg_cfg);
  3251. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_SEG_SIZE_REG, 0,
  3252. &pcl_info->auth_seg_size);
  3253. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_SEG_START_REG, 0,
  3254. &pcl_info->auth_seg_start);
  3255. /* reset auth iv, bytecount and key registers */
  3256. for (i = 0; i < 8; i++)
  3257. qce_add_cmd_element(pdev, &ce_vaddr,
  3258. (CRYPTO_AUTH_IV0_REG + i * sizeof(uint32_t)),
  3259. 0, NULL);
  3260. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_BYTECNT0_REG,
  3261. 0, NULL);
  3262. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_BYTECNT1_REG,
  3263. 0, NULL);
  3264. for (i = 0; i < 16; i++)
  3265. qce_add_cmd_element(pdev, &ce_vaddr,
  3266. (CRYPTO_AUTH_KEY0_REG + i * sizeof(uint32_t)),
  3267. 0, NULL);
  3268. /* set auth key */
  3269. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_KEY0_REG, 0,
  3270. &pcl_info->auth_key);
  3271. for (i = 1; i < key_reg; i++)
  3272. qce_add_cmd_element(pdev, &ce_vaddr,
  3273. (CRYPTO_AUTH_KEY0_REG + i * sizeof(uint32_t)),
  3274. 0, NULL);
  3275. /* set NONCE info */
  3276. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_INFO_NONCE0_REG, 0,
  3277. &pcl_info->auth_nonce_info);
  3278. for (i = 1; i < 4; i++)
  3279. qce_add_cmd_element(pdev, &ce_vaddr,
  3280. (CRYPTO_AUTH_INFO_NONCE0_REG +
  3281. i * sizeof(uint32_t)), 0, NULL);
  3282. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_KEY0_REG, 0,
  3283. &pcl_info->encr_key);
  3284. for (i = 1; i < key_reg; i++)
  3285. qce_add_cmd_element(pdev, &ce_vaddr,
  3286. (CRYPTO_ENCR_KEY0_REG + i * sizeof(uint32_t)),
  3287. 0, NULL);
  3288. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CNTR0_IV0_REG, 0,
  3289. &pcl_info->encr_cntr_iv);
  3290. for (i = 1; i < 4; i++)
  3291. qce_add_cmd_element(pdev, &ce_vaddr,
  3292. (CRYPTO_CNTR0_IV0_REG + i * sizeof(uint32_t)),
  3293. 0, NULL);
  3294. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_CCM_INT_CNTR0_REG, 0,
  3295. &pcl_info->encr_ccm_cntr_iv);
  3296. for (i = 1; i < 4; i++)
  3297. qce_add_cmd_element(pdev, &ce_vaddr,
  3298. (CRYPTO_ENCR_CCM_INT_CNTR0_REG + i * sizeof(uint32_t)),
  3299. 0, NULL);
  3300. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CONFIG_REG,
  3301. pdev->reg.crypto_cfg_le, NULL);
  3302. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_GOPROC_REG,
  3303. ((1 << CRYPTO_GO) | (1 << CRYPTO_RESULTS_DUMP) |
  3304. (1 << CRYPTO_CLR_CNTXT)), &pcl_info->go_proc);
  3305. pcl_info->size = (uint32_t)ce_vaddr - (uint32_t)ce_vaddr_start;
  3306. *pvaddr = (unsigned char *) ce_vaddr;
  3307. return 0;
  3308. }
  3309. static int _setup_f8_cmdlistptrs(struct qce_device *pdev,
  3310. unsigned char **pvaddr, enum qce_ota_algo_enum alg)
  3311. {
  3312. struct sps_command_element *ce_vaddr;
  3313. uint32_t ce_vaddr_start;
  3314. struct qce_cmdlistptr_ops *cmdlistptr = &pdev->ce_sps.cmdlistptr;
  3315. struct qce_cmdlist_info *pcl_info = NULL;
  3316. int i = 0;
  3317. uint32_t encr_cfg = 0;
  3318. uint32_t key_reg = 4;
  3319. *pvaddr = (unsigned char *) ALIGN(((unsigned int)(*pvaddr)),
  3320. pdev->ce_sps.ce_burst_size);
  3321. ce_vaddr = (struct sps_command_element *)(*pvaddr);
  3322. ce_vaddr_start = (uint32_t)(*pvaddr);
  3323. /*
  3324. * Designate chunks of the allocated memory to various
  3325. * command list pointers related to f8 cipher algorithm defined
  3326. * in ce_cmdlistptrs_ops structure.
  3327. */
  3328. switch (alg) {
  3329. case QCE_OTA_ALGO_KASUMI:
  3330. cmdlistptr->f8_kasumi.cmdlist = (uint32_t)ce_vaddr;
  3331. pcl_info = &(cmdlistptr->f8_kasumi);
  3332. encr_cfg = pdev->reg.encr_cfg_kasumi;
  3333. break;
  3334. case QCE_OTA_ALGO_SNOW3G:
  3335. default:
  3336. cmdlistptr->f8_snow3g.cmdlist = (uint32_t)ce_vaddr;
  3337. pcl_info = &(cmdlistptr->f8_snow3g);
  3338. encr_cfg = pdev->reg.encr_cfg_snow3g;
  3339. break;
  3340. }
  3341. /* clear status register */
  3342. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_STATUS_REG,
  3343. 0, NULL);
  3344. /* set config to big endian */
  3345. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CONFIG_REG,
  3346. pdev->reg.crypto_cfg_be, &pcl_info->crypto_cfg);
  3347. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_SEG_SIZE_REG, 0,
  3348. &pcl_info->seg_size);
  3349. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_SEG_CFG_REG, encr_cfg,
  3350. &pcl_info->encr_seg_cfg);
  3351. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_SEG_SIZE_REG, 0,
  3352. &pcl_info->encr_seg_size);
  3353. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_SEG_START_REG, 0,
  3354. &pcl_info->encr_seg_start);
  3355. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_SEG_CFG_REG, 0,
  3356. &pcl_info->auth_seg_cfg);
  3357. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_SEG_SIZE_REG,
  3358. 0, &pcl_info->auth_seg_size);
  3359. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_SEG_START_REG,
  3360. 0, &pcl_info->auth_seg_start);
  3361. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_KEY0_REG, 0,
  3362. &pcl_info->encr_key);
  3363. for (i = 1; i < key_reg; i++)
  3364. qce_add_cmd_element(pdev, &ce_vaddr,
  3365. (CRYPTO_ENCR_KEY0_REG + i * sizeof(uint32_t)),
  3366. 0, NULL);
  3367. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CNTR0_IV0_REG, 0,
  3368. &pcl_info->encr_cntr_iv);
  3369. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CNTR1_IV1_REG, 0,
  3370. NULL);
  3371. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CONFIG_REG,
  3372. pdev->reg.crypto_cfg_le, NULL);
  3373. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_GOPROC_REG,
  3374. ((1 << CRYPTO_GO) | (1 << CRYPTO_RESULTS_DUMP) |
  3375. (1 << CRYPTO_CLR_CNTXT)), &pcl_info->go_proc);
  3376. pcl_info->size = (uint32_t)ce_vaddr - (uint32_t)ce_vaddr_start;
  3377. *pvaddr = (unsigned char *) ce_vaddr;
  3378. return 0;
  3379. }
  3380. static int _setup_f9_cmdlistptrs(struct qce_device *pdev,
  3381. unsigned char **pvaddr, enum qce_ota_algo_enum alg)
  3382. {
  3383. struct sps_command_element *ce_vaddr;
  3384. uint32_t ce_vaddr_start;
  3385. struct qce_cmdlistptr_ops *cmdlistptr = &pdev->ce_sps.cmdlistptr;
  3386. struct qce_cmdlist_info *pcl_info = NULL;
  3387. int i = 0;
  3388. uint32_t auth_cfg = 0;
  3389. uint32_t iv_reg = 0;
  3390. *pvaddr = (unsigned char *) ALIGN(((unsigned int)(*pvaddr)),
  3391. pdev->ce_sps.ce_burst_size);
  3392. ce_vaddr_start = (uint32_t)(*pvaddr);
  3393. ce_vaddr = (struct sps_command_element *)(*pvaddr);
  3394. /*
  3395. * Designate chunks of the allocated memory to various
  3396. * command list pointers related to authentication operations
  3397. * defined in ce_cmdlistptrs_ops structure.
  3398. */
  3399. switch (alg) {
  3400. case QCE_OTA_ALGO_KASUMI:
  3401. cmdlistptr->f9_kasumi.cmdlist = (uint32_t)ce_vaddr;
  3402. pcl_info = &(cmdlistptr->f9_kasumi);
  3403. auth_cfg = pdev->reg.auth_cfg_kasumi;
  3404. break;
  3405. case QCE_OTA_ALGO_SNOW3G:
  3406. default:
  3407. cmdlistptr->f9_snow3g.cmdlist = (uint32_t)ce_vaddr;
  3408. pcl_info = &(cmdlistptr->f9_snow3g);
  3409. auth_cfg = pdev->reg.auth_cfg_snow3g;
  3410. };
  3411. /* clear status register */
  3412. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_STATUS_REG,
  3413. 0, NULL);
  3414. /* set config to big endian */
  3415. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CONFIG_REG,
  3416. pdev->reg.crypto_cfg_be, &pcl_info->crypto_cfg);
  3417. iv_reg = 5;
  3418. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_SEG_SIZE_REG, 0,
  3419. &pcl_info->seg_size);
  3420. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_SEG_CFG_REG, 0,
  3421. &pcl_info->encr_seg_cfg);
  3422. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_SEG_CFG_REG,
  3423. auth_cfg, &pcl_info->auth_seg_cfg);
  3424. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_SEG_SIZE_REG, 0,
  3425. &pcl_info->auth_seg_size);
  3426. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_SEG_START_REG, 0,
  3427. &pcl_info->auth_seg_start);
  3428. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_IV0_REG, 0,
  3429. &pcl_info->auth_iv);
  3430. for (i = 1; i < iv_reg; i++) {
  3431. qce_add_cmd_element(pdev, &ce_vaddr,
  3432. (CRYPTO_AUTH_IV0_REG + i*sizeof(uint32_t)),
  3433. 0, NULL);
  3434. }
  3435. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_BYTECNT0_REG,
  3436. 0, &pcl_info->auth_bytecount);
  3437. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_BYTECNT1_REG, 0, NULL);
  3438. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CONFIG_REG,
  3439. pdev->reg.crypto_cfg_le, NULL);
  3440. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_GOPROC_REG,
  3441. ((1 << CRYPTO_GO) | (1 << CRYPTO_RESULTS_DUMP) |
  3442. (1 << CRYPTO_CLR_CNTXT)), &pcl_info->go_proc);
  3443. pcl_info->size = (uint32_t)ce_vaddr - (uint32_t)ce_vaddr_start;
  3444. *pvaddr = (unsigned char *) ce_vaddr;
  3445. return 0;
  3446. }
  3447. static int _setup_unlock_pipe_cmdlistptrs(struct qce_device *pdev,
  3448. unsigned char **pvaddr)
  3449. {
  3450. struct sps_command_element *ce_vaddr;
  3451. uint32_t ce_vaddr_start = (uint32_t)(*pvaddr);
  3452. struct qce_cmdlistptr_ops *cmdlistptr = &pdev->ce_sps.cmdlistptr;
  3453. struct qce_cmdlist_info *pcl_info = NULL;
  3454. *pvaddr = (unsigned char *) ALIGN(((unsigned int)(*pvaddr)),
  3455. pdev->ce_sps.ce_burst_size);
  3456. ce_vaddr = (struct sps_command_element *)(*pvaddr);
  3457. cmdlistptr->unlock_all_pipes.cmdlist = (uint32_t)ce_vaddr;
  3458. pcl_info = &(cmdlistptr->unlock_all_pipes);
  3459. /*
  3460. * Designate chunks of the allocated memory to command list
  3461. * to unlock pipes.
  3462. */
  3463. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CONFIG_REG,
  3464. CRYPTO_CONFIG_RESET, NULL);
  3465. pcl_info->size = (uint32_t)ce_vaddr - (uint32_t)ce_vaddr_start;
  3466. *pvaddr = (unsigned char *) ce_vaddr;
  3467. return 0;
  3468. }
  3469. static int qce_setup_cmdlistptrs(struct qce_device *pdev,
  3470. unsigned char **pvaddr)
  3471. {
  3472. struct sps_command_element *ce_vaddr =
  3473. (struct sps_command_element *)(*pvaddr);
  3474. /*
  3475. * Designate chunks of the allocated memory to various
  3476. * command list pointers related to operations defined
  3477. * in ce_cmdlistptrs_ops structure.
  3478. */
  3479. ce_vaddr =
  3480. (struct sps_command_element *) ALIGN(((unsigned int) ce_vaddr),
  3481. pdev->ce_sps.ce_burst_size);
  3482. *pvaddr = (unsigned char *) ce_vaddr;
  3483. _setup_cipher_aes_cmdlistptrs(pdev, pvaddr, QCE_MODE_CBC, true);
  3484. _setup_cipher_aes_cmdlistptrs(pdev, pvaddr, QCE_MODE_CTR, true);
  3485. _setup_cipher_aes_cmdlistptrs(pdev, pvaddr, QCE_MODE_ECB, true);
  3486. _setup_cipher_aes_cmdlistptrs(pdev, pvaddr, QCE_MODE_XTS, true);
  3487. _setup_cipher_aes_cmdlistptrs(pdev, pvaddr, QCE_MODE_CBC, false);
  3488. _setup_cipher_aes_cmdlistptrs(pdev, pvaddr, QCE_MODE_CTR, false);
  3489. _setup_cipher_aes_cmdlistptrs(pdev, pvaddr, QCE_MODE_ECB, false);
  3490. _setup_cipher_aes_cmdlistptrs(pdev, pvaddr, QCE_MODE_XTS, false);
  3491. _setup_cipher_des_cmdlistptrs(pdev, pvaddr, CIPHER_ALG_DES, true);
  3492. _setup_cipher_des_cmdlistptrs(pdev, pvaddr, CIPHER_ALG_DES, false);
  3493. _setup_cipher_des_cmdlistptrs(pdev, pvaddr, CIPHER_ALG_3DES, true);
  3494. _setup_cipher_des_cmdlistptrs(pdev, pvaddr, CIPHER_ALG_3DES, false);
  3495. _setup_auth_cmdlistptrs(pdev, pvaddr, QCE_HASH_SHA1, false);
  3496. _setup_auth_cmdlistptrs(pdev, pvaddr, QCE_HASH_SHA256, false);
  3497. _setup_auth_cmdlistptrs(pdev, pvaddr, QCE_HASH_SHA1_HMAC, false);
  3498. _setup_auth_cmdlistptrs(pdev, pvaddr, QCE_HASH_SHA256_HMAC, false);
  3499. _setup_auth_cmdlistptrs(pdev, pvaddr, QCE_HASH_AES_CMAC, true);
  3500. _setup_auth_cmdlistptrs(pdev, pvaddr, QCE_HASH_AES_CMAC, false);
  3501. _setup_aead_cmdlistptrs(pdev, pvaddr, CIPHER_ALG_DES, QCE_MODE_CBC,
  3502. DES_KEY_SIZE);
  3503. _setup_aead_cmdlistptrs(pdev, pvaddr, CIPHER_ALG_DES, QCE_MODE_ECB,
  3504. DES_KEY_SIZE);
  3505. _setup_aead_cmdlistptrs(pdev, pvaddr, CIPHER_ALG_3DES, QCE_MODE_CBC,
  3506. DES3_EDE_KEY_SIZE);
  3507. _setup_aead_cmdlistptrs(pdev, pvaddr, CIPHER_ALG_3DES, QCE_MODE_ECB,
  3508. DES3_EDE_KEY_SIZE);
  3509. _setup_aead_cmdlistptrs(pdev, pvaddr, CIPHER_ALG_AES, QCE_MODE_CBC,
  3510. AES128_KEY_SIZE);
  3511. _setup_aead_cmdlistptrs(pdev, pvaddr, CIPHER_ALG_AES, QCE_MODE_ECB,
  3512. AES128_KEY_SIZE);
  3513. _setup_aead_cmdlistptrs(pdev, pvaddr, CIPHER_ALG_AES, QCE_MODE_CBC,
  3514. AES256_KEY_SIZE);
  3515. _setup_aead_cmdlistptrs(pdev, pvaddr, CIPHER_ALG_AES, QCE_MODE_ECB,
  3516. AES256_KEY_SIZE);
  3517. _setup_aead_ccm_cmdlistptrs(pdev, pvaddr, true);
  3518. _setup_aead_ccm_cmdlistptrs(pdev, pvaddr, false);
  3519. _setup_f8_cmdlistptrs(pdev, pvaddr, QCE_OTA_ALGO_KASUMI);
  3520. _setup_f8_cmdlistptrs(pdev, pvaddr, QCE_OTA_ALGO_SNOW3G);
  3521. _setup_f9_cmdlistptrs(pdev, pvaddr, QCE_OTA_ALGO_KASUMI);
  3522. _setup_f9_cmdlistptrs(pdev, pvaddr, QCE_OTA_ALGO_SNOW3G);
  3523. _setup_unlock_pipe_cmdlistptrs(pdev, pvaddr);
  3524. return 0;
  3525. }
  3526. static int qce_setup_ce_sps_data(struct qce_device *pce_dev)
  3527. {
  3528. unsigned char *vaddr;
  3529. vaddr = pce_dev->coh_vmem;
  3530. vaddr = (unsigned char *) ALIGN(((unsigned int)vaddr),
  3531. pce_dev->ce_sps.ce_burst_size);
  3532. /* Allow for 256 descriptor (cmd and data) entries per pipe */
  3533. pce_dev->ce_sps.in_transfer.iovec = (struct sps_iovec *)vaddr;
  3534. pce_dev->ce_sps.in_transfer.iovec_phys =
  3535. (uint32_t)GET_PHYS_ADDR(vaddr);
  3536. vaddr += QCE_MAX_NUM_DSCR * sizeof(struct sps_iovec);
  3537. pce_dev->ce_sps.out_transfer.iovec = (struct sps_iovec *)vaddr;
  3538. pce_dev->ce_sps.out_transfer.iovec_phys =
  3539. (uint32_t)GET_PHYS_ADDR(vaddr);
  3540. vaddr += QCE_MAX_NUM_DSCR * sizeof(struct sps_iovec);
  3541. if (pce_dev->support_cmd_dscr)
  3542. qce_setup_cmdlistptrs(pce_dev, &vaddr);
  3543. vaddr = (unsigned char *) ALIGN(((unsigned int)vaddr),
  3544. pce_dev->ce_sps.ce_burst_size);
  3545. pce_dev->ce_sps.result_dump = (uint32_t)vaddr;
  3546. pce_dev->ce_sps.result = (struct ce_result_dump_format *)vaddr;
  3547. vaddr += CRYPTO_RESULT_DUMP_SIZE;
  3548. pce_dev->ce_sps.ignore_buffer = (uint32_t)vaddr;
  3549. vaddr += pce_dev->ce_sps.ce_burst_size * 2;
  3550. if ((vaddr - pce_dev->coh_vmem) > pce_dev->memsize)
  3551. panic("qce50: Not enough coherent memory. Allocate %x , need %x",
  3552. pce_dev->memsize, vaddr - pce_dev->coh_vmem);
  3553. return 0;
  3554. }
  3555. static int qce_init_ce_cfg_val(struct qce_device *pce_dev)
  3556. {
  3557. uint32_t beats = (pce_dev->ce_sps.ce_burst_size >> 3) - 1;
  3558. uint32_t pipe_pair = pce_dev->ce_sps.pipe_pair_index;
  3559. pce_dev->reg.crypto_cfg_be = (beats << CRYPTO_REQ_SIZE) |
  3560. BIT(CRYPTO_MASK_DOUT_INTR) | BIT(CRYPTO_MASK_DIN_INTR) |
  3561. BIT(CRYPTO_MASK_OP_DONE_INTR) | (0 << CRYPTO_HIGH_SPD_EN_N) |
  3562. (pipe_pair << CRYPTO_PIPE_SET_SELECT);
  3563. pce_dev->reg.crypto_cfg_le =
  3564. (pce_dev->reg.crypto_cfg_be | CRYPTO_LITTLE_ENDIAN_MASK);
  3565. /* Initialize encr_cfg register for AES alg */
  3566. pce_dev->reg.encr_cfg_aes_cbc_128 =
  3567. (CRYPTO_ENCR_KEY_SZ_AES128 << CRYPTO_ENCR_KEY_SZ) |
  3568. (CRYPTO_ENCR_ALG_AES << CRYPTO_ENCR_ALG) |
  3569. (CRYPTO_ENCR_MODE_CBC << CRYPTO_ENCR_MODE);
  3570. pce_dev->reg.encr_cfg_aes_cbc_256 =
  3571. (CRYPTO_ENCR_KEY_SZ_AES256 << CRYPTO_ENCR_KEY_SZ) |
  3572. (CRYPTO_ENCR_ALG_AES << CRYPTO_ENCR_ALG) |
  3573. (CRYPTO_ENCR_MODE_CBC << CRYPTO_ENCR_MODE);
  3574. pce_dev->reg.encr_cfg_aes_ctr_128 =
  3575. (CRYPTO_ENCR_KEY_SZ_AES128 << CRYPTO_ENCR_KEY_SZ) |
  3576. (CRYPTO_ENCR_ALG_AES << CRYPTO_ENCR_ALG) |
  3577. (CRYPTO_ENCR_MODE_CTR << CRYPTO_ENCR_MODE);
  3578. pce_dev->reg.encr_cfg_aes_ctr_256 =
  3579. (CRYPTO_ENCR_KEY_SZ_AES256 << CRYPTO_ENCR_KEY_SZ) |
  3580. (CRYPTO_ENCR_ALG_AES << CRYPTO_ENCR_ALG) |
  3581. (CRYPTO_ENCR_MODE_CTR << CRYPTO_ENCR_MODE);
  3582. pce_dev->reg.encr_cfg_aes_xts_128 =
  3583. (CRYPTO_ENCR_KEY_SZ_AES128 << CRYPTO_ENCR_KEY_SZ) |
  3584. (CRYPTO_ENCR_ALG_AES << CRYPTO_ENCR_ALG) |
  3585. (CRYPTO_ENCR_MODE_XTS << CRYPTO_ENCR_MODE);
  3586. pce_dev->reg.encr_cfg_aes_xts_256 =
  3587. (CRYPTO_ENCR_KEY_SZ_AES256 << CRYPTO_ENCR_KEY_SZ) |
  3588. (CRYPTO_ENCR_ALG_AES << CRYPTO_ENCR_ALG) |
  3589. (CRYPTO_ENCR_MODE_XTS << CRYPTO_ENCR_MODE);
  3590. pce_dev->reg.encr_cfg_aes_ecb_128 =
  3591. (CRYPTO_ENCR_KEY_SZ_AES128 << CRYPTO_ENCR_KEY_SZ) |
  3592. (CRYPTO_ENCR_ALG_AES << CRYPTO_ENCR_ALG) |
  3593. (CRYPTO_ENCR_MODE_ECB << CRYPTO_ENCR_MODE);
  3594. pce_dev->reg.encr_cfg_aes_ecb_256 =
  3595. (CRYPTO_ENCR_KEY_SZ_AES256 << CRYPTO_ENCR_KEY_SZ) |
  3596. (CRYPTO_ENCR_ALG_AES << CRYPTO_ENCR_ALG) |
  3597. (CRYPTO_ENCR_MODE_ECB << CRYPTO_ENCR_MODE);
  3598. pce_dev->reg.encr_cfg_aes_ccm_128 =
  3599. (CRYPTO_ENCR_KEY_SZ_AES128 << CRYPTO_ENCR_KEY_SZ) |
  3600. (CRYPTO_ENCR_ALG_AES << CRYPTO_ENCR_ALG) |
  3601. (CRYPTO_ENCR_MODE_CCM << CRYPTO_ENCR_MODE)|
  3602. (CRYPTO_LAST_CCM_XFR << CRYPTO_LAST_CCM);
  3603. pce_dev->reg.encr_cfg_aes_ccm_256 =
  3604. (CRYPTO_ENCR_KEY_SZ_AES256 << CRYPTO_ENCR_KEY_SZ) |
  3605. (CRYPTO_ENCR_ALG_AES << CRYPTO_ENCR_ALG) |
  3606. (CRYPTO_ENCR_MODE_CCM << CRYPTO_ENCR_MODE) |
  3607. (CRYPTO_LAST_CCM_XFR << CRYPTO_LAST_CCM);
  3608. /* Initialize encr_cfg register for DES alg */
  3609. pce_dev->reg.encr_cfg_des_ecb =
  3610. (CRYPTO_ENCR_KEY_SZ_DES << CRYPTO_ENCR_KEY_SZ) |
  3611. (CRYPTO_ENCR_ALG_DES << CRYPTO_ENCR_ALG) |
  3612. (CRYPTO_ENCR_MODE_ECB << CRYPTO_ENCR_MODE);
  3613. pce_dev->reg.encr_cfg_des_cbc =
  3614. (CRYPTO_ENCR_KEY_SZ_DES << CRYPTO_ENCR_KEY_SZ) |
  3615. (CRYPTO_ENCR_ALG_DES << CRYPTO_ENCR_ALG) |
  3616. (CRYPTO_ENCR_MODE_CBC << CRYPTO_ENCR_MODE);
  3617. pce_dev->reg.encr_cfg_3des_ecb =
  3618. (CRYPTO_ENCR_KEY_SZ_3DES << CRYPTO_ENCR_KEY_SZ) |
  3619. (CRYPTO_ENCR_ALG_DES << CRYPTO_ENCR_ALG) |
  3620. (CRYPTO_ENCR_MODE_ECB << CRYPTO_ENCR_MODE);
  3621. pce_dev->reg.encr_cfg_3des_cbc =
  3622. (CRYPTO_ENCR_KEY_SZ_3DES << CRYPTO_ENCR_KEY_SZ) |
  3623. (CRYPTO_ENCR_ALG_DES << CRYPTO_ENCR_ALG) |
  3624. (CRYPTO_ENCR_MODE_CBC << CRYPTO_ENCR_MODE);
  3625. /* Initialize encr_cfg register for kasumi/snow3g alg */
  3626. pce_dev->reg.encr_cfg_kasumi =
  3627. (CRYPTO_ENCR_ALG_KASUMI << CRYPTO_ENCR_ALG);
  3628. pce_dev->reg.encr_cfg_snow3g =
  3629. (CRYPTO_ENCR_ALG_SNOW_3G << CRYPTO_ENCR_ALG);
  3630. /* Initialize auth_cfg register for CMAC alg */
  3631. pce_dev->reg.auth_cfg_cmac_128 =
  3632. (1 << CRYPTO_LAST) | (1 << CRYPTO_FIRST) |
  3633. (CRYPTO_AUTH_MODE_CMAC << CRYPTO_AUTH_MODE)|
  3634. (CRYPTO_AUTH_SIZE_ENUM_16_BYTES << CRYPTO_AUTH_SIZE) |
  3635. (CRYPTO_AUTH_ALG_AES << CRYPTO_AUTH_ALG) |
  3636. (CRYPTO_AUTH_KEY_SZ_AES128 << CRYPTO_AUTH_KEY_SIZE);
  3637. pce_dev->reg.auth_cfg_cmac_256 =
  3638. (1 << CRYPTO_LAST) | (1 << CRYPTO_FIRST) |
  3639. (CRYPTO_AUTH_MODE_CMAC << CRYPTO_AUTH_MODE)|
  3640. (CRYPTO_AUTH_SIZE_ENUM_16_BYTES << CRYPTO_AUTH_SIZE) |
  3641. (CRYPTO_AUTH_ALG_AES << CRYPTO_AUTH_ALG) |
  3642. (CRYPTO_AUTH_KEY_SZ_AES256 << CRYPTO_AUTH_KEY_SIZE);
  3643. /* Initialize auth_cfg register for HMAC alg */
  3644. pce_dev->reg.auth_cfg_hmac_sha1 =
  3645. (CRYPTO_AUTH_MODE_HMAC << CRYPTO_AUTH_MODE)|
  3646. (CRYPTO_AUTH_SIZE_SHA1 << CRYPTO_AUTH_SIZE) |
  3647. (CRYPTO_AUTH_ALG_SHA << CRYPTO_AUTH_ALG) |
  3648. (CRYPTO_AUTH_POS_BEFORE << CRYPTO_AUTH_POS);
  3649. pce_dev->reg.auth_cfg_hmac_sha256 =
  3650. (CRYPTO_AUTH_MODE_HMAC << CRYPTO_AUTH_MODE)|
  3651. (CRYPTO_AUTH_SIZE_SHA256 << CRYPTO_AUTH_SIZE) |
  3652. (CRYPTO_AUTH_ALG_SHA << CRYPTO_AUTH_ALG) |
  3653. (CRYPTO_AUTH_POS_BEFORE << CRYPTO_AUTH_POS);
  3654. /* Initialize auth_cfg register for SHA1/256 alg */
  3655. pce_dev->reg.auth_cfg_sha1 =
  3656. (CRYPTO_AUTH_MODE_HASH << CRYPTO_AUTH_MODE)|
  3657. (CRYPTO_AUTH_SIZE_SHA1 << CRYPTO_AUTH_SIZE) |
  3658. (CRYPTO_AUTH_ALG_SHA << CRYPTO_AUTH_ALG) |
  3659. (CRYPTO_AUTH_POS_BEFORE << CRYPTO_AUTH_POS);
  3660. pce_dev->reg.auth_cfg_sha256 =
  3661. (CRYPTO_AUTH_MODE_HASH << CRYPTO_AUTH_MODE)|
  3662. (CRYPTO_AUTH_SIZE_SHA256 << CRYPTO_AUTH_SIZE) |
  3663. (CRYPTO_AUTH_ALG_SHA << CRYPTO_AUTH_ALG) |
  3664. (CRYPTO_AUTH_POS_BEFORE << CRYPTO_AUTH_POS);
  3665. /* Initialize auth_cfg register for AEAD alg */
  3666. pce_dev->reg.auth_cfg_aead_sha1_hmac =
  3667. (CRYPTO_AUTH_MODE_HMAC << CRYPTO_AUTH_MODE)|
  3668. (CRYPTO_AUTH_SIZE_SHA1 << CRYPTO_AUTH_SIZE) |
  3669. (CRYPTO_AUTH_ALG_SHA << CRYPTO_AUTH_ALG) |
  3670. (1 << CRYPTO_LAST) | (1 << CRYPTO_FIRST);
  3671. pce_dev->reg.auth_cfg_aead_sha256_hmac =
  3672. (CRYPTO_AUTH_MODE_HMAC << CRYPTO_AUTH_MODE)|
  3673. (CRYPTO_AUTH_SIZE_SHA256 << CRYPTO_AUTH_SIZE) |
  3674. (CRYPTO_AUTH_ALG_SHA << CRYPTO_AUTH_ALG) |
  3675. (1 << CRYPTO_LAST) | (1 << CRYPTO_FIRST);
  3676. pce_dev->reg.auth_cfg_aes_ccm_128 =
  3677. (1 << CRYPTO_LAST) | (1 << CRYPTO_FIRST) |
  3678. (CRYPTO_AUTH_MODE_CCM << CRYPTO_AUTH_MODE)|
  3679. (CRYPTO_AUTH_ALG_AES << CRYPTO_AUTH_ALG) |
  3680. (CRYPTO_AUTH_KEY_SZ_AES128 << CRYPTO_AUTH_KEY_SIZE) |
  3681. ((MAX_NONCE/sizeof(uint32_t)) << CRYPTO_AUTH_NONCE_NUM_WORDS);
  3682. pce_dev->reg.auth_cfg_aes_ccm_128 &= ~(1 << CRYPTO_USE_HW_KEY_AUTH);
  3683. pce_dev->reg.auth_cfg_aes_ccm_256 =
  3684. (1 << CRYPTO_LAST) | (1 << CRYPTO_FIRST) |
  3685. (CRYPTO_AUTH_MODE_CCM << CRYPTO_AUTH_MODE)|
  3686. (CRYPTO_AUTH_ALG_AES << CRYPTO_AUTH_ALG) |
  3687. (CRYPTO_AUTH_KEY_SZ_AES256 << CRYPTO_AUTH_KEY_SIZE) |
  3688. ((MAX_NONCE/sizeof(uint32_t)) << CRYPTO_AUTH_NONCE_NUM_WORDS);
  3689. pce_dev->reg.auth_cfg_aes_ccm_256 &= ~(1 << CRYPTO_USE_HW_KEY_AUTH);
  3690. /* Initialize auth_cfg register for kasumi/snow3g */
  3691. pce_dev->reg.auth_cfg_kasumi =
  3692. (CRYPTO_AUTH_ALG_KASUMI << CRYPTO_AUTH_ALG) |
  3693. BIT(CRYPTO_FIRST) | BIT(CRYPTO_LAST);
  3694. pce_dev->reg.auth_cfg_snow3g =
  3695. (CRYPTO_AUTH_ALG_SNOW3G << CRYPTO_AUTH_ALG) |
  3696. BIT(CRYPTO_FIRST) | BIT(CRYPTO_LAST);
  3697. return 0;
  3698. }
  3699. static int _qce_aead_ccm_req(void *handle, struct qce_req *q_req)
  3700. {
  3701. struct qce_device *pce_dev = (struct qce_device *) handle;
  3702. struct aead_request *areq = (struct aead_request *) q_req->areq;
  3703. uint32_t authsize = q_req->authsize;
  3704. uint32_t totallen_in, out_len;
  3705. uint32_t hw_pad_out = 0;
  3706. int rc = 0;
  3707. int ce_burst_size;
  3708. struct qce_cmdlist_info *cmdlistinfo = NULL;
  3709. ce_burst_size = pce_dev->ce_sps.ce_burst_size;
  3710. totallen_in = areq->cryptlen + areq->assoclen;
  3711. if (q_req->dir == QCE_ENCRYPT) {
  3712. q_req->cryptlen = areq->cryptlen;
  3713. out_len = areq->cryptlen + authsize;
  3714. hw_pad_out = ALIGN(authsize, ce_burst_size) - authsize;
  3715. } else {
  3716. q_req->cryptlen = areq->cryptlen - authsize;
  3717. out_len = q_req->cryptlen;
  3718. hw_pad_out = authsize;
  3719. }
  3720. if (pce_dev->ce_sps.minor_version == 0) {
  3721. /*
  3722. * For crypto 5.0 that has burst size alignment requirement
  3723. * for data descritpor,
  3724. * the agent above(qcrypto) prepares the src scatter list with
  3725. * memory starting with associated data, followed by
  3726. * data stream to be ciphered.
  3727. * The destination scatter list is pointing to the same
  3728. * data area as source.
  3729. */
  3730. pce_dev->src_nents = count_sg(areq->src, totallen_in);
  3731. } else {
  3732. pce_dev->src_nents = count_sg(areq->src, areq->cryptlen);
  3733. }
  3734. pce_dev->assoc_nents = count_sg(areq->assoc, areq->assoclen);
  3735. pce_dev->authsize = q_req->authsize;
  3736. /* associated data input */
  3737. qce_dma_map_sg(pce_dev->pdev, areq->assoc, pce_dev->assoc_nents,
  3738. DMA_TO_DEVICE);
  3739. /* cipher input */
  3740. qce_dma_map_sg(pce_dev->pdev, areq->src, pce_dev->src_nents,
  3741. (areq->src == areq->dst) ? DMA_BIDIRECTIONAL :
  3742. DMA_TO_DEVICE);
  3743. /* cipher + mac output for encryption */
  3744. if (areq->src != areq->dst) {
  3745. if (pce_dev->ce_sps.minor_version == 0)
  3746. /*
  3747. * The destination scatter list is pointing to the same
  3748. * data area as src.
  3749. * Note, the associated data will be pass-through
  3750. * at the begining of destination area.
  3751. */
  3752. pce_dev->dst_nents = count_sg(areq->dst,
  3753. out_len + areq->assoclen);
  3754. else
  3755. pce_dev->dst_nents = count_sg(areq->dst, out_len);
  3756. qce_dma_map_sg(pce_dev->pdev, areq->dst, pce_dev->dst_nents,
  3757. DMA_FROM_DEVICE);
  3758. } else {
  3759. pce_dev->dst_nents = pce_dev->src_nents;
  3760. }
  3761. if (pce_dev->support_cmd_dscr) {
  3762. _ce_get_cipher_cmdlistinfo(pce_dev, q_req, &cmdlistinfo);
  3763. /* set up crypto device */
  3764. rc = _ce_setup_cipher(pce_dev, q_req, totallen_in,
  3765. areq->assoclen, cmdlistinfo);
  3766. } else {
  3767. /* set up crypto device */
  3768. rc = _ce_setup_cipher_direct(pce_dev, q_req, totallen_in,
  3769. areq->assoclen);
  3770. }
  3771. if (rc < 0)
  3772. goto bad;
  3773. /* setup for callback, and issue command to bam */
  3774. pce_dev->areq = q_req->areq;
  3775. pce_dev->qce_cb = q_req->qce_cb;
  3776. /* Register callback event for EOT (End of transfer) event. */
  3777. pce_dev->ce_sps.producer.event.callback = _aead_sps_producer_callback;
  3778. pce_dev->ce_sps.producer.event.options = SPS_O_DESC_DONE;
  3779. rc = sps_register_event(pce_dev->ce_sps.producer.pipe,
  3780. &pce_dev->ce_sps.producer.event);
  3781. if (rc) {
  3782. pr_err("Producer callback registration failed rc = %d\n", rc);
  3783. goto bad;
  3784. }
  3785. _qce_sps_iovec_count_init(pce_dev);
  3786. if (pce_dev->support_cmd_dscr)
  3787. _qce_sps_add_cmd(pce_dev, SPS_IOVEC_FLAG_LOCK, cmdlistinfo,
  3788. &pce_dev->ce_sps.in_transfer);
  3789. if (pce_dev->ce_sps.minor_version == 0) {
  3790. if (_qce_sps_add_sg_data(pce_dev, areq->src, totallen_in,
  3791. &pce_dev->ce_sps.in_transfer))
  3792. goto bad;
  3793. _qce_set_flag(&pce_dev->ce_sps.in_transfer,
  3794. SPS_IOVEC_FLAG_EOT|SPS_IOVEC_FLAG_NWD);
  3795. /*
  3796. * The destination data should be big enough to
  3797. * include CCM padding.
  3798. */
  3799. if (_qce_sps_add_sg_data(pce_dev, areq->dst, out_len +
  3800. areq->assoclen + hw_pad_out,
  3801. &pce_dev->ce_sps.out_transfer))
  3802. goto bad;
  3803. if (totallen_in > SPS_MAX_PKT_SIZE) {
  3804. _qce_set_flag(&pce_dev->ce_sps.out_transfer,
  3805. SPS_IOVEC_FLAG_INT);
  3806. pce_dev->ce_sps.producer.event.options =
  3807. SPS_O_DESC_DONE;
  3808. pce_dev->ce_sps.producer_state = QCE_PIPE_STATE_IDLE;
  3809. } else {
  3810. if (_qce_sps_add_data(GET_PHYS_ADDR(
  3811. pce_dev->ce_sps.result_dump),
  3812. CRYPTO_RESULT_DUMP_SIZE,
  3813. &pce_dev->ce_sps.out_transfer))
  3814. goto bad;
  3815. _qce_set_flag(&pce_dev->ce_sps.out_transfer,
  3816. SPS_IOVEC_FLAG_INT);
  3817. pce_dev->ce_sps.producer_state = QCE_PIPE_STATE_COMP;
  3818. }
  3819. } else {
  3820. if (_qce_sps_add_sg_data(pce_dev, areq->assoc, areq->assoclen,
  3821. &pce_dev->ce_sps.in_transfer))
  3822. goto bad;
  3823. if (_qce_sps_add_sg_data(pce_dev, areq->src, areq->cryptlen,
  3824. &pce_dev->ce_sps.in_transfer))
  3825. goto bad;
  3826. _qce_set_flag(&pce_dev->ce_sps.in_transfer,
  3827. SPS_IOVEC_FLAG_EOT|SPS_IOVEC_FLAG_NWD);
  3828. /* Pass through to ignore associated data*/
  3829. if (_qce_sps_add_data(
  3830. GET_PHYS_ADDR(pce_dev->ce_sps.ignore_buffer),
  3831. areq->assoclen,
  3832. &pce_dev->ce_sps.out_transfer))
  3833. goto bad;
  3834. if (_qce_sps_add_sg_data(pce_dev, areq->dst, out_len,
  3835. &pce_dev->ce_sps.out_transfer))
  3836. goto bad;
  3837. /* Pass through to ignore hw_pad (padding of the MAC data) */
  3838. if (_qce_sps_add_data(
  3839. GET_PHYS_ADDR(pce_dev->ce_sps.ignore_buffer),
  3840. hw_pad_out, &pce_dev->ce_sps.out_transfer))
  3841. goto bad;
  3842. if (totallen_in > SPS_MAX_PKT_SIZE) {
  3843. _qce_set_flag(&pce_dev->ce_sps.out_transfer,
  3844. SPS_IOVEC_FLAG_INT);
  3845. pce_dev->ce_sps.producer_state = QCE_PIPE_STATE_IDLE;
  3846. } else {
  3847. if (_qce_sps_add_data(
  3848. GET_PHYS_ADDR(pce_dev->ce_sps.result_dump),
  3849. CRYPTO_RESULT_DUMP_SIZE,
  3850. &pce_dev->ce_sps.out_transfer))
  3851. goto bad;
  3852. _qce_set_flag(&pce_dev->ce_sps.out_transfer,
  3853. SPS_IOVEC_FLAG_INT);
  3854. pce_dev->ce_sps.producer_state = QCE_PIPE_STATE_COMP;
  3855. }
  3856. }
  3857. rc = _qce_sps_transfer(pce_dev);
  3858. if (rc)
  3859. goto bad;
  3860. return 0;
  3861. bad:
  3862. if (pce_dev->assoc_nents) {
  3863. qce_dma_unmap_sg(pce_dev->pdev, areq->assoc,
  3864. pce_dev->assoc_nents, DMA_TO_DEVICE);
  3865. }
  3866. if (pce_dev->src_nents) {
  3867. qce_dma_unmap_sg(pce_dev->pdev, areq->src, pce_dev->src_nents,
  3868. (areq->src == areq->dst) ? DMA_BIDIRECTIONAL :
  3869. DMA_TO_DEVICE);
  3870. }
  3871. if (areq->src != areq->dst) {
  3872. qce_dma_unmap_sg(pce_dev->pdev, areq->dst, pce_dev->dst_nents,
  3873. DMA_FROM_DEVICE);
  3874. }
  3875. return rc;
  3876. }
  3877. static int _qce_suspend(void *handle)
  3878. {
  3879. struct qce_device *pce_dev = (struct qce_device *)handle;
  3880. struct sps_pipe *sps_pipe_info;
  3881. if (handle == NULL)
  3882. return -ENODEV;
  3883. qce_enable_clk(pce_dev);
  3884. sps_pipe_info = pce_dev->ce_sps.consumer.pipe;
  3885. sps_disconnect(sps_pipe_info);
  3886. sps_pipe_info = pce_dev->ce_sps.producer.pipe;
  3887. sps_disconnect(sps_pipe_info);
  3888. qce_disable_clk(pce_dev);
  3889. return 0;
  3890. }
  3891. static int _qce_resume(void *handle)
  3892. {
  3893. struct qce_device *pce_dev = (struct qce_device *)handle;
  3894. struct sps_pipe *sps_pipe_info;
  3895. struct sps_connect *sps_connect_info;
  3896. int rc;
  3897. if (handle == NULL)
  3898. return -ENODEV;
  3899. qce_enable_clk(pce_dev);
  3900. sps_pipe_info = pce_dev->ce_sps.consumer.pipe;
  3901. sps_connect_info = &pce_dev->ce_sps.consumer.connect;
  3902. memset(sps_connect_info->desc.base, 0x00, sps_connect_info->desc.size);
  3903. rc = sps_connect(sps_pipe_info, sps_connect_info);
  3904. if (rc) {
  3905. pr_err("sps_connect() fail pipe_handle=0x%x, rc = %d\n",
  3906. (u32)sps_pipe_info, rc);
  3907. return rc;
  3908. }
  3909. sps_pipe_info = pce_dev->ce_sps.producer.pipe;
  3910. sps_connect_info = &pce_dev->ce_sps.producer.connect;
  3911. memset(sps_connect_info->desc.base, 0x00, sps_connect_info->desc.size);
  3912. rc = sps_connect(sps_pipe_info, sps_connect_info);
  3913. if (rc)
  3914. pr_err("sps_connect() fail pipe_handle=0x%x, rc = %d\n",
  3915. (u32)sps_pipe_info, rc);
  3916. pce_dev->ce_sps.out_transfer.user = pce_dev->ce_sps.producer.pipe;
  3917. pce_dev->ce_sps.in_transfer.user = pce_dev->ce_sps.consumer.pipe;
  3918. qce_disable_clk(pce_dev);
  3919. return rc;
  3920. }
  3921. struct qce_pm_table qce_pm_table = {_qce_suspend, _qce_resume};
  3922. EXPORT_SYMBOL(qce_pm_table);
  3923. int qce_aead_req(void *handle, struct qce_req *q_req)
  3924. {
  3925. struct qce_device *pce_dev;
  3926. struct aead_request *areq;
  3927. uint32_t authsize;
  3928. struct crypto_aead *aead;
  3929. uint32_t ivsize;
  3930. uint32_t totallen;
  3931. int rc;
  3932. struct qce_cmdlist_info *cmdlistinfo = NULL;
  3933. if (q_req->mode == QCE_MODE_CCM)
  3934. return _qce_aead_ccm_req(handle, q_req);
  3935. pce_dev = (struct qce_device *) handle;
  3936. areq = (struct aead_request *) q_req->areq;
  3937. aead = crypto_aead_reqtfm(areq);
  3938. ivsize = crypto_aead_ivsize(aead);
  3939. q_req->ivsize = ivsize;
  3940. authsize = q_req->authsize;
  3941. if (q_req->dir == QCE_ENCRYPT)
  3942. q_req->cryptlen = areq->cryptlen;
  3943. else
  3944. q_req->cryptlen = areq->cryptlen - authsize;
  3945. if ((q_req->cryptlen > UINT_MAX - areq->assoclen) ||
  3946. (q_req->cryptlen + areq->assoclen > UINT_MAX - ivsize)) {
  3947. pr_err("Integer overflow on total aead req length.\n");
  3948. return -EINVAL;
  3949. }
  3950. totallen = q_req->cryptlen + areq->assoclen + ivsize;
  3951. if (pce_dev->support_cmd_dscr) {
  3952. cmdlistinfo = _ce_get_aead_cmdlistinfo(pce_dev, q_req);
  3953. if (cmdlistinfo == NULL) {
  3954. pr_err("Unsupported aead ciphering algorithm %d, mode %d, ciphering key length %d, auth digest size %d\n",
  3955. q_req->alg, q_req->mode, q_req->encklen,
  3956. q_req->authsize);
  3957. return -EINVAL;
  3958. }
  3959. /* set up crypto device */
  3960. rc = _ce_setup_aead(pce_dev, q_req, totallen,
  3961. areq->assoclen + ivsize, cmdlistinfo);
  3962. if (rc < 0)
  3963. return -EINVAL;
  3964. };
  3965. pce_dev->assoc_nents = count_sg(areq->assoc, areq->assoclen);
  3966. if (pce_dev->ce_sps.minor_version == 0) {
  3967. /*
  3968. * For crypto 5.0 that has burst size alignment requirement
  3969. * for data descritpor,
  3970. * the agent above(qcrypto) prepares the src scatter list with
  3971. * memory starting with associated data, followed by
  3972. * iv, and data stream to be ciphered.
  3973. */
  3974. pce_dev->src_nents = count_sg(areq->src, totallen);
  3975. } else {
  3976. pce_dev->src_nents = count_sg(areq->src, q_req->cryptlen);
  3977. };
  3978. pce_dev->ivsize = q_req->ivsize;
  3979. pce_dev->authsize = q_req->authsize;
  3980. pce_dev->phy_iv_in = 0;
  3981. /* associated data input */
  3982. qce_dma_map_sg(pce_dev->pdev, areq->assoc, pce_dev->assoc_nents,
  3983. DMA_TO_DEVICE);
  3984. /* cipher input */
  3985. qce_dma_map_sg(pce_dev->pdev, areq->src, pce_dev->src_nents,
  3986. (areq->src == areq->dst) ? DMA_BIDIRECTIONAL :
  3987. DMA_TO_DEVICE);
  3988. /* cipher output for encryption */
  3989. if (areq->src != areq->dst) {
  3990. if (pce_dev->ce_sps.minor_version == 0)
  3991. /*
  3992. * The destination scatter list is pointing to the same
  3993. * data area as source.
  3994. */
  3995. pce_dev->dst_nents = count_sg(areq->dst, totallen);
  3996. else
  3997. pce_dev->dst_nents = count_sg(areq->dst,
  3998. q_req->cryptlen);
  3999. qce_dma_map_sg(pce_dev->pdev, areq->dst, pce_dev->dst_nents,
  4000. DMA_FROM_DEVICE);
  4001. }
  4002. /* cipher iv for input */
  4003. if (pce_dev->ce_sps.minor_version != 0)
  4004. pce_dev->phy_iv_in = dma_map_single(pce_dev->pdev, q_req->iv,
  4005. ivsize, DMA_TO_DEVICE);
  4006. /* setup for callback, and issue command to bam */
  4007. pce_dev->areq = q_req->areq;
  4008. pce_dev->qce_cb = q_req->qce_cb;
  4009. /* Register callback event for EOT (End of transfer) event. */
  4010. pce_dev->ce_sps.producer.event.callback = _aead_sps_producer_callback;
  4011. pce_dev->ce_sps.producer.event.options = SPS_O_DESC_DONE;
  4012. rc = sps_register_event(pce_dev->ce_sps.producer.pipe,
  4013. &pce_dev->ce_sps.producer.event);
  4014. if (rc) {
  4015. pr_err("Producer callback registration failed rc = %d\n", rc);
  4016. goto bad;
  4017. }
  4018. _qce_sps_iovec_count_init(pce_dev);
  4019. if (pce_dev->support_cmd_dscr) {
  4020. _qce_sps_add_cmd(pce_dev, SPS_IOVEC_FLAG_LOCK, cmdlistinfo,
  4021. &pce_dev->ce_sps.in_transfer);
  4022. } else {
  4023. rc = _ce_setup_aead_direct(pce_dev, q_req, totallen,
  4024. areq->assoclen + ivsize);
  4025. if (rc)
  4026. goto bad;
  4027. }
  4028. if (pce_dev->ce_sps.minor_version == 0) {
  4029. if (_qce_sps_add_sg_data(pce_dev, areq->src, totallen,
  4030. &pce_dev->ce_sps.in_transfer))
  4031. goto bad;
  4032. _qce_set_flag(&pce_dev->ce_sps.in_transfer,
  4033. SPS_IOVEC_FLAG_EOT|SPS_IOVEC_FLAG_NWD);
  4034. if (_qce_sps_add_sg_data(pce_dev, areq->dst, totallen,
  4035. &pce_dev->ce_sps.out_transfer))
  4036. goto bad;
  4037. if (totallen > SPS_MAX_PKT_SIZE) {
  4038. _qce_set_flag(&pce_dev->ce_sps.out_transfer,
  4039. SPS_IOVEC_FLAG_INT);
  4040. pce_dev->ce_sps.producer.event.options =
  4041. SPS_O_DESC_DONE;
  4042. pce_dev->ce_sps.producer_state = QCE_PIPE_STATE_IDLE;
  4043. } else {
  4044. if (_qce_sps_add_data(GET_PHYS_ADDR(
  4045. pce_dev->ce_sps.result_dump),
  4046. CRYPTO_RESULT_DUMP_SIZE,
  4047. &pce_dev->ce_sps.out_transfer))
  4048. goto bad;
  4049. _qce_set_flag(&pce_dev->ce_sps.out_transfer,
  4050. SPS_IOVEC_FLAG_INT);
  4051. pce_dev->ce_sps.producer_state = QCE_PIPE_STATE_COMP;
  4052. }
  4053. } else {
  4054. if (_qce_sps_add_sg_data(pce_dev, areq->assoc, areq->assoclen,
  4055. &pce_dev->ce_sps.in_transfer))
  4056. goto bad;
  4057. if (_qce_sps_add_data((uint32_t)pce_dev->phy_iv_in, ivsize,
  4058. &pce_dev->ce_sps.in_transfer))
  4059. goto bad;
  4060. if (_qce_sps_add_sg_data(pce_dev, areq->src, q_req->cryptlen,
  4061. &pce_dev->ce_sps.in_transfer))
  4062. goto bad;
  4063. _qce_set_flag(&pce_dev->ce_sps.in_transfer,
  4064. SPS_IOVEC_FLAG_EOT|SPS_IOVEC_FLAG_NWD);
  4065. /* Pass through to ignore associated + iv data*/
  4066. if (_qce_sps_add_data(
  4067. GET_PHYS_ADDR(pce_dev->ce_sps.ignore_buffer),
  4068. (ivsize + areq->assoclen),
  4069. &pce_dev->ce_sps.out_transfer))
  4070. goto bad;
  4071. if (_qce_sps_add_sg_data(pce_dev, areq->dst, q_req->cryptlen,
  4072. &pce_dev->ce_sps.out_transfer))
  4073. goto bad;
  4074. if (totallen > SPS_MAX_PKT_SIZE) {
  4075. _qce_set_flag(&pce_dev->ce_sps.out_transfer,
  4076. SPS_IOVEC_FLAG_INT);
  4077. pce_dev->ce_sps.producer_state = QCE_PIPE_STATE_IDLE;
  4078. } else {
  4079. if (_qce_sps_add_data(
  4080. GET_PHYS_ADDR(pce_dev->ce_sps.result_dump),
  4081. CRYPTO_RESULT_DUMP_SIZE,
  4082. &pce_dev->ce_sps.out_transfer))
  4083. goto bad;
  4084. _qce_set_flag(&pce_dev->ce_sps.out_transfer,
  4085. SPS_IOVEC_FLAG_INT);
  4086. pce_dev->ce_sps.producer_state = QCE_PIPE_STATE_COMP;
  4087. }
  4088. }
  4089. rc = _qce_sps_transfer(pce_dev);
  4090. if (rc)
  4091. goto bad;
  4092. return 0;
  4093. bad:
  4094. if (pce_dev->assoc_nents) {
  4095. qce_dma_unmap_sg(pce_dev->pdev, areq->assoc,
  4096. pce_dev->assoc_nents, DMA_TO_DEVICE);
  4097. }
  4098. if (pce_dev->src_nents) {
  4099. qce_dma_unmap_sg(pce_dev->pdev, areq->src, pce_dev->src_nents,
  4100. (areq->src == areq->dst) ? DMA_BIDIRECTIONAL :
  4101. DMA_TO_DEVICE);
  4102. }
  4103. if (areq->src != areq->dst) {
  4104. qce_dma_unmap_sg(pce_dev->pdev, areq->dst, pce_dev->dst_nents,
  4105. DMA_FROM_DEVICE);
  4106. }
  4107. if (pce_dev->phy_iv_in) {
  4108. dma_unmap_single(pce_dev->pdev, pce_dev->phy_iv_in,
  4109. ivsize, DMA_TO_DEVICE);
  4110. }
  4111. return rc;
  4112. }
  4113. EXPORT_SYMBOL(qce_aead_req);
  4114. int qce_ablk_cipher_req(void *handle, struct qce_req *c_req)
  4115. {
  4116. int rc = 0;
  4117. struct qce_device *pce_dev = (struct qce_device *) handle;
  4118. struct ablkcipher_request *areq = (struct ablkcipher_request *)
  4119. c_req->areq;
  4120. struct qce_cmdlist_info *cmdlistinfo = NULL;
  4121. pce_dev->src_nents = 0;
  4122. pce_dev->dst_nents = 0;
  4123. /* cipher input */
  4124. pce_dev->src_nents = count_sg(areq->src, areq->nbytes);
  4125. qce_dma_map_sg(pce_dev->pdev, areq->src, pce_dev->src_nents,
  4126. (areq->src == areq->dst) ? DMA_BIDIRECTIONAL :
  4127. DMA_TO_DEVICE);
  4128. /* cipher output */
  4129. if (areq->src != areq->dst) {
  4130. pce_dev->dst_nents = count_sg(areq->dst, areq->nbytes);
  4131. qce_dma_map_sg(pce_dev->pdev, areq->dst,
  4132. pce_dev->dst_nents, DMA_FROM_DEVICE);
  4133. } else {
  4134. pce_dev->dst_nents = pce_dev->src_nents;
  4135. }
  4136. pce_dev->dir = c_req->dir;
  4137. if ((pce_dev->ce_sps.minor_version == 0) && (c_req->dir == QCE_DECRYPT)
  4138. && (c_req->mode == QCE_MODE_CBC)) {
  4139. memcpy(pce_dev->dec_iv, (unsigned char *)sg_virt(areq->src) +
  4140. areq->src->length - 16,
  4141. NUM_OF_CRYPTO_CNTR_IV_REG * CRYPTO_REG_SIZE);
  4142. }
  4143. /* set up crypto device */
  4144. if (pce_dev->support_cmd_dscr) {
  4145. _ce_get_cipher_cmdlistinfo(pce_dev, c_req, &cmdlistinfo);
  4146. rc = _ce_setup_cipher(pce_dev, c_req, areq->nbytes, 0,
  4147. cmdlistinfo);
  4148. } else {
  4149. rc = _ce_setup_cipher_direct(pce_dev, c_req, areq->nbytes, 0);
  4150. }
  4151. if (rc < 0)
  4152. goto bad;
  4153. /* setup for client callback, and issue command to BAM */
  4154. pce_dev->areq = areq;
  4155. pce_dev->qce_cb = c_req->qce_cb;
  4156. /* Register callback event for EOT (End of transfer) event. */
  4157. pce_dev->ce_sps.producer.event.callback =
  4158. _ablk_cipher_sps_producer_callback;
  4159. pce_dev->ce_sps.producer.event.options = SPS_O_DESC_DONE;
  4160. rc = sps_register_event(pce_dev->ce_sps.producer.pipe,
  4161. &pce_dev->ce_sps.producer.event);
  4162. if (rc) {
  4163. pr_err("Producer callback registration failed rc = %d\n", rc);
  4164. goto bad;
  4165. }
  4166. _qce_sps_iovec_count_init(pce_dev);
  4167. if (pce_dev->support_cmd_dscr)
  4168. _qce_sps_add_cmd(pce_dev, SPS_IOVEC_FLAG_LOCK, cmdlistinfo,
  4169. &pce_dev->ce_sps.in_transfer);
  4170. if (_qce_sps_add_sg_data(pce_dev, areq->src, areq->nbytes,
  4171. &pce_dev->ce_sps.in_transfer))
  4172. goto bad;
  4173. _qce_set_flag(&pce_dev->ce_sps.in_transfer,
  4174. SPS_IOVEC_FLAG_EOT|SPS_IOVEC_FLAG_NWD);
  4175. if (_qce_sps_add_sg_data(pce_dev, areq->dst, areq->nbytes,
  4176. &pce_dev->ce_sps.out_transfer))
  4177. goto bad;
  4178. if (areq->nbytes > SPS_MAX_PKT_SIZE) {
  4179. _qce_set_flag(&pce_dev->ce_sps.out_transfer,
  4180. SPS_IOVEC_FLAG_INT);
  4181. pce_dev->ce_sps.producer_state = QCE_PIPE_STATE_IDLE;
  4182. } else {
  4183. pce_dev->ce_sps.producer_state = QCE_PIPE_STATE_COMP;
  4184. if (_qce_sps_add_data(
  4185. GET_PHYS_ADDR(pce_dev->ce_sps.result_dump),
  4186. CRYPTO_RESULT_DUMP_SIZE,
  4187. &pce_dev->ce_sps.out_transfer))
  4188. goto bad;
  4189. _qce_set_flag(&pce_dev->ce_sps.out_transfer,
  4190. SPS_IOVEC_FLAG_INT);
  4191. }
  4192. rc = _qce_sps_transfer(pce_dev);
  4193. if (rc)
  4194. goto bad;
  4195. return 0;
  4196. bad:
  4197. if (areq->src != areq->dst) {
  4198. if (pce_dev->dst_nents) {
  4199. qce_dma_unmap_sg(pce_dev->pdev, areq->dst,
  4200. pce_dev->dst_nents, DMA_FROM_DEVICE);
  4201. }
  4202. }
  4203. if (pce_dev->src_nents) {
  4204. qce_dma_unmap_sg(pce_dev->pdev, areq->src,
  4205. pce_dev->src_nents,
  4206. (areq->src == areq->dst) ?
  4207. DMA_BIDIRECTIONAL : DMA_TO_DEVICE);
  4208. }
  4209. return rc;
  4210. }
  4211. EXPORT_SYMBOL(qce_ablk_cipher_req);
  4212. int qce_process_sha_req(void *handle, struct qce_sha_req *sreq)
  4213. {
  4214. struct qce_device *pce_dev = (struct qce_device *) handle;
  4215. int rc;
  4216. struct ahash_request *areq = (struct ahash_request *)sreq->areq;
  4217. struct qce_cmdlist_info *cmdlistinfo = NULL;
  4218. pce_dev->src_nents = count_sg(sreq->src, sreq->size);
  4219. qce_dma_map_sg(pce_dev->pdev, sreq->src, pce_dev->src_nents,
  4220. DMA_TO_DEVICE);
  4221. if (pce_dev->support_cmd_dscr) {
  4222. _ce_get_hash_cmdlistinfo(pce_dev, sreq, &cmdlistinfo);
  4223. rc = _ce_setup_hash(pce_dev, sreq, cmdlistinfo);
  4224. } else {
  4225. rc = _ce_setup_hash_direct(pce_dev, sreq);
  4226. }
  4227. if (rc < 0)
  4228. goto bad;
  4229. pce_dev->areq = areq;
  4230. pce_dev->qce_cb = sreq->qce_cb;
  4231. /* Register callback event for EOT (End of transfer) event. */
  4232. pce_dev->ce_sps.producer.event.callback = _sha_sps_producer_callback;
  4233. pce_dev->ce_sps.producer.event.options = SPS_O_DESC_DONE;
  4234. rc = sps_register_event(pce_dev->ce_sps.producer.pipe,
  4235. &pce_dev->ce_sps.producer.event);
  4236. if (rc) {
  4237. pr_err("Producer callback registration failed rc = %d\n", rc);
  4238. goto bad;
  4239. }
  4240. _qce_sps_iovec_count_init(pce_dev);
  4241. if (pce_dev->support_cmd_dscr)
  4242. _qce_sps_add_cmd(pce_dev, SPS_IOVEC_FLAG_LOCK, cmdlistinfo,
  4243. &pce_dev->ce_sps.in_transfer);
  4244. if (_qce_sps_add_sg_data(pce_dev, areq->src, areq->nbytes,
  4245. &pce_dev->ce_sps.in_transfer))
  4246. goto bad;
  4247. if (areq->nbytes)
  4248. _qce_set_flag(&pce_dev->ce_sps.in_transfer,
  4249. SPS_IOVEC_FLAG_EOT|SPS_IOVEC_FLAG_NWD);
  4250. if (_qce_sps_add_data(GET_PHYS_ADDR(pce_dev->ce_sps.result_dump),
  4251. CRYPTO_RESULT_DUMP_SIZE,
  4252. &pce_dev->ce_sps.out_transfer))
  4253. goto bad;
  4254. _qce_set_flag(&pce_dev->ce_sps.out_transfer, SPS_IOVEC_FLAG_INT);
  4255. rc = _qce_sps_transfer(pce_dev);
  4256. if (rc)
  4257. goto bad;
  4258. return 0;
  4259. bad:
  4260. if (pce_dev->src_nents) {
  4261. qce_dma_unmap_sg(pce_dev->pdev, sreq->src,
  4262. pce_dev->src_nents, DMA_TO_DEVICE);
  4263. }
  4264. return rc;
  4265. }
  4266. EXPORT_SYMBOL(qce_process_sha_req);
  4267. int qce_f8_req(void *handle, struct qce_f8_req *req,
  4268. void *cookie, qce_comp_func_ptr_t qce_cb)
  4269. {
  4270. struct qce_device *pce_dev = (struct qce_device *) handle;
  4271. bool key_stream_mode;
  4272. dma_addr_t dst;
  4273. int rc;
  4274. struct qce_cmdlist_info *cmdlistinfo;
  4275. switch (req->algorithm) {
  4276. case QCE_OTA_ALGO_KASUMI:
  4277. cmdlistinfo = &pce_dev->ce_sps.cmdlistptr.f8_kasumi;
  4278. break;
  4279. case QCE_OTA_ALGO_SNOW3G:
  4280. cmdlistinfo = &pce_dev->ce_sps.cmdlistptr.f8_snow3g;
  4281. break;
  4282. default:
  4283. return -EINVAL;
  4284. };
  4285. key_stream_mode = (req->data_in == NULL);
  4286. if ((key_stream_mode && (req->data_len & 0xf)) ||
  4287. (req->bearer >= QCE_OTA_MAX_BEARER))
  4288. return -EINVAL;
  4289. /* F8 cipher input */
  4290. if (key_stream_mode)
  4291. pce_dev->phy_ota_src = 0;
  4292. else {
  4293. pce_dev->phy_ota_src = dma_map_single(pce_dev->pdev,
  4294. req->data_in, req->data_len,
  4295. (req->data_in == req->data_out) ?
  4296. DMA_BIDIRECTIONAL : DMA_TO_DEVICE);
  4297. }
  4298. /* F8 cipher output */
  4299. if (req->data_in != req->data_out) {
  4300. dst = dma_map_single(pce_dev->pdev, req->data_out,
  4301. req->data_len, DMA_FROM_DEVICE);
  4302. pce_dev->phy_ota_dst = dst;
  4303. } else {
  4304. /* in place ciphering */
  4305. dst = pce_dev->phy_ota_src;
  4306. pce_dev->phy_ota_dst = 0;
  4307. }
  4308. pce_dev->ota_size = req->data_len;
  4309. /* set up crypto device */
  4310. if (pce_dev->support_cmd_dscr)
  4311. rc = _ce_f8_setup(pce_dev, req, key_stream_mode, 1, 0,
  4312. req->data_len, cmdlistinfo);
  4313. else
  4314. rc = _ce_f8_setup_direct(pce_dev, req, key_stream_mode, 1, 0,
  4315. req->data_len);
  4316. if (rc < 0)
  4317. goto bad;
  4318. /* setup for callback, and issue command to sps */
  4319. pce_dev->areq = cookie;
  4320. pce_dev->qce_cb = qce_cb;
  4321. /* Register producer callback event for DESC_DONE event. */
  4322. pce_dev->ce_sps.producer.event.callback =
  4323. _f8_sps_producer_callback;
  4324. pce_dev->ce_sps.producer.event.options = SPS_O_DESC_DONE;
  4325. rc = sps_register_event(pce_dev->ce_sps.producer.pipe,
  4326. &pce_dev->ce_sps.producer.event);
  4327. if (rc) {
  4328. pr_err("Producer callback registration failed rc = %d\n", rc);
  4329. goto bad;
  4330. }
  4331. _qce_sps_iovec_count_init(pce_dev);
  4332. if (pce_dev->support_cmd_dscr)
  4333. _qce_sps_add_cmd(pce_dev, SPS_IOVEC_FLAG_LOCK, cmdlistinfo,
  4334. &pce_dev->ce_sps.in_transfer);
  4335. if (!key_stream_mode) {
  4336. _qce_sps_add_data((uint32_t)pce_dev->phy_ota_src, req->data_len,
  4337. &pce_dev->ce_sps.in_transfer);
  4338. _qce_set_flag(&pce_dev->ce_sps.in_transfer,
  4339. SPS_IOVEC_FLAG_EOT|SPS_IOVEC_FLAG_NWD);
  4340. }
  4341. _qce_sps_add_data((uint32_t)dst, req->data_len,
  4342. &pce_dev->ce_sps.out_transfer);
  4343. if (req->data_len > SPS_MAX_PKT_SIZE) {
  4344. _qce_set_flag(&pce_dev->ce_sps.out_transfer,
  4345. SPS_IOVEC_FLAG_INT);
  4346. pce_dev->ce_sps.producer_state = QCE_PIPE_STATE_IDLE;
  4347. } else {
  4348. pce_dev->ce_sps.producer_state = QCE_PIPE_STATE_COMP;
  4349. _qce_sps_add_data(GET_PHYS_ADDR(pce_dev->ce_sps.result_dump),
  4350. CRYPTO_RESULT_DUMP_SIZE,
  4351. &pce_dev->ce_sps.out_transfer);
  4352. _qce_set_flag(&pce_dev->ce_sps.out_transfer,
  4353. SPS_IOVEC_FLAG_INT);
  4354. }
  4355. rc = _qce_sps_transfer(pce_dev);
  4356. if (rc)
  4357. goto bad;
  4358. return 0;
  4359. bad:
  4360. if (pce_dev->phy_ota_dst != 0)
  4361. dma_unmap_single(pce_dev->pdev, pce_dev->phy_ota_dst,
  4362. req->data_len, DMA_FROM_DEVICE);
  4363. if (pce_dev->phy_ota_src != 0)
  4364. dma_unmap_single(pce_dev->pdev, pce_dev->phy_ota_src,
  4365. req->data_len,
  4366. (req->data_in == req->data_out) ?
  4367. DMA_BIDIRECTIONAL : DMA_TO_DEVICE);
  4368. return rc;
  4369. }
  4370. EXPORT_SYMBOL(qce_f8_req);
  4371. int qce_f8_multi_pkt_req(void *handle, struct qce_f8_multi_pkt_req *mreq,
  4372. void *cookie, qce_comp_func_ptr_t qce_cb)
  4373. {
  4374. struct qce_device *pce_dev = (struct qce_device *) handle;
  4375. uint16_t num_pkt = mreq->num_pkt;
  4376. uint16_t cipher_start = mreq->cipher_start;
  4377. uint16_t cipher_size = mreq->cipher_size;
  4378. struct qce_f8_req *req = &mreq->qce_f8_req;
  4379. uint32_t total;
  4380. dma_addr_t dst = 0;
  4381. int rc = 0;
  4382. struct qce_cmdlist_info *cmdlistinfo;
  4383. switch (req->algorithm) {
  4384. case QCE_OTA_ALGO_KASUMI:
  4385. cmdlistinfo = &pce_dev->ce_sps.cmdlistptr.f8_kasumi;
  4386. break;
  4387. case QCE_OTA_ALGO_SNOW3G:
  4388. cmdlistinfo = &pce_dev->ce_sps.cmdlistptr.f8_snow3g;
  4389. break;
  4390. default:
  4391. return -EINVAL;
  4392. };
  4393. total = num_pkt * req->data_len;
  4394. /* F8 cipher input */
  4395. pce_dev->phy_ota_src = dma_map_single(pce_dev->pdev,
  4396. req->data_in, total,
  4397. (req->data_in == req->data_out) ?
  4398. DMA_BIDIRECTIONAL : DMA_TO_DEVICE);
  4399. /* F8 cipher output */
  4400. if (req->data_in != req->data_out) {
  4401. dst = dma_map_single(pce_dev->pdev, req->data_out, total,
  4402. DMA_FROM_DEVICE);
  4403. pce_dev->phy_ota_dst = dst;
  4404. } else {
  4405. /* in place ciphering */
  4406. dst = pce_dev->phy_ota_src;
  4407. pce_dev->phy_ota_dst = 0;
  4408. }
  4409. pce_dev->ota_size = total;
  4410. /* set up crypto device */
  4411. if (pce_dev->support_cmd_dscr)
  4412. rc = _ce_f8_setup(pce_dev, req, false, num_pkt, cipher_start,
  4413. cipher_size, cmdlistinfo);
  4414. else
  4415. rc = _ce_f8_setup_direct(pce_dev, req, false, num_pkt,
  4416. cipher_start, cipher_size);
  4417. if (rc)
  4418. goto bad;
  4419. /* setup for callback, and issue command to sps */
  4420. pce_dev->areq = cookie;
  4421. pce_dev->qce_cb = qce_cb;
  4422. /* Register producer callback event for DESC_DONE event. */
  4423. pce_dev->ce_sps.producer.event.callback =
  4424. _f8_sps_producer_callback;
  4425. pce_dev->ce_sps.producer.event.options = SPS_O_DESC_DONE;
  4426. rc = sps_register_event(pce_dev->ce_sps.producer.pipe,
  4427. &pce_dev->ce_sps.producer.event);
  4428. if (rc) {
  4429. pr_err("Producer callback registration failed rc = %d\n", rc);
  4430. goto bad;
  4431. }
  4432. _qce_sps_iovec_count_init(pce_dev);
  4433. if (pce_dev->support_cmd_dscr)
  4434. _qce_sps_add_cmd(pce_dev, SPS_IOVEC_FLAG_LOCK, cmdlistinfo,
  4435. &pce_dev->ce_sps.in_transfer);
  4436. _qce_sps_add_data((uint32_t)pce_dev->phy_ota_src, total,
  4437. &pce_dev->ce_sps.in_transfer);
  4438. _qce_set_flag(&pce_dev->ce_sps.in_transfer,
  4439. SPS_IOVEC_FLAG_EOT|SPS_IOVEC_FLAG_NWD);
  4440. _qce_sps_add_data((uint32_t)dst, total,
  4441. &pce_dev->ce_sps.out_transfer);
  4442. if (total > SPS_MAX_PKT_SIZE) {
  4443. _qce_set_flag(&pce_dev->ce_sps.out_transfer,
  4444. SPS_IOVEC_FLAG_INT);
  4445. pce_dev->ce_sps.producer_state = QCE_PIPE_STATE_IDLE;
  4446. } else {
  4447. pce_dev->ce_sps.producer_state = QCE_PIPE_STATE_COMP;
  4448. _qce_sps_add_data(GET_PHYS_ADDR(pce_dev->ce_sps.result_dump),
  4449. CRYPTO_RESULT_DUMP_SIZE,
  4450. &pce_dev->ce_sps.out_transfer);
  4451. _qce_set_flag(&pce_dev->ce_sps.out_transfer,
  4452. SPS_IOVEC_FLAG_INT);
  4453. }
  4454. rc = _qce_sps_transfer(pce_dev);
  4455. if (rc == 0)
  4456. return 0;
  4457. bad:
  4458. if (pce_dev->phy_ota_dst)
  4459. dma_unmap_single(pce_dev->pdev, pce_dev->phy_ota_dst, total,
  4460. DMA_FROM_DEVICE);
  4461. dma_unmap_single(pce_dev->pdev, pce_dev->phy_ota_src, total,
  4462. (req->data_in == req->data_out) ?
  4463. DMA_BIDIRECTIONAL : DMA_TO_DEVICE);
  4464. return rc;
  4465. }
  4466. EXPORT_SYMBOL(qce_f8_multi_pkt_req);
  4467. int qce_f9_req(void *handle, struct qce_f9_req *req, void *cookie,
  4468. qce_comp_func_ptr_t qce_cb)
  4469. {
  4470. struct qce_device *pce_dev = (struct qce_device *) handle;
  4471. int rc;
  4472. struct qce_cmdlist_info *cmdlistinfo;
  4473. switch (req->algorithm) {
  4474. case QCE_OTA_ALGO_KASUMI:
  4475. cmdlistinfo = &pce_dev->ce_sps.cmdlistptr.f9_kasumi;
  4476. break;
  4477. case QCE_OTA_ALGO_SNOW3G:
  4478. cmdlistinfo = &pce_dev->ce_sps.cmdlistptr.f9_snow3g;
  4479. break;
  4480. default:
  4481. return -EINVAL;
  4482. };
  4483. pce_dev->phy_ota_src = dma_map_single(pce_dev->pdev, req->message,
  4484. req->msize, DMA_TO_DEVICE);
  4485. pce_dev->ota_size = req->msize;
  4486. if (pce_dev->support_cmd_dscr)
  4487. rc = _ce_f9_setup(pce_dev, req, cmdlistinfo);
  4488. else
  4489. rc = _ce_f9_setup_direct(pce_dev, req);
  4490. if (rc < 0)
  4491. goto bad;
  4492. /* setup for callback, and issue command to sps */
  4493. pce_dev->areq = cookie;
  4494. pce_dev->qce_cb = qce_cb;
  4495. /* Register producer callback event for DESC_DONE event. */
  4496. pce_dev->ce_sps.producer.event.callback = _f9_sps_producer_callback;
  4497. pce_dev->ce_sps.producer.event.options = SPS_O_DESC_DONE;
  4498. rc = sps_register_event(pce_dev->ce_sps.producer.pipe,
  4499. &pce_dev->ce_sps.producer.event);
  4500. if (rc) {
  4501. pr_err("Producer callback registration failed rc = %d\n", rc);
  4502. goto bad;
  4503. }
  4504. _qce_sps_iovec_count_init(pce_dev);
  4505. if (pce_dev->support_cmd_dscr)
  4506. _qce_sps_add_cmd(pce_dev, SPS_IOVEC_FLAG_LOCK, cmdlistinfo,
  4507. &pce_dev->ce_sps.in_transfer);
  4508. _qce_sps_add_data((uint32_t)pce_dev->phy_ota_src, req->msize,
  4509. &pce_dev->ce_sps.in_transfer);
  4510. _qce_set_flag(&pce_dev->ce_sps.in_transfer,
  4511. SPS_IOVEC_FLAG_EOT|SPS_IOVEC_FLAG_NWD);
  4512. _qce_sps_add_data(GET_PHYS_ADDR(pce_dev->ce_sps.result_dump),
  4513. CRYPTO_RESULT_DUMP_SIZE,
  4514. &pce_dev->ce_sps.out_transfer);
  4515. _qce_set_flag(&pce_dev->ce_sps.out_transfer, SPS_IOVEC_FLAG_INT);
  4516. rc = _qce_sps_transfer(pce_dev);
  4517. if (rc)
  4518. goto bad;
  4519. return 0;
  4520. bad:
  4521. dma_unmap_single(pce_dev->pdev, pce_dev->phy_ota_src,
  4522. req->msize, DMA_TO_DEVICE);
  4523. return rc;
  4524. }
  4525. EXPORT_SYMBOL(qce_f9_req);
  4526. static int __qce_get_device_tree_data(struct platform_device *pdev,
  4527. struct qce_device *pce_dev)
  4528. {
  4529. struct resource *resource;
  4530. int rc = 0;
  4531. pce_dev->is_shared = of_property_read_bool((&pdev->dev)->of_node,
  4532. "qcom,ce-hw-shared");
  4533. pce_dev->support_hw_key = of_property_read_bool((&pdev->dev)->of_node,
  4534. "qcom,ce-hw-key");
  4535. pce_dev->use_sw_aes_cbc_ecb_ctr_algo =
  4536. of_property_read_bool((&pdev->dev)->of_node,
  4537. "qcom,use-sw-aes-cbc-ecb-ctr-algo");
  4538. pce_dev->use_sw_aead_algo =
  4539. of_property_read_bool((&pdev->dev)->of_node,
  4540. "qcom,use-sw-aead-algo");
  4541. pce_dev->use_sw_aes_xts_algo =
  4542. of_property_read_bool((&pdev->dev)->of_node,
  4543. "qcom,use-sw-aes-xts-algo");
  4544. pce_dev->use_sw_ahash_algo =
  4545. of_property_read_bool((&pdev->dev)->of_node,
  4546. "qcom,use-sw-ahash-algo");
  4547. pce_dev->use_sw_hmac_algo =
  4548. of_property_read_bool((&pdev->dev)->of_node,
  4549. "qcom,use-sw-hmac-algo");
  4550. pce_dev->use_sw_aes_ccm_algo =
  4551. of_property_read_bool((&pdev->dev)->of_node,
  4552. "qcom,use-sw-aes-ccm-algo");
  4553. pce_dev->support_clk_mgmt_sus_res = of_property_read_bool(
  4554. (&pdev->dev)->of_node, "qcom,clk-mgmt-sus-res");
  4555. if (of_property_read_u32((&pdev->dev)->of_node,
  4556. "qcom,bam-pipe-pair",
  4557. &pce_dev->ce_sps.pipe_pair_index)) {
  4558. pr_err("Fail to get bam pipe pair information.\n");
  4559. return -EINVAL;
  4560. } else {
  4561. pr_warn("bam_pipe_pair=0x%x", pce_dev->ce_sps.pipe_pair_index);
  4562. }
  4563. if (of_property_read_u32((&pdev->dev)->of_node,
  4564. "qcom,ce-device",
  4565. &pce_dev->ce_sps.ce_device)) {
  4566. pr_err("Fail to get CE device information.\n");
  4567. return -EINVAL;
  4568. } else {
  4569. pr_warn("ce-device =0x%x", pce_dev->ce_sps.ce_device);
  4570. }
  4571. pce_dev->ce_sps.dest_pipe_index = 2 * pce_dev->ce_sps.pipe_pair_index;
  4572. pce_dev->ce_sps.src_pipe_index = pce_dev->ce_sps.dest_pipe_index + 1;
  4573. resource = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  4574. "crypto-base");
  4575. if (resource) {
  4576. pce_dev->phy_iobase = resource->start;
  4577. pce_dev->iobase = ioremap_nocache(resource->start,
  4578. resource_size(resource));
  4579. if (!pce_dev->iobase) {
  4580. pr_err("Can not map CRYPTO io memory\n");
  4581. return -ENOMEM;
  4582. }
  4583. } else {
  4584. pr_err("CRYPTO HW mem unavailable.\n");
  4585. return -ENODEV;
  4586. }
  4587. pr_warn("ce_phy_reg_base=0x%x ", pce_dev->phy_iobase);
  4588. pr_warn("ce_virt_reg_base=0x%x\n", (uint32_t)pce_dev->iobase);
  4589. resource = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  4590. "crypto-bam-base");
  4591. if (resource) {
  4592. pce_dev->bam_mem = resource->start;
  4593. pce_dev->bam_mem_size = resource_size(resource);
  4594. } else {
  4595. pr_err("CRYPTO BAM mem unavailable.\n");
  4596. rc = -ENODEV;
  4597. goto err_getting_bam_info;
  4598. }
  4599. pr_warn("ce_bam_phy_reg_base=0x%x ", pce_dev->bam_mem);
  4600. resource = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  4601. if (resource) {
  4602. pce_dev->ce_sps.bam_irq = resource->start;
  4603. pr_warn("CRYPTO BAM IRQ = %d.\n", pce_dev->ce_sps.bam_irq);
  4604. } else {
  4605. pr_err("CRYPTO BAM IRQ unavailable.\n");
  4606. goto err_dev;
  4607. }
  4608. return rc;
  4609. err_dev:
  4610. if (pce_dev->ce_sps.bam_iobase)
  4611. iounmap(pce_dev->ce_sps.bam_iobase);
  4612. err_getting_bam_info:
  4613. if (pce_dev->iobase)
  4614. iounmap(pce_dev->iobase);
  4615. return rc;
  4616. }
  4617. static int __qce_init_clk(struct qce_device *pce_dev)
  4618. {
  4619. int rc = 0;
  4620. struct clk *ce_core_clk;
  4621. struct clk *ce_clk;
  4622. struct clk *ce_core_src_clk;
  4623. struct clk *ce_bus_clk;
  4624. /* Get CE3 src core clk. */
  4625. ce_core_src_clk = clk_get(pce_dev->pdev, "core_clk_src");
  4626. if (!IS_ERR(ce_core_src_clk)) {
  4627. pce_dev->ce_core_src_clk = ce_core_src_clk;
  4628. /* Set the core src clk @100Mhz */
  4629. rc = clk_set_rate(pce_dev->ce_core_src_clk, 100000000);
  4630. if (rc) {
  4631. clk_put(pce_dev->ce_core_src_clk);
  4632. pce_dev->ce_core_src_clk = NULL;
  4633. pr_err("Unable to set the core src clk @100Mhz.\n");
  4634. goto err_clk;
  4635. }
  4636. } else {
  4637. pr_warn("Unable to get CE core src clk, set to NULL\n");
  4638. pce_dev->ce_core_src_clk = NULL;
  4639. }
  4640. /* Get CE core clk */
  4641. ce_core_clk = clk_get(pce_dev->pdev, "core_clk");
  4642. if (IS_ERR(ce_core_clk)) {
  4643. rc = PTR_ERR(ce_core_clk);
  4644. pr_err("Unable to get CE core clk\n");
  4645. if (pce_dev->ce_core_src_clk != NULL)
  4646. clk_put(pce_dev->ce_core_src_clk);
  4647. goto err_clk;
  4648. }
  4649. pce_dev->ce_core_clk = ce_core_clk;
  4650. /* Get CE Interface clk */
  4651. ce_clk = clk_get(pce_dev->pdev, "iface_clk");
  4652. if (IS_ERR(ce_clk)) {
  4653. rc = PTR_ERR(ce_clk);
  4654. pr_err("Unable to get CE interface clk\n");
  4655. if (pce_dev->ce_core_src_clk != NULL)
  4656. clk_put(pce_dev->ce_core_src_clk);
  4657. clk_put(pce_dev->ce_core_clk);
  4658. goto err_clk;
  4659. }
  4660. pce_dev->ce_clk = ce_clk;
  4661. /* Get CE AXI clk */
  4662. ce_bus_clk = clk_get(pce_dev->pdev, "bus_clk");
  4663. if (IS_ERR(ce_bus_clk)) {
  4664. rc = PTR_ERR(ce_bus_clk);
  4665. pr_err("Unable to get CE BUS interface clk\n");
  4666. if (pce_dev->ce_core_src_clk != NULL)
  4667. clk_put(pce_dev->ce_core_src_clk);
  4668. clk_put(pce_dev->ce_core_clk);
  4669. clk_put(pce_dev->ce_clk);
  4670. goto err_clk;
  4671. }
  4672. pce_dev->ce_bus_clk = ce_bus_clk;
  4673. err_clk:
  4674. if (rc)
  4675. pr_err("Unable to init CE clks, rc = %d\n", rc);
  4676. return rc;
  4677. }
  4678. static void __qce_deinit_clk(struct qce_device *pce_dev)
  4679. {
  4680. if (pce_dev->ce_clk != NULL) {
  4681. clk_put(pce_dev->ce_clk);
  4682. pce_dev->ce_clk = NULL;
  4683. }
  4684. if (pce_dev->ce_core_clk != NULL) {
  4685. clk_put(pce_dev->ce_core_clk);
  4686. pce_dev->ce_core_clk = NULL;
  4687. }
  4688. if (pce_dev->ce_bus_clk != NULL) {
  4689. clk_put(pce_dev->ce_bus_clk);
  4690. pce_dev->ce_bus_clk = NULL;
  4691. }
  4692. if (pce_dev->ce_core_src_clk != NULL) {
  4693. clk_put(pce_dev->ce_core_src_clk);
  4694. pce_dev->ce_core_src_clk = NULL;
  4695. }
  4696. }
  4697. int qce_enable_clk(void *handle)
  4698. {
  4699. struct qce_device *pce_dev = (struct qce_device *) handle;
  4700. int rc = 0;
  4701. /* Enable CE core clk */
  4702. if (pce_dev->ce_core_clk != NULL) {
  4703. rc = clk_prepare_enable(pce_dev->ce_core_clk);
  4704. if (rc) {
  4705. pr_err("Unable to enable/prepare CE core clk\n");
  4706. return rc;
  4707. }
  4708. }
  4709. /* Enable CE clk */
  4710. if (pce_dev->ce_clk != NULL) {
  4711. rc = clk_prepare_enable(pce_dev->ce_clk);
  4712. if (rc) {
  4713. pr_err("Unable to enable/prepare CE iface clk\n");
  4714. clk_disable_unprepare(pce_dev->ce_core_clk);
  4715. return rc;
  4716. }
  4717. }
  4718. /* Enable AXI clk */
  4719. if (pce_dev->ce_bus_clk != NULL) {
  4720. rc = clk_prepare_enable(pce_dev->ce_bus_clk);
  4721. if (rc) {
  4722. pr_err("Unable to enable/prepare CE BUS clk\n");
  4723. clk_disable_unprepare(pce_dev->ce_clk);
  4724. clk_disable_unprepare(pce_dev->ce_core_clk);
  4725. return rc;
  4726. }
  4727. }
  4728. return rc;
  4729. }
  4730. EXPORT_SYMBOL(qce_enable_clk);
  4731. int qce_disable_clk(void *handle)
  4732. {
  4733. struct qce_device *pce_dev = (struct qce_device *) handle;
  4734. int rc = 0;
  4735. if (pce_dev->ce_clk != NULL)
  4736. clk_disable_unprepare(pce_dev->ce_clk);
  4737. if (pce_dev->ce_core_clk != NULL)
  4738. clk_disable_unprepare(pce_dev->ce_core_clk);
  4739. if (pce_dev->ce_bus_clk != NULL)
  4740. clk_disable_unprepare(pce_dev->ce_bus_clk);
  4741. return rc;
  4742. }
  4743. EXPORT_SYMBOL(qce_disable_clk);
  4744. /* crypto engine open function. */
  4745. void *qce_open(struct platform_device *pdev, int *rc)
  4746. {
  4747. struct qce_device *pce_dev;
  4748. pce_dev = kzalloc(sizeof(struct qce_device), GFP_KERNEL);
  4749. if (!pce_dev) {
  4750. *rc = -ENOMEM;
  4751. pr_err("Can not allocate memory: %d\n", *rc);
  4752. return NULL;
  4753. }
  4754. pce_dev->pdev = &pdev->dev;
  4755. if (pdev->dev.of_node) {
  4756. *rc = __qce_get_device_tree_data(pdev, pce_dev);
  4757. if (*rc)
  4758. goto err_pce_dev;
  4759. } else {
  4760. *rc = -EINVAL;
  4761. pr_err("Device Node not found.\n");
  4762. goto err_pce_dev;
  4763. }
  4764. pce_dev->memsize = 10 * PAGE_SIZE;
  4765. pce_dev->coh_vmem = dma_alloc_coherent(pce_dev->pdev,
  4766. pce_dev->memsize, &pce_dev->coh_pmem, GFP_KERNEL);
  4767. if (pce_dev->coh_vmem == NULL) {
  4768. *rc = -ENOMEM;
  4769. pr_err("Can not allocate coherent memory for sps data\n");
  4770. goto err_iobase;
  4771. }
  4772. *rc = __qce_init_clk(pce_dev);
  4773. if (*rc)
  4774. goto err_mem;
  4775. *rc = qce_enable_clk(pce_dev);
  4776. if (*rc)
  4777. goto err_enable_clk;
  4778. if (_probe_ce_engine(pce_dev)) {
  4779. *rc = -ENXIO;
  4780. goto err;
  4781. }
  4782. *rc = 0;
  4783. qce_init_ce_cfg_val(pce_dev);
  4784. *rc = qce_sps_init(pce_dev);
  4785. if (*rc)
  4786. goto err;
  4787. qce_setup_ce_sps_data(pce_dev);
  4788. qce_disable_clk(pce_dev);
  4789. return pce_dev;
  4790. err:
  4791. qce_disable_clk(pce_dev);
  4792. err_enable_clk:
  4793. __qce_deinit_clk(pce_dev);
  4794. err_mem:
  4795. if (pce_dev->coh_vmem)
  4796. dma_free_coherent(pce_dev->pdev, pce_dev->memsize,
  4797. pce_dev->coh_vmem, pce_dev->coh_pmem);
  4798. err_iobase:
  4799. if (pce_dev->iobase)
  4800. iounmap(pce_dev->iobase);
  4801. err_pce_dev:
  4802. kfree(pce_dev);
  4803. return NULL;
  4804. }
  4805. EXPORT_SYMBOL(qce_open);
  4806. /* crypto engine close function. */
  4807. int qce_close(void *handle)
  4808. {
  4809. struct qce_device *pce_dev = (struct qce_device *) handle;
  4810. if (handle == NULL)
  4811. return -ENODEV;
  4812. qce_enable_clk(pce_dev);
  4813. qce_sps_exit(pce_dev);
  4814. if (pce_dev->iobase)
  4815. iounmap(pce_dev->iobase);
  4816. if (pce_dev->coh_vmem)
  4817. dma_free_coherent(pce_dev->pdev, pce_dev->memsize,
  4818. pce_dev->coh_vmem, pce_dev->coh_pmem);
  4819. qce_disable_clk(pce_dev);
  4820. __qce_deinit_clk(pce_dev);
  4821. kfree(handle);
  4822. return 0;
  4823. }
  4824. EXPORT_SYMBOL(qce_close);
  4825. #define OTA_SUPPORT_MASK (1 << CRYPTO_ENCR_SNOW3G_SEL |\
  4826. 1 << CRYPTO_ENCR_KASUMI_SEL |\
  4827. 1 << CRYPTO_AUTH_SNOW3G_SEL |\
  4828. 1 << CRYPTO_AUTH_KASUMI_SEL)
  4829. int qce_hw_support(void *handle, struct ce_hw_support *ce_support)
  4830. {
  4831. struct qce_device *pce_dev = (struct qce_device *)handle;
  4832. if (ce_support == NULL)
  4833. return -EINVAL;
  4834. ce_support->sha1_hmac_20 = false;
  4835. ce_support->sha1_hmac = false;
  4836. ce_support->sha256_hmac = false;
  4837. ce_support->sha_hmac = true;
  4838. ce_support->cmac = true;
  4839. ce_support->aes_key_192 = false;
  4840. ce_support->aes_xts = true;
  4841. if ((pce_dev->engines_avail & OTA_SUPPORT_MASK) == OTA_SUPPORT_MASK)
  4842. ce_support->ota = true;
  4843. else
  4844. ce_support->ota = false;
  4845. ce_support->bam = true;
  4846. ce_support->is_shared = (pce_dev->is_shared == 1) ? true : false;
  4847. ce_support->hw_key = pce_dev->support_hw_key;
  4848. ce_support->aes_ccm = true;
  4849. ce_support->clk_mgmt_sus_res = pce_dev->support_clk_mgmt_sus_res;
  4850. if (pce_dev->ce_sps.minor_version)
  4851. ce_support->aligned_only = false;
  4852. else
  4853. ce_support->aligned_only = true;
  4854. ce_support->use_sw_aes_cbc_ecb_ctr_algo =
  4855. pce_dev->use_sw_aes_cbc_ecb_ctr_algo;
  4856. ce_support->use_sw_aead_algo =
  4857. pce_dev->use_sw_aead_algo;
  4858. ce_support->use_sw_aes_xts_algo =
  4859. pce_dev->use_sw_aes_xts_algo;
  4860. ce_support->use_sw_ahash_algo =
  4861. pce_dev->use_sw_ahash_algo;
  4862. ce_support->use_sw_hmac_algo =
  4863. pce_dev->use_sw_hmac_algo;
  4864. ce_support->use_sw_aes_ccm_algo =
  4865. pce_dev->use_sw_aes_ccm_algo;
  4866. ce_support->ce_device = pce_dev->ce_sps.ce_device;
  4867. return 0;
  4868. }
  4869. EXPORT_SYMBOL(qce_hw_support);
  4870. MODULE_LICENSE("GPL v2");
  4871. MODULE_DESCRIPTION("Crypto Engine driver");