caamalg.c 73 KB

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