omap-aes.c 32 KB

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