omap-sham.c 31 KB

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