mv_cesa.c 28 KB

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