qce50.c 195 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * QTI Crypto Engine driver.
  4. *
  5. * Copyright (c) 2012-2021, The Linux Foundation. All rights reserved.
  6. * Copyright (c) 2023 Qualcomm Innovation Center, Inc. All rights reserved.
  7. */
  8. #define pr_fmt(fmt) "QCE50: %s: " fmt, __func__
  9. #include <linux/types.h>
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/mod_devicetable.h>
  13. #include <linux/device.h>
  14. #include <linux/clk.h>
  15. #include <linux/err.h>
  16. #include <linux/dma-mapping.h>
  17. #include <linux/io.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/spinlock.h>
  20. #include <linux/delay.h>
  21. #include <linux/crypto.h>
  22. #include <linux/bitops.h>
  23. #include <crypto/hash.h>
  24. #include <crypto/sha1.h>
  25. #include <soc/qcom/socinfo.h>
  26. #include <linux/iommu.h>
  27. #include "qcrypto.h"
  28. #include "qce.h"
  29. #include "qce50.h"
  30. #include "qcryptohw_50.h"
  31. #include "qce_ota.h"
  32. #define CRYPTO_SMMU_IOVA_START 0x10000000
  33. #define CRYPTO_SMMU_IOVA_SIZE 0x40000000
  34. #define CRYPTO_CONFIG_RESET 0xE01EF
  35. #define MAX_SPS_DESC_FIFO_SIZE 0xfff0
  36. #define QCE_MAX_NUM_DSCR 0x200
  37. #define QCE_SECTOR_SIZE 0x200
  38. #define CE_CLK_100MHZ 100000000
  39. #define CE_CLK_DIV 1000000
  40. #define CRYPTO_CORE_MAJOR_VER_NUM 0x05
  41. #define CRYPTO_CORE_MINOR_VER_NUM 0x03
  42. #define CRYPTO_CORE_STEP_VER_NUM 0x1
  43. #define CRYPTO_REQ_USER_PAT 0xdead0000
  44. static DEFINE_MUTEX(bam_register_lock);
  45. static DEFINE_MUTEX(qce_iomap_mutex);
  46. struct bam_registration_info {
  47. struct list_head qlist;
  48. unsigned long handle;
  49. uint32_t cnt;
  50. uint32_t bam_mem;
  51. void __iomem *bam_iobase;
  52. bool support_cmd_dscr;
  53. };
  54. static LIST_HEAD(qce50_bam_list);
  55. /* Used to determine the mode */
  56. #define MAX_BUNCH_MODE_REQ 2
  57. /* Max number of request supported */
  58. #define MAX_QCE_BAM_REQ 8
  59. /* Interrupt flag will be set for every SET_INTR_AT_REQ request */
  60. #define SET_INTR_AT_REQ (MAX_QCE_BAM_REQ / 2)
  61. /* To create extra request space to hold dummy request */
  62. #define MAX_QCE_BAM_REQ_WITH_DUMMY_REQ (MAX_QCE_BAM_REQ + 1)
  63. /* Allocate the memory for MAX_QCE_BAM_REQ + 1 (for dummy request) */
  64. #define MAX_QCE_ALLOC_BAM_REQ MAX_QCE_BAM_REQ_WITH_DUMMY_REQ
  65. /* QCE driver modes */
  66. #define IN_INTERRUPT_MODE 0
  67. #define IN_BUNCH_MODE 1
  68. /* Dummy request data length */
  69. #define DUMMY_REQ_DATA_LEN 64
  70. /* Delay timer to expire when in bunch mode */
  71. #define DELAY_IN_JIFFIES 5
  72. /* Index to point the dummy request */
  73. #define DUMMY_REQ_INDEX MAX_QCE_BAM_REQ
  74. #define TOTAL_IOVEC_SPACE_PER_PIPE (QCE_MAX_NUM_DSCR * sizeof(struct sps_iovec))
  75. #define AES_CTR_IV_CTR_SIZE 64
  76. #define QCE_STATUS1_NO_ERROR 0x2000006
  77. // Crypto Engines 5.7 and below
  78. // Key timer expiry for pipes 1-15 (Status3)
  79. #define CRYPTO5_LEGACY_TIMER_EXPIRED_STATUS3 0x0000FF00
  80. // Key timer expiry for pipes 16-19 (Status6)
  81. #define CRYPTO5_LEGACY_TIMER_EXPIRED_STATUS6 0x00000300
  82. // Key pause for pipes 1-15 (Status3)
  83. #define CRYPTO5_LEGACY_KEY_PAUSE_STATUS3 0xFF000000
  84. // Key pause for pipes 16-19 (Status6)
  85. #define CRYPTO5_LEGACY_KEY_PAUSE_STATUS6 0x3000000
  86. // Crypto Engines 5.8 and above
  87. // Key timer expiry for all pipes (Status3)
  88. #define CRYPTO58_TIMER_EXPIRED 0x00000010
  89. // Key pause for all pipes (Status3)
  90. #define CRYPTO58_KEY_PAUSE 0x00001000
  91. // Key index for Status3 (Timer and Key Pause)
  92. #define KEY_INDEX_SHIFT 16
  93. enum qce_owner {
  94. QCE_OWNER_NONE = 0,
  95. QCE_OWNER_CLIENT = 1,
  96. QCE_OWNER_TIMEOUT = 2
  97. };
  98. struct dummy_request {
  99. struct qce_sha_req sreq;
  100. struct scatterlist sg;
  101. struct ahash_request areq;
  102. };
  103. /*
  104. * CE HW device structure.
  105. * Each engine has an instance of the structure.
  106. * Each engine can only handle one crypto operation at one time. It is up to
  107. * the sw above to ensure single threading of operation on an engine.
  108. */
  109. struct qce_device {
  110. struct device *pdev; /* Handle to platform_device structure */
  111. struct bam_registration_info *pbam;
  112. unsigned char *coh_vmem; /* Allocated coherent virtual memory */
  113. dma_addr_t coh_pmem; /* Allocated coherent physical memory */
  114. int memsize; /* Memory allocated */
  115. unsigned char *iovec_vmem; /* Allocate iovec virtual memory */
  116. int iovec_memsize; /* Memory allocated */
  117. uint32_t bam_mem; /* bam physical address, from DT */
  118. uint32_t bam_mem_size; /* bam io size, from DT */
  119. int is_shared; /* CE HW is shared */
  120. bool support_cmd_dscr;
  121. bool support_hw_key;
  122. bool support_clk_mgmt_sus_res;
  123. bool support_only_core_src_clk;
  124. bool request_bw_before_clk;
  125. void __iomem *iobase; /* Virtual io base of CE HW */
  126. unsigned int phy_iobase; /* Physical io base of CE HW */
  127. struct clk *ce_core_src_clk; /* Handle to CE src clk*/
  128. struct clk *ce_core_clk; /* Handle to CE clk */
  129. struct clk *ce_clk; /* Handle to CE clk */
  130. struct clk *ce_bus_clk; /* Handle to CE AXI clk*/
  131. bool no_get_around;
  132. bool no_ccm_mac_status_get_around;
  133. unsigned int ce_opp_freq_hz;
  134. bool use_sw_aes_cbc_ecb_ctr_algo;
  135. bool use_sw_aead_algo;
  136. bool use_sw_aes_xts_algo;
  137. bool use_sw_ahash_algo;
  138. bool use_sw_hmac_algo;
  139. bool use_sw_aes_ccm_algo;
  140. uint32_t engines_avail;
  141. struct qce_ce_cfg_reg_setting reg;
  142. struct ce_bam_info ce_bam_info;
  143. struct ce_request_info ce_request_info[MAX_QCE_ALLOC_BAM_REQ];
  144. unsigned int ce_request_index;
  145. enum qce_owner owner;
  146. atomic_t no_of_queued_req;
  147. struct timer_list timer;
  148. struct dummy_request dummyreq;
  149. unsigned int mode;
  150. unsigned int intr_cadence;
  151. unsigned int dev_no;
  152. struct qce_driver_stats qce_stats;
  153. atomic_t bunch_cmd_seq;
  154. atomic_t last_intr_seq;
  155. bool cadence_flag;
  156. uint8_t *dummyreq_in_buf;
  157. struct dma_iommu_mapping *smmu_mapping;
  158. bool enable_s1_smmu;
  159. bool no_clock_support;
  160. bool kernel_pipes_support;
  161. bool offload_pipes_support;
  162. };
  163. static void print_notify_debug(struct sps_event_notify *notify);
  164. static void _sps_producer_callback(struct sps_event_notify *notify);
  165. static int qce_dummy_req(struct qce_device *pce_dev);
  166. static int _qce50_disp_stats;
  167. /* Standard initialization vector for SHA-1, source: FIPS 180-2 */
  168. static uint32_t _std_init_vector_sha1[] = {
  169. 0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0
  170. };
  171. /* Standard initialization vector for SHA-256, source: FIPS 180-2 */
  172. static uint32_t _std_init_vector_sha256[] = {
  173. 0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A,
  174. 0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19
  175. };
  176. /*
  177. * Requests for offload operations do not require explicit dma operations
  178. * as they already have SMMU mapped source/destination buffers.
  179. */
  180. static bool is_offload_op(int op)
  181. {
  182. return (op == QCE_OFFLOAD_HLOS_HLOS || op == QCE_OFFLOAD_HLOS_CPB ||
  183. op == QCE_OFFLOAD_CPB_HLOS);
  184. }
  185. static uint32_t qce_get_config_be(struct qce_device *pce_dev,
  186. uint32_t pipe_pair)
  187. {
  188. uint32_t beats = (pce_dev->ce_bam_info.ce_burst_size >> 3) - 1;
  189. return (beats << CRYPTO_REQ_SIZE |
  190. BIT(CRYPTO_MASK_DOUT_INTR) | BIT(CRYPTO_MASK_DIN_INTR) |
  191. BIT(CRYPTO_MASK_OP_DONE_INTR) | 0 << CRYPTO_HIGH_SPD_EN_N |
  192. pipe_pair << CRYPTO_PIPE_SET_SELECT);
  193. }
  194. static void dump_status_regs(unsigned int *status)
  195. {
  196. pr_info("%s: CRYPTO_STATUS_REG = 0x%x\n", __func__, status[0]);
  197. pr_info("%s: CRYPTO_STATUS2_REG = 0x%x\n", __func__, status[1]);
  198. pr_info("%s: CRYPTO_STATUS3_REG = 0x%x\n", __func__, status[2]);
  199. pr_info("%s: CRYPTO_STATUS4_REG = 0x%x\n", __func__, status[3]);
  200. pr_info("%s: CRYPTO_STATUS5_REG = 0x%x\n", __func__, status[4]);
  201. pr_info("%s: CRYPTO_STATUS6_REG = 0x%x\n", __func__, status[5]);
  202. }
  203. void qce_get_crypto_status(void *handle, struct qce_error *error)
  204. {
  205. struct qce_device *pce_dev = (struct qce_device *) handle;
  206. unsigned int status[6] = {0};
  207. status[0] = readl_relaxed(pce_dev->iobase + CRYPTO_STATUS_REG);
  208. status[1] = readl_relaxed(pce_dev->iobase + CRYPTO_STATUS2_REG);
  209. status[2] = readl_relaxed(pce_dev->iobase + CRYPTO_STATUS3_REG);
  210. status[3] = readl_relaxed(pce_dev->iobase + CRYPTO_STATUS4_REG);
  211. status[4] = readl_relaxed(pce_dev->iobase + CRYPTO_STATUS5_REG);
  212. status[5] = readl_relaxed(pce_dev->iobase + CRYPTO_STATUS6_REG);
  213. #ifdef QCE_DEBUG
  214. dump_status_regs(status);
  215. #endif
  216. if (status[0] != QCE_STATUS1_NO_ERROR || status[1]) {
  217. if (pce_dev->ce_bam_info.minor_version >= 8) {
  218. if (status[2] & CRYPTO58_TIMER_EXPIRED) {
  219. error->timer_error = true;
  220. pr_err("%s: timer expired, index = 0x%x\n",
  221. __func__, (status[2] >> KEY_INDEX_SHIFT));
  222. } else if (status[2] & CRYPTO58_KEY_PAUSE) {
  223. error->key_paused = true;
  224. pr_err("%s: key paused, index = 0x%x\n",
  225. __func__, (status[2] >> KEY_INDEX_SHIFT));
  226. } else {
  227. pr_err("%s: generic error, refer all status\n",
  228. __func__);
  229. error->generic_error = true;
  230. }
  231. } else {
  232. if ((status[2] & CRYPTO5_LEGACY_TIMER_EXPIRED_STATUS3) ||
  233. (status[5] & CRYPTO5_LEGACY_TIMER_EXPIRED_STATUS6)) {
  234. error->timer_error = true;
  235. pr_err("%s: timer expired, refer status 3 and 6\n",
  236. __func__);
  237. }
  238. else if ((status[2] & CRYPTO5_LEGACY_KEY_PAUSE_STATUS3) ||
  239. (status[5] & CRYPTO5_LEGACY_KEY_PAUSE_STATUS6)) {
  240. error->key_paused = true;
  241. pr_err("%s: key paused, reder status 3 and 6\n",
  242. __func__);
  243. } else {
  244. pr_err("%s: generic error, refer all status\n",
  245. __func__);
  246. error->generic_error = true;
  247. }
  248. }
  249. dump_status_regs(status);
  250. return;
  251. }
  252. error->no_error = true;
  253. pr_info("%s: No crypto error, status1 = 0x%x\n",
  254. __func__, status[0]);
  255. return;
  256. }
  257. EXPORT_SYMBOL(qce_get_crypto_status);
  258. static int qce_crypto_config(struct qce_device *pce_dev,
  259. enum qce_offload_op_enum offload_op)
  260. {
  261. uint32_t config_be = 0;
  262. switch (offload_op) {
  263. case QCE_OFFLOAD_NONE:
  264. config_be = qce_get_config_be(pce_dev,
  265. pce_dev->ce_bam_info.pipe_pair_index[QCE_OFFLOAD_NONE]);
  266. break;
  267. case QCE_OFFLOAD_HLOS_HLOS:
  268. config_be = qce_get_config_be(pce_dev,
  269. pce_dev->ce_bam_info.pipe_pair_index[QCE_OFFLOAD_HLOS_HLOS]);
  270. break;
  271. case QCE_OFFLOAD_HLOS_CPB:
  272. config_be = qce_get_config_be(pce_dev,
  273. pce_dev->ce_bam_info.pipe_pair_index[QCE_OFFLOAD_HLOS_CPB]);
  274. break;
  275. case QCE_OFFLOAD_CPB_HLOS:
  276. config_be = qce_get_config_be(pce_dev,
  277. pce_dev->ce_bam_info.pipe_pair_index[QCE_OFFLOAD_CPB_HLOS]);
  278. break;
  279. default:
  280. pr_err("%s: Valid pipe config not set, offload op = %d\n",
  281. __func__, offload_op);
  282. return -EINVAL;
  283. }
  284. pce_dev->reg.crypto_cfg_be = config_be;
  285. pce_dev->reg.crypto_cfg_le = (config_be |
  286. CRYPTO_LITTLE_ENDIAN_MASK);
  287. return 0;
  288. }
  289. static void qce_enable_clock_gating(struct qce_device *pce_dev)
  290. {
  291. /* This feature might cause some HW issues, noop till resolved. */
  292. return;
  293. }
  294. /*
  295. * IV counter mask is be set based on the values sent through the offload ioctl
  296. * calls. Currently for offload operations, it is 64 bytes of mask for AES CTR,
  297. * and 128 bytes of mask for AES CBC.
  298. */
  299. static void qce_set_iv_ctr_mask(struct qce_device *pce_dev,
  300. struct qce_req *creq)
  301. {
  302. if (creq->iv_ctr_size == AES_CTR_IV_CTR_SIZE) {
  303. pce_dev->reg.encr_cntr_mask_0 = 0x0;
  304. pce_dev->reg.encr_cntr_mask_1 = 0x0;
  305. pce_dev->reg.encr_cntr_mask_2 = 0xFFFFFFFF;
  306. pce_dev->reg.encr_cntr_mask_3 = 0xFFFFFFFF;
  307. } else {
  308. pce_dev->reg.encr_cntr_mask_0 = 0xFFFFFFFF;
  309. pce_dev->reg.encr_cntr_mask_1 = 0xFFFFFFFF;
  310. pce_dev->reg.encr_cntr_mask_2 = 0xFFFFFFFF;
  311. pce_dev->reg.encr_cntr_mask_3 = 0xFFFFFFFF;
  312. }
  313. return;
  314. }
  315. static void _byte_stream_to_net_words(uint32_t *iv, unsigned char *b,
  316. unsigned int len)
  317. {
  318. unsigned int n;
  319. n = len / sizeof(uint32_t);
  320. for (; n > 0; n--) {
  321. *iv = ((*b << 24) & 0xff000000) |
  322. (((*(b+1)) << 16) & 0xff0000) |
  323. (((*(b+2)) << 8) & 0xff00) |
  324. (*(b+3) & 0xff);
  325. b += sizeof(uint32_t);
  326. iv++;
  327. }
  328. n = len % sizeof(uint32_t);
  329. if (n == 3) {
  330. *iv = ((*b << 24) & 0xff000000) |
  331. (((*(b+1)) << 16) & 0xff0000) |
  332. (((*(b+2)) << 8) & 0xff00);
  333. } else if (n == 2) {
  334. *iv = ((*b << 24) & 0xff000000) |
  335. (((*(b+1)) << 16) & 0xff0000);
  336. } else if (n == 1) {
  337. *iv = ((*b << 24) & 0xff000000);
  338. }
  339. }
  340. static void _byte_stream_swap_to_net_words(uint32_t *iv, unsigned char *b,
  341. unsigned int len)
  342. {
  343. unsigned int i, j;
  344. unsigned char swap_iv[AES_IV_LENGTH];
  345. memset(swap_iv, 0, AES_IV_LENGTH);
  346. for (i = (AES_IV_LENGTH-len), j = len-1; i < AES_IV_LENGTH; i++, j--)
  347. swap_iv[i] = b[j];
  348. _byte_stream_to_net_words(iv, swap_iv, AES_IV_LENGTH);
  349. }
  350. static int count_sg(struct scatterlist *sg, int nbytes)
  351. {
  352. int i;
  353. for (i = 0; nbytes > 0; i++, sg = sg_next(sg))
  354. nbytes -= sg->length;
  355. return i;
  356. }
  357. static int qce_dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
  358. enum dma_data_direction direction)
  359. {
  360. int i;
  361. for (i = 0; i < nents; ++i) {
  362. dma_map_sg(dev, sg, 1, direction);
  363. sg = sg_next(sg);
  364. }
  365. return nents;
  366. }
  367. static int qce_dma_unmap_sg(struct device *dev, struct scatterlist *sg,
  368. int nents, enum dma_data_direction direction)
  369. {
  370. int i;
  371. for (i = 0; i < nents; ++i) {
  372. dma_unmap_sg(dev, sg, 1, direction);
  373. sg = sg_next(sg);
  374. }
  375. return nents;
  376. }
  377. static int _probe_ce_engine(struct qce_device *pce_dev)
  378. {
  379. unsigned int rev;
  380. unsigned int maj_rev, min_rev, step_rev;
  381. rev = readl_relaxed(pce_dev->iobase + CRYPTO_VERSION_REG);
  382. /*
  383. * Ensure previous instructions (setting the GO register)
  384. * was completed before checking the version.
  385. */
  386. mb();
  387. maj_rev = (rev & CRYPTO_CORE_MAJOR_REV_MASK) >> CRYPTO_CORE_MAJOR_REV;
  388. min_rev = (rev & CRYPTO_CORE_MINOR_REV_MASK) >> CRYPTO_CORE_MINOR_REV;
  389. step_rev = (rev & CRYPTO_CORE_STEP_REV_MASK) >> CRYPTO_CORE_STEP_REV;
  390. if (maj_rev != CRYPTO_CORE_MAJOR_VER_NUM) {
  391. pr_err("Unsupported QTI crypto device at 0x%x, rev %d.%d.%d\n",
  392. pce_dev->phy_iobase, maj_rev, min_rev, step_rev);
  393. return -EIO;
  394. }
  395. /*
  396. * The majority of crypto HW bugs have been fixed in 5.3.0 and
  397. * above. That allows a single sps transfer of consumer
  398. * pipe, and a single sps transfer of producer pipe
  399. * for a crypto request. no_get_around flag indicates this.
  400. *
  401. * In 5.3.1, the CCM MAC_FAILED in result dump issue is
  402. * fixed. no_ccm_mac_status_get_around flag indicates this.
  403. */
  404. pce_dev->no_get_around = (min_rev >=
  405. CRYPTO_CORE_MINOR_VER_NUM) ? true : false;
  406. if (min_rev > CRYPTO_CORE_MINOR_VER_NUM)
  407. pce_dev->no_ccm_mac_status_get_around = true;
  408. else if ((min_rev == CRYPTO_CORE_MINOR_VER_NUM) &&
  409. (step_rev >= CRYPTO_CORE_STEP_VER_NUM))
  410. pce_dev->no_ccm_mac_status_get_around = true;
  411. else
  412. pce_dev->no_ccm_mac_status_get_around = false;
  413. pce_dev->ce_bam_info.minor_version = min_rev;
  414. pce_dev->ce_bam_info.major_version = maj_rev;
  415. pce_dev->engines_avail = readl_relaxed(pce_dev->iobase +
  416. CRYPTO_ENGINES_AVAIL);
  417. dev_info(pce_dev->pdev, "QTI Crypto %d.%d.%d device found @0x%x\n",
  418. maj_rev, min_rev, step_rev, pce_dev->phy_iobase);
  419. pce_dev->ce_bam_info.ce_burst_size = MAX_CE_BAM_BURST_SIZE;
  420. dev_dbg(pce_dev->pdev, "CE device = %#x IO base, CE = %pK Consumer (IN) PIPE %d,\nProducer (OUT) PIPE %d IO base BAM = %pK\nBAM IRQ %d Engines Availability = %#x\n",
  421. pce_dev->ce_bam_info.ce_device, pce_dev->iobase,
  422. pce_dev->ce_bam_info.dest_pipe_index,
  423. pce_dev->ce_bam_info.src_pipe_index,
  424. pce_dev->ce_bam_info.bam_iobase,
  425. pce_dev->ce_bam_info.bam_irq, pce_dev->engines_avail);
  426. return 0;
  427. };
  428. static struct qce_cmdlist_info *_ce_get_hash_cmdlistinfo(
  429. struct qce_device *pce_dev,
  430. int req_info, struct qce_sha_req *sreq)
  431. {
  432. struct ce_sps_data *pce_sps_data;
  433. struct qce_cmdlistptr_ops *cmdlistptr;
  434. pce_sps_data = &pce_dev->ce_request_info[req_info].ce_sps;
  435. cmdlistptr = &pce_sps_data->cmdlistptr;
  436. switch (sreq->alg) {
  437. case QCE_HASH_SHA1:
  438. return &cmdlistptr->auth_sha1;
  439. case QCE_HASH_SHA256:
  440. return &cmdlistptr->auth_sha256;
  441. case QCE_HASH_SHA1_HMAC:
  442. return &cmdlistptr->auth_sha1_hmac;
  443. case QCE_HASH_SHA256_HMAC:
  444. return &cmdlistptr->auth_sha256_hmac;
  445. case QCE_HASH_AES_CMAC:
  446. if (sreq->authklen == AES128_KEY_SIZE)
  447. return &cmdlistptr->auth_aes_128_cmac;
  448. return &cmdlistptr->auth_aes_256_cmac;
  449. default:
  450. return NULL;
  451. }
  452. return NULL;
  453. }
  454. static int _ce_setup_hash(struct qce_device *pce_dev,
  455. struct qce_sha_req *sreq,
  456. struct qce_cmdlist_info *cmdlistinfo)
  457. {
  458. uint32_t auth32[SHA256_DIGEST_SIZE / sizeof(uint32_t)];
  459. uint32_t diglen;
  460. int i;
  461. uint32_t mackey32[SHA_HMAC_KEY_SIZE/sizeof(uint32_t)] = {
  462. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  463. bool sha1 = false;
  464. struct sps_command_element *pce = NULL;
  465. bool use_hw_key = false;
  466. bool use_pipe_key = false;
  467. uint32_t authk_size_in_word = sreq->authklen/sizeof(uint32_t);
  468. uint32_t auth_cfg;
  469. if (qce_crypto_config(pce_dev, QCE_OFFLOAD_NONE))
  470. return -EINVAL;
  471. pce = cmdlistinfo->crypto_cfg;
  472. pce->data = pce_dev->reg.crypto_cfg_be;
  473. pce = cmdlistinfo->crypto_cfg_le;
  474. pce->data = pce_dev->reg.crypto_cfg_le;
  475. if ((sreq->alg == QCE_HASH_SHA1_HMAC) ||
  476. (sreq->alg == QCE_HASH_SHA256_HMAC) ||
  477. (sreq->alg == QCE_HASH_AES_CMAC)) {
  478. /* no more check for null key. use flag */
  479. if ((sreq->flags & QCRYPTO_CTX_USE_HW_KEY)
  480. == QCRYPTO_CTX_USE_HW_KEY)
  481. use_hw_key = true;
  482. else if ((sreq->flags & QCRYPTO_CTX_USE_PIPE_KEY) ==
  483. QCRYPTO_CTX_USE_PIPE_KEY)
  484. use_pipe_key = true;
  485. pce = cmdlistinfo->go_proc;
  486. if (use_hw_key) {
  487. pce->addr = (uint32_t)(CRYPTO_GOPROC_QC_KEY_REG +
  488. pce_dev->phy_iobase);
  489. } else {
  490. pce->addr = (uint32_t)(CRYPTO_GOPROC_REG +
  491. pce_dev->phy_iobase);
  492. pce = cmdlistinfo->auth_key;
  493. if (!use_pipe_key) {
  494. _byte_stream_to_net_words(mackey32,
  495. sreq->authkey,
  496. sreq->authklen);
  497. for (i = 0; i < authk_size_in_word; i++, pce++)
  498. pce->data = mackey32[i];
  499. }
  500. }
  501. }
  502. if (sreq->alg == QCE_HASH_AES_CMAC)
  503. goto go_proc;
  504. /* if not the last, the size has to be on the block boundary */
  505. if (!sreq->last_blk && (sreq->size % SHA256_BLOCK_SIZE))
  506. return -EIO;
  507. switch (sreq->alg) {
  508. case QCE_HASH_SHA1:
  509. case QCE_HASH_SHA1_HMAC:
  510. diglen = SHA1_DIGEST_SIZE;
  511. sha1 = true;
  512. break;
  513. case QCE_HASH_SHA256:
  514. case QCE_HASH_SHA256_HMAC:
  515. diglen = SHA256_DIGEST_SIZE;
  516. break;
  517. default:
  518. return -EINVAL;
  519. }
  520. /* write 20/32 bytes, 5/8 words into auth_iv for SHA1/SHA256 */
  521. if (sreq->first_blk) {
  522. if (sha1) {
  523. for (i = 0; i < 5; i++)
  524. auth32[i] = _std_init_vector_sha1[i];
  525. } else {
  526. for (i = 0; i < 8; i++)
  527. auth32[i] = _std_init_vector_sha256[i];
  528. }
  529. } else {
  530. _byte_stream_to_net_words(auth32, sreq->digest, diglen);
  531. }
  532. pce = cmdlistinfo->auth_iv;
  533. for (i = 0; i < 5; i++, pce++)
  534. pce->data = auth32[i];
  535. if ((sreq->alg == QCE_HASH_SHA256) ||
  536. (sreq->alg == QCE_HASH_SHA256_HMAC)) {
  537. for (i = 5; i < 8; i++, pce++)
  538. pce->data = auth32[i];
  539. }
  540. /* write auth_bytecnt 0/1, start with 0 */
  541. pce = cmdlistinfo->auth_bytecount;
  542. for (i = 0; i < 2; i++, pce++)
  543. pce->data = sreq->auth_data[i];
  544. /* Set/reset last bit in CFG register */
  545. pce = cmdlistinfo->auth_seg_cfg;
  546. auth_cfg = pce->data & ~(1 << CRYPTO_LAST |
  547. 1 << CRYPTO_FIRST |
  548. 1 << CRYPTO_USE_PIPE_KEY_AUTH |
  549. 1 << CRYPTO_USE_HW_KEY_AUTH);
  550. if (sreq->last_blk)
  551. auth_cfg |= 1 << CRYPTO_LAST;
  552. if (sreq->first_blk)
  553. auth_cfg |= 1 << CRYPTO_FIRST;
  554. if (use_hw_key)
  555. auth_cfg |= 1 << CRYPTO_USE_HW_KEY_AUTH;
  556. if (use_pipe_key)
  557. auth_cfg |= 1 << CRYPTO_USE_PIPE_KEY_AUTH;
  558. pce->data = auth_cfg;
  559. go_proc:
  560. /* write auth seg size */
  561. pce = cmdlistinfo->auth_seg_size;
  562. pce->data = sreq->size;
  563. pce = cmdlistinfo->encr_seg_cfg;
  564. pce->data = 0;
  565. /* write auth seg size start*/
  566. pce = cmdlistinfo->auth_seg_start;
  567. pce->data = 0;
  568. /* write seg size */
  569. pce = cmdlistinfo->seg_size;
  570. /* always ensure there is input data. ZLT does not work for bam-ndp */
  571. if (sreq->size)
  572. pce->data = sreq->size;
  573. else
  574. pce->data = pce_dev->ce_bam_info.ce_burst_size;
  575. return 0;
  576. }
  577. static struct qce_cmdlist_info *_ce_get_aead_cmdlistinfo(
  578. struct qce_device *pce_dev,
  579. int req_info, struct qce_req *creq)
  580. {
  581. struct ce_sps_data *pce_sps_data;
  582. struct qce_cmdlistptr_ops *cmdlistptr;
  583. pce_sps_data = &pce_dev->ce_request_info[req_info].ce_sps;
  584. cmdlistptr = &pce_sps_data->cmdlistptr;
  585. switch (creq->alg) {
  586. case CIPHER_ALG_DES:
  587. switch (creq->mode) {
  588. case QCE_MODE_CBC:
  589. if (creq->auth_alg == QCE_HASH_SHA1_HMAC)
  590. return &cmdlistptr->aead_hmac_sha1_cbc_des;
  591. else if (creq->auth_alg == QCE_HASH_SHA256_HMAC)
  592. return &cmdlistptr->aead_hmac_sha256_cbc_des;
  593. else
  594. return NULL;
  595. break;
  596. default:
  597. return NULL;
  598. }
  599. break;
  600. case CIPHER_ALG_3DES:
  601. switch (creq->mode) {
  602. case QCE_MODE_CBC:
  603. if (creq->auth_alg == QCE_HASH_SHA1_HMAC)
  604. return &cmdlistptr->aead_hmac_sha1_cbc_3des;
  605. else if (creq->auth_alg == QCE_HASH_SHA256_HMAC)
  606. return &cmdlistptr->aead_hmac_sha256_cbc_3des;
  607. else
  608. return NULL;
  609. break;
  610. default:
  611. return NULL;
  612. }
  613. break;
  614. case CIPHER_ALG_AES:
  615. switch (creq->mode) {
  616. case QCE_MODE_CBC:
  617. if (creq->encklen == AES128_KEY_SIZE) {
  618. if (creq->auth_alg == QCE_HASH_SHA1_HMAC)
  619. return
  620. &cmdlistptr->aead_hmac_sha1_cbc_aes_128;
  621. else if (creq->auth_alg == QCE_HASH_SHA256_HMAC)
  622. return
  623. &cmdlistptr->aead_hmac_sha256_cbc_aes_128;
  624. else
  625. return NULL;
  626. } else if (creq->encklen == AES256_KEY_SIZE) {
  627. if (creq->auth_alg == QCE_HASH_SHA1_HMAC)
  628. return &cmdlistptr->aead_hmac_sha1_cbc_aes_256;
  629. else if (creq->auth_alg == QCE_HASH_SHA256_HMAC)
  630. return
  631. &cmdlistptr->aead_hmac_sha256_cbc_aes_256;
  632. else
  633. return NULL;
  634. } else
  635. return NULL;
  636. break;
  637. default:
  638. return NULL;
  639. }
  640. break;
  641. default:
  642. return NULL;
  643. }
  644. return NULL;
  645. }
  646. static int _ce_setup_aead(struct qce_device *pce_dev, struct qce_req *q_req,
  647. uint32_t totallen_in, uint32_t coffset,
  648. struct qce_cmdlist_info *cmdlistinfo)
  649. {
  650. int32_t authk_size_in_word = SHA_HMAC_KEY_SIZE/sizeof(uint32_t);
  651. int i;
  652. uint32_t mackey32[SHA_HMAC_KEY_SIZE/sizeof(uint32_t)] = {0};
  653. struct sps_command_element *pce;
  654. uint32_t a_cfg;
  655. uint32_t enckey32[(MAX_CIPHER_KEY_SIZE*2)/sizeof(uint32_t)] = {0};
  656. uint32_t enciv32[MAX_IV_LENGTH/sizeof(uint32_t)] = {0};
  657. uint32_t enck_size_in_word = 0;
  658. uint32_t enciv_in_word;
  659. uint32_t key_size;
  660. uint32_t encr_cfg = 0;
  661. uint32_t ivsize = q_req->ivsize;
  662. key_size = q_req->encklen;
  663. enck_size_in_word = key_size/sizeof(uint32_t);
  664. if (qce_crypto_config(pce_dev, q_req->offload_op))
  665. return -EINVAL;
  666. pce = cmdlistinfo->crypto_cfg;
  667. pce->data = pce_dev->reg.crypto_cfg_be;
  668. pce = cmdlistinfo->crypto_cfg_le;
  669. pce->data = pce_dev->reg.crypto_cfg_le;
  670. switch (q_req->alg) {
  671. case CIPHER_ALG_DES:
  672. enciv_in_word = 2;
  673. break;
  674. case CIPHER_ALG_3DES:
  675. enciv_in_word = 2;
  676. break;
  677. case CIPHER_ALG_AES:
  678. if ((key_size != AES128_KEY_SIZE) &&
  679. (key_size != AES256_KEY_SIZE))
  680. return -EINVAL;
  681. enciv_in_word = 4;
  682. break;
  683. default:
  684. return -EINVAL;
  685. }
  686. /* only support cbc mode */
  687. if (q_req->mode != QCE_MODE_CBC)
  688. return -EINVAL;
  689. _byte_stream_to_net_words(enciv32, q_req->iv, ivsize);
  690. pce = cmdlistinfo->encr_cntr_iv;
  691. for (i = 0; i < enciv_in_word; i++, pce++)
  692. pce->data = enciv32[i];
  693. /*
  694. * write encr key
  695. * do not use hw key or pipe key
  696. */
  697. _byte_stream_to_net_words(enckey32, q_req->enckey, key_size);
  698. pce = cmdlistinfo->encr_key;
  699. for (i = 0; i < enck_size_in_word; i++, pce++)
  700. pce->data = enckey32[i];
  701. /* write encr seg cfg */
  702. pce = cmdlistinfo->encr_seg_cfg;
  703. encr_cfg = pce->data;
  704. if (q_req->dir == QCE_ENCRYPT)
  705. encr_cfg |= (1 << CRYPTO_ENCODE);
  706. else
  707. encr_cfg &= ~(1 << CRYPTO_ENCODE);
  708. pce->data = encr_cfg;
  709. /* we only support sha1-hmac and sha256-hmac at this point */
  710. _byte_stream_to_net_words(mackey32, q_req->authkey,
  711. q_req->authklen);
  712. pce = cmdlistinfo->auth_key;
  713. for (i = 0; i < authk_size_in_word; i++, pce++)
  714. pce->data = mackey32[i];
  715. pce = cmdlistinfo->auth_iv;
  716. if (q_req->auth_alg == QCE_HASH_SHA1_HMAC)
  717. for (i = 0; i < 5; i++, pce++)
  718. pce->data = _std_init_vector_sha1[i];
  719. else
  720. for (i = 0; i < 8; i++, pce++)
  721. pce->data = _std_init_vector_sha256[i];
  722. /* write auth_bytecnt 0/1, start with 0 */
  723. pce = cmdlistinfo->auth_bytecount;
  724. for (i = 0; i < 2; i++, pce++)
  725. pce->data = 0;
  726. pce = cmdlistinfo->auth_seg_cfg;
  727. a_cfg = pce->data;
  728. a_cfg &= ~(CRYPTO_AUTH_POS_MASK);
  729. if (q_req->dir == QCE_ENCRYPT)
  730. a_cfg |= (CRYPTO_AUTH_POS_AFTER << CRYPTO_AUTH_POS);
  731. else
  732. a_cfg |= (CRYPTO_AUTH_POS_BEFORE << CRYPTO_AUTH_POS);
  733. pce->data = a_cfg;
  734. /* write auth seg size */
  735. pce = cmdlistinfo->auth_seg_size;
  736. pce->data = totallen_in;
  737. /* write auth seg size start*/
  738. pce = cmdlistinfo->auth_seg_start;
  739. pce->data = 0;
  740. /* write seg size */
  741. pce = cmdlistinfo->seg_size;
  742. pce->data = totallen_in;
  743. /* write encr seg size */
  744. pce = cmdlistinfo->encr_seg_size;
  745. pce->data = q_req->cryptlen;
  746. /* write encr seg start */
  747. pce = cmdlistinfo->encr_seg_start;
  748. pce->data = (coffset & 0xffff);
  749. return 0;
  750. }
  751. static struct qce_cmdlist_info *_ce_get_cipher_cmdlistinfo(
  752. struct qce_device *pce_dev,
  753. int req_info, struct qce_req *creq)
  754. {
  755. struct ce_request_info *preq_info;
  756. struct ce_sps_data *pce_sps_data;
  757. struct qce_cmdlistptr_ops *cmdlistptr;
  758. preq_info = &pce_dev->ce_request_info[req_info];
  759. pce_sps_data = &preq_info->ce_sps;
  760. cmdlistptr = &pce_sps_data->cmdlistptr;
  761. if (creq->alg != CIPHER_ALG_AES) {
  762. switch (creq->alg) {
  763. case CIPHER_ALG_DES:
  764. if (creq->mode == QCE_MODE_ECB)
  765. return &cmdlistptr->cipher_des_ecb;
  766. return &cmdlistptr->cipher_des_cbc;
  767. case CIPHER_ALG_3DES:
  768. if (creq->mode == QCE_MODE_ECB)
  769. return &cmdlistptr->cipher_3des_ecb;
  770. return &cmdlistptr->cipher_3des_cbc;
  771. default:
  772. return NULL;
  773. }
  774. } else {
  775. switch (creq->mode) {
  776. case QCE_MODE_ECB:
  777. if (creq->encklen == AES128_KEY_SIZE)
  778. return &cmdlistptr->cipher_aes_128_ecb;
  779. return &cmdlistptr->cipher_aes_256_ecb;
  780. case QCE_MODE_CBC:
  781. case QCE_MODE_CTR:
  782. if (creq->encklen == AES128_KEY_SIZE)
  783. return &cmdlistptr->cipher_aes_128_cbc_ctr;
  784. return &cmdlistptr->cipher_aes_256_cbc_ctr;
  785. case QCE_MODE_XTS:
  786. if (creq->encklen/2 == AES128_KEY_SIZE)
  787. return &cmdlistptr->cipher_aes_128_xts;
  788. return &cmdlistptr->cipher_aes_256_xts;
  789. case QCE_MODE_CCM:
  790. if (creq->encklen == AES128_KEY_SIZE)
  791. return &cmdlistptr->aead_aes_128_ccm;
  792. return &cmdlistptr->aead_aes_256_ccm;
  793. default:
  794. return NULL;
  795. }
  796. }
  797. return NULL;
  798. }
  799. static int _ce_setup_cipher(struct qce_device *pce_dev, struct qce_req *creq,
  800. uint32_t totallen_in, uint32_t coffset,
  801. struct qce_cmdlist_info *cmdlistinfo)
  802. {
  803. uint32_t enckey32[(MAX_CIPHER_KEY_SIZE * 2)/sizeof(uint32_t)] = {
  804. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  805. uint32_t enciv32[MAX_IV_LENGTH / sizeof(uint32_t)] = {
  806. 0, 0, 0, 0};
  807. uint32_t enck_size_in_word = 0;
  808. uint32_t key_size;
  809. bool use_hw_key = false;
  810. bool use_pipe_key = false;
  811. uint32_t encr_cfg = 0;
  812. uint32_t ivsize = creq->ivsize;
  813. int i;
  814. struct sps_command_element *pce = NULL;
  815. bool is_des_cipher = false;
  816. if (creq->mode == QCE_MODE_XTS)
  817. key_size = creq->encklen/2;
  818. else
  819. key_size = creq->encklen;
  820. if (qce_crypto_config(pce_dev, creq->offload_op))
  821. return -EINVAL;
  822. pce = cmdlistinfo->crypto_cfg;
  823. pce->data = pce_dev->reg.crypto_cfg_be;
  824. pce = cmdlistinfo->crypto_cfg_le;
  825. pce->data = pce_dev->reg.crypto_cfg_le;
  826. pce = cmdlistinfo->go_proc;
  827. if ((creq->flags & QCRYPTO_CTX_USE_HW_KEY) == QCRYPTO_CTX_USE_HW_KEY) {
  828. use_hw_key = true;
  829. } else {
  830. if ((creq->flags & QCRYPTO_CTX_USE_PIPE_KEY) ==
  831. QCRYPTO_CTX_USE_PIPE_KEY)
  832. use_pipe_key = true;
  833. }
  834. if (use_hw_key)
  835. pce->addr = (uint32_t)(CRYPTO_GOPROC_QC_KEY_REG +
  836. pce_dev->phy_iobase);
  837. else
  838. pce->addr = (uint32_t)(CRYPTO_GOPROC_REG +
  839. pce_dev->phy_iobase);
  840. if (!use_pipe_key && !use_hw_key) {
  841. _byte_stream_to_net_words(enckey32, creq->enckey, key_size);
  842. enck_size_in_word = key_size/sizeof(uint32_t);
  843. }
  844. if ((creq->op == QCE_REQ_AEAD) && (creq->mode == QCE_MODE_CCM)) {
  845. uint32_t authklen32 = creq->encklen/sizeof(uint32_t);
  846. uint32_t noncelen32 = MAX_NONCE/sizeof(uint32_t);
  847. uint32_t nonce32[MAX_NONCE/sizeof(uint32_t)] = {0, 0, 0, 0};
  848. uint32_t auth_cfg = 0;
  849. /* write nonce */
  850. _byte_stream_to_net_words(nonce32, creq->nonce, MAX_NONCE);
  851. pce = cmdlistinfo->auth_nonce_info;
  852. for (i = 0; i < noncelen32; i++, pce++)
  853. pce->data = nonce32[i];
  854. if (creq->authklen == AES128_KEY_SIZE)
  855. auth_cfg = pce_dev->reg.auth_cfg_aes_ccm_128;
  856. else {
  857. if (creq->authklen == AES256_KEY_SIZE)
  858. auth_cfg = pce_dev->reg.auth_cfg_aes_ccm_256;
  859. }
  860. if (creq->dir == QCE_ENCRYPT)
  861. auth_cfg |= (CRYPTO_AUTH_POS_BEFORE << CRYPTO_AUTH_POS);
  862. else
  863. auth_cfg |= (CRYPTO_AUTH_POS_AFTER << CRYPTO_AUTH_POS);
  864. auth_cfg |= ((creq->authsize - 1) << CRYPTO_AUTH_SIZE);
  865. if (use_hw_key) {
  866. auth_cfg |= (1 << CRYPTO_USE_HW_KEY_AUTH);
  867. } else {
  868. auth_cfg &= ~(1 << CRYPTO_USE_HW_KEY_AUTH);
  869. /* write auth key */
  870. pce = cmdlistinfo->auth_key;
  871. for (i = 0; i < authklen32; i++, pce++)
  872. pce->data = enckey32[i];
  873. }
  874. pce = cmdlistinfo->auth_seg_cfg;
  875. pce->data = auth_cfg;
  876. pce = cmdlistinfo->auth_seg_size;
  877. if (creq->dir == QCE_ENCRYPT)
  878. pce->data = totallen_in;
  879. else
  880. pce->data = totallen_in - creq->authsize;
  881. pce = cmdlistinfo->auth_seg_start;
  882. pce->data = 0;
  883. } else {
  884. if (creq->op != QCE_REQ_AEAD) {
  885. pce = cmdlistinfo->auth_seg_cfg;
  886. pce->data = 0;
  887. }
  888. }
  889. switch (creq->mode) {
  890. case QCE_MODE_ECB:
  891. if (key_size == AES128_KEY_SIZE)
  892. encr_cfg = pce_dev->reg.encr_cfg_aes_ecb_128;
  893. else
  894. encr_cfg = pce_dev->reg.encr_cfg_aes_ecb_256;
  895. break;
  896. case QCE_MODE_CBC:
  897. if (key_size == AES128_KEY_SIZE)
  898. encr_cfg = pce_dev->reg.encr_cfg_aes_cbc_128;
  899. else
  900. encr_cfg = pce_dev->reg.encr_cfg_aes_cbc_256;
  901. break;
  902. case QCE_MODE_XTS:
  903. if (key_size == AES128_KEY_SIZE)
  904. encr_cfg = pce_dev->reg.encr_cfg_aes_xts_128;
  905. else
  906. encr_cfg = pce_dev->reg.encr_cfg_aes_xts_256;
  907. break;
  908. case QCE_MODE_CCM:
  909. if (key_size == AES128_KEY_SIZE)
  910. encr_cfg = pce_dev->reg.encr_cfg_aes_ccm_128;
  911. else
  912. encr_cfg = pce_dev->reg.encr_cfg_aes_ccm_256;
  913. encr_cfg |= (CRYPTO_ENCR_MODE_CCM << CRYPTO_ENCR_MODE) |
  914. (CRYPTO_LAST_CCM_XFR << CRYPTO_LAST_CCM);
  915. break;
  916. case QCE_MODE_CTR:
  917. default:
  918. if (key_size == AES128_KEY_SIZE)
  919. encr_cfg = pce_dev->reg.encr_cfg_aes_ctr_128;
  920. else
  921. encr_cfg = pce_dev->reg.encr_cfg_aes_ctr_256;
  922. break;
  923. }
  924. switch (creq->alg) {
  925. case CIPHER_ALG_DES:
  926. if (creq->mode != QCE_MODE_ECB) {
  927. if (ivsize > MAX_IV_LENGTH) {
  928. pr_err("%s: error: Invalid length parameter\n",
  929. __func__);
  930. return -EINVAL;
  931. }
  932. _byte_stream_to_net_words(enciv32, creq->iv, ivsize);
  933. pce = cmdlistinfo->encr_cntr_iv;
  934. pce->data = enciv32[0];
  935. pce++;
  936. pce->data = enciv32[1];
  937. }
  938. if (!use_hw_key) {
  939. pce = cmdlistinfo->encr_key;
  940. pce->data = enckey32[0];
  941. pce++;
  942. pce->data = enckey32[1];
  943. }
  944. is_des_cipher = true;
  945. break;
  946. case CIPHER_ALG_3DES:
  947. if (creq->mode != QCE_MODE_ECB) {
  948. if (ivsize > MAX_IV_LENGTH) {
  949. pr_err("%s: error: Invalid length parameter\n",
  950. __func__);
  951. return -EINVAL;
  952. }
  953. _byte_stream_to_net_words(enciv32, creq->iv, ivsize);
  954. pce = cmdlistinfo->encr_cntr_iv;
  955. pce->data = enciv32[0];
  956. pce++;
  957. pce->data = enciv32[1];
  958. }
  959. if (!use_hw_key) {
  960. /* write encr key */
  961. pce = cmdlistinfo->encr_key;
  962. for (i = 0; i < 6; i++, pce++)
  963. pce->data = enckey32[i];
  964. }
  965. is_des_cipher = true;
  966. break;
  967. case CIPHER_ALG_AES:
  968. default:
  969. if (creq->mode == QCE_MODE_XTS) {
  970. uint32_t xtskey32[MAX_CIPHER_KEY_SIZE/sizeof(uint32_t)]
  971. = {0, 0, 0, 0, 0, 0, 0, 0};
  972. uint32_t xtsklen =
  973. creq->encklen/(2 * sizeof(uint32_t));
  974. if (!use_hw_key && !use_pipe_key) {
  975. _byte_stream_to_net_words(xtskey32,
  976. (creq->enckey + creq->encklen/2),
  977. creq->encklen/2);
  978. /* write xts encr key */
  979. pce = cmdlistinfo->encr_xts_key;
  980. for (i = 0; i < xtsklen; i++, pce++)
  981. pce->data = xtskey32[i];
  982. }
  983. /* write xts du size */
  984. pce = cmdlistinfo->encr_xts_du_size;
  985. switch (creq->flags & QCRYPTO_CTX_XTS_MASK) {
  986. case QCRYPTO_CTX_XTS_DU_SIZE_512B:
  987. pce->data = min((unsigned int)QCE_SECTOR_SIZE,
  988. creq->cryptlen);
  989. break;
  990. case QCRYPTO_CTX_XTS_DU_SIZE_1KB:
  991. pce->data =
  992. min((unsigned int)QCE_SECTOR_SIZE * 2,
  993. creq->cryptlen);
  994. break;
  995. default:
  996. pce->data = creq->cryptlen;
  997. break;
  998. }
  999. }
  1000. if (creq->mode != QCE_MODE_ECB) {
  1001. if (ivsize > MAX_IV_LENGTH) {
  1002. pr_err("%s: error: Invalid length parameter\n",
  1003. __func__);
  1004. return -EINVAL;
  1005. }
  1006. if (creq->mode == QCE_MODE_XTS)
  1007. _byte_stream_swap_to_net_words(enciv32,
  1008. creq->iv, ivsize);
  1009. else
  1010. _byte_stream_to_net_words(enciv32, creq->iv,
  1011. ivsize);
  1012. /* write encr cntr iv */
  1013. pce = cmdlistinfo->encr_cntr_iv;
  1014. for (i = 0; i < 4; i++, pce++)
  1015. pce->data = enciv32[i];
  1016. if (creq->mode == QCE_MODE_CCM) {
  1017. /* write cntr iv for ccm */
  1018. pce = cmdlistinfo->encr_ccm_cntr_iv;
  1019. for (i = 0; i < 4; i++, pce++)
  1020. pce->data = enciv32[i];
  1021. /* update cntr_iv[3] by one */
  1022. pce = cmdlistinfo->encr_cntr_iv;
  1023. pce += 3;
  1024. pce->data += 1;
  1025. }
  1026. }
  1027. if (creq->op == QCE_REQ_ABLK_CIPHER_NO_KEY) {
  1028. encr_cfg |= (CRYPTO_ENCR_KEY_SZ_AES128 <<
  1029. CRYPTO_ENCR_KEY_SZ);
  1030. } else {
  1031. if (!use_hw_key) {
  1032. /* write encr key */
  1033. pce = cmdlistinfo->encr_key;
  1034. for (i = 0; i < enck_size_in_word; i++, pce++)
  1035. pce->data = enckey32[i];
  1036. }
  1037. } /* else of if (creq->op == QCE_REQ_ABLK_CIPHER_NO_KEY) */
  1038. break;
  1039. } /* end of switch (creq->mode) */
  1040. if (use_pipe_key)
  1041. encr_cfg |= (CRYPTO_USE_PIPE_KEY_ENCR_ENABLED
  1042. << CRYPTO_USE_PIPE_KEY_ENCR);
  1043. /* write encr seg cfg */
  1044. pce = cmdlistinfo->encr_seg_cfg;
  1045. if ((creq->alg == CIPHER_ALG_DES) || (creq->alg == CIPHER_ALG_3DES)) {
  1046. if (creq->dir == QCE_ENCRYPT)
  1047. pce->data |= (1 << CRYPTO_ENCODE);
  1048. else
  1049. pce->data &= ~(1 << CRYPTO_ENCODE);
  1050. encr_cfg = pce->data;
  1051. } else {
  1052. encr_cfg |=
  1053. ((creq->dir == QCE_ENCRYPT) ? 1 : 0) << CRYPTO_ENCODE;
  1054. }
  1055. if (use_hw_key)
  1056. encr_cfg |= (CRYPTO_USE_HW_KEY << CRYPTO_USE_HW_KEY_ENCR);
  1057. else
  1058. encr_cfg &= ~(CRYPTO_USE_HW_KEY << CRYPTO_USE_HW_KEY_ENCR);
  1059. pce->data = encr_cfg;
  1060. /* write encr seg size */
  1061. pce = cmdlistinfo->encr_seg_size;
  1062. if (creq->is_copy_op) {
  1063. pce->data = 0;
  1064. } else {
  1065. if ((creq->mode == QCE_MODE_CCM) && (creq->dir == QCE_DECRYPT))
  1066. pce->data = (creq->cryptlen + creq->authsize);
  1067. else
  1068. pce->data = creq->cryptlen;
  1069. }
  1070. /* write encr seg start */
  1071. pce = cmdlistinfo->encr_seg_start;
  1072. pce->data = (coffset & 0xffff);
  1073. /* write seg size */
  1074. pce = cmdlistinfo->seg_size;
  1075. pce->data = totallen_in;
  1076. if (!is_des_cipher) {
  1077. /* pattern info */
  1078. pce = cmdlistinfo->pattern_info;
  1079. pce->data = creq->pattern_info;
  1080. /* block offset */
  1081. pce = cmdlistinfo->block_offset;
  1082. pce->data = (creq->block_offset << 4) |
  1083. (creq->block_offset ? 1: 0);
  1084. /* IV counter size */
  1085. qce_set_iv_ctr_mask(pce_dev, creq);
  1086. pce = cmdlistinfo->encr_mask_3;
  1087. pce->data = pce_dev->reg.encr_cntr_mask_3;
  1088. pce = cmdlistinfo->encr_mask_2;
  1089. pce->data = pce_dev->reg.encr_cntr_mask_2;
  1090. pce = cmdlistinfo->encr_mask_1;
  1091. pce->data = pce_dev->reg.encr_cntr_mask_1;
  1092. pce = cmdlistinfo->encr_mask_0;
  1093. pce->data = pce_dev->reg.encr_cntr_mask_0;
  1094. }
  1095. pce = cmdlistinfo->go_proc;
  1096. pce->data = 0;
  1097. if (is_offload_op(creq->offload_op))
  1098. pce->data = ((1 << CRYPTO_GO) | (1 << CRYPTO_CLR_CNTXT));
  1099. else
  1100. pce->data = ((1 << CRYPTO_GO) | (1 << CRYPTO_CLR_CNTXT) |
  1101. (1 << CRYPTO_RESULTS_DUMP));
  1102. return 0;
  1103. }
  1104. static int _ce_f9_setup(struct qce_device *pce_dev, struct qce_f9_req *req,
  1105. struct qce_cmdlist_info *cmdlistinfo)
  1106. {
  1107. uint32_t ikey32[OTA_KEY_SIZE/sizeof(uint32_t)];
  1108. uint32_t key_size_in_word = OTA_KEY_SIZE/sizeof(uint32_t);
  1109. uint32_t cfg;
  1110. struct sps_command_element *pce;
  1111. int i;
  1112. switch (req->algorithm) {
  1113. case QCE_OTA_ALGO_KASUMI:
  1114. cfg = pce_dev->reg.auth_cfg_kasumi;
  1115. break;
  1116. case QCE_OTA_ALGO_SNOW3G:
  1117. default:
  1118. cfg = pce_dev->reg.auth_cfg_snow3g;
  1119. break;
  1120. }
  1121. if (qce_crypto_config(pce_dev, QCE_OFFLOAD_NONE))
  1122. return -EINVAL;
  1123. pce = cmdlistinfo->crypto_cfg;
  1124. pce->data = pce_dev->reg.crypto_cfg_be;
  1125. pce = cmdlistinfo->crypto_cfg_le;
  1126. pce->data = pce_dev->reg.crypto_cfg_le;
  1127. /* write key in CRYPTO_AUTH_IV0-3_REG */
  1128. _byte_stream_to_net_words(ikey32, &req->ikey[0], OTA_KEY_SIZE);
  1129. pce = cmdlistinfo->auth_iv;
  1130. for (i = 0; i < key_size_in_word; i++, pce++)
  1131. pce->data = ikey32[i];
  1132. /* write last bits in CRYPTO_AUTH_IV4_REG */
  1133. pce->data = req->last_bits;
  1134. /* write fresh to CRYPTO_AUTH_BYTECNT0_REG */
  1135. pce = cmdlistinfo->auth_bytecount;
  1136. pce->data = req->fresh;
  1137. /* write count-i to CRYPTO_AUTH_BYTECNT1_REG */
  1138. pce++;
  1139. pce->data = req->count_i;
  1140. /* write auth seg cfg */
  1141. pce = cmdlistinfo->auth_seg_cfg;
  1142. if (req->direction == QCE_OTA_DIR_DOWNLINK)
  1143. cfg |= BIT(CRYPTO_F9_DIRECTION);
  1144. pce->data = cfg;
  1145. /* write auth seg size */
  1146. pce = cmdlistinfo->auth_seg_size;
  1147. pce->data = req->msize;
  1148. /* write auth seg start*/
  1149. pce = cmdlistinfo->auth_seg_start;
  1150. pce->data = 0;
  1151. /* write seg size */
  1152. pce = cmdlistinfo->seg_size;
  1153. pce->data = req->msize;
  1154. /* write go */
  1155. pce = cmdlistinfo->go_proc;
  1156. pce->addr = (uint32_t)(CRYPTO_GOPROC_REG + pce_dev->phy_iobase);
  1157. return 0;
  1158. }
  1159. static int _ce_f8_setup(struct qce_device *pce_dev, struct qce_f8_req *req,
  1160. bool key_stream_mode, uint16_t npkts, uint16_t cipher_offset,
  1161. uint16_t cipher_size,
  1162. struct qce_cmdlist_info *cmdlistinfo)
  1163. {
  1164. uint32_t ckey32[OTA_KEY_SIZE/sizeof(uint32_t)];
  1165. uint32_t key_size_in_word = OTA_KEY_SIZE/sizeof(uint32_t);
  1166. uint32_t cfg;
  1167. struct sps_command_element *pce;
  1168. int i;
  1169. switch (req->algorithm) {
  1170. case QCE_OTA_ALGO_KASUMI:
  1171. cfg = pce_dev->reg.encr_cfg_kasumi;
  1172. break;
  1173. case QCE_OTA_ALGO_SNOW3G:
  1174. default:
  1175. cfg = pce_dev->reg.encr_cfg_snow3g;
  1176. break;
  1177. }
  1178. if (qce_crypto_config(pce_dev, QCE_OFFLOAD_NONE))
  1179. return -EINVAL;
  1180. pce = cmdlistinfo->crypto_cfg;
  1181. pce->data = pce_dev->reg.crypto_cfg_be;
  1182. pce = cmdlistinfo->crypto_cfg_le;
  1183. pce->data = pce_dev->reg.crypto_cfg_le;
  1184. /* write key */
  1185. _byte_stream_to_net_words(ckey32, &req->ckey[0], OTA_KEY_SIZE);
  1186. pce = cmdlistinfo->encr_key;
  1187. for (i = 0; i < key_size_in_word; i++, pce++)
  1188. pce->data = ckey32[i];
  1189. /* write encr seg cfg */
  1190. pce = cmdlistinfo->encr_seg_cfg;
  1191. if (key_stream_mode)
  1192. cfg |= BIT(CRYPTO_F8_KEYSTREAM_ENABLE);
  1193. if (req->direction == QCE_OTA_DIR_DOWNLINK)
  1194. cfg |= BIT(CRYPTO_F8_DIRECTION);
  1195. pce->data = cfg;
  1196. /* write encr seg start */
  1197. pce = cmdlistinfo->encr_seg_start;
  1198. pce->data = (cipher_offset & 0xffff);
  1199. /* write encr seg size */
  1200. pce = cmdlistinfo->encr_seg_size;
  1201. pce->data = cipher_size;
  1202. /* write seg size */
  1203. pce = cmdlistinfo->seg_size;
  1204. pce->data = req->data_len;
  1205. /* write cntr0_iv0 for countC */
  1206. pce = cmdlistinfo->encr_cntr_iv;
  1207. pce->data = req->count_c;
  1208. /* write cntr1_iv1 for nPkts, and bearer */
  1209. pce++;
  1210. if (npkts == 1)
  1211. npkts = 0;
  1212. pce->data = req->bearer << CRYPTO_CNTR1_IV1_REG_F8_BEARER |
  1213. npkts << CRYPTO_CNTR1_IV1_REG_F8_PKT_CNT;
  1214. /* write go */
  1215. pce = cmdlistinfo->go_proc;
  1216. pce->addr = (uint32_t)(CRYPTO_GOPROC_REG + pce_dev->phy_iobase);
  1217. return 0;
  1218. }
  1219. static void _qce_dump_descr_fifos(struct qce_device *pce_dev, int req_info)
  1220. {
  1221. int i, j, ents;
  1222. struct ce_sps_data *pce_sps_data;
  1223. struct sps_iovec *iovec;
  1224. uint32_t cmd_flags = SPS_IOVEC_FLAG_CMD;
  1225. pce_sps_data = &pce_dev->ce_request_info[req_info].ce_sps;
  1226. iovec = pce_sps_data->in_transfer.iovec;
  1227. pr_info("==============================================\n");
  1228. pr_info("CONSUMER (TX/IN/DEST) PIPE DESCRIPTOR\n");
  1229. pr_info("==============================================\n");
  1230. for (i = 0; i < pce_sps_data->in_transfer.iovec_count; i++) {
  1231. pr_info(" [%d] addr=0x%x size=0x%x flags=0x%x\n", i,
  1232. iovec->addr, iovec->size, iovec->flags);
  1233. if (iovec->flags & cmd_flags) {
  1234. struct sps_command_element *pced;
  1235. pced = (struct sps_command_element *)
  1236. (GET_VIRT_ADDR(iovec->addr));
  1237. ents = iovec->size/(sizeof(struct sps_command_element));
  1238. for (j = 0; j < ents; j++) {
  1239. pr_info(" [%d] [0x%x] 0x%x\n", j,
  1240. pced->addr, pced->data);
  1241. pced++;
  1242. }
  1243. }
  1244. iovec++;
  1245. }
  1246. pr_info("==============================================\n");
  1247. pr_info("PRODUCER (RX/OUT/SRC) PIPE DESCRIPTOR\n");
  1248. pr_info("==============================================\n");
  1249. iovec = pce_sps_data->out_transfer.iovec;
  1250. for (i = 0; i < pce_sps_data->out_transfer.iovec_count; i++) {
  1251. pr_info(" [%d] addr=0x%x size=0x%x flags=0x%x\n", i,
  1252. iovec->addr, iovec->size, iovec->flags);
  1253. iovec++;
  1254. }
  1255. }
  1256. #ifdef QCE_DEBUG
  1257. static void _qce_dump_descr_fifos_dbg(struct qce_device *pce_dev, int req_info)
  1258. {
  1259. _qce_dump_descr_fifos(pce_dev, req_info);
  1260. }
  1261. #define QCE_WRITE_REG(val, addr) \
  1262. { \
  1263. pr_info(" [0x%pK] 0x%x\n", addr, (uint32_t)val); \
  1264. writel_relaxed(val, addr); \
  1265. }
  1266. #else
  1267. static void _qce_dump_descr_fifos_dbg(struct qce_device *pce_dev, int req_info)
  1268. {
  1269. }
  1270. #define QCE_WRITE_REG(val, addr) \
  1271. writel_relaxed(val, addr)
  1272. #endif
  1273. static int _ce_setup_hash_direct(struct qce_device *pce_dev,
  1274. struct qce_sha_req *sreq)
  1275. {
  1276. uint32_t auth32[SHA256_DIGEST_SIZE / sizeof(uint32_t)];
  1277. uint32_t diglen;
  1278. bool use_hw_key = false;
  1279. bool use_pipe_key = false;
  1280. int i;
  1281. uint32_t mackey32[SHA_HMAC_KEY_SIZE/sizeof(uint32_t)] = {
  1282. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  1283. uint32_t authk_size_in_word = sreq->authklen/sizeof(uint32_t);
  1284. bool sha1 = false;
  1285. uint32_t auth_cfg = 0;
  1286. /* clear status */
  1287. QCE_WRITE_REG(0, pce_dev->iobase + CRYPTO_STATUS_REG);
  1288. if (qce_crypto_config(pce_dev, QCE_OFFLOAD_NONE))
  1289. return -EINVAL;
  1290. QCE_WRITE_REG(pce_dev->reg.crypto_cfg_be, (pce_dev->iobase +
  1291. CRYPTO_CONFIG_REG));
  1292. /*
  1293. * Ensure previous instructions (setting the CONFIG register)
  1294. * was completed before issuing starting to set other config register
  1295. * This is to ensure the configurations are done in correct endian-ness
  1296. * as set in the CONFIG registers
  1297. */
  1298. mb();
  1299. if (sreq->alg == QCE_HASH_AES_CMAC) {
  1300. /* write seg_cfg */
  1301. QCE_WRITE_REG(0, pce_dev->iobase + CRYPTO_AUTH_SEG_CFG_REG);
  1302. /* write seg_cfg */
  1303. QCE_WRITE_REG(0, pce_dev->iobase + CRYPTO_ENCR_SEG_CFG_REG);
  1304. /* write seg_cfg */
  1305. QCE_WRITE_REG(0, pce_dev->iobase + CRYPTO_ENCR_SEG_SIZE_REG);
  1306. /* Clear auth_ivn, auth_keyn registers */
  1307. for (i = 0; i < 16; i++) {
  1308. QCE_WRITE_REG(0, (pce_dev->iobase +
  1309. (CRYPTO_AUTH_IV0_REG + i*sizeof(uint32_t))));
  1310. QCE_WRITE_REG(0, (pce_dev->iobase +
  1311. (CRYPTO_AUTH_KEY0_REG + i*sizeof(uint32_t))));
  1312. }
  1313. /* write auth_bytecnt 0/1/2/3, start with 0 */
  1314. for (i = 0; i < 4; i++)
  1315. QCE_WRITE_REG(0, pce_dev->iobase +
  1316. CRYPTO_AUTH_BYTECNT0_REG +
  1317. i * sizeof(uint32_t));
  1318. if (sreq->authklen == AES128_KEY_SIZE)
  1319. auth_cfg = pce_dev->reg.auth_cfg_cmac_128;
  1320. else
  1321. auth_cfg = pce_dev->reg.auth_cfg_cmac_256;
  1322. }
  1323. if ((sreq->alg == QCE_HASH_SHA1_HMAC) ||
  1324. (sreq->alg == QCE_HASH_SHA256_HMAC) ||
  1325. (sreq->alg == QCE_HASH_AES_CMAC)) {
  1326. _byte_stream_to_net_words(mackey32, sreq->authkey,
  1327. sreq->authklen);
  1328. /* no more check for null key. use flag to check*/
  1329. if ((sreq->flags & QCRYPTO_CTX_USE_HW_KEY) ==
  1330. QCRYPTO_CTX_USE_HW_KEY) {
  1331. use_hw_key = true;
  1332. } else if ((sreq->flags & QCRYPTO_CTX_USE_PIPE_KEY) ==
  1333. QCRYPTO_CTX_USE_PIPE_KEY) {
  1334. use_pipe_key = true;
  1335. } else {
  1336. /* setup key */
  1337. for (i = 0; i < authk_size_in_word; i++)
  1338. QCE_WRITE_REG(mackey32[i], (pce_dev->iobase +
  1339. (CRYPTO_AUTH_KEY0_REG +
  1340. i*sizeof(uint32_t))));
  1341. }
  1342. }
  1343. if (sreq->alg == QCE_HASH_AES_CMAC)
  1344. goto go_proc;
  1345. /* if not the last, the size has to be on the block boundary */
  1346. if (!sreq->last_blk && (sreq->size % SHA256_BLOCK_SIZE))
  1347. return -EIO;
  1348. switch (sreq->alg) {
  1349. case QCE_HASH_SHA1:
  1350. auth_cfg = pce_dev->reg.auth_cfg_sha1;
  1351. diglen = SHA1_DIGEST_SIZE;
  1352. sha1 = true;
  1353. break;
  1354. case QCE_HASH_SHA1_HMAC:
  1355. auth_cfg = pce_dev->reg.auth_cfg_hmac_sha1;
  1356. diglen = SHA1_DIGEST_SIZE;
  1357. sha1 = true;
  1358. break;
  1359. case QCE_HASH_SHA256:
  1360. auth_cfg = pce_dev->reg.auth_cfg_sha256;
  1361. diglen = SHA256_DIGEST_SIZE;
  1362. break;
  1363. case QCE_HASH_SHA256_HMAC:
  1364. auth_cfg = pce_dev->reg.auth_cfg_hmac_sha256;
  1365. diglen = SHA256_DIGEST_SIZE;
  1366. break;
  1367. default:
  1368. return -EINVAL;
  1369. }
  1370. /* write 20/32 bytes, 5/8 words into auth_iv for SHA1/SHA256 */
  1371. if (sreq->first_blk) {
  1372. if (sha1) {
  1373. for (i = 0; i < 5; i++)
  1374. auth32[i] = _std_init_vector_sha1[i];
  1375. } else {
  1376. for (i = 0; i < 8; i++)
  1377. auth32[i] = _std_init_vector_sha256[i];
  1378. }
  1379. } else {
  1380. _byte_stream_to_net_words(auth32, sreq->digest, diglen);
  1381. }
  1382. /* Set auth_ivn, auth_keyn registers */
  1383. for (i = 0; i < 5; i++)
  1384. QCE_WRITE_REG(auth32[i], (pce_dev->iobase +
  1385. (CRYPTO_AUTH_IV0_REG + i*sizeof(uint32_t))));
  1386. if ((sreq->alg == QCE_HASH_SHA256) ||
  1387. (sreq->alg == QCE_HASH_SHA256_HMAC)) {
  1388. for (i = 5; i < 8; i++)
  1389. QCE_WRITE_REG(auth32[i], (pce_dev->iobase +
  1390. (CRYPTO_AUTH_IV0_REG + i*sizeof(uint32_t))));
  1391. }
  1392. /* write auth_bytecnt 0/1/2/3, start with 0 */
  1393. for (i = 0; i < 2; i++)
  1394. QCE_WRITE_REG(sreq->auth_data[i], pce_dev->iobase +
  1395. CRYPTO_AUTH_BYTECNT0_REG +
  1396. i * sizeof(uint32_t));
  1397. /* Set/reset last bit in CFG register */
  1398. if (sreq->last_blk)
  1399. auth_cfg |= 1 << CRYPTO_LAST;
  1400. else
  1401. auth_cfg &= ~(1 << CRYPTO_LAST);
  1402. if (sreq->first_blk)
  1403. auth_cfg |= 1 << CRYPTO_FIRST;
  1404. else
  1405. auth_cfg &= ~(1 << CRYPTO_FIRST);
  1406. if (use_hw_key)
  1407. auth_cfg |= 1 << CRYPTO_USE_HW_KEY_AUTH;
  1408. if (use_pipe_key)
  1409. auth_cfg |= 1 << CRYPTO_USE_PIPE_KEY_AUTH;
  1410. go_proc:
  1411. /* write seg_cfg */
  1412. QCE_WRITE_REG(auth_cfg, pce_dev->iobase + CRYPTO_AUTH_SEG_CFG_REG);
  1413. /* write auth seg_size */
  1414. QCE_WRITE_REG(sreq->size, pce_dev->iobase + CRYPTO_AUTH_SEG_SIZE_REG);
  1415. /* write auth_seg_start */
  1416. QCE_WRITE_REG(0, pce_dev->iobase + CRYPTO_AUTH_SEG_START_REG);
  1417. /* reset encr seg_cfg */
  1418. QCE_WRITE_REG(0, pce_dev->iobase + CRYPTO_ENCR_SEG_CFG_REG);
  1419. /* write seg_size */
  1420. QCE_WRITE_REG(sreq->size, pce_dev->iobase + CRYPTO_SEG_SIZE_REG);
  1421. QCE_WRITE_REG(pce_dev->reg.crypto_cfg_le, (pce_dev->iobase +
  1422. CRYPTO_CONFIG_REG));
  1423. /* issue go to crypto */
  1424. if (!use_hw_key) {
  1425. QCE_WRITE_REG(((1 << CRYPTO_GO) | (1 << CRYPTO_RESULTS_DUMP) |
  1426. (1 << CRYPTO_CLR_CNTXT)),
  1427. pce_dev->iobase + CRYPTO_GOPROC_REG);
  1428. } else {
  1429. QCE_WRITE_REG(((1 << CRYPTO_GO) | (1 << CRYPTO_RESULTS_DUMP)),
  1430. pce_dev->iobase + CRYPTO_GOPROC_QC_KEY_REG);
  1431. }
  1432. /*
  1433. * Ensure previous instructions (setting the GO register)
  1434. * was completed before issuing a DMA transfer request
  1435. */
  1436. mb();
  1437. return 0;
  1438. }
  1439. static int _ce_setup_aead_direct(struct qce_device *pce_dev,
  1440. struct qce_req *q_req, uint32_t totallen_in, uint32_t coffset)
  1441. {
  1442. int32_t authk_size_in_word = SHA_HMAC_KEY_SIZE/sizeof(uint32_t);
  1443. int i;
  1444. uint32_t mackey32[SHA_HMAC_KEY_SIZE/sizeof(uint32_t)] = {0};
  1445. uint32_t a_cfg;
  1446. uint32_t enckey32[(MAX_CIPHER_KEY_SIZE*2)/sizeof(uint32_t)] = {0};
  1447. uint32_t enciv32[MAX_IV_LENGTH/sizeof(uint32_t)] = {0};
  1448. uint32_t enck_size_in_word = 0;
  1449. uint32_t enciv_in_word;
  1450. uint32_t key_size;
  1451. uint32_t ivsize = q_req->ivsize;
  1452. uint32_t encr_cfg;
  1453. /* clear status */
  1454. QCE_WRITE_REG(0, pce_dev->iobase + CRYPTO_STATUS_REG);
  1455. if (qce_crypto_config(pce_dev, q_req->offload_op))
  1456. return -EINVAL;
  1457. QCE_WRITE_REG(pce_dev->reg.crypto_cfg_be, (pce_dev->iobase +
  1458. CRYPTO_CONFIG_REG));
  1459. /*
  1460. * Ensure previous instructions (setting the CONFIG register)
  1461. * was completed before issuing starting to set other config register
  1462. * This is to ensure the configurations are done in correct endian-ness
  1463. * as set in the CONFIG registers
  1464. */
  1465. mb();
  1466. key_size = q_req->encklen;
  1467. enck_size_in_word = key_size/sizeof(uint32_t);
  1468. switch (q_req->alg) {
  1469. case CIPHER_ALG_DES:
  1470. switch (q_req->mode) {
  1471. case QCE_MODE_CBC:
  1472. encr_cfg = pce_dev->reg.encr_cfg_des_cbc;
  1473. break;
  1474. default:
  1475. return -EINVAL;
  1476. }
  1477. enciv_in_word = 2;
  1478. break;
  1479. case CIPHER_ALG_3DES:
  1480. switch (q_req->mode) {
  1481. case QCE_MODE_CBC:
  1482. encr_cfg = pce_dev->reg.encr_cfg_3des_cbc;
  1483. break;
  1484. default:
  1485. return -EINVAL;
  1486. }
  1487. enciv_in_word = 2;
  1488. break;
  1489. case CIPHER_ALG_AES:
  1490. switch (q_req->mode) {
  1491. case QCE_MODE_CBC:
  1492. if (key_size == AES128_KEY_SIZE)
  1493. encr_cfg = pce_dev->reg.encr_cfg_aes_cbc_128;
  1494. else if (key_size == AES256_KEY_SIZE)
  1495. encr_cfg = pce_dev->reg.encr_cfg_aes_cbc_256;
  1496. else
  1497. return -EINVAL;
  1498. break;
  1499. default:
  1500. return -EINVAL;
  1501. }
  1502. enciv_in_word = 4;
  1503. break;
  1504. default:
  1505. return -EINVAL;
  1506. }
  1507. /* write CNTR0_IV0_REG */
  1508. if (q_req->mode != QCE_MODE_ECB) {
  1509. _byte_stream_to_net_words(enciv32, q_req->iv, ivsize);
  1510. for (i = 0; i < enciv_in_word; i++)
  1511. QCE_WRITE_REG(enciv32[i], pce_dev->iobase +
  1512. (CRYPTO_CNTR0_IV0_REG + i * sizeof(uint32_t)));
  1513. }
  1514. /*
  1515. * write encr key
  1516. * do not use hw key or pipe key
  1517. */
  1518. _byte_stream_to_net_words(enckey32, q_req->enckey, key_size);
  1519. for (i = 0; i < enck_size_in_word; i++)
  1520. QCE_WRITE_REG(enckey32[i], pce_dev->iobase +
  1521. (CRYPTO_ENCR_KEY0_REG + i * sizeof(uint32_t)));
  1522. /* write encr seg cfg */
  1523. if (q_req->dir == QCE_ENCRYPT)
  1524. encr_cfg |= (1 << CRYPTO_ENCODE);
  1525. QCE_WRITE_REG(encr_cfg, pce_dev->iobase + CRYPTO_ENCR_SEG_CFG_REG);
  1526. /* we only support sha1-hmac and sha256-hmac at this point */
  1527. _byte_stream_to_net_words(mackey32, q_req->authkey,
  1528. q_req->authklen);
  1529. for (i = 0; i < authk_size_in_word; i++)
  1530. QCE_WRITE_REG(mackey32[i], pce_dev->iobase +
  1531. (CRYPTO_AUTH_KEY0_REG + i * sizeof(uint32_t)));
  1532. if (q_req->auth_alg == QCE_HASH_SHA1_HMAC) {
  1533. for (i = 0; i < 5; i++)
  1534. QCE_WRITE_REG(_std_init_vector_sha1[i],
  1535. pce_dev->iobase +
  1536. (CRYPTO_AUTH_IV0_REG + i * sizeof(uint32_t)));
  1537. } else {
  1538. for (i = 0; i < 8; i++)
  1539. QCE_WRITE_REG(_std_init_vector_sha256[i],
  1540. pce_dev->iobase +
  1541. (CRYPTO_AUTH_IV0_REG + i * sizeof(uint32_t)));
  1542. }
  1543. /* write auth_bytecnt 0/1, start with 0 */
  1544. QCE_WRITE_REG(0, pce_dev->iobase + CRYPTO_AUTH_BYTECNT0_REG);
  1545. QCE_WRITE_REG(0, pce_dev->iobase + CRYPTO_AUTH_BYTECNT1_REG);
  1546. /* write encr seg size */
  1547. QCE_WRITE_REG(q_req->cryptlen, pce_dev->iobase +
  1548. CRYPTO_ENCR_SEG_SIZE_REG);
  1549. /* write encr start */
  1550. QCE_WRITE_REG(coffset & 0xffff, pce_dev->iobase +
  1551. CRYPTO_ENCR_SEG_START_REG);
  1552. if (q_req->auth_alg == QCE_HASH_SHA1_HMAC)
  1553. a_cfg = pce_dev->reg.auth_cfg_aead_sha1_hmac;
  1554. else
  1555. a_cfg = pce_dev->reg.auth_cfg_aead_sha256_hmac;
  1556. if (q_req->dir == QCE_ENCRYPT)
  1557. a_cfg |= (CRYPTO_AUTH_POS_AFTER << CRYPTO_AUTH_POS);
  1558. else
  1559. a_cfg |= (CRYPTO_AUTH_POS_BEFORE << CRYPTO_AUTH_POS);
  1560. /* write auth seg_cfg */
  1561. QCE_WRITE_REG(a_cfg, pce_dev->iobase + CRYPTO_AUTH_SEG_CFG_REG);
  1562. /* write auth seg_size */
  1563. QCE_WRITE_REG(totallen_in, pce_dev->iobase + CRYPTO_AUTH_SEG_SIZE_REG);
  1564. /* write auth_seg_start */
  1565. QCE_WRITE_REG(0, pce_dev->iobase + CRYPTO_AUTH_SEG_START_REG);
  1566. /* write seg_size */
  1567. QCE_WRITE_REG(totallen_in, pce_dev->iobase + CRYPTO_SEG_SIZE_REG);
  1568. QCE_WRITE_REG(pce_dev->reg.crypto_cfg_le, (pce_dev->iobase +
  1569. CRYPTO_CONFIG_REG));
  1570. /* issue go to crypto */
  1571. QCE_WRITE_REG(((1 << CRYPTO_GO) | (1 << CRYPTO_RESULTS_DUMP) |
  1572. (1 << CRYPTO_CLR_CNTXT)),
  1573. pce_dev->iobase + CRYPTO_GOPROC_REG);
  1574. /*
  1575. * Ensure previous instructions (setting the GO register)
  1576. * was completed before issuing a DMA transfer request
  1577. */
  1578. mb();
  1579. return 0;
  1580. }
  1581. static int _ce_setup_cipher_direct(struct qce_device *pce_dev,
  1582. struct qce_req *creq, uint32_t totallen_in, uint32_t coffset)
  1583. {
  1584. uint32_t enckey32[(MAX_CIPHER_KEY_SIZE * 2)/sizeof(uint32_t)] = {
  1585. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  1586. uint32_t enciv32[MAX_IV_LENGTH / sizeof(uint32_t)] = {
  1587. 0, 0, 0, 0};
  1588. uint32_t enck_size_in_word = 0;
  1589. uint32_t key_size;
  1590. bool use_hw_key = false;
  1591. bool use_pipe_key = false;
  1592. uint32_t encr_cfg = 0;
  1593. uint32_t ivsize = creq->ivsize;
  1594. int i;
  1595. /* clear status */
  1596. QCE_WRITE_REG(0, pce_dev->iobase + CRYPTO_STATUS_REG);
  1597. if (qce_crypto_config(pce_dev, creq->offload_op))
  1598. return -EINVAL;
  1599. QCE_WRITE_REG(pce_dev->reg.crypto_cfg_be,
  1600. (pce_dev->iobase + CRYPTO_CONFIG_REG));
  1601. QCE_WRITE_REG(pce_dev->reg.crypto_cfg_le,
  1602. (pce_dev->iobase + CRYPTO_CONFIG_REG));
  1603. /*
  1604. * Ensure previous instructions (setting the CONFIG register)
  1605. * was completed before issuing starting to set other config register
  1606. * This is to ensure the configurations are done in correct endian-ness
  1607. * as set in the CONFIG registers
  1608. */
  1609. mb();
  1610. if (creq->mode == QCE_MODE_XTS)
  1611. key_size = creq->encklen/2;
  1612. else
  1613. key_size = creq->encklen;
  1614. if ((creq->flags & QCRYPTO_CTX_USE_HW_KEY) == QCRYPTO_CTX_USE_HW_KEY) {
  1615. use_hw_key = true;
  1616. } else {
  1617. if ((creq->flags & QCRYPTO_CTX_USE_PIPE_KEY) ==
  1618. QCRYPTO_CTX_USE_PIPE_KEY)
  1619. use_pipe_key = true;
  1620. }
  1621. if (!use_pipe_key && !use_hw_key) {
  1622. _byte_stream_to_net_words(enckey32, creq->enckey, key_size);
  1623. enck_size_in_word = key_size/sizeof(uint32_t);
  1624. }
  1625. if ((creq->op == QCE_REQ_AEAD) && (creq->mode == QCE_MODE_CCM)) {
  1626. uint32_t authklen32 = creq->encklen/sizeof(uint32_t);
  1627. uint32_t noncelen32 = MAX_NONCE/sizeof(uint32_t);
  1628. uint32_t nonce32[MAX_NONCE/sizeof(uint32_t)] = {0, 0, 0, 0};
  1629. uint32_t auth_cfg = 0;
  1630. /* Clear auth_ivn, auth_keyn registers */
  1631. for (i = 0; i < 16; i++) {
  1632. QCE_WRITE_REG(0, (pce_dev->iobase +
  1633. (CRYPTO_AUTH_IV0_REG + i*sizeof(uint32_t))));
  1634. QCE_WRITE_REG(0, (pce_dev->iobase +
  1635. (CRYPTO_AUTH_KEY0_REG + i*sizeof(uint32_t))));
  1636. }
  1637. /* write auth_bytecnt 0/1/2/3, start with 0 */
  1638. for (i = 0; i < 4; i++)
  1639. QCE_WRITE_REG(0, pce_dev->iobase +
  1640. CRYPTO_AUTH_BYTECNT0_REG +
  1641. i * sizeof(uint32_t));
  1642. /* write nonce */
  1643. _byte_stream_to_net_words(nonce32, creq->nonce, MAX_NONCE);
  1644. for (i = 0; i < noncelen32; i++)
  1645. QCE_WRITE_REG(nonce32[i], pce_dev->iobase +
  1646. CRYPTO_AUTH_INFO_NONCE0_REG +
  1647. (i*sizeof(uint32_t)));
  1648. if (creq->authklen == AES128_KEY_SIZE)
  1649. auth_cfg = pce_dev->reg.auth_cfg_aes_ccm_128;
  1650. else {
  1651. if (creq->authklen == AES256_KEY_SIZE)
  1652. auth_cfg = pce_dev->reg.auth_cfg_aes_ccm_256;
  1653. }
  1654. if (creq->dir == QCE_ENCRYPT)
  1655. auth_cfg |= (CRYPTO_AUTH_POS_BEFORE << CRYPTO_AUTH_POS);
  1656. else
  1657. auth_cfg |= (CRYPTO_AUTH_POS_AFTER << CRYPTO_AUTH_POS);
  1658. auth_cfg |= ((creq->authsize - 1) << CRYPTO_AUTH_SIZE);
  1659. if (use_hw_key) {
  1660. auth_cfg |= (1 << CRYPTO_USE_HW_KEY_AUTH);
  1661. } else {
  1662. auth_cfg &= ~(1 << CRYPTO_USE_HW_KEY_AUTH);
  1663. /* write auth key */
  1664. for (i = 0; i < authklen32; i++)
  1665. QCE_WRITE_REG(enckey32[i], pce_dev->iobase +
  1666. CRYPTO_AUTH_KEY0_REG + (i*sizeof(uint32_t)));
  1667. }
  1668. QCE_WRITE_REG(auth_cfg, pce_dev->iobase +
  1669. CRYPTO_AUTH_SEG_CFG_REG);
  1670. if (creq->dir == QCE_ENCRYPT) {
  1671. QCE_WRITE_REG(totallen_in, pce_dev->iobase +
  1672. CRYPTO_AUTH_SEG_SIZE_REG);
  1673. } else {
  1674. QCE_WRITE_REG((totallen_in - creq->authsize),
  1675. pce_dev->iobase + CRYPTO_AUTH_SEG_SIZE_REG);
  1676. }
  1677. QCE_WRITE_REG(0, pce_dev->iobase + CRYPTO_AUTH_SEG_START_REG);
  1678. } else {
  1679. if (creq->op != QCE_REQ_AEAD)
  1680. QCE_WRITE_REG(0, pce_dev->iobase +
  1681. CRYPTO_AUTH_SEG_CFG_REG);
  1682. }
  1683. /*
  1684. * Ensure previous instructions (write to all AUTH registers)
  1685. * was completed before accessing a register that is not in
  1686. * in the same 1K range.
  1687. */
  1688. mb();
  1689. switch (creq->mode) {
  1690. case QCE_MODE_ECB:
  1691. if (key_size == AES128_KEY_SIZE)
  1692. encr_cfg = pce_dev->reg.encr_cfg_aes_ecb_128;
  1693. else
  1694. encr_cfg = pce_dev->reg.encr_cfg_aes_ecb_256;
  1695. break;
  1696. case QCE_MODE_CBC:
  1697. if (key_size == AES128_KEY_SIZE)
  1698. encr_cfg = pce_dev->reg.encr_cfg_aes_cbc_128;
  1699. else
  1700. encr_cfg = pce_dev->reg.encr_cfg_aes_cbc_256;
  1701. break;
  1702. case QCE_MODE_XTS:
  1703. if (key_size == AES128_KEY_SIZE)
  1704. encr_cfg = pce_dev->reg.encr_cfg_aes_xts_128;
  1705. else
  1706. encr_cfg = pce_dev->reg.encr_cfg_aes_xts_256;
  1707. break;
  1708. case QCE_MODE_CCM:
  1709. if (key_size == AES128_KEY_SIZE)
  1710. encr_cfg = pce_dev->reg.encr_cfg_aes_ccm_128;
  1711. else
  1712. encr_cfg = pce_dev->reg.encr_cfg_aes_ccm_256;
  1713. break;
  1714. case QCE_MODE_CTR:
  1715. default:
  1716. if (key_size == AES128_KEY_SIZE)
  1717. encr_cfg = pce_dev->reg.encr_cfg_aes_ctr_128;
  1718. else
  1719. encr_cfg = pce_dev->reg.encr_cfg_aes_ctr_256;
  1720. break;
  1721. }
  1722. switch (creq->alg) {
  1723. case CIPHER_ALG_DES:
  1724. if (creq->mode != QCE_MODE_ECB) {
  1725. encr_cfg = pce_dev->reg.encr_cfg_des_cbc;
  1726. _byte_stream_to_net_words(enciv32, creq->iv, ivsize);
  1727. QCE_WRITE_REG(enciv32[0], pce_dev->iobase +
  1728. CRYPTO_CNTR0_IV0_REG);
  1729. QCE_WRITE_REG(enciv32[1], pce_dev->iobase +
  1730. CRYPTO_CNTR1_IV1_REG);
  1731. } else {
  1732. encr_cfg = pce_dev->reg.encr_cfg_des_ecb;
  1733. }
  1734. if (!use_hw_key) {
  1735. QCE_WRITE_REG(enckey32[0], pce_dev->iobase +
  1736. CRYPTO_ENCR_KEY0_REG);
  1737. QCE_WRITE_REG(enckey32[1], pce_dev->iobase +
  1738. CRYPTO_ENCR_KEY1_REG);
  1739. }
  1740. break;
  1741. case CIPHER_ALG_3DES:
  1742. if (creq->mode != QCE_MODE_ECB) {
  1743. _byte_stream_to_net_words(enciv32, creq->iv, ivsize);
  1744. QCE_WRITE_REG(enciv32[0], pce_dev->iobase +
  1745. CRYPTO_CNTR0_IV0_REG);
  1746. QCE_WRITE_REG(enciv32[1], pce_dev->iobase +
  1747. CRYPTO_CNTR1_IV1_REG);
  1748. encr_cfg = pce_dev->reg.encr_cfg_3des_cbc;
  1749. } else {
  1750. encr_cfg = pce_dev->reg.encr_cfg_3des_ecb;
  1751. }
  1752. if (!use_hw_key) {
  1753. /* write encr key */
  1754. for (i = 0; i < 6; i++)
  1755. QCE_WRITE_REG(enckey32[0], (pce_dev->iobase +
  1756. (CRYPTO_ENCR_KEY0_REG + i * sizeof(uint32_t))));
  1757. }
  1758. break;
  1759. case CIPHER_ALG_AES:
  1760. default:
  1761. if (creq->mode == QCE_MODE_XTS) {
  1762. uint32_t xtskey32[MAX_CIPHER_KEY_SIZE/sizeof(uint32_t)]
  1763. = {0, 0, 0, 0, 0, 0, 0, 0};
  1764. uint32_t xtsklen =
  1765. creq->encklen/(2 * sizeof(uint32_t));
  1766. if (!use_hw_key && !use_pipe_key) {
  1767. _byte_stream_to_net_words(xtskey32,
  1768. (creq->enckey + creq->encklen/2),
  1769. creq->encklen/2);
  1770. /* write xts encr key */
  1771. for (i = 0; i < xtsklen; i++)
  1772. QCE_WRITE_REG(xtskey32[i],
  1773. pce_dev->iobase +
  1774. CRYPTO_ENCR_XTS_KEY0_REG +
  1775. (i * sizeof(uint32_t)));
  1776. }
  1777. /* write xts du size */
  1778. switch (creq->flags & QCRYPTO_CTX_XTS_MASK) {
  1779. case QCRYPTO_CTX_XTS_DU_SIZE_512B:
  1780. QCE_WRITE_REG(
  1781. min((uint32_t)QCE_SECTOR_SIZE,
  1782. creq->cryptlen), pce_dev->iobase +
  1783. CRYPTO_ENCR_XTS_DU_SIZE_REG);
  1784. break;
  1785. case QCRYPTO_CTX_XTS_DU_SIZE_1KB:
  1786. QCE_WRITE_REG(
  1787. min((uint32_t)(QCE_SECTOR_SIZE * 2),
  1788. creq->cryptlen), pce_dev->iobase +
  1789. CRYPTO_ENCR_XTS_DU_SIZE_REG);
  1790. break;
  1791. default:
  1792. QCE_WRITE_REG(creq->cryptlen,
  1793. pce_dev->iobase +
  1794. CRYPTO_ENCR_XTS_DU_SIZE_REG);
  1795. break;
  1796. }
  1797. }
  1798. if (creq->mode != QCE_MODE_ECB) {
  1799. if (creq->mode == QCE_MODE_XTS)
  1800. _byte_stream_swap_to_net_words(enciv32,
  1801. creq->iv, ivsize);
  1802. else
  1803. _byte_stream_to_net_words(enciv32, creq->iv,
  1804. ivsize);
  1805. /* write encr cntr iv */
  1806. for (i = 0; i <= 3; i++)
  1807. QCE_WRITE_REG(enciv32[i], pce_dev->iobase +
  1808. CRYPTO_CNTR0_IV0_REG +
  1809. (i * sizeof(uint32_t)));
  1810. if (creq->mode == QCE_MODE_CCM) {
  1811. /* write cntr iv for ccm */
  1812. for (i = 0; i <= 3; i++)
  1813. QCE_WRITE_REG(enciv32[i],
  1814. pce_dev->iobase +
  1815. CRYPTO_ENCR_CCM_INT_CNTR0_REG +
  1816. (i * sizeof(uint32_t)));
  1817. /* update cntr_iv[3] by one */
  1818. QCE_WRITE_REG((enciv32[3] + 1),
  1819. pce_dev->iobase +
  1820. CRYPTO_CNTR0_IV0_REG +
  1821. (3 * sizeof(uint32_t)));
  1822. }
  1823. }
  1824. if (creq->op == QCE_REQ_ABLK_CIPHER_NO_KEY) {
  1825. encr_cfg |= (CRYPTO_ENCR_KEY_SZ_AES128 <<
  1826. CRYPTO_ENCR_KEY_SZ);
  1827. } else {
  1828. if (!use_hw_key && !use_pipe_key) {
  1829. for (i = 0; i < enck_size_in_word; i++)
  1830. QCE_WRITE_REG(enckey32[i],
  1831. pce_dev->iobase +
  1832. CRYPTO_ENCR_KEY0_REG +
  1833. (i * sizeof(uint32_t)));
  1834. }
  1835. } /* else of if (creq->op == QCE_REQ_ABLK_CIPHER_NO_KEY) */
  1836. break;
  1837. } /* end of switch (creq->mode) */
  1838. if (use_pipe_key)
  1839. encr_cfg |= (CRYPTO_USE_PIPE_KEY_ENCR_ENABLED
  1840. << CRYPTO_USE_PIPE_KEY_ENCR);
  1841. /* write encr seg cfg */
  1842. encr_cfg |= ((creq->dir == QCE_ENCRYPT) ? 1 : 0) << CRYPTO_ENCODE;
  1843. if (use_hw_key)
  1844. encr_cfg |= (CRYPTO_USE_HW_KEY << CRYPTO_USE_HW_KEY_ENCR);
  1845. else
  1846. encr_cfg &= ~(CRYPTO_USE_HW_KEY << CRYPTO_USE_HW_KEY_ENCR);
  1847. /* write encr seg cfg */
  1848. QCE_WRITE_REG(encr_cfg, pce_dev->iobase + CRYPTO_ENCR_SEG_CFG_REG);
  1849. /* write encr seg size */
  1850. if ((creq->mode == QCE_MODE_CCM) && (creq->dir == QCE_DECRYPT)) {
  1851. QCE_WRITE_REG((creq->cryptlen + creq->authsize),
  1852. pce_dev->iobase + CRYPTO_ENCR_SEG_SIZE_REG);
  1853. } else {
  1854. QCE_WRITE_REG(creq->cryptlen,
  1855. pce_dev->iobase + CRYPTO_ENCR_SEG_SIZE_REG);
  1856. }
  1857. /* write pattern */
  1858. if (creq->is_pattern_valid)
  1859. QCE_WRITE_REG(creq->pattern_info, pce_dev->iobase +
  1860. CRYPTO_DATA_PATT_PROC_CFG_REG);
  1861. /* write block offset to CRYPTO_DATA_PARTIAL_BLOCK_PROC_CFG? */
  1862. QCE_WRITE_REG(((creq->block_offset << 4) |
  1863. (creq->block_offset ? 1 : 0)),
  1864. pce_dev->iobase + CRYPTO_DATA_PARTIAL_BLOCK_PROC_CFG_REG);
  1865. /* write encr seg start */
  1866. QCE_WRITE_REG((coffset & 0xffff),
  1867. pce_dev->iobase + CRYPTO_ENCR_SEG_START_REG);
  1868. /* write encr counter mask */
  1869. qce_set_iv_ctr_mask(pce_dev, creq);
  1870. QCE_WRITE_REG(pce_dev->reg.encr_cntr_mask_3,
  1871. pce_dev->iobase + CRYPTO_CNTR_MASK_REG);
  1872. QCE_WRITE_REG(pce_dev->reg.encr_cntr_mask_2,
  1873. pce_dev->iobase + CRYPTO_CNTR_MASK_REG2);
  1874. QCE_WRITE_REG(pce_dev->reg.encr_cntr_mask_1,
  1875. pce_dev->iobase + CRYPTO_CNTR_MASK_REG1);
  1876. QCE_WRITE_REG(pce_dev->reg.encr_cntr_mask_0,
  1877. pce_dev->iobase + CRYPTO_CNTR_MASK_REG0);
  1878. /* write seg size */
  1879. QCE_WRITE_REG(totallen_in, pce_dev->iobase + CRYPTO_SEG_SIZE_REG);
  1880. /* issue go to crypto */
  1881. if (!use_hw_key) {
  1882. QCE_WRITE_REG(((1 << CRYPTO_GO) | (1 << CRYPTO_RESULTS_DUMP) |
  1883. (1 << CRYPTO_CLR_CNTXT)),
  1884. pce_dev->iobase + CRYPTO_GOPROC_REG);
  1885. } else {
  1886. QCE_WRITE_REG(((1 << CRYPTO_GO) | (1 << CRYPTO_RESULTS_DUMP)),
  1887. pce_dev->iobase + CRYPTO_GOPROC_QC_KEY_REG);
  1888. }
  1889. /*
  1890. * Ensure previous instructions (setting the GO register)
  1891. * was completed before issuing a DMA transfer request
  1892. */
  1893. mb();
  1894. return 0;
  1895. }
  1896. static int _ce_f9_setup_direct(struct qce_device *pce_dev,
  1897. struct qce_f9_req *req)
  1898. {
  1899. uint32_t ikey32[OTA_KEY_SIZE/sizeof(uint32_t)];
  1900. uint32_t key_size_in_word = OTA_KEY_SIZE/sizeof(uint32_t);
  1901. uint32_t auth_cfg;
  1902. int i;
  1903. switch (req->algorithm) {
  1904. case QCE_OTA_ALGO_KASUMI:
  1905. auth_cfg = pce_dev->reg.auth_cfg_kasumi;
  1906. break;
  1907. case QCE_OTA_ALGO_SNOW3G:
  1908. default:
  1909. auth_cfg = pce_dev->reg.auth_cfg_snow3g;
  1910. break;
  1911. }
  1912. if (qce_crypto_config(pce_dev, QCE_OFFLOAD_NONE))
  1913. return -EINVAL;
  1914. /* clear status */
  1915. QCE_WRITE_REG(0, pce_dev->iobase + CRYPTO_STATUS_REG);
  1916. /* set big endian configuration */
  1917. QCE_WRITE_REG(pce_dev->reg.crypto_cfg_be, (pce_dev->iobase +
  1918. CRYPTO_CONFIG_REG));
  1919. /*
  1920. * Ensure previous instructions (setting the CONFIG register)
  1921. * was completed before issuing starting to set other config register
  1922. * This is to ensure the configurations are done in correct endian-ness
  1923. * as set in the CONFIG registers
  1924. */
  1925. mb();
  1926. /* write enc_seg_cfg */
  1927. QCE_WRITE_REG(0, pce_dev->iobase + CRYPTO_ENCR_SEG_CFG_REG);
  1928. /* write ecn_seg_size */
  1929. QCE_WRITE_REG(0, pce_dev->iobase + CRYPTO_ENCR_SEG_SIZE_REG);
  1930. /* write key in CRYPTO_AUTH_IV0-3_REG */
  1931. _byte_stream_to_net_words(ikey32, &req->ikey[0], OTA_KEY_SIZE);
  1932. for (i = 0; i < key_size_in_word; i++)
  1933. QCE_WRITE_REG(ikey32[i], (pce_dev->iobase +
  1934. (CRYPTO_AUTH_IV0_REG + i*sizeof(uint32_t))));
  1935. /* write last bits in CRYPTO_AUTH_IV4_REG */
  1936. QCE_WRITE_REG(req->last_bits, (pce_dev->iobase +
  1937. CRYPTO_AUTH_IV4_REG));
  1938. /* write fresh to CRYPTO_AUTH_BYTECNT0_REG */
  1939. QCE_WRITE_REG(req->fresh, (pce_dev->iobase +
  1940. CRYPTO_AUTH_BYTECNT0_REG));
  1941. /* write count-i to CRYPTO_AUTH_BYTECNT1_REG */
  1942. QCE_WRITE_REG(req->count_i, (pce_dev->iobase +
  1943. CRYPTO_AUTH_BYTECNT1_REG));
  1944. /* write auth seg cfg */
  1945. if (req->direction == QCE_OTA_DIR_DOWNLINK)
  1946. auth_cfg |= BIT(CRYPTO_F9_DIRECTION);
  1947. QCE_WRITE_REG(auth_cfg, pce_dev->iobase + CRYPTO_AUTH_SEG_CFG_REG);
  1948. /* write auth seg size */
  1949. QCE_WRITE_REG(req->msize, pce_dev->iobase + CRYPTO_AUTH_SEG_SIZE_REG);
  1950. /* write auth seg start*/
  1951. QCE_WRITE_REG(0, pce_dev->iobase + CRYPTO_AUTH_SEG_START_REG);
  1952. /* write seg size */
  1953. QCE_WRITE_REG(req->msize, pce_dev->iobase + CRYPTO_SEG_SIZE_REG);
  1954. /* set little endian configuration before go*/
  1955. QCE_WRITE_REG(pce_dev->reg.crypto_cfg_le, (pce_dev->iobase +
  1956. CRYPTO_CONFIG_REG));
  1957. /* write go */
  1958. QCE_WRITE_REG(((1 << CRYPTO_GO) | (1 << CRYPTO_RESULTS_DUMP) |
  1959. (1 << CRYPTO_CLR_CNTXT)),
  1960. pce_dev->iobase + CRYPTO_GOPROC_REG);
  1961. /*
  1962. * Ensure previous instructions (setting the GO register)
  1963. * was completed before issuing a DMA transfer request
  1964. */
  1965. mb();
  1966. return 0;
  1967. }
  1968. static int _ce_f8_setup_direct(struct qce_device *pce_dev,
  1969. struct qce_f8_req *req, bool key_stream_mode,
  1970. uint16_t npkts, uint16_t cipher_offset, uint16_t cipher_size)
  1971. {
  1972. int i = 0;
  1973. uint32_t encr_cfg = 0;
  1974. uint32_t ckey32[OTA_KEY_SIZE/sizeof(uint32_t)];
  1975. uint32_t key_size_in_word = OTA_KEY_SIZE/sizeof(uint32_t);
  1976. switch (req->algorithm) {
  1977. case QCE_OTA_ALGO_KASUMI:
  1978. encr_cfg = pce_dev->reg.encr_cfg_kasumi;
  1979. break;
  1980. case QCE_OTA_ALGO_SNOW3G:
  1981. default:
  1982. encr_cfg = pce_dev->reg.encr_cfg_snow3g;
  1983. break;
  1984. }
  1985. /* clear status */
  1986. QCE_WRITE_REG(0, pce_dev->iobase + CRYPTO_STATUS_REG);
  1987. /* set big endian configuration */
  1988. if (qce_crypto_config(pce_dev, QCE_OFFLOAD_NONE))
  1989. return -EINVAL;
  1990. QCE_WRITE_REG(pce_dev->reg.crypto_cfg_be, (pce_dev->iobase +
  1991. CRYPTO_CONFIG_REG));
  1992. /* write auth seg configuration */
  1993. QCE_WRITE_REG(0, pce_dev->iobase + CRYPTO_AUTH_SEG_CFG_REG);
  1994. /* write auth seg size */
  1995. QCE_WRITE_REG(0, pce_dev->iobase + CRYPTO_AUTH_SEG_SIZE_REG);
  1996. /* write key */
  1997. _byte_stream_to_net_words(ckey32, &req->ckey[0], OTA_KEY_SIZE);
  1998. for (i = 0; i < key_size_in_word; i++)
  1999. QCE_WRITE_REG(ckey32[i], (pce_dev->iobase +
  2000. (CRYPTO_ENCR_KEY0_REG + i*sizeof(uint32_t))));
  2001. /* write encr seg cfg */
  2002. if (key_stream_mode)
  2003. encr_cfg |= BIT(CRYPTO_F8_KEYSTREAM_ENABLE);
  2004. if (req->direction == QCE_OTA_DIR_DOWNLINK)
  2005. encr_cfg |= BIT(CRYPTO_F8_DIRECTION);
  2006. QCE_WRITE_REG(encr_cfg, pce_dev->iobase +
  2007. CRYPTO_ENCR_SEG_CFG_REG);
  2008. /* write encr seg start */
  2009. QCE_WRITE_REG((cipher_offset & 0xffff), pce_dev->iobase +
  2010. CRYPTO_ENCR_SEG_START_REG);
  2011. /* write encr seg size */
  2012. QCE_WRITE_REG(cipher_size, pce_dev->iobase +
  2013. CRYPTO_ENCR_SEG_SIZE_REG);
  2014. /* write seg size */
  2015. QCE_WRITE_REG(req->data_len, pce_dev->iobase +
  2016. CRYPTO_SEG_SIZE_REG);
  2017. /* write cntr0_iv0 for countC */
  2018. QCE_WRITE_REG(req->count_c, pce_dev->iobase +
  2019. CRYPTO_CNTR0_IV0_REG);
  2020. /* write cntr1_iv1 for nPkts, and bearer */
  2021. if (npkts == 1)
  2022. npkts = 0;
  2023. QCE_WRITE_REG(req->bearer << CRYPTO_CNTR1_IV1_REG_F8_BEARER |
  2024. npkts << CRYPTO_CNTR1_IV1_REG_F8_PKT_CNT,
  2025. pce_dev->iobase + CRYPTO_CNTR1_IV1_REG);
  2026. /* set little endian configuration before go*/
  2027. QCE_WRITE_REG(pce_dev->reg.crypto_cfg_le, (pce_dev->iobase +
  2028. CRYPTO_CONFIG_REG));
  2029. /* write go */
  2030. QCE_WRITE_REG(((1 << CRYPTO_GO) | (1 << CRYPTO_RESULTS_DUMP) |
  2031. (1 << CRYPTO_CLR_CNTXT)),
  2032. pce_dev->iobase + CRYPTO_GOPROC_REG);
  2033. /*
  2034. * Ensure previous instructions (setting the GO register)
  2035. * was completed before issuing a DMA transfer request
  2036. */
  2037. mb();
  2038. return 0;
  2039. }
  2040. static int _qce_unlock_other_pipes(struct qce_device *pce_dev, int req_info)
  2041. {
  2042. int rc = 0;
  2043. struct ce_sps_data *pce_sps_data = &pce_dev->ce_request_info
  2044. [req_info].ce_sps;
  2045. uint16_t op = pce_dev->ce_request_info[req_info].offload_op;
  2046. if (pce_dev->no_get_around || !pce_dev->support_cmd_dscr)
  2047. return rc;
  2048. rc = sps_transfer_one(pce_dev->ce_bam_info.consumer[op].pipe,
  2049. GET_PHYS_ADDR(
  2050. pce_sps_data->cmdlistptr.unlock_all_pipes.cmdlist),
  2051. 0, NULL, (SPS_IOVEC_FLAG_CMD | SPS_IOVEC_FLAG_UNLOCK));
  2052. if (rc) {
  2053. pr_err("sps_xfr_one() fail rc=%d\n", rc);
  2054. rc = -EINVAL;
  2055. }
  2056. return rc;
  2057. }
  2058. static int qce_sps_set_irqs(struct qce_device *pce_dev, bool enable)
  2059. {
  2060. if (enable)
  2061. return sps_bam_enable_irqs(pce_dev->ce_bam_info.bam_handle);
  2062. else
  2063. return sps_bam_disable_irqs(pce_dev->ce_bam_info.bam_handle);
  2064. }
  2065. int qce_set_irqs(void *handle, bool enable)
  2066. {
  2067. return qce_sps_set_irqs(handle, enable);
  2068. }
  2069. EXPORT_SYMBOL(qce_set_irqs);
  2070. static inline void qce_free_req_info(struct qce_device *pce_dev, int req_info,
  2071. bool is_complete);
  2072. static int qce_sps_pipe_reset(struct qce_device *pce_dev, int op)
  2073. {
  2074. int rc = -1;
  2075. struct sps_pipe *sps_pipe_info = NULL;
  2076. struct sps_connect *sps_connect_info = NULL;
  2077. /* Reset both the pipe sets in the pipe group */
  2078. sps_pipe_reset(pce_dev->ce_bam_info.bam_handle,
  2079. pce_dev->ce_bam_info.dest_pipe_index[op]);
  2080. sps_pipe_reset(pce_dev->ce_bam_info.bam_handle,
  2081. pce_dev->ce_bam_info.src_pipe_index[op]);
  2082. /* Reconnect to consumer pipe */
  2083. sps_pipe_info = pce_dev->ce_bam_info.consumer[op].pipe;
  2084. sps_connect_info = &pce_dev->ce_bam_info.consumer[op].connect;
  2085. rc = sps_disconnect(sps_pipe_info);
  2086. if (rc) {
  2087. pr_err("sps_disconnect() fail pipe=0x%lx, rc = %d\n",
  2088. (uintptr_t)sps_pipe_info, rc);
  2089. goto exit;
  2090. }
  2091. memset(sps_connect_info->desc.base, 0x00,
  2092. sps_connect_info->desc.size);
  2093. rc = sps_connect(sps_pipe_info, sps_connect_info);
  2094. if (rc) {
  2095. pr_err("sps_connect() fail pipe=0x%lx, rc = %d\n",
  2096. (uintptr_t)sps_pipe_info, rc);
  2097. goto exit;
  2098. }
  2099. /* Reconnect to producer pipe */
  2100. sps_pipe_info = pce_dev->ce_bam_info.producer[op].pipe;
  2101. sps_connect_info = &pce_dev->ce_bam_info.producer[op].connect;
  2102. rc = sps_disconnect(sps_pipe_info);
  2103. if (rc) {
  2104. pr_err("sps_connect() fail pipe=0x%lx, rc = %d\n",
  2105. (uintptr_t)sps_pipe_info, rc);
  2106. goto exit;
  2107. }
  2108. memset(sps_connect_info->desc.base, 0x00,
  2109. sps_connect_info->desc.size);
  2110. rc = sps_connect(sps_pipe_info, sps_connect_info);
  2111. if (rc) {
  2112. pr_err("sps_connect() fail pipe=0x%lx, rc = %d\n",
  2113. (uintptr_t)sps_pipe_info, rc);
  2114. goto exit;
  2115. }
  2116. /* Register producer callback */
  2117. rc = sps_register_event(sps_pipe_info,
  2118. &pce_dev->ce_bam_info.producer[op].event);
  2119. if (rc)
  2120. pr_err("Producer cb registration failed rc = %d\n",
  2121. rc);
  2122. exit:
  2123. return rc;
  2124. }
  2125. #define MAX_RESET_TIME_RETRIES 1000
  2126. int qce_manage_timeout(void *handle, int req_info)
  2127. {
  2128. struct qce_device *pce_dev = (struct qce_device *) handle;
  2129. struct skcipher_request *areq;
  2130. struct ce_request_info *preq_info;
  2131. qce_comp_func_ptr_t qce_callback;
  2132. uint16_t op = pce_dev->ce_request_info[req_info].offload_op;
  2133. struct qce_error error = {0};
  2134. int retries = 0;
  2135. preq_info = &pce_dev->ce_request_info[req_info];
  2136. qce_callback = preq_info->qce_cb;
  2137. areq = (struct skcipher_request *) preq_info->areq;
  2138. pr_info("%s: req info = %d, offload op = %d\n", __func__, req_info, op);
  2139. if (qce_sps_pipe_reset(pce_dev, op))
  2140. pr_err("%s: pipe reset failed\n", __func__);
  2141. qce_get_crypto_status(pce_dev, &error);
  2142. while (!error.no_error && retries < MAX_RESET_TIME_RETRIES) {
  2143. usleep_range(3000, 5000);
  2144. retries++;
  2145. qce_get_crypto_status(pce_dev, &error);
  2146. pr_info("%s: waiting for reset to complete\n", __func__);
  2147. }
  2148. // Write memory barrier
  2149. wmb();
  2150. if (_qce_unlock_other_pipes(pce_dev, req_info))
  2151. pr_err("%s: fail unlock other pipes\n", __func__);
  2152. qce_enable_clock_gating(pce_dev);
  2153. if (!atomic_read(&preq_info->in_use)) {
  2154. pr_err("request information %d already done\n", req_info);
  2155. return -ENXIO;
  2156. }
  2157. qce_free_req_info(pce_dev, req_info, true);
  2158. return 0;
  2159. }
  2160. EXPORT_SYMBOL(qce_manage_timeout);
  2161. static int _aead_complete(struct qce_device *pce_dev, int req_info)
  2162. {
  2163. struct aead_request *areq;
  2164. unsigned char mac[SHA256_DIGEST_SIZE];
  2165. uint32_t ccm_fail_status = 0;
  2166. uint32_t result_dump_status = 0;
  2167. int32_t result_status = 0;
  2168. struct ce_request_info *preq_info;
  2169. struct ce_sps_data *pce_sps_data;
  2170. qce_comp_func_ptr_t qce_callback;
  2171. preq_info = &pce_dev->ce_request_info[req_info];
  2172. pce_sps_data = &preq_info->ce_sps;
  2173. qce_callback = preq_info->qce_cb;
  2174. areq = (struct aead_request *) preq_info->areq;
  2175. if (areq->src != areq->dst) {
  2176. qce_dma_unmap_sg(pce_dev->pdev, areq->dst, preq_info->dst_nents,
  2177. DMA_FROM_DEVICE);
  2178. }
  2179. qce_dma_unmap_sg(pce_dev->pdev, areq->src, preq_info->src_nents,
  2180. (areq->src == areq->dst) ? DMA_BIDIRECTIONAL :
  2181. DMA_TO_DEVICE);
  2182. if (preq_info->asg)
  2183. qce_dma_unmap_sg(pce_dev->pdev, preq_info->asg,
  2184. preq_info->assoc_nents, DMA_TO_DEVICE);
  2185. /* check MAC */
  2186. memcpy(mac, (char *)(&pce_sps_data->result->auth_iv[0]),
  2187. SHA256_DIGEST_SIZE);
  2188. /* read status before unlock */
  2189. if (preq_info->dir == QCE_DECRYPT) {
  2190. if (pce_dev->no_get_around)
  2191. if (pce_dev->no_ccm_mac_status_get_around)
  2192. ccm_fail_status =
  2193. be32_to_cpu(pce_sps_data->result->status);
  2194. else
  2195. ccm_fail_status =
  2196. be32_to_cpu(pce_sps_data->result_null->status);
  2197. else
  2198. ccm_fail_status = readl_relaxed(pce_dev->iobase +
  2199. CRYPTO_STATUS_REG);
  2200. }
  2201. if (_qce_unlock_other_pipes(pce_dev, req_info)) {
  2202. qce_free_req_info(pce_dev, req_info, true);
  2203. qce_callback(areq, mac, NULL, -ENXIO);
  2204. return -ENXIO;
  2205. }
  2206. result_dump_status = be32_to_cpu(pce_sps_data->result->status);
  2207. pce_sps_data->result->status = 0;
  2208. if (result_dump_status & ((1 << CRYPTO_SW_ERR) | (1 << CRYPTO_AXI_ERR)
  2209. | (1 << CRYPTO_HSD_ERR))) {
  2210. pr_err("aead operation error. Status %x\n", result_dump_status);
  2211. result_status = -ENXIO;
  2212. } else if (pce_sps_data->consumer_status |
  2213. pce_sps_data->producer_status) {
  2214. pr_err("aead sps operation error. sps status %x %x\n",
  2215. pce_sps_data->consumer_status,
  2216. pce_sps_data->producer_status);
  2217. result_status = -ENXIO;
  2218. }
  2219. if (!atomic_read(&preq_info->in_use)) {
  2220. pr_err("request information %d already done\n", req_info);
  2221. return -ENXIO;
  2222. }
  2223. if (preq_info->mode == QCE_MODE_CCM) {
  2224. /*
  2225. * Not from result dump, instead, use the status we just
  2226. * read of device for MAC_FAILED.
  2227. */
  2228. if (result_status == 0 && (preq_info->dir == QCE_DECRYPT) &&
  2229. (ccm_fail_status & (1 << CRYPTO_MAC_FAILED)))
  2230. result_status = -EBADMSG;
  2231. qce_free_req_info(pce_dev, req_info, true);
  2232. qce_callback(areq, mac, NULL, result_status);
  2233. } else {
  2234. uint32_t ivsize = 0;
  2235. struct crypto_aead *aead;
  2236. unsigned char iv[NUM_OF_CRYPTO_CNTR_IV_REG * CRYPTO_REG_SIZE];
  2237. aead = crypto_aead_reqtfm(areq);
  2238. ivsize = crypto_aead_ivsize(aead);
  2239. memcpy(iv, (char *)(pce_sps_data->result->encr_cntr_iv),
  2240. sizeof(iv));
  2241. qce_free_req_info(pce_dev, req_info, true);
  2242. qce_callback(areq, mac, iv, result_status);
  2243. }
  2244. return 0;
  2245. }
  2246. static int _sha_complete(struct qce_device *pce_dev, int req_info)
  2247. {
  2248. struct ahash_request *areq;
  2249. unsigned char digest[SHA256_DIGEST_SIZE];
  2250. uint32_t bytecount32[2];
  2251. int32_t result_status = 0;
  2252. uint32_t result_dump_status;
  2253. struct ce_request_info *preq_info;
  2254. struct ce_sps_data *pce_sps_data;
  2255. qce_comp_func_ptr_t qce_callback;
  2256. preq_info = &pce_dev->ce_request_info[req_info];
  2257. pce_sps_data = &preq_info->ce_sps;
  2258. qce_callback = preq_info->qce_cb;
  2259. areq = (struct ahash_request *) preq_info->areq;
  2260. if (!areq) {
  2261. pr_err("sha operation error. areq is NULL\n");
  2262. return -ENXIO;
  2263. }
  2264. qce_dma_unmap_sg(pce_dev->pdev, areq->src, preq_info->src_nents,
  2265. DMA_TO_DEVICE);
  2266. memcpy(digest, (char *)(&pce_sps_data->result->auth_iv[0]),
  2267. SHA256_DIGEST_SIZE);
  2268. _byte_stream_to_net_words(bytecount32,
  2269. (unsigned char *)pce_sps_data->result->auth_byte_count,
  2270. 2 * CRYPTO_REG_SIZE);
  2271. if (_qce_unlock_other_pipes(pce_dev, req_info)) {
  2272. qce_free_req_info(pce_dev, req_info, true);
  2273. qce_callback(areq, digest, (char *)bytecount32,
  2274. -ENXIO);
  2275. return -ENXIO;
  2276. }
  2277. result_dump_status = be32_to_cpu(pce_sps_data->result->status);
  2278. pce_sps_data->result->status = 0;
  2279. if (result_dump_status & ((1 << CRYPTO_SW_ERR) | (1 << CRYPTO_AXI_ERR)
  2280. | (1 << CRYPTO_HSD_ERR))) {
  2281. pr_err("sha operation error. Status %x\n", result_dump_status);
  2282. result_status = -ENXIO;
  2283. } else if (pce_sps_data->consumer_status) {
  2284. pr_err("sha sps operation error. sps status %x\n",
  2285. pce_sps_data->consumer_status);
  2286. result_status = -ENXIO;
  2287. }
  2288. if (!atomic_read(&preq_info->in_use)) {
  2289. pr_err("request information %d already done\n", req_info);
  2290. return -ENXIO;
  2291. }
  2292. qce_free_req_info(pce_dev, req_info, true);
  2293. qce_callback(areq, digest, (char *)bytecount32, result_status);
  2294. return 0;
  2295. }
  2296. static int _f9_complete(struct qce_device *pce_dev, int req_info)
  2297. {
  2298. uint32_t mac_i;
  2299. int32_t result_status = 0;
  2300. uint32_t result_dump_status;
  2301. struct ce_request_info *preq_info;
  2302. struct ce_sps_data *pce_sps_data;
  2303. qce_comp_func_ptr_t qce_callback;
  2304. void *areq;
  2305. preq_info = &pce_dev->ce_request_info[req_info];
  2306. pce_sps_data = &preq_info->ce_sps;
  2307. qce_callback = preq_info->qce_cb;
  2308. areq = preq_info->areq;
  2309. dma_unmap_single(pce_dev->pdev, preq_info->phy_ota_src,
  2310. preq_info->ota_size, DMA_TO_DEVICE);
  2311. _byte_stream_to_net_words(&mac_i,
  2312. (char *)(&pce_sps_data->result->auth_iv[0]),
  2313. CRYPTO_REG_SIZE);
  2314. if (_qce_unlock_other_pipes(pce_dev, req_info)) {
  2315. qce_free_req_info(pce_dev, req_info, true);
  2316. qce_callback(areq, NULL, NULL, -ENXIO);
  2317. return -ENXIO;
  2318. }
  2319. result_dump_status = be32_to_cpu(pce_sps_data->result->status);
  2320. pce_sps_data->result->status = 0;
  2321. if (result_dump_status & ((1 << CRYPTO_SW_ERR) | (1 << CRYPTO_AXI_ERR)
  2322. | (1 << CRYPTO_HSD_ERR))) {
  2323. pr_err("f9 operation error. Status %x\n", result_dump_status);
  2324. result_status = -ENXIO;
  2325. } else if (pce_sps_data->consumer_status |
  2326. pce_sps_data->producer_status) {
  2327. pr_err("f9 sps operation error. sps status %x %x\n",
  2328. pce_sps_data->consumer_status,
  2329. pce_sps_data->producer_status);
  2330. result_status = -ENXIO;
  2331. }
  2332. qce_free_req_info(pce_dev, req_info, true);
  2333. qce_callback(areq, (char *)&mac_i, NULL, result_status);
  2334. return 0;
  2335. }
  2336. static int _ablk_cipher_complete(struct qce_device *pce_dev, int req_info)
  2337. {
  2338. struct skcipher_request *areq;
  2339. unsigned char iv[NUM_OF_CRYPTO_CNTR_IV_REG * CRYPTO_REG_SIZE];
  2340. int32_t result_status = 0;
  2341. uint32_t result_dump_status;
  2342. struct ce_request_info *preq_info;
  2343. struct ce_sps_data *pce_sps_data;
  2344. qce_comp_func_ptr_t qce_callback;
  2345. preq_info = &pce_dev->ce_request_info[req_info];
  2346. pce_sps_data = &preq_info->ce_sps;
  2347. qce_callback = preq_info->qce_cb;
  2348. areq = (struct skcipher_request *) preq_info->areq;
  2349. if (!is_offload_op(preq_info->offload_op)) {
  2350. if (areq->src != areq->dst)
  2351. qce_dma_unmap_sg(pce_dev->pdev, areq->dst,
  2352. preq_info->dst_nents, DMA_FROM_DEVICE);
  2353. qce_dma_unmap_sg(pce_dev->pdev, areq->src,
  2354. preq_info->src_nents,
  2355. (areq->src == areq->dst) ? DMA_BIDIRECTIONAL :
  2356. DMA_TO_DEVICE);
  2357. }
  2358. if (_qce_unlock_other_pipes(pce_dev, req_info)) {
  2359. qce_free_req_info(pce_dev, req_info, true);
  2360. qce_callback(areq, NULL, NULL, -ENXIO);
  2361. return -ENXIO;
  2362. }
  2363. result_dump_status = be32_to_cpu(pce_sps_data->result->status);
  2364. pce_sps_data->result->status = 0;
  2365. if (!is_offload_op(preq_info->offload_op)) {
  2366. if (result_dump_status & ((1 << CRYPTO_SW_ERR) |
  2367. (1 << CRYPTO_AXI_ERR) | (1 << CRYPTO_HSD_ERR))) {
  2368. pr_err("ablk_cipher operation error. Status %x\n",
  2369. result_dump_status);
  2370. result_status = -ENXIO;
  2371. }
  2372. }
  2373. if (pce_sps_data->consumer_status |
  2374. pce_sps_data->producer_status) {
  2375. pr_err("ablk_cipher sps operation error. sps status %x %x\n",
  2376. pce_sps_data->consumer_status,
  2377. pce_sps_data->producer_status);
  2378. result_status = -ENXIO;
  2379. }
  2380. if (preq_info->mode == QCE_MODE_ECB) {
  2381. qce_free_req_info(pce_dev, req_info, true);
  2382. qce_callback(areq, NULL, NULL, pce_sps_data->consumer_status |
  2383. result_status);
  2384. } else {
  2385. if (pce_dev->ce_bam_info.minor_version == 0) {
  2386. if (preq_info->mode == QCE_MODE_CBC) {
  2387. if (preq_info->dir == QCE_DECRYPT)
  2388. memcpy(iv, (char *)preq_info->dec_iv,
  2389. sizeof(iv));
  2390. else
  2391. memcpy(iv, (unsigned char *)
  2392. (sg_virt(areq->src) +
  2393. areq->src->length - 16),
  2394. sizeof(iv));
  2395. }
  2396. if ((preq_info->mode == QCE_MODE_CTR) ||
  2397. (preq_info->mode == QCE_MODE_XTS)) {
  2398. uint32_t num_blk = 0;
  2399. uint32_t cntr_iv3 = 0;
  2400. unsigned long long cntr_iv64 = 0;
  2401. unsigned char *b = (unsigned char *)(&cntr_iv3);
  2402. memcpy(iv, areq->iv, sizeof(iv));
  2403. if (preq_info->mode != QCE_MODE_XTS)
  2404. num_blk = areq->cryptlen/16;
  2405. else
  2406. num_blk = 1;
  2407. cntr_iv3 = ((*(iv + 12) << 24) & 0xff000000) |
  2408. (((*(iv + 13)) << 16) & 0xff0000) |
  2409. (((*(iv + 14)) << 8) & 0xff00) |
  2410. (*(iv + 15) & 0xff);
  2411. cntr_iv64 =
  2412. (((unsigned long long)cntr_iv3 &
  2413. 0xFFFFFFFFULL) +
  2414. (unsigned long long)num_blk) %
  2415. (unsigned long long)(0x100000000ULL);
  2416. cntr_iv3 = (u32)(cntr_iv64 & 0xFFFFFFFF);
  2417. *(iv + 15) = (char)(*b);
  2418. *(iv + 14) = (char)(*(b + 1));
  2419. *(iv + 13) = (char)(*(b + 2));
  2420. *(iv + 12) = (char)(*(b + 3));
  2421. }
  2422. } else {
  2423. memcpy(iv,
  2424. (char *)(pce_sps_data->result->encr_cntr_iv),
  2425. sizeof(iv));
  2426. }
  2427. if (!atomic_read(&preq_info->in_use)) {
  2428. pr_err("request information %d already done\n", req_info);
  2429. return -ENXIO;
  2430. }
  2431. qce_free_req_info(pce_dev, req_info, true);
  2432. qce_callback(areq, NULL, iv, result_status);
  2433. }
  2434. return 0;
  2435. }
  2436. static int _f8_complete(struct qce_device *pce_dev, int req_info)
  2437. {
  2438. int32_t result_status = 0;
  2439. uint32_t result_dump_status;
  2440. uint32_t result_dump_status2;
  2441. struct ce_request_info *preq_info;
  2442. struct ce_sps_data *pce_sps_data;
  2443. qce_comp_func_ptr_t qce_callback;
  2444. void *areq;
  2445. preq_info = &pce_dev->ce_request_info[req_info];
  2446. pce_sps_data = &preq_info->ce_sps;
  2447. qce_callback = preq_info->qce_cb;
  2448. areq = preq_info->areq;
  2449. if (preq_info->phy_ota_dst)
  2450. dma_unmap_single(pce_dev->pdev, preq_info->phy_ota_dst,
  2451. preq_info->ota_size, DMA_FROM_DEVICE);
  2452. if (preq_info->phy_ota_src)
  2453. dma_unmap_single(pce_dev->pdev, preq_info->phy_ota_src,
  2454. preq_info->ota_size, (preq_info->phy_ota_dst) ?
  2455. DMA_TO_DEVICE : DMA_BIDIRECTIONAL);
  2456. if (_qce_unlock_other_pipes(pce_dev, req_info)) {
  2457. qce_free_req_info(pce_dev, req_info, true);
  2458. qce_callback(areq, NULL, NULL, -ENXIO);
  2459. return -ENXIO;
  2460. }
  2461. result_dump_status = be32_to_cpu(pce_sps_data->result->status);
  2462. result_dump_status2 = be32_to_cpu(pce_sps_data->result->status2);
  2463. if ((result_dump_status & ((1 << CRYPTO_SW_ERR) | (1 << CRYPTO_AXI_ERR)
  2464. | (1 << CRYPTO_HSD_ERR)))) {
  2465. pr_err(
  2466. "f8 oper error. Dump Sta %x Sta2 %x req %d\n",
  2467. result_dump_status, result_dump_status2, req_info);
  2468. result_status = -ENXIO;
  2469. } else if (pce_sps_data->consumer_status |
  2470. pce_sps_data->producer_status) {
  2471. pr_err("f8 sps operation error. sps status %x %x\n",
  2472. pce_sps_data->consumer_status,
  2473. pce_sps_data->producer_status);
  2474. result_status = -ENXIO;
  2475. }
  2476. pce_sps_data->result->status = 0;
  2477. pce_sps_data->result->status2 = 0;
  2478. qce_free_req_info(pce_dev, req_info, true);
  2479. qce_callback(areq, NULL, NULL, result_status);
  2480. return 0;
  2481. }
  2482. static void _qce_sps_iovec_count_init(struct qce_device *pce_dev, int req_info)
  2483. {
  2484. struct ce_sps_data *pce_sps_data = &pce_dev->ce_request_info[req_info]
  2485. .ce_sps;
  2486. pce_sps_data->in_transfer.iovec_count = 0;
  2487. pce_sps_data->out_transfer.iovec_count = 0;
  2488. }
  2489. static void _qce_set_flag(struct sps_transfer *sps_bam_pipe, uint32_t flag)
  2490. {
  2491. struct sps_iovec *iovec;
  2492. if (sps_bam_pipe->iovec_count == 0)
  2493. return;
  2494. iovec = sps_bam_pipe->iovec + (sps_bam_pipe->iovec_count - 1);
  2495. iovec->flags |= flag;
  2496. }
  2497. static int _qce_sps_add_data(dma_addr_t paddr, uint32_t len,
  2498. struct sps_transfer *sps_bam_pipe)
  2499. {
  2500. struct sps_iovec *iovec = sps_bam_pipe->iovec +
  2501. sps_bam_pipe->iovec_count;
  2502. uint32_t data_cnt;
  2503. while (len > 0) {
  2504. if (sps_bam_pipe->iovec_count == QCE_MAX_NUM_DSCR) {
  2505. pr_err("Num of descrptor %d exceed max (%d)\n",
  2506. sps_bam_pipe->iovec_count,
  2507. (uint32_t)QCE_MAX_NUM_DSCR);
  2508. return -ENOMEM;
  2509. }
  2510. if (len > SPS_MAX_PKT_SIZE)
  2511. data_cnt = SPS_MAX_PKT_SIZE;
  2512. else
  2513. data_cnt = len;
  2514. iovec->size = data_cnt;
  2515. iovec->addr = SPS_GET_LOWER_ADDR(paddr);
  2516. iovec->flags = SPS_GET_UPPER_ADDR(paddr);
  2517. sps_bam_pipe->iovec_count++;
  2518. iovec++;
  2519. paddr += data_cnt;
  2520. len -= data_cnt;
  2521. }
  2522. return 0;
  2523. }
  2524. static int _qce_sps_add_sg_data(struct qce_device *pce_dev,
  2525. struct scatterlist *sg_src, uint32_t nbytes,
  2526. struct sps_transfer *sps_bam_pipe)
  2527. {
  2528. uint32_t data_cnt, len;
  2529. dma_addr_t addr;
  2530. struct sps_iovec *iovec = sps_bam_pipe->iovec +
  2531. sps_bam_pipe->iovec_count;
  2532. while (nbytes > 0 && sg_src) {
  2533. len = min(nbytes, sg_dma_len(sg_src));
  2534. nbytes -= len;
  2535. addr = sg_dma_address(sg_src);
  2536. if (pce_dev->ce_bam_info.minor_version == 0)
  2537. len = ALIGN(len, pce_dev->ce_bam_info.ce_burst_size);
  2538. while (len > 0) {
  2539. if (sps_bam_pipe->iovec_count == QCE_MAX_NUM_DSCR) {
  2540. pr_err("Num of descrptor %d exceed max (%d)\n",
  2541. sps_bam_pipe->iovec_count,
  2542. (uint32_t)QCE_MAX_NUM_DSCR);
  2543. return -ENOMEM;
  2544. }
  2545. if (len > SPS_MAX_PKT_SIZE) {
  2546. data_cnt = SPS_MAX_PKT_SIZE;
  2547. iovec->size = data_cnt;
  2548. iovec->addr = SPS_GET_LOWER_ADDR(addr);
  2549. iovec->flags = SPS_GET_UPPER_ADDR(addr);
  2550. } else {
  2551. data_cnt = len;
  2552. iovec->size = data_cnt;
  2553. iovec->addr = SPS_GET_LOWER_ADDR(addr);
  2554. iovec->flags = SPS_GET_UPPER_ADDR(addr);
  2555. }
  2556. iovec++;
  2557. sps_bam_pipe->iovec_count++;
  2558. addr += data_cnt;
  2559. len -= data_cnt;
  2560. }
  2561. sg_src = sg_next(sg_src);
  2562. }
  2563. return 0;
  2564. }
  2565. static int _qce_sps_add_sg_data_off(struct qce_device *pce_dev,
  2566. struct scatterlist *sg_src, uint32_t nbytes, uint32_t off,
  2567. struct sps_transfer *sps_bam_pipe)
  2568. {
  2569. uint32_t data_cnt, len;
  2570. dma_addr_t addr;
  2571. struct sps_iovec *iovec = sps_bam_pipe->iovec +
  2572. sps_bam_pipe->iovec_count;
  2573. unsigned int res_within_sg;
  2574. if (!sg_src)
  2575. return -ENOENT;
  2576. res_within_sg = sg_dma_len(sg_src);
  2577. while (off > 0) {
  2578. if (!sg_src) {
  2579. pr_err("broken sg list off %d nbytes %d\n",
  2580. off, nbytes);
  2581. return -ENOENT;
  2582. }
  2583. len = sg_dma_len(sg_src);
  2584. if (off < len) {
  2585. res_within_sg = len - off;
  2586. break;
  2587. }
  2588. off -= len;
  2589. sg_src = sg_next(sg_src);
  2590. if (sg_src)
  2591. res_within_sg = sg_dma_len(sg_src);
  2592. }
  2593. while (nbytes > 0 && sg_src) {
  2594. len = min(nbytes, res_within_sg);
  2595. nbytes -= len;
  2596. addr = sg_dma_address(sg_src) + off;
  2597. if (pce_dev->ce_bam_info.minor_version == 0)
  2598. len = ALIGN(len, pce_dev->ce_bam_info.ce_burst_size);
  2599. while (len > 0) {
  2600. if (sps_bam_pipe->iovec_count == QCE_MAX_NUM_DSCR) {
  2601. pr_err("Num of descrptor %d exceed max (%d)\n",
  2602. sps_bam_pipe->iovec_count,
  2603. (uint32_t)QCE_MAX_NUM_DSCR);
  2604. return -ENOMEM;
  2605. }
  2606. if (len > SPS_MAX_PKT_SIZE) {
  2607. data_cnt = SPS_MAX_PKT_SIZE;
  2608. iovec->size = data_cnt;
  2609. iovec->addr = SPS_GET_LOWER_ADDR(addr);
  2610. iovec->flags = SPS_GET_UPPER_ADDR(addr);
  2611. } else {
  2612. data_cnt = len;
  2613. iovec->size = data_cnt;
  2614. iovec->addr = SPS_GET_LOWER_ADDR(addr);
  2615. iovec->flags = SPS_GET_UPPER_ADDR(addr);
  2616. }
  2617. iovec++;
  2618. sps_bam_pipe->iovec_count++;
  2619. addr += data_cnt;
  2620. len -= data_cnt;
  2621. }
  2622. if (nbytes) {
  2623. sg_src = sg_next(sg_src);
  2624. if (!sg_src) {
  2625. pr_err("more data bytes %d\n", nbytes);
  2626. return -ENOMEM;
  2627. }
  2628. res_within_sg = sg_dma_len(sg_src);
  2629. off = 0;
  2630. }
  2631. }
  2632. return 0;
  2633. }
  2634. static int _qce_sps_add_cmd(struct qce_device *pce_dev, uint32_t flag,
  2635. struct qce_cmdlist_info *cmdptr,
  2636. struct sps_transfer *sps_bam_pipe)
  2637. {
  2638. dma_addr_t paddr = GET_PHYS_ADDR(cmdptr->cmdlist);
  2639. struct sps_iovec *iovec = sps_bam_pipe->iovec +
  2640. sps_bam_pipe->iovec_count;
  2641. iovec->size = cmdptr->size;
  2642. iovec->addr = SPS_GET_LOWER_ADDR(paddr);
  2643. iovec->flags = SPS_GET_UPPER_ADDR(paddr) | SPS_IOVEC_FLAG_CMD | flag;
  2644. sps_bam_pipe->iovec_count++;
  2645. if (sps_bam_pipe->iovec_count >= QCE_MAX_NUM_DSCR) {
  2646. pr_err("Num of descrptor %d exceed max (%d)\n",
  2647. sps_bam_pipe->iovec_count, (uint32_t)QCE_MAX_NUM_DSCR);
  2648. return -ENOMEM;
  2649. }
  2650. return 0;
  2651. }
  2652. static int _qce_sps_transfer(struct qce_device *pce_dev, int req_info)
  2653. {
  2654. int rc = 0;
  2655. struct ce_sps_data *pce_sps_data;
  2656. uint16_t op = pce_dev->ce_request_info[req_info].offload_op;
  2657. pce_sps_data = &pce_dev->ce_request_info[req_info].ce_sps;
  2658. pce_sps_data->out_transfer.user =
  2659. (void *)((uintptr_t)(CRYPTO_REQ_USER_PAT |
  2660. (unsigned int) req_info));
  2661. pce_sps_data->in_transfer.user =
  2662. (void *)((uintptr_t)(CRYPTO_REQ_USER_PAT |
  2663. (unsigned int) req_info));
  2664. _qce_dump_descr_fifos_dbg(pce_dev, req_info);
  2665. if (pce_sps_data->in_transfer.iovec_count) {
  2666. rc = sps_transfer(pce_dev->ce_bam_info.consumer[op].pipe,
  2667. &pce_sps_data->in_transfer);
  2668. if (rc) {
  2669. pr_err("sps_xfr() fail (cons pipe=0x%lx) rc = %d\n",
  2670. (uintptr_t)pce_dev->ce_bam_info.consumer[op].pipe,
  2671. rc);
  2672. goto ret;
  2673. }
  2674. }
  2675. rc = sps_transfer(pce_dev->ce_bam_info.producer[op].pipe,
  2676. &pce_sps_data->out_transfer);
  2677. if (rc)
  2678. pr_err("sps_xfr() fail (producer pipe=0x%lx) rc = %d\n",
  2679. (uintptr_t)pce_dev->ce_bam_info.producer[op].pipe, rc);
  2680. ret:
  2681. if (rc)
  2682. _qce_dump_descr_fifos(pce_dev, req_info);
  2683. return rc;
  2684. }
  2685. /**
  2686. * Allocate and Connect a CE peripheral's SPS endpoint
  2687. *
  2688. * This function allocates endpoint context and
  2689. * connect it with memory endpoint by calling
  2690. * appropriate SPS driver APIs.
  2691. *
  2692. * Also registers a SPS callback function with
  2693. * SPS driver
  2694. *
  2695. * This function should only be called once typically
  2696. * during driver probe.
  2697. *
  2698. * @pce_dev - Pointer to qce_device structure
  2699. * @ep - Pointer to sps endpoint data structure
  2700. * @index - Points to crypto use case
  2701. * @is_produce - 1 means Producer endpoint
  2702. * 0 means Consumer endpoint
  2703. *
  2704. * @return - 0 if successful else negative value.
  2705. *
  2706. */
  2707. static int qce_sps_init_ep_conn(struct qce_device *pce_dev,
  2708. struct qce_sps_ep_conn_data *ep,
  2709. int index,
  2710. bool is_producer)
  2711. {
  2712. int rc = 0;
  2713. struct sps_pipe *sps_pipe_info;
  2714. struct sps_connect *sps_connect_info = &ep->connect;
  2715. struct sps_register_event *sps_event = &ep->event;
  2716. /* Allocate endpoint context */
  2717. sps_pipe_info = sps_alloc_endpoint();
  2718. if (!sps_pipe_info) {
  2719. pr_err("sps_alloc_endpoint() failed!!! is_producer=%d\n",
  2720. is_producer);
  2721. rc = -ENOMEM;
  2722. goto out;
  2723. }
  2724. /* Now save the sps pipe handle */
  2725. ep->pipe = sps_pipe_info;
  2726. /* Get default connection configuration for an endpoint */
  2727. rc = sps_get_config(sps_pipe_info, sps_connect_info);
  2728. if (rc) {
  2729. pr_err("sps_get_config() fail pipe_handle=0x%lx, rc = %d\n",
  2730. (uintptr_t)sps_pipe_info, rc);
  2731. goto get_config_err;
  2732. }
  2733. /* Modify the default connection configuration */
  2734. if (is_producer) {
  2735. /*
  2736. * For CE producer transfer, source should be
  2737. * CE peripheral where as destination should
  2738. * be system memory.
  2739. */
  2740. sps_connect_info->source = pce_dev->ce_bam_info.bam_handle;
  2741. sps_connect_info->destination = SPS_DEV_HANDLE_MEM;
  2742. /* Producer pipe will handle this connection */
  2743. sps_connect_info->mode = SPS_MODE_SRC;
  2744. sps_connect_info->options =
  2745. SPS_O_AUTO_ENABLE | SPS_O_DESC_DONE;
  2746. } else {
  2747. /* For CE consumer transfer, source should be
  2748. * system memory where as destination should
  2749. * CE peripheral
  2750. */
  2751. sps_connect_info->source = SPS_DEV_HANDLE_MEM;
  2752. sps_connect_info->destination = pce_dev->ce_bam_info.bam_handle;
  2753. sps_connect_info->mode = SPS_MODE_DEST;
  2754. sps_connect_info->options =
  2755. SPS_O_AUTO_ENABLE;
  2756. }
  2757. /* Producer pipe index */
  2758. sps_connect_info->src_pipe_index =
  2759. pce_dev->ce_bam_info.src_pipe_index[index];
  2760. /* Consumer pipe index */
  2761. sps_connect_info->dest_pipe_index =
  2762. pce_dev->ce_bam_info.dest_pipe_index[index];
  2763. /* Set pipe group */
  2764. sps_connect_info->lock_group =
  2765. pce_dev->ce_bam_info.pipe_pair_index[index];
  2766. sps_connect_info->event_thresh = 0x10;
  2767. /*
  2768. * Max. no of scatter/gather buffers that can
  2769. * be passed by block layer = 32 (NR_SG).
  2770. * Each BAM descritor needs 64 bits (8 bytes).
  2771. * One BAM descriptor is required per buffer transfer.
  2772. * So we would require total 256 (32 * 8) bytes of descriptor FIFO.
  2773. * But due to HW limitation we need to allocate atleast one extra
  2774. * descriptor memory (256 bytes + 8 bytes). But in order to be
  2775. * in power of 2, we are allocating 512 bytes of memory.
  2776. */
  2777. sps_connect_info->desc.size = QCE_MAX_NUM_DSCR * MAX_QCE_ALLOC_BAM_REQ *
  2778. sizeof(struct sps_iovec);
  2779. if (sps_connect_info->desc.size > MAX_SPS_DESC_FIFO_SIZE)
  2780. sps_connect_info->desc.size = MAX_SPS_DESC_FIFO_SIZE;
  2781. sps_connect_info->desc.base = dma_alloc_coherent(pce_dev->pdev,
  2782. sps_connect_info->desc.size,
  2783. &sps_connect_info->desc.phys_base,
  2784. GFP_KERNEL | __GFP_ZERO);
  2785. if (sps_connect_info->desc.base == NULL) {
  2786. rc = -ENOMEM;
  2787. pr_err("Can not allocate coherent memory for sps data\n");
  2788. goto get_config_err;
  2789. }
  2790. /* Establish connection between peripheral and memory endpoint */
  2791. rc = sps_connect(sps_pipe_info, sps_connect_info);
  2792. if (rc) {
  2793. pr_err("sps_connect() fail pipe_handle=0x%lx, rc = %d\n",
  2794. (uintptr_t)sps_pipe_info, rc);
  2795. goto sps_connect_err;
  2796. }
  2797. sps_event->mode = SPS_TRIGGER_CALLBACK;
  2798. sps_event->xfer_done = NULL;
  2799. sps_event->user = (void *)pce_dev;
  2800. if (is_producer) {
  2801. sps_event->options = SPS_O_EOT | SPS_O_DESC_DONE;
  2802. sps_event->callback = _sps_producer_callback;
  2803. rc = sps_register_event(ep->pipe, sps_event);
  2804. if (rc) {
  2805. pr_err("Producer callback registration failed rc=%d\n",
  2806. rc);
  2807. goto sps_connect_err;
  2808. }
  2809. } else {
  2810. sps_event->options = SPS_O_EOT;
  2811. sps_event->callback = NULL;
  2812. }
  2813. pr_debug("success, %s : pipe_handle=0x%lx, desc fifo base (phy) = 0x%pK\n",
  2814. is_producer ? "PRODUCER(RX/OUT)" : "CONSUMER(TX/IN)",
  2815. (uintptr_t)sps_pipe_info, &sps_connect_info->desc.phys_base);
  2816. goto out;
  2817. sps_connect_err:
  2818. dma_free_coherent(pce_dev->pdev,
  2819. sps_connect_info->desc.size,
  2820. sps_connect_info->desc.base,
  2821. sps_connect_info->desc.phys_base);
  2822. get_config_err:
  2823. sps_free_endpoint(sps_pipe_info);
  2824. out:
  2825. return rc;
  2826. }
  2827. /**
  2828. * Disconnect and Deallocate a CE peripheral's SPS endpoint
  2829. *
  2830. * This function disconnect endpoint and deallocates
  2831. * endpoint context.
  2832. *
  2833. * This function should only be called once typically
  2834. * during driver remove.
  2835. *
  2836. * @pce_dev - Pointer to qce_device structure
  2837. * @ep - Pointer to sps endpoint data structure
  2838. *
  2839. */
  2840. static void qce_sps_exit_ep_conn(struct qce_device *pce_dev,
  2841. struct qce_sps_ep_conn_data *ep)
  2842. {
  2843. struct sps_pipe *sps_pipe_info = ep->pipe;
  2844. struct sps_connect *sps_connect_info = &ep->connect;
  2845. sps_disconnect(sps_pipe_info);
  2846. dma_free_coherent(pce_dev->pdev,
  2847. sps_connect_info->desc.size,
  2848. sps_connect_info->desc.base,
  2849. sps_connect_info->desc.phys_base);
  2850. sps_free_endpoint(sps_pipe_info);
  2851. }
  2852. static void qce_sps_release_bam(struct qce_device *pce_dev)
  2853. {
  2854. struct bam_registration_info *pbam;
  2855. mutex_lock(&bam_register_lock);
  2856. pbam = pce_dev->pbam;
  2857. if (pbam == NULL)
  2858. goto ret;
  2859. pbam->cnt--;
  2860. if (pbam->cnt > 0)
  2861. goto ret;
  2862. if (pce_dev->ce_bam_info.bam_handle) {
  2863. sps_deregister_bam_device(pce_dev->ce_bam_info.bam_handle);
  2864. pr_debug("deregister bam handle 0x%lx\n",
  2865. pce_dev->ce_bam_info.bam_handle);
  2866. pce_dev->ce_bam_info.bam_handle = 0;
  2867. }
  2868. iounmap(pbam->bam_iobase);
  2869. pr_debug("delete bam 0x%x\n", pbam->bam_mem);
  2870. list_del(&pbam->qlist);
  2871. kfree(pbam);
  2872. ret:
  2873. pce_dev->pbam = NULL;
  2874. mutex_unlock(&bam_register_lock);
  2875. }
  2876. static int qce_sps_get_bam(struct qce_device *pce_dev)
  2877. {
  2878. int rc = 0;
  2879. struct sps_bam_props bam = {0};
  2880. struct bam_registration_info *pbam = NULL;
  2881. struct bam_registration_info *p;
  2882. uint32_t bam_cfg = 0;
  2883. mutex_lock(&bam_register_lock);
  2884. list_for_each_entry(p, &qce50_bam_list, qlist) {
  2885. if (p->bam_mem == pce_dev->bam_mem) {
  2886. pbam = p; /* found */
  2887. break;
  2888. }
  2889. }
  2890. if (pbam) {
  2891. pr_debug("found bam 0x%x\n", pbam->bam_mem);
  2892. pbam->cnt++;
  2893. pce_dev->ce_bam_info.bam_handle = pbam->handle;
  2894. pce_dev->ce_bam_info.bam_mem = pbam->bam_mem;
  2895. pce_dev->ce_bam_info.bam_iobase = pbam->bam_iobase;
  2896. pce_dev->pbam = pbam;
  2897. pce_dev->support_cmd_dscr = pbam->support_cmd_dscr;
  2898. goto ret;
  2899. }
  2900. pbam = kzalloc(sizeof(struct bam_registration_info), GFP_KERNEL);
  2901. if (!pbam) {
  2902. rc = -ENOMEM;
  2903. goto ret;
  2904. }
  2905. pbam->cnt = 1;
  2906. pbam->bam_mem = pce_dev->bam_mem;
  2907. pbam->bam_iobase = ioremap(pce_dev->bam_mem,
  2908. pce_dev->bam_mem_size);
  2909. if (!pbam->bam_iobase) {
  2910. kfree(pbam);
  2911. rc = -ENOMEM;
  2912. pr_err("Can not map BAM io memory\n");
  2913. goto ret;
  2914. }
  2915. pce_dev->ce_bam_info.bam_mem = pbam->bam_mem;
  2916. pce_dev->ce_bam_info.bam_iobase = pbam->bam_iobase;
  2917. pbam->handle = 0;
  2918. pr_debug("allocate bam 0x%x\n", pbam->bam_mem);
  2919. bam_cfg = readl_relaxed(pce_dev->ce_bam_info.bam_iobase +
  2920. CRYPTO_BAM_CNFG_BITS_REG);
  2921. pbam->support_cmd_dscr = (bam_cfg & CRYPTO_BAM_CD_ENABLE_MASK) ?
  2922. true : false;
  2923. if (!pbam->support_cmd_dscr) {
  2924. pr_info("qce50 don't support command descriptor. bam_cfg%x\n",
  2925. bam_cfg);
  2926. pce_dev->no_get_around = false;
  2927. }
  2928. pce_dev->support_cmd_dscr = pbam->support_cmd_dscr;
  2929. bam.phys_addr = pce_dev->ce_bam_info.bam_mem;
  2930. bam.virt_addr = pce_dev->ce_bam_info.bam_iobase;
  2931. /*
  2932. * This event threshold value is only significant for BAM-to-BAM
  2933. * transfer. It's ignored for BAM-to-System mode transfer.
  2934. */
  2935. bam.event_threshold = 0x10; /* Pipe event threshold */
  2936. /*
  2937. * This threshold controls when the BAM publish
  2938. * the descriptor size on the sideband interface.
  2939. * SPS HW will only be used when
  2940. * data transfer size > 64 bytes.
  2941. */
  2942. bam.summing_threshold = 64;
  2943. /* SPS driver wll handle the crypto BAM IRQ */
  2944. bam.irq = (u32)pce_dev->ce_bam_info.bam_irq;
  2945. /*
  2946. * Set flag to indicate BAM global device control is managed
  2947. * remotely.
  2948. */
  2949. if (!pce_dev->support_cmd_dscr || pce_dev->is_shared)
  2950. bam.manage = SPS_BAM_MGR_DEVICE_REMOTE;
  2951. else
  2952. bam.manage = SPS_BAM_MGR_LOCAL;
  2953. bam.ee = pce_dev->ce_bam_info.bam_ee;
  2954. bam.ipc_loglevel = QCE_BAM_DEFAULT_IPC_LOGLVL;
  2955. bam.options |= SPS_BAM_CACHED_WP;
  2956. pr_debug("bam physical base=0x%lx\n", (uintptr_t)bam.phys_addr);
  2957. pr_debug("bam virtual base=0x%pK\n", bam.virt_addr);
  2958. /* Register CE Peripheral BAM device to SPS driver */
  2959. rc = sps_register_bam_device(&bam, &pbam->handle);
  2960. if (rc) {
  2961. pr_err("sps_register_bam_device() failed! err=%d\n", rc);
  2962. rc = -EIO;
  2963. iounmap(pbam->bam_iobase);
  2964. kfree(pbam);
  2965. goto ret;
  2966. }
  2967. pce_dev->pbam = pbam;
  2968. list_add_tail(&pbam->qlist, &qce50_bam_list);
  2969. pce_dev->ce_bam_info.bam_handle = pbam->handle;
  2970. ret:
  2971. mutex_unlock(&bam_register_lock);
  2972. return rc;
  2973. }
  2974. /**
  2975. * Initialize SPS HW connected with CE core
  2976. *
  2977. * This function register BAM HW resources with
  2978. * SPS driver and then initialize 2 SPS endpoints
  2979. *
  2980. * This function should only be called once typically
  2981. * during driver probe.
  2982. *
  2983. * @pce_dev - Pointer to qce_device structure
  2984. *
  2985. * @return - 0 if successful else negative value.
  2986. *
  2987. */
  2988. static int qce_sps_init(struct qce_device *pce_dev)
  2989. {
  2990. int rc = 0, i = 0;
  2991. rc = qce_sps_get_bam(pce_dev);
  2992. if (rc)
  2993. return rc;
  2994. pr_debug("BAM device registered. bam_handle=0x%lx\n",
  2995. pce_dev->ce_bam_info.bam_handle);
  2996. for (i = 0; i < QCE_OFFLOAD_OPER_LAST; i++) {
  2997. if (i == QCE_OFFLOAD_NONE && !(pce_dev->kernel_pipes_support))
  2998. continue;
  2999. else if ((i > 0) && !(pce_dev->offload_pipes_support))
  3000. break;
  3001. rc = qce_sps_init_ep_conn(pce_dev,
  3002. &pce_dev->ce_bam_info.producer[i], i, true);
  3003. if (rc)
  3004. goto sps_connect_producer_err;
  3005. rc = qce_sps_init_ep_conn(pce_dev,
  3006. &pce_dev->ce_bam_info.consumer[i], i, false);
  3007. if (rc)
  3008. goto sps_connect_consumer_err;
  3009. }
  3010. pr_info(" QTI MSM CE-BAM at 0x%016llx irq %d\n",
  3011. (unsigned long long)pce_dev->ce_bam_info.bam_mem,
  3012. (unsigned int)pce_dev->ce_bam_info.bam_irq);
  3013. return rc;
  3014. sps_connect_consumer_err:
  3015. qce_sps_exit_ep_conn(pce_dev, &pce_dev->ce_bam_info.producer[i]);
  3016. sps_connect_producer_err:
  3017. qce_sps_release_bam(pce_dev);
  3018. return rc;
  3019. }
  3020. static inline int qce_alloc_req_info(struct qce_device *pce_dev)
  3021. {
  3022. int i;
  3023. int request_index = pce_dev->ce_request_index;
  3024. for (i = 0; i < MAX_QCE_BAM_REQ; i++) {
  3025. request_index++;
  3026. if (request_index >= MAX_QCE_BAM_REQ)
  3027. request_index = 0;
  3028. if (!atomic_xchg(
  3029. &pce_dev->ce_request_info[request_index].in_use,
  3030. true)) {
  3031. pce_dev->ce_request_index = request_index;
  3032. return request_index;
  3033. }
  3034. }
  3035. pr_warn("pcedev %d no reqs available no_of_queued_req %d\n",
  3036. pce_dev->dev_no, atomic_read(
  3037. &pce_dev->no_of_queued_req));
  3038. return -EBUSY;
  3039. }
  3040. static inline void qce_free_req_info(struct qce_device *pce_dev, int req_info,
  3041. bool is_complete)
  3042. {
  3043. pce_dev->ce_request_info[req_info].xfer_type = QCE_XFER_TYPE_LAST;
  3044. if (atomic_xchg(&pce_dev->ce_request_info[req_info].in_use,
  3045. false)) {
  3046. if (req_info < MAX_QCE_BAM_REQ && is_complete)
  3047. atomic_dec(&pce_dev->no_of_queued_req);
  3048. } else
  3049. pr_warn("request info %d free already\n", req_info);
  3050. }
  3051. static void print_notify_debug(struct sps_event_notify *notify)
  3052. {
  3053. phys_addr_t addr =
  3054. DESC_FULL_ADDR((phys_addr_t) notify->data.transfer.iovec.flags,
  3055. notify->data.transfer.iovec.addr);
  3056. pr_debug("sps ev_id=%d, addr=0x%pa, size=0x%x, flags=0x%x user=0x%pK\n",
  3057. notify->event_id, &addr,
  3058. notify->data.transfer.iovec.size,
  3059. notify->data.transfer.iovec.flags,
  3060. notify->data.transfer.user);
  3061. }
  3062. static void _qce_req_complete(struct qce_device *pce_dev, unsigned int req_info)
  3063. {
  3064. struct ce_request_info *preq_info;
  3065. preq_info = &pce_dev->ce_request_info[req_info];
  3066. switch (preq_info->xfer_type) {
  3067. case QCE_XFER_CIPHERING:
  3068. _ablk_cipher_complete(pce_dev, req_info);
  3069. break;
  3070. case QCE_XFER_HASHING:
  3071. _sha_complete(pce_dev, req_info);
  3072. break;
  3073. case QCE_XFER_AEAD:
  3074. _aead_complete(pce_dev, req_info);
  3075. break;
  3076. case QCE_XFER_F8:
  3077. _f8_complete(pce_dev, req_info);
  3078. break;
  3079. case QCE_XFER_F9:
  3080. _f9_complete(pce_dev, req_info);
  3081. break;
  3082. default:
  3083. qce_free_req_info(pce_dev, req_info, true);
  3084. break;
  3085. }
  3086. }
  3087. static void qce_multireq_timeout(struct timer_list *data)
  3088. {
  3089. struct qce_device *pce_dev = from_timer(pce_dev, data, timer);
  3090. int ret = 0;
  3091. int last_seq;
  3092. unsigned long flags;
  3093. last_seq = atomic_read(&pce_dev->bunch_cmd_seq);
  3094. if (last_seq == 0 ||
  3095. last_seq != atomic_read(&pce_dev->last_intr_seq)) {
  3096. atomic_set(&pce_dev->last_intr_seq, last_seq);
  3097. mod_timer(&(pce_dev->timer), (jiffies + DELAY_IN_JIFFIES));
  3098. return;
  3099. }
  3100. /* last bunch mode command time out */
  3101. /*
  3102. * From here to dummy request finish sps request and set owner back
  3103. * to none, we disable interrupt.
  3104. * So it won't get preempted or interrupted. If bam inerrupts happen
  3105. * between, and completion callback gets called from BAM, a new
  3106. * request may be issued by the client driver. Deadlock may happen.
  3107. */
  3108. local_irq_save(flags);
  3109. if (cmpxchg(&pce_dev->owner, QCE_OWNER_NONE, QCE_OWNER_TIMEOUT)
  3110. != QCE_OWNER_NONE) {
  3111. local_irq_restore(flags);
  3112. mod_timer(&(pce_dev->timer), (jiffies + DELAY_IN_JIFFIES));
  3113. return;
  3114. }
  3115. ret = qce_dummy_req(pce_dev);
  3116. if (ret)
  3117. pr_warn("pcedev %d: Failed to insert dummy req\n",
  3118. pce_dev->dev_no);
  3119. cmpxchg(&pce_dev->owner, QCE_OWNER_TIMEOUT, QCE_OWNER_NONE);
  3120. pce_dev->mode = IN_INTERRUPT_MODE;
  3121. local_irq_restore(flags);
  3122. del_timer(&(pce_dev->timer));
  3123. pce_dev->qce_stats.no_of_timeouts++;
  3124. pr_debug("pcedev %d mode switch to INTR\n", pce_dev->dev_no);
  3125. }
  3126. void qce_get_driver_stats(void *handle)
  3127. {
  3128. struct qce_device *pce_dev = (struct qce_device *) handle;
  3129. if (!_qce50_disp_stats)
  3130. return;
  3131. pr_info("Engine %d timeout occuured %d\n", pce_dev->dev_no,
  3132. pce_dev->qce_stats.no_of_timeouts);
  3133. pr_info("Engine %d dummy request inserted %d\n", pce_dev->dev_no,
  3134. pce_dev->qce_stats.no_of_dummy_reqs);
  3135. if (pce_dev->mode)
  3136. pr_info("Engine %d is in BUNCH MODE\n", pce_dev->dev_no);
  3137. else
  3138. pr_info("Engine %d is in INTERRUPT MODE\n", pce_dev->dev_no);
  3139. pr_info("Engine %d outstanding request %d\n", pce_dev->dev_no,
  3140. atomic_read(&pce_dev->no_of_queued_req));
  3141. }
  3142. EXPORT_SYMBOL(qce_get_driver_stats);
  3143. void qce_clear_driver_stats(void *handle)
  3144. {
  3145. struct qce_device *pce_dev = (struct qce_device *) handle;
  3146. pce_dev->qce_stats.no_of_timeouts = 0;
  3147. pce_dev->qce_stats.no_of_dummy_reqs = 0;
  3148. }
  3149. EXPORT_SYMBOL(qce_clear_driver_stats);
  3150. static void _sps_producer_callback(struct sps_event_notify *notify)
  3151. {
  3152. struct qce_device *pce_dev = (struct qce_device *)
  3153. ((struct sps_event_notify *)notify)->user;
  3154. int rc = 0;
  3155. unsigned int req_info;
  3156. struct ce_sps_data *pce_sps_data;
  3157. struct ce_request_info *preq_info;
  3158. uint16_t op;
  3159. print_notify_debug(notify);
  3160. req_info = (unsigned int)((uintptr_t)notify->data.transfer.user);
  3161. if ((req_info & 0xffff0000) != CRYPTO_REQ_USER_PAT) {
  3162. pr_warn("request information %d out of range\n", req_info);
  3163. return;
  3164. }
  3165. req_info = req_info & 0x00ff;
  3166. if (req_info < 0 || req_info >= MAX_QCE_ALLOC_BAM_REQ) {
  3167. pr_warn("request information %d out of range\n", req_info);
  3168. return;
  3169. }
  3170. preq_info = &pce_dev->ce_request_info[req_info];
  3171. if (!atomic_read(&preq_info->in_use)) {
  3172. pr_err("request information %d already done\n", req_info);
  3173. return;
  3174. }
  3175. op = pce_dev->ce_request_info[req_info].offload_op;
  3176. pce_sps_data = &preq_info->ce_sps;
  3177. if ((preq_info->xfer_type == QCE_XFER_CIPHERING ||
  3178. preq_info->xfer_type == QCE_XFER_AEAD) &&
  3179. pce_sps_data->producer_state == QCE_PIPE_STATE_IDLE) {
  3180. pce_sps_data->producer_state = QCE_PIPE_STATE_COMP;
  3181. if (!is_offload_op(op) && (op < QCE_OFFLOAD_OPER_LAST)) {
  3182. pce_sps_data->out_transfer.iovec_count = 0;
  3183. _qce_sps_add_data(GET_PHYS_ADDR(
  3184. pce_sps_data->result_dump),
  3185. CRYPTO_RESULT_DUMP_SIZE,
  3186. &pce_sps_data->out_transfer);
  3187. _qce_set_flag(&pce_sps_data->out_transfer,
  3188. SPS_IOVEC_FLAG_INT);
  3189. rc = sps_transfer(
  3190. pce_dev->ce_bam_info.producer[op].pipe,
  3191. &pce_sps_data->out_transfer);
  3192. if (rc) {
  3193. pr_err("sps_xfr fail (prod pipe=0x%lx) rc = %d\n",
  3194. (uintptr_t)pce_dev->ce_bam_info.producer[op].pipe,
  3195. rc);
  3196. }
  3197. }
  3198. return;
  3199. }
  3200. _qce_req_complete(pce_dev, req_info);
  3201. }
  3202. /**
  3203. * De-initialize SPS HW connected with CE core
  3204. *
  3205. * This function deinitialize SPS endpoints and then
  3206. * deregisters BAM resources from SPS driver.
  3207. *
  3208. * This function should only be called once typically
  3209. * during driver remove.
  3210. *
  3211. * @pce_dev - Pointer to qce_device structure
  3212. *
  3213. */
  3214. static void qce_sps_exit(struct qce_device *pce_dev)
  3215. {
  3216. int i = 0;
  3217. for (i = 0; i < QCE_OFFLOAD_OPER_LAST; i++) {
  3218. if (i == QCE_OFFLOAD_NONE && !(pce_dev->kernel_pipes_support))
  3219. continue;
  3220. else if ((i > 0) && !(pce_dev->offload_pipes_support))
  3221. break;
  3222. qce_sps_exit_ep_conn(pce_dev,
  3223. &pce_dev->ce_bam_info.consumer[i]);
  3224. qce_sps_exit_ep_conn(pce_dev,
  3225. &pce_dev->ce_bam_info.producer[i]);
  3226. }
  3227. qce_sps_release_bam(pce_dev);
  3228. }
  3229. static void qce_add_cmd_element(struct qce_device *pdev,
  3230. struct sps_command_element **cmd_ptr, u32 addr,
  3231. u32 data, struct sps_command_element **populate)
  3232. {
  3233. (*cmd_ptr)->addr = (uint32_t)(addr + pdev->phy_iobase);
  3234. (*cmd_ptr)->command = 0;
  3235. (*cmd_ptr)->data = data;
  3236. (*cmd_ptr)->mask = 0xFFFFFFFF;
  3237. (*cmd_ptr)->reserved = 0;
  3238. if (populate != NULL)
  3239. *populate = *cmd_ptr;
  3240. (*cmd_ptr)++;
  3241. }
  3242. static int _setup_cipher_aes_cmdlistptrs(struct qce_device *pdev, int cri_index,
  3243. unsigned char **pvaddr, enum qce_cipher_mode_enum mode,
  3244. bool key_128)
  3245. {
  3246. struct sps_command_element *ce_vaddr;
  3247. uintptr_t ce_vaddr_start;
  3248. struct qce_cmdlistptr_ops *cmdlistptr;
  3249. struct qce_cmdlist_info *pcl_info = NULL;
  3250. int i = 0;
  3251. uint32_t encr_cfg = 0;
  3252. uint32_t key_reg = 0;
  3253. uint32_t xts_key_reg = 0;
  3254. uint32_t iv_reg = 0;
  3255. cmdlistptr = &pdev->ce_request_info[cri_index].ce_sps.cmdlistptr;
  3256. *pvaddr = (unsigned char *)ALIGN(((uintptr_t)(*pvaddr)),
  3257. pdev->ce_bam_info.ce_burst_size);
  3258. ce_vaddr = (struct sps_command_element *)(*pvaddr);
  3259. ce_vaddr_start = (uintptr_t)(*pvaddr);
  3260. /*
  3261. * Designate chunks of the allocated memory to various
  3262. * command list pointers related to AES cipher operations defined
  3263. * in ce_cmdlistptrs_ops structure.
  3264. */
  3265. switch (mode) {
  3266. case QCE_MODE_CBC:
  3267. case QCE_MODE_CTR:
  3268. if (key_128) {
  3269. cmdlistptr->cipher_aes_128_cbc_ctr.cmdlist =
  3270. (uintptr_t)ce_vaddr;
  3271. pcl_info = &(cmdlistptr->cipher_aes_128_cbc_ctr);
  3272. if (mode == QCE_MODE_CBC)
  3273. encr_cfg = pdev->reg.encr_cfg_aes_cbc_128;
  3274. else
  3275. encr_cfg = pdev->reg.encr_cfg_aes_ctr_128;
  3276. iv_reg = 4;
  3277. key_reg = 4;
  3278. xts_key_reg = 0;
  3279. } else {
  3280. cmdlistptr->cipher_aes_256_cbc_ctr.cmdlist =
  3281. (uintptr_t)ce_vaddr;
  3282. pcl_info = &(cmdlistptr->cipher_aes_256_cbc_ctr);
  3283. if (mode == QCE_MODE_CBC)
  3284. encr_cfg = pdev->reg.encr_cfg_aes_cbc_256;
  3285. else
  3286. encr_cfg = pdev->reg.encr_cfg_aes_ctr_256;
  3287. iv_reg = 4;
  3288. key_reg = 8;
  3289. xts_key_reg = 0;
  3290. }
  3291. break;
  3292. case QCE_MODE_ECB:
  3293. if (key_128) {
  3294. cmdlistptr->cipher_aes_128_ecb.cmdlist =
  3295. (uintptr_t)ce_vaddr;
  3296. pcl_info = &(cmdlistptr->cipher_aes_128_ecb);
  3297. encr_cfg = pdev->reg.encr_cfg_aes_ecb_128;
  3298. iv_reg = 0;
  3299. key_reg = 4;
  3300. xts_key_reg = 0;
  3301. } else {
  3302. cmdlistptr->cipher_aes_256_ecb.cmdlist =
  3303. (uintptr_t)ce_vaddr;
  3304. pcl_info = &(cmdlistptr->cipher_aes_256_ecb);
  3305. encr_cfg = pdev->reg.encr_cfg_aes_ecb_256;
  3306. iv_reg = 0;
  3307. key_reg = 8;
  3308. xts_key_reg = 0;
  3309. }
  3310. break;
  3311. case QCE_MODE_XTS:
  3312. if (key_128) {
  3313. cmdlistptr->cipher_aes_128_xts.cmdlist =
  3314. (uintptr_t)ce_vaddr;
  3315. pcl_info = &(cmdlistptr->cipher_aes_128_xts);
  3316. encr_cfg = pdev->reg.encr_cfg_aes_xts_128;
  3317. iv_reg = 4;
  3318. key_reg = 4;
  3319. xts_key_reg = 4;
  3320. } else {
  3321. cmdlistptr->cipher_aes_256_xts.cmdlist =
  3322. (uintptr_t)ce_vaddr;
  3323. pcl_info = &(cmdlistptr->cipher_aes_256_xts);
  3324. encr_cfg = pdev->reg.encr_cfg_aes_xts_256;
  3325. iv_reg = 4;
  3326. key_reg = 8;
  3327. xts_key_reg = 8;
  3328. }
  3329. break;
  3330. default:
  3331. pr_err("Unknown mode of operation %d received, exiting now\n",
  3332. mode);
  3333. return -EINVAL;
  3334. break;
  3335. }
  3336. /* clear status register */
  3337. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_STATUS_REG, 0, NULL);
  3338. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_STATUS2_REG, 0, NULL);
  3339. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_STATUS3_REG, 0, NULL);
  3340. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_STATUS4_REG, 0, NULL);
  3341. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_STATUS5_REG, 0, NULL);
  3342. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_STATUS6_REG, 0, NULL);
  3343. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CONFIG_REG,
  3344. pdev->reg.crypto_cfg_be, &pcl_info->crypto_cfg);
  3345. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_SEG_SIZE_REG, 0,
  3346. &pcl_info->seg_size);
  3347. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_SEG_CFG_REG, encr_cfg,
  3348. &pcl_info->encr_seg_cfg);
  3349. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_SEG_SIZE_REG, 0,
  3350. &pcl_info->encr_seg_size);
  3351. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_SEG_START_REG, 0,
  3352. &pcl_info->encr_seg_start);
  3353. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CNTR_MASK_REG,
  3354. pdev->reg.encr_cntr_mask_3, &pcl_info->encr_mask_3);
  3355. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CNTR_MASK_REG2,
  3356. pdev->reg.encr_cntr_mask_2, &pcl_info->encr_mask_2);
  3357. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CNTR_MASK_REG1,
  3358. pdev->reg.encr_cntr_mask_1, &pcl_info->encr_mask_1);
  3359. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CNTR_MASK_REG0,
  3360. pdev->reg.encr_cntr_mask_0, &pcl_info->encr_mask_0);
  3361. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_SEG_CFG_REG, 0,
  3362. &pcl_info->auth_seg_cfg);
  3363. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_DATA_PATT_PROC_CFG_REG, 0,
  3364. &pcl_info->pattern_info);
  3365. qce_add_cmd_element(pdev, &ce_vaddr,
  3366. CRYPTO_DATA_PARTIAL_BLOCK_PROC_CFG_REG, 0,
  3367. &pcl_info->block_offset);
  3368. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_KEY0_REG, 0,
  3369. &pcl_info->encr_key);
  3370. for (i = 1; i < key_reg; i++)
  3371. qce_add_cmd_element(pdev, &ce_vaddr,
  3372. (CRYPTO_ENCR_KEY0_REG + i * sizeof(uint32_t)),
  3373. 0, NULL);
  3374. if (xts_key_reg) {
  3375. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_XTS_KEY0_REG,
  3376. 0, &pcl_info->encr_xts_key);
  3377. for (i = 1; i < xts_key_reg; i++)
  3378. qce_add_cmd_element(pdev, &ce_vaddr,
  3379. (CRYPTO_ENCR_XTS_KEY0_REG +
  3380. i * sizeof(uint32_t)), 0, NULL);
  3381. qce_add_cmd_element(pdev, &ce_vaddr,
  3382. CRYPTO_ENCR_XTS_DU_SIZE_REG, 0,
  3383. &pcl_info->encr_xts_du_size);
  3384. }
  3385. if (iv_reg) {
  3386. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CNTR0_IV0_REG, 0,
  3387. &pcl_info->encr_cntr_iv);
  3388. for (i = 1; i < iv_reg; i++)
  3389. qce_add_cmd_element(pdev, &ce_vaddr,
  3390. (CRYPTO_CNTR0_IV0_REG + i * sizeof(uint32_t)),
  3391. 0, NULL);
  3392. }
  3393. /* Add dummy to align size to burst-size multiple */
  3394. if (mode == QCE_MODE_XTS) {
  3395. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_SEG_SIZE_REG,
  3396. 0, &pcl_info->auth_seg_size);
  3397. } else {
  3398. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_SEG_SIZE_REG,
  3399. 0, &pcl_info->auth_seg_size);
  3400. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_SEG_START_REG,
  3401. 0, &pcl_info->auth_seg_size);
  3402. }
  3403. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CONFIG_REG,
  3404. pdev->reg.crypto_cfg_le, &pcl_info->crypto_cfg_le);
  3405. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_GOPROC_REG,
  3406. ((1 << CRYPTO_GO) | (1 << CRYPTO_RESULTS_DUMP) |
  3407. (1 << CRYPTO_CLR_CNTXT)), &pcl_info->go_proc);
  3408. pcl_info->size = (uintptr_t)ce_vaddr - (uintptr_t)ce_vaddr_start;
  3409. *pvaddr = (unsigned char *) ce_vaddr;
  3410. return 0;
  3411. }
  3412. static int _setup_cipher_des_cmdlistptrs(struct qce_device *pdev, int cri_index,
  3413. unsigned char **pvaddr, enum qce_cipher_alg_enum alg,
  3414. bool mode_cbc)
  3415. {
  3416. struct sps_command_element *ce_vaddr;
  3417. uintptr_t ce_vaddr_start;
  3418. struct qce_cmdlistptr_ops *cmdlistptr;
  3419. struct qce_cmdlist_info *pcl_info = NULL;
  3420. int i = 0;
  3421. uint32_t encr_cfg = 0;
  3422. uint32_t key_reg = 0;
  3423. uint32_t iv_reg = 0;
  3424. cmdlistptr = &pdev->ce_request_info[cri_index].ce_sps.cmdlistptr;
  3425. *pvaddr = (unsigned char *)ALIGN(((uintptr_t)(*pvaddr)),
  3426. pdev->ce_bam_info.ce_burst_size);
  3427. ce_vaddr = (struct sps_command_element *)(*pvaddr);
  3428. ce_vaddr_start = (uintptr_t)(*pvaddr);
  3429. /*
  3430. * Designate chunks of the allocated memory to various
  3431. * command list pointers related to cipher operations defined
  3432. * in ce_cmdlistptrs_ops structure.
  3433. */
  3434. switch (alg) {
  3435. case CIPHER_ALG_DES:
  3436. if (mode_cbc) {
  3437. cmdlistptr->cipher_des_cbc.cmdlist =
  3438. (uintptr_t)ce_vaddr;
  3439. pcl_info = &(cmdlistptr->cipher_des_cbc);
  3440. encr_cfg = pdev->reg.encr_cfg_des_cbc;
  3441. iv_reg = 2;
  3442. key_reg = 2;
  3443. } else {
  3444. cmdlistptr->cipher_des_ecb.cmdlist =
  3445. (uintptr_t)ce_vaddr;
  3446. pcl_info = &(cmdlistptr->cipher_des_ecb);
  3447. encr_cfg = pdev->reg.encr_cfg_des_ecb;
  3448. iv_reg = 0;
  3449. key_reg = 2;
  3450. }
  3451. break;
  3452. case CIPHER_ALG_3DES:
  3453. if (mode_cbc) {
  3454. cmdlistptr->cipher_3des_cbc.cmdlist =
  3455. (uintptr_t)ce_vaddr;
  3456. pcl_info = &(cmdlistptr->cipher_3des_cbc);
  3457. encr_cfg = pdev->reg.encr_cfg_3des_cbc;
  3458. iv_reg = 2;
  3459. key_reg = 6;
  3460. } else {
  3461. cmdlistptr->cipher_3des_ecb.cmdlist =
  3462. (uintptr_t)ce_vaddr;
  3463. pcl_info = &(cmdlistptr->cipher_3des_ecb);
  3464. encr_cfg = pdev->reg.encr_cfg_3des_ecb;
  3465. iv_reg = 0;
  3466. key_reg = 6;
  3467. }
  3468. break;
  3469. default:
  3470. pr_err("Unknown algorithms %d received, exiting now\n", alg);
  3471. return -EINVAL;
  3472. break;
  3473. }
  3474. /* clear status register */
  3475. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_STATUS_REG, 0, NULL);
  3476. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CONFIG_REG,
  3477. pdev->reg.crypto_cfg_be, &pcl_info->crypto_cfg);
  3478. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_SEG_SIZE_REG, 0,
  3479. &pcl_info->seg_size);
  3480. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_SEG_CFG_REG, encr_cfg,
  3481. &pcl_info->encr_seg_cfg);
  3482. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_SEG_SIZE_REG, 0,
  3483. &pcl_info->encr_seg_size);
  3484. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_SEG_START_REG, 0,
  3485. &pcl_info->encr_seg_start);
  3486. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_SEG_CFG_REG, 0,
  3487. &pcl_info->auth_seg_cfg);
  3488. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_KEY0_REG, 0,
  3489. &pcl_info->encr_key);
  3490. for (i = 1; i < key_reg; i++)
  3491. qce_add_cmd_element(pdev, &ce_vaddr,
  3492. (CRYPTO_ENCR_KEY0_REG + i * sizeof(uint32_t)),
  3493. 0, NULL);
  3494. if (iv_reg) {
  3495. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CNTR0_IV0_REG, 0,
  3496. &pcl_info->encr_cntr_iv);
  3497. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CNTR1_IV1_REG, 0,
  3498. NULL);
  3499. }
  3500. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CONFIG_REG,
  3501. pdev->reg.crypto_cfg_le, &pcl_info->crypto_cfg_le);
  3502. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_GOPROC_REG,
  3503. ((1 << CRYPTO_GO) | (1 << CRYPTO_RESULTS_DUMP) |
  3504. (1 << CRYPTO_CLR_CNTXT)), &pcl_info->go_proc);
  3505. pcl_info->size = (uintptr_t)ce_vaddr - (uintptr_t)ce_vaddr_start;
  3506. *pvaddr = (unsigned char *) ce_vaddr;
  3507. return 0;
  3508. }
  3509. static int _setup_cipher_null_cmdlistptrs(struct qce_device *pdev,
  3510. int cri_index, unsigned char **pvaddr)
  3511. {
  3512. struct sps_command_element *ce_vaddr;
  3513. uintptr_t ce_vaddr_start;
  3514. struct qce_cmdlistptr_ops *cmdlistptr = &pdev->ce_request_info
  3515. [cri_index].ce_sps.cmdlistptr;
  3516. struct qce_cmdlist_info *pcl_info = NULL;
  3517. *pvaddr = (unsigned char *)ALIGN(((uintptr_t)(*pvaddr)),
  3518. pdev->ce_bam_info.ce_burst_size);
  3519. ce_vaddr_start = (uintptr_t)(*pvaddr);
  3520. ce_vaddr = (struct sps_command_element *)(*pvaddr);
  3521. cmdlistptr->cipher_null.cmdlist = (uintptr_t)ce_vaddr;
  3522. pcl_info = &(cmdlistptr->cipher_null);
  3523. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_SEG_SIZE_REG,
  3524. pdev->ce_bam_info.ce_burst_size, NULL);
  3525. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_SEG_CFG_REG,
  3526. pdev->reg.encr_cfg_aes_ecb_128, NULL);
  3527. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_SEG_SIZE_REG, 0,
  3528. NULL);
  3529. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_SEG_START_REG, 0,
  3530. NULL);
  3531. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_SEG_CFG_REG,
  3532. 0, NULL);
  3533. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_SEG_SIZE_REG,
  3534. 0, NULL);
  3535. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_SEG_START_REG, 0,
  3536. NULL);
  3537. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_GOPROC_REG,
  3538. ((1 << CRYPTO_GO) | (1 << CRYPTO_RESULTS_DUMP) |
  3539. (1 << CRYPTO_CLR_CNTXT)), &pcl_info->go_proc);
  3540. pcl_info->size = (uintptr_t)ce_vaddr - (uintptr_t)ce_vaddr_start;
  3541. *pvaddr = (unsigned char *) ce_vaddr;
  3542. return 0;
  3543. }
  3544. static int _setup_auth_cmdlistptrs(struct qce_device *pdev, int cri_index,
  3545. unsigned char **pvaddr, enum qce_hash_alg_enum alg,
  3546. bool key_128)
  3547. {
  3548. struct sps_command_element *ce_vaddr;
  3549. uintptr_t ce_vaddr_start;
  3550. struct qce_cmdlistptr_ops *cmdlistptr;
  3551. struct qce_cmdlist_info *pcl_info = NULL;
  3552. int i = 0;
  3553. uint32_t key_reg = 0;
  3554. uint32_t auth_cfg = 0;
  3555. uint32_t iv_reg = 0;
  3556. cmdlistptr = &pdev->ce_request_info[cri_index].ce_sps.cmdlistptr;
  3557. *pvaddr = (unsigned char *)ALIGN(((uintptr_t)(*pvaddr)),
  3558. pdev->ce_bam_info.ce_burst_size);
  3559. ce_vaddr_start = (uintptr_t)(*pvaddr);
  3560. ce_vaddr = (struct sps_command_element *)(*pvaddr);
  3561. /*
  3562. * Designate chunks of the allocated memory to various
  3563. * command list pointers related to authentication operations
  3564. * defined in ce_cmdlistptrs_ops structure.
  3565. */
  3566. switch (alg) {
  3567. case QCE_HASH_SHA1:
  3568. cmdlistptr->auth_sha1.cmdlist = (uintptr_t)ce_vaddr;
  3569. pcl_info = &(cmdlistptr->auth_sha1);
  3570. auth_cfg = pdev->reg.auth_cfg_sha1;
  3571. iv_reg = 5;
  3572. /* clear status register */
  3573. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_STATUS_REG,
  3574. 0, NULL);
  3575. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CONFIG_REG,
  3576. pdev->reg.crypto_cfg_be, &pcl_info->crypto_cfg);
  3577. break;
  3578. case QCE_HASH_SHA256:
  3579. cmdlistptr->auth_sha256.cmdlist = (uintptr_t)ce_vaddr;
  3580. pcl_info = &(cmdlistptr->auth_sha256);
  3581. auth_cfg = pdev->reg.auth_cfg_sha256;
  3582. iv_reg = 8;
  3583. /* clear status register */
  3584. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_STATUS_REG,
  3585. 0, NULL);
  3586. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CONFIG_REG,
  3587. pdev->reg.crypto_cfg_be, &pcl_info->crypto_cfg);
  3588. /* 1 dummy write */
  3589. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_SEG_SIZE_REG,
  3590. 0, NULL);
  3591. break;
  3592. case QCE_HASH_SHA1_HMAC:
  3593. cmdlistptr->auth_sha1_hmac.cmdlist = (uintptr_t)ce_vaddr;
  3594. pcl_info = &(cmdlistptr->auth_sha1_hmac);
  3595. auth_cfg = pdev->reg.auth_cfg_hmac_sha1;
  3596. key_reg = 16;
  3597. iv_reg = 5;
  3598. /* clear status register */
  3599. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_STATUS_REG,
  3600. 0, NULL);
  3601. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CONFIG_REG,
  3602. pdev->reg.crypto_cfg_be, &pcl_info->crypto_cfg);
  3603. break;
  3604. case QCE_HASH_SHA256_HMAC:
  3605. cmdlistptr->auth_sha256_hmac.cmdlist = (uintptr_t)ce_vaddr;
  3606. pcl_info = &(cmdlistptr->auth_sha256_hmac);
  3607. auth_cfg = pdev->reg.auth_cfg_hmac_sha256;
  3608. key_reg = 16;
  3609. iv_reg = 8;
  3610. /* clear status register */
  3611. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_STATUS_REG, 0,
  3612. NULL);
  3613. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CONFIG_REG,
  3614. pdev->reg.crypto_cfg_be, &pcl_info->crypto_cfg);
  3615. /* 1 dummy write */
  3616. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_SEG_SIZE_REG,
  3617. 0, NULL);
  3618. break;
  3619. case QCE_HASH_AES_CMAC:
  3620. if (key_128) {
  3621. cmdlistptr->auth_aes_128_cmac.cmdlist =
  3622. (uintptr_t)ce_vaddr;
  3623. pcl_info = &(cmdlistptr->auth_aes_128_cmac);
  3624. auth_cfg = pdev->reg.auth_cfg_cmac_128;
  3625. key_reg = 4;
  3626. } else {
  3627. cmdlistptr->auth_aes_256_cmac.cmdlist =
  3628. (uintptr_t)ce_vaddr;
  3629. pcl_info = &(cmdlistptr->auth_aes_256_cmac);
  3630. auth_cfg = pdev->reg.auth_cfg_cmac_256;
  3631. key_reg = 8;
  3632. }
  3633. /* clear status register */
  3634. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_STATUS_REG, 0,
  3635. NULL);
  3636. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CONFIG_REG,
  3637. pdev->reg.crypto_cfg_be, &pcl_info->crypto_cfg);
  3638. /* 1 dummy write */
  3639. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_SEG_SIZE_REG,
  3640. 0, NULL);
  3641. break;
  3642. default:
  3643. pr_err("Unknown algorithms %d received, exiting now\n", alg);
  3644. return -EINVAL;
  3645. break;
  3646. }
  3647. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_SEG_SIZE_REG, 0,
  3648. &pcl_info->seg_size);
  3649. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_SEG_CFG_REG, 0,
  3650. &pcl_info->encr_seg_cfg);
  3651. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_SEG_CFG_REG,
  3652. auth_cfg, &pcl_info->auth_seg_cfg);
  3653. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_SEG_SIZE_REG, 0,
  3654. &pcl_info->auth_seg_size);
  3655. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_SEG_START_REG, 0,
  3656. &pcl_info->auth_seg_start);
  3657. if (alg == QCE_HASH_AES_CMAC) {
  3658. /* reset auth iv, bytecount and key registers */
  3659. for (i = 0; i < 16; i++)
  3660. qce_add_cmd_element(pdev, &ce_vaddr,
  3661. (CRYPTO_AUTH_IV0_REG + i * sizeof(uint32_t)),
  3662. 0, NULL);
  3663. for (i = 0; i < 16; i++)
  3664. qce_add_cmd_element(pdev, &ce_vaddr,
  3665. (CRYPTO_AUTH_KEY0_REG + i*sizeof(uint32_t)),
  3666. 0, NULL);
  3667. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_BYTECNT0_REG,
  3668. 0, NULL);
  3669. } else {
  3670. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_IV0_REG, 0,
  3671. &pcl_info->auth_iv);
  3672. for (i = 1; i < iv_reg; i++)
  3673. qce_add_cmd_element(pdev, &ce_vaddr,
  3674. (CRYPTO_AUTH_IV0_REG + i*sizeof(uint32_t)),
  3675. 0, NULL);
  3676. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_BYTECNT0_REG,
  3677. 0, &pcl_info->auth_bytecount);
  3678. }
  3679. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_BYTECNT1_REG, 0, NULL);
  3680. if (key_reg) {
  3681. qce_add_cmd_element(pdev, &ce_vaddr,
  3682. CRYPTO_AUTH_KEY0_REG, 0, &pcl_info->auth_key);
  3683. for (i = 1; i < key_reg; i++)
  3684. qce_add_cmd_element(pdev, &ce_vaddr,
  3685. (CRYPTO_AUTH_KEY0_REG + i*sizeof(uint32_t)),
  3686. 0, NULL);
  3687. }
  3688. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CONFIG_REG,
  3689. pdev->reg.crypto_cfg_le, &pcl_info->crypto_cfg_le);
  3690. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_GOPROC_REG,
  3691. ((1 << CRYPTO_GO) | (1 << CRYPTO_RESULTS_DUMP) |
  3692. (1 << CRYPTO_CLR_CNTXT)), &pcl_info->go_proc);
  3693. pcl_info->size = (uintptr_t)ce_vaddr - (uintptr_t)ce_vaddr_start;
  3694. *pvaddr = (unsigned char *) ce_vaddr;
  3695. return 0;
  3696. }
  3697. static int _setup_aead_cmdlistptrs(struct qce_device *pdev,
  3698. int cri_index,
  3699. unsigned char **pvaddr,
  3700. uint32_t alg,
  3701. uint32_t mode,
  3702. uint32_t key_size,
  3703. bool sha1)
  3704. {
  3705. struct sps_command_element *ce_vaddr;
  3706. uintptr_t ce_vaddr_start;
  3707. struct qce_cmdlistptr_ops *cmd;
  3708. struct qce_cmdlist_info *pcl_info = NULL;
  3709. uint32_t key_reg;
  3710. uint32_t iv_reg;
  3711. uint32_t i;
  3712. uint32_t enciv_in_word;
  3713. uint32_t encr_cfg;
  3714. cmd = &pdev->ce_request_info[cri_index].ce_sps.cmdlistptr;
  3715. *pvaddr = (unsigned char *)ALIGN(((uintptr_t)(*pvaddr)),
  3716. pdev->ce_bam_info.ce_burst_size);
  3717. ce_vaddr_start = (uintptr_t)(*pvaddr);
  3718. ce_vaddr = (struct sps_command_element *)(*pvaddr);
  3719. switch (alg) {
  3720. case CIPHER_ALG_DES:
  3721. switch (mode) {
  3722. case QCE_MODE_CBC:
  3723. if (sha1) {
  3724. cmd->aead_hmac_sha1_cbc_des.cmdlist =
  3725. (uintptr_t)ce_vaddr;
  3726. pcl_info =
  3727. &(cmd->aead_hmac_sha1_cbc_des);
  3728. } else {
  3729. cmd->aead_hmac_sha256_cbc_des.cmdlist =
  3730. (uintptr_t)ce_vaddr;
  3731. pcl_info =
  3732. &(cmd->aead_hmac_sha256_cbc_des);
  3733. }
  3734. encr_cfg = pdev->reg.encr_cfg_des_cbc;
  3735. break;
  3736. default:
  3737. return -EINVAL;
  3738. }
  3739. enciv_in_word = 2;
  3740. break;
  3741. case CIPHER_ALG_3DES:
  3742. switch (mode) {
  3743. case QCE_MODE_CBC:
  3744. if (sha1) {
  3745. cmd->aead_hmac_sha1_cbc_3des.cmdlist =
  3746. (uintptr_t)ce_vaddr;
  3747. pcl_info =
  3748. &(cmd->aead_hmac_sha1_cbc_3des);
  3749. } else {
  3750. cmd->aead_hmac_sha256_cbc_3des.cmdlist =
  3751. (uintptr_t)ce_vaddr;
  3752. pcl_info =
  3753. &(cmd->aead_hmac_sha256_cbc_3des);
  3754. }
  3755. encr_cfg = pdev->reg.encr_cfg_3des_cbc;
  3756. break;
  3757. default:
  3758. return -EINVAL;
  3759. }
  3760. enciv_in_word = 2;
  3761. break;
  3762. case CIPHER_ALG_AES:
  3763. switch (mode) {
  3764. case QCE_MODE_CBC:
  3765. if (key_size == AES128_KEY_SIZE) {
  3766. if (sha1) {
  3767. cmd->aead_hmac_sha1_cbc_aes_128.cmdlist =
  3768. (uintptr_t)ce_vaddr;
  3769. pcl_info =
  3770. &(cmd->aead_hmac_sha1_cbc_aes_128);
  3771. } else {
  3772. cmd->aead_hmac_sha256_cbc_aes_128.cmdlist
  3773. = (uintptr_t)ce_vaddr;
  3774. pcl_info =
  3775. &(cmd->aead_hmac_sha256_cbc_aes_128);
  3776. }
  3777. encr_cfg = pdev->reg.encr_cfg_aes_cbc_128;
  3778. } else if (key_size == AES256_KEY_SIZE) {
  3779. if (sha1) {
  3780. cmd->aead_hmac_sha1_cbc_aes_256.cmdlist =
  3781. (uintptr_t)ce_vaddr;
  3782. pcl_info =
  3783. &(cmd->aead_hmac_sha1_cbc_aes_256);
  3784. } else {
  3785. cmd->aead_hmac_sha256_cbc_aes_256.cmdlist =
  3786. (uintptr_t)ce_vaddr;
  3787. pcl_info =
  3788. &(cmd->aead_hmac_sha256_cbc_aes_256);
  3789. }
  3790. encr_cfg = pdev->reg.encr_cfg_aes_cbc_256;
  3791. } else {
  3792. return -EINVAL;
  3793. }
  3794. break;
  3795. default:
  3796. return -EINVAL;
  3797. }
  3798. enciv_in_word = 4;
  3799. break;
  3800. default:
  3801. return -EINVAL;
  3802. }
  3803. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_STATUS_REG, 0, NULL);
  3804. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CONFIG_REG,
  3805. pdev->reg.crypto_cfg_be, &pcl_info->crypto_cfg);
  3806. key_reg = key_size/sizeof(uint32_t);
  3807. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_KEY0_REG, 0,
  3808. &pcl_info->encr_key);
  3809. for (i = 1; i < key_reg; i++)
  3810. qce_add_cmd_element(pdev, &ce_vaddr,
  3811. (CRYPTO_ENCR_KEY0_REG + i * sizeof(uint32_t)),
  3812. 0, NULL);
  3813. if (mode != QCE_MODE_ECB) {
  3814. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CNTR0_IV0_REG, 0,
  3815. &pcl_info->encr_cntr_iv);
  3816. for (i = 1; i < enciv_in_word; i++)
  3817. qce_add_cmd_element(pdev, &ce_vaddr,
  3818. (CRYPTO_CNTR0_IV0_REG + i * sizeof(uint32_t)),
  3819. 0, NULL);
  3820. }
  3821. if (sha1)
  3822. iv_reg = 5;
  3823. else
  3824. iv_reg = 8;
  3825. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_IV0_REG, 0,
  3826. &pcl_info->auth_iv);
  3827. for (i = 1; i < iv_reg; i++)
  3828. qce_add_cmd_element(pdev, &ce_vaddr,
  3829. (CRYPTO_AUTH_IV0_REG + i*sizeof(uint32_t)),
  3830. 0, NULL);
  3831. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_BYTECNT0_REG,
  3832. 0, &pcl_info->auth_bytecount);
  3833. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_BYTECNT1_REG, 0, NULL);
  3834. key_reg = SHA_HMAC_KEY_SIZE/sizeof(uint32_t);
  3835. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_KEY0_REG, 0,
  3836. &pcl_info->auth_key);
  3837. for (i = 1; i < key_reg; i++)
  3838. qce_add_cmd_element(pdev, &ce_vaddr,
  3839. (CRYPTO_AUTH_KEY0_REG + i*sizeof(uint32_t)), 0, NULL);
  3840. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_SEG_SIZE_REG, 0,
  3841. &pcl_info->seg_size);
  3842. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_SEG_CFG_REG, encr_cfg,
  3843. &pcl_info->encr_seg_cfg);
  3844. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_SEG_SIZE_REG, 0,
  3845. &pcl_info->encr_seg_size);
  3846. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_SEG_START_REG, 0,
  3847. &pcl_info->encr_seg_start);
  3848. if (sha1)
  3849. qce_add_cmd_element(
  3850. pdev,
  3851. &ce_vaddr,
  3852. CRYPTO_AUTH_SEG_CFG_REG,
  3853. pdev->reg.auth_cfg_aead_sha1_hmac,
  3854. &pcl_info->auth_seg_cfg);
  3855. else
  3856. qce_add_cmd_element(
  3857. pdev,
  3858. &ce_vaddr,
  3859. CRYPTO_AUTH_SEG_CFG_REG,
  3860. pdev->reg.auth_cfg_aead_sha256_hmac,
  3861. &pcl_info->auth_seg_cfg);
  3862. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_SEG_SIZE_REG, 0,
  3863. &pcl_info->auth_seg_size);
  3864. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_SEG_START_REG, 0,
  3865. &pcl_info->auth_seg_start);
  3866. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CONFIG_REG,
  3867. pdev->reg.crypto_cfg_le, &pcl_info->crypto_cfg_le);
  3868. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_GOPROC_REG,
  3869. ((1 << CRYPTO_GO) | (1 << CRYPTO_RESULTS_DUMP) |
  3870. (1 << CRYPTO_CLR_CNTXT)), &pcl_info->go_proc);
  3871. pcl_info->size = (uintptr_t)ce_vaddr - (uintptr_t)ce_vaddr_start;
  3872. *pvaddr = (unsigned char *) ce_vaddr;
  3873. return 0;
  3874. }
  3875. static int _setup_aead_ccm_cmdlistptrs(struct qce_device *pdev, int cri_index,
  3876. unsigned char **pvaddr, bool key_128)
  3877. {
  3878. struct sps_command_element *ce_vaddr;
  3879. uintptr_t ce_vaddr_start;
  3880. struct qce_cmdlistptr_ops *cmdlistptr = &pdev->ce_request_info
  3881. [cri_index].ce_sps.cmdlistptr;
  3882. struct qce_cmdlist_info *pcl_info = NULL;
  3883. int i = 0;
  3884. uint32_t encr_cfg = 0;
  3885. uint32_t auth_cfg = 0;
  3886. uint32_t key_reg = 0;
  3887. *pvaddr = (unsigned char *)ALIGN(((uintptr_t)(*pvaddr)),
  3888. pdev->ce_bam_info.ce_burst_size);
  3889. ce_vaddr_start = (uintptr_t)(*pvaddr);
  3890. ce_vaddr = (struct sps_command_element *)(*pvaddr);
  3891. /*
  3892. * Designate chunks of the allocated memory to various
  3893. * command list pointers related to aead operations
  3894. * defined in ce_cmdlistptrs_ops structure.
  3895. */
  3896. if (key_128) {
  3897. cmdlistptr->aead_aes_128_ccm.cmdlist =
  3898. (uintptr_t)ce_vaddr;
  3899. pcl_info = &(cmdlistptr->aead_aes_128_ccm);
  3900. auth_cfg = pdev->reg.auth_cfg_aes_ccm_128;
  3901. encr_cfg = pdev->reg.encr_cfg_aes_ccm_128;
  3902. key_reg = 4;
  3903. } else {
  3904. cmdlistptr->aead_aes_256_ccm.cmdlist =
  3905. (uintptr_t)ce_vaddr;
  3906. pcl_info = &(cmdlistptr->aead_aes_256_ccm);
  3907. auth_cfg = pdev->reg.auth_cfg_aes_ccm_256;
  3908. encr_cfg = pdev->reg.encr_cfg_aes_ccm_256;
  3909. key_reg = 8;
  3910. }
  3911. /* clear status register */
  3912. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_STATUS_REG, 0, NULL);
  3913. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CONFIG_REG,
  3914. pdev->reg.crypto_cfg_be, &pcl_info->crypto_cfg);
  3915. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_SEG_CFG_REG, 0, NULL);
  3916. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_SEG_START_REG, 0,
  3917. NULL);
  3918. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_SEG_SIZE_REG, 0,
  3919. &pcl_info->seg_size);
  3920. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_SEG_CFG_REG,
  3921. encr_cfg, &pcl_info->encr_seg_cfg);
  3922. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_SEG_SIZE_REG, 0,
  3923. &pcl_info->encr_seg_size);
  3924. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_SEG_START_REG, 0,
  3925. &pcl_info->encr_seg_start);
  3926. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CNTR_MASK_REG,
  3927. pdev->reg.encr_cntr_mask_3, &pcl_info->encr_mask_3);
  3928. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CNTR_MASK_REG0,
  3929. pdev->reg.encr_cntr_mask_2, &pcl_info->encr_mask_2);
  3930. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CNTR_MASK_REG1,
  3931. pdev->reg.encr_cntr_mask_1, &pcl_info->encr_mask_1);
  3932. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CNTR_MASK_REG2,
  3933. pdev->reg.encr_cntr_mask_0, &pcl_info->encr_mask_0);
  3934. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_SEG_CFG_REG,
  3935. auth_cfg, &pcl_info->auth_seg_cfg);
  3936. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_SEG_SIZE_REG, 0,
  3937. &pcl_info->auth_seg_size);
  3938. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_SEG_START_REG, 0,
  3939. &pcl_info->auth_seg_start);
  3940. /* reset auth iv, bytecount and key registers */
  3941. for (i = 0; i < 8; i++)
  3942. qce_add_cmd_element(pdev, &ce_vaddr,
  3943. (CRYPTO_AUTH_IV0_REG + i * sizeof(uint32_t)),
  3944. 0, NULL);
  3945. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_BYTECNT0_REG,
  3946. 0, NULL);
  3947. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_BYTECNT1_REG,
  3948. 0, NULL);
  3949. for (i = 0; i < 16; i++)
  3950. qce_add_cmd_element(pdev, &ce_vaddr,
  3951. (CRYPTO_AUTH_KEY0_REG + i * sizeof(uint32_t)),
  3952. 0, NULL);
  3953. /* set auth key */
  3954. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_KEY0_REG, 0,
  3955. &pcl_info->auth_key);
  3956. for (i = 1; i < key_reg; i++)
  3957. qce_add_cmd_element(pdev, &ce_vaddr,
  3958. (CRYPTO_AUTH_KEY0_REG + i * sizeof(uint32_t)),
  3959. 0, NULL);
  3960. /* set NONCE info */
  3961. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_INFO_NONCE0_REG, 0,
  3962. &pcl_info->auth_nonce_info);
  3963. for (i = 1; i < 4; i++)
  3964. qce_add_cmd_element(pdev, &ce_vaddr,
  3965. (CRYPTO_AUTH_INFO_NONCE0_REG +
  3966. i * sizeof(uint32_t)), 0, NULL);
  3967. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_KEY0_REG, 0,
  3968. &pcl_info->encr_key);
  3969. for (i = 1; i < key_reg; i++)
  3970. qce_add_cmd_element(pdev, &ce_vaddr,
  3971. (CRYPTO_ENCR_KEY0_REG + i * sizeof(uint32_t)),
  3972. 0, NULL);
  3973. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CNTR0_IV0_REG, 0,
  3974. &pcl_info->encr_cntr_iv);
  3975. for (i = 1; i < 4; i++)
  3976. qce_add_cmd_element(pdev, &ce_vaddr,
  3977. (CRYPTO_CNTR0_IV0_REG + i * sizeof(uint32_t)),
  3978. 0, NULL);
  3979. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_CCM_INT_CNTR0_REG, 0,
  3980. &pcl_info->encr_ccm_cntr_iv);
  3981. for (i = 1; i < 4; i++)
  3982. qce_add_cmd_element(pdev, &ce_vaddr,
  3983. (CRYPTO_ENCR_CCM_INT_CNTR0_REG + i * sizeof(uint32_t)),
  3984. 0, NULL);
  3985. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CONFIG_REG,
  3986. pdev->reg.crypto_cfg_le, &pcl_info->crypto_cfg_le);
  3987. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_GOPROC_REG,
  3988. ((1 << CRYPTO_GO) | (1 << CRYPTO_RESULTS_DUMP) |
  3989. (1 << CRYPTO_CLR_CNTXT)), &pcl_info->go_proc);
  3990. pcl_info->size = (uintptr_t)ce_vaddr - (uintptr_t)ce_vaddr_start;
  3991. *pvaddr = (unsigned char *) ce_vaddr;
  3992. return 0;
  3993. }
  3994. static int _setup_f8_cmdlistptrs(struct qce_device *pdev, int cri_index,
  3995. unsigned char **pvaddr, enum qce_ota_algo_enum alg)
  3996. {
  3997. struct sps_command_element *ce_vaddr;
  3998. uintptr_t ce_vaddr_start;
  3999. struct qce_cmdlistptr_ops *cmdlistptr;
  4000. struct qce_cmdlist_info *pcl_info = NULL;
  4001. int i = 0;
  4002. uint32_t encr_cfg = 0;
  4003. uint32_t key_reg = 4;
  4004. cmdlistptr = &pdev->ce_request_info[cri_index].ce_sps.cmdlistptr;
  4005. *pvaddr = (unsigned char *)ALIGN(((uintptr_t)(*pvaddr)),
  4006. pdev->ce_bam_info.ce_burst_size);
  4007. ce_vaddr = (struct sps_command_element *)(*pvaddr);
  4008. ce_vaddr_start = (uintptr_t)(*pvaddr);
  4009. /*
  4010. * Designate chunks of the allocated memory to various
  4011. * command list pointers related to f8 cipher algorithm defined
  4012. * in ce_cmdlistptrs_ops structure.
  4013. */
  4014. switch (alg) {
  4015. case QCE_OTA_ALGO_KASUMI:
  4016. cmdlistptr->f8_kasumi.cmdlist = (uintptr_t)ce_vaddr;
  4017. pcl_info = &(cmdlistptr->f8_kasumi);
  4018. encr_cfg = pdev->reg.encr_cfg_kasumi;
  4019. break;
  4020. case QCE_OTA_ALGO_SNOW3G:
  4021. default:
  4022. cmdlistptr->f8_snow3g.cmdlist = (uintptr_t)ce_vaddr;
  4023. pcl_info = &(cmdlistptr->f8_snow3g);
  4024. encr_cfg = pdev->reg.encr_cfg_snow3g;
  4025. break;
  4026. }
  4027. /* clear status register */
  4028. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_STATUS_REG,
  4029. 0, NULL);
  4030. /* set config to big endian */
  4031. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CONFIG_REG,
  4032. pdev->reg.crypto_cfg_be, &pcl_info->crypto_cfg);
  4033. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_SEG_SIZE_REG, 0,
  4034. &pcl_info->seg_size);
  4035. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_SEG_CFG_REG, encr_cfg,
  4036. &pcl_info->encr_seg_cfg);
  4037. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_SEG_SIZE_REG, 0,
  4038. &pcl_info->encr_seg_size);
  4039. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_SEG_START_REG, 0,
  4040. &pcl_info->encr_seg_start);
  4041. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_SEG_CFG_REG, 0,
  4042. &pcl_info->auth_seg_cfg);
  4043. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_SEG_SIZE_REG,
  4044. 0, &pcl_info->auth_seg_size);
  4045. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_SEG_START_REG,
  4046. 0, &pcl_info->auth_seg_start);
  4047. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_KEY0_REG, 0,
  4048. &pcl_info->encr_key);
  4049. for (i = 1; i < key_reg; i++)
  4050. qce_add_cmd_element(pdev, &ce_vaddr,
  4051. (CRYPTO_ENCR_KEY0_REG + i * sizeof(uint32_t)),
  4052. 0, NULL);
  4053. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CNTR0_IV0_REG, 0,
  4054. &pcl_info->encr_cntr_iv);
  4055. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CNTR1_IV1_REG, 0,
  4056. NULL);
  4057. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CONFIG_REG,
  4058. pdev->reg.crypto_cfg_le, &pcl_info->crypto_cfg_le);
  4059. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_GOPROC_REG,
  4060. ((1 << CRYPTO_GO) | (1 << CRYPTO_RESULTS_DUMP) |
  4061. (1 << CRYPTO_CLR_CNTXT)), &pcl_info->go_proc);
  4062. pcl_info->size = (uintptr_t)ce_vaddr - (uintptr_t)ce_vaddr_start;
  4063. *pvaddr = (unsigned char *) ce_vaddr;
  4064. return 0;
  4065. }
  4066. static int _setup_f9_cmdlistptrs(struct qce_device *pdev, int cri_index,
  4067. unsigned char **pvaddr, enum qce_ota_algo_enum alg)
  4068. {
  4069. struct sps_command_element *ce_vaddr;
  4070. uintptr_t ce_vaddr_start;
  4071. struct qce_cmdlistptr_ops *cmdlistptr;
  4072. struct qce_cmdlist_info *pcl_info = NULL;
  4073. int i = 0;
  4074. uint32_t auth_cfg = 0;
  4075. uint32_t iv_reg = 0;
  4076. cmdlistptr = &pdev->ce_request_info[cri_index].ce_sps.cmdlistptr;
  4077. *pvaddr = (unsigned char *)ALIGN(((uintptr_t)(*pvaddr)),
  4078. pdev->ce_bam_info.ce_burst_size);
  4079. ce_vaddr_start = (uintptr_t)(*pvaddr);
  4080. ce_vaddr = (struct sps_command_element *)(*pvaddr);
  4081. /*
  4082. * Designate chunks of the allocated memory to various
  4083. * command list pointers related to authentication operations
  4084. * defined in ce_cmdlistptrs_ops structure.
  4085. */
  4086. switch (alg) {
  4087. case QCE_OTA_ALGO_KASUMI:
  4088. cmdlistptr->f9_kasumi.cmdlist = (uintptr_t)ce_vaddr;
  4089. pcl_info = &(cmdlistptr->f9_kasumi);
  4090. auth_cfg = pdev->reg.auth_cfg_kasumi;
  4091. break;
  4092. case QCE_OTA_ALGO_SNOW3G:
  4093. default:
  4094. cmdlistptr->f9_snow3g.cmdlist = (uintptr_t)ce_vaddr;
  4095. pcl_info = &(cmdlistptr->f9_snow3g);
  4096. auth_cfg = pdev->reg.auth_cfg_snow3g;
  4097. }
  4098. /* clear status register */
  4099. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_STATUS_REG,
  4100. 0, NULL);
  4101. /* set config to big endian */
  4102. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CONFIG_REG,
  4103. pdev->reg.crypto_cfg_be, &pcl_info->crypto_cfg);
  4104. iv_reg = 5;
  4105. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_SEG_SIZE_REG, 0,
  4106. &pcl_info->seg_size);
  4107. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_ENCR_SEG_CFG_REG, 0,
  4108. &pcl_info->encr_seg_cfg);
  4109. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_SEG_CFG_REG,
  4110. auth_cfg, &pcl_info->auth_seg_cfg);
  4111. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_SEG_SIZE_REG, 0,
  4112. &pcl_info->auth_seg_size);
  4113. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_SEG_START_REG, 0,
  4114. &pcl_info->auth_seg_start);
  4115. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_IV0_REG, 0,
  4116. &pcl_info->auth_iv);
  4117. for (i = 1; i < iv_reg; i++) {
  4118. qce_add_cmd_element(pdev, &ce_vaddr,
  4119. (CRYPTO_AUTH_IV0_REG + i*sizeof(uint32_t)),
  4120. 0, NULL);
  4121. }
  4122. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_BYTECNT0_REG,
  4123. 0, &pcl_info->auth_bytecount);
  4124. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_AUTH_BYTECNT1_REG, 0, NULL);
  4125. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CONFIG_REG,
  4126. pdev->reg.crypto_cfg_le, &pcl_info->crypto_cfg_le);
  4127. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_GOPROC_REG,
  4128. ((1 << CRYPTO_GO) | (1 << CRYPTO_RESULTS_DUMP) |
  4129. (1 << CRYPTO_CLR_CNTXT)), &pcl_info->go_proc);
  4130. pcl_info->size = (uintptr_t)ce_vaddr - (uintptr_t)ce_vaddr_start;
  4131. *pvaddr = (unsigned char *) ce_vaddr;
  4132. return 0;
  4133. }
  4134. static int _setup_unlock_pipe_cmdlistptrs(struct qce_device *pdev,
  4135. int cri_index, unsigned char **pvaddr)
  4136. {
  4137. struct sps_command_element *ce_vaddr;
  4138. uintptr_t ce_vaddr_start = (uintptr_t)(*pvaddr);
  4139. struct qce_cmdlistptr_ops *cmdlistptr;
  4140. struct qce_cmdlist_info *pcl_info = NULL;
  4141. cmdlistptr = &pdev->ce_request_info[cri_index].ce_sps.cmdlistptr;
  4142. *pvaddr = (unsigned char *)ALIGN(((uintptr_t)(*pvaddr)),
  4143. pdev->ce_bam_info.ce_burst_size);
  4144. ce_vaddr = (struct sps_command_element *)(*pvaddr);
  4145. cmdlistptr->unlock_all_pipes.cmdlist = (uintptr_t)ce_vaddr;
  4146. pcl_info = &(cmdlistptr->unlock_all_pipes);
  4147. /*
  4148. * Designate chunks of the allocated memory to command list
  4149. * to unlock pipes.
  4150. */
  4151. qce_add_cmd_element(pdev, &ce_vaddr, CRYPTO_CONFIG_REG,
  4152. CRYPTO_CONFIG_RESET, NULL);
  4153. pcl_info->size = (uintptr_t)ce_vaddr - (uintptr_t)ce_vaddr_start;
  4154. *pvaddr = (unsigned char *) ce_vaddr;
  4155. return 0;
  4156. }
  4157. static int qce_setup_cmdlistptrs(struct qce_device *pdev, int cri_index,
  4158. unsigned char **pvaddr)
  4159. {
  4160. struct sps_command_element *ce_vaddr =
  4161. (struct sps_command_element *)(*pvaddr);
  4162. /*
  4163. * Designate chunks of the allocated memory to various
  4164. * command list pointers related to operations defined
  4165. * in ce_cmdlistptrs_ops structure.
  4166. */
  4167. ce_vaddr =
  4168. (struct sps_command_element *)ALIGN(((uintptr_t) ce_vaddr),
  4169. pdev->ce_bam_info.ce_burst_size);
  4170. *pvaddr = (unsigned char *) ce_vaddr;
  4171. _setup_cipher_aes_cmdlistptrs(pdev, cri_index, pvaddr, QCE_MODE_CBC,
  4172. true);
  4173. _setup_cipher_aes_cmdlistptrs(pdev, cri_index, pvaddr, QCE_MODE_CTR,
  4174. true);
  4175. _setup_cipher_aes_cmdlistptrs(pdev, cri_index, pvaddr, QCE_MODE_ECB,
  4176. true);
  4177. _setup_cipher_aes_cmdlistptrs(pdev, cri_index, pvaddr, QCE_MODE_XTS,
  4178. true);
  4179. _setup_cipher_aes_cmdlistptrs(pdev, cri_index, pvaddr, QCE_MODE_CBC,
  4180. false);
  4181. _setup_cipher_aes_cmdlistptrs(pdev, cri_index, pvaddr, QCE_MODE_CTR,
  4182. false);
  4183. _setup_cipher_aes_cmdlistptrs(pdev, cri_index, pvaddr, QCE_MODE_ECB,
  4184. false);
  4185. _setup_cipher_aes_cmdlistptrs(pdev, cri_index, pvaddr, QCE_MODE_XTS,
  4186. false);
  4187. _setup_cipher_des_cmdlistptrs(pdev, cri_index, pvaddr, CIPHER_ALG_DES,
  4188. true);
  4189. _setup_cipher_des_cmdlistptrs(pdev, cri_index, pvaddr, CIPHER_ALG_DES,
  4190. false);
  4191. _setup_cipher_des_cmdlistptrs(pdev, cri_index, pvaddr, CIPHER_ALG_3DES,
  4192. true);
  4193. _setup_cipher_des_cmdlistptrs(pdev, cri_index, pvaddr, CIPHER_ALG_3DES,
  4194. false);
  4195. _setup_auth_cmdlistptrs(pdev, cri_index, pvaddr, QCE_HASH_SHA1,
  4196. false);
  4197. _setup_auth_cmdlistptrs(pdev, cri_index, pvaddr, QCE_HASH_SHA256,
  4198. false);
  4199. _setup_auth_cmdlistptrs(pdev, cri_index, pvaddr, QCE_HASH_SHA1_HMAC,
  4200. false);
  4201. _setup_auth_cmdlistptrs(pdev, cri_index, pvaddr, QCE_HASH_SHA256_HMAC,
  4202. false);
  4203. _setup_auth_cmdlistptrs(pdev, cri_index, pvaddr, QCE_HASH_AES_CMAC,
  4204. true);
  4205. _setup_auth_cmdlistptrs(pdev, cri_index, pvaddr, QCE_HASH_AES_CMAC,
  4206. false);
  4207. _setup_aead_cmdlistptrs(pdev, cri_index, pvaddr, CIPHER_ALG_DES,
  4208. QCE_MODE_CBC, DES_KEY_SIZE, true);
  4209. _setup_aead_cmdlistptrs(pdev, cri_index, pvaddr, CIPHER_ALG_3DES,
  4210. QCE_MODE_CBC, DES3_EDE_KEY_SIZE, true);
  4211. _setup_aead_cmdlistptrs(pdev, cri_index, pvaddr, CIPHER_ALG_AES,
  4212. QCE_MODE_CBC, AES128_KEY_SIZE, true);
  4213. _setup_aead_cmdlistptrs(pdev, cri_index, pvaddr, CIPHER_ALG_AES,
  4214. QCE_MODE_CBC, AES256_KEY_SIZE, true);
  4215. _setup_aead_cmdlistptrs(pdev, cri_index, pvaddr, CIPHER_ALG_DES,
  4216. QCE_MODE_CBC, DES_KEY_SIZE, false);
  4217. _setup_aead_cmdlistptrs(pdev, cri_index, pvaddr, CIPHER_ALG_3DES,
  4218. QCE_MODE_CBC, DES3_EDE_KEY_SIZE, false);
  4219. _setup_aead_cmdlistptrs(pdev, cri_index, pvaddr, CIPHER_ALG_AES,
  4220. QCE_MODE_CBC, AES128_KEY_SIZE, false);
  4221. _setup_aead_cmdlistptrs(pdev, cri_index, pvaddr, CIPHER_ALG_AES,
  4222. QCE_MODE_CBC, AES256_KEY_SIZE, false);
  4223. _setup_cipher_null_cmdlistptrs(pdev, cri_index, pvaddr);
  4224. _setup_aead_ccm_cmdlistptrs(pdev, cri_index, pvaddr, true);
  4225. _setup_aead_ccm_cmdlistptrs(pdev, cri_index, pvaddr, false);
  4226. _setup_f8_cmdlistptrs(pdev, cri_index, pvaddr, QCE_OTA_ALGO_KASUMI);
  4227. _setup_f8_cmdlistptrs(pdev, cri_index, pvaddr, QCE_OTA_ALGO_SNOW3G);
  4228. _setup_f9_cmdlistptrs(pdev, cri_index, pvaddr, QCE_OTA_ALGO_KASUMI);
  4229. _setup_f9_cmdlistptrs(pdev, cri_index, pvaddr, QCE_OTA_ALGO_SNOW3G);
  4230. _setup_unlock_pipe_cmdlistptrs(pdev, cri_index, pvaddr);
  4231. return 0;
  4232. }
  4233. static int qce_setup_ce_sps_data(struct qce_device *pce_dev)
  4234. {
  4235. unsigned char *vaddr;
  4236. int i;
  4237. unsigned char *iovec_vaddr;
  4238. int iovec_memsize;
  4239. vaddr = pce_dev->coh_vmem;
  4240. vaddr = (unsigned char *)ALIGN(((uintptr_t)vaddr),
  4241. pce_dev->ce_bam_info.ce_burst_size);
  4242. iovec_vaddr = pce_dev->iovec_vmem;
  4243. iovec_memsize = pce_dev->iovec_memsize;
  4244. for (i = 0; i < MAX_QCE_ALLOC_BAM_REQ; i++) {
  4245. /* Allow for 256 descriptor (cmd and data) entries per pipe */
  4246. pce_dev->ce_request_info[i].ce_sps.in_transfer.iovec =
  4247. (struct sps_iovec *)iovec_vaddr;
  4248. pce_dev->ce_request_info[i].ce_sps.in_transfer.iovec_phys =
  4249. virt_to_phys(
  4250. pce_dev->ce_request_info[i].ce_sps.in_transfer.iovec);
  4251. iovec_vaddr += TOTAL_IOVEC_SPACE_PER_PIPE;
  4252. iovec_memsize -= TOTAL_IOVEC_SPACE_PER_PIPE;
  4253. pce_dev->ce_request_info[i].ce_sps.out_transfer.iovec =
  4254. (struct sps_iovec *)iovec_vaddr;
  4255. pce_dev->ce_request_info[i].ce_sps.out_transfer.iovec_phys =
  4256. virt_to_phys(
  4257. pce_dev->ce_request_info[i].ce_sps.out_transfer.iovec);
  4258. iovec_vaddr += TOTAL_IOVEC_SPACE_PER_PIPE;
  4259. iovec_memsize -= TOTAL_IOVEC_SPACE_PER_PIPE;
  4260. if (pce_dev->support_cmd_dscr)
  4261. qce_setup_cmdlistptrs(pce_dev, i, &vaddr);
  4262. vaddr = (unsigned char *)ALIGN(((uintptr_t)vaddr),
  4263. pce_dev->ce_bam_info.ce_burst_size);
  4264. pce_dev->ce_request_info[i].ce_sps.result_dump =
  4265. (uintptr_t)vaddr;
  4266. pce_dev->ce_request_info[i].ce_sps.result_dump_phy =
  4267. GET_PHYS_ADDR((uintptr_t)vaddr);
  4268. pce_dev->ce_request_info[i].ce_sps.result =
  4269. (struct ce_result_dump_format *)vaddr;
  4270. vaddr += CRYPTO_RESULT_DUMP_SIZE;
  4271. pce_dev->ce_request_info[i].ce_sps.result_dump_null =
  4272. (uintptr_t)vaddr;
  4273. pce_dev->ce_request_info[i].ce_sps.result_dump_null_phy =
  4274. GET_PHYS_ADDR((uintptr_t)vaddr);
  4275. pce_dev->ce_request_info[i].ce_sps.result_null =
  4276. (struct ce_result_dump_format *)vaddr;
  4277. vaddr += CRYPTO_RESULT_DUMP_SIZE;
  4278. pce_dev->ce_request_info[i].ce_sps.ignore_buffer =
  4279. (uintptr_t)vaddr;
  4280. vaddr += pce_dev->ce_bam_info.ce_burst_size * 2;
  4281. }
  4282. if ((vaddr - pce_dev->coh_vmem) > pce_dev->memsize ||
  4283. iovec_memsize < 0)
  4284. panic("qce50: Not enough coherent memory. Allocate %x , need %lx\n",
  4285. pce_dev->memsize, (uintptr_t)vaddr -
  4286. (uintptr_t)pce_dev->coh_vmem);
  4287. return 0;
  4288. }
  4289. static int qce_init_ce_cfg_val(struct qce_device *pce_dev)
  4290. {
  4291. uint32_t pipe_pair =
  4292. pce_dev->ce_bam_info.pipe_pair_index[QCE_OFFLOAD_NONE];
  4293. pce_dev->reg.crypto_cfg_be = qce_get_config_be(pce_dev, pipe_pair);
  4294. pce_dev->reg.crypto_cfg_le =
  4295. (pce_dev->reg.crypto_cfg_be | CRYPTO_LITTLE_ENDIAN_MASK);
  4296. /* Initialize encr_cfg register for AES alg */
  4297. pce_dev->reg.encr_cfg_aes_cbc_128 =
  4298. (CRYPTO_ENCR_KEY_SZ_AES128 << CRYPTO_ENCR_KEY_SZ) |
  4299. (CRYPTO_ENCR_ALG_AES << CRYPTO_ENCR_ALG) |
  4300. (CRYPTO_ENCR_MODE_CBC << CRYPTO_ENCR_MODE);
  4301. pce_dev->reg.encr_cfg_aes_cbc_256 =
  4302. (CRYPTO_ENCR_KEY_SZ_AES256 << CRYPTO_ENCR_KEY_SZ) |
  4303. (CRYPTO_ENCR_ALG_AES << CRYPTO_ENCR_ALG) |
  4304. (CRYPTO_ENCR_MODE_CBC << CRYPTO_ENCR_MODE);
  4305. pce_dev->reg.encr_cfg_aes_ctr_128 =
  4306. (CRYPTO_ENCR_KEY_SZ_AES128 << CRYPTO_ENCR_KEY_SZ) |
  4307. (CRYPTO_ENCR_ALG_AES << CRYPTO_ENCR_ALG) |
  4308. (CRYPTO_ENCR_MODE_CTR << CRYPTO_ENCR_MODE);
  4309. pce_dev->reg.encr_cfg_aes_ctr_256 =
  4310. (CRYPTO_ENCR_KEY_SZ_AES256 << CRYPTO_ENCR_KEY_SZ) |
  4311. (CRYPTO_ENCR_ALG_AES << CRYPTO_ENCR_ALG) |
  4312. (CRYPTO_ENCR_MODE_CTR << CRYPTO_ENCR_MODE);
  4313. pce_dev->reg.encr_cfg_aes_xts_128 =
  4314. (CRYPTO_ENCR_KEY_SZ_AES128 << CRYPTO_ENCR_KEY_SZ) |
  4315. (CRYPTO_ENCR_ALG_AES << CRYPTO_ENCR_ALG) |
  4316. (CRYPTO_ENCR_MODE_XTS << CRYPTO_ENCR_MODE);
  4317. pce_dev->reg.encr_cfg_aes_xts_256 =
  4318. (CRYPTO_ENCR_KEY_SZ_AES256 << CRYPTO_ENCR_KEY_SZ) |
  4319. (CRYPTO_ENCR_ALG_AES << CRYPTO_ENCR_ALG) |
  4320. (CRYPTO_ENCR_MODE_XTS << CRYPTO_ENCR_MODE);
  4321. pce_dev->reg.encr_cfg_aes_ecb_128 =
  4322. (CRYPTO_ENCR_KEY_SZ_AES128 << CRYPTO_ENCR_KEY_SZ) |
  4323. (CRYPTO_ENCR_ALG_AES << CRYPTO_ENCR_ALG) |
  4324. (CRYPTO_ENCR_MODE_ECB << CRYPTO_ENCR_MODE);
  4325. pce_dev->reg.encr_cfg_aes_ecb_256 =
  4326. (CRYPTO_ENCR_KEY_SZ_AES256 << CRYPTO_ENCR_KEY_SZ) |
  4327. (CRYPTO_ENCR_ALG_AES << CRYPTO_ENCR_ALG) |
  4328. (CRYPTO_ENCR_MODE_ECB << CRYPTO_ENCR_MODE);
  4329. pce_dev->reg.encr_cfg_aes_ccm_128 =
  4330. (CRYPTO_ENCR_KEY_SZ_AES128 << CRYPTO_ENCR_KEY_SZ) |
  4331. (CRYPTO_ENCR_ALG_AES << CRYPTO_ENCR_ALG) |
  4332. (CRYPTO_ENCR_MODE_CCM << CRYPTO_ENCR_MODE)|
  4333. (CRYPTO_LAST_CCM_XFR << CRYPTO_LAST_CCM);
  4334. pce_dev->reg.encr_cfg_aes_ccm_256 =
  4335. (CRYPTO_ENCR_KEY_SZ_AES256 << CRYPTO_ENCR_KEY_SZ) |
  4336. (CRYPTO_ENCR_ALG_AES << CRYPTO_ENCR_ALG) |
  4337. (CRYPTO_ENCR_MODE_CCM << CRYPTO_ENCR_MODE) |
  4338. (CRYPTO_LAST_CCM_XFR << CRYPTO_LAST_CCM);
  4339. /* Initialize encr_cfg register for DES alg */
  4340. pce_dev->reg.encr_cfg_des_ecb =
  4341. (CRYPTO_ENCR_KEY_SZ_DES << CRYPTO_ENCR_KEY_SZ) |
  4342. (CRYPTO_ENCR_ALG_DES << CRYPTO_ENCR_ALG) |
  4343. (CRYPTO_ENCR_MODE_ECB << CRYPTO_ENCR_MODE);
  4344. pce_dev->reg.encr_cfg_des_cbc =
  4345. (CRYPTO_ENCR_KEY_SZ_DES << CRYPTO_ENCR_KEY_SZ) |
  4346. (CRYPTO_ENCR_ALG_DES << CRYPTO_ENCR_ALG) |
  4347. (CRYPTO_ENCR_MODE_CBC << CRYPTO_ENCR_MODE);
  4348. pce_dev->reg.encr_cfg_3des_ecb =
  4349. (CRYPTO_ENCR_KEY_SZ_3DES << CRYPTO_ENCR_KEY_SZ) |
  4350. (CRYPTO_ENCR_ALG_DES << CRYPTO_ENCR_ALG) |
  4351. (CRYPTO_ENCR_MODE_ECB << CRYPTO_ENCR_MODE);
  4352. pce_dev->reg.encr_cfg_3des_cbc =
  4353. (CRYPTO_ENCR_KEY_SZ_3DES << CRYPTO_ENCR_KEY_SZ) |
  4354. (CRYPTO_ENCR_ALG_DES << CRYPTO_ENCR_ALG) |
  4355. (CRYPTO_ENCR_MODE_CBC << CRYPTO_ENCR_MODE);
  4356. /* Initialize encr_cfg register for kasumi/snow3g alg */
  4357. pce_dev->reg.encr_cfg_kasumi =
  4358. (CRYPTO_ENCR_ALG_KASUMI << CRYPTO_ENCR_ALG);
  4359. pce_dev->reg.encr_cfg_snow3g =
  4360. (CRYPTO_ENCR_ALG_SNOW_3G << CRYPTO_ENCR_ALG);
  4361. /* Initialize auth_cfg register for CMAC alg */
  4362. pce_dev->reg.auth_cfg_cmac_128 =
  4363. (1 << CRYPTO_LAST) | (1 << CRYPTO_FIRST) |
  4364. (CRYPTO_AUTH_MODE_CMAC << CRYPTO_AUTH_MODE)|
  4365. (CRYPTO_AUTH_SIZE_ENUM_16_BYTES << CRYPTO_AUTH_SIZE) |
  4366. (CRYPTO_AUTH_ALG_AES << CRYPTO_AUTH_ALG) |
  4367. (CRYPTO_AUTH_KEY_SZ_AES128 << CRYPTO_AUTH_KEY_SIZE);
  4368. pce_dev->reg.auth_cfg_cmac_256 =
  4369. (1 << CRYPTO_LAST) | (1 << CRYPTO_FIRST) |
  4370. (CRYPTO_AUTH_MODE_CMAC << CRYPTO_AUTH_MODE)|
  4371. (CRYPTO_AUTH_SIZE_ENUM_16_BYTES << CRYPTO_AUTH_SIZE) |
  4372. (CRYPTO_AUTH_ALG_AES << CRYPTO_AUTH_ALG) |
  4373. (CRYPTO_AUTH_KEY_SZ_AES256 << CRYPTO_AUTH_KEY_SIZE);
  4374. /* Initialize auth_cfg register for HMAC alg */
  4375. pce_dev->reg.auth_cfg_hmac_sha1 =
  4376. (CRYPTO_AUTH_MODE_HMAC << CRYPTO_AUTH_MODE)|
  4377. (CRYPTO_AUTH_SIZE_SHA1 << CRYPTO_AUTH_SIZE) |
  4378. (CRYPTO_AUTH_ALG_SHA << CRYPTO_AUTH_ALG) |
  4379. (CRYPTO_AUTH_POS_BEFORE << CRYPTO_AUTH_POS);
  4380. pce_dev->reg.auth_cfg_hmac_sha256 =
  4381. (CRYPTO_AUTH_MODE_HMAC << CRYPTO_AUTH_MODE)|
  4382. (CRYPTO_AUTH_SIZE_SHA256 << CRYPTO_AUTH_SIZE) |
  4383. (CRYPTO_AUTH_ALG_SHA << CRYPTO_AUTH_ALG) |
  4384. (CRYPTO_AUTH_POS_BEFORE << CRYPTO_AUTH_POS);
  4385. /* Initialize auth_cfg register for SHA1/256 alg */
  4386. pce_dev->reg.auth_cfg_sha1 =
  4387. (CRYPTO_AUTH_MODE_HASH << CRYPTO_AUTH_MODE)|
  4388. (CRYPTO_AUTH_SIZE_SHA1 << CRYPTO_AUTH_SIZE) |
  4389. (CRYPTO_AUTH_ALG_SHA << CRYPTO_AUTH_ALG) |
  4390. (CRYPTO_AUTH_POS_BEFORE << CRYPTO_AUTH_POS);
  4391. pce_dev->reg.auth_cfg_sha256 =
  4392. (CRYPTO_AUTH_MODE_HASH << CRYPTO_AUTH_MODE)|
  4393. (CRYPTO_AUTH_SIZE_SHA256 << CRYPTO_AUTH_SIZE) |
  4394. (CRYPTO_AUTH_ALG_SHA << CRYPTO_AUTH_ALG) |
  4395. (CRYPTO_AUTH_POS_BEFORE << CRYPTO_AUTH_POS);
  4396. /* Initialize auth_cfg register for AEAD alg */
  4397. pce_dev->reg.auth_cfg_aead_sha1_hmac =
  4398. (CRYPTO_AUTH_MODE_HMAC << CRYPTO_AUTH_MODE)|
  4399. (CRYPTO_AUTH_SIZE_SHA1 << CRYPTO_AUTH_SIZE) |
  4400. (CRYPTO_AUTH_ALG_SHA << CRYPTO_AUTH_ALG) |
  4401. (1 << CRYPTO_LAST) | (1 << CRYPTO_FIRST);
  4402. pce_dev->reg.auth_cfg_aead_sha256_hmac =
  4403. (CRYPTO_AUTH_MODE_HMAC << CRYPTO_AUTH_MODE)|
  4404. (CRYPTO_AUTH_SIZE_SHA256 << CRYPTO_AUTH_SIZE) |
  4405. (CRYPTO_AUTH_ALG_SHA << CRYPTO_AUTH_ALG) |
  4406. (1 << CRYPTO_LAST) | (1 << CRYPTO_FIRST);
  4407. pce_dev->reg.auth_cfg_aes_ccm_128 =
  4408. (1 << CRYPTO_LAST) | (1 << CRYPTO_FIRST) |
  4409. (CRYPTO_AUTH_MODE_CCM << CRYPTO_AUTH_MODE)|
  4410. (CRYPTO_AUTH_ALG_AES << CRYPTO_AUTH_ALG) |
  4411. (CRYPTO_AUTH_KEY_SZ_AES128 << CRYPTO_AUTH_KEY_SIZE) |
  4412. ((MAX_NONCE/sizeof(uint32_t)) << CRYPTO_AUTH_NONCE_NUM_WORDS);
  4413. pce_dev->reg.auth_cfg_aes_ccm_128 &= ~(1 << CRYPTO_USE_HW_KEY_AUTH);
  4414. pce_dev->reg.auth_cfg_aes_ccm_256 =
  4415. (1 << CRYPTO_LAST) | (1 << CRYPTO_FIRST) |
  4416. (CRYPTO_AUTH_MODE_CCM << CRYPTO_AUTH_MODE)|
  4417. (CRYPTO_AUTH_ALG_AES << CRYPTO_AUTH_ALG) |
  4418. (CRYPTO_AUTH_KEY_SZ_AES256 << CRYPTO_AUTH_KEY_SIZE) |
  4419. ((MAX_NONCE/sizeof(uint32_t)) << CRYPTO_AUTH_NONCE_NUM_WORDS);
  4420. pce_dev->reg.auth_cfg_aes_ccm_256 &= ~(1 << CRYPTO_USE_HW_KEY_AUTH);
  4421. /* Initialize auth_cfg register for kasumi/snow3g */
  4422. pce_dev->reg.auth_cfg_kasumi =
  4423. (CRYPTO_AUTH_ALG_KASUMI << CRYPTO_AUTH_ALG) |
  4424. BIT(CRYPTO_FIRST) | BIT(CRYPTO_LAST);
  4425. pce_dev->reg.auth_cfg_snow3g =
  4426. (CRYPTO_AUTH_ALG_SNOW3G << CRYPTO_AUTH_ALG) |
  4427. BIT(CRYPTO_FIRST) | BIT(CRYPTO_LAST);
  4428. /* Initialize IV counter mask values */
  4429. pce_dev->reg.encr_cntr_mask_3 = 0xFFFFFFFF;
  4430. pce_dev->reg.encr_cntr_mask_2 = 0xFFFFFFFF;
  4431. pce_dev->reg.encr_cntr_mask_1 = 0xFFFFFFFF;
  4432. pce_dev->reg.encr_cntr_mask_0 = 0xFFFFFFFF;
  4433. return 0;
  4434. }
  4435. static void _qce_ccm_get_around_input(struct qce_device *pce_dev,
  4436. struct ce_request_info *preq_info, enum qce_cipher_dir_enum dir)
  4437. {
  4438. struct qce_cmdlist_info *cmdlistinfo;
  4439. struct ce_sps_data *pce_sps_data;
  4440. pce_sps_data = &preq_info->ce_sps;
  4441. if ((dir == QCE_DECRYPT) && pce_dev->no_get_around &&
  4442. !(pce_dev->no_ccm_mac_status_get_around)) {
  4443. cmdlistinfo = &pce_sps_data->cmdlistptr.cipher_null;
  4444. _qce_sps_add_cmd(pce_dev, 0, cmdlistinfo,
  4445. &pce_sps_data->in_transfer);
  4446. _qce_sps_add_data(GET_PHYS_ADDR(pce_sps_data->ignore_buffer),
  4447. pce_dev->ce_bam_info.ce_burst_size,
  4448. &pce_sps_data->in_transfer);
  4449. _qce_set_flag(&pce_sps_data->in_transfer,
  4450. SPS_IOVEC_FLAG_EOT | SPS_IOVEC_FLAG_NWD);
  4451. }
  4452. }
  4453. static void _qce_ccm_get_around_output(struct qce_device *pce_dev,
  4454. struct ce_request_info *preq_info, enum qce_cipher_dir_enum dir)
  4455. {
  4456. struct ce_sps_data *pce_sps_data;
  4457. pce_sps_data = &preq_info->ce_sps;
  4458. if ((dir == QCE_DECRYPT) && pce_dev->no_get_around &&
  4459. !(pce_dev->no_ccm_mac_status_get_around)) {
  4460. _qce_sps_add_data(GET_PHYS_ADDR(pce_sps_data->ignore_buffer),
  4461. pce_dev->ce_bam_info.ce_burst_size,
  4462. &pce_sps_data->out_transfer);
  4463. _qce_sps_add_data(GET_PHYS_ADDR(pce_sps_data->result_dump_null),
  4464. CRYPTO_RESULT_DUMP_SIZE, &pce_sps_data->out_transfer);
  4465. }
  4466. }
  4467. /* QCE_DUMMY_REQ */
  4468. static void qce_dummy_complete(void *cookie, unsigned char *digest,
  4469. unsigned char *authdata, int ret)
  4470. {
  4471. if (!cookie)
  4472. pr_err("invalid cookie\n");
  4473. }
  4474. static int qce_dummy_req(struct qce_device *pce_dev)
  4475. {
  4476. int ret = 0;
  4477. if (atomic_xchg(
  4478. &pce_dev->ce_request_info[DUMMY_REQ_INDEX].in_use, true))
  4479. return -EBUSY;
  4480. ret = qce_process_sha_req(pce_dev, NULL);
  4481. pce_dev->qce_stats.no_of_dummy_reqs++;
  4482. return ret;
  4483. }
  4484. static int select_mode(struct qce_device *pce_dev,
  4485. struct ce_request_info *preq_info)
  4486. {
  4487. struct ce_sps_data *pce_sps_data = &preq_info->ce_sps;
  4488. unsigned int no_of_queued_req;
  4489. unsigned int cadence;
  4490. if (!pce_dev->no_get_around) {
  4491. _qce_set_flag(&pce_sps_data->out_transfer, SPS_IOVEC_FLAG_INT);
  4492. return 0;
  4493. }
  4494. /*
  4495. * claim ownership of device
  4496. */
  4497. again:
  4498. if (cmpxchg(&pce_dev->owner, QCE_OWNER_NONE, QCE_OWNER_CLIENT)
  4499. != QCE_OWNER_NONE) {
  4500. ndelay(40);
  4501. goto again;
  4502. }
  4503. no_of_queued_req = atomic_inc_return(&pce_dev->no_of_queued_req);
  4504. if (pce_dev->mode == IN_INTERRUPT_MODE) {
  4505. if (no_of_queued_req >= MAX_BUNCH_MODE_REQ) {
  4506. pce_dev->mode = IN_BUNCH_MODE;
  4507. pr_debug("pcedev %d mode switch to BUNCH\n",
  4508. pce_dev->dev_no);
  4509. _qce_set_flag(&pce_sps_data->out_transfer,
  4510. SPS_IOVEC_FLAG_INT);
  4511. pce_dev->intr_cadence = 0;
  4512. atomic_set(&pce_dev->bunch_cmd_seq, 1);
  4513. atomic_set(&pce_dev->last_intr_seq, 1);
  4514. mod_timer(&(pce_dev->timer),
  4515. (jiffies + DELAY_IN_JIFFIES));
  4516. } else {
  4517. _qce_set_flag(&pce_sps_data->out_transfer,
  4518. SPS_IOVEC_FLAG_INT);
  4519. }
  4520. } else {
  4521. pce_dev->intr_cadence++;
  4522. cadence = (preq_info->req_len >> 7) + 1;
  4523. if (cadence > SET_INTR_AT_REQ)
  4524. cadence = SET_INTR_AT_REQ;
  4525. if (pce_dev->intr_cadence < cadence || ((pce_dev->intr_cadence
  4526. == cadence) && pce_dev->cadence_flag))
  4527. atomic_inc(&pce_dev->bunch_cmd_seq);
  4528. else {
  4529. _qce_set_flag(&pce_sps_data->out_transfer,
  4530. SPS_IOVEC_FLAG_INT);
  4531. pce_dev->intr_cadence = 0;
  4532. atomic_set(&pce_dev->bunch_cmd_seq, 0);
  4533. atomic_set(&pce_dev->last_intr_seq, 0);
  4534. pce_dev->cadence_flag = !pce_dev->cadence_flag;
  4535. }
  4536. }
  4537. return 0;
  4538. }
  4539. static int _qce_aead_ccm_req(void *handle, struct qce_req *q_req)
  4540. {
  4541. int rc = 0;
  4542. struct qce_device *pce_dev = (struct qce_device *) handle;
  4543. struct aead_request *areq = (struct aead_request *) q_req->areq;
  4544. uint32_t authsize = q_req->authsize;
  4545. uint32_t totallen_in, out_len;
  4546. uint32_t hw_pad_out = 0;
  4547. int ce_burst_size;
  4548. struct qce_cmdlist_info *cmdlistinfo = NULL;
  4549. int req_info = -1;
  4550. struct ce_request_info *preq_info;
  4551. struct ce_sps_data *pce_sps_data;
  4552. req_info = qce_alloc_req_info(pce_dev);
  4553. if (req_info < 0)
  4554. return -EBUSY;
  4555. q_req->current_req_info = req_info;
  4556. preq_info = &pce_dev->ce_request_info[req_info];
  4557. pce_sps_data = &preq_info->ce_sps;
  4558. ce_burst_size = pce_dev->ce_bam_info.ce_burst_size;
  4559. totallen_in = areq->cryptlen + q_req->assoclen;
  4560. if (q_req->dir == QCE_ENCRYPT) {
  4561. q_req->cryptlen = areq->cryptlen;
  4562. out_len = areq->cryptlen + authsize;
  4563. hw_pad_out = ALIGN(authsize, ce_burst_size) - authsize;
  4564. } else {
  4565. q_req->cryptlen = areq->cryptlen - authsize;
  4566. out_len = q_req->cryptlen;
  4567. hw_pad_out = authsize;
  4568. }
  4569. /*
  4570. * For crypto 5.0 that has burst size alignment requirement
  4571. * for data descritpor,
  4572. * the agent above(qcrypto) prepares the src scatter list with
  4573. * memory starting with associated data, followed by
  4574. * data stream to be ciphered.
  4575. * The destination scatter list is pointing to the same
  4576. * data area as source.
  4577. */
  4578. if (pce_dev->ce_bam_info.minor_version == 0)
  4579. preq_info->src_nents = count_sg(areq->src, totallen_in);
  4580. else
  4581. preq_info->src_nents = count_sg(areq->src, areq->cryptlen +
  4582. areq->assoclen);
  4583. if (q_req->assoclen) {
  4584. preq_info->assoc_nents = count_sg(q_req->asg, q_req->assoclen);
  4585. /* formatted associated data input */
  4586. qce_dma_map_sg(pce_dev->pdev, q_req->asg,
  4587. preq_info->assoc_nents, DMA_TO_DEVICE);
  4588. preq_info->asg = q_req->asg;
  4589. } else {
  4590. preq_info->assoc_nents = 0;
  4591. preq_info->asg = NULL;
  4592. }
  4593. /* cipher input */
  4594. qce_dma_map_sg(pce_dev->pdev, areq->src, preq_info->src_nents,
  4595. (areq->src == areq->dst) ? DMA_BIDIRECTIONAL :
  4596. DMA_TO_DEVICE);
  4597. /* cipher + mac output for encryption */
  4598. if (areq->src != areq->dst) {
  4599. /*
  4600. * The destination scatter list is pointing to the same
  4601. * data area as src.
  4602. * Note, the associated data will be pass-through
  4603. * at the beginning of destination area.
  4604. */
  4605. preq_info->dst_nents = count_sg(areq->dst,
  4606. out_len + areq->assoclen);
  4607. qce_dma_map_sg(pce_dev->pdev, areq->dst, preq_info->dst_nents,
  4608. DMA_FROM_DEVICE);
  4609. } else {
  4610. preq_info->dst_nents = preq_info->src_nents;
  4611. }
  4612. if (pce_dev->support_cmd_dscr) {
  4613. cmdlistinfo = _ce_get_cipher_cmdlistinfo(pce_dev, req_info,
  4614. q_req);
  4615. if (cmdlistinfo == NULL) {
  4616. pr_err("Unsupported cipher algorithm %d, mode %d\n",
  4617. q_req->alg, q_req->mode);
  4618. qce_free_req_info(pce_dev, req_info, false);
  4619. return -EINVAL;
  4620. }
  4621. /* set up crypto device */
  4622. rc = _ce_setup_cipher(pce_dev, q_req, totallen_in,
  4623. q_req->assoclen, cmdlistinfo);
  4624. } else {
  4625. /* set up crypto device */
  4626. rc = _ce_setup_cipher_direct(pce_dev, q_req, totallen_in,
  4627. q_req->assoclen);
  4628. }
  4629. if (rc < 0)
  4630. goto bad;
  4631. preq_info->mode = q_req->mode;
  4632. /* setup for callback, and issue command to bam */
  4633. preq_info->areq = q_req->areq;
  4634. preq_info->qce_cb = q_req->qce_cb;
  4635. preq_info->dir = q_req->dir;
  4636. /* setup xfer type for producer callback handling */
  4637. preq_info->xfer_type = QCE_XFER_AEAD;
  4638. preq_info->req_len = totallen_in;
  4639. _qce_sps_iovec_count_init(pce_dev, req_info);
  4640. if (pce_dev->support_cmd_dscr && cmdlistinfo) {
  4641. rc = _qce_sps_add_cmd(pce_dev, SPS_IOVEC_FLAG_LOCK,
  4642. cmdlistinfo, &pce_sps_data->in_transfer);
  4643. if (rc)
  4644. goto bad;
  4645. }
  4646. if (pce_dev->ce_bam_info.minor_version == 0) {
  4647. goto bad;
  4648. } else {
  4649. if (q_req->assoclen) {
  4650. rc = _qce_sps_add_sg_data(pce_dev, q_req->asg,
  4651. q_req->assoclen, &pce_sps_data->in_transfer);
  4652. if (rc)
  4653. goto bad;
  4654. }
  4655. rc = _qce_sps_add_sg_data_off(pce_dev, areq->src, areq->cryptlen,
  4656. areq->assoclen,
  4657. &pce_sps_data->in_transfer);
  4658. if (rc)
  4659. goto bad;
  4660. _qce_set_flag(&pce_sps_data->in_transfer,
  4661. SPS_IOVEC_FLAG_EOT|SPS_IOVEC_FLAG_NWD);
  4662. _qce_ccm_get_around_input(pce_dev, preq_info, q_req->dir);
  4663. if (pce_dev->no_get_around) {
  4664. rc = _qce_sps_add_cmd(pce_dev, SPS_IOVEC_FLAG_UNLOCK,
  4665. &pce_sps_data->cmdlistptr.unlock_all_pipes,
  4666. &pce_sps_data->in_transfer);
  4667. if (rc)
  4668. goto bad;
  4669. }
  4670. /* Pass through to ignore associated data*/
  4671. rc = _qce_sps_add_data(
  4672. GET_PHYS_ADDR(pce_sps_data->ignore_buffer),
  4673. q_req->assoclen,
  4674. &pce_sps_data->out_transfer);
  4675. if (rc)
  4676. goto bad;
  4677. rc = _qce_sps_add_sg_data_off(pce_dev, areq->dst, out_len,
  4678. areq->assoclen,
  4679. &pce_sps_data->out_transfer);
  4680. if (rc)
  4681. goto bad;
  4682. /* Pass through to ignore hw_pad (padding of the MAC data) */
  4683. rc = _qce_sps_add_data(
  4684. GET_PHYS_ADDR(pce_sps_data->ignore_buffer),
  4685. hw_pad_out, &pce_sps_data->out_transfer);
  4686. if (rc)
  4687. goto bad;
  4688. if (pce_dev->no_get_around ||
  4689. totallen_in <= SPS_MAX_PKT_SIZE) {
  4690. rc = _qce_sps_add_data(
  4691. GET_PHYS_ADDR(pce_sps_data->result_dump),
  4692. CRYPTO_RESULT_DUMP_SIZE,
  4693. &pce_sps_data->out_transfer);
  4694. if (rc)
  4695. goto bad;
  4696. pce_sps_data->producer_state = QCE_PIPE_STATE_COMP;
  4697. } else {
  4698. pce_sps_data->producer_state = QCE_PIPE_STATE_IDLE;
  4699. }
  4700. _qce_ccm_get_around_output(pce_dev, preq_info, q_req->dir);
  4701. select_mode(pce_dev, preq_info);
  4702. rc = _qce_sps_transfer(pce_dev, req_info);
  4703. cmpxchg(&pce_dev->owner, QCE_OWNER_CLIENT, QCE_OWNER_NONE);
  4704. }
  4705. if (rc)
  4706. goto bad;
  4707. return 0;
  4708. bad:
  4709. if (preq_info->assoc_nents) {
  4710. qce_dma_unmap_sg(pce_dev->pdev, q_req->asg,
  4711. preq_info->assoc_nents, DMA_TO_DEVICE);
  4712. }
  4713. if (preq_info->src_nents) {
  4714. qce_dma_unmap_sg(pce_dev->pdev, areq->src, preq_info->src_nents,
  4715. (areq->src == areq->dst) ? DMA_BIDIRECTIONAL :
  4716. DMA_TO_DEVICE);
  4717. }
  4718. if (areq->src != areq->dst) {
  4719. qce_dma_unmap_sg(pce_dev->pdev, areq->dst, preq_info->dst_nents,
  4720. DMA_FROM_DEVICE);
  4721. }
  4722. qce_free_req_info(pce_dev, req_info, false);
  4723. return rc;
  4724. }
  4725. static int _qce_suspend(void *handle)
  4726. {
  4727. struct qce_device *pce_dev = (struct qce_device *)handle;
  4728. struct sps_pipe *sps_pipe_info;
  4729. int i = 0;
  4730. if (handle == NULL)
  4731. return -ENODEV;
  4732. for (i = 0; i < QCE_OFFLOAD_OPER_LAST; i++) {
  4733. if (i == QCE_OFFLOAD_NONE && !(pce_dev->kernel_pipes_support))
  4734. continue;
  4735. else if ((i > 0) && !(pce_dev->offload_pipes_support))
  4736. break;
  4737. sps_pipe_info = pce_dev->ce_bam_info.consumer[i].pipe;
  4738. sps_disconnect(sps_pipe_info);
  4739. sps_pipe_info = pce_dev->ce_bam_info.producer[i].pipe;
  4740. sps_disconnect(sps_pipe_info);
  4741. }
  4742. return 0;
  4743. }
  4744. static int _qce_resume(void *handle)
  4745. {
  4746. struct qce_device *pce_dev = (struct qce_device *)handle;
  4747. struct sps_pipe *sps_pipe_info;
  4748. struct sps_connect *sps_connect_info;
  4749. int rc, i;
  4750. rc = -ENODEV;
  4751. if (handle == NULL)
  4752. return rc;
  4753. for (i = 0; i < QCE_OFFLOAD_OPER_LAST; i++) {
  4754. if (i == QCE_OFFLOAD_NONE && !(pce_dev->kernel_pipes_support))
  4755. continue;
  4756. else if ((i > 0) && !(pce_dev->offload_pipes_support))
  4757. break;
  4758. sps_pipe_info = pce_dev->ce_bam_info.consumer[i].pipe;
  4759. sps_connect_info = &pce_dev->ce_bam_info.consumer[i].connect;
  4760. memset(sps_connect_info->desc.base, 0x00,
  4761. sps_connect_info->desc.size);
  4762. rc = sps_connect(sps_pipe_info, sps_connect_info);
  4763. if (rc) {
  4764. pr_err("sps_connect() fail pipe=0x%lx, rc = %d\n",
  4765. (uintptr_t)sps_pipe_info, rc);
  4766. return rc;
  4767. }
  4768. sps_pipe_info = pce_dev->ce_bam_info.producer[i].pipe;
  4769. sps_connect_info = &pce_dev->ce_bam_info.producer[i].connect;
  4770. memset(sps_connect_info->desc.base, 0x00,
  4771. sps_connect_info->desc.size);
  4772. rc = sps_connect(sps_pipe_info, sps_connect_info);
  4773. if (rc)
  4774. pr_err("sps_connect() fail pipe=0x%lx, rc = %d\n",
  4775. (uintptr_t)sps_pipe_info, rc);
  4776. rc = sps_register_event(sps_pipe_info,
  4777. &pce_dev->ce_bam_info.producer[i].event);
  4778. if (rc)
  4779. pr_err("Producer cb registration failed rc = %d\n",
  4780. rc);
  4781. }
  4782. qce_enable_clock_gating(pce_dev);
  4783. return rc;
  4784. }
  4785. struct qce_pm_table qce_pm_table = {_qce_suspend, _qce_resume};
  4786. EXPORT_SYMBOL(qce_pm_table);
  4787. int qce_aead_req(void *handle, struct qce_req *q_req)
  4788. {
  4789. struct qce_device *pce_dev = (struct qce_device *)handle;
  4790. struct aead_request *areq;
  4791. uint32_t authsize;
  4792. struct crypto_aead *aead;
  4793. uint32_t ivsize;
  4794. uint32_t totallen;
  4795. int rc = 0;
  4796. struct qce_cmdlist_info *cmdlistinfo = NULL;
  4797. int req_info = -1;
  4798. struct ce_sps_data *pce_sps_data;
  4799. struct ce_request_info *preq_info;
  4800. if (q_req->mode == QCE_MODE_CCM)
  4801. return _qce_aead_ccm_req(handle, q_req);
  4802. req_info = qce_alloc_req_info(pce_dev);
  4803. if (req_info < 0)
  4804. return -EBUSY;
  4805. q_req->current_req_info = req_info;
  4806. preq_info = &pce_dev->ce_request_info[req_info];
  4807. pce_sps_data = &preq_info->ce_sps;
  4808. areq = (struct aead_request *) q_req->areq;
  4809. aead = crypto_aead_reqtfm(areq);
  4810. ivsize = crypto_aead_ivsize(aead);
  4811. q_req->ivsize = ivsize;
  4812. authsize = q_req->authsize;
  4813. if (q_req->dir == QCE_ENCRYPT)
  4814. q_req->cryptlen = areq->cryptlen;
  4815. else
  4816. q_req->cryptlen = areq->cryptlen - authsize;
  4817. if (q_req->cryptlen > UINT_MAX - areq->assoclen) {
  4818. pr_err("Integer overflow on total aead req length.\n");
  4819. return -EINVAL;
  4820. }
  4821. totallen = q_req->cryptlen + areq->assoclen;
  4822. if (pce_dev->support_cmd_dscr) {
  4823. cmdlistinfo = _ce_get_aead_cmdlistinfo(pce_dev,
  4824. req_info, q_req);
  4825. if (cmdlistinfo == NULL) {
  4826. pr_err("Unsupported aead ciphering algorithm %d, mode %d, ciphering key length %d, auth digest size %d\n",
  4827. q_req->alg, q_req->mode, q_req->encklen,
  4828. q_req->authsize);
  4829. qce_free_req_info(pce_dev, req_info, false);
  4830. return -EINVAL;
  4831. }
  4832. /* set up crypto device */
  4833. rc = _ce_setup_aead(pce_dev, q_req, totallen,
  4834. areq->assoclen, cmdlistinfo);
  4835. if (rc < 0) {
  4836. qce_free_req_info(pce_dev, req_info, false);
  4837. return -EINVAL;
  4838. }
  4839. }
  4840. /*
  4841. * For crypto 5.0 that has burst size alignment requirement
  4842. * for data descritpor,
  4843. * the agent above(qcrypto) prepares the src scatter list with
  4844. * memory starting with associated data, followed by
  4845. * iv, and data stream to be ciphered.
  4846. */
  4847. preq_info->src_nents = count_sg(areq->src, totallen);
  4848. /* cipher input */
  4849. qce_dma_map_sg(pce_dev->pdev, areq->src, preq_info->src_nents,
  4850. (areq->src == areq->dst) ? DMA_BIDIRECTIONAL :
  4851. DMA_TO_DEVICE);
  4852. /* cipher output for encryption */
  4853. if (areq->src != areq->dst) {
  4854. preq_info->dst_nents = count_sg(areq->dst, totallen);
  4855. qce_dma_map_sg(pce_dev->pdev, areq->dst, preq_info->dst_nents,
  4856. DMA_FROM_DEVICE);
  4857. }
  4858. /* setup for callback, and issue command to bam */
  4859. preq_info->areq = q_req->areq;
  4860. preq_info->qce_cb = q_req->qce_cb;
  4861. preq_info->dir = q_req->dir;
  4862. preq_info->asg = NULL;
  4863. preq_info->offload_op = QCE_OFFLOAD_NONE;
  4864. /* setup xfer type for producer callback handling */
  4865. preq_info->xfer_type = QCE_XFER_AEAD;
  4866. preq_info->req_len = totallen;
  4867. _qce_sps_iovec_count_init(pce_dev, req_info);
  4868. if (pce_dev->support_cmd_dscr) {
  4869. rc = _qce_sps_add_cmd(pce_dev, SPS_IOVEC_FLAG_LOCK,
  4870. cmdlistinfo, &pce_sps_data->in_transfer);
  4871. if (rc)
  4872. goto bad;
  4873. } else {
  4874. rc = _ce_setup_aead_direct(pce_dev, q_req, totallen,
  4875. areq->assoclen);
  4876. if (rc)
  4877. goto bad;
  4878. }
  4879. preq_info->mode = q_req->mode;
  4880. if (pce_dev->ce_bam_info.minor_version == 0) {
  4881. rc = _qce_sps_add_sg_data(pce_dev, areq->src, totallen,
  4882. &pce_sps_data->in_transfer);
  4883. if (rc)
  4884. goto bad;
  4885. _qce_set_flag(&pce_sps_data->in_transfer,
  4886. SPS_IOVEC_FLAG_EOT|SPS_IOVEC_FLAG_NWD);
  4887. rc = _qce_sps_add_sg_data(pce_dev, areq->dst, totallen,
  4888. &pce_sps_data->out_transfer);
  4889. if (rc)
  4890. goto bad;
  4891. if (totallen > SPS_MAX_PKT_SIZE) {
  4892. _qce_set_flag(&pce_sps_data->out_transfer,
  4893. SPS_IOVEC_FLAG_INT);
  4894. pce_sps_data->producer_state = QCE_PIPE_STATE_IDLE;
  4895. } else {
  4896. rc = _qce_sps_add_data(GET_PHYS_ADDR(
  4897. pce_sps_data->result_dump),
  4898. CRYPTO_RESULT_DUMP_SIZE,
  4899. &pce_sps_data->out_transfer);
  4900. if (rc)
  4901. goto bad;
  4902. _qce_set_flag(&pce_sps_data->out_transfer,
  4903. SPS_IOVEC_FLAG_INT);
  4904. pce_sps_data->producer_state = QCE_PIPE_STATE_COMP;
  4905. }
  4906. rc = _qce_sps_transfer(pce_dev, req_info);
  4907. } else {
  4908. rc = _qce_sps_add_sg_data(pce_dev, areq->src, totallen,
  4909. &pce_sps_data->in_transfer);
  4910. if (rc)
  4911. goto bad;
  4912. _qce_set_flag(&pce_sps_data->in_transfer,
  4913. SPS_IOVEC_FLAG_EOT|SPS_IOVEC_FLAG_NWD);
  4914. if (pce_dev->no_get_around) {
  4915. rc = _qce_sps_add_cmd(pce_dev, SPS_IOVEC_FLAG_UNLOCK,
  4916. &pce_sps_data->cmdlistptr.unlock_all_pipes,
  4917. &pce_sps_data->in_transfer);
  4918. if (rc)
  4919. goto bad;
  4920. }
  4921. rc = _qce_sps_add_sg_data(pce_dev, areq->dst, totallen,
  4922. &pce_sps_data->out_transfer);
  4923. if (rc)
  4924. goto bad;
  4925. if (pce_dev->no_get_around || totallen <= SPS_MAX_PKT_SIZE) {
  4926. rc = _qce_sps_add_data(
  4927. GET_PHYS_ADDR(pce_sps_data->result_dump),
  4928. CRYPTO_RESULT_DUMP_SIZE,
  4929. &pce_sps_data->out_transfer);
  4930. if (rc)
  4931. goto bad;
  4932. pce_sps_data->producer_state = QCE_PIPE_STATE_COMP;
  4933. } else {
  4934. pce_sps_data->producer_state = QCE_PIPE_STATE_IDLE;
  4935. }
  4936. select_mode(pce_dev, preq_info);
  4937. rc = _qce_sps_transfer(pce_dev, req_info);
  4938. cmpxchg(&pce_dev->owner, QCE_OWNER_CLIENT, QCE_OWNER_NONE);
  4939. }
  4940. if (rc)
  4941. goto bad;
  4942. return 0;
  4943. bad:
  4944. if (preq_info->src_nents)
  4945. qce_dma_unmap_sg(pce_dev->pdev, areq->src, preq_info->src_nents,
  4946. (areq->src == areq->dst) ? DMA_BIDIRECTIONAL :
  4947. DMA_TO_DEVICE);
  4948. if (areq->src != areq->dst)
  4949. qce_dma_unmap_sg(pce_dev->pdev, areq->dst, preq_info->dst_nents,
  4950. DMA_FROM_DEVICE);
  4951. qce_free_req_info(pce_dev, req_info, false);
  4952. return rc;
  4953. }
  4954. EXPORT_SYMBOL(qce_aead_req);
  4955. int qce_ablk_cipher_req(void *handle, struct qce_req *c_req)
  4956. {
  4957. int rc = 0;
  4958. struct qce_device *pce_dev = (struct qce_device *) handle;
  4959. struct skcipher_request *areq = (struct skcipher_request *)
  4960. c_req->areq;
  4961. struct qce_cmdlist_info *cmdlistinfo = NULL;
  4962. int req_info = -1;
  4963. struct ce_sps_data *pce_sps_data;
  4964. struct ce_request_info *preq_info;
  4965. req_info = qce_alloc_req_info(pce_dev);
  4966. if (req_info < 0)
  4967. return -EBUSY;
  4968. c_req->current_req_info = req_info;
  4969. preq_info = &pce_dev->ce_request_info[req_info];
  4970. pce_sps_data = &preq_info->ce_sps;
  4971. preq_info->src_nents = 0;
  4972. preq_info->dst_nents = 0;
  4973. /* cipher input */
  4974. preq_info->src_nents = count_sg(areq->src, areq->cryptlen);
  4975. if (!is_offload_op(c_req->offload_op))
  4976. qce_dma_map_sg(pce_dev->pdev, areq->src,
  4977. preq_info->src_nents,
  4978. (areq->src == areq->dst) ? DMA_BIDIRECTIONAL :
  4979. DMA_TO_DEVICE);
  4980. /* cipher output */
  4981. if (areq->src != areq->dst) {
  4982. preq_info->dst_nents = count_sg(areq->dst, areq->cryptlen);
  4983. if (!is_offload_op(c_req->offload_op))
  4984. qce_dma_map_sg(pce_dev->pdev, areq->dst,
  4985. preq_info->dst_nents, DMA_FROM_DEVICE);
  4986. } else {
  4987. preq_info->dst_nents = preq_info->src_nents;
  4988. }
  4989. preq_info->dir = c_req->dir;
  4990. if ((pce_dev->ce_bam_info.minor_version == 0) &&
  4991. (preq_info->dir == QCE_DECRYPT) &&
  4992. (c_req->mode == QCE_MODE_CBC)) {
  4993. memcpy(preq_info->dec_iv, (unsigned char *)
  4994. sg_virt(areq->src) + areq->src->length - 16,
  4995. NUM_OF_CRYPTO_CNTR_IV_REG * CRYPTO_REG_SIZE);
  4996. }
  4997. /* set up crypto device */
  4998. if (pce_dev->support_cmd_dscr) {
  4999. cmdlistinfo = _ce_get_cipher_cmdlistinfo(pce_dev,
  5000. req_info, c_req);
  5001. if (cmdlistinfo == NULL) {
  5002. pr_err("Unsupported cipher algorithm %d, mode %d\n",
  5003. c_req->alg, c_req->mode);
  5004. qce_free_req_info(pce_dev, req_info, false);
  5005. return -EINVAL;
  5006. }
  5007. rc = _ce_setup_cipher(pce_dev, c_req, areq->cryptlen, 0,
  5008. cmdlistinfo);
  5009. } else {
  5010. rc = _ce_setup_cipher_direct(pce_dev, c_req, areq->cryptlen, 0);
  5011. }
  5012. if (rc < 0)
  5013. goto bad;
  5014. preq_info->mode = c_req->mode;
  5015. preq_info->offload_op = c_req->offload_op;
  5016. /* setup for client callback, and issue command to BAM */
  5017. preq_info->areq = areq;
  5018. preq_info->qce_cb = c_req->qce_cb;
  5019. /* setup xfer type for producer callback handling */
  5020. preq_info->xfer_type = QCE_XFER_CIPHERING;
  5021. preq_info->req_len = areq->cryptlen;
  5022. _qce_sps_iovec_count_init(pce_dev, req_info);
  5023. if (pce_dev->support_cmd_dscr && cmdlistinfo) {
  5024. rc = _qce_sps_add_cmd(pce_dev, SPS_IOVEC_FLAG_LOCK,
  5025. cmdlistinfo, &pce_sps_data->in_transfer);
  5026. if (rc)
  5027. goto bad;
  5028. }
  5029. rc = _qce_sps_add_data(areq->src->dma_address, areq->cryptlen,
  5030. &pce_sps_data->in_transfer);
  5031. if (rc)
  5032. goto bad;
  5033. _qce_set_flag(&pce_sps_data->in_transfer,
  5034. SPS_IOVEC_FLAG_EOT|SPS_IOVEC_FLAG_NWD);
  5035. if (pce_dev->no_get_around) {
  5036. rc = _qce_sps_add_cmd(pce_dev, SPS_IOVEC_FLAG_UNLOCK,
  5037. &pce_sps_data->cmdlistptr.unlock_all_pipes,
  5038. &pce_sps_data->in_transfer);
  5039. if (rc)
  5040. goto bad;
  5041. }
  5042. rc = _qce_sps_add_data(areq->dst->dma_address, areq->cryptlen,
  5043. &pce_sps_data->out_transfer);
  5044. if (rc)
  5045. goto bad;
  5046. if (pce_dev->no_get_around || areq->cryptlen <= SPS_MAX_PKT_SIZE) {
  5047. pce_sps_data->producer_state = QCE_PIPE_STATE_COMP;
  5048. if (!is_offload_op(c_req->offload_op)) {
  5049. rc = _qce_sps_add_data(
  5050. GET_PHYS_ADDR(pce_sps_data->result_dump),
  5051. CRYPTO_RESULT_DUMP_SIZE,
  5052. &pce_sps_data->out_transfer);
  5053. if (rc)
  5054. goto bad;
  5055. }
  5056. } else {
  5057. pce_sps_data->producer_state = QCE_PIPE_STATE_IDLE;
  5058. }
  5059. select_mode(pce_dev, preq_info);
  5060. rc = _qce_sps_transfer(pce_dev, req_info);
  5061. cmpxchg(&pce_dev->owner, QCE_OWNER_CLIENT, QCE_OWNER_NONE);
  5062. if (rc)
  5063. goto bad;
  5064. return 0;
  5065. bad:
  5066. if (!is_offload_op(c_req->offload_op)) {
  5067. if (areq->src != areq->dst)
  5068. if (preq_info->dst_nents)
  5069. qce_dma_unmap_sg(pce_dev->pdev, areq->dst,
  5070. preq_info->dst_nents, DMA_FROM_DEVICE);
  5071. if (preq_info->src_nents)
  5072. qce_dma_unmap_sg(pce_dev->pdev, areq->src,
  5073. preq_info->src_nents,
  5074. (areq->src == areq->dst) ?
  5075. DMA_BIDIRECTIONAL : DMA_TO_DEVICE);
  5076. }
  5077. qce_free_req_info(pce_dev, req_info, false);
  5078. return rc;
  5079. }
  5080. EXPORT_SYMBOL(qce_ablk_cipher_req);
  5081. int qce_process_sha_req(void *handle, struct qce_sha_req *sreq)
  5082. {
  5083. struct qce_device *pce_dev = (struct qce_device *) handle;
  5084. int rc;
  5085. struct ahash_request *areq;
  5086. struct qce_cmdlist_info *cmdlistinfo = NULL;
  5087. int req_info = -1;
  5088. struct ce_sps_data *pce_sps_data;
  5089. struct ce_request_info *preq_info;
  5090. bool is_dummy = false;
  5091. if (!sreq) {
  5092. sreq = &(pce_dev->dummyreq.sreq);
  5093. req_info = DUMMY_REQ_INDEX;
  5094. is_dummy = true;
  5095. } else {
  5096. req_info = qce_alloc_req_info(pce_dev);
  5097. if (req_info < 0)
  5098. return -EBUSY;
  5099. }
  5100. sreq->current_req_info = req_info;
  5101. areq = (struct ahash_request *)sreq->areq;
  5102. preq_info = &pce_dev->ce_request_info[req_info];
  5103. pce_sps_data = &preq_info->ce_sps;
  5104. preq_info->src_nents = count_sg(sreq->src, sreq->size);
  5105. qce_dma_map_sg(pce_dev->pdev, sreq->src, preq_info->src_nents,
  5106. DMA_TO_DEVICE);
  5107. if (pce_dev->support_cmd_dscr) {
  5108. cmdlistinfo = _ce_get_hash_cmdlistinfo(pce_dev, req_info, sreq);
  5109. if (cmdlistinfo == NULL) {
  5110. pr_err("Unsupported hash algorithm %d\n", sreq->alg);
  5111. qce_free_req_info(pce_dev, req_info, false);
  5112. return -EINVAL;
  5113. }
  5114. rc = _ce_setup_hash(pce_dev, sreq, cmdlistinfo);
  5115. } else {
  5116. rc = _ce_setup_hash_direct(pce_dev, sreq);
  5117. }
  5118. if (rc < 0)
  5119. goto bad;
  5120. preq_info->areq = areq;
  5121. preq_info->qce_cb = sreq->qce_cb;
  5122. preq_info->offload_op = QCE_OFFLOAD_NONE;
  5123. /* setup xfer type for producer callback handling */
  5124. preq_info->xfer_type = QCE_XFER_HASHING;
  5125. preq_info->req_len = sreq->size;
  5126. _qce_sps_iovec_count_init(pce_dev, req_info);
  5127. if (pce_dev->support_cmd_dscr && cmdlistinfo) {
  5128. rc = _qce_sps_add_cmd(pce_dev, SPS_IOVEC_FLAG_LOCK,
  5129. cmdlistinfo, &pce_sps_data->in_transfer);
  5130. if (rc)
  5131. goto bad;
  5132. }
  5133. rc = _qce_sps_add_sg_data(pce_dev, areq->src, areq->nbytes,
  5134. &pce_sps_data->in_transfer);
  5135. if (rc)
  5136. goto bad;
  5137. /* always ensure there is input data. ZLT does not work for bam-ndp */
  5138. if (!areq->nbytes) {
  5139. rc = _qce_sps_add_data(
  5140. GET_PHYS_ADDR(pce_sps_data->ignore_buffer),
  5141. pce_dev->ce_bam_info.ce_burst_size,
  5142. &pce_sps_data->in_transfer);
  5143. if (rc)
  5144. goto bad;
  5145. }
  5146. _qce_set_flag(&pce_sps_data->in_transfer,
  5147. SPS_IOVEC_FLAG_EOT|SPS_IOVEC_FLAG_NWD);
  5148. if (pce_dev->no_get_around) {
  5149. rc = _qce_sps_add_cmd(pce_dev, SPS_IOVEC_FLAG_UNLOCK,
  5150. &pce_sps_data->cmdlistptr.unlock_all_pipes,
  5151. &pce_sps_data->in_transfer);
  5152. if (rc)
  5153. goto bad;
  5154. }
  5155. rc = _qce_sps_add_data(GET_PHYS_ADDR(pce_sps_data->result_dump),
  5156. CRYPTO_RESULT_DUMP_SIZE,
  5157. &pce_sps_data->out_transfer);
  5158. if (rc)
  5159. goto bad;
  5160. if (is_dummy) {
  5161. _qce_set_flag(&pce_sps_data->out_transfer, SPS_IOVEC_FLAG_INT);
  5162. rc = _qce_sps_transfer(pce_dev, req_info);
  5163. } else {
  5164. select_mode(pce_dev, preq_info);
  5165. rc = _qce_sps_transfer(pce_dev, req_info);
  5166. cmpxchg(&pce_dev->owner, QCE_OWNER_CLIENT, QCE_OWNER_NONE);
  5167. }
  5168. if (rc)
  5169. goto bad;
  5170. return 0;
  5171. bad:
  5172. if (preq_info->src_nents) {
  5173. qce_dma_unmap_sg(pce_dev->pdev, sreq->src,
  5174. preq_info->src_nents, DMA_TO_DEVICE);
  5175. }
  5176. qce_free_req_info(pce_dev, req_info, false);
  5177. return rc;
  5178. }
  5179. EXPORT_SYMBOL(qce_process_sha_req);
  5180. int qce_f8_req(void *handle, struct qce_f8_req *req,
  5181. void *cookie, qce_comp_func_ptr_t qce_cb)
  5182. {
  5183. struct qce_device *pce_dev = (struct qce_device *) handle;
  5184. bool key_stream_mode;
  5185. dma_addr_t dst;
  5186. int rc;
  5187. struct qce_cmdlist_info *cmdlistinfo;
  5188. int req_info = -1;
  5189. struct ce_request_info *preq_info;
  5190. struct ce_sps_data *pce_sps_data;
  5191. req_info = qce_alloc_req_info(pce_dev);
  5192. if (req_info < 0)
  5193. return -EBUSY;
  5194. req->current_req_info = req_info;
  5195. preq_info = &pce_dev->ce_request_info[req_info];
  5196. pce_sps_data = &preq_info->ce_sps;
  5197. switch (req->algorithm) {
  5198. case QCE_OTA_ALGO_KASUMI:
  5199. cmdlistinfo = &pce_sps_data->cmdlistptr.f8_kasumi;
  5200. break;
  5201. case QCE_OTA_ALGO_SNOW3G:
  5202. cmdlistinfo = &pce_sps_data->cmdlistptr.f8_snow3g;
  5203. break;
  5204. default:
  5205. qce_free_req_info(pce_dev, req_info, false);
  5206. return -EINVAL;
  5207. }
  5208. key_stream_mode = (req->data_in == NULL);
  5209. /* don't support key stream mode */
  5210. if (key_stream_mode || (req->bearer >= QCE_OTA_MAX_BEARER)) {
  5211. qce_free_req_info(pce_dev, req_info, false);
  5212. return -EINVAL;
  5213. }
  5214. /* F8 cipher input */
  5215. preq_info->phy_ota_src = dma_map_single(pce_dev->pdev,
  5216. req->data_in, req->data_len,
  5217. (req->data_in == req->data_out) ?
  5218. DMA_BIDIRECTIONAL : DMA_TO_DEVICE);
  5219. /* F8 cipher output */
  5220. if (req->data_in != req->data_out) {
  5221. dst = dma_map_single(pce_dev->pdev, req->data_out,
  5222. req->data_len, DMA_FROM_DEVICE);
  5223. preq_info->phy_ota_dst = dst;
  5224. } else {
  5225. /* in place ciphering */
  5226. dst = preq_info->phy_ota_src;
  5227. preq_info->phy_ota_dst = 0;
  5228. }
  5229. preq_info->ota_size = req->data_len;
  5230. /* set up crypto device */
  5231. if (pce_dev->support_cmd_dscr)
  5232. rc = _ce_f8_setup(pce_dev, req, key_stream_mode, 1, 0,
  5233. req->data_len, cmdlistinfo);
  5234. else
  5235. rc = _ce_f8_setup_direct(pce_dev, req, key_stream_mode, 1, 0,
  5236. req->data_len);
  5237. if (rc < 0)
  5238. goto bad;
  5239. /* setup for callback, and issue command to sps */
  5240. preq_info->areq = cookie;
  5241. preq_info->qce_cb = qce_cb;
  5242. preq_info->offload_op = QCE_OFFLOAD_NONE;
  5243. /* setup xfer type for producer callback handling */
  5244. preq_info->xfer_type = QCE_XFER_F8;
  5245. preq_info->req_len = req->data_len;
  5246. _qce_sps_iovec_count_init(pce_dev, req_info);
  5247. if (pce_dev->support_cmd_dscr) {
  5248. rc = _qce_sps_add_cmd(pce_dev, SPS_IOVEC_FLAG_LOCK,
  5249. cmdlistinfo, &pce_sps_data->in_transfer);
  5250. if (rc)
  5251. goto bad;
  5252. }
  5253. rc = _qce_sps_add_data((uint32_t)preq_info->phy_ota_src, req->data_len,
  5254. &pce_sps_data->in_transfer);
  5255. if (rc)
  5256. goto bad;
  5257. _qce_set_flag(&pce_sps_data->in_transfer,
  5258. SPS_IOVEC_FLAG_EOT|SPS_IOVEC_FLAG_NWD);
  5259. rc = _qce_sps_add_cmd(pce_dev, SPS_IOVEC_FLAG_UNLOCK,
  5260. &pce_sps_data->cmdlistptr.unlock_all_pipes,
  5261. &pce_sps_data->in_transfer);
  5262. if (rc)
  5263. goto bad;
  5264. rc = _qce_sps_add_data((uint32_t)dst, req->data_len,
  5265. &pce_sps_data->out_transfer);
  5266. if (rc)
  5267. goto bad;
  5268. rc = _qce_sps_add_data(GET_PHYS_ADDR(pce_sps_data->result_dump),
  5269. CRYPTO_RESULT_DUMP_SIZE,
  5270. &pce_sps_data->out_transfer);
  5271. if (rc)
  5272. goto bad;
  5273. select_mode(pce_dev, preq_info);
  5274. rc = _qce_sps_transfer(pce_dev, req_info);
  5275. cmpxchg(&pce_dev->owner, QCE_OWNER_CLIENT, QCE_OWNER_NONE);
  5276. if (rc)
  5277. goto bad;
  5278. return 0;
  5279. bad:
  5280. if (preq_info->phy_ota_dst != 0)
  5281. dma_unmap_single(pce_dev->pdev, preq_info->phy_ota_dst,
  5282. req->data_len, DMA_FROM_DEVICE);
  5283. if (preq_info->phy_ota_src != 0)
  5284. dma_unmap_single(pce_dev->pdev, preq_info->phy_ota_src,
  5285. req->data_len,
  5286. (req->data_in == req->data_out) ?
  5287. DMA_BIDIRECTIONAL : DMA_TO_DEVICE);
  5288. qce_free_req_info(pce_dev, req_info, false);
  5289. return rc;
  5290. }
  5291. EXPORT_SYMBOL(qce_f8_req);
  5292. int qce_f8_multi_pkt_req(void *handle, struct qce_f8_multi_pkt_req *mreq,
  5293. void *cookie, qce_comp_func_ptr_t qce_cb)
  5294. {
  5295. struct qce_device *pce_dev = (struct qce_device *) handle;
  5296. uint16_t num_pkt = mreq->num_pkt;
  5297. uint16_t cipher_start = mreq->cipher_start;
  5298. uint16_t cipher_size = mreq->cipher_size;
  5299. struct qce_f8_req *req = &mreq->qce_f8_req;
  5300. uint32_t total;
  5301. dma_addr_t dst = 0;
  5302. int rc = 0;
  5303. struct qce_cmdlist_info *cmdlistinfo;
  5304. int req_info = -1;
  5305. struct ce_request_info *preq_info;
  5306. struct ce_sps_data *pce_sps_data;
  5307. req_info = qce_alloc_req_info(pce_dev);
  5308. if (req_info < 0)
  5309. return -EBUSY;
  5310. req->current_req_info = req_info;
  5311. preq_info = &pce_dev->ce_request_info[req_info];
  5312. pce_sps_data = &preq_info->ce_sps;
  5313. switch (req->algorithm) {
  5314. case QCE_OTA_ALGO_KASUMI:
  5315. cmdlistinfo = &pce_sps_data->cmdlistptr.f8_kasumi;
  5316. break;
  5317. case QCE_OTA_ALGO_SNOW3G:
  5318. cmdlistinfo = &pce_sps_data->cmdlistptr.f8_snow3g;
  5319. break;
  5320. default:
  5321. qce_free_req_info(pce_dev, req_info, false);
  5322. return -EINVAL;
  5323. }
  5324. total = num_pkt * req->data_len;
  5325. /* F8 cipher input */
  5326. preq_info->phy_ota_src = dma_map_single(pce_dev->pdev,
  5327. req->data_in, total,
  5328. (req->data_in == req->data_out) ?
  5329. DMA_BIDIRECTIONAL : DMA_TO_DEVICE);
  5330. /* F8 cipher output */
  5331. if (req->data_in != req->data_out) {
  5332. dst = dma_map_single(pce_dev->pdev, req->data_out, total,
  5333. DMA_FROM_DEVICE);
  5334. preq_info->phy_ota_dst = dst;
  5335. } else {
  5336. /* in place ciphering */
  5337. dst = preq_info->phy_ota_src;
  5338. preq_info->phy_ota_dst = 0;
  5339. }
  5340. preq_info->ota_size = total;
  5341. /* set up crypto device */
  5342. if (pce_dev->support_cmd_dscr)
  5343. rc = _ce_f8_setup(pce_dev, req, false, num_pkt, cipher_start,
  5344. cipher_size, cmdlistinfo);
  5345. else
  5346. rc = _ce_f8_setup_direct(pce_dev, req, false, num_pkt,
  5347. cipher_start, cipher_size);
  5348. if (rc)
  5349. goto bad;
  5350. /* setup for callback, and issue command to sps */
  5351. preq_info->areq = cookie;
  5352. preq_info->qce_cb = qce_cb;
  5353. preq_info->offload_op = QCE_OFFLOAD_NONE;
  5354. /* setup xfer type for producer callback handling */
  5355. preq_info->xfer_type = QCE_XFER_F8;
  5356. preq_info->req_len = total;
  5357. _qce_sps_iovec_count_init(pce_dev, req_info);
  5358. if (pce_dev->support_cmd_dscr) {
  5359. rc = _qce_sps_add_cmd(pce_dev, SPS_IOVEC_FLAG_LOCK,
  5360. cmdlistinfo, &pce_sps_data->in_transfer);
  5361. goto bad;
  5362. }
  5363. rc = _qce_sps_add_data((uint32_t)preq_info->phy_ota_src, total,
  5364. &pce_sps_data->in_transfer);
  5365. if (rc)
  5366. goto bad;
  5367. _qce_set_flag(&pce_sps_data->in_transfer,
  5368. SPS_IOVEC_FLAG_EOT|SPS_IOVEC_FLAG_NWD);
  5369. rc = _qce_sps_add_cmd(pce_dev, SPS_IOVEC_FLAG_UNLOCK,
  5370. &pce_sps_data->cmdlistptr.unlock_all_pipes,
  5371. &pce_sps_data->in_transfer);
  5372. if (rc)
  5373. goto bad;
  5374. rc = _qce_sps_add_data((uint32_t)dst, total,
  5375. &pce_sps_data->out_transfer);
  5376. if (rc)
  5377. goto bad;
  5378. rc = _qce_sps_add_data(GET_PHYS_ADDR(pce_sps_data->result_dump),
  5379. CRYPTO_RESULT_DUMP_SIZE,
  5380. &pce_sps_data->out_transfer);
  5381. if (rc)
  5382. goto bad;
  5383. select_mode(pce_dev, preq_info);
  5384. rc = _qce_sps_transfer(pce_dev, req_info);
  5385. cmpxchg(&pce_dev->owner, QCE_OWNER_CLIENT, QCE_OWNER_NONE);
  5386. if (rc == 0)
  5387. return 0;
  5388. bad:
  5389. if (preq_info->phy_ota_dst)
  5390. dma_unmap_single(pce_dev->pdev, preq_info->phy_ota_dst, total,
  5391. DMA_FROM_DEVICE);
  5392. dma_unmap_single(pce_dev->pdev, preq_info->phy_ota_src, total,
  5393. (req->data_in == req->data_out) ?
  5394. DMA_BIDIRECTIONAL : DMA_TO_DEVICE);
  5395. qce_free_req_info(pce_dev, req_info, false);
  5396. return rc;
  5397. }
  5398. EXPORT_SYMBOL(qce_f8_multi_pkt_req);
  5399. int qce_f9_req(void *handle, struct qce_f9_req *req, void *cookie,
  5400. qce_comp_func_ptr_t qce_cb)
  5401. {
  5402. struct qce_device *pce_dev = (struct qce_device *) handle;
  5403. int rc;
  5404. struct qce_cmdlist_info *cmdlistinfo;
  5405. int req_info = -1;
  5406. struct ce_sps_data *pce_sps_data;
  5407. struct ce_request_info *preq_info;
  5408. req_info = qce_alloc_req_info(pce_dev);
  5409. if (req_info < 0)
  5410. return -EBUSY;
  5411. req->current_req_info = req_info;
  5412. preq_info = &pce_dev->ce_request_info[req_info];
  5413. pce_sps_data = &preq_info->ce_sps;
  5414. switch (req->algorithm) {
  5415. case QCE_OTA_ALGO_KASUMI:
  5416. cmdlistinfo = &pce_sps_data->cmdlistptr.f9_kasumi;
  5417. break;
  5418. case QCE_OTA_ALGO_SNOW3G:
  5419. cmdlistinfo = &pce_sps_data->cmdlistptr.f9_snow3g;
  5420. break;
  5421. default:
  5422. qce_free_req_info(pce_dev, req_info, false);
  5423. return -EINVAL;
  5424. }
  5425. preq_info->phy_ota_src = dma_map_single(pce_dev->pdev, req->message,
  5426. req->msize, DMA_TO_DEVICE);
  5427. preq_info->ota_size = req->msize;
  5428. if (pce_dev->support_cmd_dscr)
  5429. rc = _ce_f9_setup(pce_dev, req, cmdlistinfo);
  5430. else
  5431. rc = _ce_f9_setup_direct(pce_dev, req);
  5432. if (rc < 0)
  5433. goto bad;
  5434. /* setup for callback, and issue command to sps */
  5435. preq_info->areq = cookie;
  5436. preq_info->qce_cb = qce_cb;
  5437. preq_info->offload_op = QCE_OFFLOAD_NONE;
  5438. /* setup xfer type for producer callback handling */
  5439. preq_info->xfer_type = QCE_XFER_F9;
  5440. preq_info->req_len = req->msize;
  5441. _qce_sps_iovec_count_init(pce_dev, req_info);
  5442. if (pce_dev->support_cmd_dscr) {
  5443. rc = _qce_sps_add_cmd(pce_dev, SPS_IOVEC_FLAG_LOCK,
  5444. cmdlistinfo, &pce_sps_data->in_transfer);
  5445. if (rc)
  5446. goto bad;
  5447. }
  5448. rc = _qce_sps_add_data((uint32_t)preq_info->phy_ota_src, req->msize,
  5449. &pce_sps_data->in_transfer);
  5450. if (rc)
  5451. goto bad;
  5452. _qce_set_flag(&pce_sps_data->in_transfer,
  5453. SPS_IOVEC_FLAG_EOT|SPS_IOVEC_FLAG_NWD);
  5454. rc = _qce_sps_add_cmd(pce_dev, SPS_IOVEC_FLAG_UNLOCK,
  5455. &pce_sps_data->cmdlistptr.unlock_all_pipes,
  5456. &pce_sps_data->in_transfer);
  5457. if (rc)
  5458. goto bad;
  5459. rc = _qce_sps_add_data(GET_PHYS_ADDR(pce_sps_data->result_dump),
  5460. CRYPTO_RESULT_DUMP_SIZE,
  5461. &pce_sps_data->out_transfer);
  5462. if (rc)
  5463. goto bad;
  5464. select_mode(pce_dev, preq_info);
  5465. rc = _qce_sps_transfer(pce_dev, req_info);
  5466. cmpxchg(&pce_dev->owner, QCE_OWNER_CLIENT, QCE_OWNER_NONE);
  5467. if (rc)
  5468. goto bad;
  5469. return 0;
  5470. bad:
  5471. dma_unmap_single(pce_dev->pdev, preq_info->phy_ota_src,
  5472. req->msize, DMA_TO_DEVICE);
  5473. qce_free_req_info(pce_dev, req_info, false);
  5474. return rc;
  5475. }
  5476. EXPORT_SYMBOL(qce_f9_req);
  5477. static int __qce_get_device_tree_data(struct platform_device *pdev,
  5478. struct qce_device *pce_dev)
  5479. {
  5480. struct resource *resource;
  5481. int rc = 0, i = 0;
  5482. pce_dev->is_shared = of_property_read_bool((&pdev->dev)->of_node,
  5483. "qcom,ce-hw-shared");
  5484. pce_dev->support_hw_key = of_property_read_bool((&pdev->dev)->of_node,
  5485. "qcom,ce-hw-key");
  5486. pce_dev->use_sw_aes_cbc_ecb_ctr_algo =
  5487. of_property_read_bool((&pdev->dev)->of_node,
  5488. "qcom,use-sw-aes-cbc-ecb-ctr-algo");
  5489. pce_dev->use_sw_aead_algo =
  5490. of_property_read_bool((&pdev->dev)->of_node,
  5491. "qcom,use-sw-aead-algo");
  5492. pce_dev->use_sw_aes_xts_algo =
  5493. of_property_read_bool((&pdev->dev)->of_node,
  5494. "qcom,use-sw-aes-xts-algo");
  5495. pce_dev->use_sw_ahash_algo =
  5496. of_property_read_bool((&pdev->dev)->of_node,
  5497. "qcom,use-sw-ahash-algo");
  5498. pce_dev->use_sw_hmac_algo =
  5499. of_property_read_bool((&pdev->dev)->of_node,
  5500. "qcom,use-sw-hmac-algo");
  5501. pce_dev->use_sw_aes_ccm_algo =
  5502. of_property_read_bool((&pdev->dev)->of_node,
  5503. "qcom,use-sw-aes-ccm-algo");
  5504. pce_dev->support_clk_mgmt_sus_res = of_property_read_bool(
  5505. (&pdev->dev)->of_node, "qcom,clk-mgmt-sus-res");
  5506. pce_dev->support_only_core_src_clk = of_property_read_bool(
  5507. (&pdev->dev)->of_node, "qcom,support-core-clk-only");
  5508. pce_dev->request_bw_before_clk = of_property_read_bool(
  5509. (&pdev->dev)->of_node, "qcom,request-bw-before-clk");
  5510. pce_dev->kernel_pipes_support = true;
  5511. if (of_property_read_u32((&pdev->dev)->of_node,
  5512. "qcom,bam-pipe-pair",
  5513. &pce_dev->ce_bam_info.pipe_pair_index[QCE_OFFLOAD_NONE])) {
  5514. pr_warn("Kernel pipes not supported.\n");
  5515. //Unused pipe, just as failsafe.
  5516. pce_dev->ce_bam_info.pipe_pair_index[QCE_OFFLOAD_NONE] = 2;
  5517. pce_dev->kernel_pipes_support = false;
  5518. }
  5519. if (of_property_read_bool((&pdev->dev)->of_node,
  5520. "qcom,offload-ops-support")) {
  5521. pce_dev->offload_pipes_support = true;
  5522. if (of_property_read_u32((&pdev->dev)->of_node,
  5523. "qcom,bam-pipe-offload-cpb-hlos",
  5524. &pce_dev->ce_bam_info.pipe_pair_index[QCE_OFFLOAD_CPB_HLOS])) {
  5525. pr_err("Fail to get bam offload cpb-hlos pipe pair info.\n");
  5526. return -EINVAL;
  5527. }
  5528. if (of_property_read_u32((&pdev->dev)->of_node,
  5529. "qcom,bam-pipe-offload-hlos-hlos",
  5530. &pce_dev->ce_bam_info.pipe_pair_index[QCE_OFFLOAD_HLOS_HLOS])) {
  5531. pr_err("Fail to get bam offload hlos-hlos info.\n");
  5532. return -EINVAL;
  5533. }
  5534. if (of_property_read_u32((&pdev->dev)->of_node,
  5535. "qcom,bam-pipe-offload-hlos-cpb",
  5536. &pce_dev->ce_bam_info.pipe_pair_index[QCE_OFFLOAD_HLOS_CPB])) {
  5537. pr_err("Fail to get bam offload hlos-cpb info\n");
  5538. return -EINVAL;
  5539. }
  5540. }
  5541. if (of_property_read_u32((&pdev->dev)->of_node,
  5542. "qcom,ce-device",
  5543. &pce_dev->ce_bam_info.ce_device)) {
  5544. pr_err("Fail to get CE device information.\n");
  5545. return -EINVAL;
  5546. }
  5547. if (of_property_read_u32((&pdev->dev)->of_node,
  5548. "qcom,ce-hw-instance",
  5549. &pce_dev->ce_bam_info.ce_hw_instance)) {
  5550. pr_err("Fail to get CE hw instance information.\n");
  5551. return -EINVAL;
  5552. }
  5553. if (of_property_read_u32((&pdev->dev)->of_node,
  5554. "qcom,bam-ee",
  5555. &pce_dev->ce_bam_info.bam_ee)) {
  5556. pr_info("BAM Apps EE is not defined, setting to default 1\n");
  5557. pce_dev->ce_bam_info.bam_ee = 1;
  5558. }
  5559. if (of_property_read_u32((&pdev->dev)->of_node,
  5560. "qcom,ce-opp-freq",
  5561. &pce_dev->ce_opp_freq_hz)) {
  5562. pr_info("CE operating frequency is not defined, setting to default 100MHZ\n");
  5563. pce_dev->ce_opp_freq_hz = CE_CLK_100MHZ;
  5564. }
  5565. if (of_property_read_bool((&pdev->dev)->of_node, "qcom,smmu-s1-enable"))
  5566. pce_dev->enable_s1_smmu = true;
  5567. pce_dev->no_clock_support = of_property_read_bool((&pdev->dev)->of_node,
  5568. "qcom,no-clock-support");
  5569. for (i = 0; i < QCE_OFFLOAD_OPER_LAST; i++) {
  5570. /* Source/destination pipes for all usecases */
  5571. pce_dev->ce_bam_info.dest_pipe_index[i] =
  5572. 2 * pce_dev->ce_bam_info.pipe_pair_index[i];
  5573. pce_dev->ce_bam_info.src_pipe_index[i] =
  5574. pce_dev->ce_bam_info.dest_pipe_index[i] + 1;
  5575. }
  5576. resource = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  5577. "crypto-base");
  5578. if (resource) {
  5579. pce_dev->phy_iobase = resource->start;
  5580. pce_dev->iobase = ioremap(resource->start,
  5581. resource_size(resource));
  5582. if (!pce_dev->iobase) {
  5583. pr_err("Can not map CRYPTO io memory\n");
  5584. return -ENOMEM;
  5585. }
  5586. } else {
  5587. pr_err("CRYPTO HW mem unavailable.\n");
  5588. return -ENODEV;
  5589. }
  5590. resource = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  5591. "crypto-bam-base");
  5592. if (resource) {
  5593. pce_dev->bam_mem = resource->start;
  5594. pce_dev->bam_mem_size = resource_size(resource);
  5595. } else {
  5596. pr_err("CRYPTO BAM mem unavailable.\n");
  5597. rc = -ENODEV;
  5598. goto err_getting_bam_info;
  5599. }
  5600. pce_dev->ce_bam_info.bam_irq = platform_get_irq(pdev,0);
  5601. if (pce_dev->ce_bam_info.bam_irq < 0) {
  5602. pr_err("CRYPTO BAM IRQ unavailable.\n");
  5603. goto err_dev;
  5604. }
  5605. return rc;
  5606. err_dev:
  5607. if (pce_dev->ce_bam_info.bam_iobase)
  5608. iounmap(pce_dev->ce_bam_info.bam_iobase);
  5609. err_getting_bam_info:
  5610. if (pce_dev->iobase)
  5611. iounmap(pce_dev->iobase);
  5612. return rc;
  5613. }
  5614. static int __qce_init_clk(struct qce_device *pce_dev)
  5615. {
  5616. int rc = 0;
  5617. if (pce_dev->no_clock_support) {
  5618. pr_debug("No clock support defined in dts\n");
  5619. return rc;
  5620. }
  5621. pce_dev->ce_core_src_clk = clk_get(pce_dev->pdev, "core_clk_src");
  5622. if (!IS_ERR(pce_dev->ce_core_src_clk)) {
  5623. if (pce_dev->request_bw_before_clk)
  5624. goto skip_set_rate;
  5625. rc = clk_set_rate(pce_dev->ce_core_src_clk,
  5626. pce_dev->ce_opp_freq_hz);
  5627. if (rc) {
  5628. pr_err("Unable to set the core src clk @%uMhz.\n",
  5629. pce_dev->ce_opp_freq_hz/CE_CLK_DIV);
  5630. goto exit_put_core_src_clk;
  5631. }
  5632. } else {
  5633. if (pce_dev->support_only_core_src_clk) {
  5634. rc = PTR_ERR(pce_dev->ce_core_src_clk);
  5635. pce_dev->ce_core_src_clk = NULL;
  5636. pr_err("Unable to get CE core src clk\n");
  5637. return rc;
  5638. }
  5639. pr_warn("Unable to get CE core src clk, set to NULL\n");
  5640. pce_dev->ce_core_src_clk = NULL;
  5641. }
  5642. skip_set_rate:
  5643. if (pce_dev->support_only_core_src_clk) {
  5644. pce_dev->ce_core_clk = NULL;
  5645. pce_dev->ce_clk = NULL;
  5646. pce_dev->ce_bus_clk = NULL;
  5647. } else {
  5648. pce_dev->ce_core_clk = clk_get(pce_dev->pdev, "core_clk");
  5649. if (IS_ERR(pce_dev->ce_core_clk)) {
  5650. rc = PTR_ERR(pce_dev->ce_core_clk);
  5651. pr_err("Unable to get CE core clk\n");
  5652. goto exit_put_core_src_clk;
  5653. }
  5654. pce_dev->ce_clk = clk_get(pce_dev->pdev, "iface_clk");
  5655. if (IS_ERR(pce_dev->ce_clk)) {
  5656. rc = PTR_ERR(pce_dev->ce_clk);
  5657. pr_err("Unable to get CE interface clk\n");
  5658. goto exit_put_core_clk;
  5659. }
  5660. pce_dev->ce_bus_clk = clk_get(pce_dev->pdev, "bus_clk");
  5661. if (IS_ERR(pce_dev->ce_bus_clk)) {
  5662. rc = PTR_ERR(pce_dev->ce_bus_clk);
  5663. pr_err("Unable to get CE BUS interface clk\n");
  5664. goto exit_put_iface_clk;
  5665. }
  5666. }
  5667. return rc;
  5668. exit_put_iface_clk:
  5669. if (pce_dev->ce_clk)
  5670. clk_put(pce_dev->ce_clk);
  5671. exit_put_core_clk:
  5672. if (pce_dev->ce_core_clk)
  5673. clk_put(pce_dev->ce_core_clk);
  5674. exit_put_core_src_clk:
  5675. if (pce_dev->ce_core_src_clk)
  5676. clk_put(pce_dev->ce_core_src_clk);
  5677. pr_err("Unable to init CE clks, rc = %d\n", rc);
  5678. return rc;
  5679. }
  5680. static void __qce_deinit_clk(struct qce_device *pce_dev)
  5681. {
  5682. if (pce_dev->no_clock_support) {
  5683. pr_debug("No clock support defined in dts\n");
  5684. return;
  5685. }
  5686. if (pce_dev->ce_bus_clk)
  5687. clk_put(pce_dev->ce_bus_clk);
  5688. if (pce_dev->ce_clk)
  5689. clk_put(pce_dev->ce_clk);
  5690. if (pce_dev->ce_core_clk)
  5691. clk_put(pce_dev->ce_core_clk);
  5692. if (pce_dev->ce_core_src_clk)
  5693. clk_put(pce_dev->ce_core_src_clk);
  5694. }
  5695. int qce_enable_clk(void *handle)
  5696. {
  5697. struct qce_device *pce_dev = (struct qce_device *)handle;
  5698. int rc = 0;
  5699. if (pce_dev->no_clock_support) {
  5700. pr_debug("No clock support defined in dts\n");
  5701. return rc;
  5702. }
  5703. if (pce_dev->ce_core_src_clk) {
  5704. rc = clk_prepare_enable(pce_dev->ce_core_src_clk);
  5705. if (rc) {
  5706. pr_err("Unable to enable/prepare CE core src clk\n");
  5707. return rc;
  5708. }
  5709. }
  5710. if (pce_dev->support_only_core_src_clk)
  5711. return rc;
  5712. if (pce_dev->ce_core_clk) {
  5713. rc = clk_prepare_enable(pce_dev->ce_core_clk);
  5714. if (rc) {
  5715. pr_err("Unable to enable/prepare CE core clk\n");
  5716. goto exit_disable_core_src_clk;
  5717. }
  5718. }
  5719. if (pce_dev->ce_clk) {
  5720. rc = clk_prepare_enable(pce_dev->ce_clk);
  5721. if (rc) {
  5722. pr_err("Unable to enable/prepare CE iface clk\n");
  5723. goto exit_disable_core_clk;
  5724. }
  5725. }
  5726. if (pce_dev->ce_bus_clk) {
  5727. rc = clk_prepare_enable(pce_dev->ce_bus_clk);
  5728. if (rc) {
  5729. pr_err("Unable to enable/prepare CE BUS clk\n");
  5730. goto exit_disable_ce_clk;
  5731. }
  5732. }
  5733. return rc;
  5734. exit_disable_ce_clk:
  5735. if (pce_dev->ce_clk)
  5736. clk_disable_unprepare(pce_dev->ce_clk);
  5737. exit_disable_core_clk:
  5738. if (pce_dev->ce_core_clk)
  5739. clk_disable_unprepare(pce_dev->ce_core_clk);
  5740. exit_disable_core_src_clk:
  5741. if (pce_dev->ce_core_src_clk)
  5742. clk_disable_unprepare(pce_dev->ce_core_src_clk);
  5743. return rc;
  5744. }
  5745. EXPORT_SYMBOL(qce_enable_clk);
  5746. int qce_disable_clk(void *handle)
  5747. {
  5748. struct qce_device *pce_dev = (struct qce_device *) handle;
  5749. if (pce_dev->no_clock_support) {
  5750. pr_debug("No clock support defined in dts\n");
  5751. return 0;
  5752. }
  5753. if (pce_dev->ce_bus_clk)
  5754. clk_disable_unprepare(pce_dev->ce_bus_clk);
  5755. if (pce_dev->ce_clk)
  5756. clk_disable_unprepare(pce_dev->ce_clk);
  5757. if (pce_dev->ce_core_clk)
  5758. clk_disable_unprepare(pce_dev->ce_core_clk);
  5759. if (pce_dev->ce_core_src_clk)
  5760. clk_disable_unprepare(pce_dev->ce_core_src_clk);
  5761. return 0;
  5762. }
  5763. EXPORT_SYMBOL(qce_disable_clk);
  5764. /* dummy req setup */
  5765. static int setup_dummy_req(struct qce_device *pce_dev)
  5766. {
  5767. char *input =
  5768. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopqopqrpqrs";
  5769. int len = DUMMY_REQ_DATA_LEN;
  5770. memcpy(pce_dev->dummyreq_in_buf, input, len);
  5771. sg_init_one(&pce_dev->dummyreq.sg, pce_dev->dummyreq_in_buf, len);
  5772. pce_dev->dummyreq.sreq.alg = QCE_HASH_SHA1;
  5773. pce_dev->dummyreq.sreq.qce_cb = qce_dummy_complete;
  5774. pce_dev->dummyreq.sreq.src = &pce_dev->dummyreq.sg;
  5775. pce_dev->dummyreq.sreq.auth_data[0] = 0;
  5776. pce_dev->dummyreq.sreq.auth_data[1] = 0;
  5777. pce_dev->dummyreq.sreq.auth_data[2] = 0;
  5778. pce_dev->dummyreq.sreq.auth_data[3] = 0;
  5779. pce_dev->dummyreq.sreq.first_blk = true;
  5780. pce_dev->dummyreq.sreq.last_blk = true;
  5781. pce_dev->dummyreq.sreq.size = len;
  5782. pce_dev->dummyreq.sreq.areq = &pce_dev->dummyreq.areq;
  5783. pce_dev->dummyreq.sreq.flags = 0;
  5784. pce_dev->dummyreq.sreq.authkey = NULL;
  5785. pce_dev->dummyreq.areq.src = pce_dev->dummyreq.sreq.src;
  5786. pce_dev->dummyreq.areq.nbytes = pce_dev->dummyreq.sreq.size;
  5787. return 0;
  5788. }
  5789. static int qce_smmu_init(struct qce_device *pce_dev)
  5790. {
  5791. struct device *dev = pce_dev->pdev;
  5792. if (!dev->dma_parms) {
  5793. dev->dma_parms = devm_kzalloc(dev,
  5794. sizeof(*dev->dma_parms), GFP_KERNEL);
  5795. if (!dev->dma_parms)
  5796. return -ENOMEM;
  5797. }
  5798. dma_set_max_seg_size(dev, DMA_BIT_MASK(32));
  5799. dma_set_seg_boundary(dev, (unsigned long)DMA_BIT_MASK(64));
  5800. return 0;
  5801. }
  5802. /* crypto engine open function. */
  5803. void *qce_open(struct platform_device *pdev, int *rc)
  5804. {
  5805. struct qce_device *pce_dev;
  5806. int i;
  5807. static int pcedev_no = 1;
  5808. pce_dev = kzalloc(sizeof(struct qce_device), GFP_KERNEL);
  5809. if (!pce_dev) {
  5810. *rc = -ENOMEM;
  5811. pr_err("Can not allocate memory: %d\n", *rc);
  5812. return NULL;
  5813. }
  5814. pce_dev->pdev = &pdev->dev;
  5815. mutex_lock(&qce_iomap_mutex);
  5816. if (pdev->dev.of_node) {
  5817. *rc = __qce_get_device_tree_data(pdev, pce_dev);
  5818. if (*rc)
  5819. goto err_pce_dev;
  5820. } else {
  5821. *rc = -EINVAL;
  5822. pr_err("Device Node not found.\n");
  5823. goto err_pce_dev;
  5824. }
  5825. if (pce_dev->enable_s1_smmu) {
  5826. if (qce_smmu_init(pce_dev)) {
  5827. *rc = -EIO;
  5828. goto err_pce_dev;
  5829. }
  5830. }
  5831. for (i = 0; i < MAX_QCE_ALLOC_BAM_REQ; i++)
  5832. atomic_set(&pce_dev->ce_request_info[i].in_use, false);
  5833. pce_dev->ce_request_index = 0;
  5834. pce_dev->memsize = 10 * PAGE_SIZE * MAX_QCE_ALLOC_BAM_REQ;
  5835. pce_dev->coh_vmem = dma_alloc_coherent(pce_dev->pdev,
  5836. pce_dev->memsize, &pce_dev->coh_pmem, GFP_KERNEL);
  5837. if (pce_dev->coh_vmem == NULL) {
  5838. *rc = -ENOMEM;
  5839. pr_err("Can not allocate coherent memory for sps data\n");
  5840. goto err_iobase;
  5841. }
  5842. pce_dev->iovec_memsize = TOTAL_IOVEC_SPACE_PER_PIPE *
  5843. MAX_QCE_ALLOC_BAM_REQ * 2;
  5844. pce_dev->iovec_vmem = kzalloc(pce_dev->iovec_memsize, GFP_KERNEL);
  5845. if (pce_dev->iovec_vmem == NULL)
  5846. goto err_mem;
  5847. pce_dev->dummyreq_in_buf = kzalloc(DUMMY_REQ_DATA_LEN, GFP_KERNEL);
  5848. if (pce_dev->dummyreq_in_buf == NULL)
  5849. goto err_mem;
  5850. *rc = __qce_init_clk(pce_dev);
  5851. if (*rc)
  5852. goto err_mem;
  5853. *rc = qce_enable_clk(pce_dev);
  5854. if (*rc)
  5855. goto err_enable_clk;
  5856. if (_probe_ce_engine(pce_dev)) {
  5857. *rc = -ENXIO;
  5858. goto err;
  5859. }
  5860. *rc = 0;
  5861. qce_init_ce_cfg_val(pce_dev);
  5862. *rc = qce_sps_init(pce_dev);
  5863. if (*rc)
  5864. goto err;
  5865. qce_setup_ce_sps_data(pce_dev);
  5866. qce_disable_clk(pce_dev);
  5867. setup_dummy_req(pce_dev);
  5868. atomic_set(&pce_dev->no_of_queued_req, 0);
  5869. pce_dev->mode = IN_INTERRUPT_MODE;
  5870. timer_setup(&(pce_dev->timer), qce_multireq_timeout, 0);
  5871. //pce_dev->timer.function = qce_multireq_timeout;
  5872. //pce_dev->timer.data = (unsigned long)pce_dev;
  5873. pce_dev->timer.expires = jiffies + DELAY_IN_JIFFIES;
  5874. pce_dev->intr_cadence = 0;
  5875. pce_dev->dev_no = pcedev_no;
  5876. pcedev_no++;
  5877. pce_dev->owner = QCE_OWNER_NONE;
  5878. qce_enable_clock_gating(pce_dev);
  5879. mutex_unlock(&qce_iomap_mutex);
  5880. return pce_dev;
  5881. err:
  5882. qce_disable_clk(pce_dev);
  5883. err_enable_clk:
  5884. __qce_deinit_clk(pce_dev);
  5885. err_mem:
  5886. kfree(pce_dev->dummyreq_in_buf);
  5887. kfree(pce_dev->iovec_vmem);
  5888. if (pce_dev->coh_vmem)
  5889. dma_free_coherent(pce_dev->pdev, pce_dev->memsize,
  5890. pce_dev->coh_vmem, pce_dev->coh_pmem);
  5891. err_iobase:
  5892. if (pce_dev->iobase)
  5893. iounmap(pce_dev->iobase);
  5894. err_pce_dev:
  5895. mutex_unlock(&qce_iomap_mutex);
  5896. kfree(pce_dev);
  5897. return NULL;
  5898. }
  5899. EXPORT_SYMBOL(qce_open);
  5900. /* crypto engine close function. */
  5901. int qce_close(void *handle)
  5902. {
  5903. struct qce_device *pce_dev = (struct qce_device *) handle;
  5904. if (handle == NULL)
  5905. return -ENODEV;
  5906. mutex_lock(&qce_iomap_mutex);
  5907. qce_enable_clk(pce_dev);
  5908. qce_sps_exit(pce_dev);
  5909. if (pce_dev->iobase)
  5910. iounmap(pce_dev->iobase);
  5911. if (pce_dev->coh_vmem)
  5912. dma_free_coherent(pce_dev->pdev, pce_dev->memsize,
  5913. pce_dev->coh_vmem, pce_dev->coh_pmem);
  5914. kfree(pce_dev->dummyreq_in_buf);
  5915. kfree(pce_dev->iovec_vmem);
  5916. qce_disable_clk(pce_dev);
  5917. __qce_deinit_clk(pce_dev);
  5918. mutex_unlock(&qce_iomap_mutex);
  5919. kfree(handle);
  5920. return 0;
  5921. }
  5922. EXPORT_SYMBOL(qce_close);
  5923. #define OTA_SUPPORT_MASK (1 << CRYPTO_ENCR_SNOW3G_SEL |\
  5924. 1 << CRYPTO_ENCR_KASUMI_SEL |\
  5925. 1 << CRYPTO_AUTH_SNOW3G_SEL |\
  5926. 1 << CRYPTO_AUTH_KASUMI_SEL)
  5927. int qce_hw_support(void *handle, struct ce_hw_support *ce_support)
  5928. {
  5929. struct qce_device *pce_dev = (struct qce_device *)handle;
  5930. if (ce_support == NULL)
  5931. return -EINVAL;
  5932. ce_support->sha1_hmac_20 = false;
  5933. ce_support->sha1_hmac = false;
  5934. ce_support->sha256_hmac = false;
  5935. ce_support->sha_hmac = true;
  5936. ce_support->cmac = true;
  5937. ce_support->aes_key_192 = false;
  5938. ce_support->aes_xts = true;
  5939. if ((pce_dev->engines_avail & OTA_SUPPORT_MASK) == OTA_SUPPORT_MASK)
  5940. ce_support->ota = true;
  5941. else
  5942. ce_support->ota = false;
  5943. ce_support->bam = true;
  5944. ce_support->is_shared = (pce_dev->is_shared == 1) ? true : false;
  5945. ce_support->hw_key = pce_dev->support_hw_key;
  5946. ce_support->aes_ccm = true;
  5947. ce_support->clk_mgmt_sus_res = pce_dev->support_clk_mgmt_sus_res;
  5948. ce_support->req_bw_before_clk = pce_dev->request_bw_before_clk;
  5949. if (pce_dev->ce_bam_info.minor_version)
  5950. ce_support->aligned_only = false;
  5951. else
  5952. ce_support->aligned_only = true;
  5953. ce_support->use_sw_aes_cbc_ecb_ctr_algo =
  5954. pce_dev->use_sw_aes_cbc_ecb_ctr_algo;
  5955. ce_support->use_sw_aead_algo =
  5956. pce_dev->use_sw_aead_algo;
  5957. ce_support->use_sw_aes_xts_algo =
  5958. pce_dev->use_sw_aes_xts_algo;
  5959. ce_support->use_sw_ahash_algo =
  5960. pce_dev->use_sw_ahash_algo;
  5961. ce_support->use_sw_hmac_algo =
  5962. pce_dev->use_sw_hmac_algo;
  5963. ce_support->use_sw_aes_ccm_algo =
  5964. pce_dev->use_sw_aes_ccm_algo;
  5965. ce_support->ce_device = pce_dev->ce_bam_info.ce_device;
  5966. ce_support->ce_hw_instance = pce_dev->ce_bam_info.ce_hw_instance;
  5967. if (pce_dev->no_get_around)
  5968. ce_support->max_request = MAX_QCE_BAM_REQ;
  5969. else
  5970. ce_support->max_request = 1;
  5971. return 0;
  5972. }
  5973. EXPORT_SYMBOL(qce_hw_support);
  5974. void qce_dump_req(void *handle)
  5975. {
  5976. int i;
  5977. bool req_in_use;
  5978. struct qce_device *pce_dev = (struct qce_device *)handle;
  5979. for (i = 0; i < MAX_QCE_BAM_REQ; i++) {
  5980. req_in_use = atomic_read(&pce_dev->ce_request_info[i].in_use);
  5981. pr_info("%s: %d %d\n", __func__, i, req_in_use);
  5982. if (req_in_use)
  5983. _qce_dump_descr_fifos(pce_dev, i);
  5984. }
  5985. }
  5986. EXPORT_SYMBOL(qce_dump_req);
  5987. MODULE_LICENSE("GPL v2");
  5988. MODULE_DESCRIPTION("Crypto Engine driver");