mv_cesa.c 27 KB

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