caamalg.c 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219
  1. /*
  2. * caam - Freescale FSL CAAM support for crypto API
  3. *
  4. * Copyright 2008-2011 Freescale Semiconductor, Inc.
  5. *
  6. * Based on talitos crypto API driver.
  7. *
  8. * relationship of job descriptors to shared descriptors (SteveC Dec 10 2008):
  9. *
  10. * --------------- ---------------
  11. * | JobDesc #1 |-------------------->| ShareDesc |
  12. * | *(packet 1) | | (PDB) |
  13. * --------------- |------------->| (hashKey) |
  14. * . | | (cipherKey) |
  15. * . | |-------->| (operation) |
  16. * --------------- | | ---------------
  17. * | JobDesc #2 |------| |
  18. * | *(packet 2) | |
  19. * --------------- |
  20. * . |
  21. * . |
  22. * --------------- |
  23. * | JobDesc #3 |------------
  24. * | *(packet 3) |
  25. * ---------------
  26. *
  27. * The SharedDesc never changes for a connection unless rekeyed, but
  28. * each packet will likely be in a different place. So all we need
  29. * to know to process the packet is where the input is, where the
  30. * output goes, and what context we want to process with. Context is
  31. * in the SharedDesc, packet references in the JobDesc.
  32. *
  33. * So, a job desc looks like:
  34. *
  35. * ---------------------
  36. * | Header |
  37. * | ShareDesc Pointer |
  38. * | SEQ_OUT_PTR |
  39. * | (output buffer) |
  40. * | (output length) |
  41. * | SEQ_IN_PTR |
  42. * | (input buffer) |
  43. * | (input length) |
  44. * ---------------------
  45. */
  46. #include "compat.h"
  47. #include "regs.h"
  48. #include "intern.h"
  49. #include "desc_constr.h"
  50. #include "jr.h"
  51. #include "error.h"
  52. #include "sg_sw_sec4.h"
  53. #include "key_gen.h"
  54. /*
  55. * crypto alg
  56. */
  57. #define CAAM_CRA_PRIORITY 3000
  58. /* max key is sum of AES_MAX_KEY_SIZE, max split key size */
  59. #define CAAM_MAX_KEY_SIZE (AES_MAX_KEY_SIZE + \
  60. SHA512_DIGEST_SIZE * 2)
  61. /* max IV is max of AES_BLOCK_SIZE, DES3_EDE_BLOCK_SIZE */
  62. #define CAAM_MAX_IV_LENGTH 16
  63. /* length of descriptors text */
  64. #define DESC_JOB_IO_LEN (CAAM_CMD_SZ * 5 + CAAM_PTR_SZ * 3)
  65. #define DESC_AEAD_BASE (4 * CAAM_CMD_SZ)
  66. #define DESC_AEAD_ENC_LEN (DESC_AEAD_BASE + 16 * CAAM_CMD_SZ)
  67. #define DESC_AEAD_DEC_LEN (DESC_AEAD_BASE + 21 * CAAM_CMD_SZ)
  68. #define DESC_AEAD_GIVENC_LEN (DESC_AEAD_ENC_LEN + 7 * CAAM_CMD_SZ)
  69. #define DESC_ABLKCIPHER_BASE (3 * CAAM_CMD_SZ)
  70. #define DESC_ABLKCIPHER_ENC_LEN (DESC_ABLKCIPHER_BASE + \
  71. 20 * CAAM_CMD_SZ)
  72. #define DESC_ABLKCIPHER_DEC_LEN (DESC_ABLKCIPHER_BASE + \
  73. 15 * CAAM_CMD_SZ)
  74. #define DESC_MAX_USED_BYTES (DESC_AEAD_GIVENC_LEN + \
  75. CAAM_MAX_KEY_SIZE)
  76. #define DESC_MAX_USED_LEN (DESC_MAX_USED_BYTES / CAAM_CMD_SZ)
  77. #ifdef DEBUG
  78. /* for print_hex_dumps with line references */
  79. #define xstr(s) str(s)
  80. #define str(s) #s
  81. #define debug(format, arg...) printk(format, arg)
  82. #else
  83. #define debug(format, arg...)
  84. #endif
  85. /* Set DK bit in class 1 operation if shared */
  86. static inline void append_dec_op1(u32 *desc, u32 type)
  87. {
  88. u32 *jump_cmd, *uncond_jump_cmd;
  89. jump_cmd = append_jump(desc, JUMP_TEST_ALL | JUMP_COND_SHRD);
  90. append_operation(desc, type | OP_ALG_AS_INITFINAL |
  91. OP_ALG_DECRYPT);
  92. uncond_jump_cmd = append_jump(desc, JUMP_TEST_ALL);
  93. set_jump_tgt_here(desc, jump_cmd);
  94. append_operation(desc, type | OP_ALG_AS_INITFINAL |
  95. OP_ALG_DECRYPT | OP_ALG_AAI_DK);
  96. set_jump_tgt_here(desc, uncond_jump_cmd);
  97. }
  98. /*
  99. * Wait for completion of class 1 key loading before allowing
  100. * error propagation
  101. */
  102. static inline void append_dec_shr_done(u32 *desc)
  103. {
  104. u32 *jump_cmd;
  105. jump_cmd = append_jump(desc, JUMP_CLASS_CLASS1 | JUMP_TEST_ALL);
  106. set_jump_tgt_here(desc, jump_cmd);
  107. append_cmd(desc, SET_OK_NO_PROP_ERRORS | CMD_LOAD);
  108. }
  109. /*
  110. * For aead functions, read payload and write payload,
  111. * both of which are specified in req->src and req->dst
  112. */
  113. static inline void aead_append_src_dst(u32 *desc, u32 msg_type)
  114. {
  115. append_seq_fifo_load(desc, 0, FIFOLD_CLASS_BOTH |
  116. KEY_VLF | msg_type | FIFOLD_TYPE_LASTBOTH);
  117. append_seq_fifo_store(desc, 0, FIFOST_TYPE_MESSAGE_DATA | KEY_VLF);
  118. }
  119. /*
  120. * For aead encrypt and decrypt, read iv for both classes
  121. */
  122. static inline void aead_append_ld_iv(u32 *desc, int ivsize)
  123. {
  124. append_cmd(desc, CMD_SEQ_LOAD | LDST_SRCDST_BYTE_CONTEXT |
  125. LDST_CLASS_1_CCB | ivsize);
  126. append_move(desc, MOVE_SRC_CLASS1CTX | MOVE_DEST_CLASS2INFIFO | ivsize);
  127. }
  128. /*
  129. * For ablkcipher encrypt and decrypt, read from req->src and
  130. * write to req->dst
  131. */
  132. static inline void ablkcipher_append_src_dst(u32 *desc)
  133. {
  134. append_math_add(desc, VARSEQOUTLEN, SEQINLEN, REG0, CAAM_CMD_SZ);
  135. append_math_add(desc, VARSEQINLEN, SEQINLEN, REG0, CAAM_CMD_SZ);
  136. append_seq_fifo_load(desc, 0, FIFOLD_CLASS_CLASS1 |
  137. KEY_VLF | FIFOLD_TYPE_MSG | FIFOLD_TYPE_LAST1);
  138. append_seq_fifo_store(desc, 0, FIFOST_TYPE_MESSAGE_DATA | KEY_VLF);
  139. }
  140. /*
  141. * If all data, including src (with assoc and iv) or dst (with iv only) are
  142. * contiguous
  143. */
  144. #define GIV_SRC_CONTIG 1
  145. #define GIV_DST_CONTIG (1 << 1)
  146. /*
  147. * per-session context
  148. */
  149. struct caam_ctx {
  150. struct device *jrdev;
  151. u32 sh_desc_enc[DESC_MAX_USED_LEN];
  152. u32 sh_desc_dec[DESC_MAX_USED_LEN];
  153. u32 sh_desc_givenc[DESC_MAX_USED_LEN];
  154. dma_addr_t sh_desc_enc_dma;
  155. dma_addr_t sh_desc_dec_dma;
  156. dma_addr_t sh_desc_givenc_dma;
  157. u32 class1_alg_type;
  158. u32 class2_alg_type;
  159. u32 alg_op;
  160. u8 key[CAAM_MAX_KEY_SIZE];
  161. dma_addr_t key_dma;
  162. unsigned int enckeylen;
  163. unsigned int split_key_len;
  164. unsigned int split_key_pad_len;
  165. unsigned int authsize;
  166. };
  167. static void append_key_aead(u32 *desc, struct caam_ctx *ctx,
  168. int keys_fit_inline)
  169. {
  170. if (keys_fit_inline) {
  171. append_key_as_imm(desc, ctx->key, ctx->split_key_pad_len,
  172. ctx->split_key_len, CLASS_2 |
  173. KEY_DEST_MDHA_SPLIT | KEY_ENC);
  174. append_key_as_imm(desc, (void *)ctx->key +
  175. ctx->split_key_pad_len, ctx->enckeylen,
  176. ctx->enckeylen, CLASS_1 | KEY_DEST_CLASS_REG);
  177. } else {
  178. append_key(desc, ctx->key_dma, ctx->split_key_len, CLASS_2 |
  179. KEY_DEST_MDHA_SPLIT | KEY_ENC);
  180. append_key(desc, ctx->key_dma + ctx->split_key_pad_len,
  181. ctx->enckeylen, CLASS_1 | KEY_DEST_CLASS_REG);
  182. }
  183. }
  184. static void init_sh_desc_key_aead(u32 *desc, struct caam_ctx *ctx,
  185. int keys_fit_inline)
  186. {
  187. u32 *key_jump_cmd;
  188. init_sh_desc(desc, HDR_SHARE_WAIT);
  189. /* Skip if already shared */
  190. key_jump_cmd = append_jump(desc, JUMP_JSL | JUMP_TEST_ALL |
  191. JUMP_COND_SHRD);
  192. append_key_aead(desc, ctx, keys_fit_inline);
  193. set_jump_tgt_here(desc, key_jump_cmd);
  194. /* Propagate errors from shared to job descriptor */
  195. append_cmd(desc, SET_OK_NO_PROP_ERRORS | CMD_LOAD);
  196. }
  197. static int aead_set_sh_desc(struct crypto_aead *aead)
  198. {
  199. struct aead_tfm *tfm = &aead->base.crt_aead;
  200. struct caam_ctx *ctx = crypto_aead_ctx(aead);
  201. struct device *jrdev = ctx->jrdev;
  202. bool keys_fit_inline = 0;
  203. u32 *key_jump_cmd, *jump_cmd;
  204. u32 geniv, moveiv;
  205. u32 *desc;
  206. if (!ctx->enckeylen || !ctx->authsize)
  207. return 0;
  208. /*
  209. * Job Descriptor and Shared Descriptors
  210. * must all fit into the 64-word Descriptor h/w Buffer
  211. */
  212. if (DESC_AEAD_ENC_LEN + DESC_JOB_IO_LEN +
  213. ctx->split_key_pad_len + ctx->enckeylen <=
  214. CAAM_DESC_BYTES_MAX)
  215. keys_fit_inline = 1;
  216. /* aead_encrypt shared descriptor */
  217. desc = ctx->sh_desc_enc;
  218. init_sh_desc_key_aead(desc, ctx, keys_fit_inline);
  219. /* Class 2 operation */
  220. append_operation(desc, ctx->class2_alg_type |
  221. OP_ALG_AS_INITFINAL | OP_ALG_ENCRYPT);
  222. /* cryptlen = seqoutlen - authsize */
  223. append_math_sub_imm_u32(desc, REG3, SEQOUTLEN, IMM, ctx->authsize);
  224. /* assoclen + cryptlen = seqinlen - ivsize */
  225. append_math_sub_imm_u32(desc, REG2, SEQINLEN, IMM, tfm->ivsize);
  226. /* assoclen + cryptlen = (assoclen + cryptlen) - cryptlen */
  227. append_math_sub(desc, VARSEQINLEN, REG2, REG3, CAAM_CMD_SZ);
  228. /* read assoc before reading payload */
  229. append_seq_fifo_load(desc, 0, FIFOLD_CLASS_CLASS2 | FIFOLD_TYPE_MSG |
  230. KEY_VLF);
  231. aead_append_ld_iv(desc, tfm->ivsize);
  232. /* Class 1 operation */
  233. append_operation(desc, ctx->class1_alg_type |
  234. OP_ALG_AS_INITFINAL | OP_ALG_ENCRYPT);
  235. /* Read and write cryptlen bytes */
  236. append_math_add(desc, VARSEQINLEN, ZERO, REG3, CAAM_CMD_SZ);
  237. append_math_add(desc, VARSEQOUTLEN, ZERO, REG3, CAAM_CMD_SZ);
  238. aead_append_src_dst(desc, FIFOLD_TYPE_MSG1OUT2);
  239. /* Write ICV */
  240. append_seq_store(desc, ctx->authsize, LDST_CLASS_2_CCB |
  241. LDST_SRCDST_BYTE_CONTEXT);
  242. ctx->sh_desc_enc_dma = dma_map_single(jrdev, desc,
  243. desc_bytes(desc),
  244. DMA_TO_DEVICE);
  245. if (dma_mapping_error(jrdev, ctx->sh_desc_enc_dma)) {
  246. dev_err(jrdev, "unable to map shared descriptor\n");
  247. return -ENOMEM;
  248. }
  249. #ifdef DEBUG
  250. print_hex_dump(KERN_ERR, "aead enc shdesc@"xstr(__LINE__)": ",
  251. DUMP_PREFIX_ADDRESS, 16, 4, desc,
  252. desc_bytes(desc), 1);
  253. #endif
  254. /*
  255. * Job Descriptor and Shared Descriptors
  256. * must all fit into the 64-word Descriptor h/w Buffer
  257. */
  258. if (DESC_AEAD_DEC_LEN + DESC_JOB_IO_LEN +
  259. ctx->split_key_pad_len + ctx->enckeylen <=
  260. CAAM_DESC_BYTES_MAX)
  261. keys_fit_inline = 1;
  262. desc = ctx->sh_desc_dec;
  263. /* aead_decrypt shared descriptor */
  264. init_sh_desc(desc, HDR_SHARE_WAIT);
  265. /* Skip if already shared */
  266. key_jump_cmd = append_jump(desc, JUMP_JSL | JUMP_TEST_ALL |
  267. JUMP_COND_SHRD);
  268. append_key_aead(desc, ctx, keys_fit_inline);
  269. /* Only propagate error immediately if shared */
  270. jump_cmd = append_jump(desc, JUMP_TEST_ALL);
  271. set_jump_tgt_here(desc, key_jump_cmd);
  272. append_cmd(desc, SET_OK_NO_PROP_ERRORS | CMD_LOAD);
  273. set_jump_tgt_here(desc, jump_cmd);
  274. /* Class 2 operation */
  275. append_operation(desc, ctx->class2_alg_type |
  276. OP_ALG_AS_INITFINAL | OP_ALG_DECRYPT | OP_ALG_ICV_ON);
  277. /* assoclen + cryptlen = seqinlen - ivsize */
  278. append_math_sub_imm_u32(desc, REG3, SEQINLEN, IMM,
  279. ctx->authsize + tfm->ivsize)
  280. /* assoclen = (assoclen + cryptlen) - cryptlen */
  281. append_math_sub(desc, REG2, SEQOUTLEN, REG0, CAAM_CMD_SZ);
  282. append_math_sub(desc, VARSEQINLEN, REG3, REG2, CAAM_CMD_SZ);
  283. /* read assoc before reading payload */
  284. append_seq_fifo_load(desc, 0, FIFOLD_CLASS_CLASS2 | FIFOLD_TYPE_MSG |
  285. KEY_VLF);
  286. aead_append_ld_iv(desc, tfm->ivsize);
  287. append_dec_op1(desc, ctx->class1_alg_type);
  288. /* Read and write cryptlen bytes */
  289. append_math_add(desc, VARSEQINLEN, ZERO, REG2, CAAM_CMD_SZ);
  290. append_math_add(desc, VARSEQOUTLEN, ZERO, REG2, CAAM_CMD_SZ);
  291. aead_append_src_dst(desc, FIFOLD_TYPE_MSG);
  292. /* Load ICV */
  293. append_seq_fifo_load(desc, ctx->authsize, FIFOLD_CLASS_CLASS2 |
  294. FIFOLD_TYPE_LAST2 | FIFOLD_TYPE_ICV);
  295. append_dec_shr_done(desc);
  296. ctx->sh_desc_dec_dma = dma_map_single(jrdev, desc,
  297. desc_bytes(desc),
  298. DMA_TO_DEVICE);
  299. if (dma_mapping_error(jrdev, ctx->sh_desc_dec_dma)) {
  300. dev_err(jrdev, "unable to map shared descriptor\n");
  301. return -ENOMEM;
  302. }
  303. #ifdef DEBUG
  304. print_hex_dump(KERN_ERR, "aead dec shdesc@"xstr(__LINE__)": ",
  305. DUMP_PREFIX_ADDRESS, 16, 4, desc,
  306. desc_bytes(desc), 1);
  307. #endif
  308. /*
  309. * Job Descriptor and Shared Descriptors
  310. * must all fit into the 64-word Descriptor h/w Buffer
  311. */
  312. if (DESC_AEAD_GIVENC_LEN + DESC_JOB_IO_LEN +
  313. ctx->split_key_pad_len + ctx->enckeylen <=
  314. CAAM_DESC_BYTES_MAX)
  315. keys_fit_inline = 1;
  316. /* aead_givencrypt shared descriptor */
  317. desc = ctx->sh_desc_givenc;
  318. init_sh_desc_key_aead(desc, ctx, keys_fit_inline);
  319. /* Generate IV */
  320. geniv = NFIFOENTRY_STYPE_PAD | NFIFOENTRY_DEST_DECO |
  321. NFIFOENTRY_DTYPE_MSG | NFIFOENTRY_LC1 |
  322. NFIFOENTRY_PTYPE_RND | (tfm->ivsize << NFIFOENTRY_DLEN_SHIFT);
  323. append_load_imm_u32(desc, geniv, LDST_CLASS_IND_CCB |
  324. LDST_SRCDST_WORD_INFO_FIFO | LDST_IMM);
  325. append_cmd(desc, CMD_LOAD | DISABLE_AUTO_INFO_FIFO);
  326. append_move(desc, MOVE_SRC_INFIFO |
  327. MOVE_DEST_CLASS1CTX | (tfm->ivsize << MOVE_LEN_SHIFT));
  328. append_cmd(desc, CMD_LOAD | ENABLE_AUTO_INFO_FIFO);
  329. /* Copy IV to class 1 context */
  330. append_move(desc, MOVE_SRC_CLASS1CTX |
  331. MOVE_DEST_OUTFIFO | (tfm->ivsize << MOVE_LEN_SHIFT));
  332. /* Return to encryption */
  333. append_operation(desc, ctx->class2_alg_type |
  334. OP_ALG_AS_INITFINAL | OP_ALG_ENCRYPT);
  335. /* ivsize + cryptlen = seqoutlen - authsize */
  336. append_math_sub_imm_u32(desc, REG3, SEQOUTLEN, IMM, ctx->authsize);
  337. /* assoclen = seqinlen - (ivsize + cryptlen) */
  338. append_math_sub(desc, VARSEQINLEN, SEQINLEN, REG3, CAAM_CMD_SZ);
  339. /* read assoc before reading payload */
  340. append_seq_fifo_load(desc, 0, FIFOLD_CLASS_CLASS2 | FIFOLD_TYPE_MSG |
  341. KEY_VLF);
  342. /* Copy iv from class 1 ctx to class 2 fifo*/
  343. moveiv = NFIFOENTRY_STYPE_OFIFO | NFIFOENTRY_DEST_CLASS2 |
  344. NFIFOENTRY_DTYPE_MSG | (tfm->ivsize << NFIFOENTRY_DLEN_SHIFT);
  345. append_load_imm_u32(desc, moveiv, LDST_CLASS_IND_CCB |
  346. LDST_SRCDST_WORD_INFO_FIFO | LDST_IMM);
  347. append_load_imm_u32(desc, tfm->ivsize, LDST_CLASS_2_CCB |
  348. LDST_SRCDST_WORD_DATASZ_REG | LDST_IMM);
  349. /* Class 1 operation */
  350. append_operation(desc, ctx->class1_alg_type |
  351. OP_ALG_AS_INITFINAL | OP_ALG_ENCRYPT);
  352. /* Will write ivsize + cryptlen */
  353. append_math_add(desc, VARSEQOUTLEN, SEQINLEN, REG0, CAAM_CMD_SZ);
  354. /* Not need to reload iv */
  355. append_seq_fifo_load(desc, tfm->ivsize,
  356. FIFOLD_CLASS_SKIP);
  357. /* Will read cryptlen */
  358. append_math_add(desc, VARSEQINLEN, SEQINLEN, REG0, CAAM_CMD_SZ);
  359. aead_append_src_dst(desc, FIFOLD_TYPE_MSG1OUT2);
  360. /* Write ICV */
  361. append_seq_store(desc, ctx->authsize, LDST_CLASS_2_CCB |
  362. LDST_SRCDST_BYTE_CONTEXT);
  363. ctx->sh_desc_givenc_dma = dma_map_single(jrdev, desc,
  364. desc_bytes(desc),
  365. DMA_TO_DEVICE);
  366. if (dma_mapping_error(jrdev, ctx->sh_desc_givenc_dma)) {
  367. dev_err(jrdev, "unable to map shared descriptor\n");
  368. return -ENOMEM;
  369. }
  370. #ifdef DEBUG
  371. print_hex_dump(KERN_ERR, "aead givenc shdesc@"xstr(__LINE__)": ",
  372. DUMP_PREFIX_ADDRESS, 16, 4, desc,
  373. desc_bytes(desc), 1);
  374. #endif
  375. return 0;
  376. }
  377. static int aead_setauthsize(struct crypto_aead *authenc,
  378. unsigned int authsize)
  379. {
  380. struct caam_ctx *ctx = crypto_aead_ctx(authenc);
  381. ctx->authsize = authsize;
  382. aead_set_sh_desc(authenc);
  383. return 0;
  384. }
  385. static u32 gen_split_aead_key(struct caam_ctx *ctx, const u8 *key_in,
  386. u32 authkeylen)
  387. {
  388. return gen_split_key(ctx->jrdev, ctx->key, ctx->split_key_len,
  389. ctx->split_key_pad_len, key_in, authkeylen,
  390. ctx->alg_op);
  391. }
  392. static int aead_setkey(struct crypto_aead *aead,
  393. const u8 *key, unsigned int keylen)
  394. {
  395. /* Sizes for MDHA pads (*not* keys): MD5, SHA1, 224, 256, 384, 512 */
  396. static const u8 mdpadlen[] = { 16, 20, 32, 32, 64, 64 };
  397. struct caam_ctx *ctx = crypto_aead_ctx(aead);
  398. struct device *jrdev = ctx->jrdev;
  399. struct rtattr *rta = (void *)key;
  400. struct crypto_authenc_key_param *param;
  401. unsigned int authkeylen;
  402. unsigned int enckeylen;
  403. int ret = 0;
  404. param = RTA_DATA(rta);
  405. enckeylen = be32_to_cpu(param->enckeylen);
  406. key += RTA_ALIGN(rta->rta_len);
  407. keylen -= RTA_ALIGN(rta->rta_len);
  408. if (keylen < enckeylen)
  409. goto badkey;
  410. authkeylen = keylen - enckeylen;
  411. if (keylen > CAAM_MAX_KEY_SIZE)
  412. goto badkey;
  413. /* Pick class 2 key length from algorithm submask */
  414. ctx->split_key_len = mdpadlen[(ctx->alg_op & OP_ALG_ALGSEL_SUBMASK) >>
  415. OP_ALG_ALGSEL_SHIFT] * 2;
  416. ctx->split_key_pad_len = ALIGN(ctx->split_key_len, 16);
  417. #ifdef DEBUG
  418. printk(KERN_ERR "keylen %d enckeylen %d authkeylen %d\n",
  419. keylen, enckeylen, authkeylen);
  420. printk(KERN_ERR "split_key_len %d split_key_pad_len %d\n",
  421. ctx->split_key_len, ctx->split_key_pad_len);
  422. print_hex_dump(KERN_ERR, "key in @"xstr(__LINE__)": ",
  423. DUMP_PREFIX_ADDRESS, 16, 4, key, keylen, 1);
  424. #endif
  425. ret = gen_split_aead_key(ctx, key, authkeylen);
  426. if (ret) {
  427. goto badkey;
  428. }
  429. /* postpend encryption key to auth split key */
  430. memcpy(ctx->key + ctx->split_key_pad_len, key + authkeylen, enckeylen);
  431. ctx->key_dma = dma_map_single(jrdev, ctx->key, ctx->split_key_pad_len +
  432. enckeylen, DMA_TO_DEVICE);
  433. if (dma_mapping_error(jrdev, ctx->key_dma)) {
  434. dev_err(jrdev, "unable to map key i/o memory\n");
  435. return -ENOMEM;
  436. }
  437. #ifdef DEBUG
  438. print_hex_dump(KERN_ERR, "ctx.key@"xstr(__LINE__)": ",
  439. DUMP_PREFIX_ADDRESS, 16, 4, ctx->key,
  440. ctx->split_key_pad_len + enckeylen, 1);
  441. #endif
  442. ctx->enckeylen = enckeylen;
  443. ret = aead_set_sh_desc(aead);
  444. if (ret) {
  445. dma_unmap_single(jrdev, ctx->key_dma, ctx->split_key_pad_len +
  446. enckeylen, DMA_TO_DEVICE);
  447. }
  448. return ret;
  449. badkey:
  450. crypto_aead_set_flags(aead, CRYPTO_TFM_RES_BAD_KEY_LEN);
  451. return -EINVAL;
  452. }
  453. static int ablkcipher_setkey(struct crypto_ablkcipher *ablkcipher,
  454. const u8 *key, unsigned int keylen)
  455. {
  456. struct caam_ctx *ctx = crypto_ablkcipher_ctx(ablkcipher);
  457. struct ablkcipher_tfm *tfm = &ablkcipher->base.crt_ablkcipher;
  458. struct device *jrdev = ctx->jrdev;
  459. int ret = 0;
  460. u32 *key_jump_cmd, *jump_cmd;
  461. u32 *desc;
  462. #ifdef DEBUG
  463. print_hex_dump(KERN_ERR, "key in @"xstr(__LINE__)": ",
  464. DUMP_PREFIX_ADDRESS, 16, 4, key, keylen, 1);
  465. #endif
  466. memcpy(ctx->key, key, keylen);
  467. ctx->key_dma = dma_map_single(jrdev, ctx->key, keylen,
  468. DMA_TO_DEVICE);
  469. if (dma_mapping_error(jrdev, ctx->key_dma)) {
  470. dev_err(jrdev, "unable to map key i/o memory\n");
  471. return -ENOMEM;
  472. }
  473. ctx->enckeylen = keylen;
  474. /* ablkcipher_encrypt shared descriptor */
  475. desc = ctx->sh_desc_enc;
  476. init_sh_desc(desc, HDR_SHARE_WAIT);
  477. /* Skip if already shared */
  478. key_jump_cmd = append_jump(desc, JUMP_JSL | JUMP_TEST_ALL |
  479. JUMP_COND_SHRD);
  480. /* Load class1 key only */
  481. append_key_as_imm(desc, (void *)ctx->key, ctx->enckeylen,
  482. ctx->enckeylen, CLASS_1 |
  483. KEY_DEST_CLASS_REG);
  484. set_jump_tgt_here(desc, key_jump_cmd);
  485. /* Propagate errors from shared to job descriptor */
  486. append_cmd(desc, SET_OK_NO_PROP_ERRORS | CMD_LOAD);
  487. /* Load iv */
  488. append_cmd(desc, CMD_SEQ_LOAD | LDST_SRCDST_BYTE_CONTEXT |
  489. LDST_CLASS_1_CCB | tfm->ivsize);
  490. /* Load operation */
  491. append_operation(desc, ctx->class1_alg_type |
  492. OP_ALG_AS_INITFINAL | OP_ALG_ENCRYPT);
  493. /* Perform operation */
  494. ablkcipher_append_src_dst(desc);
  495. ctx->sh_desc_enc_dma = dma_map_single(jrdev, desc,
  496. desc_bytes(desc),
  497. DMA_TO_DEVICE);
  498. if (dma_mapping_error(jrdev, ctx->sh_desc_enc_dma)) {
  499. dev_err(jrdev, "unable to map shared descriptor\n");
  500. return -ENOMEM;
  501. }
  502. #ifdef DEBUG
  503. print_hex_dump(KERN_ERR, "ablkcipher enc shdesc@"xstr(__LINE__)": ",
  504. DUMP_PREFIX_ADDRESS, 16, 4, desc,
  505. desc_bytes(desc), 1);
  506. #endif
  507. /* ablkcipher_decrypt shared descriptor */
  508. desc = ctx->sh_desc_dec;
  509. init_sh_desc(desc, HDR_SHARE_WAIT);
  510. /* Skip if already shared */
  511. key_jump_cmd = append_jump(desc, JUMP_JSL | JUMP_TEST_ALL |
  512. JUMP_COND_SHRD);
  513. /* Load class1 key only */
  514. append_key_as_imm(desc, (void *)ctx->key, ctx->enckeylen,
  515. ctx->enckeylen, CLASS_1 |
  516. KEY_DEST_CLASS_REG);
  517. /* For aead, only propagate error immediately if shared */
  518. jump_cmd = append_jump(desc, JUMP_TEST_ALL);
  519. set_jump_tgt_here(desc, key_jump_cmd);
  520. append_cmd(desc, SET_OK_NO_PROP_ERRORS | CMD_LOAD);
  521. set_jump_tgt_here(desc, jump_cmd);
  522. /* load IV */
  523. append_cmd(desc, CMD_SEQ_LOAD | LDST_SRCDST_BYTE_CONTEXT |
  524. LDST_CLASS_1_CCB | tfm->ivsize);
  525. /* Choose operation */
  526. append_dec_op1(desc, ctx->class1_alg_type);
  527. /* Perform operation */
  528. ablkcipher_append_src_dst(desc);
  529. /* Wait for key to load before allowing propagating error */
  530. append_dec_shr_done(desc);
  531. ctx->sh_desc_dec_dma = dma_map_single(jrdev, desc,
  532. desc_bytes(desc),
  533. DMA_TO_DEVICE);
  534. if (dma_mapping_error(jrdev, ctx->sh_desc_enc_dma)) {
  535. dev_err(jrdev, "unable to map shared descriptor\n");
  536. return -ENOMEM;
  537. }
  538. #ifdef DEBUG
  539. print_hex_dump(KERN_ERR, "ablkcipher dec shdesc@"xstr(__LINE__)": ",
  540. DUMP_PREFIX_ADDRESS, 16, 4, desc,
  541. desc_bytes(desc), 1);
  542. #endif
  543. return ret;
  544. }
  545. /*
  546. * aead_edesc - s/w-extended aead descriptor
  547. * @assoc_nents: number of segments in associated data (SPI+Seq) scatterlist
  548. * @src_nents: number of segments in input scatterlist
  549. * @dst_nents: number of segments in output scatterlist
  550. * @iv_dma: dma address of iv for checking continuity and link table
  551. * @desc: h/w descriptor (variable length; must not exceed MAX_CAAM_DESCSIZE)
  552. * @sec4_sg_bytes: length of dma mapped sec4_sg space
  553. * @sec4_sg_dma: bus physical mapped address of h/w link table
  554. * @hw_desc: the h/w job descriptor followed by any referenced link tables
  555. */
  556. struct aead_edesc {
  557. int assoc_nents;
  558. int src_nents;
  559. int dst_nents;
  560. dma_addr_t iv_dma;
  561. int sec4_sg_bytes;
  562. dma_addr_t sec4_sg_dma;
  563. struct sec4_sg_entry *sec4_sg;
  564. u32 hw_desc[0];
  565. };
  566. /*
  567. * ablkcipher_edesc - s/w-extended ablkcipher descriptor
  568. * @src_nents: number of segments in input scatterlist
  569. * @dst_nents: number of segments in output scatterlist
  570. * @iv_dma: dma address of iv for checking continuity and link table
  571. * @desc: h/w descriptor (variable length; must not exceed MAX_CAAM_DESCSIZE)
  572. * @sec4_sg_bytes: length of dma mapped sec4_sg space
  573. * @sec4_sg_dma: bus physical mapped address of h/w link table
  574. * @hw_desc: the h/w job descriptor followed by any referenced link tables
  575. */
  576. struct ablkcipher_edesc {
  577. int src_nents;
  578. int dst_nents;
  579. dma_addr_t iv_dma;
  580. int sec4_sg_bytes;
  581. dma_addr_t sec4_sg_dma;
  582. struct sec4_sg_entry *sec4_sg;
  583. u32 hw_desc[0];
  584. };
  585. static void caam_unmap(struct device *dev, struct scatterlist *src,
  586. struct scatterlist *dst, int src_nents, int dst_nents,
  587. dma_addr_t iv_dma, int ivsize, dma_addr_t sec4_sg_dma,
  588. int sec4_sg_bytes)
  589. {
  590. if (unlikely(dst != src)) {
  591. dma_unmap_sg(dev, src, src_nents, DMA_TO_DEVICE);
  592. dma_unmap_sg(dev, dst, dst_nents, DMA_FROM_DEVICE);
  593. } else {
  594. dma_unmap_sg(dev, src, src_nents, DMA_BIDIRECTIONAL);
  595. }
  596. if (iv_dma)
  597. dma_unmap_single(dev, iv_dma, ivsize, DMA_TO_DEVICE);
  598. if (sec4_sg_bytes)
  599. dma_unmap_single(dev, sec4_sg_dma, sec4_sg_bytes,
  600. DMA_TO_DEVICE);
  601. }
  602. static void aead_unmap(struct device *dev,
  603. struct aead_edesc *edesc,
  604. struct aead_request *req)
  605. {
  606. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  607. int ivsize = crypto_aead_ivsize(aead);
  608. dma_unmap_sg(dev, req->assoc, edesc->assoc_nents, DMA_TO_DEVICE);
  609. caam_unmap(dev, req->src, req->dst,
  610. edesc->src_nents, edesc->dst_nents,
  611. edesc->iv_dma, ivsize, edesc->sec4_sg_dma,
  612. edesc->sec4_sg_bytes);
  613. }
  614. static void ablkcipher_unmap(struct device *dev,
  615. struct ablkcipher_edesc *edesc,
  616. struct ablkcipher_request *req)
  617. {
  618. struct crypto_ablkcipher *ablkcipher = crypto_ablkcipher_reqtfm(req);
  619. int ivsize = crypto_ablkcipher_ivsize(ablkcipher);
  620. caam_unmap(dev, req->src, req->dst,
  621. edesc->src_nents, edesc->dst_nents,
  622. edesc->iv_dma, ivsize, edesc->sec4_sg_dma,
  623. edesc->sec4_sg_bytes);
  624. }
  625. static void aead_encrypt_done(struct device *jrdev, u32 *desc, u32 err,
  626. void *context)
  627. {
  628. struct aead_request *req = context;
  629. struct aead_edesc *edesc;
  630. #ifdef DEBUG
  631. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  632. struct caam_ctx *ctx = crypto_aead_ctx(aead);
  633. int ivsize = crypto_aead_ivsize(aead);
  634. dev_err(jrdev, "%s %d: err 0x%x\n", __func__, __LINE__, err);
  635. #endif
  636. edesc = (struct aead_edesc *)((char *)desc -
  637. offsetof(struct aead_edesc, hw_desc));
  638. if (err) {
  639. char tmp[CAAM_ERROR_STR_MAX];
  640. dev_err(jrdev, "%08x: %s\n", err, caam_jr_strstatus(tmp, err));
  641. }
  642. aead_unmap(jrdev, edesc, req);
  643. #ifdef DEBUG
  644. print_hex_dump(KERN_ERR, "assoc @"xstr(__LINE__)": ",
  645. DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(req->assoc),
  646. req->assoclen , 1);
  647. print_hex_dump(KERN_ERR, "dstiv @"xstr(__LINE__)": ",
  648. DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(req->src) - ivsize,
  649. edesc->src_nents ? 100 : ivsize, 1);
  650. print_hex_dump(KERN_ERR, "dst @"xstr(__LINE__)": ",
  651. DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(req->src),
  652. edesc->src_nents ? 100 : req->cryptlen +
  653. ctx->authsize + 4, 1);
  654. #endif
  655. kfree(edesc);
  656. aead_request_complete(req, err);
  657. }
  658. static void aead_decrypt_done(struct device *jrdev, u32 *desc, u32 err,
  659. void *context)
  660. {
  661. struct aead_request *req = context;
  662. struct aead_edesc *edesc;
  663. #ifdef DEBUG
  664. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  665. struct caam_ctx *ctx = crypto_aead_ctx(aead);
  666. int ivsize = crypto_aead_ivsize(aead);
  667. dev_err(jrdev, "%s %d: err 0x%x\n", __func__, __LINE__, err);
  668. #endif
  669. edesc = (struct aead_edesc *)((char *)desc -
  670. offsetof(struct aead_edesc, hw_desc));
  671. #ifdef DEBUG
  672. print_hex_dump(KERN_ERR, "dstiv @"xstr(__LINE__)": ",
  673. DUMP_PREFIX_ADDRESS, 16, 4, req->iv,
  674. ivsize, 1);
  675. print_hex_dump(KERN_ERR, "dst @"xstr(__LINE__)": ",
  676. DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(req->dst),
  677. req->cryptlen, 1);
  678. #endif
  679. if (err) {
  680. char tmp[CAAM_ERROR_STR_MAX];
  681. dev_err(jrdev, "%08x: %s\n", err, caam_jr_strstatus(tmp, err));
  682. }
  683. aead_unmap(jrdev, edesc, req);
  684. /*
  685. * verify hw auth check passed else return -EBADMSG
  686. */
  687. if ((err & JRSTA_CCBERR_ERRID_MASK) == JRSTA_CCBERR_ERRID_ICVCHK)
  688. err = -EBADMSG;
  689. #ifdef DEBUG
  690. print_hex_dump(KERN_ERR, "iphdrout@"xstr(__LINE__)": ",
  691. DUMP_PREFIX_ADDRESS, 16, 4,
  692. ((char *)sg_virt(req->assoc) - sizeof(struct iphdr)),
  693. sizeof(struct iphdr) + req->assoclen +
  694. ((req->cryptlen > 1500) ? 1500 : req->cryptlen) +
  695. ctx->authsize + 36, 1);
  696. if (!err && edesc->sec4_sg_bytes) {
  697. struct scatterlist *sg = sg_last(req->src, edesc->src_nents);
  698. print_hex_dump(KERN_ERR, "sglastout@"xstr(__LINE__)": ",
  699. DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(sg),
  700. sg->length + ctx->authsize + 16, 1);
  701. }
  702. #endif
  703. kfree(edesc);
  704. aead_request_complete(req, err);
  705. }
  706. static void ablkcipher_encrypt_done(struct device *jrdev, u32 *desc, u32 err,
  707. void *context)
  708. {
  709. struct ablkcipher_request *req = context;
  710. struct ablkcipher_edesc *edesc;
  711. #ifdef DEBUG
  712. struct crypto_ablkcipher *ablkcipher = crypto_ablkcipher_reqtfm(req);
  713. int ivsize = crypto_ablkcipher_ivsize(ablkcipher);
  714. dev_err(jrdev, "%s %d: err 0x%x\n", __func__, __LINE__, err);
  715. #endif
  716. edesc = (struct ablkcipher_edesc *)((char *)desc -
  717. offsetof(struct ablkcipher_edesc, hw_desc));
  718. if (err) {
  719. char tmp[CAAM_ERROR_STR_MAX];
  720. dev_err(jrdev, "%08x: %s\n", err, caam_jr_strstatus(tmp, err));
  721. }
  722. #ifdef DEBUG
  723. print_hex_dump(KERN_ERR, "dstiv @"xstr(__LINE__)": ",
  724. DUMP_PREFIX_ADDRESS, 16, 4, req->info,
  725. edesc->src_nents > 1 ? 100 : ivsize, 1);
  726. print_hex_dump(KERN_ERR, "dst @"xstr(__LINE__)": ",
  727. DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(req->src),
  728. edesc->dst_nents > 1 ? 100 : req->nbytes, 1);
  729. #endif
  730. ablkcipher_unmap(jrdev, edesc, req);
  731. kfree(edesc);
  732. ablkcipher_request_complete(req, err);
  733. }
  734. static void ablkcipher_decrypt_done(struct device *jrdev, u32 *desc, u32 err,
  735. void *context)
  736. {
  737. struct ablkcipher_request *req = context;
  738. struct ablkcipher_edesc *edesc;
  739. #ifdef DEBUG
  740. struct crypto_ablkcipher *ablkcipher = crypto_ablkcipher_reqtfm(req);
  741. int ivsize = crypto_ablkcipher_ivsize(ablkcipher);
  742. dev_err(jrdev, "%s %d: err 0x%x\n", __func__, __LINE__, err);
  743. #endif
  744. edesc = (struct ablkcipher_edesc *)((char *)desc -
  745. offsetof(struct ablkcipher_edesc, hw_desc));
  746. if (err) {
  747. char tmp[CAAM_ERROR_STR_MAX];
  748. dev_err(jrdev, "%08x: %s\n", err, caam_jr_strstatus(tmp, err));
  749. }
  750. #ifdef DEBUG
  751. print_hex_dump(KERN_ERR, "dstiv @"xstr(__LINE__)": ",
  752. DUMP_PREFIX_ADDRESS, 16, 4, req->info,
  753. ivsize, 1);
  754. print_hex_dump(KERN_ERR, "dst @"xstr(__LINE__)": ",
  755. DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(req->src),
  756. edesc->dst_nents > 1 ? 100 : req->nbytes, 1);
  757. #endif
  758. ablkcipher_unmap(jrdev, edesc, req);
  759. kfree(edesc);
  760. ablkcipher_request_complete(req, err);
  761. }
  762. /*
  763. * Fill in aead job descriptor
  764. */
  765. static void init_aead_job(u32 *sh_desc, dma_addr_t ptr,
  766. struct aead_edesc *edesc,
  767. struct aead_request *req,
  768. bool all_contig, bool encrypt)
  769. {
  770. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  771. struct caam_ctx *ctx = crypto_aead_ctx(aead);
  772. int ivsize = crypto_aead_ivsize(aead);
  773. int authsize = ctx->authsize;
  774. u32 *desc = edesc->hw_desc;
  775. u32 out_options = 0, in_options;
  776. dma_addr_t dst_dma, src_dma;
  777. int len, sec4_sg_index = 0;
  778. #ifdef DEBUG
  779. debug("assoclen %d cryptlen %d authsize %d\n",
  780. req->assoclen, req->cryptlen, authsize);
  781. print_hex_dump(KERN_ERR, "assoc @"xstr(__LINE__)": ",
  782. DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(req->assoc),
  783. req->assoclen , 1);
  784. print_hex_dump(KERN_ERR, "presciv@"xstr(__LINE__)": ",
  785. DUMP_PREFIX_ADDRESS, 16, 4, req->iv,
  786. edesc->src_nents ? 100 : ivsize, 1);
  787. print_hex_dump(KERN_ERR, "src @"xstr(__LINE__)": ",
  788. DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(req->src),
  789. edesc->src_nents ? 100 : req->cryptlen, 1);
  790. print_hex_dump(KERN_ERR, "shrdesc@"xstr(__LINE__)": ",
  791. DUMP_PREFIX_ADDRESS, 16, 4, sh_desc,
  792. desc_bytes(sh_desc), 1);
  793. #endif
  794. len = desc_len(sh_desc);
  795. init_job_desc_shared(desc, ptr, len, HDR_SHARE_DEFER | HDR_REVERSE);
  796. if (all_contig) {
  797. src_dma = sg_dma_address(req->assoc);
  798. in_options = 0;
  799. } else {
  800. src_dma = edesc->sec4_sg_dma;
  801. sec4_sg_index += (edesc->assoc_nents ? : 1) + 1 +
  802. (edesc->src_nents ? : 1);
  803. in_options = LDST_SGF;
  804. }
  805. if (encrypt)
  806. append_seq_in_ptr(desc, src_dma, req->assoclen + ivsize +
  807. req->cryptlen - authsize, in_options);
  808. else
  809. append_seq_in_ptr(desc, src_dma, req->assoclen + ivsize +
  810. req->cryptlen, in_options);
  811. if (likely(req->src == req->dst)) {
  812. if (all_contig) {
  813. dst_dma = sg_dma_address(req->src);
  814. } else {
  815. dst_dma = src_dma + sizeof(struct sec4_sg_entry) *
  816. ((edesc->assoc_nents ? : 1) + 1);
  817. out_options = LDST_SGF;
  818. }
  819. } else {
  820. if (!edesc->dst_nents) {
  821. dst_dma = sg_dma_address(req->dst);
  822. } else {
  823. dst_dma = edesc->sec4_sg_dma +
  824. sec4_sg_index *
  825. sizeof(struct sec4_sg_entry);
  826. out_options = LDST_SGF;
  827. }
  828. }
  829. if (encrypt)
  830. append_seq_out_ptr(desc, dst_dma, req->cryptlen, out_options);
  831. else
  832. append_seq_out_ptr(desc, dst_dma, req->cryptlen - authsize,
  833. out_options);
  834. }
  835. /*
  836. * Fill in aead givencrypt job descriptor
  837. */
  838. static void init_aead_giv_job(u32 *sh_desc, dma_addr_t ptr,
  839. struct aead_edesc *edesc,
  840. struct aead_request *req,
  841. int contig)
  842. {
  843. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  844. struct caam_ctx *ctx = crypto_aead_ctx(aead);
  845. int ivsize = crypto_aead_ivsize(aead);
  846. int authsize = ctx->authsize;
  847. u32 *desc = edesc->hw_desc;
  848. u32 out_options = 0, in_options;
  849. dma_addr_t dst_dma, src_dma;
  850. int len, sec4_sg_index = 0;
  851. #ifdef DEBUG
  852. debug("assoclen %d cryptlen %d authsize %d\n",
  853. req->assoclen, req->cryptlen, authsize);
  854. print_hex_dump(KERN_ERR, "assoc @"xstr(__LINE__)": ",
  855. DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(req->assoc),
  856. req->assoclen , 1);
  857. print_hex_dump(KERN_ERR, "presciv@"xstr(__LINE__)": ",
  858. DUMP_PREFIX_ADDRESS, 16, 4, req->iv, ivsize, 1);
  859. print_hex_dump(KERN_ERR, "src @"xstr(__LINE__)": ",
  860. DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(req->src),
  861. edesc->src_nents > 1 ? 100 : req->cryptlen, 1);
  862. print_hex_dump(KERN_ERR, "shrdesc@"xstr(__LINE__)": ",
  863. DUMP_PREFIX_ADDRESS, 16, 4, sh_desc,
  864. desc_bytes(sh_desc), 1);
  865. #endif
  866. len = desc_len(sh_desc);
  867. init_job_desc_shared(desc, ptr, len, HDR_SHARE_DEFER | HDR_REVERSE);
  868. if (contig & GIV_SRC_CONTIG) {
  869. src_dma = sg_dma_address(req->assoc);
  870. in_options = 0;
  871. } else {
  872. src_dma = edesc->sec4_sg_dma;
  873. sec4_sg_index += edesc->assoc_nents + 1 + edesc->src_nents;
  874. in_options = LDST_SGF;
  875. }
  876. append_seq_in_ptr(desc, src_dma, req->assoclen + ivsize +
  877. req->cryptlen - authsize, in_options);
  878. if (contig & GIV_DST_CONTIG) {
  879. dst_dma = edesc->iv_dma;
  880. } else {
  881. if (likely(req->src == req->dst)) {
  882. dst_dma = src_dma + sizeof(struct sec4_sg_entry) *
  883. edesc->assoc_nents;
  884. out_options = LDST_SGF;
  885. } else {
  886. dst_dma = edesc->sec4_sg_dma +
  887. sec4_sg_index *
  888. sizeof(struct sec4_sg_entry);
  889. out_options = LDST_SGF;
  890. }
  891. }
  892. append_seq_out_ptr(desc, dst_dma, ivsize + req->cryptlen, out_options);
  893. }
  894. /*
  895. * Fill in ablkcipher job descriptor
  896. */
  897. static void init_ablkcipher_job(u32 *sh_desc, dma_addr_t ptr,
  898. struct ablkcipher_edesc *edesc,
  899. struct ablkcipher_request *req,
  900. bool iv_contig)
  901. {
  902. struct crypto_ablkcipher *ablkcipher = crypto_ablkcipher_reqtfm(req);
  903. int ivsize = crypto_ablkcipher_ivsize(ablkcipher);
  904. u32 *desc = edesc->hw_desc;
  905. u32 out_options = 0, in_options;
  906. dma_addr_t dst_dma, src_dma;
  907. int len, sec4_sg_index = 0;
  908. #ifdef DEBUG
  909. print_hex_dump(KERN_ERR, "presciv@"xstr(__LINE__)": ",
  910. DUMP_PREFIX_ADDRESS, 16, 4, req->info,
  911. ivsize, 1);
  912. print_hex_dump(KERN_ERR, "src @"xstr(__LINE__)": ",
  913. DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(req->src),
  914. edesc->src_nents ? 100 : req->nbytes, 1);
  915. #endif
  916. len = desc_len(sh_desc);
  917. init_job_desc_shared(desc, ptr, len, HDR_SHARE_DEFER | HDR_REVERSE);
  918. if (iv_contig) {
  919. src_dma = edesc->iv_dma;
  920. in_options = 0;
  921. } else {
  922. src_dma = edesc->sec4_sg_dma;
  923. sec4_sg_index += (iv_contig ? 0 : 1) + edesc->src_nents;
  924. in_options = LDST_SGF;
  925. }
  926. append_seq_in_ptr(desc, src_dma, req->nbytes + ivsize, in_options);
  927. if (likely(req->src == req->dst)) {
  928. if (!edesc->src_nents && iv_contig) {
  929. dst_dma = sg_dma_address(req->src);
  930. } else {
  931. dst_dma = edesc->sec4_sg_dma +
  932. sizeof(struct sec4_sg_entry);
  933. out_options = LDST_SGF;
  934. }
  935. } else {
  936. if (!edesc->dst_nents) {
  937. dst_dma = sg_dma_address(req->dst);
  938. } else {
  939. dst_dma = edesc->sec4_sg_dma +
  940. sec4_sg_index * sizeof(struct sec4_sg_entry);
  941. out_options = LDST_SGF;
  942. }
  943. }
  944. append_seq_out_ptr(desc, dst_dma, req->nbytes, out_options);
  945. }
  946. /*
  947. * allocate and map the aead extended descriptor
  948. */
  949. static struct aead_edesc *aead_edesc_alloc(struct aead_request *req,
  950. int desc_bytes, bool *all_contig_ptr)
  951. {
  952. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  953. struct caam_ctx *ctx = crypto_aead_ctx(aead);
  954. struct device *jrdev = ctx->jrdev;
  955. gfp_t flags = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG |
  956. CRYPTO_TFM_REQ_MAY_SLEEP)) ? GFP_KERNEL : GFP_ATOMIC;
  957. int assoc_nents, src_nents, dst_nents = 0;
  958. struct aead_edesc *edesc;
  959. dma_addr_t iv_dma = 0;
  960. int sgc;
  961. bool all_contig = true;
  962. int ivsize = crypto_aead_ivsize(aead);
  963. int sec4_sg_index, sec4_sg_len = 0, sec4_sg_bytes;
  964. assoc_nents = sg_count(req->assoc, req->assoclen);
  965. src_nents = sg_count(req->src, req->cryptlen);
  966. if (unlikely(req->dst != req->src))
  967. dst_nents = sg_count(req->dst, req->cryptlen);
  968. sgc = dma_map_sg(jrdev, req->assoc, assoc_nents ? : 1,
  969. DMA_BIDIRECTIONAL);
  970. if (likely(req->src == req->dst)) {
  971. sgc = dma_map_sg(jrdev, req->src, src_nents ? : 1,
  972. DMA_BIDIRECTIONAL);
  973. } else {
  974. sgc = dma_map_sg(jrdev, req->src, src_nents ? : 1,
  975. DMA_TO_DEVICE);
  976. sgc = dma_map_sg(jrdev, req->dst, dst_nents ? : 1,
  977. DMA_FROM_DEVICE);
  978. }
  979. /* Check if data are contiguous */
  980. iv_dma = dma_map_single(jrdev, req->iv, ivsize, DMA_TO_DEVICE);
  981. if (assoc_nents || sg_dma_address(req->assoc) + req->assoclen !=
  982. iv_dma || src_nents || iv_dma + ivsize !=
  983. sg_dma_address(req->src)) {
  984. all_contig = false;
  985. assoc_nents = assoc_nents ? : 1;
  986. src_nents = src_nents ? : 1;
  987. sec4_sg_len = assoc_nents + 1 + src_nents;
  988. }
  989. sec4_sg_len += dst_nents;
  990. sec4_sg_bytes = sec4_sg_len * sizeof(struct sec4_sg_entry);
  991. /* allocate space for base edesc and hw desc commands, link tables */
  992. edesc = kmalloc(sizeof(struct aead_edesc) + desc_bytes +
  993. sec4_sg_bytes, GFP_DMA | flags);
  994. if (!edesc) {
  995. dev_err(jrdev, "could not allocate extended descriptor\n");
  996. return ERR_PTR(-ENOMEM);
  997. }
  998. edesc->assoc_nents = assoc_nents;
  999. edesc->src_nents = src_nents;
  1000. edesc->dst_nents = dst_nents;
  1001. edesc->iv_dma = iv_dma;
  1002. edesc->sec4_sg_bytes = sec4_sg_bytes;
  1003. edesc->sec4_sg = (void *)edesc + sizeof(struct aead_edesc) +
  1004. desc_bytes;
  1005. edesc->sec4_sg_dma = dma_map_single(jrdev, edesc->sec4_sg,
  1006. sec4_sg_bytes, DMA_TO_DEVICE);
  1007. *all_contig_ptr = all_contig;
  1008. sec4_sg_index = 0;
  1009. if (!all_contig) {
  1010. sg_to_sec4_sg(req->assoc,
  1011. (assoc_nents ? : 1),
  1012. edesc->sec4_sg +
  1013. sec4_sg_index, 0);
  1014. sec4_sg_index += assoc_nents ? : 1;
  1015. dma_to_sec4_sg_one(edesc->sec4_sg + sec4_sg_index,
  1016. iv_dma, ivsize, 0);
  1017. sec4_sg_index += 1;
  1018. sg_to_sec4_sg_last(req->src,
  1019. (src_nents ? : 1),
  1020. edesc->sec4_sg +
  1021. sec4_sg_index, 0);
  1022. sec4_sg_index += src_nents ? : 1;
  1023. }
  1024. if (dst_nents) {
  1025. sg_to_sec4_sg_last(req->dst, dst_nents,
  1026. edesc->sec4_sg + sec4_sg_index, 0);
  1027. }
  1028. return edesc;
  1029. }
  1030. static int aead_encrypt(struct aead_request *req)
  1031. {
  1032. struct aead_edesc *edesc;
  1033. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  1034. struct caam_ctx *ctx = crypto_aead_ctx(aead);
  1035. struct device *jrdev = ctx->jrdev;
  1036. bool all_contig;
  1037. u32 *desc;
  1038. int ret = 0;
  1039. req->cryptlen += ctx->authsize;
  1040. /* allocate extended descriptor */
  1041. edesc = aead_edesc_alloc(req, DESC_JOB_IO_LEN *
  1042. CAAM_CMD_SZ, &all_contig);
  1043. if (IS_ERR(edesc))
  1044. return PTR_ERR(edesc);
  1045. /* Create and submit job descriptor */
  1046. init_aead_job(ctx->sh_desc_enc, ctx->sh_desc_enc_dma, edesc, req,
  1047. all_contig, true);
  1048. #ifdef DEBUG
  1049. print_hex_dump(KERN_ERR, "aead jobdesc@"xstr(__LINE__)": ",
  1050. DUMP_PREFIX_ADDRESS, 16, 4, edesc->hw_desc,
  1051. desc_bytes(edesc->hw_desc), 1);
  1052. #endif
  1053. desc = edesc->hw_desc;
  1054. ret = caam_jr_enqueue(jrdev, desc, aead_encrypt_done, req);
  1055. if (!ret) {
  1056. ret = -EINPROGRESS;
  1057. } else {
  1058. aead_unmap(jrdev, edesc, req);
  1059. kfree(edesc);
  1060. }
  1061. return ret;
  1062. }
  1063. static int aead_decrypt(struct aead_request *req)
  1064. {
  1065. struct aead_edesc *edesc;
  1066. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  1067. struct caam_ctx *ctx = crypto_aead_ctx(aead);
  1068. struct device *jrdev = ctx->jrdev;
  1069. bool all_contig;
  1070. u32 *desc;
  1071. int ret = 0;
  1072. /* allocate extended descriptor */
  1073. edesc = aead_edesc_alloc(req, DESC_JOB_IO_LEN *
  1074. CAAM_CMD_SZ, &all_contig);
  1075. if (IS_ERR(edesc))
  1076. return PTR_ERR(edesc);
  1077. #ifdef DEBUG
  1078. print_hex_dump(KERN_ERR, "dec src@"xstr(__LINE__)": ",
  1079. DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(req->src),
  1080. req->cryptlen, 1);
  1081. #endif
  1082. /* Create and submit job descriptor*/
  1083. init_aead_job(ctx->sh_desc_dec,
  1084. ctx->sh_desc_dec_dma, edesc, req, all_contig, false);
  1085. #ifdef DEBUG
  1086. print_hex_dump(KERN_ERR, "aead jobdesc@"xstr(__LINE__)": ",
  1087. DUMP_PREFIX_ADDRESS, 16, 4, edesc->hw_desc,
  1088. desc_bytes(edesc->hw_desc), 1);
  1089. #endif
  1090. desc = edesc->hw_desc;
  1091. ret = caam_jr_enqueue(jrdev, desc, aead_decrypt_done, req);
  1092. if (!ret) {
  1093. ret = -EINPROGRESS;
  1094. } else {
  1095. aead_unmap(jrdev, edesc, req);
  1096. kfree(edesc);
  1097. }
  1098. return ret;
  1099. }
  1100. /*
  1101. * allocate and map the aead extended descriptor for aead givencrypt
  1102. */
  1103. static struct aead_edesc *aead_giv_edesc_alloc(struct aead_givcrypt_request
  1104. *greq, int desc_bytes,
  1105. u32 *contig_ptr)
  1106. {
  1107. struct aead_request *req = &greq->areq;
  1108. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  1109. struct caam_ctx *ctx = crypto_aead_ctx(aead);
  1110. struct device *jrdev = ctx->jrdev;
  1111. gfp_t flags = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG |
  1112. CRYPTO_TFM_REQ_MAY_SLEEP)) ? GFP_KERNEL : GFP_ATOMIC;
  1113. int assoc_nents, src_nents, dst_nents = 0;
  1114. struct aead_edesc *edesc;
  1115. dma_addr_t iv_dma = 0;
  1116. int sgc;
  1117. u32 contig = GIV_SRC_CONTIG | GIV_DST_CONTIG;
  1118. int ivsize = crypto_aead_ivsize(aead);
  1119. int sec4_sg_index, sec4_sg_len = 0, sec4_sg_bytes;
  1120. assoc_nents = sg_count(req->assoc, req->assoclen);
  1121. src_nents = sg_count(req->src, req->cryptlen);
  1122. if (unlikely(req->dst != req->src))
  1123. dst_nents = sg_count(req->dst, req->cryptlen);
  1124. sgc = dma_map_sg(jrdev, req->assoc, assoc_nents ? : 1,
  1125. DMA_BIDIRECTIONAL);
  1126. if (likely(req->src == req->dst)) {
  1127. sgc = dma_map_sg(jrdev, req->src, src_nents ? : 1,
  1128. DMA_BIDIRECTIONAL);
  1129. } else {
  1130. sgc = dma_map_sg(jrdev, req->src, src_nents ? : 1,
  1131. DMA_TO_DEVICE);
  1132. sgc = dma_map_sg(jrdev, req->dst, dst_nents ? : 1,
  1133. DMA_FROM_DEVICE);
  1134. }
  1135. /* Check if data are contiguous */
  1136. iv_dma = dma_map_single(jrdev, greq->giv, ivsize, DMA_TO_DEVICE);
  1137. if (assoc_nents || sg_dma_address(req->assoc) + req->assoclen !=
  1138. iv_dma || src_nents || iv_dma + ivsize != sg_dma_address(req->src))
  1139. contig &= ~GIV_SRC_CONTIG;
  1140. if (dst_nents || iv_dma + ivsize != sg_dma_address(req->dst))
  1141. contig &= ~GIV_DST_CONTIG;
  1142. if (unlikely(req->src != req->dst)) {
  1143. dst_nents = dst_nents ? : 1;
  1144. sec4_sg_len += 1;
  1145. }
  1146. if (!(contig & GIV_SRC_CONTIG)) {
  1147. assoc_nents = assoc_nents ? : 1;
  1148. src_nents = src_nents ? : 1;
  1149. sec4_sg_len += assoc_nents + 1 + src_nents;
  1150. if (likely(req->src == req->dst))
  1151. contig &= ~GIV_DST_CONTIG;
  1152. }
  1153. sec4_sg_len += dst_nents;
  1154. sec4_sg_bytes = sec4_sg_len * sizeof(struct sec4_sg_entry);
  1155. /* allocate space for base edesc and hw desc commands, link tables */
  1156. edesc = kmalloc(sizeof(struct aead_edesc) + desc_bytes +
  1157. sec4_sg_bytes, GFP_DMA | flags);
  1158. if (!edesc) {
  1159. dev_err(jrdev, "could not allocate extended descriptor\n");
  1160. return ERR_PTR(-ENOMEM);
  1161. }
  1162. edesc->assoc_nents = assoc_nents;
  1163. edesc->src_nents = src_nents;
  1164. edesc->dst_nents = dst_nents;
  1165. edesc->iv_dma = iv_dma;
  1166. edesc->sec4_sg_bytes = sec4_sg_bytes;
  1167. edesc->sec4_sg = (void *)edesc + sizeof(struct aead_edesc) +
  1168. desc_bytes;
  1169. edesc->sec4_sg_dma = dma_map_single(jrdev, edesc->sec4_sg,
  1170. sec4_sg_bytes, DMA_TO_DEVICE);
  1171. *contig_ptr = contig;
  1172. sec4_sg_index = 0;
  1173. if (!(contig & GIV_SRC_CONTIG)) {
  1174. sg_to_sec4_sg(req->assoc, assoc_nents,
  1175. edesc->sec4_sg +
  1176. sec4_sg_index, 0);
  1177. sec4_sg_index += assoc_nents;
  1178. dma_to_sec4_sg_one(edesc->sec4_sg + sec4_sg_index,
  1179. iv_dma, ivsize, 0);
  1180. sec4_sg_index += 1;
  1181. sg_to_sec4_sg_last(req->src, src_nents,
  1182. edesc->sec4_sg +
  1183. sec4_sg_index, 0);
  1184. sec4_sg_index += src_nents;
  1185. }
  1186. if (unlikely(req->src != req->dst && !(contig & GIV_DST_CONTIG))) {
  1187. dma_to_sec4_sg_one(edesc->sec4_sg + sec4_sg_index,
  1188. iv_dma, ivsize, 0);
  1189. sec4_sg_index += 1;
  1190. sg_to_sec4_sg_last(req->dst, dst_nents,
  1191. edesc->sec4_sg + sec4_sg_index, 0);
  1192. }
  1193. return edesc;
  1194. }
  1195. static int aead_givencrypt(struct aead_givcrypt_request *areq)
  1196. {
  1197. struct aead_request *req = &areq->areq;
  1198. struct aead_edesc *edesc;
  1199. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  1200. struct caam_ctx *ctx = crypto_aead_ctx(aead);
  1201. struct device *jrdev = ctx->jrdev;
  1202. u32 contig;
  1203. u32 *desc;
  1204. int ret = 0;
  1205. req->cryptlen += ctx->authsize;
  1206. /* allocate extended descriptor */
  1207. edesc = aead_giv_edesc_alloc(areq, DESC_JOB_IO_LEN *
  1208. CAAM_CMD_SZ, &contig);
  1209. if (IS_ERR(edesc))
  1210. return PTR_ERR(edesc);
  1211. #ifdef DEBUG
  1212. print_hex_dump(KERN_ERR, "giv src@"xstr(__LINE__)": ",
  1213. DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(req->src),
  1214. req->cryptlen, 1);
  1215. #endif
  1216. /* Create and submit job descriptor*/
  1217. init_aead_giv_job(ctx->sh_desc_givenc,
  1218. ctx->sh_desc_givenc_dma, edesc, req, contig);
  1219. #ifdef DEBUG
  1220. print_hex_dump(KERN_ERR, "aead jobdesc@"xstr(__LINE__)": ",
  1221. DUMP_PREFIX_ADDRESS, 16, 4, edesc->hw_desc,
  1222. desc_bytes(edesc->hw_desc), 1);
  1223. #endif
  1224. desc = edesc->hw_desc;
  1225. ret = caam_jr_enqueue(jrdev, desc, aead_encrypt_done, req);
  1226. if (!ret) {
  1227. ret = -EINPROGRESS;
  1228. } else {
  1229. aead_unmap(jrdev, edesc, req);
  1230. kfree(edesc);
  1231. }
  1232. return ret;
  1233. }
  1234. /*
  1235. * allocate and map the ablkcipher extended descriptor for ablkcipher
  1236. */
  1237. static struct ablkcipher_edesc *ablkcipher_edesc_alloc(struct ablkcipher_request
  1238. *req, int desc_bytes,
  1239. bool *iv_contig_out)
  1240. {
  1241. struct crypto_ablkcipher *ablkcipher = crypto_ablkcipher_reqtfm(req);
  1242. struct caam_ctx *ctx = crypto_ablkcipher_ctx(ablkcipher);
  1243. struct device *jrdev = ctx->jrdev;
  1244. gfp_t flags = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG |
  1245. CRYPTO_TFM_REQ_MAY_SLEEP)) ?
  1246. GFP_KERNEL : GFP_ATOMIC;
  1247. int src_nents, dst_nents = 0, sec4_sg_bytes;
  1248. struct ablkcipher_edesc *edesc;
  1249. dma_addr_t iv_dma = 0;
  1250. bool iv_contig = false;
  1251. int sgc;
  1252. int ivsize = crypto_ablkcipher_ivsize(ablkcipher);
  1253. int sec4_sg_index;
  1254. src_nents = sg_count(req->src, req->nbytes);
  1255. if (unlikely(req->dst != req->src))
  1256. dst_nents = sg_count(req->dst, req->nbytes);
  1257. if (likely(req->src == req->dst)) {
  1258. sgc = dma_map_sg(jrdev, req->src, src_nents ? : 1,
  1259. DMA_BIDIRECTIONAL);
  1260. } else {
  1261. sgc = dma_map_sg(jrdev, req->src, src_nents ? : 1,
  1262. DMA_TO_DEVICE);
  1263. sgc = dma_map_sg(jrdev, req->dst, dst_nents ? : 1,
  1264. DMA_FROM_DEVICE);
  1265. }
  1266. /*
  1267. * Check if iv can be contiguous with source and destination.
  1268. * If so, include it. If not, create scatterlist.
  1269. */
  1270. iv_dma = dma_map_single(jrdev, req->info, ivsize, DMA_TO_DEVICE);
  1271. if (!src_nents && iv_dma + ivsize == sg_dma_address(req->src))
  1272. iv_contig = true;
  1273. else
  1274. src_nents = src_nents ? : 1;
  1275. sec4_sg_bytes = ((iv_contig ? 0 : 1) + src_nents + dst_nents) *
  1276. sizeof(struct sec4_sg_entry);
  1277. /* allocate space for base edesc and hw desc commands, link tables */
  1278. edesc = kmalloc(sizeof(struct ablkcipher_edesc) + desc_bytes +
  1279. sec4_sg_bytes, GFP_DMA | flags);
  1280. if (!edesc) {
  1281. dev_err(jrdev, "could not allocate extended descriptor\n");
  1282. return ERR_PTR(-ENOMEM);
  1283. }
  1284. edesc->src_nents = src_nents;
  1285. edesc->dst_nents = dst_nents;
  1286. edesc->sec4_sg_bytes = sec4_sg_bytes;
  1287. edesc->sec4_sg = (void *)edesc + sizeof(struct ablkcipher_edesc) +
  1288. desc_bytes;
  1289. sec4_sg_index = 0;
  1290. if (!iv_contig) {
  1291. dma_to_sec4_sg_one(edesc->sec4_sg, iv_dma, ivsize, 0);
  1292. sg_to_sec4_sg_last(req->src, src_nents,
  1293. edesc->sec4_sg + 1, 0);
  1294. sec4_sg_index += 1 + src_nents;
  1295. }
  1296. if (unlikely(dst_nents)) {
  1297. sg_to_sec4_sg_last(req->dst, dst_nents,
  1298. edesc->sec4_sg + sec4_sg_index, 0);
  1299. }
  1300. edesc->sec4_sg_dma = dma_map_single(jrdev, edesc->sec4_sg,
  1301. sec4_sg_bytes, DMA_TO_DEVICE);
  1302. edesc->iv_dma = iv_dma;
  1303. #ifdef DEBUG
  1304. print_hex_dump(KERN_ERR, "ablkcipher sec4_sg@"xstr(__LINE__)": ",
  1305. DUMP_PREFIX_ADDRESS, 16, 4, edesc->sec4_sg,
  1306. sec4_sg_bytes, 1);
  1307. #endif
  1308. *iv_contig_out = iv_contig;
  1309. return edesc;
  1310. }
  1311. static int ablkcipher_encrypt(struct ablkcipher_request *req)
  1312. {
  1313. struct ablkcipher_edesc *edesc;
  1314. struct crypto_ablkcipher *ablkcipher = crypto_ablkcipher_reqtfm(req);
  1315. struct caam_ctx *ctx = crypto_ablkcipher_ctx(ablkcipher);
  1316. struct device *jrdev = ctx->jrdev;
  1317. bool iv_contig;
  1318. u32 *desc;
  1319. int ret = 0;
  1320. /* allocate extended descriptor */
  1321. edesc = ablkcipher_edesc_alloc(req, DESC_JOB_IO_LEN *
  1322. CAAM_CMD_SZ, &iv_contig);
  1323. if (IS_ERR(edesc))
  1324. return PTR_ERR(edesc);
  1325. /* Create and submit job descriptor*/
  1326. init_ablkcipher_job(ctx->sh_desc_enc,
  1327. ctx->sh_desc_enc_dma, edesc, req, iv_contig);
  1328. #ifdef DEBUG
  1329. print_hex_dump(KERN_ERR, "ablkcipher jobdesc@"xstr(__LINE__)": ",
  1330. DUMP_PREFIX_ADDRESS, 16, 4, edesc->hw_desc,
  1331. desc_bytes(edesc->hw_desc), 1);
  1332. #endif
  1333. desc = edesc->hw_desc;
  1334. ret = caam_jr_enqueue(jrdev, desc, ablkcipher_encrypt_done, req);
  1335. if (!ret) {
  1336. ret = -EINPROGRESS;
  1337. } else {
  1338. ablkcipher_unmap(jrdev, edesc, req);
  1339. kfree(edesc);
  1340. }
  1341. return ret;
  1342. }
  1343. static int ablkcipher_decrypt(struct ablkcipher_request *req)
  1344. {
  1345. struct ablkcipher_edesc *edesc;
  1346. struct crypto_ablkcipher *ablkcipher = crypto_ablkcipher_reqtfm(req);
  1347. struct caam_ctx *ctx = crypto_ablkcipher_ctx(ablkcipher);
  1348. struct device *jrdev = ctx->jrdev;
  1349. bool iv_contig;
  1350. u32 *desc;
  1351. int ret = 0;
  1352. /* allocate extended descriptor */
  1353. edesc = ablkcipher_edesc_alloc(req, DESC_JOB_IO_LEN *
  1354. CAAM_CMD_SZ, &iv_contig);
  1355. if (IS_ERR(edesc))
  1356. return PTR_ERR(edesc);
  1357. /* Create and submit job descriptor*/
  1358. init_ablkcipher_job(ctx->sh_desc_dec,
  1359. ctx->sh_desc_dec_dma, edesc, req, iv_contig);
  1360. desc = edesc->hw_desc;
  1361. #ifdef DEBUG
  1362. print_hex_dump(KERN_ERR, "ablkcipher jobdesc@"xstr(__LINE__)": ",
  1363. DUMP_PREFIX_ADDRESS, 16, 4, edesc->hw_desc,
  1364. desc_bytes(edesc->hw_desc), 1);
  1365. #endif
  1366. ret = caam_jr_enqueue(jrdev, desc, ablkcipher_decrypt_done, req);
  1367. if (!ret) {
  1368. ret = -EINPROGRESS;
  1369. } else {
  1370. ablkcipher_unmap(jrdev, edesc, req);
  1371. kfree(edesc);
  1372. }
  1373. return ret;
  1374. }
  1375. #define template_aead template_u.aead
  1376. #define template_ablkcipher template_u.ablkcipher
  1377. struct caam_alg_template {
  1378. char name[CRYPTO_MAX_ALG_NAME];
  1379. char driver_name[CRYPTO_MAX_ALG_NAME];
  1380. unsigned int blocksize;
  1381. u32 type;
  1382. union {
  1383. struct ablkcipher_alg ablkcipher;
  1384. struct aead_alg aead;
  1385. struct blkcipher_alg blkcipher;
  1386. struct cipher_alg cipher;
  1387. struct compress_alg compress;
  1388. struct rng_alg rng;
  1389. } template_u;
  1390. u32 class1_alg_type;
  1391. u32 class2_alg_type;
  1392. u32 alg_op;
  1393. };
  1394. static struct caam_alg_template driver_algs[] = {
  1395. /* single-pass ipsec_esp descriptor */
  1396. {
  1397. .name = "authenc(hmac(md5),cbc(aes))",
  1398. .driver_name = "authenc-hmac-md5-cbc-aes-caam",
  1399. .blocksize = AES_BLOCK_SIZE,
  1400. .type = CRYPTO_ALG_TYPE_AEAD,
  1401. .template_aead = {
  1402. .setkey = aead_setkey,
  1403. .setauthsize = aead_setauthsize,
  1404. .encrypt = aead_encrypt,
  1405. .decrypt = aead_decrypt,
  1406. .givencrypt = aead_givencrypt,
  1407. .geniv = "<built-in>",
  1408. .ivsize = AES_BLOCK_SIZE,
  1409. .maxauthsize = MD5_DIGEST_SIZE,
  1410. },
  1411. .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
  1412. .class2_alg_type = OP_ALG_ALGSEL_MD5 | OP_ALG_AAI_HMAC_PRECOMP,
  1413. .alg_op = OP_ALG_ALGSEL_MD5 | OP_ALG_AAI_HMAC,
  1414. },
  1415. {
  1416. .name = "authenc(hmac(sha1),cbc(aes))",
  1417. .driver_name = "authenc-hmac-sha1-cbc-aes-caam",
  1418. .blocksize = AES_BLOCK_SIZE,
  1419. .type = CRYPTO_ALG_TYPE_AEAD,
  1420. .template_aead = {
  1421. .setkey = aead_setkey,
  1422. .setauthsize = aead_setauthsize,
  1423. .encrypt = aead_encrypt,
  1424. .decrypt = aead_decrypt,
  1425. .givencrypt = aead_givencrypt,
  1426. .geniv = "<built-in>",
  1427. .ivsize = AES_BLOCK_SIZE,
  1428. .maxauthsize = SHA1_DIGEST_SIZE,
  1429. },
  1430. .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
  1431. .class2_alg_type = OP_ALG_ALGSEL_SHA1 | OP_ALG_AAI_HMAC_PRECOMP,
  1432. .alg_op = OP_ALG_ALGSEL_SHA1 | OP_ALG_AAI_HMAC,
  1433. },
  1434. {
  1435. .name = "authenc(hmac(sha224),cbc(aes))",
  1436. .driver_name = "authenc-hmac-sha224-cbc-aes-caam",
  1437. .blocksize = AES_BLOCK_SIZE,
  1438. .template_aead = {
  1439. .setkey = aead_setkey,
  1440. .setauthsize = aead_setauthsize,
  1441. .encrypt = aead_encrypt,
  1442. .decrypt = aead_decrypt,
  1443. .givencrypt = aead_givencrypt,
  1444. .geniv = "<built-in>",
  1445. .ivsize = AES_BLOCK_SIZE,
  1446. .maxauthsize = SHA224_DIGEST_SIZE,
  1447. },
  1448. .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
  1449. .class2_alg_type = OP_ALG_ALGSEL_SHA224 |
  1450. OP_ALG_AAI_HMAC_PRECOMP,
  1451. .alg_op = OP_ALG_ALGSEL_SHA224 | OP_ALG_AAI_HMAC,
  1452. },
  1453. {
  1454. .name = "authenc(hmac(sha256),cbc(aes))",
  1455. .driver_name = "authenc-hmac-sha256-cbc-aes-caam",
  1456. .blocksize = AES_BLOCK_SIZE,
  1457. .type = CRYPTO_ALG_TYPE_AEAD,
  1458. .template_aead = {
  1459. .setkey = aead_setkey,
  1460. .setauthsize = aead_setauthsize,
  1461. .encrypt = aead_encrypt,
  1462. .decrypt = aead_decrypt,
  1463. .givencrypt = aead_givencrypt,
  1464. .geniv = "<built-in>",
  1465. .ivsize = AES_BLOCK_SIZE,
  1466. .maxauthsize = SHA256_DIGEST_SIZE,
  1467. },
  1468. .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
  1469. .class2_alg_type = OP_ALG_ALGSEL_SHA256 |
  1470. OP_ALG_AAI_HMAC_PRECOMP,
  1471. .alg_op = OP_ALG_ALGSEL_SHA256 | OP_ALG_AAI_HMAC,
  1472. },
  1473. {
  1474. .name = "authenc(hmac(sha384),cbc(aes))",
  1475. .driver_name = "authenc-hmac-sha384-cbc-aes-caam",
  1476. .blocksize = AES_BLOCK_SIZE,
  1477. .template_aead = {
  1478. .setkey = aead_setkey,
  1479. .setauthsize = aead_setauthsize,
  1480. .encrypt = aead_encrypt,
  1481. .decrypt = aead_decrypt,
  1482. .givencrypt = aead_givencrypt,
  1483. .geniv = "<built-in>",
  1484. .ivsize = AES_BLOCK_SIZE,
  1485. .maxauthsize = SHA384_DIGEST_SIZE,
  1486. },
  1487. .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
  1488. .class2_alg_type = OP_ALG_ALGSEL_SHA384 |
  1489. OP_ALG_AAI_HMAC_PRECOMP,
  1490. .alg_op = OP_ALG_ALGSEL_SHA384 | OP_ALG_AAI_HMAC,
  1491. },
  1492. {
  1493. .name = "authenc(hmac(sha512),cbc(aes))",
  1494. .driver_name = "authenc-hmac-sha512-cbc-aes-caam",
  1495. .blocksize = AES_BLOCK_SIZE,
  1496. .type = CRYPTO_ALG_TYPE_AEAD,
  1497. .template_aead = {
  1498. .setkey = aead_setkey,
  1499. .setauthsize = aead_setauthsize,
  1500. .encrypt = aead_encrypt,
  1501. .decrypt = aead_decrypt,
  1502. .givencrypt = aead_givencrypt,
  1503. .geniv = "<built-in>",
  1504. .ivsize = AES_BLOCK_SIZE,
  1505. .maxauthsize = SHA512_DIGEST_SIZE,
  1506. },
  1507. .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
  1508. .class2_alg_type = OP_ALG_ALGSEL_SHA512 |
  1509. OP_ALG_AAI_HMAC_PRECOMP,
  1510. .alg_op = OP_ALG_ALGSEL_SHA512 | OP_ALG_AAI_HMAC,
  1511. },
  1512. {
  1513. .name = "authenc(hmac(md5),cbc(des3_ede))",
  1514. .driver_name = "authenc-hmac-md5-cbc-des3_ede-caam",
  1515. .blocksize = DES3_EDE_BLOCK_SIZE,
  1516. .type = CRYPTO_ALG_TYPE_AEAD,
  1517. .template_aead = {
  1518. .setkey = aead_setkey,
  1519. .setauthsize = aead_setauthsize,
  1520. .encrypt = aead_encrypt,
  1521. .decrypt = aead_decrypt,
  1522. .givencrypt = aead_givencrypt,
  1523. .geniv = "<built-in>",
  1524. .ivsize = DES3_EDE_BLOCK_SIZE,
  1525. .maxauthsize = MD5_DIGEST_SIZE,
  1526. },
  1527. .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
  1528. .class2_alg_type = OP_ALG_ALGSEL_MD5 | OP_ALG_AAI_HMAC_PRECOMP,
  1529. .alg_op = OP_ALG_ALGSEL_MD5 | OP_ALG_AAI_HMAC,
  1530. },
  1531. {
  1532. .name = "authenc(hmac(sha1),cbc(des3_ede))",
  1533. .driver_name = "authenc-hmac-sha1-cbc-des3_ede-caam",
  1534. .blocksize = DES3_EDE_BLOCK_SIZE,
  1535. .type = CRYPTO_ALG_TYPE_AEAD,
  1536. .template_aead = {
  1537. .setkey = aead_setkey,
  1538. .setauthsize = aead_setauthsize,
  1539. .encrypt = aead_encrypt,
  1540. .decrypt = aead_decrypt,
  1541. .givencrypt = aead_givencrypt,
  1542. .geniv = "<built-in>",
  1543. .ivsize = DES3_EDE_BLOCK_SIZE,
  1544. .maxauthsize = SHA1_DIGEST_SIZE,
  1545. },
  1546. .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
  1547. .class2_alg_type = OP_ALG_ALGSEL_SHA1 | OP_ALG_AAI_HMAC_PRECOMP,
  1548. .alg_op = OP_ALG_ALGSEL_SHA1 | OP_ALG_AAI_HMAC,
  1549. },
  1550. {
  1551. .name = "authenc(hmac(sha224),cbc(des3_ede))",
  1552. .driver_name = "authenc-hmac-sha224-cbc-des3_ede-caam",
  1553. .blocksize = DES3_EDE_BLOCK_SIZE,
  1554. .template_aead = {
  1555. .setkey = aead_setkey,
  1556. .setauthsize = aead_setauthsize,
  1557. .encrypt = aead_encrypt,
  1558. .decrypt = aead_decrypt,
  1559. .givencrypt = aead_givencrypt,
  1560. .geniv = "<built-in>",
  1561. .ivsize = DES3_EDE_BLOCK_SIZE,
  1562. .maxauthsize = SHA224_DIGEST_SIZE,
  1563. },
  1564. .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
  1565. .class2_alg_type = OP_ALG_ALGSEL_SHA224 |
  1566. OP_ALG_AAI_HMAC_PRECOMP,
  1567. .alg_op = OP_ALG_ALGSEL_SHA224 | OP_ALG_AAI_HMAC,
  1568. },
  1569. {
  1570. .name = "authenc(hmac(sha256),cbc(des3_ede))",
  1571. .driver_name = "authenc-hmac-sha256-cbc-des3_ede-caam",
  1572. .blocksize = DES3_EDE_BLOCK_SIZE,
  1573. .type = CRYPTO_ALG_TYPE_AEAD,
  1574. .template_aead = {
  1575. .setkey = aead_setkey,
  1576. .setauthsize = aead_setauthsize,
  1577. .encrypt = aead_encrypt,
  1578. .decrypt = aead_decrypt,
  1579. .givencrypt = aead_givencrypt,
  1580. .geniv = "<built-in>",
  1581. .ivsize = DES3_EDE_BLOCK_SIZE,
  1582. .maxauthsize = SHA256_DIGEST_SIZE,
  1583. },
  1584. .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
  1585. .class2_alg_type = OP_ALG_ALGSEL_SHA256 |
  1586. OP_ALG_AAI_HMAC_PRECOMP,
  1587. .alg_op = OP_ALG_ALGSEL_SHA256 | OP_ALG_AAI_HMAC,
  1588. },
  1589. {
  1590. .name = "authenc(hmac(sha384),cbc(des3_ede))",
  1591. .driver_name = "authenc-hmac-sha384-cbc-des3_ede-caam",
  1592. .blocksize = DES3_EDE_BLOCK_SIZE,
  1593. .template_aead = {
  1594. .setkey = aead_setkey,
  1595. .setauthsize = aead_setauthsize,
  1596. .encrypt = aead_encrypt,
  1597. .decrypt = aead_decrypt,
  1598. .givencrypt = aead_givencrypt,
  1599. .geniv = "<built-in>",
  1600. .ivsize = DES3_EDE_BLOCK_SIZE,
  1601. .maxauthsize = SHA384_DIGEST_SIZE,
  1602. },
  1603. .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
  1604. .class2_alg_type = OP_ALG_ALGSEL_SHA384 |
  1605. OP_ALG_AAI_HMAC_PRECOMP,
  1606. .alg_op = OP_ALG_ALGSEL_SHA384 | OP_ALG_AAI_HMAC,
  1607. },
  1608. {
  1609. .name = "authenc(hmac(sha512),cbc(des3_ede))",
  1610. .driver_name = "authenc-hmac-sha512-cbc-des3_ede-caam",
  1611. .blocksize = DES3_EDE_BLOCK_SIZE,
  1612. .type = CRYPTO_ALG_TYPE_AEAD,
  1613. .template_aead = {
  1614. .setkey = aead_setkey,
  1615. .setauthsize = aead_setauthsize,
  1616. .encrypt = aead_encrypt,
  1617. .decrypt = aead_decrypt,
  1618. .givencrypt = aead_givencrypt,
  1619. .geniv = "<built-in>",
  1620. .ivsize = DES3_EDE_BLOCK_SIZE,
  1621. .maxauthsize = SHA512_DIGEST_SIZE,
  1622. },
  1623. .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
  1624. .class2_alg_type = OP_ALG_ALGSEL_SHA512 |
  1625. OP_ALG_AAI_HMAC_PRECOMP,
  1626. .alg_op = OP_ALG_ALGSEL_SHA512 | OP_ALG_AAI_HMAC,
  1627. },
  1628. {
  1629. .name = "authenc(hmac(md5),cbc(des))",
  1630. .driver_name = "authenc-hmac-md5-cbc-des-caam",
  1631. .blocksize = DES_BLOCK_SIZE,
  1632. .type = CRYPTO_ALG_TYPE_AEAD,
  1633. .template_aead = {
  1634. .setkey = aead_setkey,
  1635. .setauthsize = aead_setauthsize,
  1636. .encrypt = aead_encrypt,
  1637. .decrypt = aead_decrypt,
  1638. .givencrypt = aead_givencrypt,
  1639. .geniv = "<built-in>",
  1640. .ivsize = DES_BLOCK_SIZE,
  1641. .maxauthsize = MD5_DIGEST_SIZE,
  1642. },
  1643. .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
  1644. .class2_alg_type = OP_ALG_ALGSEL_MD5 | OP_ALG_AAI_HMAC_PRECOMP,
  1645. .alg_op = OP_ALG_ALGSEL_MD5 | OP_ALG_AAI_HMAC,
  1646. },
  1647. {
  1648. .name = "authenc(hmac(sha1),cbc(des))",
  1649. .driver_name = "authenc-hmac-sha1-cbc-des-caam",
  1650. .blocksize = DES_BLOCK_SIZE,
  1651. .type = CRYPTO_ALG_TYPE_AEAD,
  1652. .template_aead = {
  1653. .setkey = aead_setkey,
  1654. .setauthsize = aead_setauthsize,
  1655. .encrypt = aead_encrypt,
  1656. .decrypt = aead_decrypt,
  1657. .givencrypt = aead_givencrypt,
  1658. .geniv = "<built-in>",
  1659. .ivsize = DES_BLOCK_SIZE,
  1660. .maxauthsize = SHA1_DIGEST_SIZE,
  1661. },
  1662. .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
  1663. .class2_alg_type = OP_ALG_ALGSEL_SHA1 | OP_ALG_AAI_HMAC_PRECOMP,
  1664. .alg_op = OP_ALG_ALGSEL_SHA1 | OP_ALG_AAI_HMAC,
  1665. },
  1666. {
  1667. .name = "authenc(hmac(sha224),cbc(des))",
  1668. .driver_name = "authenc-hmac-sha224-cbc-des-caam",
  1669. .blocksize = DES_BLOCK_SIZE,
  1670. .template_aead = {
  1671. .setkey = aead_setkey,
  1672. .setauthsize = aead_setauthsize,
  1673. .encrypt = aead_encrypt,
  1674. .decrypt = aead_decrypt,
  1675. .givencrypt = aead_givencrypt,
  1676. .geniv = "<built-in>",
  1677. .ivsize = DES_BLOCK_SIZE,
  1678. .maxauthsize = SHA224_DIGEST_SIZE,
  1679. },
  1680. .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
  1681. .class2_alg_type = OP_ALG_ALGSEL_SHA224 |
  1682. OP_ALG_AAI_HMAC_PRECOMP,
  1683. .alg_op = OP_ALG_ALGSEL_SHA224 | OP_ALG_AAI_HMAC,
  1684. },
  1685. {
  1686. .name = "authenc(hmac(sha256),cbc(des))",
  1687. .driver_name = "authenc-hmac-sha256-cbc-des-caam",
  1688. .blocksize = DES_BLOCK_SIZE,
  1689. .type = CRYPTO_ALG_TYPE_AEAD,
  1690. .template_aead = {
  1691. .setkey = aead_setkey,
  1692. .setauthsize = aead_setauthsize,
  1693. .encrypt = aead_encrypt,
  1694. .decrypt = aead_decrypt,
  1695. .givencrypt = aead_givencrypt,
  1696. .geniv = "<built-in>",
  1697. .ivsize = DES_BLOCK_SIZE,
  1698. .maxauthsize = SHA256_DIGEST_SIZE,
  1699. },
  1700. .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
  1701. .class2_alg_type = OP_ALG_ALGSEL_SHA256 |
  1702. OP_ALG_AAI_HMAC_PRECOMP,
  1703. .alg_op = OP_ALG_ALGSEL_SHA256 | OP_ALG_AAI_HMAC,
  1704. },
  1705. {
  1706. .name = "authenc(hmac(sha384),cbc(des))",
  1707. .driver_name = "authenc-hmac-sha384-cbc-des-caam",
  1708. .blocksize = DES_BLOCK_SIZE,
  1709. .template_aead = {
  1710. .setkey = aead_setkey,
  1711. .setauthsize = aead_setauthsize,
  1712. .encrypt = aead_encrypt,
  1713. .decrypt = aead_decrypt,
  1714. .givencrypt = aead_givencrypt,
  1715. .geniv = "<built-in>",
  1716. .ivsize = DES_BLOCK_SIZE,
  1717. .maxauthsize = SHA384_DIGEST_SIZE,
  1718. },
  1719. .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
  1720. .class2_alg_type = OP_ALG_ALGSEL_SHA384 |
  1721. OP_ALG_AAI_HMAC_PRECOMP,
  1722. .alg_op = OP_ALG_ALGSEL_SHA384 | OP_ALG_AAI_HMAC,
  1723. },
  1724. {
  1725. .name = "authenc(hmac(sha512),cbc(des))",
  1726. .driver_name = "authenc-hmac-sha512-cbc-des-caam",
  1727. .blocksize = DES_BLOCK_SIZE,
  1728. .type = CRYPTO_ALG_TYPE_AEAD,
  1729. .template_aead = {
  1730. .setkey = aead_setkey,
  1731. .setauthsize = aead_setauthsize,
  1732. .encrypt = aead_encrypt,
  1733. .decrypt = aead_decrypt,
  1734. .givencrypt = aead_givencrypt,
  1735. .geniv = "<built-in>",
  1736. .ivsize = DES_BLOCK_SIZE,
  1737. .maxauthsize = SHA512_DIGEST_SIZE,
  1738. },
  1739. .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
  1740. .class2_alg_type = OP_ALG_ALGSEL_SHA512 |
  1741. OP_ALG_AAI_HMAC_PRECOMP,
  1742. .alg_op = OP_ALG_ALGSEL_SHA512 | OP_ALG_AAI_HMAC,
  1743. },
  1744. /* ablkcipher descriptor */
  1745. {
  1746. .name = "cbc(aes)",
  1747. .driver_name = "cbc-aes-caam",
  1748. .blocksize = AES_BLOCK_SIZE,
  1749. .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
  1750. .template_ablkcipher = {
  1751. .setkey = ablkcipher_setkey,
  1752. .encrypt = ablkcipher_encrypt,
  1753. .decrypt = ablkcipher_decrypt,
  1754. .geniv = "eseqiv",
  1755. .min_keysize = AES_MIN_KEY_SIZE,
  1756. .max_keysize = AES_MAX_KEY_SIZE,
  1757. .ivsize = AES_BLOCK_SIZE,
  1758. },
  1759. .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
  1760. },
  1761. {
  1762. .name = "cbc(des3_ede)",
  1763. .driver_name = "cbc-3des-caam",
  1764. .blocksize = DES3_EDE_BLOCK_SIZE,
  1765. .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
  1766. .template_ablkcipher = {
  1767. .setkey = ablkcipher_setkey,
  1768. .encrypt = ablkcipher_encrypt,
  1769. .decrypt = ablkcipher_decrypt,
  1770. .geniv = "eseqiv",
  1771. .min_keysize = DES3_EDE_KEY_SIZE,
  1772. .max_keysize = DES3_EDE_KEY_SIZE,
  1773. .ivsize = DES3_EDE_BLOCK_SIZE,
  1774. },
  1775. .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
  1776. },
  1777. {
  1778. .name = "cbc(des)",
  1779. .driver_name = "cbc-des-caam",
  1780. .blocksize = DES_BLOCK_SIZE,
  1781. .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
  1782. .template_ablkcipher = {
  1783. .setkey = ablkcipher_setkey,
  1784. .encrypt = ablkcipher_encrypt,
  1785. .decrypt = ablkcipher_decrypt,
  1786. .geniv = "eseqiv",
  1787. .min_keysize = DES_KEY_SIZE,
  1788. .max_keysize = DES_KEY_SIZE,
  1789. .ivsize = DES_BLOCK_SIZE,
  1790. },
  1791. .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
  1792. }
  1793. };
  1794. struct caam_crypto_alg {
  1795. struct list_head entry;
  1796. struct device *ctrldev;
  1797. int class1_alg_type;
  1798. int class2_alg_type;
  1799. int alg_op;
  1800. struct crypto_alg crypto_alg;
  1801. };
  1802. static int caam_cra_init(struct crypto_tfm *tfm)
  1803. {
  1804. struct crypto_alg *alg = tfm->__crt_alg;
  1805. struct caam_crypto_alg *caam_alg =
  1806. container_of(alg, struct caam_crypto_alg, crypto_alg);
  1807. struct caam_ctx *ctx = crypto_tfm_ctx(tfm);
  1808. struct caam_drv_private *priv = dev_get_drvdata(caam_alg->ctrldev);
  1809. int tgt_jr = atomic_inc_return(&priv->tfm_count);
  1810. /*
  1811. * distribute tfms across job rings to ensure in-order
  1812. * crypto request processing per tfm
  1813. */
  1814. ctx->jrdev = priv->jrdev[(tgt_jr / 2) % priv->total_jobrs];
  1815. /* copy descriptor header template value */
  1816. ctx->class1_alg_type = OP_TYPE_CLASS1_ALG | caam_alg->class1_alg_type;
  1817. ctx->class2_alg_type = OP_TYPE_CLASS2_ALG | caam_alg->class2_alg_type;
  1818. ctx->alg_op = OP_TYPE_CLASS2_ALG | caam_alg->alg_op;
  1819. return 0;
  1820. }
  1821. static void caam_cra_exit(struct crypto_tfm *tfm)
  1822. {
  1823. struct caam_ctx *ctx = crypto_tfm_ctx(tfm);
  1824. if (ctx->sh_desc_enc_dma &&
  1825. !dma_mapping_error(ctx->jrdev, ctx->sh_desc_enc_dma))
  1826. dma_unmap_single(ctx->jrdev, ctx->sh_desc_enc_dma,
  1827. desc_bytes(ctx->sh_desc_enc), DMA_TO_DEVICE);
  1828. if (ctx->sh_desc_dec_dma &&
  1829. !dma_mapping_error(ctx->jrdev, ctx->sh_desc_dec_dma))
  1830. dma_unmap_single(ctx->jrdev, ctx->sh_desc_dec_dma,
  1831. desc_bytes(ctx->sh_desc_dec), DMA_TO_DEVICE);
  1832. if (ctx->sh_desc_givenc_dma &&
  1833. !dma_mapping_error(ctx->jrdev, ctx->sh_desc_givenc_dma))
  1834. dma_unmap_single(ctx->jrdev, ctx->sh_desc_givenc_dma,
  1835. desc_bytes(ctx->sh_desc_givenc),
  1836. DMA_TO_DEVICE);
  1837. }
  1838. static void __exit caam_algapi_exit(void)
  1839. {
  1840. struct device_node *dev_node;
  1841. struct platform_device *pdev;
  1842. struct device *ctrldev;
  1843. struct caam_drv_private *priv;
  1844. struct caam_crypto_alg *t_alg, *n;
  1845. dev_node = of_find_compatible_node(NULL, NULL, "fsl,sec-v4.0");
  1846. if (!dev_node) {
  1847. dev_node = of_find_compatible_node(NULL, NULL, "fsl,sec4.0");
  1848. if (!dev_node)
  1849. return;
  1850. }
  1851. pdev = of_find_device_by_node(dev_node);
  1852. if (!pdev)
  1853. return;
  1854. ctrldev = &pdev->dev;
  1855. of_node_put(dev_node);
  1856. priv = dev_get_drvdata(ctrldev);
  1857. if (!priv->alg_list.next)
  1858. return;
  1859. list_for_each_entry_safe(t_alg, n, &priv->alg_list, entry) {
  1860. crypto_unregister_alg(&t_alg->crypto_alg);
  1861. list_del(&t_alg->entry);
  1862. kfree(t_alg);
  1863. }
  1864. }
  1865. static struct caam_crypto_alg *caam_alg_alloc(struct device *ctrldev,
  1866. struct caam_alg_template
  1867. *template)
  1868. {
  1869. struct caam_crypto_alg *t_alg;
  1870. struct crypto_alg *alg;
  1871. t_alg = kzalloc(sizeof(struct caam_crypto_alg), GFP_KERNEL);
  1872. if (!t_alg) {
  1873. dev_err(ctrldev, "failed to allocate t_alg\n");
  1874. return ERR_PTR(-ENOMEM);
  1875. }
  1876. alg = &t_alg->crypto_alg;
  1877. snprintf(alg->cra_name, CRYPTO_MAX_ALG_NAME, "%s", template->name);
  1878. snprintf(alg->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
  1879. template->driver_name);
  1880. alg->cra_module = THIS_MODULE;
  1881. alg->cra_init = caam_cra_init;
  1882. alg->cra_exit = caam_cra_exit;
  1883. alg->cra_priority = CAAM_CRA_PRIORITY;
  1884. alg->cra_blocksize = template->blocksize;
  1885. alg->cra_alignmask = 0;
  1886. alg->cra_ctxsize = sizeof(struct caam_ctx);
  1887. alg->cra_flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_KERN_DRIVER_ONLY |
  1888. template->type;
  1889. switch (template->type) {
  1890. case CRYPTO_ALG_TYPE_ABLKCIPHER:
  1891. alg->cra_type = &crypto_ablkcipher_type;
  1892. alg->cra_ablkcipher = template->template_ablkcipher;
  1893. break;
  1894. case CRYPTO_ALG_TYPE_AEAD:
  1895. alg->cra_type = &crypto_aead_type;
  1896. alg->cra_aead = template->template_aead;
  1897. break;
  1898. }
  1899. t_alg->class1_alg_type = template->class1_alg_type;
  1900. t_alg->class2_alg_type = template->class2_alg_type;
  1901. t_alg->alg_op = template->alg_op;
  1902. t_alg->ctrldev = ctrldev;
  1903. return t_alg;
  1904. }
  1905. static int __init caam_algapi_init(void)
  1906. {
  1907. struct device_node *dev_node;
  1908. struct platform_device *pdev;
  1909. struct device *ctrldev;
  1910. struct caam_drv_private *priv;
  1911. int i = 0, err = 0;
  1912. dev_node = of_find_compatible_node(NULL, NULL, "fsl,sec-v4.0");
  1913. if (!dev_node) {
  1914. dev_node = of_find_compatible_node(NULL, NULL, "fsl,sec4.0");
  1915. if (!dev_node)
  1916. return -ENODEV;
  1917. }
  1918. pdev = of_find_device_by_node(dev_node);
  1919. if (!pdev)
  1920. return -ENODEV;
  1921. ctrldev = &pdev->dev;
  1922. priv = dev_get_drvdata(ctrldev);
  1923. of_node_put(dev_node);
  1924. INIT_LIST_HEAD(&priv->alg_list);
  1925. atomic_set(&priv->tfm_count, -1);
  1926. /* register crypto algorithms the device supports */
  1927. for (i = 0; i < ARRAY_SIZE(driver_algs); i++) {
  1928. /* TODO: check if h/w supports alg */
  1929. struct caam_crypto_alg *t_alg;
  1930. t_alg = caam_alg_alloc(ctrldev, &driver_algs[i]);
  1931. if (IS_ERR(t_alg)) {
  1932. err = PTR_ERR(t_alg);
  1933. dev_warn(ctrldev, "%s alg allocation failed\n",
  1934. driver_algs[i].driver_name);
  1935. continue;
  1936. }
  1937. err = crypto_register_alg(&t_alg->crypto_alg);
  1938. if (err) {
  1939. dev_warn(ctrldev, "%s alg registration failed\n",
  1940. t_alg->crypto_alg.cra_driver_name);
  1941. kfree(t_alg);
  1942. } else
  1943. list_add_tail(&t_alg->entry, &priv->alg_list);
  1944. }
  1945. if (!list_empty(&priv->alg_list))
  1946. dev_info(ctrldev, "%s algorithms registered in /proc/crypto\n",
  1947. (char *)of_get_property(dev_node, "compatible", NULL));
  1948. return err;
  1949. }
  1950. module_init(caam_algapi_init);
  1951. module_exit(caam_algapi_exit);
  1952. MODULE_LICENSE("GPL");
  1953. MODULE_DESCRIPTION("FSL CAAM support for crypto API");
  1954. MODULE_AUTHOR("Freescale Semiconductor - NMG/STC");