qce50.c 192 KB

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