omap-aes.c 31 KB

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