talitos.c 100 KB

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