omap-sham.c 34 KB

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