caamhash.c 56 KB

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