mv_cesa.c 28 KB

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