mv_cesa.c 27 KB

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