ccp-ops.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842
  1. /*
  2. * AMD Cryptographic Coprocessor (CCP) driver
  3. *
  4. * Copyright (C) 2013,2016 Advanced Micro Devices, Inc.
  5. *
  6. * Author: Tom Lendacky <thomas.lendacky@amd.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/kernel.h>
  14. #include <linux/pci.h>
  15. #include <linux/interrupt.h>
  16. #include <crypto/scatterwalk.h>
  17. #include <linux/ccp.h>
  18. #include "ccp-dev.h"
  19. /* SHA initial context values */
  20. static const __be32 ccp_sha1_init[CCP_SHA_CTXSIZE / sizeof(__be32)] = {
  21. cpu_to_be32(SHA1_H0), cpu_to_be32(SHA1_H1),
  22. cpu_to_be32(SHA1_H2), cpu_to_be32(SHA1_H3),
  23. cpu_to_be32(SHA1_H4), 0, 0, 0,
  24. };
  25. static const __be32 ccp_sha224_init[CCP_SHA_CTXSIZE / sizeof(__be32)] = {
  26. cpu_to_be32(SHA224_H0), cpu_to_be32(SHA224_H1),
  27. cpu_to_be32(SHA224_H2), cpu_to_be32(SHA224_H3),
  28. cpu_to_be32(SHA224_H4), cpu_to_be32(SHA224_H5),
  29. cpu_to_be32(SHA224_H6), cpu_to_be32(SHA224_H7),
  30. };
  31. static const __be32 ccp_sha256_init[CCP_SHA_CTXSIZE / sizeof(__be32)] = {
  32. cpu_to_be32(SHA256_H0), cpu_to_be32(SHA256_H1),
  33. cpu_to_be32(SHA256_H2), cpu_to_be32(SHA256_H3),
  34. cpu_to_be32(SHA256_H4), cpu_to_be32(SHA256_H5),
  35. cpu_to_be32(SHA256_H6), cpu_to_be32(SHA256_H7),
  36. };
  37. static u32 ccp_alloc_ksb(struct ccp_device *ccp, unsigned int count)
  38. {
  39. int start;
  40. for (;;) {
  41. mutex_lock(&ccp->ksb_mutex);
  42. start = (u32)bitmap_find_next_zero_area(ccp->ksb,
  43. ccp->ksb_count,
  44. ccp->ksb_start,
  45. count, 0);
  46. if (start <= ccp->ksb_count) {
  47. bitmap_set(ccp->ksb, start, count);
  48. mutex_unlock(&ccp->ksb_mutex);
  49. break;
  50. }
  51. ccp->ksb_avail = 0;
  52. mutex_unlock(&ccp->ksb_mutex);
  53. /* Wait for KSB entries to become available */
  54. if (wait_event_interruptible(ccp->ksb_queue, ccp->ksb_avail))
  55. return 0;
  56. }
  57. return KSB_START + start;
  58. }
  59. static void ccp_free_ksb(struct ccp_device *ccp, unsigned int start,
  60. unsigned int count)
  61. {
  62. if (!start)
  63. return;
  64. mutex_lock(&ccp->ksb_mutex);
  65. bitmap_clear(ccp->ksb, start - KSB_START, count);
  66. ccp->ksb_avail = 1;
  67. mutex_unlock(&ccp->ksb_mutex);
  68. wake_up_interruptible_all(&ccp->ksb_queue);
  69. }
  70. static u32 ccp_gen_jobid(struct ccp_device *ccp)
  71. {
  72. return atomic_inc_return(&ccp->current_id) & CCP_JOBID_MASK;
  73. }
  74. static void ccp_sg_free(struct ccp_sg_workarea *wa)
  75. {
  76. if (wa->dma_count)
  77. dma_unmap_sg(wa->dma_dev, wa->dma_sg, wa->nents, wa->dma_dir);
  78. wa->dma_count = 0;
  79. }
  80. static int ccp_init_sg_workarea(struct ccp_sg_workarea *wa, struct device *dev,
  81. struct scatterlist *sg, u64 len,
  82. enum dma_data_direction dma_dir)
  83. {
  84. memset(wa, 0, sizeof(*wa));
  85. wa->sg = sg;
  86. if (!sg)
  87. return 0;
  88. wa->nents = sg_nents_for_len(sg, len);
  89. if (wa->nents < 0)
  90. return wa->nents;
  91. wa->bytes_left = len;
  92. wa->sg_used = 0;
  93. if (len == 0)
  94. return 0;
  95. if (dma_dir == DMA_NONE)
  96. return 0;
  97. wa->dma_sg = sg;
  98. wa->dma_dev = dev;
  99. wa->dma_dir = dma_dir;
  100. wa->dma_count = dma_map_sg(dev, sg, wa->nents, dma_dir);
  101. if (!wa->dma_count)
  102. return -ENOMEM;
  103. return 0;
  104. }
  105. static void ccp_update_sg_workarea(struct ccp_sg_workarea *wa, unsigned int len)
  106. {
  107. unsigned int nbytes = min_t(u64, len, wa->bytes_left);
  108. if (!wa->sg)
  109. return;
  110. wa->sg_used += nbytes;
  111. wa->bytes_left -= nbytes;
  112. if (wa->sg_used == wa->sg->length) {
  113. wa->sg = sg_next(wa->sg);
  114. wa->sg_used = 0;
  115. }
  116. }
  117. static void ccp_dm_free(struct ccp_dm_workarea *wa)
  118. {
  119. if (wa->length <= CCP_DMAPOOL_MAX_SIZE) {
  120. if (wa->address)
  121. dma_pool_free(wa->dma_pool, wa->address,
  122. wa->dma.address);
  123. } else {
  124. if (wa->dma.address)
  125. dma_unmap_single(wa->dev, wa->dma.address, wa->length,
  126. wa->dma.dir);
  127. kfree(wa->address);
  128. }
  129. wa->address = NULL;
  130. wa->dma.address = 0;
  131. }
  132. static int ccp_init_dm_workarea(struct ccp_dm_workarea *wa,
  133. struct ccp_cmd_queue *cmd_q,
  134. unsigned int len,
  135. enum dma_data_direction dir)
  136. {
  137. memset(wa, 0, sizeof(*wa));
  138. if (!len)
  139. return 0;
  140. wa->dev = cmd_q->ccp->dev;
  141. wa->length = len;
  142. if (len <= CCP_DMAPOOL_MAX_SIZE) {
  143. wa->dma_pool = cmd_q->dma_pool;
  144. wa->address = dma_pool_alloc(wa->dma_pool, GFP_KERNEL,
  145. &wa->dma.address);
  146. if (!wa->address)
  147. return -ENOMEM;
  148. wa->dma.length = CCP_DMAPOOL_MAX_SIZE;
  149. memset(wa->address, 0, CCP_DMAPOOL_MAX_SIZE);
  150. } else {
  151. wa->address = kzalloc(len, GFP_KERNEL);
  152. if (!wa->address)
  153. return -ENOMEM;
  154. wa->dma.address = dma_map_single(wa->dev, wa->address, len,
  155. dir);
  156. if (!wa->dma.address)
  157. return -ENOMEM;
  158. wa->dma.length = len;
  159. }
  160. wa->dma.dir = dir;
  161. return 0;
  162. }
  163. static void ccp_set_dm_area(struct ccp_dm_workarea *wa, unsigned int wa_offset,
  164. struct scatterlist *sg, unsigned int sg_offset,
  165. unsigned int len)
  166. {
  167. WARN_ON(!wa->address);
  168. scatterwalk_map_and_copy(wa->address + wa_offset, sg, sg_offset, len,
  169. 0);
  170. }
  171. static void ccp_get_dm_area(struct ccp_dm_workarea *wa, unsigned int wa_offset,
  172. struct scatterlist *sg, unsigned int sg_offset,
  173. unsigned int len)
  174. {
  175. WARN_ON(!wa->address);
  176. scatterwalk_map_and_copy(wa->address + wa_offset, sg, sg_offset, len,
  177. 1);
  178. }
  179. static int ccp_reverse_set_dm_area(struct ccp_dm_workarea *wa,
  180. struct scatterlist *sg,
  181. unsigned int len, unsigned int se_len,
  182. bool sign_extend)
  183. {
  184. unsigned int nbytes, sg_offset, dm_offset, ksb_len, i;
  185. u8 buffer[CCP_REVERSE_BUF_SIZE];
  186. if (WARN_ON(se_len > sizeof(buffer)))
  187. return -EINVAL;
  188. sg_offset = len;
  189. dm_offset = 0;
  190. nbytes = len;
  191. while (nbytes) {
  192. ksb_len = min_t(unsigned int, nbytes, se_len);
  193. sg_offset -= ksb_len;
  194. scatterwalk_map_and_copy(buffer, sg, sg_offset, ksb_len, 0);
  195. for (i = 0; i < ksb_len; i++)
  196. wa->address[dm_offset + i] = buffer[ksb_len - i - 1];
  197. dm_offset += ksb_len;
  198. nbytes -= ksb_len;
  199. if ((ksb_len != se_len) && sign_extend) {
  200. /* Must sign-extend to nearest sign-extend length */
  201. if (wa->address[dm_offset - 1] & 0x80)
  202. memset(wa->address + dm_offset, 0xff,
  203. se_len - ksb_len);
  204. }
  205. }
  206. return 0;
  207. }
  208. static void ccp_reverse_get_dm_area(struct ccp_dm_workarea *wa,
  209. struct scatterlist *sg,
  210. unsigned int len)
  211. {
  212. unsigned int nbytes, sg_offset, dm_offset, ksb_len, i;
  213. u8 buffer[CCP_REVERSE_BUF_SIZE];
  214. sg_offset = 0;
  215. dm_offset = len;
  216. nbytes = len;
  217. while (nbytes) {
  218. ksb_len = min_t(unsigned int, nbytes, sizeof(buffer));
  219. dm_offset -= ksb_len;
  220. for (i = 0; i < ksb_len; i++)
  221. buffer[ksb_len - i - 1] = wa->address[dm_offset + i];
  222. scatterwalk_map_and_copy(buffer, sg, sg_offset, ksb_len, 1);
  223. sg_offset += ksb_len;
  224. nbytes -= ksb_len;
  225. }
  226. }
  227. static void ccp_free_data(struct ccp_data *data, struct ccp_cmd_queue *cmd_q)
  228. {
  229. ccp_dm_free(&data->dm_wa);
  230. ccp_sg_free(&data->sg_wa);
  231. }
  232. static int ccp_init_data(struct ccp_data *data, struct ccp_cmd_queue *cmd_q,
  233. struct scatterlist *sg, u64 sg_len,
  234. unsigned int dm_len,
  235. enum dma_data_direction dir)
  236. {
  237. int ret;
  238. memset(data, 0, sizeof(*data));
  239. ret = ccp_init_sg_workarea(&data->sg_wa, cmd_q->ccp->dev, sg, sg_len,
  240. dir);
  241. if (ret)
  242. goto e_err;
  243. ret = ccp_init_dm_workarea(&data->dm_wa, cmd_q, dm_len, dir);
  244. if (ret)
  245. goto e_err;
  246. return 0;
  247. e_err:
  248. ccp_free_data(data, cmd_q);
  249. return ret;
  250. }
  251. static unsigned int ccp_queue_buf(struct ccp_data *data, unsigned int from)
  252. {
  253. struct ccp_sg_workarea *sg_wa = &data->sg_wa;
  254. struct ccp_dm_workarea *dm_wa = &data->dm_wa;
  255. unsigned int buf_count, nbytes;
  256. /* Clear the buffer if setting it */
  257. if (!from)
  258. memset(dm_wa->address, 0, dm_wa->length);
  259. if (!sg_wa->sg)
  260. return 0;
  261. /* Perform the copy operation
  262. * nbytes will always be <= UINT_MAX because dm_wa->length is
  263. * an unsigned int
  264. */
  265. nbytes = min_t(u64, sg_wa->bytes_left, dm_wa->length);
  266. scatterwalk_map_and_copy(dm_wa->address, sg_wa->sg, sg_wa->sg_used,
  267. nbytes, from);
  268. /* Update the structures and generate the count */
  269. buf_count = 0;
  270. while (sg_wa->bytes_left && (buf_count < dm_wa->length)) {
  271. nbytes = min(sg_wa->sg->length - sg_wa->sg_used,
  272. dm_wa->length - buf_count);
  273. nbytes = min_t(u64, sg_wa->bytes_left, nbytes);
  274. buf_count += nbytes;
  275. ccp_update_sg_workarea(sg_wa, nbytes);
  276. }
  277. return buf_count;
  278. }
  279. static unsigned int ccp_fill_queue_buf(struct ccp_data *data)
  280. {
  281. return ccp_queue_buf(data, 0);
  282. }
  283. static unsigned int ccp_empty_queue_buf(struct ccp_data *data)
  284. {
  285. return ccp_queue_buf(data, 1);
  286. }
  287. static void ccp_prepare_data(struct ccp_data *src, struct ccp_data *dst,
  288. struct ccp_op *op, unsigned int block_size,
  289. bool blocksize_op)
  290. {
  291. unsigned int sg_src_len, sg_dst_len, op_len;
  292. /* The CCP can only DMA from/to one address each per operation. This
  293. * requires that we find the smallest DMA area between the source
  294. * and destination. The resulting len values will always be <= UINT_MAX
  295. * because the dma length is an unsigned int.
  296. */
  297. sg_src_len = sg_dma_len(src->sg_wa.sg) - src->sg_wa.sg_used;
  298. sg_src_len = min_t(u64, src->sg_wa.bytes_left, sg_src_len);
  299. if (dst) {
  300. sg_dst_len = sg_dma_len(dst->sg_wa.sg) - dst->sg_wa.sg_used;
  301. sg_dst_len = min_t(u64, src->sg_wa.bytes_left, sg_dst_len);
  302. op_len = min(sg_src_len, sg_dst_len);
  303. } else {
  304. op_len = sg_src_len;
  305. }
  306. /* The data operation length will be at least block_size in length
  307. * or the smaller of available sg room remaining for the source or
  308. * the destination
  309. */
  310. op_len = max(op_len, block_size);
  311. /* Unless we have to buffer data, there's no reason to wait */
  312. op->soc = 0;
  313. if (sg_src_len < block_size) {
  314. /* Not enough data in the sg element, so it
  315. * needs to be buffered into a blocksize chunk
  316. */
  317. int cp_len = ccp_fill_queue_buf(src);
  318. op->soc = 1;
  319. op->src.u.dma.address = src->dm_wa.dma.address;
  320. op->src.u.dma.offset = 0;
  321. op->src.u.dma.length = (blocksize_op) ? block_size : cp_len;
  322. } else {
  323. /* Enough data in the sg element, but we need to
  324. * adjust for any previously copied data
  325. */
  326. op->src.u.dma.address = sg_dma_address(src->sg_wa.sg);
  327. op->src.u.dma.offset = src->sg_wa.sg_used;
  328. op->src.u.dma.length = op_len & ~(block_size - 1);
  329. ccp_update_sg_workarea(&src->sg_wa, op->src.u.dma.length);
  330. }
  331. if (dst) {
  332. if (sg_dst_len < block_size) {
  333. /* Not enough room in the sg element or we're on the
  334. * last piece of data (when using padding), so the
  335. * output needs to be buffered into a blocksize chunk
  336. */
  337. op->soc = 1;
  338. op->dst.u.dma.address = dst->dm_wa.dma.address;
  339. op->dst.u.dma.offset = 0;
  340. op->dst.u.dma.length = op->src.u.dma.length;
  341. } else {
  342. /* Enough room in the sg element, but we need to
  343. * adjust for any previously used area
  344. */
  345. op->dst.u.dma.address = sg_dma_address(dst->sg_wa.sg);
  346. op->dst.u.dma.offset = dst->sg_wa.sg_used;
  347. op->dst.u.dma.length = op->src.u.dma.length;
  348. }
  349. }
  350. }
  351. static void ccp_process_data(struct ccp_data *src, struct ccp_data *dst,
  352. struct ccp_op *op)
  353. {
  354. op->init = 0;
  355. if (dst) {
  356. if (op->dst.u.dma.address == dst->dm_wa.dma.address)
  357. ccp_empty_queue_buf(dst);
  358. else
  359. ccp_update_sg_workarea(&dst->sg_wa,
  360. op->dst.u.dma.length);
  361. }
  362. }
  363. static int ccp_copy_to_from_ksb(struct ccp_cmd_queue *cmd_q,
  364. struct ccp_dm_workarea *wa, u32 jobid, u32 ksb,
  365. u32 byte_swap, bool from)
  366. {
  367. struct ccp_op op;
  368. memset(&op, 0, sizeof(op));
  369. op.cmd_q = cmd_q;
  370. op.jobid = jobid;
  371. op.eom = 1;
  372. if (from) {
  373. op.soc = 1;
  374. op.src.type = CCP_MEMTYPE_KSB;
  375. op.src.u.ksb = ksb;
  376. op.dst.type = CCP_MEMTYPE_SYSTEM;
  377. op.dst.u.dma.address = wa->dma.address;
  378. op.dst.u.dma.length = wa->length;
  379. } else {
  380. op.src.type = CCP_MEMTYPE_SYSTEM;
  381. op.src.u.dma.address = wa->dma.address;
  382. op.src.u.dma.length = wa->length;
  383. op.dst.type = CCP_MEMTYPE_KSB;
  384. op.dst.u.ksb = ksb;
  385. }
  386. op.u.passthru.byte_swap = byte_swap;
  387. return cmd_q->ccp->vdata->perform->perform_passthru(&op);
  388. }
  389. static int ccp_copy_to_ksb(struct ccp_cmd_queue *cmd_q,
  390. struct ccp_dm_workarea *wa, u32 jobid, u32 ksb,
  391. u32 byte_swap)
  392. {
  393. return ccp_copy_to_from_ksb(cmd_q, wa, jobid, ksb, byte_swap, false);
  394. }
  395. static int ccp_copy_from_ksb(struct ccp_cmd_queue *cmd_q,
  396. struct ccp_dm_workarea *wa, u32 jobid, u32 ksb,
  397. u32 byte_swap)
  398. {
  399. return ccp_copy_to_from_ksb(cmd_q, wa, jobid, ksb, byte_swap, true);
  400. }
  401. static int ccp_run_aes_cmac_cmd(struct ccp_cmd_queue *cmd_q,
  402. struct ccp_cmd *cmd)
  403. {
  404. struct ccp_aes_engine *aes = &cmd->u.aes;
  405. struct ccp_dm_workarea key, ctx;
  406. struct ccp_data src;
  407. struct ccp_op op;
  408. unsigned int dm_offset;
  409. int ret;
  410. if (!((aes->key_len == AES_KEYSIZE_128) ||
  411. (aes->key_len == AES_KEYSIZE_192) ||
  412. (aes->key_len == AES_KEYSIZE_256)))
  413. return -EINVAL;
  414. if (aes->src_len & (AES_BLOCK_SIZE - 1))
  415. return -EINVAL;
  416. if (aes->iv_len != AES_BLOCK_SIZE)
  417. return -EINVAL;
  418. if (!aes->key || !aes->iv || !aes->src)
  419. return -EINVAL;
  420. if (aes->cmac_final) {
  421. if (aes->cmac_key_len != AES_BLOCK_SIZE)
  422. return -EINVAL;
  423. if (!aes->cmac_key)
  424. return -EINVAL;
  425. }
  426. BUILD_BUG_ON(CCP_AES_KEY_KSB_COUNT != 1);
  427. BUILD_BUG_ON(CCP_AES_CTX_KSB_COUNT != 1);
  428. ret = -EIO;
  429. memset(&op, 0, sizeof(op));
  430. op.cmd_q = cmd_q;
  431. op.jobid = ccp_gen_jobid(cmd_q->ccp);
  432. op.ksb_key = cmd_q->ksb_key;
  433. op.ksb_ctx = cmd_q->ksb_ctx;
  434. op.init = 1;
  435. op.u.aes.type = aes->type;
  436. op.u.aes.mode = aes->mode;
  437. op.u.aes.action = aes->action;
  438. /* All supported key sizes fit in a single (32-byte) KSB entry
  439. * and must be in little endian format. Use the 256-bit byte
  440. * swap passthru option to convert from big endian to little
  441. * endian.
  442. */
  443. ret = ccp_init_dm_workarea(&key, cmd_q,
  444. CCP_AES_KEY_KSB_COUNT * CCP_KSB_BYTES,
  445. DMA_TO_DEVICE);
  446. if (ret)
  447. return ret;
  448. dm_offset = CCP_KSB_BYTES - aes->key_len;
  449. ccp_set_dm_area(&key, dm_offset, aes->key, 0, aes->key_len);
  450. ret = ccp_copy_to_ksb(cmd_q, &key, op.jobid, op.ksb_key,
  451. CCP_PASSTHRU_BYTESWAP_256BIT);
  452. if (ret) {
  453. cmd->engine_error = cmd_q->cmd_error;
  454. goto e_key;
  455. }
  456. /* The AES context fits in a single (32-byte) KSB entry and
  457. * must be in little endian format. Use the 256-bit byte swap
  458. * passthru option to convert from big endian to little endian.
  459. */
  460. ret = ccp_init_dm_workarea(&ctx, cmd_q,
  461. CCP_AES_CTX_KSB_COUNT * CCP_KSB_BYTES,
  462. DMA_BIDIRECTIONAL);
  463. if (ret)
  464. goto e_key;
  465. dm_offset = CCP_KSB_BYTES - AES_BLOCK_SIZE;
  466. ccp_set_dm_area(&ctx, dm_offset, aes->iv, 0, aes->iv_len);
  467. ret = ccp_copy_to_ksb(cmd_q, &ctx, op.jobid, op.ksb_ctx,
  468. CCP_PASSTHRU_BYTESWAP_256BIT);
  469. if (ret) {
  470. cmd->engine_error = cmd_q->cmd_error;
  471. goto e_ctx;
  472. }
  473. /* Send data to the CCP AES engine */
  474. ret = ccp_init_data(&src, cmd_q, aes->src, aes->src_len,
  475. AES_BLOCK_SIZE, DMA_TO_DEVICE);
  476. if (ret)
  477. goto e_ctx;
  478. while (src.sg_wa.bytes_left) {
  479. ccp_prepare_data(&src, NULL, &op, AES_BLOCK_SIZE, true);
  480. if (aes->cmac_final && !src.sg_wa.bytes_left) {
  481. op.eom = 1;
  482. /* Push the K1/K2 key to the CCP now */
  483. ret = ccp_copy_from_ksb(cmd_q, &ctx, op.jobid,
  484. op.ksb_ctx,
  485. CCP_PASSTHRU_BYTESWAP_256BIT);
  486. if (ret) {
  487. cmd->engine_error = cmd_q->cmd_error;
  488. goto e_src;
  489. }
  490. ccp_set_dm_area(&ctx, 0, aes->cmac_key, 0,
  491. aes->cmac_key_len);
  492. ret = ccp_copy_to_ksb(cmd_q, &ctx, op.jobid, op.ksb_ctx,
  493. CCP_PASSTHRU_BYTESWAP_256BIT);
  494. if (ret) {
  495. cmd->engine_error = cmd_q->cmd_error;
  496. goto e_src;
  497. }
  498. }
  499. ret = cmd_q->ccp->vdata->perform->perform_aes(&op);
  500. if (ret) {
  501. cmd->engine_error = cmd_q->cmd_error;
  502. goto e_src;
  503. }
  504. ccp_process_data(&src, NULL, &op);
  505. }
  506. /* Retrieve the AES context - convert from LE to BE using
  507. * 32-byte (256-bit) byteswapping
  508. */
  509. ret = ccp_copy_from_ksb(cmd_q, &ctx, op.jobid, op.ksb_ctx,
  510. CCP_PASSTHRU_BYTESWAP_256BIT);
  511. if (ret) {
  512. cmd->engine_error = cmd_q->cmd_error;
  513. goto e_src;
  514. }
  515. /* ...but we only need AES_BLOCK_SIZE bytes */
  516. dm_offset = CCP_KSB_BYTES - AES_BLOCK_SIZE;
  517. ccp_get_dm_area(&ctx, dm_offset, aes->iv, 0, aes->iv_len);
  518. e_src:
  519. ccp_free_data(&src, cmd_q);
  520. e_ctx:
  521. ccp_dm_free(&ctx);
  522. e_key:
  523. ccp_dm_free(&key);
  524. return ret;
  525. }
  526. static int ccp_run_aes_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
  527. {
  528. struct ccp_aes_engine *aes = &cmd->u.aes;
  529. struct ccp_dm_workarea key, ctx;
  530. struct ccp_data src, dst;
  531. struct ccp_op op;
  532. unsigned int dm_offset;
  533. bool in_place = false;
  534. int ret;
  535. if (aes->mode == CCP_AES_MODE_CMAC)
  536. return ccp_run_aes_cmac_cmd(cmd_q, cmd);
  537. if (!((aes->key_len == AES_KEYSIZE_128) ||
  538. (aes->key_len == AES_KEYSIZE_192) ||
  539. (aes->key_len == AES_KEYSIZE_256)))
  540. return -EINVAL;
  541. if (((aes->mode == CCP_AES_MODE_ECB) ||
  542. (aes->mode == CCP_AES_MODE_CBC) ||
  543. (aes->mode == CCP_AES_MODE_CFB)) &&
  544. (aes->src_len & (AES_BLOCK_SIZE - 1)))
  545. return -EINVAL;
  546. if (!aes->key || !aes->src || !aes->dst)
  547. return -EINVAL;
  548. if (aes->mode != CCP_AES_MODE_ECB) {
  549. if (aes->iv_len != AES_BLOCK_SIZE)
  550. return -EINVAL;
  551. if (!aes->iv)
  552. return -EINVAL;
  553. }
  554. BUILD_BUG_ON(CCP_AES_KEY_KSB_COUNT != 1);
  555. BUILD_BUG_ON(CCP_AES_CTX_KSB_COUNT != 1);
  556. ret = -EIO;
  557. memset(&op, 0, sizeof(op));
  558. op.cmd_q = cmd_q;
  559. op.jobid = ccp_gen_jobid(cmd_q->ccp);
  560. op.ksb_key = cmd_q->ksb_key;
  561. op.ksb_ctx = cmd_q->ksb_ctx;
  562. op.init = (aes->mode == CCP_AES_MODE_ECB) ? 0 : 1;
  563. op.u.aes.type = aes->type;
  564. op.u.aes.mode = aes->mode;
  565. op.u.aes.action = aes->action;
  566. /* All supported key sizes fit in a single (32-byte) KSB entry
  567. * and must be in little endian format. Use the 256-bit byte
  568. * swap passthru option to convert from big endian to little
  569. * endian.
  570. */
  571. ret = ccp_init_dm_workarea(&key, cmd_q,
  572. CCP_AES_KEY_KSB_COUNT * CCP_KSB_BYTES,
  573. DMA_TO_DEVICE);
  574. if (ret)
  575. return ret;
  576. dm_offset = CCP_KSB_BYTES - aes->key_len;
  577. ccp_set_dm_area(&key, dm_offset, aes->key, 0, aes->key_len);
  578. ret = ccp_copy_to_ksb(cmd_q, &key, op.jobid, op.ksb_key,
  579. CCP_PASSTHRU_BYTESWAP_256BIT);
  580. if (ret) {
  581. cmd->engine_error = cmd_q->cmd_error;
  582. goto e_key;
  583. }
  584. /* The AES context fits in a single (32-byte) KSB entry and
  585. * must be in little endian format. Use the 256-bit byte swap
  586. * passthru option to convert from big endian to little endian.
  587. */
  588. ret = ccp_init_dm_workarea(&ctx, cmd_q,
  589. CCP_AES_CTX_KSB_COUNT * CCP_KSB_BYTES,
  590. DMA_BIDIRECTIONAL);
  591. if (ret)
  592. goto e_key;
  593. if (aes->mode != CCP_AES_MODE_ECB) {
  594. /* Load the AES context - conver to LE */
  595. dm_offset = CCP_KSB_BYTES - AES_BLOCK_SIZE;
  596. ccp_set_dm_area(&ctx, dm_offset, aes->iv, 0, aes->iv_len);
  597. ret = ccp_copy_to_ksb(cmd_q, &ctx, op.jobid, op.ksb_ctx,
  598. CCP_PASSTHRU_BYTESWAP_256BIT);
  599. if (ret) {
  600. cmd->engine_error = cmd_q->cmd_error;
  601. goto e_ctx;
  602. }
  603. }
  604. /* Prepare the input and output data workareas. For in-place
  605. * operations we need to set the dma direction to BIDIRECTIONAL
  606. * and copy the src workarea to the dst workarea.
  607. */
  608. if (sg_virt(aes->src) == sg_virt(aes->dst))
  609. in_place = true;
  610. ret = ccp_init_data(&src, cmd_q, aes->src, aes->src_len,
  611. AES_BLOCK_SIZE,
  612. in_place ? DMA_BIDIRECTIONAL : DMA_TO_DEVICE);
  613. if (ret)
  614. goto e_ctx;
  615. if (in_place) {
  616. dst = src;
  617. } else {
  618. ret = ccp_init_data(&dst, cmd_q, aes->dst, aes->src_len,
  619. AES_BLOCK_SIZE, DMA_FROM_DEVICE);
  620. if (ret)
  621. goto e_src;
  622. }
  623. /* Send data to the CCP AES engine */
  624. while (src.sg_wa.bytes_left) {
  625. ccp_prepare_data(&src, &dst, &op, AES_BLOCK_SIZE, true);
  626. if (!src.sg_wa.bytes_left) {
  627. op.eom = 1;
  628. /* Since we don't retrieve the AES context in ECB
  629. * mode we have to wait for the operation to complete
  630. * on the last piece of data
  631. */
  632. if (aes->mode == CCP_AES_MODE_ECB)
  633. op.soc = 1;
  634. }
  635. ret = cmd_q->ccp->vdata->perform->perform_aes(&op);
  636. if (ret) {
  637. cmd->engine_error = cmd_q->cmd_error;
  638. goto e_dst;
  639. }
  640. ccp_process_data(&src, &dst, &op);
  641. }
  642. if (aes->mode != CCP_AES_MODE_ECB) {
  643. /* Retrieve the AES context - convert from LE to BE using
  644. * 32-byte (256-bit) byteswapping
  645. */
  646. ret = ccp_copy_from_ksb(cmd_q, &ctx, op.jobid, op.ksb_ctx,
  647. CCP_PASSTHRU_BYTESWAP_256BIT);
  648. if (ret) {
  649. cmd->engine_error = cmd_q->cmd_error;
  650. goto e_dst;
  651. }
  652. /* ...but we only need AES_BLOCK_SIZE bytes */
  653. dm_offset = CCP_KSB_BYTES - AES_BLOCK_SIZE;
  654. ccp_get_dm_area(&ctx, dm_offset, aes->iv, 0, aes->iv_len);
  655. }
  656. e_dst:
  657. if (!in_place)
  658. ccp_free_data(&dst, cmd_q);
  659. e_src:
  660. ccp_free_data(&src, cmd_q);
  661. e_ctx:
  662. ccp_dm_free(&ctx);
  663. e_key:
  664. ccp_dm_free(&key);
  665. return ret;
  666. }
  667. static int ccp_run_xts_aes_cmd(struct ccp_cmd_queue *cmd_q,
  668. struct ccp_cmd *cmd)
  669. {
  670. struct ccp_xts_aes_engine *xts = &cmd->u.xts;
  671. struct ccp_dm_workarea key, ctx;
  672. struct ccp_data src, dst;
  673. struct ccp_op op;
  674. unsigned int unit_size, dm_offset;
  675. bool in_place = false;
  676. int ret;
  677. switch (xts->unit_size) {
  678. case CCP_XTS_AES_UNIT_SIZE_16:
  679. unit_size = 16;
  680. break;
  681. case CCP_XTS_AES_UNIT_SIZE_512:
  682. unit_size = 512;
  683. break;
  684. case CCP_XTS_AES_UNIT_SIZE_1024:
  685. unit_size = 1024;
  686. break;
  687. case CCP_XTS_AES_UNIT_SIZE_2048:
  688. unit_size = 2048;
  689. break;
  690. case CCP_XTS_AES_UNIT_SIZE_4096:
  691. unit_size = 4096;
  692. break;
  693. default:
  694. return -EINVAL;
  695. }
  696. if (xts->key_len != AES_KEYSIZE_128)
  697. return -EINVAL;
  698. if (!xts->final && (xts->src_len & (AES_BLOCK_SIZE - 1)))
  699. return -EINVAL;
  700. if (xts->iv_len != AES_BLOCK_SIZE)
  701. return -EINVAL;
  702. if (!xts->key || !xts->iv || !xts->src || !xts->dst)
  703. return -EINVAL;
  704. BUILD_BUG_ON(CCP_XTS_AES_KEY_KSB_COUNT != 1);
  705. BUILD_BUG_ON(CCP_XTS_AES_CTX_KSB_COUNT != 1);
  706. ret = -EIO;
  707. memset(&op, 0, sizeof(op));
  708. op.cmd_q = cmd_q;
  709. op.jobid = ccp_gen_jobid(cmd_q->ccp);
  710. op.ksb_key = cmd_q->ksb_key;
  711. op.ksb_ctx = cmd_q->ksb_ctx;
  712. op.init = 1;
  713. op.u.xts.action = xts->action;
  714. op.u.xts.unit_size = xts->unit_size;
  715. /* All supported key sizes fit in a single (32-byte) KSB entry
  716. * and must be in little endian format. Use the 256-bit byte
  717. * swap passthru option to convert from big endian to little
  718. * endian.
  719. */
  720. ret = ccp_init_dm_workarea(&key, cmd_q,
  721. CCP_XTS_AES_KEY_KSB_COUNT * CCP_KSB_BYTES,
  722. DMA_TO_DEVICE);
  723. if (ret)
  724. return ret;
  725. dm_offset = CCP_KSB_BYTES - AES_KEYSIZE_128;
  726. ccp_set_dm_area(&key, dm_offset, xts->key, 0, xts->key_len);
  727. ccp_set_dm_area(&key, 0, xts->key, dm_offset, xts->key_len);
  728. ret = ccp_copy_to_ksb(cmd_q, &key, op.jobid, op.ksb_key,
  729. CCP_PASSTHRU_BYTESWAP_256BIT);
  730. if (ret) {
  731. cmd->engine_error = cmd_q->cmd_error;
  732. goto e_key;
  733. }
  734. /* The AES context fits in a single (32-byte) KSB entry and
  735. * for XTS is already in little endian format so no byte swapping
  736. * is needed.
  737. */
  738. ret = ccp_init_dm_workarea(&ctx, cmd_q,
  739. CCP_XTS_AES_CTX_KSB_COUNT * CCP_KSB_BYTES,
  740. DMA_BIDIRECTIONAL);
  741. if (ret)
  742. goto e_key;
  743. ccp_set_dm_area(&ctx, 0, xts->iv, 0, xts->iv_len);
  744. ret = ccp_copy_to_ksb(cmd_q, &ctx, op.jobid, op.ksb_ctx,
  745. CCP_PASSTHRU_BYTESWAP_NOOP);
  746. if (ret) {
  747. cmd->engine_error = cmd_q->cmd_error;
  748. goto e_ctx;
  749. }
  750. /* Prepare the input and output data workareas. For in-place
  751. * operations we need to set the dma direction to BIDIRECTIONAL
  752. * and copy the src workarea to the dst workarea.
  753. */
  754. if (sg_virt(xts->src) == sg_virt(xts->dst))
  755. in_place = true;
  756. ret = ccp_init_data(&src, cmd_q, xts->src, xts->src_len,
  757. unit_size,
  758. in_place ? DMA_BIDIRECTIONAL : DMA_TO_DEVICE);
  759. if (ret)
  760. goto e_ctx;
  761. if (in_place) {
  762. dst = src;
  763. } else {
  764. ret = ccp_init_data(&dst, cmd_q, xts->dst, xts->src_len,
  765. unit_size, DMA_FROM_DEVICE);
  766. if (ret)
  767. goto e_src;
  768. }
  769. /* Send data to the CCP AES engine */
  770. while (src.sg_wa.bytes_left) {
  771. ccp_prepare_data(&src, &dst, &op, unit_size, true);
  772. if (!src.sg_wa.bytes_left)
  773. op.eom = 1;
  774. ret = cmd_q->ccp->vdata->perform->perform_xts_aes(&op);
  775. if (ret) {
  776. cmd->engine_error = cmd_q->cmd_error;
  777. goto e_dst;
  778. }
  779. ccp_process_data(&src, &dst, &op);
  780. }
  781. /* Retrieve the AES context - convert from LE to BE using
  782. * 32-byte (256-bit) byteswapping
  783. */
  784. ret = ccp_copy_from_ksb(cmd_q, &ctx, op.jobid, op.ksb_ctx,
  785. CCP_PASSTHRU_BYTESWAP_256BIT);
  786. if (ret) {
  787. cmd->engine_error = cmd_q->cmd_error;
  788. goto e_dst;
  789. }
  790. /* ...but we only need AES_BLOCK_SIZE bytes */
  791. dm_offset = CCP_KSB_BYTES - AES_BLOCK_SIZE;
  792. ccp_get_dm_area(&ctx, dm_offset, xts->iv, 0, xts->iv_len);
  793. e_dst:
  794. if (!in_place)
  795. ccp_free_data(&dst, cmd_q);
  796. e_src:
  797. ccp_free_data(&src, cmd_q);
  798. e_ctx:
  799. ccp_dm_free(&ctx);
  800. e_key:
  801. ccp_dm_free(&key);
  802. return ret;
  803. }
  804. static int ccp_run_sha_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
  805. {
  806. struct ccp_sha_engine *sha = &cmd->u.sha;
  807. struct ccp_dm_workarea ctx;
  808. struct ccp_data src;
  809. struct ccp_op op;
  810. int ret;
  811. if (sha->ctx_len != CCP_SHA_CTXSIZE)
  812. return -EINVAL;
  813. if (!sha->ctx)
  814. return -EINVAL;
  815. if (!sha->final && (sha->src_len & (CCP_SHA_BLOCKSIZE - 1)))
  816. return -EINVAL;
  817. if (!sha->src_len) {
  818. const u8 *sha_zero;
  819. /* Not final, just return */
  820. if (!sha->final)
  821. return 0;
  822. /* CCP can't do a zero length sha operation so the caller
  823. * must buffer the data.
  824. */
  825. if (sha->msg_bits)
  826. return -EINVAL;
  827. /* The CCP cannot perform zero-length sha operations so the
  828. * caller is required to buffer data for the final operation.
  829. * However, a sha operation for a message with a total length
  830. * of zero is valid so known values are required to supply
  831. * the result.
  832. */
  833. switch (sha->type) {
  834. case CCP_SHA_TYPE_1:
  835. sha_zero = sha1_zero_message_hash;
  836. break;
  837. case CCP_SHA_TYPE_224:
  838. sha_zero = sha224_zero_message_hash;
  839. break;
  840. case CCP_SHA_TYPE_256:
  841. sha_zero = sha256_zero_message_hash;
  842. break;
  843. default:
  844. return -EINVAL;
  845. }
  846. scatterwalk_map_and_copy((void *)sha_zero, sha->ctx, 0,
  847. sha->ctx_len, 1);
  848. return 0;
  849. }
  850. if (!sha->src)
  851. return -EINVAL;
  852. BUILD_BUG_ON(CCP_SHA_KSB_COUNT != 1);
  853. memset(&op, 0, sizeof(op));
  854. op.cmd_q = cmd_q;
  855. op.jobid = ccp_gen_jobid(cmd_q->ccp);
  856. op.ksb_ctx = cmd_q->ksb_ctx;
  857. op.u.sha.type = sha->type;
  858. op.u.sha.msg_bits = sha->msg_bits;
  859. /* The SHA context fits in a single (32-byte) KSB entry and
  860. * must be in little endian format. Use the 256-bit byte swap
  861. * passthru option to convert from big endian to little endian.
  862. */
  863. ret = ccp_init_dm_workarea(&ctx, cmd_q,
  864. CCP_SHA_KSB_COUNT * CCP_KSB_BYTES,
  865. DMA_BIDIRECTIONAL);
  866. if (ret)
  867. return ret;
  868. if (sha->first) {
  869. const __be32 *init;
  870. switch (sha->type) {
  871. case CCP_SHA_TYPE_1:
  872. init = ccp_sha1_init;
  873. break;
  874. case CCP_SHA_TYPE_224:
  875. init = ccp_sha224_init;
  876. break;
  877. case CCP_SHA_TYPE_256:
  878. init = ccp_sha256_init;
  879. break;
  880. default:
  881. ret = -EINVAL;
  882. goto e_ctx;
  883. }
  884. memcpy(ctx.address, init, CCP_SHA_CTXSIZE);
  885. } else {
  886. ccp_set_dm_area(&ctx, 0, sha->ctx, 0, sha->ctx_len);
  887. }
  888. ret = ccp_copy_to_ksb(cmd_q, &ctx, op.jobid, op.ksb_ctx,
  889. CCP_PASSTHRU_BYTESWAP_256BIT);
  890. if (ret) {
  891. cmd->engine_error = cmd_q->cmd_error;
  892. goto e_ctx;
  893. }
  894. /* Send data to the CCP SHA engine */
  895. ret = ccp_init_data(&src, cmd_q, sha->src, sha->src_len,
  896. CCP_SHA_BLOCKSIZE, DMA_TO_DEVICE);
  897. if (ret)
  898. goto e_ctx;
  899. while (src.sg_wa.bytes_left) {
  900. ccp_prepare_data(&src, NULL, &op, CCP_SHA_BLOCKSIZE, false);
  901. if (sha->final && !src.sg_wa.bytes_left)
  902. op.eom = 1;
  903. ret = cmd_q->ccp->vdata->perform->perform_sha(&op);
  904. if (ret) {
  905. cmd->engine_error = cmd_q->cmd_error;
  906. goto e_data;
  907. }
  908. ccp_process_data(&src, NULL, &op);
  909. }
  910. /* Retrieve the SHA context - convert from LE to BE using
  911. * 32-byte (256-bit) byteswapping to BE
  912. */
  913. ret = ccp_copy_from_ksb(cmd_q, &ctx, op.jobid, op.ksb_ctx,
  914. CCP_PASSTHRU_BYTESWAP_256BIT);
  915. if (ret) {
  916. cmd->engine_error = cmd_q->cmd_error;
  917. goto e_data;
  918. }
  919. ccp_get_dm_area(&ctx, 0, sha->ctx, 0, sha->ctx_len);
  920. if (sha->final && sha->opad) {
  921. /* HMAC operation, recursively perform final SHA */
  922. struct ccp_cmd hmac_cmd;
  923. struct scatterlist sg;
  924. u64 block_size, digest_size;
  925. u8 *hmac_buf;
  926. switch (sha->type) {
  927. case CCP_SHA_TYPE_1:
  928. block_size = SHA1_BLOCK_SIZE;
  929. digest_size = SHA1_DIGEST_SIZE;
  930. break;
  931. case CCP_SHA_TYPE_224:
  932. block_size = SHA224_BLOCK_SIZE;
  933. digest_size = SHA224_DIGEST_SIZE;
  934. break;
  935. case CCP_SHA_TYPE_256:
  936. block_size = SHA256_BLOCK_SIZE;
  937. digest_size = SHA256_DIGEST_SIZE;
  938. break;
  939. default:
  940. ret = -EINVAL;
  941. goto e_data;
  942. }
  943. if (sha->opad_len != block_size) {
  944. ret = -EINVAL;
  945. goto e_data;
  946. }
  947. hmac_buf = kmalloc(block_size + digest_size, GFP_KERNEL);
  948. if (!hmac_buf) {
  949. ret = -ENOMEM;
  950. goto e_data;
  951. }
  952. sg_init_one(&sg, hmac_buf, block_size + digest_size);
  953. scatterwalk_map_and_copy(hmac_buf, sha->opad, 0, block_size, 0);
  954. memcpy(hmac_buf + block_size, ctx.address, digest_size);
  955. memset(&hmac_cmd, 0, sizeof(hmac_cmd));
  956. hmac_cmd.engine = CCP_ENGINE_SHA;
  957. hmac_cmd.u.sha.type = sha->type;
  958. hmac_cmd.u.sha.ctx = sha->ctx;
  959. hmac_cmd.u.sha.ctx_len = sha->ctx_len;
  960. hmac_cmd.u.sha.src = &sg;
  961. hmac_cmd.u.sha.src_len = block_size + digest_size;
  962. hmac_cmd.u.sha.opad = NULL;
  963. hmac_cmd.u.sha.opad_len = 0;
  964. hmac_cmd.u.sha.first = 1;
  965. hmac_cmd.u.sha.final = 1;
  966. hmac_cmd.u.sha.msg_bits = (block_size + digest_size) << 3;
  967. ret = ccp_run_sha_cmd(cmd_q, &hmac_cmd);
  968. if (ret)
  969. cmd->engine_error = hmac_cmd.engine_error;
  970. kfree(hmac_buf);
  971. }
  972. e_data:
  973. ccp_free_data(&src, cmd_q);
  974. e_ctx:
  975. ccp_dm_free(&ctx);
  976. return ret;
  977. }
  978. static int ccp_run_rsa_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
  979. {
  980. struct ccp_rsa_engine *rsa = &cmd->u.rsa;
  981. struct ccp_dm_workarea exp, src;
  982. struct ccp_data dst;
  983. struct ccp_op op;
  984. unsigned int ksb_count, i_len, o_len;
  985. int ret;
  986. if (rsa->key_size > CCP_RSA_MAX_WIDTH)
  987. return -EINVAL;
  988. if (!rsa->exp || !rsa->mod || !rsa->src || !rsa->dst)
  989. return -EINVAL;
  990. /* The RSA modulus must precede the message being acted upon, so
  991. * it must be copied to a DMA area where the message and the
  992. * modulus can be concatenated. Therefore the input buffer
  993. * length required is twice the output buffer length (which
  994. * must be a multiple of 256-bits).
  995. */
  996. o_len = ((rsa->key_size + 255) / 256) * 32;
  997. i_len = o_len * 2;
  998. ksb_count = o_len / CCP_KSB_BYTES;
  999. memset(&op, 0, sizeof(op));
  1000. op.cmd_q = cmd_q;
  1001. op.jobid = ccp_gen_jobid(cmd_q->ccp);
  1002. op.ksb_key = ccp_alloc_ksb(cmd_q->ccp, ksb_count);
  1003. if (!op.ksb_key)
  1004. return -EIO;
  1005. /* The RSA exponent may span multiple (32-byte) KSB entries and must
  1006. * be in little endian format. Reverse copy each 32-byte chunk
  1007. * of the exponent (En chunk to E0 chunk, E(n-1) chunk to E1 chunk)
  1008. * and each byte within that chunk and do not perform any byte swap
  1009. * operations on the passthru operation.
  1010. */
  1011. ret = ccp_init_dm_workarea(&exp, cmd_q, o_len, DMA_TO_DEVICE);
  1012. if (ret)
  1013. goto e_ksb;
  1014. ret = ccp_reverse_set_dm_area(&exp, rsa->exp, rsa->exp_len,
  1015. CCP_KSB_BYTES, false);
  1016. if (ret)
  1017. goto e_exp;
  1018. ret = ccp_copy_to_ksb(cmd_q, &exp, op.jobid, op.ksb_key,
  1019. CCP_PASSTHRU_BYTESWAP_NOOP);
  1020. if (ret) {
  1021. cmd->engine_error = cmd_q->cmd_error;
  1022. goto e_exp;
  1023. }
  1024. /* Concatenate the modulus and the message. Both the modulus and
  1025. * the operands must be in little endian format. Since the input
  1026. * is in big endian format it must be converted.
  1027. */
  1028. ret = ccp_init_dm_workarea(&src, cmd_q, i_len, DMA_TO_DEVICE);
  1029. if (ret)
  1030. goto e_exp;
  1031. ret = ccp_reverse_set_dm_area(&src, rsa->mod, rsa->mod_len,
  1032. CCP_KSB_BYTES, false);
  1033. if (ret)
  1034. goto e_src;
  1035. src.address += o_len; /* Adjust the address for the copy operation */
  1036. ret = ccp_reverse_set_dm_area(&src, rsa->src, rsa->src_len,
  1037. CCP_KSB_BYTES, false);
  1038. if (ret)
  1039. goto e_src;
  1040. src.address -= o_len; /* Reset the address to original value */
  1041. /* Prepare the output area for the operation */
  1042. ret = ccp_init_data(&dst, cmd_q, rsa->dst, rsa->mod_len,
  1043. o_len, DMA_FROM_DEVICE);
  1044. if (ret)
  1045. goto e_src;
  1046. op.soc = 1;
  1047. op.src.u.dma.address = src.dma.address;
  1048. op.src.u.dma.offset = 0;
  1049. op.src.u.dma.length = i_len;
  1050. op.dst.u.dma.address = dst.dm_wa.dma.address;
  1051. op.dst.u.dma.offset = 0;
  1052. op.dst.u.dma.length = o_len;
  1053. op.u.rsa.mod_size = rsa->key_size;
  1054. op.u.rsa.input_len = i_len;
  1055. ret = cmd_q->ccp->vdata->perform->perform_rsa(&op);
  1056. if (ret) {
  1057. cmd->engine_error = cmd_q->cmd_error;
  1058. goto e_dst;
  1059. }
  1060. ccp_reverse_get_dm_area(&dst.dm_wa, rsa->dst, rsa->mod_len);
  1061. e_dst:
  1062. ccp_free_data(&dst, cmd_q);
  1063. e_src:
  1064. ccp_dm_free(&src);
  1065. e_exp:
  1066. ccp_dm_free(&exp);
  1067. e_ksb:
  1068. ccp_free_ksb(cmd_q->ccp, op.ksb_key, ksb_count);
  1069. return ret;
  1070. }
  1071. static int ccp_run_passthru_cmd(struct ccp_cmd_queue *cmd_q,
  1072. struct ccp_cmd *cmd)
  1073. {
  1074. struct ccp_passthru_engine *pt = &cmd->u.passthru;
  1075. struct ccp_dm_workarea mask;
  1076. struct ccp_data src, dst;
  1077. struct ccp_op op;
  1078. bool in_place = false;
  1079. unsigned int i;
  1080. int ret;
  1081. if (!pt->final && (pt->src_len & (CCP_PASSTHRU_BLOCKSIZE - 1)))
  1082. return -EINVAL;
  1083. if (!pt->src || !pt->dst)
  1084. return -EINVAL;
  1085. if (pt->bit_mod != CCP_PASSTHRU_BITWISE_NOOP) {
  1086. if (pt->mask_len != CCP_PASSTHRU_MASKSIZE)
  1087. return -EINVAL;
  1088. if (!pt->mask)
  1089. return -EINVAL;
  1090. }
  1091. BUILD_BUG_ON(CCP_PASSTHRU_KSB_COUNT != 1);
  1092. memset(&op, 0, sizeof(op));
  1093. op.cmd_q = cmd_q;
  1094. op.jobid = ccp_gen_jobid(cmd_q->ccp);
  1095. if (pt->bit_mod != CCP_PASSTHRU_BITWISE_NOOP) {
  1096. /* Load the mask */
  1097. op.ksb_key = cmd_q->ksb_key;
  1098. ret = ccp_init_dm_workarea(&mask, cmd_q,
  1099. CCP_PASSTHRU_KSB_COUNT *
  1100. CCP_KSB_BYTES,
  1101. DMA_TO_DEVICE);
  1102. if (ret)
  1103. return ret;
  1104. ccp_set_dm_area(&mask, 0, pt->mask, 0, pt->mask_len);
  1105. ret = ccp_copy_to_ksb(cmd_q, &mask, op.jobid, op.ksb_key,
  1106. CCP_PASSTHRU_BYTESWAP_NOOP);
  1107. if (ret) {
  1108. cmd->engine_error = cmd_q->cmd_error;
  1109. goto e_mask;
  1110. }
  1111. }
  1112. /* Prepare the input and output data workareas. For in-place
  1113. * operations we need to set the dma direction to BIDIRECTIONAL
  1114. * and copy the src workarea to the dst workarea.
  1115. */
  1116. if (sg_virt(pt->src) == sg_virt(pt->dst))
  1117. in_place = true;
  1118. ret = ccp_init_data(&src, cmd_q, pt->src, pt->src_len,
  1119. CCP_PASSTHRU_MASKSIZE,
  1120. in_place ? DMA_BIDIRECTIONAL : DMA_TO_DEVICE);
  1121. if (ret)
  1122. goto e_mask;
  1123. if (in_place) {
  1124. dst = src;
  1125. } else {
  1126. ret = ccp_init_data(&dst, cmd_q, pt->dst, pt->src_len,
  1127. CCP_PASSTHRU_MASKSIZE, DMA_FROM_DEVICE);
  1128. if (ret)
  1129. goto e_src;
  1130. }
  1131. /* Send data to the CCP Passthru engine
  1132. * Because the CCP engine works on a single source and destination
  1133. * dma address at a time, each entry in the source scatterlist
  1134. * (after the dma_map_sg call) must be less than or equal to the
  1135. * (remaining) length in the destination scatterlist entry and the
  1136. * length must be a multiple of CCP_PASSTHRU_BLOCKSIZE
  1137. */
  1138. dst.sg_wa.sg_used = 0;
  1139. for (i = 1; i <= src.sg_wa.dma_count; i++) {
  1140. if (!dst.sg_wa.sg ||
  1141. (dst.sg_wa.sg->length < src.sg_wa.sg->length)) {
  1142. ret = -EINVAL;
  1143. goto e_dst;
  1144. }
  1145. if (i == src.sg_wa.dma_count) {
  1146. op.eom = 1;
  1147. op.soc = 1;
  1148. }
  1149. op.src.type = CCP_MEMTYPE_SYSTEM;
  1150. op.src.u.dma.address = sg_dma_address(src.sg_wa.sg);
  1151. op.src.u.dma.offset = 0;
  1152. op.src.u.dma.length = sg_dma_len(src.sg_wa.sg);
  1153. op.dst.type = CCP_MEMTYPE_SYSTEM;
  1154. op.dst.u.dma.address = sg_dma_address(dst.sg_wa.sg);
  1155. op.dst.u.dma.offset = dst.sg_wa.sg_used;
  1156. op.dst.u.dma.length = op.src.u.dma.length;
  1157. ret = cmd_q->ccp->vdata->perform->perform_passthru(&op);
  1158. if (ret) {
  1159. cmd->engine_error = cmd_q->cmd_error;
  1160. goto e_dst;
  1161. }
  1162. dst.sg_wa.sg_used += src.sg_wa.sg->length;
  1163. if (dst.sg_wa.sg_used == dst.sg_wa.sg->length) {
  1164. dst.sg_wa.sg = sg_next(dst.sg_wa.sg);
  1165. dst.sg_wa.sg_used = 0;
  1166. }
  1167. src.sg_wa.sg = sg_next(src.sg_wa.sg);
  1168. }
  1169. e_dst:
  1170. if (!in_place)
  1171. ccp_free_data(&dst, cmd_q);
  1172. e_src:
  1173. ccp_free_data(&src, cmd_q);
  1174. e_mask:
  1175. if (pt->bit_mod != CCP_PASSTHRU_BITWISE_NOOP)
  1176. ccp_dm_free(&mask);
  1177. return ret;
  1178. }
  1179. static int ccp_run_passthru_nomap_cmd(struct ccp_cmd_queue *cmd_q,
  1180. struct ccp_cmd *cmd)
  1181. {
  1182. struct ccp_passthru_nomap_engine *pt = &cmd->u.passthru_nomap;
  1183. struct ccp_dm_workarea mask;
  1184. struct ccp_op op;
  1185. int ret;
  1186. if (!pt->final && (pt->src_len & (CCP_PASSTHRU_BLOCKSIZE - 1)))
  1187. return -EINVAL;
  1188. if (!pt->src_dma || !pt->dst_dma)
  1189. return -EINVAL;
  1190. if (pt->bit_mod != CCP_PASSTHRU_BITWISE_NOOP) {
  1191. if (pt->mask_len != CCP_PASSTHRU_MASKSIZE)
  1192. return -EINVAL;
  1193. if (!pt->mask)
  1194. return -EINVAL;
  1195. }
  1196. BUILD_BUG_ON(CCP_PASSTHRU_KSB_COUNT != 1);
  1197. memset(&op, 0, sizeof(op));
  1198. op.cmd_q = cmd_q;
  1199. op.jobid = ccp_gen_jobid(cmd_q->ccp);
  1200. if (pt->bit_mod != CCP_PASSTHRU_BITWISE_NOOP) {
  1201. /* Load the mask */
  1202. op.ksb_key = cmd_q->ksb_key;
  1203. mask.length = pt->mask_len;
  1204. mask.dma.address = pt->mask;
  1205. mask.dma.length = pt->mask_len;
  1206. ret = ccp_copy_to_ksb(cmd_q, &mask, op.jobid, op.ksb_key,
  1207. CCP_PASSTHRU_BYTESWAP_NOOP);
  1208. if (ret) {
  1209. cmd->engine_error = cmd_q->cmd_error;
  1210. return ret;
  1211. }
  1212. }
  1213. /* Send data to the CCP Passthru engine */
  1214. op.eom = 1;
  1215. op.soc = 1;
  1216. op.src.type = CCP_MEMTYPE_SYSTEM;
  1217. op.src.u.dma.address = pt->src_dma;
  1218. op.src.u.dma.offset = 0;
  1219. op.src.u.dma.length = pt->src_len;
  1220. op.dst.type = CCP_MEMTYPE_SYSTEM;
  1221. op.dst.u.dma.address = pt->dst_dma;
  1222. op.dst.u.dma.offset = 0;
  1223. op.dst.u.dma.length = pt->src_len;
  1224. ret = cmd_q->ccp->vdata->perform->perform_passthru(&op);
  1225. if (ret)
  1226. cmd->engine_error = cmd_q->cmd_error;
  1227. return ret;
  1228. }
  1229. static int ccp_run_ecc_mm_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
  1230. {
  1231. struct ccp_ecc_engine *ecc = &cmd->u.ecc;
  1232. struct ccp_dm_workarea src, dst;
  1233. struct ccp_op op;
  1234. int ret;
  1235. u8 *save;
  1236. if (!ecc->u.mm.operand_1 ||
  1237. (ecc->u.mm.operand_1_len > CCP_ECC_MODULUS_BYTES))
  1238. return -EINVAL;
  1239. if (ecc->function != CCP_ECC_FUNCTION_MINV_384BIT)
  1240. if (!ecc->u.mm.operand_2 ||
  1241. (ecc->u.mm.operand_2_len > CCP_ECC_MODULUS_BYTES))
  1242. return -EINVAL;
  1243. if (!ecc->u.mm.result ||
  1244. (ecc->u.mm.result_len < CCP_ECC_MODULUS_BYTES))
  1245. return -EINVAL;
  1246. memset(&op, 0, sizeof(op));
  1247. op.cmd_q = cmd_q;
  1248. op.jobid = ccp_gen_jobid(cmd_q->ccp);
  1249. /* Concatenate the modulus and the operands. Both the modulus and
  1250. * the operands must be in little endian format. Since the input
  1251. * is in big endian format it must be converted and placed in a
  1252. * fixed length buffer.
  1253. */
  1254. ret = ccp_init_dm_workarea(&src, cmd_q, CCP_ECC_SRC_BUF_SIZE,
  1255. DMA_TO_DEVICE);
  1256. if (ret)
  1257. return ret;
  1258. /* Save the workarea address since it is updated in order to perform
  1259. * the concatenation
  1260. */
  1261. save = src.address;
  1262. /* Copy the ECC modulus */
  1263. ret = ccp_reverse_set_dm_area(&src, ecc->mod, ecc->mod_len,
  1264. CCP_ECC_OPERAND_SIZE, false);
  1265. if (ret)
  1266. goto e_src;
  1267. src.address += CCP_ECC_OPERAND_SIZE;
  1268. /* Copy the first operand */
  1269. ret = ccp_reverse_set_dm_area(&src, ecc->u.mm.operand_1,
  1270. ecc->u.mm.operand_1_len,
  1271. CCP_ECC_OPERAND_SIZE, false);
  1272. if (ret)
  1273. goto e_src;
  1274. src.address += CCP_ECC_OPERAND_SIZE;
  1275. if (ecc->function != CCP_ECC_FUNCTION_MINV_384BIT) {
  1276. /* Copy the second operand */
  1277. ret = ccp_reverse_set_dm_area(&src, ecc->u.mm.operand_2,
  1278. ecc->u.mm.operand_2_len,
  1279. CCP_ECC_OPERAND_SIZE, false);
  1280. if (ret)
  1281. goto e_src;
  1282. src.address += CCP_ECC_OPERAND_SIZE;
  1283. }
  1284. /* Restore the workarea address */
  1285. src.address = save;
  1286. /* Prepare the output area for the operation */
  1287. ret = ccp_init_dm_workarea(&dst, cmd_q, CCP_ECC_DST_BUF_SIZE,
  1288. DMA_FROM_DEVICE);
  1289. if (ret)
  1290. goto e_src;
  1291. op.soc = 1;
  1292. op.src.u.dma.address = src.dma.address;
  1293. op.src.u.dma.offset = 0;
  1294. op.src.u.dma.length = src.length;
  1295. op.dst.u.dma.address = dst.dma.address;
  1296. op.dst.u.dma.offset = 0;
  1297. op.dst.u.dma.length = dst.length;
  1298. op.u.ecc.function = cmd->u.ecc.function;
  1299. ret = cmd_q->ccp->vdata->perform->perform_ecc(&op);
  1300. if (ret) {
  1301. cmd->engine_error = cmd_q->cmd_error;
  1302. goto e_dst;
  1303. }
  1304. ecc->ecc_result = le16_to_cpup(
  1305. (const __le16 *)(dst.address + CCP_ECC_RESULT_OFFSET));
  1306. if (!(ecc->ecc_result & CCP_ECC_RESULT_SUCCESS)) {
  1307. ret = -EIO;
  1308. goto e_dst;
  1309. }
  1310. /* Save the ECC result */
  1311. ccp_reverse_get_dm_area(&dst, ecc->u.mm.result, CCP_ECC_MODULUS_BYTES);
  1312. e_dst:
  1313. ccp_dm_free(&dst);
  1314. e_src:
  1315. ccp_dm_free(&src);
  1316. return ret;
  1317. }
  1318. static int ccp_run_ecc_pm_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
  1319. {
  1320. struct ccp_ecc_engine *ecc = &cmd->u.ecc;
  1321. struct ccp_dm_workarea src, dst;
  1322. struct ccp_op op;
  1323. int ret;
  1324. u8 *save;
  1325. if (!ecc->u.pm.point_1.x ||
  1326. (ecc->u.pm.point_1.x_len > CCP_ECC_MODULUS_BYTES) ||
  1327. !ecc->u.pm.point_1.y ||
  1328. (ecc->u.pm.point_1.y_len > CCP_ECC_MODULUS_BYTES))
  1329. return -EINVAL;
  1330. if (ecc->function == CCP_ECC_FUNCTION_PADD_384BIT) {
  1331. if (!ecc->u.pm.point_2.x ||
  1332. (ecc->u.pm.point_2.x_len > CCP_ECC_MODULUS_BYTES) ||
  1333. !ecc->u.pm.point_2.y ||
  1334. (ecc->u.pm.point_2.y_len > CCP_ECC_MODULUS_BYTES))
  1335. return -EINVAL;
  1336. } else {
  1337. if (!ecc->u.pm.domain_a ||
  1338. (ecc->u.pm.domain_a_len > CCP_ECC_MODULUS_BYTES))
  1339. return -EINVAL;
  1340. if (ecc->function == CCP_ECC_FUNCTION_PMUL_384BIT)
  1341. if (!ecc->u.pm.scalar ||
  1342. (ecc->u.pm.scalar_len > CCP_ECC_MODULUS_BYTES))
  1343. return -EINVAL;
  1344. }
  1345. if (!ecc->u.pm.result.x ||
  1346. (ecc->u.pm.result.x_len < CCP_ECC_MODULUS_BYTES) ||
  1347. !ecc->u.pm.result.y ||
  1348. (ecc->u.pm.result.y_len < CCP_ECC_MODULUS_BYTES))
  1349. return -EINVAL;
  1350. memset(&op, 0, sizeof(op));
  1351. op.cmd_q = cmd_q;
  1352. op.jobid = ccp_gen_jobid(cmd_q->ccp);
  1353. /* Concatenate the modulus and the operands. Both the modulus and
  1354. * the operands must be in little endian format. Since the input
  1355. * is in big endian format it must be converted and placed in a
  1356. * fixed length buffer.
  1357. */
  1358. ret = ccp_init_dm_workarea(&src, cmd_q, CCP_ECC_SRC_BUF_SIZE,
  1359. DMA_TO_DEVICE);
  1360. if (ret)
  1361. return ret;
  1362. /* Save the workarea address since it is updated in order to perform
  1363. * the concatenation
  1364. */
  1365. save = src.address;
  1366. /* Copy the ECC modulus */
  1367. ret = ccp_reverse_set_dm_area(&src, ecc->mod, ecc->mod_len,
  1368. CCP_ECC_OPERAND_SIZE, false);
  1369. if (ret)
  1370. goto e_src;
  1371. src.address += CCP_ECC_OPERAND_SIZE;
  1372. /* Copy the first point X and Y coordinate */
  1373. ret = ccp_reverse_set_dm_area(&src, ecc->u.pm.point_1.x,
  1374. ecc->u.pm.point_1.x_len,
  1375. CCP_ECC_OPERAND_SIZE, false);
  1376. if (ret)
  1377. goto e_src;
  1378. src.address += CCP_ECC_OPERAND_SIZE;
  1379. ret = ccp_reverse_set_dm_area(&src, ecc->u.pm.point_1.y,
  1380. ecc->u.pm.point_1.y_len,
  1381. CCP_ECC_OPERAND_SIZE, false);
  1382. if (ret)
  1383. goto e_src;
  1384. src.address += CCP_ECC_OPERAND_SIZE;
  1385. /* Set the first point Z coordianate to 1 */
  1386. *src.address = 0x01;
  1387. src.address += CCP_ECC_OPERAND_SIZE;
  1388. if (ecc->function == CCP_ECC_FUNCTION_PADD_384BIT) {
  1389. /* Copy the second point X and Y coordinate */
  1390. ret = ccp_reverse_set_dm_area(&src, ecc->u.pm.point_2.x,
  1391. ecc->u.pm.point_2.x_len,
  1392. CCP_ECC_OPERAND_SIZE, false);
  1393. if (ret)
  1394. goto e_src;
  1395. src.address += CCP_ECC_OPERAND_SIZE;
  1396. ret = ccp_reverse_set_dm_area(&src, ecc->u.pm.point_2.y,
  1397. ecc->u.pm.point_2.y_len,
  1398. CCP_ECC_OPERAND_SIZE, false);
  1399. if (ret)
  1400. goto e_src;
  1401. src.address += CCP_ECC_OPERAND_SIZE;
  1402. /* Set the second point Z coordianate to 1 */
  1403. *src.address = 0x01;
  1404. src.address += CCP_ECC_OPERAND_SIZE;
  1405. } else {
  1406. /* Copy the Domain "a" parameter */
  1407. ret = ccp_reverse_set_dm_area(&src, ecc->u.pm.domain_a,
  1408. ecc->u.pm.domain_a_len,
  1409. CCP_ECC_OPERAND_SIZE, false);
  1410. if (ret)
  1411. goto e_src;
  1412. src.address += CCP_ECC_OPERAND_SIZE;
  1413. if (ecc->function == CCP_ECC_FUNCTION_PMUL_384BIT) {
  1414. /* Copy the scalar value */
  1415. ret = ccp_reverse_set_dm_area(&src, ecc->u.pm.scalar,
  1416. ecc->u.pm.scalar_len,
  1417. CCP_ECC_OPERAND_SIZE,
  1418. false);
  1419. if (ret)
  1420. goto e_src;
  1421. src.address += CCP_ECC_OPERAND_SIZE;
  1422. }
  1423. }
  1424. /* Restore the workarea address */
  1425. src.address = save;
  1426. /* Prepare the output area for the operation */
  1427. ret = ccp_init_dm_workarea(&dst, cmd_q, CCP_ECC_DST_BUF_SIZE,
  1428. DMA_FROM_DEVICE);
  1429. if (ret)
  1430. goto e_src;
  1431. op.soc = 1;
  1432. op.src.u.dma.address = src.dma.address;
  1433. op.src.u.dma.offset = 0;
  1434. op.src.u.dma.length = src.length;
  1435. op.dst.u.dma.address = dst.dma.address;
  1436. op.dst.u.dma.offset = 0;
  1437. op.dst.u.dma.length = dst.length;
  1438. op.u.ecc.function = cmd->u.ecc.function;
  1439. ret = cmd_q->ccp->vdata->perform->perform_ecc(&op);
  1440. if (ret) {
  1441. cmd->engine_error = cmd_q->cmd_error;
  1442. goto e_dst;
  1443. }
  1444. ecc->ecc_result = le16_to_cpup(
  1445. (const __le16 *)(dst.address + CCP_ECC_RESULT_OFFSET));
  1446. if (!(ecc->ecc_result & CCP_ECC_RESULT_SUCCESS)) {
  1447. ret = -EIO;
  1448. goto e_dst;
  1449. }
  1450. /* Save the workarea address since it is updated as we walk through
  1451. * to copy the point math result
  1452. */
  1453. save = dst.address;
  1454. /* Save the ECC result X and Y coordinates */
  1455. ccp_reverse_get_dm_area(&dst, ecc->u.pm.result.x,
  1456. CCP_ECC_MODULUS_BYTES);
  1457. dst.address += CCP_ECC_OUTPUT_SIZE;
  1458. ccp_reverse_get_dm_area(&dst, ecc->u.pm.result.y,
  1459. CCP_ECC_MODULUS_BYTES);
  1460. dst.address += CCP_ECC_OUTPUT_SIZE;
  1461. /* Restore the workarea address */
  1462. dst.address = save;
  1463. e_dst:
  1464. ccp_dm_free(&dst);
  1465. e_src:
  1466. ccp_dm_free(&src);
  1467. return ret;
  1468. }
  1469. static int ccp_run_ecc_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
  1470. {
  1471. struct ccp_ecc_engine *ecc = &cmd->u.ecc;
  1472. ecc->ecc_result = 0;
  1473. if (!ecc->mod ||
  1474. (ecc->mod_len > CCP_ECC_MODULUS_BYTES))
  1475. return -EINVAL;
  1476. switch (ecc->function) {
  1477. case CCP_ECC_FUNCTION_MMUL_384BIT:
  1478. case CCP_ECC_FUNCTION_MADD_384BIT:
  1479. case CCP_ECC_FUNCTION_MINV_384BIT:
  1480. return ccp_run_ecc_mm_cmd(cmd_q, cmd);
  1481. case CCP_ECC_FUNCTION_PADD_384BIT:
  1482. case CCP_ECC_FUNCTION_PMUL_384BIT:
  1483. case CCP_ECC_FUNCTION_PDBL_384BIT:
  1484. return ccp_run_ecc_pm_cmd(cmd_q, cmd);
  1485. default:
  1486. return -EINVAL;
  1487. }
  1488. }
  1489. int ccp_run_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
  1490. {
  1491. int ret;
  1492. cmd->engine_error = 0;
  1493. cmd_q->cmd_error = 0;
  1494. cmd_q->int_rcvd = 0;
  1495. cmd_q->free_slots = CMD_Q_DEPTH(ioread32(cmd_q->reg_status));
  1496. switch (cmd->engine) {
  1497. case CCP_ENGINE_AES:
  1498. ret = ccp_run_aes_cmd(cmd_q, cmd);
  1499. break;
  1500. case CCP_ENGINE_XTS_AES_128:
  1501. ret = ccp_run_xts_aes_cmd(cmd_q, cmd);
  1502. break;
  1503. case CCP_ENGINE_SHA:
  1504. ret = ccp_run_sha_cmd(cmd_q, cmd);
  1505. break;
  1506. case CCP_ENGINE_RSA:
  1507. ret = ccp_run_rsa_cmd(cmd_q, cmd);
  1508. break;
  1509. case CCP_ENGINE_PASSTHRU:
  1510. if (cmd->flags & CCP_CMD_PASSTHRU_NO_DMA_MAP)
  1511. ret = ccp_run_passthru_nomap_cmd(cmd_q, cmd);
  1512. else
  1513. ret = ccp_run_passthru_cmd(cmd_q, cmd);
  1514. break;
  1515. case CCP_ENGINE_ECC:
  1516. ret = ccp_run_ecc_cmd(cmd_q, cmd);
  1517. break;
  1518. default:
  1519. ret = -EINVAL;
  1520. }
  1521. return ret;
  1522. }