atmel-sha.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597
  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. /* SHA flags */
  42. #define SHA_FLAGS_BUSY BIT(0)
  43. #define SHA_FLAGS_FINAL BIT(1)
  44. #define SHA_FLAGS_DMA_ACTIVE BIT(2)
  45. #define SHA_FLAGS_OUTPUT_READY BIT(3)
  46. #define SHA_FLAGS_INIT BIT(4)
  47. #define SHA_FLAGS_CPU BIT(5)
  48. #define SHA_FLAGS_DMA_READY BIT(6)
  49. #define SHA_FLAGS_FINUP BIT(16)
  50. #define SHA_FLAGS_SG BIT(17)
  51. #define SHA_FLAGS_ALGO_MASK GENMASK(22, 18)
  52. #define SHA_FLAGS_SHA1 BIT(18)
  53. #define SHA_FLAGS_SHA224 BIT(19)
  54. #define SHA_FLAGS_SHA256 BIT(20)
  55. #define SHA_FLAGS_SHA384 BIT(21)
  56. #define SHA_FLAGS_SHA512 BIT(22)
  57. #define SHA_FLAGS_ERROR BIT(23)
  58. #define SHA_FLAGS_PAD BIT(24)
  59. #define SHA_FLAGS_RESTORE BIT(25)
  60. #define SHA_OP_UPDATE 1
  61. #define SHA_OP_FINAL 2
  62. #define SHA_BUFFER_LEN (PAGE_SIZE / 16)
  63. #define ATMEL_SHA_DMA_THRESHOLD 56
  64. struct atmel_sha_caps {
  65. bool has_dma;
  66. bool has_dualbuff;
  67. bool has_sha224;
  68. bool has_sha_384_512;
  69. bool has_uihv;
  70. };
  71. struct atmel_sha_dev;
  72. /*
  73. * .statesize = sizeof(struct atmel_sha_reqctx) must be <= PAGE_SIZE / 8 as
  74. * tested by the ahash_prepare_alg() function.
  75. */
  76. struct atmel_sha_reqctx {
  77. struct atmel_sha_dev *dd;
  78. unsigned long flags;
  79. unsigned long op;
  80. u8 digest[SHA512_DIGEST_SIZE] __aligned(sizeof(u32));
  81. u64 digcnt[2];
  82. size_t bufcnt;
  83. size_t buflen;
  84. dma_addr_t dma_addr;
  85. /* walk state */
  86. struct scatterlist *sg;
  87. unsigned int offset; /* offset in current sg */
  88. unsigned int total; /* total request */
  89. size_t block_size;
  90. u8 buffer[SHA_BUFFER_LEN + SHA512_BLOCK_SIZE] __aligned(sizeof(u32));
  91. };
  92. struct atmel_sha_ctx {
  93. struct atmel_sha_dev *dd;
  94. unsigned long flags;
  95. };
  96. #define ATMEL_SHA_QUEUE_LENGTH 50
  97. struct atmel_sha_dma {
  98. struct dma_chan *chan;
  99. struct dma_slave_config dma_conf;
  100. };
  101. struct atmel_sha_dev {
  102. struct list_head list;
  103. unsigned long phys_base;
  104. struct device *dev;
  105. struct clk *iclk;
  106. int irq;
  107. void __iomem *io_base;
  108. spinlock_t lock;
  109. int err;
  110. struct tasklet_struct done_task;
  111. struct tasklet_struct queue_task;
  112. unsigned long flags;
  113. struct crypto_queue queue;
  114. struct ahash_request *req;
  115. struct atmel_sha_dma dma_lch_in;
  116. struct atmel_sha_caps caps;
  117. u32 hw_version;
  118. };
  119. struct atmel_sha_drv {
  120. struct list_head dev_list;
  121. spinlock_t lock;
  122. };
  123. static struct atmel_sha_drv atmel_sha = {
  124. .dev_list = LIST_HEAD_INIT(atmel_sha.dev_list),
  125. .lock = __SPIN_LOCK_UNLOCKED(atmel_sha.lock),
  126. };
  127. static inline u32 atmel_sha_read(struct atmel_sha_dev *dd, u32 offset)
  128. {
  129. return readl_relaxed(dd->io_base + offset);
  130. }
  131. static inline void atmel_sha_write(struct atmel_sha_dev *dd,
  132. u32 offset, u32 value)
  133. {
  134. writel_relaxed(value, dd->io_base + offset);
  135. }
  136. static size_t atmel_sha_append_sg(struct atmel_sha_reqctx *ctx)
  137. {
  138. size_t count;
  139. while ((ctx->bufcnt < ctx->buflen) && ctx->total) {
  140. count = min(ctx->sg->length - ctx->offset, ctx->total);
  141. count = min(count, ctx->buflen - ctx->bufcnt);
  142. if (count <= 0) {
  143. /*
  144. * Check if count <= 0 because the buffer is full or
  145. * because the sg length is 0. In the latest case,
  146. * check if there is another sg in the list, a 0 length
  147. * sg doesn't necessarily mean the end of the sg list.
  148. */
  149. if ((ctx->sg->length == 0) && !sg_is_last(ctx->sg)) {
  150. ctx->sg = sg_next(ctx->sg);
  151. continue;
  152. } else {
  153. break;
  154. }
  155. }
  156. scatterwalk_map_and_copy(ctx->buffer + ctx->bufcnt, ctx->sg,
  157. ctx->offset, count, 0);
  158. ctx->bufcnt += count;
  159. ctx->offset += count;
  160. ctx->total -= count;
  161. if (ctx->offset == ctx->sg->length) {
  162. ctx->sg = sg_next(ctx->sg);
  163. if (ctx->sg)
  164. ctx->offset = 0;
  165. else
  166. ctx->total = 0;
  167. }
  168. }
  169. return 0;
  170. }
  171. /*
  172. * The purpose of this padding is to ensure that the padded message is a
  173. * multiple of 512 bits (SHA1/SHA224/SHA256) or 1024 bits (SHA384/SHA512).
  174. * The bit "1" is appended at the end of the message followed by
  175. * "padlen-1" zero bits. Then a 64 bits block (SHA1/SHA224/SHA256) or
  176. * 128 bits block (SHA384/SHA512) equals to the message length in bits
  177. * is appended.
  178. *
  179. * For SHA1/SHA224/SHA256, padlen is calculated as followed:
  180. * - if message length < 56 bytes then padlen = 56 - message length
  181. * - else padlen = 64 + 56 - message length
  182. *
  183. * For SHA384/SHA512, padlen is calculated as followed:
  184. * - if message length < 112 bytes then padlen = 112 - message length
  185. * - else padlen = 128 + 112 - message length
  186. */
  187. static void atmel_sha_fill_padding(struct atmel_sha_reqctx *ctx, int length)
  188. {
  189. unsigned int index, padlen;
  190. u64 bits[2];
  191. u64 size[2];
  192. size[0] = ctx->digcnt[0];
  193. size[1] = ctx->digcnt[1];
  194. size[0] += ctx->bufcnt;
  195. if (size[0] < ctx->bufcnt)
  196. size[1]++;
  197. size[0] += length;
  198. if (size[0] < length)
  199. size[1]++;
  200. bits[1] = cpu_to_be64(size[0] << 3);
  201. bits[0] = cpu_to_be64(size[1] << 3 | size[0] >> 61);
  202. if (ctx->flags & (SHA_FLAGS_SHA384 | SHA_FLAGS_SHA512)) {
  203. index = ctx->bufcnt & 0x7f;
  204. padlen = (index < 112) ? (112 - index) : ((128+112) - index);
  205. *(ctx->buffer + ctx->bufcnt) = 0x80;
  206. memset(ctx->buffer + ctx->bufcnt + 1, 0, padlen-1);
  207. memcpy(ctx->buffer + ctx->bufcnt + padlen, bits, 16);
  208. ctx->bufcnt += padlen + 16;
  209. ctx->flags |= SHA_FLAGS_PAD;
  210. } else {
  211. index = ctx->bufcnt & 0x3f;
  212. padlen = (index < 56) ? (56 - index) : ((64+56) - index);
  213. *(ctx->buffer + ctx->bufcnt) = 0x80;
  214. memset(ctx->buffer + ctx->bufcnt + 1, 0, padlen-1);
  215. memcpy(ctx->buffer + ctx->bufcnt + padlen, &bits[1], 8);
  216. ctx->bufcnt += padlen + 8;
  217. ctx->flags |= SHA_FLAGS_PAD;
  218. }
  219. }
  220. static int atmel_sha_init(struct ahash_request *req)
  221. {
  222. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  223. struct atmel_sha_ctx *tctx = crypto_ahash_ctx(tfm);
  224. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  225. struct atmel_sha_dev *dd = NULL;
  226. struct atmel_sha_dev *tmp;
  227. spin_lock_bh(&atmel_sha.lock);
  228. if (!tctx->dd) {
  229. list_for_each_entry(tmp, &atmel_sha.dev_list, list) {
  230. dd = tmp;
  231. break;
  232. }
  233. tctx->dd = dd;
  234. } else {
  235. dd = tctx->dd;
  236. }
  237. spin_unlock_bh(&atmel_sha.lock);
  238. ctx->dd = dd;
  239. ctx->flags = 0;
  240. dev_dbg(dd->dev, "init: digest size: %d\n",
  241. crypto_ahash_digestsize(tfm));
  242. switch (crypto_ahash_digestsize(tfm)) {
  243. case SHA1_DIGEST_SIZE:
  244. ctx->flags |= SHA_FLAGS_SHA1;
  245. ctx->block_size = SHA1_BLOCK_SIZE;
  246. break;
  247. case SHA224_DIGEST_SIZE:
  248. ctx->flags |= SHA_FLAGS_SHA224;
  249. ctx->block_size = SHA224_BLOCK_SIZE;
  250. break;
  251. case SHA256_DIGEST_SIZE:
  252. ctx->flags |= SHA_FLAGS_SHA256;
  253. ctx->block_size = SHA256_BLOCK_SIZE;
  254. break;
  255. case SHA384_DIGEST_SIZE:
  256. ctx->flags |= SHA_FLAGS_SHA384;
  257. ctx->block_size = SHA384_BLOCK_SIZE;
  258. break;
  259. case SHA512_DIGEST_SIZE:
  260. ctx->flags |= SHA_FLAGS_SHA512;
  261. ctx->block_size = SHA512_BLOCK_SIZE;
  262. break;
  263. default:
  264. return -EINVAL;
  265. break;
  266. }
  267. ctx->bufcnt = 0;
  268. ctx->digcnt[0] = 0;
  269. ctx->digcnt[1] = 0;
  270. ctx->buflen = SHA_BUFFER_LEN;
  271. return 0;
  272. }
  273. static void atmel_sha_write_ctrl(struct atmel_sha_dev *dd, int dma)
  274. {
  275. struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
  276. u32 valmr = SHA_MR_MODE_AUTO;
  277. unsigned int i, hashsize = 0;
  278. if (likely(dma)) {
  279. if (!dd->caps.has_dma)
  280. atmel_sha_write(dd, SHA_IER, SHA_INT_TXBUFE);
  281. valmr = SHA_MR_MODE_PDC;
  282. if (dd->caps.has_dualbuff)
  283. valmr |= SHA_MR_DUALBUFF;
  284. } else {
  285. atmel_sha_write(dd, SHA_IER, SHA_INT_DATARDY);
  286. }
  287. switch (ctx->flags & SHA_FLAGS_ALGO_MASK) {
  288. case SHA_FLAGS_SHA1:
  289. valmr |= SHA_MR_ALGO_SHA1;
  290. hashsize = SHA1_DIGEST_SIZE;
  291. break;
  292. case SHA_FLAGS_SHA224:
  293. valmr |= SHA_MR_ALGO_SHA224;
  294. hashsize = SHA256_DIGEST_SIZE;
  295. break;
  296. case SHA_FLAGS_SHA256:
  297. valmr |= SHA_MR_ALGO_SHA256;
  298. hashsize = SHA256_DIGEST_SIZE;
  299. break;
  300. case SHA_FLAGS_SHA384:
  301. valmr |= SHA_MR_ALGO_SHA384;
  302. hashsize = SHA512_DIGEST_SIZE;
  303. break;
  304. case SHA_FLAGS_SHA512:
  305. valmr |= SHA_MR_ALGO_SHA512;
  306. hashsize = SHA512_DIGEST_SIZE;
  307. break;
  308. default:
  309. break;
  310. }
  311. /* Setting CR_FIRST only for the first iteration */
  312. if (!(ctx->digcnt[0] || ctx->digcnt[1])) {
  313. atmel_sha_write(dd, SHA_CR, SHA_CR_FIRST);
  314. } else if (dd->caps.has_uihv && (ctx->flags & SHA_FLAGS_RESTORE)) {
  315. const u32 *hash = (const u32 *)ctx->digest;
  316. /*
  317. * Restore the hardware context: update the User Initialize
  318. * Hash Value (UIHV) with the value saved when the latest
  319. * 'update' operation completed on this very same crypto
  320. * request.
  321. */
  322. ctx->flags &= ~SHA_FLAGS_RESTORE;
  323. atmel_sha_write(dd, SHA_CR, SHA_CR_WUIHV);
  324. for (i = 0; i < hashsize / sizeof(u32); ++i)
  325. atmel_sha_write(dd, SHA_REG_DIN(i), hash[i]);
  326. atmel_sha_write(dd, SHA_CR, SHA_CR_FIRST);
  327. valmr |= SHA_MR_UIHV;
  328. }
  329. /*
  330. * WARNING: If the UIHV feature is not available, the hardware CANNOT
  331. * process concurrent requests: the internal registers used to store
  332. * the hash/digest are still set to the partial digest output values
  333. * computed during the latest round.
  334. */
  335. atmel_sha_write(dd, SHA_MR, valmr);
  336. }
  337. static int atmel_sha_xmit_cpu(struct atmel_sha_dev *dd, const u8 *buf,
  338. size_t length, int final)
  339. {
  340. struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
  341. int count, len32;
  342. const u32 *buffer = (const u32 *)buf;
  343. dev_dbg(dd->dev, "xmit_cpu: digcnt: 0x%llx 0x%llx, length: %d, final: %d\n",
  344. ctx->digcnt[1], ctx->digcnt[0], length, final);
  345. atmel_sha_write_ctrl(dd, 0);
  346. /* should be non-zero before next lines to disable clocks later */
  347. ctx->digcnt[0] += length;
  348. if (ctx->digcnt[0] < length)
  349. ctx->digcnt[1]++;
  350. if (final)
  351. dd->flags |= SHA_FLAGS_FINAL; /* catch last interrupt */
  352. len32 = DIV_ROUND_UP(length, sizeof(u32));
  353. dd->flags |= SHA_FLAGS_CPU;
  354. for (count = 0; count < len32; count++)
  355. atmel_sha_write(dd, SHA_REG_DIN(count), buffer[count]);
  356. return -EINPROGRESS;
  357. }
  358. static int atmel_sha_xmit_pdc(struct atmel_sha_dev *dd, dma_addr_t dma_addr1,
  359. size_t length1, dma_addr_t dma_addr2, size_t length2, int final)
  360. {
  361. struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
  362. int len32;
  363. dev_dbg(dd->dev, "xmit_pdc: digcnt: 0x%llx 0x%llx, length: %d, final: %d\n",
  364. ctx->digcnt[1], ctx->digcnt[0], length1, final);
  365. len32 = DIV_ROUND_UP(length1, sizeof(u32));
  366. atmel_sha_write(dd, SHA_PTCR, SHA_PTCR_TXTDIS);
  367. atmel_sha_write(dd, SHA_TPR, dma_addr1);
  368. atmel_sha_write(dd, SHA_TCR, len32);
  369. len32 = DIV_ROUND_UP(length2, sizeof(u32));
  370. atmel_sha_write(dd, SHA_TNPR, dma_addr2);
  371. atmel_sha_write(dd, SHA_TNCR, len32);
  372. atmel_sha_write_ctrl(dd, 1);
  373. /* should be non-zero before next lines to disable clocks later */
  374. ctx->digcnt[0] += length1;
  375. if (ctx->digcnt[0] < length1)
  376. ctx->digcnt[1]++;
  377. if (final)
  378. dd->flags |= SHA_FLAGS_FINAL; /* catch last interrupt */
  379. dd->flags |= SHA_FLAGS_DMA_ACTIVE;
  380. /* Start DMA transfer */
  381. atmel_sha_write(dd, SHA_PTCR, SHA_PTCR_TXTEN);
  382. return -EINPROGRESS;
  383. }
  384. static void atmel_sha_dma_callback(void *data)
  385. {
  386. struct atmel_sha_dev *dd = data;
  387. /* dma_lch_in - completed - wait DATRDY */
  388. atmel_sha_write(dd, SHA_IER, SHA_INT_DATARDY);
  389. }
  390. static int atmel_sha_xmit_dma(struct atmel_sha_dev *dd, dma_addr_t dma_addr1,
  391. size_t length1, dma_addr_t dma_addr2, size_t length2, int final)
  392. {
  393. struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
  394. struct dma_async_tx_descriptor *in_desc;
  395. struct scatterlist sg[2];
  396. dev_dbg(dd->dev, "xmit_dma: digcnt: 0x%llx 0x%llx, length: %d, final: %d\n",
  397. ctx->digcnt[1], ctx->digcnt[0], length1, final);
  398. dd->dma_lch_in.dma_conf.src_maxburst = 16;
  399. dd->dma_lch_in.dma_conf.dst_maxburst = 16;
  400. dmaengine_slave_config(dd->dma_lch_in.chan, &dd->dma_lch_in.dma_conf);
  401. if (length2) {
  402. sg_init_table(sg, 2);
  403. sg_dma_address(&sg[0]) = dma_addr1;
  404. sg_dma_len(&sg[0]) = length1;
  405. sg_dma_address(&sg[1]) = dma_addr2;
  406. sg_dma_len(&sg[1]) = length2;
  407. in_desc = dmaengine_prep_slave_sg(dd->dma_lch_in.chan, sg, 2,
  408. DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  409. } else {
  410. sg_init_table(sg, 1);
  411. sg_dma_address(&sg[0]) = dma_addr1;
  412. sg_dma_len(&sg[0]) = length1;
  413. in_desc = dmaengine_prep_slave_sg(dd->dma_lch_in.chan, sg, 1,
  414. DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  415. }
  416. if (!in_desc)
  417. return -EINVAL;
  418. in_desc->callback = atmel_sha_dma_callback;
  419. in_desc->callback_param = dd;
  420. atmel_sha_write_ctrl(dd, 1);
  421. /* should be non-zero before next lines to disable clocks later */
  422. ctx->digcnt[0] += length1;
  423. if (ctx->digcnt[0] < length1)
  424. ctx->digcnt[1]++;
  425. if (final)
  426. dd->flags |= SHA_FLAGS_FINAL; /* catch last interrupt */
  427. dd->flags |= SHA_FLAGS_DMA_ACTIVE;
  428. /* Start DMA transfer */
  429. dmaengine_submit(in_desc);
  430. dma_async_issue_pending(dd->dma_lch_in.chan);
  431. return -EINPROGRESS;
  432. }
  433. static int atmel_sha_xmit_start(struct atmel_sha_dev *dd, dma_addr_t dma_addr1,
  434. size_t length1, dma_addr_t dma_addr2, size_t length2, int final)
  435. {
  436. if (dd->caps.has_dma)
  437. return atmel_sha_xmit_dma(dd, dma_addr1, length1,
  438. dma_addr2, length2, final);
  439. else
  440. return atmel_sha_xmit_pdc(dd, dma_addr1, length1,
  441. dma_addr2, length2, final);
  442. }
  443. static int atmel_sha_update_cpu(struct atmel_sha_dev *dd)
  444. {
  445. struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
  446. int bufcnt;
  447. atmel_sha_append_sg(ctx);
  448. atmel_sha_fill_padding(ctx, 0);
  449. bufcnt = ctx->bufcnt;
  450. ctx->bufcnt = 0;
  451. return atmel_sha_xmit_cpu(dd, ctx->buffer, bufcnt, 1);
  452. }
  453. static int atmel_sha_xmit_dma_map(struct atmel_sha_dev *dd,
  454. struct atmel_sha_reqctx *ctx,
  455. size_t length, int final)
  456. {
  457. ctx->dma_addr = dma_map_single(dd->dev, ctx->buffer,
  458. ctx->buflen + ctx->block_size, DMA_TO_DEVICE);
  459. if (dma_mapping_error(dd->dev, ctx->dma_addr)) {
  460. dev_err(dd->dev, "dma %u bytes error\n", ctx->buflen +
  461. ctx->block_size);
  462. return -EINVAL;
  463. }
  464. ctx->flags &= ~SHA_FLAGS_SG;
  465. /* next call does not fail... so no unmap in the case of error */
  466. return atmel_sha_xmit_start(dd, ctx->dma_addr, length, 0, 0, final);
  467. }
  468. static int atmel_sha_update_dma_slow(struct atmel_sha_dev *dd)
  469. {
  470. struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
  471. unsigned int final;
  472. size_t count;
  473. atmel_sha_append_sg(ctx);
  474. final = (ctx->flags & SHA_FLAGS_FINUP) && !ctx->total;
  475. dev_dbg(dd->dev, "slow: bufcnt: %u, digcnt: 0x%llx 0x%llx, final: %d\n",
  476. ctx->bufcnt, ctx->digcnt[1], ctx->digcnt[0], final);
  477. if (final)
  478. atmel_sha_fill_padding(ctx, 0);
  479. if (final || (ctx->bufcnt == ctx->buflen)) {
  480. count = ctx->bufcnt;
  481. ctx->bufcnt = 0;
  482. return atmel_sha_xmit_dma_map(dd, ctx, count, final);
  483. }
  484. return 0;
  485. }
  486. static int atmel_sha_update_dma_start(struct atmel_sha_dev *dd)
  487. {
  488. struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
  489. unsigned int length, final, tail;
  490. struct scatterlist *sg;
  491. unsigned int count;
  492. if (!ctx->total)
  493. return 0;
  494. if (ctx->bufcnt || ctx->offset)
  495. return atmel_sha_update_dma_slow(dd);
  496. dev_dbg(dd->dev, "fast: digcnt: 0x%llx 0x%llx, bufcnt: %u, total: %u\n",
  497. ctx->digcnt[1], ctx->digcnt[0], ctx->bufcnt, ctx->total);
  498. sg = ctx->sg;
  499. if (!IS_ALIGNED(sg->offset, sizeof(u32)))
  500. return atmel_sha_update_dma_slow(dd);
  501. if (!sg_is_last(sg) && !IS_ALIGNED(sg->length, ctx->block_size))
  502. /* size is not ctx->block_size aligned */
  503. return atmel_sha_update_dma_slow(dd);
  504. length = min(ctx->total, sg->length);
  505. if (sg_is_last(sg)) {
  506. if (!(ctx->flags & SHA_FLAGS_FINUP)) {
  507. /* not last sg must be ctx->block_size aligned */
  508. tail = length & (ctx->block_size - 1);
  509. length -= tail;
  510. }
  511. }
  512. ctx->total -= length;
  513. ctx->offset = length; /* offset where to start slow */
  514. final = (ctx->flags & SHA_FLAGS_FINUP) && !ctx->total;
  515. /* Add padding */
  516. if (final) {
  517. tail = length & (ctx->block_size - 1);
  518. length -= tail;
  519. ctx->total += tail;
  520. ctx->offset = length; /* offset where to start slow */
  521. sg = ctx->sg;
  522. atmel_sha_append_sg(ctx);
  523. atmel_sha_fill_padding(ctx, length);
  524. ctx->dma_addr = dma_map_single(dd->dev, ctx->buffer,
  525. ctx->buflen + ctx->block_size, DMA_TO_DEVICE);
  526. if (dma_mapping_error(dd->dev, ctx->dma_addr)) {
  527. dev_err(dd->dev, "dma %u bytes error\n",
  528. ctx->buflen + ctx->block_size);
  529. return -EINVAL;
  530. }
  531. if (length == 0) {
  532. ctx->flags &= ~SHA_FLAGS_SG;
  533. count = ctx->bufcnt;
  534. ctx->bufcnt = 0;
  535. return atmel_sha_xmit_start(dd, ctx->dma_addr, count, 0,
  536. 0, final);
  537. } else {
  538. ctx->sg = sg;
  539. if (!dma_map_sg(dd->dev, ctx->sg, 1,
  540. DMA_TO_DEVICE)) {
  541. dev_err(dd->dev, "dma_map_sg error\n");
  542. return -EINVAL;
  543. }
  544. ctx->flags |= SHA_FLAGS_SG;
  545. count = ctx->bufcnt;
  546. ctx->bufcnt = 0;
  547. return atmel_sha_xmit_start(dd, sg_dma_address(ctx->sg),
  548. length, ctx->dma_addr, count, final);
  549. }
  550. }
  551. if (!dma_map_sg(dd->dev, ctx->sg, 1, DMA_TO_DEVICE)) {
  552. dev_err(dd->dev, "dma_map_sg error\n");
  553. return -EINVAL;
  554. }
  555. ctx->flags |= SHA_FLAGS_SG;
  556. /* next call does not fail... so no unmap in the case of error */
  557. return atmel_sha_xmit_start(dd, sg_dma_address(ctx->sg), length, 0,
  558. 0, final);
  559. }
  560. static int atmel_sha_update_dma_stop(struct atmel_sha_dev *dd)
  561. {
  562. struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
  563. if (ctx->flags & SHA_FLAGS_SG) {
  564. dma_unmap_sg(dd->dev, ctx->sg, 1, DMA_TO_DEVICE);
  565. if (ctx->sg->length == ctx->offset) {
  566. ctx->sg = sg_next(ctx->sg);
  567. if (ctx->sg)
  568. ctx->offset = 0;
  569. }
  570. if (ctx->flags & SHA_FLAGS_PAD) {
  571. dma_unmap_single(dd->dev, ctx->dma_addr,
  572. ctx->buflen + ctx->block_size, DMA_TO_DEVICE);
  573. }
  574. } else {
  575. dma_unmap_single(dd->dev, ctx->dma_addr, ctx->buflen +
  576. ctx->block_size, DMA_TO_DEVICE);
  577. }
  578. return 0;
  579. }
  580. static int atmel_sha_update_req(struct atmel_sha_dev *dd)
  581. {
  582. struct ahash_request *req = dd->req;
  583. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  584. int err;
  585. dev_dbg(dd->dev, "update_req: total: %u, digcnt: 0x%llx 0x%llx\n",
  586. ctx->total, ctx->digcnt[1], ctx->digcnt[0]);
  587. if (ctx->flags & SHA_FLAGS_CPU)
  588. err = atmel_sha_update_cpu(dd);
  589. else
  590. err = atmel_sha_update_dma_start(dd);
  591. /* wait for dma completion before can take more data */
  592. dev_dbg(dd->dev, "update: err: %d, digcnt: 0x%llx 0%llx\n",
  593. err, ctx->digcnt[1], ctx->digcnt[0]);
  594. return err;
  595. }
  596. static int atmel_sha_final_req(struct atmel_sha_dev *dd)
  597. {
  598. struct ahash_request *req = dd->req;
  599. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  600. int err = 0;
  601. int count;
  602. if (ctx->bufcnt >= ATMEL_SHA_DMA_THRESHOLD) {
  603. atmel_sha_fill_padding(ctx, 0);
  604. count = ctx->bufcnt;
  605. ctx->bufcnt = 0;
  606. err = atmel_sha_xmit_dma_map(dd, ctx, count, 1);
  607. }
  608. /* faster to handle last block with cpu */
  609. else {
  610. atmel_sha_fill_padding(ctx, 0);
  611. count = ctx->bufcnt;
  612. ctx->bufcnt = 0;
  613. err = atmel_sha_xmit_cpu(dd, ctx->buffer, count, 1);
  614. }
  615. dev_dbg(dd->dev, "final_req: err: %d\n", err);
  616. return err;
  617. }
  618. static void atmel_sha_copy_hash(struct ahash_request *req)
  619. {
  620. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  621. u32 *hash = (u32 *)ctx->digest;
  622. unsigned int i, hashsize;
  623. switch (ctx->flags & SHA_FLAGS_ALGO_MASK) {
  624. case SHA_FLAGS_SHA1:
  625. hashsize = SHA1_DIGEST_SIZE;
  626. break;
  627. case SHA_FLAGS_SHA224:
  628. case SHA_FLAGS_SHA256:
  629. hashsize = SHA256_DIGEST_SIZE;
  630. break;
  631. case SHA_FLAGS_SHA384:
  632. case SHA_FLAGS_SHA512:
  633. hashsize = SHA512_DIGEST_SIZE;
  634. break;
  635. default:
  636. /* Should not happen... */
  637. return;
  638. }
  639. for (i = 0; i < hashsize / sizeof(u32); ++i)
  640. hash[i] = atmel_sha_read(ctx->dd, SHA_REG_DIGEST(i));
  641. ctx->flags |= SHA_FLAGS_RESTORE;
  642. }
  643. static void atmel_sha_copy_ready_hash(struct ahash_request *req)
  644. {
  645. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  646. if (!req->result)
  647. return;
  648. if (ctx->flags & SHA_FLAGS_SHA1)
  649. memcpy(req->result, ctx->digest, SHA1_DIGEST_SIZE);
  650. else if (ctx->flags & SHA_FLAGS_SHA224)
  651. memcpy(req->result, ctx->digest, SHA224_DIGEST_SIZE);
  652. else if (ctx->flags & SHA_FLAGS_SHA256)
  653. memcpy(req->result, ctx->digest, SHA256_DIGEST_SIZE);
  654. else if (ctx->flags & SHA_FLAGS_SHA384)
  655. memcpy(req->result, ctx->digest, SHA384_DIGEST_SIZE);
  656. else
  657. memcpy(req->result, ctx->digest, SHA512_DIGEST_SIZE);
  658. }
  659. static int atmel_sha_finish(struct ahash_request *req)
  660. {
  661. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  662. struct atmel_sha_dev *dd = ctx->dd;
  663. if (ctx->digcnt[0] || ctx->digcnt[1])
  664. atmel_sha_copy_ready_hash(req);
  665. dev_dbg(dd->dev, "digcnt: 0x%llx 0x%llx, bufcnt: %d\n", ctx->digcnt[1],
  666. ctx->digcnt[0], ctx->bufcnt);
  667. return 0;
  668. }
  669. static void atmel_sha_finish_req(struct ahash_request *req, int err)
  670. {
  671. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  672. struct atmel_sha_dev *dd = ctx->dd;
  673. if (!err) {
  674. atmel_sha_copy_hash(req);
  675. if (SHA_FLAGS_FINAL & dd->flags)
  676. err = atmel_sha_finish(req);
  677. } else {
  678. ctx->flags |= SHA_FLAGS_ERROR;
  679. }
  680. /* atomic operation is not needed here */
  681. dd->flags &= ~(SHA_FLAGS_BUSY | SHA_FLAGS_FINAL | SHA_FLAGS_CPU |
  682. SHA_FLAGS_DMA_READY | SHA_FLAGS_OUTPUT_READY);
  683. clk_disable(dd->iclk);
  684. if (req->base.complete)
  685. req->base.complete(&req->base, err);
  686. /* handle new request */
  687. tasklet_schedule(&dd->queue_task);
  688. }
  689. static int atmel_sha_hw_init(struct atmel_sha_dev *dd)
  690. {
  691. int err;
  692. err = clk_enable(dd->iclk);
  693. if (err)
  694. return err;
  695. if (!(SHA_FLAGS_INIT & dd->flags)) {
  696. atmel_sha_write(dd, SHA_CR, SHA_CR_SWRST);
  697. dd->flags |= SHA_FLAGS_INIT;
  698. dd->err = 0;
  699. }
  700. return 0;
  701. }
  702. static inline unsigned int atmel_sha_get_version(struct atmel_sha_dev *dd)
  703. {
  704. return atmel_sha_read(dd, SHA_HW_VERSION) & 0x00000fff;
  705. }
  706. static void atmel_sha_hw_version_init(struct atmel_sha_dev *dd)
  707. {
  708. atmel_sha_hw_init(dd);
  709. dd->hw_version = atmel_sha_get_version(dd);
  710. dev_info(dd->dev,
  711. "version: 0x%x\n", dd->hw_version);
  712. clk_disable(dd->iclk);
  713. }
  714. static int atmel_sha_handle_queue(struct atmel_sha_dev *dd,
  715. struct ahash_request *req)
  716. {
  717. struct crypto_async_request *async_req, *backlog;
  718. struct atmel_sha_reqctx *ctx;
  719. unsigned long flags;
  720. int err = 0, ret = 0;
  721. spin_lock_irqsave(&dd->lock, flags);
  722. if (req)
  723. ret = ahash_enqueue_request(&dd->queue, req);
  724. if (SHA_FLAGS_BUSY & dd->flags) {
  725. spin_unlock_irqrestore(&dd->lock, flags);
  726. return ret;
  727. }
  728. backlog = crypto_get_backlog(&dd->queue);
  729. async_req = crypto_dequeue_request(&dd->queue);
  730. if (async_req)
  731. dd->flags |= SHA_FLAGS_BUSY;
  732. spin_unlock_irqrestore(&dd->lock, flags);
  733. if (!async_req)
  734. return ret;
  735. if (backlog)
  736. backlog->complete(backlog, -EINPROGRESS);
  737. req = ahash_request_cast(async_req);
  738. dd->req = req;
  739. ctx = ahash_request_ctx(req);
  740. dev_dbg(dd->dev, "handling new req, op: %lu, nbytes: %d\n",
  741. ctx->op, req->nbytes);
  742. err = atmel_sha_hw_init(dd);
  743. if (err)
  744. goto err1;
  745. if (ctx->op == SHA_OP_UPDATE) {
  746. err = atmel_sha_update_req(dd);
  747. if (err != -EINPROGRESS && (ctx->flags & SHA_FLAGS_FINUP))
  748. /* no final() after finup() */
  749. err = atmel_sha_final_req(dd);
  750. } else if (ctx->op == SHA_OP_FINAL) {
  751. err = atmel_sha_final_req(dd);
  752. }
  753. err1:
  754. if (err != -EINPROGRESS)
  755. /* done_task will not finish it, so do it here */
  756. atmel_sha_finish_req(req, err);
  757. dev_dbg(dd->dev, "exit, err: %d\n", err);
  758. return ret;
  759. }
  760. static int atmel_sha_enqueue(struct ahash_request *req, unsigned int op)
  761. {
  762. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  763. struct atmel_sha_ctx *tctx = crypto_tfm_ctx(req->base.tfm);
  764. struct atmel_sha_dev *dd = tctx->dd;
  765. ctx->op = op;
  766. return atmel_sha_handle_queue(dd, req);
  767. }
  768. static int atmel_sha_update(struct ahash_request *req)
  769. {
  770. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  771. if (!req->nbytes)
  772. return 0;
  773. ctx->total = req->nbytes;
  774. ctx->sg = req->src;
  775. ctx->offset = 0;
  776. if (ctx->flags & SHA_FLAGS_FINUP) {
  777. if (ctx->bufcnt + ctx->total < ATMEL_SHA_DMA_THRESHOLD)
  778. /* faster to use CPU for short transfers */
  779. ctx->flags |= SHA_FLAGS_CPU;
  780. } else if (ctx->bufcnt + ctx->total < ctx->buflen) {
  781. atmel_sha_append_sg(ctx);
  782. return 0;
  783. }
  784. return atmel_sha_enqueue(req, SHA_OP_UPDATE);
  785. }
  786. static int atmel_sha_final(struct ahash_request *req)
  787. {
  788. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  789. ctx->flags |= SHA_FLAGS_FINUP;
  790. if (ctx->flags & SHA_FLAGS_ERROR)
  791. return 0; /* uncompleted hash is not needed */
  792. if (ctx->flags & SHA_FLAGS_PAD)
  793. /* copy ready hash (+ finalize hmac) */
  794. return atmel_sha_finish(req);
  795. return atmel_sha_enqueue(req, SHA_OP_FINAL);
  796. }
  797. static int atmel_sha_finup(struct ahash_request *req)
  798. {
  799. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  800. int err1, err2;
  801. ctx->flags |= SHA_FLAGS_FINUP;
  802. err1 = atmel_sha_update(req);
  803. if (err1 == -EINPROGRESS ||
  804. (err1 == -EBUSY && (ahash_request_flags(req) &
  805. CRYPTO_TFM_REQ_MAY_BACKLOG)))
  806. return err1;
  807. /*
  808. * final() has to be always called to cleanup resources
  809. * even if udpate() failed, except EINPROGRESS
  810. */
  811. err2 = atmel_sha_final(req);
  812. return err1 ?: err2;
  813. }
  814. static int atmel_sha_digest(struct ahash_request *req)
  815. {
  816. return atmel_sha_init(req) ?: atmel_sha_finup(req);
  817. }
  818. static int atmel_sha_export(struct ahash_request *req, void *out)
  819. {
  820. const struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  821. memcpy(out, ctx, sizeof(*ctx));
  822. return 0;
  823. }
  824. static int atmel_sha_import(struct ahash_request *req, const void *in)
  825. {
  826. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  827. memcpy(ctx, in, sizeof(*ctx));
  828. return 0;
  829. }
  830. static int atmel_sha_cra_init(struct crypto_tfm *tfm)
  831. {
  832. crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
  833. sizeof(struct atmel_sha_reqctx));
  834. return 0;
  835. }
  836. static struct ahash_alg sha_1_256_algs[] = {
  837. {
  838. .init = atmel_sha_init,
  839. .update = atmel_sha_update,
  840. .final = atmel_sha_final,
  841. .finup = atmel_sha_finup,
  842. .digest = atmel_sha_digest,
  843. .export = atmel_sha_export,
  844. .import = atmel_sha_import,
  845. .halg = {
  846. .digestsize = SHA1_DIGEST_SIZE,
  847. .statesize = sizeof(struct atmel_sha_reqctx),
  848. .base = {
  849. .cra_name = "sha1",
  850. .cra_driver_name = "atmel-sha1",
  851. .cra_priority = 100,
  852. .cra_flags = CRYPTO_ALG_ASYNC,
  853. .cra_blocksize = SHA1_BLOCK_SIZE,
  854. .cra_ctxsize = sizeof(struct atmel_sha_ctx),
  855. .cra_alignmask = 0,
  856. .cra_module = THIS_MODULE,
  857. .cra_init = atmel_sha_cra_init,
  858. }
  859. }
  860. },
  861. {
  862. .init = atmel_sha_init,
  863. .update = atmel_sha_update,
  864. .final = atmel_sha_final,
  865. .finup = atmel_sha_finup,
  866. .digest = atmel_sha_digest,
  867. .export = atmel_sha_export,
  868. .import = atmel_sha_import,
  869. .halg = {
  870. .digestsize = SHA256_DIGEST_SIZE,
  871. .statesize = sizeof(struct atmel_sha_reqctx),
  872. .base = {
  873. .cra_name = "sha256",
  874. .cra_driver_name = "atmel-sha256",
  875. .cra_priority = 100,
  876. .cra_flags = CRYPTO_ALG_ASYNC,
  877. .cra_blocksize = SHA256_BLOCK_SIZE,
  878. .cra_ctxsize = sizeof(struct atmel_sha_ctx),
  879. .cra_alignmask = 0,
  880. .cra_module = THIS_MODULE,
  881. .cra_init = atmel_sha_cra_init,
  882. }
  883. }
  884. },
  885. };
  886. static struct ahash_alg sha_224_alg = {
  887. .init = atmel_sha_init,
  888. .update = atmel_sha_update,
  889. .final = atmel_sha_final,
  890. .finup = atmel_sha_finup,
  891. .digest = atmel_sha_digest,
  892. .export = atmel_sha_export,
  893. .import = atmel_sha_import,
  894. .halg = {
  895. .digestsize = SHA224_DIGEST_SIZE,
  896. .statesize = sizeof(struct atmel_sha_reqctx),
  897. .base = {
  898. .cra_name = "sha224",
  899. .cra_driver_name = "atmel-sha224",
  900. .cra_priority = 100,
  901. .cra_flags = CRYPTO_ALG_ASYNC,
  902. .cra_blocksize = SHA224_BLOCK_SIZE,
  903. .cra_ctxsize = sizeof(struct atmel_sha_ctx),
  904. .cra_alignmask = 0,
  905. .cra_module = THIS_MODULE,
  906. .cra_init = atmel_sha_cra_init,
  907. }
  908. }
  909. };
  910. static struct ahash_alg sha_384_512_algs[] = {
  911. {
  912. .init = atmel_sha_init,
  913. .update = atmel_sha_update,
  914. .final = atmel_sha_final,
  915. .finup = atmel_sha_finup,
  916. .digest = atmel_sha_digest,
  917. .export = atmel_sha_export,
  918. .import = atmel_sha_import,
  919. .halg = {
  920. .digestsize = SHA384_DIGEST_SIZE,
  921. .statesize = sizeof(struct atmel_sha_reqctx),
  922. .base = {
  923. .cra_name = "sha384",
  924. .cra_driver_name = "atmel-sha384",
  925. .cra_priority = 100,
  926. .cra_flags = CRYPTO_ALG_ASYNC,
  927. .cra_blocksize = SHA384_BLOCK_SIZE,
  928. .cra_ctxsize = sizeof(struct atmel_sha_ctx),
  929. .cra_alignmask = 0x3,
  930. .cra_module = THIS_MODULE,
  931. .cra_init = atmel_sha_cra_init,
  932. }
  933. }
  934. },
  935. {
  936. .init = atmel_sha_init,
  937. .update = atmel_sha_update,
  938. .final = atmel_sha_final,
  939. .finup = atmel_sha_finup,
  940. .digest = atmel_sha_digest,
  941. .export = atmel_sha_export,
  942. .import = atmel_sha_import,
  943. .halg = {
  944. .digestsize = SHA512_DIGEST_SIZE,
  945. .statesize = sizeof(struct atmel_sha_reqctx),
  946. .base = {
  947. .cra_name = "sha512",
  948. .cra_driver_name = "atmel-sha512",
  949. .cra_priority = 100,
  950. .cra_flags = CRYPTO_ALG_ASYNC,
  951. .cra_blocksize = SHA512_BLOCK_SIZE,
  952. .cra_ctxsize = sizeof(struct atmel_sha_ctx),
  953. .cra_alignmask = 0x3,
  954. .cra_module = THIS_MODULE,
  955. .cra_init = atmel_sha_cra_init,
  956. }
  957. }
  958. },
  959. };
  960. static void atmel_sha_queue_task(unsigned long data)
  961. {
  962. struct atmel_sha_dev *dd = (struct atmel_sha_dev *)data;
  963. atmel_sha_handle_queue(dd, NULL);
  964. }
  965. static void atmel_sha_done_task(unsigned long data)
  966. {
  967. struct atmel_sha_dev *dd = (struct atmel_sha_dev *)data;
  968. int err = 0;
  969. if (SHA_FLAGS_CPU & dd->flags) {
  970. if (SHA_FLAGS_OUTPUT_READY & dd->flags) {
  971. dd->flags &= ~SHA_FLAGS_OUTPUT_READY;
  972. goto finish;
  973. }
  974. } else if (SHA_FLAGS_DMA_READY & dd->flags) {
  975. if (SHA_FLAGS_DMA_ACTIVE & dd->flags) {
  976. dd->flags &= ~SHA_FLAGS_DMA_ACTIVE;
  977. atmel_sha_update_dma_stop(dd);
  978. if (dd->err) {
  979. err = dd->err;
  980. goto finish;
  981. }
  982. }
  983. if (SHA_FLAGS_OUTPUT_READY & dd->flags) {
  984. /* hash or semi-hash ready */
  985. dd->flags &= ~(SHA_FLAGS_DMA_READY |
  986. SHA_FLAGS_OUTPUT_READY);
  987. err = atmel_sha_update_dma_start(dd);
  988. if (err != -EINPROGRESS)
  989. goto finish;
  990. }
  991. }
  992. return;
  993. finish:
  994. /* finish curent request */
  995. atmel_sha_finish_req(dd->req, err);
  996. }
  997. static irqreturn_t atmel_sha_irq(int irq, void *dev_id)
  998. {
  999. struct atmel_sha_dev *sha_dd = dev_id;
  1000. u32 reg;
  1001. reg = atmel_sha_read(sha_dd, SHA_ISR);
  1002. if (reg & atmel_sha_read(sha_dd, SHA_IMR)) {
  1003. atmel_sha_write(sha_dd, SHA_IDR, reg);
  1004. if (SHA_FLAGS_BUSY & sha_dd->flags) {
  1005. sha_dd->flags |= SHA_FLAGS_OUTPUT_READY;
  1006. if (!(SHA_FLAGS_CPU & sha_dd->flags))
  1007. sha_dd->flags |= SHA_FLAGS_DMA_READY;
  1008. tasklet_schedule(&sha_dd->done_task);
  1009. } else {
  1010. dev_warn(sha_dd->dev, "SHA interrupt when no active requests.\n");
  1011. }
  1012. return IRQ_HANDLED;
  1013. }
  1014. return IRQ_NONE;
  1015. }
  1016. static void atmel_sha_unregister_algs(struct atmel_sha_dev *dd)
  1017. {
  1018. int i;
  1019. for (i = 0; i < ARRAY_SIZE(sha_1_256_algs); i++)
  1020. crypto_unregister_ahash(&sha_1_256_algs[i]);
  1021. if (dd->caps.has_sha224)
  1022. crypto_unregister_ahash(&sha_224_alg);
  1023. if (dd->caps.has_sha_384_512) {
  1024. for (i = 0; i < ARRAY_SIZE(sha_384_512_algs); i++)
  1025. crypto_unregister_ahash(&sha_384_512_algs[i]);
  1026. }
  1027. }
  1028. static int atmel_sha_register_algs(struct atmel_sha_dev *dd)
  1029. {
  1030. int err, i, j;
  1031. for (i = 0; i < ARRAY_SIZE(sha_1_256_algs); i++) {
  1032. err = crypto_register_ahash(&sha_1_256_algs[i]);
  1033. if (err)
  1034. goto err_sha_1_256_algs;
  1035. }
  1036. if (dd->caps.has_sha224) {
  1037. err = crypto_register_ahash(&sha_224_alg);
  1038. if (err)
  1039. goto err_sha_224_algs;
  1040. }
  1041. if (dd->caps.has_sha_384_512) {
  1042. for (i = 0; i < ARRAY_SIZE(sha_384_512_algs); i++) {
  1043. err = crypto_register_ahash(&sha_384_512_algs[i]);
  1044. if (err)
  1045. goto err_sha_384_512_algs;
  1046. }
  1047. }
  1048. return 0;
  1049. err_sha_384_512_algs:
  1050. for (j = 0; j < i; j++)
  1051. crypto_unregister_ahash(&sha_384_512_algs[j]);
  1052. crypto_unregister_ahash(&sha_224_alg);
  1053. err_sha_224_algs:
  1054. i = ARRAY_SIZE(sha_1_256_algs);
  1055. err_sha_1_256_algs:
  1056. for (j = 0; j < i; j++)
  1057. crypto_unregister_ahash(&sha_1_256_algs[j]);
  1058. return err;
  1059. }
  1060. static bool atmel_sha_filter(struct dma_chan *chan, void *slave)
  1061. {
  1062. struct at_dma_slave *sl = slave;
  1063. if (sl && sl->dma_dev == chan->device->dev) {
  1064. chan->private = sl;
  1065. return true;
  1066. } else {
  1067. return false;
  1068. }
  1069. }
  1070. static int atmel_sha_dma_init(struct atmel_sha_dev *dd,
  1071. struct crypto_platform_data *pdata)
  1072. {
  1073. int err = -ENOMEM;
  1074. dma_cap_mask_t mask_in;
  1075. /* Try to grab DMA channel */
  1076. dma_cap_zero(mask_in);
  1077. dma_cap_set(DMA_SLAVE, mask_in);
  1078. dd->dma_lch_in.chan = dma_request_slave_channel_compat(mask_in,
  1079. atmel_sha_filter, &pdata->dma_slave->rxdata, dd->dev, "tx");
  1080. if (!dd->dma_lch_in.chan) {
  1081. dev_warn(dd->dev, "no DMA channel available\n");
  1082. return err;
  1083. }
  1084. dd->dma_lch_in.dma_conf.direction = DMA_MEM_TO_DEV;
  1085. dd->dma_lch_in.dma_conf.dst_addr = dd->phys_base +
  1086. SHA_REG_DIN(0);
  1087. dd->dma_lch_in.dma_conf.src_maxburst = 1;
  1088. dd->dma_lch_in.dma_conf.src_addr_width =
  1089. DMA_SLAVE_BUSWIDTH_4_BYTES;
  1090. dd->dma_lch_in.dma_conf.dst_maxburst = 1;
  1091. dd->dma_lch_in.dma_conf.dst_addr_width =
  1092. DMA_SLAVE_BUSWIDTH_4_BYTES;
  1093. dd->dma_lch_in.dma_conf.device_fc = false;
  1094. return 0;
  1095. }
  1096. static void atmel_sha_dma_cleanup(struct atmel_sha_dev *dd)
  1097. {
  1098. dma_release_channel(dd->dma_lch_in.chan);
  1099. }
  1100. static void atmel_sha_get_cap(struct atmel_sha_dev *dd)
  1101. {
  1102. dd->caps.has_dma = 0;
  1103. dd->caps.has_dualbuff = 0;
  1104. dd->caps.has_sha224 = 0;
  1105. dd->caps.has_sha_384_512 = 0;
  1106. dd->caps.has_uihv = 0;
  1107. /* keep only major version number */
  1108. switch (dd->hw_version & 0xff0) {
  1109. case 0x510:
  1110. dd->caps.has_dma = 1;
  1111. dd->caps.has_dualbuff = 1;
  1112. dd->caps.has_sha224 = 1;
  1113. dd->caps.has_sha_384_512 = 1;
  1114. dd->caps.has_uihv = 1;
  1115. break;
  1116. case 0x420:
  1117. dd->caps.has_dma = 1;
  1118. dd->caps.has_dualbuff = 1;
  1119. dd->caps.has_sha224 = 1;
  1120. dd->caps.has_sha_384_512 = 1;
  1121. dd->caps.has_uihv = 1;
  1122. break;
  1123. case 0x410:
  1124. dd->caps.has_dma = 1;
  1125. dd->caps.has_dualbuff = 1;
  1126. dd->caps.has_sha224 = 1;
  1127. dd->caps.has_sha_384_512 = 1;
  1128. break;
  1129. case 0x400:
  1130. dd->caps.has_dma = 1;
  1131. dd->caps.has_dualbuff = 1;
  1132. dd->caps.has_sha224 = 1;
  1133. break;
  1134. case 0x320:
  1135. break;
  1136. default:
  1137. dev_warn(dd->dev,
  1138. "Unmanaged sha version, set minimum capabilities\n");
  1139. break;
  1140. }
  1141. }
  1142. #if defined(CONFIG_OF)
  1143. static const struct of_device_id atmel_sha_dt_ids[] = {
  1144. { .compatible = "atmel,at91sam9g46-sha" },
  1145. { /* sentinel */ }
  1146. };
  1147. MODULE_DEVICE_TABLE(of, atmel_sha_dt_ids);
  1148. static struct crypto_platform_data *atmel_sha_of_init(struct platform_device *pdev)
  1149. {
  1150. struct device_node *np = pdev->dev.of_node;
  1151. struct crypto_platform_data *pdata;
  1152. if (!np) {
  1153. dev_err(&pdev->dev, "device node not found\n");
  1154. return ERR_PTR(-EINVAL);
  1155. }
  1156. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  1157. if (!pdata) {
  1158. dev_err(&pdev->dev, "could not allocate memory for pdata\n");
  1159. return ERR_PTR(-ENOMEM);
  1160. }
  1161. pdata->dma_slave = devm_kzalloc(&pdev->dev,
  1162. sizeof(*(pdata->dma_slave)),
  1163. GFP_KERNEL);
  1164. if (!pdata->dma_slave) {
  1165. dev_err(&pdev->dev, "could not allocate memory for dma_slave\n");
  1166. return ERR_PTR(-ENOMEM);
  1167. }
  1168. return pdata;
  1169. }
  1170. #else /* CONFIG_OF */
  1171. static inline struct crypto_platform_data *atmel_sha_of_init(struct platform_device *dev)
  1172. {
  1173. return ERR_PTR(-EINVAL);
  1174. }
  1175. #endif
  1176. static int atmel_sha_probe(struct platform_device *pdev)
  1177. {
  1178. struct atmel_sha_dev *sha_dd;
  1179. struct crypto_platform_data *pdata;
  1180. struct device *dev = &pdev->dev;
  1181. struct resource *sha_res;
  1182. int err;
  1183. sha_dd = devm_kzalloc(&pdev->dev, sizeof(*sha_dd), GFP_KERNEL);
  1184. if (sha_dd == NULL) {
  1185. dev_err(dev, "unable to alloc data struct.\n");
  1186. err = -ENOMEM;
  1187. goto sha_dd_err;
  1188. }
  1189. sha_dd->dev = dev;
  1190. platform_set_drvdata(pdev, sha_dd);
  1191. INIT_LIST_HEAD(&sha_dd->list);
  1192. spin_lock_init(&sha_dd->lock);
  1193. tasklet_init(&sha_dd->done_task, atmel_sha_done_task,
  1194. (unsigned long)sha_dd);
  1195. tasklet_init(&sha_dd->queue_task, atmel_sha_queue_task,
  1196. (unsigned long)sha_dd);
  1197. crypto_init_queue(&sha_dd->queue, ATMEL_SHA_QUEUE_LENGTH);
  1198. sha_dd->irq = -1;
  1199. /* Get the base address */
  1200. sha_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1201. if (!sha_res) {
  1202. dev_err(dev, "no MEM resource info\n");
  1203. err = -ENODEV;
  1204. goto res_err;
  1205. }
  1206. sha_dd->phys_base = sha_res->start;
  1207. /* Get the IRQ */
  1208. sha_dd->irq = platform_get_irq(pdev, 0);
  1209. if (sha_dd->irq < 0) {
  1210. dev_err(dev, "no IRQ resource info\n");
  1211. err = sha_dd->irq;
  1212. goto res_err;
  1213. }
  1214. err = devm_request_irq(&pdev->dev, sha_dd->irq, atmel_sha_irq,
  1215. IRQF_SHARED, "atmel-sha", sha_dd);
  1216. if (err) {
  1217. dev_err(dev, "unable to request sha irq.\n");
  1218. goto res_err;
  1219. }
  1220. /* Initializing the clock */
  1221. sha_dd->iclk = devm_clk_get(&pdev->dev, "sha_clk");
  1222. if (IS_ERR(sha_dd->iclk)) {
  1223. dev_err(dev, "clock initialization failed.\n");
  1224. err = PTR_ERR(sha_dd->iclk);
  1225. goto res_err;
  1226. }
  1227. sha_dd->io_base = devm_ioremap_resource(&pdev->dev, sha_res);
  1228. if (IS_ERR(sha_dd->io_base)) {
  1229. dev_err(dev, "can't ioremap\n");
  1230. err = PTR_ERR(sha_dd->io_base);
  1231. goto res_err;
  1232. }
  1233. err = clk_prepare(sha_dd->iclk);
  1234. if (err)
  1235. goto res_err;
  1236. atmel_sha_hw_version_init(sha_dd);
  1237. atmel_sha_get_cap(sha_dd);
  1238. if (sha_dd->caps.has_dma) {
  1239. pdata = pdev->dev.platform_data;
  1240. if (!pdata) {
  1241. pdata = atmel_sha_of_init(pdev);
  1242. if (IS_ERR(pdata)) {
  1243. dev_err(&pdev->dev, "platform data not available\n");
  1244. err = PTR_ERR(pdata);
  1245. goto iclk_unprepare;
  1246. }
  1247. }
  1248. if (!pdata->dma_slave) {
  1249. err = -ENXIO;
  1250. goto iclk_unprepare;
  1251. }
  1252. err = atmel_sha_dma_init(sha_dd, pdata);
  1253. if (err)
  1254. goto err_sha_dma;
  1255. dev_info(dev, "using %s for DMA transfers\n",
  1256. dma_chan_name(sha_dd->dma_lch_in.chan));
  1257. }
  1258. spin_lock(&atmel_sha.lock);
  1259. list_add_tail(&sha_dd->list, &atmel_sha.dev_list);
  1260. spin_unlock(&atmel_sha.lock);
  1261. err = atmel_sha_register_algs(sha_dd);
  1262. if (err)
  1263. goto err_algs;
  1264. dev_info(dev, "Atmel SHA1/SHA256%s%s\n",
  1265. sha_dd->caps.has_sha224 ? "/SHA224" : "",
  1266. sha_dd->caps.has_sha_384_512 ? "/SHA384/SHA512" : "");
  1267. return 0;
  1268. err_algs:
  1269. spin_lock(&atmel_sha.lock);
  1270. list_del(&sha_dd->list);
  1271. spin_unlock(&atmel_sha.lock);
  1272. if (sha_dd->caps.has_dma)
  1273. atmel_sha_dma_cleanup(sha_dd);
  1274. err_sha_dma:
  1275. iclk_unprepare:
  1276. clk_unprepare(sha_dd->iclk);
  1277. res_err:
  1278. tasklet_kill(&sha_dd->queue_task);
  1279. tasklet_kill(&sha_dd->done_task);
  1280. sha_dd_err:
  1281. dev_err(dev, "initialization failed.\n");
  1282. return err;
  1283. }
  1284. static int atmel_sha_remove(struct platform_device *pdev)
  1285. {
  1286. static struct atmel_sha_dev *sha_dd;
  1287. sha_dd = platform_get_drvdata(pdev);
  1288. if (!sha_dd)
  1289. return -ENODEV;
  1290. spin_lock(&atmel_sha.lock);
  1291. list_del(&sha_dd->list);
  1292. spin_unlock(&atmel_sha.lock);
  1293. atmel_sha_unregister_algs(sha_dd);
  1294. tasklet_kill(&sha_dd->queue_task);
  1295. tasklet_kill(&sha_dd->done_task);
  1296. if (sha_dd->caps.has_dma)
  1297. atmel_sha_dma_cleanup(sha_dd);
  1298. clk_unprepare(sha_dd->iclk);
  1299. return 0;
  1300. }
  1301. static struct platform_driver atmel_sha_driver = {
  1302. .probe = atmel_sha_probe,
  1303. .remove = atmel_sha_remove,
  1304. .driver = {
  1305. .name = "atmel_sha",
  1306. .of_match_table = of_match_ptr(atmel_sha_dt_ids),
  1307. },
  1308. };
  1309. module_platform_driver(atmel_sha_driver);
  1310. MODULE_DESCRIPTION("Atmel SHA (1/256/224/384/512) hw acceleration support.");
  1311. MODULE_LICENSE("GPL v2");
  1312. MODULE_AUTHOR("Nicolas Royer - Eukréa Electromatique");