omap-sham.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309
  1. /*
  2. * Cryptographic API.
  3. *
  4. * Support for OMAP SHA1/MD5 HW acceleration.
  5. *
  6. * Copyright (c) 2010 Nokia Corporation
  7. * Author: Dmitry Kasatkin <dmitry.kasatkin@nokia.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 old omap-sha1-md5.c driver.
  14. */
  15. #define pr_fmt(fmt) "%s: " fmt, __func__
  16. #include <linux/err.h>
  17. #include <linux/device.h>
  18. #include <linux/module.h>
  19. #include <linux/init.h>
  20. #include <linux/errno.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/kernel.h>
  23. #include <linux/clk.h>
  24. #include <linux/irq.h>
  25. #include <linux/io.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/scatterlist.h>
  28. #include <linux/dma-mapping.h>
  29. #include <linux/delay.h>
  30. #include <linux/crypto.h>
  31. #include <linux/cryptohash.h>
  32. #include <crypto/scatterwalk.h>
  33. #include <crypto/algapi.h>
  34. #include <crypto/sha.h>
  35. #include <crypto/hash.h>
  36. #include <crypto/internal/hash.h>
  37. #include <plat/cpu.h>
  38. #include <plat/dma.h>
  39. #include <mach/irqs.h>
  40. #define SHA_REG_DIGEST(x) (0x00 + ((x) * 0x04))
  41. #define SHA_REG_DIN(x) (0x1C + ((x) * 0x04))
  42. #define SHA1_MD5_BLOCK_SIZE SHA1_BLOCK_SIZE
  43. #define MD5_DIGEST_SIZE 16
  44. #define SHA_REG_DIGCNT 0x14
  45. #define SHA_REG_CTRL 0x18
  46. #define SHA_REG_CTRL_LENGTH (0xFFFFFFFF << 5)
  47. #define SHA_REG_CTRL_CLOSE_HASH (1 << 4)
  48. #define SHA_REG_CTRL_ALGO_CONST (1 << 3)
  49. #define SHA_REG_CTRL_ALGO (1 << 2)
  50. #define SHA_REG_CTRL_INPUT_READY (1 << 1)
  51. #define SHA_REG_CTRL_OUTPUT_READY (1 << 0)
  52. #define SHA_REG_REV 0x5C
  53. #define SHA_REG_REV_MAJOR 0xF0
  54. #define SHA_REG_REV_MINOR 0x0F
  55. #define SHA_REG_MASK 0x60
  56. #define SHA_REG_MASK_DMA_EN (1 << 3)
  57. #define SHA_REG_MASK_IT_EN (1 << 2)
  58. #define SHA_REG_MASK_SOFTRESET (1 << 1)
  59. #define SHA_REG_AUTOIDLE (1 << 0)
  60. #define SHA_REG_SYSSTATUS 0x64
  61. #define SHA_REG_SYSSTATUS_RESETDONE (1 << 0)
  62. #define DEFAULT_TIMEOUT_INTERVAL HZ
  63. #define FLAGS_FINUP 0x0002
  64. #define FLAGS_FINAL 0x0004
  65. #define FLAGS_SG 0x0008
  66. #define FLAGS_SHA1 0x0010
  67. #define FLAGS_DMA_ACTIVE 0x0020
  68. #define FLAGS_OUTPUT_READY 0x0040
  69. #define FLAGS_INIT 0x0100
  70. #define FLAGS_CPU 0x0200
  71. #define FLAGS_HMAC 0x0400
  72. #define FLAGS_ERROR 0x0800
  73. #define FLAGS_BUSY 0x1000
  74. #define OP_UPDATE 1
  75. #define OP_FINAL 2
  76. #define OMAP_ALIGN_MASK (sizeof(u32)-1)
  77. #define OMAP_ALIGNED __attribute__((aligned(sizeof(u32))))
  78. #define BUFLEN PAGE_SIZE
  79. struct omap_sham_dev;
  80. struct omap_sham_reqctx {
  81. struct omap_sham_dev *dd;
  82. unsigned long flags;
  83. unsigned long op;
  84. u8 digest[SHA1_DIGEST_SIZE] OMAP_ALIGNED;
  85. size_t digcnt;
  86. size_t bufcnt;
  87. size_t buflen;
  88. dma_addr_t dma_addr;
  89. /* walk state */
  90. struct scatterlist *sg;
  91. unsigned int offset; /* offset in current sg */
  92. unsigned int total; /* total request */
  93. u8 buffer[0] OMAP_ALIGNED;
  94. };
  95. struct omap_sham_hmac_ctx {
  96. struct crypto_shash *shash;
  97. u8 ipad[SHA1_MD5_BLOCK_SIZE];
  98. u8 opad[SHA1_MD5_BLOCK_SIZE];
  99. };
  100. struct omap_sham_ctx {
  101. struct omap_sham_dev *dd;
  102. unsigned long flags;
  103. /* fallback stuff */
  104. struct crypto_shash *fallback;
  105. struct omap_sham_hmac_ctx base[0];
  106. };
  107. #define OMAP_SHAM_QUEUE_LENGTH 1
  108. struct omap_sham_dev {
  109. struct list_head list;
  110. unsigned long phys_base;
  111. struct device *dev;
  112. void __iomem *io_base;
  113. int irq;
  114. struct clk *iclk;
  115. spinlock_t lock;
  116. int err;
  117. int dma;
  118. int dma_lch;
  119. struct tasklet_struct done_task;
  120. struct tasklet_struct queue_task;
  121. unsigned long flags;
  122. struct crypto_queue queue;
  123. struct ahash_request *req;
  124. };
  125. struct omap_sham_drv {
  126. struct list_head dev_list;
  127. spinlock_t lock;
  128. unsigned long flags;
  129. };
  130. static struct omap_sham_drv sham = {
  131. .dev_list = LIST_HEAD_INIT(sham.dev_list),
  132. .lock = __SPIN_LOCK_UNLOCKED(sham.lock),
  133. };
  134. static inline u32 omap_sham_read(struct omap_sham_dev *dd, u32 offset)
  135. {
  136. return __raw_readl(dd->io_base + offset);
  137. }
  138. static inline void omap_sham_write(struct omap_sham_dev *dd,
  139. u32 offset, u32 value)
  140. {
  141. __raw_writel(value, dd->io_base + offset);
  142. }
  143. static inline void omap_sham_write_mask(struct omap_sham_dev *dd, u32 address,
  144. u32 value, u32 mask)
  145. {
  146. u32 val;
  147. val = omap_sham_read(dd, address);
  148. val &= ~mask;
  149. val |= value;
  150. omap_sham_write(dd, address, val);
  151. }
  152. static inline int omap_sham_wait(struct omap_sham_dev *dd, u32 offset, u32 bit)
  153. {
  154. unsigned long timeout = jiffies + DEFAULT_TIMEOUT_INTERVAL;
  155. while (!(omap_sham_read(dd, offset) & bit)) {
  156. if (time_is_before_jiffies(timeout))
  157. return -ETIMEDOUT;
  158. }
  159. return 0;
  160. }
  161. static void omap_sham_copy_hash(struct ahash_request *req, int out)
  162. {
  163. struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
  164. u32 *hash = (u32 *)ctx->digest;
  165. int i;
  166. /* MD5 is almost unused. So copy sha1 size to reduce code */
  167. for (i = 0; i < SHA1_DIGEST_SIZE / sizeof(u32); i++) {
  168. if (out)
  169. hash[i] = omap_sham_read(ctx->dd,
  170. SHA_REG_DIGEST(i));
  171. else
  172. omap_sham_write(ctx->dd,
  173. SHA_REG_DIGEST(i), hash[i]);
  174. }
  175. }
  176. static void omap_sham_copy_ready_hash(struct ahash_request *req)
  177. {
  178. struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
  179. u32 *in = (u32 *)ctx->digest;
  180. u32 *hash = (u32 *)req->result;
  181. int i;
  182. if (!hash)
  183. return;
  184. if (likely(ctx->flags & FLAGS_SHA1)) {
  185. /* SHA1 results are in big endian */
  186. for (i = 0; i < SHA1_DIGEST_SIZE / sizeof(u32); i++)
  187. hash[i] = be32_to_cpu(in[i]);
  188. } else {
  189. /* MD5 results are in little endian */
  190. for (i = 0; i < MD5_DIGEST_SIZE / sizeof(u32); i++)
  191. hash[i] = le32_to_cpu(in[i]);
  192. }
  193. }
  194. static int omap_sham_hw_init(struct omap_sham_dev *dd)
  195. {
  196. clk_enable(dd->iclk);
  197. if (!(dd->flags & FLAGS_INIT)) {
  198. omap_sham_write_mask(dd, SHA_REG_MASK,
  199. SHA_REG_MASK_SOFTRESET, SHA_REG_MASK_SOFTRESET);
  200. if (omap_sham_wait(dd, SHA_REG_SYSSTATUS,
  201. SHA_REG_SYSSTATUS_RESETDONE))
  202. return -ETIMEDOUT;
  203. dd->flags |= FLAGS_INIT;
  204. dd->err = 0;
  205. }
  206. return 0;
  207. }
  208. static void omap_sham_write_ctrl(struct omap_sham_dev *dd, size_t length,
  209. int final, int dma)
  210. {
  211. struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req);
  212. u32 val = length << 5, mask;
  213. if (likely(ctx->digcnt))
  214. omap_sham_write(dd, SHA_REG_DIGCNT, ctx->digcnt);
  215. omap_sham_write_mask(dd, SHA_REG_MASK,
  216. SHA_REG_MASK_IT_EN | (dma ? SHA_REG_MASK_DMA_EN : 0),
  217. SHA_REG_MASK_IT_EN | SHA_REG_MASK_DMA_EN);
  218. /*
  219. * Setting ALGO_CONST only for the first iteration
  220. * and CLOSE_HASH only for the last one.
  221. */
  222. if (ctx->flags & FLAGS_SHA1)
  223. val |= SHA_REG_CTRL_ALGO;
  224. if (!ctx->digcnt)
  225. val |= SHA_REG_CTRL_ALGO_CONST;
  226. if (final)
  227. val |= SHA_REG_CTRL_CLOSE_HASH;
  228. mask = SHA_REG_CTRL_ALGO_CONST | SHA_REG_CTRL_CLOSE_HASH |
  229. SHA_REG_CTRL_ALGO | SHA_REG_CTRL_LENGTH;
  230. omap_sham_write_mask(dd, SHA_REG_CTRL, val, mask);
  231. }
  232. static int omap_sham_xmit_cpu(struct omap_sham_dev *dd, const u8 *buf,
  233. size_t length, int final)
  234. {
  235. struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req);
  236. int count, len32;
  237. const u32 *buffer = (const u32 *)buf;
  238. dev_dbg(dd->dev, "xmit_cpu: digcnt: %d, length: %d, final: %d\n",
  239. ctx->digcnt, length, final);
  240. omap_sham_write_ctrl(dd, length, final, 0);
  241. /* should be non-zero before next lines to disable clocks later */
  242. ctx->digcnt += length;
  243. if (omap_sham_wait(dd, SHA_REG_CTRL, SHA_REG_CTRL_INPUT_READY))
  244. return -ETIMEDOUT;
  245. if (final)
  246. ctx->flags |= FLAGS_FINAL; /* catch last interrupt */
  247. len32 = DIV_ROUND_UP(length, sizeof(u32));
  248. for (count = 0; count < len32; count++)
  249. omap_sham_write(dd, SHA_REG_DIN(count), buffer[count]);
  250. return -EINPROGRESS;
  251. }
  252. static int omap_sham_xmit_dma(struct omap_sham_dev *dd, dma_addr_t dma_addr,
  253. size_t length, int final)
  254. {
  255. struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req);
  256. int len32;
  257. dev_dbg(dd->dev, "xmit_dma: digcnt: %d, length: %d, final: %d\n",
  258. ctx->digcnt, length, final);
  259. len32 = DIV_ROUND_UP(length, sizeof(u32));
  260. omap_set_dma_transfer_params(dd->dma_lch, OMAP_DMA_DATA_TYPE_S32, len32,
  261. 1, OMAP_DMA_SYNC_PACKET, dd->dma,
  262. OMAP_DMA_DST_SYNC_PREFETCH);
  263. omap_set_dma_src_params(dd->dma_lch, 0, OMAP_DMA_AMODE_POST_INC,
  264. dma_addr, 0, 0);
  265. omap_sham_write_ctrl(dd, length, final, 1);
  266. ctx->digcnt += length;
  267. if (final)
  268. ctx->flags |= FLAGS_FINAL; /* catch last interrupt */
  269. dd->flags |= FLAGS_DMA_ACTIVE;
  270. omap_start_dma(dd->dma_lch);
  271. return -EINPROGRESS;
  272. }
  273. static size_t omap_sham_append_buffer(struct omap_sham_reqctx *ctx,
  274. const u8 *data, size_t length)
  275. {
  276. size_t count = min(length, ctx->buflen - ctx->bufcnt);
  277. count = min(count, ctx->total);
  278. if (count <= 0)
  279. return 0;
  280. memcpy(ctx->buffer + ctx->bufcnt, data, count);
  281. ctx->bufcnt += count;
  282. return count;
  283. }
  284. static size_t omap_sham_append_sg(struct omap_sham_reqctx *ctx)
  285. {
  286. size_t count;
  287. while (ctx->sg) {
  288. count = omap_sham_append_buffer(ctx,
  289. sg_virt(ctx->sg) + ctx->offset,
  290. ctx->sg->length - ctx->offset);
  291. if (!count)
  292. break;
  293. ctx->offset += count;
  294. ctx->total -= count;
  295. if (ctx->offset == ctx->sg->length) {
  296. ctx->sg = sg_next(ctx->sg);
  297. if (ctx->sg)
  298. ctx->offset = 0;
  299. else
  300. ctx->total = 0;
  301. }
  302. }
  303. return 0;
  304. }
  305. static int omap_sham_xmit_dma_map(struct omap_sham_dev *dd,
  306. struct omap_sham_reqctx *ctx,
  307. size_t length, int final)
  308. {
  309. ctx->dma_addr = dma_map_single(dd->dev, ctx->buffer, ctx->buflen,
  310. DMA_TO_DEVICE);
  311. if (dma_mapping_error(dd->dev, ctx->dma_addr)) {
  312. dev_err(dd->dev, "dma %u bytes error\n", ctx->buflen);
  313. return -EINVAL;
  314. }
  315. ctx->flags &= ~FLAGS_SG;
  316. /* next call does not fail... so no unmap in the case of error */
  317. return omap_sham_xmit_dma(dd, ctx->dma_addr, length, final);
  318. }
  319. static int omap_sham_update_dma_slow(struct omap_sham_dev *dd)
  320. {
  321. struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req);
  322. unsigned int final;
  323. size_t count;
  324. omap_sham_append_sg(ctx);
  325. final = (ctx->flags & FLAGS_FINUP) && !ctx->total;
  326. dev_dbg(dd->dev, "slow: bufcnt: %u, digcnt: %d, final: %d\n",
  327. ctx->bufcnt, ctx->digcnt, final);
  328. if (final || (ctx->bufcnt == ctx->buflen && ctx->total)) {
  329. count = ctx->bufcnt;
  330. ctx->bufcnt = 0;
  331. return omap_sham_xmit_dma_map(dd, ctx, count, final);
  332. }
  333. return 0;
  334. }
  335. /* Start address alignment */
  336. #define SG_AA(sg) (IS_ALIGNED(sg->offset, sizeof(u32)))
  337. /* SHA1 block size alignment */
  338. #define SG_SA(sg) (IS_ALIGNED(sg->length, SHA1_MD5_BLOCK_SIZE))
  339. static int omap_sham_update_dma_start(struct omap_sham_dev *dd)
  340. {
  341. struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req);
  342. unsigned int length, final, tail;
  343. struct scatterlist *sg;
  344. if (!ctx->total)
  345. return 0;
  346. if (ctx->bufcnt || ctx->offset)
  347. return omap_sham_update_dma_slow(dd);
  348. dev_dbg(dd->dev, "fast: digcnt: %d, bufcnt: %u, total: %u\n",
  349. ctx->digcnt, ctx->bufcnt, ctx->total);
  350. sg = ctx->sg;
  351. if (!SG_AA(sg))
  352. return omap_sham_update_dma_slow(dd);
  353. if (!sg_is_last(sg) && !SG_SA(sg))
  354. /* size is not SHA1_BLOCK_SIZE aligned */
  355. return omap_sham_update_dma_slow(dd);
  356. length = min(ctx->total, sg->length);
  357. if (sg_is_last(sg)) {
  358. if (!(ctx->flags & FLAGS_FINUP)) {
  359. /* not last sg must be SHA1_MD5_BLOCK_SIZE aligned */
  360. tail = length & (SHA1_MD5_BLOCK_SIZE - 1);
  361. /* without finup() we need one block to close hash */
  362. if (!tail)
  363. tail = SHA1_MD5_BLOCK_SIZE;
  364. length -= tail;
  365. }
  366. }
  367. if (!dma_map_sg(dd->dev, ctx->sg, 1, DMA_TO_DEVICE)) {
  368. dev_err(dd->dev, "dma_map_sg error\n");
  369. return -EINVAL;
  370. }
  371. ctx->flags |= FLAGS_SG;
  372. ctx->total -= length;
  373. ctx->offset = length; /* offset where to start slow */
  374. final = (ctx->flags & FLAGS_FINUP) && !ctx->total;
  375. /* next call does not fail... so no unmap in the case of error */
  376. return omap_sham_xmit_dma(dd, sg_dma_address(ctx->sg), length, final);
  377. }
  378. static int omap_sham_update_cpu(struct omap_sham_dev *dd)
  379. {
  380. struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req);
  381. int bufcnt;
  382. omap_sham_append_sg(ctx);
  383. bufcnt = ctx->bufcnt;
  384. ctx->bufcnt = 0;
  385. return omap_sham_xmit_cpu(dd, ctx->buffer, bufcnt, 1);
  386. }
  387. static int omap_sham_update_dma_stop(struct omap_sham_dev *dd)
  388. {
  389. struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req);
  390. omap_stop_dma(dd->dma_lch);
  391. if (ctx->flags & FLAGS_SG) {
  392. dma_unmap_sg(dd->dev, ctx->sg, 1, DMA_TO_DEVICE);
  393. if (ctx->sg->length == ctx->offset) {
  394. ctx->sg = sg_next(ctx->sg);
  395. if (ctx->sg)
  396. ctx->offset = 0;
  397. }
  398. } else {
  399. dma_unmap_single(dd->dev, ctx->dma_addr, ctx->buflen,
  400. DMA_TO_DEVICE);
  401. }
  402. return 0;
  403. }
  404. static int omap_sham_init(struct ahash_request *req)
  405. {
  406. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  407. struct omap_sham_ctx *tctx = crypto_ahash_ctx(tfm);
  408. struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
  409. struct omap_sham_dev *dd = NULL, *tmp;
  410. spin_lock_bh(&sham.lock);
  411. if (!tctx->dd) {
  412. list_for_each_entry(tmp, &sham.dev_list, list) {
  413. dd = tmp;
  414. break;
  415. }
  416. tctx->dd = dd;
  417. } else {
  418. dd = tctx->dd;
  419. }
  420. spin_unlock_bh(&sham.lock);
  421. ctx->dd = dd;
  422. ctx->flags = 0;
  423. dev_dbg(dd->dev, "init: digest size: %d\n",
  424. crypto_ahash_digestsize(tfm));
  425. if (crypto_ahash_digestsize(tfm) == SHA1_DIGEST_SIZE)
  426. ctx->flags |= FLAGS_SHA1;
  427. ctx->bufcnt = 0;
  428. ctx->digcnt = 0;
  429. ctx->buflen = BUFLEN;
  430. if (tctx->flags & FLAGS_HMAC) {
  431. struct omap_sham_hmac_ctx *bctx = tctx->base;
  432. memcpy(ctx->buffer, bctx->ipad, SHA1_MD5_BLOCK_SIZE);
  433. ctx->bufcnt = SHA1_MD5_BLOCK_SIZE;
  434. ctx->flags |= FLAGS_HMAC;
  435. }
  436. return 0;
  437. }
  438. static int omap_sham_update_req(struct omap_sham_dev *dd)
  439. {
  440. struct ahash_request *req = dd->req;
  441. struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
  442. int err;
  443. dev_dbg(dd->dev, "update_req: total: %u, digcnt: %d, finup: %d\n",
  444. ctx->total, ctx->digcnt, (ctx->flags & FLAGS_FINUP) != 0);
  445. if (ctx->flags & FLAGS_CPU)
  446. err = omap_sham_update_cpu(dd);
  447. else
  448. err = omap_sham_update_dma_start(dd);
  449. /* wait for dma completion before can take more data */
  450. dev_dbg(dd->dev, "update: err: %d, digcnt: %d\n", err, ctx->digcnt);
  451. return err;
  452. }
  453. static int omap_sham_final_req(struct omap_sham_dev *dd)
  454. {
  455. struct ahash_request *req = dd->req;
  456. struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
  457. int err = 0, use_dma = 1;
  458. if (ctx->bufcnt <= 64)
  459. /* faster to handle last block with cpu */
  460. use_dma = 0;
  461. if (use_dma)
  462. err = omap_sham_xmit_dma_map(dd, ctx, ctx->bufcnt, 1);
  463. else
  464. err = omap_sham_xmit_cpu(dd, ctx->buffer, ctx->bufcnt, 1);
  465. ctx->bufcnt = 0;
  466. dev_dbg(dd->dev, "final_req: err: %d\n", err);
  467. return err;
  468. }
  469. static int omap_sham_finish_hmac(struct ahash_request *req)
  470. {
  471. struct omap_sham_ctx *tctx = crypto_tfm_ctx(req->base.tfm);
  472. struct omap_sham_hmac_ctx *bctx = tctx->base;
  473. int bs = crypto_shash_blocksize(bctx->shash);
  474. int ds = crypto_shash_digestsize(bctx->shash);
  475. struct {
  476. struct shash_desc shash;
  477. char ctx[crypto_shash_descsize(bctx->shash)];
  478. } desc;
  479. desc.shash.tfm = bctx->shash;
  480. desc.shash.flags = 0; /* not CRYPTO_TFM_REQ_MAY_SLEEP */
  481. return crypto_shash_init(&desc.shash) ?:
  482. crypto_shash_update(&desc.shash, bctx->opad, bs) ?:
  483. crypto_shash_finup(&desc.shash, req->result, ds, req->result);
  484. }
  485. static int omap_sham_finish(struct ahash_request *req)
  486. {
  487. struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
  488. struct omap_sham_dev *dd = ctx->dd;
  489. int err = 0;
  490. if (ctx->digcnt) {
  491. omap_sham_copy_ready_hash(req);
  492. if (ctx->flags & FLAGS_HMAC)
  493. err = omap_sham_finish_hmac(req);
  494. }
  495. dev_dbg(dd->dev, "digcnt: %d, bufcnt: %d\n", ctx->digcnt, ctx->bufcnt);
  496. return err;
  497. }
  498. static void omap_sham_finish_req(struct ahash_request *req, int err)
  499. {
  500. struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
  501. struct omap_sham_dev *dd = ctx->dd;
  502. if (!err) {
  503. omap_sham_copy_hash(ctx->dd->req, 1);
  504. if (ctx->flags & FLAGS_FINAL)
  505. err = omap_sham_finish(req);
  506. } else {
  507. ctx->flags |= FLAGS_ERROR;
  508. }
  509. clk_disable(dd->iclk);
  510. dd->flags &= ~FLAGS_BUSY;
  511. if (req->base.complete)
  512. req->base.complete(&req->base, err);
  513. }
  514. static int omap_sham_handle_queue(struct omap_sham_dev *dd,
  515. struct ahash_request *req)
  516. {
  517. struct crypto_async_request *async_req, *backlog;
  518. struct omap_sham_reqctx *ctx;
  519. struct ahash_request *prev_req;
  520. unsigned long flags;
  521. int err = 0, ret = 0;
  522. spin_lock_irqsave(&dd->lock, flags);
  523. if (req)
  524. ret = ahash_enqueue_request(&dd->queue, req);
  525. if (dd->flags & FLAGS_BUSY) {
  526. spin_unlock_irqrestore(&dd->lock, flags);
  527. return ret;
  528. }
  529. backlog = crypto_get_backlog(&dd->queue);
  530. async_req = crypto_dequeue_request(&dd->queue);
  531. if (async_req)
  532. dd->flags |= FLAGS_BUSY;
  533. spin_unlock_irqrestore(&dd->lock, flags);
  534. if (!async_req)
  535. return ret;
  536. if (backlog)
  537. backlog->complete(backlog, -EINPROGRESS);
  538. req = ahash_request_cast(async_req);
  539. prev_req = dd->req;
  540. dd->req = req;
  541. ctx = ahash_request_ctx(req);
  542. dev_dbg(dd->dev, "handling new req, op: %lu, nbytes: %d\n",
  543. ctx->op, req->nbytes);
  544. err = omap_sham_hw_init(dd);
  545. if (err)
  546. goto err1;
  547. omap_set_dma_dest_params(dd->dma_lch, 0,
  548. OMAP_DMA_AMODE_CONSTANT,
  549. dd->phys_base + SHA_REG_DIN(0), 0, 16);
  550. omap_set_dma_dest_burst_mode(dd->dma_lch,
  551. OMAP_DMA_DATA_BURST_16);
  552. omap_set_dma_src_burst_mode(dd->dma_lch,
  553. OMAP_DMA_DATA_BURST_4);
  554. if (ctx->digcnt)
  555. /* request has changed - restore hash */
  556. omap_sham_copy_hash(req, 0);
  557. if (ctx->op == OP_UPDATE) {
  558. err = omap_sham_update_req(dd);
  559. if (err != -EINPROGRESS && (ctx->flags & FLAGS_FINUP))
  560. /* no final() after finup() */
  561. err = omap_sham_final_req(dd);
  562. } else if (ctx->op == OP_FINAL) {
  563. err = omap_sham_final_req(dd);
  564. }
  565. err1:
  566. if (err != -EINPROGRESS) {
  567. /* done_task will not finish it, so do it here */
  568. omap_sham_finish_req(req, err);
  569. tasklet_schedule(&dd->queue_task);
  570. }
  571. dev_dbg(dd->dev, "exit, err: %d\n", err);
  572. return ret;
  573. }
  574. static int omap_sham_enqueue(struct ahash_request *req, unsigned int op)
  575. {
  576. struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
  577. struct omap_sham_ctx *tctx = crypto_tfm_ctx(req->base.tfm);
  578. struct omap_sham_dev *dd = tctx->dd;
  579. ctx->op = op;
  580. return omap_sham_handle_queue(dd, req);
  581. }
  582. static int omap_sham_update(struct ahash_request *req)
  583. {
  584. struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
  585. if (!req->nbytes)
  586. return 0;
  587. ctx->total = req->nbytes;
  588. ctx->sg = req->src;
  589. ctx->offset = 0;
  590. if (ctx->flags & FLAGS_FINUP) {
  591. if ((ctx->digcnt + ctx->bufcnt + ctx->total) < 9) {
  592. /*
  593. * OMAP HW accel works only with buffers >= 9
  594. * will switch to bypass in final()
  595. * final has the same request and data
  596. */
  597. omap_sham_append_sg(ctx);
  598. return 0;
  599. } else if (ctx->bufcnt + ctx->total <= SHA1_MD5_BLOCK_SIZE) {
  600. /*
  601. * faster to use CPU for short transfers
  602. */
  603. ctx->flags |= FLAGS_CPU;
  604. }
  605. } else if (ctx->bufcnt + ctx->total < ctx->buflen) {
  606. omap_sham_append_sg(ctx);
  607. return 0;
  608. }
  609. return omap_sham_enqueue(req, OP_UPDATE);
  610. }
  611. static int omap_sham_shash_digest(struct crypto_shash *shash, u32 flags,
  612. const u8 *data, unsigned int len, u8 *out)
  613. {
  614. struct {
  615. struct shash_desc shash;
  616. char ctx[crypto_shash_descsize(shash)];
  617. } desc;
  618. desc.shash.tfm = shash;
  619. desc.shash.flags = flags & CRYPTO_TFM_REQ_MAY_SLEEP;
  620. return crypto_shash_digest(&desc.shash, data, len, out);
  621. }
  622. static int omap_sham_final_shash(struct ahash_request *req)
  623. {
  624. struct omap_sham_ctx *tctx = crypto_tfm_ctx(req->base.tfm);
  625. struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
  626. return omap_sham_shash_digest(tctx->fallback, req->base.flags,
  627. ctx->buffer, ctx->bufcnt, req->result);
  628. }
  629. static int omap_sham_final(struct ahash_request *req)
  630. {
  631. struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
  632. ctx->flags |= FLAGS_FINUP;
  633. if (ctx->flags & FLAGS_ERROR)
  634. return 0; /* uncompleted hash is not needed */
  635. /* OMAP HW accel works only with buffers >= 9 */
  636. /* HMAC is always >= 9 because ipad == block size */
  637. if ((ctx->digcnt + ctx->bufcnt) < 9)
  638. return omap_sham_final_shash(req);
  639. else if (ctx->bufcnt)
  640. return omap_sham_enqueue(req, OP_FINAL);
  641. /* copy ready hash (+ finalize hmac) */
  642. return omap_sham_finish(req);
  643. }
  644. static int omap_sham_finup(struct ahash_request *req)
  645. {
  646. struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
  647. int err1, err2;
  648. ctx->flags |= FLAGS_FINUP;
  649. err1 = omap_sham_update(req);
  650. if (err1 == -EINPROGRESS || err1 == -EBUSY)
  651. return err1;
  652. /*
  653. * final() has to be always called to cleanup resources
  654. * even if udpate() failed, except EINPROGRESS
  655. */
  656. err2 = omap_sham_final(req);
  657. return err1 ?: err2;
  658. }
  659. static int omap_sham_digest(struct ahash_request *req)
  660. {
  661. return omap_sham_init(req) ?: omap_sham_finup(req);
  662. }
  663. static int omap_sham_setkey(struct crypto_ahash *tfm, const u8 *key,
  664. unsigned int keylen)
  665. {
  666. struct omap_sham_ctx *tctx = crypto_ahash_ctx(tfm);
  667. struct omap_sham_hmac_ctx *bctx = tctx->base;
  668. int bs = crypto_shash_blocksize(bctx->shash);
  669. int ds = crypto_shash_digestsize(bctx->shash);
  670. int err, i;
  671. err = crypto_shash_setkey(tctx->fallback, key, keylen);
  672. if (err)
  673. return err;
  674. if (keylen > bs) {
  675. err = omap_sham_shash_digest(bctx->shash,
  676. crypto_shash_get_flags(bctx->shash),
  677. key, keylen, bctx->ipad);
  678. if (err)
  679. return err;
  680. keylen = ds;
  681. } else {
  682. memcpy(bctx->ipad, key, keylen);
  683. }
  684. memset(bctx->ipad + keylen, 0, bs - keylen);
  685. memcpy(bctx->opad, bctx->ipad, bs);
  686. for (i = 0; i < bs; i++) {
  687. bctx->ipad[i] ^= 0x36;
  688. bctx->opad[i] ^= 0x5c;
  689. }
  690. return err;
  691. }
  692. static int omap_sham_cra_init_alg(struct crypto_tfm *tfm, const char *alg_base)
  693. {
  694. struct omap_sham_ctx *tctx = crypto_tfm_ctx(tfm);
  695. const char *alg_name = crypto_tfm_alg_name(tfm);
  696. /* Allocate a fallback and abort if it failed. */
  697. tctx->fallback = crypto_alloc_shash(alg_name, 0,
  698. CRYPTO_ALG_NEED_FALLBACK);
  699. if (IS_ERR(tctx->fallback)) {
  700. pr_err("omap-sham: fallback driver '%s' "
  701. "could not be loaded.\n", alg_name);
  702. return PTR_ERR(tctx->fallback);
  703. }
  704. crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
  705. sizeof(struct omap_sham_reqctx) + BUFLEN);
  706. if (alg_base) {
  707. struct omap_sham_hmac_ctx *bctx = tctx->base;
  708. tctx->flags |= FLAGS_HMAC;
  709. bctx->shash = crypto_alloc_shash(alg_base, 0,
  710. CRYPTO_ALG_NEED_FALLBACK);
  711. if (IS_ERR(bctx->shash)) {
  712. pr_err("omap-sham: base driver '%s' "
  713. "could not be loaded.\n", alg_base);
  714. crypto_free_shash(tctx->fallback);
  715. return PTR_ERR(bctx->shash);
  716. }
  717. }
  718. return 0;
  719. }
  720. static int omap_sham_cra_init(struct crypto_tfm *tfm)
  721. {
  722. return omap_sham_cra_init_alg(tfm, NULL);
  723. }
  724. static int omap_sham_cra_sha1_init(struct crypto_tfm *tfm)
  725. {
  726. return omap_sham_cra_init_alg(tfm, "sha1");
  727. }
  728. static int omap_sham_cra_md5_init(struct crypto_tfm *tfm)
  729. {
  730. return omap_sham_cra_init_alg(tfm, "md5");
  731. }
  732. static void omap_sham_cra_exit(struct crypto_tfm *tfm)
  733. {
  734. struct omap_sham_ctx *tctx = crypto_tfm_ctx(tfm);
  735. crypto_free_shash(tctx->fallback);
  736. tctx->fallback = NULL;
  737. if (tctx->flags & FLAGS_HMAC) {
  738. struct omap_sham_hmac_ctx *bctx = tctx->base;
  739. crypto_free_shash(bctx->shash);
  740. }
  741. }
  742. static struct ahash_alg algs[] = {
  743. {
  744. .init = omap_sham_init,
  745. .update = omap_sham_update,
  746. .final = omap_sham_final,
  747. .finup = omap_sham_finup,
  748. .digest = omap_sham_digest,
  749. .halg.digestsize = SHA1_DIGEST_SIZE,
  750. .halg.base = {
  751. .cra_name = "sha1",
  752. .cra_driver_name = "omap-sha1",
  753. .cra_priority = 100,
  754. .cra_flags = CRYPTO_ALG_TYPE_AHASH |
  755. CRYPTO_ALG_ASYNC |
  756. CRYPTO_ALG_NEED_FALLBACK,
  757. .cra_blocksize = SHA1_BLOCK_SIZE,
  758. .cra_ctxsize = sizeof(struct omap_sham_ctx),
  759. .cra_alignmask = 0,
  760. .cra_module = THIS_MODULE,
  761. .cra_init = omap_sham_cra_init,
  762. .cra_exit = omap_sham_cra_exit,
  763. }
  764. },
  765. {
  766. .init = omap_sham_init,
  767. .update = omap_sham_update,
  768. .final = omap_sham_final,
  769. .finup = omap_sham_finup,
  770. .digest = omap_sham_digest,
  771. .halg.digestsize = MD5_DIGEST_SIZE,
  772. .halg.base = {
  773. .cra_name = "md5",
  774. .cra_driver_name = "omap-md5",
  775. .cra_priority = 100,
  776. .cra_flags = CRYPTO_ALG_TYPE_AHASH |
  777. CRYPTO_ALG_ASYNC |
  778. CRYPTO_ALG_NEED_FALLBACK,
  779. .cra_blocksize = SHA1_BLOCK_SIZE,
  780. .cra_ctxsize = sizeof(struct omap_sham_ctx),
  781. .cra_alignmask = OMAP_ALIGN_MASK,
  782. .cra_module = THIS_MODULE,
  783. .cra_init = omap_sham_cra_init,
  784. .cra_exit = omap_sham_cra_exit,
  785. }
  786. },
  787. {
  788. .init = omap_sham_init,
  789. .update = omap_sham_update,
  790. .final = omap_sham_final,
  791. .finup = omap_sham_finup,
  792. .digest = omap_sham_digest,
  793. .setkey = omap_sham_setkey,
  794. .halg.digestsize = SHA1_DIGEST_SIZE,
  795. .halg.base = {
  796. .cra_name = "hmac(sha1)",
  797. .cra_driver_name = "omap-hmac-sha1",
  798. .cra_priority = 100,
  799. .cra_flags = CRYPTO_ALG_TYPE_AHASH |
  800. CRYPTO_ALG_ASYNC |
  801. CRYPTO_ALG_NEED_FALLBACK,
  802. .cra_blocksize = SHA1_BLOCK_SIZE,
  803. .cra_ctxsize = sizeof(struct omap_sham_ctx) +
  804. sizeof(struct omap_sham_hmac_ctx),
  805. .cra_alignmask = OMAP_ALIGN_MASK,
  806. .cra_module = THIS_MODULE,
  807. .cra_init = omap_sham_cra_sha1_init,
  808. .cra_exit = omap_sham_cra_exit,
  809. }
  810. },
  811. {
  812. .init = omap_sham_init,
  813. .update = omap_sham_update,
  814. .final = omap_sham_final,
  815. .finup = omap_sham_finup,
  816. .digest = omap_sham_digest,
  817. .setkey = omap_sham_setkey,
  818. .halg.digestsize = MD5_DIGEST_SIZE,
  819. .halg.base = {
  820. .cra_name = "hmac(md5)",
  821. .cra_driver_name = "omap-hmac-md5",
  822. .cra_priority = 100,
  823. .cra_flags = CRYPTO_ALG_TYPE_AHASH |
  824. CRYPTO_ALG_ASYNC |
  825. CRYPTO_ALG_NEED_FALLBACK,
  826. .cra_blocksize = SHA1_BLOCK_SIZE,
  827. .cra_ctxsize = sizeof(struct omap_sham_ctx) +
  828. sizeof(struct omap_sham_hmac_ctx),
  829. .cra_alignmask = OMAP_ALIGN_MASK,
  830. .cra_module = THIS_MODULE,
  831. .cra_init = omap_sham_cra_md5_init,
  832. .cra_exit = omap_sham_cra_exit,
  833. }
  834. }
  835. };
  836. static void omap_sham_done_task(unsigned long data)
  837. {
  838. struct omap_sham_dev *dd = (struct omap_sham_dev *)data;
  839. struct ahash_request *req = dd->req;
  840. struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
  841. int ready = 0, err = 0;
  842. if (ctx->flags & FLAGS_OUTPUT_READY) {
  843. ctx->flags &= ~FLAGS_OUTPUT_READY;
  844. ready = 1;
  845. }
  846. if (dd->flags & FLAGS_DMA_ACTIVE) {
  847. dd->flags &= ~FLAGS_DMA_ACTIVE;
  848. omap_sham_update_dma_stop(dd);
  849. if (!dd->err)
  850. err = omap_sham_update_dma_start(dd);
  851. }
  852. err = dd->err ? : err;
  853. if (err != -EINPROGRESS && (ready || err)) {
  854. dev_dbg(dd->dev, "update done: err: %d\n", err);
  855. /* finish curent request */
  856. omap_sham_finish_req(req, err);
  857. /* start new request */
  858. omap_sham_handle_queue(dd, NULL);
  859. }
  860. }
  861. static void omap_sham_queue_task(unsigned long data)
  862. {
  863. struct omap_sham_dev *dd = (struct omap_sham_dev *)data;
  864. omap_sham_handle_queue(dd, NULL);
  865. }
  866. static irqreturn_t omap_sham_irq(int irq, void *dev_id)
  867. {
  868. struct omap_sham_dev *dd = dev_id;
  869. struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req);
  870. if (!ctx) {
  871. dev_err(dd->dev, "unknown interrupt.\n");
  872. return IRQ_HANDLED;
  873. }
  874. if (unlikely(ctx->flags & FLAGS_FINAL))
  875. /* final -> allow device to go to power-saving mode */
  876. omap_sham_write_mask(dd, SHA_REG_CTRL, 0, SHA_REG_CTRL_LENGTH);
  877. omap_sham_write_mask(dd, SHA_REG_CTRL, SHA_REG_CTRL_OUTPUT_READY,
  878. SHA_REG_CTRL_OUTPUT_READY);
  879. omap_sham_read(dd, SHA_REG_CTRL);
  880. ctx->flags |= FLAGS_OUTPUT_READY;
  881. dd->err = 0;
  882. tasklet_schedule(&dd->done_task);
  883. return IRQ_HANDLED;
  884. }
  885. static void omap_sham_dma_callback(int lch, u16 ch_status, void *data)
  886. {
  887. struct omap_sham_dev *dd = data;
  888. if (ch_status != OMAP_DMA_BLOCK_IRQ) {
  889. pr_err("omap-sham DMA error status: 0x%hx\n", ch_status);
  890. dd->err = -EIO;
  891. dd->flags &= ~FLAGS_INIT; /* request to re-initialize */
  892. }
  893. tasklet_schedule(&dd->done_task);
  894. }
  895. static int omap_sham_dma_init(struct omap_sham_dev *dd)
  896. {
  897. int err;
  898. dd->dma_lch = -1;
  899. err = omap_request_dma(dd->dma, dev_name(dd->dev),
  900. omap_sham_dma_callback, dd, &dd->dma_lch);
  901. if (err) {
  902. dev_err(dd->dev, "Unable to request DMA channel\n");
  903. return err;
  904. }
  905. return 0;
  906. }
  907. static void omap_sham_dma_cleanup(struct omap_sham_dev *dd)
  908. {
  909. if (dd->dma_lch >= 0) {
  910. omap_free_dma(dd->dma_lch);
  911. dd->dma_lch = -1;
  912. }
  913. }
  914. static int __devinit omap_sham_probe(struct platform_device *pdev)
  915. {
  916. struct omap_sham_dev *dd;
  917. struct device *dev = &pdev->dev;
  918. struct resource *res;
  919. int err, i, j;
  920. dd = kzalloc(sizeof(struct omap_sham_dev), GFP_KERNEL);
  921. if (dd == NULL) {
  922. dev_err(dev, "unable to alloc data struct.\n");
  923. err = -ENOMEM;
  924. goto data_err;
  925. }
  926. dd->dev = dev;
  927. platform_set_drvdata(pdev, dd);
  928. INIT_LIST_HEAD(&dd->list);
  929. spin_lock_init(&dd->lock);
  930. tasklet_init(&dd->done_task, omap_sham_done_task, (unsigned long)dd);
  931. tasklet_init(&dd->queue_task, omap_sham_queue_task, (unsigned long)dd);
  932. crypto_init_queue(&dd->queue, OMAP_SHAM_QUEUE_LENGTH);
  933. dd->irq = -1;
  934. /* Get the base address */
  935. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  936. if (!res) {
  937. dev_err(dev, "no MEM resource info\n");
  938. err = -ENODEV;
  939. goto res_err;
  940. }
  941. dd->phys_base = res->start;
  942. /* Get the DMA */
  943. res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
  944. if (!res) {
  945. dev_err(dev, "no DMA resource info\n");
  946. err = -ENODEV;
  947. goto res_err;
  948. }
  949. dd->dma = res->start;
  950. /* Get the IRQ */
  951. dd->irq = platform_get_irq(pdev, 0);
  952. if (dd->irq < 0) {
  953. dev_err(dev, "no IRQ resource info\n");
  954. err = dd->irq;
  955. goto res_err;
  956. }
  957. err = request_irq(dd->irq, omap_sham_irq,
  958. IRQF_TRIGGER_LOW, dev_name(dev), dd);
  959. if (err) {
  960. dev_err(dev, "unable to request irq.\n");
  961. goto res_err;
  962. }
  963. err = omap_sham_dma_init(dd);
  964. if (err)
  965. goto dma_err;
  966. /* Initializing the clock */
  967. dd->iclk = clk_get(dev, "ick");
  968. if (IS_ERR(dd->iclk)) {
  969. dev_err(dev, "clock intialization failed.\n");
  970. err = PTR_ERR(dd->iclk);
  971. goto clk_err;
  972. }
  973. dd->io_base = ioremap(dd->phys_base, SZ_4K);
  974. if (!dd->io_base) {
  975. dev_err(dev, "can't ioremap\n");
  976. err = -ENOMEM;
  977. goto io_err;
  978. }
  979. clk_enable(dd->iclk);
  980. dev_info(dev, "hw accel on OMAP rev %u.%u\n",
  981. (omap_sham_read(dd, SHA_REG_REV) & SHA_REG_REV_MAJOR) >> 4,
  982. omap_sham_read(dd, SHA_REG_REV) & SHA_REG_REV_MINOR);
  983. clk_disable(dd->iclk);
  984. spin_lock(&sham.lock);
  985. list_add_tail(&dd->list, &sham.dev_list);
  986. spin_unlock(&sham.lock);
  987. for (i = 0; i < ARRAY_SIZE(algs); i++) {
  988. err = crypto_register_ahash(&algs[i]);
  989. if (err)
  990. goto err_algs;
  991. }
  992. return 0;
  993. err_algs:
  994. for (j = 0; j < i; j++)
  995. crypto_unregister_ahash(&algs[j]);
  996. iounmap(dd->io_base);
  997. io_err:
  998. clk_put(dd->iclk);
  999. clk_err:
  1000. omap_sham_dma_cleanup(dd);
  1001. dma_err:
  1002. if (dd->irq >= 0)
  1003. free_irq(dd->irq, dd);
  1004. res_err:
  1005. kfree(dd);
  1006. dd = NULL;
  1007. data_err:
  1008. dev_err(dev, "initialization failed.\n");
  1009. return err;
  1010. }
  1011. static int __devexit omap_sham_remove(struct platform_device *pdev)
  1012. {
  1013. static struct omap_sham_dev *dd;
  1014. int i;
  1015. dd = platform_get_drvdata(pdev);
  1016. if (!dd)
  1017. return -ENODEV;
  1018. spin_lock(&sham.lock);
  1019. list_del(&dd->list);
  1020. spin_unlock(&sham.lock);
  1021. for (i = 0; i < ARRAY_SIZE(algs); i++)
  1022. crypto_unregister_ahash(&algs[i]);
  1023. tasklet_kill(&dd->done_task);
  1024. tasklet_kill(&dd->queue_task);
  1025. iounmap(dd->io_base);
  1026. clk_put(dd->iclk);
  1027. omap_sham_dma_cleanup(dd);
  1028. if (dd->irq >= 0)
  1029. free_irq(dd->irq, dd);
  1030. kfree(dd);
  1031. dd = NULL;
  1032. return 0;
  1033. }
  1034. static struct platform_driver omap_sham_driver = {
  1035. .probe = omap_sham_probe,
  1036. .remove = omap_sham_remove,
  1037. .driver = {
  1038. .name = "omap-sham",
  1039. .owner = THIS_MODULE,
  1040. },
  1041. };
  1042. static int __init omap_sham_mod_init(void)
  1043. {
  1044. pr_info("loading %s driver\n", "omap-sham");
  1045. if (!cpu_class_is_omap2() ||
  1046. (omap_type() != OMAP2_DEVICE_TYPE_SEC &&
  1047. omap_type() != OMAP2_DEVICE_TYPE_EMU)) {
  1048. pr_err("Unsupported cpu\n");
  1049. return -ENODEV;
  1050. }
  1051. return platform_driver_register(&omap_sham_driver);
  1052. }
  1053. static void __exit omap_sham_mod_exit(void)
  1054. {
  1055. platform_driver_unregister(&omap_sham_driver);
  1056. }
  1057. module_init(omap_sham_mod_init);
  1058. module_exit(omap_sham_mod_exit);
  1059. MODULE_DESCRIPTION("OMAP SHA1/MD5 hw acceleration support.");
  1060. MODULE_LICENSE("GPL v2");
  1061. MODULE_AUTHOR("Dmitry Kasatkin");