talitos.c 97 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424
  1. /*
  2. * talitos - Freescale Integrated Security Engine (SEC) device driver
  3. *
  4. * Copyright (c) 2008-2011 Freescale Semiconductor, Inc.
  5. *
  6. * Scatterlist Crypto API glue code copied from files with the following:
  7. * Copyright (c) 2006-2007 Herbert Xu <herbert@gondor.apana.org.au>
  8. *
  9. * Crypto algorithm registration code copied from hifn driver:
  10. * 2007+ Copyright (c) Evgeniy Polyakov <johnpol@2ka.mipt.ru>
  11. * All rights reserved.
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  26. */
  27. #include <linux/kernel.h>
  28. #include <linux/module.h>
  29. #include <linux/mod_devicetable.h>
  30. #include <linux/device.h>
  31. #include <linux/interrupt.h>
  32. #include <linux/crypto.h>
  33. #include <linux/hw_random.h>
  34. #include <linux/of_address.h>
  35. #include <linux/of_irq.h>
  36. #include <linux/of_platform.h>
  37. #include <linux/dma-mapping.h>
  38. #include <linux/io.h>
  39. #include <linux/spinlock.h>
  40. #include <linux/rtnetlink.h>
  41. #include <linux/slab.h>
  42. #include <crypto/algapi.h>
  43. #include <crypto/aes.h>
  44. #include <crypto/des.h>
  45. #include <crypto/sha.h>
  46. #include <crypto/md5.h>
  47. #include <crypto/internal/aead.h>
  48. #include <crypto/authenc.h>
  49. #include <crypto/skcipher.h>
  50. #include <crypto/hash.h>
  51. #include <crypto/internal/hash.h>
  52. #include <crypto/scatterwalk.h>
  53. #include "talitos.h"
  54. static void to_talitos_ptr(struct talitos_ptr *ptr, dma_addr_t dma_addr,
  55. bool is_sec1)
  56. {
  57. ptr->ptr = cpu_to_be32(lower_32_bits(dma_addr));
  58. if (!is_sec1)
  59. ptr->eptr = upper_32_bits(dma_addr);
  60. }
  61. static void copy_talitos_ptr(struct talitos_ptr *dst_ptr,
  62. struct talitos_ptr *src_ptr, bool is_sec1)
  63. {
  64. dst_ptr->ptr = src_ptr->ptr;
  65. if (!is_sec1)
  66. dst_ptr->eptr = src_ptr->eptr;
  67. }
  68. static void to_talitos_ptr_len(struct talitos_ptr *ptr, unsigned int len,
  69. bool is_sec1)
  70. {
  71. if (is_sec1) {
  72. ptr->res = 0;
  73. ptr->len1 = cpu_to_be16(len);
  74. } else {
  75. ptr->len = cpu_to_be16(len);
  76. }
  77. }
  78. static unsigned short from_talitos_ptr_len(struct talitos_ptr *ptr,
  79. bool is_sec1)
  80. {
  81. if (is_sec1)
  82. return be16_to_cpu(ptr->len1);
  83. else
  84. return be16_to_cpu(ptr->len);
  85. }
  86. static void to_talitos_ptr_ext_set(struct talitos_ptr *ptr, u8 val,
  87. bool is_sec1)
  88. {
  89. if (!is_sec1)
  90. ptr->j_extent = val;
  91. }
  92. static void to_talitos_ptr_ext_or(struct talitos_ptr *ptr, u8 val, bool is_sec1)
  93. {
  94. if (!is_sec1)
  95. ptr->j_extent |= val;
  96. }
  97. /*
  98. * map virtual single (contiguous) pointer to h/w descriptor pointer
  99. */
  100. static void map_single_talitos_ptr(struct device *dev,
  101. struct talitos_ptr *ptr,
  102. unsigned int len, void *data,
  103. enum dma_data_direction dir)
  104. {
  105. dma_addr_t dma_addr = dma_map_single(dev, data, len, dir);
  106. struct talitos_private *priv = dev_get_drvdata(dev);
  107. bool is_sec1 = has_ftr_sec1(priv);
  108. to_talitos_ptr_len(ptr, len, is_sec1);
  109. to_talitos_ptr(ptr, dma_addr, is_sec1);
  110. to_talitos_ptr_ext_set(ptr, 0, is_sec1);
  111. }
  112. /*
  113. * unmap bus single (contiguous) h/w descriptor pointer
  114. */
  115. static void unmap_single_talitos_ptr(struct device *dev,
  116. struct talitos_ptr *ptr,
  117. enum dma_data_direction dir)
  118. {
  119. struct talitos_private *priv = dev_get_drvdata(dev);
  120. bool is_sec1 = has_ftr_sec1(priv);
  121. dma_unmap_single(dev, be32_to_cpu(ptr->ptr),
  122. from_talitos_ptr_len(ptr, is_sec1), dir);
  123. }
  124. static int reset_channel(struct device *dev, int ch)
  125. {
  126. struct talitos_private *priv = dev_get_drvdata(dev);
  127. unsigned int timeout = TALITOS_TIMEOUT;
  128. bool is_sec1 = has_ftr_sec1(priv);
  129. if (is_sec1) {
  130. setbits32(priv->chan[ch].reg + TALITOS_CCCR_LO,
  131. TALITOS1_CCCR_LO_RESET);
  132. while ((in_be32(priv->chan[ch].reg + TALITOS_CCCR_LO) &
  133. TALITOS1_CCCR_LO_RESET) && --timeout)
  134. cpu_relax();
  135. } else {
  136. setbits32(priv->chan[ch].reg + TALITOS_CCCR,
  137. TALITOS2_CCCR_RESET);
  138. while ((in_be32(priv->chan[ch].reg + TALITOS_CCCR) &
  139. TALITOS2_CCCR_RESET) && --timeout)
  140. cpu_relax();
  141. }
  142. if (timeout == 0) {
  143. dev_err(dev, "failed to reset channel %d\n", ch);
  144. return -EIO;
  145. }
  146. /* set 36-bit addressing, done writeback enable and done IRQ enable */
  147. setbits32(priv->chan[ch].reg + TALITOS_CCCR_LO, TALITOS_CCCR_LO_EAE |
  148. TALITOS_CCCR_LO_CDWE | TALITOS_CCCR_LO_CDIE);
  149. /* and ICCR writeback, if available */
  150. if (priv->features & TALITOS_FTR_HW_AUTH_CHECK)
  151. setbits32(priv->chan[ch].reg + TALITOS_CCCR_LO,
  152. TALITOS_CCCR_LO_IWSE);
  153. return 0;
  154. }
  155. static int reset_device(struct device *dev)
  156. {
  157. struct talitos_private *priv = dev_get_drvdata(dev);
  158. unsigned int timeout = TALITOS_TIMEOUT;
  159. bool is_sec1 = has_ftr_sec1(priv);
  160. u32 mcr = is_sec1 ? TALITOS1_MCR_SWR : TALITOS2_MCR_SWR;
  161. setbits32(priv->reg + TALITOS_MCR, mcr);
  162. while ((in_be32(priv->reg + TALITOS_MCR) & mcr)
  163. && --timeout)
  164. cpu_relax();
  165. if (priv->irq[1]) {
  166. mcr = TALITOS_MCR_RCA1 | TALITOS_MCR_RCA3;
  167. setbits32(priv->reg + TALITOS_MCR, mcr);
  168. }
  169. if (timeout == 0) {
  170. dev_err(dev, "failed to reset device\n");
  171. return -EIO;
  172. }
  173. return 0;
  174. }
  175. /*
  176. * Reset and initialize the device
  177. */
  178. static int init_device(struct device *dev)
  179. {
  180. struct talitos_private *priv = dev_get_drvdata(dev);
  181. int ch, err;
  182. bool is_sec1 = has_ftr_sec1(priv);
  183. /*
  184. * Master reset
  185. * errata documentation: warning: certain SEC interrupts
  186. * are not fully cleared by writing the MCR:SWR bit,
  187. * set bit twice to completely reset
  188. */
  189. err = reset_device(dev);
  190. if (err)
  191. return err;
  192. err = reset_device(dev);
  193. if (err)
  194. return err;
  195. /* reset channels */
  196. for (ch = 0; ch < priv->num_channels; ch++) {
  197. err = reset_channel(dev, ch);
  198. if (err)
  199. return err;
  200. }
  201. /* enable channel done and error interrupts */
  202. if (is_sec1) {
  203. clrbits32(priv->reg + TALITOS_IMR, TALITOS1_IMR_INIT);
  204. clrbits32(priv->reg + TALITOS_IMR_LO, TALITOS1_IMR_LO_INIT);
  205. /* disable parity error check in DEU (erroneous? test vect.) */
  206. setbits32(priv->reg_deu + TALITOS_EUICR, TALITOS1_DEUICR_KPE);
  207. } else {
  208. setbits32(priv->reg + TALITOS_IMR, TALITOS2_IMR_INIT);
  209. setbits32(priv->reg + TALITOS_IMR_LO, TALITOS2_IMR_LO_INIT);
  210. }
  211. /* disable integrity check error interrupts (use writeback instead) */
  212. if (priv->features & TALITOS_FTR_HW_AUTH_CHECK)
  213. setbits32(priv->reg_mdeu + TALITOS_EUICR_LO,
  214. TALITOS_MDEUICR_LO_ICE);
  215. return 0;
  216. }
  217. /**
  218. * talitos_submit - submits a descriptor to the device for processing
  219. * @dev: the SEC device to be used
  220. * @ch: the SEC device channel to be used
  221. * @desc: the descriptor to be processed by the device
  222. * @callback: whom to call when processing is complete
  223. * @context: a handle for use by caller (optional)
  224. *
  225. * desc must contain valid dma-mapped (bus physical) address pointers.
  226. * callback must check err and feedback in descriptor header
  227. * for device processing status.
  228. */
  229. int talitos_submit(struct device *dev, int ch, struct talitos_desc *desc,
  230. void (*callback)(struct device *dev,
  231. struct talitos_desc *desc,
  232. void *context, int error),
  233. void *context)
  234. {
  235. struct talitos_private *priv = dev_get_drvdata(dev);
  236. struct talitos_request *request;
  237. unsigned long flags;
  238. int head;
  239. bool is_sec1 = has_ftr_sec1(priv);
  240. spin_lock_irqsave(&priv->chan[ch].head_lock, flags);
  241. if (!atomic_inc_not_zero(&priv->chan[ch].submit_count)) {
  242. /* h/w fifo is full */
  243. spin_unlock_irqrestore(&priv->chan[ch].head_lock, flags);
  244. return -EAGAIN;
  245. }
  246. head = priv->chan[ch].head;
  247. request = &priv->chan[ch].fifo[head];
  248. /* map descriptor and save caller data */
  249. if (is_sec1) {
  250. desc->hdr1 = desc->hdr;
  251. desc->next_desc = 0;
  252. request->dma_desc = dma_map_single(dev, &desc->hdr1,
  253. TALITOS_DESC_SIZE,
  254. DMA_BIDIRECTIONAL);
  255. } else {
  256. request->dma_desc = dma_map_single(dev, desc,
  257. TALITOS_DESC_SIZE,
  258. DMA_BIDIRECTIONAL);
  259. }
  260. request->callback = callback;
  261. request->context = context;
  262. /* increment fifo head */
  263. priv->chan[ch].head = (priv->chan[ch].head + 1) & (priv->fifo_len - 1);
  264. smp_wmb();
  265. request->desc = desc;
  266. /* GO! */
  267. wmb();
  268. out_be32(priv->chan[ch].reg + TALITOS_FF,
  269. upper_32_bits(request->dma_desc));
  270. out_be32(priv->chan[ch].reg + TALITOS_FF_LO,
  271. lower_32_bits(request->dma_desc));
  272. spin_unlock_irqrestore(&priv->chan[ch].head_lock, flags);
  273. return -EINPROGRESS;
  274. }
  275. EXPORT_SYMBOL(talitos_submit);
  276. /*
  277. * process what was done, notify callback of error if not
  278. */
  279. static void flush_channel(struct device *dev, int ch, int error, int reset_ch)
  280. {
  281. struct talitos_private *priv = dev_get_drvdata(dev);
  282. struct talitos_request *request, saved_req;
  283. unsigned long flags;
  284. int tail, status;
  285. bool is_sec1 = has_ftr_sec1(priv);
  286. spin_lock_irqsave(&priv->chan[ch].tail_lock, flags);
  287. tail = priv->chan[ch].tail;
  288. while (priv->chan[ch].fifo[tail].desc) {
  289. __be32 hdr;
  290. request = &priv->chan[ch].fifo[tail];
  291. /* descriptors with their done bits set don't get the error */
  292. rmb();
  293. hdr = is_sec1 ? request->desc->hdr1 : request->desc->hdr;
  294. if ((hdr & DESC_HDR_DONE) == DESC_HDR_DONE)
  295. status = 0;
  296. else
  297. if (!error)
  298. break;
  299. else
  300. status = error;
  301. dma_unmap_single(dev, request->dma_desc,
  302. TALITOS_DESC_SIZE,
  303. DMA_BIDIRECTIONAL);
  304. /* copy entries so we can call callback outside lock */
  305. saved_req.desc = request->desc;
  306. saved_req.callback = request->callback;
  307. saved_req.context = request->context;
  308. /* release request entry in fifo */
  309. smp_wmb();
  310. request->desc = NULL;
  311. /* increment fifo tail */
  312. priv->chan[ch].tail = (tail + 1) & (priv->fifo_len - 1);
  313. spin_unlock_irqrestore(&priv->chan[ch].tail_lock, flags);
  314. atomic_dec(&priv->chan[ch].submit_count);
  315. saved_req.callback(dev, saved_req.desc, saved_req.context,
  316. status);
  317. /* channel may resume processing in single desc error case */
  318. if (error && !reset_ch && status == error)
  319. return;
  320. spin_lock_irqsave(&priv->chan[ch].tail_lock, flags);
  321. tail = priv->chan[ch].tail;
  322. }
  323. spin_unlock_irqrestore(&priv->chan[ch].tail_lock, flags);
  324. }
  325. /*
  326. * process completed requests for channels that have done status
  327. */
  328. #define DEF_TALITOS1_DONE(name, ch_done_mask) \
  329. static void talitos1_done_##name(unsigned long data) \
  330. { \
  331. struct device *dev = (struct device *)data; \
  332. struct talitos_private *priv = dev_get_drvdata(dev); \
  333. unsigned long flags; \
  334. \
  335. if (ch_done_mask & 0x10000000) \
  336. flush_channel(dev, 0, 0, 0); \
  337. if (priv->num_channels == 1) \
  338. goto out; \
  339. if (ch_done_mask & 0x40000000) \
  340. flush_channel(dev, 1, 0, 0); \
  341. if (ch_done_mask & 0x00010000) \
  342. flush_channel(dev, 2, 0, 0); \
  343. if (ch_done_mask & 0x00040000) \
  344. flush_channel(dev, 3, 0, 0); \
  345. \
  346. out: \
  347. /* At this point, all completed channels have been processed */ \
  348. /* Unmask done interrupts for channels completed later on. */ \
  349. spin_lock_irqsave(&priv->reg_lock, flags); \
  350. clrbits32(priv->reg + TALITOS_IMR, ch_done_mask); \
  351. clrbits32(priv->reg + TALITOS_IMR_LO, TALITOS1_IMR_LO_INIT); \
  352. spin_unlock_irqrestore(&priv->reg_lock, flags); \
  353. }
  354. DEF_TALITOS1_DONE(4ch, TALITOS1_ISR_4CHDONE)
  355. #define DEF_TALITOS2_DONE(name, ch_done_mask) \
  356. static void talitos2_done_##name(unsigned long data) \
  357. { \
  358. struct device *dev = (struct device *)data; \
  359. struct talitos_private *priv = dev_get_drvdata(dev); \
  360. unsigned long flags; \
  361. \
  362. if (ch_done_mask & 1) \
  363. flush_channel(dev, 0, 0, 0); \
  364. if (priv->num_channels == 1) \
  365. goto out; \
  366. if (ch_done_mask & (1 << 2)) \
  367. flush_channel(dev, 1, 0, 0); \
  368. if (ch_done_mask & (1 << 4)) \
  369. flush_channel(dev, 2, 0, 0); \
  370. if (ch_done_mask & (1 << 6)) \
  371. flush_channel(dev, 3, 0, 0); \
  372. \
  373. out: \
  374. /* At this point, all completed channels have been processed */ \
  375. /* Unmask done interrupts for channels completed later on. */ \
  376. spin_lock_irqsave(&priv->reg_lock, flags); \
  377. setbits32(priv->reg + TALITOS_IMR, ch_done_mask); \
  378. setbits32(priv->reg + TALITOS_IMR_LO, TALITOS2_IMR_LO_INIT); \
  379. spin_unlock_irqrestore(&priv->reg_lock, flags); \
  380. }
  381. DEF_TALITOS2_DONE(4ch, TALITOS2_ISR_4CHDONE)
  382. DEF_TALITOS2_DONE(ch0_2, TALITOS2_ISR_CH_0_2_DONE)
  383. DEF_TALITOS2_DONE(ch1_3, TALITOS2_ISR_CH_1_3_DONE)
  384. /*
  385. * locate current (offending) descriptor
  386. */
  387. static u32 current_desc_hdr(struct device *dev, int ch)
  388. {
  389. struct talitos_private *priv = dev_get_drvdata(dev);
  390. int tail, iter;
  391. dma_addr_t cur_desc;
  392. cur_desc = ((u64)in_be32(priv->chan[ch].reg + TALITOS_CDPR)) << 32;
  393. cur_desc |= in_be32(priv->chan[ch].reg + TALITOS_CDPR_LO);
  394. if (!cur_desc) {
  395. dev_err(dev, "CDPR is NULL, giving up search for offending descriptor\n");
  396. return 0;
  397. }
  398. tail = priv->chan[ch].tail;
  399. iter = tail;
  400. while (priv->chan[ch].fifo[iter].dma_desc != cur_desc) {
  401. iter = (iter + 1) & (priv->fifo_len - 1);
  402. if (iter == tail) {
  403. dev_err(dev, "couldn't locate current descriptor\n");
  404. return 0;
  405. }
  406. }
  407. return priv->chan[ch].fifo[iter].desc->hdr;
  408. }
  409. /*
  410. * user diagnostics; report root cause of error based on execution unit status
  411. */
  412. static void report_eu_error(struct device *dev, int ch, u32 desc_hdr)
  413. {
  414. struct talitos_private *priv = dev_get_drvdata(dev);
  415. int i;
  416. if (!desc_hdr)
  417. desc_hdr = in_be32(priv->chan[ch].reg + TALITOS_DESCBUF);
  418. switch (desc_hdr & DESC_HDR_SEL0_MASK) {
  419. case DESC_HDR_SEL0_AFEU:
  420. dev_err(dev, "AFEUISR 0x%08x_%08x\n",
  421. in_be32(priv->reg_afeu + TALITOS_EUISR),
  422. in_be32(priv->reg_afeu + TALITOS_EUISR_LO));
  423. break;
  424. case DESC_HDR_SEL0_DEU:
  425. dev_err(dev, "DEUISR 0x%08x_%08x\n",
  426. in_be32(priv->reg_deu + TALITOS_EUISR),
  427. in_be32(priv->reg_deu + TALITOS_EUISR_LO));
  428. break;
  429. case DESC_HDR_SEL0_MDEUA:
  430. case DESC_HDR_SEL0_MDEUB:
  431. dev_err(dev, "MDEUISR 0x%08x_%08x\n",
  432. in_be32(priv->reg_mdeu + TALITOS_EUISR),
  433. in_be32(priv->reg_mdeu + TALITOS_EUISR_LO));
  434. break;
  435. case DESC_HDR_SEL0_RNG:
  436. dev_err(dev, "RNGUISR 0x%08x_%08x\n",
  437. in_be32(priv->reg_rngu + TALITOS_ISR),
  438. in_be32(priv->reg_rngu + TALITOS_ISR_LO));
  439. break;
  440. case DESC_HDR_SEL0_PKEU:
  441. dev_err(dev, "PKEUISR 0x%08x_%08x\n",
  442. in_be32(priv->reg_pkeu + TALITOS_EUISR),
  443. in_be32(priv->reg_pkeu + TALITOS_EUISR_LO));
  444. break;
  445. case DESC_HDR_SEL0_AESU:
  446. dev_err(dev, "AESUISR 0x%08x_%08x\n",
  447. in_be32(priv->reg_aesu + TALITOS_EUISR),
  448. in_be32(priv->reg_aesu + TALITOS_EUISR_LO));
  449. break;
  450. case DESC_HDR_SEL0_CRCU:
  451. dev_err(dev, "CRCUISR 0x%08x_%08x\n",
  452. in_be32(priv->reg_crcu + TALITOS_EUISR),
  453. in_be32(priv->reg_crcu + TALITOS_EUISR_LO));
  454. break;
  455. case DESC_HDR_SEL0_KEU:
  456. dev_err(dev, "KEUISR 0x%08x_%08x\n",
  457. in_be32(priv->reg_pkeu + TALITOS_EUISR),
  458. in_be32(priv->reg_pkeu + TALITOS_EUISR_LO));
  459. break;
  460. }
  461. switch (desc_hdr & DESC_HDR_SEL1_MASK) {
  462. case DESC_HDR_SEL1_MDEUA:
  463. case DESC_HDR_SEL1_MDEUB:
  464. dev_err(dev, "MDEUISR 0x%08x_%08x\n",
  465. in_be32(priv->reg_mdeu + TALITOS_EUISR),
  466. in_be32(priv->reg_mdeu + TALITOS_EUISR_LO));
  467. break;
  468. case DESC_HDR_SEL1_CRCU:
  469. dev_err(dev, "CRCUISR 0x%08x_%08x\n",
  470. in_be32(priv->reg_crcu + TALITOS_EUISR),
  471. in_be32(priv->reg_crcu + TALITOS_EUISR_LO));
  472. break;
  473. }
  474. for (i = 0; i < 8; i++)
  475. dev_err(dev, "DESCBUF 0x%08x_%08x\n",
  476. in_be32(priv->chan[ch].reg + TALITOS_DESCBUF + 8*i),
  477. in_be32(priv->chan[ch].reg + TALITOS_DESCBUF_LO + 8*i));
  478. }
  479. /*
  480. * recover from error interrupts
  481. */
  482. static void talitos_error(struct device *dev, u32 isr, u32 isr_lo)
  483. {
  484. struct talitos_private *priv = dev_get_drvdata(dev);
  485. unsigned int timeout = TALITOS_TIMEOUT;
  486. int ch, error, reset_dev = 0;
  487. u32 v_lo;
  488. bool is_sec1 = has_ftr_sec1(priv);
  489. int reset_ch = is_sec1 ? 1 : 0; /* only SEC2 supports continuation */
  490. for (ch = 0; ch < priv->num_channels; ch++) {
  491. /* skip channels without errors */
  492. if (is_sec1) {
  493. /* bits 29, 31, 17, 19 */
  494. if (!(isr & (1 << (29 + (ch & 1) * 2 - (ch & 2) * 6))))
  495. continue;
  496. } else {
  497. if (!(isr & (1 << (ch * 2 + 1))))
  498. continue;
  499. }
  500. error = -EINVAL;
  501. v_lo = in_be32(priv->chan[ch].reg + TALITOS_CCPSR_LO);
  502. if (v_lo & TALITOS_CCPSR_LO_DOF) {
  503. dev_err(dev, "double fetch fifo overflow error\n");
  504. error = -EAGAIN;
  505. reset_ch = 1;
  506. }
  507. if (v_lo & TALITOS_CCPSR_LO_SOF) {
  508. /* h/w dropped descriptor */
  509. dev_err(dev, "single fetch fifo overflow error\n");
  510. error = -EAGAIN;
  511. }
  512. if (v_lo & TALITOS_CCPSR_LO_MDTE)
  513. dev_err(dev, "master data transfer error\n");
  514. if (v_lo & TALITOS_CCPSR_LO_SGDLZ)
  515. dev_err(dev, is_sec1 ? "pointeur not complete error\n"
  516. : "s/g data length zero error\n");
  517. if (v_lo & TALITOS_CCPSR_LO_FPZ)
  518. dev_err(dev, is_sec1 ? "parity error\n"
  519. : "fetch pointer zero error\n");
  520. if (v_lo & TALITOS_CCPSR_LO_IDH)
  521. dev_err(dev, "illegal descriptor header error\n");
  522. if (v_lo & TALITOS_CCPSR_LO_IEU)
  523. dev_err(dev, is_sec1 ? "static assignment error\n"
  524. : "invalid exec unit error\n");
  525. if (v_lo & TALITOS_CCPSR_LO_EU)
  526. report_eu_error(dev, ch, current_desc_hdr(dev, ch));
  527. if (!is_sec1) {
  528. if (v_lo & TALITOS_CCPSR_LO_GB)
  529. dev_err(dev, "gather boundary error\n");
  530. if (v_lo & TALITOS_CCPSR_LO_GRL)
  531. dev_err(dev, "gather return/length error\n");
  532. if (v_lo & TALITOS_CCPSR_LO_SB)
  533. dev_err(dev, "scatter boundary error\n");
  534. if (v_lo & TALITOS_CCPSR_LO_SRL)
  535. dev_err(dev, "scatter return/length error\n");
  536. }
  537. flush_channel(dev, ch, error, reset_ch);
  538. if (reset_ch) {
  539. reset_channel(dev, ch);
  540. } else {
  541. setbits32(priv->chan[ch].reg + TALITOS_CCCR,
  542. TALITOS2_CCCR_CONT);
  543. setbits32(priv->chan[ch].reg + TALITOS_CCCR_LO, 0);
  544. while ((in_be32(priv->chan[ch].reg + TALITOS_CCCR) &
  545. TALITOS2_CCCR_CONT) && --timeout)
  546. cpu_relax();
  547. if (timeout == 0) {
  548. dev_err(dev, "failed to restart channel %d\n",
  549. ch);
  550. reset_dev = 1;
  551. }
  552. }
  553. }
  554. if (reset_dev || (is_sec1 && isr & ~TALITOS1_ISR_4CHERR) ||
  555. (!is_sec1 && isr & ~TALITOS2_ISR_4CHERR) || isr_lo) {
  556. if (is_sec1 && (isr_lo & TALITOS1_ISR_TEA_ERR))
  557. dev_err(dev, "TEA error: ISR 0x%08x_%08x\n",
  558. isr, isr_lo);
  559. else
  560. dev_err(dev, "done overflow, internal time out, or "
  561. "rngu error: ISR 0x%08x_%08x\n", isr, isr_lo);
  562. /* purge request queues */
  563. for (ch = 0; ch < priv->num_channels; ch++)
  564. flush_channel(dev, ch, -EIO, 1);
  565. /* reset and reinitialize the device */
  566. init_device(dev);
  567. }
  568. }
  569. #define DEF_TALITOS1_INTERRUPT(name, ch_done_mask, ch_err_mask, tlet) \
  570. static irqreturn_t talitos1_interrupt_##name(int irq, void *data) \
  571. { \
  572. struct device *dev = data; \
  573. struct talitos_private *priv = dev_get_drvdata(dev); \
  574. u32 isr, isr_lo; \
  575. unsigned long flags; \
  576. \
  577. spin_lock_irqsave(&priv->reg_lock, flags); \
  578. isr = in_be32(priv->reg + TALITOS_ISR); \
  579. isr_lo = in_be32(priv->reg + TALITOS_ISR_LO); \
  580. /* Acknowledge interrupt */ \
  581. out_be32(priv->reg + TALITOS_ICR, isr & (ch_done_mask | ch_err_mask)); \
  582. out_be32(priv->reg + TALITOS_ICR_LO, isr_lo); \
  583. \
  584. if (unlikely(isr & ch_err_mask || isr_lo & TALITOS1_IMR_LO_INIT)) { \
  585. spin_unlock_irqrestore(&priv->reg_lock, flags); \
  586. talitos_error(dev, isr & ch_err_mask, isr_lo); \
  587. } \
  588. else { \
  589. if (likely(isr & ch_done_mask)) { \
  590. /* mask further done interrupts. */ \
  591. setbits32(priv->reg + TALITOS_IMR, ch_done_mask); \
  592. /* done_task will unmask done interrupts at exit */ \
  593. tasklet_schedule(&priv->done_task[tlet]); \
  594. } \
  595. spin_unlock_irqrestore(&priv->reg_lock, flags); \
  596. } \
  597. \
  598. return (isr & (ch_done_mask | ch_err_mask) || isr_lo) ? IRQ_HANDLED : \
  599. IRQ_NONE; \
  600. }
  601. DEF_TALITOS1_INTERRUPT(4ch, TALITOS1_ISR_4CHDONE, TALITOS1_ISR_4CHERR, 0)
  602. #define DEF_TALITOS2_INTERRUPT(name, ch_done_mask, ch_err_mask, tlet) \
  603. static irqreturn_t talitos2_interrupt_##name(int irq, void *data) \
  604. { \
  605. struct device *dev = data; \
  606. struct talitos_private *priv = dev_get_drvdata(dev); \
  607. u32 isr, isr_lo; \
  608. unsigned long flags; \
  609. \
  610. spin_lock_irqsave(&priv->reg_lock, flags); \
  611. isr = in_be32(priv->reg + TALITOS_ISR); \
  612. isr_lo = in_be32(priv->reg + TALITOS_ISR_LO); \
  613. /* Acknowledge interrupt */ \
  614. out_be32(priv->reg + TALITOS_ICR, isr & (ch_done_mask | ch_err_mask)); \
  615. out_be32(priv->reg + TALITOS_ICR_LO, isr_lo); \
  616. \
  617. if (unlikely(isr & ch_err_mask || isr_lo)) { \
  618. spin_unlock_irqrestore(&priv->reg_lock, flags); \
  619. talitos_error(dev, isr & ch_err_mask, isr_lo); \
  620. } \
  621. else { \
  622. if (likely(isr & ch_done_mask)) { \
  623. /* mask further done interrupts. */ \
  624. clrbits32(priv->reg + TALITOS_IMR, ch_done_mask); \
  625. /* done_task will unmask done interrupts at exit */ \
  626. tasklet_schedule(&priv->done_task[tlet]); \
  627. } \
  628. spin_unlock_irqrestore(&priv->reg_lock, flags); \
  629. } \
  630. \
  631. return (isr & (ch_done_mask | ch_err_mask) || isr_lo) ? IRQ_HANDLED : \
  632. IRQ_NONE; \
  633. }
  634. DEF_TALITOS2_INTERRUPT(4ch, TALITOS2_ISR_4CHDONE, TALITOS2_ISR_4CHERR, 0)
  635. DEF_TALITOS2_INTERRUPT(ch0_2, TALITOS2_ISR_CH_0_2_DONE, TALITOS2_ISR_CH_0_2_ERR,
  636. 0)
  637. DEF_TALITOS2_INTERRUPT(ch1_3, TALITOS2_ISR_CH_1_3_DONE, TALITOS2_ISR_CH_1_3_ERR,
  638. 1)
  639. /*
  640. * hwrng
  641. */
  642. static int talitos_rng_data_present(struct hwrng *rng, int wait)
  643. {
  644. struct device *dev = (struct device *)rng->priv;
  645. struct talitos_private *priv = dev_get_drvdata(dev);
  646. u32 ofl;
  647. int i;
  648. for (i = 0; i < 20; i++) {
  649. ofl = in_be32(priv->reg_rngu + TALITOS_EUSR_LO) &
  650. TALITOS_RNGUSR_LO_OFL;
  651. if (ofl || !wait)
  652. break;
  653. udelay(10);
  654. }
  655. return !!ofl;
  656. }
  657. static int talitos_rng_data_read(struct hwrng *rng, u32 *data)
  658. {
  659. struct device *dev = (struct device *)rng->priv;
  660. struct talitos_private *priv = dev_get_drvdata(dev);
  661. /* rng fifo requires 64-bit accesses */
  662. *data = in_be32(priv->reg_rngu + TALITOS_EU_FIFO);
  663. *data = in_be32(priv->reg_rngu + TALITOS_EU_FIFO_LO);
  664. return sizeof(u32);
  665. }
  666. static int talitos_rng_init(struct hwrng *rng)
  667. {
  668. struct device *dev = (struct device *)rng->priv;
  669. struct talitos_private *priv = dev_get_drvdata(dev);
  670. unsigned int timeout = TALITOS_TIMEOUT;
  671. setbits32(priv->reg_rngu + TALITOS_EURCR_LO, TALITOS_RNGURCR_LO_SR);
  672. while (!(in_be32(priv->reg_rngu + TALITOS_EUSR_LO)
  673. & TALITOS_RNGUSR_LO_RD)
  674. && --timeout)
  675. cpu_relax();
  676. if (timeout == 0) {
  677. dev_err(dev, "failed to reset rng hw\n");
  678. return -ENODEV;
  679. }
  680. /* start generating */
  681. setbits32(priv->reg_rngu + TALITOS_EUDSR_LO, 0);
  682. return 0;
  683. }
  684. static int talitos_register_rng(struct device *dev)
  685. {
  686. struct talitos_private *priv = dev_get_drvdata(dev);
  687. int err;
  688. priv->rng.name = dev_driver_string(dev),
  689. priv->rng.init = talitos_rng_init,
  690. priv->rng.data_present = talitos_rng_data_present,
  691. priv->rng.data_read = talitos_rng_data_read,
  692. priv->rng.priv = (unsigned long)dev;
  693. err = hwrng_register(&priv->rng);
  694. if (!err)
  695. priv->rng_registered = true;
  696. return err;
  697. }
  698. static void talitos_unregister_rng(struct device *dev)
  699. {
  700. struct talitos_private *priv = dev_get_drvdata(dev);
  701. if (!priv->rng_registered)
  702. return;
  703. hwrng_unregister(&priv->rng);
  704. priv->rng_registered = false;
  705. }
  706. /*
  707. * crypto alg
  708. */
  709. #define TALITOS_CRA_PRIORITY 3000
  710. /*
  711. * Defines a priority for doing AEAD with descriptors type
  712. * HMAC_SNOOP_NO_AFEA (HSNA) instead of type IPSEC_ESP
  713. */
  714. #define TALITOS_CRA_PRIORITY_AEAD_HSNA (TALITOS_CRA_PRIORITY - 1)
  715. #define TALITOS_MAX_KEY_SIZE (AES_MAX_KEY_SIZE + SHA512_BLOCK_SIZE)
  716. #define TALITOS_MAX_IV_LENGTH 16 /* max of AES_BLOCK_SIZE, DES3_EDE_BLOCK_SIZE */
  717. struct talitos_ctx {
  718. struct device *dev;
  719. int ch;
  720. __be32 desc_hdr_template;
  721. u8 key[TALITOS_MAX_KEY_SIZE];
  722. u8 iv[TALITOS_MAX_IV_LENGTH];
  723. unsigned int keylen;
  724. unsigned int enckeylen;
  725. unsigned int authkeylen;
  726. };
  727. #define HASH_MAX_BLOCK_SIZE SHA512_BLOCK_SIZE
  728. #define TALITOS_MDEU_MAX_CONTEXT_SIZE TALITOS_MDEU_CONTEXT_SIZE_SHA384_SHA512
  729. struct talitos_ahash_req_ctx {
  730. u32 hw_context[TALITOS_MDEU_MAX_CONTEXT_SIZE / sizeof(u32)];
  731. unsigned int hw_context_size;
  732. u8 buf[HASH_MAX_BLOCK_SIZE];
  733. u8 bufnext[HASH_MAX_BLOCK_SIZE];
  734. unsigned int swinit;
  735. unsigned int first;
  736. unsigned int last;
  737. unsigned int to_hash_later;
  738. unsigned int nbuf;
  739. struct scatterlist bufsl[2];
  740. struct scatterlist *psrc;
  741. };
  742. struct talitos_export_state {
  743. u32 hw_context[TALITOS_MDEU_MAX_CONTEXT_SIZE / sizeof(u32)];
  744. u8 buf[HASH_MAX_BLOCK_SIZE];
  745. unsigned int swinit;
  746. unsigned int first;
  747. unsigned int last;
  748. unsigned int to_hash_later;
  749. unsigned int nbuf;
  750. };
  751. static int aead_setkey(struct crypto_aead *authenc,
  752. const u8 *key, unsigned int keylen)
  753. {
  754. struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
  755. struct crypto_authenc_keys keys;
  756. if (crypto_authenc_extractkeys(&keys, key, keylen) != 0)
  757. goto badkey;
  758. if (keys.authkeylen + keys.enckeylen > TALITOS_MAX_KEY_SIZE)
  759. goto badkey;
  760. memcpy(ctx->key, keys.authkey, keys.authkeylen);
  761. memcpy(&ctx->key[keys.authkeylen], keys.enckey, keys.enckeylen);
  762. ctx->keylen = keys.authkeylen + keys.enckeylen;
  763. ctx->enckeylen = keys.enckeylen;
  764. ctx->authkeylen = keys.authkeylen;
  765. return 0;
  766. badkey:
  767. crypto_aead_set_flags(authenc, CRYPTO_TFM_RES_BAD_KEY_LEN);
  768. return -EINVAL;
  769. }
  770. /*
  771. * talitos_edesc - s/w-extended descriptor
  772. * @src_nents: number of segments in input scatterlist
  773. * @dst_nents: number of segments in output scatterlist
  774. * @icv_ool: whether ICV is out-of-line
  775. * @iv_dma: dma address of iv for checking continuity and link table
  776. * @dma_len: length of dma mapped link_tbl space
  777. * @dma_link_tbl: bus physical address of link_tbl/buf
  778. * @desc: h/w descriptor
  779. * @link_tbl: input and output h/w link tables (if {src,dst}_nents > 1) (SEC2)
  780. * @buf: input and output buffeur (if {src,dst}_nents > 1) (SEC1)
  781. *
  782. * if decrypting (with authcheck), or either one of src_nents or dst_nents
  783. * is greater than 1, an integrity check value is concatenated to the end
  784. * of link_tbl data
  785. */
  786. struct talitos_edesc {
  787. int src_nents;
  788. int dst_nents;
  789. bool icv_ool;
  790. dma_addr_t iv_dma;
  791. int dma_len;
  792. dma_addr_t dma_link_tbl;
  793. struct talitos_desc desc;
  794. union {
  795. struct talitos_ptr link_tbl[0];
  796. u8 buf[0];
  797. };
  798. };
  799. static void talitos_sg_unmap(struct device *dev,
  800. struct talitos_edesc *edesc,
  801. struct scatterlist *src,
  802. struct scatterlist *dst,
  803. unsigned int len, unsigned int offset)
  804. {
  805. struct talitos_private *priv = dev_get_drvdata(dev);
  806. bool is_sec1 = has_ftr_sec1(priv);
  807. unsigned int src_nents = edesc->src_nents ? : 1;
  808. unsigned int dst_nents = edesc->dst_nents ? : 1;
  809. if (is_sec1 && dst && dst_nents > 1) {
  810. dma_sync_single_for_device(dev, edesc->dma_link_tbl + offset,
  811. len, DMA_FROM_DEVICE);
  812. sg_pcopy_from_buffer(dst, dst_nents, edesc->buf + offset, len,
  813. offset);
  814. }
  815. if (src != dst) {
  816. if (src_nents == 1 || !is_sec1)
  817. dma_unmap_sg(dev, src, src_nents, DMA_TO_DEVICE);
  818. if (dst && (dst_nents == 1 || !is_sec1))
  819. dma_unmap_sg(dev, dst, dst_nents, DMA_FROM_DEVICE);
  820. } else if (src_nents == 1 || !is_sec1) {
  821. dma_unmap_sg(dev, src, src_nents, DMA_BIDIRECTIONAL);
  822. }
  823. }
  824. static void ipsec_esp_unmap(struct device *dev,
  825. struct talitos_edesc *edesc,
  826. struct aead_request *areq)
  827. {
  828. struct crypto_aead *aead = crypto_aead_reqtfm(areq);
  829. struct talitos_ctx *ctx = crypto_aead_ctx(aead);
  830. unsigned int ivsize = crypto_aead_ivsize(aead);
  831. if (edesc->desc.hdr & DESC_HDR_TYPE_IPSEC_ESP)
  832. unmap_single_talitos_ptr(dev, &edesc->desc.ptr[6],
  833. DMA_FROM_DEVICE);
  834. unmap_single_talitos_ptr(dev, &edesc->desc.ptr[3], DMA_TO_DEVICE);
  835. unmap_single_talitos_ptr(dev, &edesc->desc.ptr[2], DMA_TO_DEVICE);
  836. unmap_single_talitos_ptr(dev, &edesc->desc.ptr[0], DMA_TO_DEVICE);
  837. talitos_sg_unmap(dev, edesc, areq->src, areq->dst, areq->cryptlen,
  838. areq->assoclen);
  839. if (edesc->dma_len)
  840. dma_unmap_single(dev, edesc->dma_link_tbl, edesc->dma_len,
  841. DMA_BIDIRECTIONAL);
  842. if (!(edesc->desc.hdr & DESC_HDR_TYPE_IPSEC_ESP)) {
  843. unsigned int dst_nents = edesc->dst_nents ? : 1;
  844. sg_pcopy_to_buffer(areq->dst, dst_nents, ctx->iv, ivsize,
  845. areq->assoclen + areq->cryptlen - ivsize);
  846. }
  847. }
  848. /*
  849. * ipsec_esp descriptor callbacks
  850. */
  851. static void ipsec_esp_encrypt_done(struct device *dev,
  852. struct talitos_desc *desc, void *context,
  853. int err)
  854. {
  855. struct talitos_private *priv = dev_get_drvdata(dev);
  856. bool is_sec1 = has_ftr_sec1(priv);
  857. struct aead_request *areq = context;
  858. struct crypto_aead *authenc = crypto_aead_reqtfm(areq);
  859. unsigned int authsize = crypto_aead_authsize(authenc);
  860. struct talitos_edesc *edesc;
  861. struct scatterlist *sg;
  862. void *icvdata;
  863. edesc = container_of(desc, struct talitos_edesc, desc);
  864. ipsec_esp_unmap(dev, edesc, areq);
  865. /* copy the generated ICV to dst */
  866. if (edesc->icv_ool) {
  867. if (is_sec1)
  868. icvdata = edesc->buf + areq->assoclen + areq->cryptlen;
  869. else
  870. icvdata = &edesc->link_tbl[edesc->src_nents +
  871. edesc->dst_nents + 2];
  872. sg = sg_last(areq->dst, edesc->dst_nents);
  873. memcpy((char *)sg_virt(sg) + sg->length - authsize,
  874. icvdata, authsize);
  875. }
  876. kfree(edesc);
  877. aead_request_complete(areq, err);
  878. }
  879. static void ipsec_esp_decrypt_swauth_done(struct device *dev,
  880. struct talitos_desc *desc,
  881. void *context, int err)
  882. {
  883. struct aead_request *req = context;
  884. struct crypto_aead *authenc = crypto_aead_reqtfm(req);
  885. unsigned int authsize = crypto_aead_authsize(authenc);
  886. struct talitos_edesc *edesc;
  887. struct scatterlist *sg;
  888. char *oicv, *icv;
  889. struct talitos_private *priv = dev_get_drvdata(dev);
  890. bool is_sec1 = has_ftr_sec1(priv);
  891. edesc = container_of(desc, struct talitos_edesc, desc);
  892. ipsec_esp_unmap(dev, edesc, req);
  893. if (!err) {
  894. /* auth check */
  895. sg = sg_last(req->dst, edesc->dst_nents ? : 1);
  896. icv = (char *)sg_virt(sg) + sg->length - authsize;
  897. if (edesc->dma_len) {
  898. if (is_sec1)
  899. oicv = (char *)&edesc->dma_link_tbl +
  900. req->assoclen + req->cryptlen;
  901. else
  902. oicv = (char *)
  903. &edesc->link_tbl[edesc->src_nents +
  904. edesc->dst_nents + 2];
  905. if (edesc->icv_ool)
  906. icv = oicv + authsize;
  907. } else
  908. oicv = (char *)&edesc->link_tbl[0];
  909. err = crypto_memneq(oicv, icv, authsize) ? -EBADMSG : 0;
  910. }
  911. kfree(edesc);
  912. aead_request_complete(req, err);
  913. }
  914. static void ipsec_esp_decrypt_hwauth_done(struct device *dev,
  915. struct talitos_desc *desc,
  916. void *context, int err)
  917. {
  918. struct aead_request *req = context;
  919. struct talitos_edesc *edesc;
  920. edesc = container_of(desc, struct talitos_edesc, desc);
  921. ipsec_esp_unmap(dev, edesc, req);
  922. /* check ICV auth status */
  923. if (!err && ((desc->hdr_lo & DESC_HDR_LO_ICCR1_MASK) !=
  924. DESC_HDR_LO_ICCR1_PASS))
  925. err = -EBADMSG;
  926. kfree(edesc);
  927. aead_request_complete(req, err);
  928. }
  929. /*
  930. * convert scatterlist to SEC h/w link table format
  931. * stop at cryptlen bytes
  932. */
  933. static int sg_to_link_tbl_offset(struct scatterlist *sg, int sg_count,
  934. unsigned int offset, int cryptlen,
  935. struct talitos_ptr *link_tbl_ptr)
  936. {
  937. int n_sg = sg_count;
  938. int count = 0;
  939. while (cryptlen && sg && n_sg--) {
  940. unsigned int len = sg_dma_len(sg);
  941. if (offset >= len) {
  942. offset -= len;
  943. goto next;
  944. }
  945. len -= offset;
  946. if (len > cryptlen)
  947. len = cryptlen;
  948. to_talitos_ptr(link_tbl_ptr + count,
  949. sg_dma_address(sg) + offset, 0);
  950. to_talitos_ptr_len(link_tbl_ptr + count, len, 0);
  951. to_talitos_ptr_ext_set(link_tbl_ptr + count, 0, 0);
  952. count++;
  953. cryptlen -= len;
  954. offset = 0;
  955. next:
  956. sg = sg_next(sg);
  957. }
  958. /* tag end of link table */
  959. if (count > 0)
  960. to_talitos_ptr_ext_set(link_tbl_ptr + count - 1,
  961. DESC_PTR_LNKTBL_RETURN, 0);
  962. return count;
  963. }
  964. static int talitos_sg_map_ext(struct device *dev, struct scatterlist *src,
  965. unsigned int len, struct talitos_edesc *edesc,
  966. struct talitos_ptr *ptr, int sg_count,
  967. unsigned int offset, int tbl_off, int elen)
  968. {
  969. struct talitos_private *priv = dev_get_drvdata(dev);
  970. bool is_sec1 = has_ftr_sec1(priv);
  971. if (!src) {
  972. *ptr = zero_entry;
  973. return 1;
  974. }
  975. to_talitos_ptr_len(ptr, len, is_sec1);
  976. to_talitos_ptr_ext_set(ptr, elen, is_sec1);
  977. if (sg_count == 1) {
  978. to_talitos_ptr(ptr, sg_dma_address(src) + offset, is_sec1);
  979. return sg_count;
  980. }
  981. if (is_sec1) {
  982. to_talitos_ptr(ptr, edesc->dma_link_tbl + offset, is_sec1);
  983. return sg_count;
  984. }
  985. sg_count = sg_to_link_tbl_offset(src, sg_count, offset, len + elen,
  986. &edesc->link_tbl[tbl_off]);
  987. if (sg_count == 1) {
  988. /* Only one segment now, so no link tbl needed*/
  989. copy_talitos_ptr(ptr, &edesc->link_tbl[tbl_off], is_sec1);
  990. return sg_count;
  991. }
  992. to_talitos_ptr(ptr, edesc->dma_link_tbl +
  993. tbl_off * sizeof(struct talitos_ptr), is_sec1);
  994. to_talitos_ptr_ext_or(ptr, DESC_PTR_LNKTBL_JUMP, is_sec1);
  995. return sg_count;
  996. }
  997. static int talitos_sg_map(struct device *dev, struct scatterlist *src,
  998. unsigned int len, struct talitos_edesc *edesc,
  999. struct talitos_ptr *ptr, int sg_count,
  1000. unsigned int offset, int tbl_off)
  1001. {
  1002. return talitos_sg_map_ext(dev, src, len, edesc, ptr, sg_count, offset,
  1003. tbl_off, 0);
  1004. }
  1005. /*
  1006. * fill in and submit ipsec_esp descriptor
  1007. */
  1008. static int ipsec_esp(struct talitos_edesc *edesc, struct aead_request *areq,
  1009. void (*callback)(struct device *dev,
  1010. struct talitos_desc *desc,
  1011. void *context, int error))
  1012. {
  1013. struct crypto_aead *aead = crypto_aead_reqtfm(areq);
  1014. unsigned int authsize = crypto_aead_authsize(aead);
  1015. struct talitos_ctx *ctx = crypto_aead_ctx(aead);
  1016. struct device *dev = ctx->dev;
  1017. struct talitos_desc *desc = &edesc->desc;
  1018. unsigned int cryptlen = areq->cryptlen;
  1019. unsigned int ivsize = crypto_aead_ivsize(aead);
  1020. int tbl_off = 0;
  1021. int sg_count, ret;
  1022. int elen = 0;
  1023. bool sync_needed = false;
  1024. struct talitos_private *priv = dev_get_drvdata(dev);
  1025. bool is_sec1 = has_ftr_sec1(priv);
  1026. /* hmac key */
  1027. map_single_talitos_ptr(dev, &desc->ptr[0], ctx->authkeylen, &ctx->key,
  1028. DMA_TO_DEVICE);
  1029. sg_count = edesc->src_nents ?: 1;
  1030. if (is_sec1 && sg_count > 1)
  1031. sg_copy_to_buffer(areq->src, sg_count, edesc->buf,
  1032. areq->assoclen + cryptlen);
  1033. else
  1034. sg_count = dma_map_sg(dev, areq->src, sg_count,
  1035. (areq->src == areq->dst) ?
  1036. DMA_BIDIRECTIONAL : DMA_TO_DEVICE);
  1037. /* hmac data */
  1038. ret = talitos_sg_map(dev, areq->src, areq->assoclen, edesc,
  1039. &desc->ptr[1], sg_count, 0, tbl_off);
  1040. if (ret > 1) {
  1041. tbl_off += ret;
  1042. sync_needed = true;
  1043. }
  1044. /* cipher iv */
  1045. if (desc->hdr & DESC_HDR_TYPE_IPSEC_ESP) {
  1046. to_talitos_ptr(&desc->ptr[2], edesc->iv_dma, is_sec1);
  1047. to_talitos_ptr_len(&desc->ptr[2], ivsize, is_sec1);
  1048. to_talitos_ptr_ext_set(&desc->ptr[2], 0, is_sec1);
  1049. } else {
  1050. to_talitos_ptr(&desc->ptr[3], edesc->iv_dma, is_sec1);
  1051. to_talitos_ptr_len(&desc->ptr[3], ivsize, is_sec1);
  1052. to_talitos_ptr_ext_set(&desc->ptr[3], 0, is_sec1);
  1053. }
  1054. /* cipher key */
  1055. if (desc->hdr & DESC_HDR_TYPE_IPSEC_ESP)
  1056. map_single_talitos_ptr(dev, &desc->ptr[3], ctx->enckeylen,
  1057. (char *)&ctx->key + ctx->authkeylen,
  1058. DMA_TO_DEVICE);
  1059. else
  1060. map_single_talitos_ptr(dev, &desc->ptr[2], ctx->enckeylen,
  1061. (char *)&ctx->key + ctx->authkeylen,
  1062. DMA_TO_DEVICE);
  1063. /*
  1064. * cipher in
  1065. * map and adjust cipher len to aead request cryptlen.
  1066. * extent is bytes of HMAC postpended to ciphertext,
  1067. * typically 12 for ipsec
  1068. */
  1069. if ((desc->hdr & DESC_HDR_TYPE_IPSEC_ESP) &&
  1070. (desc->hdr & DESC_HDR_MODE1_MDEU_CICV))
  1071. elen = authsize;
  1072. ret = talitos_sg_map_ext(dev, areq->src, cryptlen, edesc, &desc->ptr[4],
  1073. sg_count, areq->assoclen, tbl_off, elen);
  1074. if (ret > 1) {
  1075. tbl_off += ret;
  1076. sync_needed = true;
  1077. }
  1078. /* cipher out */
  1079. if (areq->src != areq->dst) {
  1080. sg_count = edesc->dst_nents ? : 1;
  1081. if (!is_sec1 || sg_count == 1)
  1082. dma_map_sg(dev, areq->dst, sg_count, DMA_FROM_DEVICE);
  1083. }
  1084. ret = talitos_sg_map(dev, areq->dst, cryptlen, edesc, &desc->ptr[5],
  1085. sg_count, areq->assoclen, tbl_off);
  1086. if (desc->hdr & DESC_HDR_TYPE_IPSEC_ESP)
  1087. to_talitos_ptr_ext_or(&desc->ptr[5], authsize, is_sec1);
  1088. /* ICV data */
  1089. if (ret > 1) {
  1090. tbl_off += ret;
  1091. edesc->icv_ool = true;
  1092. sync_needed = true;
  1093. if (desc->hdr & DESC_HDR_TYPE_IPSEC_ESP) {
  1094. struct talitos_ptr *tbl_ptr = &edesc->link_tbl[tbl_off];
  1095. int offset = (edesc->src_nents + edesc->dst_nents + 2) *
  1096. sizeof(struct talitos_ptr) + authsize;
  1097. /* Add an entry to the link table for ICV data */
  1098. to_talitos_ptr_ext_set(tbl_ptr - 1, 0, is_sec1);
  1099. to_talitos_ptr_ext_set(tbl_ptr, DESC_PTR_LNKTBL_RETURN,
  1100. is_sec1);
  1101. to_talitos_ptr_len(tbl_ptr, authsize, is_sec1);
  1102. /* icv data follows link tables */
  1103. to_talitos_ptr(tbl_ptr, edesc->dma_link_tbl + offset,
  1104. is_sec1);
  1105. } else {
  1106. dma_addr_t addr = edesc->dma_link_tbl;
  1107. if (is_sec1)
  1108. addr += areq->assoclen + cryptlen;
  1109. else
  1110. addr += sizeof(struct talitos_ptr) * tbl_off;
  1111. to_talitos_ptr(&desc->ptr[6], addr, is_sec1);
  1112. to_talitos_ptr_len(&desc->ptr[6], authsize, is_sec1);
  1113. }
  1114. } else if (!(desc->hdr & DESC_HDR_TYPE_IPSEC_ESP)) {
  1115. ret = talitos_sg_map(dev, areq->dst, authsize, edesc,
  1116. &desc->ptr[6], sg_count, areq->assoclen +
  1117. cryptlen,
  1118. tbl_off);
  1119. if (ret > 1) {
  1120. tbl_off += ret;
  1121. edesc->icv_ool = true;
  1122. sync_needed = true;
  1123. } else {
  1124. edesc->icv_ool = false;
  1125. }
  1126. } else {
  1127. edesc->icv_ool = false;
  1128. }
  1129. /* iv out */
  1130. if (desc->hdr & DESC_HDR_TYPE_IPSEC_ESP)
  1131. map_single_talitos_ptr(dev, &desc->ptr[6], ivsize, ctx->iv,
  1132. DMA_FROM_DEVICE);
  1133. if (sync_needed)
  1134. dma_sync_single_for_device(dev, edesc->dma_link_tbl,
  1135. edesc->dma_len,
  1136. DMA_BIDIRECTIONAL);
  1137. ret = talitos_submit(dev, ctx->ch, desc, callback, areq);
  1138. if (ret != -EINPROGRESS) {
  1139. ipsec_esp_unmap(dev, edesc, areq);
  1140. kfree(edesc);
  1141. }
  1142. return ret;
  1143. }
  1144. /*
  1145. * allocate and map the extended descriptor
  1146. */
  1147. static struct talitos_edesc *talitos_edesc_alloc(struct device *dev,
  1148. struct scatterlist *src,
  1149. struct scatterlist *dst,
  1150. u8 *iv,
  1151. unsigned int assoclen,
  1152. unsigned int cryptlen,
  1153. unsigned int authsize,
  1154. unsigned int ivsize,
  1155. int icv_stashing,
  1156. u32 cryptoflags,
  1157. bool encrypt)
  1158. {
  1159. struct talitos_edesc *edesc;
  1160. int src_nents, dst_nents, alloc_len, dma_len, src_len, dst_len;
  1161. dma_addr_t iv_dma = 0;
  1162. gfp_t flags = cryptoflags & CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL :
  1163. GFP_ATOMIC;
  1164. struct talitos_private *priv = dev_get_drvdata(dev);
  1165. bool is_sec1 = has_ftr_sec1(priv);
  1166. int max_len = is_sec1 ? TALITOS1_MAX_DATA_LEN : TALITOS2_MAX_DATA_LEN;
  1167. void *err;
  1168. if (cryptlen + authsize > max_len) {
  1169. dev_err(dev, "length exceeds h/w max limit\n");
  1170. return ERR_PTR(-EINVAL);
  1171. }
  1172. if (ivsize)
  1173. iv_dma = dma_map_single(dev, iv, ivsize, DMA_TO_DEVICE);
  1174. if (!dst || dst == src) {
  1175. src_len = assoclen + cryptlen + authsize;
  1176. src_nents = sg_nents_for_len(src, src_len);
  1177. if (src_nents < 0) {
  1178. dev_err(dev, "Invalid number of src SG.\n");
  1179. err = ERR_PTR(-EINVAL);
  1180. goto error_sg;
  1181. }
  1182. src_nents = (src_nents == 1) ? 0 : src_nents;
  1183. dst_nents = dst ? src_nents : 0;
  1184. dst_len = 0;
  1185. } else { /* dst && dst != src*/
  1186. src_len = assoclen + cryptlen + (encrypt ? 0 : authsize);
  1187. src_nents = sg_nents_for_len(src, src_len);
  1188. if (src_nents < 0) {
  1189. dev_err(dev, "Invalid number of src SG.\n");
  1190. err = ERR_PTR(-EINVAL);
  1191. goto error_sg;
  1192. }
  1193. src_nents = (src_nents == 1) ? 0 : src_nents;
  1194. dst_len = assoclen + cryptlen + (encrypt ? authsize : 0);
  1195. dst_nents = sg_nents_for_len(dst, dst_len);
  1196. if (dst_nents < 0) {
  1197. dev_err(dev, "Invalid number of dst SG.\n");
  1198. err = ERR_PTR(-EINVAL);
  1199. goto error_sg;
  1200. }
  1201. dst_nents = (dst_nents == 1) ? 0 : dst_nents;
  1202. }
  1203. /*
  1204. * allocate space for base edesc plus the link tables,
  1205. * allowing for two separate entries for AD and generated ICV (+ 2),
  1206. * and space for two sets of ICVs (stashed and generated)
  1207. */
  1208. alloc_len = sizeof(struct talitos_edesc);
  1209. if (src_nents || dst_nents) {
  1210. if (is_sec1)
  1211. dma_len = (src_nents ? src_len : 0) +
  1212. (dst_nents ? dst_len : 0);
  1213. else
  1214. dma_len = (src_nents + dst_nents + 2) *
  1215. sizeof(struct talitos_ptr) + authsize * 2;
  1216. alloc_len += dma_len;
  1217. } else {
  1218. dma_len = 0;
  1219. alloc_len += icv_stashing ? authsize : 0;
  1220. }
  1221. edesc = kmalloc(alloc_len, GFP_DMA | flags);
  1222. if (!edesc) {
  1223. dev_err(dev, "could not allocate edescriptor\n");
  1224. err = ERR_PTR(-ENOMEM);
  1225. goto error_sg;
  1226. }
  1227. edesc->src_nents = src_nents;
  1228. edesc->dst_nents = dst_nents;
  1229. edesc->iv_dma = iv_dma;
  1230. edesc->dma_len = dma_len;
  1231. if (dma_len)
  1232. edesc->dma_link_tbl = dma_map_single(dev, &edesc->link_tbl[0],
  1233. edesc->dma_len,
  1234. DMA_BIDIRECTIONAL);
  1235. return edesc;
  1236. error_sg:
  1237. if (iv_dma)
  1238. dma_unmap_single(dev, iv_dma, ivsize, DMA_TO_DEVICE);
  1239. return err;
  1240. }
  1241. static struct talitos_edesc *aead_edesc_alloc(struct aead_request *areq, u8 *iv,
  1242. int icv_stashing, bool encrypt)
  1243. {
  1244. struct crypto_aead *authenc = crypto_aead_reqtfm(areq);
  1245. unsigned int authsize = crypto_aead_authsize(authenc);
  1246. struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
  1247. unsigned int ivsize = crypto_aead_ivsize(authenc);
  1248. return talitos_edesc_alloc(ctx->dev, areq->src, areq->dst,
  1249. iv, areq->assoclen, areq->cryptlen,
  1250. authsize, ivsize, icv_stashing,
  1251. areq->base.flags, encrypt);
  1252. }
  1253. static int aead_encrypt(struct aead_request *req)
  1254. {
  1255. struct crypto_aead *authenc = crypto_aead_reqtfm(req);
  1256. struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
  1257. struct talitos_edesc *edesc;
  1258. /* allocate extended descriptor */
  1259. edesc = aead_edesc_alloc(req, req->iv, 0, true);
  1260. if (IS_ERR(edesc))
  1261. return PTR_ERR(edesc);
  1262. /* set encrypt */
  1263. edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_MODE0_ENCRYPT;
  1264. return ipsec_esp(edesc, req, ipsec_esp_encrypt_done);
  1265. }
  1266. static int aead_decrypt(struct aead_request *req)
  1267. {
  1268. struct crypto_aead *authenc = crypto_aead_reqtfm(req);
  1269. unsigned int authsize = crypto_aead_authsize(authenc);
  1270. struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
  1271. struct talitos_private *priv = dev_get_drvdata(ctx->dev);
  1272. struct talitos_edesc *edesc;
  1273. struct scatterlist *sg;
  1274. void *icvdata;
  1275. req->cryptlen -= authsize;
  1276. /* allocate extended descriptor */
  1277. edesc = aead_edesc_alloc(req, req->iv, 1, false);
  1278. if (IS_ERR(edesc))
  1279. return PTR_ERR(edesc);
  1280. if ((priv->features & TALITOS_FTR_HW_AUTH_CHECK) &&
  1281. ((!edesc->src_nents && !edesc->dst_nents) ||
  1282. priv->features & TALITOS_FTR_SRC_LINK_TBL_LEN_INCLUDES_EXTENT)) {
  1283. /* decrypt and check the ICV */
  1284. edesc->desc.hdr = ctx->desc_hdr_template |
  1285. DESC_HDR_DIR_INBOUND |
  1286. DESC_HDR_MODE1_MDEU_CICV;
  1287. /* reset integrity check result bits */
  1288. edesc->desc.hdr_lo = 0;
  1289. return ipsec_esp(edesc, req, ipsec_esp_decrypt_hwauth_done);
  1290. }
  1291. /* Have to check the ICV with software */
  1292. edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_DIR_INBOUND;
  1293. /* stash incoming ICV for later cmp with ICV generated by the h/w */
  1294. if (edesc->dma_len)
  1295. icvdata = (char *)&edesc->link_tbl[edesc->src_nents +
  1296. edesc->dst_nents + 2];
  1297. else
  1298. icvdata = &edesc->link_tbl[0];
  1299. sg = sg_last(req->src, edesc->src_nents ? : 1);
  1300. memcpy(icvdata, (char *)sg_virt(sg) + sg->length - authsize, authsize);
  1301. return ipsec_esp(edesc, req, ipsec_esp_decrypt_swauth_done);
  1302. }
  1303. static int ablkcipher_setkey(struct crypto_ablkcipher *cipher,
  1304. const u8 *key, unsigned int keylen)
  1305. {
  1306. struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
  1307. u32 tmp[DES_EXPKEY_WORDS];
  1308. if (keylen > TALITOS_MAX_KEY_SIZE) {
  1309. crypto_ablkcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
  1310. return -EINVAL;
  1311. }
  1312. if (unlikely(crypto_ablkcipher_get_flags(cipher) &
  1313. CRYPTO_TFM_REQ_WEAK_KEY) &&
  1314. !des_ekey(tmp, key)) {
  1315. crypto_ablkcipher_set_flags(cipher, CRYPTO_TFM_RES_WEAK_KEY);
  1316. return -EINVAL;
  1317. }
  1318. memcpy(&ctx->key, key, keylen);
  1319. ctx->keylen = keylen;
  1320. return 0;
  1321. }
  1322. static void common_nonsnoop_unmap(struct device *dev,
  1323. struct talitos_edesc *edesc,
  1324. struct ablkcipher_request *areq)
  1325. {
  1326. unmap_single_talitos_ptr(dev, &edesc->desc.ptr[5], DMA_FROM_DEVICE);
  1327. talitos_sg_unmap(dev, edesc, areq->src, areq->dst, areq->nbytes, 0);
  1328. unmap_single_talitos_ptr(dev, &edesc->desc.ptr[2], DMA_TO_DEVICE);
  1329. unmap_single_talitos_ptr(dev, &edesc->desc.ptr[1], DMA_TO_DEVICE);
  1330. if (edesc->dma_len)
  1331. dma_unmap_single(dev, edesc->dma_link_tbl, edesc->dma_len,
  1332. DMA_BIDIRECTIONAL);
  1333. }
  1334. static void ablkcipher_done(struct device *dev,
  1335. struct talitos_desc *desc, void *context,
  1336. int err)
  1337. {
  1338. struct ablkcipher_request *areq = context;
  1339. struct talitos_edesc *edesc;
  1340. edesc = container_of(desc, struct talitos_edesc, desc);
  1341. common_nonsnoop_unmap(dev, edesc, areq);
  1342. kfree(edesc);
  1343. areq->base.complete(&areq->base, err);
  1344. }
  1345. static int common_nonsnoop(struct talitos_edesc *edesc,
  1346. struct ablkcipher_request *areq,
  1347. void (*callback) (struct device *dev,
  1348. struct talitos_desc *desc,
  1349. void *context, int error))
  1350. {
  1351. struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
  1352. struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
  1353. struct device *dev = ctx->dev;
  1354. struct talitos_desc *desc = &edesc->desc;
  1355. unsigned int cryptlen = areq->nbytes;
  1356. unsigned int ivsize = crypto_ablkcipher_ivsize(cipher);
  1357. int sg_count, ret;
  1358. bool sync_needed = false;
  1359. struct talitos_private *priv = dev_get_drvdata(dev);
  1360. bool is_sec1 = has_ftr_sec1(priv);
  1361. /* first DWORD empty */
  1362. desc->ptr[0] = zero_entry;
  1363. /* cipher iv */
  1364. to_talitos_ptr(&desc->ptr[1], edesc->iv_dma, is_sec1);
  1365. to_talitos_ptr_len(&desc->ptr[1], ivsize, is_sec1);
  1366. to_talitos_ptr_ext_set(&desc->ptr[1], 0, is_sec1);
  1367. /* cipher key */
  1368. map_single_talitos_ptr(dev, &desc->ptr[2], ctx->keylen,
  1369. (char *)&ctx->key, DMA_TO_DEVICE);
  1370. sg_count = edesc->src_nents ?: 1;
  1371. if (is_sec1 && sg_count > 1)
  1372. sg_copy_to_buffer(areq->src, sg_count, edesc->buf,
  1373. cryptlen);
  1374. else
  1375. sg_count = dma_map_sg(dev, areq->src, sg_count,
  1376. (areq->src == areq->dst) ?
  1377. DMA_BIDIRECTIONAL : DMA_TO_DEVICE);
  1378. /*
  1379. * cipher in
  1380. */
  1381. sg_count = talitos_sg_map(dev, areq->src, cryptlen, edesc,
  1382. &desc->ptr[3], sg_count, 0, 0);
  1383. if (sg_count > 1)
  1384. sync_needed = true;
  1385. /* cipher out */
  1386. if (areq->src != areq->dst) {
  1387. sg_count = edesc->dst_nents ? : 1;
  1388. if (!is_sec1 || sg_count == 1)
  1389. dma_map_sg(dev, areq->dst, sg_count, DMA_FROM_DEVICE);
  1390. }
  1391. ret = talitos_sg_map(dev, areq->dst, cryptlen, edesc, &desc->ptr[4],
  1392. sg_count, 0, (edesc->src_nents + 1));
  1393. if (ret > 1)
  1394. sync_needed = true;
  1395. /* iv out */
  1396. map_single_talitos_ptr(dev, &desc->ptr[5], ivsize, ctx->iv,
  1397. DMA_FROM_DEVICE);
  1398. /* last DWORD empty */
  1399. desc->ptr[6] = zero_entry;
  1400. if (sync_needed)
  1401. dma_sync_single_for_device(dev, edesc->dma_link_tbl,
  1402. edesc->dma_len, DMA_BIDIRECTIONAL);
  1403. ret = talitos_submit(dev, ctx->ch, desc, callback, areq);
  1404. if (ret != -EINPROGRESS) {
  1405. common_nonsnoop_unmap(dev, edesc, areq);
  1406. kfree(edesc);
  1407. }
  1408. return ret;
  1409. }
  1410. static struct talitos_edesc *ablkcipher_edesc_alloc(struct ablkcipher_request *
  1411. areq, bool encrypt)
  1412. {
  1413. struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
  1414. struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
  1415. unsigned int ivsize = crypto_ablkcipher_ivsize(cipher);
  1416. return talitos_edesc_alloc(ctx->dev, areq->src, areq->dst,
  1417. areq->info, 0, areq->nbytes, 0, ivsize, 0,
  1418. areq->base.flags, encrypt);
  1419. }
  1420. static int ablkcipher_encrypt(struct ablkcipher_request *areq)
  1421. {
  1422. struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
  1423. struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
  1424. struct talitos_edesc *edesc;
  1425. /* allocate extended descriptor */
  1426. edesc = ablkcipher_edesc_alloc(areq, true);
  1427. if (IS_ERR(edesc))
  1428. return PTR_ERR(edesc);
  1429. /* set encrypt */
  1430. edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_MODE0_ENCRYPT;
  1431. return common_nonsnoop(edesc, areq, ablkcipher_done);
  1432. }
  1433. static int ablkcipher_decrypt(struct ablkcipher_request *areq)
  1434. {
  1435. struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
  1436. struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
  1437. struct talitos_edesc *edesc;
  1438. /* allocate extended descriptor */
  1439. edesc = ablkcipher_edesc_alloc(areq, false);
  1440. if (IS_ERR(edesc))
  1441. return PTR_ERR(edesc);
  1442. edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_DIR_INBOUND;
  1443. return common_nonsnoop(edesc, areq, ablkcipher_done);
  1444. }
  1445. static void common_nonsnoop_hash_unmap(struct device *dev,
  1446. struct talitos_edesc *edesc,
  1447. struct ahash_request *areq)
  1448. {
  1449. struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
  1450. struct talitos_private *priv = dev_get_drvdata(dev);
  1451. bool is_sec1 = has_ftr_sec1(priv);
  1452. unmap_single_talitos_ptr(dev, &edesc->desc.ptr[5], DMA_FROM_DEVICE);
  1453. talitos_sg_unmap(dev, edesc, req_ctx->psrc, NULL, 0, 0);
  1454. /* When using hashctx-in, must unmap it. */
  1455. if (from_talitos_ptr_len(&edesc->desc.ptr[1], is_sec1))
  1456. unmap_single_talitos_ptr(dev, &edesc->desc.ptr[1],
  1457. DMA_TO_DEVICE);
  1458. if (from_talitos_ptr_len(&edesc->desc.ptr[2], is_sec1))
  1459. unmap_single_talitos_ptr(dev, &edesc->desc.ptr[2],
  1460. DMA_TO_DEVICE);
  1461. if (edesc->dma_len)
  1462. dma_unmap_single(dev, edesc->dma_link_tbl, edesc->dma_len,
  1463. DMA_BIDIRECTIONAL);
  1464. }
  1465. static void ahash_done(struct device *dev,
  1466. struct talitos_desc *desc, void *context,
  1467. int err)
  1468. {
  1469. struct ahash_request *areq = context;
  1470. struct talitos_edesc *edesc =
  1471. container_of(desc, struct talitos_edesc, desc);
  1472. struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
  1473. if (!req_ctx->last && req_ctx->to_hash_later) {
  1474. /* Position any partial block for next update/final/finup */
  1475. memcpy(req_ctx->buf, req_ctx->bufnext, req_ctx->to_hash_later);
  1476. req_ctx->nbuf = req_ctx->to_hash_later;
  1477. }
  1478. common_nonsnoop_hash_unmap(dev, edesc, areq);
  1479. kfree(edesc);
  1480. areq->base.complete(&areq->base, err);
  1481. }
  1482. /*
  1483. * SEC1 doesn't like hashing of 0 sized message, so we do the padding
  1484. * ourself and submit a padded block
  1485. */
  1486. void talitos_handle_buggy_hash(struct talitos_ctx *ctx,
  1487. struct talitos_edesc *edesc,
  1488. struct talitos_ptr *ptr)
  1489. {
  1490. static u8 padded_hash[64] = {
  1491. 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1492. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1493. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1494. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1495. };
  1496. pr_err_once("Bug in SEC1, padding ourself\n");
  1497. edesc->desc.hdr &= ~DESC_HDR_MODE0_MDEU_PAD;
  1498. map_single_talitos_ptr(ctx->dev, ptr, sizeof(padded_hash),
  1499. (char *)padded_hash, DMA_TO_DEVICE);
  1500. }
  1501. static int common_nonsnoop_hash(struct talitos_edesc *edesc,
  1502. struct ahash_request *areq, unsigned int length,
  1503. void (*callback) (struct device *dev,
  1504. struct talitos_desc *desc,
  1505. void *context, int error))
  1506. {
  1507. struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
  1508. struct talitos_ctx *ctx = crypto_ahash_ctx(tfm);
  1509. struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
  1510. struct device *dev = ctx->dev;
  1511. struct talitos_desc *desc = &edesc->desc;
  1512. int ret;
  1513. bool sync_needed = false;
  1514. struct talitos_private *priv = dev_get_drvdata(dev);
  1515. bool is_sec1 = has_ftr_sec1(priv);
  1516. int sg_count;
  1517. /* first DWORD empty */
  1518. desc->ptr[0] = zero_entry;
  1519. /* hash context in */
  1520. if (!req_ctx->first || req_ctx->swinit) {
  1521. map_single_talitos_ptr(dev, &desc->ptr[1],
  1522. req_ctx->hw_context_size,
  1523. (char *)req_ctx->hw_context,
  1524. DMA_TO_DEVICE);
  1525. req_ctx->swinit = 0;
  1526. } else {
  1527. desc->ptr[1] = zero_entry;
  1528. }
  1529. /* Indicate next op is not the first. */
  1530. req_ctx->first = 0;
  1531. /* HMAC key */
  1532. if (ctx->keylen)
  1533. map_single_talitos_ptr(dev, &desc->ptr[2], ctx->keylen,
  1534. (char *)&ctx->key, DMA_TO_DEVICE);
  1535. else
  1536. desc->ptr[2] = zero_entry;
  1537. sg_count = edesc->src_nents ?: 1;
  1538. if (is_sec1 && sg_count > 1)
  1539. sg_copy_to_buffer(req_ctx->psrc, sg_count, edesc->buf, length);
  1540. else
  1541. sg_count = dma_map_sg(dev, req_ctx->psrc, sg_count,
  1542. DMA_TO_DEVICE);
  1543. /*
  1544. * data in
  1545. */
  1546. sg_count = talitos_sg_map(dev, req_ctx->psrc, length, edesc,
  1547. &desc->ptr[3], sg_count, 0, 0);
  1548. if (sg_count > 1)
  1549. sync_needed = true;
  1550. /* fifth DWORD empty */
  1551. desc->ptr[4] = zero_entry;
  1552. /* hash/HMAC out -or- hash context out */
  1553. if (req_ctx->last)
  1554. map_single_talitos_ptr(dev, &desc->ptr[5],
  1555. crypto_ahash_digestsize(tfm),
  1556. areq->result, DMA_FROM_DEVICE);
  1557. else
  1558. map_single_talitos_ptr(dev, &desc->ptr[5],
  1559. req_ctx->hw_context_size,
  1560. req_ctx->hw_context, DMA_FROM_DEVICE);
  1561. /* last DWORD empty */
  1562. desc->ptr[6] = zero_entry;
  1563. if (is_sec1 && from_talitos_ptr_len(&desc->ptr[3], true) == 0)
  1564. talitos_handle_buggy_hash(ctx, edesc, &desc->ptr[3]);
  1565. if (sync_needed)
  1566. dma_sync_single_for_device(dev, edesc->dma_link_tbl,
  1567. edesc->dma_len, DMA_BIDIRECTIONAL);
  1568. ret = talitos_submit(dev, ctx->ch, desc, callback, areq);
  1569. if (ret != -EINPROGRESS) {
  1570. common_nonsnoop_hash_unmap(dev, edesc, areq);
  1571. kfree(edesc);
  1572. }
  1573. return ret;
  1574. }
  1575. static struct talitos_edesc *ahash_edesc_alloc(struct ahash_request *areq,
  1576. unsigned int nbytes)
  1577. {
  1578. struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
  1579. struct talitos_ctx *ctx = crypto_ahash_ctx(tfm);
  1580. struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
  1581. return talitos_edesc_alloc(ctx->dev, req_ctx->psrc, NULL, NULL, 0,
  1582. nbytes, 0, 0, 0, areq->base.flags, false);
  1583. }
  1584. static int ahash_init(struct ahash_request *areq)
  1585. {
  1586. struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
  1587. struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
  1588. /* Initialize the context */
  1589. req_ctx->nbuf = 0;
  1590. req_ctx->first = 1; /* first indicates h/w must init its context */
  1591. req_ctx->swinit = 0; /* assume h/w init of context */
  1592. req_ctx->hw_context_size =
  1593. (crypto_ahash_digestsize(tfm) <= SHA256_DIGEST_SIZE)
  1594. ? TALITOS_MDEU_CONTEXT_SIZE_MD5_SHA1_SHA256
  1595. : TALITOS_MDEU_CONTEXT_SIZE_SHA384_SHA512;
  1596. return 0;
  1597. }
  1598. /*
  1599. * on h/w without explicit sha224 support, we initialize h/w context
  1600. * manually with sha224 constants, and tell it to run sha256.
  1601. */
  1602. static int ahash_init_sha224_swinit(struct ahash_request *areq)
  1603. {
  1604. struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
  1605. ahash_init(areq);
  1606. req_ctx->swinit = 1;/* prevent h/w initting context with sha256 values*/
  1607. req_ctx->hw_context[0] = SHA224_H0;
  1608. req_ctx->hw_context[1] = SHA224_H1;
  1609. req_ctx->hw_context[2] = SHA224_H2;
  1610. req_ctx->hw_context[3] = SHA224_H3;
  1611. req_ctx->hw_context[4] = SHA224_H4;
  1612. req_ctx->hw_context[5] = SHA224_H5;
  1613. req_ctx->hw_context[6] = SHA224_H6;
  1614. req_ctx->hw_context[7] = SHA224_H7;
  1615. /* init 64-bit count */
  1616. req_ctx->hw_context[8] = 0;
  1617. req_ctx->hw_context[9] = 0;
  1618. return 0;
  1619. }
  1620. static int ahash_process_req(struct ahash_request *areq, unsigned int nbytes)
  1621. {
  1622. struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
  1623. struct talitos_ctx *ctx = crypto_ahash_ctx(tfm);
  1624. struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
  1625. struct talitos_edesc *edesc;
  1626. unsigned int blocksize =
  1627. crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
  1628. unsigned int nbytes_to_hash;
  1629. unsigned int to_hash_later;
  1630. unsigned int nsg;
  1631. int nents;
  1632. if (!req_ctx->last && (nbytes + req_ctx->nbuf <= blocksize)) {
  1633. /* Buffer up to one whole block */
  1634. nents = sg_nents_for_len(areq->src, nbytes);
  1635. if (nents < 0) {
  1636. dev_err(ctx->dev, "Invalid number of src SG.\n");
  1637. return nents;
  1638. }
  1639. sg_copy_to_buffer(areq->src, nents,
  1640. req_ctx->buf + req_ctx->nbuf, nbytes);
  1641. req_ctx->nbuf += nbytes;
  1642. return 0;
  1643. }
  1644. /* At least (blocksize + 1) bytes are available to hash */
  1645. nbytes_to_hash = nbytes + req_ctx->nbuf;
  1646. to_hash_later = nbytes_to_hash & (blocksize - 1);
  1647. if (req_ctx->last)
  1648. to_hash_later = 0;
  1649. else if (to_hash_later)
  1650. /* There is a partial block. Hash the full block(s) now */
  1651. nbytes_to_hash -= to_hash_later;
  1652. else {
  1653. /* Keep one block buffered */
  1654. nbytes_to_hash -= blocksize;
  1655. to_hash_later = blocksize;
  1656. }
  1657. /* Chain in any previously buffered data */
  1658. if (req_ctx->nbuf) {
  1659. nsg = (req_ctx->nbuf < nbytes_to_hash) ? 2 : 1;
  1660. sg_init_table(req_ctx->bufsl, nsg);
  1661. sg_set_buf(req_ctx->bufsl, req_ctx->buf, req_ctx->nbuf);
  1662. if (nsg > 1)
  1663. sg_chain(req_ctx->bufsl, 2, areq->src);
  1664. req_ctx->psrc = req_ctx->bufsl;
  1665. } else
  1666. req_ctx->psrc = areq->src;
  1667. if (to_hash_later) {
  1668. nents = sg_nents_for_len(areq->src, nbytes);
  1669. if (nents < 0) {
  1670. dev_err(ctx->dev, "Invalid number of src SG.\n");
  1671. return nents;
  1672. }
  1673. sg_pcopy_to_buffer(areq->src, nents,
  1674. req_ctx->bufnext,
  1675. to_hash_later,
  1676. nbytes - to_hash_later);
  1677. }
  1678. req_ctx->to_hash_later = to_hash_later;
  1679. /* Allocate extended descriptor */
  1680. edesc = ahash_edesc_alloc(areq, nbytes_to_hash);
  1681. if (IS_ERR(edesc))
  1682. return PTR_ERR(edesc);
  1683. edesc->desc.hdr = ctx->desc_hdr_template;
  1684. /* On last one, request SEC to pad; otherwise continue */
  1685. if (req_ctx->last)
  1686. edesc->desc.hdr |= DESC_HDR_MODE0_MDEU_PAD;
  1687. else
  1688. edesc->desc.hdr |= DESC_HDR_MODE0_MDEU_CONT;
  1689. /* request SEC to INIT hash. */
  1690. if (req_ctx->first && !req_ctx->swinit)
  1691. edesc->desc.hdr |= DESC_HDR_MODE0_MDEU_INIT;
  1692. /* When the tfm context has a keylen, it's an HMAC.
  1693. * A first or last (ie. not middle) descriptor must request HMAC.
  1694. */
  1695. if (ctx->keylen && (req_ctx->first || req_ctx->last))
  1696. edesc->desc.hdr |= DESC_HDR_MODE0_MDEU_HMAC;
  1697. return common_nonsnoop_hash(edesc, areq, nbytes_to_hash,
  1698. ahash_done);
  1699. }
  1700. static int ahash_update(struct ahash_request *areq)
  1701. {
  1702. struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
  1703. req_ctx->last = 0;
  1704. return ahash_process_req(areq, areq->nbytes);
  1705. }
  1706. static int ahash_final(struct ahash_request *areq)
  1707. {
  1708. struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
  1709. req_ctx->last = 1;
  1710. return ahash_process_req(areq, 0);
  1711. }
  1712. static int ahash_finup(struct ahash_request *areq)
  1713. {
  1714. struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
  1715. req_ctx->last = 1;
  1716. return ahash_process_req(areq, areq->nbytes);
  1717. }
  1718. static int ahash_digest(struct ahash_request *areq)
  1719. {
  1720. struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
  1721. struct crypto_ahash *ahash = crypto_ahash_reqtfm(areq);
  1722. ahash->init(areq);
  1723. req_ctx->last = 1;
  1724. return ahash_process_req(areq, areq->nbytes);
  1725. }
  1726. static int ahash_export(struct ahash_request *areq, void *out)
  1727. {
  1728. struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
  1729. struct talitos_export_state *export = out;
  1730. memcpy(export->hw_context, req_ctx->hw_context,
  1731. req_ctx->hw_context_size);
  1732. memcpy(export->buf, req_ctx->buf, req_ctx->nbuf);
  1733. export->swinit = req_ctx->swinit;
  1734. export->first = req_ctx->first;
  1735. export->last = req_ctx->last;
  1736. export->to_hash_later = req_ctx->to_hash_later;
  1737. export->nbuf = req_ctx->nbuf;
  1738. return 0;
  1739. }
  1740. static int ahash_import(struct ahash_request *areq, const void *in)
  1741. {
  1742. struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
  1743. struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
  1744. const struct talitos_export_state *export = in;
  1745. memset(req_ctx, 0, sizeof(*req_ctx));
  1746. req_ctx->hw_context_size =
  1747. (crypto_ahash_digestsize(tfm) <= SHA256_DIGEST_SIZE)
  1748. ? TALITOS_MDEU_CONTEXT_SIZE_MD5_SHA1_SHA256
  1749. : TALITOS_MDEU_CONTEXT_SIZE_SHA384_SHA512;
  1750. memcpy(req_ctx->hw_context, export->hw_context,
  1751. req_ctx->hw_context_size);
  1752. memcpy(req_ctx->buf, export->buf, export->nbuf);
  1753. req_ctx->swinit = export->swinit;
  1754. req_ctx->first = export->first;
  1755. req_ctx->last = export->last;
  1756. req_ctx->to_hash_later = export->to_hash_later;
  1757. req_ctx->nbuf = export->nbuf;
  1758. return 0;
  1759. }
  1760. struct keyhash_result {
  1761. struct completion completion;
  1762. int err;
  1763. };
  1764. static void keyhash_complete(struct crypto_async_request *req, int err)
  1765. {
  1766. struct keyhash_result *res = req->data;
  1767. if (err == -EINPROGRESS)
  1768. return;
  1769. res->err = err;
  1770. complete(&res->completion);
  1771. }
  1772. static int keyhash(struct crypto_ahash *tfm, const u8 *key, unsigned int keylen,
  1773. u8 *hash)
  1774. {
  1775. struct talitos_ctx *ctx = crypto_tfm_ctx(crypto_ahash_tfm(tfm));
  1776. struct scatterlist sg[1];
  1777. struct ahash_request *req;
  1778. struct keyhash_result hresult;
  1779. int ret;
  1780. init_completion(&hresult.completion);
  1781. req = ahash_request_alloc(tfm, GFP_KERNEL);
  1782. if (!req)
  1783. return -ENOMEM;
  1784. /* Keep tfm keylen == 0 during hash of the long key */
  1785. ctx->keylen = 0;
  1786. ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
  1787. keyhash_complete, &hresult);
  1788. sg_init_one(&sg[0], key, keylen);
  1789. ahash_request_set_crypt(req, sg, hash, keylen);
  1790. ret = crypto_ahash_digest(req);
  1791. switch (ret) {
  1792. case 0:
  1793. break;
  1794. case -EINPROGRESS:
  1795. case -EBUSY:
  1796. ret = wait_for_completion_interruptible(
  1797. &hresult.completion);
  1798. if (!ret)
  1799. ret = hresult.err;
  1800. break;
  1801. default:
  1802. break;
  1803. }
  1804. ahash_request_free(req);
  1805. return ret;
  1806. }
  1807. static int ahash_setkey(struct crypto_ahash *tfm, const u8 *key,
  1808. unsigned int keylen)
  1809. {
  1810. struct talitos_ctx *ctx = crypto_tfm_ctx(crypto_ahash_tfm(tfm));
  1811. unsigned int blocksize =
  1812. crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
  1813. unsigned int digestsize = crypto_ahash_digestsize(tfm);
  1814. unsigned int keysize = keylen;
  1815. u8 hash[SHA512_DIGEST_SIZE];
  1816. int ret;
  1817. if (keylen <= blocksize)
  1818. memcpy(ctx->key, key, keysize);
  1819. else {
  1820. /* Must get the hash of the long key */
  1821. ret = keyhash(tfm, key, keylen, hash);
  1822. if (ret) {
  1823. crypto_ahash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
  1824. return -EINVAL;
  1825. }
  1826. keysize = digestsize;
  1827. memcpy(ctx->key, hash, digestsize);
  1828. }
  1829. ctx->keylen = keysize;
  1830. return 0;
  1831. }
  1832. struct talitos_alg_template {
  1833. u32 type;
  1834. u32 priority;
  1835. union {
  1836. struct crypto_alg crypto;
  1837. struct ahash_alg hash;
  1838. struct aead_alg aead;
  1839. } alg;
  1840. __be32 desc_hdr_template;
  1841. };
  1842. static struct talitos_alg_template driver_algs[] = {
  1843. /* AEAD algorithms. These use a single-pass ipsec_esp descriptor */
  1844. { .type = CRYPTO_ALG_TYPE_AEAD,
  1845. .alg.aead = {
  1846. .base = {
  1847. .cra_name = "authenc(hmac(sha1),cbc(aes))",
  1848. .cra_driver_name = "authenc-hmac-sha1-"
  1849. "cbc-aes-talitos",
  1850. .cra_blocksize = AES_BLOCK_SIZE,
  1851. .cra_flags = CRYPTO_ALG_ASYNC,
  1852. },
  1853. .ivsize = AES_BLOCK_SIZE,
  1854. .maxauthsize = SHA1_DIGEST_SIZE,
  1855. },
  1856. .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
  1857. DESC_HDR_SEL0_AESU |
  1858. DESC_HDR_MODE0_AESU_CBC |
  1859. DESC_HDR_SEL1_MDEUA |
  1860. DESC_HDR_MODE1_MDEU_INIT |
  1861. DESC_HDR_MODE1_MDEU_PAD |
  1862. DESC_HDR_MODE1_MDEU_SHA1_HMAC,
  1863. },
  1864. { .type = CRYPTO_ALG_TYPE_AEAD,
  1865. .priority = TALITOS_CRA_PRIORITY_AEAD_HSNA,
  1866. .alg.aead = {
  1867. .base = {
  1868. .cra_name = "authenc(hmac(sha1),cbc(aes))",
  1869. .cra_driver_name = "authenc-hmac-sha1-"
  1870. "cbc-aes-talitos",
  1871. .cra_blocksize = AES_BLOCK_SIZE,
  1872. .cra_flags = CRYPTO_ALG_ASYNC,
  1873. },
  1874. .ivsize = AES_BLOCK_SIZE,
  1875. .maxauthsize = SHA1_DIGEST_SIZE,
  1876. },
  1877. .desc_hdr_template = DESC_HDR_TYPE_HMAC_SNOOP_NO_AFEU |
  1878. DESC_HDR_SEL0_AESU |
  1879. DESC_HDR_MODE0_AESU_CBC |
  1880. DESC_HDR_SEL1_MDEUA |
  1881. DESC_HDR_MODE1_MDEU_INIT |
  1882. DESC_HDR_MODE1_MDEU_PAD |
  1883. DESC_HDR_MODE1_MDEU_SHA1_HMAC,
  1884. },
  1885. { .type = CRYPTO_ALG_TYPE_AEAD,
  1886. .alg.aead = {
  1887. .base = {
  1888. .cra_name = "authenc(hmac(sha1),"
  1889. "cbc(des3_ede))",
  1890. .cra_driver_name = "authenc-hmac-sha1-"
  1891. "cbc-3des-talitos",
  1892. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  1893. .cra_flags = CRYPTO_ALG_ASYNC,
  1894. },
  1895. .ivsize = DES3_EDE_BLOCK_SIZE,
  1896. .maxauthsize = SHA1_DIGEST_SIZE,
  1897. },
  1898. .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
  1899. DESC_HDR_SEL0_DEU |
  1900. DESC_HDR_MODE0_DEU_CBC |
  1901. DESC_HDR_MODE0_DEU_3DES |
  1902. DESC_HDR_SEL1_MDEUA |
  1903. DESC_HDR_MODE1_MDEU_INIT |
  1904. DESC_HDR_MODE1_MDEU_PAD |
  1905. DESC_HDR_MODE1_MDEU_SHA1_HMAC,
  1906. },
  1907. { .type = CRYPTO_ALG_TYPE_AEAD,
  1908. .priority = TALITOS_CRA_PRIORITY_AEAD_HSNA,
  1909. .alg.aead = {
  1910. .base = {
  1911. .cra_name = "authenc(hmac(sha1),"
  1912. "cbc(des3_ede))",
  1913. .cra_driver_name = "authenc-hmac-sha1-"
  1914. "cbc-3des-talitos",
  1915. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  1916. .cra_flags = CRYPTO_ALG_ASYNC,
  1917. },
  1918. .ivsize = DES3_EDE_BLOCK_SIZE,
  1919. .maxauthsize = SHA1_DIGEST_SIZE,
  1920. },
  1921. .desc_hdr_template = DESC_HDR_TYPE_HMAC_SNOOP_NO_AFEU |
  1922. DESC_HDR_SEL0_DEU |
  1923. DESC_HDR_MODE0_DEU_CBC |
  1924. DESC_HDR_MODE0_DEU_3DES |
  1925. DESC_HDR_SEL1_MDEUA |
  1926. DESC_HDR_MODE1_MDEU_INIT |
  1927. DESC_HDR_MODE1_MDEU_PAD |
  1928. DESC_HDR_MODE1_MDEU_SHA1_HMAC,
  1929. },
  1930. { .type = CRYPTO_ALG_TYPE_AEAD,
  1931. .alg.aead = {
  1932. .base = {
  1933. .cra_name = "authenc(hmac(sha224),cbc(aes))",
  1934. .cra_driver_name = "authenc-hmac-sha224-"
  1935. "cbc-aes-talitos",
  1936. .cra_blocksize = AES_BLOCK_SIZE,
  1937. .cra_flags = CRYPTO_ALG_ASYNC,
  1938. },
  1939. .ivsize = AES_BLOCK_SIZE,
  1940. .maxauthsize = SHA224_DIGEST_SIZE,
  1941. },
  1942. .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
  1943. DESC_HDR_SEL0_AESU |
  1944. DESC_HDR_MODE0_AESU_CBC |
  1945. DESC_HDR_SEL1_MDEUA |
  1946. DESC_HDR_MODE1_MDEU_INIT |
  1947. DESC_HDR_MODE1_MDEU_PAD |
  1948. DESC_HDR_MODE1_MDEU_SHA224_HMAC,
  1949. },
  1950. { .type = CRYPTO_ALG_TYPE_AEAD,
  1951. .priority = TALITOS_CRA_PRIORITY_AEAD_HSNA,
  1952. .alg.aead = {
  1953. .base = {
  1954. .cra_name = "authenc(hmac(sha224),cbc(aes))",
  1955. .cra_driver_name = "authenc-hmac-sha224-"
  1956. "cbc-aes-talitos",
  1957. .cra_blocksize = AES_BLOCK_SIZE,
  1958. .cra_flags = CRYPTO_ALG_ASYNC,
  1959. },
  1960. .ivsize = AES_BLOCK_SIZE,
  1961. .maxauthsize = SHA224_DIGEST_SIZE,
  1962. },
  1963. .desc_hdr_template = DESC_HDR_TYPE_HMAC_SNOOP_NO_AFEU |
  1964. DESC_HDR_SEL0_AESU |
  1965. DESC_HDR_MODE0_AESU_CBC |
  1966. DESC_HDR_SEL1_MDEUA |
  1967. DESC_HDR_MODE1_MDEU_INIT |
  1968. DESC_HDR_MODE1_MDEU_PAD |
  1969. DESC_HDR_MODE1_MDEU_SHA224_HMAC,
  1970. },
  1971. { .type = CRYPTO_ALG_TYPE_AEAD,
  1972. .alg.aead = {
  1973. .base = {
  1974. .cra_name = "authenc(hmac(sha224),"
  1975. "cbc(des3_ede))",
  1976. .cra_driver_name = "authenc-hmac-sha224-"
  1977. "cbc-3des-talitos",
  1978. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  1979. .cra_flags = CRYPTO_ALG_ASYNC,
  1980. },
  1981. .ivsize = DES3_EDE_BLOCK_SIZE,
  1982. .maxauthsize = SHA224_DIGEST_SIZE,
  1983. },
  1984. .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
  1985. DESC_HDR_SEL0_DEU |
  1986. DESC_HDR_MODE0_DEU_CBC |
  1987. DESC_HDR_MODE0_DEU_3DES |
  1988. DESC_HDR_SEL1_MDEUA |
  1989. DESC_HDR_MODE1_MDEU_INIT |
  1990. DESC_HDR_MODE1_MDEU_PAD |
  1991. DESC_HDR_MODE1_MDEU_SHA224_HMAC,
  1992. },
  1993. { .type = CRYPTO_ALG_TYPE_AEAD,
  1994. .priority = TALITOS_CRA_PRIORITY_AEAD_HSNA,
  1995. .alg.aead = {
  1996. .base = {
  1997. .cra_name = "authenc(hmac(sha224),"
  1998. "cbc(des3_ede))",
  1999. .cra_driver_name = "authenc-hmac-sha224-"
  2000. "cbc-3des-talitos",
  2001. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  2002. .cra_flags = CRYPTO_ALG_ASYNC,
  2003. },
  2004. .ivsize = DES3_EDE_BLOCK_SIZE,
  2005. .maxauthsize = SHA224_DIGEST_SIZE,
  2006. },
  2007. .desc_hdr_template = DESC_HDR_TYPE_HMAC_SNOOP_NO_AFEU |
  2008. DESC_HDR_SEL0_DEU |
  2009. DESC_HDR_MODE0_DEU_CBC |
  2010. DESC_HDR_MODE0_DEU_3DES |
  2011. DESC_HDR_SEL1_MDEUA |
  2012. DESC_HDR_MODE1_MDEU_INIT |
  2013. DESC_HDR_MODE1_MDEU_PAD |
  2014. DESC_HDR_MODE1_MDEU_SHA224_HMAC,
  2015. },
  2016. { .type = CRYPTO_ALG_TYPE_AEAD,
  2017. .alg.aead = {
  2018. .base = {
  2019. .cra_name = "authenc(hmac(sha256),cbc(aes))",
  2020. .cra_driver_name = "authenc-hmac-sha256-"
  2021. "cbc-aes-talitos",
  2022. .cra_blocksize = AES_BLOCK_SIZE,
  2023. .cra_flags = CRYPTO_ALG_ASYNC,
  2024. },
  2025. .ivsize = AES_BLOCK_SIZE,
  2026. .maxauthsize = SHA256_DIGEST_SIZE,
  2027. },
  2028. .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
  2029. DESC_HDR_SEL0_AESU |
  2030. DESC_HDR_MODE0_AESU_CBC |
  2031. DESC_HDR_SEL1_MDEUA |
  2032. DESC_HDR_MODE1_MDEU_INIT |
  2033. DESC_HDR_MODE1_MDEU_PAD |
  2034. DESC_HDR_MODE1_MDEU_SHA256_HMAC,
  2035. },
  2036. { .type = CRYPTO_ALG_TYPE_AEAD,
  2037. .priority = TALITOS_CRA_PRIORITY_AEAD_HSNA,
  2038. .alg.aead = {
  2039. .base = {
  2040. .cra_name = "authenc(hmac(sha256),cbc(aes))",
  2041. .cra_driver_name = "authenc-hmac-sha256-"
  2042. "cbc-aes-talitos",
  2043. .cra_blocksize = AES_BLOCK_SIZE,
  2044. .cra_flags = CRYPTO_ALG_ASYNC,
  2045. },
  2046. .ivsize = AES_BLOCK_SIZE,
  2047. .maxauthsize = SHA256_DIGEST_SIZE,
  2048. },
  2049. .desc_hdr_template = DESC_HDR_TYPE_HMAC_SNOOP_NO_AFEU |
  2050. DESC_HDR_SEL0_AESU |
  2051. DESC_HDR_MODE0_AESU_CBC |
  2052. DESC_HDR_SEL1_MDEUA |
  2053. DESC_HDR_MODE1_MDEU_INIT |
  2054. DESC_HDR_MODE1_MDEU_PAD |
  2055. DESC_HDR_MODE1_MDEU_SHA256_HMAC,
  2056. },
  2057. { .type = CRYPTO_ALG_TYPE_AEAD,
  2058. .alg.aead = {
  2059. .base = {
  2060. .cra_name = "authenc(hmac(sha256),"
  2061. "cbc(des3_ede))",
  2062. .cra_driver_name = "authenc-hmac-sha256-"
  2063. "cbc-3des-talitos",
  2064. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  2065. .cra_flags = CRYPTO_ALG_ASYNC,
  2066. },
  2067. .ivsize = DES3_EDE_BLOCK_SIZE,
  2068. .maxauthsize = SHA256_DIGEST_SIZE,
  2069. },
  2070. .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
  2071. DESC_HDR_SEL0_DEU |
  2072. DESC_HDR_MODE0_DEU_CBC |
  2073. DESC_HDR_MODE0_DEU_3DES |
  2074. DESC_HDR_SEL1_MDEUA |
  2075. DESC_HDR_MODE1_MDEU_INIT |
  2076. DESC_HDR_MODE1_MDEU_PAD |
  2077. DESC_HDR_MODE1_MDEU_SHA256_HMAC,
  2078. },
  2079. { .type = CRYPTO_ALG_TYPE_AEAD,
  2080. .priority = TALITOS_CRA_PRIORITY_AEAD_HSNA,
  2081. .alg.aead = {
  2082. .base = {
  2083. .cra_name = "authenc(hmac(sha256),"
  2084. "cbc(des3_ede))",
  2085. .cra_driver_name = "authenc-hmac-sha256-"
  2086. "cbc-3des-talitos",
  2087. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  2088. .cra_flags = CRYPTO_ALG_ASYNC,
  2089. },
  2090. .ivsize = DES3_EDE_BLOCK_SIZE,
  2091. .maxauthsize = SHA256_DIGEST_SIZE,
  2092. },
  2093. .desc_hdr_template = DESC_HDR_TYPE_HMAC_SNOOP_NO_AFEU |
  2094. DESC_HDR_SEL0_DEU |
  2095. DESC_HDR_MODE0_DEU_CBC |
  2096. DESC_HDR_MODE0_DEU_3DES |
  2097. DESC_HDR_SEL1_MDEUA |
  2098. DESC_HDR_MODE1_MDEU_INIT |
  2099. DESC_HDR_MODE1_MDEU_PAD |
  2100. DESC_HDR_MODE1_MDEU_SHA256_HMAC,
  2101. },
  2102. { .type = CRYPTO_ALG_TYPE_AEAD,
  2103. .alg.aead = {
  2104. .base = {
  2105. .cra_name = "authenc(hmac(sha384),cbc(aes))",
  2106. .cra_driver_name = "authenc-hmac-sha384-"
  2107. "cbc-aes-talitos",
  2108. .cra_blocksize = AES_BLOCK_SIZE,
  2109. .cra_flags = CRYPTO_ALG_ASYNC,
  2110. },
  2111. .ivsize = AES_BLOCK_SIZE,
  2112. .maxauthsize = SHA384_DIGEST_SIZE,
  2113. },
  2114. .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
  2115. DESC_HDR_SEL0_AESU |
  2116. DESC_HDR_MODE0_AESU_CBC |
  2117. DESC_HDR_SEL1_MDEUB |
  2118. DESC_HDR_MODE1_MDEU_INIT |
  2119. DESC_HDR_MODE1_MDEU_PAD |
  2120. DESC_HDR_MODE1_MDEUB_SHA384_HMAC,
  2121. },
  2122. { .type = CRYPTO_ALG_TYPE_AEAD,
  2123. .alg.aead = {
  2124. .base = {
  2125. .cra_name = "authenc(hmac(sha384),"
  2126. "cbc(des3_ede))",
  2127. .cra_driver_name = "authenc-hmac-sha384-"
  2128. "cbc-3des-talitos",
  2129. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  2130. .cra_flags = CRYPTO_ALG_ASYNC,
  2131. },
  2132. .ivsize = DES3_EDE_BLOCK_SIZE,
  2133. .maxauthsize = SHA384_DIGEST_SIZE,
  2134. },
  2135. .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
  2136. DESC_HDR_SEL0_DEU |
  2137. DESC_HDR_MODE0_DEU_CBC |
  2138. DESC_HDR_MODE0_DEU_3DES |
  2139. DESC_HDR_SEL1_MDEUB |
  2140. DESC_HDR_MODE1_MDEU_INIT |
  2141. DESC_HDR_MODE1_MDEU_PAD |
  2142. DESC_HDR_MODE1_MDEUB_SHA384_HMAC,
  2143. },
  2144. { .type = CRYPTO_ALG_TYPE_AEAD,
  2145. .alg.aead = {
  2146. .base = {
  2147. .cra_name = "authenc(hmac(sha512),cbc(aes))",
  2148. .cra_driver_name = "authenc-hmac-sha512-"
  2149. "cbc-aes-talitos",
  2150. .cra_blocksize = AES_BLOCK_SIZE,
  2151. .cra_flags = CRYPTO_ALG_ASYNC,
  2152. },
  2153. .ivsize = AES_BLOCK_SIZE,
  2154. .maxauthsize = SHA512_DIGEST_SIZE,
  2155. },
  2156. .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
  2157. DESC_HDR_SEL0_AESU |
  2158. DESC_HDR_MODE0_AESU_CBC |
  2159. DESC_HDR_SEL1_MDEUB |
  2160. DESC_HDR_MODE1_MDEU_INIT |
  2161. DESC_HDR_MODE1_MDEU_PAD |
  2162. DESC_HDR_MODE1_MDEUB_SHA512_HMAC,
  2163. },
  2164. { .type = CRYPTO_ALG_TYPE_AEAD,
  2165. .alg.aead = {
  2166. .base = {
  2167. .cra_name = "authenc(hmac(sha512),"
  2168. "cbc(des3_ede))",
  2169. .cra_driver_name = "authenc-hmac-sha512-"
  2170. "cbc-3des-talitos",
  2171. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  2172. .cra_flags = CRYPTO_ALG_ASYNC,
  2173. },
  2174. .ivsize = DES3_EDE_BLOCK_SIZE,
  2175. .maxauthsize = SHA512_DIGEST_SIZE,
  2176. },
  2177. .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
  2178. DESC_HDR_SEL0_DEU |
  2179. DESC_HDR_MODE0_DEU_CBC |
  2180. DESC_HDR_MODE0_DEU_3DES |
  2181. DESC_HDR_SEL1_MDEUB |
  2182. DESC_HDR_MODE1_MDEU_INIT |
  2183. DESC_HDR_MODE1_MDEU_PAD |
  2184. DESC_HDR_MODE1_MDEUB_SHA512_HMAC,
  2185. },
  2186. { .type = CRYPTO_ALG_TYPE_AEAD,
  2187. .alg.aead = {
  2188. .base = {
  2189. .cra_name = "authenc(hmac(md5),cbc(aes))",
  2190. .cra_driver_name = "authenc-hmac-md5-"
  2191. "cbc-aes-talitos",
  2192. .cra_blocksize = AES_BLOCK_SIZE,
  2193. .cra_flags = CRYPTO_ALG_ASYNC,
  2194. },
  2195. .ivsize = AES_BLOCK_SIZE,
  2196. .maxauthsize = MD5_DIGEST_SIZE,
  2197. },
  2198. .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
  2199. DESC_HDR_SEL0_AESU |
  2200. DESC_HDR_MODE0_AESU_CBC |
  2201. DESC_HDR_SEL1_MDEUA |
  2202. DESC_HDR_MODE1_MDEU_INIT |
  2203. DESC_HDR_MODE1_MDEU_PAD |
  2204. DESC_HDR_MODE1_MDEU_MD5_HMAC,
  2205. },
  2206. { .type = CRYPTO_ALG_TYPE_AEAD,
  2207. .priority = TALITOS_CRA_PRIORITY_AEAD_HSNA,
  2208. .alg.aead = {
  2209. .base = {
  2210. .cra_name = "authenc(hmac(md5),cbc(aes))",
  2211. .cra_driver_name = "authenc-hmac-md5-"
  2212. "cbc-aes-talitos",
  2213. .cra_blocksize = AES_BLOCK_SIZE,
  2214. .cra_flags = CRYPTO_ALG_ASYNC,
  2215. },
  2216. .ivsize = AES_BLOCK_SIZE,
  2217. .maxauthsize = MD5_DIGEST_SIZE,
  2218. },
  2219. .desc_hdr_template = DESC_HDR_TYPE_HMAC_SNOOP_NO_AFEU |
  2220. DESC_HDR_SEL0_AESU |
  2221. DESC_HDR_MODE0_AESU_CBC |
  2222. DESC_HDR_SEL1_MDEUA |
  2223. DESC_HDR_MODE1_MDEU_INIT |
  2224. DESC_HDR_MODE1_MDEU_PAD |
  2225. DESC_HDR_MODE1_MDEU_MD5_HMAC,
  2226. },
  2227. { .type = CRYPTO_ALG_TYPE_AEAD,
  2228. .alg.aead = {
  2229. .base = {
  2230. .cra_name = "authenc(hmac(md5),cbc(des3_ede))",
  2231. .cra_driver_name = "authenc-hmac-md5-"
  2232. "cbc-3des-talitos",
  2233. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  2234. .cra_flags = CRYPTO_ALG_ASYNC,
  2235. },
  2236. .ivsize = DES3_EDE_BLOCK_SIZE,
  2237. .maxauthsize = MD5_DIGEST_SIZE,
  2238. },
  2239. .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
  2240. DESC_HDR_SEL0_DEU |
  2241. DESC_HDR_MODE0_DEU_CBC |
  2242. DESC_HDR_MODE0_DEU_3DES |
  2243. DESC_HDR_SEL1_MDEUA |
  2244. DESC_HDR_MODE1_MDEU_INIT |
  2245. DESC_HDR_MODE1_MDEU_PAD |
  2246. DESC_HDR_MODE1_MDEU_MD5_HMAC,
  2247. },
  2248. { .type = CRYPTO_ALG_TYPE_AEAD,
  2249. .priority = TALITOS_CRA_PRIORITY_AEAD_HSNA,
  2250. .alg.aead = {
  2251. .base = {
  2252. .cra_name = "authenc(hmac(md5),cbc(des3_ede))",
  2253. .cra_driver_name = "authenc-hmac-md5-"
  2254. "cbc-3des-talitos",
  2255. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  2256. .cra_flags = CRYPTO_ALG_ASYNC,
  2257. },
  2258. .ivsize = DES3_EDE_BLOCK_SIZE,
  2259. .maxauthsize = MD5_DIGEST_SIZE,
  2260. },
  2261. .desc_hdr_template = DESC_HDR_TYPE_HMAC_SNOOP_NO_AFEU |
  2262. DESC_HDR_SEL0_DEU |
  2263. DESC_HDR_MODE0_DEU_CBC |
  2264. DESC_HDR_MODE0_DEU_3DES |
  2265. DESC_HDR_SEL1_MDEUA |
  2266. DESC_HDR_MODE1_MDEU_INIT |
  2267. DESC_HDR_MODE1_MDEU_PAD |
  2268. DESC_HDR_MODE1_MDEU_MD5_HMAC,
  2269. },
  2270. /* ABLKCIPHER algorithms. */
  2271. { .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
  2272. .alg.crypto = {
  2273. .cra_name = "ecb(aes)",
  2274. .cra_driver_name = "ecb-aes-talitos",
  2275. .cra_blocksize = AES_BLOCK_SIZE,
  2276. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
  2277. CRYPTO_ALG_ASYNC,
  2278. .cra_ablkcipher = {
  2279. .min_keysize = AES_MIN_KEY_SIZE,
  2280. .max_keysize = AES_MAX_KEY_SIZE,
  2281. .ivsize = AES_BLOCK_SIZE,
  2282. }
  2283. },
  2284. .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
  2285. DESC_HDR_SEL0_AESU,
  2286. },
  2287. { .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
  2288. .alg.crypto = {
  2289. .cra_name = "cbc(aes)",
  2290. .cra_driver_name = "cbc-aes-talitos",
  2291. .cra_blocksize = AES_BLOCK_SIZE,
  2292. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
  2293. CRYPTO_ALG_ASYNC,
  2294. .cra_ablkcipher = {
  2295. .min_keysize = AES_MIN_KEY_SIZE,
  2296. .max_keysize = AES_MAX_KEY_SIZE,
  2297. .ivsize = AES_BLOCK_SIZE,
  2298. }
  2299. },
  2300. .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
  2301. DESC_HDR_SEL0_AESU |
  2302. DESC_HDR_MODE0_AESU_CBC,
  2303. },
  2304. { .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
  2305. .alg.crypto = {
  2306. .cra_name = "ctr(aes)",
  2307. .cra_driver_name = "ctr-aes-talitos",
  2308. .cra_blocksize = AES_BLOCK_SIZE,
  2309. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
  2310. CRYPTO_ALG_ASYNC,
  2311. .cra_ablkcipher = {
  2312. .min_keysize = AES_MIN_KEY_SIZE,
  2313. .max_keysize = AES_MAX_KEY_SIZE,
  2314. .ivsize = AES_BLOCK_SIZE,
  2315. }
  2316. },
  2317. .desc_hdr_template = DESC_HDR_TYPE_AESU_CTR_NONSNOOP |
  2318. DESC_HDR_SEL0_AESU |
  2319. DESC_HDR_MODE0_AESU_CTR,
  2320. },
  2321. { .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
  2322. .alg.crypto = {
  2323. .cra_name = "ecb(des)",
  2324. .cra_driver_name = "ecb-des-talitos",
  2325. .cra_blocksize = DES_BLOCK_SIZE,
  2326. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
  2327. CRYPTO_ALG_ASYNC,
  2328. .cra_ablkcipher = {
  2329. .min_keysize = DES_KEY_SIZE,
  2330. .max_keysize = DES_KEY_SIZE,
  2331. .ivsize = DES_BLOCK_SIZE,
  2332. }
  2333. },
  2334. .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
  2335. DESC_HDR_SEL0_DEU,
  2336. },
  2337. { .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
  2338. .alg.crypto = {
  2339. .cra_name = "cbc(des)",
  2340. .cra_driver_name = "cbc-des-talitos",
  2341. .cra_blocksize = DES_BLOCK_SIZE,
  2342. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
  2343. CRYPTO_ALG_ASYNC,
  2344. .cra_ablkcipher = {
  2345. .min_keysize = DES_KEY_SIZE,
  2346. .max_keysize = DES_KEY_SIZE,
  2347. .ivsize = DES_BLOCK_SIZE,
  2348. }
  2349. },
  2350. .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
  2351. DESC_HDR_SEL0_DEU |
  2352. DESC_HDR_MODE0_DEU_CBC,
  2353. },
  2354. { .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
  2355. .alg.crypto = {
  2356. .cra_name = "ecb(des3_ede)",
  2357. .cra_driver_name = "ecb-3des-talitos",
  2358. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  2359. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
  2360. CRYPTO_ALG_ASYNC,
  2361. .cra_ablkcipher = {
  2362. .min_keysize = DES3_EDE_KEY_SIZE,
  2363. .max_keysize = DES3_EDE_KEY_SIZE,
  2364. .ivsize = DES3_EDE_BLOCK_SIZE,
  2365. }
  2366. },
  2367. .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
  2368. DESC_HDR_SEL0_DEU |
  2369. DESC_HDR_MODE0_DEU_3DES,
  2370. },
  2371. { .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
  2372. .alg.crypto = {
  2373. .cra_name = "cbc(des3_ede)",
  2374. .cra_driver_name = "cbc-3des-talitos",
  2375. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  2376. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
  2377. CRYPTO_ALG_ASYNC,
  2378. .cra_ablkcipher = {
  2379. .min_keysize = DES3_EDE_KEY_SIZE,
  2380. .max_keysize = DES3_EDE_KEY_SIZE,
  2381. .ivsize = DES3_EDE_BLOCK_SIZE,
  2382. }
  2383. },
  2384. .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
  2385. DESC_HDR_SEL0_DEU |
  2386. DESC_HDR_MODE0_DEU_CBC |
  2387. DESC_HDR_MODE0_DEU_3DES,
  2388. },
  2389. /* AHASH algorithms. */
  2390. { .type = CRYPTO_ALG_TYPE_AHASH,
  2391. .alg.hash = {
  2392. .halg.digestsize = MD5_DIGEST_SIZE,
  2393. .halg.statesize = sizeof(struct talitos_export_state),
  2394. .halg.base = {
  2395. .cra_name = "md5",
  2396. .cra_driver_name = "md5-talitos",
  2397. .cra_blocksize = MD5_HMAC_BLOCK_SIZE,
  2398. .cra_flags = CRYPTO_ALG_TYPE_AHASH |
  2399. CRYPTO_ALG_ASYNC,
  2400. }
  2401. },
  2402. .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
  2403. DESC_HDR_SEL0_MDEUA |
  2404. DESC_HDR_MODE0_MDEU_MD5,
  2405. },
  2406. { .type = CRYPTO_ALG_TYPE_AHASH,
  2407. .alg.hash = {
  2408. .halg.digestsize = SHA1_DIGEST_SIZE,
  2409. .halg.statesize = sizeof(struct talitos_export_state),
  2410. .halg.base = {
  2411. .cra_name = "sha1",
  2412. .cra_driver_name = "sha1-talitos",
  2413. .cra_blocksize = SHA1_BLOCK_SIZE,
  2414. .cra_flags = CRYPTO_ALG_TYPE_AHASH |
  2415. CRYPTO_ALG_ASYNC,
  2416. }
  2417. },
  2418. .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
  2419. DESC_HDR_SEL0_MDEUA |
  2420. DESC_HDR_MODE0_MDEU_SHA1,
  2421. },
  2422. { .type = CRYPTO_ALG_TYPE_AHASH,
  2423. .alg.hash = {
  2424. .halg.digestsize = SHA224_DIGEST_SIZE,
  2425. .halg.statesize = sizeof(struct talitos_export_state),
  2426. .halg.base = {
  2427. .cra_name = "sha224",
  2428. .cra_driver_name = "sha224-talitos",
  2429. .cra_blocksize = SHA224_BLOCK_SIZE,
  2430. .cra_flags = CRYPTO_ALG_TYPE_AHASH |
  2431. CRYPTO_ALG_ASYNC,
  2432. }
  2433. },
  2434. .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
  2435. DESC_HDR_SEL0_MDEUA |
  2436. DESC_HDR_MODE0_MDEU_SHA224,
  2437. },
  2438. { .type = CRYPTO_ALG_TYPE_AHASH,
  2439. .alg.hash = {
  2440. .halg.digestsize = SHA256_DIGEST_SIZE,
  2441. .halg.statesize = sizeof(struct talitos_export_state),
  2442. .halg.base = {
  2443. .cra_name = "sha256",
  2444. .cra_driver_name = "sha256-talitos",
  2445. .cra_blocksize = SHA256_BLOCK_SIZE,
  2446. .cra_flags = CRYPTO_ALG_TYPE_AHASH |
  2447. CRYPTO_ALG_ASYNC,
  2448. }
  2449. },
  2450. .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
  2451. DESC_HDR_SEL0_MDEUA |
  2452. DESC_HDR_MODE0_MDEU_SHA256,
  2453. },
  2454. { .type = CRYPTO_ALG_TYPE_AHASH,
  2455. .alg.hash = {
  2456. .halg.digestsize = SHA384_DIGEST_SIZE,
  2457. .halg.statesize = sizeof(struct talitos_export_state),
  2458. .halg.base = {
  2459. .cra_name = "sha384",
  2460. .cra_driver_name = "sha384-talitos",
  2461. .cra_blocksize = SHA384_BLOCK_SIZE,
  2462. .cra_flags = CRYPTO_ALG_TYPE_AHASH |
  2463. CRYPTO_ALG_ASYNC,
  2464. }
  2465. },
  2466. .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
  2467. DESC_HDR_SEL0_MDEUB |
  2468. DESC_HDR_MODE0_MDEUB_SHA384,
  2469. },
  2470. { .type = CRYPTO_ALG_TYPE_AHASH,
  2471. .alg.hash = {
  2472. .halg.digestsize = SHA512_DIGEST_SIZE,
  2473. .halg.statesize = sizeof(struct talitos_export_state),
  2474. .halg.base = {
  2475. .cra_name = "sha512",
  2476. .cra_driver_name = "sha512-talitos",
  2477. .cra_blocksize = SHA512_BLOCK_SIZE,
  2478. .cra_flags = CRYPTO_ALG_TYPE_AHASH |
  2479. CRYPTO_ALG_ASYNC,
  2480. }
  2481. },
  2482. .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
  2483. DESC_HDR_SEL0_MDEUB |
  2484. DESC_HDR_MODE0_MDEUB_SHA512,
  2485. },
  2486. { .type = CRYPTO_ALG_TYPE_AHASH,
  2487. .alg.hash = {
  2488. .halg.digestsize = MD5_DIGEST_SIZE,
  2489. .halg.statesize = sizeof(struct talitos_export_state),
  2490. .halg.base = {
  2491. .cra_name = "hmac(md5)",
  2492. .cra_driver_name = "hmac-md5-talitos",
  2493. .cra_blocksize = MD5_HMAC_BLOCK_SIZE,
  2494. .cra_flags = CRYPTO_ALG_TYPE_AHASH |
  2495. CRYPTO_ALG_ASYNC,
  2496. }
  2497. },
  2498. .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
  2499. DESC_HDR_SEL0_MDEUA |
  2500. DESC_HDR_MODE0_MDEU_MD5,
  2501. },
  2502. { .type = CRYPTO_ALG_TYPE_AHASH,
  2503. .alg.hash = {
  2504. .halg.digestsize = SHA1_DIGEST_SIZE,
  2505. .halg.statesize = sizeof(struct talitos_export_state),
  2506. .halg.base = {
  2507. .cra_name = "hmac(sha1)",
  2508. .cra_driver_name = "hmac-sha1-talitos",
  2509. .cra_blocksize = SHA1_BLOCK_SIZE,
  2510. .cra_flags = CRYPTO_ALG_TYPE_AHASH |
  2511. CRYPTO_ALG_ASYNC,
  2512. }
  2513. },
  2514. .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
  2515. DESC_HDR_SEL0_MDEUA |
  2516. DESC_HDR_MODE0_MDEU_SHA1,
  2517. },
  2518. { .type = CRYPTO_ALG_TYPE_AHASH,
  2519. .alg.hash = {
  2520. .halg.digestsize = SHA224_DIGEST_SIZE,
  2521. .halg.statesize = sizeof(struct talitos_export_state),
  2522. .halg.base = {
  2523. .cra_name = "hmac(sha224)",
  2524. .cra_driver_name = "hmac-sha224-talitos",
  2525. .cra_blocksize = SHA224_BLOCK_SIZE,
  2526. .cra_flags = CRYPTO_ALG_TYPE_AHASH |
  2527. CRYPTO_ALG_ASYNC,
  2528. }
  2529. },
  2530. .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
  2531. DESC_HDR_SEL0_MDEUA |
  2532. DESC_HDR_MODE0_MDEU_SHA224,
  2533. },
  2534. { .type = CRYPTO_ALG_TYPE_AHASH,
  2535. .alg.hash = {
  2536. .halg.digestsize = SHA256_DIGEST_SIZE,
  2537. .halg.statesize = sizeof(struct talitos_export_state),
  2538. .halg.base = {
  2539. .cra_name = "hmac(sha256)",
  2540. .cra_driver_name = "hmac-sha256-talitos",
  2541. .cra_blocksize = SHA256_BLOCK_SIZE,
  2542. .cra_flags = CRYPTO_ALG_TYPE_AHASH |
  2543. CRYPTO_ALG_ASYNC,
  2544. }
  2545. },
  2546. .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
  2547. DESC_HDR_SEL0_MDEUA |
  2548. DESC_HDR_MODE0_MDEU_SHA256,
  2549. },
  2550. { .type = CRYPTO_ALG_TYPE_AHASH,
  2551. .alg.hash = {
  2552. .halg.digestsize = SHA384_DIGEST_SIZE,
  2553. .halg.statesize = sizeof(struct talitos_export_state),
  2554. .halg.base = {
  2555. .cra_name = "hmac(sha384)",
  2556. .cra_driver_name = "hmac-sha384-talitos",
  2557. .cra_blocksize = SHA384_BLOCK_SIZE,
  2558. .cra_flags = CRYPTO_ALG_TYPE_AHASH |
  2559. CRYPTO_ALG_ASYNC,
  2560. }
  2561. },
  2562. .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
  2563. DESC_HDR_SEL0_MDEUB |
  2564. DESC_HDR_MODE0_MDEUB_SHA384,
  2565. },
  2566. { .type = CRYPTO_ALG_TYPE_AHASH,
  2567. .alg.hash = {
  2568. .halg.digestsize = SHA512_DIGEST_SIZE,
  2569. .halg.statesize = sizeof(struct talitos_export_state),
  2570. .halg.base = {
  2571. .cra_name = "hmac(sha512)",
  2572. .cra_driver_name = "hmac-sha512-talitos",
  2573. .cra_blocksize = SHA512_BLOCK_SIZE,
  2574. .cra_flags = CRYPTO_ALG_TYPE_AHASH |
  2575. CRYPTO_ALG_ASYNC,
  2576. }
  2577. },
  2578. .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
  2579. DESC_HDR_SEL0_MDEUB |
  2580. DESC_HDR_MODE0_MDEUB_SHA512,
  2581. }
  2582. };
  2583. struct talitos_crypto_alg {
  2584. struct list_head entry;
  2585. struct device *dev;
  2586. struct talitos_alg_template algt;
  2587. };
  2588. static int talitos_init_common(struct talitos_ctx *ctx,
  2589. struct talitos_crypto_alg *talitos_alg)
  2590. {
  2591. struct talitos_private *priv;
  2592. /* update context with ptr to dev */
  2593. ctx->dev = talitos_alg->dev;
  2594. /* assign SEC channel to tfm in round-robin fashion */
  2595. priv = dev_get_drvdata(ctx->dev);
  2596. ctx->ch = atomic_inc_return(&priv->last_chan) &
  2597. (priv->num_channels - 1);
  2598. /* copy descriptor header template value */
  2599. ctx->desc_hdr_template = talitos_alg->algt.desc_hdr_template;
  2600. /* select done notification */
  2601. ctx->desc_hdr_template |= DESC_HDR_DONE_NOTIFY;
  2602. return 0;
  2603. }
  2604. static int talitos_cra_init(struct crypto_tfm *tfm)
  2605. {
  2606. struct crypto_alg *alg = tfm->__crt_alg;
  2607. struct talitos_crypto_alg *talitos_alg;
  2608. struct talitos_ctx *ctx = crypto_tfm_ctx(tfm);
  2609. if ((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) == CRYPTO_ALG_TYPE_AHASH)
  2610. talitos_alg = container_of(__crypto_ahash_alg(alg),
  2611. struct talitos_crypto_alg,
  2612. algt.alg.hash);
  2613. else
  2614. talitos_alg = container_of(alg, struct talitos_crypto_alg,
  2615. algt.alg.crypto);
  2616. return talitos_init_common(ctx, talitos_alg);
  2617. }
  2618. static int talitos_cra_init_aead(struct crypto_aead *tfm)
  2619. {
  2620. struct aead_alg *alg = crypto_aead_alg(tfm);
  2621. struct talitos_crypto_alg *talitos_alg;
  2622. struct talitos_ctx *ctx = crypto_aead_ctx(tfm);
  2623. talitos_alg = container_of(alg, struct talitos_crypto_alg,
  2624. algt.alg.aead);
  2625. return talitos_init_common(ctx, talitos_alg);
  2626. }
  2627. static int talitos_cra_init_ahash(struct crypto_tfm *tfm)
  2628. {
  2629. struct talitos_ctx *ctx = crypto_tfm_ctx(tfm);
  2630. talitos_cra_init(tfm);
  2631. ctx->keylen = 0;
  2632. crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
  2633. sizeof(struct talitos_ahash_req_ctx));
  2634. return 0;
  2635. }
  2636. /*
  2637. * given the alg's descriptor header template, determine whether descriptor
  2638. * type and primary/secondary execution units required match the hw
  2639. * capabilities description provided in the device tree node.
  2640. */
  2641. static int hw_supports(struct device *dev, __be32 desc_hdr_template)
  2642. {
  2643. struct talitos_private *priv = dev_get_drvdata(dev);
  2644. int ret;
  2645. ret = (1 << DESC_TYPE(desc_hdr_template) & priv->desc_types) &&
  2646. (1 << PRIMARY_EU(desc_hdr_template) & priv->exec_units);
  2647. if (SECONDARY_EU(desc_hdr_template))
  2648. ret = ret && (1 << SECONDARY_EU(desc_hdr_template)
  2649. & priv->exec_units);
  2650. return ret;
  2651. }
  2652. static int talitos_remove(struct platform_device *ofdev)
  2653. {
  2654. struct device *dev = &ofdev->dev;
  2655. struct talitos_private *priv = dev_get_drvdata(dev);
  2656. struct talitos_crypto_alg *t_alg, *n;
  2657. int i;
  2658. list_for_each_entry_safe(t_alg, n, &priv->alg_list, entry) {
  2659. switch (t_alg->algt.type) {
  2660. case CRYPTO_ALG_TYPE_ABLKCIPHER:
  2661. break;
  2662. case CRYPTO_ALG_TYPE_AEAD:
  2663. crypto_unregister_aead(&t_alg->algt.alg.aead);
  2664. case CRYPTO_ALG_TYPE_AHASH:
  2665. crypto_unregister_ahash(&t_alg->algt.alg.hash);
  2666. break;
  2667. }
  2668. list_del(&t_alg->entry);
  2669. kfree(t_alg);
  2670. }
  2671. if (hw_supports(dev, DESC_HDR_SEL0_RNG))
  2672. talitos_unregister_rng(dev);
  2673. for (i = 0; priv->chan && i < priv->num_channels; i++)
  2674. kfree(priv->chan[i].fifo);
  2675. kfree(priv->chan);
  2676. for (i = 0; i < 2; i++)
  2677. if (priv->irq[i]) {
  2678. free_irq(priv->irq[i], dev);
  2679. irq_dispose_mapping(priv->irq[i]);
  2680. }
  2681. tasklet_kill(&priv->done_task[0]);
  2682. if (priv->irq[1])
  2683. tasklet_kill(&priv->done_task[1]);
  2684. iounmap(priv->reg);
  2685. kfree(priv);
  2686. return 0;
  2687. }
  2688. static struct talitos_crypto_alg *talitos_alg_alloc(struct device *dev,
  2689. struct talitos_alg_template
  2690. *template)
  2691. {
  2692. struct talitos_private *priv = dev_get_drvdata(dev);
  2693. struct talitos_crypto_alg *t_alg;
  2694. struct crypto_alg *alg;
  2695. t_alg = kzalloc(sizeof(struct talitos_crypto_alg), GFP_KERNEL);
  2696. if (!t_alg)
  2697. return ERR_PTR(-ENOMEM);
  2698. t_alg->algt = *template;
  2699. switch (t_alg->algt.type) {
  2700. case CRYPTO_ALG_TYPE_ABLKCIPHER:
  2701. alg = &t_alg->algt.alg.crypto;
  2702. alg->cra_init = talitos_cra_init;
  2703. alg->cra_type = &crypto_ablkcipher_type;
  2704. alg->cra_ablkcipher.setkey = ablkcipher_setkey;
  2705. alg->cra_ablkcipher.encrypt = ablkcipher_encrypt;
  2706. alg->cra_ablkcipher.decrypt = ablkcipher_decrypt;
  2707. alg->cra_ablkcipher.geniv = "eseqiv";
  2708. break;
  2709. case CRYPTO_ALG_TYPE_AEAD:
  2710. alg = &t_alg->algt.alg.aead.base;
  2711. t_alg->algt.alg.aead.init = talitos_cra_init_aead;
  2712. t_alg->algt.alg.aead.setkey = aead_setkey;
  2713. t_alg->algt.alg.aead.encrypt = aead_encrypt;
  2714. t_alg->algt.alg.aead.decrypt = aead_decrypt;
  2715. if (!(priv->features & TALITOS_FTR_SHA224_HWINIT) &&
  2716. !strncmp(alg->cra_name, "authenc(hmac(sha224)", 20)) {
  2717. kfree(t_alg);
  2718. return ERR_PTR(-ENOTSUPP);
  2719. }
  2720. break;
  2721. case CRYPTO_ALG_TYPE_AHASH:
  2722. alg = &t_alg->algt.alg.hash.halg.base;
  2723. alg->cra_init = talitos_cra_init_ahash;
  2724. alg->cra_type = &crypto_ahash_type;
  2725. t_alg->algt.alg.hash.init = ahash_init;
  2726. t_alg->algt.alg.hash.update = ahash_update;
  2727. t_alg->algt.alg.hash.final = ahash_final;
  2728. t_alg->algt.alg.hash.finup = ahash_finup;
  2729. t_alg->algt.alg.hash.digest = ahash_digest;
  2730. if (!strncmp(alg->cra_name, "hmac", 4))
  2731. t_alg->algt.alg.hash.setkey = ahash_setkey;
  2732. t_alg->algt.alg.hash.import = ahash_import;
  2733. t_alg->algt.alg.hash.export = ahash_export;
  2734. if (!(priv->features & TALITOS_FTR_HMAC_OK) &&
  2735. !strncmp(alg->cra_name, "hmac", 4)) {
  2736. kfree(t_alg);
  2737. return ERR_PTR(-ENOTSUPP);
  2738. }
  2739. if (!(priv->features & TALITOS_FTR_SHA224_HWINIT) &&
  2740. (!strcmp(alg->cra_name, "sha224") ||
  2741. !strcmp(alg->cra_name, "hmac(sha224)"))) {
  2742. t_alg->algt.alg.hash.init = ahash_init_sha224_swinit;
  2743. t_alg->algt.desc_hdr_template =
  2744. DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
  2745. DESC_HDR_SEL0_MDEUA |
  2746. DESC_HDR_MODE0_MDEU_SHA256;
  2747. }
  2748. break;
  2749. default:
  2750. dev_err(dev, "unknown algorithm type %d\n", t_alg->algt.type);
  2751. kfree(t_alg);
  2752. return ERR_PTR(-EINVAL);
  2753. }
  2754. alg->cra_module = THIS_MODULE;
  2755. if (t_alg->algt.priority)
  2756. alg->cra_priority = t_alg->algt.priority;
  2757. else
  2758. alg->cra_priority = TALITOS_CRA_PRIORITY;
  2759. alg->cra_alignmask = 0;
  2760. alg->cra_ctxsize = sizeof(struct talitos_ctx);
  2761. alg->cra_flags |= CRYPTO_ALG_KERN_DRIVER_ONLY;
  2762. t_alg->dev = dev;
  2763. return t_alg;
  2764. }
  2765. static int talitos_probe_irq(struct platform_device *ofdev)
  2766. {
  2767. struct device *dev = &ofdev->dev;
  2768. struct device_node *np = ofdev->dev.of_node;
  2769. struct talitos_private *priv = dev_get_drvdata(dev);
  2770. int err;
  2771. bool is_sec1 = has_ftr_sec1(priv);
  2772. priv->irq[0] = irq_of_parse_and_map(np, 0);
  2773. if (!priv->irq[0]) {
  2774. dev_err(dev, "failed to map irq\n");
  2775. return -EINVAL;
  2776. }
  2777. if (is_sec1) {
  2778. err = request_irq(priv->irq[0], talitos1_interrupt_4ch, 0,
  2779. dev_driver_string(dev), dev);
  2780. goto primary_out;
  2781. }
  2782. priv->irq[1] = irq_of_parse_and_map(np, 1);
  2783. /* get the primary irq line */
  2784. if (!priv->irq[1]) {
  2785. err = request_irq(priv->irq[0], talitos2_interrupt_4ch, 0,
  2786. dev_driver_string(dev), dev);
  2787. goto primary_out;
  2788. }
  2789. err = request_irq(priv->irq[0], talitos2_interrupt_ch0_2, 0,
  2790. dev_driver_string(dev), dev);
  2791. if (err)
  2792. goto primary_out;
  2793. /* get the secondary irq line */
  2794. err = request_irq(priv->irq[1], talitos2_interrupt_ch1_3, 0,
  2795. dev_driver_string(dev), dev);
  2796. if (err) {
  2797. dev_err(dev, "failed to request secondary irq\n");
  2798. irq_dispose_mapping(priv->irq[1]);
  2799. priv->irq[1] = 0;
  2800. }
  2801. return err;
  2802. primary_out:
  2803. if (err) {
  2804. dev_err(dev, "failed to request primary irq\n");
  2805. irq_dispose_mapping(priv->irq[0]);
  2806. priv->irq[0] = 0;
  2807. }
  2808. return err;
  2809. }
  2810. static int talitos_probe(struct platform_device *ofdev)
  2811. {
  2812. struct device *dev = &ofdev->dev;
  2813. struct device_node *np = ofdev->dev.of_node;
  2814. struct talitos_private *priv;
  2815. const unsigned int *prop;
  2816. int i, err;
  2817. int stride;
  2818. priv = kzalloc(sizeof(struct talitos_private), GFP_KERNEL);
  2819. if (!priv)
  2820. return -ENOMEM;
  2821. INIT_LIST_HEAD(&priv->alg_list);
  2822. dev_set_drvdata(dev, priv);
  2823. priv->ofdev = ofdev;
  2824. spin_lock_init(&priv->reg_lock);
  2825. priv->reg = of_iomap(np, 0);
  2826. if (!priv->reg) {
  2827. dev_err(dev, "failed to of_iomap\n");
  2828. err = -ENOMEM;
  2829. goto err_out;
  2830. }
  2831. /* get SEC version capabilities from device tree */
  2832. prop = of_get_property(np, "fsl,num-channels", NULL);
  2833. if (prop)
  2834. priv->num_channels = *prop;
  2835. prop = of_get_property(np, "fsl,channel-fifo-len", NULL);
  2836. if (prop)
  2837. priv->chfifo_len = *prop;
  2838. prop = of_get_property(np, "fsl,exec-units-mask", NULL);
  2839. if (prop)
  2840. priv->exec_units = *prop;
  2841. prop = of_get_property(np, "fsl,descriptor-types-mask", NULL);
  2842. if (prop)
  2843. priv->desc_types = *prop;
  2844. if (!is_power_of_2(priv->num_channels) || !priv->chfifo_len ||
  2845. !priv->exec_units || !priv->desc_types) {
  2846. dev_err(dev, "invalid property data in device tree node\n");
  2847. err = -EINVAL;
  2848. goto err_out;
  2849. }
  2850. if (of_device_is_compatible(np, "fsl,sec3.0"))
  2851. priv->features |= TALITOS_FTR_SRC_LINK_TBL_LEN_INCLUDES_EXTENT;
  2852. if (of_device_is_compatible(np, "fsl,sec2.1"))
  2853. priv->features |= TALITOS_FTR_HW_AUTH_CHECK |
  2854. TALITOS_FTR_SHA224_HWINIT |
  2855. TALITOS_FTR_HMAC_OK;
  2856. if (of_device_is_compatible(np, "fsl,sec1.0"))
  2857. priv->features |= TALITOS_FTR_SEC1;
  2858. if (of_device_is_compatible(np, "fsl,sec1.2")) {
  2859. priv->reg_deu = priv->reg + TALITOS12_DEU;
  2860. priv->reg_aesu = priv->reg + TALITOS12_AESU;
  2861. priv->reg_mdeu = priv->reg + TALITOS12_MDEU;
  2862. stride = TALITOS1_CH_STRIDE;
  2863. } else if (of_device_is_compatible(np, "fsl,sec1.0")) {
  2864. priv->reg_deu = priv->reg + TALITOS10_DEU;
  2865. priv->reg_aesu = priv->reg + TALITOS10_AESU;
  2866. priv->reg_mdeu = priv->reg + TALITOS10_MDEU;
  2867. priv->reg_afeu = priv->reg + TALITOS10_AFEU;
  2868. priv->reg_rngu = priv->reg + TALITOS10_RNGU;
  2869. priv->reg_pkeu = priv->reg + TALITOS10_PKEU;
  2870. stride = TALITOS1_CH_STRIDE;
  2871. } else {
  2872. priv->reg_deu = priv->reg + TALITOS2_DEU;
  2873. priv->reg_aesu = priv->reg + TALITOS2_AESU;
  2874. priv->reg_mdeu = priv->reg + TALITOS2_MDEU;
  2875. priv->reg_afeu = priv->reg + TALITOS2_AFEU;
  2876. priv->reg_rngu = priv->reg + TALITOS2_RNGU;
  2877. priv->reg_pkeu = priv->reg + TALITOS2_PKEU;
  2878. priv->reg_keu = priv->reg + TALITOS2_KEU;
  2879. priv->reg_crcu = priv->reg + TALITOS2_CRCU;
  2880. stride = TALITOS2_CH_STRIDE;
  2881. }
  2882. err = talitos_probe_irq(ofdev);
  2883. if (err)
  2884. goto err_out;
  2885. if (of_device_is_compatible(np, "fsl,sec1.0")) {
  2886. tasklet_init(&priv->done_task[0], talitos1_done_4ch,
  2887. (unsigned long)dev);
  2888. } else {
  2889. if (!priv->irq[1]) {
  2890. tasklet_init(&priv->done_task[0], talitos2_done_4ch,
  2891. (unsigned long)dev);
  2892. } else {
  2893. tasklet_init(&priv->done_task[0], talitos2_done_ch0_2,
  2894. (unsigned long)dev);
  2895. tasklet_init(&priv->done_task[1], talitos2_done_ch1_3,
  2896. (unsigned long)dev);
  2897. }
  2898. }
  2899. priv->chan = kzalloc(sizeof(struct talitos_channel) *
  2900. priv->num_channels, GFP_KERNEL);
  2901. if (!priv->chan) {
  2902. dev_err(dev, "failed to allocate channel management space\n");
  2903. err = -ENOMEM;
  2904. goto err_out;
  2905. }
  2906. priv->fifo_len = roundup_pow_of_two(priv->chfifo_len);
  2907. for (i = 0; i < priv->num_channels; i++) {
  2908. priv->chan[i].reg = priv->reg + stride * (i + 1);
  2909. if (!priv->irq[1] || !(i & 1))
  2910. priv->chan[i].reg += TALITOS_CH_BASE_OFFSET;
  2911. spin_lock_init(&priv->chan[i].head_lock);
  2912. spin_lock_init(&priv->chan[i].tail_lock);
  2913. priv->chan[i].fifo = kzalloc(sizeof(struct talitos_request) *
  2914. priv->fifo_len, GFP_KERNEL);
  2915. if (!priv->chan[i].fifo) {
  2916. dev_err(dev, "failed to allocate request fifo %d\n", i);
  2917. err = -ENOMEM;
  2918. goto err_out;
  2919. }
  2920. atomic_set(&priv->chan[i].submit_count,
  2921. -(priv->chfifo_len - 1));
  2922. }
  2923. dma_set_mask(dev, DMA_BIT_MASK(36));
  2924. /* reset and initialize the h/w */
  2925. err = init_device(dev);
  2926. if (err) {
  2927. dev_err(dev, "failed to initialize device\n");
  2928. goto err_out;
  2929. }
  2930. /* register the RNG, if available */
  2931. if (hw_supports(dev, DESC_HDR_SEL0_RNG)) {
  2932. err = talitos_register_rng(dev);
  2933. if (err) {
  2934. dev_err(dev, "failed to register hwrng: %d\n", err);
  2935. goto err_out;
  2936. } else
  2937. dev_info(dev, "hwrng\n");
  2938. }
  2939. /* register crypto algorithms the device supports */
  2940. for (i = 0; i < ARRAY_SIZE(driver_algs); i++) {
  2941. if (hw_supports(dev, driver_algs[i].desc_hdr_template)) {
  2942. struct talitos_crypto_alg *t_alg;
  2943. struct crypto_alg *alg = NULL;
  2944. t_alg = talitos_alg_alloc(dev, &driver_algs[i]);
  2945. if (IS_ERR(t_alg)) {
  2946. err = PTR_ERR(t_alg);
  2947. if (err == -ENOTSUPP)
  2948. continue;
  2949. goto err_out;
  2950. }
  2951. switch (t_alg->algt.type) {
  2952. case CRYPTO_ALG_TYPE_ABLKCIPHER:
  2953. err = crypto_register_alg(
  2954. &t_alg->algt.alg.crypto);
  2955. alg = &t_alg->algt.alg.crypto;
  2956. break;
  2957. case CRYPTO_ALG_TYPE_AEAD:
  2958. err = crypto_register_aead(
  2959. &t_alg->algt.alg.aead);
  2960. alg = &t_alg->algt.alg.aead.base;
  2961. break;
  2962. case CRYPTO_ALG_TYPE_AHASH:
  2963. err = crypto_register_ahash(
  2964. &t_alg->algt.alg.hash);
  2965. alg = &t_alg->algt.alg.hash.halg.base;
  2966. break;
  2967. }
  2968. if (err) {
  2969. dev_err(dev, "%s alg registration failed\n",
  2970. alg->cra_driver_name);
  2971. kfree(t_alg);
  2972. } else
  2973. list_add_tail(&t_alg->entry, &priv->alg_list);
  2974. }
  2975. }
  2976. if (!list_empty(&priv->alg_list))
  2977. dev_info(dev, "%s algorithms registered in /proc/crypto\n",
  2978. (char *)of_get_property(np, "compatible", NULL));
  2979. return 0;
  2980. err_out:
  2981. talitos_remove(ofdev);
  2982. return err;
  2983. }
  2984. static const struct of_device_id talitos_match[] = {
  2985. #ifdef CONFIG_CRYPTO_DEV_TALITOS1
  2986. {
  2987. .compatible = "fsl,sec1.0",
  2988. },
  2989. #endif
  2990. #ifdef CONFIG_CRYPTO_DEV_TALITOS2
  2991. {
  2992. .compatible = "fsl,sec2.0",
  2993. },
  2994. #endif
  2995. {},
  2996. };
  2997. MODULE_DEVICE_TABLE(of, talitos_match);
  2998. static struct platform_driver talitos_driver = {
  2999. .driver = {
  3000. .name = "talitos",
  3001. .of_match_table = talitos_match,
  3002. },
  3003. .probe = talitos_probe,
  3004. .remove = talitos_remove,
  3005. };
  3006. module_platform_driver(talitos_driver);
  3007. MODULE_LICENSE("GPL");
  3008. MODULE_AUTHOR("Kim Phillips <kim.phillips@freescale.com>");
  3009. MODULE_DESCRIPTION("Freescale integrated security engine (SEC) driver");