mv_cesa.c 27 KB

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