omap-aes.c 31 KB

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