atmel-tdes.c 37 KB

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