mv_cesa.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146
  1. /*
  2. * Support for Marvell's crypto engine which can be found on some Orion5X
  3. * boards.
  4. *
  5. * Author: Sebastian Andrzej Siewior < sebastian at breakpoint dot cc >
  6. * License: GPLv2
  7. *
  8. */
  9. #include <crypto/aes.h>
  10. #include <crypto/algapi.h>
  11. #include <linux/crypto.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/io.h>
  14. #include <linux/kthread.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/scatterlist.h>
  17. #include <linux/slab.h>
  18. #include <crypto/internal/hash.h>
  19. #include <crypto/sha.h>
  20. #include "mv_cesa.h"
  21. #define MV_CESA "MV-CESA:"
  22. #define MAX_HW_HASH_SIZE 0xFFFF
  23. /*
  24. * STM:
  25. * /---------------------------------------\
  26. * | | request complete
  27. * \./ |
  28. * IDLE -> new request -> BUSY -> done -> DEQUEUE
  29. * /°\ |
  30. * | | more scatter entries
  31. * \________________/
  32. */
  33. enum engine_status {
  34. ENGINE_IDLE,
  35. ENGINE_BUSY,
  36. ENGINE_W_DEQUEUE,
  37. };
  38. /**
  39. * struct req_progress - used for every crypt request
  40. * @src_sg_it: sg iterator for src
  41. * @dst_sg_it: sg iterator for dst
  42. * @sg_src_left: bytes left in src to process (scatter list)
  43. * @src_start: offset to add to src start position (scatter list)
  44. * @crypt_len: length of current hw crypt/hash process
  45. * @hw_nbytes: total bytes to process in hw for this request
  46. * @copy_back: whether to copy data back (crypt) or not (hash)
  47. * @sg_dst_left: bytes left dst to process in this scatter list
  48. * @dst_start: offset to add to dst start position (scatter list)
  49. * @hw_processed_bytes: number of bytes processed by hw (request).
  50. *
  51. * sg helper are used to iterate over the scatterlist. Since the size of the
  52. * SRAM may be less than the scatter size, this struct struct is used to keep
  53. * track of progress within current scatterlist.
  54. */
  55. struct req_progress {
  56. struct sg_mapping_iter src_sg_it;
  57. struct sg_mapping_iter dst_sg_it;
  58. void (*complete) (void);
  59. void (*process) (int is_first);
  60. /* src mostly */
  61. int sg_src_left;
  62. int src_start;
  63. int crypt_len;
  64. int hw_nbytes;
  65. /* dst mostly */
  66. int copy_back;
  67. int sg_dst_left;
  68. int dst_start;
  69. int hw_processed_bytes;
  70. };
  71. struct crypto_priv {
  72. void __iomem *reg;
  73. void __iomem *sram;
  74. int irq;
  75. struct task_struct *queue_th;
  76. /* the lock protects queue and eng_st */
  77. spinlock_t lock;
  78. struct crypto_queue queue;
  79. enum engine_status eng_st;
  80. struct crypto_async_request *cur_req;
  81. struct req_progress p;
  82. int max_req_size;
  83. int sram_size;
  84. int has_sha1;
  85. int has_hmac_sha1;
  86. };
  87. static struct crypto_priv *cpg;
  88. struct mv_ctx {
  89. u8 aes_enc_key[AES_KEY_LEN];
  90. u32 aes_dec_key[8];
  91. int key_len;
  92. u32 need_calc_aes_dkey;
  93. };
  94. enum crypto_op {
  95. COP_AES_ECB,
  96. COP_AES_CBC,
  97. };
  98. struct mv_req_ctx {
  99. enum crypto_op op;
  100. int decrypt;
  101. };
  102. enum hash_op {
  103. COP_SHA1,
  104. COP_HMAC_SHA1
  105. };
  106. struct mv_tfm_hash_ctx {
  107. struct crypto_shash *fallback;
  108. struct crypto_shash *base_hash;
  109. u32 ivs[2 * SHA1_DIGEST_SIZE / 4];
  110. int count_add;
  111. enum hash_op op;
  112. };
  113. struct mv_req_hash_ctx {
  114. u64 count;
  115. u32 state[SHA1_DIGEST_SIZE / 4];
  116. u8 buffer[SHA1_BLOCK_SIZE];
  117. int first_hash; /* marks that we don't have previous state */
  118. int last_chunk; /* marks that this is the 'final' request */
  119. int extra_bytes; /* unprocessed bytes in buffer */
  120. enum hash_op op;
  121. int count_add;
  122. struct scatterlist dummysg;
  123. };
  124. static void compute_aes_dec_key(struct mv_ctx *ctx)
  125. {
  126. struct crypto_aes_ctx gen_aes_key;
  127. int key_pos;
  128. if (!ctx->need_calc_aes_dkey)
  129. return;
  130. crypto_aes_expand_key(&gen_aes_key, ctx->aes_enc_key, ctx->key_len);
  131. key_pos = ctx->key_len + 24;
  132. memcpy(ctx->aes_dec_key, &gen_aes_key.key_enc[key_pos], 4 * 4);
  133. switch (ctx->key_len) {
  134. case AES_KEYSIZE_256:
  135. key_pos -= 2;
  136. /* fall */
  137. case AES_KEYSIZE_192:
  138. key_pos -= 2;
  139. memcpy(&ctx->aes_dec_key[4], &gen_aes_key.key_enc[key_pos],
  140. 4 * 4);
  141. break;
  142. }
  143. ctx->need_calc_aes_dkey = 0;
  144. }
  145. static int mv_setkey_aes(struct crypto_ablkcipher *cipher, const u8 *key,
  146. unsigned int len)
  147. {
  148. struct crypto_tfm *tfm = crypto_ablkcipher_tfm(cipher);
  149. struct mv_ctx *ctx = crypto_tfm_ctx(tfm);
  150. switch (len) {
  151. case AES_KEYSIZE_128:
  152. case AES_KEYSIZE_192:
  153. case AES_KEYSIZE_256:
  154. break;
  155. default:
  156. crypto_ablkcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
  157. return -EINVAL;
  158. }
  159. ctx->key_len = len;
  160. ctx->need_calc_aes_dkey = 1;
  161. memcpy(ctx->aes_enc_key, key, AES_KEY_LEN);
  162. return 0;
  163. }
  164. static void copy_src_to_buf(struct req_progress *p, char *dbuf, int len)
  165. {
  166. int ret;
  167. void *sbuf;
  168. int 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 (1) {
  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. 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. ctx->extra_bytes = hw_bytes % SHA1_BLOCK_SIZE;
  461. if (ctx->extra_bytes != 0
  462. && (!ctx->last_chunk || ctx->count > MAX_HW_HASH_SIZE))
  463. hw_bytes -= ctx->extra_bytes;
  464. else
  465. ctx->extra_bytes = 0;
  466. num_sgs = count_sgs(req->src, req->nbytes);
  467. sg_miter_start(&p->src_sg_it, req->src, num_sgs, SG_MITER_FROM_SG);
  468. if (hw_bytes) {
  469. p->hw_nbytes = hw_bytes;
  470. p->complete = mv_hash_algo_completion;
  471. p->process = mv_process_hash_current;
  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. /* dummy buffer of 4 bytes */
  609. sg_init_one(&ctx->dummysg, ctx->buffer, 4);
  610. /* I think I'm allowed to do that... */
  611. ahash_request_set_crypt(req, &ctx->dummysg, req->result, 0);
  612. mv_update_hash_req_ctx(ctx, 1, 0);
  613. return mv_handle_req(&req->base);
  614. }
  615. static int mv_hash_finup(struct ahash_request *req)
  616. {
  617. mv_update_hash_req_ctx(ahash_request_ctx(req), 1, req->nbytes);
  618. return mv_handle_req(&req->base);
  619. }
  620. static int mv_hash_digest(struct ahash_request *req)
  621. {
  622. const struct mv_tfm_hash_ctx *tfm_ctx = crypto_tfm_ctx(req->base.tfm);
  623. mv_init_hash_req_ctx(ahash_request_ctx(req), tfm_ctx->op, 1,
  624. req->nbytes, tfm_ctx->count_add);
  625. return mv_handle_req(&req->base);
  626. }
  627. static void mv_hash_init_ivs(struct mv_tfm_hash_ctx *ctx, const void *istate,
  628. const void *ostate)
  629. {
  630. const struct sha1_state *isha1_state = istate, *osha1_state = ostate;
  631. int i;
  632. for (i = 0; i < 5; i++) {
  633. ctx->ivs[i] = cpu_to_be32(isha1_state->state[i]);
  634. ctx->ivs[i + 5] = cpu_to_be32(osha1_state->state[i]);
  635. }
  636. }
  637. static int mv_hash_setkey(struct crypto_ahash *tfm, const u8 * key,
  638. unsigned int keylen)
  639. {
  640. int rc;
  641. struct mv_tfm_hash_ctx *ctx = crypto_tfm_ctx(&tfm->base);
  642. int bs, ds, ss;
  643. if (!ctx->base_hash)
  644. return 0;
  645. rc = crypto_shash_setkey(ctx->fallback, key, keylen);
  646. if (rc)
  647. return rc;
  648. /* Can't see a way to extract the ipad/opad from the fallback tfm
  649. so I'm basically copying code from the hmac module */
  650. bs = crypto_shash_blocksize(ctx->base_hash);
  651. ds = crypto_shash_digestsize(ctx->base_hash);
  652. ss = crypto_shash_statesize(ctx->base_hash);
  653. {
  654. struct {
  655. struct shash_desc shash;
  656. char ctx[crypto_shash_descsize(ctx->base_hash)];
  657. } desc;
  658. unsigned int i;
  659. char ipad[ss];
  660. char opad[ss];
  661. desc.shash.tfm = ctx->base_hash;
  662. desc.shash.flags = crypto_shash_get_flags(ctx->base_hash) &
  663. CRYPTO_TFM_REQ_MAY_SLEEP;
  664. if (keylen > bs) {
  665. int err;
  666. err =
  667. crypto_shash_digest(&desc.shash, key, keylen, ipad);
  668. if (err)
  669. return err;
  670. keylen = ds;
  671. } else
  672. memcpy(ipad, key, keylen);
  673. memset(ipad + keylen, 0, bs - keylen);
  674. memcpy(opad, ipad, bs);
  675. for (i = 0; i < bs; i++) {
  676. ipad[i] ^= 0x36;
  677. opad[i] ^= 0x5c;
  678. }
  679. rc = crypto_shash_init(&desc.shash) ? :
  680. crypto_shash_update(&desc.shash, ipad, bs) ? :
  681. crypto_shash_export(&desc.shash, ipad) ? :
  682. crypto_shash_init(&desc.shash) ? :
  683. crypto_shash_update(&desc.shash, opad, bs) ? :
  684. crypto_shash_export(&desc.shash, opad);
  685. if (rc == 0)
  686. mv_hash_init_ivs(ctx, ipad, opad);
  687. return rc;
  688. }
  689. }
  690. static int mv_cra_hash_init(struct crypto_tfm *tfm, const char *base_hash_name,
  691. enum hash_op op, int count_add)
  692. {
  693. const char *fallback_driver_name = tfm->__crt_alg->cra_name;
  694. struct mv_tfm_hash_ctx *ctx = crypto_tfm_ctx(tfm);
  695. struct crypto_shash *fallback_tfm = NULL;
  696. struct crypto_shash *base_hash = NULL;
  697. int err = -ENOMEM;
  698. ctx->op = op;
  699. ctx->count_add = count_add;
  700. /* Allocate a fallback and abort if it failed. */
  701. fallback_tfm = crypto_alloc_shash(fallback_driver_name, 0,
  702. CRYPTO_ALG_NEED_FALLBACK);
  703. if (IS_ERR(fallback_tfm)) {
  704. printk(KERN_WARNING MV_CESA
  705. "Fallback driver '%s' could not be loaded!\n",
  706. fallback_driver_name);
  707. err = PTR_ERR(fallback_tfm);
  708. goto out;
  709. }
  710. ctx->fallback = fallback_tfm;
  711. if (base_hash_name) {
  712. /* Allocate a hash to compute the ipad/opad of hmac. */
  713. base_hash = crypto_alloc_shash(base_hash_name, 0,
  714. CRYPTO_ALG_NEED_FALLBACK);
  715. if (IS_ERR(base_hash)) {
  716. printk(KERN_WARNING MV_CESA
  717. "Base driver '%s' could not be loaded!\n",
  718. base_hash_name);
  719. err = PTR_ERR(base_hash);
  720. goto err_bad_base;
  721. }
  722. }
  723. ctx->base_hash = base_hash;
  724. crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
  725. sizeof(struct mv_req_hash_ctx) +
  726. crypto_shash_descsize(ctx->fallback));
  727. return 0;
  728. err_bad_base:
  729. crypto_free_shash(fallback_tfm);
  730. out:
  731. return err;
  732. }
  733. static void mv_cra_hash_exit(struct crypto_tfm *tfm)
  734. {
  735. struct mv_tfm_hash_ctx *ctx = crypto_tfm_ctx(tfm);
  736. crypto_free_shash(ctx->fallback);
  737. if (ctx->base_hash)
  738. crypto_free_shash(ctx->base_hash);
  739. }
  740. static int mv_cra_hash_sha1_init(struct crypto_tfm *tfm)
  741. {
  742. return mv_cra_hash_init(tfm, NULL, COP_SHA1, 0);
  743. }
  744. static int mv_cra_hash_hmac_sha1_init(struct crypto_tfm *tfm)
  745. {
  746. return mv_cra_hash_init(tfm, "sha1", COP_HMAC_SHA1, SHA1_BLOCK_SIZE);
  747. }
  748. irqreturn_t crypto_int(int irq, void *priv)
  749. {
  750. u32 val;
  751. val = readl(cpg->reg + SEC_ACCEL_INT_STATUS);
  752. if (!(val & SEC_INT_ACCEL0_DONE))
  753. return IRQ_NONE;
  754. val &= ~SEC_INT_ACCEL0_DONE;
  755. writel(val, cpg->reg + FPGA_INT_STATUS);
  756. writel(val, cpg->reg + SEC_ACCEL_INT_STATUS);
  757. BUG_ON(cpg->eng_st != ENGINE_BUSY);
  758. cpg->eng_st = ENGINE_W_DEQUEUE;
  759. wake_up_process(cpg->queue_th);
  760. return IRQ_HANDLED;
  761. }
  762. struct crypto_alg mv_aes_alg_ecb = {
  763. .cra_name = "ecb(aes)",
  764. .cra_driver_name = "mv-ecb-aes",
  765. .cra_priority = 300,
  766. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  767. .cra_blocksize = 16,
  768. .cra_ctxsize = sizeof(struct mv_ctx),
  769. .cra_alignmask = 0,
  770. .cra_type = &crypto_ablkcipher_type,
  771. .cra_module = THIS_MODULE,
  772. .cra_init = mv_cra_init,
  773. .cra_u = {
  774. .ablkcipher = {
  775. .min_keysize = AES_MIN_KEY_SIZE,
  776. .max_keysize = AES_MAX_KEY_SIZE,
  777. .setkey = mv_setkey_aes,
  778. .encrypt = mv_enc_aes_ecb,
  779. .decrypt = mv_dec_aes_ecb,
  780. },
  781. },
  782. };
  783. struct crypto_alg mv_aes_alg_cbc = {
  784. .cra_name = "cbc(aes)",
  785. .cra_driver_name = "mv-cbc-aes",
  786. .cra_priority = 300,
  787. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  788. .cra_blocksize = AES_BLOCK_SIZE,
  789. .cra_ctxsize = sizeof(struct mv_ctx),
  790. .cra_alignmask = 0,
  791. .cra_type = &crypto_ablkcipher_type,
  792. .cra_module = THIS_MODULE,
  793. .cra_init = mv_cra_init,
  794. .cra_u = {
  795. .ablkcipher = {
  796. .ivsize = AES_BLOCK_SIZE,
  797. .min_keysize = AES_MIN_KEY_SIZE,
  798. .max_keysize = AES_MAX_KEY_SIZE,
  799. .setkey = mv_setkey_aes,
  800. .encrypt = mv_enc_aes_cbc,
  801. .decrypt = mv_dec_aes_cbc,
  802. },
  803. },
  804. };
  805. struct ahash_alg mv_sha1_alg = {
  806. .init = mv_hash_init,
  807. .update = mv_hash_update,
  808. .final = mv_hash_final,
  809. .finup = mv_hash_finup,
  810. .digest = mv_hash_digest,
  811. .halg = {
  812. .digestsize = SHA1_DIGEST_SIZE,
  813. .base = {
  814. .cra_name = "sha1",
  815. .cra_driver_name = "mv-sha1",
  816. .cra_priority = 300,
  817. .cra_flags =
  818. CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK,
  819. .cra_blocksize = SHA1_BLOCK_SIZE,
  820. .cra_ctxsize = sizeof(struct mv_tfm_hash_ctx),
  821. .cra_init = mv_cra_hash_sha1_init,
  822. .cra_exit = mv_cra_hash_exit,
  823. .cra_module = THIS_MODULE,
  824. }
  825. }
  826. };
  827. struct ahash_alg mv_hmac_sha1_alg = {
  828. .init = mv_hash_init,
  829. .update = mv_hash_update,
  830. .final = mv_hash_final,
  831. .finup = mv_hash_finup,
  832. .digest = mv_hash_digest,
  833. .setkey = mv_hash_setkey,
  834. .halg = {
  835. .digestsize = SHA1_DIGEST_SIZE,
  836. .base = {
  837. .cra_name = "hmac(sha1)",
  838. .cra_driver_name = "mv-hmac-sha1",
  839. .cra_priority = 300,
  840. .cra_flags =
  841. CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK,
  842. .cra_blocksize = SHA1_BLOCK_SIZE,
  843. .cra_ctxsize = sizeof(struct mv_tfm_hash_ctx),
  844. .cra_init = mv_cra_hash_hmac_sha1_init,
  845. .cra_exit = mv_cra_hash_exit,
  846. .cra_module = THIS_MODULE,
  847. }
  848. }
  849. };
  850. static int mv_probe(struct platform_device *pdev)
  851. {
  852. struct crypto_priv *cp;
  853. struct resource *res;
  854. int irq;
  855. int ret;
  856. if (cpg) {
  857. printk(KERN_ERR MV_CESA "Second crypto dev?\n");
  858. return -EEXIST;
  859. }
  860. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "regs");
  861. if (!res)
  862. return -ENXIO;
  863. cp = kzalloc(sizeof(*cp), GFP_KERNEL);
  864. if (!cp)
  865. return -ENOMEM;
  866. spin_lock_init(&cp->lock);
  867. crypto_init_queue(&cp->queue, 50);
  868. cp->reg = ioremap(res->start, resource_size(res));
  869. if (!cp->reg) {
  870. ret = -ENOMEM;
  871. goto err;
  872. }
  873. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "sram");
  874. if (!res) {
  875. ret = -ENXIO;
  876. goto err_unmap_reg;
  877. }
  878. cp->sram_size = resource_size(res);
  879. cp->max_req_size = cp->sram_size - SRAM_CFG_SPACE;
  880. cp->sram = ioremap(res->start, cp->sram_size);
  881. if (!cp->sram) {
  882. ret = -ENOMEM;
  883. goto err_unmap_reg;
  884. }
  885. irq = platform_get_irq(pdev, 0);
  886. if (irq < 0 || irq == NO_IRQ) {
  887. ret = irq;
  888. goto err_unmap_sram;
  889. }
  890. cp->irq = irq;
  891. platform_set_drvdata(pdev, cp);
  892. cpg = cp;
  893. cp->queue_th = kthread_run(queue_manag, cp, "mv_crypto");
  894. if (IS_ERR(cp->queue_th)) {
  895. ret = PTR_ERR(cp->queue_th);
  896. goto err_unmap_sram;
  897. }
  898. ret = request_irq(irq, crypto_int, IRQF_DISABLED, dev_name(&pdev->dev),
  899. cp);
  900. if (ret)
  901. goto err_thread;
  902. writel(SEC_INT_ACCEL0_DONE, cpg->reg + SEC_ACCEL_INT_MASK);
  903. writel(SEC_CFG_STOP_DIG_ERR, cpg->reg + SEC_ACCEL_CFG);
  904. writel(SRAM_CONFIG, cpg->reg + SEC_ACCEL_DESC_P0);
  905. ret = crypto_register_alg(&mv_aes_alg_ecb);
  906. if (ret) {
  907. printk(KERN_WARNING MV_CESA
  908. "Could not register aes-ecb driver\n");
  909. goto err_irq;
  910. }
  911. ret = crypto_register_alg(&mv_aes_alg_cbc);
  912. if (ret) {
  913. printk(KERN_WARNING MV_CESA
  914. "Could not register aes-cbc driver\n");
  915. goto err_unreg_ecb;
  916. }
  917. ret = crypto_register_ahash(&mv_sha1_alg);
  918. if (ret == 0)
  919. cpg->has_sha1 = 1;
  920. else
  921. printk(KERN_WARNING MV_CESA "Could not register sha1 driver\n");
  922. ret = crypto_register_ahash(&mv_hmac_sha1_alg);
  923. if (ret == 0) {
  924. cpg->has_hmac_sha1 = 1;
  925. } else {
  926. printk(KERN_WARNING MV_CESA
  927. "Could not register hmac-sha1 driver\n");
  928. }
  929. return 0;
  930. err_unreg_ecb:
  931. crypto_unregister_alg(&mv_aes_alg_ecb);
  932. err_irq:
  933. free_irq(irq, cp);
  934. err_thread:
  935. kthread_stop(cp->queue_th);
  936. err_unmap_sram:
  937. iounmap(cp->sram);
  938. err_unmap_reg:
  939. iounmap(cp->reg);
  940. err:
  941. kfree(cp);
  942. cpg = NULL;
  943. platform_set_drvdata(pdev, NULL);
  944. return ret;
  945. }
  946. static int mv_remove(struct platform_device *pdev)
  947. {
  948. struct crypto_priv *cp = platform_get_drvdata(pdev);
  949. crypto_unregister_alg(&mv_aes_alg_ecb);
  950. crypto_unregister_alg(&mv_aes_alg_cbc);
  951. if (cp->has_sha1)
  952. crypto_unregister_ahash(&mv_sha1_alg);
  953. if (cp->has_hmac_sha1)
  954. crypto_unregister_ahash(&mv_hmac_sha1_alg);
  955. kthread_stop(cp->queue_th);
  956. free_irq(cp->irq, cp);
  957. memset(cp->sram, 0, cp->sram_size);
  958. iounmap(cp->sram);
  959. iounmap(cp->reg);
  960. kfree(cp);
  961. cpg = NULL;
  962. return 0;
  963. }
  964. static struct platform_driver marvell_crypto = {
  965. .probe = mv_probe,
  966. .remove = mv_remove,
  967. .driver = {
  968. .owner = THIS_MODULE,
  969. .name = "mv_crypto",
  970. },
  971. };
  972. MODULE_ALIAS("platform:mv_crypto");
  973. static int __init mv_crypto_init(void)
  974. {
  975. return platform_driver_register(&marvell_crypto);
  976. }
  977. module_init(mv_crypto_init);
  978. static void __exit mv_crypto_exit(void)
  979. {
  980. platform_driver_unregister(&marvell_crypto);
  981. }
  982. module_exit(mv_crypto_exit);
  983. MODULE_AUTHOR("Sebastian Andrzej Siewior <sebastian@breakpoint.cc>");
  984. MODULE_DESCRIPTION("Support for Marvell's cryptographic engine");
  985. MODULE_LICENSE("GPL");