atmel-tdes.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525
  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. unsigned int 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: %u\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 %d 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 %d 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(dd->total, sg_dma_len(dd->in_sg));
  425. count = min(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: %u\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. int err = -ENOMEM;
  586. dma_cap_mask_t mask;
  587. dma_cap_zero(mask);
  588. dma_cap_set(DMA_SLAVE, mask);
  589. /* Try to grab 2 DMA channels */
  590. dd->dma_lch_in.chan = dma_request_slave_channel_compat(mask,
  591. atmel_tdes_filter, &pdata->dma_slave->rxdata, dd->dev, "tx");
  592. if (!dd->dma_lch_in.chan)
  593. goto err_dma_in;
  594. dd->dma_lch_in.dma_conf.direction = DMA_MEM_TO_DEV;
  595. dd->dma_lch_in.dma_conf.dst_addr = dd->phys_base +
  596. TDES_IDATA1R;
  597. dd->dma_lch_in.dma_conf.src_maxburst = 1;
  598. dd->dma_lch_in.dma_conf.src_addr_width =
  599. DMA_SLAVE_BUSWIDTH_4_BYTES;
  600. dd->dma_lch_in.dma_conf.dst_maxburst = 1;
  601. dd->dma_lch_in.dma_conf.dst_addr_width =
  602. DMA_SLAVE_BUSWIDTH_4_BYTES;
  603. dd->dma_lch_in.dma_conf.device_fc = false;
  604. dd->dma_lch_out.chan = dma_request_slave_channel_compat(mask,
  605. atmel_tdes_filter, &pdata->dma_slave->txdata, dd->dev, "rx");
  606. if (!dd->dma_lch_out.chan)
  607. goto err_dma_out;
  608. dd->dma_lch_out.dma_conf.direction = DMA_DEV_TO_MEM;
  609. dd->dma_lch_out.dma_conf.src_addr = dd->phys_base +
  610. TDES_ODATA1R;
  611. dd->dma_lch_out.dma_conf.src_maxburst = 1;
  612. dd->dma_lch_out.dma_conf.src_addr_width =
  613. DMA_SLAVE_BUSWIDTH_4_BYTES;
  614. dd->dma_lch_out.dma_conf.dst_maxburst = 1;
  615. dd->dma_lch_out.dma_conf.dst_addr_width =
  616. DMA_SLAVE_BUSWIDTH_4_BYTES;
  617. dd->dma_lch_out.dma_conf.device_fc = false;
  618. return 0;
  619. err_dma_out:
  620. dma_release_channel(dd->dma_lch_in.chan);
  621. err_dma_in:
  622. dev_warn(dd->dev, "no DMA channel available\n");
  623. return err;
  624. }
  625. static void atmel_tdes_dma_cleanup(struct atmel_tdes_dev *dd)
  626. {
  627. dma_release_channel(dd->dma_lch_in.chan);
  628. dma_release_channel(dd->dma_lch_out.chan);
  629. }
  630. static int atmel_des_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
  631. unsigned int keylen)
  632. {
  633. u32 tmp[DES_EXPKEY_WORDS];
  634. int err;
  635. struct crypto_tfm *ctfm = crypto_ablkcipher_tfm(tfm);
  636. struct atmel_tdes_ctx *ctx = crypto_ablkcipher_ctx(tfm);
  637. if (keylen != DES_KEY_SIZE) {
  638. crypto_ablkcipher_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
  639. return -EINVAL;
  640. }
  641. err = des_ekey(tmp, key);
  642. if (err == 0 && (ctfm->crt_flags & CRYPTO_TFM_REQ_WEAK_KEY)) {
  643. ctfm->crt_flags |= CRYPTO_TFM_RES_WEAK_KEY;
  644. return -EINVAL;
  645. }
  646. memcpy(ctx->key, key, keylen);
  647. ctx->keylen = keylen;
  648. return 0;
  649. }
  650. static int atmel_tdes_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
  651. unsigned int keylen)
  652. {
  653. struct atmel_tdes_ctx *ctx = crypto_ablkcipher_ctx(tfm);
  654. const char *alg_name;
  655. alg_name = crypto_tfm_alg_name(crypto_ablkcipher_tfm(tfm));
  656. /*
  657. * HW bug in cfb 3-keys mode.
  658. */
  659. if (!ctx->dd->caps.has_cfb_3keys && strstr(alg_name, "cfb")
  660. && (keylen != 2*DES_KEY_SIZE)) {
  661. crypto_ablkcipher_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
  662. return -EINVAL;
  663. } else if ((keylen != 2*DES_KEY_SIZE) && (keylen != 3*DES_KEY_SIZE)) {
  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_tdes_ecb_encrypt(struct ablkcipher_request *req)
  672. {
  673. return atmel_tdes_crypt(req, TDES_FLAGS_ENCRYPT);
  674. }
  675. static int atmel_tdes_ecb_decrypt(struct ablkcipher_request *req)
  676. {
  677. return atmel_tdes_crypt(req, 0);
  678. }
  679. static int atmel_tdes_cbc_encrypt(struct ablkcipher_request *req)
  680. {
  681. return atmel_tdes_crypt(req, TDES_FLAGS_ENCRYPT | TDES_FLAGS_CBC);
  682. }
  683. static int atmel_tdes_cbc_decrypt(struct ablkcipher_request *req)
  684. {
  685. return atmel_tdes_crypt(req, TDES_FLAGS_CBC);
  686. }
  687. static int atmel_tdes_cfb_encrypt(struct ablkcipher_request *req)
  688. {
  689. return atmel_tdes_crypt(req, TDES_FLAGS_ENCRYPT | TDES_FLAGS_CFB);
  690. }
  691. static int atmel_tdes_cfb_decrypt(struct ablkcipher_request *req)
  692. {
  693. return atmel_tdes_crypt(req, TDES_FLAGS_CFB);
  694. }
  695. static int atmel_tdes_cfb8_encrypt(struct ablkcipher_request *req)
  696. {
  697. return atmel_tdes_crypt(req, TDES_FLAGS_ENCRYPT | TDES_FLAGS_CFB |
  698. TDES_FLAGS_CFB8);
  699. }
  700. static int atmel_tdes_cfb8_decrypt(struct ablkcipher_request *req)
  701. {
  702. return atmel_tdes_crypt(req, TDES_FLAGS_CFB | TDES_FLAGS_CFB8);
  703. }
  704. static int atmel_tdes_cfb16_encrypt(struct ablkcipher_request *req)
  705. {
  706. return atmel_tdes_crypt(req, TDES_FLAGS_ENCRYPT | TDES_FLAGS_CFB |
  707. TDES_FLAGS_CFB16);
  708. }
  709. static int atmel_tdes_cfb16_decrypt(struct ablkcipher_request *req)
  710. {
  711. return atmel_tdes_crypt(req, TDES_FLAGS_CFB | TDES_FLAGS_CFB16);
  712. }
  713. static int atmel_tdes_cfb32_encrypt(struct ablkcipher_request *req)
  714. {
  715. return atmel_tdes_crypt(req, TDES_FLAGS_ENCRYPT | TDES_FLAGS_CFB |
  716. TDES_FLAGS_CFB32);
  717. }
  718. static int atmel_tdes_cfb32_decrypt(struct ablkcipher_request *req)
  719. {
  720. return atmel_tdes_crypt(req, TDES_FLAGS_CFB | TDES_FLAGS_CFB32);
  721. }
  722. static int atmel_tdes_ofb_encrypt(struct ablkcipher_request *req)
  723. {
  724. return atmel_tdes_crypt(req, TDES_FLAGS_ENCRYPT | TDES_FLAGS_OFB);
  725. }
  726. static int atmel_tdes_ofb_decrypt(struct ablkcipher_request *req)
  727. {
  728. return atmel_tdes_crypt(req, TDES_FLAGS_OFB);
  729. }
  730. static int atmel_tdes_cra_init(struct crypto_tfm *tfm)
  731. {
  732. struct atmel_tdes_ctx *ctx = crypto_tfm_ctx(tfm);
  733. struct atmel_tdes_dev *dd;
  734. tfm->crt_ablkcipher.reqsize = sizeof(struct atmel_tdes_reqctx);
  735. dd = atmel_tdes_find_dev(ctx);
  736. if (!dd)
  737. return -ENODEV;
  738. return 0;
  739. }
  740. static void atmel_tdes_cra_exit(struct crypto_tfm *tfm)
  741. {
  742. }
  743. static struct crypto_alg tdes_algs[] = {
  744. {
  745. .cra_name = "ecb(des)",
  746. .cra_driver_name = "atmel-ecb-des",
  747. .cra_priority = 100,
  748. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  749. .cra_blocksize = DES_BLOCK_SIZE,
  750. .cra_ctxsize = sizeof(struct atmel_tdes_ctx),
  751. .cra_alignmask = 0x7,
  752. .cra_type = &crypto_ablkcipher_type,
  753. .cra_module = THIS_MODULE,
  754. .cra_init = atmel_tdes_cra_init,
  755. .cra_exit = atmel_tdes_cra_exit,
  756. .cra_u.ablkcipher = {
  757. .min_keysize = DES_KEY_SIZE,
  758. .max_keysize = DES_KEY_SIZE,
  759. .setkey = atmel_des_setkey,
  760. .encrypt = atmel_tdes_ecb_encrypt,
  761. .decrypt = atmel_tdes_ecb_decrypt,
  762. }
  763. },
  764. {
  765. .cra_name = "cbc(des)",
  766. .cra_driver_name = "atmel-cbc-des",
  767. .cra_priority = 100,
  768. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  769. .cra_blocksize = DES_BLOCK_SIZE,
  770. .cra_ctxsize = sizeof(struct atmel_tdes_ctx),
  771. .cra_alignmask = 0x7,
  772. .cra_type = &crypto_ablkcipher_type,
  773. .cra_module = THIS_MODULE,
  774. .cra_init = atmel_tdes_cra_init,
  775. .cra_exit = atmel_tdes_cra_exit,
  776. .cra_u.ablkcipher = {
  777. .min_keysize = DES_KEY_SIZE,
  778. .max_keysize = DES_KEY_SIZE,
  779. .ivsize = DES_BLOCK_SIZE,
  780. .setkey = atmel_des_setkey,
  781. .encrypt = atmel_tdes_cbc_encrypt,
  782. .decrypt = atmel_tdes_cbc_decrypt,
  783. }
  784. },
  785. {
  786. .cra_name = "cfb(des)",
  787. .cra_driver_name = "atmel-cfb-des",
  788. .cra_priority = 100,
  789. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  790. .cra_blocksize = DES_BLOCK_SIZE,
  791. .cra_ctxsize = sizeof(struct atmel_tdes_ctx),
  792. .cra_alignmask = 0x7,
  793. .cra_type = &crypto_ablkcipher_type,
  794. .cra_module = THIS_MODULE,
  795. .cra_init = atmel_tdes_cra_init,
  796. .cra_exit = atmel_tdes_cra_exit,
  797. .cra_u.ablkcipher = {
  798. .min_keysize = DES_KEY_SIZE,
  799. .max_keysize = DES_KEY_SIZE,
  800. .ivsize = DES_BLOCK_SIZE,
  801. .setkey = atmel_des_setkey,
  802. .encrypt = atmel_tdes_cfb_encrypt,
  803. .decrypt = atmel_tdes_cfb_decrypt,
  804. }
  805. },
  806. {
  807. .cra_name = "cfb8(des)",
  808. .cra_driver_name = "atmel-cfb8-des",
  809. .cra_priority = 100,
  810. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  811. .cra_blocksize = CFB8_BLOCK_SIZE,
  812. .cra_ctxsize = sizeof(struct atmel_tdes_ctx),
  813. .cra_alignmask = 0,
  814. .cra_type = &crypto_ablkcipher_type,
  815. .cra_module = THIS_MODULE,
  816. .cra_init = atmel_tdes_cra_init,
  817. .cra_exit = atmel_tdes_cra_exit,
  818. .cra_u.ablkcipher = {
  819. .min_keysize = DES_KEY_SIZE,
  820. .max_keysize = DES_KEY_SIZE,
  821. .ivsize = DES_BLOCK_SIZE,
  822. .setkey = atmel_des_setkey,
  823. .encrypt = atmel_tdes_cfb8_encrypt,
  824. .decrypt = atmel_tdes_cfb8_decrypt,
  825. }
  826. },
  827. {
  828. .cra_name = "cfb16(des)",
  829. .cra_driver_name = "atmel-cfb16-des",
  830. .cra_priority = 100,
  831. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  832. .cra_blocksize = CFB16_BLOCK_SIZE,
  833. .cra_ctxsize = sizeof(struct atmel_tdes_ctx),
  834. .cra_alignmask = 0x1,
  835. .cra_type = &crypto_ablkcipher_type,
  836. .cra_module = THIS_MODULE,
  837. .cra_init = atmel_tdes_cra_init,
  838. .cra_exit = atmel_tdes_cra_exit,
  839. .cra_u.ablkcipher = {
  840. .min_keysize = DES_KEY_SIZE,
  841. .max_keysize = DES_KEY_SIZE,
  842. .ivsize = DES_BLOCK_SIZE,
  843. .setkey = atmel_des_setkey,
  844. .encrypt = atmel_tdes_cfb16_encrypt,
  845. .decrypt = atmel_tdes_cfb16_decrypt,
  846. }
  847. },
  848. {
  849. .cra_name = "cfb32(des)",
  850. .cra_driver_name = "atmel-cfb32-des",
  851. .cra_priority = 100,
  852. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  853. .cra_blocksize = CFB32_BLOCK_SIZE,
  854. .cra_ctxsize = sizeof(struct atmel_tdes_ctx),
  855. .cra_alignmask = 0x3,
  856. .cra_type = &crypto_ablkcipher_type,
  857. .cra_module = THIS_MODULE,
  858. .cra_init = atmel_tdes_cra_init,
  859. .cra_exit = atmel_tdes_cra_exit,
  860. .cra_u.ablkcipher = {
  861. .min_keysize = DES_KEY_SIZE,
  862. .max_keysize = DES_KEY_SIZE,
  863. .ivsize = DES_BLOCK_SIZE,
  864. .setkey = atmel_des_setkey,
  865. .encrypt = atmel_tdes_cfb32_encrypt,
  866. .decrypt = atmel_tdes_cfb32_decrypt,
  867. }
  868. },
  869. {
  870. .cra_name = "ofb(des)",
  871. .cra_driver_name = "atmel-ofb-des",
  872. .cra_priority = 100,
  873. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  874. .cra_blocksize = DES_BLOCK_SIZE,
  875. .cra_ctxsize = sizeof(struct atmel_tdes_ctx),
  876. .cra_alignmask = 0x7,
  877. .cra_type = &crypto_ablkcipher_type,
  878. .cra_module = THIS_MODULE,
  879. .cra_init = atmel_tdes_cra_init,
  880. .cra_exit = atmel_tdes_cra_exit,
  881. .cra_u.ablkcipher = {
  882. .min_keysize = DES_KEY_SIZE,
  883. .max_keysize = DES_KEY_SIZE,
  884. .ivsize = DES_BLOCK_SIZE,
  885. .setkey = atmel_des_setkey,
  886. .encrypt = atmel_tdes_ofb_encrypt,
  887. .decrypt = atmel_tdes_ofb_decrypt,
  888. }
  889. },
  890. {
  891. .cra_name = "ecb(des3_ede)",
  892. .cra_driver_name = "atmel-ecb-tdes",
  893. .cra_priority = 100,
  894. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  895. .cra_blocksize = DES_BLOCK_SIZE,
  896. .cra_ctxsize = sizeof(struct atmel_tdes_ctx),
  897. .cra_alignmask = 0x7,
  898. .cra_type = &crypto_ablkcipher_type,
  899. .cra_module = THIS_MODULE,
  900. .cra_init = atmel_tdes_cra_init,
  901. .cra_exit = atmel_tdes_cra_exit,
  902. .cra_u.ablkcipher = {
  903. .min_keysize = 2 * DES_KEY_SIZE,
  904. .max_keysize = 3 * DES_KEY_SIZE,
  905. .setkey = atmel_tdes_setkey,
  906. .encrypt = atmel_tdes_ecb_encrypt,
  907. .decrypt = atmel_tdes_ecb_decrypt,
  908. }
  909. },
  910. {
  911. .cra_name = "cbc(des3_ede)",
  912. .cra_driver_name = "atmel-cbc-tdes",
  913. .cra_priority = 100,
  914. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  915. .cra_blocksize = DES_BLOCK_SIZE,
  916. .cra_ctxsize = sizeof(struct atmel_tdes_ctx),
  917. .cra_alignmask = 0x7,
  918. .cra_type = &crypto_ablkcipher_type,
  919. .cra_module = THIS_MODULE,
  920. .cra_init = atmel_tdes_cra_init,
  921. .cra_exit = atmel_tdes_cra_exit,
  922. .cra_u.ablkcipher = {
  923. .min_keysize = 2*DES_KEY_SIZE,
  924. .max_keysize = 3*DES_KEY_SIZE,
  925. .ivsize = DES_BLOCK_SIZE,
  926. .setkey = atmel_tdes_setkey,
  927. .encrypt = atmel_tdes_cbc_encrypt,
  928. .decrypt = atmel_tdes_cbc_decrypt,
  929. }
  930. },
  931. {
  932. .cra_name = "cfb(des3_ede)",
  933. .cra_driver_name = "atmel-cfb-tdes",
  934. .cra_priority = 100,
  935. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  936. .cra_blocksize = DES_BLOCK_SIZE,
  937. .cra_ctxsize = sizeof(struct atmel_tdes_ctx),
  938. .cra_alignmask = 0x7,
  939. .cra_type = &crypto_ablkcipher_type,
  940. .cra_module = THIS_MODULE,
  941. .cra_init = atmel_tdes_cra_init,
  942. .cra_exit = atmel_tdes_cra_exit,
  943. .cra_u.ablkcipher = {
  944. .min_keysize = 2*DES_KEY_SIZE,
  945. .max_keysize = 2*DES_KEY_SIZE,
  946. .ivsize = DES_BLOCK_SIZE,
  947. .setkey = atmel_tdes_setkey,
  948. .encrypt = atmel_tdes_cfb_encrypt,
  949. .decrypt = atmel_tdes_cfb_decrypt,
  950. }
  951. },
  952. {
  953. .cra_name = "cfb8(des3_ede)",
  954. .cra_driver_name = "atmel-cfb8-tdes",
  955. .cra_priority = 100,
  956. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  957. .cra_blocksize = CFB8_BLOCK_SIZE,
  958. .cra_ctxsize = sizeof(struct atmel_tdes_ctx),
  959. .cra_alignmask = 0,
  960. .cra_type = &crypto_ablkcipher_type,
  961. .cra_module = THIS_MODULE,
  962. .cra_init = atmel_tdes_cra_init,
  963. .cra_exit = atmel_tdes_cra_exit,
  964. .cra_u.ablkcipher = {
  965. .min_keysize = 2*DES_KEY_SIZE,
  966. .max_keysize = 2*DES_KEY_SIZE,
  967. .ivsize = DES_BLOCK_SIZE,
  968. .setkey = atmel_tdes_setkey,
  969. .encrypt = atmel_tdes_cfb8_encrypt,
  970. .decrypt = atmel_tdes_cfb8_decrypt,
  971. }
  972. },
  973. {
  974. .cra_name = "cfb16(des3_ede)",
  975. .cra_driver_name = "atmel-cfb16-tdes",
  976. .cra_priority = 100,
  977. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  978. .cra_blocksize = CFB16_BLOCK_SIZE,
  979. .cra_ctxsize = sizeof(struct atmel_tdes_ctx),
  980. .cra_alignmask = 0x1,
  981. .cra_type = &crypto_ablkcipher_type,
  982. .cra_module = THIS_MODULE,
  983. .cra_init = atmel_tdes_cra_init,
  984. .cra_exit = atmel_tdes_cra_exit,
  985. .cra_u.ablkcipher = {
  986. .min_keysize = 2*DES_KEY_SIZE,
  987. .max_keysize = 2*DES_KEY_SIZE,
  988. .ivsize = DES_BLOCK_SIZE,
  989. .setkey = atmel_tdes_setkey,
  990. .encrypt = atmel_tdes_cfb16_encrypt,
  991. .decrypt = atmel_tdes_cfb16_decrypt,
  992. }
  993. },
  994. {
  995. .cra_name = "cfb32(des3_ede)",
  996. .cra_driver_name = "atmel-cfb32-tdes",
  997. .cra_priority = 100,
  998. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  999. .cra_blocksize = CFB32_BLOCK_SIZE,
  1000. .cra_ctxsize = sizeof(struct atmel_tdes_ctx),
  1001. .cra_alignmask = 0x3,
  1002. .cra_type = &crypto_ablkcipher_type,
  1003. .cra_module = THIS_MODULE,
  1004. .cra_init = atmel_tdes_cra_init,
  1005. .cra_exit = atmel_tdes_cra_exit,
  1006. .cra_u.ablkcipher = {
  1007. .min_keysize = 2*DES_KEY_SIZE,
  1008. .max_keysize = 2*DES_KEY_SIZE,
  1009. .ivsize = DES_BLOCK_SIZE,
  1010. .setkey = atmel_tdes_setkey,
  1011. .encrypt = atmel_tdes_cfb32_encrypt,
  1012. .decrypt = atmel_tdes_cfb32_decrypt,
  1013. }
  1014. },
  1015. {
  1016. .cra_name = "ofb(des3_ede)",
  1017. .cra_driver_name = "atmel-ofb-tdes",
  1018. .cra_priority = 100,
  1019. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  1020. .cra_blocksize = DES_BLOCK_SIZE,
  1021. .cra_ctxsize = sizeof(struct atmel_tdes_ctx),
  1022. .cra_alignmask = 0x7,
  1023. .cra_type = &crypto_ablkcipher_type,
  1024. .cra_module = THIS_MODULE,
  1025. .cra_init = atmel_tdes_cra_init,
  1026. .cra_exit = atmel_tdes_cra_exit,
  1027. .cra_u.ablkcipher = {
  1028. .min_keysize = 2*DES_KEY_SIZE,
  1029. .max_keysize = 3*DES_KEY_SIZE,
  1030. .ivsize = DES_BLOCK_SIZE,
  1031. .setkey = atmel_tdes_setkey,
  1032. .encrypt = atmel_tdes_ofb_encrypt,
  1033. .decrypt = atmel_tdes_ofb_decrypt,
  1034. }
  1035. },
  1036. };
  1037. static void atmel_tdes_queue_task(unsigned long data)
  1038. {
  1039. struct atmel_tdes_dev *dd = (struct atmel_tdes_dev *)data;
  1040. atmel_tdes_handle_queue(dd, NULL);
  1041. }
  1042. static void atmel_tdes_done_task(unsigned long data)
  1043. {
  1044. struct atmel_tdes_dev *dd = (struct atmel_tdes_dev *) data;
  1045. int err;
  1046. if (!(dd->flags & TDES_FLAGS_DMA))
  1047. err = atmel_tdes_crypt_pdc_stop(dd);
  1048. else
  1049. err = atmel_tdes_crypt_dma_stop(dd);
  1050. err = dd->err ? : err;
  1051. if (dd->total && !err) {
  1052. if (dd->flags & TDES_FLAGS_FAST) {
  1053. dd->in_sg = sg_next(dd->in_sg);
  1054. dd->out_sg = sg_next(dd->out_sg);
  1055. if (!dd->in_sg || !dd->out_sg)
  1056. err = -EINVAL;
  1057. }
  1058. if (!err)
  1059. err = atmel_tdes_crypt_start(dd);
  1060. if (!err)
  1061. return; /* DMA started. Not fininishing. */
  1062. }
  1063. atmel_tdes_finish_req(dd, err);
  1064. atmel_tdes_handle_queue(dd, NULL);
  1065. }
  1066. static irqreturn_t atmel_tdes_irq(int irq, void *dev_id)
  1067. {
  1068. struct atmel_tdes_dev *tdes_dd = dev_id;
  1069. u32 reg;
  1070. reg = atmel_tdes_read(tdes_dd, TDES_ISR);
  1071. if (reg & atmel_tdes_read(tdes_dd, TDES_IMR)) {
  1072. atmel_tdes_write(tdes_dd, TDES_IDR, reg);
  1073. if (TDES_FLAGS_BUSY & tdes_dd->flags)
  1074. tasklet_schedule(&tdes_dd->done_task);
  1075. else
  1076. dev_warn(tdes_dd->dev, "TDES interrupt when no active requests.\n");
  1077. return IRQ_HANDLED;
  1078. }
  1079. return IRQ_NONE;
  1080. }
  1081. static void atmel_tdes_unregister_algs(struct atmel_tdes_dev *dd)
  1082. {
  1083. int i;
  1084. for (i = 0; i < ARRAY_SIZE(tdes_algs); i++)
  1085. crypto_unregister_alg(&tdes_algs[i]);
  1086. }
  1087. static int atmel_tdes_register_algs(struct atmel_tdes_dev *dd)
  1088. {
  1089. int err, i, j;
  1090. for (i = 0; i < ARRAY_SIZE(tdes_algs); i++) {
  1091. err = crypto_register_alg(&tdes_algs[i]);
  1092. if (err)
  1093. goto err_tdes_algs;
  1094. }
  1095. return 0;
  1096. err_tdes_algs:
  1097. for (j = 0; j < i; j++)
  1098. crypto_unregister_alg(&tdes_algs[j]);
  1099. return err;
  1100. }
  1101. static void atmel_tdes_get_cap(struct atmel_tdes_dev *dd)
  1102. {
  1103. dd->caps.has_dma = 0;
  1104. dd->caps.has_cfb_3keys = 0;
  1105. /* keep only major version number */
  1106. switch (dd->hw_version & 0xf00) {
  1107. case 0x700:
  1108. dd->caps.has_dma = 1;
  1109. dd->caps.has_cfb_3keys = 1;
  1110. break;
  1111. case 0x600:
  1112. break;
  1113. default:
  1114. dev_warn(dd->dev,
  1115. "Unmanaged tdes version, set minimum capabilities\n");
  1116. break;
  1117. }
  1118. }
  1119. #if defined(CONFIG_OF)
  1120. static const struct of_device_id atmel_tdes_dt_ids[] = {
  1121. { .compatible = "atmel,at91sam9g46-tdes" },
  1122. { /* sentinel */ }
  1123. };
  1124. MODULE_DEVICE_TABLE(of, atmel_tdes_dt_ids);
  1125. static struct crypto_platform_data *atmel_tdes_of_init(struct platform_device *pdev)
  1126. {
  1127. struct device_node *np = pdev->dev.of_node;
  1128. struct crypto_platform_data *pdata;
  1129. if (!np) {
  1130. dev_err(&pdev->dev, "device node not found\n");
  1131. return ERR_PTR(-EINVAL);
  1132. }
  1133. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  1134. if (!pdata) {
  1135. dev_err(&pdev->dev, "could not allocate memory for pdata\n");
  1136. return ERR_PTR(-ENOMEM);
  1137. }
  1138. pdata->dma_slave = devm_kzalloc(&pdev->dev,
  1139. sizeof(*(pdata->dma_slave)),
  1140. GFP_KERNEL);
  1141. if (!pdata->dma_slave) {
  1142. dev_err(&pdev->dev, "could not allocate memory for dma_slave\n");
  1143. return ERR_PTR(-ENOMEM);
  1144. }
  1145. return pdata;
  1146. }
  1147. #else /* CONFIG_OF */
  1148. static inline struct crypto_platform_data *atmel_tdes_of_init(struct platform_device *pdev)
  1149. {
  1150. return ERR_PTR(-EINVAL);
  1151. }
  1152. #endif
  1153. static int atmel_tdes_probe(struct platform_device *pdev)
  1154. {
  1155. struct atmel_tdes_dev *tdes_dd;
  1156. struct crypto_platform_data *pdata;
  1157. struct device *dev = &pdev->dev;
  1158. struct resource *tdes_res;
  1159. int err;
  1160. tdes_dd = devm_kmalloc(&pdev->dev, sizeof(*tdes_dd), GFP_KERNEL);
  1161. if (tdes_dd == NULL) {
  1162. dev_err(dev, "unable to alloc data struct.\n");
  1163. err = -ENOMEM;
  1164. goto tdes_dd_err;
  1165. }
  1166. tdes_dd->dev = dev;
  1167. platform_set_drvdata(pdev, tdes_dd);
  1168. INIT_LIST_HEAD(&tdes_dd->list);
  1169. spin_lock_init(&tdes_dd->lock);
  1170. tasklet_init(&tdes_dd->done_task, atmel_tdes_done_task,
  1171. (unsigned long)tdes_dd);
  1172. tasklet_init(&tdes_dd->queue_task, atmel_tdes_queue_task,
  1173. (unsigned long)tdes_dd);
  1174. crypto_init_queue(&tdes_dd->queue, ATMEL_TDES_QUEUE_LENGTH);
  1175. tdes_dd->irq = -1;
  1176. /* Get the base address */
  1177. tdes_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1178. if (!tdes_res) {
  1179. dev_err(dev, "no MEM resource info\n");
  1180. err = -ENODEV;
  1181. goto res_err;
  1182. }
  1183. tdes_dd->phys_base = tdes_res->start;
  1184. /* Get the IRQ */
  1185. tdes_dd->irq = platform_get_irq(pdev, 0);
  1186. if (tdes_dd->irq < 0) {
  1187. dev_err(dev, "no IRQ resource info\n");
  1188. err = tdes_dd->irq;
  1189. goto res_err;
  1190. }
  1191. err = devm_request_irq(&pdev->dev, tdes_dd->irq, atmel_tdes_irq,
  1192. IRQF_SHARED, "atmel-tdes", tdes_dd);
  1193. if (err) {
  1194. dev_err(dev, "unable to request tdes irq.\n");
  1195. goto res_err;
  1196. }
  1197. /* Initializing the clock */
  1198. tdes_dd->iclk = devm_clk_get(&pdev->dev, "tdes_clk");
  1199. if (IS_ERR(tdes_dd->iclk)) {
  1200. dev_err(dev, "clock initialization failed.\n");
  1201. err = PTR_ERR(tdes_dd->iclk);
  1202. goto res_err;
  1203. }
  1204. tdes_dd->io_base = devm_ioremap_resource(&pdev->dev, tdes_res);
  1205. if (IS_ERR(tdes_dd->io_base)) {
  1206. dev_err(dev, "can't ioremap\n");
  1207. err = PTR_ERR(tdes_dd->io_base);
  1208. goto res_err;
  1209. }
  1210. atmel_tdes_hw_version_init(tdes_dd);
  1211. atmel_tdes_get_cap(tdes_dd);
  1212. err = atmel_tdes_buff_init(tdes_dd);
  1213. if (err)
  1214. goto err_tdes_buff;
  1215. if (tdes_dd->caps.has_dma) {
  1216. pdata = pdev->dev.platform_data;
  1217. if (!pdata) {
  1218. pdata = atmel_tdes_of_init(pdev);
  1219. if (IS_ERR(pdata)) {
  1220. dev_err(&pdev->dev, "platform data not available\n");
  1221. err = PTR_ERR(pdata);
  1222. goto err_pdata;
  1223. }
  1224. }
  1225. if (!pdata->dma_slave) {
  1226. err = -ENXIO;
  1227. goto err_pdata;
  1228. }
  1229. err = atmel_tdes_dma_init(tdes_dd, pdata);
  1230. if (err)
  1231. goto err_tdes_dma;
  1232. dev_info(dev, "using %s, %s for DMA transfers\n",
  1233. dma_chan_name(tdes_dd->dma_lch_in.chan),
  1234. dma_chan_name(tdes_dd->dma_lch_out.chan));
  1235. }
  1236. spin_lock(&atmel_tdes.lock);
  1237. list_add_tail(&tdes_dd->list, &atmel_tdes.dev_list);
  1238. spin_unlock(&atmel_tdes.lock);
  1239. err = atmel_tdes_register_algs(tdes_dd);
  1240. if (err)
  1241. goto err_algs;
  1242. dev_info(dev, "Atmel DES/TDES\n");
  1243. return 0;
  1244. err_algs:
  1245. spin_lock(&atmel_tdes.lock);
  1246. list_del(&tdes_dd->list);
  1247. spin_unlock(&atmel_tdes.lock);
  1248. if (tdes_dd->caps.has_dma)
  1249. atmel_tdes_dma_cleanup(tdes_dd);
  1250. err_tdes_dma:
  1251. err_pdata:
  1252. atmel_tdes_buff_cleanup(tdes_dd);
  1253. err_tdes_buff:
  1254. res_err:
  1255. tasklet_kill(&tdes_dd->done_task);
  1256. tasklet_kill(&tdes_dd->queue_task);
  1257. tdes_dd_err:
  1258. dev_err(dev, "initialization failed.\n");
  1259. return err;
  1260. }
  1261. static int atmel_tdes_remove(struct platform_device *pdev)
  1262. {
  1263. static struct atmel_tdes_dev *tdes_dd;
  1264. tdes_dd = platform_get_drvdata(pdev);
  1265. if (!tdes_dd)
  1266. return -ENODEV;
  1267. spin_lock(&atmel_tdes.lock);
  1268. list_del(&tdes_dd->list);
  1269. spin_unlock(&atmel_tdes.lock);
  1270. atmel_tdes_unregister_algs(tdes_dd);
  1271. tasklet_kill(&tdes_dd->done_task);
  1272. tasklet_kill(&tdes_dd->queue_task);
  1273. if (tdes_dd->caps.has_dma)
  1274. atmel_tdes_dma_cleanup(tdes_dd);
  1275. atmel_tdes_buff_cleanup(tdes_dd);
  1276. return 0;
  1277. }
  1278. static struct platform_driver atmel_tdes_driver = {
  1279. .probe = atmel_tdes_probe,
  1280. .remove = atmel_tdes_remove,
  1281. .driver = {
  1282. .name = "atmel_tdes",
  1283. .of_match_table = of_match_ptr(atmel_tdes_dt_ids),
  1284. },
  1285. };
  1286. module_platform_driver(atmel_tdes_driver);
  1287. MODULE_DESCRIPTION("Atmel DES/TDES hw acceleration support.");
  1288. MODULE_LICENSE("GPL v2");
  1289. MODULE_AUTHOR("Nicolas Royer - Eukréa Electromatique");