mv_cesa.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147
  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. /* GO */
  248. writel(SEC_CMD_EN_SEC_ACCL0, cpg->reg + SEC_ACCEL_CMD);
  249. /*
  250. * XXX: add timer if the interrupt does not occur for some mystery
  251. * reason
  252. */
  253. }
  254. static void mv_crypto_algo_completion(void)
  255. {
  256. struct ablkcipher_request *req = ablkcipher_request_cast(cpg->cur_req);
  257. struct mv_req_ctx *req_ctx = ablkcipher_request_ctx(req);
  258. sg_miter_stop(&cpg->p.src_sg_it);
  259. sg_miter_stop(&cpg->p.dst_sg_it);
  260. if (req_ctx->op != COP_AES_CBC)
  261. return ;
  262. memcpy(req->info, cpg->sram + SRAM_DATA_IV_BUF, 16);
  263. }
  264. static void mv_process_hash_current(int first_block)
  265. {
  266. struct ahash_request *req = ahash_request_cast(cpg->cur_req);
  267. struct mv_req_hash_ctx *req_ctx = ahash_request_ctx(req);
  268. struct req_progress *p = &cpg->p;
  269. struct sec_accel_config op = { 0 };
  270. int is_last;
  271. switch (req_ctx->op) {
  272. case COP_SHA1:
  273. default:
  274. op.config = CFG_OP_MAC_ONLY | CFG_MACM_SHA1;
  275. break;
  276. case COP_HMAC_SHA1:
  277. op.config = CFG_OP_MAC_ONLY | CFG_MACM_HMAC_SHA1;
  278. break;
  279. }
  280. op.mac_src_p =
  281. MAC_SRC_DATA_P(SRAM_DATA_IN_START) | MAC_SRC_TOTAL_LEN((u32)
  282. req_ctx->
  283. count);
  284. setup_data_in();
  285. op.mac_digest =
  286. MAC_DIGEST_P(SRAM_DIGEST_BUF) | MAC_FRAG_LEN(p->crypt_len);
  287. op.mac_iv =
  288. MAC_INNER_IV_P(SRAM_HMAC_IV_IN) |
  289. MAC_OUTER_IV_P(SRAM_HMAC_IV_OUT);
  290. is_last = req_ctx->last_chunk
  291. && (p->hw_processed_bytes + p->crypt_len >= p->hw_nbytes)
  292. && (req_ctx->count <= MAX_HW_HASH_SIZE);
  293. if (req_ctx->first_hash) {
  294. if (is_last)
  295. op.config |= CFG_NOT_FRAG;
  296. else
  297. op.config |= CFG_FIRST_FRAG;
  298. req_ctx->first_hash = 0;
  299. } else {
  300. if (is_last)
  301. op.config |= CFG_LAST_FRAG;
  302. else
  303. op.config |= CFG_MID_FRAG;
  304. }
  305. memcpy(cpg->sram + SRAM_CONFIG, &op, sizeof(struct sec_accel_config));
  306. /* GO */
  307. writel(SEC_CMD_EN_SEC_ACCL0, cpg->reg + SEC_ACCEL_CMD);
  308. /*
  309. * XXX: add timer if the interrupt does not occur for some mystery
  310. * reason
  311. */
  312. }
  313. static inline int mv_hash_import_sha1_ctx(const struct mv_req_hash_ctx *ctx,
  314. struct shash_desc *desc)
  315. {
  316. int i;
  317. struct sha1_state shash_state;
  318. shash_state.count = ctx->count + ctx->count_add;
  319. for (i = 0; i < 5; i++)
  320. shash_state.state[i] = ctx->state[i];
  321. memcpy(shash_state.buffer, ctx->buffer, sizeof(shash_state.buffer));
  322. return crypto_shash_import(desc, &shash_state);
  323. }
  324. static int mv_hash_final_fallback(struct ahash_request *req)
  325. {
  326. const struct mv_tfm_hash_ctx *tfm_ctx = crypto_tfm_ctx(req->base.tfm);
  327. struct mv_req_hash_ctx *req_ctx = ahash_request_ctx(req);
  328. struct {
  329. struct shash_desc shash;
  330. char ctx[crypto_shash_descsize(tfm_ctx->fallback)];
  331. } desc;
  332. int rc;
  333. desc.shash.tfm = tfm_ctx->fallback;
  334. desc.shash.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
  335. if (unlikely(req_ctx->first_hash)) {
  336. crypto_shash_init(&desc.shash);
  337. crypto_shash_update(&desc.shash, req_ctx->buffer,
  338. req_ctx->extra_bytes);
  339. } else {
  340. /* only SHA1 for now....
  341. */
  342. rc = mv_hash_import_sha1_ctx(req_ctx, &desc.shash);
  343. if (rc)
  344. goto out;
  345. }
  346. rc = crypto_shash_final(&desc.shash, req->result);
  347. out:
  348. return rc;
  349. }
  350. static void mv_hash_algo_completion(void)
  351. {
  352. struct ahash_request *req = ahash_request_cast(cpg->cur_req);
  353. struct mv_req_hash_ctx *ctx = ahash_request_ctx(req);
  354. if (ctx->extra_bytes)
  355. copy_src_to_buf(&cpg->p, ctx->buffer, ctx->extra_bytes);
  356. sg_miter_stop(&cpg->p.src_sg_it);
  357. ctx->state[0] = readl(cpg->reg + DIGEST_INITIAL_VAL_A);
  358. ctx->state[1] = readl(cpg->reg + DIGEST_INITIAL_VAL_B);
  359. ctx->state[2] = readl(cpg->reg + DIGEST_INITIAL_VAL_C);
  360. ctx->state[3] = readl(cpg->reg + DIGEST_INITIAL_VAL_D);
  361. ctx->state[4] = readl(cpg->reg + DIGEST_INITIAL_VAL_E);
  362. if (likely(ctx->last_chunk)) {
  363. if (likely(ctx->count <= MAX_HW_HASH_SIZE)) {
  364. memcpy(req->result, cpg->sram + SRAM_DIGEST_BUF,
  365. crypto_ahash_digestsize(crypto_ahash_reqtfm
  366. (req)));
  367. } else
  368. mv_hash_final_fallback(req);
  369. }
  370. }
  371. static void dequeue_complete_req(void)
  372. {
  373. struct crypto_async_request *req = cpg->cur_req;
  374. void *buf;
  375. int ret;
  376. cpg->p.hw_processed_bytes += cpg->p.crypt_len;
  377. if (cpg->p.copy_back) {
  378. int need_copy_len = cpg->p.crypt_len;
  379. int sram_offset = 0;
  380. do {
  381. int dst_copy;
  382. if (!cpg->p.sg_dst_left) {
  383. ret = sg_miter_next(&cpg->p.dst_sg_it);
  384. BUG_ON(!ret);
  385. cpg->p.sg_dst_left = cpg->p.dst_sg_it.length;
  386. cpg->p.dst_start = 0;
  387. }
  388. buf = cpg->p.dst_sg_it.addr;
  389. buf += cpg->p.dst_start;
  390. dst_copy = min(need_copy_len, cpg->p.sg_dst_left);
  391. memcpy(buf,
  392. cpg->sram + SRAM_DATA_OUT_START + sram_offset,
  393. dst_copy);
  394. sram_offset += dst_copy;
  395. cpg->p.sg_dst_left -= dst_copy;
  396. need_copy_len -= dst_copy;
  397. cpg->p.dst_start += dst_copy;
  398. } while (need_copy_len > 0);
  399. }
  400. cpg->p.crypt_len = 0;
  401. BUG_ON(cpg->eng_st != ENGINE_W_DEQUEUE);
  402. if (cpg->p.hw_processed_bytes < cpg->p.hw_nbytes) {
  403. /* process next scatter list entry */
  404. cpg->eng_st = ENGINE_BUSY;
  405. cpg->p.process(0);
  406. } else {
  407. cpg->p.complete();
  408. cpg->eng_st = ENGINE_IDLE;
  409. local_bh_disable();
  410. req->complete(req, 0);
  411. local_bh_enable();
  412. }
  413. }
  414. static int count_sgs(struct scatterlist *sl, unsigned int total_bytes)
  415. {
  416. int i = 0;
  417. size_t cur_len;
  418. while (1) {
  419. cur_len = sl[i].length;
  420. ++i;
  421. if (total_bytes > cur_len)
  422. total_bytes -= cur_len;
  423. else
  424. break;
  425. }
  426. return i;
  427. }
  428. static void mv_start_new_crypt_req(struct ablkcipher_request *req)
  429. {
  430. struct req_progress *p = &cpg->p;
  431. int num_sgs;
  432. cpg->cur_req = &req->base;
  433. memset(p, 0, sizeof(struct req_progress));
  434. p->hw_nbytes = req->nbytes;
  435. p->complete = mv_crypto_algo_completion;
  436. p->process = mv_process_current_q;
  437. p->copy_back = 1;
  438. num_sgs = count_sgs(req->src, req->nbytes);
  439. sg_miter_start(&p->src_sg_it, req->src, num_sgs, SG_MITER_FROM_SG);
  440. num_sgs = count_sgs(req->dst, req->nbytes);
  441. sg_miter_start(&p->dst_sg_it, req->dst, num_sgs, SG_MITER_TO_SG);
  442. mv_process_current_q(1);
  443. }
  444. static void mv_start_new_hash_req(struct ahash_request *req)
  445. {
  446. struct req_progress *p = &cpg->p;
  447. struct mv_req_hash_ctx *ctx = ahash_request_ctx(req);
  448. const struct mv_tfm_hash_ctx *tfm_ctx = crypto_tfm_ctx(req->base.tfm);
  449. int num_sgs, hw_bytes, old_extra_bytes, rc;
  450. cpg->cur_req = &req->base;
  451. memset(p, 0, sizeof(struct req_progress));
  452. hw_bytes = req->nbytes + ctx->extra_bytes;
  453. old_extra_bytes = ctx->extra_bytes;
  454. if (unlikely(ctx->extra_bytes)) {
  455. memcpy(cpg->sram + SRAM_DATA_IN_START, ctx->buffer,
  456. ctx->extra_bytes);
  457. p->crypt_len = ctx->extra_bytes;
  458. }
  459. memcpy(cpg->sram + SRAM_HMAC_IV_IN, tfm_ctx->ivs, sizeof(tfm_ctx->ivs));
  460. if (unlikely(!ctx->first_hash)) {
  461. writel(ctx->state[0], cpg->reg + DIGEST_INITIAL_VAL_A);
  462. writel(ctx->state[1], cpg->reg + DIGEST_INITIAL_VAL_B);
  463. writel(ctx->state[2], cpg->reg + DIGEST_INITIAL_VAL_C);
  464. writel(ctx->state[3], cpg->reg + DIGEST_INITIAL_VAL_D);
  465. writel(ctx->state[4], cpg->reg + DIGEST_INITIAL_VAL_E);
  466. }
  467. ctx->extra_bytes = hw_bytes % SHA1_BLOCK_SIZE;
  468. if (ctx->extra_bytes != 0
  469. && (!ctx->last_chunk || ctx->count > MAX_HW_HASH_SIZE))
  470. hw_bytes -= ctx->extra_bytes;
  471. else
  472. ctx->extra_bytes = 0;
  473. num_sgs = count_sgs(req->src, req->nbytes);
  474. sg_miter_start(&p->src_sg_it, req->src, num_sgs, SG_MITER_FROM_SG);
  475. if (hw_bytes) {
  476. p->hw_nbytes = hw_bytes;
  477. p->complete = mv_hash_algo_completion;
  478. p->process = mv_process_hash_current;
  479. mv_process_hash_current(1);
  480. } else {
  481. copy_src_to_buf(p, ctx->buffer + old_extra_bytes,
  482. ctx->extra_bytes - old_extra_bytes);
  483. sg_miter_stop(&p->src_sg_it);
  484. if (ctx->last_chunk)
  485. rc = mv_hash_final_fallback(req);
  486. else
  487. rc = 0;
  488. cpg->eng_st = ENGINE_IDLE;
  489. local_bh_disable();
  490. req->base.complete(&req->base, rc);
  491. local_bh_enable();
  492. }
  493. }
  494. static int queue_manag(void *data)
  495. {
  496. cpg->eng_st = ENGINE_IDLE;
  497. do {
  498. struct crypto_async_request *async_req = NULL;
  499. struct crypto_async_request *backlog;
  500. __set_current_state(TASK_INTERRUPTIBLE);
  501. if (cpg->eng_st == ENGINE_W_DEQUEUE)
  502. dequeue_complete_req();
  503. spin_lock_irq(&cpg->lock);
  504. if (cpg->eng_st == ENGINE_IDLE) {
  505. backlog = crypto_get_backlog(&cpg->queue);
  506. async_req = crypto_dequeue_request(&cpg->queue);
  507. if (async_req) {
  508. BUG_ON(cpg->eng_st != ENGINE_IDLE);
  509. cpg->eng_st = ENGINE_BUSY;
  510. }
  511. }
  512. spin_unlock_irq(&cpg->lock);
  513. if (backlog) {
  514. backlog->complete(backlog, -EINPROGRESS);
  515. backlog = NULL;
  516. }
  517. if (async_req) {
  518. if (async_req->tfm->__crt_alg->cra_type !=
  519. &crypto_ahash_type) {
  520. struct ablkcipher_request *req =
  521. ablkcipher_request_cast(async_req);
  522. mv_start_new_crypt_req(req);
  523. } else {
  524. struct ahash_request *req =
  525. ahash_request_cast(async_req);
  526. mv_start_new_hash_req(req);
  527. }
  528. async_req = NULL;
  529. }
  530. schedule();
  531. } while (!kthread_should_stop());
  532. return 0;
  533. }
  534. static int mv_handle_req(struct crypto_async_request *req)
  535. {
  536. unsigned long flags;
  537. int ret;
  538. spin_lock_irqsave(&cpg->lock, flags);
  539. ret = crypto_enqueue_request(&cpg->queue, req);
  540. spin_unlock_irqrestore(&cpg->lock, flags);
  541. wake_up_process(cpg->queue_th);
  542. return ret;
  543. }
  544. static int mv_enc_aes_ecb(struct ablkcipher_request *req)
  545. {
  546. struct mv_req_ctx *req_ctx = ablkcipher_request_ctx(req);
  547. req_ctx->op = COP_AES_ECB;
  548. req_ctx->decrypt = 0;
  549. return mv_handle_req(&req->base);
  550. }
  551. static int mv_dec_aes_ecb(struct ablkcipher_request *req)
  552. {
  553. struct mv_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  554. struct mv_req_ctx *req_ctx = ablkcipher_request_ctx(req);
  555. req_ctx->op = COP_AES_ECB;
  556. req_ctx->decrypt = 1;
  557. compute_aes_dec_key(ctx);
  558. return mv_handle_req(&req->base);
  559. }
  560. static int mv_enc_aes_cbc(struct ablkcipher_request *req)
  561. {
  562. struct mv_req_ctx *req_ctx = ablkcipher_request_ctx(req);
  563. req_ctx->op = COP_AES_CBC;
  564. req_ctx->decrypt = 0;
  565. return mv_handle_req(&req->base);
  566. }
  567. static int mv_dec_aes_cbc(struct ablkcipher_request *req)
  568. {
  569. struct mv_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  570. struct mv_req_ctx *req_ctx = ablkcipher_request_ctx(req);
  571. req_ctx->op = COP_AES_CBC;
  572. req_ctx->decrypt = 1;
  573. compute_aes_dec_key(ctx);
  574. return mv_handle_req(&req->base);
  575. }
  576. static int mv_cra_init(struct crypto_tfm *tfm)
  577. {
  578. tfm->crt_ablkcipher.reqsize = sizeof(struct mv_req_ctx);
  579. return 0;
  580. }
  581. static void mv_init_hash_req_ctx(struct mv_req_hash_ctx *ctx, int op,
  582. int is_last, unsigned int req_len,
  583. int count_add)
  584. {
  585. memset(ctx, 0, sizeof(*ctx));
  586. ctx->op = op;
  587. ctx->count = req_len;
  588. ctx->first_hash = 1;
  589. ctx->last_chunk = is_last;
  590. ctx->count_add = count_add;
  591. }
  592. static void mv_update_hash_req_ctx(struct mv_req_hash_ctx *ctx, int is_last,
  593. unsigned req_len)
  594. {
  595. ctx->last_chunk = is_last;
  596. ctx->count += req_len;
  597. }
  598. static int mv_hash_init(struct ahash_request *req)
  599. {
  600. const struct mv_tfm_hash_ctx *tfm_ctx = crypto_tfm_ctx(req->base.tfm);
  601. mv_init_hash_req_ctx(ahash_request_ctx(req), tfm_ctx->op, 0, 0,
  602. tfm_ctx->count_add);
  603. return 0;
  604. }
  605. static int mv_hash_update(struct ahash_request *req)
  606. {
  607. if (!req->nbytes)
  608. return 0;
  609. mv_update_hash_req_ctx(ahash_request_ctx(req), 0, req->nbytes);
  610. return mv_handle_req(&req->base);
  611. }
  612. static int mv_hash_final(struct ahash_request *req)
  613. {
  614. struct mv_req_hash_ctx *ctx = ahash_request_ctx(req);
  615. /* dummy buffer of 4 bytes */
  616. sg_init_one(&ctx->dummysg, ctx->buffer, 4);
  617. /* I think I'm allowed to do that... */
  618. ahash_request_set_crypt(req, &ctx->dummysg, req->result, 0);
  619. mv_update_hash_req_ctx(ctx, 1, 0);
  620. return mv_handle_req(&req->base);
  621. }
  622. static int mv_hash_finup(struct ahash_request *req)
  623. {
  624. mv_update_hash_req_ctx(ahash_request_ctx(req), 1, req->nbytes);
  625. return mv_handle_req(&req->base);
  626. }
  627. static int mv_hash_digest(struct ahash_request *req)
  628. {
  629. const struct mv_tfm_hash_ctx *tfm_ctx = crypto_tfm_ctx(req->base.tfm);
  630. mv_init_hash_req_ctx(ahash_request_ctx(req), tfm_ctx->op, 1,
  631. req->nbytes, tfm_ctx->count_add);
  632. return mv_handle_req(&req->base);
  633. }
  634. static void mv_hash_init_ivs(struct mv_tfm_hash_ctx *ctx, const void *istate,
  635. const void *ostate)
  636. {
  637. const struct sha1_state *isha1_state = istate, *osha1_state = ostate;
  638. int i;
  639. for (i = 0; i < 5; i++) {
  640. ctx->ivs[i] = cpu_to_be32(isha1_state->state[i]);
  641. ctx->ivs[i + 5] = cpu_to_be32(osha1_state->state[i]);
  642. }
  643. }
  644. static int mv_hash_setkey(struct crypto_ahash *tfm, const u8 * key,
  645. unsigned int keylen)
  646. {
  647. int rc;
  648. struct mv_tfm_hash_ctx *ctx = crypto_tfm_ctx(&tfm->base);
  649. int bs, ds, ss;
  650. if (!ctx->base_hash)
  651. return 0;
  652. rc = crypto_shash_setkey(ctx->fallback, key, keylen);
  653. if (rc)
  654. return rc;
  655. /* Can't see a way to extract the ipad/opad from the fallback tfm
  656. so I'm basically copying code from the hmac module */
  657. bs = crypto_shash_blocksize(ctx->base_hash);
  658. ds = crypto_shash_digestsize(ctx->base_hash);
  659. ss = crypto_shash_statesize(ctx->base_hash);
  660. {
  661. struct {
  662. struct shash_desc shash;
  663. char ctx[crypto_shash_descsize(ctx->base_hash)];
  664. } desc;
  665. unsigned int i;
  666. char ipad[ss];
  667. char opad[ss];
  668. desc.shash.tfm = ctx->base_hash;
  669. desc.shash.flags = crypto_shash_get_flags(ctx->base_hash) &
  670. CRYPTO_TFM_REQ_MAY_SLEEP;
  671. if (keylen > bs) {
  672. int err;
  673. err =
  674. crypto_shash_digest(&desc.shash, key, keylen, ipad);
  675. if (err)
  676. return err;
  677. keylen = ds;
  678. } else
  679. memcpy(ipad, key, keylen);
  680. memset(ipad + keylen, 0, bs - keylen);
  681. memcpy(opad, ipad, bs);
  682. for (i = 0; i < bs; i++) {
  683. ipad[i] ^= 0x36;
  684. opad[i] ^= 0x5c;
  685. }
  686. rc = crypto_shash_init(&desc.shash) ? :
  687. crypto_shash_update(&desc.shash, ipad, bs) ? :
  688. crypto_shash_export(&desc.shash, ipad) ? :
  689. crypto_shash_init(&desc.shash) ? :
  690. crypto_shash_update(&desc.shash, opad, bs) ? :
  691. crypto_shash_export(&desc.shash, opad);
  692. if (rc == 0)
  693. mv_hash_init_ivs(ctx, ipad, opad);
  694. return rc;
  695. }
  696. }
  697. static int mv_cra_hash_init(struct crypto_tfm *tfm, const char *base_hash_name,
  698. enum hash_op op, int count_add)
  699. {
  700. const char *fallback_driver_name = tfm->__crt_alg->cra_name;
  701. struct mv_tfm_hash_ctx *ctx = crypto_tfm_ctx(tfm);
  702. struct crypto_shash *fallback_tfm = NULL;
  703. struct crypto_shash *base_hash = NULL;
  704. int err = -ENOMEM;
  705. ctx->op = op;
  706. ctx->count_add = count_add;
  707. /* Allocate a fallback and abort if it failed. */
  708. fallback_tfm = crypto_alloc_shash(fallback_driver_name, 0,
  709. CRYPTO_ALG_NEED_FALLBACK);
  710. if (IS_ERR(fallback_tfm)) {
  711. printk(KERN_WARNING MV_CESA
  712. "Fallback driver '%s' could not be loaded!\n",
  713. fallback_driver_name);
  714. err = PTR_ERR(fallback_tfm);
  715. goto out;
  716. }
  717. ctx->fallback = fallback_tfm;
  718. if (base_hash_name) {
  719. /* Allocate a hash to compute the ipad/opad of hmac. */
  720. base_hash = crypto_alloc_shash(base_hash_name, 0,
  721. CRYPTO_ALG_NEED_FALLBACK);
  722. if (IS_ERR(base_hash)) {
  723. printk(KERN_WARNING MV_CESA
  724. "Base driver '%s' could not be loaded!\n",
  725. base_hash_name);
  726. err = PTR_ERR(base_hash);
  727. goto err_bad_base;
  728. }
  729. }
  730. ctx->base_hash = base_hash;
  731. crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
  732. sizeof(struct mv_req_hash_ctx) +
  733. crypto_shash_descsize(ctx->fallback));
  734. return 0;
  735. err_bad_base:
  736. crypto_free_shash(fallback_tfm);
  737. out:
  738. return err;
  739. }
  740. static void mv_cra_hash_exit(struct crypto_tfm *tfm)
  741. {
  742. struct mv_tfm_hash_ctx *ctx = crypto_tfm_ctx(tfm);
  743. crypto_free_shash(ctx->fallback);
  744. if (ctx->base_hash)
  745. crypto_free_shash(ctx->base_hash);
  746. }
  747. static int mv_cra_hash_sha1_init(struct crypto_tfm *tfm)
  748. {
  749. return mv_cra_hash_init(tfm, NULL, COP_SHA1, 0);
  750. }
  751. static int mv_cra_hash_hmac_sha1_init(struct crypto_tfm *tfm)
  752. {
  753. return mv_cra_hash_init(tfm, "sha1", COP_HMAC_SHA1, SHA1_BLOCK_SIZE);
  754. }
  755. irqreturn_t crypto_int(int irq, void *priv)
  756. {
  757. u32 val;
  758. val = readl(cpg->reg + SEC_ACCEL_INT_STATUS);
  759. if (!(val & SEC_INT_ACCEL0_DONE))
  760. return IRQ_NONE;
  761. val &= ~SEC_INT_ACCEL0_DONE;
  762. writel(val, cpg->reg + FPGA_INT_STATUS);
  763. writel(val, cpg->reg + SEC_ACCEL_INT_STATUS);
  764. BUG_ON(cpg->eng_st != ENGINE_BUSY);
  765. cpg->eng_st = ENGINE_W_DEQUEUE;
  766. wake_up_process(cpg->queue_th);
  767. return IRQ_HANDLED;
  768. }
  769. struct crypto_alg mv_aes_alg_ecb = {
  770. .cra_name = "ecb(aes)",
  771. .cra_driver_name = "mv-ecb-aes",
  772. .cra_priority = 300,
  773. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  774. .cra_blocksize = 16,
  775. .cra_ctxsize = sizeof(struct mv_ctx),
  776. .cra_alignmask = 0,
  777. .cra_type = &crypto_ablkcipher_type,
  778. .cra_module = THIS_MODULE,
  779. .cra_init = mv_cra_init,
  780. .cra_u = {
  781. .ablkcipher = {
  782. .min_keysize = AES_MIN_KEY_SIZE,
  783. .max_keysize = AES_MAX_KEY_SIZE,
  784. .setkey = mv_setkey_aes,
  785. .encrypt = mv_enc_aes_ecb,
  786. .decrypt = mv_dec_aes_ecb,
  787. },
  788. },
  789. };
  790. struct crypto_alg mv_aes_alg_cbc = {
  791. .cra_name = "cbc(aes)",
  792. .cra_driver_name = "mv-cbc-aes",
  793. .cra_priority = 300,
  794. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  795. .cra_blocksize = AES_BLOCK_SIZE,
  796. .cra_ctxsize = sizeof(struct mv_ctx),
  797. .cra_alignmask = 0,
  798. .cra_type = &crypto_ablkcipher_type,
  799. .cra_module = THIS_MODULE,
  800. .cra_init = mv_cra_init,
  801. .cra_u = {
  802. .ablkcipher = {
  803. .ivsize = AES_BLOCK_SIZE,
  804. .min_keysize = AES_MIN_KEY_SIZE,
  805. .max_keysize = AES_MAX_KEY_SIZE,
  806. .setkey = mv_setkey_aes,
  807. .encrypt = mv_enc_aes_cbc,
  808. .decrypt = mv_dec_aes_cbc,
  809. },
  810. },
  811. };
  812. struct ahash_alg mv_sha1_alg = {
  813. .init = mv_hash_init,
  814. .update = mv_hash_update,
  815. .final = mv_hash_final,
  816. .finup = mv_hash_finup,
  817. .digest = mv_hash_digest,
  818. .halg = {
  819. .digestsize = SHA1_DIGEST_SIZE,
  820. .base = {
  821. .cra_name = "sha1",
  822. .cra_driver_name = "mv-sha1",
  823. .cra_priority = 300,
  824. .cra_flags =
  825. CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK,
  826. .cra_blocksize = SHA1_BLOCK_SIZE,
  827. .cra_ctxsize = sizeof(struct mv_tfm_hash_ctx),
  828. .cra_init = mv_cra_hash_sha1_init,
  829. .cra_exit = mv_cra_hash_exit,
  830. .cra_module = THIS_MODULE,
  831. }
  832. }
  833. };
  834. struct ahash_alg mv_hmac_sha1_alg = {
  835. .init = mv_hash_init,
  836. .update = mv_hash_update,
  837. .final = mv_hash_final,
  838. .finup = mv_hash_finup,
  839. .digest = mv_hash_digest,
  840. .setkey = mv_hash_setkey,
  841. .halg = {
  842. .digestsize = SHA1_DIGEST_SIZE,
  843. .base = {
  844. .cra_name = "hmac(sha1)",
  845. .cra_driver_name = "mv-hmac-sha1",
  846. .cra_priority = 300,
  847. .cra_flags =
  848. CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK,
  849. .cra_blocksize = SHA1_BLOCK_SIZE,
  850. .cra_ctxsize = sizeof(struct mv_tfm_hash_ctx),
  851. .cra_init = mv_cra_hash_hmac_sha1_init,
  852. .cra_exit = mv_cra_hash_exit,
  853. .cra_module = THIS_MODULE,
  854. }
  855. }
  856. };
  857. static int mv_probe(struct platform_device *pdev)
  858. {
  859. struct crypto_priv *cp;
  860. struct resource *res;
  861. int irq;
  862. int ret;
  863. if (cpg) {
  864. printk(KERN_ERR MV_CESA "Second crypto dev?\n");
  865. return -EEXIST;
  866. }
  867. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "regs");
  868. if (!res)
  869. return -ENXIO;
  870. cp = kzalloc(sizeof(*cp), GFP_KERNEL);
  871. if (!cp)
  872. return -ENOMEM;
  873. spin_lock_init(&cp->lock);
  874. crypto_init_queue(&cp->queue, 50);
  875. cp->reg = ioremap(res->start, resource_size(res));
  876. if (!cp->reg) {
  877. ret = -ENOMEM;
  878. goto err;
  879. }
  880. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "sram");
  881. if (!res) {
  882. ret = -ENXIO;
  883. goto err_unmap_reg;
  884. }
  885. cp->sram_size = resource_size(res);
  886. cp->max_req_size = cp->sram_size - SRAM_CFG_SPACE;
  887. cp->sram = ioremap(res->start, cp->sram_size);
  888. if (!cp->sram) {
  889. ret = -ENOMEM;
  890. goto err_unmap_reg;
  891. }
  892. irq = platform_get_irq(pdev, 0);
  893. if (irq < 0 || irq == NO_IRQ) {
  894. ret = irq;
  895. goto err_unmap_sram;
  896. }
  897. cp->irq = irq;
  898. platform_set_drvdata(pdev, cp);
  899. cpg = cp;
  900. cp->queue_th = kthread_run(queue_manag, cp, "mv_crypto");
  901. if (IS_ERR(cp->queue_th)) {
  902. ret = PTR_ERR(cp->queue_th);
  903. goto err_unmap_sram;
  904. }
  905. ret = request_irq(irq, crypto_int, IRQF_DISABLED, dev_name(&pdev->dev),
  906. cp);
  907. if (ret)
  908. goto err_thread;
  909. writel(SEC_INT_ACCEL0_DONE, cpg->reg + SEC_ACCEL_INT_MASK);
  910. writel(SEC_CFG_STOP_DIG_ERR, cpg->reg + SEC_ACCEL_CFG);
  911. writel(SRAM_CONFIG, cpg->reg + SEC_ACCEL_DESC_P0);
  912. ret = crypto_register_alg(&mv_aes_alg_ecb);
  913. if (ret)
  914. goto err_irq;
  915. ret = crypto_register_alg(&mv_aes_alg_cbc);
  916. if (ret)
  917. goto err_unreg_ecb;
  918. ret = crypto_register_ahash(&mv_sha1_alg);
  919. if (ret == 0)
  920. cpg->has_sha1 = 1;
  921. else
  922. printk(KERN_WARNING MV_CESA "Could not register sha1 driver\n");
  923. ret = crypto_register_ahash(&mv_hmac_sha1_alg);
  924. if (ret == 0) {
  925. cpg->has_hmac_sha1 = 1;
  926. } else {
  927. printk(KERN_WARNING MV_CESA
  928. "Could not register hmac-sha1 driver\n");
  929. }
  930. return 0;
  931. err_unreg_ecb:
  932. crypto_unregister_alg(&mv_aes_alg_ecb);
  933. err_irq:
  934. free_irq(irq, cp);
  935. err_thread:
  936. kthread_stop(cp->queue_th);
  937. err_unmap_sram:
  938. iounmap(cp->sram);
  939. err_unmap_reg:
  940. iounmap(cp->reg);
  941. err:
  942. kfree(cp);
  943. cpg = NULL;
  944. platform_set_drvdata(pdev, NULL);
  945. return ret;
  946. }
  947. static int mv_remove(struct platform_device *pdev)
  948. {
  949. struct crypto_priv *cp = platform_get_drvdata(pdev);
  950. crypto_unregister_alg(&mv_aes_alg_ecb);
  951. crypto_unregister_alg(&mv_aes_alg_cbc);
  952. if (cp->has_sha1)
  953. crypto_unregister_ahash(&mv_sha1_alg);
  954. if (cp->has_hmac_sha1)
  955. crypto_unregister_ahash(&mv_hmac_sha1_alg);
  956. kthread_stop(cp->queue_th);
  957. free_irq(cp->irq, cp);
  958. memset(cp->sram, 0, cp->sram_size);
  959. iounmap(cp->sram);
  960. iounmap(cp->reg);
  961. kfree(cp);
  962. cpg = NULL;
  963. return 0;
  964. }
  965. static struct platform_driver marvell_crypto = {
  966. .probe = mv_probe,
  967. .remove = mv_remove,
  968. .driver = {
  969. .owner = THIS_MODULE,
  970. .name = "mv_crypto",
  971. },
  972. };
  973. MODULE_ALIAS("platform:mv_crypto");
  974. static int __init mv_crypto_init(void)
  975. {
  976. return platform_driver_register(&marvell_crypto);
  977. }
  978. module_init(mv_crypto_init);
  979. static void __exit mv_crypto_exit(void)
  980. {
  981. platform_driver_unregister(&marvell_crypto);
  982. }
  983. module_exit(mv_crypto_exit);
  984. MODULE_AUTHOR("Sebastian Andrzej Siewior <sebastian@breakpoint.cc>");
  985. MODULE_DESCRIPTION("Support for Marvell's cryptographic engine");
  986. MODULE_LICENSE("GPL");