talitos.c 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607
  1. /*
  2. * talitos - Freescale Integrated Security Engine (SEC) device driver
  3. *
  4. * Copyright (c) 2008-2010 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_platform.h>
  35. #include <linux/dma-mapping.h>
  36. #include <linux/io.h>
  37. #include <linux/spinlock.h>
  38. #include <linux/rtnetlink.h>
  39. #include <linux/slab.h>
  40. #include <crypto/algapi.h>
  41. #include <crypto/aes.h>
  42. #include <crypto/des.h>
  43. #include <crypto/sha.h>
  44. #include <crypto/md5.h>
  45. #include <crypto/aead.h>
  46. #include <crypto/authenc.h>
  47. #include <crypto/skcipher.h>
  48. #include <crypto/hash.h>
  49. #include <crypto/internal/hash.h>
  50. #include <crypto/scatterwalk.h>
  51. #include "talitos.h"
  52. #define TALITOS_TIMEOUT 100000
  53. #define TALITOS_MAX_DATA_LEN 65535
  54. #define DESC_TYPE(desc_hdr) ((be32_to_cpu(desc_hdr) >> 3) & 0x1f)
  55. #define PRIMARY_EU(desc_hdr) ((be32_to_cpu(desc_hdr) >> 28) & 0xf)
  56. #define SECONDARY_EU(desc_hdr) ((be32_to_cpu(desc_hdr) >> 16) & 0xf)
  57. /* descriptor pointer entry */
  58. struct talitos_ptr {
  59. __be16 len; /* length */
  60. u8 j_extent; /* jump to sg link table and/or extent */
  61. u8 eptr; /* extended address */
  62. __be32 ptr; /* address */
  63. };
  64. static const struct talitos_ptr zero_entry = {
  65. .len = 0,
  66. .j_extent = 0,
  67. .eptr = 0,
  68. .ptr = 0
  69. };
  70. /* descriptor */
  71. struct talitos_desc {
  72. __be32 hdr; /* header high bits */
  73. __be32 hdr_lo; /* header low bits */
  74. struct talitos_ptr ptr[7]; /* ptr/len pair array */
  75. };
  76. /**
  77. * talitos_request - descriptor submission request
  78. * @desc: descriptor pointer (kernel virtual)
  79. * @dma_desc: descriptor's physical bus address
  80. * @callback: whom to call when descriptor processing is done
  81. * @context: caller context (optional)
  82. */
  83. struct talitos_request {
  84. struct talitos_desc *desc;
  85. dma_addr_t dma_desc;
  86. void (*callback) (struct device *dev, struct talitos_desc *desc,
  87. void *context, int error);
  88. void *context;
  89. };
  90. /* per-channel fifo management */
  91. struct talitos_channel {
  92. /* request fifo */
  93. struct talitos_request *fifo;
  94. /* number of requests pending in channel h/w fifo */
  95. atomic_t submit_count ____cacheline_aligned;
  96. /* request submission (head) lock */
  97. spinlock_t head_lock ____cacheline_aligned;
  98. /* index to next free descriptor request */
  99. int head;
  100. /* request release (tail) lock */
  101. spinlock_t tail_lock ____cacheline_aligned;
  102. /* index to next in-progress/done descriptor request */
  103. int tail;
  104. };
  105. struct talitos_private {
  106. struct device *dev;
  107. struct platform_device *ofdev;
  108. void __iomem *reg;
  109. int irq;
  110. /* SEC version geometry (from device tree node) */
  111. unsigned int num_channels;
  112. unsigned int chfifo_len;
  113. unsigned int exec_units;
  114. unsigned int desc_types;
  115. /* SEC Compatibility info */
  116. unsigned long features;
  117. /*
  118. * length of the request fifo
  119. * fifo_len is chfifo_len rounded up to next power of 2
  120. * so we can use bitwise ops to wrap
  121. */
  122. unsigned int fifo_len;
  123. struct talitos_channel *chan;
  124. /* next channel to be assigned next incoming descriptor */
  125. atomic_t last_chan ____cacheline_aligned;
  126. /* request callback tasklet */
  127. struct tasklet_struct done_task;
  128. /* list of registered algorithms */
  129. struct list_head alg_list;
  130. /* hwrng device */
  131. struct hwrng rng;
  132. };
  133. /* .features flag */
  134. #define TALITOS_FTR_SRC_LINK_TBL_LEN_INCLUDES_EXTENT 0x00000001
  135. #define TALITOS_FTR_HW_AUTH_CHECK 0x00000002
  136. #define TALITOS_FTR_SHA224_HWINIT 0x00000004
  137. static void to_talitos_ptr(struct talitos_ptr *talitos_ptr, dma_addr_t dma_addr)
  138. {
  139. talitos_ptr->ptr = cpu_to_be32(lower_32_bits(dma_addr));
  140. talitos_ptr->eptr = upper_32_bits(dma_addr);
  141. }
  142. /*
  143. * map virtual single (contiguous) pointer to h/w descriptor pointer
  144. */
  145. static void map_single_talitos_ptr(struct device *dev,
  146. struct talitos_ptr *talitos_ptr,
  147. unsigned short len, void *data,
  148. unsigned char extent,
  149. enum dma_data_direction dir)
  150. {
  151. dma_addr_t dma_addr = dma_map_single(dev, data, len, dir);
  152. talitos_ptr->len = cpu_to_be16(len);
  153. to_talitos_ptr(talitos_ptr, dma_addr);
  154. talitos_ptr->j_extent = extent;
  155. }
  156. /*
  157. * unmap bus single (contiguous) h/w descriptor pointer
  158. */
  159. static void unmap_single_talitos_ptr(struct device *dev,
  160. struct talitos_ptr *talitos_ptr,
  161. enum dma_data_direction dir)
  162. {
  163. dma_unmap_single(dev, be32_to_cpu(talitos_ptr->ptr),
  164. be16_to_cpu(talitos_ptr->len), dir);
  165. }
  166. static int reset_channel(struct device *dev, int ch)
  167. {
  168. struct talitos_private *priv = dev_get_drvdata(dev);
  169. unsigned int timeout = TALITOS_TIMEOUT;
  170. setbits32(priv->reg + TALITOS_CCCR(ch), TALITOS_CCCR_RESET);
  171. while ((in_be32(priv->reg + TALITOS_CCCR(ch)) & TALITOS_CCCR_RESET)
  172. && --timeout)
  173. cpu_relax();
  174. if (timeout == 0) {
  175. dev_err(dev, "failed to reset channel %d\n", ch);
  176. return -EIO;
  177. }
  178. /* set 36-bit addressing, done writeback enable and done IRQ enable */
  179. setbits32(priv->reg + TALITOS_CCCR_LO(ch), TALITOS_CCCR_LO_EAE |
  180. TALITOS_CCCR_LO_CDWE | TALITOS_CCCR_LO_CDIE);
  181. /* and ICCR writeback, if available */
  182. if (priv->features & TALITOS_FTR_HW_AUTH_CHECK)
  183. setbits32(priv->reg + TALITOS_CCCR_LO(ch),
  184. TALITOS_CCCR_LO_IWSE);
  185. return 0;
  186. }
  187. static int reset_device(struct device *dev)
  188. {
  189. struct talitos_private *priv = dev_get_drvdata(dev);
  190. unsigned int timeout = TALITOS_TIMEOUT;
  191. setbits32(priv->reg + TALITOS_MCR, TALITOS_MCR_SWR);
  192. while ((in_be32(priv->reg + TALITOS_MCR) & TALITOS_MCR_SWR)
  193. && --timeout)
  194. cpu_relax();
  195. if (timeout == 0) {
  196. dev_err(dev, "failed to reset device\n");
  197. return -EIO;
  198. }
  199. return 0;
  200. }
  201. /*
  202. * Reset and initialize the device
  203. */
  204. static int init_device(struct device *dev)
  205. {
  206. struct talitos_private *priv = dev_get_drvdata(dev);
  207. int ch, err;
  208. /*
  209. * Master reset
  210. * errata documentation: warning: certain SEC interrupts
  211. * are not fully cleared by writing the MCR:SWR bit,
  212. * set bit twice to completely reset
  213. */
  214. err = reset_device(dev);
  215. if (err)
  216. return err;
  217. err = reset_device(dev);
  218. if (err)
  219. return err;
  220. /* reset channels */
  221. for (ch = 0; ch < priv->num_channels; ch++) {
  222. err = reset_channel(dev, ch);
  223. if (err)
  224. return err;
  225. }
  226. /* enable channel done and error interrupts */
  227. setbits32(priv->reg + TALITOS_IMR, TALITOS_IMR_INIT);
  228. setbits32(priv->reg + TALITOS_IMR_LO, TALITOS_IMR_LO_INIT);
  229. /* disable integrity check error interrupts (use writeback instead) */
  230. if (priv->features & TALITOS_FTR_HW_AUTH_CHECK)
  231. setbits32(priv->reg + TALITOS_MDEUICR_LO,
  232. TALITOS_MDEUICR_LO_ICE);
  233. return 0;
  234. }
  235. /**
  236. * talitos_submit - submits a descriptor to the device for processing
  237. * @dev: the SEC device to be used
  238. * @desc: the descriptor to be processed by the device
  239. * @callback: whom to call when processing is complete
  240. * @context: a handle for use by caller (optional)
  241. *
  242. * desc must contain valid dma-mapped (bus physical) address pointers.
  243. * callback must check err and feedback in descriptor header
  244. * for device processing status.
  245. */
  246. static int talitos_submit(struct device *dev, struct talitos_desc *desc,
  247. void (*callback)(struct device *dev,
  248. struct talitos_desc *desc,
  249. void *context, int error),
  250. void *context)
  251. {
  252. struct talitos_private *priv = dev_get_drvdata(dev);
  253. struct talitos_request *request;
  254. unsigned long flags, ch;
  255. int head;
  256. /* select done notification */
  257. desc->hdr |= DESC_HDR_DONE_NOTIFY;
  258. /* emulate SEC's round-robin channel fifo polling scheme */
  259. ch = atomic_inc_return(&priv->last_chan) & (priv->num_channels - 1);
  260. spin_lock_irqsave(&priv->chan[ch].head_lock, flags);
  261. if (!atomic_inc_not_zero(&priv->chan[ch].submit_count)) {
  262. /* h/w fifo is full */
  263. spin_unlock_irqrestore(&priv->chan[ch].head_lock, flags);
  264. return -EAGAIN;
  265. }
  266. head = priv->chan[ch].head;
  267. request = &priv->chan[ch].fifo[head];
  268. /* map descriptor and save caller data */
  269. request->dma_desc = dma_map_single(dev, desc, sizeof(*desc),
  270. DMA_BIDIRECTIONAL);
  271. request->callback = callback;
  272. request->context = context;
  273. /* increment fifo head */
  274. priv->chan[ch].head = (priv->chan[ch].head + 1) & (priv->fifo_len - 1);
  275. smp_wmb();
  276. request->desc = desc;
  277. /* GO! */
  278. wmb();
  279. out_be32(priv->reg + TALITOS_FF(ch), upper_32_bits(request->dma_desc));
  280. out_be32(priv->reg + TALITOS_FF_LO(ch),
  281. lower_32_bits(request->dma_desc));
  282. spin_unlock_irqrestore(&priv->chan[ch].head_lock, flags);
  283. return -EINPROGRESS;
  284. }
  285. /*
  286. * process what was done, notify callback of error if not
  287. */
  288. static void flush_channel(struct device *dev, int ch, int error, int reset_ch)
  289. {
  290. struct talitos_private *priv = dev_get_drvdata(dev);
  291. struct talitos_request *request, saved_req;
  292. unsigned long flags;
  293. int tail, status;
  294. spin_lock_irqsave(&priv->chan[ch].tail_lock, flags);
  295. tail = priv->chan[ch].tail;
  296. while (priv->chan[ch].fifo[tail].desc) {
  297. request = &priv->chan[ch].fifo[tail];
  298. /* descriptors with their done bits set don't get the error */
  299. rmb();
  300. if ((request->desc->hdr & DESC_HDR_DONE) == DESC_HDR_DONE)
  301. status = 0;
  302. else
  303. if (!error)
  304. break;
  305. else
  306. status = error;
  307. dma_unmap_single(dev, request->dma_desc,
  308. sizeof(struct talitos_desc),
  309. DMA_BIDIRECTIONAL);
  310. /* copy entries so we can call callback outside lock */
  311. saved_req.desc = request->desc;
  312. saved_req.callback = request->callback;
  313. saved_req.context = request->context;
  314. /* release request entry in fifo */
  315. smp_wmb();
  316. request->desc = NULL;
  317. /* increment fifo tail */
  318. priv->chan[ch].tail = (tail + 1) & (priv->fifo_len - 1);
  319. spin_unlock_irqrestore(&priv->chan[ch].tail_lock, flags);
  320. atomic_dec(&priv->chan[ch].submit_count);
  321. saved_req.callback(dev, saved_req.desc, saved_req.context,
  322. status);
  323. /* channel may resume processing in single desc error case */
  324. if (error && !reset_ch && status == error)
  325. return;
  326. spin_lock_irqsave(&priv->chan[ch].tail_lock, flags);
  327. tail = priv->chan[ch].tail;
  328. }
  329. spin_unlock_irqrestore(&priv->chan[ch].tail_lock, flags);
  330. }
  331. /*
  332. * process completed requests for channels that have done status
  333. */
  334. static void talitos_done(unsigned long data)
  335. {
  336. struct device *dev = (struct device *)data;
  337. struct talitos_private *priv = dev_get_drvdata(dev);
  338. int ch;
  339. for (ch = 0; ch < priv->num_channels; ch++)
  340. flush_channel(dev, ch, 0, 0);
  341. /* At this point, all completed channels have been processed.
  342. * Unmask done interrupts for channels completed later on.
  343. */
  344. setbits32(priv->reg + TALITOS_IMR, TALITOS_IMR_INIT);
  345. setbits32(priv->reg + TALITOS_IMR_LO, TALITOS_IMR_LO_INIT);
  346. }
  347. /*
  348. * locate current (offending) descriptor
  349. */
  350. static struct talitos_desc *current_desc(struct device *dev, int ch)
  351. {
  352. struct talitos_private *priv = dev_get_drvdata(dev);
  353. int tail = priv->chan[ch].tail;
  354. dma_addr_t cur_desc;
  355. cur_desc = in_be32(priv->reg + TALITOS_CDPR_LO(ch));
  356. while (priv->chan[ch].fifo[tail].dma_desc != cur_desc) {
  357. tail = (tail + 1) & (priv->fifo_len - 1);
  358. if (tail == priv->chan[ch].tail) {
  359. dev_err(dev, "couldn't locate current descriptor\n");
  360. return NULL;
  361. }
  362. }
  363. return priv->chan[ch].fifo[tail].desc;
  364. }
  365. /*
  366. * user diagnostics; report root cause of error based on execution unit status
  367. */
  368. static void report_eu_error(struct device *dev, int ch,
  369. struct talitos_desc *desc)
  370. {
  371. struct talitos_private *priv = dev_get_drvdata(dev);
  372. int i;
  373. switch (desc->hdr & DESC_HDR_SEL0_MASK) {
  374. case DESC_HDR_SEL0_AFEU:
  375. dev_err(dev, "AFEUISR 0x%08x_%08x\n",
  376. in_be32(priv->reg + TALITOS_AFEUISR),
  377. in_be32(priv->reg + TALITOS_AFEUISR_LO));
  378. break;
  379. case DESC_HDR_SEL0_DEU:
  380. dev_err(dev, "DEUISR 0x%08x_%08x\n",
  381. in_be32(priv->reg + TALITOS_DEUISR),
  382. in_be32(priv->reg + TALITOS_DEUISR_LO));
  383. break;
  384. case DESC_HDR_SEL0_MDEUA:
  385. case DESC_HDR_SEL0_MDEUB:
  386. dev_err(dev, "MDEUISR 0x%08x_%08x\n",
  387. in_be32(priv->reg + TALITOS_MDEUISR),
  388. in_be32(priv->reg + TALITOS_MDEUISR_LO));
  389. break;
  390. case DESC_HDR_SEL0_RNG:
  391. dev_err(dev, "RNGUISR 0x%08x_%08x\n",
  392. in_be32(priv->reg + TALITOS_RNGUISR),
  393. in_be32(priv->reg + TALITOS_RNGUISR_LO));
  394. break;
  395. case DESC_HDR_SEL0_PKEU:
  396. dev_err(dev, "PKEUISR 0x%08x_%08x\n",
  397. in_be32(priv->reg + TALITOS_PKEUISR),
  398. in_be32(priv->reg + TALITOS_PKEUISR_LO));
  399. break;
  400. case DESC_HDR_SEL0_AESU:
  401. dev_err(dev, "AESUISR 0x%08x_%08x\n",
  402. in_be32(priv->reg + TALITOS_AESUISR),
  403. in_be32(priv->reg + TALITOS_AESUISR_LO));
  404. break;
  405. case DESC_HDR_SEL0_CRCU:
  406. dev_err(dev, "CRCUISR 0x%08x_%08x\n",
  407. in_be32(priv->reg + TALITOS_CRCUISR),
  408. in_be32(priv->reg + TALITOS_CRCUISR_LO));
  409. break;
  410. case DESC_HDR_SEL0_KEU:
  411. dev_err(dev, "KEUISR 0x%08x_%08x\n",
  412. in_be32(priv->reg + TALITOS_KEUISR),
  413. in_be32(priv->reg + TALITOS_KEUISR_LO));
  414. break;
  415. }
  416. switch (desc->hdr & DESC_HDR_SEL1_MASK) {
  417. case DESC_HDR_SEL1_MDEUA:
  418. case DESC_HDR_SEL1_MDEUB:
  419. dev_err(dev, "MDEUISR 0x%08x_%08x\n",
  420. in_be32(priv->reg + TALITOS_MDEUISR),
  421. in_be32(priv->reg + TALITOS_MDEUISR_LO));
  422. break;
  423. case DESC_HDR_SEL1_CRCU:
  424. dev_err(dev, "CRCUISR 0x%08x_%08x\n",
  425. in_be32(priv->reg + TALITOS_CRCUISR),
  426. in_be32(priv->reg + TALITOS_CRCUISR_LO));
  427. break;
  428. }
  429. for (i = 0; i < 8; i++)
  430. dev_err(dev, "DESCBUF 0x%08x_%08x\n",
  431. in_be32(priv->reg + TALITOS_DESCBUF(ch) + 8*i),
  432. in_be32(priv->reg + TALITOS_DESCBUF_LO(ch) + 8*i));
  433. }
  434. /*
  435. * recover from error interrupts
  436. */
  437. static void talitos_error(unsigned long data, u32 isr, u32 isr_lo)
  438. {
  439. struct device *dev = (struct device *)data;
  440. struct talitos_private *priv = dev_get_drvdata(dev);
  441. unsigned int timeout = TALITOS_TIMEOUT;
  442. int ch, error, reset_dev = 0, reset_ch = 0;
  443. u32 v, v_lo;
  444. for (ch = 0; ch < priv->num_channels; ch++) {
  445. /* skip channels without errors */
  446. if (!(isr & (1 << (ch * 2 + 1))))
  447. continue;
  448. error = -EINVAL;
  449. v = in_be32(priv->reg + TALITOS_CCPSR(ch));
  450. v_lo = in_be32(priv->reg + TALITOS_CCPSR_LO(ch));
  451. if (v_lo & TALITOS_CCPSR_LO_DOF) {
  452. dev_err(dev, "double fetch fifo overflow error\n");
  453. error = -EAGAIN;
  454. reset_ch = 1;
  455. }
  456. if (v_lo & TALITOS_CCPSR_LO_SOF) {
  457. /* h/w dropped descriptor */
  458. dev_err(dev, "single fetch fifo overflow error\n");
  459. error = -EAGAIN;
  460. }
  461. if (v_lo & TALITOS_CCPSR_LO_MDTE)
  462. dev_err(dev, "master data transfer error\n");
  463. if (v_lo & TALITOS_CCPSR_LO_SGDLZ)
  464. dev_err(dev, "s/g data length zero error\n");
  465. if (v_lo & TALITOS_CCPSR_LO_FPZ)
  466. dev_err(dev, "fetch pointer zero error\n");
  467. if (v_lo & TALITOS_CCPSR_LO_IDH)
  468. dev_err(dev, "illegal descriptor header error\n");
  469. if (v_lo & TALITOS_CCPSR_LO_IEU)
  470. dev_err(dev, "invalid execution unit error\n");
  471. if (v_lo & TALITOS_CCPSR_LO_EU)
  472. report_eu_error(dev, ch, current_desc(dev, ch));
  473. if (v_lo & TALITOS_CCPSR_LO_GB)
  474. dev_err(dev, "gather boundary error\n");
  475. if (v_lo & TALITOS_CCPSR_LO_GRL)
  476. dev_err(dev, "gather return/length error\n");
  477. if (v_lo & TALITOS_CCPSR_LO_SB)
  478. dev_err(dev, "scatter boundary error\n");
  479. if (v_lo & TALITOS_CCPSR_LO_SRL)
  480. dev_err(dev, "scatter return/length error\n");
  481. flush_channel(dev, ch, error, reset_ch);
  482. if (reset_ch) {
  483. reset_channel(dev, ch);
  484. } else {
  485. setbits32(priv->reg + TALITOS_CCCR(ch),
  486. TALITOS_CCCR_CONT);
  487. setbits32(priv->reg + TALITOS_CCCR_LO(ch), 0);
  488. while ((in_be32(priv->reg + TALITOS_CCCR(ch)) &
  489. TALITOS_CCCR_CONT) && --timeout)
  490. cpu_relax();
  491. if (timeout == 0) {
  492. dev_err(dev, "failed to restart channel %d\n",
  493. ch);
  494. reset_dev = 1;
  495. }
  496. }
  497. }
  498. if (reset_dev || isr & ~TALITOS_ISR_CHERR || isr_lo) {
  499. dev_err(dev, "done overflow, internal time out, or rngu error: "
  500. "ISR 0x%08x_%08x\n", isr, isr_lo);
  501. /* purge request queues */
  502. for (ch = 0; ch < priv->num_channels; ch++)
  503. flush_channel(dev, ch, -EIO, 1);
  504. /* reset and reinitialize the device */
  505. init_device(dev);
  506. }
  507. }
  508. static irqreturn_t talitos_interrupt(int irq, void *data)
  509. {
  510. struct device *dev = data;
  511. struct talitos_private *priv = dev_get_drvdata(dev);
  512. u32 isr, isr_lo;
  513. isr = in_be32(priv->reg + TALITOS_ISR);
  514. isr_lo = in_be32(priv->reg + TALITOS_ISR_LO);
  515. /* Acknowledge interrupt */
  516. out_be32(priv->reg + TALITOS_ICR, isr);
  517. out_be32(priv->reg + TALITOS_ICR_LO, isr_lo);
  518. if (unlikely((isr & ~TALITOS_ISR_CHDONE) || isr_lo))
  519. talitos_error((unsigned long)data, isr, isr_lo);
  520. else
  521. if (likely(isr & TALITOS_ISR_CHDONE)) {
  522. /* mask further done interrupts. */
  523. clrbits32(priv->reg + TALITOS_IMR, TALITOS_IMR_DONE);
  524. /* done_task will unmask done interrupts at exit */
  525. tasklet_schedule(&priv->done_task);
  526. }
  527. return (isr || isr_lo) ? IRQ_HANDLED : IRQ_NONE;
  528. }
  529. /*
  530. * hwrng
  531. */
  532. static int talitos_rng_data_present(struct hwrng *rng, int wait)
  533. {
  534. struct device *dev = (struct device *)rng->priv;
  535. struct talitos_private *priv = dev_get_drvdata(dev);
  536. u32 ofl;
  537. int i;
  538. for (i = 0; i < 20; i++) {
  539. ofl = in_be32(priv->reg + TALITOS_RNGUSR_LO) &
  540. TALITOS_RNGUSR_LO_OFL;
  541. if (ofl || !wait)
  542. break;
  543. udelay(10);
  544. }
  545. return !!ofl;
  546. }
  547. static int talitos_rng_data_read(struct hwrng *rng, u32 *data)
  548. {
  549. struct device *dev = (struct device *)rng->priv;
  550. struct talitos_private *priv = dev_get_drvdata(dev);
  551. /* rng fifo requires 64-bit accesses */
  552. *data = in_be32(priv->reg + TALITOS_RNGU_FIFO);
  553. *data = in_be32(priv->reg + TALITOS_RNGU_FIFO_LO);
  554. return sizeof(u32);
  555. }
  556. static int talitos_rng_init(struct hwrng *rng)
  557. {
  558. struct device *dev = (struct device *)rng->priv;
  559. struct talitos_private *priv = dev_get_drvdata(dev);
  560. unsigned int timeout = TALITOS_TIMEOUT;
  561. setbits32(priv->reg + TALITOS_RNGURCR_LO, TALITOS_RNGURCR_LO_SR);
  562. while (!(in_be32(priv->reg + TALITOS_RNGUSR_LO) & TALITOS_RNGUSR_LO_RD)
  563. && --timeout)
  564. cpu_relax();
  565. if (timeout == 0) {
  566. dev_err(dev, "failed to reset rng hw\n");
  567. return -ENODEV;
  568. }
  569. /* start generating */
  570. setbits32(priv->reg + TALITOS_RNGUDSR_LO, 0);
  571. return 0;
  572. }
  573. static int talitos_register_rng(struct device *dev)
  574. {
  575. struct talitos_private *priv = dev_get_drvdata(dev);
  576. priv->rng.name = dev_driver_string(dev),
  577. priv->rng.init = talitos_rng_init,
  578. priv->rng.data_present = talitos_rng_data_present,
  579. priv->rng.data_read = talitos_rng_data_read,
  580. priv->rng.priv = (unsigned long)dev;
  581. return hwrng_register(&priv->rng);
  582. }
  583. static void talitos_unregister_rng(struct device *dev)
  584. {
  585. struct talitos_private *priv = dev_get_drvdata(dev);
  586. hwrng_unregister(&priv->rng);
  587. }
  588. /*
  589. * crypto alg
  590. */
  591. #define TALITOS_CRA_PRIORITY 3000
  592. #define TALITOS_MAX_KEY_SIZE 64
  593. #define TALITOS_MAX_IV_LENGTH 16 /* max of AES_BLOCK_SIZE, DES3_EDE_BLOCK_SIZE */
  594. #define MD5_BLOCK_SIZE 64
  595. struct talitos_ctx {
  596. struct device *dev;
  597. __be32 desc_hdr_template;
  598. u8 key[TALITOS_MAX_KEY_SIZE];
  599. u8 iv[TALITOS_MAX_IV_LENGTH];
  600. unsigned int keylen;
  601. unsigned int enckeylen;
  602. unsigned int authkeylen;
  603. unsigned int authsize;
  604. };
  605. #define HASH_MAX_BLOCK_SIZE SHA512_BLOCK_SIZE
  606. #define TALITOS_MDEU_MAX_CONTEXT_SIZE TALITOS_MDEU_CONTEXT_SIZE_SHA384_SHA512
  607. struct talitos_ahash_req_ctx {
  608. u32 hw_context[TALITOS_MDEU_MAX_CONTEXT_SIZE / sizeof(u32)];
  609. unsigned int hw_context_size;
  610. u8 buf[HASH_MAX_BLOCK_SIZE];
  611. u8 bufnext[HASH_MAX_BLOCK_SIZE];
  612. unsigned int swinit;
  613. unsigned int first;
  614. unsigned int last;
  615. unsigned int to_hash_later;
  616. u64 nbuf;
  617. struct scatterlist bufsl[2];
  618. struct scatterlist *psrc;
  619. };
  620. static int aead_setauthsize(struct crypto_aead *authenc,
  621. unsigned int authsize)
  622. {
  623. struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
  624. ctx->authsize = authsize;
  625. return 0;
  626. }
  627. static int aead_setkey(struct crypto_aead *authenc,
  628. const u8 *key, unsigned int keylen)
  629. {
  630. struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
  631. struct rtattr *rta = (void *)key;
  632. struct crypto_authenc_key_param *param;
  633. unsigned int authkeylen;
  634. unsigned int enckeylen;
  635. if (!RTA_OK(rta, keylen))
  636. goto badkey;
  637. if (rta->rta_type != CRYPTO_AUTHENC_KEYA_PARAM)
  638. goto badkey;
  639. if (RTA_PAYLOAD(rta) < sizeof(*param))
  640. goto badkey;
  641. param = RTA_DATA(rta);
  642. enckeylen = be32_to_cpu(param->enckeylen);
  643. key += RTA_ALIGN(rta->rta_len);
  644. keylen -= RTA_ALIGN(rta->rta_len);
  645. if (keylen < enckeylen)
  646. goto badkey;
  647. authkeylen = keylen - enckeylen;
  648. if (keylen > TALITOS_MAX_KEY_SIZE)
  649. goto badkey;
  650. memcpy(&ctx->key, key, keylen);
  651. ctx->keylen = keylen;
  652. ctx->enckeylen = enckeylen;
  653. ctx->authkeylen = authkeylen;
  654. return 0;
  655. badkey:
  656. crypto_aead_set_flags(authenc, CRYPTO_TFM_RES_BAD_KEY_LEN);
  657. return -EINVAL;
  658. }
  659. /*
  660. * talitos_edesc - s/w-extended descriptor
  661. * @src_nents: number of segments in input scatterlist
  662. * @dst_nents: number of segments in output scatterlist
  663. * @dma_len: length of dma mapped link_tbl space
  664. * @dma_link_tbl: bus physical address of link_tbl
  665. * @desc: h/w descriptor
  666. * @link_tbl: input and output h/w link tables (if {src,dst}_nents > 1)
  667. *
  668. * if decrypting (with authcheck), or either one of src_nents or dst_nents
  669. * is greater than 1, an integrity check value is concatenated to the end
  670. * of link_tbl data
  671. */
  672. struct talitos_edesc {
  673. int src_nents;
  674. int dst_nents;
  675. int src_is_chained;
  676. int dst_is_chained;
  677. int dma_len;
  678. dma_addr_t dma_link_tbl;
  679. struct talitos_desc desc;
  680. struct talitos_ptr link_tbl[0];
  681. };
  682. static int talitos_map_sg(struct device *dev, struct scatterlist *sg,
  683. unsigned int nents, enum dma_data_direction dir,
  684. int chained)
  685. {
  686. if (unlikely(chained))
  687. while (sg) {
  688. dma_map_sg(dev, sg, 1, dir);
  689. sg = scatterwalk_sg_next(sg);
  690. }
  691. else
  692. dma_map_sg(dev, sg, nents, dir);
  693. return nents;
  694. }
  695. static void talitos_unmap_sg_chain(struct device *dev, struct scatterlist *sg,
  696. enum dma_data_direction dir)
  697. {
  698. while (sg) {
  699. dma_unmap_sg(dev, sg, 1, dir);
  700. sg = scatterwalk_sg_next(sg);
  701. }
  702. }
  703. static void talitos_sg_unmap(struct device *dev,
  704. struct talitos_edesc *edesc,
  705. struct scatterlist *src,
  706. struct scatterlist *dst)
  707. {
  708. unsigned int src_nents = edesc->src_nents ? : 1;
  709. unsigned int dst_nents = edesc->dst_nents ? : 1;
  710. if (src != dst) {
  711. if (edesc->src_is_chained)
  712. talitos_unmap_sg_chain(dev, src, DMA_TO_DEVICE);
  713. else
  714. dma_unmap_sg(dev, src, src_nents, DMA_TO_DEVICE);
  715. if (dst) {
  716. if (edesc->dst_is_chained)
  717. talitos_unmap_sg_chain(dev, dst,
  718. DMA_FROM_DEVICE);
  719. else
  720. dma_unmap_sg(dev, dst, dst_nents,
  721. DMA_FROM_DEVICE);
  722. }
  723. } else
  724. if (edesc->src_is_chained)
  725. talitos_unmap_sg_chain(dev, src, DMA_BIDIRECTIONAL);
  726. else
  727. dma_unmap_sg(dev, src, src_nents, DMA_BIDIRECTIONAL);
  728. }
  729. static void ipsec_esp_unmap(struct device *dev,
  730. struct talitos_edesc *edesc,
  731. struct aead_request *areq)
  732. {
  733. unmap_single_talitos_ptr(dev, &edesc->desc.ptr[6], DMA_FROM_DEVICE);
  734. unmap_single_talitos_ptr(dev, &edesc->desc.ptr[3], DMA_TO_DEVICE);
  735. unmap_single_talitos_ptr(dev, &edesc->desc.ptr[2], DMA_TO_DEVICE);
  736. unmap_single_talitos_ptr(dev, &edesc->desc.ptr[0], DMA_TO_DEVICE);
  737. dma_unmap_sg(dev, areq->assoc, 1, DMA_TO_DEVICE);
  738. talitos_sg_unmap(dev, edesc, areq->src, areq->dst);
  739. if (edesc->dma_len)
  740. dma_unmap_single(dev, edesc->dma_link_tbl, edesc->dma_len,
  741. DMA_BIDIRECTIONAL);
  742. }
  743. /*
  744. * ipsec_esp descriptor callbacks
  745. */
  746. static void ipsec_esp_encrypt_done(struct device *dev,
  747. struct talitos_desc *desc, void *context,
  748. int err)
  749. {
  750. struct aead_request *areq = context;
  751. struct crypto_aead *authenc = crypto_aead_reqtfm(areq);
  752. struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
  753. struct talitos_edesc *edesc;
  754. struct scatterlist *sg;
  755. void *icvdata;
  756. edesc = container_of(desc, struct talitos_edesc, desc);
  757. ipsec_esp_unmap(dev, edesc, areq);
  758. /* copy the generated ICV to dst */
  759. if (edesc->dma_len) {
  760. icvdata = &edesc->link_tbl[edesc->src_nents +
  761. edesc->dst_nents + 2];
  762. sg = sg_last(areq->dst, edesc->dst_nents);
  763. memcpy((char *)sg_virt(sg) + sg->length - ctx->authsize,
  764. icvdata, ctx->authsize);
  765. }
  766. kfree(edesc);
  767. aead_request_complete(areq, err);
  768. }
  769. static void ipsec_esp_decrypt_swauth_done(struct device *dev,
  770. struct talitos_desc *desc,
  771. void *context, int err)
  772. {
  773. struct aead_request *req = context;
  774. struct crypto_aead *authenc = crypto_aead_reqtfm(req);
  775. struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
  776. struct talitos_edesc *edesc;
  777. struct scatterlist *sg;
  778. void *icvdata;
  779. edesc = container_of(desc, struct talitos_edesc, desc);
  780. ipsec_esp_unmap(dev, edesc, req);
  781. if (!err) {
  782. /* auth check */
  783. if (edesc->dma_len)
  784. icvdata = &edesc->link_tbl[edesc->src_nents +
  785. edesc->dst_nents + 2];
  786. else
  787. icvdata = &edesc->link_tbl[0];
  788. sg = sg_last(req->dst, edesc->dst_nents ? : 1);
  789. err = memcmp(icvdata, (char *)sg_virt(sg) + sg->length -
  790. ctx->authsize, ctx->authsize) ? -EBADMSG : 0;
  791. }
  792. kfree(edesc);
  793. aead_request_complete(req, err);
  794. }
  795. static void ipsec_esp_decrypt_hwauth_done(struct device *dev,
  796. struct talitos_desc *desc,
  797. void *context, int err)
  798. {
  799. struct aead_request *req = context;
  800. struct talitos_edesc *edesc;
  801. edesc = container_of(desc, struct talitos_edesc, desc);
  802. ipsec_esp_unmap(dev, edesc, req);
  803. /* check ICV auth status */
  804. if (!err && ((desc->hdr_lo & DESC_HDR_LO_ICCR1_MASK) !=
  805. DESC_HDR_LO_ICCR1_PASS))
  806. err = -EBADMSG;
  807. kfree(edesc);
  808. aead_request_complete(req, err);
  809. }
  810. /*
  811. * convert scatterlist to SEC h/w link table format
  812. * stop at cryptlen bytes
  813. */
  814. static int sg_to_link_tbl(struct scatterlist *sg, int sg_count,
  815. int cryptlen, struct talitos_ptr *link_tbl_ptr)
  816. {
  817. int n_sg = sg_count;
  818. while (n_sg--) {
  819. to_talitos_ptr(link_tbl_ptr, sg_dma_address(sg));
  820. link_tbl_ptr->len = cpu_to_be16(sg_dma_len(sg));
  821. link_tbl_ptr->j_extent = 0;
  822. link_tbl_ptr++;
  823. cryptlen -= sg_dma_len(sg);
  824. sg = scatterwalk_sg_next(sg);
  825. }
  826. /* adjust (decrease) last one (or two) entry's len to cryptlen */
  827. link_tbl_ptr--;
  828. while (be16_to_cpu(link_tbl_ptr->len) <= (-cryptlen)) {
  829. /* Empty this entry, and move to previous one */
  830. cryptlen += be16_to_cpu(link_tbl_ptr->len);
  831. link_tbl_ptr->len = 0;
  832. sg_count--;
  833. link_tbl_ptr--;
  834. }
  835. link_tbl_ptr->len = cpu_to_be16(be16_to_cpu(link_tbl_ptr->len)
  836. + cryptlen);
  837. /* tag end of link table */
  838. link_tbl_ptr->j_extent = DESC_PTR_LNKTBL_RETURN;
  839. return sg_count;
  840. }
  841. /*
  842. * fill in and submit ipsec_esp descriptor
  843. */
  844. static int ipsec_esp(struct talitos_edesc *edesc, struct aead_request *areq,
  845. u8 *giv, u64 seq,
  846. void (*callback) (struct device *dev,
  847. struct talitos_desc *desc,
  848. void *context, int error))
  849. {
  850. struct crypto_aead *aead = crypto_aead_reqtfm(areq);
  851. struct talitos_ctx *ctx = crypto_aead_ctx(aead);
  852. struct device *dev = ctx->dev;
  853. struct talitos_desc *desc = &edesc->desc;
  854. unsigned int cryptlen = areq->cryptlen;
  855. unsigned int authsize = ctx->authsize;
  856. unsigned int ivsize = crypto_aead_ivsize(aead);
  857. int sg_count, ret;
  858. int sg_link_tbl_len;
  859. /* hmac key */
  860. map_single_talitos_ptr(dev, &desc->ptr[0], ctx->authkeylen, &ctx->key,
  861. 0, DMA_TO_DEVICE);
  862. /* hmac data */
  863. map_single_talitos_ptr(dev, &desc->ptr[1], areq->assoclen + ivsize,
  864. sg_virt(areq->assoc), 0, DMA_TO_DEVICE);
  865. /* cipher iv */
  866. map_single_talitos_ptr(dev, &desc->ptr[2], ivsize, giv ?: areq->iv, 0,
  867. DMA_TO_DEVICE);
  868. /* cipher key */
  869. map_single_talitos_ptr(dev, &desc->ptr[3], ctx->enckeylen,
  870. (char *)&ctx->key + ctx->authkeylen, 0,
  871. DMA_TO_DEVICE);
  872. /*
  873. * cipher in
  874. * map and adjust cipher len to aead request cryptlen.
  875. * extent is bytes of HMAC postpended to ciphertext,
  876. * typically 12 for ipsec
  877. */
  878. desc->ptr[4].len = cpu_to_be16(cryptlen);
  879. desc->ptr[4].j_extent = authsize;
  880. sg_count = talitos_map_sg(dev, areq->src, edesc->src_nents ? : 1,
  881. (areq->src == areq->dst) ? DMA_BIDIRECTIONAL
  882. : DMA_TO_DEVICE,
  883. edesc->src_is_chained);
  884. if (sg_count == 1) {
  885. to_talitos_ptr(&desc->ptr[4], sg_dma_address(areq->src));
  886. } else {
  887. sg_link_tbl_len = cryptlen;
  888. if (edesc->desc.hdr & DESC_HDR_MODE1_MDEU_CICV)
  889. sg_link_tbl_len = cryptlen + authsize;
  890. sg_count = sg_to_link_tbl(areq->src, sg_count, sg_link_tbl_len,
  891. &edesc->link_tbl[0]);
  892. if (sg_count > 1) {
  893. desc->ptr[4].j_extent |= DESC_PTR_LNKTBL_JUMP;
  894. to_talitos_ptr(&desc->ptr[4], edesc->dma_link_tbl);
  895. dma_sync_single_for_device(dev, edesc->dma_link_tbl,
  896. edesc->dma_len,
  897. DMA_BIDIRECTIONAL);
  898. } else {
  899. /* Only one segment now, so no link tbl needed */
  900. to_talitos_ptr(&desc->ptr[4],
  901. sg_dma_address(areq->src));
  902. }
  903. }
  904. /* cipher out */
  905. desc->ptr[5].len = cpu_to_be16(cryptlen);
  906. desc->ptr[5].j_extent = authsize;
  907. if (areq->src != areq->dst)
  908. sg_count = talitos_map_sg(dev, areq->dst,
  909. edesc->dst_nents ? : 1,
  910. DMA_FROM_DEVICE,
  911. edesc->dst_is_chained);
  912. if (sg_count == 1) {
  913. to_talitos_ptr(&desc->ptr[5], sg_dma_address(areq->dst));
  914. } else {
  915. struct talitos_ptr *link_tbl_ptr =
  916. &edesc->link_tbl[edesc->src_nents + 1];
  917. to_talitos_ptr(&desc->ptr[5], edesc->dma_link_tbl +
  918. (edesc->src_nents + 1) *
  919. sizeof(struct talitos_ptr));
  920. sg_count = sg_to_link_tbl(areq->dst, sg_count, cryptlen,
  921. link_tbl_ptr);
  922. /* Add an entry to the link table for ICV data */
  923. link_tbl_ptr += sg_count - 1;
  924. link_tbl_ptr->j_extent = 0;
  925. sg_count++;
  926. link_tbl_ptr++;
  927. link_tbl_ptr->j_extent = DESC_PTR_LNKTBL_RETURN;
  928. link_tbl_ptr->len = cpu_to_be16(authsize);
  929. /* icv data follows link tables */
  930. to_talitos_ptr(link_tbl_ptr, edesc->dma_link_tbl +
  931. (edesc->src_nents + edesc->dst_nents + 2) *
  932. sizeof(struct talitos_ptr));
  933. desc->ptr[5].j_extent |= DESC_PTR_LNKTBL_JUMP;
  934. dma_sync_single_for_device(ctx->dev, edesc->dma_link_tbl,
  935. edesc->dma_len, DMA_BIDIRECTIONAL);
  936. }
  937. /* iv out */
  938. map_single_talitos_ptr(dev, &desc->ptr[6], ivsize, ctx->iv, 0,
  939. DMA_FROM_DEVICE);
  940. ret = talitos_submit(dev, desc, callback, areq);
  941. if (ret != -EINPROGRESS) {
  942. ipsec_esp_unmap(dev, edesc, areq);
  943. kfree(edesc);
  944. }
  945. return ret;
  946. }
  947. /*
  948. * derive number of elements in scatterlist
  949. */
  950. static int sg_count(struct scatterlist *sg_list, int nbytes, int *chained)
  951. {
  952. struct scatterlist *sg = sg_list;
  953. int sg_nents = 0;
  954. *chained = 0;
  955. while (nbytes > 0) {
  956. sg_nents++;
  957. nbytes -= sg->length;
  958. if (!sg_is_last(sg) && (sg + 1)->length == 0)
  959. *chained = 1;
  960. sg = scatterwalk_sg_next(sg);
  961. }
  962. return sg_nents;
  963. }
  964. /**
  965. * sg_copy_end_to_buffer - Copy end data from SG list to a linear buffer
  966. * @sgl: The SG list
  967. * @nents: Number of SG entries
  968. * @buf: Where to copy to
  969. * @buflen: The number of bytes to copy
  970. * @skip: The number of bytes to skip before copying.
  971. * Note: skip + buflen should equal SG total size.
  972. *
  973. * Returns the number of copied bytes.
  974. *
  975. **/
  976. static size_t sg_copy_end_to_buffer(struct scatterlist *sgl, unsigned int nents,
  977. void *buf, size_t buflen, unsigned int skip)
  978. {
  979. unsigned int offset = 0;
  980. unsigned int boffset = 0;
  981. struct sg_mapping_iter miter;
  982. unsigned long flags;
  983. unsigned int sg_flags = SG_MITER_ATOMIC;
  984. size_t total_buffer = buflen + skip;
  985. sg_flags |= SG_MITER_FROM_SG;
  986. sg_miter_start(&miter, sgl, nents, sg_flags);
  987. local_irq_save(flags);
  988. while (sg_miter_next(&miter) && offset < total_buffer) {
  989. unsigned int len;
  990. unsigned int ignore;
  991. if ((offset + miter.length) > skip) {
  992. if (offset < skip) {
  993. /* Copy part of this segment */
  994. ignore = skip - offset;
  995. len = miter.length - ignore;
  996. if (boffset + len > buflen)
  997. len = buflen - boffset;
  998. memcpy(buf + boffset, miter.addr + ignore, len);
  999. } else {
  1000. /* Copy all of this segment (up to buflen) */
  1001. len = miter.length;
  1002. if (boffset + len > buflen)
  1003. len = buflen - boffset;
  1004. memcpy(buf + boffset, miter.addr, len);
  1005. }
  1006. boffset += len;
  1007. }
  1008. offset += miter.length;
  1009. }
  1010. sg_miter_stop(&miter);
  1011. local_irq_restore(flags);
  1012. return boffset;
  1013. }
  1014. /*
  1015. * allocate and map the extended descriptor
  1016. */
  1017. static struct talitos_edesc *talitos_edesc_alloc(struct device *dev,
  1018. struct scatterlist *src,
  1019. struct scatterlist *dst,
  1020. int hash_result,
  1021. unsigned int cryptlen,
  1022. unsigned int authsize,
  1023. int icv_stashing,
  1024. u32 cryptoflags)
  1025. {
  1026. struct talitos_edesc *edesc;
  1027. int src_nents, dst_nents, alloc_len, dma_len;
  1028. int src_chained, dst_chained = 0;
  1029. gfp_t flags = cryptoflags & CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL :
  1030. GFP_ATOMIC;
  1031. if (cryptlen + authsize > TALITOS_MAX_DATA_LEN) {
  1032. dev_err(dev, "length exceeds h/w max limit\n");
  1033. return ERR_PTR(-EINVAL);
  1034. }
  1035. src_nents = sg_count(src, cryptlen + authsize, &src_chained);
  1036. src_nents = (src_nents == 1) ? 0 : src_nents;
  1037. if (hash_result) {
  1038. dst_nents = 0;
  1039. } else {
  1040. if (dst == src) {
  1041. dst_nents = src_nents;
  1042. } else {
  1043. dst_nents = sg_count(dst, cryptlen + authsize,
  1044. &dst_chained);
  1045. dst_nents = (dst_nents == 1) ? 0 : dst_nents;
  1046. }
  1047. }
  1048. /*
  1049. * allocate space for base edesc plus the link tables,
  1050. * allowing for two separate entries for ICV and generated ICV (+ 2),
  1051. * and the ICV data itself
  1052. */
  1053. alloc_len = sizeof(struct talitos_edesc);
  1054. if (src_nents || dst_nents) {
  1055. dma_len = (src_nents + dst_nents + 2) *
  1056. sizeof(struct talitos_ptr) + authsize;
  1057. alloc_len += dma_len;
  1058. } else {
  1059. dma_len = 0;
  1060. alloc_len += icv_stashing ? authsize : 0;
  1061. }
  1062. edesc = kmalloc(alloc_len, GFP_DMA | flags);
  1063. if (!edesc) {
  1064. dev_err(dev, "could not allocate edescriptor\n");
  1065. return ERR_PTR(-ENOMEM);
  1066. }
  1067. edesc->src_nents = src_nents;
  1068. edesc->dst_nents = dst_nents;
  1069. edesc->src_is_chained = src_chained;
  1070. edesc->dst_is_chained = dst_chained;
  1071. edesc->dma_len = dma_len;
  1072. if (dma_len)
  1073. edesc->dma_link_tbl = dma_map_single(dev, &edesc->link_tbl[0],
  1074. edesc->dma_len,
  1075. DMA_BIDIRECTIONAL);
  1076. return edesc;
  1077. }
  1078. static struct talitos_edesc *aead_edesc_alloc(struct aead_request *areq,
  1079. int icv_stashing)
  1080. {
  1081. struct crypto_aead *authenc = crypto_aead_reqtfm(areq);
  1082. struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
  1083. return talitos_edesc_alloc(ctx->dev, areq->src, areq->dst, 0,
  1084. areq->cryptlen, ctx->authsize, icv_stashing,
  1085. areq->base.flags);
  1086. }
  1087. static int aead_encrypt(struct aead_request *req)
  1088. {
  1089. struct crypto_aead *authenc = crypto_aead_reqtfm(req);
  1090. struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
  1091. struct talitos_edesc *edesc;
  1092. /* allocate extended descriptor */
  1093. edesc = aead_edesc_alloc(req, 0);
  1094. if (IS_ERR(edesc))
  1095. return PTR_ERR(edesc);
  1096. /* set encrypt */
  1097. edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_MODE0_ENCRYPT;
  1098. return ipsec_esp(edesc, req, NULL, 0, ipsec_esp_encrypt_done);
  1099. }
  1100. static int aead_decrypt(struct aead_request *req)
  1101. {
  1102. struct crypto_aead *authenc = crypto_aead_reqtfm(req);
  1103. struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
  1104. unsigned int authsize = ctx->authsize;
  1105. struct talitos_private *priv = dev_get_drvdata(ctx->dev);
  1106. struct talitos_edesc *edesc;
  1107. struct scatterlist *sg;
  1108. void *icvdata;
  1109. req->cryptlen -= authsize;
  1110. /* allocate extended descriptor */
  1111. edesc = aead_edesc_alloc(req, 1);
  1112. if (IS_ERR(edesc))
  1113. return PTR_ERR(edesc);
  1114. if ((priv->features & TALITOS_FTR_HW_AUTH_CHECK) &&
  1115. ((!edesc->src_nents && !edesc->dst_nents) ||
  1116. priv->features & TALITOS_FTR_SRC_LINK_TBL_LEN_INCLUDES_EXTENT)) {
  1117. /* decrypt and check the ICV */
  1118. edesc->desc.hdr = ctx->desc_hdr_template |
  1119. DESC_HDR_DIR_INBOUND |
  1120. DESC_HDR_MODE1_MDEU_CICV;
  1121. /* reset integrity check result bits */
  1122. edesc->desc.hdr_lo = 0;
  1123. return ipsec_esp(edesc, req, NULL, 0,
  1124. ipsec_esp_decrypt_hwauth_done);
  1125. }
  1126. /* Have to check the ICV with software */
  1127. edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_DIR_INBOUND;
  1128. /* stash incoming ICV for later cmp with ICV generated by the h/w */
  1129. if (edesc->dma_len)
  1130. icvdata = &edesc->link_tbl[edesc->src_nents +
  1131. edesc->dst_nents + 2];
  1132. else
  1133. icvdata = &edesc->link_tbl[0];
  1134. sg = sg_last(req->src, edesc->src_nents ? : 1);
  1135. memcpy(icvdata, (char *)sg_virt(sg) + sg->length - ctx->authsize,
  1136. ctx->authsize);
  1137. return ipsec_esp(edesc, req, NULL, 0, ipsec_esp_decrypt_swauth_done);
  1138. }
  1139. static int aead_givencrypt(struct aead_givcrypt_request *req)
  1140. {
  1141. struct aead_request *areq = &req->areq;
  1142. struct crypto_aead *authenc = crypto_aead_reqtfm(areq);
  1143. struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
  1144. struct talitos_edesc *edesc;
  1145. /* allocate extended descriptor */
  1146. edesc = aead_edesc_alloc(areq, 0);
  1147. if (IS_ERR(edesc))
  1148. return PTR_ERR(edesc);
  1149. /* set encrypt */
  1150. edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_MODE0_ENCRYPT;
  1151. memcpy(req->giv, ctx->iv, crypto_aead_ivsize(authenc));
  1152. /* avoid consecutive packets going out with same IV */
  1153. *(__be64 *)req->giv ^= cpu_to_be64(req->seq);
  1154. return ipsec_esp(edesc, areq, req->giv, req->seq,
  1155. ipsec_esp_encrypt_done);
  1156. }
  1157. static int ablkcipher_setkey(struct crypto_ablkcipher *cipher,
  1158. const u8 *key, unsigned int keylen)
  1159. {
  1160. struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
  1161. struct ablkcipher_alg *alg = crypto_ablkcipher_alg(cipher);
  1162. if (keylen > TALITOS_MAX_KEY_SIZE)
  1163. goto badkey;
  1164. if (keylen < alg->min_keysize || keylen > alg->max_keysize)
  1165. goto badkey;
  1166. memcpy(&ctx->key, key, keylen);
  1167. ctx->keylen = keylen;
  1168. return 0;
  1169. badkey:
  1170. crypto_ablkcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
  1171. return -EINVAL;
  1172. }
  1173. static void common_nonsnoop_unmap(struct device *dev,
  1174. struct talitos_edesc *edesc,
  1175. struct ablkcipher_request *areq)
  1176. {
  1177. unmap_single_talitos_ptr(dev, &edesc->desc.ptr[5], DMA_FROM_DEVICE);
  1178. unmap_single_talitos_ptr(dev, &edesc->desc.ptr[2], DMA_TO_DEVICE);
  1179. unmap_single_talitos_ptr(dev, &edesc->desc.ptr[1], DMA_TO_DEVICE);
  1180. talitos_sg_unmap(dev, edesc, areq->src, areq->dst);
  1181. if (edesc->dma_len)
  1182. dma_unmap_single(dev, edesc->dma_link_tbl, edesc->dma_len,
  1183. DMA_BIDIRECTIONAL);
  1184. }
  1185. static void ablkcipher_done(struct device *dev,
  1186. struct talitos_desc *desc, void *context,
  1187. int err)
  1188. {
  1189. struct ablkcipher_request *areq = context;
  1190. struct talitos_edesc *edesc;
  1191. edesc = container_of(desc, struct talitos_edesc, desc);
  1192. common_nonsnoop_unmap(dev, edesc, areq);
  1193. kfree(edesc);
  1194. areq->base.complete(&areq->base, err);
  1195. }
  1196. static int common_nonsnoop(struct talitos_edesc *edesc,
  1197. struct ablkcipher_request *areq,
  1198. u8 *giv,
  1199. void (*callback) (struct device *dev,
  1200. struct talitos_desc *desc,
  1201. void *context, int error))
  1202. {
  1203. struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
  1204. struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
  1205. struct device *dev = ctx->dev;
  1206. struct talitos_desc *desc = &edesc->desc;
  1207. unsigned int cryptlen = areq->nbytes;
  1208. unsigned int ivsize;
  1209. int sg_count, ret;
  1210. /* first DWORD empty */
  1211. desc->ptr[0].len = 0;
  1212. to_talitos_ptr(&desc->ptr[0], 0);
  1213. desc->ptr[0].j_extent = 0;
  1214. /* cipher iv */
  1215. ivsize = crypto_ablkcipher_ivsize(cipher);
  1216. map_single_talitos_ptr(dev, &desc->ptr[1], ivsize, giv ?: areq->info, 0,
  1217. DMA_TO_DEVICE);
  1218. /* cipher key */
  1219. map_single_talitos_ptr(dev, &desc->ptr[2], ctx->keylen,
  1220. (char *)&ctx->key, 0, DMA_TO_DEVICE);
  1221. /*
  1222. * cipher in
  1223. */
  1224. desc->ptr[3].len = cpu_to_be16(cryptlen);
  1225. desc->ptr[3].j_extent = 0;
  1226. sg_count = talitos_map_sg(dev, areq->src, edesc->src_nents ? : 1,
  1227. (areq->src == areq->dst) ? DMA_BIDIRECTIONAL
  1228. : DMA_TO_DEVICE,
  1229. edesc->src_is_chained);
  1230. if (sg_count == 1) {
  1231. to_talitos_ptr(&desc->ptr[3], sg_dma_address(areq->src));
  1232. } else {
  1233. sg_count = sg_to_link_tbl(areq->src, sg_count, cryptlen,
  1234. &edesc->link_tbl[0]);
  1235. if (sg_count > 1) {
  1236. to_talitos_ptr(&desc->ptr[3], edesc->dma_link_tbl);
  1237. desc->ptr[3].j_extent |= DESC_PTR_LNKTBL_JUMP;
  1238. dma_sync_single_for_device(dev, edesc->dma_link_tbl,
  1239. edesc->dma_len,
  1240. DMA_BIDIRECTIONAL);
  1241. } else {
  1242. /* Only one segment now, so no link tbl needed */
  1243. to_talitos_ptr(&desc->ptr[3],
  1244. sg_dma_address(areq->src));
  1245. }
  1246. }
  1247. /* cipher out */
  1248. desc->ptr[4].len = cpu_to_be16(cryptlen);
  1249. desc->ptr[4].j_extent = 0;
  1250. if (areq->src != areq->dst)
  1251. sg_count = talitos_map_sg(dev, areq->dst,
  1252. edesc->dst_nents ? : 1,
  1253. DMA_FROM_DEVICE,
  1254. edesc->dst_is_chained);
  1255. if (sg_count == 1) {
  1256. to_talitos_ptr(&desc->ptr[4], sg_dma_address(areq->dst));
  1257. } else {
  1258. struct talitos_ptr *link_tbl_ptr =
  1259. &edesc->link_tbl[edesc->src_nents + 1];
  1260. to_talitos_ptr(&desc->ptr[4], edesc->dma_link_tbl +
  1261. (edesc->src_nents + 1) *
  1262. sizeof(struct talitos_ptr));
  1263. desc->ptr[4].j_extent |= DESC_PTR_LNKTBL_JUMP;
  1264. sg_count = sg_to_link_tbl(areq->dst, sg_count, cryptlen,
  1265. link_tbl_ptr);
  1266. dma_sync_single_for_device(ctx->dev, edesc->dma_link_tbl,
  1267. edesc->dma_len, DMA_BIDIRECTIONAL);
  1268. }
  1269. /* iv out */
  1270. map_single_talitos_ptr(dev, &desc->ptr[5], ivsize, ctx->iv, 0,
  1271. DMA_FROM_DEVICE);
  1272. /* last DWORD empty */
  1273. desc->ptr[6].len = 0;
  1274. to_talitos_ptr(&desc->ptr[6], 0);
  1275. desc->ptr[6].j_extent = 0;
  1276. ret = talitos_submit(dev, desc, callback, areq);
  1277. if (ret != -EINPROGRESS) {
  1278. common_nonsnoop_unmap(dev, edesc, areq);
  1279. kfree(edesc);
  1280. }
  1281. return ret;
  1282. }
  1283. static struct talitos_edesc *ablkcipher_edesc_alloc(struct ablkcipher_request *
  1284. areq)
  1285. {
  1286. struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
  1287. struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
  1288. return talitos_edesc_alloc(ctx->dev, areq->src, areq->dst, 0,
  1289. areq->nbytes, 0, 0, areq->base.flags);
  1290. }
  1291. static int ablkcipher_encrypt(struct ablkcipher_request *areq)
  1292. {
  1293. struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
  1294. struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
  1295. struct talitos_edesc *edesc;
  1296. /* allocate extended descriptor */
  1297. edesc = ablkcipher_edesc_alloc(areq);
  1298. if (IS_ERR(edesc))
  1299. return PTR_ERR(edesc);
  1300. /* set encrypt */
  1301. edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_MODE0_ENCRYPT;
  1302. return common_nonsnoop(edesc, areq, NULL, ablkcipher_done);
  1303. }
  1304. static int ablkcipher_decrypt(struct ablkcipher_request *areq)
  1305. {
  1306. struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
  1307. struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
  1308. struct talitos_edesc *edesc;
  1309. /* allocate extended descriptor */
  1310. edesc = ablkcipher_edesc_alloc(areq);
  1311. if (IS_ERR(edesc))
  1312. return PTR_ERR(edesc);
  1313. edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_DIR_INBOUND;
  1314. return common_nonsnoop(edesc, areq, NULL, ablkcipher_done);
  1315. }
  1316. static void common_nonsnoop_hash_unmap(struct device *dev,
  1317. struct talitos_edesc *edesc,
  1318. struct ahash_request *areq)
  1319. {
  1320. struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
  1321. unmap_single_talitos_ptr(dev, &edesc->desc.ptr[5], DMA_FROM_DEVICE);
  1322. /* When using hashctx-in, must unmap it. */
  1323. if (edesc->desc.ptr[1].len)
  1324. unmap_single_talitos_ptr(dev, &edesc->desc.ptr[1],
  1325. DMA_TO_DEVICE);
  1326. if (edesc->desc.ptr[2].len)
  1327. unmap_single_talitos_ptr(dev, &edesc->desc.ptr[2],
  1328. DMA_TO_DEVICE);
  1329. talitos_sg_unmap(dev, edesc, req_ctx->psrc, NULL);
  1330. if (edesc->dma_len)
  1331. dma_unmap_single(dev, edesc->dma_link_tbl, edesc->dma_len,
  1332. DMA_BIDIRECTIONAL);
  1333. }
  1334. static void ahash_done(struct device *dev,
  1335. struct talitos_desc *desc, void *context,
  1336. int err)
  1337. {
  1338. struct ahash_request *areq = context;
  1339. struct talitos_edesc *edesc =
  1340. container_of(desc, struct talitos_edesc, desc);
  1341. struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
  1342. if (!req_ctx->last && req_ctx->to_hash_later) {
  1343. /* Position any partial block for next update/final/finup */
  1344. memcpy(req_ctx->buf, req_ctx->bufnext, req_ctx->to_hash_later);
  1345. req_ctx->nbuf = req_ctx->to_hash_later;
  1346. }
  1347. common_nonsnoop_hash_unmap(dev, edesc, areq);
  1348. kfree(edesc);
  1349. areq->base.complete(&areq->base, err);
  1350. }
  1351. static int common_nonsnoop_hash(struct talitos_edesc *edesc,
  1352. struct ahash_request *areq, unsigned int length,
  1353. void (*callback) (struct device *dev,
  1354. struct talitos_desc *desc,
  1355. void *context, int error))
  1356. {
  1357. struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
  1358. struct talitos_ctx *ctx = crypto_ahash_ctx(tfm);
  1359. struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
  1360. struct device *dev = ctx->dev;
  1361. struct talitos_desc *desc = &edesc->desc;
  1362. int sg_count, ret;
  1363. /* first DWORD empty */
  1364. desc->ptr[0] = zero_entry;
  1365. /* hash context in */
  1366. if (!req_ctx->first || req_ctx->swinit) {
  1367. map_single_talitos_ptr(dev, &desc->ptr[1],
  1368. req_ctx->hw_context_size,
  1369. (char *)req_ctx->hw_context, 0,
  1370. DMA_TO_DEVICE);
  1371. req_ctx->swinit = 0;
  1372. } else {
  1373. desc->ptr[1] = zero_entry;
  1374. /* Indicate next op is not the first. */
  1375. req_ctx->first = 0;
  1376. }
  1377. /* HMAC key */
  1378. if (ctx->keylen)
  1379. map_single_talitos_ptr(dev, &desc->ptr[2], ctx->keylen,
  1380. (char *)&ctx->key, 0, DMA_TO_DEVICE);
  1381. else
  1382. desc->ptr[2] = zero_entry;
  1383. /*
  1384. * data in
  1385. */
  1386. desc->ptr[3].len = cpu_to_be16(length);
  1387. desc->ptr[3].j_extent = 0;
  1388. sg_count = talitos_map_sg(dev, req_ctx->psrc,
  1389. edesc->src_nents ? : 1,
  1390. DMA_TO_DEVICE,
  1391. edesc->src_is_chained);
  1392. if (sg_count == 1) {
  1393. to_talitos_ptr(&desc->ptr[3], sg_dma_address(req_ctx->psrc));
  1394. } else {
  1395. sg_count = sg_to_link_tbl(req_ctx->psrc, sg_count, length,
  1396. &edesc->link_tbl[0]);
  1397. if (sg_count > 1) {
  1398. desc->ptr[3].j_extent |= DESC_PTR_LNKTBL_JUMP;
  1399. to_talitos_ptr(&desc->ptr[3], edesc->dma_link_tbl);
  1400. dma_sync_single_for_device(ctx->dev,
  1401. edesc->dma_link_tbl,
  1402. edesc->dma_len,
  1403. DMA_BIDIRECTIONAL);
  1404. } else {
  1405. /* Only one segment now, so no link tbl needed */
  1406. to_talitos_ptr(&desc->ptr[3],
  1407. sg_dma_address(req_ctx->psrc));
  1408. }
  1409. }
  1410. /* fifth DWORD empty */
  1411. desc->ptr[4] = zero_entry;
  1412. /* hash/HMAC out -or- hash context out */
  1413. if (req_ctx->last)
  1414. map_single_talitos_ptr(dev, &desc->ptr[5],
  1415. crypto_ahash_digestsize(tfm),
  1416. areq->result, 0, DMA_FROM_DEVICE);
  1417. else
  1418. map_single_talitos_ptr(dev, &desc->ptr[5],
  1419. req_ctx->hw_context_size,
  1420. req_ctx->hw_context, 0, DMA_FROM_DEVICE);
  1421. /* last DWORD empty */
  1422. desc->ptr[6] = zero_entry;
  1423. ret = talitos_submit(dev, desc, callback, areq);
  1424. if (ret != -EINPROGRESS) {
  1425. common_nonsnoop_hash_unmap(dev, edesc, areq);
  1426. kfree(edesc);
  1427. }
  1428. return ret;
  1429. }
  1430. static struct talitos_edesc *ahash_edesc_alloc(struct ahash_request *areq,
  1431. unsigned int nbytes)
  1432. {
  1433. struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
  1434. struct talitos_ctx *ctx = crypto_ahash_ctx(tfm);
  1435. struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
  1436. return talitos_edesc_alloc(ctx->dev, req_ctx->psrc, NULL, 1,
  1437. nbytes, 0, 0, areq->base.flags);
  1438. }
  1439. static int ahash_init(struct ahash_request *areq)
  1440. {
  1441. struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
  1442. struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
  1443. /* Initialize the context */
  1444. req_ctx->nbuf = 0;
  1445. req_ctx->first = 1; /* first indicates h/w must init its context */
  1446. req_ctx->swinit = 0; /* assume h/w init of context */
  1447. req_ctx->hw_context_size =
  1448. (crypto_ahash_digestsize(tfm) <= SHA256_DIGEST_SIZE)
  1449. ? TALITOS_MDEU_CONTEXT_SIZE_MD5_SHA1_SHA256
  1450. : TALITOS_MDEU_CONTEXT_SIZE_SHA384_SHA512;
  1451. return 0;
  1452. }
  1453. /*
  1454. * on h/w without explicit sha224 support, we initialize h/w context
  1455. * manually with sha224 constants, and tell it to run sha256.
  1456. */
  1457. static int ahash_init_sha224_swinit(struct ahash_request *areq)
  1458. {
  1459. struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
  1460. ahash_init(areq);
  1461. req_ctx->swinit = 1;/* prevent h/w initting context with sha256 values*/
  1462. req_ctx->hw_context[0] = SHA224_H0;
  1463. req_ctx->hw_context[1] = SHA224_H1;
  1464. req_ctx->hw_context[2] = SHA224_H2;
  1465. req_ctx->hw_context[3] = SHA224_H3;
  1466. req_ctx->hw_context[4] = SHA224_H4;
  1467. req_ctx->hw_context[5] = SHA224_H5;
  1468. req_ctx->hw_context[6] = SHA224_H6;
  1469. req_ctx->hw_context[7] = SHA224_H7;
  1470. /* init 64-bit count */
  1471. req_ctx->hw_context[8] = 0;
  1472. req_ctx->hw_context[9] = 0;
  1473. return 0;
  1474. }
  1475. static int ahash_process_req(struct ahash_request *areq, unsigned int nbytes)
  1476. {
  1477. struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
  1478. struct talitos_ctx *ctx = crypto_ahash_ctx(tfm);
  1479. struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
  1480. struct talitos_edesc *edesc;
  1481. unsigned int blocksize =
  1482. crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
  1483. unsigned int nbytes_to_hash;
  1484. unsigned int to_hash_later;
  1485. unsigned int nsg;
  1486. int chained;
  1487. if (!req_ctx->last && (nbytes + req_ctx->nbuf <= blocksize)) {
  1488. /* Buffer up to one whole block */
  1489. sg_copy_to_buffer(areq->src,
  1490. sg_count(areq->src, nbytes, &chained),
  1491. req_ctx->buf + req_ctx->nbuf, nbytes);
  1492. req_ctx->nbuf += nbytes;
  1493. return 0;
  1494. }
  1495. /* At least (blocksize + 1) bytes are available to hash */
  1496. nbytes_to_hash = nbytes + req_ctx->nbuf;
  1497. to_hash_later = nbytes_to_hash & (blocksize - 1);
  1498. if (req_ctx->last)
  1499. to_hash_later = 0;
  1500. else if (to_hash_later)
  1501. /* There is a partial block. Hash the full block(s) now */
  1502. nbytes_to_hash -= to_hash_later;
  1503. else {
  1504. /* Keep one block buffered */
  1505. nbytes_to_hash -= blocksize;
  1506. to_hash_later = blocksize;
  1507. }
  1508. /* Chain in any previously buffered data */
  1509. if (req_ctx->nbuf) {
  1510. nsg = (req_ctx->nbuf < nbytes_to_hash) ? 2 : 1;
  1511. sg_init_table(req_ctx->bufsl, nsg);
  1512. sg_set_buf(req_ctx->bufsl, req_ctx->buf, req_ctx->nbuf);
  1513. if (nsg > 1)
  1514. scatterwalk_sg_chain(req_ctx->bufsl, 2, areq->src);
  1515. req_ctx->psrc = req_ctx->bufsl;
  1516. } else
  1517. req_ctx->psrc = areq->src;
  1518. if (to_hash_later) {
  1519. int nents = sg_count(areq->src, nbytes, &chained);
  1520. sg_copy_end_to_buffer(areq->src, nents,
  1521. req_ctx->bufnext,
  1522. to_hash_later,
  1523. nbytes - to_hash_later);
  1524. }
  1525. req_ctx->to_hash_later = to_hash_later;
  1526. /* Allocate extended descriptor */
  1527. edesc = ahash_edesc_alloc(areq, nbytes_to_hash);
  1528. if (IS_ERR(edesc))
  1529. return PTR_ERR(edesc);
  1530. edesc->desc.hdr = ctx->desc_hdr_template;
  1531. /* On last one, request SEC to pad; otherwise continue */
  1532. if (req_ctx->last)
  1533. edesc->desc.hdr |= DESC_HDR_MODE0_MDEU_PAD;
  1534. else
  1535. edesc->desc.hdr |= DESC_HDR_MODE0_MDEU_CONT;
  1536. /* request SEC to INIT hash. */
  1537. if (req_ctx->first && !req_ctx->swinit)
  1538. edesc->desc.hdr |= DESC_HDR_MODE0_MDEU_INIT;
  1539. /* When the tfm context has a keylen, it's an HMAC.
  1540. * A first or last (ie. not middle) descriptor must request HMAC.
  1541. */
  1542. if (ctx->keylen && (req_ctx->first || req_ctx->last))
  1543. edesc->desc.hdr |= DESC_HDR_MODE0_MDEU_HMAC;
  1544. return common_nonsnoop_hash(edesc, areq, nbytes_to_hash,
  1545. ahash_done);
  1546. }
  1547. static int ahash_update(struct ahash_request *areq)
  1548. {
  1549. struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
  1550. req_ctx->last = 0;
  1551. return ahash_process_req(areq, areq->nbytes);
  1552. }
  1553. static int ahash_final(struct ahash_request *areq)
  1554. {
  1555. struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
  1556. req_ctx->last = 1;
  1557. return ahash_process_req(areq, 0);
  1558. }
  1559. static int ahash_finup(struct ahash_request *areq)
  1560. {
  1561. struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
  1562. req_ctx->last = 1;
  1563. return ahash_process_req(areq, areq->nbytes);
  1564. }
  1565. static int ahash_digest(struct ahash_request *areq)
  1566. {
  1567. struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
  1568. struct crypto_ahash *ahash = crypto_ahash_reqtfm(areq);
  1569. ahash->init(areq);
  1570. req_ctx->last = 1;
  1571. return ahash_process_req(areq, areq->nbytes);
  1572. }
  1573. struct talitos_alg_template {
  1574. u32 type;
  1575. union {
  1576. struct crypto_alg crypto;
  1577. struct ahash_alg hash;
  1578. } alg;
  1579. __be32 desc_hdr_template;
  1580. };
  1581. static struct talitos_alg_template driver_algs[] = {
  1582. /* AEAD algorithms. These use a single-pass ipsec_esp descriptor */
  1583. { .type = CRYPTO_ALG_TYPE_AEAD,
  1584. .alg.crypto = {
  1585. .cra_name = "authenc(hmac(sha1),cbc(aes))",
  1586. .cra_driver_name = "authenc-hmac-sha1-cbc-aes-talitos",
  1587. .cra_blocksize = AES_BLOCK_SIZE,
  1588. .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
  1589. .cra_type = &crypto_aead_type,
  1590. .cra_aead = {
  1591. .setkey = aead_setkey,
  1592. .setauthsize = aead_setauthsize,
  1593. .encrypt = aead_encrypt,
  1594. .decrypt = aead_decrypt,
  1595. .givencrypt = aead_givencrypt,
  1596. .geniv = "<built-in>",
  1597. .ivsize = AES_BLOCK_SIZE,
  1598. .maxauthsize = SHA1_DIGEST_SIZE,
  1599. }
  1600. },
  1601. .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
  1602. DESC_HDR_SEL0_AESU |
  1603. DESC_HDR_MODE0_AESU_CBC |
  1604. DESC_HDR_SEL1_MDEUA |
  1605. DESC_HDR_MODE1_MDEU_INIT |
  1606. DESC_HDR_MODE1_MDEU_PAD |
  1607. DESC_HDR_MODE1_MDEU_SHA1_HMAC,
  1608. },
  1609. { .type = CRYPTO_ALG_TYPE_AEAD,
  1610. .alg.crypto = {
  1611. .cra_name = "authenc(hmac(sha1),cbc(des3_ede))",
  1612. .cra_driver_name = "authenc-hmac-sha1-cbc-3des-talitos",
  1613. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  1614. .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
  1615. .cra_type = &crypto_aead_type,
  1616. .cra_aead = {
  1617. .setkey = aead_setkey,
  1618. .setauthsize = aead_setauthsize,
  1619. .encrypt = aead_encrypt,
  1620. .decrypt = aead_decrypt,
  1621. .givencrypt = aead_givencrypt,
  1622. .geniv = "<built-in>",
  1623. .ivsize = DES3_EDE_BLOCK_SIZE,
  1624. .maxauthsize = SHA1_DIGEST_SIZE,
  1625. }
  1626. },
  1627. .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
  1628. DESC_HDR_SEL0_DEU |
  1629. DESC_HDR_MODE0_DEU_CBC |
  1630. DESC_HDR_MODE0_DEU_3DES |
  1631. DESC_HDR_SEL1_MDEUA |
  1632. DESC_HDR_MODE1_MDEU_INIT |
  1633. DESC_HDR_MODE1_MDEU_PAD |
  1634. DESC_HDR_MODE1_MDEU_SHA1_HMAC,
  1635. },
  1636. { .type = CRYPTO_ALG_TYPE_AEAD,
  1637. .alg.crypto = {
  1638. .cra_name = "authenc(hmac(sha256),cbc(aes))",
  1639. .cra_driver_name = "authenc-hmac-sha256-cbc-aes-talitos",
  1640. .cra_blocksize = AES_BLOCK_SIZE,
  1641. .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
  1642. .cra_type = &crypto_aead_type,
  1643. .cra_aead = {
  1644. .setkey = aead_setkey,
  1645. .setauthsize = aead_setauthsize,
  1646. .encrypt = aead_encrypt,
  1647. .decrypt = aead_decrypt,
  1648. .givencrypt = aead_givencrypt,
  1649. .geniv = "<built-in>",
  1650. .ivsize = AES_BLOCK_SIZE,
  1651. .maxauthsize = SHA256_DIGEST_SIZE,
  1652. }
  1653. },
  1654. .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
  1655. DESC_HDR_SEL0_AESU |
  1656. DESC_HDR_MODE0_AESU_CBC |
  1657. DESC_HDR_SEL1_MDEUA |
  1658. DESC_HDR_MODE1_MDEU_INIT |
  1659. DESC_HDR_MODE1_MDEU_PAD |
  1660. DESC_HDR_MODE1_MDEU_SHA256_HMAC,
  1661. },
  1662. { .type = CRYPTO_ALG_TYPE_AEAD,
  1663. .alg.crypto = {
  1664. .cra_name = "authenc(hmac(sha256),cbc(des3_ede))",
  1665. .cra_driver_name = "authenc-hmac-sha256-cbc-3des-talitos",
  1666. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  1667. .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
  1668. .cra_type = &crypto_aead_type,
  1669. .cra_aead = {
  1670. .setkey = aead_setkey,
  1671. .setauthsize = aead_setauthsize,
  1672. .encrypt = aead_encrypt,
  1673. .decrypt = aead_decrypt,
  1674. .givencrypt = aead_givencrypt,
  1675. .geniv = "<built-in>",
  1676. .ivsize = DES3_EDE_BLOCK_SIZE,
  1677. .maxauthsize = SHA256_DIGEST_SIZE,
  1678. }
  1679. },
  1680. .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
  1681. DESC_HDR_SEL0_DEU |
  1682. DESC_HDR_MODE0_DEU_CBC |
  1683. DESC_HDR_MODE0_DEU_3DES |
  1684. DESC_HDR_SEL1_MDEUA |
  1685. DESC_HDR_MODE1_MDEU_INIT |
  1686. DESC_HDR_MODE1_MDEU_PAD |
  1687. DESC_HDR_MODE1_MDEU_SHA256_HMAC,
  1688. },
  1689. { .type = CRYPTO_ALG_TYPE_AEAD,
  1690. .alg.crypto = {
  1691. .cra_name = "authenc(hmac(md5),cbc(aes))",
  1692. .cra_driver_name = "authenc-hmac-md5-cbc-aes-talitos",
  1693. .cra_blocksize = AES_BLOCK_SIZE,
  1694. .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
  1695. .cra_type = &crypto_aead_type,
  1696. .cra_aead = {
  1697. .setkey = aead_setkey,
  1698. .setauthsize = aead_setauthsize,
  1699. .encrypt = aead_encrypt,
  1700. .decrypt = aead_decrypt,
  1701. .givencrypt = aead_givencrypt,
  1702. .geniv = "<built-in>",
  1703. .ivsize = AES_BLOCK_SIZE,
  1704. .maxauthsize = MD5_DIGEST_SIZE,
  1705. }
  1706. },
  1707. .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
  1708. DESC_HDR_SEL0_AESU |
  1709. DESC_HDR_MODE0_AESU_CBC |
  1710. DESC_HDR_SEL1_MDEUA |
  1711. DESC_HDR_MODE1_MDEU_INIT |
  1712. DESC_HDR_MODE1_MDEU_PAD |
  1713. DESC_HDR_MODE1_MDEU_MD5_HMAC,
  1714. },
  1715. { .type = CRYPTO_ALG_TYPE_AEAD,
  1716. .alg.crypto = {
  1717. .cra_name = "authenc(hmac(md5),cbc(des3_ede))",
  1718. .cra_driver_name = "authenc-hmac-md5-cbc-3des-talitos",
  1719. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  1720. .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
  1721. .cra_type = &crypto_aead_type,
  1722. .cra_aead = {
  1723. .setkey = aead_setkey,
  1724. .setauthsize = aead_setauthsize,
  1725. .encrypt = aead_encrypt,
  1726. .decrypt = aead_decrypt,
  1727. .givencrypt = aead_givencrypt,
  1728. .geniv = "<built-in>",
  1729. .ivsize = DES3_EDE_BLOCK_SIZE,
  1730. .maxauthsize = MD5_DIGEST_SIZE,
  1731. }
  1732. },
  1733. .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
  1734. DESC_HDR_SEL0_DEU |
  1735. DESC_HDR_MODE0_DEU_CBC |
  1736. DESC_HDR_MODE0_DEU_3DES |
  1737. DESC_HDR_SEL1_MDEUA |
  1738. DESC_HDR_MODE1_MDEU_INIT |
  1739. DESC_HDR_MODE1_MDEU_PAD |
  1740. DESC_HDR_MODE1_MDEU_MD5_HMAC,
  1741. },
  1742. /* ABLKCIPHER algorithms. */
  1743. { .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
  1744. .alg.crypto = {
  1745. .cra_name = "cbc(aes)",
  1746. .cra_driver_name = "cbc-aes-talitos",
  1747. .cra_blocksize = AES_BLOCK_SIZE,
  1748. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
  1749. CRYPTO_ALG_ASYNC,
  1750. .cra_type = &crypto_ablkcipher_type,
  1751. .cra_ablkcipher = {
  1752. .setkey = ablkcipher_setkey,
  1753. .encrypt = ablkcipher_encrypt,
  1754. .decrypt = ablkcipher_decrypt,
  1755. .geniv = "eseqiv",
  1756. .min_keysize = AES_MIN_KEY_SIZE,
  1757. .max_keysize = AES_MAX_KEY_SIZE,
  1758. .ivsize = AES_BLOCK_SIZE,
  1759. }
  1760. },
  1761. .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
  1762. DESC_HDR_SEL0_AESU |
  1763. DESC_HDR_MODE0_AESU_CBC,
  1764. },
  1765. { .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
  1766. .alg.crypto = {
  1767. .cra_name = "cbc(des3_ede)",
  1768. .cra_driver_name = "cbc-3des-talitos",
  1769. .cra_blocksize = DES3_EDE_BLOCK_SIZE,
  1770. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
  1771. CRYPTO_ALG_ASYNC,
  1772. .cra_type = &crypto_ablkcipher_type,
  1773. .cra_ablkcipher = {
  1774. .setkey = ablkcipher_setkey,
  1775. .encrypt = ablkcipher_encrypt,
  1776. .decrypt = ablkcipher_decrypt,
  1777. .geniv = "eseqiv",
  1778. .min_keysize = DES3_EDE_KEY_SIZE,
  1779. .max_keysize = DES3_EDE_KEY_SIZE,
  1780. .ivsize = DES3_EDE_BLOCK_SIZE,
  1781. }
  1782. },
  1783. .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
  1784. DESC_HDR_SEL0_DEU |
  1785. DESC_HDR_MODE0_DEU_CBC |
  1786. DESC_HDR_MODE0_DEU_3DES,
  1787. },
  1788. /* AHASH algorithms. */
  1789. { .type = CRYPTO_ALG_TYPE_AHASH,
  1790. .alg.hash = {
  1791. .init = ahash_init,
  1792. .update = ahash_update,
  1793. .final = ahash_final,
  1794. .finup = ahash_finup,
  1795. .digest = ahash_digest,
  1796. .halg.digestsize = MD5_DIGEST_SIZE,
  1797. .halg.base = {
  1798. .cra_name = "md5",
  1799. .cra_driver_name = "md5-talitos",
  1800. .cra_blocksize = MD5_BLOCK_SIZE,
  1801. .cra_flags = CRYPTO_ALG_TYPE_AHASH |
  1802. CRYPTO_ALG_ASYNC,
  1803. .cra_type = &crypto_ahash_type
  1804. }
  1805. },
  1806. .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
  1807. DESC_HDR_SEL0_MDEUA |
  1808. DESC_HDR_MODE0_MDEU_MD5,
  1809. },
  1810. { .type = CRYPTO_ALG_TYPE_AHASH,
  1811. .alg.hash = {
  1812. .init = ahash_init,
  1813. .update = ahash_update,
  1814. .final = ahash_final,
  1815. .finup = ahash_finup,
  1816. .digest = ahash_digest,
  1817. .halg.digestsize = SHA1_DIGEST_SIZE,
  1818. .halg.base = {
  1819. .cra_name = "sha1",
  1820. .cra_driver_name = "sha1-talitos",
  1821. .cra_blocksize = SHA1_BLOCK_SIZE,
  1822. .cra_flags = CRYPTO_ALG_TYPE_AHASH |
  1823. CRYPTO_ALG_ASYNC,
  1824. .cra_type = &crypto_ahash_type
  1825. }
  1826. },
  1827. .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
  1828. DESC_HDR_SEL0_MDEUA |
  1829. DESC_HDR_MODE0_MDEU_SHA1,
  1830. },
  1831. { .type = CRYPTO_ALG_TYPE_AHASH,
  1832. .alg.hash = {
  1833. .init = ahash_init,
  1834. .update = ahash_update,
  1835. .final = ahash_final,
  1836. .finup = ahash_finup,
  1837. .digest = ahash_digest,
  1838. .halg.digestsize = SHA224_DIGEST_SIZE,
  1839. .halg.base = {
  1840. .cra_name = "sha224",
  1841. .cra_driver_name = "sha224-talitos",
  1842. .cra_blocksize = SHA224_BLOCK_SIZE,
  1843. .cra_flags = CRYPTO_ALG_TYPE_AHASH |
  1844. CRYPTO_ALG_ASYNC,
  1845. .cra_type = &crypto_ahash_type
  1846. }
  1847. },
  1848. .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
  1849. DESC_HDR_SEL0_MDEUA |
  1850. DESC_HDR_MODE0_MDEU_SHA224,
  1851. },
  1852. { .type = CRYPTO_ALG_TYPE_AHASH,
  1853. .alg.hash = {
  1854. .init = ahash_init,
  1855. .update = ahash_update,
  1856. .final = ahash_final,
  1857. .finup = ahash_finup,
  1858. .digest = ahash_digest,
  1859. .halg.digestsize = SHA256_DIGEST_SIZE,
  1860. .halg.base = {
  1861. .cra_name = "sha256",
  1862. .cra_driver_name = "sha256-talitos",
  1863. .cra_blocksize = SHA256_BLOCK_SIZE,
  1864. .cra_flags = CRYPTO_ALG_TYPE_AHASH |
  1865. CRYPTO_ALG_ASYNC,
  1866. .cra_type = &crypto_ahash_type
  1867. }
  1868. },
  1869. .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
  1870. DESC_HDR_SEL0_MDEUA |
  1871. DESC_HDR_MODE0_MDEU_SHA256,
  1872. },
  1873. { .type = CRYPTO_ALG_TYPE_AHASH,
  1874. .alg.hash = {
  1875. .init = ahash_init,
  1876. .update = ahash_update,
  1877. .final = ahash_final,
  1878. .finup = ahash_finup,
  1879. .digest = ahash_digest,
  1880. .halg.digestsize = SHA384_DIGEST_SIZE,
  1881. .halg.base = {
  1882. .cra_name = "sha384",
  1883. .cra_driver_name = "sha384-talitos",
  1884. .cra_blocksize = SHA384_BLOCK_SIZE,
  1885. .cra_flags = CRYPTO_ALG_TYPE_AHASH |
  1886. CRYPTO_ALG_ASYNC,
  1887. .cra_type = &crypto_ahash_type
  1888. }
  1889. },
  1890. .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
  1891. DESC_HDR_SEL0_MDEUB |
  1892. DESC_HDR_MODE0_MDEUB_SHA384,
  1893. },
  1894. { .type = CRYPTO_ALG_TYPE_AHASH,
  1895. .alg.hash = {
  1896. .init = ahash_init,
  1897. .update = ahash_update,
  1898. .final = ahash_final,
  1899. .finup = ahash_finup,
  1900. .digest = ahash_digest,
  1901. .halg.digestsize = SHA512_DIGEST_SIZE,
  1902. .halg.base = {
  1903. .cra_name = "sha512",
  1904. .cra_driver_name = "sha512-talitos",
  1905. .cra_blocksize = SHA512_BLOCK_SIZE,
  1906. .cra_flags = CRYPTO_ALG_TYPE_AHASH |
  1907. CRYPTO_ALG_ASYNC,
  1908. .cra_type = &crypto_ahash_type
  1909. }
  1910. },
  1911. .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
  1912. DESC_HDR_SEL0_MDEUB |
  1913. DESC_HDR_MODE0_MDEUB_SHA512,
  1914. },
  1915. };
  1916. struct talitos_crypto_alg {
  1917. struct list_head entry;
  1918. struct device *dev;
  1919. struct talitos_alg_template algt;
  1920. };
  1921. static int talitos_cra_init(struct crypto_tfm *tfm)
  1922. {
  1923. struct crypto_alg *alg = tfm->__crt_alg;
  1924. struct talitos_crypto_alg *talitos_alg;
  1925. struct talitos_ctx *ctx = crypto_tfm_ctx(tfm);
  1926. if ((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) == CRYPTO_ALG_TYPE_AHASH)
  1927. talitos_alg = container_of(__crypto_ahash_alg(alg),
  1928. struct talitos_crypto_alg,
  1929. algt.alg.hash);
  1930. else
  1931. talitos_alg = container_of(alg, struct talitos_crypto_alg,
  1932. algt.alg.crypto);
  1933. /* update context with ptr to dev */
  1934. ctx->dev = talitos_alg->dev;
  1935. /* copy descriptor header template value */
  1936. ctx->desc_hdr_template = talitos_alg->algt.desc_hdr_template;
  1937. return 0;
  1938. }
  1939. static int talitos_cra_init_aead(struct crypto_tfm *tfm)
  1940. {
  1941. struct talitos_ctx *ctx = crypto_tfm_ctx(tfm);
  1942. talitos_cra_init(tfm);
  1943. /* random first IV */
  1944. get_random_bytes(ctx->iv, TALITOS_MAX_IV_LENGTH);
  1945. return 0;
  1946. }
  1947. static int talitos_cra_init_ahash(struct crypto_tfm *tfm)
  1948. {
  1949. struct talitos_ctx *ctx = crypto_tfm_ctx(tfm);
  1950. talitos_cra_init(tfm);
  1951. ctx->keylen = 0;
  1952. crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
  1953. sizeof(struct talitos_ahash_req_ctx));
  1954. return 0;
  1955. }
  1956. /*
  1957. * given the alg's descriptor header template, determine whether descriptor
  1958. * type and primary/secondary execution units required match the hw
  1959. * capabilities description provided in the device tree node.
  1960. */
  1961. static int hw_supports(struct device *dev, __be32 desc_hdr_template)
  1962. {
  1963. struct talitos_private *priv = dev_get_drvdata(dev);
  1964. int ret;
  1965. ret = (1 << DESC_TYPE(desc_hdr_template) & priv->desc_types) &&
  1966. (1 << PRIMARY_EU(desc_hdr_template) & priv->exec_units);
  1967. if (SECONDARY_EU(desc_hdr_template))
  1968. ret = ret && (1 << SECONDARY_EU(desc_hdr_template)
  1969. & priv->exec_units);
  1970. return ret;
  1971. }
  1972. static int talitos_remove(struct platform_device *ofdev)
  1973. {
  1974. struct device *dev = &ofdev->dev;
  1975. struct talitos_private *priv = dev_get_drvdata(dev);
  1976. struct talitos_crypto_alg *t_alg, *n;
  1977. int i;
  1978. list_for_each_entry_safe(t_alg, n, &priv->alg_list, entry) {
  1979. switch (t_alg->algt.type) {
  1980. case CRYPTO_ALG_TYPE_ABLKCIPHER:
  1981. case CRYPTO_ALG_TYPE_AEAD:
  1982. crypto_unregister_alg(&t_alg->algt.alg.crypto);
  1983. break;
  1984. case CRYPTO_ALG_TYPE_AHASH:
  1985. crypto_unregister_ahash(&t_alg->algt.alg.hash);
  1986. break;
  1987. }
  1988. list_del(&t_alg->entry);
  1989. kfree(t_alg);
  1990. }
  1991. if (hw_supports(dev, DESC_HDR_SEL0_RNG))
  1992. talitos_unregister_rng(dev);
  1993. for (i = 0; i < priv->num_channels; i++)
  1994. kfree(priv->chan[i].fifo);
  1995. kfree(priv->chan);
  1996. if (priv->irq != NO_IRQ) {
  1997. free_irq(priv->irq, dev);
  1998. irq_dispose_mapping(priv->irq);
  1999. }
  2000. tasklet_kill(&priv->done_task);
  2001. iounmap(priv->reg);
  2002. dev_set_drvdata(dev, NULL);
  2003. kfree(priv);
  2004. return 0;
  2005. }
  2006. static struct talitos_crypto_alg *talitos_alg_alloc(struct device *dev,
  2007. struct talitos_alg_template
  2008. *template)
  2009. {
  2010. struct talitos_private *priv = dev_get_drvdata(dev);
  2011. struct talitos_crypto_alg *t_alg;
  2012. struct crypto_alg *alg;
  2013. t_alg = kzalloc(sizeof(struct talitos_crypto_alg), GFP_KERNEL);
  2014. if (!t_alg)
  2015. return ERR_PTR(-ENOMEM);
  2016. t_alg->algt = *template;
  2017. switch (t_alg->algt.type) {
  2018. case CRYPTO_ALG_TYPE_ABLKCIPHER:
  2019. alg = &t_alg->algt.alg.crypto;
  2020. alg->cra_init = talitos_cra_init;
  2021. break;
  2022. case CRYPTO_ALG_TYPE_AEAD:
  2023. alg = &t_alg->algt.alg.crypto;
  2024. alg->cra_init = talitos_cra_init_aead;
  2025. break;
  2026. case CRYPTO_ALG_TYPE_AHASH:
  2027. alg = &t_alg->algt.alg.hash.halg.base;
  2028. alg->cra_init = talitos_cra_init_ahash;
  2029. if (!(priv->features & TALITOS_FTR_SHA224_HWINIT) &&
  2030. !strcmp(alg->cra_name, "sha224")) {
  2031. t_alg->algt.alg.hash.init = ahash_init_sha224_swinit;
  2032. t_alg->algt.desc_hdr_template =
  2033. DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
  2034. DESC_HDR_SEL0_MDEUA |
  2035. DESC_HDR_MODE0_MDEU_SHA256;
  2036. }
  2037. break;
  2038. default:
  2039. dev_err(dev, "unknown algorithm type %d\n", t_alg->algt.type);
  2040. return ERR_PTR(-EINVAL);
  2041. }
  2042. alg->cra_module = THIS_MODULE;
  2043. alg->cra_priority = TALITOS_CRA_PRIORITY;
  2044. alg->cra_alignmask = 0;
  2045. alg->cra_ctxsize = sizeof(struct talitos_ctx);
  2046. t_alg->dev = dev;
  2047. return t_alg;
  2048. }
  2049. static int talitos_probe(struct platform_device *ofdev)
  2050. {
  2051. struct device *dev = &ofdev->dev;
  2052. struct device_node *np = ofdev->dev.of_node;
  2053. struct talitos_private *priv;
  2054. const unsigned int *prop;
  2055. int i, err;
  2056. priv = kzalloc(sizeof(struct talitos_private), GFP_KERNEL);
  2057. if (!priv)
  2058. return -ENOMEM;
  2059. dev_set_drvdata(dev, priv);
  2060. priv->ofdev = ofdev;
  2061. tasklet_init(&priv->done_task, talitos_done, (unsigned long)dev);
  2062. INIT_LIST_HEAD(&priv->alg_list);
  2063. priv->irq = irq_of_parse_and_map(np, 0);
  2064. if (priv->irq == NO_IRQ) {
  2065. dev_err(dev, "failed to map irq\n");
  2066. err = -EINVAL;
  2067. goto err_out;
  2068. }
  2069. /* get the irq line */
  2070. err = request_irq(priv->irq, talitos_interrupt, 0,
  2071. dev_driver_string(dev), dev);
  2072. if (err) {
  2073. dev_err(dev, "failed to request irq %d\n", priv->irq);
  2074. irq_dispose_mapping(priv->irq);
  2075. priv->irq = NO_IRQ;
  2076. goto err_out;
  2077. }
  2078. priv->reg = of_iomap(np, 0);
  2079. if (!priv->reg) {
  2080. dev_err(dev, "failed to of_iomap\n");
  2081. err = -ENOMEM;
  2082. goto err_out;
  2083. }
  2084. /* get SEC version capabilities from device tree */
  2085. prop = of_get_property(np, "fsl,num-channels", NULL);
  2086. if (prop)
  2087. priv->num_channels = *prop;
  2088. prop = of_get_property(np, "fsl,channel-fifo-len", NULL);
  2089. if (prop)
  2090. priv->chfifo_len = *prop;
  2091. prop = of_get_property(np, "fsl,exec-units-mask", NULL);
  2092. if (prop)
  2093. priv->exec_units = *prop;
  2094. prop = of_get_property(np, "fsl,descriptor-types-mask", NULL);
  2095. if (prop)
  2096. priv->desc_types = *prop;
  2097. if (!is_power_of_2(priv->num_channels) || !priv->chfifo_len ||
  2098. !priv->exec_units || !priv->desc_types) {
  2099. dev_err(dev, "invalid property data in device tree node\n");
  2100. err = -EINVAL;
  2101. goto err_out;
  2102. }
  2103. if (of_device_is_compatible(np, "fsl,sec3.0"))
  2104. priv->features |= TALITOS_FTR_SRC_LINK_TBL_LEN_INCLUDES_EXTENT;
  2105. if (of_device_is_compatible(np, "fsl,sec2.1"))
  2106. priv->features |= TALITOS_FTR_HW_AUTH_CHECK |
  2107. TALITOS_FTR_SHA224_HWINIT;
  2108. priv->chan = kzalloc(sizeof(struct talitos_channel) *
  2109. priv->num_channels, GFP_KERNEL);
  2110. if (!priv->chan) {
  2111. dev_err(dev, "failed to allocate channel management space\n");
  2112. err = -ENOMEM;
  2113. goto err_out;
  2114. }
  2115. for (i = 0; i < priv->num_channels; i++) {
  2116. spin_lock_init(&priv->chan[i].head_lock);
  2117. spin_lock_init(&priv->chan[i].tail_lock);
  2118. }
  2119. priv->fifo_len = roundup_pow_of_two(priv->chfifo_len);
  2120. for (i = 0; i < priv->num_channels; i++) {
  2121. priv->chan[i].fifo = kzalloc(sizeof(struct talitos_request) *
  2122. priv->fifo_len, GFP_KERNEL);
  2123. if (!priv->chan[i].fifo) {
  2124. dev_err(dev, "failed to allocate request fifo %d\n", i);
  2125. err = -ENOMEM;
  2126. goto err_out;
  2127. }
  2128. }
  2129. for (i = 0; i < priv->num_channels; i++)
  2130. atomic_set(&priv->chan[i].submit_count,
  2131. -(priv->chfifo_len - 1));
  2132. dma_set_mask(dev, DMA_BIT_MASK(36));
  2133. /* reset and initialize the h/w */
  2134. err = init_device(dev);
  2135. if (err) {
  2136. dev_err(dev, "failed to initialize device\n");
  2137. goto err_out;
  2138. }
  2139. /* register the RNG, if available */
  2140. if (hw_supports(dev, DESC_HDR_SEL0_RNG)) {
  2141. err = talitos_register_rng(dev);
  2142. if (err) {
  2143. dev_err(dev, "failed to register hwrng: %d\n", err);
  2144. goto err_out;
  2145. } else
  2146. dev_info(dev, "hwrng\n");
  2147. }
  2148. /* register crypto algorithms the device supports */
  2149. for (i = 0; i < ARRAY_SIZE(driver_algs); i++) {
  2150. if (hw_supports(dev, driver_algs[i].desc_hdr_template)) {
  2151. struct talitos_crypto_alg *t_alg;
  2152. char *name = NULL;
  2153. t_alg = talitos_alg_alloc(dev, &driver_algs[i]);
  2154. if (IS_ERR(t_alg)) {
  2155. err = PTR_ERR(t_alg);
  2156. goto err_out;
  2157. }
  2158. switch (t_alg->algt.type) {
  2159. case CRYPTO_ALG_TYPE_ABLKCIPHER:
  2160. case CRYPTO_ALG_TYPE_AEAD:
  2161. err = crypto_register_alg(
  2162. &t_alg->algt.alg.crypto);
  2163. name = t_alg->algt.alg.crypto.cra_driver_name;
  2164. break;
  2165. case CRYPTO_ALG_TYPE_AHASH:
  2166. err = crypto_register_ahash(
  2167. &t_alg->algt.alg.hash);
  2168. name =
  2169. t_alg->algt.alg.hash.halg.base.cra_driver_name;
  2170. break;
  2171. }
  2172. if (err) {
  2173. dev_err(dev, "%s alg registration failed\n",
  2174. name);
  2175. kfree(t_alg);
  2176. } else {
  2177. list_add_tail(&t_alg->entry, &priv->alg_list);
  2178. dev_info(dev, "%s\n", name);
  2179. }
  2180. }
  2181. }
  2182. return 0;
  2183. err_out:
  2184. talitos_remove(ofdev);
  2185. return err;
  2186. }
  2187. static const struct of_device_id talitos_match[] = {
  2188. {
  2189. .compatible = "fsl,sec2.0",
  2190. },
  2191. {},
  2192. };
  2193. MODULE_DEVICE_TABLE(of, talitos_match);
  2194. static struct platform_driver talitos_driver = {
  2195. .driver = {
  2196. .name = "talitos",
  2197. .owner = THIS_MODULE,
  2198. .of_match_table = talitos_match,
  2199. },
  2200. .probe = talitos_probe,
  2201. .remove = talitos_remove,
  2202. };
  2203. static int __init talitos_init(void)
  2204. {
  2205. return platform_driver_register(&talitos_driver);
  2206. }
  2207. module_init(talitos_init);
  2208. static void __exit talitos_exit(void)
  2209. {
  2210. platform_driver_unregister(&talitos_driver);
  2211. }
  2212. module_exit(talitos_exit);
  2213. MODULE_LICENSE("GPL");
  2214. MODULE_AUTHOR("Kim Phillips <kim.phillips@freescale.com>");
  2215. MODULE_DESCRIPTION("Freescale integrated security engine (SEC) driver");