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