atmel-sha.c 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907
  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. #if IS_ENABLED(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, 0, 0);
  1907. if (IS_ERR(tfm)) {
  1908. err = PTR_ERR(tfm);
  1909. goto error;
  1910. }
  1911. tctx = crypto_ahash_ctx(tfm);
  1912. tctx->start = atmel_sha_authenc_start;
  1913. tctx->flags = mode;
  1914. auth = kzalloc(sizeof(*auth), GFP_KERNEL);
  1915. if (!auth) {
  1916. err = -ENOMEM;
  1917. goto err_free_ahash;
  1918. }
  1919. auth->tfm = tfm;
  1920. return auth;
  1921. err_free_ahash:
  1922. crypto_free_ahash(tfm);
  1923. error:
  1924. return ERR_PTR(err);
  1925. }
  1926. EXPORT_SYMBOL_GPL(atmel_sha_authenc_spawn);
  1927. void atmel_sha_authenc_free(struct atmel_sha_authenc_ctx *auth)
  1928. {
  1929. if (auth)
  1930. crypto_free_ahash(auth->tfm);
  1931. kfree(auth);
  1932. }
  1933. EXPORT_SYMBOL_GPL(atmel_sha_authenc_free);
  1934. int atmel_sha_authenc_setkey(struct atmel_sha_authenc_ctx *auth,
  1935. const u8 *key, unsigned int keylen,
  1936. u32 *flags)
  1937. {
  1938. struct crypto_ahash *tfm = auth->tfm;
  1939. int err;
  1940. crypto_ahash_clear_flags(tfm, CRYPTO_TFM_REQ_MASK);
  1941. crypto_ahash_set_flags(tfm, *flags & CRYPTO_TFM_REQ_MASK);
  1942. err = crypto_ahash_setkey(tfm, key, keylen);
  1943. *flags = crypto_ahash_get_flags(tfm);
  1944. return err;
  1945. }
  1946. EXPORT_SYMBOL_GPL(atmel_sha_authenc_setkey);
  1947. int atmel_sha_authenc_schedule(struct ahash_request *req,
  1948. struct atmel_sha_authenc_ctx *auth,
  1949. atmel_aes_authenc_fn_t cb,
  1950. struct atmel_aes_dev *aes_dev)
  1951. {
  1952. struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req);
  1953. struct atmel_sha_reqctx *ctx = &authctx->base;
  1954. struct crypto_ahash *tfm = auth->tfm;
  1955. struct atmel_sha_ctx *tctx = crypto_ahash_ctx(tfm);
  1956. struct atmel_sha_dev *dd;
  1957. /* Reset request context (MUST be done first). */
  1958. memset(authctx, 0, sizeof(*authctx));
  1959. /* Get SHA device. */
  1960. dd = atmel_sha_find_dev(tctx);
  1961. if (!dd)
  1962. return cb(aes_dev, -ENODEV, false);
  1963. /* Init request context. */
  1964. ctx->dd = dd;
  1965. ctx->buflen = SHA_BUFFER_LEN;
  1966. authctx->cb = cb;
  1967. authctx->aes_dev = aes_dev;
  1968. ahash_request_set_tfm(req, tfm);
  1969. ahash_request_set_callback(req, 0, atmel_sha_authenc_complete, req);
  1970. return atmel_sha_handle_queue(dd, req);
  1971. }
  1972. EXPORT_SYMBOL_GPL(atmel_sha_authenc_schedule);
  1973. int atmel_sha_authenc_init(struct ahash_request *req,
  1974. struct scatterlist *assoc, unsigned int assoclen,
  1975. unsigned int textlen,
  1976. atmel_aes_authenc_fn_t cb,
  1977. struct atmel_aes_dev *aes_dev)
  1978. {
  1979. struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req);
  1980. struct atmel_sha_reqctx *ctx = &authctx->base;
  1981. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  1982. struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
  1983. struct atmel_sha_dev *dd = ctx->dd;
  1984. if (unlikely(!IS_ALIGNED(assoclen, sizeof(u32))))
  1985. return atmel_sha_complete(dd, -EINVAL);
  1986. authctx->cb = cb;
  1987. authctx->aes_dev = aes_dev;
  1988. authctx->assoc = assoc;
  1989. authctx->assoclen = assoclen;
  1990. authctx->textlen = textlen;
  1991. ctx->flags = hmac->base.flags;
  1992. return atmel_sha_hmac_setup(dd, atmel_sha_authenc_init2);
  1993. }
  1994. EXPORT_SYMBOL_GPL(atmel_sha_authenc_init);
  1995. static int atmel_sha_authenc_init2(struct atmel_sha_dev *dd)
  1996. {
  1997. struct ahash_request *req = dd->req;
  1998. struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req);
  1999. struct atmel_sha_reqctx *ctx = &authctx->base;
  2000. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  2001. struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
  2002. size_t hs = ctx->hash_size;
  2003. size_t i, num_words = hs / sizeof(u32);
  2004. u32 mr, msg_size;
  2005. atmel_sha_write(dd, SHA_CR, SHA_CR_WUIHV);
  2006. for (i = 0; i < num_words; ++i)
  2007. atmel_sha_write(dd, SHA_REG_DIN(i), hmac->ipad[i]);
  2008. atmel_sha_write(dd, SHA_CR, SHA_CR_WUIEHV);
  2009. for (i = 0; i < num_words; ++i)
  2010. atmel_sha_write(dd, SHA_REG_DIN(i), hmac->opad[i]);
  2011. mr = (SHA_MR_MODE_IDATAR0 |
  2012. SHA_MR_HMAC |
  2013. SHA_MR_DUALBUFF);
  2014. mr |= ctx->flags & SHA_FLAGS_ALGO_MASK;
  2015. atmel_sha_write(dd, SHA_MR, mr);
  2016. msg_size = authctx->assoclen + authctx->textlen;
  2017. atmel_sha_write(dd, SHA_MSR, msg_size);
  2018. atmel_sha_write(dd, SHA_BCR, msg_size);
  2019. atmel_sha_write(dd, SHA_CR, SHA_CR_FIRST);
  2020. /* Process assoc data. */
  2021. return atmel_sha_cpu_start(dd, authctx->assoc, authctx->assoclen,
  2022. true, false,
  2023. atmel_sha_authenc_init_done);
  2024. }
  2025. static int atmel_sha_authenc_init_done(struct atmel_sha_dev *dd)
  2026. {
  2027. struct ahash_request *req = dd->req;
  2028. struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req);
  2029. return authctx->cb(authctx->aes_dev, 0, dd->is_async);
  2030. }
  2031. int atmel_sha_authenc_final(struct ahash_request *req,
  2032. u32 *digest, unsigned int digestlen,
  2033. atmel_aes_authenc_fn_t cb,
  2034. struct atmel_aes_dev *aes_dev)
  2035. {
  2036. struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req);
  2037. struct atmel_sha_reqctx *ctx = &authctx->base;
  2038. struct atmel_sha_dev *dd = ctx->dd;
  2039. switch (ctx->flags & SHA_FLAGS_ALGO_MASK) {
  2040. case SHA_FLAGS_SHA1:
  2041. authctx->digestlen = SHA1_DIGEST_SIZE;
  2042. break;
  2043. case SHA_FLAGS_SHA224:
  2044. authctx->digestlen = SHA224_DIGEST_SIZE;
  2045. break;
  2046. case SHA_FLAGS_SHA256:
  2047. authctx->digestlen = SHA256_DIGEST_SIZE;
  2048. break;
  2049. case SHA_FLAGS_SHA384:
  2050. authctx->digestlen = SHA384_DIGEST_SIZE;
  2051. break;
  2052. case SHA_FLAGS_SHA512:
  2053. authctx->digestlen = SHA512_DIGEST_SIZE;
  2054. break;
  2055. default:
  2056. return atmel_sha_complete(dd, -EINVAL);
  2057. }
  2058. if (authctx->digestlen > digestlen)
  2059. authctx->digestlen = digestlen;
  2060. authctx->cb = cb;
  2061. authctx->aes_dev = aes_dev;
  2062. authctx->digest = digest;
  2063. return atmel_sha_wait_for_data_ready(dd,
  2064. atmel_sha_authenc_final_done);
  2065. }
  2066. EXPORT_SYMBOL_GPL(atmel_sha_authenc_final);
  2067. static int atmel_sha_authenc_final_done(struct atmel_sha_dev *dd)
  2068. {
  2069. struct ahash_request *req = dd->req;
  2070. struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req);
  2071. size_t i, num_words = authctx->digestlen / sizeof(u32);
  2072. for (i = 0; i < num_words; ++i)
  2073. authctx->digest[i] = atmel_sha_read(dd, SHA_REG_DIGEST(i));
  2074. return atmel_sha_complete(dd, 0);
  2075. }
  2076. void atmel_sha_authenc_abort(struct ahash_request *req)
  2077. {
  2078. struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req);
  2079. struct atmel_sha_reqctx *ctx = &authctx->base;
  2080. struct atmel_sha_dev *dd = ctx->dd;
  2081. /* Prevent atmel_sha_complete() from calling req->base.complete(). */
  2082. dd->is_async = false;
  2083. dd->force_complete = false;
  2084. (void)atmel_sha_complete(dd, 0);
  2085. }
  2086. EXPORT_SYMBOL_GPL(atmel_sha_authenc_abort);
  2087. #endif /* CONFIG_CRYPTO_DEV_ATMEL_AUTHENC */
  2088. static void atmel_sha_unregister_algs(struct atmel_sha_dev *dd)
  2089. {
  2090. int i;
  2091. if (dd->caps.has_hmac)
  2092. for (i = 0; i < ARRAY_SIZE(sha_hmac_algs); i++)
  2093. crypto_unregister_ahash(&sha_hmac_algs[i]);
  2094. for (i = 0; i < ARRAY_SIZE(sha_1_256_algs); i++)
  2095. crypto_unregister_ahash(&sha_1_256_algs[i]);
  2096. if (dd->caps.has_sha224)
  2097. crypto_unregister_ahash(&sha_224_alg);
  2098. if (dd->caps.has_sha_384_512) {
  2099. for (i = 0; i < ARRAY_SIZE(sha_384_512_algs); i++)
  2100. crypto_unregister_ahash(&sha_384_512_algs[i]);
  2101. }
  2102. }
  2103. static int atmel_sha_register_algs(struct atmel_sha_dev *dd)
  2104. {
  2105. int err, i, j;
  2106. for (i = 0; i < ARRAY_SIZE(sha_1_256_algs); i++) {
  2107. err = crypto_register_ahash(&sha_1_256_algs[i]);
  2108. if (err)
  2109. goto err_sha_1_256_algs;
  2110. }
  2111. if (dd->caps.has_sha224) {
  2112. err = crypto_register_ahash(&sha_224_alg);
  2113. if (err)
  2114. goto err_sha_224_algs;
  2115. }
  2116. if (dd->caps.has_sha_384_512) {
  2117. for (i = 0; i < ARRAY_SIZE(sha_384_512_algs); i++) {
  2118. err = crypto_register_ahash(&sha_384_512_algs[i]);
  2119. if (err)
  2120. goto err_sha_384_512_algs;
  2121. }
  2122. }
  2123. if (dd->caps.has_hmac) {
  2124. for (i = 0; i < ARRAY_SIZE(sha_hmac_algs); i++) {
  2125. err = crypto_register_ahash(&sha_hmac_algs[i]);
  2126. if (err)
  2127. goto err_sha_hmac_algs;
  2128. }
  2129. }
  2130. return 0;
  2131. /*i = ARRAY_SIZE(sha_hmac_algs);*/
  2132. err_sha_hmac_algs:
  2133. for (j = 0; j < i; j++)
  2134. crypto_unregister_ahash(&sha_hmac_algs[j]);
  2135. i = ARRAY_SIZE(sha_384_512_algs);
  2136. err_sha_384_512_algs:
  2137. for (j = 0; j < i; j++)
  2138. crypto_unregister_ahash(&sha_384_512_algs[j]);
  2139. crypto_unregister_ahash(&sha_224_alg);
  2140. err_sha_224_algs:
  2141. i = ARRAY_SIZE(sha_1_256_algs);
  2142. err_sha_1_256_algs:
  2143. for (j = 0; j < i; j++)
  2144. crypto_unregister_ahash(&sha_1_256_algs[j]);
  2145. return err;
  2146. }
  2147. static bool atmel_sha_filter(struct dma_chan *chan, void *slave)
  2148. {
  2149. struct at_dma_slave *sl = slave;
  2150. if (sl && sl->dma_dev == chan->device->dev) {
  2151. chan->private = sl;
  2152. return true;
  2153. } else {
  2154. return false;
  2155. }
  2156. }
  2157. static int atmel_sha_dma_init(struct atmel_sha_dev *dd,
  2158. struct crypto_platform_data *pdata)
  2159. {
  2160. dma_cap_mask_t mask_in;
  2161. /* Try to grab DMA channel */
  2162. dma_cap_zero(mask_in);
  2163. dma_cap_set(DMA_SLAVE, mask_in);
  2164. dd->dma_lch_in.chan = dma_request_slave_channel_compat(mask_in,
  2165. atmel_sha_filter, &pdata->dma_slave->rxdata, dd->dev, "tx");
  2166. if (!dd->dma_lch_in.chan) {
  2167. dev_warn(dd->dev, "no DMA channel available\n");
  2168. return -ENODEV;
  2169. }
  2170. dd->dma_lch_in.dma_conf.direction = DMA_MEM_TO_DEV;
  2171. dd->dma_lch_in.dma_conf.dst_addr = dd->phys_base +
  2172. SHA_REG_DIN(0);
  2173. dd->dma_lch_in.dma_conf.src_maxburst = 1;
  2174. dd->dma_lch_in.dma_conf.src_addr_width =
  2175. DMA_SLAVE_BUSWIDTH_4_BYTES;
  2176. dd->dma_lch_in.dma_conf.dst_maxburst = 1;
  2177. dd->dma_lch_in.dma_conf.dst_addr_width =
  2178. DMA_SLAVE_BUSWIDTH_4_BYTES;
  2179. dd->dma_lch_in.dma_conf.device_fc = false;
  2180. return 0;
  2181. }
  2182. static void atmel_sha_dma_cleanup(struct atmel_sha_dev *dd)
  2183. {
  2184. dma_release_channel(dd->dma_lch_in.chan);
  2185. }
  2186. static void atmel_sha_get_cap(struct atmel_sha_dev *dd)
  2187. {
  2188. dd->caps.has_dma = 0;
  2189. dd->caps.has_dualbuff = 0;
  2190. dd->caps.has_sha224 = 0;
  2191. dd->caps.has_sha_384_512 = 0;
  2192. dd->caps.has_uihv = 0;
  2193. dd->caps.has_hmac = 0;
  2194. /* keep only major version number */
  2195. switch (dd->hw_version & 0xff0) {
  2196. case 0x510:
  2197. dd->caps.has_dma = 1;
  2198. dd->caps.has_dualbuff = 1;
  2199. dd->caps.has_sha224 = 1;
  2200. dd->caps.has_sha_384_512 = 1;
  2201. dd->caps.has_uihv = 1;
  2202. dd->caps.has_hmac = 1;
  2203. break;
  2204. case 0x420:
  2205. dd->caps.has_dma = 1;
  2206. dd->caps.has_dualbuff = 1;
  2207. dd->caps.has_sha224 = 1;
  2208. dd->caps.has_sha_384_512 = 1;
  2209. dd->caps.has_uihv = 1;
  2210. break;
  2211. case 0x410:
  2212. dd->caps.has_dma = 1;
  2213. dd->caps.has_dualbuff = 1;
  2214. dd->caps.has_sha224 = 1;
  2215. dd->caps.has_sha_384_512 = 1;
  2216. break;
  2217. case 0x400:
  2218. dd->caps.has_dma = 1;
  2219. dd->caps.has_dualbuff = 1;
  2220. dd->caps.has_sha224 = 1;
  2221. break;
  2222. case 0x320:
  2223. break;
  2224. default:
  2225. dev_warn(dd->dev,
  2226. "Unmanaged sha version, set minimum capabilities\n");
  2227. break;
  2228. }
  2229. }
  2230. #if defined(CONFIG_OF)
  2231. static const struct of_device_id atmel_sha_dt_ids[] = {
  2232. { .compatible = "atmel,at91sam9g46-sha" },
  2233. { /* sentinel */ }
  2234. };
  2235. MODULE_DEVICE_TABLE(of, atmel_sha_dt_ids);
  2236. static struct crypto_platform_data *atmel_sha_of_init(struct platform_device *pdev)
  2237. {
  2238. struct device_node *np = pdev->dev.of_node;
  2239. struct crypto_platform_data *pdata;
  2240. if (!np) {
  2241. dev_err(&pdev->dev, "device node not found\n");
  2242. return ERR_PTR(-EINVAL);
  2243. }
  2244. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  2245. if (!pdata)
  2246. return ERR_PTR(-ENOMEM);
  2247. pdata->dma_slave = devm_kzalloc(&pdev->dev,
  2248. sizeof(*(pdata->dma_slave)),
  2249. GFP_KERNEL);
  2250. if (!pdata->dma_slave)
  2251. return ERR_PTR(-ENOMEM);
  2252. return pdata;
  2253. }
  2254. #else /* CONFIG_OF */
  2255. static inline struct crypto_platform_data *atmel_sha_of_init(struct platform_device *dev)
  2256. {
  2257. return ERR_PTR(-EINVAL);
  2258. }
  2259. #endif
  2260. static int atmel_sha_probe(struct platform_device *pdev)
  2261. {
  2262. struct atmel_sha_dev *sha_dd;
  2263. struct crypto_platform_data *pdata;
  2264. struct device *dev = &pdev->dev;
  2265. struct resource *sha_res;
  2266. int err;
  2267. sha_dd = devm_kzalloc(&pdev->dev, sizeof(*sha_dd), GFP_KERNEL);
  2268. if (sha_dd == NULL) {
  2269. err = -ENOMEM;
  2270. goto sha_dd_err;
  2271. }
  2272. sha_dd->dev = dev;
  2273. platform_set_drvdata(pdev, sha_dd);
  2274. INIT_LIST_HEAD(&sha_dd->list);
  2275. spin_lock_init(&sha_dd->lock);
  2276. tasklet_init(&sha_dd->done_task, atmel_sha_done_task,
  2277. (unsigned long)sha_dd);
  2278. tasklet_init(&sha_dd->queue_task, atmel_sha_queue_task,
  2279. (unsigned long)sha_dd);
  2280. crypto_init_queue(&sha_dd->queue, ATMEL_SHA_QUEUE_LENGTH);
  2281. /* Get the base address */
  2282. sha_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2283. if (!sha_res) {
  2284. dev_err(dev, "no MEM resource info\n");
  2285. err = -ENODEV;
  2286. goto res_err;
  2287. }
  2288. sha_dd->phys_base = sha_res->start;
  2289. /* Get the IRQ */
  2290. sha_dd->irq = platform_get_irq(pdev, 0);
  2291. if (sha_dd->irq < 0) {
  2292. dev_err(dev, "no IRQ resource info\n");
  2293. err = sha_dd->irq;
  2294. goto res_err;
  2295. }
  2296. err = devm_request_irq(&pdev->dev, sha_dd->irq, atmel_sha_irq,
  2297. IRQF_SHARED, "atmel-sha", sha_dd);
  2298. if (err) {
  2299. dev_err(dev, "unable to request sha irq.\n");
  2300. goto res_err;
  2301. }
  2302. /* Initializing the clock */
  2303. sha_dd->iclk = devm_clk_get(&pdev->dev, "sha_clk");
  2304. if (IS_ERR(sha_dd->iclk)) {
  2305. dev_err(dev, "clock initialization failed.\n");
  2306. err = PTR_ERR(sha_dd->iclk);
  2307. goto res_err;
  2308. }
  2309. sha_dd->io_base = devm_ioremap_resource(&pdev->dev, sha_res);
  2310. if (IS_ERR(sha_dd->io_base)) {
  2311. dev_err(dev, "can't ioremap\n");
  2312. err = PTR_ERR(sha_dd->io_base);
  2313. goto res_err;
  2314. }
  2315. err = clk_prepare(sha_dd->iclk);
  2316. if (err)
  2317. goto res_err;
  2318. atmel_sha_hw_version_init(sha_dd);
  2319. atmel_sha_get_cap(sha_dd);
  2320. if (sha_dd->caps.has_dma) {
  2321. pdata = pdev->dev.platform_data;
  2322. if (!pdata) {
  2323. pdata = atmel_sha_of_init(pdev);
  2324. if (IS_ERR(pdata)) {
  2325. dev_err(&pdev->dev, "platform data not available\n");
  2326. err = PTR_ERR(pdata);
  2327. goto iclk_unprepare;
  2328. }
  2329. }
  2330. if (!pdata->dma_slave) {
  2331. err = -ENXIO;
  2332. goto iclk_unprepare;
  2333. }
  2334. err = atmel_sha_dma_init(sha_dd, pdata);
  2335. if (err)
  2336. goto err_sha_dma;
  2337. dev_info(dev, "using %s for DMA transfers\n",
  2338. dma_chan_name(sha_dd->dma_lch_in.chan));
  2339. }
  2340. spin_lock(&atmel_sha.lock);
  2341. list_add_tail(&sha_dd->list, &atmel_sha.dev_list);
  2342. spin_unlock(&atmel_sha.lock);
  2343. err = atmel_sha_register_algs(sha_dd);
  2344. if (err)
  2345. goto err_algs;
  2346. dev_info(dev, "Atmel SHA1/SHA256%s%s\n",
  2347. sha_dd->caps.has_sha224 ? "/SHA224" : "",
  2348. sha_dd->caps.has_sha_384_512 ? "/SHA384/SHA512" : "");
  2349. return 0;
  2350. err_algs:
  2351. spin_lock(&atmel_sha.lock);
  2352. list_del(&sha_dd->list);
  2353. spin_unlock(&atmel_sha.lock);
  2354. if (sha_dd->caps.has_dma)
  2355. atmel_sha_dma_cleanup(sha_dd);
  2356. err_sha_dma:
  2357. iclk_unprepare:
  2358. clk_unprepare(sha_dd->iclk);
  2359. res_err:
  2360. tasklet_kill(&sha_dd->queue_task);
  2361. tasklet_kill(&sha_dd->done_task);
  2362. sha_dd_err:
  2363. dev_err(dev, "initialization failed.\n");
  2364. return err;
  2365. }
  2366. static int atmel_sha_remove(struct platform_device *pdev)
  2367. {
  2368. struct atmel_sha_dev *sha_dd;
  2369. sha_dd = platform_get_drvdata(pdev);
  2370. if (!sha_dd)
  2371. return -ENODEV;
  2372. spin_lock(&atmel_sha.lock);
  2373. list_del(&sha_dd->list);
  2374. spin_unlock(&atmel_sha.lock);
  2375. atmel_sha_unregister_algs(sha_dd);
  2376. tasklet_kill(&sha_dd->queue_task);
  2377. tasklet_kill(&sha_dd->done_task);
  2378. if (sha_dd->caps.has_dma)
  2379. atmel_sha_dma_cleanup(sha_dd);
  2380. clk_unprepare(sha_dd->iclk);
  2381. return 0;
  2382. }
  2383. static struct platform_driver atmel_sha_driver = {
  2384. .probe = atmel_sha_probe,
  2385. .remove = atmel_sha_remove,
  2386. .driver = {
  2387. .name = "atmel_sha",
  2388. .of_match_table = of_match_ptr(atmel_sha_dt_ids),
  2389. },
  2390. };
  2391. module_platform_driver(atmel_sha_driver);
  2392. MODULE_DESCRIPTION("Atmel SHA (1/256/224/384/512) hw acceleration support.");
  2393. MODULE_LICENSE("GPL v2");
  2394. MODULE_AUTHOR("Nicolas Royer - Eukréa Electromatique");