atmel-aes.c 35 KB

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