mv_cesa.c 28 KB

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