mv_cesa.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153
  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 <crypto/internal/hash.h>
  19. #include <crypto/sha.h>
  20. #include "mv_cesa.h"
  21. #define MV_CESA "MV-CESA:"
  22. #define MAX_HW_HASH_SIZE 0xFFFF
  23. /*
  24. * STM:
  25. * /---------------------------------------\
  26. * | | request complete
  27. * \./ |
  28. * IDLE -> new request -> BUSY -> done -> DEQUEUE
  29. * /°\ |
  30. * | | more scatter entries
  31. * \________________/
  32. */
  33. enum engine_status {
  34. ENGINE_IDLE,
  35. ENGINE_BUSY,
  36. ENGINE_W_DEQUEUE,
  37. };
  38. /**
  39. * struct req_progress - used for every crypt request
  40. * @src_sg_it: sg iterator for src
  41. * @dst_sg_it: sg iterator for dst
  42. * @sg_src_left: bytes left in src to process (scatter list)
  43. * @src_start: offset to add to src start position (scatter list)
  44. * @crypt_len: length of current hw crypt/hash process
  45. * @hw_nbytes: total bytes to process in hw for this request
  46. * @copy_back: whether to copy data back (crypt) or not (hash)
  47. * @sg_dst_left: bytes left dst to process in this scatter list
  48. * @dst_start: offset to add to dst start position (scatter list)
  49. * @hw_processed_bytes: number of bytes processed by hw (request).
  50. *
  51. * sg helper are used to iterate over the scatterlist. Since the size of the
  52. * SRAM may be less than the scatter size, this struct struct is used to keep
  53. * track of progress within current scatterlist.
  54. */
  55. struct req_progress {
  56. struct sg_mapping_iter src_sg_it;
  57. struct sg_mapping_iter dst_sg_it;
  58. void (*complete) (void);
  59. void (*process) (int is_first);
  60. /* src mostly */
  61. int sg_src_left;
  62. int src_start;
  63. int crypt_len;
  64. int hw_nbytes;
  65. /* dst mostly */
  66. int copy_back;
  67. int sg_dst_left;
  68. int dst_start;
  69. int hw_processed_bytes;
  70. };
  71. struct crypto_priv {
  72. void __iomem *reg;
  73. void __iomem *sram;
  74. int irq;
  75. struct task_struct *queue_th;
  76. /* the lock protects queue and eng_st */
  77. spinlock_t lock;
  78. struct crypto_queue queue;
  79. enum engine_status eng_st;
  80. struct crypto_async_request *cur_req;
  81. struct req_progress p;
  82. int max_req_size;
  83. int sram_size;
  84. int has_sha1;
  85. int has_hmac_sha1;
  86. };
  87. static struct crypto_priv *cpg;
  88. struct mv_ctx {
  89. u8 aes_enc_key[AES_KEY_LEN];
  90. u32 aes_dec_key[8];
  91. int key_len;
  92. u32 need_calc_aes_dkey;
  93. };
  94. enum crypto_op {
  95. COP_AES_ECB,
  96. COP_AES_CBC,
  97. };
  98. struct mv_req_ctx {
  99. enum crypto_op op;
  100. int decrypt;
  101. };
  102. enum hash_op {
  103. COP_SHA1,
  104. COP_HMAC_SHA1
  105. };
  106. struct mv_tfm_hash_ctx {
  107. struct crypto_shash *fallback;
  108. struct crypto_shash *base_hash;
  109. u32 ivs[2 * SHA1_DIGEST_SIZE / 4];
  110. int count_add;
  111. enum hash_op op;
  112. };
  113. struct mv_req_hash_ctx {
  114. u64 count;
  115. u32 state[SHA1_DIGEST_SIZE / 4];
  116. u8 buffer[SHA1_BLOCK_SIZE];
  117. int first_hash; /* marks that we don't have previous state */
  118. int last_chunk; /* marks that this is the 'final' request */
  119. int extra_bytes; /* unprocessed bytes in buffer */
  120. enum hash_op op;
  121. int count_add;
  122. struct scatterlist dummysg;
  123. };
  124. static void compute_aes_dec_key(struct mv_ctx *ctx)
  125. {
  126. struct crypto_aes_ctx gen_aes_key;
  127. int key_pos;
  128. if (!ctx->need_calc_aes_dkey)
  129. return;
  130. crypto_aes_expand_key(&gen_aes_key, ctx->aes_enc_key, ctx->key_len);
  131. key_pos = ctx->key_len + 24;
  132. memcpy(ctx->aes_dec_key, &gen_aes_key.key_enc[key_pos], 4 * 4);
  133. switch (ctx->key_len) {
  134. case AES_KEYSIZE_256:
  135. key_pos -= 2;
  136. /* fall */
  137. case AES_KEYSIZE_192:
  138. key_pos -= 2;
  139. memcpy(&ctx->aes_dec_key[4], &gen_aes_key.key_enc[key_pos],
  140. 4 * 4);
  141. break;
  142. }
  143. ctx->need_calc_aes_dkey = 0;
  144. }
  145. static int mv_setkey_aes(struct crypto_ablkcipher *cipher, const u8 *key,
  146. unsigned int len)
  147. {
  148. struct crypto_tfm *tfm = crypto_ablkcipher_tfm(cipher);
  149. struct mv_ctx *ctx = crypto_tfm_ctx(tfm);
  150. switch (len) {
  151. case AES_KEYSIZE_128:
  152. case AES_KEYSIZE_192:
  153. case AES_KEYSIZE_256:
  154. break;
  155. default:
  156. crypto_ablkcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
  157. return -EINVAL;
  158. }
  159. ctx->key_len = len;
  160. ctx->need_calc_aes_dkey = 1;
  161. memcpy(ctx->aes_enc_key, key, AES_KEY_LEN);
  162. return 0;
  163. }
  164. static void copy_src_to_buf(struct req_progress *p, char *dbuf, int len)
  165. {
  166. int ret;
  167. void *sbuf;
  168. int copied = 0;
  169. while (1) {
  170. if (!p->sg_src_left) {
  171. ret = sg_miter_next(&p->src_sg_it);
  172. BUG_ON(!ret);
  173. p->sg_src_left = p->src_sg_it.length;
  174. p->src_start = 0;
  175. }
  176. sbuf = p->src_sg_it.addr + p->src_start;
  177. if (p->sg_src_left <= len - copied) {
  178. memcpy(dbuf + copied, sbuf, p->sg_src_left);
  179. copied += p->sg_src_left;
  180. p->sg_src_left = 0;
  181. if (copied >= len)
  182. break;
  183. } else {
  184. int copy_len = len - copied;
  185. memcpy(dbuf + copied, sbuf, copy_len);
  186. p->src_start += copy_len;
  187. p->sg_src_left -= copy_len;
  188. break;
  189. }
  190. }
  191. }
  192. static void setup_data_in(void)
  193. {
  194. struct req_progress *p = &cpg->p;
  195. int data_in_sram =
  196. min(p->hw_nbytes - p->hw_processed_bytes, cpg->max_req_size);
  197. copy_src_to_buf(p, cpg->sram + SRAM_DATA_IN_START + p->crypt_len,
  198. data_in_sram - p->crypt_len);
  199. p->crypt_len = data_in_sram;
  200. }
  201. static void mv_process_current_q(int first_block)
  202. {
  203. struct ablkcipher_request *req = ablkcipher_request_cast(cpg->cur_req);
  204. struct mv_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  205. struct mv_req_ctx *req_ctx = ablkcipher_request_ctx(req);
  206. struct sec_accel_config op;
  207. switch (req_ctx->op) {
  208. case COP_AES_ECB:
  209. op.config = CFG_OP_CRYPT_ONLY | CFG_ENCM_AES | CFG_ENC_MODE_ECB;
  210. break;
  211. case COP_AES_CBC:
  212. default:
  213. op.config = CFG_OP_CRYPT_ONLY | CFG_ENCM_AES | CFG_ENC_MODE_CBC;
  214. op.enc_iv = ENC_IV_POINT(SRAM_DATA_IV) |
  215. ENC_IV_BUF_POINT(SRAM_DATA_IV_BUF);
  216. if (first_block)
  217. memcpy(cpg->sram + SRAM_DATA_IV, req->info, 16);
  218. break;
  219. }
  220. if (req_ctx->decrypt) {
  221. op.config |= CFG_DIR_DEC;
  222. memcpy(cpg->sram + SRAM_DATA_KEY_P, ctx->aes_dec_key,
  223. AES_KEY_LEN);
  224. } else {
  225. op.config |= CFG_DIR_ENC;
  226. memcpy(cpg->sram + SRAM_DATA_KEY_P, ctx->aes_enc_key,
  227. AES_KEY_LEN);
  228. }
  229. switch (ctx->key_len) {
  230. case AES_KEYSIZE_128:
  231. op.config |= CFG_AES_LEN_128;
  232. break;
  233. case AES_KEYSIZE_192:
  234. op.config |= CFG_AES_LEN_192;
  235. break;
  236. case AES_KEYSIZE_256:
  237. op.config |= CFG_AES_LEN_256;
  238. break;
  239. }
  240. op.enc_p = ENC_P_SRC(SRAM_DATA_IN_START) |
  241. ENC_P_DST(SRAM_DATA_OUT_START);
  242. op.enc_key_p = SRAM_DATA_KEY_P;
  243. setup_data_in();
  244. op.enc_len = cpg->p.crypt_len;
  245. memcpy(cpg->sram + SRAM_CONFIG, &op,
  246. sizeof(struct sec_accel_config));
  247. writel(SRAM_CONFIG, cpg->reg + SEC_ACCEL_DESC_P0);
  248. /* GO */
  249. writel(SEC_CMD_EN_SEC_ACCL0, cpg->reg + SEC_ACCEL_CMD);
  250. /*
  251. * XXX: add timer if the interrupt does not occur for some mystery
  252. * reason
  253. */
  254. }
  255. static void mv_crypto_algo_completion(void)
  256. {
  257. struct ablkcipher_request *req = ablkcipher_request_cast(cpg->cur_req);
  258. struct mv_req_ctx *req_ctx = ablkcipher_request_ctx(req);
  259. sg_miter_stop(&cpg->p.src_sg_it);
  260. sg_miter_stop(&cpg->p.dst_sg_it);
  261. if (req_ctx->op != COP_AES_CBC)
  262. return ;
  263. memcpy(req->info, cpg->sram + SRAM_DATA_IV_BUF, 16);
  264. }
  265. static void mv_process_hash_current(int first_block)
  266. {
  267. struct ahash_request *req = ahash_request_cast(cpg->cur_req);
  268. struct mv_req_hash_ctx *req_ctx = ahash_request_ctx(req);
  269. struct req_progress *p = &cpg->p;
  270. struct sec_accel_config op = { 0 };
  271. int is_last;
  272. switch (req_ctx->op) {
  273. case COP_SHA1:
  274. default:
  275. op.config = CFG_OP_MAC_ONLY | CFG_MACM_SHA1;
  276. break;
  277. case COP_HMAC_SHA1:
  278. op.config = CFG_OP_MAC_ONLY | CFG_MACM_HMAC_SHA1;
  279. break;
  280. }
  281. op.mac_src_p =
  282. MAC_SRC_DATA_P(SRAM_DATA_IN_START) | MAC_SRC_TOTAL_LEN((u32)
  283. req_ctx->
  284. count);
  285. setup_data_in();
  286. op.mac_digest =
  287. MAC_DIGEST_P(SRAM_DIGEST_BUF) | MAC_FRAG_LEN(p->crypt_len);
  288. op.mac_iv =
  289. MAC_INNER_IV_P(SRAM_HMAC_IV_IN) |
  290. MAC_OUTER_IV_P(SRAM_HMAC_IV_OUT);
  291. is_last = req_ctx->last_chunk
  292. && (p->hw_processed_bytes + p->crypt_len >= p->hw_nbytes)
  293. && (req_ctx->count <= MAX_HW_HASH_SIZE);
  294. if (req_ctx->first_hash) {
  295. if (is_last)
  296. op.config |= CFG_NOT_FRAG;
  297. else
  298. op.config |= CFG_FIRST_FRAG;
  299. req_ctx->first_hash = 0;
  300. } else {
  301. if (is_last)
  302. op.config |= CFG_LAST_FRAG;
  303. else
  304. op.config |= CFG_MID_FRAG;
  305. }
  306. memcpy(cpg->sram + SRAM_CONFIG, &op, sizeof(struct sec_accel_config));
  307. writel(SRAM_CONFIG, cpg->reg + SEC_ACCEL_DESC_P0);
  308. /* GO */
  309. writel(SEC_CMD_EN_SEC_ACCL0, cpg->reg + SEC_ACCEL_CMD);
  310. /*
  311. * XXX: add timer if the interrupt does not occur for some mystery
  312. * reason
  313. */
  314. }
  315. static inline int mv_hash_import_sha1_ctx(const struct mv_req_hash_ctx *ctx,
  316. struct shash_desc *desc)
  317. {
  318. int i;
  319. struct sha1_state shash_state;
  320. shash_state.count = ctx->count + ctx->count_add;
  321. for (i = 0; i < 5; i++)
  322. shash_state.state[i] = ctx->state[i];
  323. memcpy(shash_state.buffer, ctx->buffer, sizeof(shash_state.buffer));
  324. return crypto_shash_import(desc, &shash_state);
  325. }
  326. static int mv_hash_final_fallback(struct ahash_request *req)
  327. {
  328. const struct mv_tfm_hash_ctx *tfm_ctx = crypto_tfm_ctx(req->base.tfm);
  329. struct mv_req_hash_ctx *req_ctx = ahash_request_ctx(req);
  330. struct {
  331. struct shash_desc shash;
  332. char ctx[crypto_shash_descsize(tfm_ctx->fallback)];
  333. } desc;
  334. int rc;
  335. desc.shash.tfm = tfm_ctx->fallback;
  336. desc.shash.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
  337. if (unlikely(req_ctx->first_hash)) {
  338. crypto_shash_init(&desc.shash);
  339. crypto_shash_update(&desc.shash, req_ctx->buffer,
  340. req_ctx->extra_bytes);
  341. } else {
  342. /* only SHA1 for now....
  343. */
  344. rc = mv_hash_import_sha1_ctx(req_ctx, &desc.shash);
  345. if (rc)
  346. goto out;
  347. }
  348. rc = crypto_shash_final(&desc.shash, req->result);
  349. out:
  350. return rc;
  351. }
  352. static void mv_hash_algo_completion(void)
  353. {
  354. struct ahash_request *req = ahash_request_cast(cpg->cur_req);
  355. struct mv_req_hash_ctx *ctx = ahash_request_ctx(req);
  356. if (ctx->extra_bytes)
  357. copy_src_to_buf(&cpg->p, ctx->buffer, ctx->extra_bytes);
  358. sg_miter_stop(&cpg->p.src_sg_it);
  359. ctx->state[0] = readl(cpg->reg + DIGEST_INITIAL_VAL_A);
  360. ctx->state[1] = readl(cpg->reg + DIGEST_INITIAL_VAL_B);
  361. ctx->state[2] = readl(cpg->reg + DIGEST_INITIAL_VAL_C);
  362. ctx->state[3] = readl(cpg->reg + DIGEST_INITIAL_VAL_D);
  363. ctx->state[4] = readl(cpg->reg + DIGEST_INITIAL_VAL_E);
  364. if (likely(ctx->last_chunk)) {
  365. if (likely(ctx->count <= MAX_HW_HASH_SIZE)) {
  366. memcpy(req->result, cpg->sram + SRAM_DIGEST_BUF,
  367. crypto_ahash_digestsize(crypto_ahash_reqtfm
  368. (req)));
  369. } else
  370. mv_hash_final_fallback(req);
  371. }
  372. }
  373. static void dequeue_complete_req(void)
  374. {
  375. struct crypto_async_request *req = cpg->cur_req;
  376. void *buf;
  377. int ret;
  378. cpg->p.hw_processed_bytes += cpg->p.crypt_len;
  379. if (cpg->p.copy_back) {
  380. int need_copy_len = cpg->p.crypt_len;
  381. int sram_offset = 0;
  382. do {
  383. int dst_copy;
  384. if (!cpg->p.sg_dst_left) {
  385. ret = sg_miter_next(&cpg->p.dst_sg_it);
  386. BUG_ON(!ret);
  387. cpg->p.sg_dst_left = cpg->p.dst_sg_it.length;
  388. cpg->p.dst_start = 0;
  389. }
  390. buf = cpg->p.dst_sg_it.addr;
  391. buf += cpg->p.dst_start;
  392. dst_copy = min(need_copy_len, cpg->p.sg_dst_left);
  393. memcpy(buf,
  394. cpg->sram + SRAM_DATA_OUT_START + sram_offset,
  395. dst_copy);
  396. sram_offset += dst_copy;
  397. cpg->p.sg_dst_left -= dst_copy;
  398. need_copy_len -= dst_copy;
  399. cpg->p.dst_start += dst_copy;
  400. } while (need_copy_len > 0);
  401. }
  402. cpg->p.crypt_len = 0;
  403. BUG_ON(cpg->eng_st != ENGINE_W_DEQUEUE);
  404. if (cpg->p.hw_processed_bytes < cpg->p.hw_nbytes) {
  405. /* process next scatter list entry */
  406. cpg->eng_st = ENGINE_BUSY;
  407. cpg->p.process(0);
  408. } else {
  409. cpg->p.complete();
  410. cpg->eng_st = ENGINE_IDLE;
  411. local_bh_disable();
  412. req->complete(req, 0);
  413. local_bh_enable();
  414. }
  415. }
  416. static int count_sgs(struct scatterlist *sl, unsigned int total_bytes)
  417. {
  418. int i = 0;
  419. size_t cur_len;
  420. while (1) {
  421. cur_len = sl[i].length;
  422. ++i;
  423. if (total_bytes > cur_len)
  424. total_bytes -= cur_len;
  425. else
  426. break;
  427. }
  428. return i;
  429. }
  430. static void mv_start_new_crypt_req(struct ablkcipher_request *req)
  431. {
  432. struct req_progress *p = &cpg->p;
  433. int num_sgs;
  434. cpg->cur_req = &req->base;
  435. memset(p, 0, sizeof(struct req_progress));
  436. p->hw_nbytes = req->nbytes;
  437. p->complete = mv_crypto_algo_completion;
  438. p->process = mv_process_current_q;
  439. p->copy_back = 1;
  440. num_sgs = count_sgs(req->src, req->nbytes);
  441. sg_miter_start(&p->src_sg_it, req->src, num_sgs, SG_MITER_FROM_SG);
  442. num_sgs = count_sgs(req->dst, req->nbytes);
  443. sg_miter_start(&p->dst_sg_it, req->dst, num_sgs, SG_MITER_TO_SG);
  444. mv_process_current_q(1);
  445. }
  446. static void mv_start_new_hash_req(struct ahash_request *req)
  447. {
  448. struct req_progress *p = &cpg->p;
  449. struct mv_req_hash_ctx *ctx = ahash_request_ctx(req);
  450. const struct mv_tfm_hash_ctx *tfm_ctx = crypto_tfm_ctx(req->base.tfm);
  451. int num_sgs, hw_bytes, old_extra_bytes, rc;
  452. cpg->cur_req = &req->base;
  453. memset(p, 0, sizeof(struct req_progress));
  454. hw_bytes = req->nbytes + ctx->extra_bytes;
  455. old_extra_bytes = ctx->extra_bytes;
  456. if (unlikely(ctx->extra_bytes)) {
  457. memcpy(cpg->sram + SRAM_DATA_IN_START, ctx->buffer,
  458. ctx->extra_bytes);
  459. p->crypt_len = ctx->extra_bytes;
  460. }
  461. memcpy(cpg->sram + SRAM_HMAC_IV_IN, tfm_ctx->ivs, sizeof(tfm_ctx->ivs));
  462. if (unlikely(!ctx->first_hash)) {
  463. writel(ctx->state[0], cpg->reg + DIGEST_INITIAL_VAL_A);
  464. writel(ctx->state[1], cpg->reg + DIGEST_INITIAL_VAL_B);
  465. writel(ctx->state[2], cpg->reg + DIGEST_INITIAL_VAL_C);
  466. writel(ctx->state[3], cpg->reg + DIGEST_INITIAL_VAL_D);
  467. writel(ctx->state[4], cpg->reg + DIGEST_INITIAL_VAL_E);
  468. }
  469. ctx->extra_bytes = hw_bytes % SHA1_BLOCK_SIZE;
  470. if (ctx->extra_bytes != 0
  471. && (!ctx->last_chunk || ctx->count > MAX_HW_HASH_SIZE))
  472. hw_bytes -= ctx->extra_bytes;
  473. else
  474. ctx->extra_bytes = 0;
  475. num_sgs = count_sgs(req->src, req->nbytes);
  476. sg_miter_start(&p->src_sg_it, req->src, num_sgs, SG_MITER_FROM_SG);
  477. if (hw_bytes) {
  478. p->hw_nbytes = hw_bytes;
  479. p->complete = mv_hash_algo_completion;
  480. p->process = mv_process_hash_current;
  481. mv_process_hash_current(1);
  482. } else {
  483. copy_src_to_buf(p, ctx->buffer + old_extra_bytes,
  484. ctx->extra_bytes - old_extra_bytes);
  485. sg_miter_stop(&p->src_sg_it);
  486. if (ctx->last_chunk)
  487. rc = mv_hash_final_fallback(req);
  488. else
  489. rc = 0;
  490. cpg->eng_st = ENGINE_IDLE;
  491. local_bh_disable();
  492. req->base.complete(&req->base, rc);
  493. local_bh_enable();
  494. }
  495. }
  496. static int queue_manag(void *data)
  497. {
  498. cpg->eng_st = ENGINE_IDLE;
  499. do {
  500. struct crypto_async_request *async_req = NULL;
  501. struct crypto_async_request *backlog;
  502. __set_current_state(TASK_INTERRUPTIBLE);
  503. if (cpg->eng_st == ENGINE_W_DEQUEUE)
  504. dequeue_complete_req();
  505. spin_lock_irq(&cpg->lock);
  506. if (cpg->eng_st == ENGINE_IDLE) {
  507. backlog = crypto_get_backlog(&cpg->queue);
  508. async_req = crypto_dequeue_request(&cpg->queue);
  509. if (async_req) {
  510. BUG_ON(cpg->eng_st != ENGINE_IDLE);
  511. cpg->eng_st = ENGINE_BUSY;
  512. }
  513. }
  514. spin_unlock_irq(&cpg->lock);
  515. if (backlog) {
  516. backlog->complete(backlog, -EINPROGRESS);
  517. backlog = NULL;
  518. }
  519. if (async_req) {
  520. if (async_req->tfm->__crt_alg->cra_type !=
  521. &crypto_ahash_type) {
  522. struct ablkcipher_request *req =
  523. container_of(async_req,
  524. struct ablkcipher_request,
  525. base);
  526. mv_start_new_crypt_req(req);
  527. } else {
  528. struct ahash_request *req =
  529. ahash_request_cast(async_req);
  530. mv_start_new_hash_req(req);
  531. }
  532. async_req = NULL;
  533. }
  534. schedule();
  535. } while (!kthread_should_stop());
  536. return 0;
  537. }
  538. static int mv_handle_req(struct crypto_async_request *req)
  539. {
  540. unsigned long flags;
  541. int ret;
  542. spin_lock_irqsave(&cpg->lock, flags);
  543. ret = crypto_enqueue_request(&cpg->queue, req);
  544. spin_unlock_irqrestore(&cpg->lock, flags);
  545. wake_up_process(cpg->queue_th);
  546. return ret;
  547. }
  548. static int mv_enc_aes_ecb(struct ablkcipher_request *req)
  549. {
  550. struct mv_req_ctx *req_ctx = ablkcipher_request_ctx(req);
  551. req_ctx->op = COP_AES_ECB;
  552. req_ctx->decrypt = 0;
  553. return mv_handle_req(&req->base);
  554. }
  555. static int mv_dec_aes_ecb(struct ablkcipher_request *req)
  556. {
  557. struct mv_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  558. struct mv_req_ctx *req_ctx = ablkcipher_request_ctx(req);
  559. req_ctx->op = COP_AES_ECB;
  560. req_ctx->decrypt = 1;
  561. compute_aes_dec_key(ctx);
  562. return mv_handle_req(&req->base);
  563. }
  564. static int mv_enc_aes_cbc(struct ablkcipher_request *req)
  565. {
  566. struct mv_req_ctx *req_ctx = ablkcipher_request_ctx(req);
  567. req_ctx->op = COP_AES_CBC;
  568. req_ctx->decrypt = 0;
  569. return mv_handle_req(&req->base);
  570. }
  571. static int mv_dec_aes_cbc(struct ablkcipher_request *req)
  572. {
  573. struct mv_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  574. struct mv_req_ctx *req_ctx = ablkcipher_request_ctx(req);
  575. req_ctx->op = COP_AES_CBC;
  576. req_ctx->decrypt = 1;
  577. compute_aes_dec_key(ctx);
  578. return mv_handle_req(&req->base);
  579. }
  580. static int mv_cra_init(struct crypto_tfm *tfm)
  581. {
  582. tfm->crt_ablkcipher.reqsize = sizeof(struct mv_req_ctx);
  583. return 0;
  584. }
  585. static void mv_init_hash_req_ctx(struct mv_req_hash_ctx *ctx, int op,
  586. int is_last, unsigned int req_len,
  587. int count_add)
  588. {
  589. memset(ctx, 0, sizeof(*ctx));
  590. ctx->op = op;
  591. ctx->count = req_len;
  592. ctx->first_hash = 1;
  593. ctx->last_chunk = is_last;
  594. ctx->count_add = count_add;
  595. }
  596. static void mv_update_hash_req_ctx(struct mv_req_hash_ctx *ctx, int is_last,
  597. unsigned req_len)
  598. {
  599. ctx->last_chunk = is_last;
  600. ctx->count += req_len;
  601. }
  602. static int mv_hash_init(struct ahash_request *req)
  603. {
  604. const struct mv_tfm_hash_ctx *tfm_ctx = crypto_tfm_ctx(req->base.tfm);
  605. mv_init_hash_req_ctx(ahash_request_ctx(req), tfm_ctx->op, 0, 0,
  606. tfm_ctx->count_add);
  607. return 0;
  608. }
  609. static int mv_hash_update(struct ahash_request *req)
  610. {
  611. if (!req->nbytes)
  612. return 0;
  613. mv_update_hash_req_ctx(ahash_request_ctx(req), 0, req->nbytes);
  614. return mv_handle_req(&req->base);
  615. }
  616. static int mv_hash_final(struct ahash_request *req)
  617. {
  618. struct mv_req_hash_ctx *ctx = ahash_request_ctx(req);
  619. /* dummy buffer of 4 bytes */
  620. sg_init_one(&ctx->dummysg, ctx->buffer, 4);
  621. /* I think I'm allowed to do that... */
  622. ahash_request_set_crypt(req, &ctx->dummysg, req->result, 0);
  623. mv_update_hash_req_ctx(ctx, 1, 0);
  624. return mv_handle_req(&req->base);
  625. }
  626. static int mv_hash_finup(struct ahash_request *req)
  627. {
  628. if (!req->nbytes)
  629. return mv_hash_final(req);
  630. mv_update_hash_req_ctx(ahash_request_ctx(req), 1, req->nbytes);
  631. return mv_handle_req(&req->base);
  632. }
  633. static int mv_hash_digest(struct ahash_request *req)
  634. {
  635. const struct mv_tfm_hash_ctx *tfm_ctx = crypto_tfm_ctx(req->base.tfm);
  636. mv_init_hash_req_ctx(ahash_request_ctx(req), tfm_ctx->op, 1,
  637. req->nbytes, tfm_ctx->count_add);
  638. return mv_handle_req(&req->base);
  639. }
  640. static void mv_hash_init_ivs(struct mv_tfm_hash_ctx *ctx, const void *istate,
  641. const void *ostate)
  642. {
  643. const struct sha1_state *isha1_state = istate, *osha1_state = ostate;
  644. int i;
  645. for (i = 0; i < 5; i++) {
  646. ctx->ivs[i] = cpu_to_be32(isha1_state->state[i]);
  647. ctx->ivs[i + 5] = cpu_to_be32(osha1_state->state[i]);
  648. }
  649. }
  650. static int mv_hash_setkey(struct crypto_ahash *tfm, const u8 * key,
  651. unsigned int keylen)
  652. {
  653. int rc;
  654. struct mv_tfm_hash_ctx *ctx = crypto_tfm_ctx(&tfm->base);
  655. int bs, ds, ss;
  656. if (!ctx->base_hash)
  657. return 0;
  658. rc = crypto_shash_setkey(ctx->fallback, key, keylen);
  659. if (rc)
  660. return rc;
  661. /* Can't see a way to extract the ipad/opad from the fallback tfm
  662. so I'm basically copying code from the hmac module */
  663. bs = crypto_shash_blocksize(ctx->base_hash);
  664. ds = crypto_shash_digestsize(ctx->base_hash);
  665. ss = crypto_shash_statesize(ctx->base_hash);
  666. {
  667. struct {
  668. struct shash_desc shash;
  669. char ctx[crypto_shash_descsize(ctx->base_hash)];
  670. } desc;
  671. unsigned int i;
  672. char ipad[ss];
  673. char opad[ss];
  674. desc.shash.tfm = ctx->base_hash;
  675. desc.shash.flags = crypto_shash_get_flags(ctx->base_hash) &
  676. CRYPTO_TFM_REQ_MAY_SLEEP;
  677. if (keylen > bs) {
  678. int err;
  679. err =
  680. crypto_shash_digest(&desc.shash, key, keylen, ipad);
  681. if (err)
  682. return err;
  683. keylen = ds;
  684. } else
  685. memcpy(ipad, key, keylen);
  686. memset(ipad + keylen, 0, bs - keylen);
  687. memcpy(opad, ipad, bs);
  688. for (i = 0; i < bs; i++) {
  689. ipad[i] ^= 0x36;
  690. opad[i] ^= 0x5c;
  691. }
  692. rc = crypto_shash_init(&desc.shash) ? :
  693. crypto_shash_update(&desc.shash, ipad, bs) ? :
  694. crypto_shash_export(&desc.shash, ipad) ? :
  695. crypto_shash_init(&desc.shash) ? :
  696. crypto_shash_update(&desc.shash, opad, bs) ? :
  697. crypto_shash_export(&desc.shash, opad);
  698. if (rc == 0)
  699. mv_hash_init_ivs(ctx, ipad, opad);
  700. return rc;
  701. }
  702. }
  703. static int mv_cra_hash_init(struct crypto_tfm *tfm, const char *base_hash_name,
  704. enum hash_op op, int count_add)
  705. {
  706. const char *fallback_driver_name = tfm->__crt_alg->cra_name;
  707. struct mv_tfm_hash_ctx *ctx = crypto_tfm_ctx(tfm);
  708. struct crypto_shash *fallback_tfm = NULL;
  709. struct crypto_shash *base_hash = NULL;
  710. int err = -ENOMEM;
  711. ctx->op = op;
  712. ctx->count_add = count_add;
  713. /* Allocate a fallback and abort if it failed. */
  714. fallback_tfm = crypto_alloc_shash(fallback_driver_name, 0,
  715. CRYPTO_ALG_NEED_FALLBACK);
  716. if (IS_ERR(fallback_tfm)) {
  717. printk(KERN_WARNING MV_CESA
  718. "Fallback driver '%s' could not be loaded!\n",
  719. fallback_driver_name);
  720. err = PTR_ERR(fallback_tfm);
  721. goto out;
  722. }
  723. ctx->fallback = fallback_tfm;
  724. if (base_hash_name) {
  725. /* Allocate a hash to compute the ipad/opad of hmac. */
  726. base_hash = crypto_alloc_shash(base_hash_name, 0,
  727. CRYPTO_ALG_NEED_FALLBACK);
  728. if (IS_ERR(base_hash)) {
  729. printk(KERN_WARNING MV_CESA
  730. "Base driver '%s' could not be loaded!\n",
  731. base_hash_name);
  732. err = PTR_ERR(fallback_tfm);
  733. goto err_bad_base;
  734. }
  735. }
  736. ctx->base_hash = base_hash;
  737. crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
  738. sizeof(struct mv_req_hash_ctx) +
  739. crypto_shash_descsize(ctx->fallback));
  740. return 0;
  741. err_bad_base:
  742. crypto_free_shash(fallback_tfm);
  743. out:
  744. return err;
  745. }
  746. static void mv_cra_hash_exit(struct crypto_tfm *tfm)
  747. {
  748. struct mv_tfm_hash_ctx *ctx = crypto_tfm_ctx(tfm);
  749. crypto_free_shash(ctx->fallback);
  750. if (ctx->base_hash)
  751. crypto_free_shash(ctx->base_hash);
  752. }
  753. static int mv_cra_hash_sha1_init(struct crypto_tfm *tfm)
  754. {
  755. return mv_cra_hash_init(tfm, NULL, COP_SHA1, 0);
  756. }
  757. static int mv_cra_hash_hmac_sha1_init(struct crypto_tfm *tfm)
  758. {
  759. return mv_cra_hash_init(tfm, "sha1", COP_HMAC_SHA1, SHA1_BLOCK_SIZE);
  760. }
  761. irqreturn_t crypto_int(int irq, void *priv)
  762. {
  763. u32 val;
  764. val = readl(cpg->reg + SEC_ACCEL_INT_STATUS);
  765. if (!(val & SEC_INT_ACCEL0_DONE))
  766. return IRQ_NONE;
  767. val &= ~SEC_INT_ACCEL0_DONE;
  768. writel(val, cpg->reg + FPGA_INT_STATUS);
  769. writel(val, cpg->reg + SEC_ACCEL_INT_STATUS);
  770. BUG_ON(cpg->eng_st != ENGINE_BUSY);
  771. cpg->eng_st = ENGINE_W_DEQUEUE;
  772. wake_up_process(cpg->queue_th);
  773. return IRQ_HANDLED;
  774. }
  775. struct crypto_alg mv_aes_alg_ecb = {
  776. .cra_name = "ecb(aes)",
  777. .cra_driver_name = "mv-ecb-aes",
  778. .cra_priority = 300,
  779. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  780. .cra_blocksize = 16,
  781. .cra_ctxsize = sizeof(struct mv_ctx),
  782. .cra_alignmask = 0,
  783. .cra_type = &crypto_ablkcipher_type,
  784. .cra_module = THIS_MODULE,
  785. .cra_init = mv_cra_init,
  786. .cra_u = {
  787. .ablkcipher = {
  788. .min_keysize = AES_MIN_KEY_SIZE,
  789. .max_keysize = AES_MAX_KEY_SIZE,
  790. .setkey = mv_setkey_aes,
  791. .encrypt = mv_enc_aes_ecb,
  792. .decrypt = mv_dec_aes_ecb,
  793. },
  794. },
  795. };
  796. struct crypto_alg mv_aes_alg_cbc = {
  797. .cra_name = "cbc(aes)",
  798. .cra_driver_name = "mv-cbc-aes",
  799. .cra_priority = 300,
  800. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  801. .cra_blocksize = AES_BLOCK_SIZE,
  802. .cra_ctxsize = sizeof(struct mv_ctx),
  803. .cra_alignmask = 0,
  804. .cra_type = &crypto_ablkcipher_type,
  805. .cra_module = THIS_MODULE,
  806. .cra_init = mv_cra_init,
  807. .cra_u = {
  808. .ablkcipher = {
  809. .ivsize = AES_BLOCK_SIZE,
  810. .min_keysize = AES_MIN_KEY_SIZE,
  811. .max_keysize = AES_MAX_KEY_SIZE,
  812. .setkey = mv_setkey_aes,
  813. .encrypt = mv_enc_aes_cbc,
  814. .decrypt = mv_dec_aes_cbc,
  815. },
  816. },
  817. };
  818. struct ahash_alg mv_sha1_alg = {
  819. .init = mv_hash_init,
  820. .update = mv_hash_update,
  821. .final = mv_hash_final,
  822. .finup = mv_hash_finup,
  823. .digest = mv_hash_digest,
  824. .halg = {
  825. .digestsize = SHA1_DIGEST_SIZE,
  826. .base = {
  827. .cra_name = "sha1",
  828. .cra_driver_name = "mv-sha1",
  829. .cra_priority = 300,
  830. .cra_flags =
  831. CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK,
  832. .cra_blocksize = SHA1_BLOCK_SIZE,
  833. .cra_ctxsize = sizeof(struct mv_tfm_hash_ctx),
  834. .cra_init = mv_cra_hash_sha1_init,
  835. .cra_exit = mv_cra_hash_exit,
  836. .cra_module = THIS_MODULE,
  837. }
  838. }
  839. };
  840. struct ahash_alg mv_hmac_sha1_alg = {
  841. .init = mv_hash_init,
  842. .update = mv_hash_update,
  843. .final = mv_hash_final,
  844. .finup = mv_hash_finup,
  845. .digest = mv_hash_digest,
  846. .setkey = mv_hash_setkey,
  847. .halg = {
  848. .digestsize = SHA1_DIGEST_SIZE,
  849. .base = {
  850. .cra_name = "hmac(sha1)",
  851. .cra_driver_name = "mv-hmac-sha1",
  852. .cra_priority = 300,
  853. .cra_flags =
  854. CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK,
  855. .cra_blocksize = SHA1_BLOCK_SIZE,
  856. .cra_ctxsize = sizeof(struct mv_tfm_hash_ctx),
  857. .cra_init = mv_cra_hash_hmac_sha1_init,
  858. .cra_exit = mv_cra_hash_exit,
  859. .cra_module = THIS_MODULE,
  860. }
  861. }
  862. };
  863. static int mv_probe(struct platform_device *pdev)
  864. {
  865. struct crypto_priv *cp;
  866. struct resource *res;
  867. int irq;
  868. int ret;
  869. if (cpg) {
  870. printk(KERN_ERR MV_CESA "Second crypto dev?\n");
  871. return -EEXIST;
  872. }
  873. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "regs");
  874. if (!res)
  875. return -ENXIO;
  876. cp = kzalloc(sizeof(*cp), GFP_KERNEL);
  877. if (!cp)
  878. return -ENOMEM;
  879. spin_lock_init(&cp->lock);
  880. crypto_init_queue(&cp->queue, 50);
  881. cp->reg = ioremap(res->start, resource_size(res));
  882. if (!cp->reg) {
  883. ret = -ENOMEM;
  884. goto err;
  885. }
  886. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "sram");
  887. if (!res) {
  888. ret = -ENXIO;
  889. goto err_unmap_reg;
  890. }
  891. cp->sram_size = resource_size(res);
  892. cp->max_req_size = cp->sram_size - SRAM_CFG_SPACE;
  893. cp->sram = ioremap(res->start, cp->sram_size);
  894. if (!cp->sram) {
  895. ret = -ENOMEM;
  896. goto err_unmap_reg;
  897. }
  898. irq = platform_get_irq(pdev, 0);
  899. if (irq < 0 || irq == NO_IRQ) {
  900. ret = irq;
  901. goto err_unmap_sram;
  902. }
  903. cp->irq = irq;
  904. platform_set_drvdata(pdev, cp);
  905. cpg = cp;
  906. cp->queue_th = kthread_run(queue_manag, cp, "mv_crypto");
  907. if (IS_ERR(cp->queue_th)) {
  908. ret = PTR_ERR(cp->queue_th);
  909. goto err_unmap_sram;
  910. }
  911. ret = request_irq(irq, crypto_int, IRQF_DISABLED, dev_name(&pdev->dev),
  912. cp);
  913. if (ret)
  914. goto err_thread;
  915. writel(SEC_INT_ACCEL0_DONE, cpg->reg + SEC_ACCEL_INT_MASK);
  916. writel(SEC_CFG_STOP_DIG_ERR, cpg->reg + SEC_ACCEL_CFG);
  917. ret = crypto_register_alg(&mv_aes_alg_ecb);
  918. if (ret)
  919. goto err_irq;
  920. ret = crypto_register_alg(&mv_aes_alg_cbc);
  921. if (ret)
  922. goto err_unreg_ecb;
  923. ret = crypto_register_ahash(&mv_sha1_alg);
  924. if (ret == 0)
  925. cpg->has_sha1 = 1;
  926. else
  927. printk(KERN_WARNING MV_CESA "Could not register sha1 driver\n");
  928. ret = crypto_register_ahash(&mv_hmac_sha1_alg);
  929. if (ret == 0) {
  930. cpg->has_hmac_sha1 = 1;
  931. } else {
  932. printk(KERN_WARNING MV_CESA
  933. "Could not register hmac-sha1 driver\n");
  934. }
  935. return 0;
  936. err_unreg_ecb:
  937. crypto_unregister_alg(&mv_aes_alg_ecb);
  938. err_irq:
  939. free_irq(irq, cp);
  940. err_thread:
  941. kthread_stop(cp->queue_th);
  942. err_unmap_sram:
  943. iounmap(cp->sram);
  944. err_unmap_reg:
  945. iounmap(cp->reg);
  946. err:
  947. kfree(cp);
  948. cpg = NULL;
  949. platform_set_drvdata(pdev, NULL);
  950. return ret;
  951. }
  952. static int mv_remove(struct platform_device *pdev)
  953. {
  954. struct crypto_priv *cp = platform_get_drvdata(pdev);
  955. crypto_unregister_alg(&mv_aes_alg_ecb);
  956. crypto_unregister_alg(&mv_aes_alg_cbc);
  957. if (cp->has_sha1)
  958. crypto_unregister_ahash(&mv_sha1_alg);
  959. if (cp->has_hmac_sha1)
  960. crypto_unregister_ahash(&mv_hmac_sha1_alg);
  961. kthread_stop(cp->queue_th);
  962. free_irq(cp->irq, cp);
  963. memset(cp->sram, 0, cp->sram_size);
  964. iounmap(cp->sram);
  965. iounmap(cp->reg);
  966. kfree(cp);
  967. cpg = NULL;
  968. return 0;
  969. }
  970. static struct platform_driver marvell_crypto = {
  971. .probe = mv_probe,
  972. .remove = mv_remove,
  973. .driver = {
  974. .owner = THIS_MODULE,
  975. .name = "mv_crypto",
  976. },
  977. };
  978. MODULE_ALIAS("platform:mv_crypto");
  979. static int __init mv_crypto_init(void)
  980. {
  981. return platform_driver_register(&marvell_crypto);
  982. }
  983. module_init(mv_crypto_init);
  984. static void __exit mv_crypto_exit(void)
  985. {
  986. platform_driver_unregister(&marvell_crypto);
  987. }
  988. module_exit(mv_crypto_exit);
  989. MODULE_AUTHOR("Sebastian Andrzej Siewior <sebastian@breakpoint.cc>");
  990. MODULE_DESCRIPTION("Support for Marvell's cryptographic engine");
  991. MODULE_LICENSE("GPL");