omap-aes.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965
  1. /*
  2. * Cryptographic API.
  3. *
  4. * Support for OMAP AES 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. */
  14. #define pr_fmt(fmt) "%s: " fmt, __func__
  15. #include <linux/err.h>
  16. #include <linux/module.h>
  17. #include <linux/init.h>
  18. #include <linux/errno.h>
  19. #include <linux/kernel.h>
  20. #include <linux/clk.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/scatterlist.h>
  23. #include <linux/dma-mapping.h>
  24. #include <linux/io.h>
  25. #include <linux/crypto.h>
  26. #include <linux/interrupt.h>
  27. #include <crypto/scatterwalk.h>
  28. #include <crypto/aes.h>
  29. #include <plat/cpu.h>
  30. #include <plat/dma.h>
  31. /* OMAP TRM gives bitfields as start:end, where start is the higher bit
  32. number. For example 7:0 */
  33. #define FLD_MASK(start, end) (((1 << ((start) - (end) + 1)) - 1) << (end))
  34. #define FLD_VAL(val, start, end) (((val) << (end)) & FLD_MASK(start, end))
  35. #define AES_REG_KEY(x) (0x1C - ((x ^ 0x01) * 0x04))
  36. #define AES_REG_IV(x) (0x20 + ((x) * 0x04))
  37. #define AES_REG_CTRL 0x30
  38. #define AES_REG_CTRL_CTR_WIDTH (1 << 7)
  39. #define AES_REG_CTRL_CTR (1 << 6)
  40. #define AES_REG_CTRL_CBC (1 << 5)
  41. #define AES_REG_CTRL_KEY_SIZE (3 << 3)
  42. #define AES_REG_CTRL_DIRECTION (1 << 2)
  43. #define AES_REG_CTRL_INPUT_READY (1 << 1)
  44. #define AES_REG_CTRL_OUTPUT_READY (1 << 0)
  45. #define AES_REG_DATA 0x34
  46. #define AES_REG_DATA_N(x) (0x34 + ((x) * 0x04))
  47. #define AES_REG_REV 0x44
  48. #define AES_REG_REV_MAJOR 0xF0
  49. #define AES_REG_REV_MINOR 0x0F
  50. #define AES_REG_MASK 0x48
  51. #define AES_REG_MASK_SIDLE (1 << 6)
  52. #define AES_REG_MASK_START (1 << 5)
  53. #define AES_REG_MASK_DMA_OUT_EN (1 << 3)
  54. #define AES_REG_MASK_DMA_IN_EN (1 << 2)
  55. #define AES_REG_MASK_SOFTRESET (1 << 1)
  56. #define AES_REG_AUTOIDLE (1 << 0)
  57. #define AES_REG_SYSSTATUS 0x4C
  58. #define AES_REG_SYSSTATUS_RESETDONE (1 << 0)
  59. #define DEFAULT_TIMEOUT (5*HZ)
  60. #define FLAGS_MODE_MASK 0x000f
  61. #define FLAGS_ENCRYPT BIT(0)
  62. #define FLAGS_CBC BIT(1)
  63. #define FLAGS_GIV BIT(2)
  64. #define FLAGS_INIT BIT(4)
  65. #define FLAGS_FAST BIT(5)
  66. #define FLAGS_BUSY BIT(6)
  67. struct omap_aes_ctx {
  68. struct omap_aes_dev *dd;
  69. int keylen;
  70. u32 key[AES_KEYSIZE_256 / sizeof(u32)];
  71. unsigned long flags;
  72. };
  73. struct omap_aes_reqctx {
  74. unsigned long mode;
  75. };
  76. #define OMAP_AES_QUEUE_LENGTH 1
  77. #define OMAP_AES_CACHE_SIZE 0
  78. struct omap_aes_dev {
  79. struct list_head list;
  80. unsigned long phys_base;
  81. void __iomem *io_base;
  82. struct clk *iclk;
  83. struct omap_aes_ctx *ctx;
  84. struct device *dev;
  85. unsigned long flags;
  86. int err;
  87. spinlock_t lock;
  88. struct crypto_queue queue;
  89. struct tasklet_struct done_task;
  90. struct tasklet_struct queue_task;
  91. struct ablkcipher_request *req;
  92. size_t total;
  93. struct scatterlist *in_sg;
  94. size_t in_offset;
  95. struct scatterlist *out_sg;
  96. size_t out_offset;
  97. size_t buflen;
  98. void *buf_in;
  99. size_t dma_size;
  100. int dma_in;
  101. int dma_lch_in;
  102. dma_addr_t dma_addr_in;
  103. void *buf_out;
  104. int dma_out;
  105. int dma_lch_out;
  106. dma_addr_t dma_addr_out;
  107. };
  108. /* keep registered devices data here */
  109. static LIST_HEAD(dev_list);
  110. static DEFINE_SPINLOCK(list_lock);
  111. static inline u32 omap_aes_read(struct omap_aes_dev *dd, u32 offset)
  112. {
  113. return __raw_readl(dd->io_base + offset);
  114. }
  115. static inline void omap_aes_write(struct omap_aes_dev *dd, u32 offset,
  116. u32 value)
  117. {
  118. __raw_writel(value, dd->io_base + offset);
  119. }
  120. static inline void omap_aes_write_mask(struct omap_aes_dev *dd, u32 offset,
  121. u32 value, u32 mask)
  122. {
  123. u32 val;
  124. val = omap_aes_read(dd, offset);
  125. val &= ~mask;
  126. val |= value;
  127. omap_aes_write(dd, offset, val);
  128. }
  129. static void omap_aes_write_n(struct omap_aes_dev *dd, u32 offset,
  130. u32 *value, int count)
  131. {
  132. for (; count--; value++, offset += 4)
  133. omap_aes_write(dd, offset, *value);
  134. }
  135. static int omap_aes_wait(struct omap_aes_dev *dd, u32 offset, u32 bit)
  136. {
  137. unsigned long timeout = jiffies + DEFAULT_TIMEOUT;
  138. while (!(omap_aes_read(dd, offset) & bit)) {
  139. if (time_is_before_jiffies(timeout)) {
  140. dev_err(dd->dev, "omap-aes timeout\n");
  141. return -ETIMEDOUT;
  142. }
  143. }
  144. return 0;
  145. }
  146. static int omap_aes_hw_init(struct omap_aes_dev *dd)
  147. {
  148. /*
  149. * clocks are enabled when request starts and disabled when finished.
  150. * It may be long delays between requests.
  151. * Device might go to off mode to save power.
  152. */
  153. clk_enable(dd->iclk);
  154. if (!(dd->flags & FLAGS_INIT)) {
  155. /* is it necessary to reset before every operation? */
  156. omap_aes_write_mask(dd, AES_REG_MASK, AES_REG_MASK_SOFTRESET,
  157. AES_REG_MASK_SOFTRESET);
  158. /*
  159. * prevent OCP bus error (SRESP) in case an access to the module
  160. * is performed while the module is coming out of soft reset
  161. */
  162. __asm__ __volatile__("nop");
  163. __asm__ __volatile__("nop");
  164. if (omap_aes_wait(dd, AES_REG_SYSSTATUS,
  165. AES_REG_SYSSTATUS_RESETDONE))
  166. return -ETIMEDOUT;
  167. dd->flags |= FLAGS_INIT;
  168. dd->err = 0;
  169. }
  170. return 0;
  171. }
  172. static int omap_aes_write_ctrl(struct omap_aes_dev *dd)
  173. {
  174. unsigned int key32;
  175. int i, err;
  176. u32 val, mask;
  177. err = omap_aes_hw_init(dd);
  178. if (err)
  179. return err;
  180. val = 0;
  181. if (dd->dma_lch_out >= 0)
  182. val |= AES_REG_MASK_DMA_OUT_EN;
  183. if (dd->dma_lch_in >= 0)
  184. val |= AES_REG_MASK_DMA_IN_EN;
  185. mask = AES_REG_MASK_DMA_IN_EN | AES_REG_MASK_DMA_OUT_EN;
  186. omap_aes_write_mask(dd, AES_REG_MASK, val, mask);
  187. key32 = dd->ctx->keylen / sizeof(u32);
  188. /* it seems a key should always be set even if it has not changed */
  189. for (i = 0; i < key32; i++) {
  190. omap_aes_write(dd, AES_REG_KEY(i),
  191. __le32_to_cpu(dd->ctx->key[i]));
  192. }
  193. if ((dd->flags & FLAGS_CBC) && dd->req->info)
  194. omap_aes_write_n(dd, AES_REG_IV(0), dd->req->info, 4);
  195. val = FLD_VAL(((dd->ctx->keylen >> 3) - 1), 4, 3);
  196. if (dd->flags & FLAGS_CBC)
  197. val |= AES_REG_CTRL_CBC;
  198. if (dd->flags & FLAGS_ENCRYPT)
  199. val |= AES_REG_CTRL_DIRECTION;
  200. mask = AES_REG_CTRL_CBC | AES_REG_CTRL_DIRECTION |
  201. AES_REG_CTRL_KEY_SIZE;
  202. omap_aes_write_mask(dd, AES_REG_CTRL, val, mask);
  203. /* IN */
  204. omap_set_dma_dest_params(dd->dma_lch_in, 0, OMAP_DMA_AMODE_CONSTANT,
  205. dd->phys_base + AES_REG_DATA, 0, 4);
  206. omap_set_dma_dest_burst_mode(dd->dma_lch_in, OMAP_DMA_DATA_BURST_4);
  207. omap_set_dma_src_burst_mode(dd->dma_lch_in, OMAP_DMA_DATA_BURST_4);
  208. /* OUT */
  209. omap_set_dma_src_params(dd->dma_lch_out, 0, OMAP_DMA_AMODE_CONSTANT,
  210. dd->phys_base + AES_REG_DATA, 0, 4);
  211. omap_set_dma_src_burst_mode(dd->dma_lch_out, OMAP_DMA_DATA_BURST_4);
  212. omap_set_dma_dest_burst_mode(dd->dma_lch_out, OMAP_DMA_DATA_BURST_4);
  213. return 0;
  214. }
  215. static struct omap_aes_dev *omap_aes_find_dev(struct omap_aes_ctx *ctx)
  216. {
  217. struct omap_aes_dev *dd = NULL, *tmp;
  218. spin_lock_bh(&list_lock);
  219. if (!ctx->dd) {
  220. list_for_each_entry(tmp, &dev_list, list) {
  221. /* FIXME: take fist available aes core */
  222. dd = tmp;
  223. break;
  224. }
  225. ctx->dd = dd;
  226. } else {
  227. /* already found before */
  228. dd = ctx->dd;
  229. }
  230. spin_unlock_bh(&list_lock);
  231. return dd;
  232. }
  233. static void omap_aes_dma_callback(int lch, u16 ch_status, void *data)
  234. {
  235. struct omap_aes_dev *dd = data;
  236. if (ch_status != OMAP_DMA_BLOCK_IRQ) {
  237. pr_err("omap-aes DMA error status: 0x%hx\n", ch_status);
  238. dd->err = -EIO;
  239. dd->flags &= ~FLAGS_INIT; /* request to re-initialize */
  240. } else if (lch == dd->dma_lch_in) {
  241. return;
  242. }
  243. /* dma_lch_out - completed */
  244. tasklet_schedule(&dd->done_task);
  245. }
  246. static int omap_aes_dma_init(struct omap_aes_dev *dd)
  247. {
  248. int err = -ENOMEM;
  249. dd->dma_lch_out = -1;
  250. dd->dma_lch_in = -1;
  251. dd->buf_in = (void *)__get_free_pages(GFP_KERNEL, OMAP_AES_CACHE_SIZE);
  252. dd->buf_out = (void *)__get_free_pages(GFP_KERNEL, OMAP_AES_CACHE_SIZE);
  253. dd->buflen = PAGE_SIZE << OMAP_AES_CACHE_SIZE;
  254. dd->buflen &= ~(AES_BLOCK_SIZE - 1);
  255. if (!dd->buf_in || !dd->buf_out) {
  256. dev_err(dd->dev, "unable to alloc pages.\n");
  257. goto err_alloc;
  258. }
  259. /* MAP here */
  260. dd->dma_addr_in = dma_map_single(dd->dev, dd->buf_in, dd->buflen,
  261. DMA_TO_DEVICE);
  262. if (dma_mapping_error(dd->dev, dd->dma_addr_in)) {
  263. dev_err(dd->dev, "dma %d bytes error\n", dd->buflen);
  264. err = -EINVAL;
  265. goto err_map_in;
  266. }
  267. dd->dma_addr_out = dma_map_single(dd->dev, dd->buf_out, dd->buflen,
  268. DMA_FROM_DEVICE);
  269. if (dma_mapping_error(dd->dev, dd->dma_addr_out)) {
  270. dev_err(dd->dev, "dma %d bytes error\n", dd->buflen);
  271. err = -EINVAL;
  272. goto err_map_out;
  273. }
  274. err = omap_request_dma(dd->dma_in, "omap-aes-rx",
  275. omap_aes_dma_callback, dd, &dd->dma_lch_in);
  276. if (err) {
  277. dev_err(dd->dev, "Unable to request DMA channel\n");
  278. goto err_dma_in;
  279. }
  280. err = omap_request_dma(dd->dma_out, "omap-aes-tx",
  281. omap_aes_dma_callback, dd, &dd->dma_lch_out);
  282. if (err) {
  283. dev_err(dd->dev, "Unable to request DMA channel\n");
  284. goto err_dma_out;
  285. }
  286. return 0;
  287. err_dma_out:
  288. omap_free_dma(dd->dma_lch_in);
  289. err_dma_in:
  290. dma_unmap_single(dd->dev, dd->dma_addr_out, dd->buflen,
  291. DMA_FROM_DEVICE);
  292. err_map_out:
  293. dma_unmap_single(dd->dev, dd->dma_addr_in, dd->buflen, DMA_TO_DEVICE);
  294. err_map_in:
  295. free_pages((unsigned long)dd->buf_out, OMAP_AES_CACHE_SIZE);
  296. free_pages((unsigned long)dd->buf_in, OMAP_AES_CACHE_SIZE);
  297. err_alloc:
  298. if (err)
  299. pr_err("error: %d\n", err);
  300. return err;
  301. }
  302. static void omap_aes_dma_cleanup(struct omap_aes_dev *dd)
  303. {
  304. omap_free_dma(dd->dma_lch_out);
  305. omap_free_dma(dd->dma_lch_in);
  306. dma_unmap_single(dd->dev, dd->dma_addr_out, dd->buflen,
  307. DMA_FROM_DEVICE);
  308. dma_unmap_single(dd->dev, dd->dma_addr_in, dd->buflen, DMA_TO_DEVICE);
  309. free_pages((unsigned long)dd->buf_out, OMAP_AES_CACHE_SIZE);
  310. free_pages((unsigned long)dd->buf_in, OMAP_AES_CACHE_SIZE);
  311. }
  312. static void sg_copy_buf(void *buf, struct scatterlist *sg,
  313. unsigned int start, unsigned int nbytes, int out)
  314. {
  315. struct scatter_walk walk;
  316. if (!nbytes)
  317. return;
  318. scatterwalk_start(&walk, sg);
  319. scatterwalk_advance(&walk, start);
  320. scatterwalk_copychunks(buf, &walk, nbytes, out);
  321. scatterwalk_done(&walk, out, 0);
  322. }
  323. static int sg_copy(struct scatterlist **sg, size_t *offset, void *buf,
  324. size_t buflen, size_t total, int out)
  325. {
  326. unsigned int count, off = 0;
  327. while (buflen && total) {
  328. count = min((*sg)->length - *offset, total);
  329. count = min(count, buflen);
  330. if (!count)
  331. return off;
  332. /*
  333. * buflen and total are AES_BLOCK_SIZE size aligned,
  334. * so count should be also aligned
  335. */
  336. sg_copy_buf(buf + off, *sg, *offset, count, out);
  337. off += count;
  338. buflen -= count;
  339. *offset += count;
  340. total -= count;
  341. if (*offset == (*sg)->length) {
  342. *sg = sg_next(*sg);
  343. if (*sg)
  344. *offset = 0;
  345. else
  346. total = 0;
  347. }
  348. }
  349. return off;
  350. }
  351. static int omap_aes_crypt_dma(struct crypto_tfm *tfm, dma_addr_t dma_addr_in,
  352. dma_addr_t dma_addr_out, int length)
  353. {
  354. struct omap_aes_ctx *ctx = crypto_tfm_ctx(tfm);
  355. struct omap_aes_dev *dd = ctx->dd;
  356. int len32;
  357. pr_debug("len: %d\n", length);
  358. dd->dma_size = length;
  359. if (!(dd->flags & FLAGS_FAST))
  360. dma_sync_single_for_device(dd->dev, dma_addr_in, length,
  361. DMA_TO_DEVICE);
  362. len32 = DIV_ROUND_UP(length, sizeof(u32));
  363. /* IN */
  364. omap_set_dma_transfer_params(dd->dma_lch_in, OMAP_DMA_DATA_TYPE_S32,
  365. len32, 1, OMAP_DMA_SYNC_PACKET, dd->dma_in,
  366. OMAP_DMA_DST_SYNC);
  367. omap_set_dma_src_params(dd->dma_lch_in, 0, OMAP_DMA_AMODE_POST_INC,
  368. dma_addr_in, 0, 0);
  369. /* OUT */
  370. omap_set_dma_transfer_params(dd->dma_lch_out, OMAP_DMA_DATA_TYPE_S32,
  371. len32, 1, OMAP_DMA_SYNC_PACKET,
  372. dd->dma_out, OMAP_DMA_SRC_SYNC);
  373. omap_set_dma_dest_params(dd->dma_lch_out, 0, OMAP_DMA_AMODE_POST_INC,
  374. dma_addr_out, 0, 0);
  375. omap_start_dma(dd->dma_lch_in);
  376. omap_start_dma(dd->dma_lch_out);
  377. /* start DMA or disable idle mode */
  378. omap_aes_write_mask(dd, AES_REG_MASK, AES_REG_MASK_START,
  379. AES_REG_MASK_START);
  380. return 0;
  381. }
  382. static int omap_aes_crypt_dma_start(struct omap_aes_dev *dd)
  383. {
  384. struct crypto_tfm *tfm = crypto_ablkcipher_tfm(
  385. crypto_ablkcipher_reqtfm(dd->req));
  386. int err, fast = 0, in, out;
  387. size_t count;
  388. dma_addr_t addr_in, addr_out;
  389. pr_debug("total: %d\n", dd->total);
  390. if (sg_is_last(dd->in_sg) && sg_is_last(dd->out_sg)) {
  391. /* check for alignment */
  392. in = IS_ALIGNED((u32)dd->in_sg->offset, sizeof(u32));
  393. out = IS_ALIGNED((u32)dd->out_sg->offset, sizeof(u32));
  394. fast = in && out;
  395. }
  396. if (fast) {
  397. count = min(dd->total, sg_dma_len(dd->in_sg));
  398. count = min(count, sg_dma_len(dd->out_sg));
  399. if (count != dd->total) {
  400. pr_err("request length != buffer length\n");
  401. return -EINVAL;
  402. }
  403. pr_debug("fast\n");
  404. err = dma_map_sg(dd->dev, dd->in_sg, 1, DMA_TO_DEVICE);
  405. if (!err) {
  406. dev_err(dd->dev, "dma_map_sg() error\n");
  407. return -EINVAL;
  408. }
  409. err = dma_map_sg(dd->dev, dd->out_sg, 1, DMA_FROM_DEVICE);
  410. if (!err) {
  411. dev_err(dd->dev, "dma_map_sg() error\n");
  412. dma_unmap_sg(dd->dev, dd->in_sg, 1, DMA_TO_DEVICE);
  413. return -EINVAL;
  414. }
  415. addr_in = sg_dma_address(dd->in_sg);
  416. addr_out = sg_dma_address(dd->out_sg);
  417. dd->flags |= FLAGS_FAST;
  418. } else {
  419. /* use cache buffers */
  420. count = sg_copy(&dd->in_sg, &dd->in_offset, dd->buf_in,
  421. dd->buflen, dd->total, 0);
  422. addr_in = dd->dma_addr_in;
  423. addr_out = dd->dma_addr_out;
  424. dd->flags &= ~FLAGS_FAST;
  425. }
  426. dd->total -= count;
  427. err = omap_aes_crypt_dma(tfm, addr_in, addr_out, count);
  428. if (err) {
  429. dma_unmap_sg(dd->dev, dd->in_sg, 1, DMA_TO_DEVICE);
  430. dma_unmap_sg(dd->dev, dd->out_sg, 1, DMA_TO_DEVICE);
  431. }
  432. return err;
  433. }
  434. static void omap_aes_finish_req(struct omap_aes_dev *dd, int err)
  435. {
  436. struct ablkcipher_request *req = dd->req;
  437. pr_debug("err: %d\n", err);
  438. clk_disable(dd->iclk);
  439. dd->flags &= ~FLAGS_BUSY;
  440. req->base.complete(&req->base, err);
  441. }
  442. static int omap_aes_crypt_dma_stop(struct omap_aes_dev *dd)
  443. {
  444. int err = 0;
  445. size_t count;
  446. pr_debug("total: %d\n", dd->total);
  447. omap_aes_write_mask(dd, AES_REG_MASK, 0, AES_REG_MASK_START);
  448. omap_stop_dma(dd->dma_lch_in);
  449. omap_stop_dma(dd->dma_lch_out);
  450. if (dd->flags & FLAGS_FAST) {
  451. dma_unmap_sg(dd->dev, dd->out_sg, 1, DMA_FROM_DEVICE);
  452. dma_unmap_sg(dd->dev, dd->in_sg, 1, DMA_TO_DEVICE);
  453. } else {
  454. dma_sync_single_for_device(dd->dev, dd->dma_addr_out,
  455. dd->dma_size, DMA_FROM_DEVICE);
  456. /* copy data */
  457. count = sg_copy(&dd->out_sg, &dd->out_offset, dd->buf_out,
  458. dd->buflen, dd->dma_size, 1);
  459. if (count != dd->dma_size) {
  460. err = -EINVAL;
  461. pr_err("not all data converted: %u\n", count);
  462. }
  463. }
  464. return err;
  465. }
  466. static int omap_aes_handle_queue(struct omap_aes_dev *dd,
  467. struct ablkcipher_request *req)
  468. {
  469. struct crypto_async_request *async_req, *backlog;
  470. struct omap_aes_ctx *ctx;
  471. struct omap_aes_reqctx *rctx;
  472. unsigned long flags;
  473. int err, ret = 0;
  474. spin_lock_irqsave(&dd->lock, flags);
  475. if (req)
  476. ret = ablkcipher_enqueue_request(&dd->queue, req);
  477. if (dd->flags & FLAGS_BUSY) {
  478. spin_unlock_irqrestore(&dd->lock, flags);
  479. return ret;
  480. }
  481. backlog = crypto_get_backlog(&dd->queue);
  482. async_req = crypto_dequeue_request(&dd->queue);
  483. if (async_req)
  484. dd->flags |= FLAGS_BUSY;
  485. spin_unlock_irqrestore(&dd->lock, flags);
  486. if (!async_req)
  487. return ret;
  488. if (backlog)
  489. backlog->complete(backlog, -EINPROGRESS);
  490. req = ablkcipher_request_cast(async_req);
  491. /* assign new request to device */
  492. dd->req = req;
  493. dd->total = req->nbytes;
  494. dd->in_offset = 0;
  495. dd->in_sg = req->src;
  496. dd->out_offset = 0;
  497. dd->out_sg = req->dst;
  498. rctx = ablkcipher_request_ctx(req);
  499. ctx = crypto_ablkcipher_ctx(crypto_ablkcipher_reqtfm(req));
  500. rctx->mode &= FLAGS_MODE_MASK;
  501. dd->flags = (dd->flags & ~FLAGS_MODE_MASK) | rctx->mode;
  502. dd->ctx = ctx;
  503. ctx->dd = dd;
  504. err = omap_aes_write_ctrl(dd);
  505. if (!err)
  506. err = omap_aes_crypt_dma_start(dd);
  507. if (err) {
  508. /* aes_task will not finish it, so do it here */
  509. omap_aes_finish_req(dd, err);
  510. tasklet_schedule(&dd->queue_task);
  511. }
  512. return ret; /* return ret, which is enqueue return value */
  513. }
  514. static void omap_aes_done_task(unsigned long data)
  515. {
  516. struct omap_aes_dev *dd = (struct omap_aes_dev *)data;
  517. int err;
  518. pr_debug("enter\n");
  519. err = omap_aes_crypt_dma_stop(dd);
  520. err = dd->err ? : err;
  521. if (dd->total && !err) {
  522. err = omap_aes_crypt_dma_start(dd);
  523. if (!err)
  524. return; /* DMA started. Not fininishing. */
  525. }
  526. omap_aes_finish_req(dd, err);
  527. omap_aes_handle_queue(dd, NULL);
  528. pr_debug("exit\n");
  529. }
  530. static void omap_aes_queue_task(unsigned long data)
  531. {
  532. struct omap_aes_dev *dd = (struct omap_aes_dev *)data;
  533. omap_aes_handle_queue(dd, NULL);
  534. }
  535. static int omap_aes_crypt(struct ablkcipher_request *req, unsigned long mode)
  536. {
  537. struct omap_aes_ctx *ctx = crypto_ablkcipher_ctx(
  538. crypto_ablkcipher_reqtfm(req));
  539. struct omap_aes_reqctx *rctx = ablkcipher_request_ctx(req);
  540. struct omap_aes_dev *dd;
  541. pr_debug("nbytes: %d, enc: %d, cbc: %d\n", req->nbytes,
  542. !!(mode & FLAGS_ENCRYPT),
  543. !!(mode & FLAGS_CBC));
  544. if (!IS_ALIGNED(req->nbytes, AES_BLOCK_SIZE)) {
  545. pr_err("request size is not exact amount of AES blocks\n");
  546. return -EINVAL;
  547. }
  548. dd = omap_aes_find_dev(ctx);
  549. if (!dd)
  550. return -ENODEV;
  551. rctx->mode = mode;
  552. return omap_aes_handle_queue(dd, req);
  553. }
  554. /* ********************** ALG API ************************************ */
  555. static int omap_aes_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
  556. unsigned int keylen)
  557. {
  558. struct omap_aes_ctx *ctx = crypto_ablkcipher_ctx(tfm);
  559. if (keylen != AES_KEYSIZE_128 && keylen != AES_KEYSIZE_192 &&
  560. keylen != AES_KEYSIZE_256)
  561. return -EINVAL;
  562. pr_debug("enter, keylen: %d\n", keylen);
  563. memcpy(ctx->key, key, keylen);
  564. ctx->keylen = keylen;
  565. return 0;
  566. }
  567. static int omap_aes_ecb_encrypt(struct ablkcipher_request *req)
  568. {
  569. return omap_aes_crypt(req, FLAGS_ENCRYPT);
  570. }
  571. static int omap_aes_ecb_decrypt(struct ablkcipher_request *req)
  572. {
  573. return omap_aes_crypt(req, 0);
  574. }
  575. static int omap_aes_cbc_encrypt(struct ablkcipher_request *req)
  576. {
  577. return omap_aes_crypt(req, FLAGS_ENCRYPT | FLAGS_CBC);
  578. }
  579. static int omap_aes_cbc_decrypt(struct ablkcipher_request *req)
  580. {
  581. return omap_aes_crypt(req, FLAGS_CBC);
  582. }
  583. static int omap_aes_cra_init(struct crypto_tfm *tfm)
  584. {
  585. pr_debug("enter\n");
  586. tfm->crt_ablkcipher.reqsize = sizeof(struct omap_aes_reqctx);
  587. return 0;
  588. }
  589. static void omap_aes_cra_exit(struct crypto_tfm *tfm)
  590. {
  591. pr_debug("enter\n");
  592. }
  593. /* ********************** ALGS ************************************ */
  594. static struct crypto_alg algs[] = {
  595. {
  596. .cra_name = "ecb(aes)",
  597. .cra_driver_name = "ecb-aes-omap",
  598. .cra_priority = 100,
  599. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
  600. CRYPTO_ALG_KERN_DRIVER_ONLY |
  601. CRYPTO_ALG_ASYNC,
  602. .cra_blocksize = AES_BLOCK_SIZE,
  603. .cra_ctxsize = sizeof(struct omap_aes_ctx),
  604. .cra_alignmask = 0,
  605. .cra_type = &crypto_ablkcipher_type,
  606. .cra_module = THIS_MODULE,
  607. .cra_init = omap_aes_cra_init,
  608. .cra_exit = omap_aes_cra_exit,
  609. .cra_u.ablkcipher = {
  610. .min_keysize = AES_MIN_KEY_SIZE,
  611. .max_keysize = AES_MAX_KEY_SIZE,
  612. .setkey = omap_aes_setkey,
  613. .encrypt = omap_aes_ecb_encrypt,
  614. .decrypt = omap_aes_ecb_decrypt,
  615. }
  616. },
  617. {
  618. .cra_name = "cbc(aes)",
  619. .cra_driver_name = "cbc-aes-omap",
  620. .cra_priority = 100,
  621. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
  622. CRYPTO_ALG_KERN_DRIVER_ONLY |
  623. CRYPTO_ALG_ASYNC,
  624. .cra_blocksize = AES_BLOCK_SIZE,
  625. .cra_ctxsize = sizeof(struct omap_aes_ctx),
  626. .cra_alignmask = 0,
  627. .cra_type = &crypto_ablkcipher_type,
  628. .cra_module = THIS_MODULE,
  629. .cra_init = omap_aes_cra_init,
  630. .cra_exit = omap_aes_cra_exit,
  631. .cra_u.ablkcipher = {
  632. .min_keysize = AES_MIN_KEY_SIZE,
  633. .max_keysize = AES_MAX_KEY_SIZE,
  634. .ivsize = AES_BLOCK_SIZE,
  635. .setkey = omap_aes_setkey,
  636. .encrypt = omap_aes_cbc_encrypt,
  637. .decrypt = omap_aes_cbc_decrypt,
  638. }
  639. }
  640. };
  641. static int omap_aes_probe(struct platform_device *pdev)
  642. {
  643. struct device *dev = &pdev->dev;
  644. struct omap_aes_dev *dd;
  645. struct resource *res;
  646. int err = -ENOMEM, i, j;
  647. u32 reg;
  648. dd = kzalloc(sizeof(struct omap_aes_dev), GFP_KERNEL);
  649. if (dd == NULL) {
  650. dev_err(dev, "unable to alloc data struct.\n");
  651. goto err_data;
  652. }
  653. dd->dev = dev;
  654. platform_set_drvdata(pdev, dd);
  655. spin_lock_init(&dd->lock);
  656. crypto_init_queue(&dd->queue, OMAP_AES_QUEUE_LENGTH);
  657. /* Get the base address */
  658. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  659. if (!res) {
  660. dev_err(dev, "invalid resource type\n");
  661. err = -ENODEV;
  662. goto err_res;
  663. }
  664. dd->phys_base = res->start;
  665. /* Get the DMA */
  666. res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
  667. if (!res)
  668. dev_info(dev, "no DMA info\n");
  669. else
  670. dd->dma_out = res->start;
  671. /* Get the DMA */
  672. res = platform_get_resource(pdev, IORESOURCE_DMA, 1);
  673. if (!res)
  674. dev_info(dev, "no DMA info\n");
  675. else
  676. dd->dma_in = res->start;
  677. /* Initializing the clock */
  678. dd->iclk = clk_get(dev, "ick");
  679. if (IS_ERR(dd->iclk)) {
  680. dev_err(dev, "clock intialization failed.\n");
  681. err = PTR_ERR(dd->iclk);
  682. goto err_res;
  683. }
  684. dd->io_base = ioremap(dd->phys_base, SZ_4K);
  685. if (!dd->io_base) {
  686. dev_err(dev, "can't ioremap\n");
  687. err = -ENOMEM;
  688. goto err_io;
  689. }
  690. clk_enable(dd->iclk);
  691. reg = omap_aes_read(dd, AES_REG_REV);
  692. dev_info(dev, "OMAP AES hw accel rev: %u.%u\n",
  693. (reg & AES_REG_REV_MAJOR) >> 4, reg & AES_REG_REV_MINOR);
  694. clk_disable(dd->iclk);
  695. tasklet_init(&dd->done_task, omap_aes_done_task, (unsigned long)dd);
  696. tasklet_init(&dd->queue_task, omap_aes_queue_task, (unsigned long)dd);
  697. err = omap_aes_dma_init(dd);
  698. if (err)
  699. goto err_dma;
  700. INIT_LIST_HEAD(&dd->list);
  701. spin_lock(&list_lock);
  702. list_add_tail(&dd->list, &dev_list);
  703. spin_unlock(&list_lock);
  704. for (i = 0; i < ARRAY_SIZE(algs); i++) {
  705. pr_debug("i: %d\n", i);
  706. INIT_LIST_HEAD(&algs[i].cra_list);
  707. err = crypto_register_alg(&algs[i]);
  708. if (err)
  709. goto err_algs;
  710. }
  711. pr_info("probe() done\n");
  712. return 0;
  713. err_algs:
  714. for (j = 0; j < i; j++)
  715. crypto_unregister_alg(&algs[j]);
  716. omap_aes_dma_cleanup(dd);
  717. err_dma:
  718. tasklet_kill(&dd->done_task);
  719. tasklet_kill(&dd->queue_task);
  720. iounmap(dd->io_base);
  721. err_io:
  722. clk_put(dd->iclk);
  723. err_res:
  724. kfree(dd);
  725. dd = NULL;
  726. err_data:
  727. dev_err(dev, "initialization failed.\n");
  728. return err;
  729. }
  730. static int omap_aes_remove(struct platform_device *pdev)
  731. {
  732. struct omap_aes_dev *dd = platform_get_drvdata(pdev);
  733. int i;
  734. if (!dd)
  735. return -ENODEV;
  736. spin_lock(&list_lock);
  737. list_del(&dd->list);
  738. spin_unlock(&list_lock);
  739. for (i = 0; i < ARRAY_SIZE(algs); i++)
  740. crypto_unregister_alg(&algs[i]);
  741. tasklet_kill(&dd->done_task);
  742. tasklet_kill(&dd->queue_task);
  743. omap_aes_dma_cleanup(dd);
  744. iounmap(dd->io_base);
  745. clk_put(dd->iclk);
  746. kfree(dd);
  747. dd = NULL;
  748. return 0;
  749. }
  750. static struct platform_driver omap_aes_driver = {
  751. .probe = omap_aes_probe,
  752. .remove = omap_aes_remove,
  753. .driver = {
  754. .name = "omap-aes",
  755. .owner = THIS_MODULE,
  756. },
  757. };
  758. static int __init omap_aes_mod_init(void)
  759. {
  760. pr_info("loading %s driver\n", "omap-aes");
  761. if (!cpu_class_is_omap2() || omap_type() != OMAP2_DEVICE_TYPE_SEC) {
  762. pr_err("Unsupported cpu\n");
  763. return -ENODEV;
  764. }
  765. return platform_driver_register(&omap_aes_driver);
  766. }
  767. static void __exit omap_aes_mod_exit(void)
  768. {
  769. platform_driver_unregister(&omap_aes_driver);
  770. }
  771. module_init(omap_aes_mod_init);
  772. module_exit(omap_aes_mod_exit);
  773. MODULE_DESCRIPTION("OMAP AES hw acceleration support.");
  774. MODULE_LICENSE("GPL v2");
  775. MODULE_AUTHOR("Dmitry Kasatkin");