caamhash.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965
  1. /*
  2. * caam - Freescale FSL CAAM support for ahash functions of crypto API
  3. *
  4. * Copyright 2011 Freescale Semiconductor, Inc.
  5. *
  6. * Based on caamalg.c crypto API driver.
  7. *
  8. * relationship of digest job descriptor or first job descriptor after init to
  9. * shared descriptors:
  10. *
  11. * --------------- ---------------
  12. * | JobDesc #1 |-------------------->| ShareDesc |
  13. * | *(packet 1) | | (hashKey) |
  14. * --------------- | (operation) |
  15. * ---------------
  16. *
  17. * relationship of subsequent job descriptors to shared descriptors:
  18. *
  19. * --------------- ---------------
  20. * | JobDesc #2 |-------------------->| ShareDesc |
  21. * | *(packet 2) | |------------->| (hashKey) |
  22. * --------------- | |-------->| (operation) |
  23. * . | | | (load ctx2) |
  24. * . | | ---------------
  25. * --------------- | |
  26. * | JobDesc #3 |------| |
  27. * | *(packet 3) | |
  28. * --------------- |
  29. * . |
  30. * . |
  31. * --------------- |
  32. * | JobDesc #4 |------------
  33. * | *(packet 4) |
  34. * ---------------
  35. *
  36. * The SharedDesc never changes for a connection unless rekeyed, but
  37. * each packet will likely be in a different place. So all we need
  38. * to know to process the packet is where the input is, where the
  39. * output goes, and what context we want to process with. Context is
  40. * in the SharedDesc, packet references in the JobDesc.
  41. *
  42. * So, a job desc looks like:
  43. *
  44. * ---------------------
  45. * | Header |
  46. * | ShareDesc Pointer |
  47. * | SEQ_OUT_PTR |
  48. * | (output buffer) |
  49. * | (output length) |
  50. * | SEQ_IN_PTR |
  51. * | (input buffer) |
  52. * | (input length) |
  53. * ---------------------
  54. */
  55. #include "compat.h"
  56. #include "regs.h"
  57. #include "intern.h"
  58. #include "desc_constr.h"
  59. #include "jr.h"
  60. #include "error.h"
  61. #include "sg_sw_sec4.h"
  62. #include "key_gen.h"
  63. #define CAAM_CRA_PRIORITY 3000
  64. /* max hash key is max split key size */
  65. #define CAAM_MAX_HASH_KEY_SIZE (SHA512_DIGEST_SIZE * 2)
  66. #define CAAM_MAX_HASH_BLOCK_SIZE SHA512_BLOCK_SIZE
  67. #define CAAM_MAX_HASH_DIGEST_SIZE SHA512_DIGEST_SIZE
  68. /* length of descriptors text */
  69. #define DESC_AHASH_BASE (4 * CAAM_CMD_SZ)
  70. #define DESC_AHASH_UPDATE_LEN (6 * CAAM_CMD_SZ)
  71. #define DESC_AHASH_UPDATE_FIRST_LEN (DESC_AHASH_BASE + 4 * CAAM_CMD_SZ)
  72. #define DESC_AHASH_FINAL_LEN (DESC_AHASH_BASE + 5 * CAAM_CMD_SZ)
  73. #define DESC_AHASH_FINUP_LEN (DESC_AHASH_BASE + 5 * CAAM_CMD_SZ)
  74. #define DESC_AHASH_DIGEST_LEN (DESC_AHASH_BASE + 4 * CAAM_CMD_SZ)
  75. #define DESC_HASH_MAX_USED_BYTES (DESC_AHASH_FINAL_LEN + \
  76. CAAM_MAX_HASH_KEY_SIZE)
  77. #define DESC_HASH_MAX_USED_LEN (DESC_HASH_MAX_USED_BYTES / CAAM_CMD_SZ)
  78. /* caam context sizes for hashes: running digest + 8 */
  79. #define HASH_MSG_LEN 8
  80. #define MAX_CTX_LEN (HASH_MSG_LEN + SHA512_DIGEST_SIZE)
  81. #ifdef DEBUG
  82. /* for print_hex_dumps with line references */
  83. #define debug(format, arg...) printk(format, arg)
  84. #else
  85. #define debug(format, arg...)
  86. #endif
  87. static struct list_head hash_list;
  88. /* ahash per-session context */
  89. struct caam_hash_ctx {
  90. struct device *jrdev;
  91. u32 sh_desc_update[DESC_HASH_MAX_USED_LEN];
  92. u32 sh_desc_update_first[DESC_HASH_MAX_USED_LEN];
  93. u32 sh_desc_fin[DESC_HASH_MAX_USED_LEN];
  94. u32 sh_desc_digest[DESC_HASH_MAX_USED_LEN];
  95. u32 sh_desc_finup[DESC_HASH_MAX_USED_LEN];
  96. dma_addr_t sh_desc_update_dma;
  97. dma_addr_t sh_desc_update_first_dma;
  98. dma_addr_t sh_desc_fin_dma;
  99. dma_addr_t sh_desc_digest_dma;
  100. dma_addr_t sh_desc_finup_dma;
  101. u32 alg_type;
  102. u32 alg_op;
  103. u8 key[CAAM_MAX_HASH_KEY_SIZE];
  104. dma_addr_t key_dma;
  105. int ctx_len;
  106. unsigned int split_key_len;
  107. unsigned int split_key_pad_len;
  108. };
  109. /* ahash state */
  110. struct caam_hash_state {
  111. dma_addr_t buf_dma;
  112. dma_addr_t ctx_dma;
  113. u8 buf_0[CAAM_MAX_HASH_BLOCK_SIZE] ____cacheline_aligned;
  114. int buflen_0;
  115. u8 buf_1[CAAM_MAX_HASH_BLOCK_SIZE] ____cacheline_aligned;
  116. int buflen_1;
  117. u8 caam_ctx[MAX_CTX_LEN];
  118. int (*update)(struct ahash_request *req);
  119. int (*final)(struct ahash_request *req);
  120. int (*finup)(struct ahash_request *req);
  121. int current_buf;
  122. };
  123. /* Common job descriptor seq in/out ptr routines */
  124. /* Map state->caam_ctx, and append seq_out_ptr command that points to it */
  125. static inline int map_seq_out_ptr_ctx(u32 *desc, struct device *jrdev,
  126. struct caam_hash_state *state,
  127. int ctx_len)
  128. {
  129. state->ctx_dma = dma_map_single(jrdev, state->caam_ctx,
  130. ctx_len, DMA_FROM_DEVICE);
  131. if (dma_mapping_error(jrdev, state->ctx_dma)) {
  132. dev_err(jrdev, "unable to map ctx\n");
  133. return -ENOMEM;
  134. }
  135. append_seq_out_ptr(desc, state->ctx_dma, ctx_len, 0);
  136. return 0;
  137. }
  138. /* Map req->result, and append seq_out_ptr command that points to it */
  139. static inline dma_addr_t map_seq_out_ptr_result(u32 *desc, struct device *jrdev,
  140. u8 *result, int digestsize)
  141. {
  142. dma_addr_t dst_dma;
  143. dst_dma = dma_map_single(jrdev, result, digestsize, DMA_FROM_DEVICE);
  144. append_seq_out_ptr(desc, dst_dma, digestsize, 0);
  145. return dst_dma;
  146. }
  147. /* Map current buffer in state and put it in link table */
  148. static inline dma_addr_t buf_map_to_sec4_sg(struct device *jrdev,
  149. struct sec4_sg_entry *sec4_sg,
  150. u8 *buf, int buflen)
  151. {
  152. dma_addr_t buf_dma;
  153. buf_dma = dma_map_single(jrdev, buf, buflen, DMA_TO_DEVICE);
  154. dma_to_sec4_sg_one(sec4_sg, buf_dma, buflen, 0);
  155. return buf_dma;
  156. }
  157. /* Map req->src and put it in link table */
  158. static inline void src_map_to_sec4_sg(struct device *jrdev,
  159. struct scatterlist *src, int src_nents,
  160. struct sec4_sg_entry *sec4_sg,
  161. bool chained)
  162. {
  163. dma_map_sg_chained(jrdev, src, src_nents, DMA_TO_DEVICE, chained);
  164. sg_to_sec4_sg_last(src, src_nents, sec4_sg, 0);
  165. }
  166. /*
  167. * Only put buffer in link table if it contains data, which is possible,
  168. * since a buffer has previously been used, and needs to be unmapped,
  169. */
  170. static inline dma_addr_t
  171. try_buf_map_to_sec4_sg(struct device *jrdev, struct sec4_sg_entry *sec4_sg,
  172. u8 *buf, dma_addr_t buf_dma, int buflen,
  173. int last_buflen)
  174. {
  175. if (buf_dma && !dma_mapping_error(jrdev, buf_dma))
  176. dma_unmap_single(jrdev, buf_dma, last_buflen, DMA_TO_DEVICE);
  177. if (buflen)
  178. buf_dma = buf_map_to_sec4_sg(jrdev, sec4_sg, buf, buflen);
  179. else
  180. buf_dma = 0;
  181. return buf_dma;
  182. }
  183. /* Map state->caam_ctx, and add it to link table */
  184. static inline int ctx_map_to_sec4_sg(u32 *desc, struct device *jrdev,
  185. struct caam_hash_state *state, int ctx_len,
  186. struct sec4_sg_entry *sec4_sg, u32 flag)
  187. {
  188. state->ctx_dma = dma_map_single(jrdev, state->caam_ctx, ctx_len, flag);
  189. if (dma_mapping_error(jrdev, state->ctx_dma)) {
  190. dev_err(jrdev, "unable to map ctx\n");
  191. return -ENOMEM;
  192. }
  193. dma_to_sec4_sg_one(sec4_sg, state->ctx_dma, ctx_len, 0);
  194. return 0;
  195. }
  196. /* Common shared descriptor commands */
  197. static inline void append_key_ahash(u32 *desc, struct caam_hash_ctx *ctx)
  198. {
  199. append_key_as_imm(desc, ctx->key, ctx->split_key_pad_len,
  200. ctx->split_key_len, CLASS_2 |
  201. KEY_DEST_MDHA_SPLIT | KEY_ENC);
  202. }
  203. /* Append key if it has been set */
  204. static inline void init_sh_desc_key_ahash(u32 *desc, struct caam_hash_ctx *ctx)
  205. {
  206. u32 *key_jump_cmd;
  207. init_sh_desc(desc, HDR_SHARE_SERIAL);
  208. if (ctx->split_key_len) {
  209. /* Skip if already shared */
  210. key_jump_cmd = append_jump(desc, JUMP_JSL | JUMP_TEST_ALL |
  211. JUMP_COND_SHRD);
  212. append_key_ahash(desc, ctx);
  213. set_jump_tgt_here(desc, key_jump_cmd);
  214. }
  215. /* Propagate errors from shared to job descriptor */
  216. append_cmd(desc, SET_OK_NO_PROP_ERRORS | CMD_LOAD);
  217. }
  218. /*
  219. * For ahash read data from seqin following state->caam_ctx,
  220. * and write resulting class2 context to seqout, which may be state->caam_ctx
  221. * or req->result
  222. */
  223. static inline void ahash_append_load_str(u32 *desc, int digestsize)
  224. {
  225. /* Calculate remaining bytes to read */
  226. append_math_add(desc, VARSEQINLEN, SEQINLEN, REG0, CAAM_CMD_SZ);
  227. /* Read remaining bytes */
  228. append_seq_fifo_load(desc, 0, FIFOLD_CLASS_CLASS2 | FIFOLD_TYPE_LAST2 |
  229. FIFOLD_TYPE_MSG | KEY_VLF);
  230. /* Store class2 context bytes */
  231. append_seq_store(desc, digestsize, LDST_CLASS_2_CCB |
  232. LDST_SRCDST_BYTE_CONTEXT);
  233. }
  234. /*
  235. * For ahash update, final and finup, import context, read and write to seqout
  236. */
  237. static inline void ahash_ctx_data_to_out(u32 *desc, u32 op, u32 state,
  238. int digestsize,
  239. struct caam_hash_ctx *ctx)
  240. {
  241. init_sh_desc_key_ahash(desc, ctx);
  242. /* Import context from software */
  243. append_cmd(desc, CMD_SEQ_LOAD | LDST_SRCDST_BYTE_CONTEXT |
  244. LDST_CLASS_2_CCB | ctx->ctx_len);
  245. /* Class 2 operation */
  246. append_operation(desc, op | state | OP_ALG_ENCRYPT);
  247. /*
  248. * Load from buf and/or src and write to req->result or state->context
  249. */
  250. ahash_append_load_str(desc, digestsize);
  251. }
  252. /* For ahash firsts and digest, read and write to seqout */
  253. static inline void ahash_data_to_out(u32 *desc, u32 op, u32 state,
  254. int digestsize, struct caam_hash_ctx *ctx)
  255. {
  256. init_sh_desc_key_ahash(desc, ctx);
  257. /* Class 2 operation */
  258. append_operation(desc, op | state | OP_ALG_ENCRYPT);
  259. /*
  260. * Load from buf and/or src and write to req->result or state->context
  261. */
  262. ahash_append_load_str(desc, digestsize);
  263. }
  264. static int ahash_set_sh_desc(struct crypto_ahash *ahash)
  265. {
  266. struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
  267. int digestsize = crypto_ahash_digestsize(ahash);
  268. struct device *jrdev = ctx->jrdev;
  269. u32 have_key = 0;
  270. u32 *desc;
  271. if (ctx->split_key_len)
  272. have_key = OP_ALG_AAI_HMAC_PRECOMP;
  273. /* ahash_update shared descriptor */
  274. desc = ctx->sh_desc_update;
  275. init_sh_desc(desc, HDR_SHARE_SERIAL);
  276. /* Import context from software */
  277. append_cmd(desc, CMD_SEQ_LOAD | LDST_SRCDST_BYTE_CONTEXT |
  278. LDST_CLASS_2_CCB | ctx->ctx_len);
  279. /* Class 2 operation */
  280. append_operation(desc, ctx->alg_type | OP_ALG_AS_UPDATE |
  281. OP_ALG_ENCRYPT);
  282. /* Load data and write to result or context */
  283. ahash_append_load_str(desc, ctx->ctx_len);
  284. ctx->sh_desc_update_dma = dma_map_single(jrdev, desc, desc_bytes(desc),
  285. DMA_TO_DEVICE);
  286. if (dma_mapping_error(jrdev, ctx->sh_desc_update_dma)) {
  287. dev_err(jrdev, "unable to map shared descriptor\n");
  288. return -ENOMEM;
  289. }
  290. #ifdef DEBUG
  291. print_hex_dump(KERN_ERR,
  292. "ahash update shdesc@"__stringify(__LINE__)": ",
  293. DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc), 1);
  294. #endif
  295. /* ahash_update_first shared descriptor */
  296. desc = ctx->sh_desc_update_first;
  297. ahash_data_to_out(desc, have_key | ctx->alg_type, OP_ALG_AS_INIT,
  298. ctx->ctx_len, ctx);
  299. ctx->sh_desc_update_first_dma = dma_map_single(jrdev, desc,
  300. desc_bytes(desc),
  301. DMA_TO_DEVICE);
  302. if (dma_mapping_error(jrdev, ctx->sh_desc_update_first_dma)) {
  303. dev_err(jrdev, "unable to map shared descriptor\n");
  304. return -ENOMEM;
  305. }
  306. #ifdef DEBUG
  307. print_hex_dump(KERN_ERR,
  308. "ahash update first shdesc@"__stringify(__LINE__)": ",
  309. DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc), 1);
  310. #endif
  311. /* ahash_final shared descriptor */
  312. desc = ctx->sh_desc_fin;
  313. ahash_ctx_data_to_out(desc, have_key | ctx->alg_type,
  314. OP_ALG_AS_FINALIZE, digestsize, ctx);
  315. ctx->sh_desc_fin_dma = dma_map_single(jrdev, desc, desc_bytes(desc),
  316. DMA_TO_DEVICE);
  317. if (dma_mapping_error(jrdev, ctx->sh_desc_fin_dma)) {
  318. dev_err(jrdev, "unable to map shared descriptor\n");
  319. return -ENOMEM;
  320. }
  321. #ifdef DEBUG
  322. print_hex_dump(KERN_ERR, "ahash final shdesc@"__stringify(__LINE__)": ",
  323. DUMP_PREFIX_ADDRESS, 16, 4, desc,
  324. desc_bytes(desc), 1);
  325. #endif
  326. /* ahash_finup shared descriptor */
  327. desc = ctx->sh_desc_finup;
  328. ahash_ctx_data_to_out(desc, have_key | ctx->alg_type,
  329. OP_ALG_AS_FINALIZE, digestsize, ctx);
  330. ctx->sh_desc_finup_dma = dma_map_single(jrdev, desc, desc_bytes(desc),
  331. DMA_TO_DEVICE);
  332. if (dma_mapping_error(jrdev, ctx->sh_desc_finup_dma)) {
  333. dev_err(jrdev, "unable to map shared descriptor\n");
  334. return -ENOMEM;
  335. }
  336. #ifdef DEBUG
  337. print_hex_dump(KERN_ERR, "ahash finup shdesc@"__stringify(__LINE__)": ",
  338. DUMP_PREFIX_ADDRESS, 16, 4, desc,
  339. desc_bytes(desc), 1);
  340. #endif
  341. /* ahash_digest shared descriptor */
  342. desc = ctx->sh_desc_digest;
  343. ahash_data_to_out(desc, have_key | ctx->alg_type, OP_ALG_AS_INITFINAL,
  344. digestsize, ctx);
  345. ctx->sh_desc_digest_dma = dma_map_single(jrdev, desc,
  346. desc_bytes(desc),
  347. DMA_TO_DEVICE);
  348. if (dma_mapping_error(jrdev, ctx->sh_desc_digest_dma)) {
  349. dev_err(jrdev, "unable to map shared descriptor\n");
  350. return -ENOMEM;
  351. }
  352. #ifdef DEBUG
  353. print_hex_dump(KERN_ERR,
  354. "ahash digest shdesc@"__stringify(__LINE__)": ",
  355. DUMP_PREFIX_ADDRESS, 16, 4, desc,
  356. desc_bytes(desc), 1);
  357. #endif
  358. return 0;
  359. }
  360. static int gen_split_hash_key(struct caam_hash_ctx *ctx, const u8 *key_in,
  361. u32 keylen)
  362. {
  363. return gen_split_key(ctx->jrdev, ctx->key, ctx->split_key_len,
  364. ctx->split_key_pad_len, key_in, keylen,
  365. ctx->alg_op);
  366. }
  367. /* Digest hash size if it is too large */
  368. static int hash_digest_key(struct caam_hash_ctx *ctx, const u8 *key_in,
  369. u32 *keylen, u8 *key_out, u32 digestsize)
  370. {
  371. struct device *jrdev = ctx->jrdev;
  372. u32 *desc;
  373. struct split_key_result result;
  374. dma_addr_t src_dma, dst_dma;
  375. int ret = 0;
  376. desc = kmalloc(CAAM_CMD_SZ * 8 + CAAM_PTR_SZ * 2, GFP_KERNEL | GFP_DMA);
  377. if (!desc) {
  378. dev_err(jrdev, "unable to allocate key input memory\n");
  379. return -ENOMEM;
  380. }
  381. init_job_desc(desc, 0);
  382. src_dma = dma_map_single(jrdev, (void *)key_in, *keylen,
  383. DMA_TO_DEVICE);
  384. if (dma_mapping_error(jrdev, src_dma)) {
  385. dev_err(jrdev, "unable to map key input memory\n");
  386. kfree(desc);
  387. return -ENOMEM;
  388. }
  389. dst_dma = dma_map_single(jrdev, (void *)key_out, digestsize,
  390. DMA_FROM_DEVICE);
  391. if (dma_mapping_error(jrdev, dst_dma)) {
  392. dev_err(jrdev, "unable to map key output memory\n");
  393. dma_unmap_single(jrdev, src_dma, *keylen, DMA_TO_DEVICE);
  394. kfree(desc);
  395. return -ENOMEM;
  396. }
  397. /* Job descriptor to perform unkeyed hash on key_in */
  398. append_operation(desc, ctx->alg_type | OP_ALG_ENCRYPT |
  399. OP_ALG_AS_INITFINAL);
  400. append_seq_in_ptr(desc, src_dma, *keylen, 0);
  401. append_seq_fifo_load(desc, *keylen, FIFOLD_CLASS_CLASS2 |
  402. FIFOLD_TYPE_LAST2 | FIFOLD_TYPE_MSG);
  403. append_seq_out_ptr(desc, dst_dma, digestsize, 0);
  404. append_seq_store(desc, digestsize, LDST_CLASS_2_CCB |
  405. LDST_SRCDST_BYTE_CONTEXT);
  406. #ifdef DEBUG
  407. print_hex_dump(KERN_ERR, "key_in@"__stringify(__LINE__)": ",
  408. DUMP_PREFIX_ADDRESS, 16, 4, key_in, *keylen, 1);
  409. print_hex_dump(KERN_ERR, "jobdesc@"__stringify(__LINE__)": ",
  410. DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc), 1);
  411. #endif
  412. result.err = 0;
  413. init_completion(&result.completion);
  414. ret = caam_jr_enqueue(jrdev, desc, split_key_done, &result);
  415. if (!ret) {
  416. /* in progress */
  417. wait_for_completion_interruptible(&result.completion);
  418. ret = result.err;
  419. #ifdef DEBUG
  420. print_hex_dump(KERN_ERR,
  421. "digested key@"__stringify(__LINE__)": ",
  422. DUMP_PREFIX_ADDRESS, 16, 4, key_in,
  423. digestsize, 1);
  424. #endif
  425. }
  426. dma_unmap_single(jrdev, src_dma, *keylen, DMA_TO_DEVICE);
  427. dma_unmap_single(jrdev, dst_dma, digestsize, DMA_FROM_DEVICE);
  428. *keylen = digestsize;
  429. kfree(desc);
  430. return ret;
  431. }
  432. static int ahash_setkey(struct crypto_ahash *ahash,
  433. const u8 *key, unsigned int keylen)
  434. {
  435. /* Sizes for MDHA pads (*not* keys): MD5, SHA1, 224, 256, 384, 512 */
  436. static const u8 mdpadlen[] = { 16, 20, 32, 32, 64, 64 };
  437. struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
  438. struct device *jrdev = ctx->jrdev;
  439. int blocksize = crypto_tfm_alg_blocksize(&ahash->base);
  440. int digestsize = crypto_ahash_digestsize(ahash);
  441. int ret = 0;
  442. u8 *hashed_key = NULL;
  443. #ifdef DEBUG
  444. printk(KERN_ERR "keylen %d\n", keylen);
  445. #endif
  446. if (keylen > blocksize) {
  447. hashed_key = kmalloc(sizeof(u8) * digestsize, GFP_KERNEL |
  448. GFP_DMA);
  449. if (!hashed_key)
  450. return -ENOMEM;
  451. ret = hash_digest_key(ctx, key, &keylen, hashed_key,
  452. digestsize);
  453. if (ret)
  454. goto badkey;
  455. key = hashed_key;
  456. }
  457. /* Pick class 2 key length from algorithm submask */
  458. ctx->split_key_len = mdpadlen[(ctx->alg_op & OP_ALG_ALGSEL_SUBMASK) >>
  459. OP_ALG_ALGSEL_SHIFT] * 2;
  460. ctx->split_key_pad_len = ALIGN(ctx->split_key_len, 16);
  461. #ifdef DEBUG
  462. printk(KERN_ERR "split_key_len %d split_key_pad_len %d\n",
  463. ctx->split_key_len, ctx->split_key_pad_len);
  464. print_hex_dump(KERN_ERR, "key in @"__stringify(__LINE__)": ",
  465. DUMP_PREFIX_ADDRESS, 16, 4, key, keylen, 1);
  466. #endif
  467. ret = gen_split_hash_key(ctx, key, keylen);
  468. if (ret)
  469. goto badkey;
  470. ctx->key_dma = dma_map_single(jrdev, ctx->key, ctx->split_key_pad_len,
  471. DMA_TO_DEVICE);
  472. if (dma_mapping_error(jrdev, ctx->key_dma)) {
  473. dev_err(jrdev, "unable to map key i/o memory\n");
  474. ret = -ENOMEM;
  475. goto map_err;
  476. }
  477. #ifdef DEBUG
  478. print_hex_dump(KERN_ERR, "ctx.key@"__stringify(__LINE__)": ",
  479. DUMP_PREFIX_ADDRESS, 16, 4, ctx->key,
  480. ctx->split_key_pad_len, 1);
  481. #endif
  482. ret = ahash_set_sh_desc(ahash);
  483. if (ret) {
  484. dma_unmap_single(jrdev, ctx->key_dma, ctx->split_key_pad_len,
  485. DMA_TO_DEVICE);
  486. }
  487. map_err:
  488. kfree(hashed_key);
  489. return ret;
  490. badkey:
  491. kfree(hashed_key);
  492. crypto_ahash_set_flags(ahash, CRYPTO_TFM_RES_BAD_KEY_LEN);
  493. return -EINVAL;
  494. }
  495. /*
  496. * ahash_edesc - s/w-extended ahash descriptor
  497. * @dst_dma: physical mapped address of req->result
  498. * @sec4_sg_dma: physical mapped address of h/w link table
  499. * @chained: if source is chained
  500. * @src_nents: number of segments in input scatterlist
  501. * @sec4_sg_bytes: length of dma mapped sec4_sg space
  502. * @sec4_sg: pointer to h/w link table
  503. * @hw_desc: the h/w job descriptor followed by any referenced link tables
  504. */
  505. struct ahash_edesc {
  506. dma_addr_t dst_dma;
  507. dma_addr_t sec4_sg_dma;
  508. bool chained;
  509. int src_nents;
  510. int sec4_sg_bytes;
  511. struct sec4_sg_entry *sec4_sg;
  512. u32 hw_desc[0];
  513. };
  514. static inline void ahash_unmap(struct device *dev,
  515. struct ahash_edesc *edesc,
  516. struct ahash_request *req, int dst_len)
  517. {
  518. if (edesc->src_nents)
  519. dma_unmap_sg_chained(dev, req->src, edesc->src_nents,
  520. DMA_TO_DEVICE, edesc->chained);
  521. if (edesc->dst_dma)
  522. dma_unmap_single(dev, edesc->dst_dma, dst_len, DMA_FROM_DEVICE);
  523. if (edesc->sec4_sg_bytes)
  524. dma_unmap_single(dev, edesc->sec4_sg_dma,
  525. edesc->sec4_sg_bytes, DMA_TO_DEVICE);
  526. }
  527. static inline void ahash_unmap_ctx(struct device *dev,
  528. struct ahash_edesc *edesc,
  529. struct ahash_request *req, int dst_len, u32 flag)
  530. {
  531. struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
  532. struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
  533. struct caam_hash_state *state = ahash_request_ctx(req);
  534. if (state->ctx_dma)
  535. dma_unmap_single(dev, state->ctx_dma, ctx->ctx_len, flag);
  536. ahash_unmap(dev, edesc, req, dst_len);
  537. }
  538. static void ahash_done(struct device *jrdev, u32 *desc, u32 err,
  539. void *context)
  540. {
  541. struct ahash_request *req = context;
  542. struct ahash_edesc *edesc;
  543. struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
  544. int digestsize = crypto_ahash_digestsize(ahash);
  545. #ifdef DEBUG
  546. struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
  547. struct caam_hash_state *state = ahash_request_ctx(req);
  548. dev_err(jrdev, "%s %d: err 0x%x\n", __func__, __LINE__, err);
  549. #endif
  550. edesc = (struct ahash_edesc *)((char *)desc -
  551. offsetof(struct ahash_edesc, hw_desc));
  552. if (err)
  553. caam_jr_strstatus(jrdev, err);
  554. ahash_unmap(jrdev, edesc, req, digestsize);
  555. kfree(edesc);
  556. #ifdef DEBUG
  557. print_hex_dump(KERN_ERR, "ctx@"__stringify(__LINE__)": ",
  558. DUMP_PREFIX_ADDRESS, 16, 4, state->caam_ctx,
  559. ctx->ctx_len, 1);
  560. if (req->result)
  561. print_hex_dump(KERN_ERR, "result@"__stringify(__LINE__)": ",
  562. DUMP_PREFIX_ADDRESS, 16, 4, req->result,
  563. digestsize, 1);
  564. #endif
  565. req->base.complete(&req->base, err);
  566. }
  567. static void ahash_done_bi(struct device *jrdev, u32 *desc, u32 err,
  568. void *context)
  569. {
  570. struct ahash_request *req = context;
  571. struct ahash_edesc *edesc;
  572. struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
  573. struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
  574. #ifdef DEBUG
  575. struct caam_hash_state *state = ahash_request_ctx(req);
  576. int digestsize = crypto_ahash_digestsize(ahash);
  577. dev_err(jrdev, "%s %d: err 0x%x\n", __func__, __LINE__, err);
  578. #endif
  579. edesc = (struct ahash_edesc *)((char *)desc -
  580. offsetof(struct ahash_edesc, hw_desc));
  581. if (err)
  582. caam_jr_strstatus(jrdev, err);
  583. ahash_unmap_ctx(jrdev, edesc, req, ctx->ctx_len, DMA_BIDIRECTIONAL);
  584. kfree(edesc);
  585. #ifdef DEBUG
  586. print_hex_dump(KERN_ERR, "ctx@"__stringify(__LINE__)": ",
  587. DUMP_PREFIX_ADDRESS, 16, 4, state->caam_ctx,
  588. ctx->ctx_len, 1);
  589. if (req->result)
  590. print_hex_dump(KERN_ERR, "result@"__stringify(__LINE__)": ",
  591. DUMP_PREFIX_ADDRESS, 16, 4, req->result,
  592. digestsize, 1);
  593. #endif
  594. req->base.complete(&req->base, err);
  595. }
  596. static void ahash_done_ctx_src(struct device *jrdev, u32 *desc, u32 err,
  597. void *context)
  598. {
  599. struct ahash_request *req = context;
  600. struct ahash_edesc *edesc;
  601. struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
  602. int digestsize = crypto_ahash_digestsize(ahash);
  603. #ifdef DEBUG
  604. struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
  605. struct caam_hash_state *state = ahash_request_ctx(req);
  606. dev_err(jrdev, "%s %d: err 0x%x\n", __func__, __LINE__, err);
  607. #endif
  608. edesc = (struct ahash_edesc *)((char *)desc -
  609. offsetof(struct ahash_edesc, hw_desc));
  610. if (err)
  611. caam_jr_strstatus(jrdev, err);
  612. ahash_unmap_ctx(jrdev, edesc, req, digestsize, DMA_TO_DEVICE);
  613. kfree(edesc);
  614. #ifdef DEBUG
  615. print_hex_dump(KERN_ERR, "ctx@"__stringify(__LINE__)": ",
  616. DUMP_PREFIX_ADDRESS, 16, 4, state->caam_ctx,
  617. ctx->ctx_len, 1);
  618. if (req->result)
  619. print_hex_dump(KERN_ERR, "result@"__stringify(__LINE__)": ",
  620. DUMP_PREFIX_ADDRESS, 16, 4, req->result,
  621. digestsize, 1);
  622. #endif
  623. req->base.complete(&req->base, err);
  624. }
  625. static void ahash_done_ctx_dst(struct device *jrdev, u32 *desc, u32 err,
  626. void *context)
  627. {
  628. struct ahash_request *req = context;
  629. struct ahash_edesc *edesc;
  630. struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
  631. struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
  632. #ifdef DEBUG
  633. struct caam_hash_state *state = ahash_request_ctx(req);
  634. int digestsize = crypto_ahash_digestsize(ahash);
  635. dev_err(jrdev, "%s %d: err 0x%x\n", __func__, __LINE__, err);
  636. #endif
  637. edesc = (struct ahash_edesc *)((char *)desc -
  638. offsetof(struct ahash_edesc, hw_desc));
  639. if (err)
  640. caam_jr_strstatus(jrdev, err);
  641. ahash_unmap_ctx(jrdev, edesc, req, ctx->ctx_len, DMA_FROM_DEVICE);
  642. kfree(edesc);
  643. #ifdef DEBUG
  644. print_hex_dump(KERN_ERR, "ctx@"__stringify(__LINE__)": ",
  645. DUMP_PREFIX_ADDRESS, 16, 4, state->caam_ctx,
  646. ctx->ctx_len, 1);
  647. if (req->result)
  648. print_hex_dump(KERN_ERR, "result@"__stringify(__LINE__)": ",
  649. DUMP_PREFIX_ADDRESS, 16, 4, req->result,
  650. digestsize, 1);
  651. #endif
  652. req->base.complete(&req->base, err);
  653. }
  654. /* submit update job descriptor */
  655. static int ahash_update_ctx(struct ahash_request *req)
  656. {
  657. struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
  658. struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
  659. struct caam_hash_state *state = ahash_request_ctx(req);
  660. struct device *jrdev = ctx->jrdev;
  661. gfp_t flags = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG |
  662. CRYPTO_TFM_REQ_MAY_SLEEP)) ? GFP_KERNEL : GFP_ATOMIC;
  663. u8 *buf = state->current_buf ? state->buf_1 : state->buf_0;
  664. int *buflen = state->current_buf ? &state->buflen_1 : &state->buflen_0;
  665. u8 *next_buf = state->current_buf ? state->buf_0 : state->buf_1;
  666. int *next_buflen = state->current_buf ? &state->buflen_0 :
  667. &state->buflen_1, last_buflen;
  668. int in_len = *buflen + req->nbytes, to_hash;
  669. u32 *sh_desc = ctx->sh_desc_update, *desc;
  670. dma_addr_t ptr = ctx->sh_desc_update_dma;
  671. int src_nents, sec4_sg_bytes, sec4_sg_src_index;
  672. struct ahash_edesc *edesc;
  673. bool chained = false;
  674. int ret = 0;
  675. int sh_len;
  676. last_buflen = *next_buflen;
  677. *next_buflen = in_len & (crypto_tfm_alg_blocksize(&ahash->base) - 1);
  678. to_hash = in_len - *next_buflen;
  679. if (to_hash) {
  680. src_nents = __sg_count(req->src, req->nbytes - (*next_buflen),
  681. &chained);
  682. sec4_sg_src_index = 1 + (*buflen ? 1 : 0);
  683. sec4_sg_bytes = (sec4_sg_src_index + src_nents) *
  684. sizeof(struct sec4_sg_entry);
  685. /*
  686. * allocate space for base edesc and hw desc commands,
  687. * link tables
  688. */
  689. edesc = kmalloc(sizeof(struct ahash_edesc) + DESC_JOB_IO_LEN +
  690. sec4_sg_bytes, GFP_DMA | flags);
  691. if (!edesc) {
  692. dev_err(jrdev,
  693. "could not allocate extended descriptor\n");
  694. return -ENOMEM;
  695. }
  696. edesc->src_nents = src_nents;
  697. edesc->chained = chained;
  698. edesc->sec4_sg_bytes = sec4_sg_bytes;
  699. edesc->sec4_sg = (void *)edesc + sizeof(struct ahash_edesc) +
  700. DESC_JOB_IO_LEN;
  701. ret = ctx_map_to_sec4_sg(desc, jrdev, state, ctx->ctx_len,
  702. edesc->sec4_sg, DMA_BIDIRECTIONAL);
  703. if (ret)
  704. return ret;
  705. state->buf_dma = try_buf_map_to_sec4_sg(jrdev,
  706. edesc->sec4_sg + 1,
  707. buf, state->buf_dma,
  708. *buflen, last_buflen);
  709. if (src_nents) {
  710. src_map_to_sec4_sg(jrdev, req->src, src_nents,
  711. edesc->sec4_sg + sec4_sg_src_index,
  712. chained);
  713. if (*next_buflen)
  714. scatterwalk_map_and_copy(next_buf, req->src,
  715. to_hash - *buflen,
  716. *next_buflen, 0);
  717. } else {
  718. (edesc->sec4_sg + sec4_sg_src_index - 1)->len |=
  719. SEC4_SG_LEN_FIN;
  720. }
  721. state->current_buf = !state->current_buf;
  722. sh_len = desc_len(sh_desc);
  723. desc = edesc->hw_desc;
  724. init_job_desc_shared(desc, ptr, sh_len, HDR_SHARE_DEFER |
  725. HDR_REVERSE);
  726. edesc->sec4_sg_dma = dma_map_single(jrdev, edesc->sec4_sg,
  727. sec4_sg_bytes,
  728. DMA_TO_DEVICE);
  729. if (dma_mapping_error(jrdev, edesc->sec4_sg_dma)) {
  730. dev_err(jrdev, "unable to map S/G table\n");
  731. return -ENOMEM;
  732. }
  733. append_seq_in_ptr(desc, edesc->sec4_sg_dma, ctx->ctx_len +
  734. to_hash, LDST_SGF);
  735. append_seq_out_ptr(desc, state->ctx_dma, ctx->ctx_len, 0);
  736. #ifdef DEBUG
  737. print_hex_dump(KERN_ERR, "jobdesc@"__stringify(__LINE__)": ",
  738. DUMP_PREFIX_ADDRESS, 16, 4, desc,
  739. desc_bytes(desc), 1);
  740. #endif
  741. ret = caam_jr_enqueue(jrdev, desc, ahash_done_bi, req);
  742. if (!ret) {
  743. ret = -EINPROGRESS;
  744. } else {
  745. ahash_unmap_ctx(jrdev, edesc, req, ctx->ctx_len,
  746. DMA_BIDIRECTIONAL);
  747. kfree(edesc);
  748. }
  749. } else if (*next_buflen) {
  750. scatterwalk_map_and_copy(buf + *buflen, req->src, 0,
  751. req->nbytes, 0);
  752. *buflen = *next_buflen;
  753. *next_buflen = last_buflen;
  754. }
  755. #ifdef DEBUG
  756. print_hex_dump(KERN_ERR, "buf@"__stringify(__LINE__)": ",
  757. DUMP_PREFIX_ADDRESS, 16, 4, buf, *buflen, 1);
  758. print_hex_dump(KERN_ERR, "next buf@"__stringify(__LINE__)": ",
  759. DUMP_PREFIX_ADDRESS, 16, 4, next_buf,
  760. *next_buflen, 1);
  761. #endif
  762. return ret;
  763. }
  764. static int ahash_final_ctx(struct ahash_request *req)
  765. {
  766. struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
  767. struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
  768. struct caam_hash_state *state = ahash_request_ctx(req);
  769. struct device *jrdev = ctx->jrdev;
  770. gfp_t flags = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG |
  771. CRYPTO_TFM_REQ_MAY_SLEEP)) ? GFP_KERNEL : GFP_ATOMIC;
  772. u8 *buf = state->current_buf ? state->buf_1 : state->buf_0;
  773. int buflen = state->current_buf ? state->buflen_1 : state->buflen_0;
  774. int last_buflen = state->current_buf ? state->buflen_0 :
  775. state->buflen_1;
  776. u32 *sh_desc = ctx->sh_desc_fin, *desc;
  777. dma_addr_t ptr = ctx->sh_desc_fin_dma;
  778. int sec4_sg_bytes;
  779. int digestsize = crypto_ahash_digestsize(ahash);
  780. struct ahash_edesc *edesc;
  781. int ret = 0;
  782. int sh_len;
  783. sec4_sg_bytes = (1 + (buflen ? 1 : 0)) * sizeof(struct sec4_sg_entry);
  784. /* allocate space for base edesc and hw desc commands, link tables */
  785. edesc = kmalloc(sizeof(struct ahash_edesc) + DESC_JOB_IO_LEN +
  786. sec4_sg_bytes, GFP_DMA | flags);
  787. if (!edesc) {
  788. dev_err(jrdev, "could not allocate extended descriptor\n");
  789. return -ENOMEM;
  790. }
  791. sh_len = desc_len(sh_desc);
  792. desc = edesc->hw_desc;
  793. init_job_desc_shared(desc, ptr, sh_len, HDR_SHARE_DEFER | HDR_REVERSE);
  794. edesc->sec4_sg_bytes = sec4_sg_bytes;
  795. edesc->sec4_sg = (void *)edesc + sizeof(struct ahash_edesc) +
  796. DESC_JOB_IO_LEN;
  797. edesc->src_nents = 0;
  798. ret = ctx_map_to_sec4_sg(desc, jrdev, state, ctx->ctx_len,
  799. edesc->sec4_sg, DMA_TO_DEVICE);
  800. if (ret)
  801. return ret;
  802. state->buf_dma = try_buf_map_to_sec4_sg(jrdev, edesc->sec4_sg + 1,
  803. buf, state->buf_dma, buflen,
  804. last_buflen);
  805. (edesc->sec4_sg + sec4_sg_bytes - 1)->len |= SEC4_SG_LEN_FIN;
  806. edesc->sec4_sg_dma = dma_map_single(jrdev, edesc->sec4_sg,
  807. sec4_sg_bytes, DMA_TO_DEVICE);
  808. if (dma_mapping_error(jrdev, edesc->sec4_sg_dma)) {
  809. dev_err(jrdev, "unable to map S/G table\n");
  810. return -ENOMEM;
  811. }
  812. append_seq_in_ptr(desc, edesc->sec4_sg_dma, ctx->ctx_len + buflen,
  813. LDST_SGF);
  814. edesc->dst_dma = map_seq_out_ptr_result(desc, jrdev, req->result,
  815. digestsize);
  816. if (dma_mapping_error(jrdev, edesc->dst_dma)) {
  817. dev_err(jrdev, "unable to map dst\n");
  818. return -ENOMEM;
  819. }
  820. #ifdef DEBUG
  821. print_hex_dump(KERN_ERR, "jobdesc@"__stringify(__LINE__)": ",
  822. DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc), 1);
  823. #endif
  824. ret = caam_jr_enqueue(jrdev, desc, ahash_done_ctx_src, req);
  825. if (!ret) {
  826. ret = -EINPROGRESS;
  827. } else {
  828. ahash_unmap_ctx(jrdev, edesc, req, digestsize, DMA_FROM_DEVICE);
  829. kfree(edesc);
  830. }
  831. return ret;
  832. }
  833. static int ahash_finup_ctx(struct ahash_request *req)
  834. {
  835. struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
  836. struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
  837. struct caam_hash_state *state = ahash_request_ctx(req);
  838. struct device *jrdev = ctx->jrdev;
  839. gfp_t flags = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG |
  840. CRYPTO_TFM_REQ_MAY_SLEEP)) ? GFP_KERNEL : GFP_ATOMIC;
  841. u8 *buf = state->current_buf ? state->buf_1 : state->buf_0;
  842. int buflen = state->current_buf ? state->buflen_1 : state->buflen_0;
  843. int last_buflen = state->current_buf ? state->buflen_0 :
  844. state->buflen_1;
  845. u32 *sh_desc = ctx->sh_desc_finup, *desc;
  846. dma_addr_t ptr = ctx->sh_desc_finup_dma;
  847. int sec4_sg_bytes, sec4_sg_src_index;
  848. int src_nents;
  849. int digestsize = crypto_ahash_digestsize(ahash);
  850. struct ahash_edesc *edesc;
  851. bool chained = false;
  852. int ret = 0;
  853. int sh_len;
  854. src_nents = __sg_count(req->src, req->nbytes, &chained);
  855. sec4_sg_src_index = 1 + (buflen ? 1 : 0);
  856. sec4_sg_bytes = (sec4_sg_src_index + src_nents) *
  857. sizeof(struct sec4_sg_entry);
  858. /* allocate space for base edesc and hw desc commands, link tables */
  859. edesc = kmalloc(sizeof(struct ahash_edesc) + DESC_JOB_IO_LEN +
  860. sec4_sg_bytes, GFP_DMA | flags);
  861. if (!edesc) {
  862. dev_err(jrdev, "could not allocate extended descriptor\n");
  863. return -ENOMEM;
  864. }
  865. sh_len = desc_len(sh_desc);
  866. desc = edesc->hw_desc;
  867. init_job_desc_shared(desc, ptr, sh_len, HDR_SHARE_DEFER | HDR_REVERSE);
  868. edesc->src_nents = src_nents;
  869. edesc->chained = chained;
  870. edesc->sec4_sg_bytes = sec4_sg_bytes;
  871. edesc->sec4_sg = (void *)edesc + sizeof(struct ahash_edesc) +
  872. DESC_JOB_IO_LEN;
  873. ret = ctx_map_to_sec4_sg(desc, jrdev, state, ctx->ctx_len,
  874. edesc->sec4_sg, DMA_TO_DEVICE);
  875. if (ret)
  876. return ret;
  877. state->buf_dma = try_buf_map_to_sec4_sg(jrdev, edesc->sec4_sg + 1,
  878. buf, state->buf_dma, buflen,
  879. last_buflen);
  880. src_map_to_sec4_sg(jrdev, req->src, src_nents, edesc->sec4_sg +
  881. sec4_sg_src_index, chained);
  882. edesc->sec4_sg_dma = dma_map_single(jrdev, edesc->sec4_sg,
  883. sec4_sg_bytes, DMA_TO_DEVICE);
  884. if (dma_mapping_error(jrdev, edesc->sec4_sg_dma)) {
  885. dev_err(jrdev, "unable to map S/G table\n");
  886. return -ENOMEM;
  887. }
  888. append_seq_in_ptr(desc, edesc->sec4_sg_dma, ctx->ctx_len +
  889. buflen + req->nbytes, LDST_SGF);
  890. edesc->dst_dma = map_seq_out_ptr_result(desc, jrdev, req->result,
  891. digestsize);
  892. if (dma_mapping_error(jrdev, edesc->dst_dma)) {
  893. dev_err(jrdev, "unable to map dst\n");
  894. return -ENOMEM;
  895. }
  896. #ifdef DEBUG
  897. print_hex_dump(KERN_ERR, "jobdesc@"__stringify(__LINE__)": ",
  898. DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc), 1);
  899. #endif
  900. ret = caam_jr_enqueue(jrdev, desc, ahash_done_ctx_src, req);
  901. if (!ret) {
  902. ret = -EINPROGRESS;
  903. } else {
  904. ahash_unmap_ctx(jrdev, edesc, req, digestsize, DMA_FROM_DEVICE);
  905. kfree(edesc);
  906. }
  907. return ret;
  908. }
  909. static int ahash_digest(struct ahash_request *req)
  910. {
  911. struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
  912. struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
  913. struct device *jrdev = ctx->jrdev;
  914. gfp_t flags = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG |
  915. CRYPTO_TFM_REQ_MAY_SLEEP)) ? GFP_KERNEL : GFP_ATOMIC;
  916. u32 *sh_desc = ctx->sh_desc_digest, *desc;
  917. dma_addr_t ptr = ctx->sh_desc_digest_dma;
  918. int digestsize = crypto_ahash_digestsize(ahash);
  919. int src_nents, sec4_sg_bytes;
  920. dma_addr_t src_dma;
  921. struct ahash_edesc *edesc;
  922. bool chained = false;
  923. int ret = 0;
  924. u32 options;
  925. int sh_len;
  926. src_nents = sg_count(req->src, req->nbytes, &chained);
  927. dma_map_sg_chained(jrdev, req->src, src_nents ? : 1, DMA_TO_DEVICE,
  928. chained);
  929. sec4_sg_bytes = src_nents * sizeof(struct sec4_sg_entry);
  930. /* allocate space for base edesc and hw desc commands, link tables */
  931. edesc = kmalloc(sizeof(struct ahash_edesc) + sec4_sg_bytes +
  932. DESC_JOB_IO_LEN, GFP_DMA | flags);
  933. if (!edesc) {
  934. dev_err(jrdev, "could not allocate extended descriptor\n");
  935. return -ENOMEM;
  936. }
  937. edesc->sec4_sg = (void *)edesc + sizeof(struct ahash_edesc) +
  938. DESC_JOB_IO_LEN;
  939. edesc->sec4_sg_bytes = sec4_sg_bytes;
  940. edesc->src_nents = src_nents;
  941. edesc->chained = chained;
  942. sh_len = desc_len(sh_desc);
  943. desc = edesc->hw_desc;
  944. init_job_desc_shared(desc, ptr, sh_len, HDR_SHARE_DEFER | HDR_REVERSE);
  945. if (src_nents) {
  946. sg_to_sec4_sg_last(req->src, src_nents, edesc->sec4_sg, 0);
  947. edesc->sec4_sg_dma = dma_map_single(jrdev, edesc->sec4_sg,
  948. sec4_sg_bytes, DMA_TO_DEVICE);
  949. if (dma_mapping_error(jrdev, edesc->sec4_sg_dma)) {
  950. dev_err(jrdev, "unable to map S/G table\n");
  951. return -ENOMEM;
  952. }
  953. src_dma = edesc->sec4_sg_dma;
  954. options = LDST_SGF;
  955. } else {
  956. src_dma = sg_dma_address(req->src);
  957. options = 0;
  958. }
  959. append_seq_in_ptr(desc, src_dma, req->nbytes, options);
  960. edesc->dst_dma = map_seq_out_ptr_result(desc, jrdev, req->result,
  961. digestsize);
  962. if (dma_mapping_error(jrdev, edesc->dst_dma)) {
  963. dev_err(jrdev, "unable to map dst\n");
  964. return -ENOMEM;
  965. }
  966. #ifdef DEBUG
  967. print_hex_dump(KERN_ERR, "jobdesc@"__stringify(__LINE__)": ",
  968. DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc), 1);
  969. #endif
  970. ret = caam_jr_enqueue(jrdev, desc, ahash_done, req);
  971. if (!ret) {
  972. ret = -EINPROGRESS;
  973. } else {
  974. ahash_unmap(jrdev, edesc, req, digestsize);
  975. kfree(edesc);
  976. }
  977. return ret;
  978. }
  979. /* submit ahash final if it the first job descriptor */
  980. static int ahash_final_no_ctx(struct ahash_request *req)
  981. {
  982. struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
  983. struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
  984. struct caam_hash_state *state = ahash_request_ctx(req);
  985. struct device *jrdev = ctx->jrdev;
  986. gfp_t flags = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG |
  987. CRYPTO_TFM_REQ_MAY_SLEEP)) ? GFP_KERNEL : GFP_ATOMIC;
  988. u8 *buf = state->current_buf ? state->buf_1 : state->buf_0;
  989. int buflen = state->current_buf ? state->buflen_1 : state->buflen_0;
  990. u32 *sh_desc = ctx->sh_desc_digest, *desc;
  991. dma_addr_t ptr = ctx->sh_desc_digest_dma;
  992. int digestsize = crypto_ahash_digestsize(ahash);
  993. struct ahash_edesc *edesc;
  994. int ret = 0;
  995. int sh_len;
  996. /* allocate space for base edesc and hw desc commands, link tables */
  997. edesc = kmalloc(sizeof(struct ahash_edesc) + DESC_JOB_IO_LEN,
  998. GFP_DMA | flags);
  999. if (!edesc) {
  1000. dev_err(jrdev, "could not allocate extended descriptor\n");
  1001. return -ENOMEM;
  1002. }
  1003. edesc->sec4_sg_bytes = 0;
  1004. sh_len = desc_len(sh_desc);
  1005. desc = edesc->hw_desc;
  1006. init_job_desc_shared(desc, ptr, sh_len, HDR_SHARE_DEFER | HDR_REVERSE);
  1007. state->buf_dma = dma_map_single(jrdev, buf, buflen, DMA_TO_DEVICE);
  1008. if (dma_mapping_error(jrdev, state->buf_dma)) {
  1009. dev_err(jrdev, "unable to map src\n");
  1010. return -ENOMEM;
  1011. }
  1012. append_seq_in_ptr(desc, state->buf_dma, buflen, 0);
  1013. edesc->dst_dma = map_seq_out_ptr_result(desc, jrdev, req->result,
  1014. digestsize);
  1015. if (dma_mapping_error(jrdev, edesc->dst_dma)) {
  1016. dev_err(jrdev, "unable to map dst\n");
  1017. return -ENOMEM;
  1018. }
  1019. edesc->src_nents = 0;
  1020. #ifdef DEBUG
  1021. print_hex_dump(KERN_ERR, "jobdesc@"__stringify(__LINE__)": ",
  1022. DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc), 1);
  1023. #endif
  1024. ret = caam_jr_enqueue(jrdev, desc, ahash_done, req);
  1025. if (!ret) {
  1026. ret = -EINPROGRESS;
  1027. } else {
  1028. ahash_unmap(jrdev, edesc, req, digestsize);
  1029. kfree(edesc);
  1030. }
  1031. return ret;
  1032. }
  1033. /* submit ahash update if it the first job descriptor after update */
  1034. static int ahash_update_no_ctx(struct ahash_request *req)
  1035. {
  1036. struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
  1037. struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
  1038. struct caam_hash_state *state = ahash_request_ctx(req);
  1039. struct device *jrdev = ctx->jrdev;
  1040. gfp_t flags = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG |
  1041. CRYPTO_TFM_REQ_MAY_SLEEP)) ? GFP_KERNEL : GFP_ATOMIC;
  1042. u8 *buf = state->current_buf ? state->buf_1 : state->buf_0;
  1043. int *buflen = state->current_buf ? &state->buflen_1 : &state->buflen_0;
  1044. u8 *next_buf = state->current_buf ? state->buf_0 : state->buf_1;
  1045. int *next_buflen = state->current_buf ? &state->buflen_0 :
  1046. &state->buflen_1;
  1047. int in_len = *buflen + req->nbytes, to_hash;
  1048. int sec4_sg_bytes, src_nents;
  1049. struct ahash_edesc *edesc;
  1050. u32 *desc, *sh_desc = ctx->sh_desc_update_first;
  1051. dma_addr_t ptr = ctx->sh_desc_update_first_dma;
  1052. bool chained = false;
  1053. int ret = 0;
  1054. int sh_len;
  1055. *next_buflen = in_len & (crypto_tfm_alg_blocksize(&ahash->base) - 1);
  1056. to_hash = in_len - *next_buflen;
  1057. if (to_hash) {
  1058. src_nents = __sg_count(req->src, req->nbytes - (*next_buflen),
  1059. &chained);
  1060. sec4_sg_bytes = (1 + src_nents) *
  1061. sizeof(struct sec4_sg_entry);
  1062. /*
  1063. * allocate space for base edesc and hw desc commands,
  1064. * link tables
  1065. */
  1066. edesc = kmalloc(sizeof(struct ahash_edesc) + DESC_JOB_IO_LEN +
  1067. sec4_sg_bytes, GFP_DMA | flags);
  1068. if (!edesc) {
  1069. dev_err(jrdev,
  1070. "could not allocate extended descriptor\n");
  1071. return -ENOMEM;
  1072. }
  1073. edesc->src_nents = src_nents;
  1074. edesc->chained = chained;
  1075. edesc->sec4_sg_bytes = sec4_sg_bytes;
  1076. edesc->sec4_sg = (void *)edesc + sizeof(struct ahash_edesc) +
  1077. DESC_JOB_IO_LEN;
  1078. edesc->dst_dma = 0;
  1079. state->buf_dma = buf_map_to_sec4_sg(jrdev, edesc->sec4_sg,
  1080. buf, *buflen);
  1081. src_map_to_sec4_sg(jrdev, req->src, src_nents,
  1082. edesc->sec4_sg + 1, chained);
  1083. if (*next_buflen) {
  1084. scatterwalk_map_and_copy(next_buf, req->src,
  1085. to_hash - *buflen,
  1086. *next_buflen, 0);
  1087. }
  1088. state->current_buf = !state->current_buf;
  1089. sh_len = desc_len(sh_desc);
  1090. desc = edesc->hw_desc;
  1091. init_job_desc_shared(desc, ptr, sh_len, HDR_SHARE_DEFER |
  1092. HDR_REVERSE);
  1093. edesc->sec4_sg_dma = dma_map_single(jrdev, edesc->sec4_sg,
  1094. sec4_sg_bytes,
  1095. DMA_TO_DEVICE);
  1096. if (dma_mapping_error(jrdev, edesc->sec4_sg_dma)) {
  1097. dev_err(jrdev, "unable to map S/G table\n");
  1098. return -ENOMEM;
  1099. }
  1100. append_seq_in_ptr(desc, edesc->sec4_sg_dma, to_hash, LDST_SGF);
  1101. ret = map_seq_out_ptr_ctx(desc, jrdev, state, ctx->ctx_len);
  1102. if (ret)
  1103. return ret;
  1104. #ifdef DEBUG
  1105. print_hex_dump(KERN_ERR, "jobdesc@"__stringify(__LINE__)": ",
  1106. DUMP_PREFIX_ADDRESS, 16, 4, desc,
  1107. desc_bytes(desc), 1);
  1108. #endif
  1109. ret = caam_jr_enqueue(jrdev, desc, ahash_done_ctx_dst, req);
  1110. if (!ret) {
  1111. ret = -EINPROGRESS;
  1112. state->update = ahash_update_ctx;
  1113. state->finup = ahash_finup_ctx;
  1114. state->final = ahash_final_ctx;
  1115. } else {
  1116. ahash_unmap_ctx(jrdev, edesc, req, ctx->ctx_len,
  1117. DMA_TO_DEVICE);
  1118. kfree(edesc);
  1119. }
  1120. } else if (*next_buflen) {
  1121. scatterwalk_map_and_copy(buf + *buflen, req->src, 0,
  1122. req->nbytes, 0);
  1123. *buflen = *next_buflen;
  1124. *next_buflen = 0;
  1125. }
  1126. #ifdef DEBUG
  1127. print_hex_dump(KERN_ERR, "buf@"__stringify(__LINE__)": ",
  1128. DUMP_PREFIX_ADDRESS, 16, 4, buf, *buflen, 1);
  1129. print_hex_dump(KERN_ERR, "next buf@"__stringify(__LINE__)": ",
  1130. DUMP_PREFIX_ADDRESS, 16, 4, next_buf,
  1131. *next_buflen, 1);
  1132. #endif
  1133. return ret;
  1134. }
  1135. /* submit ahash finup if it the first job descriptor after update */
  1136. static int ahash_finup_no_ctx(struct ahash_request *req)
  1137. {
  1138. struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
  1139. struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
  1140. struct caam_hash_state *state = ahash_request_ctx(req);
  1141. struct device *jrdev = ctx->jrdev;
  1142. gfp_t flags = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG |
  1143. CRYPTO_TFM_REQ_MAY_SLEEP)) ? GFP_KERNEL : GFP_ATOMIC;
  1144. u8 *buf = state->current_buf ? state->buf_1 : state->buf_0;
  1145. int buflen = state->current_buf ? state->buflen_1 : state->buflen_0;
  1146. int last_buflen = state->current_buf ? state->buflen_0 :
  1147. state->buflen_1;
  1148. u32 *sh_desc = ctx->sh_desc_digest, *desc;
  1149. dma_addr_t ptr = ctx->sh_desc_digest_dma;
  1150. int sec4_sg_bytes, sec4_sg_src_index, src_nents;
  1151. int digestsize = crypto_ahash_digestsize(ahash);
  1152. struct ahash_edesc *edesc;
  1153. bool chained = false;
  1154. int sh_len;
  1155. int ret = 0;
  1156. src_nents = __sg_count(req->src, req->nbytes, &chained);
  1157. sec4_sg_src_index = 2;
  1158. sec4_sg_bytes = (sec4_sg_src_index + src_nents) *
  1159. sizeof(struct sec4_sg_entry);
  1160. /* allocate space for base edesc and hw desc commands, link tables */
  1161. edesc = kmalloc(sizeof(struct ahash_edesc) + DESC_JOB_IO_LEN +
  1162. sec4_sg_bytes, GFP_DMA | flags);
  1163. if (!edesc) {
  1164. dev_err(jrdev, "could not allocate extended descriptor\n");
  1165. return -ENOMEM;
  1166. }
  1167. sh_len = desc_len(sh_desc);
  1168. desc = edesc->hw_desc;
  1169. init_job_desc_shared(desc, ptr, sh_len, HDR_SHARE_DEFER | HDR_REVERSE);
  1170. edesc->src_nents = src_nents;
  1171. edesc->chained = chained;
  1172. edesc->sec4_sg_bytes = sec4_sg_bytes;
  1173. edesc->sec4_sg = (void *)edesc + sizeof(struct ahash_edesc) +
  1174. DESC_JOB_IO_LEN;
  1175. state->buf_dma = try_buf_map_to_sec4_sg(jrdev, edesc->sec4_sg, buf,
  1176. state->buf_dma, buflen,
  1177. last_buflen);
  1178. src_map_to_sec4_sg(jrdev, req->src, src_nents, edesc->sec4_sg + 1,
  1179. chained);
  1180. edesc->sec4_sg_dma = dma_map_single(jrdev, edesc->sec4_sg,
  1181. sec4_sg_bytes, DMA_TO_DEVICE);
  1182. if (dma_mapping_error(jrdev, edesc->sec4_sg_dma)) {
  1183. dev_err(jrdev, "unable to map S/G table\n");
  1184. return -ENOMEM;
  1185. }
  1186. append_seq_in_ptr(desc, edesc->sec4_sg_dma, buflen +
  1187. req->nbytes, LDST_SGF);
  1188. edesc->dst_dma = map_seq_out_ptr_result(desc, jrdev, req->result,
  1189. digestsize);
  1190. if (dma_mapping_error(jrdev, edesc->dst_dma)) {
  1191. dev_err(jrdev, "unable to map dst\n");
  1192. return -ENOMEM;
  1193. }
  1194. #ifdef DEBUG
  1195. print_hex_dump(KERN_ERR, "jobdesc@"__stringify(__LINE__)": ",
  1196. DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc), 1);
  1197. #endif
  1198. ret = caam_jr_enqueue(jrdev, desc, ahash_done, req);
  1199. if (!ret) {
  1200. ret = -EINPROGRESS;
  1201. } else {
  1202. ahash_unmap(jrdev, edesc, req, digestsize);
  1203. kfree(edesc);
  1204. }
  1205. return ret;
  1206. }
  1207. /* submit first update job descriptor after init */
  1208. static int ahash_update_first(struct ahash_request *req)
  1209. {
  1210. struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
  1211. struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
  1212. struct caam_hash_state *state = ahash_request_ctx(req);
  1213. struct device *jrdev = ctx->jrdev;
  1214. gfp_t flags = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG |
  1215. CRYPTO_TFM_REQ_MAY_SLEEP)) ? GFP_KERNEL : GFP_ATOMIC;
  1216. u8 *next_buf = state->current_buf ? state->buf_1 : state->buf_0;
  1217. int *next_buflen = state->current_buf ?
  1218. &state->buflen_1 : &state->buflen_0;
  1219. int to_hash;
  1220. u32 *sh_desc = ctx->sh_desc_update_first, *desc;
  1221. dma_addr_t ptr = ctx->sh_desc_update_first_dma;
  1222. int sec4_sg_bytes, src_nents;
  1223. dma_addr_t src_dma;
  1224. u32 options;
  1225. struct ahash_edesc *edesc;
  1226. bool chained = false;
  1227. int ret = 0;
  1228. int sh_len;
  1229. *next_buflen = req->nbytes & (crypto_tfm_alg_blocksize(&ahash->base) -
  1230. 1);
  1231. to_hash = req->nbytes - *next_buflen;
  1232. if (to_hash) {
  1233. src_nents = sg_count(req->src, req->nbytes - (*next_buflen),
  1234. &chained);
  1235. dma_map_sg_chained(jrdev, req->src, src_nents ? : 1,
  1236. DMA_TO_DEVICE, chained);
  1237. sec4_sg_bytes = src_nents * sizeof(struct sec4_sg_entry);
  1238. /*
  1239. * allocate space for base edesc and hw desc commands,
  1240. * link tables
  1241. */
  1242. edesc = kmalloc(sizeof(struct ahash_edesc) + DESC_JOB_IO_LEN +
  1243. sec4_sg_bytes, GFP_DMA | flags);
  1244. if (!edesc) {
  1245. dev_err(jrdev,
  1246. "could not allocate extended descriptor\n");
  1247. return -ENOMEM;
  1248. }
  1249. edesc->src_nents = src_nents;
  1250. edesc->chained = chained;
  1251. edesc->sec4_sg_bytes = sec4_sg_bytes;
  1252. edesc->sec4_sg = (void *)edesc + sizeof(struct ahash_edesc) +
  1253. DESC_JOB_IO_LEN;
  1254. edesc->dst_dma = 0;
  1255. if (src_nents) {
  1256. sg_to_sec4_sg_last(req->src, src_nents,
  1257. edesc->sec4_sg, 0);
  1258. edesc->sec4_sg_dma = dma_map_single(jrdev,
  1259. edesc->sec4_sg,
  1260. sec4_sg_bytes,
  1261. DMA_TO_DEVICE);
  1262. if (dma_mapping_error(jrdev, edesc->sec4_sg_dma)) {
  1263. dev_err(jrdev, "unable to map S/G table\n");
  1264. return -ENOMEM;
  1265. }
  1266. src_dma = edesc->sec4_sg_dma;
  1267. options = LDST_SGF;
  1268. } else {
  1269. src_dma = sg_dma_address(req->src);
  1270. options = 0;
  1271. }
  1272. if (*next_buflen)
  1273. scatterwalk_map_and_copy(next_buf, req->src, to_hash,
  1274. *next_buflen, 0);
  1275. sh_len = desc_len(sh_desc);
  1276. desc = edesc->hw_desc;
  1277. init_job_desc_shared(desc, ptr, sh_len, HDR_SHARE_DEFER |
  1278. HDR_REVERSE);
  1279. append_seq_in_ptr(desc, src_dma, to_hash, options);
  1280. ret = map_seq_out_ptr_ctx(desc, jrdev, state, ctx->ctx_len);
  1281. if (ret)
  1282. return ret;
  1283. #ifdef DEBUG
  1284. print_hex_dump(KERN_ERR, "jobdesc@"__stringify(__LINE__)": ",
  1285. DUMP_PREFIX_ADDRESS, 16, 4, desc,
  1286. desc_bytes(desc), 1);
  1287. #endif
  1288. ret = caam_jr_enqueue(jrdev, desc, ahash_done_ctx_dst,
  1289. req);
  1290. if (!ret) {
  1291. ret = -EINPROGRESS;
  1292. state->update = ahash_update_ctx;
  1293. state->finup = ahash_finup_ctx;
  1294. state->final = ahash_final_ctx;
  1295. } else {
  1296. ahash_unmap_ctx(jrdev, edesc, req, ctx->ctx_len,
  1297. DMA_TO_DEVICE);
  1298. kfree(edesc);
  1299. }
  1300. } else if (*next_buflen) {
  1301. state->update = ahash_update_no_ctx;
  1302. state->finup = ahash_finup_no_ctx;
  1303. state->final = ahash_final_no_ctx;
  1304. scatterwalk_map_and_copy(next_buf, req->src, 0,
  1305. req->nbytes, 0);
  1306. }
  1307. #ifdef DEBUG
  1308. print_hex_dump(KERN_ERR, "next buf@"__stringify(__LINE__)": ",
  1309. DUMP_PREFIX_ADDRESS, 16, 4, next_buf,
  1310. *next_buflen, 1);
  1311. #endif
  1312. return ret;
  1313. }
  1314. static int ahash_finup_first(struct ahash_request *req)
  1315. {
  1316. return ahash_digest(req);
  1317. }
  1318. static int ahash_init(struct ahash_request *req)
  1319. {
  1320. struct caam_hash_state *state = ahash_request_ctx(req);
  1321. state->update = ahash_update_first;
  1322. state->finup = ahash_finup_first;
  1323. state->final = ahash_final_no_ctx;
  1324. state->current_buf = 0;
  1325. state->buf_dma = 0;
  1326. state->buflen_0 = 0;
  1327. state->buflen_1 = 0;
  1328. return 0;
  1329. }
  1330. static int ahash_update(struct ahash_request *req)
  1331. {
  1332. struct caam_hash_state *state = ahash_request_ctx(req);
  1333. return state->update(req);
  1334. }
  1335. static int ahash_finup(struct ahash_request *req)
  1336. {
  1337. struct caam_hash_state *state = ahash_request_ctx(req);
  1338. return state->finup(req);
  1339. }
  1340. static int ahash_final(struct ahash_request *req)
  1341. {
  1342. struct caam_hash_state *state = ahash_request_ctx(req);
  1343. return state->final(req);
  1344. }
  1345. static int ahash_export(struct ahash_request *req, void *out)
  1346. {
  1347. struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
  1348. struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
  1349. struct caam_hash_state *state = ahash_request_ctx(req);
  1350. memcpy(out, ctx, sizeof(struct caam_hash_ctx));
  1351. memcpy(out + sizeof(struct caam_hash_ctx), state,
  1352. sizeof(struct caam_hash_state));
  1353. return 0;
  1354. }
  1355. static int ahash_import(struct ahash_request *req, const void *in)
  1356. {
  1357. struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
  1358. struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
  1359. struct caam_hash_state *state = ahash_request_ctx(req);
  1360. memcpy(ctx, in, sizeof(struct caam_hash_ctx));
  1361. memcpy(state, in + sizeof(struct caam_hash_ctx),
  1362. sizeof(struct caam_hash_state));
  1363. return 0;
  1364. }
  1365. struct caam_hash_template {
  1366. char name[CRYPTO_MAX_ALG_NAME];
  1367. char driver_name[CRYPTO_MAX_ALG_NAME];
  1368. char hmac_name[CRYPTO_MAX_ALG_NAME];
  1369. char hmac_driver_name[CRYPTO_MAX_ALG_NAME];
  1370. unsigned int blocksize;
  1371. struct ahash_alg template_ahash;
  1372. u32 alg_type;
  1373. u32 alg_op;
  1374. };
  1375. /* ahash descriptors */
  1376. static struct caam_hash_template driver_hash[] = {
  1377. {
  1378. .name = "sha1",
  1379. .driver_name = "sha1-caam",
  1380. .hmac_name = "hmac(sha1)",
  1381. .hmac_driver_name = "hmac-sha1-caam",
  1382. .blocksize = SHA1_BLOCK_SIZE,
  1383. .template_ahash = {
  1384. .init = ahash_init,
  1385. .update = ahash_update,
  1386. .final = ahash_final,
  1387. .finup = ahash_finup,
  1388. .digest = ahash_digest,
  1389. .export = ahash_export,
  1390. .import = ahash_import,
  1391. .setkey = ahash_setkey,
  1392. .halg = {
  1393. .digestsize = SHA1_DIGEST_SIZE,
  1394. },
  1395. },
  1396. .alg_type = OP_ALG_ALGSEL_SHA1,
  1397. .alg_op = OP_ALG_ALGSEL_SHA1 | OP_ALG_AAI_HMAC,
  1398. }, {
  1399. .name = "sha224",
  1400. .driver_name = "sha224-caam",
  1401. .hmac_name = "hmac(sha224)",
  1402. .hmac_driver_name = "hmac-sha224-caam",
  1403. .blocksize = SHA224_BLOCK_SIZE,
  1404. .template_ahash = {
  1405. .init = ahash_init,
  1406. .update = ahash_update,
  1407. .final = ahash_final,
  1408. .finup = ahash_finup,
  1409. .digest = ahash_digest,
  1410. .export = ahash_export,
  1411. .import = ahash_import,
  1412. .setkey = ahash_setkey,
  1413. .halg = {
  1414. .digestsize = SHA224_DIGEST_SIZE,
  1415. },
  1416. },
  1417. .alg_type = OP_ALG_ALGSEL_SHA224,
  1418. .alg_op = OP_ALG_ALGSEL_SHA224 | OP_ALG_AAI_HMAC,
  1419. }, {
  1420. .name = "sha256",
  1421. .driver_name = "sha256-caam",
  1422. .hmac_name = "hmac(sha256)",
  1423. .hmac_driver_name = "hmac-sha256-caam",
  1424. .blocksize = SHA256_BLOCK_SIZE,
  1425. .template_ahash = {
  1426. .init = ahash_init,
  1427. .update = ahash_update,
  1428. .final = ahash_final,
  1429. .finup = ahash_finup,
  1430. .digest = ahash_digest,
  1431. .export = ahash_export,
  1432. .import = ahash_import,
  1433. .setkey = ahash_setkey,
  1434. .halg = {
  1435. .digestsize = SHA256_DIGEST_SIZE,
  1436. },
  1437. },
  1438. .alg_type = OP_ALG_ALGSEL_SHA256,
  1439. .alg_op = OP_ALG_ALGSEL_SHA256 | OP_ALG_AAI_HMAC,
  1440. }, {
  1441. .name = "sha384",
  1442. .driver_name = "sha384-caam",
  1443. .hmac_name = "hmac(sha384)",
  1444. .hmac_driver_name = "hmac-sha384-caam",
  1445. .blocksize = SHA384_BLOCK_SIZE,
  1446. .template_ahash = {
  1447. .init = ahash_init,
  1448. .update = ahash_update,
  1449. .final = ahash_final,
  1450. .finup = ahash_finup,
  1451. .digest = ahash_digest,
  1452. .export = ahash_export,
  1453. .import = ahash_import,
  1454. .setkey = ahash_setkey,
  1455. .halg = {
  1456. .digestsize = SHA384_DIGEST_SIZE,
  1457. },
  1458. },
  1459. .alg_type = OP_ALG_ALGSEL_SHA384,
  1460. .alg_op = OP_ALG_ALGSEL_SHA384 | OP_ALG_AAI_HMAC,
  1461. }, {
  1462. .name = "sha512",
  1463. .driver_name = "sha512-caam",
  1464. .hmac_name = "hmac(sha512)",
  1465. .hmac_driver_name = "hmac-sha512-caam",
  1466. .blocksize = SHA512_BLOCK_SIZE,
  1467. .template_ahash = {
  1468. .init = ahash_init,
  1469. .update = ahash_update,
  1470. .final = ahash_final,
  1471. .finup = ahash_finup,
  1472. .digest = ahash_digest,
  1473. .export = ahash_export,
  1474. .import = ahash_import,
  1475. .setkey = ahash_setkey,
  1476. .halg = {
  1477. .digestsize = SHA512_DIGEST_SIZE,
  1478. },
  1479. },
  1480. .alg_type = OP_ALG_ALGSEL_SHA512,
  1481. .alg_op = OP_ALG_ALGSEL_SHA512 | OP_ALG_AAI_HMAC,
  1482. }, {
  1483. .name = "md5",
  1484. .driver_name = "md5-caam",
  1485. .hmac_name = "hmac(md5)",
  1486. .hmac_driver_name = "hmac-md5-caam",
  1487. .blocksize = MD5_BLOCK_WORDS * 4,
  1488. .template_ahash = {
  1489. .init = ahash_init,
  1490. .update = ahash_update,
  1491. .final = ahash_final,
  1492. .finup = ahash_finup,
  1493. .digest = ahash_digest,
  1494. .export = ahash_export,
  1495. .import = ahash_import,
  1496. .setkey = ahash_setkey,
  1497. .halg = {
  1498. .digestsize = MD5_DIGEST_SIZE,
  1499. },
  1500. },
  1501. .alg_type = OP_ALG_ALGSEL_MD5,
  1502. .alg_op = OP_ALG_ALGSEL_MD5 | OP_ALG_AAI_HMAC,
  1503. },
  1504. };
  1505. struct caam_hash_alg {
  1506. struct list_head entry;
  1507. int alg_type;
  1508. int alg_op;
  1509. struct ahash_alg ahash_alg;
  1510. };
  1511. static int caam_hash_cra_init(struct crypto_tfm *tfm)
  1512. {
  1513. struct crypto_ahash *ahash = __crypto_ahash_cast(tfm);
  1514. struct crypto_alg *base = tfm->__crt_alg;
  1515. struct hash_alg_common *halg =
  1516. container_of(base, struct hash_alg_common, base);
  1517. struct ahash_alg *alg =
  1518. container_of(halg, struct ahash_alg, halg);
  1519. struct caam_hash_alg *caam_hash =
  1520. container_of(alg, struct caam_hash_alg, ahash_alg);
  1521. struct caam_hash_ctx *ctx = crypto_tfm_ctx(tfm);
  1522. /* Sizes for MDHA running digests: MD5, SHA1, 224, 256, 384, 512 */
  1523. static const u8 runninglen[] = { HASH_MSG_LEN + MD5_DIGEST_SIZE,
  1524. HASH_MSG_LEN + SHA1_DIGEST_SIZE,
  1525. HASH_MSG_LEN + 32,
  1526. HASH_MSG_LEN + SHA256_DIGEST_SIZE,
  1527. HASH_MSG_LEN + 64,
  1528. HASH_MSG_LEN + SHA512_DIGEST_SIZE };
  1529. int ret = 0;
  1530. /*
  1531. * Get a Job ring from Job Ring driver to ensure in-order
  1532. * crypto request processing per tfm
  1533. */
  1534. ctx->jrdev = caam_jr_alloc();
  1535. if (IS_ERR(ctx->jrdev)) {
  1536. pr_err("Job Ring Device allocation for transform failed\n");
  1537. return PTR_ERR(ctx->jrdev);
  1538. }
  1539. /* copy descriptor header template value */
  1540. ctx->alg_type = OP_TYPE_CLASS2_ALG | caam_hash->alg_type;
  1541. ctx->alg_op = OP_TYPE_CLASS2_ALG | caam_hash->alg_op;
  1542. ctx->ctx_len = runninglen[(ctx->alg_op & OP_ALG_ALGSEL_SUBMASK) >>
  1543. OP_ALG_ALGSEL_SHIFT];
  1544. crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
  1545. sizeof(struct caam_hash_state));
  1546. ret = ahash_set_sh_desc(ahash);
  1547. return ret;
  1548. }
  1549. static void caam_hash_cra_exit(struct crypto_tfm *tfm)
  1550. {
  1551. struct caam_hash_ctx *ctx = crypto_tfm_ctx(tfm);
  1552. if (ctx->sh_desc_update_dma &&
  1553. !dma_mapping_error(ctx->jrdev, ctx->sh_desc_update_dma))
  1554. dma_unmap_single(ctx->jrdev, ctx->sh_desc_update_dma,
  1555. desc_bytes(ctx->sh_desc_update),
  1556. DMA_TO_DEVICE);
  1557. if (ctx->sh_desc_update_first_dma &&
  1558. !dma_mapping_error(ctx->jrdev, ctx->sh_desc_update_first_dma))
  1559. dma_unmap_single(ctx->jrdev, ctx->sh_desc_update_first_dma,
  1560. desc_bytes(ctx->sh_desc_update_first),
  1561. DMA_TO_DEVICE);
  1562. if (ctx->sh_desc_fin_dma &&
  1563. !dma_mapping_error(ctx->jrdev, ctx->sh_desc_fin_dma))
  1564. dma_unmap_single(ctx->jrdev, ctx->sh_desc_fin_dma,
  1565. desc_bytes(ctx->sh_desc_fin), DMA_TO_DEVICE);
  1566. if (ctx->sh_desc_digest_dma &&
  1567. !dma_mapping_error(ctx->jrdev, ctx->sh_desc_digest_dma))
  1568. dma_unmap_single(ctx->jrdev, ctx->sh_desc_digest_dma,
  1569. desc_bytes(ctx->sh_desc_digest),
  1570. DMA_TO_DEVICE);
  1571. if (ctx->sh_desc_finup_dma &&
  1572. !dma_mapping_error(ctx->jrdev, ctx->sh_desc_finup_dma))
  1573. dma_unmap_single(ctx->jrdev, ctx->sh_desc_finup_dma,
  1574. desc_bytes(ctx->sh_desc_finup), DMA_TO_DEVICE);
  1575. caam_jr_free(ctx->jrdev);
  1576. }
  1577. static void __exit caam_algapi_hash_exit(void)
  1578. {
  1579. struct caam_hash_alg *t_alg, *n;
  1580. if (!hash_list.next)
  1581. return;
  1582. list_for_each_entry_safe(t_alg, n, &hash_list, entry) {
  1583. crypto_unregister_ahash(&t_alg->ahash_alg);
  1584. list_del(&t_alg->entry);
  1585. kfree(t_alg);
  1586. }
  1587. }
  1588. static struct caam_hash_alg *
  1589. caam_hash_alloc(struct caam_hash_template *template,
  1590. bool keyed)
  1591. {
  1592. struct caam_hash_alg *t_alg;
  1593. struct ahash_alg *halg;
  1594. struct crypto_alg *alg;
  1595. t_alg = kzalloc(sizeof(struct caam_hash_alg), GFP_KERNEL);
  1596. if (!t_alg) {
  1597. pr_err("failed to allocate t_alg\n");
  1598. return ERR_PTR(-ENOMEM);
  1599. }
  1600. t_alg->ahash_alg = template->template_ahash;
  1601. halg = &t_alg->ahash_alg;
  1602. alg = &halg->halg.base;
  1603. if (keyed) {
  1604. snprintf(alg->cra_name, CRYPTO_MAX_ALG_NAME, "%s",
  1605. template->hmac_name);
  1606. snprintf(alg->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
  1607. template->hmac_driver_name);
  1608. } else {
  1609. snprintf(alg->cra_name, CRYPTO_MAX_ALG_NAME, "%s",
  1610. template->name);
  1611. snprintf(alg->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
  1612. template->driver_name);
  1613. }
  1614. alg->cra_module = THIS_MODULE;
  1615. alg->cra_init = caam_hash_cra_init;
  1616. alg->cra_exit = caam_hash_cra_exit;
  1617. alg->cra_ctxsize = sizeof(struct caam_hash_ctx);
  1618. alg->cra_priority = CAAM_CRA_PRIORITY;
  1619. alg->cra_blocksize = template->blocksize;
  1620. alg->cra_alignmask = 0;
  1621. alg->cra_flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_TYPE_AHASH;
  1622. alg->cra_type = &crypto_ahash_type;
  1623. t_alg->alg_type = template->alg_type;
  1624. t_alg->alg_op = template->alg_op;
  1625. return t_alg;
  1626. }
  1627. static int __init caam_algapi_hash_init(void)
  1628. {
  1629. struct device_node *dev_node;
  1630. struct platform_device *pdev;
  1631. struct device *ctrldev;
  1632. void *priv;
  1633. int i = 0, err = 0;
  1634. dev_node = of_find_compatible_node(NULL, NULL, "fsl,sec-v4.0");
  1635. if (!dev_node) {
  1636. dev_node = of_find_compatible_node(NULL, NULL, "fsl,sec4.0");
  1637. if (!dev_node)
  1638. return -ENODEV;
  1639. }
  1640. pdev = of_find_device_by_node(dev_node);
  1641. if (!pdev) {
  1642. of_node_put(dev_node);
  1643. return -ENODEV;
  1644. }
  1645. ctrldev = &pdev->dev;
  1646. priv = dev_get_drvdata(ctrldev);
  1647. of_node_put(dev_node);
  1648. /*
  1649. * If priv is NULL, it's probably because the caam driver wasn't
  1650. * properly initialized (e.g. RNG4 init failed). Thus, bail out here.
  1651. */
  1652. if (!priv)
  1653. return -ENODEV;
  1654. INIT_LIST_HEAD(&hash_list);
  1655. /* register crypto algorithms the device supports */
  1656. for (i = 0; i < ARRAY_SIZE(driver_hash); i++) {
  1657. /* TODO: check if h/w supports alg */
  1658. struct caam_hash_alg *t_alg;
  1659. /* register hmac version */
  1660. t_alg = caam_hash_alloc(&driver_hash[i], true);
  1661. if (IS_ERR(t_alg)) {
  1662. err = PTR_ERR(t_alg);
  1663. pr_warn("%s alg allocation failed\n",
  1664. driver_hash[i].driver_name);
  1665. continue;
  1666. }
  1667. err = crypto_register_ahash(&t_alg->ahash_alg);
  1668. if (err) {
  1669. pr_warn("%s alg registration failed\n",
  1670. t_alg->ahash_alg.halg.base.cra_driver_name);
  1671. kfree(t_alg);
  1672. } else
  1673. list_add_tail(&t_alg->entry, &hash_list);
  1674. /* register unkeyed version */
  1675. t_alg = caam_hash_alloc(&driver_hash[i], false);
  1676. if (IS_ERR(t_alg)) {
  1677. err = PTR_ERR(t_alg);
  1678. pr_warn("%s alg allocation failed\n",
  1679. driver_hash[i].driver_name);
  1680. continue;
  1681. }
  1682. err = crypto_register_ahash(&t_alg->ahash_alg);
  1683. if (err) {
  1684. pr_warn("%s alg registration failed\n",
  1685. t_alg->ahash_alg.halg.base.cra_driver_name);
  1686. kfree(t_alg);
  1687. } else
  1688. list_add_tail(&t_alg->entry, &hash_list);
  1689. }
  1690. return err;
  1691. }
  1692. module_init(caam_algapi_hash_init);
  1693. module_exit(caam_algapi_hash_exit);
  1694. MODULE_LICENSE("GPL");
  1695. MODULE_DESCRIPTION("FSL CAAM support for ahash functions of crypto API");
  1696. MODULE_AUTHOR("Freescale Semiconductor - NMG");