ccp-ops.c 49 KB

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