caamhash.c 52 KB

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