omap-aes.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326
  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. * Copyright (c) 2011 Texas Instruments Incorporated
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as published
  12. * by the Free Software Foundation.
  13. *
  14. */
  15. #define pr_fmt(fmt) "%20s: " fmt, __func__
  16. #define prn(num) pr_debug(#num "=%d\n", num)
  17. #define prx(num) pr_debug(#num "=%x\n", num)
  18. #include <linux/err.h>
  19. #include <linux/module.h>
  20. #include <linux/init.h>
  21. #include <linux/errno.h>
  22. #include <linux/kernel.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/scatterlist.h>
  25. #include <linux/dma-mapping.h>
  26. #include <linux/dmaengine.h>
  27. #include <linux/pm_runtime.h>
  28. #include <linux/of.h>
  29. #include <linux/of_device.h>
  30. #include <linux/of_address.h>
  31. #include <linux/io.h>
  32. #include <linux/crypto.h>
  33. #include <linux/interrupt.h>
  34. #include <crypto/scatterwalk.h>
  35. #include <crypto/aes.h>
  36. #include <crypto/engine.h>
  37. #include <crypto/internal/skcipher.h>
  38. #define DST_MAXBURST 4
  39. #define DMA_MIN (DST_MAXBURST * sizeof(u32))
  40. #define _calc_walked(inout) (dd->inout##_walk.offset - dd->inout##_sg->offset)
  41. /* OMAP TRM gives bitfields as start:end, where start is the higher bit
  42. number. For example 7:0 */
  43. #define FLD_MASK(start, end) (((1 << ((start) - (end) + 1)) - 1) << (end))
  44. #define FLD_VAL(val, start, end) (((val) << (end)) & FLD_MASK(start, end))
  45. #define AES_REG_KEY(dd, x) ((dd)->pdata->key_ofs - \
  46. ((x ^ 0x01) * 0x04))
  47. #define AES_REG_IV(dd, x) ((dd)->pdata->iv_ofs + ((x) * 0x04))
  48. #define AES_REG_CTRL(dd) ((dd)->pdata->ctrl_ofs)
  49. #define AES_REG_CTRL_CTR_WIDTH_MASK GENMASK(8, 7)
  50. #define AES_REG_CTRL_CTR_WIDTH_32 0
  51. #define AES_REG_CTRL_CTR_WIDTH_64 BIT(7)
  52. #define AES_REG_CTRL_CTR_WIDTH_96 BIT(8)
  53. #define AES_REG_CTRL_CTR_WIDTH_128 GENMASK(8, 7)
  54. #define AES_REG_CTRL_CTR BIT(6)
  55. #define AES_REG_CTRL_CBC BIT(5)
  56. #define AES_REG_CTRL_KEY_SIZE GENMASK(4, 3)
  57. #define AES_REG_CTRL_DIRECTION BIT(2)
  58. #define AES_REG_CTRL_INPUT_READY BIT(1)
  59. #define AES_REG_CTRL_OUTPUT_READY BIT(0)
  60. #define AES_REG_CTRL_MASK GENMASK(24, 2)
  61. #define AES_REG_DATA_N(dd, x) ((dd)->pdata->data_ofs + ((x) * 0x04))
  62. #define AES_REG_REV(dd) ((dd)->pdata->rev_ofs)
  63. #define AES_REG_MASK(dd) ((dd)->pdata->mask_ofs)
  64. #define AES_REG_MASK_SIDLE BIT(6)
  65. #define AES_REG_MASK_START BIT(5)
  66. #define AES_REG_MASK_DMA_OUT_EN BIT(3)
  67. #define AES_REG_MASK_DMA_IN_EN BIT(2)
  68. #define AES_REG_MASK_SOFTRESET BIT(1)
  69. #define AES_REG_AUTOIDLE BIT(0)
  70. #define AES_REG_LENGTH_N(x) (0x54 + ((x) * 0x04))
  71. #define AES_REG_IRQ_STATUS(dd) ((dd)->pdata->irq_status_ofs)
  72. #define AES_REG_IRQ_ENABLE(dd) ((dd)->pdata->irq_enable_ofs)
  73. #define AES_REG_IRQ_DATA_IN BIT(1)
  74. #define AES_REG_IRQ_DATA_OUT BIT(2)
  75. #define DEFAULT_TIMEOUT (5*HZ)
  76. #define DEFAULT_AUTOSUSPEND_DELAY 1000
  77. #define FLAGS_MODE_MASK 0x000f
  78. #define FLAGS_ENCRYPT BIT(0)
  79. #define FLAGS_CBC BIT(1)
  80. #define FLAGS_GIV BIT(2)
  81. #define FLAGS_CTR BIT(3)
  82. #define FLAGS_INIT BIT(4)
  83. #define FLAGS_FAST BIT(5)
  84. #define FLAGS_BUSY BIT(6)
  85. #define AES_BLOCK_WORDS (AES_BLOCK_SIZE >> 2)
  86. struct omap_aes_ctx {
  87. struct omap_aes_dev *dd;
  88. int keylen;
  89. u32 key[AES_KEYSIZE_256 / sizeof(u32)];
  90. unsigned long flags;
  91. struct crypto_skcipher *fallback;
  92. };
  93. struct omap_aes_reqctx {
  94. unsigned long mode;
  95. };
  96. #define OMAP_AES_QUEUE_LENGTH 1
  97. #define OMAP_AES_CACHE_SIZE 0
  98. struct omap_aes_algs_info {
  99. struct crypto_alg *algs_list;
  100. unsigned int size;
  101. unsigned int registered;
  102. };
  103. struct omap_aes_pdata {
  104. struct omap_aes_algs_info *algs_info;
  105. unsigned int algs_info_size;
  106. void (*trigger)(struct omap_aes_dev *dd, int length);
  107. u32 key_ofs;
  108. u32 iv_ofs;
  109. u32 ctrl_ofs;
  110. u32 data_ofs;
  111. u32 rev_ofs;
  112. u32 mask_ofs;
  113. u32 irq_enable_ofs;
  114. u32 irq_status_ofs;
  115. u32 dma_enable_in;
  116. u32 dma_enable_out;
  117. u32 dma_start;
  118. u32 major_mask;
  119. u32 major_shift;
  120. u32 minor_mask;
  121. u32 minor_shift;
  122. };
  123. struct omap_aes_dev {
  124. struct list_head list;
  125. unsigned long phys_base;
  126. void __iomem *io_base;
  127. struct omap_aes_ctx *ctx;
  128. struct device *dev;
  129. unsigned long flags;
  130. int err;
  131. struct tasklet_struct done_task;
  132. struct ablkcipher_request *req;
  133. struct crypto_engine *engine;
  134. /*
  135. * total is used by PIO mode for book keeping so introduce
  136. * variable total_save as need it to calc page_order
  137. */
  138. size_t total;
  139. size_t total_save;
  140. struct scatterlist *in_sg;
  141. struct scatterlist *out_sg;
  142. /* Buffers for copying for unaligned cases */
  143. struct scatterlist in_sgl;
  144. struct scatterlist out_sgl;
  145. struct scatterlist *orig_out;
  146. int sgs_copied;
  147. struct scatter_walk in_walk;
  148. struct scatter_walk out_walk;
  149. struct dma_chan *dma_lch_in;
  150. struct dma_chan *dma_lch_out;
  151. int in_sg_len;
  152. int out_sg_len;
  153. int pio_only;
  154. const struct omap_aes_pdata *pdata;
  155. };
  156. /* keep registered devices data here */
  157. static LIST_HEAD(dev_list);
  158. static DEFINE_SPINLOCK(list_lock);
  159. #ifdef DEBUG
  160. #define omap_aes_read(dd, offset) \
  161. ({ \
  162. int _read_ret; \
  163. _read_ret = __raw_readl(dd->io_base + offset); \
  164. pr_debug("omap_aes_read(" #offset "=%#x)= %#x\n", \
  165. offset, _read_ret); \
  166. _read_ret; \
  167. })
  168. #else
  169. static inline u32 omap_aes_read(struct omap_aes_dev *dd, u32 offset)
  170. {
  171. return __raw_readl(dd->io_base + offset);
  172. }
  173. #endif
  174. #ifdef DEBUG
  175. #define omap_aes_write(dd, offset, value) \
  176. do { \
  177. pr_debug("omap_aes_write(" #offset "=%#x) value=%#x\n", \
  178. offset, value); \
  179. __raw_writel(value, dd->io_base + offset); \
  180. } while (0)
  181. #else
  182. static inline void omap_aes_write(struct omap_aes_dev *dd, u32 offset,
  183. u32 value)
  184. {
  185. __raw_writel(value, dd->io_base + offset);
  186. }
  187. #endif
  188. static inline void omap_aes_write_mask(struct omap_aes_dev *dd, u32 offset,
  189. u32 value, u32 mask)
  190. {
  191. u32 val;
  192. val = omap_aes_read(dd, offset);
  193. val &= ~mask;
  194. val |= value;
  195. omap_aes_write(dd, offset, val);
  196. }
  197. static void omap_aes_write_n(struct omap_aes_dev *dd, u32 offset,
  198. u32 *value, int count)
  199. {
  200. for (; count--; value++, offset += 4)
  201. omap_aes_write(dd, offset, *value);
  202. }
  203. static int omap_aes_hw_init(struct omap_aes_dev *dd)
  204. {
  205. int err;
  206. if (!(dd->flags & FLAGS_INIT)) {
  207. dd->flags |= FLAGS_INIT;
  208. dd->err = 0;
  209. }
  210. err = pm_runtime_get_sync(dd->dev);
  211. if (err < 0) {
  212. dev_err(dd->dev, "failed to get sync: %d\n", err);
  213. return err;
  214. }
  215. return 0;
  216. }
  217. static int omap_aes_write_ctrl(struct omap_aes_dev *dd)
  218. {
  219. unsigned int key32;
  220. int i, err;
  221. u32 val;
  222. err = omap_aes_hw_init(dd);
  223. if (err)
  224. return err;
  225. key32 = dd->ctx->keylen / sizeof(u32);
  226. /* it seems a key should always be set even if it has not changed */
  227. for (i = 0; i < key32; i++) {
  228. omap_aes_write(dd, AES_REG_KEY(dd, i),
  229. __le32_to_cpu(dd->ctx->key[i]));
  230. }
  231. if ((dd->flags & (FLAGS_CBC | FLAGS_CTR)) && dd->req->info)
  232. omap_aes_write_n(dd, AES_REG_IV(dd, 0), dd->req->info, 4);
  233. val = FLD_VAL(((dd->ctx->keylen >> 3) - 1), 4, 3);
  234. if (dd->flags & FLAGS_CBC)
  235. val |= AES_REG_CTRL_CBC;
  236. if (dd->flags & FLAGS_CTR)
  237. val |= AES_REG_CTRL_CTR | AES_REG_CTRL_CTR_WIDTH_128;
  238. if (dd->flags & FLAGS_ENCRYPT)
  239. val |= AES_REG_CTRL_DIRECTION;
  240. omap_aes_write_mask(dd, AES_REG_CTRL(dd), val, AES_REG_CTRL_MASK);
  241. return 0;
  242. }
  243. static void omap_aes_dma_trigger_omap2(struct omap_aes_dev *dd, int length)
  244. {
  245. u32 mask, val;
  246. val = dd->pdata->dma_start;
  247. if (dd->dma_lch_out != NULL)
  248. val |= dd->pdata->dma_enable_out;
  249. if (dd->dma_lch_in != NULL)
  250. val |= dd->pdata->dma_enable_in;
  251. mask = dd->pdata->dma_enable_out | dd->pdata->dma_enable_in |
  252. dd->pdata->dma_start;
  253. omap_aes_write_mask(dd, AES_REG_MASK(dd), val, mask);
  254. }
  255. static void omap_aes_dma_trigger_omap4(struct omap_aes_dev *dd, int length)
  256. {
  257. omap_aes_write(dd, AES_REG_LENGTH_N(0), length);
  258. omap_aes_write(dd, AES_REG_LENGTH_N(1), 0);
  259. omap_aes_dma_trigger_omap2(dd, length);
  260. }
  261. static void omap_aes_dma_stop(struct omap_aes_dev *dd)
  262. {
  263. u32 mask;
  264. mask = dd->pdata->dma_enable_out | dd->pdata->dma_enable_in |
  265. dd->pdata->dma_start;
  266. omap_aes_write_mask(dd, AES_REG_MASK(dd), 0, mask);
  267. }
  268. static struct omap_aes_dev *omap_aes_find_dev(struct omap_aes_ctx *ctx)
  269. {
  270. struct omap_aes_dev *dd;
  271. spin_lock_bh(&list_lock);
  272. dd = list_first_entry(&dev_list, struct omap_aes_dev, list);
  273. list_move_tail(&dd->list, &dev_list);
  274. ctx->dd = dd;
  275. spin_unlock_bh(&list_lock);
  276. return dd;
  277. }
  278. static void omap_aes_dma_out_callback(void *data)
  279. {
  280. struct omap_aes_dev *dd = data;
  281. /* dma_lch_out - completed */
  282. tasklet_schedule(&dd->done_task);
  283. }
  284. static int omap_aes_dma_init(struct omap_aes_dev *dd)
  285. {
  286. int err;
  287. dd->dma_lch_out = NULL;
  288. dd->dma_lch_in = NULL;
  289. dd->dma_lch_in = dma_request_chan(dd->dev, "rx");
  290. if (IS_ERR(dd->dma_lch_in)) {
  291. dev_err(dd->dev, "Unable to request in DMA channel\n");
  292. return PTR_ERR(dd->dma_lch_in);
  293. }
  294. dd->dma_lch_out = dma_request_chan(dd->dev, "tx");
  295. if (IS_ERR(dd->dma_lch_out)) {
  296. dev_err(dd->dev, "Unable to request out DMA channel\n");
  297. err = PTR_ERR(dd->dma_lch_out);
  298. goto err_dma_out;
  299. }
  300. return 0;
  301. err_dma_out:
  302. dma_release_channel(dd->dma_lch_in);
  303. return err;
  304. }
  305. static void omap_aes_dma_cleanup(struct omap_aes_dev *dd)
  306. {
  307. if (dd->pio_only)
  308. return;
  309. dma_release_channel(dd->dma_lch_out);
  310. dma_release_channel(dd->dma_lch_in);
  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 omap_aes_crypt_dma(struct crypto_tfm *tfm,
  324. struct scatterlist *in_sg, struct scatterlist *out_sg,
  325. int in_sg_len, int out_sg_len)
  326. {
  327. struct omap_aes_ctx *ctx = crypto_tfm_ctx(tfm);
  328. struct omap_aes_dev *dd = ctx->dd;
  329. struct dma_async_tx_descriptor *tx_in, *tx_out;
  330. struct dma_slave_config cfg;
  331. int ret;
  332. if (dd->pio_only) {
  333. scatterwalk_start(&dd->in_walk, dd->in_sg);
  334. scatterwalk_start(&dd->out_walk, dd->out_sg);
  335. /* Enable DATAIN interrupt and let it take
  336. care of the rest */
  337. omap_aes_write(dd, AES_REG_IRQ_ENABLE(dd), 0x2);
  338. return 0;
  339. }
  340. dma_sync_sg_for_device(dd->dev, dd->in_sg, in_sg_len, DMA_TO_DEVICE);
  341. memset(&cfg, 0, sizeof(cfg));
  342. cfg.src_addr = dd->phys_base + AES_REG_DATA_N(dd, 0);
  343. cfg.dst_addr = dd->phys_base + AES_REG_DATA_N(dd, 0);
  344. cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  345. cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  346. cfg.src_maxburst = DST_MAXBURST;
  347. cfg.dst_maxburst = DST_MAXBURST;
  348. /* IN */
  349. ret = dmaengine_slave_config(dd->dma_lch_in, &cfg);
  350. if (ret) {
  351. dev_err(dd->dev, "can't configure IN dmaengine slave: %d\n",
  352. ret);
  353. return ret;
  354. }
  355. tx_in = dmaengine_prep_slave_sg(dd->dma_lch_in, in_sg, in_sg_len,
  356. DMA_MEM_TO_DEV,
  357. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  358. if (!tx_in) {
  359. dev_err(dd->dev, "IN prep_slave_sg() failed\n");
  360. return -EINVAL;
  361. }
  362. /* No callback necessary */
  363. tx_in->callback_param = dd;
  364. /* OUT */
  365. ret = dmaengine_slave_config(dd->dma_lch_out, &cfg);
  366. if (ret) {
  367. dev_err(dd->dev, "can't configure OUT dmaengine slave: %d\n",
  368. ret);
  369. return ret;
  370. }
  371. tx_out = dmaengine_prep_slave_sg(dd->dma_lch_out, out_sg, out_sg_len,
  372. DMA_DEV_TO_MEM,
  373. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  374. if (!tx_out) {
  375. dev_err(dd->dev, "OUT prep_slave_sg() failed\n");
  376. return -EINVAL;
  377. }
  378. tx_out->callback = omap_aes_dma_out_callback;
  379. tx_out->callback_param = dd;
  380. dmaengine_submit(tx_in);
  381. dmaengine_submit(tx_out);
  382. dma_async_issue_pending(dd->dma_lch_in);
  383. dma_async_issue_pending(dd->dma_lch_out);
  384. /* start DMA */
  385. dd->pdata->trigger(dd, dd->total);
  386. return 0;
  387. }
  388. static int omap_aes_crypt_dma_start(struct omap_aes_dev *dd)
  389. {
  390. struct crypto_tfm *tfm = crypto_ablkcipher_tfm(
  391. crypto_ablkcipher_reqtfm(dd->req));
  392. int err;
  393. pr_debug("total: %d\n", dd->total);
  394. if (!dd->pio_only) {
  395. err = dma_map_sg(dd->dev, dd->in_sg, dd->in_sg_len,
  396. DMA_TO_DEVICE);
  397. if (!err) {
  398. dev_err(dd->dev, "dma_map_sg() error\n");
  399. return -EINVAL;
  400. }
  401. err = dma_map_sg(dd->dev, dd->out_sg, dd->out_sg_len,
  402. DMA_FROM_DEVICE);
  403. if (!err) {
  404. dev_err(dd->dev, "dma_map_sg() error\n");
  405. return -EINVAL;
  406. }
  407. }
  408. err = omap_aes_crypt_dma(tfm, dd->in_sg, dd->out_sg, dd->in_sg_len,
  409. dd->out_sg_len);
  410. if (err && !dd->pio_only) {
  411. dma_unmap_sg(dd->dev, dd->in_sg, dd->in_sg_len, DMA_TO_DEVICE);
  412. dma_unmap_sg(dd->dev, dd->out_sg, dd->out_sg_len,
  413. DMA_FROM_DEVICE);
  414. }
  415. return err;
  416. }
  417. static void omap_aes_finish_req(struct omap_aes_dev *dd, int err)
  418. {
  419. struct ablkcipher_request *req = dd->req;
  420. pr_debug("err: %d\n", err);
  421. crypto_finalize_cipher_request(dd->engine, req, err);
  422. pm_runtime_mark_last_busy(dd->dev);
  423. pm_runtime_put_autosuspend(dd->dev);
  424. }
  425. static int omap_aes_crypt_dma_stop(struct omap_aes_dev *dd)
  426. {
  427. pr_debug("total: %d\n", dd->total);
  428. omap_aes_dma_stop(dd);
  429. return 0;
  430. }
  431. static int omap_aes_check_aligned(struct scatterlist *sg, int total)
  432. {
  433. int len = 0;
  434. if (!IS_ALIGNED(total, AES_BLOCK_SIZE))
  435. return -EINVAL;
  436. while (sg) {
  437. if (!IS_ALIGNED(sg->offset, 4))
  438. return -1;
  439. if (!IS_ALIGNED(sg->length, AES_BLOCK_SIZE))
  440. return -1;
  441. len += sg->length;
  442. sg = sg_next(sg);
  443. }
  444. if (len != total)
  445. return -1;
  446. return 0;
  447. }
  448. static int omap_aes_copy_sgs(struct omap_aes_dev *dd)
  449. {
  450. void *buf_in, *buf_out;
  451. int pages, total;
  452. total = ALIGN(dd->total, AES_BLOCK_SIZE);
  453. pages = get_order(total);
  454. buf_in = (void *)__get_free_pages(GFP_ATOMIC, pages);
  455. buf_out = (void *)__get_free_pages(GFP_ATOMIC, pages);
  456. if (!buf_in || !buf_out) {
  457. pr_err("Couldn't allocated pages for unaligned cases.\n");
  458. return -1;
  459. }
  460. dd->orig_out = dd->out_sg;
  461. sg_copy_buf(buf_in, dd->in_sg, 0, dd->total, 0);
  462. sg_init_table(&dd->in_sgl, 1);
  463. sg_set_buf(&dd->in_sgl, buf_in, total);
  464. dd->in_sg = &dd->in_sgl;
  465. dd->in_sg_len = 1;
  466. sg_init_table(&dd->out_sgl, 1);
  467. sg_set_buf(&dd->out_sgl, buf_out, total);
  468. dd->out_sg = &dd->out_sgl;
  469. dd->out_sg_len = 1;
  470. return 0;
  471. }
  472. static int omap_aes_handle_queue(struct omap_aes_dev *dd,
  473. struct ablkcipher_request *req)
  474. {
  475. if (req)
  476. return crypto_transfer_cipher_request_to_engine(dd->engine, req);
  477. return 0;
  478. }
  479. static int omap_aes_prepare_req(struct crypto_engine *engine,
  480. struct ablkcipher_request *req)
  481. {
  482. struct omap_aes_ctx *ctx = crypto_ablkcipher_ctx(
  483. crypto_ablkcipher_reqtfm(req));
  484. struct omap_aes_dev *dd = ctx->dd;
  485. struct omap_aes_reqctx *rctx;
  486. if (!dd)
  487. return -ENODEV;
  488. /* assign new request to device */
  489. dd->req = req;
  490. dd->total = req->nbytes;
  491. dd->total_save = req->nbytes;
  492. dd->in_sg = req->src;
  493. dd->out_sg = req->dst;
  494. dd->in_sg_len = sg_nents_for_len(dd->in_sg, dd->total);
  495. if (dd->in_sg_len < 0)
  496. return dd->in_sg_len;
  497. dd->out_sg_len = sg_nents_for_len(dd->out_sg, dd->total);
  498. if (dd->out_sg_len < 0)
  499. return dd->out_sg_len;
  500. if (omap_aes_check_aligned(dd->in_sg, dd->total) ||
  501. omap_aes_check_aligned(dd->out_sg, dd->total)) {
  502. if (omap_aes_copy_sgs(dd))
  503. pr_err("Failed to copy SGs for unaligned cases\n");
  504. dd->sgs_copied = 1;
  505. } else {
  506. dd->sgs_copied = 0;
  507. }
  508. rctx = ablkcipher_request_ctx(req);
  509. ctx = crypto_ablkcipher_ctx(crypto_ablkcipher_reqtfm(req));
  510. rctx->mode &= FLAGS_MODE_MASK;
  511. dd->flags = (dd->flags & ~FLAGS_MODE_MASK) | rctx->mode;
  512. dd->ctx = ctx;
  513. ctx->dd = dd;
  514. return omap_aes_write_ctrl(dd);
  515. }
  516. static int omap_aes_crypt_req(struct crypto_engine *engine,
  517. struct ablkcipher_request *req)
  518. {
  519. struct omap_aes_ctx *ctx = crypto_ablkcipher_ctx(
  520. crypto_ablkcipher_reqtfm(req));
  521. struct omap_aes_dev *dd = ctx->dd;
  522. if (!dd)
  523. return -ENODEV;
  524. return omap_aes_crypt_dma_start(dd);
  525. }
  526. static void omap_aes_done_task(unsigned long data)
  527. {
  528. struct omap_aes_dev *dd = (struct omap_aes_dev *)data;
  529. void *buf_in, *buf_out;
  530. int pages, len;
  531. pr_debug("enter done_task\n");
  532. if (!dd->pio_only) {
  533. dma_sync_sg_for_device(dd->dev, dd->out_sg, dd->out_sg_len,
  534. DMA_FROM_DEVICE);
  535. dma_unmap_sg(dd->dev, dd->in_sg, dd->in_sg_len, DMA_TO_DEVICE);
  536. dma_unmap_sg(dd->dev, dd->out_sg, dd->out_sg_len,
  537. DMA_FROM_DEVICE);
  538. omap_aes_crypt_dma_stop(dd);
  539. }
  540. if (dd->sgs_copied) {
  541. buf_in = sg_virt(&dd->in_sgl);
  542. buf_out = sg_virt(&dd->out_sgl);
  543. sg_copy_buf(buf_out, dd->orig_out, 0, dd->total_save, 1);
  544. len = ALIGN(dd->total_save, AES_BLOCK_SIZE);
  545. pages = get_order(len);
  546. free_pages((unsigned long)buf_in, pages);
  547. free_pages((unsigned long)buf_out, pages);
  548. }
  549. omap_aes_finish_req(dd, 0);
  550. pr_debug("exit\n");
  551. }
  552. static int omap_aes_crypt(struct ablkcipher_request *req, unsigned long mode)
  553. {
  554. struct omap_aes_ctx *ctx = crypto_ablkcipher_ctx(
  555. crypto_ablkcipher_reqtfm(req));
  556. struct omap_aes_reqctx *rctx = ablkcipher_request_ctx(req);
  557. struct omap_aes_dev *dd;
  558. int ret;
  559. pr_debug("nbytes: %d, enc: %d, cbc: %d\n", req->nbytes,
  560. !!(mode & FLAGS_ENCRYPT),
  561. !!(mode & FLAGS_CBC));
  562. if (req->nbytes < 200) {
  563. SKCIPHER_REQUEST_ON_STACK(subreq, ctx->fallback);
  564. skcipher_request_set_tfm(subreq, ctx->fallback);
  565. skcipher_request_set_callback(subreq, req->base.flags, NULL,
  566. NULL);
  567. skcipher_request_set_crypt(subreq, req->src, req->dst,
  568. req->nbytes, req->info);
  569. if (mode & FLAGS_ENCRYPT)
  570. ret = crypto_skcipher_encrypt(subreq);
  571. else
  572. ret = crypto_skcipher_decrypt(subreq);
  573. skcipher_request_zero(subreq);
  574. return ret;
  575. }
  576. dd = omap_aes_find_dev(ctx);
  577. if (!dd)
  578. return -ENODEV;
  579. rctx->mode = mode;
  580. return omap_aes_handle_queue(dd, req);
  581. }
  582. /* ********************** ALG API ************************************ */
  583. static int omap_aes_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
  584. unsigned int keylen)
  585. {
  586. struct omap_aes_ctx *ctx = crypto_ablkcipher_ctx(tfm);
  587. int ret;
  588. if (keylen != AES_KEYSIZE_128 && keylen != AES_KEYSIZE_192 &&
  589. keylen != AES_KEYSIZE_256)
  590. return -EINVAL;
  591. pr_debug("enter, keylen: %d\n", keylen);
  592. memcpy(ctx->key, key, keylen);
  593. ctx->keylen = keylen;
  594. crypto_skcipher_clear_flags(ctx->fallback, CRYPTO_TFM_REQ_MASK);
  595. crypto_skcipher_set_flags(ctx->fallback, tfm->base.crt_flags &
  596. CRYPTO_TFM_REQ_MASK);
  597. ret = crypto_skcipher_setkey(ctx->fallback, key, keylen);
  598. if (!ret)
  599. return 0;
  600. return 0;
  601. }
  602. static int omap_aes_ecb_encrypt(struct ablkcipher_request *req)
  603. {
  604. return omap_aes_crypt(req, FLAGS_ENCRYPT);
  605. }
  606. static int omap_aes_ecb_decrypt(struct ablkcipher_request *req)
  607. {
  608. return omap_aes_crypt(req, 0);
  609. }
  610. static int omap_aes_cbc_encrypt(struct ablkcipher_request *req)
  611. {
  612. return omap_aes_crypt(req, FLAGS_ENCRYPT | FLAGS_CBC);
  613. }
  614. static int omap_aes_cbc_decrypt(struct ablkcipher_request *req)
  615. {
  616. return omap_aes_crypt(req, FLAGS_CBC);
  617. }
  618. static int omap_aes_ctr_encrypt(struct ablkcipher_request *req)
  619. {
  620. return omap_aes_crypt(req, FLAGS_ENCRYPT | FLAGS_CTR);
  621. }
  622. static int omap_aes_ctr_decrypt(struct ablkcipher_request *req)
  623. {
  624. return omap_aes_crypt(req, FLAGS_CTR);
  625. }
  626. static int omap_aes_cra_init(struct crypto_tfm *tfm)
  627. {
  628. const char *name = crypto_tfm_alg_name(tfm);
  629. const u32 flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK;
  630. struct omap_aes_ctx *ctx = crypto_tfm_ctx(tfm);
  631. struct crypto_skcipher *blk;
  632. blk = crypto_alloc_skcipher(name, 0, flags);
  633. if (IS_ERR(blk))
  634. return PTR_ERR(blk);
  635. ctx->fallback = blk;
  636. tfm->crt_ablkcipher.reqsize = sizeof(struct omap_aes_reqctx);
  637. return 0;
  638. }
  639. static void omap_aes_cra_exit(struct crypto_tfm *tfm)
  640. {
  641. struct omap_aes_ctx *ctx = crypto_tfm_ctx(tfm);
  642. if (ctx->fallback)
  643. crypto_free_skcipher(ctx->fallback);
  644. ctx->fallback = NULL;
  645. }
  646. /* ********************** ALGS ************************************ */
  647. static struct crypto_alg algs_ecb_cbc[] = {
  648. {
  649. .cra_name = "ecb(aes)",
  650. .cra_driver_name = "ecb-aes-omap",
  651. .cra_priority = 300,
  652. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
  653. CRYPTO_ALG_KERN_DRIVER_ONLY |
  654. CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK,
  655. .cra_blocksize = AES_BLOCK_SIZE,
  656. .cra_ctxsize = sizeof(struct omap_aes_ctx),
  657. .cra_alignmask = 0,
  658. .cra_type = &crypto_ablkcipher_type,
  659. .cra_module = THIS_MODULE,
  660. .cra_init = omap_aes_cra_init,
  661. .cra_exit = omap_aes_cra_exit,
  662. .cra_u.ablkcipher = {
  663. .min_keysize = AES_MIN_KEY_SIZE,
  664. .max_keysize = AES_MAX_KEY_SIZE,
  665. .setkey = omap_aes_setkey,
  666. .encrypt = omap_aes_ecb_encrypt,
  667. .decrypt = omap_aes_ecb_decrypt,
  668. }
  669. },
  670. {
  671. .cra_name = "cbc(aes)",
  672. .cra_driver_name = "cbc-aes-omap",
  673. .cra_priority = 300,
  674. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
  675. CRYPTO_ALG_KERN_DRIVER_ONLY |
  676. CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK,
  677. .cra_blocksize = AES_BLOCK_SIZE,
  678. .cra_ctxsize = sizeof(struct omap_aes_ctx),
  679. .cra_alignmask = 0,
  680. .cra_type = &crypto_ablkcipher_type,
  681. .cra_module = THIS_MODULE,
  682. .cra_init = omap_aes_cra_init,
  683. .cra_exit = omap_aes_cra_exit,
  684. .cra_u.ablkcipher = {
  685. .min_keysize = AES_MIN_KEY_SIZE,
  686. .max_keysize = AES_MAX_KEY_SIZE,
  687. .ivsize = AES_BLOCK_SIZE,
  688. .setkey = omap_aes_setkey,
  689. .encrypt = omap_aes_cbc_encrypt,
  690. .decrypt = omap_aes_cbc_decrypt,
  691. }
  692. }
  693. };
  694. static struct crypto_alg algs_ctr[] = {
  695. {
  696. .cra_name = "ctr(aes)",
  697. .cra_driver_name = "ctr-aes-omap",
  698. .cra_priority = 300,
  699. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
  700. CRYPTO_ALG_KERN_DRIVER_ONLY |
  701. CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK,
  702. .cra_blocksize = AES_BLOCK_SIZE,
  703. .cra_ctxsize = sizeof(struct omap_aes_ctx),
  704. .cra_alignmask = 0,
  705. .cra_type = &crypto_ablkcipher_type,
  706. .cra_module = THIS_MODULE,
  707. .cra_init = omap_aes_cra_init,
  708. .cra_exit = omap_aes_cra_exit,
  709. .cra_u.ablkcipher = {
  710. .min_keysize = AES_MIN_KEY_SIZE,
  711. .max_keysize = AES_MAX_KEY_SIZE,
  712. .geniv = "eseqiv",
  713. .ivsize = AES_BLOCK_SIZE,
  714. .setkey = omap_aes_setkey,
  715. .encrypt = omap_aes_ctr_encrypt,
  716. .decrypt = omap_aes_ctr_decrypt,
  717. }
  718. } ,
  719. };
  720. static struct omap_aes_algs_info omap_aes_algs_info_ecb_cbc[] = {
  721. {
  722. .algs_list = algs_ecb_cbc,
  723. .size = ARRAY_SIZE(algs_ecb_cbc),
  724. },
  725. };
  726. static const struct omap_aes_pdata omap_aes_pdata_omap2 = {
  727. .algs_info = omap_aes_algs_info_ecb_cbc,
  728. .algs_info_size = ARRAY_SIZE(omap_aes_algs_info_ecb_cbc),
  729. .trigger = omap_aes_dma_trigger_omap2,
  730. .key_ofs = 0x1c,
  731. .iv_ofs = 0x20,
  732. .ctrl_ofs = 0x30,
  733. .data_ofs = 0x34,
  734. .rev_ofs = 0x44,
  735. .mask_ofs = 0x48,
  736. .dma_enable_in = BIT(2),
  737. .dma_enable_out = BIT(3),
  738. .dma_start = BIT(5),
  739. .major_mask = 0xf0,
  740. .major_shift = 4,
  741. .minor_mask = 0x0f,
  742. .minor_shift = 0,
  743. };
  744. #ifdef CONFIG_OF
  745. static struct omap_aes_algs_info omap_aes_algs_info_ecb_cbc_ctr[] = {
  746. {
  747. .algs_list = algs_ecb_cbc,
  748. .size = ARRAY_SIZE(algs_ecb_cbc),
  749. },
  750. {
  751. .algs_list = algs_ctr,
  752. .size = ARRAY_SIZE(algs_ctr),
  753. },
  754. };
  755. static const struct omap_aes_pdata omap_aes_pdata_omap3 = {
  756. .algs_info = omap_aes_algs_info_ecb_cbc_ctr,
  757. .algs_info_size = ARRAY_SIZE(omap_aes_algs_info_ecb_cbc_ctr),
  758. .trigger = omap_aes_dma_trigger_omap2,
  759. .key_ofs = 0x1c,
  760. .iv_ofs = 0x20,
  761. .ctrl_ofs = 0x30,
  762. .data_ofs = 0x34,
  763. .rev_ofs = 0x44,
  764. .mask_ofs = 0x48,
  765. .dma_enable_in = BIT(2),
  766. .dma_enable_out = BIT(3),
  767. .dma_start = BIT(5),
  768. .major_mask = 0xf0,
  769. .major_shift = 4,
  770. .minor_mask = 0x0f,
  771. .minor_shift = 0,
  772. };
  773. static const struct omap_aes_pdata omap_aes_pdata_omap4 = {
  774. .algs_info = omap_aes_algs_info_ecb_cbc_ctr,
  775. .algs_info_size = ARRAY_SIZE(omap_aes_algs_info_ecb_cbc_ctr),
  776. .trigger = omap_aes_dma_trigger_omap4,
  777. .key_ofs = 0x3c,
  778. .iv_ofs = 0x40,
  779. .ctrl_ofs = 0x50,
  780. .data_ofs = 0x60,
  781. .rev_ofs = 0x80,
  782. .mask_ofs = 0x84,
  783. .irq_status_ofs = 0x8c,
  784. .irq_enable_ofs = 0x90,
  785. .dma_enable_in = BIT(5),
  786. .dma_enable_out = BIT(6),
  787. .major_mask = 0x0700,
  788. .major_shift = 8,
  789. .minor_mask = 0x003f,
  790. .minor_shift = 0,
  791. };
  792. static irqreturn_t omap_aes_irq(int irq, void *dev_id)
  793. {
  794. struct omap_aes_dev *dd = dev_id;
  795. u32 status, i;
  796. u32 *src, *dst;
  797. status = omap_aes_read(dd, AES_REG_IRQ_STATUS(dd));
  798. if (status & AES_REG_IRQ_DATA_IN) {
  799. omap_aes_write(dd, AES_REG_IRQ_ENABLE(dd), 0x0);
  800. BUG_ON(!dd->in_sg);
  801. BUG_ON(_calc_walked(in) > dd->in_sg->length);
  802. src = sg_virt(dd->in_sg) + _calc_walked(in);
  803. for (i = 0; i < AES_BLOCK_WORDS; i++) {
  804. omap_aes_write(dd, AES_REG_DATA_N(dd, i), *src);
  805. scatterwalk_advance(&dd->in_walk, 4);
  806. if (dd->in_sg->length == _calc_walked(in)) {
  807. dd->in_sg = sg_next(dd->in_sg);
  808. if (dd->in_sg) {
  809. scatterwalk_start(&dd->in_walk,
  810. dd->in_sg);
  811. src = sg_virt(dd->in_sg) +
  812. _calc_walked(in);
  813. }
  814. } else {
  815. src++;
  816. }
  817. }
  818. /* Clear IRQ status */
  819. status &= ~AES_REG_IRQ_DATA_IN;
  820. omap_aes_write(dd, AES_REG_IRQ_STATUS(dd), status);
  821. /* Enable DATA_OUT interrupt */
  822. omap_aes_write(dd, AES_REG_IRQ_ENABLE(dd), 0x4);
  823. } else if (status & AES_REG_IRQ_DATA_OUT) {
  824. omap_aes_write(dd, AES_REG_IRQ_ENABLE(dd), 0x0);
  825. BUG_ON(!dd->out_sg);
  826. BUG_ON(_calc_walked(out) > dd->out_sg->length);
  827. dst = sg_virt(dd->out_sg) + _calc_walked(out);
  828. for (i = 0; i < AES_BLOCK_WORDS; i++) {
  829. *dst = omap_aes_read(dd, AES_REG_DATA_N(dd, i));
  830. scatterwalk_advance(&dd->out_walk, 4);
  831. if (dd->out_sg->length == _calc_walked(out)) {
  832. dd->out_sg = sg_next(dd->out_sg);
  833. if (dd->out_sg) {
  834. scatterwalk_start(&dd->out_walk,
  835. dd->out_sg);
  836. dst = sg_virt(dd->out_sg) +
  837. _calc_walked(out);
  838. }
  839. } else {
  840. dst++;
  841. }
  842. }
  843. dd->total -= min_t(size_t, AES_BLOCK_SIZE, dd->total);
  844. /* Clear IRQ status */
  845. status &= ~AES_REG_IRQ_DATA_OUT;
  846. omap_aes_write(dd, AES_REG_IRQ_STATUS(dd), status);
  847. if (!dd->total)
  848. /* All bytes read! */
  849. tasklet_schedule(&dd->done_task);
  850. else
  851. /* Enable DATA_IN interrupt for next block */
  852. omap_aes_write(dd, AES_REG_IRQ_ENABLE(dd), 0x2);
  853. }
  854. return IRQ_HANDLED;
  855. }
  856. static const struct of_device_id omap_aes_of_match[] = {
  857. {
  858. .compatible = "ti,omap2-aes",
  859. .data = &omap_aes_pdata_omap2,
  860. },
  861. {
  862. .compatible = "ti,omap3-aes",
  863. .data = &omap_aes_pdata_omap3,
  864. },
  865. {
  866. .compatible = "ti,omap4-aes",
  867. .data = &omap_aes_pdata_omap4,
  868. },
  869. {},
  870. };
  871. MODULE_DEVICE_TABLE(of, omap_aes_of_match);
  872. static int omap_aes_get_res_of(struct omap_aes_dev *dd,
  873. struct device *dev, struct resource *res)
  874. {
  875. struct device_node *node = dev->of_node;
  876. const struct of_device_id *match;
  877. int err = 0;
  878. match = of_match_device(of_match_ptr(omap_aes_of_match), dev);
  879. if (!match) {
  880. dev_err(dev, "no compatible OF match\n");
  881. err = -EINVAL;
  882. goto err;
  883. }
  884. err = of_address_to_resource(node, 0, res);
  885. if (err < 0) {
  886. dev_err(dev, "can't translate OF node address\n");
  887. err = -EINVAL;
  888. goto err;
  889. }
  890. dd->pdata = match->data;
  891. err:
  892. return err;
  893. }
  894. #else
  895. static const struct of_device_id omap_aes_of_match[] = {
  896. {},
  897. };
  898. static int omap_aes_get_res_of(struct omap_aes_dev *dd,
  899. struct device *dev, struct resource *res)
  900. {
  901. return -EINVAL;
  902. }
  903. #endif
  904. static int omap_aes_get_res_pdev(struct omap_aes_dev *dd,
  905. struct platform_device *pdev, struct resource *res)
  906. {
  907. struct device *dev = &pdev->dev;
  908. struct resource *r;
  909. int err = 0;
  910. /* Get the base address */
  911. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  912. if (!r) {
  913. dev_err(dev, "no MEM resource info\n");
  914. err = -ENODEV;
  915. goto err;
  916. }
  917. memcpy(res, r, sizeof(*res));
  918. /* Only OMAP2/3 can be non-DT */
  919. dd->pdata = &omap_aes_pdata_omap2;
  920. err:
  921. return err;
  922. }
  923. static int omap_aes_probe(struct platform_device *pdev)
  924. {
  925. struct device *dev = &pdev->dev;
  926. struct omap_aes_dev *dd;
  927. struct crypto_alg *algp;
  928. struct resource res;
  929. int err = -ENOMEM, i, j, irq = -1;
  930. u32 reg;
  931. dd = devm_kzalloc(dev, sizeof(struct omap_aes_dev), GFP_KERNEL);
  932. if (dd == NULL) {
  933. dev_err(dev, "unable to alloc data struct.\n");
  934. goto err_data;
  935. }
  936. dd->dev = dev;
  937. platform_set_drvdata(pdev, dd);
  938. err = (dev->of_node) ? omap_aes_get_res_of(dd, dev, &res) :
  939. omap_aes_get_res_pdev(dd, pdev, &res);
  940. if (err)
  941. goto err_res;
  942. dd->io_base = devm_ioremap_resource(dev, &res);
  943. if (IS_ERR(dd->io_base)) {
  944. err = PTR_ERR(dd->io_base);
  945. goto err_res;
  946. }
  947. dd->phys_base = res.start;
  948. pm_runtime_use_autosuspend(dev);
  949. pm_runtime_set_autosuspend_delay(dev, DEFAULT_AUTOSUSPEND_DELAY);
  950. pm_runtime_enable(dev);
  951. err = pm_runtime_get_sync(dev);
  952. if (err < 0) {
  953. dev_err(dev, "%s: failed to get_sync(%d)\n",
  954. __func__, err);
  955. goto err_res;
  956. }
  957. omap_aes_dma_stop(dd);
  958. reg = omap_aes_read(dd, AES_REG_REV(dd));
  959. pm_runtime_put_sync(dev);
  960. dev_info(dev, "OMAP AES hw accel rev: %u.%u\n",
  961. (reg & dd->pdata->major_mask) >> dd->pdata->major_shift,
  962. (reg & dd->pdata->minor_mask) >> dd->pdata->minor_shift);
  963. tasklet_init(&dd->done_task, omap_aes_done_task, (unsigned long)dd);
  964. err = omap_aes_dma_init(dd);
  965. if (err == -EPROBE_DEFER) {
  966. goto err_irq;
  967. } else if (err && AES_REG_IRQ_STATUS(dd) && AES_REG_IRQ_ENABLE(dd)) {
  968. dd->pio_only = 1;
  969. irq = platform_get_irq(pdev, 0);
  970. if (irq < 0) {
  971. dev_err(dev, "can't get IRQ resource\n");
  972. goto err_irq;
  973. }
  974. err = devm_request_irq(dev, irq, omap_aes_irq, 0,
  975. dev_name(dev), dd);
  976. if (err) {
  977. dev_err(dev, "Unable to grab omap-aes IRQ\n");
  978. goto err_irq;
  979. }
  980. }
  981. INIT_LIST_HEAD(&dd->list);
  982. spin_lock(&list_lock);
  983. list_add_tail(&dd->list, &dev_list);
  984. spin_unlock(&list_lock);
  985. /* Initialize crypto engine */
  986. dd->engine = crypto_engine_alloc_init(dev, 1);
  987. if (!dd->engine) {
  988. err = -ENOMEM;
  989. goto err_engine;
  990. }
  991. dd->engine->prepare_cipher_request = omap_aes_prepare_req;
  992. dd->engine->cipher_one_request = omap_aes_crypt_req;
  993. err = crypto_engine_start(dd->engine);
  994. if (err)
  995. goto err_engine;
  996. for (i = 0; i < dd->pdata->algs_info_size; i++) {
  997. if (!dd->pdata->algs_info[i].registered) {
  998. for (j = 0; j < dd->pdata->algs_info[i].size; j++) {
  999. algp = &dd->pdata->algs_info[i].algs_list[j];
  1000. pr_debug("reg alg: %s\n", algp->cra_name);
  1001. INIT_LIST_HEAD(&algp->cra_list);
  1002. err = crypto_register_alg(algp);
  1003. if (err)
  1004. goto err_algs;
  1005. dd->pdata->algs_info[i].registered++;
  1006. }
  1007. }
  1008. }
  1009. return 0;
  1010. err_algs:
  1011. for (i = dd->pdata->algs_info_size - 1; i >= 0; i--)
  1012. for (j = dd->pdata->algs_info[i].registered - 1; j >= 0; j--)
  1013. crypto_unregister_alg(
  1014. &dd->pdata->algs_info[i].algs_list[j]);
  1015. err_engine:
  1016. if (dd->engine)
  1017. crypto_engine_exit(dd->engine);
  1018. omap_aes_dma_cleanup(dd);
  1019. err_irq:
  1020. tasklet_kill(&dd->done_task);
  1021. pm_runtime_disable(dev);
  1022. err_res:
  1023. dd = NULL;
  1024. err_data:
  1025. dev_err(dev, "initialization failed.\n");
  1026. return err;
  1027. }
  1028. static int omap_aes_remove(struct platform_device *pdev)
  1029. {
  1030. struct omap_aes_dev *dd = platform_get_drvdata(pdev);
  1031. int i, j;
  1032. if (!dd)
  1033. return -ENODEV;
  1034. spin_lock(&list_lock);
  1035. list_del(&dd->list);
  1036. spin_unlock(&list_lock);
  1037. for (i = dd->pdata->algs_info_size - 1; i >= 0; i--)
  1038. for (j = dd->pdata->algs_info[i].registered - 1; j >= 0; j--)
  1039. crypto_unregister_alg(
  1040. &dd->pdata->algs_info[i].algs_list[j]);
  1041. crypto_engine_exit(dd->engine);
  1042. tasklet_kill(&dd->done_task);
  1043. omap_aes_dma_cleanup(dd);
  1044. pm_runtime_disable(dd->dev);
  1045. dd = NULL;
  1046. return 0;
  1047. }
  1048. #ifdef CONFIG_PM_SLEEP
  1049. static int omap_aes_suspend(struct device *dev)
  1050. {
  1051. pm_runtime_put_sync(dev);
  1052. return 0;
  1053. }
  1054. static int omap_aes_resume(struct device *dev)
  1055. {
  1056. pm_runtime_get_sync(dev);
  1057. return 0;
  1058. }
  1059. #endif
  1060. static SIMPLE_DEV_PM_OPS(omap_aes_pm_ops, omap_aes_suspend, omap_aes_resume);
  1061. static struct platform_driver omap_aes_driver = {
  1062. .probe = omap_aes_probe,
  1063. .remove = omap_aes_remove,
  1064. .driver = {
  1065. .name = "omap-aes",
  1066. .pm = &omap_aes_pm_ops,
  1067. .of_match_table = omap_aes_of_match,
  1068. },
  1069. };
  1070. module_platform_driver(omap_aes_driver);
  1071. MODULE_DESCRIPTION("OMAP AES hw acceleration support.");
  1072. MODULE_LICENSE("GPL v2");
  1073. MODULE_AUTHOR("Dmitry Kasatkin");