mv_cesa.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193
  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 <linux/of.h>
  23. #include <linux/of_platform.h>
  24. #include <linux/of_irq.h>
  25. #include "mv_cesa.h"
  26. #define MV_CESA "MV-CESA:"
  27. #define MAX_HW_HASH_SIZE 0xFFFF
  28. #define MV_CESA_EXPIRE 500 /* msec */
  29. /*
  30. * STM:
  31. * /---------------------------------------\
  32. * | | request complete
  33. * \./ |
  34. * IDLE -> new request -> BUSY -> done -> DEQUEUE
  35. * /°\ |
  36. * | | more scatter entries
  37. * \________________/
  38. */
  39. enum engine_status {
  40. ENGINE_IDLE,
  41. ENGINE_BUSY,
  42. ENGINE_W_DEQUEUE,
  43. };
  44. /**
  45. * struct req_progress - used for every crypt request
  46. * @src_sg_it: sg iterator for src
  47. * @dst_sg_it: sg iterator for dst
  48. * @sg_src_left: bytes left in src to process (scatter list)
  49. * @src_start: offset to add to src start position (scatter list)
  50. * @crypt_len: length of current hw crypt/hash process
  51. * @hw_nbytes: total bytes to process in hw for this request
  52. * @copy_back: whether to copy data back (crypt) or not (hash)
  53. * @sg_dst_left: bytes left dst to process in this scatter list
  54. * @dst_start: offset to add to dst start position (scatter list)
  55. * @hw_processed_bytes: number of bytes processed by hw (request).
  56. *
  57. * sg helper are used to iterate over the scatterlist. Since the size of the
  58. * SRAM may be less than the scatter size, this struct struct is used to keep
  59. * track of progress within current scatterlist.
  60. */
  61. struct req_progress {
  62. struct sg_mapping_iter src_sg_it;
  63. struct sg_mapping_iter dst_sg_it;
  64. void (*complete) (void);
  65. void (*process) (int is_first);
  66. /* src mostly */
  67. int sg_src_left;
  68. int src_start;
  69. int crypt_len;
  70. int hw_nbytes;
  71. /* dst mostly */
  72. int copy_back;
  73. int sg_dst_left;
  74. int dst_start;
  75. int hw_processed_bytes;
  76. };
  77. struct crypto_priv {
  78. void __iomem *reg;
  79. void __iomem *sram;
  80. int irq;
  81. struct clk *clk;
  82. struct task_struct *queue_th;
  83. /* the lock protects queue and eng_st */
  84. spinlock_t lock;
  85. struct crypto_queue queue;
  86. enum engine_status eng_st;
  87. struct timer_list completion_timer;
  88. struct crypto_async_request *cur_req;
  89. struct req_progress p;
  90. int max_req_size;
  91. int sram_size;
  92. int has_sha1;
  93. int has_hmac_sha1;
  94. };
  95. static struct crypto_priv *cpg;
  96. struct mv_ctx {
  97. u8 aes_enc_key[AES_KEY_LEN];
  98. u32 aes_dec_key[8];
  99. int key_len;
  100. u32 need_calc_aes_dkey;
  101. };
  102. enum crypto_op {
  103. COP_AES_ECB,
  104. COP_AES_CBC,
  105. };
  106. struct mv_req_ctx {
  107. enum crypto_op op;
  108. int decrypt;
  109. };
  110. enum hash_op {
  111. COP_SHA1,
  112. COP_HMAC_SHA1
  113. };
  114. struct mv_tfm_hash_ctx {
  115. struct crypto_shash *fallback;
  116. struct crypto_shash *base_hash;
  117. u32 ivs[2 * SHA1_DIGEST_SIZE / 4];
  118. int count_add;
  119. enum hash_op op;
  120. };
  121. struct mv_req_hash_ctx {
  122. u64 count;
  123. u32 state[SHA1_DIGEST_SIZE / 4];
  124. u8 buffer[SHA1_BLOCK_SIZE];
  125. int first_hash; /* marks that we don't have previous state */
  126. int last_chunk; /* marks that this is the 'final' request */
  127. int extra_bytes; /* unprocessed bytes in buffer */
  128. enum hash_op op;
  129. int count_add;
  130. };
  131. static void mv_completion_timer_callback(unsigned long unused)
  132. {
  133. int active = readl(cpg->reg + SEC_ACCEL_CMD) & SEC_CMD_EN_SEC_ACCL0;
  134. printk(KERN_ERR MV_CESA
  135. "completion timer expired (CESA %sactive), cleaning up.\n",
  136. active ? "" : "in");
  137. del_timer(&cpg->completion_timer);
  138. writel(SEC_CMD_DISABLE_SEC, cpg->reg + SEC_ACCEL_CMD);
  139. while(readl(cpg->reg + SEC_ACCEL_CMD) & SEC_CMD_DISABLE_SEC)
  140. printk(KERN_INFO MV_CESA "%s: waiting for engine finishing\n", __func__);
  141. cpg->eng_st = ENGINE_W_DEQUEUE;
  142. wake_up_process(cpg->queue_th);
  143. }
  144. static void mv_setup_timer(void)
  145. {
  146. setup_timer(&cpg->completion_timer, &mv_completion_timer_callback, 0);
  147. mod_timer(&cpg->completion_timer,
  148. jiffies + msecs_to_jiffies(MV_CESA_EXPIRE));
  149. }
  150. static void compute_aes_dec_key(struct mv_ctx *ctx)
  151. {
  152. struct crypto_aes_ctx gen_aes_key;
  153. int key_pos;
  154. if (!ctx->need_calc_aes_dkey)
  155. return;
  156. crypto_aes_expand_key(&gen_aes_key, ctx->aes_enc_key, ctx->key_len);
  157. key_pos = ctx->key_len + 24;
  158. memcpy(ctx->aes_dec_key, &gen_aes_key.key_enc[key_pos], 4 * 4);
  159. switch (ctx->key_len) {
  160. case AES_KEYSIZE_256:
  161. key_pos -= 2;
  162. /* fall */
  163. case AES_KEYSIZE_192:
  164. key_pos -= 2;
  165. memcpy(&ctx->aes_dec_key[4], &gen_aes_key.key_enc[key_pos],
  166. 4 * 4);
  167. break;
  168. }
  169. ctx->need_calc_aes_dkey = 0;
  170. }
  171. static int mv_setkey_aes(struct crypto_ablkcipher *cipher, const u8 *key,
  172. unsigned int len)
  173. {
  174. struct crypto_tfm *tfm = crypto_ablkcipher_tfm(cipher);
  175. struct mv_ctx *ctx = crypto_tfm_ctx(tfm);
  176. switch (len) {
  177. case AES_KEYSIZE_128:
  178. case AES_KEYSIZE_192:
  179. case AES_KEYSIZE_256:
  180. break;
  181. default:
  182. crypto_ablkcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
  183. return -EINVAL;
  184. }
  185. ctx->key_len = len;
  186. ctx->need_calc_aes_dkey = 1;
  187. memcpy(ctx->aes_enc_key, key, AES_KEY_LEN);
  188. return 0;
  189. }
  190. static void copy_src_to_buf(struct req_progress *p, char *dbuf, int len)
  191. {
  192. int ret;
  193. void *sbuf;
  194. int copy_len;
  195. while (len) {
  196. if (!p->sg_src_left) {
  197. ret = sg_miter_next(&p->src_sg_it);
  198. BUG_ON(!ret);
  199. p->sg_src_left = p->src_sg_it.length;
  200. p->src_start = 0;
  201. }
  202. sbuf = p->src_sg_it.addr + p->src_start;
  203. copy_len = min(p->sg_src_left, len);
  204. memcpy(dbuf, sbuf, copy_len);
  205. p->src_start += copy_len;
  206. p->sg_src_left -= copy_len;
  207. len -= copy_len;
  208. dbuf += copy_len;
  209. }
  210. }
  211. static void setup_data_in(void)
  212. {
  213. struct req_progress *p = &cpg->p;
  214. int data_in_sram =
  215. min(p->hw_nbytes - p->hw_processed_bytes, cpg->max_req_size);
  216. copy_src_to_buf(p, cpg->sram + SRAM_DATA_IN_START + p->crypt_len,
  217. data_in_sram - p->crypt_len);
  218. p->crypt_len = data_in_sram;
  219. }
  220. static void mv_process_current_q(int first_block)
  221. {
  222. struct ablkcipher_request *req = ablkcipher_request_cast(cpg->cur_req);
  223. struct mv_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  224. struct mv_req_ctx *req_ctx = ablkcipher_request_ctx(req);
  225. struct sec_accel_config op;
  226. switch (req_ctx->op) {
  227. case COP_AES_ECB:
  228. op.config = CFG_OP_CRYPT_ONLY | CFG_ENCM_AES | CFG_ENC_MODE_ECB;
  229. break;
  230. case COP_AES_CBC:
  231. default:
  232. op.config = CFG_OP_CRYPT_ONLY | CFG_ENCM_AES | CFG_ENC_MODE_CBC;
  233. op.enc_iv = ENC_IV_POINT(SRAM_DATA_IV) |
  234. ENC_IV_BUF_POINT(SRAM_DATA_IV_BUF);
  235. if (first_block)
  236. memcpy(cpg->sram + SRAM_DATA_IV, req->info, 16);
  237. break;
  238. }
  239. if (req_ctx->decrypt) {
  240. op.config |= CFG_DIR_DEC;
  241. memcpy(cpg->sram + SRAM_DATA_KEY_P, ctx->aes_dec_key,
  242. AES_KEY_LEN);
  243. } else {
  244. op.config |= CFG_DIR_ENC;
  245. memcpy(cpg->sram + SRAM_DATA_KEY_P, ctx->aes_enc_key,
  246. AES_KEY_LEN);
  247. }
  248. switch (ctx->key_len) {
  249. case AES_KEYSIZE_128:
  250. op.config |= CFG_AES_LEN_128;
  251. break;
  252. case AES_KEYSIZE_192:
  253. op.config |= CFG_AES_LEN_192;
  254. break;
  255. case AES_KEYSIZE_256:
  256. op.config |= CFG_AES_LEN_256;
  257. break;
  258. }
  259. op.enc_p = ENC_P_SRC(SRAM_DATA_IN_START) |
  260. ENC_P_DST(SRAM_DATA_OUT_START);
  261. op.enc_key_p = SRAM_DATA_KEY_P;
  262. setup_data_in();
  263. op.enc_len = cpg->p.crypt_len;
  264. memcpy(cpg->sram + SRAM_CONFIG, &op,
  265. sizeof(struct sec_accel_config));
  266. /* GO */
  267. mv_setup_timer();
  268. writel(SEC_CMD_EN_SEC_ACCL0, cpg->reg + SEC_ACCEL_CMD);
  269. }
  270. static void mv_crypto_algo_completion(void)
  271. {
  272. struct ablkcipher_request *req = ablkcipher_request_cast(cpg->cur_req);
  273. struct mv_req_ctx *req_ctx = ablkcipher_request_ctx(req);
  274. sg_miter_stop(&cpg->p.src_sg_it);
  275. sg_miter_stop(&cpg->p.dst_sg_it);
  276. if (req_ctx->op != COP_AES_CBC)
  277. return ;
  278. memcpy(req->info, cpg->sram + SRAM_DATA_IV_BUF, 16);
  279. }
  280. static void mv_process_hash_current(int first_block)
  281. {
  282. struct ahash_request *req = ahash_request_cast(cpg->cur_req);
  283. const struct mv_tfm_hash_ctx *tfm_ctx = crypto_tfm_ctx(req->base.tfm);
  284. struct mv_req_hash_ctx *req_ctx = ahash_request_ctx(req);
  285. struct req_progress *p = &cpg->p;
  286. struct sec_accel_config op = { 0 };
  287. int is_last;
  288. switch (req_ctx->op) {
  289. case COP_SHA1:
  290. default:
  291. op.config = CFG_OP_MAC_ONLY | CFG_MACM_SHA1;
  292. break;
  293. case COP_HMAC_SHA1:
  294. op.config = CFG_OP_MAC_ONLY | CFG_MACM_HMAC_SHA1;
  295. memcpy(cpg->sram + SRAM_HMAC_IV_IN,
  296. tfm_ctx->ivs, sizeof(tfm_ctx->ivs));
  297. break;
  298. }
  299. op.mac_src_p =
  300. MAC_SRC_DATA_P(SRAM_DATA_IN_START) | MAC_SRC_TOTAL_LEN((u32)
  301. req_ctx->
  302. count);
  303. setup_data_in();
  304. op.mac_digest =
  305. MAC_DIGEST_P(SRAM_DIGEST_BUF) | MAC_FRAG_LEN(p->crypt_len);
  306. op.mac_iv =
  307. MAC_INNER_IV_P(SRAM_HMAC_IV_IN) |
  308. MAC_OUTER_IV_P(SRAM_HMAC_IV_OUT);
  309. is_last = req_ctx->last_chunk
  310. && (p->hw_processed_bytes + p->crypt_len >= p->hw_nbytes)
  311. && (req_ctx->count <= MAX_HW_HASH_SIZE);
  312. if (req_ctx->first_hash) {
  313. if (is_last)
  314. op.config |= CFG_NOT_FRAG;
  315. else
  316. op.config |= CFG_FIRST_FRAG;
  317. req_ctx->first_hash = 0;
  318. } else {
  319. if (is_last)
  320. op.config |= CFG_LAST_FRAG;
  321. else
  322. op.config |= CFG_MID_FRAG;
  323. if (first_block) {
  324. writel(req_ctx->state[0], cpg->reg + DIGEST_INITIAL_VAL_A);
  325. writel(req_ctx->state[1], cpg->reg + DIGEST_INITIAL_VAL_B);
  326. writel(req_ctx->state[2], cpg->reg + DIGEST_INITIAL_VAL_C);
  327. writel(req_ctx->state[3], cpg->reg + DIGEST_INITIAL_VAL_D);
  328. writel(req_ctx->state[4], cpg->reg + DIGEST_INITIAL_VAL_E);
  329. }
  330. }
  331. memcpy(cpg->sram + SRAM_CONFIG, &op, sizeof(struct sec_accel_config));
  332. /* GO */
  333. mv_setup_timer();
  334. writel(SEC_CMD_EN_SEC_ACCL0, cpg->reg + SEC_ACCEL_CMD);
  335. }
  336. static inline int mv_hash_import_sha1_ctx(const struct mv_req_hash_ctx *ctx,
  337. struct shash_desc *desc)
  338. {
  339. int i;
  340. struct sha1_state shash_state;
  341. shash_state.count = ctx->count + ctx->count_add;
  342. for (i = 0; i < 5; i++)
  343. shash_state.state[i] = ctx->state[i];
  344. memcpy(shash_state.buffer, ctx->buffer, sizeof(shash_state.buffer));
  345. return crypto_shash_import(desc, &shash_state);
  346. }
  347. static int mv_hash_final_fallback(struct ahash_request *req)
  348. {
  349. const struct mv_tfm_hash_ctx *tfm_ctx = crypto_tfm_ctx(req->base.tfm);
  350. struct mv_req_hash_ctx *req_ctx = ahash_request_ctx(req);
  351. SHASH_DESC_ON_STACK(shash, tfm_ctx->fallback);
  352. int rc;
  353. shash->tfm = tfm_ctx->fallback;
  354. shash->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
  355. if (unlikely(req_ctx->first_hash)) {
  356. crypto_shash_init(shash);
  357. crypto_shash_update(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, shash);
  363. if (rc)
  364. goto out;
  365. }
  366. rc = crypto_shash_final(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 (crypto_tfm_alg_type(async_req->tfm) !=
  537. CRYPTO_ALG_TYPE_AHASH) {
  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. SHASH_DESC_ON_STACK(shash, ctx->base_hash);
  677. unsigned int i;
  678. char ipad[ss];
  679. char opad[ss];
  680. shash->tfm = ctx->base_hash;
  681. shash->flags = crypto_shash_get_flags(ctx->base_hash) &
  682. CRYPTO_TFM_REQ_MAY_SLEEP;
  683. if (keylen > bs) {
  684. int err;
  685. err =
  686. crypto_shash_digest(shash, key, keylen, ipad);
  687. if (err)
  688. return err;
  689. keylen = ds;
  690. } else
  691. memcpy(ipad, key, keylen);
  692. memset(ipad + keylen, 0, bs - keylen);
  693. memcpy(opad, ipad, bs);
  694. for (i = 0; i < bs; i++) {
  695. ipad[i] ^= 0x36;
  696. opad[i] ^= 0x5c;
  697. }
  698. rc = crypto_shash_init(shash) ? :
  699. crypto_shash_update(shash, ipad, bs) ? :
  700. crypto_shash_export(shash, ipad) ? :
  701. crypto_shash_init(shash) ? :
  702. crypto_shash_update(shash, opad, bs) ? :
  703. crypto_shash_export(shash, opad);
  704. if (rc == 0)
  705. mv_hash_init_ivs(ctx, ipad, opad);
  706. return rc;
  707. }
  708. }
  709. static int mv_cra_hash_init(struct crypto_tfm *tfm, const char *base_hash_name,
  710. enum hash_op op, int count_add)
  711. {
  712. const char *fallback_driver_name = crypto_tfm_alg_name(tfm);
  713. struct mv_tfm_hash_ctx *ctx = crypto_tfm_ctx(tfm);
  714. struct crypto_shash *fallback_tfm = NULL;
  715. struct crypto_shash *base_hash = NULL;
  716. int err = -ENOMEM;
  717. ctx->op = op;
  718. ctx->count_add = count_add;
  719. /* Allocate a fallback and abort if it failed. */
  720. fallback_tfm = crypto_alloc_shash(fallback_driver_name, 0,
  721. CRYPTO_ALG_NEED_FALLBACK);
  722. if (IS_ERR(fallback_tfm)) {
  723. printk(KERN_WARNING MV_CESA
  724. "Fallback driver '%s' could not be loaded!\n",
  725. fallback_driver_name);
  726. err = PTR_ERR(fallback_tfm);
  727. goto out;
  728. }
  729. ctx->fallback = fallback_tfm;
  730. if (base_hash_name) {
  731. /* Allocate a hash to compute the ipad/opad of hmac. */
  732. base_hash = crypto_alloc_shash(base_hash_name, 0,
  733. CRYPTO_ALG_NEED_FALLBACK);
  734. if (IS_ERR(base_hash)) {
  735. printk(KERN_WARNING MV_CESA
  736. "Base driver '%s' could not be loaded!\n",
  737. base_hash_name);
  738. err = PTR_ERR(base_hash);
  739. goto err_bad_base;
  740. }
  741. }
  742. ctx->base_hash = base_hash;
  743. crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
  744. sizeof(struct mv_req_hash_ctx) +
  745. crypto_shash_descsize(ctx->fallback));
  746. return 0;
  747. err_bad_base:
  748. crypto_free_shash(fallback_tfm);
  749. out:
  750. return err;
  751. }
  752. static void mv_cra_hash_exit(struct crypto_tfm *tfm)
  753. {
  754. struct mv_tfm_hash_ctx *ctx = crypto_tfm_ctx(tfm);
  755. crypto_free_shash(ctx->fallback);
  756. if (ctx->base_hash)
  757. crypto_free_shash(ctx->base_hash);
  758. }
  759. static int mv_cra_hash_sha1_init(struct crypto_tfm *tfm)
  760. {
  761. return mv_cra_hash_init(tfm, NULL, COP_SHA1, 0);
  762. }
  763. static int mv_cra_hash_hmac_sha1_init(struct crypto_tfm *tfm)
  764. {
  765. return mv_cra_hash_init(tfm, "sha1", COP_HMAC_SHA1, SHA1_BLOCK_SIZE);
  766. }
  767. static irqreturn_t crypto_int(int irq, void *priv)
  768. {
  769. u32 val;
  770. val = readl(cpg->reg + SEC_ACCEL_INT_STATUS);
  771. if (!(val & SEC_INT_ACCEL0_DONE))
  772. return IRQ_NONE;
  773. if (!del_timer(&cpg->completion_timer)) {
  774. printk(KERN_WARNING MV_CESA
  775. "got an interrupt but no pending timer?\n");
  776. }
  777. val &= ~SEC_INT_ACCEL0_DONE;
  778. writel(val, cpg->reg + FPGA_INT_STATUS);
  779. writel(val, cpg->reg + SEC_ACCEL_INT_STATUS);
  780. BUG_ON(cpg->eng_st != ENGINE_BUSY);
  781. cpg->eng_st = ENGINE_W_DEQUEUE;
  782. wake_up_process(cpg->queue_th);
  783. return IRQ_HANDLED;
  784. }
  785. static struct crypto_alg mv_aes_alg_ecb = {
  786. .cra_name = "ecb(aes)",
  787. .cra_driver_name = "mv-ecb-aes",
  788. .cra_priority = 300,
  789. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
  790. CRYPTO_ALG_KERN_DRIVER_ONLY | CRYPTO_ALG_ASYNC,
  791. .cra_blocksize = 16,
  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. .min_keysize = AES_MIN_KEY_SIZE,
  800. .max_keysize = AES_MAX_KEY_SIZE,
  801. .setkey = mv_setkey_aes,
  802. .encrypt = mv_enc_aes_ecb,
  803. .decrypt = mv_dec_aes_ecb,
  804. },
  805. },
  806. };
  807. static struct crypto_alg mv_aes_alg_cbc = {
  808. .cra_name = "cbc(aes)",
  809. .cra_driver_name = "mv-cbc-aes",
  810. .cra_priority = 300,
  811. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
  812. CRYPTO_ALG_KERN_DRIVER_ONLY | CRYPTO_ALG_ASYNC,
  813. .cra_blocksize = AES_BLOCK_SIZE,
  814. .cra_ctxsize = sizeof(struct mv_ctx),
  815. .cra_alignmask = 0,
  816. .cra_type = &crypto_ablkcipher_type,
  817. .cra_module = THIS_MODULE,
  818. .cra_init = mv_cra_init,
  819. .cra_u = {
  820. .ablkcipher = {
  821. .ivsize = AES_BLOCK_SIZE,
  822. .min_keysize = AES_MIN_KEY_SIZE,
  823. .max_keysize = AES_MAX_KEY_SIZE,
  824. .setkey = mv_setkey_aes,
  825. .encrypt = mv_enc_aes_cbc,
  826. .decrypt = mv_dec_aes_cbc,
  827. },
  828. },
  829. };
  830. static struct ahash_alg mv_sha1_alg = {
  831. .init = mv_hash_init,
  832. .update = mv_hash_update,
  833. .final = mv_hash_final,
  834. .finup = mv_hash_finup,
  835. .digest = mv_hash_digest,
  836. .halg = {
  837. .digestsize = SHA1_DIGEST_SIZE,
  838. .base = {
  839. .cra_name = "sha1",
  840. .cra_driver_name = "mv-sha1",
  841. .cra_priority = 300,
  842. .cra_flags =
  843. CRYPTO_ALG_ASYNC | CRYPTO_ALG_KERN_DRIVER_ONLY |
  844. CRYPTO_ALG_NEED_FALLBACK,
  845. .cra_blocksize = SHA1_BLOCK_SIZE,
  846. .cra_ctxsize = sizeof(struct mv_tfm_hash_ctx),
  847. .cra_init = mv_cra_hash_sha1_init,
  848. .cra_exit = mv_cra_hash_exit,
  849. .cra_module = THIS_MODULE,
  850. }
  851. }
  852. };
  853. static struct ahash_alg mv_hmac_sha1_alg = {
  854. .init = mv_hash_init,
  855. .update = mv_hash_update,
  856. .final = mv_hash_final,
  857. .finup = mv_hash_finup,
  858. .digest = mv_hash_digest,
  859. .setkey = mv_hash_setkey,
  860. .halg = {
  861. .digestsize = SHA1_DIGEST_SIZE,
  862. .base = {
  863. .cra_name = "hmac(sha1)",
  864. .cra_driver_name = "mv-hmac-sha1",
  865. .cra_priority = 300,
  866. .cra_flags =
  867. CRYPTO_ALG_ASYNC | CRYPTO_ALG_KERN_DRIVER_ONLY |
  868. CRYPTO_ALG_NEED_FALLBACK,
  869. .cra_blocksize = SHA1_BLOCK_SIZE,
  870. .cra_ctxsize = sizeof(struct mv_tfm_hash_ctx),
  871. .cra_init = mv_cra_hash_hmac_sha1_init,
  872. .cra_exit = mv_cra_hash_exit,
  873. .cra_module = THIS_MODULE,
  874. }
  875. }
  876. };
  877. static int mv_probe(struct platform_device *pdev)
  878. {
  879. struct crypto_priv *cp;
  880. struct resource *res;
  881. int irq;
  882. int ret;
  883. if (cpg) {
  884. printk(KERN_ERR MV_CESA "Second crypto dev?\n");
  885. return -EEXIST;
  886. }
  887. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "regs");
  888. if (!res)
  889. return -ENXIO;
  890. cp = kzalloc(sizeof(*cp), GFP_KERNEL);
  891. if (!cp)
  892. return -ENOMEM;
  893. spin_lock_init(&cp->lock);
  894. crypto_init_queue(&cp->queue, 50);
  895. cp->reg = ioremap(res->start, resource_size(res));
  896. if (!cp->reg) {
  897. ret = -ENOMEM;
  898. goto err;
  899. }
  900. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "sram");
  901. if (!res) {
  902. ret = -ENXIO;
  903. goto err_unmap_reg;
  904. }
  905. cp->sram_size = resource_size(res);
  906. cp->max_req_size = cp->sram_size - SRAM_CFG_SPACE;
  907. cp->sram = ioremap(res->start, cp->sram_size);
  908. if (!cp->sram) {
  909. ret = -ENOMEM;
  910. goto err_unmap_reg;
  911. }
  912. if (pdev->dev.of_node)
  913. irq = irq_of_parse_and_map(pdev->dev.of_node, 0);
  914. else
  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, 0, 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. return ret;
  984. }
  985. static int mv_remove(struct platform_device *pdev)
  986. {
  987. struct crypto_priv *cp = platform_get_drvdata(pdev);
  988. crypto_unregister_alg(&mv_aes_alg_ecb);
  989. crypto_unregister_alg(&mv_aes_alg_cbc);
  990. if (cp->has_sha1)
  991. crypto_unregister_ahash(&mv_sha1_alg);
  992. if (cp->has_hmac_sha1)
  993. crypto_unregister_ahash(&mv_hmac_sha1_alg);
  994. kthread_stop(cp->queue_th);
  995. free_irq(cp->irq, cp);
  996. memset(cp->sram, 0, cp->sram_size);
  997. iounmap(cp->sram);
  998. iounmap(cp->reg);
  999. if (!IS_ERR(cp->clk)) {
  1000. clk_disable_unprepare(cp->clk);
  1001. clk_put(cp->clk);
  1002. }
  1003. kfree(cp);
  1004. cpg = NULL;
  1005. return 0;
  1006. }
  1007. static const struct of_device_id mv_cesa_of_match_table[] = {
  1008. { .compatible = "marvell,orion-crypto", },
  1009. {}
  1010. };
  1011. MODULE_DEVICE_TABLE(of, mv_cesa_of_match_table);
  1012. static struct platform_driver marvell_crypto = {
  1013. .probe = mv_probe,
  1014. .remove = mv_remove,
  1015. .driver = {
  1016. .name = "mv_crypto",
  1017. .of_match_table = mv_cesa_of_match_table,
  1018. },
  1019. };
  1020. MODULE_ALIAS("platform:mv_crypto");
  1021. module_platform_driver(marvell_crypto);
  1022. MODULE_AUTHOR("Sebastian Andrzej Siewior <sebastian@breakpoint.cc>");
  1023. MODULE_DESCRIPTION("Support for Marvell's cryptographic engine");
  1024. MODULE_LICENSE("GPL");