mv_cesa.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  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. op.config = CFG_OP_CRYPT_ONLY | CFG_ENCM_AES | CFG_ENC_MODE_CBC;
  158. op.enc_iv = ENC_IV_POINT(SRAM_DATA_IV) |
  159. ENC_IV_BUF_POINT(SRAM_DATA_IV_BUF);
  160. if (first_block)
  161. memcpy(cpg->sram + SRAM_DATA_IV, req->info, 16);
  162. break;
  163. }
  164. if (req_ctx->decrypt) {
  165. op.config |= CFG_DIR_DEC;
  166. memcpy(cpg->sram + SRAM_DATA_KEY_P, ctx->aes_dec_key,
  167. AES_KEY_LEN);
  168. } else {
  169. op.config |= CFG_DIR_ENC;
  170. memcpy(cpg->sram + SRAM_DATA_KEY_P, ctx->aes_enc_key,
  171. AES_KEY_LEN);
  172. }
  173. switch (ctx->key_len) {
  174. case AES_KEYSIZE_128:
  175. op.config |= CFG_AES_LEN_128;
  176. break;
  177. case AES_KEYSIZE_192:
  178. op.config |= CFG_AES_LEN_192;
  179. break;
  180. case AES_KEYSIZE_256:
  181. op.config |= CFG_AES_LEN_256;
  182. break;
  183. }
  184. op.enc_p = ENC_P_SRC(SRAM_DATA_IN_START) |
  185. ENC_P_DST(SRAM_DATA_OUT_START);
  186. op.enc_key_p = SRAM_DATA_KEY_P;
  187. setup_data_in(req);
  188. op.enc_len = cpg->p.crypt_len;
  189. memcpy(cpg->sram + SRAM_CONFIG, &op,
  190. sizeof(struct sec_accel_config));
  191. writel(SRAM_CONFIG, cpg->reg + SEC_ACCEL_DESC_P0);
  192. /* GO */
  193. writel(SEC_CMD_EN_SEC_ACCL0, cpg->reg + SEC_ACCEL_CMD);
  194. /*
  195. * XXX: add timer if the interrupt does not occur for some mystery
  196. * reason
  197. */
  198. }
  199. static void mv_crypto_algo_completion(void)
  200. {
  201. struct ablkcipher_request *req = cpg->cur_req;
  202. struct mv_req_ctx *req_ctx = ablkcipher_request_ctx(req);
  203. if (req_ctx->op != COP_AES_CBC)
  204. return ;
  205. memcpy(req->info, cpg->sram + SRAM_DATA_IV_BUF, 16);
  206. }
  207. static void dequeue_complete_req(void)
  208. {
  209. struct ablkcipher_request *req = cpg->cur_req;
  210. void *buf;
  211. int ret;
  212. cpg->p.total_req_bytes += cpg->p.crypt_len;
  213. do {
  214. int dst_copy;
  215. if (!cpg->p.sg_dst_left) {
  216. ret = sg_miter_next(&cpg->p.dst_sg_it);
  217. BUG_ON(!ret);
  218. cpg->p.sg_dst_left = cpg->p.dst_sg_it.length;
  219. cpg->p.dst_start = 0;
  220. }
  221. buf = cpg->p.dst_sg_it.addr;
  222. buf += cpg->p.dst_start;
  223. dst_copy = min(cpg->p.crypt_len, cpg->p.sg_dst_left);
  224. memcpy(buf, cpg->sram + SRAM_DATA_OUT_START, dst_copy);
  225. cpg->p.sg_dst_left -= dst_copy;
  226. cpg->p.crypt_len -= dst_copy;
  227. cpg->p.dst_start += dst_copy;
  228. } while (cpg->p.crypt_len > 0);
  229. BUG_ON(cpg->eng_st != ENGINE_W_DEQUEUE);
  230. if (cpg->p.total_req_bytes < req->nbytes) {
  231. /* process next scatter list entry */
  232. cpg->eng_st = ENGINE_BUSY;
  233. mv_process_current_q(0);
  234. } else {
  235. sg_miter_stop(&cpg->p.src_sg_it);
  236. sg_miter_stop(&cpg->p.dst_sg_it);
  237. mv_crypto_algo_completion();
  238. cpg->eng_st = ENGINE_IDLE;
  239. req->base.complete(&req->base, 0);
  240. }
  241. }
  242. static int count_sgs(struct scatterlist *sl, unsigned int total_bytes)
  243. {
  244. int i = 0;
  245. do {
  246. total_bytes -= sl[i].length;
  247. i++;
  248. } while (total_bytes > 0);
  249. return i;
  250. }
  251. static void mv_enqueue_new_req(struct ablkcipher_request *req)
  252. {
  253. int num_sgs;
  254. cpg->cur_req = req;
  255. memset(&cpg->p, 0, sizeof(struct req_progress));
  256. num_sgs = count_sgs(req->src, req->nbytes);
  257. sg_miter_start(&cpg->p.src_sg_it, req->src, num_sgs, SG_MITER_FROM_SG);
  258. num_sgs = count_sgs(req->dst, req->nbytes);
  259. sg_miter_start(&cpg->p.dst_sg_it, req->dst, num_sgs, SG_MITER_TO_SG);
  260. mv_process_current_q(1);
  261. }
  262. static int queue_manag(void *data)
  263. {
  264. cpg->eng_st = ENGINE_IDLE;
  265. do {
  266. struct ablkcipher_request *req;
  267. struct crypto_async_request *async_req = NULL;
  268. struct crypto_async_request *backlog;
  269. __set_current_state(TASK_INTERRUPTIBLE);
  270. if (cpg->eng_st == ENGINE_W_DEQUEUE)
  271. dequeue_complete_req();
  272. spin_lock_irq(&cpg->lock);
  273. if (cpg->eng_st == ENGINE_IDLE) {
  274. backlog = crypto_get_backlog(&cpg->queue);
  275. async_req = crypto_dequeue_request(&cpg->queue);
  276. if (async_req) {
  277. BUG_ON(cpg->eng_st != ENGINE_IDLE);
  278. cpg->eng_st = ENGINE_BUSY;
  279. }
  280. }
  281. spin_unlock_irq(&cpg->lock);
  282. if (backlog) {
  283. backlog->complete(backlog, -EINPROGRESS);
  284. backlog = NULL;
  285. }
  286. if (async_req) {
  287. req = container_of(async_req,
  288. struct ablkcipher_request, base);
  289. mv_enqueue_new_req(req);
  290. async_req = NULL;
  291. }
  292. schedule();
  293. } while (!kthread_should_stop());
  294. return 0;
  295. }
  296. static int mv_handle_req(struct ablkcipher_request *req)
  297. {
  298. unsigned long flags;
  299. int ret;
  300. spin_lock_irqsave(&cpg->lock, flags);
  301. ret = ablkcipher_enqueue_request(&cpg->queue, req);
  302. spin_unlock_irqrestore(&cpg->lock, flags);
  303. wake_up_process(cpg->queue_th);
  304. return ret;
  305. }
  306. static int mv_enc_aes_ecb(struct ablkcipher_request *req)
  307. {
  308. struct mv_req_ctx *req_ctx = ablkcipher_request_ctx(req);
  309. req_ctx->op = COP_AES_ECB;
  310. req_ctx->decrypt = 0;
  311. return mv_handle_req(req);
  312. }
  313. static int mv_dec_aes_ecb(struct ablkcipher_request *req)
  314. {
  315. struct mv_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  316. struct mv_req_ctx *req_ctx = ablkcipher_request_ctx(req);
  317. req_ctx->op = COP_AES_ECB;
  318. req_ctx->decrypt = 1;
  319. compute_aes_dec_key(ctx);
  320. return mv_handle_req(req);
  321. }
  322. static int mv_enc_aes_cbc(struct ablkcipher_request *req)
  323. {
  324. struct mv_req_ctx *req_ctx = ablkcipher_request_ctx(req);
  325. req_ctx->op = COP_AES_CBC;
  326. req_ctx->decrypt = 0;
  327. return mv_handle_req(req);
  328. }
  329. static int mv_dec_aes_cbc(struct ablkcipher_request *req)
  330. {
  331. struct mv_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  332. struct mv_req_ctx *req_ctx = ablkcipher_request_ctx(req);
  333. req_ctx->op = COP_AES_CBC;
  334. req_ctx->decrypt = 1;
  335. compute_aes_dec_key(ctx);
  336. return mv_handle_req(req);
  337. }
  338. static int mv_cra_init(struct crypto_tfm *tfm)
  339. {
  340. tfm->crt_ablkcipher.reqsize = sizeof(struct mv_req_ctx);
  341. return 0;
  342. }
  343. irqreturn_t crypto_int(int irq, void *priv)
  344. {
  345. u32 val;
  346. val = readl(cpg->reg + SEC_ACCEL_INT_STATUS);
  347. if (!(val & SEC_INT_ACCEL0_DONE))
  348. return IRQ_NONE;
  349. val &= ~SEC_INT_ACCEL0_DONE;
  350. writel(val, cpg->reg + FPGA_INT_STATUS);
  351. writel(val, cpg->reg + SEC_ACCEL_INT_STATUS);
  352. BUG_ON(cpg->eng_st != ENGINE_BUSY);
  353. cpg->eng_st = ENGINE_W_DEQUEUE;
  354. wake_up_process(cpg->queue_th);
  355. return IRQ_HANDLED;
  356. }
  357. struct crypto_alg mv_aes_alg_ecb = {
  358. .cra_name = "ecb(aes)",
  359. .cra_driver_name = "mv-ecb-aes",
  360. .cra_priority = 300,
  361. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  362. .cra_blocksize = 16,
  363. .cra_ctxsize = sizeof(struct mv_ctx),
  364. .cra_alignmask = 0,
  365. .cra_type = &crypto_ablkcipher_type,
  366. .cra_module = THIS_MODULE,
  367. .cra_init = mv_cra_init,
  368. .cra_u = {
  369. .ablkcipher = {
  370. .min_keysize = AES_MIN_KEY_SIZE,
  371. .max_keysize = AES_MAX_KEY_SIZE,
  372. .setkey = mv_setkey_aes,
  373. .encrypt = mv_enc_aes_ecb,
  374. .decrypt = mv_dec_aes_ecb,
  375. },
  376. },
  377. };
  378. struct crypto_alg mv_aes_alg_cbc = {
  379. .cra_name = "cbc(aes)",
  380. .cra_driver_name = "mv-cbc-aes",
  381. .cra_priority = 300,
  382. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  383. .cra_blocksize = AES_BLOCK_SIZE,
  384. .cra_ctxsize = sizeof(struct mv_ctx),
  385. .cra_alignmask = 0,
  386. .cra_type = &crypto_ablkcipher_type,
  387. .cra_module = THIS_MODULE,
  388. .cra_init = mv_cra_init,
  389. .cra_u = {
  390. .ablkcipher = {
  391. .ivsize = AES_BLOCK_SIZE,
  392. .min_keysize = AES_MIN_KEY_SIZE,
  393. .max_keysize = AES_MAX_KEY_SIZE,
  394. .setkey = mv_setkey_aes,
  395. .encrypt = mv_enc_aes_cbc,
  396. .decrypt = mv_dec_aes_cbc,
  397. },
  398. },
  399. };
  400. static int mv_probe(struct platform_device *pdev)
  401. {
  402. struct crypto_priv *cp;
  403. struct resource *res;
  404. int irq;
  405. int ret;
  406. if (cpg) {
  407. printk(KERN_ERR "Second crypto dev?\n");
  408. return -EEXIST;
  409. }
  410. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "regs");
  411. if (!res)
  412. return -ENXIO;
  413. cp = kzalloc(sizeof(*cp), GFP_KERNEL);
  414. if (!cp)
  415. return -ENOMEM;
  416. spin_lock_init(&cp->lock);
  417. crypto_init_queue(&cp->queue, 50);
  418. cp->reg = ioremap(res->start, res->end - res->start + 1);
  419. if (!cp->reg) {
  420. ret = -ENOMEM;
  421. goto err;
  422. }
  423. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "sram");
  424. if (!res) {
  425. ret = -ENXIO;
  426. goto err_unmap_reg;
  427. }
  428. cp->sram_size = res->end - res->start + 1;
  429. cp->max_req_size = cp->sram_size - SRAM_CFG_SPACE;
  430. cp->sram = ioremap(res->start, cp->sram_size);
  431. if (!cp->sram) {
  432. ret = -ENOMEM;
  433. goto err_unmap_reg;
  434. }
  435. irq = platform_get_irq(pdev, 0);
  436. if (irq < 0 || irq == NO_IRQ) {
  437. ret = irq;
  438. goto err_unmap_sram;
  439. }
  440. cp->irq = irq;
  441. platform_set_drvdata(pdev, cp);
  442. cpg = cp;
  443. cp->queue_th = kthread_run(queue_manag, cp, "mv_crypto");
  444. if (IS_ERR(cp->queue_th)) {
  445. ret = PTR_ERR(cp->queue_th);
  446. goto err_thread;
  447. }
  448. ret = request_irq(irq, crypto_int, IRQF_DISABLED, dev_name(&pdev->dev),
  449. cp);
  450. if (ret)
  451. goto err_unmap_sram;
  452. writel(SEC_INT_ACCEL0_DONE, cpg->reg + SEC_ACCEL_INT_MASK);
  453. writel(SEC_CFG_STOP_DIG_ERR, cpg->reg + SEC_ACCEL_CFG);
  454. ret = crypto_register_alg(&mv_aes_alg_ecb);
  455. if (ret)
  456. goto err_reg;
  457. ret = crypto_register_alg(&mv_aes_alg_cbc);
  458. if (ret)
  459. goto err_unreg_ecb;
  460. return 0;
  461. err_unreg_ecb:
  462. crypto_unregister_alg(&mv_aes_alg_ecb);
  463. err_thread:
  464. free_irq(irq, cp);
  465. err_reg:
  466. kthread_stop(cp->queue_th);
  467. err_unmap_sram:
  468. iounmap(cp->sram);
  469. err_unmap_reg:
  470. iounmap(cp->reg);
  471. err:
  472. kfree(cp);
  473. cpg = NULL;
  474. platform_set_drvdata(pdev, NULL);
  475. return ret;
  476. }
  477. static int mv_remove(struct platform_device *pdev)
  478. {
  479. struct crypto_priv *cp = platform_get_drvdata(pdev);
  480. crypto_unregister_alg(&mv_aes_alg_ecb);
  481. crypto_unregister_alg(&mv_aes_alg_cbc);
  482. kthread_stop(cp->queue_th);
  483. free_irq(cp->irq, cp);
  484. memset(cp->sram, 0, cp->sram_size);
  485. iounmap(cp->sram);
  486. iounmap(cp->reg);
  487. kfree(cp);
  488. cpg = NULL;
  489. return 0;
  490. }
  491. static struct platform_driver marvell_crypto = {
  492. .probe = mv_probe,
  493. .remove = mv_remove,
  494. .driver = {
  495. .owner = THIS_MODULE,
  496. .name = "mv_crypto",
  497. },
  498. };
  499. MODULE_ALIAS("platform:mv_crypto");
  500. static int __init mv_crypto_init(void)
  501. {
  502. return platform_driver_register(&marvell_crypto);
  503. }
  504. module_init(mv_crypto_init);
  505. static void __exit mv_crypto_exit(void)
  506. {
  507. platform_driver_unregister(&marvell_crypto);
  508. }
  509. module_exit(mv_crypto_exit);
  510. MODULE_AUTHOR("Sebastian Andrzej Siewior <sebastian@breakpoint.cc>");
  511. MODULE_DESCRIPTION("Support for Marvell's cryptographic engine");
  512. MODULE_LICENSE("GPL");