mv_cesa.c 29 KB

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