mv_cesa.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. /*
  2. * Support for Marvell's crypto engine which can be found on some Orion5X
  3. * boards.
  4. *
  5. * Author: Sebastian Andrzej Siewior < sebastian at breakpoint dot cc >
  6. * License: GPLv2
  7. *
  8. */
  9. #include <crypto/aes.h>
  10. #include <crypto/algapi.h>
  11. #include <linux/crypto.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/io.h>
  14. #include <linux/kthread.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/scatterlist.h>
  17. #include <linux/slab.h>
  18. #include "mv_cesa.h"
  19. /*
  20. * STM:
  21. * /---------------------------------------\
  22. * | | request complete
  23. * \./ |
  24. * IDLE -> new request -> BUSY -> done -> DEQUEUE
  25. * /°\ |
  26. * | | more scatter entries
  27. * \________________/
  28. */
  29. enum engine_status {
  30. ENGINE_IDLE,
  31. ENGINE_BUSY,
  32. ENGINE_W_DEQUEUE,
  33. };
  34. /**
  35. * struct req_progress - used for every crypt request
  36. * @src_sg_it: sg iterator for src
  37. * @dst_sg_it: sg iterator for dst
  38. * @sg_src_left: bytes left in src to process (scatter list)
  39. * @src_start: offset to add to src start position (scatter list)
  40. * @crypt_len: length of current crypt process
  41. * @sg_dst_left: bytes left dst to process in this scatter list
  42. * @dst_start: offset to add to dst start position (scatter list)
  43. * @total_req_bytes: total number of bytes processed (request).
  44. *
  45. * sg helper are used to iterate over the scatterlist. Since the size of the
  46. * SRAM may be less than the scatter size, this struct struct is used to keep
  47. * track of progress within current scatterlist.
  48. */
  49. struct req_progress {
  50. struct sg_mapping_iter src_sg_it;
  51. struct sg_mapping_iter dst_sg_it;
  52. /* src mostly */
  53. int sg_src_left;
  54. int src_start;
  55. int crypt_len;
  56. /* dst mostly */
  57. int sg_dst_left;
  58. int dst_start;
  59. int total_req_bytes;
  60. };
  61. struct crypto_priv {
  62. void __iomem *reg;
  63. void __iomem *sram;
  64. int irq;
  65. struct task_struct *queue_th;
  66. /* the lock protects queue and eng_st */
  67. spinlock_t lock;
  68. struct crypto_queue queue;
  69. enum engine_status eng_st;
  70. struct ablkcipher_request *cur_req;
  71. struct req_progress p;
  72. int max_req_size;
  73. int sram_size;
  74. };
  75. static struct crypto_priv *cpg;
  76. struct mv_ctx {
  77. u8 aes_enc_key[AES_KEY_LEN];
  78. u32 aes_dec_key[8];
  79. int key_len;
  80. u32 need_calc_aes_dkey;
  81. };
  82. enum crypto_op {
  83. COP_AES_ECB,
  84. COP_AES_CBC,
  85. };
  86. struct mv_req_ctx {
  87. enum crypto_op op;
  88. int decrypt;
  89. };
  90. static void compute_aes_dec_key(struct mv_ctx *ctx)
  91. {
  92. struct crypto_aes_ctx gen_aes_key;
  93. int key_pos;
  94. if (!ctx->need_calc_aes_dkey)
  95. return;
  96. crypto_aes_expand_key(&gen_aes_key, ctx->aes_enc_key, ctx->key_len);
  97. key_pos = ctx->key_len + 24;
  98. memcpy(ctx->aes_dec_key, &gen_aes_key.key_enc[key_pos], 4 * 4);
  99. switch (ctx->key_len) {
  100. case AES_KEYSIZE_256:
  101. key_pos -= 2;
  102. /* fall */
  103. case AES_KEYSIZE_192:
  104. key_pos -= 2;
  105. memcpy(&ctx->aes_dec_key[4], &gen_aes_key.key_enc[key_pos],
  106. 4 * 4);
  107. break;
  108. }
  109. ctx->need_calc_aes_dkey = 0;
  110. }
  111. static int mv_setkey_aes(struct crypto_ablkcipher *cipher, const u8 *key,
  112. unsigned int len)
  113. {
  114. struct crypto_tfm *tfm = crypto_ablkcipher_tfm(cipher);
  115. struct mv_ctx *ctx = crypto_tfm_ctx(tfm);
  116. switch (len) {
  117. case AES_KEYSIZE_128:
  118. case AES_KEYSIZE_192:
  119. case AES_KEYSIZE_256:
  120. break;
  121. default:
  122. crypto_ablkcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
  123. return -EINVAL;
  124. }
  125. ctx->key_len = len;
  126. ctx->need_calc_aes_dkey = 1;
  127. memcpy(ctx->aes_enc_key, key, AES_KEY_LEN);
  128. return 0;
  129. }
  130. static void setup_data_in(struct ablkcipher_request *req)
  131. {
  132. int ret;
  133. void *buf;
  134. if (!cpg->p.sg_src_left) {
  135. ret = sg_miter_next(&cpg->p.src_sg_it);
  136. BUG_ON(!ret);
  137. cpg->p.sg_src_left = cpg->p.src_sg_it.length;
  138. cpg->p.src_start = 0;
  139. }
  140. cpg->p.crypt_len = min(cpg->p.sg_src_left, cpg->max_req_size);
  141. buf = cpg->p.src_sg_it.addr;
  142. buf += cpg->p.src_start;
  143. memcpy(cpg->sram + SRAM_DATA_IN_START, buf, cpg->p.crypt_len);
  144. cpg->p.sg_src_left -= cpg->p.crypt_len;
  145. cpg->p.src_start += cpg->p.crypt_len;
  146. }
  147. static void mv_process_current_q(int first_block)
  148. {
  149. struct ablkcipher_request *req = cpg->cur_req;
  150. struct mv_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  151. struct mv_req_ctx *req_ctx = ablkcipher_request_ctx(req);
  152. struct sec_accel_config op;
  153. switch (req_ctx->op) {
  154. case COP_AES_ECB:
  155. op.config = CFG_OP_CRYPT_ONLY | CFG_ENCM_AES | CFG_ENC_MODE_ECB;
  156. break;
  157. case COP_AES_CBC:
  158. op.config = CFG_OP_CRYPT_ONLY | CFG_ENCM_AES | CFG_ENC_MODE_CBC;
  159. op.enc_iv = ENC_IV_POINT(SRAM_DATA_IV) |
  160. ENC_IV_BUF_POINT(SRAM_DATA_IV_BUF);
  161. if (first_block)
  162. memcpy(cpg->sram + SRAM_DATA_IV, req->info, 16);
  163. break;
  164. }
  165. if (req_ctx->decrypt) {
  166. op.config |= CFG_DIR_DEC;
  167. memcpy(cpg->sram + SRAM_DATA_KEY_P, ctx->aes_dec_key,
  168. AES_KEY_LEN);
  169. } else {
  170. op.config |= CFG_DIR_ENC;
  171. memcpy(cpg->sram + SRAM_DATA_KEY_P, ctx->aes_enc_key,
  172. AES_KEY_LEN);
  173. }
  174. switch (ctx->key_len) {
  175. case AES_KEYSIZE_128:
  176. op.config |= CFG_AES_LEN_128;
  177. break;
  178. case AES_KEYSIZE_192:
  179. op.config |= CFG_AES_LEN_192;
  180. break;
  181. case AES_KEYSIZE_256:
  182. op.config |= CFG_AES_LEN_256;
  183. break;
  184. }
  185. op.enc_p = ENC_P_SRC(SRAM_DATA_IN_START) |
  186. ENC_P_DST(SRAM_DATA_OUT_START);
  187. op.enc_key_p = SRAM_DATA_KEY_P;
  188. setup_data_in(req);
  189. op.enc_len = cpg->p.crypt_len;
  190. memcpy(cpg->sram + SRAM_CONFIG, &op,
  191. sizeof(struct sec_accel_config));
  192. writel(SRAM_CONFIG, cpg->reg + SEC_ACCEL_DESC_P0);
  193. /* GO */
  194. writel(SEC_CMD_EN_SEC_ACCL0, cpg->reg + SEC_ACCEL_CMD);
  195. /*
  196. * XXX: add timer if the interrupt does not occur for some mystery
  197. * reason
  198. */
  199. }
  200. static void mv_crypto_algo_completion(void)
  201. {
  202. struct ablkcipher_request *req = cpg->cur_req;
  203. struct mv_req_ctx *req_ctx = ablkcipher_request_ctx(req);
  204. if (req_ctx->op != COP_AES_CBC)
  205. return ;
  206. memcpy(req->info, cpg->sram + SRAM_DATA_IV_BUF, 16);
  207. }
  208. static void dequeue_complete_req(void)
  209. {
  210. struct ablkcipher_request *req = cpg->cur_req;
  211. void *buf;
  212. int ret;
  213. cpg->p.total_req_bytes += cpg->p.crypt_len;
  214. do {
  215. int dst_copy;
  216. if (!cpg->p.sg_dst_left) {
  217. ret = sg_miter_next(&cpg->p.dst_sg_it);
  218. BUG_ON(!ret);
  219. cpg->p.sg_dst_left = cpg->p.dst_sg_it.length;
  220. cpg->p.dst_start = 0;
  221. }
  222. buf = cpg->p.dst_sg_it.addr;
  223. buf += cpg->p.dst_start;
  224. dst_copy = min(cpg->p.crypt_len, cpg->p.sg_dst_left);
  225. memcpy(buf, cpg->sram + SRAM_DATA_OUT_START, dst_copy);
  226. cpg->p.sg_dst_left -= dst_copy;
  227. cpg->p.crypt_len -= dst_copy;
  228. cpg->p.dst_start += dst_copy;
  229. } while (cpg->p.crypt_len > 0);
  230. BUG_ON(cpg->eng_st != ENGINE_W_DEQUEUE);
  231. if (cpg->p.total_req_bytes < req->nbytes) {
  232. /* process next scatter list entry */
  233. cpg->eng_st = ENGINE_BUSY;
  234. mv_process_current_q(0);
  235. } else {
  236. sg_miter_stop(&cpg->p.src_sg_it);
  237. sg_miter_stop(&cpg->p.dst_sg_it);
  238. mv_crypto_algo_completion();
  239. cpg->eng_st = ENGINE_IDLE;
  240. req->base.complete(&req->base, 0);
  241. }
  242. }
  243. static int count_sgs(struct scatterlist *sl, unsigned int total_bytes)
  244. {
  245. int i = 0;
  246. do {
  247. total_bytes -= sl[i].length;
  248. i++;
  249. } while (total_bytes > 0);
  250. return i;
  251. }
  252. static void mv_enqueue_new_req(struct ablkcipher_request *req)
  253. {
  254. int num_sgs;
  255. cpg->cur_req = req;
  256. memset(&cpg->p, 0, sizeof(struct req_progress));
  257. num_sgs = count_sgs(req->src, req->nbytes);
  258. sg_miter_start(&cpg->p.src_sg_it, req->src, num_sgs, SG_MITER_FROM_SG);
  259. num_sgs = count_sgs(req->dst, req->nbytes);
  260. sg_miter_start(&cpg->p.dst_sg_it, req->dst, num_sgs, SG_MITER_TO_SG);
  261. mv_process_current_q(1);
  262. }
  263. static int queue_manag(void *data)
  264. {
  265. cpg->eng_st = ENGINE_IDLE;
  266. do {
  267. struct ablkcipher_request *req;
  268. struct crypto_async_request *async_req = NULL;
  269. struct crypto_async_request *backlog;
  270. __set_current_state(TASK_INTERRUPTIBLE);
  271. if (cpg->eng_st == ENGINE_W_DEQUEUE)
  272. dequeue_complete_req();
  273. spin_lock_irq(&cpg->lock);
  274. if (cpg->eng_st == ENGINE_IDLE) {
  275. backlog = crypto_get_backlog(&cpg->queue);
  276. async_req = crypto_dequeue_request(&cpg->queue);
  277. if (async_req) {
  278. BUG_ON(cpg->eng_st != ENGINE_IDLE);
  279. cpg->eng_st = ENGINE_BUSY;
  280. }
  281. }
  282. spin_unlock_irq(&cpg->lock);
  283. if (backlog) {
  284. backlog->complete(backlog, -EINPROGRESS);
  285. backlog = NULL;
  286. }
  287. if (async_req) {
  288. req = container_of(async_req,
  289. struct ablkcipher_request, base);
  290. mv_enqueue_new_req(req);
  291. async_req = NULL;
  292. }
  293. schedule();
  294. } while (!kthread_should_stop());
  295. return 0;
  296. }
  297. static int mv_handle_req(struct ablkcipher_request *req)
  298. {
  299. unsigned long flags;
  300. int ret;
  301. spin_lock_irqsave(&cpg->lock, flags);
  302. ret = ablkcipher_enqueue_request(&cpg->queue, req);
  303. spin_unlock_irqrestore(&cpg->lock, flags);
  304. wake_up_process(cpg->queue_th);
  305. return ret;
  306. }
  307. static int mv_enc_aes_ecb(struct ablkcipher_request *req)
  308. {
  309. struct mv_req_ctx *req_ctx = ablkcipher_request_ctx(req);
  310. req_ctx->op = COP_AES_ECB;
  311. req_ctx->decrypt = 0;
  312. return mv_handle_req(req);
  313. }
  314. static int mv_dec_aes_ecb(struct ablkcipher_request *req)
  315. {
  316. struct mv_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  317. struct mv_req_ctx *req_ctx = ablkcipher_request_ctx(req);
  318. req_ctx->op = COP_AES_ECB;
  319. req_ctx->decrypt = 1;
  320. compute_aes_dec_key(ctx);
  321. return mv_handle_req(req);
  322. }
  323. static int mv_enc_aes_cbc(struct ablkcipher_request *req)
  324. {
  325. struct mv_req_ctx *req_ctx = ablkcipher_request_ctx(req);
  326. req_ctx->op = COP_AES_CBC;
  327. req_ctx->decrypt = 0;
  328. return mv_handle_req(req);
  329. }
  330. static int mv_dec_aes_cbc(struct ablkcipher_request *req)
  331. {
  332. struct mv_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  333. struct mv_req_ctx *req_ctx = ablkcipher_request_ctx(req);
  334. req_ctx->op = COP_AES_CBC;
  335. req_ctx->decrypt = 1;
  336. compute_aes_dec_key(ctx);
  337. return mv_handle_req(req);
  338. }
  339. static int mv_cra_init(struct crypto_tfm *tfm)
  340. {
  341. tfm->crt_ablkcipher.reqsize = sizeof(struct mv_req_ctx);
  342. return 0;
  343. }
  344. irqreturn_t crypto_int(int irq, void *priv)
  345. {
  346. u32 val;
  347. val = readl(cpg->reg + SEC_ACCEL_INT_STATUS);
  348. if (!(val & SEC_INT_ACCEL0_DONE))
  349. return IRQ_NONE;
  350. val &= ~SEC_INT_ACCEL0_DONE;
  351. writel(val, cpg->reg + FPGA_INT_STATUS);
  352. writel(val, cpg->reg + SEC_ACCEL_INT_STATUS);
  353. BUG_ON(cpg->eng_st != ENGINE_BUSY);
  354. cpg->eng_st = ENGINE_W_DEQUEUE;
  355. wake_up_process(cpg->queue_th);
  356. return IRQ_HANDLED;
  357. }
  358. struct crypto_alg mv_aes_alg_ecb = {
  359. .cra_name = "ecb(aes)",
  360. .cra_driver_name = "mv-ecb-aes",
  361. .cra_priority = 300,
  362. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  363. .cra_blocksize = 16,
  364. .cra_ctxsize = sizeof(struct mv_ctx),
  365. .cra_alignmask = 0,
  366. .cra_type = &crypto_ablkcipher_type,
  367. .cra_module = THIS_MODULE,
  368. .cra_init = mv_cra_init,
  369. .cra_u = {
  370. .ablkcipher = {
  371. .min_keysize = AES_MIN_KEY_SIZE,
  372. .max_keysize = AES_MAX_KEY_SIZE,
  373. .setkey = mv_setkey_aes,
  374. .encrypt = mv_enc_aes_ecb,
  375. .decrypt = mv_dec_aes_ecb,
  376. },
  377. },
  378. };
  379. struct crypto_alg mv_aes_alg_cbc = {
  380. .cra_name = "cbc(aes)",
  381. .cra_driver_name = "mv-cbc-aes",
  382. .cra_priority = 300,
  383. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  384. .cra_blocksize = AES_BLOCK_SIZE,
  385. .cra_ctxsize = sizeof(struct mv_ctx),
  386. .cra_alignmask = 0,
  387. .cra_type = &crypto_ablkcipher_type,
  388. .cra_module = THIS_MODULE,
  389. .cra_init = mv_cra_init,
  390. .cra_u = {
  391. .ablkcipher = {
  392. .ivsize = AES_BLOCK_SIZE,
  393. .min_keysize = AES_MIN_KEY_SIZE,
  394. .max_keysize = AES_MAX_KEY_SIZE,
  395. .setkey = mv_setkey_aes,
  396. .encrypt = mv_enc_aes_cbc,
  397. .decrypt = mv_dec_aes_cbc,
  398. },
  399. },
  400. };
  401. static int mv_probe(struct platform_device *pdev)
  402. {
  403. struct crypto_priv *cp;
  404. struct resource *res;
  405. int irq;
  406. int ret;
  407. if (cpg) {
  408. printk(KERN_ERR "Second crypto dev?\n");
  409. return -EEXIST;
  410. }
  411. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "regs");
  412. if (!res)
  413. return -ENXIO;
  414. cp = kzalloc(sizeof(*cp), GFP_KERNEL);
  415. if (!cp)
  416. return -ENOMEM;
  417. spin_lock_init(&cp->lock);
  418. crypto_init_queue(&cp->queue, 50);
  419. cp->reg = ioremap(res->start, res->end - res->start + 1);
  420. if (!cp->reg) {
  421. ret = -ENOMEM;
  422. goto err;
  423. }
  424. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "sram");
  425. if (!res) {
  426. ret = -ENXIO;
  427. goto err_unmap_reg;
  428. }
  429. cp->sram_size = res->end - res->start + 1;
  430. cp->max_req_size = cp->sram_size - SRAM_CFG_SPACE;
  431. cp->sram = ioremap(res->start, cp->sram_size);
  432. if (!cp->sram) {
  433. ret = -ENOMEM;
  434. goto err_unmap_reg;
  435. }
  436. irq = platform_get_irq(pdev, 0);
  437. if (irq < 0 || irq == NO_IRQ) {
  438. ret = irq;
  439. goto err_unmap_sram;
  440. }
  441. cp->irq = irq;
  442. platform_set_drvdata(pdev, cp);
  443. cpg = cp;
  444. cp->queue_th = kthread_run(queue_manag, cp, "mv_crypto");
  445. if (IS_ERR(cp->queue_th)) {
  446. ret = PTR_ERR(cp->queue_th);
  447. goto err_thread;
  448. }
  449. ret = request_irq(irq, crypto_int, IRQF_DISABLED, dev_name(&pdev->dev),
  450. cp);
  451. if (ret)
  452. goto err_unmap_sram;
  453. writel(SEC_INT_ACCEL0_DONE, cpg->reg + SEC_ACCEL_INT_MASK);
  454. writel(SEC_CFG_STOP_DIG_ERR, cpg->reg + SEC_ACCEL_CFG);
  455. ret = crypto_register_alg(&mv_aes_alg_ecb);
  456. if (ret)
  457. goto err_reg;
  458. ret = crypto_register_alg(&mv_aes_alg_cbc);
  459. if (ret)
  460. goto err_unreg_ecb;
  461. return 0;
  462. err_unreg_ecb:
  463. crypto_unregister_alg(&mv_aes_alg_ecb);
  464. err_thread:
  465. free_irq(irq, cp);
  466. err_reg:
  467. kthread_stop(cp->queue_th);
  468. err_unmap_sram:
  469. iounmap(cp->sram);
  470. err_unmap_reg:
  471. iounmap(cp->reg);
  472. err:
  473. kfree(cp);
  474. cpg = NULL;
  475. platform_set_drvdata(pdev, NULL);
  476. return ret;
  477. }
  478. static int mv_remove(struct platform_device *pdev)
  479. {
  480. struct crypto_priv *cp = platform_get_drvdata(pdev);
  481. crypto_unregister_alg(&mv_aes_alg_ecb);
  482. crypto_unregister_alg(&mv_aes_alg_cbc);
  483. kthread_stop(cp->queue_th);
  484. free_irq(cp->irq, cp);
  485. memset(cp->sram, 0, cp->sram_size);
  486. iounmap(cp->sram);
  487. iounmap(cp->reg);
  488. kfree(cp);
  489. cpg = NULL;
  490. return 0;
  491. }
  492. static struct platform_driver marvell_crypto = {
  493. .probe = mv_probe,
  494. .remove = mv_remove,
  495. .driver = {
  496. .owner = THIS_MODULE,
  497. .name = "mv_crypto",
  498. },
  499. };
  500. MODULE_ALIAS("platform:mv_crypto");
  501. static int __init mv_crypto_init(void)
  502. {
  503. return platform_driver_register(&marvell_crypto);
  504. }
  505. module_init(mv_crypto_init);
  506. static void __exit mv_crypto_exit(void)
  507. {
  508. platform_driver_unregister(&marvell_crypto);
  509. }
  510. module_exit(mv_crypto_exit);
  511. MODULE_AUTHOR("Sebastian Andrzej Siewior <sebastian@breakpoint.cc>");
  512. MODULE_DESCRIPTION("Support for Marvell's cryptographic engine");
  513. MODULE_LICENSE("GPL");