qce50.c 159 KB

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