qce50.c 192 KB

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