ccp-ops.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775
  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_ecc_mm_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
  1180. {
  1181. struct ccp_ecc_engine *ecc = &cmd->u.ecc;
  1182. struct ccp_dm_workarea src, dst;
  1183. struct ccp_op op;
  1184. int ret;
  1185. u8 *save;
  1186. if (!ecc->u.mm.operand_1 ||
  1187. (ecc->u.mm.operand_1_len > CCP_ECC_MODULUS_BYTES))
  1188. return -EINVAL;
  1189. if (ecc->function != CCP_ECC_FUNCTION_MINV_384BIT)
  1190. if (!ecc->u.mm.operand_2 ||
  1191. (ecc->u.mm.operand_2_len > CCP_ECC_MODULUS_BYTES))
  1192. return -EINVAL;
  1193. if (!ecc->u.mm.result ||
  1194. (ecc->u.mm.result_len < CCP_ECC_MODULUS_BYTES))
  1195. return -EINVAL;
  1196. memset(&op, 0, sizeof(op));
  1197. op.cmd_q = cmd_q;
  1198. op.jobid = ccp_gen_jobid(cmd_q->ccp);
  1199. /* Concatenate the modulus and the operands. Both the modulus and
  1200. * the operands must be in little endian format. Since the input
  1201. * is in big endian format it must be converted and placed in a
  1202. * fixed length buffer.
  1203. */
  1204. ret = ccp_init_dm_workarea(&src, cmd_q, CCP_ECC_SRC_BUF_SIZE,
  1205. DMA_TO_DEVICE);
  1206. if (ret)
  1207. return ret;
  1208. /* Save the workarea address since it is updated in order to perform
  1209. * the concatenation
  1210. */
  1211. save = src.address;
  1212. /* Copy the ECC modulus */
  1213. ret = ccp_reverse_set_dm_area(&src, ecc->mod, ecc->mod_len,
  1214. CCP_ECC_OPERAND_SIZE, false);
  1215. if (ret)
  1216. goto e_src;
  1217. src.address += CCP_ECC_OPERAND_SIZE;
  1218. /* Copy the first operand */
  1219. ret = ccp_reverse_set_dm_area(&src, ecc->u.mm.operand_1,
  1220. ecc->u.mm.operand_1_len,
  1221. CCP_ECC_OPERAND_SIZE, false);
  1222. if (ret)
  1223. goto e_src;
  1224. src.address += CCP_ECC_OPERAND_SIZE;
  1225. if (ecc->function != CCP_ECC_FUNCTION_MINV_384BIT) {
  1226. /* Copy the second operand */
  1227. ret = ccp_reverse_set_dm_area(&src, ecc->u.mm.operand_2,
  1228. ecc->u.mm.operand_2_len,
  1229. CCP_ECC_OPERAND_SIZE, false);
  1230. if (ret)
  1231. goto e_src;
  1232. src.address += CCP_ECC_OPERAND_SIZE;
  1233. }
  1234. /* Restore the workarea address */
  1235. src.address = save;
  1236. /* Prepare the output area for the operation */
  1237. ret = ccp_init_dm_workarea(&dst, cmd_q, CCP_ECC_DST_BUF_SIZE,
  1238. DMA_FROM_DEVICE);
  1239. if (ret)
  1240. goto e_src;
  1241. op.soc = 1;
  1242. op.src.u.dma.address = src.dma.address;
  1243. op.src.u.dma.offset = 0;
  1244. op.src.u.dma.length = src.length;
  1245. op.dst.u.dma.address = dst.dma.address;
  1246. op.dst.u.dma.offset = 0;
  1247. op.dst.u.dma.length = dst.length;
  1248. op.u.ecc.function = cmd->u.ecc.function;
  1249. ret = cmd_q->ccp->vdata->perform->perform_ecc(&op);
  1250. if (ret) {
  1251. cmd->engine_error = cmd_q->cmd_error;
  1252. goto e_dst;
  1253. }
  1254. ecc->ecc_result = le16_to_cpup(
  1255. (const __le16 *)(dst.address + CCP_ECC_RESULT_OFFSET));
  1256. if (!(ecc->ecc_result & CCP_ECC_RESULT_SUCCESS)) {
  1257. ret = -EIO;
  1258. goto e_dst;
  1259. }
  1260. /* Save the ECC result */
  1261. ccp_reverse_get_dm_area(&dst, ecc->u.mm.result, CCP_ECC_MODULUS_BYTES);
  1262. e_dst:
  1263. ccp_dm_free(&dst);
  1264. e_src:
  1265. ccp_dm_free(&src);
  1266. return ret;
  1267. }
  1268. static int ccp_run_ecc_pm_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
  1269. {
  1270. struct ccp_ecc_engine *ecc = &cmd->u.ecc;
  1271. struct ccp_dm_workarea src, dst;
  1272. struct ccp_op op;
  1273. int ret;
  1274. u8 *save;
  1275. if (!ecc->u.pm.point_1.x ||
  1276. (ecc->u.pm.point_1.x_len > CCP_ECC_MODULUS_BYTES) ||
  1277. !ecc->u.pm.point_1.y ||
  1278. (ecc->u.pm.point_1.y_len > CCP_ECC_MODULUS_BYTES))
  1279. return -EINVAL;
  1280. if (ecc->function == CCP_ECC_FUNCTION_PADD_384BIT) {
  1281. if (!ecc->u.pm.point_2.x ||
  1282. (ecc->u.pm.point_2.x_len > CCP_ECC_MODULUS_BYTES) ||
  1283. !ecc->u.pm.point_2.y ||
  1284. (ecc->u.pm.point_2.y_len > CCP_ECC_MODULUS_BYTES))
  1285. return -EINVAL;
  1286. } else {
  1287. if (!ecc->u.pm.domain_a ||
  1288. (ecc->u.pm.domain_a_len > CCP_ECC_MODULUS_BYTES))
  1289. return -EINVAL;
  1290. if (ecc->function == CCP_ECC_FUNCTION_PMUL_384BIT)
  1291. if (!ecc->u.pm.scalar ||
  1292. (ecc->u.pm.scalar_len > CCP_ECC_MODULUS_BYTES))
  1293. return -EINVAL;
  1294. }
  1295. if (!ecc->u.pm.result.x ||
  1296. (ecc->u.pm.result.x_len < CCP_ECC_MODULUS_BYTES) ||
  1297. !ecc->u.pm.result.y ||
  1298. (ecc->u.pm.result.y_len < CCP_ECC_MODULUS_BYTES))
  1299. return -EINVAL;
  1300. memset(&op, 0, sizeof(op));
  1301. op.cmd_q = cmd_q;
  1302. op.jobid = ccp_gen_jobid(cmd_q->ccp);
  1303. /* Concatenate the modulus and the operands. Both the modulus and
  1304. * the operands must be in little endian format. Since the input
  1305. * is in big endian format it must be converted and placed in a
  1306. * fixed length buffer.
  1307. */
  1308. ret = ccp_init_dm_workarea(&src, cmd_q, CCP_ECC_SRC_BUF_SIZE,
  1309. DMA_TO_DEVICE);
  1310. if (ret)
  1311. return ret;
  1312. /* Save the workarea address since it is updated in order to perform
  1313. * the concatenation
  1314. */
  1315. save = src.address;
  1316. /* Copy the ECC modulus */
  1317. ret = ccp_reverse_set_dm_area(&src, ecc->mod, ecc->mod_len,
  1318. CCP_ECC_OPERAND_SIZE, false);
  1319. if (ret)
  1320. goto e_src;
  1321. src.address += CCP_ECC_OPERAND_SIZE;
  1322. /* Copy the first point X and Y coordinate */
  1323. ret = ccp_reverse_set_dm_area(&src, ecc->u.pm.point_1.x,
  1324. ecc->u.pm.point_1.x_len,
  1325. CCP_ECC_OPERAND_SIZE, false);
  1326. if (ret)
  1327. goto e_src;
  1328. src.address += CCP_ECC_OPERAND_SIZE;
  1329. ret = ccp_reverse_set_dm_area(&src, ecc->u.pm.point_1.y,
  1330. ecc->u.pm.point_1.y_len,
  1331. CCP_ECC_OPERAND_SIZE, false);
  1332. if (ret)
  1333. goto e_src;
  1334. src.address += CCP_ECC_OPERAND_SIZE;
  1335. /* Set the first point Z coordianate to 1 */
  1336. *src.address = 0x01;
  1337. src.address += CCP_ECC_OPERAND_SIZE;
  1338. if (ecc->function == CCP_ECC_FUNCTION_PADD_384BIT) {
  1339. /* Copy the second point X and Y coordinate */
  1340. ret = ccp_reverse_set_dm_area(&src, ecc->u.pm.point_2.x,
  1341. ecc->u.pm.point_2.x_len,
  1342. CCP_ECC_OPERAND_SIZE, false);
  1343. if (ret)
  1344. goto e_src;
  1345. src.address += CCP_ECC_OPERAND_SIZE;
  1346. ret = ccp_reverse_set_dm_area(&src, ecc->u.pm.point_2.y,
  1347. ecc->u.pm.point_2.y_len,
  1348. CCP_ECC_OPERAND_SIZE, false);
  1349. if (ret)
  1350. goto e_src;
  1351. src.address += CCP_ECC_OPERAND_SIZE;
  1352. /* Set the second point Z coordianate to 1 */
  1353. *src.address = 0x01;
  1354. src.address += CCP_ECC_OPERAND_SIZE;
  1355. } else {
  1356. /* Copy the Domain "a" parameter */
  1357. ret = ccp_reverse_set_dm_area(&src, ecc->u.pm.domain_a,
  1358. ecc->u.pm.domain_a_len,
  1359. CCP_ECC_OPERAND_SIZE, false);
  1360. if (ret)
  1361. goto e_src;
  1362. src.address += CCP_ECC_OPERAND_SIZE;
  1363. if (ecc->function == CCP_ECC_FUNCTION_PMUL_384BIT) {
  1364. /* Copy the scalar value */
  1365. ret = ccp_reverse_set_dm_area(&src, ecc->u.pm.scalar,
  1366. ecc->u.pm.scalar_len,
  1367. CCP_ECC_OPERAND_SIZE,
  1368. false);
  1369. if (ret)
  1370. goto e_src;
  1371. src.address += CCP_ECC_OPERAND_SIZE;
  1372. }
  1373. }
  1374. /* Restore the workarea address */
  1375. src.address = save;
  1376. /* Prepare the output area for the operation */
  1377. ret = ccp_init_dm_workarea(&dst, cmd_q, CCP_ECC_DST_BUF_SIZE,
  1378. DMA_FROM_DEVICE);
  1379. if (ret)
  1380. goto e_src;
  1381. op.soc = 1;
  1382. op.src.u.dma.address = src.dma.address;
  1383. op.src.u.dma.offset = 0;
  1384. op.src.u.dma.length = src.length;
  1385. op.dst.u.dma.address = dst.dma.address;
  1386. op.dst.u.dma.offset = 0;
  1387. op.dst.u.dma.length = dst.length;
  1388. op.u.ecc.function = cmd->u.ecc.function;
  1389. ret = cmd_q->ccp->vdata->perform->perform_ecc(&op);
  1390. if (ret) {
  1391. cmd->engine_error = cmd_q->cmd_error;
  1392. goto e_dst;
  1393. }
  1394. ecc->ecc_result = le16_to_cpup(
  1395. (const __le16 *)(dst.address + CCP_ECC_RESULT_OFFSET));
  1396. if (!(ecc->ecc_result & CCP_ECC_RESULT_SUCCESS)) {
  1397. ret = -EIO;
  1398. goto e_dst;
  1399. }
  1400. /* Save the workarea address since it is updated as we walk through
  1401. * to copy the point math result
  1402. */
  1403. save = dst.address;
  1404. /* Save the ECC result X and Y coordinates */
  1405. ccp_reverse_get_dm_area(&dst, ecc->u.pm.result.x,
  1406. CCP_ECC_MODULUS_BYTES);
  1407. dst.address += CCP_ECC_OUTPUT_SIZE;
  1408. ccp_reverse_get_dm_area(&dst, ecc->u.pm.result.y,
  1409. CCP_ECC_MODULUS_BYTES);
  1410. dst.address += CCP_ECC_OUTPUT_SIZE;
  1411. /* Restore the workarea address */
  1412. dst.address = save;
  1413. e_dst:
  1414. ccp_dm_free(&dst);
  1415. e_src:
  1416. ccp_dm_free(&src);
  1417. return ret;
  1418. }
  1419. static int ccp_run_ecc_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
  1420. {
  1421. struct ccp_ecc_engine *ecc = &cmd->u.ecc;
  1422. ecc->ecc_result = 0;
  1423. if (!ecc->mod ||
  1424. (ecc->mod_len > CCP_ECC_MODULUS_BYTES))
  1425. return -EINVAL;
  1426. switch (ecc->function) {
  1427. case CCP_ECC_FUNCTION_MMUL_384BIT:
  1428. case CCP_ECC_FUNCTION_MADD_384BIT:
  1429. case CCP_ECC_FUNCTION_MINV_384BIT:
  1430. return ccp_run_ecc_mm_cmd(cmd_q, cmd);
  1431. case CCP_ECC_FUNCTION_PADD_384BIT:
  1432. case CCP_ECC_FUNCTION_PMUL_384BIT:
  1433. case CCP_ECC_FUNCTION_PDBL_384BIT:
  1434. return ccp_run_ecc_pm_cmd(cmd_q, cmd);
  1435. default:
  1436. return -EINVAL;
  1437. }
  1438. }
  1439. int ccp_run_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
  1440. {
  1441. int ret;
  1442. cmd->engine_error = 0;
  1443. cmd_q->cmd_error = 0;
  1444. cmd_q->int_rcvd = 0;
  1445. cmd_q->free_slots = CMD_Q_DEPTH(ioread32(cmd_q->reg_status));
  1446. switch (cmd->engine) {
  1447. case CCP_ENGINE_AES:
  1448. ret = ccp_run_aes_cmd(cmd_q, cmd);
  1449. break;
  1450. case CCP_ENGINE_XTS_AES_128:
  1451. ret = ccp_run_xts_aes_cmd(cmd_q, cmd);
  1452. break;
  1453. case CCP_ENGINE_SHA:
  1454. ret = ccp_run_sha_cmd(cmd_q, cmd);
  1455. break;
  1456. case CCP_ENGINE_RSA:
  1457. ret = ccp_run_rsa_cmd(cmd_q, cmd);
  1458. break;
  1459. case CCP_ENGINE_PASSTHRU:
  1460. ret = ccp_run_passthru_cmd(cmd_q, cmd);
  1461. break;
  1462. case CCP_ENGINE_ECC:
  1463. ret = ccp_run_ecc_cmd(cmd_q, cmd);
  1464. break;
  1465. default:
  1466. ret = -EINVAL;
  1467. }
  1468. return ret;
  1469. }