atmel-sha.c 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917
  1. /*
  2. * Cryptographic API.
  3. *
  4. * Support for ATMEL SHA1/SHA256 HW acceleration.
  5. *
  6. * Copyright (c) 2012 Eukréa Electromatique - ATMEL
  7. * Author: Nicolas Royer <nicolas@eukrea.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as published
  11. * by the Free Software Foundation.
  12. *
  13. * Some ideas are from omap-sham.c drivers.
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/slab.h>
  18. #include <linux/err.h>
  19. #include <linux/clk.h>
  20. #include <linux/io.h>
  21. #include <linux/hw_random.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/device.h>
  24. #include <linux/init.h>
  25. #include <linux/errno.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/irq.h>
  28. #include <linux/scatterlist.h>
  29. #include <linux/dma-mapping.h>
  30. #include <linux/of_device.h>
  31. #include <linux/delay.h>
  32. #include <linux/crypto.h>
  33. #include <linux/cryptohash.h>
  34. #include <crypto/scatterwalk.h>
  35. #include <crypto/algapi.h>
  36. #include <crypto/sha.h>
  37. #include <crypto/hash.h>
  38. #include <crypto/internal/hash.h>
  39. #include <linux/platform_data/crypto-atmel.h>
  40. #include "atmel-sha-regs.h"
  41. #include "atmel-authenc.h"
  42. /* SHA flags */
  43. #define SHA_FLAGS_BUSY BIT(0)
  44. #define SHA_FLAGS_FINAL BIT(1)
  45. #define SHA_FLAGS_DMA_ACTIVE BIT(2)
  46. #define SHA_FLAGS_OUTPUT_READY BIT(3)
  47. #define SHA_FLAGS_INIT BIT(4)
  48. #define SHA_FLAGS_CPU BIT(5)
  49. #define SHA_FLAGS_DMA_READY BIT(6)
  50. #define SHA_FLAGS_DUMP_REG BIT(7)
  51. /* bits[11:8] are reserved. */
  52. #define SHA_FLAGS_FINUP BIT(16)
  53. #define SHA_FLAGS_SG BIT(17)
  54. #define SHA_FLAGS_ERROR BIT(23)
  55. #define SHA_FLAGS_PAD BIT(24)
  56. #define SHA_FLAGS_RESTORE BIT(25)
  57. #define SHA_FLAGS_IDATAR0 BIT(26)
  58. #define SHA_FLAGS_WAIT_DATARDY BIT(27)
  59. #define SHA_OP_INIT 0
  60. #define SHA_OP_UPDATE 1
  61. #define SHA_OP_FINAL 2
  62. #define SHA_OP_DIGEST 3
  63. #define SHA_BUFFER_LEN (PAGE_SIZE / 16)
  64. #define ATMEL_SHA_DMA_THRESHOLD 56
  65. struct atmel_sha_caps {
  66. bool has_dma;
  67. bool has_dualbuff;
  68. bool has_sha224;
  69. bool has_sha_384_512;
  70. bool has_uihv;
  71. bool has_hmac;
  72. };
  73. struct atmel_sha_dev;
  74. /*
  75. * .statesize = sizeof(struct atmel_sha_reqctx) must be <= PAGE_SIZE / 8 as
  76. * tested by the ahash_prepare_alg() function.
  77. */
  78. struct atmel_sha_reqctx {
  79. struct atmel_sha_dev *dd;
  80. unsigned long flags;
  81. unsigned long op;
  82. u8 digest[SHA512_DIGEST_SIZE] __aligned(sizeof(u32));
  83. u64 digcnt[2];
  84. size_t bufcnt;
  85. size_t buflen;
  86. dma_addr_t dma_addr;
  87. /* walk state */
  88. struct scatterlist *sg;
  89. unsigned int offset; /* offset in current sg */
  90. unsigned int total; /* total request */
  91. size_t block_size;
  92. size_t hash_size;
  93. u8 buffer[SHA_BUFFER_LEN + SHA512_BLOCK_SIZE] __aligned(sizeof(u32));
  94. };
  95. typedef int (*atmel_sha_fn_t)(struct atmel_sha_dev *);
  96. struct atmel_sha_ctx {
  97. struct atmel_sha_dev *dd;
  98. atmel_sha_fn_t start;
  99. unsigned long flags;
  100. };
  101. #define ATMEL_SHA_QUEUE_LENGTH 50
  102. struct atmel_sha_dma {
  103. struct dma_chan *chan;
  104. struct dma_slave_config dma_conf;
  105. struct scatterlist *sg;
  106. int nents;
  107. unsigned int last_sg_length;
  108. };
  109. struct atmel_sha_dev {
  110. struct list_head list;
  111. unsigned long phys_base;
  112. struct device *dev;
  113. struct clk *iclk;
  114. int irq;
  115. void __iomem *io_base;
  116. spinlock_t lock;
  117. int err;
  118. struct tasklet_struct done_task;
  119. struct tasklet_struct queue_task;
  120. unsigned long flags;
  121. struct crypto_queue queue;
  122. struct ahash_request *req;
  123. bool is_async;
  124. bool force_complete;
  125. atmel_sha_fn_t resume;
  126. atmel_sha_fn_t cpu_transfer_complete;
  127. struct atmel_sha_dma dma_lch_in;
  128. struct atmel_sha_caps caps;
  129. struct scatterlist tmp;
  130. u32 hw_version;
  131. };
  132. struct atmel_sha_drv {
  133. struct list_head dev_list;
  134. spinlock_t lock;
  135. };
  136. static struct atmel_sha_drv atmel_sha = {
  137. .dev_list = LIST_HEAD_INIT(atmel_sha.dev_list),
  138. .lock = __SPIN_LOCK_UNLOCKED(atmel_sha.lock),
  139. };
  140. #ifdef VERBOSE_DEBUG
  141. static const char *atmel_sha_reg_name(u32 offset, char *tmp, size_t sz, bool wr)
  142. {
  143. switch (offset) {
  144. case SHA_CR:
  145. return "CR";
  146. case SHA_MR:
  147. return "MR";
  148. case SHA_IER:
  149. return "IER";
  150. case SHA_IDR:
  151. return "IDR";
  152. case SHA_IMR:
  153. return "IMR";
  154. case SHA_ISR:
  155. return "ISR";
  156. case SHA_MSR:
  157. return "MSR";
  158. case SHA_BCR:
  159. return "BCR";
  160. case SHA_REG_DIN(0):
  161. case SHA_REG_DIN(1):
  162. case SHA_REG_DIN(2):
  163. case SHA_REG_DIN(3):
  164. case SHA_REG_DIN(4):
  165. case SHA_REG_DIN(5):
  166. case SHA_REG_DIN(6):
  167. case SHA_REG_DIN(7):
  168. case SHA_REG_DIN(8):
  169. case SHA_REG_DIN(9):
  170. case SHA_REG_DIN(10):
  171. case SHA_REG_DIN(11):
  172. case SHA_REG_DIN(12):
  173. case SHA_REG_DIN(13):
  174. case SHA_REG_DIN(14):
  175. case SHA_REG_DIN(15):
  176. snprintf(tmp, sz, "IDATAR[%u]", (offset - SHA_REG_DIN(0)) >> 2);
  177. break;
  178. case SHA_REG_DIGEST(0):
  179. case SHA_REG_DIGEST(1):
  180. case SHA_REG_DIGEST(2):
  181. case SHA_REG_DIGEST(3):
  182. case SHA_REG_DIGEST(4):
  183. case SHA_REG_DIGEST(5):
  184. case SHA_REG_DIGEST(6):
  185. case SHA_REG_DIGEST(7):
  186. case SHA_REG_DIGEST(8):
  187. case SHA_REG_DIGEST(9):
  188. case SHA_REG_DIGEST(10):
  189. case SHA_REG_DIGEST(11):
  190. case SHA_REG_DIGEST(12):
  191. case SHA_REG_DIGEST(13):
  192. case SHA_REG_DIGEST(14):
  193. case SHA_REG_DIGEST(15):
  194. if (wr)
  195. snprintf(tmp, sz, "IDATAR[%u]",
  196. 16u + ((offset - SHA_REG_DIGEST(0)) >> 2));
  197. else
  198. snprintf(tmp, sz, "ODATAR[%u]",
  199. (offset - SHA_REG_DIGEST(0)) >> 2);
  200. break;
  201. case SHA_HW_VERSION:
  202. return "HWVER";
  203. default:
  204. snprintf(tmp, sz, "0x%02x", offset);
  205. break;
  206. }
  207. return tmp;
  208. }
  209. #endif /* VERBOSE_DEBUG */
  210. static inline u32 atmel_sha_read(struct atmel_sha_dev *dd, u32 offset)
  211. {
  212. u32 value = readl_relaxed(dd->io_base + offset);
  213. #ifdef VERBOSE_DEBUG
  214. if (dd->flags & SHA_FLAGS_DUMP_REG) {
  215. char tmp[16];
  216. dev_vdbg(dd->dev, "read 0x%08x from %s\n", value,
  217. atmel_sha_reg_name(offset, tmp, sizeof(tmp), false));
  218. }
  219. #endif /* VERBOSE_DEBUG */
  220. return value;
  221. }
  222. static inline void atmel_sha_write(struct atmel_sha_dev *dd,
  223. u32 offset, u32 value)
  224. {
  225. #ifdef VERBOSE_DEBUG
  226. if (dd->flags & SHA_FLAGS_DUMP_REG) {
  227. char tmp[16];
  228. dev_vdbg(dd->dev, "write 0x%08x into %s\n", value,
  229. atmel_sha_reg_name(offset, tmp, sizeof(tmp), true));
  230. }
  231. #endif /* VERBOSE_DEBUG */
  232. writel_relaxed(value, dd->io_base + offset);
  233. }
  234. static inline int atmel_sha_complete(struct atmel_sha_dev *dd, int err)
  235. {
  236. struct ahash_request *req = dd->req;
  237. dd->flags &= ~(SHA_FLAGS_BUSY | SHA_FLAGS_FINAL | SHA_FLAGS_CPU |
  238. SHA_FLAGS_DMA_READY | SHA_FLAGS_OUTPUT_READY |
  239. SHA_FLAGS_DUMP_REG);
  240. clk_disable(dd->iclk);
  241. if ((dd->is_async || dd->force_complete) && req->base.complete)
  242. req->base.complete(&req->base, err);
  243. /* handle new request */
  244. tasklet_schedule(&dd->queue_task);
  245. return err;
  246. }
  247. static size_t atmel_sha_append_sg(struct atmel_sha_reqctx *ctx)
  248. {
  249. size_t count;
  250. while ((ctx->bufcnt < ctx->buflen) && ctx->total) {
  251. count = min(ctx->sg->length - ctx->offset, ctx->total);
  252. count = min(count, ctx->buflen - ctx->bufcnt);
  253. if (count <= 0) {
  254. /*
  255. * Check if count <= 0 because the buffer is full or
  256. * because the sg length is 0. In the latest case,
  257. * check if there is another sg in the list, a 0 length
  258. * sg doesn't necessarily mean the end of the sg list.
  259. */
  260. if ((ctx->sg->length == 0) && !sg_is_last(ctx->sg)) {
  261. ctx->sg = sg_next(ctx->sg);
  262. continue;
  263. } else {
  264. break;
  265. }
  266. }
  267. scatterwalk_map_and_copy(ctx->buffer + ctx->bufcnt, ctx->sg,
  268. ctx->offset, count, 0);
  269. ctx->bufcnt += count;
  270. ctx->offset += count;
  271. ctx->total -= count;
  272. if (ctx->offset == ctx->sg->length) {
  273. ctx->sg = sg_next(ctx->sg);
  274. if (ctx->sg)
  275. ctx->offset = 0;
  276. else
  277. ctx->total = 0;
  278. }
  279. }
  280. return 0;
  281. }
  282. /*
  283. * The purpose of this padding is to ensure that the padded message is a
  284. * multiple of 512 bits (SHA1/SHA224/SHA256) or 1024 bits (SHA384/SHA512).
  285. * The bit "1" is appended at the end of the message followed by
  286. * "padlen-1" zero bits. Then a 64 bits block (SHA1/SHA224/SHA256) or
  287. * 128 bits block (SHA384/SHA512) equals to the message length in bits
  288. * is appended.
  289. *
  290. * For SHA1/SHA224/SHA256, padlen is calculated as followed:
  291. * - if message length < 56 bytes then padlen = 56 - message length
  292. * - else padlen = 64 + 56 - message length
  293. *
  294. * For SHA384/SHA512, padlen is calculated as followed:
  295. * - if message length < 112 bytes then padlen = 112 - message length
  296. * - else padlen = 128 + 112 - message length
  297. */
  298. static void atmel_sha_fill_padding(struct atmel_sha_reqctx *ctx, int length)
  299. {
  300. unsigned int index, padlen;
  301. u64 bits[2];
  302. u64 size[2];
  303. size[0] = ctx->digcnt[0];
  304. size[1] = ctx->digcnt[1];
  305. size[0] += ctx->bufcnt;
  306. if (size[0] < ctx->bufcnt)
  307. size[1]++;
  308. size[0] += length;
  309. if (size[0] < length)
  310. size[1]++;
  311. bits[1] = cpu_to_be64(size[0] << 3);
  312. bits[0] = cpu_to_be64(size[1] << 3 | size[0] >> 61);
  313. switch (ctx->flags & SHA_FLAGS_ALGO_MASK) {
  314. case SHA_FLAGS_SHA384:
  315. case SHA_FLAGS_SHA512:
  316. index = ctx->bufcnt & 0x7f;
  317. padlen = (index < 112) ? (112 - index) : ((128+112) - index);
  318. *(ctx->buffer + ctx->bufcnt) = 0x80;
  319. memset(ctx->buffer + ctx->bufcnt + 1, 0, padlen-1);
  320. memcpy(ctx->buffer + ctx->bufcnt + padlen, bits, 16);
  321. ctx->bufcnt += padlen + 16;
  322. ctx->flags |= SHA_FLAGS_PAD;
  323. break;
  324. default:
  325. index = ctx->bufcnt & 0x3f;
  326. padlen = (index < 56) ? (56 - index) : ((64+56) - index);
  327. *(ctx->buffer + ctx->bufcnt) = 0x80;
  328. memset(ctx->buffer + ctx->bufcnt + 1, 0, padlen-1);
  329. memcpy(ctx->buffer + ctx->bufcnt + padlen, &bits[1], 8);
  330. ctx->bufcnt += padlen + 8;
  331. ctx->flags |= SHA_FLAGS_PAD;
  332. break;
  333. }
  334. }
  335. static struct atmel_sha_dev *atmel_sha_find_dev(struct atmel_sha_ctx *tctx)
  336. {
  337. struct atmel_sha_dev *dd = NULL;
  338. struct atmel_sha_dev *tmp;
  339. spin_lock_bh(&atmel_sha.lock);
  340. if (!tctx->dd) {
  341. list_for_each_entry(tmp, &atmel_sha.dev_list, list) {
  342. dd = tmp;
  343. break;
  344. }
  345. tctx->dd = dd;
  346. } else {
  347. dd = tctx->dd;
  348. }
  349. spin_unlock_bh(&atmel_sha.lock);
  350. return dd;
  351. }
  352. static int atmel_sha_init(struct ahash_request *req)
  353. {
  354. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  355. struct atmel_sha_ctx *tctx = crypto_ahash_ctx(tfm);
  356. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  357. struct atmel_sha_dev *dd = atmel_sha_find_dev(tctx);
  358. ctx->dd = dd;
  359. ctx->flags = 0;
  360. dev_dbg(dd->dev, "init: digest size: %d\n",
  361. crypto_ahash_digestsize(tfm));
  362. switch (crypto_ahash_digestsize(tfm)) {
  363. case SHA1_DIGEST_SIZE:
  364. ctx->flags |= SHA_FLAGS_SHA1;
  365. ctx->block_size = SHA1_BLOCK_SIZE;
  366. break;
  367. case SHA224_DIGEST_SIZE:
  368. ctx->flags |= SHA_FLAGS_SHA224;
  369. ctx->block_size = SHA224_BLOCK_SIZE;
  370. break;
  371. case SHA256_DIGEST_SIZE:
  372. ctx->flags |= SHA_FLAGS_SHA256;
  373. ctx->block_size = SHA256_BLOCK_SIZE;
  374. break;
  375. case SHA384_DIGEST_SIZE:
  376. ctx->flags |= SHA_FLAGS_SHA384;
  377. ctx->block_size = SHA384_BLOCK_SIZE;
  378. break;
  379. case SHA512_DIGEST_SIZE:
  380. ctx->flags |= SHA_FLAGS_SHA512;
  381. ctx->block_size = SHA512_BLOCK_SIZE;
  382. break;
  383. default:
  384. return -EINVAL;
  385. break;
  386. }
  387. ctx->bufcnt = 0;
  388. ctx->digcnt[0] = 0;
  389. ctx->digcnt[1] = 0;
  390. ctx->buflen = SHA_BUFFER_LEN;
  391. return 0;
  392. }
  393. static void atmel_sha_write_ctrl(struct atmel_sha_dev *dd, int dma)
  394. {
  395. struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
  396. u32 valmr = SHA_MR_MODE_AUTO;
  397. unsigned int i, hashsize = 0;
  398. if (likely(dma)) {
  399. if (!dd->caps.has_dma)
  400. atmel_sha_write(dd, SHA_IER, SHA_INT_TXBUFE);
  401. valmr = SHA_MR_MODE_PDC;
  402. if (dd->caps.has_dualbuff)
  403. valmr |= SHA_MR_DUALBUFF;
  404. } else {
  405. atmel_sha_write(dd, SHA_IER, SHA_INT_DATARDY);
  406. }
  407. switch (ctx->flags & SHA_FLAGS_ALGO_MASK) {
  408. case SHA_FLAGS_SHA1:
  409. valmr |= SHA_MR_ALGO_SHA1;
  410. hashsize = SHA1_DIGEST_SIZE;
  411. break;
  412. case SHA_FLAGS_SHA224:
  413. valmr |= SHA_MR_ALGO_SHA224;
  414. hashsize = SHA256_DIGEST_SIZE;
  415. break;
  416. case SHA_FLAGS_SHA256:
  417. valmr |= SHA_MR_ALGO_SHA256;
  418. hashsize = SHA256_DIGEST_SIZE;
  419. break;
  420. case SHA_FLAGS_SHA384:
  421. valmr |= SHA_MR_ALGO_SHA384;
  422. hashsize = SHA512_DIGEST_SIZE;
  423. break;
  424. case SHA_FLAGS_SHA512:
  425. valmr |= SHA_MR_ALGO_SHA512;
  426. hashsize = SHA512_DIGEST_SIZE;
  427. break;
  428. default:
  429. break;
  430. }
  431. /* Setting CR_FIRST only for the first iteration */
  432. if (!(ctx->digcnt[0] || ctx->digcnt[1])) {
  433. atmel_sha_write(dd, SHA_CR, SHA_CR_FIRST);
  434. } else if (dd->caps.has_uihv && (ctx->flags & SHA_FLAGS_RESTORE)) {
  435. const u32 *hash = (const u32 *)ctx->digest;
  436. /*
  437. * Restore the hardware context: update the User Initialize
  438. * Hash Value (UIHV) with the value saved when the latest
  439. * 'update' operation completed on this very same crypto
  440. * request.
  441. */
  442. ctx->flags &= ~SHA_FLAGS_RESTORE;
  443. atmel_sha_write(dd, SHA_CR, SHA_CR_WUIHV);
  444. for (i = 0; i < hashsize / sizeof(u32); ++i)
  445. atmel_sha_write(dd, SHA_REG_DIN(i), hash[i]);
  446. atmel_sha_write(dd, SHA_CR, SHA_CR_FIRST);
  447. valmr |= SHA_MR_UIHV;
  448. }
  449. /*
  450. * WARNING: If the UIHV feature is not available, the hardware CANNOT
  451. * process concurrent requests: the internal registers used to store
  452. * the hash/digest are still set to the partial digest output values
  453. * computed during the latest round.
  454. */
  455. atmel_sha_write(dd, SHA_MR, valmr);
  456. }
  457. static inline int atmel_sha_wait_for_data_ready(struct atmel_sha_dev *dd,
  458. atmel_sha_fn_t resume)
  459. {
  460. u32 isr = atmel_sha_read(dd, SHA_ISR);
  461. if (unlikely(isr & SHA_INT_DATARDY))
  462. return resume(dd);
  463. dd->resume = resume;
  464. atmel_sha_write(dd, SHA_IER, SHA_INT_DATARDY);
  465. return -EINPROGRESS;
  466. }
  467. static int atmel_sha_xmit_cpu(struct atmel_sha_dev *dd, const u8 *buf,
  468. size_t length, int final)
  469. {
  470. struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
  471. int count, len32;
  472. const u32 *buffer = (const u32 *)buf;
  473. dev_dbg(dd->dev, "xmit_cpu: digcnt: 0x%llx 0x%llx, length: %zd, final: %d\n",
  474. ctx->digcnt[1], ctx->digcnt[0], length, final);
  475. atmel_sha_write_ctrl(dd, 0);
  476. /* should be non-zero before next lines to disable clocks later */
  477. ctx->digcnt[0] += length;
  478. if (ctx->digcnt[0] < length)
  479. ctx->digcnt[1]++;
  480. if (final)
  481. dd->flags |= SHA_FLAGS_FINAL; /* catch last interrupt */
  482. len32 = DIV_ROUND_UP(length, sizeof(u32));
  483. dd->flags |= SHA_FLAGS_CPU;
  484. for (count = 0; count < len32; count++)
  485. atmel_sha_write(dd, SHA_REG_DIN(count), buffer[count]);
  486. return -EINPROGRESS;
  487. }
  488. static int atmel_sha_xmit_pdc(struct atmel_sha_dev *dd, dma_addr_t dma_addr1,
  489. size_t length1, dma_addr_t dma_addr2, size_t length2, int final)
  490. {
  491. struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
  492. int len32;
  493. dev_dbg(dd->dev, "xmit_pdc: digcnt: 0x%llx 0x%llx, length: %zd, final: %d\n",
  494. ctx->digcnt[1], ctx->digcnt[0], length1, final);
  495. len32 = DIV_ROUND_UP(length1, sizeof(u32));
  496. atmel_sha_write(dd, SHA_PTCR, SHA_PTCR_TXTDIS);
  497. atmel_sha_write(dd, SHA_TPR, dma_addr1);
  498. atmel_sha_write(dd, SHA_TCR, len32);
  499. len32 = DIV_ROUND_UP(length2, sizeof(u32));
  500. atmel_sha_write(dd, SHA_TNPR, dma_addr2);
  501. atmel_sha_write(dd, SHA_TNCR, len32);
  502. atmel_sha_write_ctrl(dd, 1);
  503. /* should be non-zero before next lines to disable clocks later */
  504. ctx->digcnt[0] += length1;
  505. if (ctx->digcnt[0] < length1)
  506. ctx->digcnt[1]++;
  507. if (final)
  508. dd->flags |= SHA_FLAGS_FINAL; /* catch last interrupt */
  509. dd->flags |= SHA_FLAGS_DMA_ACTIVE;
  510. /* Start DMA transfer */
  511. atmel_sha_write(dd, SHA_PTCR, SHA_PTCR_TXTEN);
  512. return -EINPROGRESS;
  513. }
  514. static void atmel_sha_dma_callback(void *data)
  515. {
  516. struct atmel_sha_dev *dd = data;
  517. dd->is_async = true;
  518. /* dma_lch_in - completed - wait DATRDY */
  519. atmel_sha_write(dd, SHA_IER, SHA_INT_DATARDY);
  520. }
  521. static int atmel_sha_xmit_dma(struct atmel_sha_dev *dd, dma_addr_t dma_addr1,
  522. size_t length1, dma_addr_t dma_addr2, size_t length2, int final)
  523. {
  524. struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
  525. struct dma_async_tx_descriptor *in_desc;
  526. struct scatterlist sg[2];
  527. dev_dbg(dd->dev, "xmit_dma: digcnt: 0x%llx 0x%llx, length: %zd, final: %d\n",
  528. ctx->digcnt[1], ctx->digcnt[0], length1, final);
  529. dd->dma_lch_in.dma_conf.src_maxburst = 16;
  530. dd->dma_lch_in.dma_conf.dst_maxburst = 16;
  531. dmaengine_slave_config(dd->dma_lch_in.chan, &dd->dma_lch_in.dma_conf);
  532. if (length2) {
  533. sg_init_table(sg, 2);
  534. sg_dma_address(&sg[0]) = dma_addr1;
  535. sg_dma_len(&sg[0]) = length1;
  536. sg_dma_address(&sg[1]) = dma_addr2;
  537. sg_dma_len(&sg[1]) = length2;
  538. in_desc = dmaengine_prep_slave_sg(dd->dma_lch_in.chan, sg, 2,
  539. DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  540. } else {
  541. sg_init_table(sg, 1);
  542. sg_dma_address(&sg[0]) = dma_addr1;
  543. sg_dma_len(&sg[0]) = length1;
  544. in_desc = dmaengine_prep_slave_sg(dd->dma_lch_in.chan, sg, 1,
  545. DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  546. }
  547. if (!in_desc)
  548. return atmel_sha_complete(dd, -EINVAL);
  549. in_desc->callback = atmel_sha_dma_callback;
  550. in_desc->callback_param = dd;
  551. atmel_sha_write_ctrl(dd, 1);
  552. /* should be non-zero before next lines to disable clocks later */
  553. ctx->digcnt[0] += length1;
  554. if (ctx->digcnt[0] < length1)
  555. ctx->digcnt[1]++;
  556. if (final)
  557. dd->flags |= SHA_FLAGS_FINAL; /* catch last interrupt */
  558. dd->flags |= SHA_FLAGS_DMA_ACTIVE;
  559. /* Start DMA transfer */
  560. dmaengine_submit(in_desc);
  561. dma_async_issue_pending(dd->dma_lch_in.chan);
  562. return -EINPROGRESS;
  563. }
  564. static int atmel_sha_xmit_start(struct atmel_sha_dev *dd, dma_addr_t dma_addr1,
  565. size_t length1, dma_addr_t dma_addr2, size_t length2, int final)
  566. {
  567. if (dd->caps.has_dma)
  568. return atmel_sha_xmit_dma(dd, dma_addr1, length1,
  569. dma_addr2, length2, final);
  570. else
  571. return atmel_sha_xmit_pdc(dd, dma_addr1, length1,
  572. dma_addr2, length2, final);
  573. }
  574. static int atmel_sha_update_cpu(struct atmel_sha_dev *dd)
  575. {
  576. struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
  577. int bufcnt;
  578. atmel_sha_append_sg(ctx);
  579. atmel_sha_fill_padding(ctx, 0);
  580. bufcnt = ctx->bufcnt;
  581. ctx->bufcnt = 0;
  582. return atmel_sha_xmit_cpu(dd, ctx->buffer, bufcnt, 1);
  583. }
  584. static int atmel_sha_xmit_dma_map(struct atmel_sha_dev *dd,
  585. struct atmel_sha_reqctx *ctx,
  586. size_t length, int final)
  587. {
  588. ctx->dma_addr = dma_map_single(dd->dev, ctx->buffer,
  589. ctx->buflen + ctx->block_size, DMA_TO_DEVICE);
  590. if (dma_mapping_error(dd->dev, ctx->dma_addr)) {
  591. dev_err(dd->dev, "dma %zu bytes error\n", ctx->buflen +
  592. ctx->block_size);
  593. return atmel_sha_complete(dd, -EINVAL);
  594. }
  595. ctx->flags &= ~SHA_FLAGS_SG;
  596. /* next call does not fail... so no unmap in the case of error */
  597. return atmel_sha_xmit_start(dd, ctx->dma_addr, length, 0, 0, final);
  598. }
  599. static int atmel_sha_update_dma_slow(struct atmel_sha_dev *dd)
  600. {
  601. struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
  602. unsigned int final;
  603. size_t count;
  604. atmel_sha_append_sg(ctx);
  605. final = (ctx->flags & SHA_FLAGS_FINUP) && !ctx->total;
  606. dev_dbg(dd->dev, "slow: bufcnt: %zu, digcnt: 0x%llx 0x%llx, final: %d\n",
  607. ctx->bufcnt, ctx->digcnt[1], ctx->digcnt[0], final);
  608. if (final)
  609. atmel_sha_fill_padding(ctx, 0);
  610. if (final || (ctx->bufcnt == ctx->buflen)) {
  611. count = ctx->bufcnt;
  612. ctx->bufcnt = 0;
  613. return atmel_sha_xmit_dma_map(dd, ctx, count, final);
  614. }
  615. return 0;
  616. }
  617. static int atmel_sha_update_dma_start(struct atmel_sha_dev *dd)
  618. {
  619. struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
  620. unsigned int length, final, tail;
  621. struct scatterlist *sg;
  622. unsigned int count;
  623. if (!ctx->total)
  624. return 0;
  625. if (ctx->bufcnt || ctx->offset)
  626. return atmel_sha_update_dma_slow(dd);
  627. dev_dbg(dd->dev, "fast: digcnt: 0x%llx 0x%llx, bufcnt: %zd, total: %u\n",
  628. ctx->digcnt[1], ctx->digcnt[0], ctx->bufcnt, ctx->total);
  629. sg = ctx->sg;
  630. if (!IS_ALIGNED(sg->offset, sizeof(u32)))
  631. return atmel_sha_update_dma_slow(dd);
  632. if (!sg_is_last(sg) && !IS_ALIGNED(sg->length, ctx->block_size))
  633. /* size is not ctx->block_size aligned */
  634. return atmel_sha_update_dma_slow(dd);
  635. length = min(ctx->total, sg->length);
  636. if (sg_is_last(sg)) {
  637. if (!(ctx->flags & SHA_FLAGS_FINUP)) {
  638. /* not last sg must be ctx->block_size aligned */
  639. tail = length & (ctx->block_size - 1);
  640. length -= tail;
  641. }
  642. }
  643. ctx->total -= length;
  644. ctx->offset = length; /* offset where to start slow */
  645. final = (ctx->flags & SHA_FLAGS_FINUP) && !ctx->total;
  646. /* Add padding */
  647. if (final) {
  648. tail = length & (ctx->block_size - 1);
  649. length -= tail;
  650. ctx->total += tail;
  651. ctx->offset = length; /* offset where to start slow */
  652. sg = ctx->sg;
  653. atmel_sha_append_sg(ctx);
  654. atmel_sha_fill_padding(ctx, length);
  655. ctx->dma_addr = dma_map_single(dd->dev, ctx->buffer,
  656. ctx->buflen + ctx->block_size, DMA_TO_DEVICE);
  657. if (dma_mapping_error(dd->dev, ctx->dma_addr)) {
  658. dev_err(dd->dev, "dma %zu bytes error\n",
  659. ctx->buflen + ctx->block_size);
  660. return atmel_sha_complete(dd, -EINVAL);
  661. }
  662. if (length == 0) {
  663. ctx->flags &= ~SHA_FLAGS_SG;
  664. count = ctx->bufcnt;
  665. ctx->bufcnt = 0;
  666. return atmel_sha_xmit_start(dd, ctx->dma_addr, count, 0,
  667. 0, final);
  668. } else {
  669. ctx->sg = sg;
  670. if (!dma_map_sg(dd->dev, ctx->sg, 1,
  671. DMA_TO_DEVICE)) {
  672. dev_err(dd->dev, "dma_map_sg error\n");
  673. return atmel_sha_complete(dd, -EINVAL);
  674. }
  675. ctx->flags |= SHA_FLAGS_SG;
  676. count = ctx->bufcnt;
  677. ctx->bufcnt = 0;
  678. return atmel_sha_xmit_start(dd, sg_dma_address(ctx->sg),
  679. length, ctx->dma_addr, count, final);
  680. }
  681. }
  682. if (!dma_map_sg(dd->dev, ctx->sg, 1, DMA_TO_DEVICE)) {
  683. dev_err(dd->dev, "dma_map_sg error\n");
  684. return atmel_sha_complete(dd, -EINVAL);
  685. }
  686. ctx->flags |= SHA_FLAGS_SG;
  687. /* next call does not fail... so no unmap in the case of error */
  688. return atmel_sha_xmit_start(dd, sg_dma_address(ctx->sg), length, 0,
  689. 0, final);
  690. }
  691. static int atmel_sha_update_dma_stop(struct atmel_sha_dev *dd)
  692. {
  693. struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
  694. if (ctx->flags & SHA_FLAGS_SG) {
  695. dma_unmap_sg(dd->dev, ctx->sg, 1, DMA_TO_DEVICE);
  696. if (ctx->sg->length == ctx->offset) {
  697. ctx->sg = sg_next(ctx->sg);
  698. if (ctx->sg)
  699. ctx->offset = 0;
  700. }
  701. if (ctx->flags & SHA_FLAGS_PAD) {
  702. dma_unmap_single(dd->dev, ctx->dma_addr,
  703. ctx->buflen + ctx->block_size, DMA_TO_DEVICE);
  704. }
  705. } else {
  706. dma_unmap_single(dd->dev, ctx->dma_addr, ctx->buflen +
  707. ctx->block_size, DMA_TO_DEVICE);
  708. }
  709. return 0;
  710. }
  711. static int atmel_sha_update_req(struct atmel_sha_dev *dd)
  712. {
  713. struct ahash_request *req = dd->req;
  714. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  715. int err;
  716. dev_dbg(dd->dev, "update_req: total: %u, digcnt: 0x%llx 0x%llx\n",
  717. ctx->total, ctx->digcnt[1], ctx->digcnt[0]);
  718. if (ctx->flags & SHA_FLAGS_CPU)
  719. err = atmel_sha_update_cpu(dd);
  720. else
  721. err = atmel_sha_update_dma_start(dd);
  722. /* wait for dma completion before can take more data */
  723. dev_dbg(dd->dev, "update: err: %d, digcnt: 0x%llx 0%llx\n",
  724. err, ctx->digcnt[1], ctx->digcnt[0]);
  725. return err;
  726. }
  727. static int atmel_sha_final_req(struct atmel_sha_dev *dd)
  728. {
  729. struct ahash_request *req = dd->req;
  730. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  731. int err = 0;
  732. int count;
  733. if (ctx->bufcnt >= ATMEL_SHA_DMA_THRESHOLD) {
  734. atmel_sha_fill_padding(ctx, 0);
  735. count = ctx->bufcnt;
  736. ctx->bufcnt = 0;
  737. err = atmel_sha_xmit_dma_map(dd, ctx, count, 1);
  738. }
  739. /* faster to handle last block with cpu */
  740. else {
  741. atmel_sha_fill_padding(ctx, 0);
  742. count = ctx->bufcnt;
  743. ctx->bufcnt = 0;
  744. err = atmel_sha_xmit_cpu(dd, ctx->buffer, count, 1);
  745. }
  746. dev_dbg(dd->dev, "final_req: err: %d\n", err);
  747. return err;
  748. }
  749. static void atmel_sha_copy_hash(struct ahash_request *req)
  750. {
  751. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  752. u32 *hash = (u32 *)ctx->digest;
  753. unsigned int i, hashsize;
  754. switch (ctx->flags & SHA_FLAGS_ALGO_MASK) {
  755. case SHA_FLAGS_SHA1:
  756. hashsize = SHA1_DIGEST_SIZE;
  757. break;
  758. case SHA_FLAGS_SHA224:
  759. case SHA_FLAGS_SHA256:
  760. hashsize = SHA256_DIGEST_SIZE;
  761. break;
  762. case SHA_FLAGS_SHA384:
  763. case SHA_FLAGS_SHA512:
  764. hashsize = SHA512_DIGEST_SIZE;
  765. break;
  766. default:
  767. /* Should not happen... */
  768. return;
  769. }
  770. for (i = 0; i < hashsize / sizeof(u32); ++i)
  771. hash[i] = atmel_sha_read(ctx->dd, SHA_REG_DIGEST(i));
  772. ctx->flags |= SHA_FLAGS_RESTORE;
  773. }
  774. static void atmel_sha_copy_ready_hash(struct ahash_request *req)
  775. {
  776. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  777. if (!req->result)
  778. return;
  779. switch (ctx->flags & SHA_FLAGS_ALGO_MASK) {
  780. default:
  781. case SHA_FLAGS_SHA1:
  782. memcpy(req->result, ctx->digest, SHA1_DIGEST_SIZE);
  783. break;
  784. case SHA_FLAGS_SHA224:
  785. memcpy(req->result, ctx->digest, SHA224_DIGEST_SIZE);
  786. break;
  787. case SHA_FLAGS_SHA256:
  788. memcpy(req->result, ctx->digest, SHA256_DIGEST_SIZE);
  789. break;
  790. case SHA_FLAGS_SHA384:
  791. memcpy(req->result, ctx->digest, SHA384_DIGEST_SIZE);
  792. break;
  793. case SHA_FLAGS_SHA512:
  794. memcpy(req->result, ctx->digest, SHA512_DIGEST_SIZE);
  795. break;
  796. }
  797. }
  798. static int atmel_sha_finish(struct ahash_request *req)
  799. {
  800. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  801. struct atmel_sha_dev *dd = ctx->dd;
  802. if (ctx->digcnt[0] || ctx->digcnt[1])
  803. atmel_sha_copy_ready_hash(req);
  804. dev_dbg(dd->dev, "digcnt: 0x%llx 0x%llx, bufcnt: %zd\n", ctx->digcnt[1],
  805. ctx->digcnt[0], ctx->bufcnt);
  806. return 0;
  807. }
  808. static void atmel_sha_finish_req(struct ahash_request *req, int err)
  809. {
  810. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  811. struct atmel_sha_dev *dd = ctx->dd;
  812. if (!err) {
  813. atmel_sha_copy_hash(req);
  814. if (SHA_FLAGS_FINAL & dd->flags)
  815. err = atmel_sha_finish(req);
  816. } else {
  817. ctx->flags |= SHA_FLAGS_ERROR;
  818. }
  819. /* atomic operation is not needed here */
  820. (void)atmel_sha_complete(dd, err);
  821. }
  822. static int atmel_sha_hw_init(struct atmel_sha_dev *dd)
  823. {
  824. int err;
  825. err = clk_enable(dd->iclk);
  826. if (err)
  827. return err;
  828. if (!(SHA_FLAGS_INIT & dd->flags)) {
  829. atmel_sha_write(dd, SHA_CR, SHA_CR_SWRST);
  830. dd->flags |= SHA_FLAGS_INIT;
  831. dd->err = 0;
  832. }
  833. return 0;
  834. }
  835. static inline unsigned int atmel_sha_get_version(struct atmel_sha_dev *dd)
  836. {
  837. return atmel_sha_read(dd, SHA_HW_VERSION) & 0x00000fff;
  838. }
  839. static void atmel_sha_hw_version_init(struct atmel_sha_dev *dd)
  840. {
  841. atmel_sha_hw_init(dd);
  842. dd->hw_version = atmel_sha_get_version(dd);
  843. dev_info(dd->dev,
  844. "version: 0x%x\n", dd->hw_version);
  845. clk_disable(dd->iclk);
  846. }
  847. static int atmel_sha_handle_queue(struct atmel_sha_dev *dd,
  848. struct ahash_request *req)
  849. {
  850. struct crypto_async_request *async_req, *backlog;
  851. struct atmel_sha_ctx *ctx;
  852. unsigned long flags;
  853. bool start_async;
  854. int err = 0, ret = 0;
  855. spin_lock_irqsave(&dd->lock, flags);
  856. if (req)
  857. ret = ahash_enqueue_request(&dd->queue, req);
  858. if (SHA_FLAGS_BUSY & dd->flags) {
  859. spin_unlock_irqrestore(&dd->lock, flags);
  860. return ret;
  861. }
  862. backlog = crypto_get_backlog(&dd->queue);
  863. async_req = crypto_dequeue_request(&dd->queue);
  864. if (async_req)
  865. dd->flags |= SHA_FLAGS_BUSY;
  866. spin_unlock_irqrestore(&dd->lock, flags);
  867. if (!async_req)
  868. return ret;
  869. if (backlog)
  870. backlog->complete(backlog, -EINPROGRESS);
  871. ctx = crypto_tfm_ctx(async_req->tfm);
  872. dd->req = ahash_request_cast(async_req);
  873. start_async = (dd->req != req);
  874. dd->is_async = start_async;
  875. dd->force_complete = false;
  876. /* WARNING: ctx->start() MAY change dd->is_async. */
  877. err = ctx->start(dd);
  878. return (start_async) ? ret : err;
  879. }
  880. static int atmel_sha_done(struct atmel_sha_dev *dd);
  881. static int atmel_sha_start(struct atmel_sha_dev *dd)
  882. {
  883. struct ahash_request *req = dd->req;
  884. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  885. int err;
  886. dev_dbg(dd->dev, "handling new req, op: %lu, nbytes: %d\n",
  887. ctx->op, req->nbytes);
  888. err = atmel_sha_hw_init(dd);
  889. if (err)
  890. return atmel_sha_complete(dd, err);
  891. /*
  892. * atmel_sha_update_req() and atmel_sha_final_req() can return either:
  893. * -EINPROGRESS: the hardware is busy and the SHA driver will resume
  894. * its job later in the done_task.
  895. * This is the main path.
  896. *
  897. * 0: the SHA driver can continue its job then release the hardware
  898. * later, if needed, with atmel_sha_finish_req().
  899. * This is the alternate path.
  900. *
  901. * < 0: an error has occurred so atmel_sha_complete(dd, err) has already
  902. * been called, hence the hardware has been released.
  903. * The SHA driver must stop its job without calling
  904. * atmel_sha_finish_req(), otherwise atmel_sha_complete() would be
  905. * called a second time.
  906. *
  907. * Please note that currently, atmel_sha_final_req() never returns 0.
  908. */
  909. dd->resume = atmel_sha_done;
  910. if (ctx->op == SHA_OP_UPDATE) {
  911. err = atmel_sha_update_req(dd);
  912. if (!err && (ctx->flags & SHA_FLAGS_FINUP))
  913. /* no final() after finup() */
  914. err = atmel_sha_final_req(dd);
  915. } else if (ctx->op == SHA_OP_FINAL) {
  916. err = atmel_sha_final_req(dd);
  917. }
  918. if (!err)
  919. /* done_task will not finish it, so do it here */
  920. atmel_sha_finish_req(req, err);
  921. dev_dbg(dd->dev, "exit, err: %d\n", err);
  922. return err;
  923. }
  924. static int atmel_sha_enqueue(struct ahash_request *req, unsigned int op)
  925. {
  926. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  927. struct atmel_sha_ctx *tctx = crypto_tfm_ctx(req->base.tfm);
  928. struct atmel_sha_dev *dd = tctx->dd;
  929. ctx->op = op;
  930. return atmel_sha_handle_queue(dd, req);
  931. }
  932. static int atmel_sha_update(struct ahash_request *req)
  933. {
  934. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  935. if (!req->nbytes)
  936. return 0;
  937. ctx->total = req->nbytes;
  938. ctx->sg = req->src;
  939. ctx->offset = 0;
  940. if (ctx->flags & SHA_FLAGS_FINUP) {
  941. if (ctx->bufcnt + ctx->total < ATMEL_SHA_DMA_THRESHOLD)
  942. /* faster to use CPU for short transfers */
  943. ctx->flags |= SHA_FLAGS_CPU;
  944. } else if (ctx->bufcnt + ctx->total < ctx->buflen) {
  945. atmel_sha_append_sg(ctx);
  946. return 0;
  947. }
  948. return atmel_sha_enqueue(req, SHA_OP_UPDATE);
  949. }
  950. static int atmel_sha_final(struct ahash_request *req)
  951. {
  952. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  953. ctx->flags |= SHA_FLAGS_FINUP;
  954. if (ctx->flags & SHA_FLAGS_ERROR)
  955. return 0; /* uncompleted hash is not needed */
  956. if (ctx->flags & SHA_FLAGS_PAD)
  957. /* copy ready hash (+ finalize hmac) */
  958. return atmel_sha_finish(req);
  959. return atmel_sha_enqueue(req, SHA_OP_FINAL);
  960. }
  961. static int atmel_sha_finup(struct ahash_request *req)
  962. {
  963. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  964. int err1, err2;
  965. ctx->flags |= SHA_FLAGS_FINUP;
  966. err1 = atmel_sha_update(req);
  967. if (err1 == -EINPROGRESS ||
  968. (err1 == -EBUSY && (ahash_request_flags(req) &
  969. CRYPTO_TFM_REQ_MAY_BACKLOG)))
  970. return err1;
  971. /*
  972. * final() has to be always called to cleanup resources
  973. * even if udpate() failed, except EINPROGRESS
  974. */
  975. err2 = atmel_sha_final(req);
  976. return err1 ?: err2;
  977. }
  978. static int atmel_sha_digest(struct ahash_request *req)
  979. {
  980. return atmel_sha_init(req) ?: atmel_sha_finup(req);
  981. }
  982. static int atmel_sha_export(struct ahash_request *req, void *out)
  983. {
  984. const struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  985. memcpy(out, ctx, sizeof(*ctx));
  986. return 0;
  987. }
  988. static int atmel_sha_import(struct ahash_request *req, const void *in)
  989. {
  990. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  991. memcpy(ctx, in, sizeof(*ctx));
  992. return 0;
  993. }
  994. static int atmel_sha_cra_init(struct crypto_tfm *tfm)
  995. {
  996. struct atmel_sha_ctx *ctx = crypto_tfm_ctx(tfm);
  997. crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
  998. sizeof(struct atmel_sha_reqctx));
  999. ctx->start = atmel_sha_start;
  1000. return 0;
  1001. }
  1002. static struct ahash_alg sha_1_256_algs[] = {
  1003. {
  1004. .init = atmel_sha_init,
  1005. .update = atmel_sha_update,
  1006. .final = atmel_sha_final,
  1007. .finup = atmel_sha_finup,
  1008. .digest = atmel_sha_digest,
  1009. .export = atmel_sha_export,
  1010. .import = atmel_sha_import,
  1011. .halg = {
  1012. .digestsize = SHA1_DIGEST_SIZE,
  1013. .statesize = sizeof(struct atmel_sha_reqctx),
  1014. .base = {
  1015. .cra_name = "sha1",
  1016. .cra_driver_name = "atmel-sha1",
  1017. .cra_priority = 100,
  1018. .cra_flags = CRYPTO_ALG_ASYNC,
  1019. .cra_blocksize = SHA1_BLOCK_SIZE,
  1020. .cra_ctxsize = sizeof(struct atmel_sha_ctx),
  1021. .cra_alignmask = 0,
  1022. .cra_module = THIS_MODULE,
  1023. .cra_init = atmel_sha_cra_init,
  1024. }
  1025. }
  1026. },
  1027. {
  1028. .init = atmel_sha_init,
  1029. .update = atmel_sha_update,
  1030. .final = atmel_sha_final,
  1031. .finup = atmel_sha_finup,
  1032. .digest = atmel_sha_digest,
  1033. .export = atmel_sha_export,
  1034. .import = atmel_sha_import,
  1035. .halg = {
  1036. .digestsize = SHA256_DIGEST_SIZE,
  1037. .statesize = sizeof(struct atmel_sha_reqctx),
  1038. .base = {
  1039. .cra_name = "sha256",
  1040. .cra_driver_name = "atmel-sha256",
  1041. .cra_priority = 100,
  1042. .cra_flags = CRYPTO_ALG_ASYNC,
  1043. .cra_blocksize = SHA256_BLOCK_SIZE,
  1044. .cra_ctxsize = sizeof(struct atmel_sha_ctx),
  1045. .cra_alignmask = 0,
  1046. .cra_module = THIS_MODULE,
  1047. .cra_init = atmel_sha_cra_init,
  1048. }
  1049. }
  1050. },
  1051. };
  1052. static struct ahash_alg sha_224_alg = {
  1053. .init = atmel_sha_init,
  1054. .update = atmel_sha_update,
  1055. .final = atmel_sha_final,
  1056. .finup = atmel_sha_finup,
  1057. .digest = atmel_sha_digest,
  1058. .export = atmel_sha_export,
  1059. .import = atmel_sha_import,
  1060. .halg = {
  1061. .digestsize = SHA224_DIGEST_SIZE,
  1062. .statesize = sizeof(struct atmel_sha_reqctx),
  1063. .base = {
  1064. .cra_name = "sha224",
  1065. .cra_driver_name = "atmel-sha224",
  1066. .cra_priority = 100,
  1067. .cra_flags = CRYPTO_ALG_ASYNC,
  1068. .cra_blocksize = SHA224_BLOCK_SIZE,
  1069. .cra_ctxsize = sizeof(struct atmel_sha_ctx),
  1070. .cra_alignmask = 0,
  1071. .cra_module = THIS_MODULE,
  1072. .cra_init = atmel_sha_cra_init,
  1073. }
  1074. }
  1075. };
  1076. static struct ahash_alg sha_384_512_algs[] = {
  1077. {
  1078. .init = atmel_sha_init,
  1079. .update = atmel_sha_update,
  1080. .final = atmel_sha_final,
  1081. .finup = atmel_sha_finup,
  1082. .digest = atmel_sha_digest,
  1083. .export = atmel_sha_export,
  1084. .import = atmel_sha_import,
  1085. .halg = {
  1086. .digestsize = SHA384_DIGEST_SIZE,
  1087. .statesize = sizeof(struct atmel_sha_reqctx),
  1088. .base = {
  1089. .cra_name = "sha384",
  1090. .cra_driver_name = "atmel-sha384",
  1091. .cra_priority = 100,
  1092. .cra_flags = CRYPTO_ALG_ASYNC,
  1093. .cra_blocksize = SHA384_BLOCK_SIZE,
  1094. .cra_ctxsize = sizeof(struct atmel_sha_ctx),
  1095. .cra_alignmask = 0x3,
  1096. .cra_module = THIS_MODULE,
  1097. .cra_init = atmel_sha_cra_init,
  1098. }
  1099. }
  1100. },
  1101. {
  1102. .init = atmel_sha_init,
  1103. .update = atmel_sha_update,
  1104. .final = atmel_sha_final,
  1105. .finup = atmel_sha_finup,
  1106. .digest = atmel_sha_digest,
  1107. .export = atmel_sha_export,
  1108. .import = atmel_sha_import,
  1109. .halg = {
  1110. .digestsize = SHA512_DIGEST_SIZE,
  1111. .statesize = sizeof(struct atmel_sha_reqctx),
  1112. .base = {
  1113. .cra_name = "sha512",
  1114. .cra_driver_name = "atmel-sha512",
  1115. .cra_priority = 100,
  1116. .cra_flags = CRYPTO_ALG_ASYNC,
  1117. .cra_blocksize = SHA512_BLOCK_SIZE,
  1118. .cra_ctxsize = sizeof(struct atmel_sha_ctx),
  1119. .cra_alignmask = 0x3,
  1120. .cra_module = THIS_MODULE,
  1121. .cra_init = atmel_sha_cra_init,
  1122. }
  1123. }
  1124. },
  1125. };
  1126. static void atmel_sha_queue_task(unsigned long data)
  1127. {
  1128. struct atmel_sha_dev *dd = (struct atmel_sha_dev *)data;
  1129. atmel_sha_handle_queue(dd, NULL);
  1130. }
  1131. static int atmel_sha_done(struct atmel_sha_dev *dd)
  1132. {
  1133. int err = 0;
  1134. if (SHA_FLAGS_CPU & dd->flags) {
  1135. if (SHA_FLAGS_OUTPUT_READY & dd->flags) {
  1136. dd->flags &= ~SHA_FLAGS_OUTPUT_READY;
  1137. goto finish;
  1138. }
  1139. } else if (SHA_FLAGS_DMA_READY & dd->flags) {
  1140. if (SHA_FLAGS_DMA_ACTIVE & dd->flags) {
  1141. dd->flags &= ~SHA_FLAGS_DMA_ACTIVE;
  1142. atmel_sha_update_dma_stop(dd);
  1143. if (dd->err) {
  1144. err = dd->err;
  1145. goto finish;
  1146. }
  1147. }
  1148. if (SHA_FLAGS_OUTPUT_READY & dd->flags) {
  1149. /* hash or semi-hash ready */
  1150. dd->flags &= ~(SHA_FLAGS_DMA_READY |
  1151. SHA_FLAGS_OUTPUT_READY);
  1152. err = atmel_sha_update_dma_start(dd);
  1153. if (err != -EINPROGRESS)
  1154. goto finish;
  1155. }
  1156. }
  1157. return err;
  1158. finish:
  1159. /* finish curent request */
  1160. atmel_sha_finish_req(dd->req, err);
  1161. return err;
  1162. }
  1163. static void atmel_sha_done_task(unsigned long data)
  1164. {
  1165. struct atmel_sha_dev *dd = (struct atmel_sha_dev *)data;
  1166. dd->is_async = true;
  1167. (void)dd->resume(dd);
  1168. }
  1169. static irqreturn_t atmel_sha_irq(int irq, void *dev_id)
  1170. {
  1171. struct atmel_sha_dev *sha_dd = dev_id;
  1172. u32 reg;
  1173. reg = atmel_sha_read(sha_dd, SHA_ISR);
  1174. if (reg & atmel_sha_read(sha_dd, SHA_IMR)) {
  1175. atmel_sha_write(sha_dd, SHA_IDR, reg);
  1176. if (SHA_FLAGS_BUSY & sha_dd->flags) {
  1177. sha_dd->flags |= SHA_FLAGS_OUTPUT_READY;
  1178. if (!(SHA_FLAGS_CPU & sha_dd->flags))
  1179. sha_dd->flags |= SHA_FLAGS_DMA_READY;
  1180. tasklet_schedule(&sha_dd->done_task);
  1181. } else {
  1182. dev_warn(sha_dd->dev, "SHA interrupt when no active requests.\n");
  1183. }
  1184. return IRQ_HANDLED;
  1185. }
  1186. return IRQ_NONE;
  1187. }
  1188. /* DMA transfer functions */
  1189. static bool atmel_sha_dma_check_aligned(struct atmel_sha_dev *dd,
  1190. struct scatterlist *sg,
  1191. size_t len)
  1192. {
  1193. struct atmel_sha_dma *dma = &dd->dma_lch_in;
  1194. struct ahash_request *req = dd->req;
  1195. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  1196. size_t bs = ctx->block_size;
  1197. int nents;
  1198. for (nents = 0; sg; sg = sg_next(sg), ++nents) {
  1199. if (!IS_ALIGNED(sg->offset, sizeof(u32)))
  1200. return false;
  1201. /*
  1202. * This is the last sg, the only one that is allowed to
  1203. * have an unaligned length.
  1204. */
  1205. if (len <= sg->length) {
  1206. dma->nents = nents + 1;
  1207. dma->last_sg_length = sg->length;
  1208. sg->length = ALIGN(len, sizeof(u32));
  1209. return true;
  1210. }
  1211. /* All other sg lengths MUST be aligned to the block size. */
  1212. if (!IS_ALIGNED(sg->length, bs))
  1213. return false;
  1214. len -= sg->length;
  1215. }
  1216. return false;
  1217. }
  1218. static void atmel_sha_dma_callback2(void *data)
  1219. {
  1220. struct atmel_sha_dev *dd = data;
  1221. struct atmel_sha_dma *dma = &dd->dma_lch_in;
  1222. struct scatterlist *sg;
  1223. int nents;
  1224. dmaengine_terminate_all(dma->chan);
  1225. dma_unmap_sg(dd->dev, dma->sg, dma->nents, DMA_TO_DEVICE);
  1226. sg = dma->sg;
  1227. for (nents = 0; nents < dma->nents - 1; ++nents)
  1228. sg = sg_next(sg);
  1229. sg->length = dma->last_sg_length;
  1230. dd->is_async = true;
  1231. (void)atmel_sha_wait_for_data_ready(dd, dd->resume);
  1232. }
  1233. static int atmel_sha_dma_start(struct atmel_sha_dev *dd,
  1234. struct scatterlist *src,
  1235. size_t len,
  1236. atmel_sha_fn_t resume)
  1237. {
  1238. struct atmel_sha_dma *dma = &dd->dma_lch_in;
  1239. struct dma_slave_config *config = &dma->dma_conf;
  1240. struct dma_chan *chan = dma->chan;
  1241. struct dma_async_tx_descriptor *desc;
  1242. dma_cookie_t cookie;
  1243. unsigned int sg_len;
  1244. int err;
  1245. dd->resume = resume;
  1246. /*
  1247. * dma->nents has already been initialized by
  1248. * atmel_sha_dma_check_aligned().
  1249. */
  1250. dma->sg = src;
  1251. sg_len = dma_map_sg(dd->dev, dma->sg, dma->nents, DMA_TO_DEVICE);
  1252. if (!sg_len) {
  1253. err = -ENOMEM;
  1254. goto exit;
  1255. }
  1256. config->src_maxburst = 16;
  1257. config->dst_maxburst = 16;
  1258. err = dmaengine_slave_config(chan, config);
  1259. if (err)
  1260. goto unmap_sg;
  1261. desc = dmaengine_prep_slave_sg(chan, dma->sg, sg_len, DMA_MEM_TO_DEV,
  1262. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  1263. if (!desc) {
  1264. err = -ENOMEM;
  1265. goto unmap_sg;
  1266. }
  1267. desc->callback = atmel_sha_dma_callback2;
  1268. desc->callback_param = dd;
  1269. cookie = dmaengine_submit(desc);
  1270. err = dma_submit_error(cookie);
  1271. if (err)
  1272. goto unmap_sg;
  1273. dma_async_issue_pending(chan);
  1274. return -EINPROGRESS;
  1275. unmap_sg:
  1276. dma_unmap_sg(dd->dev, dma->sg, dma->nents, DMA_TO_DEVICE);
  1277. exit:
  1278. return atmel_sha_complete(dd, err);
  1279. }
  1280. /* CPU transfer functions */
  1281. static int atmel_sha_cpu_transfer(struct atmel_sha_dev *dd)
  1282. {
  1283. struct ahash_request *req = dd->req;
  1284. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  1285. const u32 *words = (const u32 *)ctx->buffer;
  1286. size_t i, num_words;
  1287. u32 isr, din, din_inc;
  1288. din_inc = (ctx->flags & SHA_FLAGS_IDATAR0) ? 0 : 1;
  1289. for (;;) {
  1290. /* Write data into the Input Data Registers. */
  1291. num_words = DIV_ROUND_UP(ctx->bufcnt, sizeof(u32));
  1292. for (i = 0, din = 0; i < num_words; ++i, din += din_inc)
  1293. atmel_sha_write(dd, SHA_REG_DIN(din), words[i]);
  1294. ctx->offset += ctx->bufcnt;
  1295. ctx->total -= ctx->bufcnt;
  1296. if (!ctx->total)
  1297. break;
  1298. /*
  1299. * Prepare next block:
  1300. * Fill ctx->buffer now with the next data to be written into
  1301. * IDATARx: it gives time for the SHA hardware to process
  1302. * the current data so the SHA_INT_DATARDY flag might be set
  1303. * in SHA_ISR when polling this register at the beginning of
  1304. * the next loop.
  1305. */
  1306. ctx->bufcnt = min_t(size_t, ctx->block_size, ctx->total);
  1307. scatterwalk_map_and_copy(ctx->buffer, ctx->sg,
  1308. ctx->offset, ctx->bufcnt, 0);
  1309. /* Wait for hardware to be ready again. */
  1310. isr = atmel_sha_read(dd, SHA_ISR);
  1311. if (!(isr & SHA_INT_DATARDY)) {
  1312. /* Not ready yet. */
  1313. dd->resume = atmel_sha_cpu_transfer;
  1314. atmel_sha_write(dd, SHA_IER, SHA_INT_DATARDY);
  1315. return -EINPROGRESS;
  1316. }
  1317. }
  1318. if (unlikely(!(ctx->flags & SHA_FLAGS_WAIT_DATARDY)))
  1319. return dd->cpu_transfer_complete(dd);
  1320. return atmel_sha_wait_for_data_ready(dd, dd->cpu_transfer_complete);
  1321. }
  1322. static int atmel_sha_cpu_start(struct atmel_sha_dev *dd,
  1323. struct scatterlist *sg,
  1324. unsigned int len,
  1325. bool idatar0_only,
  1326. bool wait_data_ready,
  1327. atmel_sha_fn_t resume)
  1328. {
  1329. struct ahash_request *req = dd->req;
  1330. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  1331. if (!len)
  1332. return resume(dd);
  1333. ctx->flags &= ~(SHA_FLAGS_IDATAR0 | SHA_FLAGS_WAIT_DATARDY);
  1334. if (idatar0_only)
  1335. ctx->flags |= SHA_FLAGS_IDATAR0;
  1336. if (wait_data_ready)
  1337. ctx->flags |= SHA_FLAGS_WAIT_DATARDY;
  1338. ctx->sg = sg;
  1339. ctx->total = len;
  1340. ctx->offset = 0;
  1341. /* Prepare the first block to be written. */
  1342. ctx->bufcnt = min_t(size_t, ctx->block_size, ctx->total);
  1343. scatterwalk_map_and_copy(ctx->buffer, ctx->sg,
  1344. ctx->offset, ctx->bufcnt, 0);
  1345. dd->cpu_transfer_complete = resume;
  1346. return atmel_sha_cpu_transfer(dd);
  1347. }
  1348. static int atmel_sha_cpu_hash(struct atmel_sha_dev *dd,
  1349. const void *data, unsigned int datalen,
  1350. bool auto_padding,
  1351. atmel_sha_fn_t resume)
  1352. {
  1353. struct ahash_request *req = dd->req;
  1354. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  1355. u32 msglen = (auto_padding) ? datalen : 0;
  1356. u32 mr = SHA_MR_MODE_AUTO;
  1357. if (!(IS_ALIGNED(datalen, ctx->block_size) || auto_padding))
  1358. return atmel_sha_complete(dd, -EINVAL);
  1359. mr |= (ctx->flags & SHA_FLAGS_ALGO_MASK);
  1360. atmel_sha_write(dd, SHA_MR, mr);
  1361. atmel_sha_write(dd, SHA_MSR, msglen);
  1362. atmel_sha_write(dd, SHA_BCR, msglen);
  1363. atmel_sha_write(dd, SHA_CR, SHA_CR_FIRST);
  1364. sg_init_one(&dd->tmp, data, datalen);
  1365. return atmel_sha_cpu_start(dd, &dd->tmp, datalen, false, true, resume);
  1366. }
  1367. /* hmac functions */
  1368. struct atmel_sha_hmac_key {
  1369. bool valid;
  1370. unsigned int keylen;
  1371. u8 buffer[SHA512_BLOCK_SIZE];
  1372. u8 *keydup;
  1373. };
  1374. static inline void atmel_sha_hmac_key_init(struct atmel_sha_hmac_key *hkey)
  1375. {
  1376. memset(hkey, 0, sizeof(*hkey));
  1377. }
  1378. static inline void atmel_sha_hmac_key_release(struct atmel_sha_hmac_key *hkey)
  1379. {
  1380. kfree(hkey->keydup);
  1381. memset(hkey, 0, sizeof(*hkey));
  1382. }
  1383. static inline int atmel_sha_hmac_key_set(struct atmel_sha_hmac_key *hkey,
  1384. const u8 *key,
  1385. unsigned int keylen)
  1386. {
  1387. atmel_sha_hmac_key_release(hkey);
  1388. if (keylen > sizeof(hkey->buffer)) {
  1389. hkey->keydup = kmemdup(key, keylen, GFP_KERNEL);
  1390. if (!hkey->keydup)
  1391. return -ENOMEM;
  1392. } else {
  1393. memcpy(hkey->buffer, key, keylen);
  1394. }
  1395. hkey->valid = true;
  1396. hkey->keylen = keylen;
  1397. return 0;
  1398. }
  1399. static inline bool atmel_sha_hmac_key_get(const struct atmel_sha_hmac_key *hkey,
  1400. const u8 **key,
  1401. unsigned int *keylen)
  1402. {
  1403. if (!hkey->valid)
  1404. return false;
  1405. *keylen = hkey->keylen;
  1406. *key = (hkey->keydup) ? hkey->keydup : hkey->buffer;
  1407. return true;
  1408. }
  1409. struct atmel_sha_hmac_ctx {
  1410. struct atmel_sha_ctx base;
  1411. struct atmel_sha_hmac_key hkey;
  1412. u32 ipad[SHA512_BLOCK_SIZE / sizeof(u32)];
  1413. u32 opad[SHA512_BLOCK_SIZE / sizeof(u32)];
  1414. atmel_sha_fn_t resume;
  1415. };
  1416. static int atmel_sha_hmac_setup(struct atmel_sha_dev *dd,
  1417. atmel_sha_fn_t resume);
  1418. static int atmel_sha_hmac_prehash_key(struct atmel_sha_dev *dd,
  1419. const u8 *key, unsigned int keylen);
  1420. static int atmel_sha_hmac_prehash_key_done(struct atmel_sha_dev *dd);
  1421. static int atmel_sha_hmac_compute_ipad_hash(struct atmel_sha_dev *dd);
  1422. static int atmel_sha_hmac_compute_opad_hash(struct atmel_sha_dev *dd);
  1423. static int atmel_sha_hmac_setup_done(struct atmel_sha_dev *dd);
  1424. static int atmel_sha_hmac_init_done(struct atmel_sha_dev *dd);
  1425. static int atmel_sha_hmac_final(struct atmel_sha_dev *dd);
  1426. static int atmel_sha_hmac_final_done(struct atmel_sha_dev *dd);
  1427. static int atmel_sha_hmac_digest2(struct atmel_sha_dev *dd);
  1428. static int atmel_sha_hmac_setup(struct atmel_sha_dev *dd,
  1429. atmel_sha_fn_t resume)
  1430. {
  1431. struct ahash_request *req = dd->req;
  1432. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  1433. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  1434. struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
  1435. unsigned int keylen;
  1436. const u8 *key;
  1437. size_t bs;
  1438. hmac->resume = resume;
  1439. switch (ctx->flags & SHA_FLAGS_ALGO_MASK) {
  1440. case SHA_FLAGS_SHA1:
  1441. ctx->block_size = SHA1_BLOCK_SIZE;
  1442. ctx->hash_size = SHA1_DIGEST_SIZE;
  1443. break;
  1444. case SHA_FLAGS_SHA224:
  1445. ctx->block_size = SHA224_BLOCK_SIZE;
  1446. ctx->hash_size = SHA256_DIGEST_SIZE;
  1447. break;
  1448. case SHA_FLAGS_SHA256:
  1449. ctx->block_size = SHA256_BLOCK_SIZE;
  1450. ctx->hash_size = SHA256_DIGEST_SIZE;
  1451. break;
  1452. case SHA_FLAGS_SHA384:
  1453. ctx->block_size = SHA384_BLOCK_SIZE;
  1454. ctx->hash_size = SHA512_DIGEST_SIZE;
  1455. break;
  1456. case SHA_FLAGS_SHA512:
  1457. ctx->block_size = SHA512_BLOCK_SIZE;
  1458. ctx->hash_size = SHA512_DIGEST_SIZE;
  1459. break;
  1460. default:
  1461. return atmel_sha_complete(dd, -EINVAL);
  1462. }
  1463. bs = ctx->block_size;
  1464. if (likely(!atmel_sha_hmac_key_get(&hmac->hkey, &key, &keylen)))
  1465. return resume(dd);
  1466. /* Compute K' from K. */
  1467. if (unlikely(keylen > bs))
  1468. return atmel_sha_hmac_prehash_key(dd, key, keylen);
  1469. /* Prepare ipad. */
  1470. memcpy((u8 *)hmac->ipad, key, keylen);
  1471. memset((u8 *)hmac->ipad + keylen, 0, bs - keylen);
  1472. return atmel_sha_hmac_compute_ipad_hash(dd);
  1473. }
  1474. static int atmel_sha_hmac_prehash_key(struct atmel_sha_dev *dd,
  1475. const u8 *key, unsigned int keylen)
  1476. {
  1477. return atmel_sha_cpu_hash(dd, key, keylen, true,
  1478. atmel_sha_hmac_prehash_key_done);
  1479. }
  1480. static int atmel_sha_hmac_prehash_key_done(struct atmel_sha_dev *dd)
  1481. {
  1482. struct ahash_request *req = dd->req;
  1483. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  1484. struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
  1485. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  1486. size_t ds = crypto_ahash_digestsize(tfm);
  1487. size_t bs = ctx->block_size;
  1488. size_t i, num_words = ds / sizeof(u32);
  1489. /* Prepare ipad. */
  1490. for (i = 0; i < num_words; ++i)
  1491. hmac->ipad[i] = atmel_sha_read(dd, SHA_REG_DIGEST(i));
  1492. memset((u8 *)hmac->ipad + ds, 0, bs - ds);
  1493. return atmel_sha_hmac_compute_ipad_hash(dd);
  1494. }
  1495. static int atmel_sha_hmac_compute_ipad_hash(struct atmel_sha_dev *dd)
  1496. {
  1497. struct ahash_request *req = dd->req;
  1498. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  1499. struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
  1500. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  1501. size_t bs = ctx->block_size;
  1502. size_t i, num_words = bs / sizeof(u32);
  1503. memcpy(hmac->opad, hmac->ipad, bs);
  1504. for (i = 0; i < num_words; ++i) {
  1505. hmac->ipad[i] ^= 0x36363636;
  1506. hmac->opad[i] ^= 0x5c5c5c5c;
  1507. }
  1508. return atmel_sha_cpu_hash(dd, hmac->ipad, bs, false,
  1509. atmel_sha_hmac_compute_opad_hash);
  1510. }
  1511. static int atmel_sha_hmac_compute_opad_hash(struct atmel_sha_dev *dd)
  1512. {
  1513. struct ahash_request *req = dd->req;
  1514. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  1515. struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
  1516. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  1517. size_t bs = ctx->block_size;
  1518. size_t hs = ctx->hash_size;
  1519. size_t i, num_words = hs / sizeof(u32);
  1520. for (i = 0; i < num_words; ++i)
  1521. hmac->ipad[i] = atmel_sha_read(dd, SHA_REG_DIGEST(i));
  1522. return atmel_sha_cpu_hash(dd, hmac->opad, bs, false,
  1523. atmel_sha_hmac_setup_done);
  1524. }
  1525. static int atmel_sha_hmac_setup_done(struct atmel_sha_dev *dd)
  1526. {
  1527. struct ahash_request *req = dd->req;
  1528. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  1529. struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
  1530. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  1531. size_t hs = ctx->hash_size;
  1532. size_t i, num_words = hs / sizeof(u32);
  1533. for (i = 0; i < num_words; ++i)
  1534. hmac->opad[i] = atmel_sha_read(dd, SHA_REG_DIGEST(i));
  1535. atmel_sha_hmac_key_release(&hmac->hkey);
  1536. return hmac->resume(dd);
  1537. }
  1538. static int atmel_sha_hmac_start(struct atmel_sha_dev *dd)
  1539. {
  1540. struct ahash_request *req = dd->req;
  1541. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  1542. int err;
  1543. err = atmel_sha_hw_init(dd);
  1544. if (err)
  1545. return atmel_sha_complete(dd, err);
  1546. switch (ctx->op) {
  1547. case SHA_OP_INIT:
  1548. err = atmel_sha_hmac_setup(dd, atmel_sha_hmac_init_done);
  1549. break;
  1550. case SHA_OP_UPDATE:
  1551. dd->resume = atmel_sha_done;
  1552. err = atmel_sha_update_req(dd);
  1553. break;
  1554. case SHA_OP_FINAL:
  1555. dd->resume = atmel_sha_hmac_final;
  1556. err = atmel_sha_final_req(dd);
  1557. break;
  1558. case SHA_OP_DIGEST:
  1559. err = atmel_sha_hmac_setup(dd, atmel_sha_hmac_digest2);
  1560. break;
  1561. default:
  1562. return atmel_sha_complete(dd, -EINVAL);
  1563. }
  1564. return err;
  1565. }
  1566. static int atmel_sha_hmac_setkey(struct crypto_ahash *tfm, const u8 *key,
  1567. unsigned int keylen)
  1568. {
  1569. struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
  1570. return atmel_sha_hmac_key_set(&hmac->hkey, key, keylen);
  1571. }
  1572. static int atmel_sha_hmac_init(struct ahash_request *req)
  1573. {
  1574. int err;
  1575. err = atmel_sha_init(req);
  1576. if (err)
  1577. return err;
  1578. return atmel_sha_enqueue(req, SHA_OP_INIT);
  1579. }
  1580. static int atmel_sha_hmac_init_done(struct atmel_sha_dev *dd)
  1581. {
  1582. struct ahash_request *req = dd->req;
  1583. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  1584. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  1585. struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
  1586. size_t bs = ctx->block_size;
  1587. size_t hs = ctx->hash_size;
  1588. ctx->bufcnt = 0;
  1589. ctx->digcnt[0] = bs;
  1590. ctx->digcnt[1] = 0;
  1591. ctx->flags |= SHA_FLAGS_RESTORE;
  1592. memcpy(ctx->digest, hmac->ipad, hs);
  1593. return atmel_sha_complete(dd, 0);
  1594. }
  1595. static int atmel_sha_hmac_final(struct atmel_sha_dev *dd)
  1596. {
  1597. struct ahash_request *req = dd->req;
  1598. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  1599. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  1600. struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
  1601. u32 *digest = (u32 *)ctx->digest;
  1602. size_t ds = crypto_ahash_digestsize(tfm);
  1603. size_t bs = ctx->block_size;
  1604. size_t hs = ctx->hash_size;
  1605. size_t i, num_words;
  1606. u32 mr;
  1607. /* Save d = SHA((K' + ipad) | msg). */
  1608. num_words = ds / sizeof(u32);
  1609. for (i = 0; i < num_words; ++i)
  1610. digest[i] = atmel_sha_read(dd, SHA_REG_DIGEST(i));
  1611. /* Restore context to finish computing SHA((K' + opad) | d). */
  1612. atmel_sha_write(dd, SHA_CR, SHA_CR_WUIHV);
  1613. num_words = hs / sizeof(u32);
  1614. for (i = 0; i < num_words; ++i)
  1615. atmel_sha_write(dd, SHA_REG_DIN(i), hmac->opad[i]);
  1616. mr = SHA_MR_MODE_AUTO | SHA_MR_UIHV;
  1617. mr |= (ctx->flags & SHA_FLAGS_ALGO_MASK);
  1618. atmel_sha_write(dd, SHA_MR, mr);
  1619. atmel_sha_write(dd, SHA_MSR, bs + ds);
  1620. atmel_sha_write(dd, SHA_BCR, ds);
  1621. atmel_sha_write(dd, SHA_CR, SHA_CR_FIRST);
  1622. sg_init_one(&dd->tmp, digest, ds);
  1623. return atmel_sha_cpu_start(dd, &dd->tmp, ds, false, true,
  1624. atmel_sha_hmac_final_done);
  1625. }
  1626. static int atmel_sha_hmac_final_done(struct atmel_sha_dev *dd)
  1627. {
  1628. /*
  1629. * req->result might not be sizeof(u32) aligned, so copy the
  1630. * digest into ctx->digest[] before memcpy() the data into
  1631. * req->result.
  1632. */
  1633. atmel_sha_copy_hash(dd->req);
  1634. atmel_sha_copy_ready_hash(dd->req);
  1635. return atmel_sha_complete(dd, 0);
  1636. }
  1637. static int atmel_sha_hmac_digest(struct ahash_request *req)
  1638. {
  1639. int err;
  1640. err = atmel_sha_init(req);
  1641. if (err)
  1642. return err;
  1643. return atmel_sha_enqueue(req, SHA_OP_DIGEST);
  1644. }
  1645. static int atmel_sha_hmac_digest2(struct atmel_sha_dev *dd)
  1646. {
  1647. struct ahash_request *req = dd->req;
  1648. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  1649. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  1650. struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
  1651. size_t hs = ctx->hash_size;
  1652. size_t i, num_words = hs / sizeof(u32);
  1653. bool use_dma = false;
  1654. u32 mr;
  1655. /* Special case for empty message. */
  1656. if (!req->nbytes)
  1657. return atmel_sha_complete(dd, -EINVAL); // TODO:
  1658. /* Check DMA threshold and alignment. */
  1659. if (req->nbytes > ATMEL_SHA_DMA_THRESHOLD &&
  1660. atmel_sha_dma_check_aligned(dd, req->src, req->nbytes))
  1661. use_dma = true;
  1662. /* Write both initial hash values to compute a HMAC. */
  1663. atmel_sha_write(dd, SHA_CR, SHA_CR_WUIHV);
  1664. for (i = 0; i < num_words; ++i)
  1665. atmel_sha_write(dd, SHA_REG_DIN(i), hmac->ipad[i]);
  1666. atmel_sha_write(dd, SHA_CR, SHA_CR_WUIEHV);
  1667. for (i = 0; i < num_words; ++i)
  1668. atmel_sha_write(dd, SHA_REG_DIN(i), hmac->opad[i]);
  1669. /* Write the Mode, Message Size, Bytes Count then Control Registers. */
  1670. mr = (SHA_MR_HMAC | SHA_MR_DUALBUFF);
  1671. mr |= ctx->flags & SHA_FLAGS_ALGO_MASK;
  1672. if (use_dma)
  1673. mr |= SHA_MR_MODE_IDATAR0;
  1674. else
  1675. mr |= SHA_MR_MODE_AUTO;
  1676. atmel_sha_write(dd, SHA_MR, mr);
  1677. atmel_sha_write(dd, SHA_MSR, req->nbytes);
  1678. atmel_sha_write(dd, SHA_BCR, req->nbytes);
  1679. atmel_sha_write(dd, SHA_CR, SHA_CR_FIRST);
  1680. /* Process data. */
  1681. if (use_dma)
  1682. return atmel_sha_dma_start(dd, req->src, req->nbytes,
  1683. atmel_sha_hmac_final_done);
  1684. return atmel_sha_cpu_start(dd, req->src, req->nbytes, false, true,
  1685. atmel_sha_hmac_final_done);
  1686. }
  1687. static int atmel_sha_hmac_cra_init(struct crypto_tfm *tfm)
  1688. {
  1689. struct atmel_sha_hmac_ctx *hmac = crypto_tfm_ctx(tfm);
  1690. crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
  1691. sizeof(struct atmel_sha_reqctx));
  1692. hmac->base.start = atmel_sha_hmac_start;
  1693. atmel_sha_hmac_key_init(&hmac->hkey);
  1694. return 0;
  1695. }
  1696. static void atmel_sha_hmac_cra_exit(struct crypto_tfm *tfm)
  1697. {
  1698. struct atmel_sha_hmac_ctx *hmac = crypto_tfm_ctx(tfm);
  1699. atmel_sha_hmac_key_release(&hmac->hkey);
  1700. }
  1701. static struct ahash_alg sha_hmac_algs[] = {
  1702. {
  1703. .init = atmel_sha_hmac_init,
  1704. .update = atmel_sha_update,
  1705. .final = atmel_sha_final,
  1706. .digest = atmel_sha_hmac_digest,
  1707. .setkey = atmel_sha_hmac_setkey,
  1708. .export = atmel_sha_export,
  1709. .import = atmel_sha_import,
  1710. .halg = {
  1711. .digestsize = SHA1_DIGEST_SIZE,
  1712. .statesize = sizeof(struct atmel_sha_reqctx),
  1713. .base = {
  1714. .cra_name = "hmac(sha1)",
  1715. .cra_driver_name = "atmel-hmac-sha1",
  1716. .cra_priority = 100,
  1717. .cra_flags = CRYPTO_ALG_ASYNC,
  1718. .cra_blocksize = SHA1_BLOCK_SIZE,
  1719. .cra_ctxsize = sizeof(struct atmel_sha_hmac_ctx),
  1720. .cra_alignmask = 0,
  1721. .cra_module = THIS_MODULE,
  1722. .cra_init = atmel_sha_hmac_cra_init,
  1723. .cra_exit = atmel_sha_hmac_cra_exit,
  1724. }
  1725. }
  1726. },
  1727. {
  1728. .init = atmel_sha_hmac_init,
  1729. .update = atmel_sha_update,
  1730. .final = atmel_sha_final,
  1731. .digest = atmel_sha_hmac_digest,
  1732. .setkey = atmel_sha_hmac_setkey,
  1733. .export = atmel_sha_export,
  1734. .import = atmel_sha_import,
  1735. .halg = {
  1736. .digestsize = SHA224_DIGEST_SIZE,
  1737. .statesize = sizeof(struct atmel_sha_reqctx),
  1738. .base = {
  1739. .cra_name = "hmac(sha224)",
  1740. .cra_driver_name = "atmel-hmac-sha224",
  1741. .cra_priority = 100,
  1742. .cra_flags = CRYPTO_ALG_ASYNC,
  1743. .cra_blocksize = SHA224_BLOCK_SIZE,
  1744. .cra_ctxsize = sizeof(struct atmel_sha_hmac_ctx),
  1745. .cra_alignmask = 0,
  1746. .cra_module = THIS_MODULE,
  1747. .cra_init = atmel_sha_hmac_cra_init,
  1748. .cra_exit = atmel_sha_hmac_cra_exit,
  1749. }
  1750. }
  1751. },
  1752. {
  1753. .init = atmel_sha_hmac_init,
  1754. .update = atmel_sha_update,
  1755. .final = atmel_sha_final,
  1756. .digest = atmel_sha_hmac_digest,
  1757. .setkey = atmel_sha_hmac_setkey,
  1758. .export = atmel_sha_export,
  1759. .import = atmel_sha_import,
  1760. .halg = {
  1761. .digestsize = SHA256_DIGEST_SIZE,
  1762. .statesize = sizeof(struct atmel_sha_reqctx),
  1763. .base = {
  1764. .cra_name = "hmac(sha256)",
  1765. .cra_driver_name = "atmel-hmac-sha256",
  1766. .cra_priority = 100,
  1767. .cra_flags = CRYPTO_ALG_ASYNC,
  1768. .cra_blocksize = SHA256_BLOCK_SIZE,
  1769. .cra_ctxsize = sizeof(struct atmel_sha_hmac_ctx),
  1770. .cra_alignmask = 0,
  1771. .cra_module = THIS_MODULE,
  1772. .cra_init = atmel_sha_hmac_cra_init,
  1773. .cra_exit = atmel_sha_hmac_cra_exit,
  1774. }
  1775. }
  1776. },
  1777. {
  1778. .init = atmel_sha_hmac_init,
  1779. .update = atmel_sha_update,
  1780. .final = atmel_sha_final,
  1781. .digest = atmel_sha_hmac_digest,
  1782. .setkey = atmel_sha_hmac_setkey,
  1783. .export = atmel_sha_export,
  1784. .import = atmel_sha_import,
  1785. .halg = {
  1786. .digestsize = SHA384_DIGEST_SIZE,
  1787. .statesize = sizeof(struct atmel_sha_reqctx),
  1788. .base = {
  1789. .cra_name = "hmac(sha384)",
  1790. .cra_driver_name = "atmel-hmac-sha384",
  1791. .cra_priority = 100,
  1792. .cra_flags = CRYPTO_ALG_ASYNC,
  1793. .cra_blocksize = SHA384_BLOCK_SIZE,
  1794. .cra_ctxsize = sizeof(struct atmel_sha_hmac_ctx),
  1795. .cra_alignmask = 0,
  1796. .cra_module = THIS_MODULE,
  1797. .cra_init = atmel_sha_hmac_cra_init,
  1798. .cra_exit = atmel_sha_hmac_cra_exit,
  1799. }
  1800. }
  1801. },
  1802. {
  1803. .init = atmel_sha_hmac_init,
  1804. .update = atmel_sha_update,
  1805. .final = atmel_sha_final,
  1806. .digest = atmel_sha_hmac_digest,
  1807. .setkey = atmel_sha_hmac_setkey,
  1808. .export = atmel_sha_export,
  1809. .import = atmel_sha_import,
  1810. .halg = {
  1811. .digestsize = SHA512_DIGEST_SIZE,
  1812. .statesize = sizeof(struct atmel_sha_reqctx),
  1813. .base = {
  1814. .cra_name = "hmac(sha512)",
  1815. .cra_driver_name = "atmel-hmac-sha512",
  1816. .cra_priority = 100,
  1817. .cra_flags = CRYPTO_ALG_ASYNC,
  1818. .cra_blocksize = SHA512_BLOCK_SIZE,
  1819. .cra_ctxsize = sizeof(struct atmel_sha_hmac_ctx),
  1820. .cra_alignmask = 0,
  1821. .cra_module = THIS_MODULE,
  1822. .cra_init = atmel_sha_hmac_cra_init,
  1823. .cra_exit = atmel_sha_hmac_cra_exit,
  1824. }
  1825. }
  1826. },
  1827. };
  1828. #ifdef CONFIG_CRYPTO_DEV_ATMEL_AUTHENC
  1829. /* authenc functions */
  1830. static int atmel_sha_authenc_init2(struct atmel_sha_dev *dd);
  1831. static int atmel_sha_authenc_init_done(struct atmel_sha_dev *dd);
  1832. static int atmel_sha_authenc_final_done(struct atmel_sha_dev *dd);
  1833. struct atmel_sha_authenc_ctx {
  1834. struct crypto_ahash *tfm;
  1835. };
  1836. struct atmel_sha_authenc_reqctx {
  1837. struct atmel_sha_reqctx base;
  1838. atmel_aes_authenc_fn_t cb;
  1839. struct atmel_aes_dev *aes_dev;
  1840. /* _init() parameters. */
  1841. struct scatterlist *assoc;
  1842. u32 assoclen;
  1843. u32 textlen;
  1844. /* _final() parameters. */
  1845. u32 *digest;
  1846. unsigned int digestlen;
  1847. };
  1848. static void atmel_sha_authenc_complete(struct crypto_async_request *areq,
  1849. int err)
  1850. {
  1851. struct ahash_request *req = areq->data;
  1852. struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req);
  1853. authctx->cb(authctx->aes_dev, err, authctx->base.dd->is_async);
  1854. }
  1855. static int atmel_sha_authenc_start(struct atmel_sha_dev *dd)
  1856. {
  1857. struct ahash_request *req = dd->req;
  1858. struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req);
  1859. int err;
  1860. /*
  1861. * Force atmel_sha_complete() to call req->base.complete(), ie
  1862. * atmel_sha_authenc_complete(), which in turn calls authctx->cb().
  1863. */
  1864. dd->force_complete = true;
  1865. err = atmel_sha_hw_init(dd);
  1866. return authctx->cb(authctx->aes_dev, err, dd->is_async);
  1867. }
  1868. bool atmel_sha_authenc_is_ready(void)
  1869. {
  1870. struct atmel_sha_ctx dummy;
  1871. dummy.dd = NULL;
  1872. return (atmel_sha_find_dev(&dummy) != NULL);
  1873. }
  1874. EXPORT_SYMBOL_GPL(atmel_sha_authenc_is_ready);
  1875. unsigned int atmel_sha_authenc_get_reqsize(void)
  1876. {
  1877. return sizeof(struct atmel_sha_authenc_reqctx);
  1878. }
  1879. EXPORT_SYMBOL_GPL(atmel_sha_authenc_get_reqsize);
  1880. struct atmel_sha_authenc_ctx *atmel_sha_authenc_spawn(unsigned long mode)
  1881. {
  1882. struct atmel_sha_authenc_ctx *auth;
  1883. struct crypto_ahash *tfm;
  1884. struct atmel_sha_ctx *tctx;
  1885. const char *name;
  1886. int err = -EINVAL;
  1887. switch (mode & SHA_FLAGS_MODE_MASK) {
  1888. case SHA_FLAGS_HMAC_SHA1:
  1889. name = "atmel-hmac-sha1";
  1890. break;
  1891. case SHA_FLAGS_HMAC_SHA224:
  1892. name = "atmel-hmac-sha224";
  1893. break;
  1894. case SHA_FLAGS_HMAC_SHA256:
  1895. name = "atmel-hmac-sha256";
  1896. break;
  1897. case SHA_FLAGS_HMAC_SHA384:
  1898. name = "atmel-hmac-sha384";
  1899. break;
  1900. case SHA_FLAGS_HMAC_SHA512:
  1901. name = "atmel-hmac-sha512";
  1902. break;
  1903. default:
  1904. goto error;
  1905. }
  1906. tfm = crypto_alloc_ahash(name,
  1907. CRYPTO_ALG_TYPE_AHASH,
  1908. CRYPTO_ALG_TYPE_AHASH_MASK);
  1909. if (IS_ERR(tfm)) {
  1910. err = PTR_ERR(tfm);
  1911. goto error;
  1912. }
  1913. tctx = crypto_ahash_ctx(tfm);
  1914. tctx->start = atmel_sha_authenc_start;
  1915. tctx->flags = mode;
  1916. auth = kzalloc(sizeof(*auth), GFP_KERNEL);
  1917. if (!auth) {
  1918. err = -ENOMEM;
  1919. goto err_free_ahash;
  1920. }
  1921. auth->tfm = tfm;
  1922. return auth;
  1923. err_free_ahash:
  1924. crypto_free_ahash(tfm);
  1925. error:
  1926. return ERR_PTR(err);
  1927. }
  1928. EXPORT_SYMBOL_GPL(atmel_sha_authenc_spawn);
  1929. void atmel_sha_authenc_free(struct atmel_sha_authenc_ctx *auth)
  1930. {
  1931. if (auth)
  1932. crypto_free_ahash(auth->tfm);
  1933. kfree(auth);
  1934. }
  1935. EXPORT_SYMBOL_GPL(atmel_sha_authenc_free);
  1936. int atmel_sha_authenc_setkey(struct atmel_sha_authenc_ctx *auth,
  1937. const u8 *key, unsigned int keylen,
  1938. u32 *flags)
  1939. {
  1940. struct crypto_ahash *tfm = auth->tfm;
  1941. int err;
  1942. crypto_ahash_clear_flags(tfm, CRYPTO_TFM_REQ_MASK);
  1943. crypto_ahash_set_flags(tfm, *flags & CRYPTO_TFM_REQ_MASK);
  1944. err = crypto_ahash_setkey(tfm, key, keylen);
  1945. *flags = crypto_ahash_get_flags(tfm);
  1946. return err;
  1947. }
  1948. EXPORT_SYMBOL_GPL(atmel_sha_authenc_setkey);
  1949. int atmel_sha_authenc_schedule(struct ahash_request *req,
  1950. struct atmel_sha_authenc_ctx *auth,
  1951. atmel_aes_authenc_fn_t cb,
  1952. struct atmel_aes_dev *aes_dev)
  1953. {
  1954. struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req);
  1955. struct atmel_sha_reqctx *ctx = &authctx->base;
  1956. struct crypto_ahash *tfm = auth->tfm;
  1957. struct atmel_sha_ctx *tctx = crypto_ahash_ctx(tfm);
  1958. struct atmel_sha_dev *dd;
  1959. /* Reset request context (MUST be done first). */
  1960. memset(authctx, 0, sizeof(*authctx));
  1961. /* Get SHA device. */
  1962. dd = atmel_sha_find_dev(tctx);
  1963. if (!dd)
  1964. return cb(aes_dev, -ENODEV, false);
  1965. /* Init request context. */
  1966. ctx->dd = dd;
  1967. ctx->buflen = SHA_BUFFER_LEN;
  1968. authctx->cb = cb;
  1969. authctx->aes_dev = aes_dev;
  1970. ahash_request_set_tfm(req, tfm);
  1971. ahash_request_set_callback(req, 0, atmel_sha_authenc_complete, req);
  1972. return atmel_sha_handle_queue(dd, req);
  1973. }
  1974. EXPORT_SYMBOL_GPL(atmel_sha_authenc_schedule);
  1975. int atmel_sha_authenc_init(struct ahash_request *req,
  1976. struct scatterlist *assoc, unsigned int assoclen,
  1977. unsigned int textlen,
  1978. atmel_aes_authenc_fn_t cb,
  1979. struct atmel_aes_dev *aes_dev)
  1980. {
  1981. struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req);
  1982. struct atmel_sha_reqctx *ctx = &authctx->base;
  1983. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  1984. struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
  1985. struct atmel_sha_dev *dd = ctx->dd;
  1986. if (unlikely(!IS_ALIGNED(assoclen, sizeof(u32))))
  1987. return atmel_sha_complete(dd, -EINVAL);
  1988. authctx->cb = cb;
  1989. authctx->aes_dev = aes_dev;
  1990. authctx->assoc = assoc;
  1991. authctx->assoclen = assoclen;
  1992. authctx->textlen = textlen;
  1993. ctx->flags = hmac->base.flags;
  1994. return atmel_sha_hmac_setup(dd, atmel_sha_authenc_init2);
  1995. }
  1996. EXPORT_SYMBOL_GPL(atmel_sha_authenc_init);
  1997. static int atmel_sha_authenc_init2(struct atmel_sha_dev *dd)
  1998. {
  1999. struct ahash_request *req = dd->req;
  2000. struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req);
  2001. struct atmel_sha_reqctx *ctx = &authctx->base;
  2002. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  2003. struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
  2004. size_t hs = ctx->hash_size;
  2005. size_t i, num_words = hs / sizeof(u32);
  2006. u32 mr, msg_size;
  2007. atmel_sha_write(dd, SHA_CR, SHA_CR_WUIHV);
  2008. for (i = 0; i < num_words; ++i)
  2009. atmel_sha_write(dd, SHA_REG_DIN(i), hmac->ipad[i]);
  2010. atmel_sha_write(dd, SHA_CR, SHA_CR_WUIEHV);
  2011. for (i = 0; i < num_words; ++i)
  2012. atmel_sha_write(dd, SHA_REG_DIN(i), hmac->opad[i]);
  2013. mr = (SHA_MR_MODE_IDATAR0 |
  2014. SHA_MR_HMAC |
  2015. SHA_MR_DUALBUFF);
  2016. mr |= ctx->flags & SHA_FLAGS_ALGO_MASK;
  2017. atmel_sha_write(dd, SHA_MR, mr);
  2018. msg_size = authctx->assoclen + authctx->textlen;
  2019. atmel_sha_write(dd, SHA_MSR, msg_size);
  2020. atmel_sha_write(dd, SHA_BCR, msg_size);
  2021. atmel_sha_write(dd, SHA_CR, SHA_CR_FIRST);
  2022. /* Process assoc data. */
  2023. return atmel_sha_cpu_start(dd, authctx->assoc, authctx->assoclen,
  2024. true, false,
  2025. atmel_sha_authenc_init_done);
  2026. }
  2027. static int atmel_sha_authenc_init_done(struct atmel_sha_dev *dd)
  2028. {
  2029. struct ahash_request *req = dd->req;
  2030. struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req);
  2031. return authctx->cb(authctx->aes_dev, 0, dd->is_async);
  2032. }
  2033. int atmel_sha_authenc_final(struct ahash_request *req,
  2034. u32 *digest, unsigned int digestlen,
  2035. atmel_aes_authenc_fn_t cb,
  2036. struct atmel_aes_dev *aes_dev)
  2037. {
  2038. struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req);
  2039. struct atmel_sha_reqctx *ctx = &authctx->base;
  2040. struct atmel_sha_dev *dd = ctx->dd;
  2041. switch (ctx->flags & SHA_FLAGS_ALGO_MASK) {
  2042. case SHA_FLAGS_SHA1:
  2043. authctx->digestlen = SHA1_DIGEST_SIZE;
  2044. break;
  2045. case SHA_FLAGS_SHA224:
  2046. authctx->digestlen = SHA224_DIGEST_SIZE;
  2047. break;
  2048. case SHA_FLAGS_SHA256:
  2049. authctx->digestlen = SHA256_DIGEST_SIZE;
  2050. break;
  2051. case SHA_FLAGS_SHA384:
  2052. authctx->digestlen = SHA384_DIGEST_SIZE;
  2053. break;
  2054. case SHA_FLAGS_SHA512:
  2055. authctx->digestlen = SHA512_DIGEST_SIZE;
  2056. break;
  2057. default:
  2058. return atmel_sha_complete(dd, -EINVAL);
  2059. }
  2060. if (authctx->digestlen > digestlen)
  2061. authctx->digestlen = digestlen;
  2062. authctx->cb = cb;
  2063. authctx->aes_dev = aes_dev;
  2064. authctx->digest = digest;
  2065. return atmel_sha_wait_for_data_ready(dd,
  2066. atmel_sha_authenc_final_done);
  2067. }
  2068. EXPORT_SYMBOL_GPL(atmel_sha_authenc_final);
  2069. static int atmel_sha_authenc_final_done(struct atmel_sha_dev *dd)
  2070. {
  2071. struct ahash_request *req = dd->req;
  2072. struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req);
  2073. size_t i, num_words = authctx->digestlen / sizeof(u32);
  2074. for (i = 0; i < num_words; ++i)
  2075. authctx->digest[i] = atmel_sha_read(dd, SHA_REG_DIGEST(i));
  2076. return atmel_sha_complete(dd, 0);
  2077. }
  2078. void atmel_sha_authenc_abort(struct ahash_request *req)
  2079. {
  2080. struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req);
  2081. struct atmel_sha_reqctx *ctx = &authctx->base;
  2082. struct atmel_sha_dev *dd = ctx->dd;
  2083. /* Prevent atmel_sha_complete() from calling req->base.complete(). */
  2084. dd->is_async = false;
  2085. dd->force_complete = false;
  2086. (void)atmel_sha_complete(dd, 0);
  2087. }
  2088. EXPORT_SYMBOL_GPL(atmel_sha_authenc_abort);
  2089. #endif /* CONFIG_CRYPTO_DEV_ATMEL_AUTHENC */
  2090. static void atmel_sha_unregister_algs(struct atmel_sha_dev *dd)
  2091. {
  2092. int i;
  2093. if (dd->caps.has_hmac)
  2094. for (i = 0; i < ARRAY_SIZE(sha_hmac_algs); i++)
  2095. crypto_unregister_ahash(&sha_hmac_algs[i]);
  2096. for (i = 0; i < ARRAY_SIZE(sha_1_256_algs); i++)
  2097. crypto_unregister_ahash(&sha_1_256_algs[i]);
  2098. if (dd->caps.has_sha224)
  2099. crypto_unregister_ahash(&sha_224_alg);
  2100. if (dd->caps.has_sha_384_512) {
  2101. for (i = 0; i < ARRAY_SIZE(sha_384_512_algs); i++)
  2102. crypto_unregister_ahash(&sha_384_512_algs[i]);
  2103. }
  2104. }
  2105. static int atmel_sha_register_algs(struct atmel_sha_dev *dd)
  2106. {
  2107. int err, i, j;
  2108. for (i = 0; i < ARRAY_SIZE(sha_1_256_algs); i++) {
  2109. err = crypto_register_ahash(&sha_1_256_algs[i]);
  2110. if (err)
  2111. goto err_sha_1_256_algs;
  2112. }
  2113. if (dd->caps.has_sha224) {
  2114. err = crypto_register_ahash(&sha_224_alg);
  2115. if (err)
  2116. goto err_sha_224_algs;
  2117. }
  2118. if (dd->caps.has_sha_384_512) {
  2119. for (i = 0; i < ARRAY_SIZE(sha_384_512_algs); i++) {
  2120. err = crypto_register_ahash(&sha_384_512_algs[i]);
  2121. if (err)
  2122. goto err_sha_384_512_algs;
  2123. }
  2124. }
  2125. if (dd->caps.has_hmac) {
  2126. for (i = 0; i < ARRAY_SIZE(sha_hmac_algs); i++) {
  2127. err = crypto_register_ahash(&sha_hmac_algs[i]);
  2128. if (err)
  2129. goto err_sha_hmac_algs;
  2130. }
  2131. }
  2132. return 0;
  2133. /*i = ARRAY_SIZE(sha_hmac_algs);*/
  2134. err_sha_hmac_algs:
  2135. for (j = 0; j < i; j++)
  2136. crypto_unregister_ahash(&sha_hmac_algs[j]);
  2137. i = ARRAY_SIZE(sha_384_512_algs);
  2138. err_sha_384_512_algs:
  2139. for (j = 0; j < i; j++)
  2140. crypto_unregister_ahash(&sha_384_512_algs[j]);
  2141. crypto_unregister_ahash(&sha_224_alg);
  2142. err_sha_224_algs:
  2143. i = ARRAY_SIZE(sha_1_256_algs);
  2144. err_sha_1_256_algs:
  2145. for (j = 0; j < i; j++)
  2146. crypto_unregister_ahash(&sha_1_256_algs[j]);
  2147. return err;
  2148. }
  2149. static bool atmel_sha_filter(struct dma_chan *chan, void *slave)
  2150. {
  2151. struct at_dma_slave *sl = slave;
  2152. if (sl && sl->dma_dev == chan->device->dev) {
  2153. chan->private = sl;
  2154. return true;
  2155. } else {
  2156. return false;
  2157. }
  2158. }
  2159. static int atmel_sha_dma_init(struct atmel_sha_dev *dd,
  2160. struct crypto_platform_data *pdata)
  2161. {
  2162. int err = -ENOMEM;
  2163. dma_cap_mask_t mask_in;
  2164. /* Try to grab DMA channel */
  2165. dma_cap_zero(mask_in);
  2166. dma_cap_set(DMA_SLAVE, mask_in);
  2167. dd->dma_lch_in.chan = dma_request_slave_channel_compat(mask_in,
  2168. atmel_sha_filter, &pdata->dma_slave->rxdata, dd->dev, "tx");
  2169. if (!dd->dma_lch_in.chan) {
  2170. dev_warn(dd->dev, "no DMA channel available\n");
  2171. return err;
  2172. }
  2173. dd->dma_lch_in.dma_conf.direction = DMA_MEM_TO_DEV;
  2174. dd->dma_lch_in.dma_conf.dst_addr = dd->phys_base +
  2175. SHA_REG_DIN(0);
  2176. dd->dma_lch_in.dma_conf.src_maxburst = 1;
  2177. dd->dma_lch_in.dma_conf.src_addr_width =
  2178. DMA_SLAVE_BUSWIDTH_4_BYTES;
  2179. dd->dma_lch_in.dma_conf.dst_maxburst = 1;
  2180. dd->dma_lch_in.dma_conf.dst_addr_width =
  2181. DMA_SLAVE_BUSWIDTH_4_BYTES;
  2182. dd->dma_lch_in.dma_conf.device_fc = false;
  2183. return 0;
  2184. }
  2185. static void atmel_sha_dma_cleanup(struct atmel_sha_dev *dd)
  2186. {
  2187. dma_release_channel(dd->dma_lch_in.chan);
  2188. }
  2189. static void atmel_sha_get_cap(struct atmel_sha_dev *dd)
  2190. {
  2191. dd->caps.has_dma = 0;
  2192. dd->caps.has_dualbuff = 0;
  2193. dd->caps.has_sha224 = 0;
  2194. dd->caps.has_sha_384_512 = 0;
  2195. dd->caps.has_uihv = 0;
  2196. dd->caps.has_hmac = 0;
  2197. /* keep only major version number */
  2198. switch (dd->hw_version & 0xff0) {
  2199. case 0x510:
  2200. dd->caps.has_dma = 1;
  2201. dd->caps.has_dualbuff = 1;
  2202. dd->caps.has_sha224 = 1;
  2203. dd->caps.has_sha_384_512 = 1;
  2204. dd->caps.has_uihv = 1;
  2205. dd->caps.has_hmac = 1;
  2206. break;
  2207. case 0x420:
  2208. dd->caps.has_dma = 1;
  2209. dd->caps.has_dualbuff = 1;
  2210. dd->caps.has_sha224 = 1;
  2211. dd->caps.has_sha_384_512 = 1;
  2212. dd->caps.has_uihv = 1;
  2213. break;
  2214. case 0x410:
  2215. dd->caps.has_dma = 1;
  2216. dd->caps.has_dualbuff = 1;
  2217. dd->caps.has_sha224 = 1;
  2218. dd->caps.has_sha_384_512 = 1;
  2219. break;
  2220. case 0x400:
  2221. dd->caps.has_dma = 1;
  2222. dd->caps.has_dualbuff = 1;
  2223. dd->caps.has_sha224 = 1;
  2224. break;
  2225. case 0x320:
  2226. break;
  2227. default:
  2228. dev_warn(dd->dev,
  2229. "Unmanaged sha version, set minimum capabilities\n");
  2230. break;
  2231. }
  2232. }
  2233. #if defined(CONFIG_OF)
  2234. static const struct of_device_id atmel_sha_dt_ids[] = {
  2235. { .compatible = "atmel,at91sam9g46-sha" },
  2236. { /* sentinel */ }
  2237. };
  2238. MODULE_DEVICE_TABLE(of, atmel_sha_dt_ids);
  2239. static struct crypto_platform_data *atmel_sha_of_init(struct platform_device *pdev)
  2240. {
  2241. struct device_node *np = pdev->dev.of_node;
  2242. struct crypto_platform_data *pdata;
  2243. if (!np) {
  2244. dev_err(&pdev->dev, "device node not found\n");
  2245. return ERR_PTR(-EINVAL);
  2246. }
  2247. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  2248. if (!pdata) {
  2249. dev_err(&pdev->dev, "could not allocate memory for pdata\n");
  2250. return ERR_PTR(-ENOMEM);
  2251. }
  2252. pdata->dma_slave = devm_kzalloc(&pdev->dev,
  2253. sizeof(*(pdata->dma_slave)),
  2254. GFP_KERNEL);
  2255. if (!pdata->dma_slave) {
  2256. dev_err(&pdev->dev, "could not allocate memory for dma_slave\n");
  2257. return ERR_PTR(-ENOMEM);
  2258. }
  2259. return pdata;
  2260. }
  2261. #else /* CONFIG_OF */
  2262. static inline struct crypto_platform_data *atmel_sha_of_init(struct platform_device *dev)
  2263. {
  2264. return ERR_PTR(-EINVAL);
  2265. }
  2266. #endif
  2267. static int atmel_sha_probe(struct platform_device *pdev)
  2268. {
  2269. struct atmel_sha_dev *sha_dd;
  2270. struct crypto_platform_data *pdata;
  2271. struct device *dev = &pdev->dev;
  2272. struct resource *sha_res;
  2273. int err;
  2274. sha_dd = devm_kzalloc(&pdev->dev, sizeof(*sha_dd), GFP_KERNEL);
  2275. if (sha_dd == NULL) {
  2276. dev_err(dev, "unable to alloc data struct.\n");
  2277. err = -ENOMEM;
  2278. goto sha_dd_err;
  2279. }
  2280. sha_dd->dev = dev;
  2281. platform_set_drvdata(pdev, sha_dd);
  2282. INIT_LIST_HEAD(&sha_dd->list);
  2283. spin_lock_init(&sha_dd->lock);
  2284. tasklet_init(&sha_dd->done_task, atmel_sha_done_task,
  2285. (unsigned long)sha_dd);
  2286. tasklet_init(&sha_dd->queue_task, atmel_sha_queue_task,
  2287. (unsigned long)sha_dd);
  2288. crypto_init_queue(&sha_dd->queue, ATMEL_SHA_QUEUE_LENGTH);
  2289. sha_dd->irq = -1;
  2290. /* Get the base address */
  2291. sha_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2292. if (!sha_res) {
  2293. dev_err(dev, "no MEM resource info\n");
  2294. err = -ENODEV;
  2295. goto res_err;
  2296. }
  2297. sha_dd->phys_base = sha_res->start;
  2298. /* Get the IRQ */
  2299. sha_dd->irq = platform_get_irq(pdev, 0);
  2300. if (sha_dd->irq < 0) {
  2301. dev_err(dev, "no IRQ resource info\n");
  2302. err = sha_dd->irq;
  2303. goto res_err;
  2304. }
  2305. err = devm_request_irq(&pdev->dev, sha_dd->irq, atmel_sha_irq,
  2306. IRQF_SHARED, "atmel-sha", sha_dd);
  2307. if (err) {
  2308. dev_err(dev, "unable to request sha irq.\n");
  2309. goto res_err;
  2310. }
  2311. /* Initializing the clock */
  2312. sha_dd->iclk = devm_clk_get(&pdev->dev, "sha_clk");
  2313. if (IS_ERR(sha_dd->iclk)) {
  2314. dev_err(dev, "clock initialization failed.\n");
  2315. err = PTR_ERR(sha_dd->iclk);
  2316. goto res_err;
  2317. }
  2318. sha_dd->io_base = devm_ioremap_resource(&pdev->dev, sha_res);
  2319. if (IS_ERR(sha_dd->io_base)) {
  2320. dev_err(dev, "can't ioremap\n");
  2321. err = PTR_ERR(sha_dd->io_base);
  2322. goto res_err;
  2323. }
  2324. err = clk_prepare(sha_dd->iclk);
  2325. if (err)
  2326. goto res_err;
  2327. atmel_sha_hw_version_init(sha_dd);
  2328. atmel_sha_get_cap(sha_dd);
  2329. if (sha_dd->caps.has_dma) {
  2330. pdata = pdev->dev.platform_data;
  2331. if (!pdata) {
  2332. pdata = atmel_sha_of_init(pdev);
  2333. if (IS_ERR(pdata)) {
  2334. dev_err(&pdev->dev, "platform data not available\n");
  2335. err = PTR_ERR(pdata);
  2336. goto iclk_unprepare;
  2337. }
  2338. }
  2339. if (!pdata->dma_slave) {
  2340. err = -ENXIO;
  2341. goto iclk_unprepare;
  2342. }
  2343. err = atmel_sha_dma_init(sha_dd, pdata);
  2344. if (err)
  2345. goto err_sha_dma;
  2346. dev_info(dev, "using %s for DMA transfers\n",
  2347. dma_chan_name(sha_dd->dma_lch_in.chan));
  2348. }
  2349. spin_lock(&atmel_sha.lock);
  2350. list_add_tail(&sha_dd->list, &atmel_sha.dev_list);
  2351. spin_unlock(&atmel_sha.lock);
  2352. err = atmel_sha_register_algs(sha_dd);
  2353. if (err)
  2354. goto err_algs;
  2355. dev_info(dev, "Atmel SHA1/SHA256%s%s\n",
  2356. sha_dd->caps.has_sha224 ? "/SHA224" : "",
  2357. sha_dd->caps.has_sha_384_512 ? "/SHA384/SHA512" : "");
  2358. return 0;
  2359. err_algs:
  2360. spin_lock(&atmel_sha.lock);
  2361. list_del(&sha_dd->list);
  2362. spin_unlock(&atmel_sha.lock);
  2363. if (sha_dd->caps.has_dma)
  2364. atmel_sha_dma_cleanup(sha_dd);
  2365. err_sha_dma:
  2366. iclk_unprepare:
  2367. clk_unprepare(sha_dd->iclk);
  2368. res_err:
  2369. tasklet_kill(&sha_dd->queue_task);
  2370. tasklet_kill(&sha_dd->done_task);
  2371. sha_dd_err:
  2372. dev_err(dev, "initialization failed.\n");
  2373. return err;
  2374. }
  2375. static int atmel_sha_remove(struct platform_device *pdev)
  2376. {
  2377. struct atmel_sha_dev *sha_dd;
  2378. sha_dd = platform_get_drvdata(pdev);
  2379. if (!sha_dd)
  2380. return -ENODEV;
  2381. spin_lock(&atmel_sha.lock);
  2382. list_del(&sha_dd->list);
  2383. spin_unlock(&atmel_sha.lock);
  2384. atmel_sha_unregister_algs(sha_dd);
  2385. tasklet_kill(&sha_dd->queue_task);
  2386. tasklet_kill(&sha_dd->done_task);
  2387. if (sha_dd->caps.has_dma)
  2388. atmel_sha_dma_cleanup(sha_dd);
  2389. clk_unprepare(sha_dd->iclk);
  2390. return 0;
  2391. }
  2392. static struct platform_driver atmel_sha_driver = {
  2393. .probe = atmel_sha_probe,
  2394. .remove = atmel_sha_remove,
  2395. .driver = {
  2396. .name = "atmel_sha",
  2397. .of_match_table = of_match_ptr(atmel_sha_dt_ids),
  2398. },
  2399. };
  2400. module_platform_driver(atmel_sha_driver);
  2401. MODULE_DESCRIPTION("Atmel SHA (1/256/224/384/512) hw acceleration support.");
  2402. MODULE_LICENSE("GPL v2");
  2403. MODULE_AUTHOR("Nicolas Royer - Eukréa Electromatique");