atmel-aes.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434
  1. /*
  2. * Cryptographic API.
  3. *
  4. * Support for ATMEL AES HW acceleration.
  5. *
  6. * Copyright (c) 2012 Eukréa Electromatique - ATMEL
  7. * Author: Nicolas Royer <nicolas@eukrea.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as published
  11. * by the Free Software Foundation.
  12. *
  13. * Some ideas are from omap-aes.c driver.
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/slab.h>
  18. #include <linux/err.h>
  19. #include <linux/clk.h>
  20. #include <linux/io.h>
  21. #include <linux/hw_random.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/device.h>
  24. #include <linux/init.h>
  25. #include <linux/errno.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/irq.h>
  28. #include <linux/scatterlist.h>
  29. #include <linux/dma-mapping.h>
  30. #include <linux/delay.h>
  31. #include <linux/crypto.h>
  32. #include <linux/cryptohash.h>
  33. #include <crypto/scatterwalk.h>
  34. #include <crypto/algapi.h>
  35. #include <crypto/aes.h>
  36. #include <crypto/hash.h>
  37. #include <crypto/internal/hash.h>
  38. #include <linux/platform_data/crypto-atmel.h>
  39. #include "atmel-aes-regs.h"
  40. #define CFB8_BLOCK_SIZE 1
  41. #define CFB16_BLOCK_SIZE 2
  42. #define CFB32_BLOCK_SIZE 4
  43. #define CFB64_BLOCK_SIZE 8
  44. /* AES flags */
  45. #define AES_FLAGS_MODE_MASK 0x03ff
  46. #define AES_FLAGS_ENCRYPT BIT(0)
  47. #define AES_FLAGS_CBC BIT(1)
  48. #define AES_FLAGS_CFB BIT(2)
  49. #define AES_FLAGS_CFB8 BIT(3)
  50. #define AES_FLAGS_CFB16 BIT(4)
  51. #define AES_FLAGS_CFB32 BIT(5)
  52. #define AES_FLAGS_CFB64 BIT(6)
  53. #define AES_FLAGS_CFB128 BIT(7)
  54. #define AES_FLAGS_OFB BIT(8)
  55. #define AES_FLAGS_CTR BIT(9)
  56. #define AES_FLAGS_INIT BIT(16)
  57. #define AES_FLAGS_DMA BIT(17)
  58. #define AES_FLAGS_BUSY BIT(18)
  59. #define AES_FLAGS_FAST BIT(19)
  60. #define ATMEL_AES_QUEUE_LENGTH 50
  61. #define ATMEL_AES_DMA_THRESHOLD 16
  62. struct atmel_aes_caps {
  63. bool has_dualbuff;
  64. bool has_cfb64;
  65. u32 max_burst_size;
  66. };
  67. struct atmel_aes_dev;
  68. struct atmel_aes_ctx {
  69. struct atmel_aes_dev *dd;
  70. int keylen;
  71. u32 key[AES_KEYSIZE_256 / sizeof(u32)];
  72. u16 block_size;
  73. };
  74. struct atmel_aes_reqctx {
  75. unsigned long mode;
  76. };
  77. struct atmel_aes_dma {
  78. struct dma_chan *chan;
  79. struct dma_slave_config dma_conf;
  80. };
  81. struct atmel_aes_dev {
  82. struct list_head list;
  83. unsigned long phys_base;
  84. void __iomem *io_base;
  85. struct atmel_aes_ctx *ctx;
  86. struct device *dev;
  87. struct clk *iclk;
  88. int irq;
  89. unsigned long flags;
  90. int err;
  91. spinlock_t lock;
  92. struct crypto_queue queue;
  93. struct tasklet_struct done_task;
  94. struct tasklet_struct queue_task;
  95. struct ablkcipher_request *req;
  96. size_t total;
  97. struct scatterlist *in_sg;
  98. unsigned int nb_in_sg;
  99. size_t in_offset;
  100. struct scatterlist *out_sg;
  101. unsigned int nb_out_sg;
  102. size_t out_offset;
  103. size_t bufcnt;
  104. size_t buflen;
  105. size_t dma_size;
  106. void *buf_in;
  107. int dma_in;
  108. dma_addr_t dma_addr_in;
  109. struct atmel_aes_dma dma_lch_in;
  110. void *buf_out;
  111. int dma_out;
  112. dma_addr_t dma_addr_out;
  113. struct atmel_aes_dma dma_lch_out;
  114. struct atmel_aes_caps caps;
  115. u32 hw_version;
  116. };
  117. struct atmel_aes_drv {
  118. struct list_head dev_list;
  119. spinlock_t lock;
  120. };
  121. static struct atmel_aes_drv atmel_aes = {
  122. .dev_list = LIST_HEAD_INIT(atmel_aes.dev_list),
  123. .lock = __SPIN_LOCK_UNLOCKED(atmel_aes.lock),
  124. };
  125. static int atmel_aes_sg_length(struct ablkcipher_request *req,
  126. struct scatterlist *sg)
  127. {
  128. unsigned int total = req->nbytes;
  129. int sg_nb;
  130. unsigned int len;
  131. struct scatterlist *sg_list;
  132. sg_nb = 0;
  133. sg_list = sg;
  134. total = req->nbytes;
  135. while (total) {
  136. len = min(sg_list->length, total);
  137. sg_nb++;
  138. total -= len;
  139. sg_list = sg_next(sg_list);
  140. if (!sg_list)
  141. total = 0;
  142. }
  143. return sg_nb;
  144. }
  145. static int atmel_aes_sg_copy(struct scatterlist **sg, size_t *offset,
  146. void *buf, size_t buflen, size_t total, int out)
  147. {
  148. unsigned int count, off = 0;
  149. while (buflen && total) {
  150. count = min((*sg)->length - *offset, total);
  151. count = min(count, buflen);
  152. if (!count)
  153. return off;
  154. scatterwalk_map_and_copy(buf + off, *sg, *offset, count, out);
  155. off += count;
  156. buflen -= count;
  157. *offset += count;
  158. total -= count;
  159. if (*offset == (*sg)->length) {
  160. *sg = sg_next(*sg);
  161. if (*sg)
  162. *offset = 0;
  163. else
  164. total = 0;
  165. }
  166. }
  167. return off;
  168. }
  169. static inline u32 atmel_aes_read(struct atmel_aes_dev *dd, u32 offset)
  170. {
  171. return readl_relaxed(dd->io_base + offset);
  172. }
  173. static inline void atmel_aes_write(struct atmel_aes_dev *dd,
  174. u32 offset, u32 value)
  175. {
  176. writel_relaxed(value, dd->io_base + offset);
  177. }
  178. static void atmel_aes_read_n(struct atmel_aes_dev *dd, u32 offset,
  179. u32 *value, int count)
  180. {
  181. for (; count--; value++, offset += 4)
  182. *value = atmel_aes_read(dd, offset);
  183. }
  184. static void atmel_aes_write_n(struct atmel_aes_dev *dd, u32 offset,
  185. u32 *value, int count)
  186. {
  187. for (; count--; value++, offset += 4)
  188. atmel_aes_write(dd, offset, *value);
  189. }
  190. static struct atmel_aes_dev *atmel_aes_find_dev(struct atmel_aes_ctx *ctx)
  191. {
  192. struct atmel_aes_dev *aes_dd = NULL;
  193. struct atmel_aes_dev *tmp;
  194. spin_lock_bh(&atmel_aes.lock);
  195. if (!ctx->dd) {
  196. list_for_each_entry(tmp, &atmel_aes.dev_list, list) {
  197. aes_dd = tmp;
  198. break;
  199. }
  200. ctx->dd = aes_dd;
  201. } else {
  202. aes_dd = ctx->dd;
  203. }
  204. spin_unlock_bh(&atmel_aes.lock);
  205. return aes_dd;
  206. }
  207. static int atmel_aes_hw_init(struct atmel_aes_dev *dd)
  208. {
  209. clk_prepare_enable(dd->iclk);
  210. if (!(dd->flags & AES_FLAGS_INIT)) {
  211. atmel_aes_write(dd, AES_CR, AES_CR_SWRST);
  212. atmel_aes_write(dd, AES_MR, 0xE << AES_MR_CKEY_OFFSET);
  213. dd->flags |= AES_FLAGS_INIT;
  214. dd->err = 0;
  215. }
  216. return 0;
  217. }
  218. static inline unsigned int atmel_aes_get_version(struct atmel_aes_dev *dd)
  219. {
  220. return atmel_aes_read(dd, AES_HW_VERSION) & 0x00000fff;
  221. }
  222. static void atmel_aes_hw_version_init(struct atmel_aes_dev *dd)
  223. {
  224. atmel_aes_hw_init(dd);
  225. dd->hw_version = atmel_aes_get_version(dd);
  226. dev_info(dd->dev,
  227. "version: 0x%x\n", dd->hw_version);
  228. clk_disable_unprepare(dd->iclk);
  229. }
  230. static void atmel_aes_finish_req(struct atmel_aes_dev *dd, int err)
  231. {
  232. struct ablkcipher_request *req = dd->req;
  233. clk_disable_unprepare(dd->iclk);
  234. dd->flags &= ~AES_FLAGS_BUSY;
  235. req->base.complete(&req->base, err);
  236. }
  237. static void atmel_aes_dma_callback(void *data)
  238. {
  239. struct atmel_aes_dev *dd = data;
  240. /* dma_lch_out - completed */
  241. tasklet_schedule(&dd->done_task);
  242. }
  243. static int atmel_aes_crypt_dma(struct atmel_aes_dev *dd,
  244. dma_addr_t dma_addr_in, dma_addr_t dma_addr_out, int length)
  245. {
  246. struct scatterlist sg[2];
  247. struct dma_async_tx_descriptor *in_desc, *out_desc;
  248. dd->dma_size = length;
  249. if (!(dd->flags & AES_FLAGS_FAST)) {
  250. dma_sync_single_for_device(dd->dev, dma_addr_in, length,
  251. DMA_TO_DEVICE);
  252. }
  253. if (dd->flags & AES_FLAGS_CFB8) {
  254. dd->dma_lch_in.dma_conf.dst_addr_width =
  255. DMA_SLAVE_BUSWIDTH_1_BYTE;
  256. dd->dma_lch_out.dma_conf.src_addr_width =
  257. DMA_SLAVE_BUSWIDTH_1_BYTE;
  258. } else if (dd->flags & AES_FLAGS_CFB16) {
  259. dd->dma_lch_in.dma_conf.dst_addr_width =
  260. DMA_SLAVE_BUSWIDTH_2_BYTES;
  261. dd->dma_lch_out.dma_conf.src_addr_width =
  262. DMA_SLAVE_BUSWIDTH_2_BYTES;
  263. } else {
  264. dd->dma_lch_in.dma_conf.dst_addr_width =
  265. DMA_SLAVE_BUSWIDTH_4_BYTES;
  266. dd->dma_lch_out.dma_conf.src_addr_width =
  267. DMA_SLAVE_BUSWIDTH_4_BYTES;
  268. }
  269. if (dd->flags & (AES_FLAGS_CFB8 | AES_FLAGS_CFB16 |
  270. AES_FLAGS_CFB32 | AES_FLAGS_CFB64)) {
  271. dd->dma_lch_in.dma_conf.src_maxburst = 1;
  272. dd->dma_lch_in.dma_conf.dst_maxburst = 1;
  273. dd->dma_lch_out.dma_conf.src_maxburst = 1;
  274. dd->dma_lch_out.dma_conf.dst_maxburst = 1;
  275. } else {
  276. dd->dma_lch_in.dma_conf.src_maxburst = dd->caps.max_burst_size;
  277. dd->dma_lch_in.dma_conf.dst_maxburst = dd->caps.max_burst_size;
  278. dd->dma_lch_out.dma_conf.src_maxburst = dd->caps.max_burst_size;
  279. dd->dma_lch_out.dma_conf.dst_maxburst = dd->caps.max_burst_size;
  280. }
  281. dmaengine_slave_config(dd->dma_lch_in.chan, &dd->dma_lch_in.dma_conf);
  282. dmaengine_slave_config(dd->dma_lch_out.chan, &dd->dma_lch_out.dma_conf);
  283. dd->flags |= AES_FLAGS_DMA;
  284. sg_init_table(&sg[0], 1);
  285. sg_dma_address(&sg[0]) = dma_addr_in;
  286. sg_dma_len(&sg[0]) = length;
  287. sg_init_table(&sg[1], 1);
  288. sg_dma_address(&sg[1]) = dma_addr_out;
  289. sg_dma_len(&sg[1]) = length;
  290. in_desc = dmaengine_prep_slave_sg(dd->dma_lch_in.chan, &sg[0],
  291. 1, DMA_MEM_TO_DEV,
  292. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  293. if (!in_desc)
  294. return -EINVAL;
  295. out_desc = dmaengine_prep_slave_sg(dd->dma_lch_out.chan, &sg[1],
  296. 1, DMA_DEV_TO_MEM,
  297. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  298. if (!out_desc)
  299. return -EINVAL;
  300. out_desc->callback = atmel_aes_dma_callback;
  301. out_desc->callback_param = dd;
  302. dmaengine_submit(out_desc);
  303. dma_async_issue_pending(dd->dma_lch_out.chan);
  304. dmaengine_submit(in_desc);
  305. dma_async_issue_pending(dd->dma_lch_in.chan);
  306. return 0;
  307. }
  308. static int atmel_aes_crypt_cpu_start(struct atmel_aes_dev *dd)
  309. {
  310. dd->flags &= ~AES_FLAGS_DMA;
  311. /* use cache buffers */
  312. dd->nb_in_sg = atmel_aes_sg_length(dd->req, dd->in_sg);
  313. if (!dd->nb_in_sg)
  314. return -EINVAL;
  315. dd->nb_out_sg = atmel_aes_sg_length(dd->req, dd->out_sg);
  316. if (!dd->nb_out_sg)
  317. return -EINVAL;
  318. dd->bufcnt = sg_copy_to_buffer(dd->in_sg, dd->nb_in_sg,
  319. dd->buf_in, dd->total);
  320. if (!dd->bufcnt)
  321. return -EINVAL;
  322. dd->total -= dd->bufcnt;
  323. atmel_aes_write(dd, AES_IER, AES_INT_DATARDY);
  324. atmel_aes_write_n(dd, AES_IDATAR(0), (u32 *) dd->buf_in,
  325. dd->bufcnt >> 2);
  326. return 0;
  327. }
  328. static int atmel_aes_crypt_dma_start(struct atmel_aes_dev *dd)
  329. {
  330. int err, fast = 0, in, out;
  331. size_t count;
  332. dma_addr_t addr_in, addr_out;
  333. if ((!dd->in_offset) && (!dd->out_offset)) {
  334. /* check for alignment */
  335. in = IS_ALIGNED((u32)dd->in_sg->offset, sizeof(u32)) &&
  336. IS_ALIGNED(dd->in_sg->length, dd->ctx->block_size);
  337. out = IS_ALIGNED((u32)dd->out_sg->offset, sizeof(u32)) &&
  338. IS_ALIGNED(dd->out_sg->length, dd->ctx->block_size);
  339. fast = in && out;
  340. if (sg_dma_len(dd->in_sg) != sg_dma_len(dd->out_sg))
  341. fast = 0;
  342. }
  343. if (fast) {
  344. count = min(dd->total, sg_dma_len(dd->in_sg));
  345. count = min(count, sg_dma_len(dd->out_sg));
  346. err = dma_map_sg(dd->dev, dd->in_sg, 1, DMA_TO_DEVICE);
  347. if (!err) {
  348. dev_err(dd->dev, "dma_map_sg() error\n");
  349. return -EINVAL;
  350. }
  351. err = dma_map_sg(dd->dev, dd->out_sg, 1,
  352. DMA_FROM_DEVICE);
  353. if (!err) {
  354. dev_err(dd->dev, "dma_map_sg() error\n");
  355. dma_unmap_sg(dd->dev, dd->in_sg, 1,
  356. DMA_TO_DEVICE);
  357. return -EINVAL;
  358. }
  359. addr_in = sg_dma_address(dd->in_sg);
  360. addr_out = sg_dma_address(dd->out_sg);
  361. dd->flags |= AES_FLAGS_FAST;
  362. } else {
  363. /* use cache buffers */
  364. count = atmel_aes_sg_copy(&dd->in_sg, &dd->in_offset,
  365. dd->buf_in, dd->buflen, dd->total, 0);
  366. addr_in = dd->dma_addr_in;
  367. addr_out = dd->dma_addr_out;
  368. dd->flags &= ~AES_FLAGS_FAST;
  369. }
  370. dd->total -= count;
  371. err = atmel_aes_crypt_dma(dd, addr_in, addr_out, count);
  372. if (err && (dd->flags & AES_FLAGS_FAST)) {
  373. dma_unmap_sg(dd->dev, dd->in_sg, 1, DMA_TO_DEVICE);
  374. dma_unmap_sg(dd->dev, dd->out_sg, 1, DMA_TO_DEVICE);
  375. }
  376. return err;
  377. }
  378. static int atmel_aes_write_ctrl(struct atmel_aes_dev *dd)
  379. {
  380. int err;
  381. u32 valcr = 0, valmr = 0;
  382. err = atmel_aes_hw_init(dd);
  383. if (err)
  384. return err;
  385. /* MR register must be set before IV registers */
  386. if (dd->ctx->keylen == AES_KEYSIZE_128)
  387. valmr |= AES_MR_KEYSIZE_128;
  388. else if (dd->ctx->keylen == AES_KEYSIZE_192)
  389. valmr |= AES_MR_KEYSIZE_192;
  390. else
  391. valmr |= AES_MR_KEYSIZE_256;
  392. if (dd->flags & AES_FLAGS_CBC) {
  393. valmr |= AES_MR_OPMOD_CBC;
  394. } else if (dd->flags & AES_FLAGS_CFB) {
  395. valmr |= AES_MR_OPMOD_CFB;
  396. if (dd->flags & AES_FLAGS_CFB8)
  397. valmr |= AES_MR_CFBS_8b;
  398. else if (dd->flags & AES_FLAGS_CFB16)
  399. valmr |= AES_MR_CFBS_16b;
  400. else if (dd->flags & AES_FLAGS_CFB32)
  401. valmr |= AES_MR_CFBS_32b;
  402. else if (dd->flags & AES_FLAGS_CFB64)
  403. valmr |= AES_MR_CFBS_64b;
  404. else if (dd->flags & AES_FLAGS_CFB128)
  405. valmr |= AES_MR_CFBS_128b;
  406. } else if (dd->flags & AES_FLAGS_OFB) {
  407. valmr |= AES_MR_OPMOD_OFB;
  408. } else if (dd->flags & AES_FLAGS_CTR) {
  409. valmr |= AES_MR_OPMOD_CTR;
  410. } else {
  411. valmr |= AES_MR_OPMOD_ECB;
  412. }
  413. if (dd->flags & AES_FLAGS_ENCRYPT)
  414. valmr |= AES_MR_CYPHER_ENC;
  415. if (dd->total > ATMEL_AES_DMA_THRESHOLD) {
  416. valmr |= AES_MR_SMOD_IDATAR0;
  417. if (dd->caps.has_dualbuff)
  418. valmr |= AES_MR_DUALBUFF;
  419. } else {
  420. valmr |= AES_MR_SMOD_AUTO;
  421. }
  422. atmel_aes_write(dd, AES_CR, valcr);
  423. atmel_aes_write(dd, AES_MR, valmr);
  424. atmel_aes_write_n(dd, AES_KEYWR(0), dd->ctx->key,
  425. dd->ctx->keylen >> 2);
  426. if (((dd->flags & AES_FLAGS_CBC) || (dd->flags & AES_FLAGS_CFB) ||
  427. (dd->flags & AES_FLAGS_OFB) || (dd->flags & AES_FLAGS_CTR)) &&
  428. dd->req->info) {
  429. atmel_aes_write_n(dd, AES_IVR(0), dd->req->info, 4);
  430. }
  431. return 0;
  432. }
  433. static int atmel_aes_handle_queue(struct atmel_aes_dev *dd,
  434. struct ablkcipher_request *req)
  435. {
  436. struct crypto_async_request *async_req, *backlog;
  437. struct atmel_aes_ctx *ctx;
  438. struct atmel_aes_reqctx *rctx;
  439. unsigned long flags;
  440. int err, ret = 0;
  441. spin_lock_irqsave(&dd->lock, flags);
  442. if (req)
  443. ret = ablkcipher_enqueue_request(&dd->queue, req);
  444. if (dd->flags & AES_FLAGS_BUSY) {
  445. spin_unlock_irqrestore(&dd->lock, flags);
  446. return ret;
  447. }
  448. backlog = crypto_get_backlog(&dd->queue);
  449. async_req = crypto_dequeue_request(&dd->queue);
  450. if (async_req)
  451. dd->flags |= AES_FLAGS_BUSY;
  452. spin_unlock_irqrestore(&dd->lock, flags);
  453. if (!async_req)
  454. return ret;
  455. if (backlog)
  456. backlog->complete(backlog, -EINPROGRESS);
  457. req = ablkcipher_request_cast(async_req);
  458. /* assign new request to device */
  459. dd->req = req;
  460. dd->total = req->nbytes;
  461. dd->in_offset = 0;
  462. dd->in_sg = req->src;
  463. dd->out_offset = 0;
  464. dd->out_sg = req->dst;
  465. rctx = ablkcipher_request_ctx(req);
  466. ctx = crypto_ablkcipher_ctx(crypto_ablkcipher_reqtfm(req));
  467. rctx->mode &= AES_FLAGS_MODE_MASK;
  468. dd->flags = (dd->flags & ~AES_FLAGS_MODE_MASK) | rctx->mode;
  469. dd->ctx = ctx;
  470. ctx->dd = dd;
  471. err = atmel_aes_write_ctrl(dd);
  472. if (!err) {
  473. if (dd->total > ATMEL_AES_DMA_THRESHOLD)
  474. err = atmel_aes_crypt_dma_start(dd);
  475. else
  476. err = atmel_aes_crypt_cpu_start(dd);
  477. }
  478. if (err) {
  479. /* aes_task will not finish it, so do it here */
  480. atmel_aes_finish_req(dd, err);
  481. tasklet_schedule(&dd->queue_task);
  482. }
  483. return ret;
  484. }
  485. static int atmel_aes_crypt_dma_stop(struct atmel_aes_dev *dd)
  486. {
  487. int err = -EINVAL;
  488. size_t count;
  489. if (dd->flags & AES_FLAGS_DMA) {
  490. err = 0;
  491. if (dd->flags & AES_FLAGS_FAST) {
  492. dma_unmap_sg(dd->dev, dd->out_sg, 1, DMA_FROM_DEVICE);
  493. dma_unmap_sg(dd->dev, dd->in_sg, 1, DMA_TO_DEVICE);
  494. } else {
  495. dma_sync_single_for_device(dd->dev, dd->dma_addr_out,
  496. dd->dma_size, DMA_FROM_DEVICE);
  497. /* copy data */
  498. count = atmel_aes_sg_copy(&dd->out_sg, &dd->out_offset,
  499. dd->buf_out, dd->buflen, dd->dma_size, 1);
  500. if (count != dd->dma_size) {
  501. err = -EINVAL;
  502. pr_err("not all data converted: %u\n", count);
  503. }
  504. }
  505. }
  506. return err;
  507. }
  508. static int atmel_aes_buff_init(struct atmel_aes_dev *dd)
  509. {
  510. int err = -ENOMEM;
  511. dd->buf_in = (void *)__get_free_pages(GFP_KERNEL, 0);
  512. dd->buf_out = (void *)__get_free_pages(GFP_KERNEL, 0);
  513. dd->buflen = PAGE_SIZE;
  514. dd->buflen &= ~(AES_BLOCK_SIZE - 1);
  515. if (!dd->buf_in || !dd->buf_out) {
  516. dev_err(dd->dev, "unable to alloc pages.\n");
  517. goto err_alloc;
  518. }
  519. /* MAP here */
  520. dd->dma_addr_in = dma_map_single(dd->dev, dd->buf_in,
  521. dd->buflen, DMA_TO_DEVICE);
  522. if (dma_mapping_error(dd->dev, dd->dma_addr_in)) {
  523. dev_err(dd->dev, "dma %d bytes error\n", dd->buflen);
  524. err = -EINVAL;
  525. goto err_map_in;
  526. }
  527. dd->dma_addr_out = dma_map_single(dd->dev, dd->buf_out,
  528. dd->buflen, DMA_FROM_DEVICE);
  529. if (dma_mapping_error(dd->dev, dd->dma_addr_out)) {
  530. dev_err(dd->dev, "dma %d bytes error\n", dd->buflen);
  531. err = -EINVAL;
  532. goto err_map_out;
  533. }
  534. return 0;
  535. err_map_out:
  536. dma_unmap_single(dd->dev, dd->dma_addr_in, dd->buflen,
  537. DMA_TO_DEVICE);
  538. err_map_in:
  539. free_page((unsigned long)dd->buf_out);
  540. free_page((unsigned long)dd->buf_in);
  541. err_alloc:
  542. if (err)
  543. pr_err("error: %d\n", err);
  544. return err;
  545. }
  546. static void atmel_aes_buff_cleanup(struct atmel_aes_dev *dd)
  547. {
  548. dma_unmap_single(dd->dev, dd->dma_addr_out, dd->buflen,
  549. DMA_FROM_DEVICE);
  550. dma_unmap_single(dd->dev, dd->dma_addr_in, dd->buflen,
  551. DMA_TO_DEVICE);
  552. free_page((unsigned long)dd->buf_out);
  553. free_page((unsigned long)dd->buf_in);
  554. }
  555. static int atmel_aes_crypt(struct ablkcipher_request *req, unsigned long mode)
  556. {
  557. struct atmel_aes_ctx *ctx = crypto_ablkcipher_ctx(
  558. crypto_ablkcipher_reqtfm(req));
  559. struct atmel_aes_reqctx *rctx = ablkcipher_request_ctx(req);
  560. struct atmel_aes_dev *dd;
  561. if (mode & AES_FLAGS_CFB8) {
  562. if (!IS_ALIGNED(req->nbytes, CFB8_BLOCK_SIZE)) {
  563. pr_err("request size is not exact amount of CFB8 blocks\n");
  564. return -EINVAL;
  565. }
  566. ctx->block_size = CFB8_BLOCK_SIZE;
  567. } else if (mode & AES_FLAGS_CFB16) {
  568. if (!IS_ALIGNED(req->nbytes, CFB16_BLOCK_SIZE)) {
  569. pr_err("request size is not exact amount of CFB16 blocks\n");
  570. return -EINVAL;
  571. }
  572. ctx->block_size = CFB16_BLOCK_SIZE;
  573. } else if (mode & AES_FLAGS_CFB32) {
  574. if (!IS_ALIGNED(req->nbytes, CFB32_BLOCK_SIZE)) {
  575. pr_err("request size is not exact amount of CFB32 blocks\n");
  576. return -EINVAL;
  577. }
  578. ctx->block_size = CFB32_BLOCK_SIZE;
  579. } else {
  580. if (!IS_ALIGNED(req->nbytes, AES_BLOCK_SIZE)) {
  581. pr_err("request size is not exact amount of AES blocks\n");
  582. return -EINVAL;
  583. }
  584. ctx->block_size = AES_BLOCK_SIZE;
  585. }
  586. dd = atmel_aes_find_dev(ctx);
  587. if (!dd)
  588. return -ENODEV;
  589. rctx->mode = mode;
  590. return atmel_aes_handle_queue(dd, req);
  591. }
  592. static bool atmel_aes_filter(struct dma_chan *chan, void *slave)
  593. {
  594. struct at_dma_slave *sl = slave;
  595. if (sl && sl->dma_dev == chan->device->dev) {
  596. chan->private = sl;
  597. return true;
  598. } else {
  599. return false;
  600. }
  601. }
  602. static int atmel_aes_dma_init(struct atmel_aes_dev *dd,
  603. struct crypto_platform_data *pdata)
  604. {
  605. int err = -ENOMEM;
  606. dma_cap_mask_t mask_in, mask_out;
  607. if (pdata && pdata->dma_slave->txdata.dma_dev &&
  608. pdata->dma_slave->rxdata.dma_dev) {
  609. /* Try to grab 2 DMA channels */
  610. dma_cap_zero(mask_in);
  611. dma_cap_set(DMA_SLAVE, mask_in);
  612. dd->dma_lch_in.chan = dma_request_channel(mask_in,
  613. atmel_aes_filter, &pdata->dma_slave->rxdata);
  614. if (!dd->dma_lch_in.chan)
  615. goto err_dma_in;
  616. dd->dma_lch_in.dma_conf.direction = DMA_MEM_TO_DEV;
  617. dd->dma_lch_in.dma_conf.dst_addr = dd->phys_base +
  618. AES_IDATAR(0);
  619. dd->dma_lch_in.dma_conf.src_maxburst = dd->caps.max_burst_size;
  620. dd->dma_lch_in.dma_conf.src_addr_width =
  621. DMA_SLAVE_BUSWIDTH_4_BYTES;
  622. dd->dma_lch_in.dma_conf.dst_maxburst = dd->caps.max_burst_size;
  623. dd->dma_lch_in.dma_conf.dst_addr_width =
  624. DMA_SLAVE_BUSWIDTH_4_BYTES;
  625. dd->dma_lch_in.dma_conf.device_fc = false;
  626. dma_cap_zero(mask_out);
  627. dma_cap_set(DMA_SLAVE, mask_out);
  628. dd->dma_lch_out.chan = dma_request_channel(mask_out,
  629. atmel_aes_filter, &pdata->dma_slave->txdata);
  630. if (!dd->dma_lch_out.chan)
  631. goto err_dma_out;
  632. dd->dma_lch_out.dma_conf.direction = DMA_DEV_TO_MEM;
  633. dd->dma_lch_out.dma_conf.src_addr = dd->phys_base +
  634. AES_ODATAR(0);
  635. dd->dma_lch_out.dma_conf.src_maxburst = dd->caps.max_burst_size;
  636. dd->dma_lch_out.dma_conf.src_addr_width =
  637. DMA_SLAVE_BUSWIDTH_4_BYTES;
  638. dd->dma_lch_out.dma_conf.dst_maxburst = dd->caps.max_burst_size;
  639. dd->dma_lch_out.dma_conf.dst_addr_width =
  640. DMA_SLAVE_BUSWIDTH_4_BYTES;
  641. dd->dma_lch_out.dma_conf.device_fc = false;
  642. return 0;
  643. } else {
  644. return -ENODEV;
  645. }
  646. err_dma_out:
  647. dma_release_channel(dd->dma_lch_in.chan);
  648. err_dma_in:
  649. return err;
  650. }
  651. static void atmel_aes_dma_cleanup(struct atmel_aes_dev *dd)
  652. {
  653. dma_release_channel(dd->dma_lch_in.chan);
  654. dma_release_channel(dd->dma_lch_out.chan);
  655. }
  656. static int atmel_aes_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
  657. unsigned int keylen)
  658. {
  659. struct atmel_aes_ctx *ctx = crypto_ablkcipher_ctx(tfm);
  660. if (keylen != AES_KEYSIZE_128 && keylen != AES_KEYSIZE_192 &&
  661. keylen != AES_KEYSIZE_256) {
  662. crypto_ablkcipher_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
  663. return -EINVAL;
  664. }
  665. memcpy(ctx->key, key, keylen);
  666. ctx->keylen = keylen;
  667. return 0;
  668. }
  669. static int atmel_aes_ecb_encrypt(struct ablkcipher_request *req)
  670. {
  671. return atmel_aes_crypt(req,
  672. AES_FLAGS_ENCRYPT);
  673. }
  674. static int atmel_aes_ecb_decrypt(struct ablkcipher_request *req)
  675. {
  676. return atmel_aes_crypt(req,
  677. 0);
  678. }
  679. static int atmel_aes_cbc_encrypt(struct ablkcipher_request *req)
  680. {
  681. return atmel_aes_crypt(req,
  682. AES_FLAGS_ENCRYPT | AES_FLAGS_CBC);
  683. }
  684. static int atmel_aes_cbc_decrypt(struct ablkcipher_request *req)
  685. {
  686. return atmel_aes_crypt(req,
  687. AES_FLAGS_CBC);
  688. }
  689. static int atmel_aes_ofb_encrypt(struct ablkcipher_request *req)
  690. {
  691. return atmel_aes_crypt(req,
  692. AES_FLAGS_ENCRYPT | AES_FLAGS_OFB);
  693. }
  694. static int atmel_aes_ofb_decrypt(struct ablkcipher_request *req)
  695. {
  696. return atmel_aes_crypt(req,
  697. AES_FLAGS_OFB);
  698. }
  699. static int atmel_aes_cfb_encrypt(struct ablkcipher_request *req)
  700. {
  701. return atmel_aes_crypt(req,
  702. AES_FLAGS_ENCRYPT | AES_FLAGS_CFB | AES_FLAGS_CFB128);
  703. }
  704. static int atmel_aes_cfb_decrypt(struct ablkcipher_request *req)
  705. {
  706. return atmel_aes_crypt(req,
  707. AES_FLAGS_CFB | AES_FLAGS_CFB128);
  708. }
  709. static int atmel_aes_cfb64_encrypt(struct ablkcipher_request *req)
  710. {
  711. return atmel_aes_crypt(req,
  712. AES_FLAGS_ENCRYPT | AES_FLAGS_CFB | AES_FLAGS_CFB64);
  713. }
  714. static int atmel_aes_cfb64_decrypt(struct ablkcipher_request *req)
  715. {
  716. return atmel_aes_crypt(req,
  717. AES_FLAGS_CFB | AES_FLAGS_CFB64);
  718. }
  719. static int atmel_aes_cfb32_encrypt(struct ablkcipher_request *req)
  720. {
  721. return atmel_aes_crypt(req,
  722. AES_FLAGS_ENCRYPT | AES_FLAGS_CFB | AES_FLAGS_CFB32);
  723. }
  724. static int atmel_aes_cfb32_decrypt(struct ablkcipher_request *req)
  725. {
  726. return atmel_aes_crypt(req,
  727. AES_FLAGS_CFB | AES_FLAGS_CFB32);
  728. }
  729. static int atmel_aes_cfb16_encrypt(struct ablkcipher_request *req)
  730. {
  731. return atmel_aes_crypt(req,
  732. AES_FLAGS_ENCRYPT | AES_FLAGS_CFB | AES_FLAGS_CFB16);
  733. }
  734. static int atmel_aes_cfb16_decrypt(struct ablkcipher_request *req)
  735. {
  736. return atmel_aes_crypt(req,
  737. AES_FLAGS_CFB | AES_FLAGS_CFB16);
  738. }
  739. static int atmel_aes_cfb8_encrypt(struct ablkcipher_request *req)
  740. {
  741. return atmel_aes_crypt(req,
  742. AES_FLAGS_ENCRYPT | AES_FLAGS_CFB | AES_FLAGS_CFB8);
  743. }
  744. static int atmel_aes_cfb8_decrypt(struct ablkcipher_request *req)
  745. {
  746. return atmel_aes_crypt(req,
  747. AES_FLAGS_CFB | AES_FLAGS_CFB8);
  748. }
  749. static int atmel_aes_ctr_encrypt(struct ablkcipher_request *req)
  750. {
  751. return atmel_aes_crypt(req,
  752. AES_FLAGS_ENCRYPT | AES_FLAGS_CTR);
  753. }
  754. static int atmel_aes_ctr_decrypt(struct ablkcipher_request *req)
  755. {
  756. return atmel_aes_crypt(req,
  757. AES_FLAGS_CTR);
  758. }
  759. static int atmel_aes_cra_init(struct crypto_tfm *tfm)
  760. {
  761. tfm->crt_ablkcipher.reqsize = sizeof(struct atmel_aes_reqctx);
  762. return 0;
  763. }
  764. static void atmel_aes_cra_exit(struct crypto_tfm *tfm)
  765. {
  766. }
  767. static struct crypto_alg aes_algs[] = {
  768. {
  769. .cra_name = "ecb(aes)",
  770. .cra_driver_name = "atmel-ecb-aes",
  771. .cra_priority = 100,
  772. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  773. .cra_blocksize = AES_BLOCK_SIZE,
  774. .cra_ctxsize = sizeof(struct atmel_aes_ctx),
  775. .cra_alignmask = 0xf,
  776. .cra_type = &crypto_ablkcipher_type,
  777. .cra_module = THIS_MODULE,
  778. .cra_init = atmel_aes_cra_init,
  779. .cra_exit = atmel_aes_cra_exit,
  780. .cra_u.ablkcipher = {
  781. .min_keysize = AES_MIN_KEY_SIZE,
  782. .max_keysize = AES_MAX_KEY_SIZE,
  783. .setkey = atmel_aes_setkey,
  784. .encrypt = atmel_aes_ecb_encrypt,
  785. .decrypt = atmel_aes_ecb_decrypt,
  786. }
  787. },
  788. {
  789. .cra_name = "cbc(aes)",
  790. .cra_driver_name = "atmel-cbc-aes",
  791. .cra_priority = 100,
  792. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  793. .cra_blocksize = AES_BLOCK_SIZE,
  794. .cra_ctxsize = sizeof(struct atmel_aes_ctx),
  795. .cra_alignmask = 0xf,
  796. .cra_type = &crypto_ablkcipher_type,
  797. .cra_module = THIS_MODULE,
  798. .cra_init = atmel_aes_cra_init,
  799. .cra_exit = atmel_aes_cra_exit,
  800. .cra_u.ablkcipher = {
  801. .min_keysize = AES_MIN_KEY_SIZE,
  802. .max_keysize = AES_MAX_KEY_SIZE,
  803. .ivsize = AES_BLOCK_SIZE,
  804. .setkey = atmel_aes_setkey,
  805. .encrypt = atmel_aes_cbc_encrypt,
  806. .decrypt = atmel_aes_cbc_decrypt,
  807. }
  808. },
  809. {
  810. .cra_name = "ofb(aes)",
  811. .cra_driver_name = "atmel-ofb-aes",
  812. .cra_priority = 100,
  813. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  814. .cra_blocksize = AES_BLOCK_SIZE,
  815. .cra_ctxsize = sizeof(struct atmel_aes_ctx),
  816. .cra_alignmask = 0xf,
  817. .cra_type = &crypto_ablkcipher_type,
  818. .cra_module = THIS_MODULE,
  819. .cra_init = atmel_aes_cra_init,
  820. .cra_exit = atmel_aes_cra_exit,
  821. .cra_u.ablkcipher = {
  822. .min_keysize = AES_MIN_KEY_SIZE,
  823. .max_keysize = AES_MAX_KEY_SIZE,
  824. .ivsize = AES_BLOCK_SIZE,
  825. .setkey = atmel_aes_setkey,
  826. .encrypt = atmel_aes_ofb_encrypt,
  827. .decrypt = atmel_aes_ofb_decrypt,
  828. }
  829. },
  830. {
  831. .cra_name = "cfb(aes)",
  832. .cra_driver_name = "atmel-cfb-aes",
  833. .cra_priority = 100,
  834. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  835. .cra_blocksize = AES_BLOCK_SIZE,
  836. .cra_ctxsize = sizeof(struct atmel_aes_ctx),
  837. .cra_alignmask = 0xf,
  838. .cra_type = &crypto_ablkcipher_type,
  839. .cra_module = THIS_MODULE,
  840. .cra_init = atmel_aes_cra_init,
  841. .cra_exit = atmel_aes_cra_exit,
  842. .cra_u.ablkcipher = {
  843. .min_keysize = AES_MIN_KEY_SIZE,
  844. .max_keysize = AES_MAX_KEY_SIZE,
  845. .ivsize = AES_BLOCK_SIZE,
  846. .setkey = atmel_aes_setkey,
  847. .encrypt = atmel_aes_cfb_encrypt,
  848. .decrypt = atmel_aes_cfb_decrypt,
  849. }
  850. },
  851. {
  852. .cra_name = "cfb32(aes)",
  853. .cra_driver_name = "atmel-cfb32-aes",
  854. .cra_priority = 100,
  855. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  856. .cra_blocksize = CFB32_BLOCK_SIZE,
  857. .cra_ctxsize = sizeof(struct atmel_aes_ctx),
  858. .cra_alignmask = 0x3,
  859. .cra_type = &crypto_ablkcipher_type,
  860. .cra_module = THIS_MODULE,
  861. .cra_init = atmel_aes_cra_init,
  862. .cra_exit = atmel_aes_cra_exit,
  863. .cra_u.ablkcipher = {
  864. .min_keysize = AES_MIN_KEY_SIZE,
  865. .max_keysize = AES_MAX_KEY_SIZE,
  866. .ivsize = AES_BLOCK_SIZE,
  867. .setkey = atmel_aes_setkey,
  868. .encrypt = atmel_aes_cfb32_encrypt,
  869. .decrypt = atmel_aes_cfb32_decrypt,
  870. }
  871. },
  872. {
  873. .cra_name = "cfb16(aes)",
  874. .cra_driver_name = "atmel-cfb16-aes",
  875. .cra_priority = 100,
  876. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  877. .cra_blocksize = CFB16_BLOCK_SIZE,
  878. .cra_ctxsize = sizeof(struct atmel_aes_ctx),
  879. .cra_alignmask = 0x1,
  880. .cra_type = &crypto_ablkcipher_type,
  881. .cra_module = THIS_MODULE,
  882. .cra_init = atmel_aes_cra_init,
  883. .cra_exit = atmel_aes_cra_exit,
  884. .cra_u.ablkcipher = {
  885. .min_keysize = AES_MIN_KEY_SIZE,
  886. .max_keysize = AES_MAX_KEY_SIZE,
  887. .ivsize = AES_BLOCK_SIZE,
  888. .setkey = atmel_aes_setkey,
  889. .encrypt = atmel_aes_cfb16_encrypt,
  890. .decrypt = atmel_aes_cfb16_decrypt,
  891. }
  892. },
  893. {
  894. .cra_name = "cfb8(aes)",
  895. .cra_driver_name = "atmel-cfb8-aes",
  896. .cra_priority = 100,
  897. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  898. .cra_blocksize = CFB64_BLOCK_SIZE,
  899. .cra_ctxsize = sizeof(struct atmel_aes_ctx),
  900. .cra_alignmask = 0x0,
  901. .cra_type = &crypto_ablkcipher_type,
  902. .cra_module = THIS_MODULE,
  903. .cra_init = atmel_aes_cra_init,
  904. .cra_exit = atmel_aes_cra_exit,
  905. .cra_u.ablkcipher = {
  906. .min_keysize = AES_MIN_KEY_SIZE,
  907. .max_keysize = AES_MAX_KEY_SIZE,
  908. .ivsize = AES_BLOCK_SIZE,
  909. .setkey = atmel_aes_setkey,
  910. .encrypt = atmel_aes_cfb8_encrypt,
  911. .decrypt = atmel_aes_cfb8_decrypt,
  912. }
  913. },
  914. {
  915. .cra_name = "ctr(aes)",
  916. .cra_driver_name = "atmel-ctr-aes",
  917. .cra_priority = 100,
  918. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  919. .cra_blocksize = AES_BLOCK_SIZE,
  920. .cra_ctxsize = sizeof(struct atmel_aes_ctx),
  921. .cra_alignmask = 0xf,
  922. .cra_type = &crypto_ablkcipher_type,
  923. .cra_module = THIS_MODULE,
  924. .cra_init = atmel_aes_cra_init,
  925. .cra_exit = atmel_aes_cra_exit,
  926. .cra_u.ablkcipher = {
  927. .min_keysize = AES_MIN_KEY_SIZE,
  928. .max_keysize = AES_MAX_KEY_SIZE,
  929. .ivsize = AES_BLOCK_SIZE,
  930. .setkey = atmel_aes_setkey,
  931. .encrypt = atmel_aes_ctr_encrypt,
  932. .decrypt = atmel_aes_ctr_decrypt,
  933. }
  934. },
  935. };
  936. static struct crypto_alg aes_cfb64_alg = {
  937. .cra_name = "cfb64(aes)",
  938. .cra_driver_name = "atmel-cfb64-aes",
  939. .cra_priority = 100,
  940. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  941. .cra_blocksize = CFB64_BLOCK_SIZE,
  942. .cra_ctxsize = sizeof(struct atmel_aes_ctx),
  943. .cra_alignmask = 0x7,
  944. .cra_type = &crypto_ablkcipher_type,
  945. .cra_module = THIS_MODULE,
  946. .cra_init = atmel_aes_cra_init,
  947. .cra_exit = atmel_aes_cra_exit,
  948. .cra_u.ablkcipher = {
  949. .min_keysize = AES_MIN_KEY_SIZE,
  950. .max_keysize = AES_MAX_KEY_SIZE,
  951. .ivsize = AES_BLOCK_SIZE,
  952. .setkey = atmel_aes_setkey,
  953. .encrypt = atmel_aes_cfb64_encrypt,
  954. .decrypt = atmel_aes_cfb64_decrypt,
  955. }
  956. };
  957. static void atmel_aes_queue_task(unsigned long data)
  958. {
  959. struct atmel_aes_dev *dd = (struct atmel_aes_dev *)data;
  960. atmel_aes_handle_queue(dd, NULL);
  961. }
  962. static void atmel_aes_done_task(unsigned long data)
  963. {
  964. struct atmel_aes_dev *dd = (struct atmel_aes_dev *) data;
  965. int err;
  966. if (!(dd->flags & AES_FLAGS_DMA)) {
  967. atmel_aes_read_n(dd, AES_ODATAR(0), (u32 *) dd->buf_out,
  968. dd->bufcnt >> 2);
  969. if (sg_copy_from_buffer(dd->out_sg, dd->nb_out_sg,
  970. dd->buf_out, dd->bufcnt))
  971. err = 0;
  972. else
  973. err = -EINVAL;
  974. goto cpu_end;
  975. }
  976. err = atmel_aes_crypt_dma_stop(dd);
  977. err = dd->err ? : err;
  978. if (dd->total && !err) {
  979. if (dd->flags & AES_FLAGS_FAST) {
  980. dd->in_sg = sg_next(dd->in_sg);
  981. dd->out_sg = sg_next(dd->out_sg);
  982. if (!dd->in_sg || !dd->out_sg)
  983. err = -EINVAL;
  984. }
  985. if (!err)
  986. err = atmel_aes_crypt_dma_start(dd);
  987. if (!err)
  988. return; /* DMA started. Not fininishing. */
  989. }
  990. cpu_end:
  991. atmel_aes_finish_req(dd, err);
  992. atmel_aes_handle_queue(dd, NULL);
  993. }
  994. static irqreturn_t atmel_aes_irq(int irq, void *dev_id)
  995. {
  996. struct atmel_aes_dev *aes_dd = dev_id;
  997. u32 reg;
  998. reg = atmel_aes_read(aes_dd, AES_ISR);
  999. if (reg & atmel_aes_read(aes_dd, AES_IMR)) {
  1000. atmel_aes_write(aes_dd, AES_IDR, reg);
  1001. if (AES_FLAGS_BUSY & aes_dd->flags)
  1002. tasklet_schedule(&aes_dd->done_task);
  1003. else
  1004. dev_warn(aes_dd->dev, "AES interrupt when no active requests.\n");
  1005. return IRQ_HANDLED;
  1006. }
  1007. return IRQ_NONE;
  1008. }
  1009. static void atmel_aes_unregister_algs(struct atmel_aes_dev *dd)
  1010. {
  1011. int i;
  1012. for (i = 0; i < ARRAY_SIZE(aes_algs); i++)
  1013. crypto_unregister_alg(&aes_algs[i]);
  1014. if (dd->caps.has_cfb64)
  1015. crypto_unregister_alg(&aes_cfb64_alg);
  1016. }
  1017. static int atmel_aes_register_algs(struct atmel_aes_dev *dd)
  1018. {
  1019. int err, i, j;
  1020. for (i = 0; i < ARRAY_SIZE(aes_algs); i++) {
  1021. err = crypto_register_alg(&aes_algs[i]);
  1022. if (err)
  1023. goto err_aes_algs;
  1024. }
  1025. if (dd->caps.has_cfb64) {
  1026. err = crypto_register_alg(&aes_cfb64_alg);
  1027. if (err)
  1028. goto err_aes_cfb64_alg;
  1029. }
  1030. return 0;
  1031. err_aes_cfb64_alg:
  1032. i = ARRAY_SIZE(aes_algs);
  1033. err_aes_algs:
  1034. for (j = 0; j < i; j++)
  1035. crypto_unregister_alg(&aes_algs[j]);
  1036. return err;
  1037. }
  1038. static void atmel_aes_get_cap(struct atmel_aes_dev *dd)
  1039. {
  1040. dd->caps.has_dualbuff = 0;
  1041. dd->caps.has_cfb64 = 0;
  1042. dd->caps.max_burst_size = 1;
  1043. /* keep only major version number */
  1044. switch (dd->hw_version & 0xff0) {
  1045. case 0x130:
  1046. dd->caps.has_dualbuff = 1;
  1047. dd->caps.has_cfb64 = 1;
  1048. dd->caps.max_burst_size = 4;
  1049. break;
  1050. case 0x120:
  1051. break;
  1052. default:
  1053. dev_warn(dd->dev,
  1054. "Unmanaged aes version, set minimum capabilities\n");
  1055. break;
  1056. }
  1057. }
  1058. static int atmel_aes_probe(struct platform_device *pdev)
  1059. {
  1060. struct atmel_aes_dev *aes_dd;
  1061. struct crypto_platform_data *pdata;
  1062. struct device *dev = &pdev->dev;
  1063. struct resource *aes_res;
  1064. unsigned long aes_phys_size;
  1065. int err;
  1066. pdata = pdev->dev.platform_data;
  1067. if (!pdata) {
  1068. err = -ENXIO;
  1069. goto aes_dd_err;
  1070. }
  1071. aes_dd = kzalloc(sizeof(struct atmel_aes_dev), GFP_KERNEL);
  1072. if (aes_dd == NULL) {
  1073. dev_err(dev, "unable to alloc data struct.\n");
  1074. err = -ENOMEM;
  1075. goto aes_dd_err;
  1076. }
  1077. aes_dd->dev = dev;
  1078. platform_set_drvdata(pdev, aes_dd);
  1079. INIT_LIST_HEAD(&aes_dd->list);
  1080. tasklet_init(&aes_dd->done_task, atmel_aes_done_task,
  1081. (unsigned long)aes_dd);
  1082. tasklet_init(&aes_dd->queue_task, atmel_aes_queue_task,
  1083. (unsigned long)aes_dd);
  1084. crypto_init_queue(&aes_dd->queue, ATMEL_AES_QUEUE_LENGTH);
  1085. aes_dd->irq = -1;
  1086. /* Get the base address */
  1087. aes_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1088. if (!aes_res) {
  1089. dev_err(dev, "no MEM resource info\n");
  1090. err = -ENODEV;
  1091. goto res_err;
  1092. }
  1093. aes_dd->phys_base = aes_res->start;
  1094. aes_phys_size = resource_size(aes_res);
  1095. /* Get the IRQ */
  1096. aes_dd->irq = platform_get_irq(pdev, 0);
  1097. if (aes_dd->irq < 0) {
  1098. dev_err(dev, "no IRQ resource info\n");
  1099. err = aes_dd->irq;
  1100. goto aes_irq_err;
  1101. }
  1102. err = request_irq(aes_dd->irq, atmel_aes_irq, IRQF_SHARED, "atmel-aes",
  1103. aes_dd);
  1104. if (err) {
  1105. dev_err(dev, "unable to request aes irq.\n");
  1106. goto aes_irq_err;
  1107. }
  1108. /* Initializing the clock */
  1109. aes_dd->iclk = clk_get(&pdev->dev, "aes_clk");
  1110. if (IS_ERR(aes_dd->iclk)) {
  1111. dev_err(dev, "clock intialization failed.\n");
  1112. err = PTR_ERR(aes_dd->iclk);
  1113. goto clk_err;
  1114. }
  1115. aes_dd->io_base = ioremap(aes_dd->phys_base, aes_phys_size);
  1116. if (!aes_dd->io_base) {
  1117. dev_err(dev, "can't ioremap\n");
  1118. err = -ENOMEM;
  1119. goto aes_io_err;
  1120. }
  1121. atmel_aes_hw_version_init(aes_dd);
  1122. atmel_aes_get_cap(aes_dd);
  1123. err = atmel_aes_buff_init(aes_dd);
  1124. if (err)
  1125. goto err_aes_buff;
  1126. err = atmel_aes_dma_init(aes_dd, pdata);
  1127. if (err)
  1128. goto err_aes_dma;
  1129. spin_lock(&atmel_aes.lock);
  1130. list_add_tail(&aes_dd->list, &atmel_aes.dev_list);
  1131. spin_unlock(&atmel_aes.lock);
  1132. err = atmel_aes_register_algs(aes_dd);
  1133. if (err)
  1134. goto err_algs;
  1135. dev_info(dev, "Atmel AES\n");
  1136. return 0;
  1137. err_algs:
  1138. spin_lock(&atmel_aes.lock);
  1139. list_del(&aes_dd->list);
  1140. spin_unlock(&atmel_aes.lock);
  1141. atmel_aes_dma_cleanup(aes_dd);
  1142. err_aes_dma:
  1143. atmel_aes_buff_cleanup(aes_dd);
  1144. err_aes_buff:
  1145. iounmap(aes_dd->io_base);
  1146. aes_io_err:
  1147. clk_put(aes_dd->iclk);
  1148. clk_err:
  1149. free_irq(aes_dd->irq, aes_dd);
  1150. aes_irq_err:
  1151. res_err:
  1152. tasklet_kill(&aes_dd->done_task);
  1153. tasklet_kill(&aes_dd->queue_task);
  1154. kfree(aes_dd);
  1155. aes_dd = NULL;
  1156. aes_dd_err:
  1157. dev_err(dev, "initialization failed.\n");
  1158. return err;
  1159. }
  1160. static int atmel_aes_remove(struct platform_device *pdev)
  1161. {
  1162. static struct atmel_aes_dev *aes_dd;
  1163. aes_dd = platform_get_drvdata(pdev);
  1164. if (!aes_dd)
  1165. return -ENODEV;
  1166. spin_lock(&atmel_aes.lock);
  1167. list_del(&aes_dd->list);
  1168. spin_unlock(&atmel_aes.lock);
  1169. atmel_aes_unregister_algs(aes_dd);
  1170. tasklet_kill(&aes_dd->done_task);
  1171. tasklet_kill(&aes_dd->queue_task);
  1172. atmel_aes_dma_cleanup(aes_dd);
  1173. iounmap(aes_dd->io_base);
  1174. clk_put(aes_dd->iclk);
  1175. if (aes_dd->irq > 0)
  1176. free_irq(aes_dd->irq, aes_dd);
  1177. kfree(aes_dd);
  1178. aes_dd = NULL;
  1179. return 0;
  1180. }
  1181. static struct platform_driver atmel_aes_driver = {
  1182. .probe = atmel_aes_probe,
  1183. .remove = atmel_aes_remove,
  1184. .driver = {
  1185. .name = "atmel_aes",
  1186. .owner = THIS_MODULE,
  1187. },
  1188. };
  1189. module_platform_driver(atmel_aes_driver);
  1190. MODULE_DESCRIPTION("Atmel AES hw acceleration support.");
  1191. MODULE_LICENSE("GPL v2");
  1192. MODULE_AUTHOR("Nicolas Royer - Eukréa Electromatique");