ccp-ops.c 52 KB

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