caamhash.c 57 KB

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