mv_cesa.c 28 KB

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