mv_cesa.c 28 KB

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