mv_cesa.c 14 KB

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