qce50.c 192 KB

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