mv_cesa.c 27 KB

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