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. }
  618. /* The data operation length will be at least block_size in length
  619. * or the smaller of available sg room remaining for the source or
  620. * the destination
  621. */
  622. op_len = max(op_len, block_size);
  623. /* Unless we have to buffer data, there's no reason to wait */
  624. op->soc = 0;
  625. if (sg_src_len < block_size) {
  626. /* Not enough data in the sg element, so it
  627. * needs to be buffered into a blocksize chunk
  628. */
  629. int cp_len = ccp_fill_queue_buf(src);
  630. op->soc = 1;
  631. op->src.u.dma.address = src->dm_wa.dma.address;
  632. op->src.u.dma.offset = 0;
  633. op->src.u.dma.length = (blocksize_op) ? block_size : cp_len;
  634. } else {
  635. /* Enough data in the sg element, but we need to
  636. * adjust for any previously copied data
  637. */
  638. op->src.u.dma.address = sg_dma_address(src->sg_wa.sg);
  639. op->src.u.dma.offset = src->sg_wa.sg_used;
  640. op->src.u.dma.length = op_len & ~(block_size - 1);
  641. ccp_update_sg_workarea(&src->sg_wa, op->src.u.dma.length);
  642. }
  643. if (dst) {
  644. if (sg_dst_len < block_size) {
  645. /* Not enough room in the sg element or we're on the
  646. * last piece of data (when using padding), so the
  647. * output needs to be buffered into a blocksize chunk
  648. */
  649. op->soc = 1;
  650. op->dst.u.dma.address = dst->dm_wa.dma.address;
  651. op->dst.u.dma.offset = 0;
  652. op->dst.u.dma.length = op->src.u.dma.length;
  653. } else {
  654. /* Enough room in the sg element, but we need to
  655. * adjust for any previously used area
  656. */
  657. op->dst.u.dma.address = sg_dma_address(dst->sg_wa.sg);
  658. op->dst.u.dma.offset = dst->sg_wa.sg_used;
  659. op->dst.u.dma.length = op->src.u.dma.length;
  660. }
  661. }
  662. }
  663. static void ccp_process_data(struct ccp_data *src, struct ccp_data *dst,
  664. struct ccp_op *op)
  665. {
  666. op->init = 0;
  667. if (dst) {
  668. if (op->dst.u.dma.address == dst->dm_wa.dma.address)
  669. ccp_empty_queue_buf(dst);
  670. else
  671. ccp_update_sg_workarea(&dst->sg_wa,
  672. op->dst.u.dma.length);
  673. }
  674. }
  675. static int ccp_copy_to_from_ksb(struct ccp_cmd_queue *cmd_q,
  676. struct ccp_dm_workarea *wa, u32 jobid, u32 ksb,
  677. u32 byte_swap, bool from)
  678. {
  679. struct ccp_op op;
  680. memset(&op, 0, sizeof(op));
  681. op.cmd_q = cmd_q;
  682. op.jobid = jobid;
  683. op.eom = 1;
  684. if (from) {
  685. op.soc = 1;
  686. op.src.type = CCP_MEMTYPE_KSB;
  687. op.src.u.ksb = ksb;
  688. op.dst.type = CCP_MEMTYPE_SYSTEM;
  689. op.dst.u.dma.address = wa->dma.address;
  690. op.dst.u.dma.length = wa->length;
  691. } else {
  692. op.src.type = CCP_MEMTYPE_SYSTEM;
  693. op.src.u.dma.address = wa->dma.address;
  694. op.src.u.dma.length = wa->length;
  695. op.dst.type = CCP_MEMTYPE_KSB;
  696. op.dst.u.ksb = ksb;
  697. }
  698. op.u.passthru.byte_swap = byte_swap;
  699. return ccp_perform_passthru(&op);
  700. }
  701. static int ccp_copy_to_ksb(struct ccp_cmd_queue *cmd_q,
  702. struct ccp_dm_workarea *wa, u32 jobid, u32 ksb,
  703. u32 byte_swap)
  704. {
  705. return ccp_copy_to_from_ksb(cmd_q, wa, jobid, ksb, byte_swap, false);
  706. }
  707. static int ccp_copy_from_ksb(struct ccp_cmd_queue *cmd_q,
  708. struct ccp_dm_workarea *wa, u32 jobid, u32 ksb,
  709. u32 byte_swap)
  710. {
  711. return ccp_copy_to_from_ksb(cmd_q, wa, jobid, ksb, byte_swap, true);
  712. }
  713. static int ccp_run_aes_cmac_cmd(struct ccp_cmd_queue *cmd_q,
  714. struct ccp_cmd *cmd)
  715. {
  716. struct ccp_aes_engine *aes = &cmd->u.aes;
  717. struct ccp_dm_workarea key, ctx;
  718. struct ccp_data src;
  719. struct ccp_op op;
  720. unsigned int dm_offset;
  721. int ret;
  722. if (!((aes->key_len == AES_KEYSIZE_128) ||
  723. (aes->key_len == AES_KEYSIZE_192) ||
  724. (aes->key_len == AES_KEYSIZE_256)))
  725. return -EINVAL;
  726. if (aes->src_len & (AES_BLOCK_SIZE - 1))
  727. return -EINVAL;
  728. if (aes->iv_len != AES_BLOCK_SIZE)
  729. return -EINVAL;
  730. if (!aes->key || !aes->iv || !aes->src)
  731. return -EINVAL;
  732. if (aes->cmac_final) {
  733. if (aes->cmac_key_len != AES_BLOCK_SIZE)
  734. return -EINVAL;
  735. if (!aes->cmac_key)
  736. return -EINVAL;
  737. }
  738. BUILD_BUG_ON(CCP_AES_KEY_KSB_COUNT != 1);
  739. BUILD_BUG_ON(CCP_AES_CTX_KSB_COUNT != 1);
  740. ret = -EIO;
  741. memset(&op, 0, sizeof(op));
  742. op.cmd_q = cmd_q;
  743. op.jobid = ccp_gen_jobid(cmd_q->ccp);
  744. op.ksb_key = cmd_q->ksb_key;
  745. op.ksb_ctx = cmd_q->ksb_ctx;
  746. op.init = 1;
  747. op.u.aes.type = aes->type;
  748. op.u.aes.mode = aes->mode;
  749. op.u.aes.action = aes->action;
  750. /* All supported key sizes fit in a single (32-byte) KSB entry
  751. * and must be in little endian format. Use the 256-bit byte
  752. * swap passthru option to convert from big endian to little
  753. * endian.
  754. */
  755. ret = ccp_init_dm_workarea(&key, cmd_q,
  756. CCP_AES_KEY_KSB_COUNT * CCP_KSB_BYTES,
  757. DMA_TO_DEVICE);
  758. if (ret)
  759. return ret;
  760. dm_offset = CCP_KSB_BYTES - aes->key_len;
  761. ccp_set_dm_area(&key, dm_offset, aes->key, 0, aes->key_len);
  762. ret = ccp_copy_to_ksb(cmd_q, &key, op.jobid, op.ksb_key,
  763. CCP_PASSTHRU_BYTESWAP_256BIT);
  764. if (ret) {
  765. cmd->engine_error = cmd_q->cmd_error;
  766. goto e_key;
  767. }
  768. /* The AES context fits in a single (32-byte) KSB entry and
  769. * must be in little endian format. Use the 256-bit byte swap
  770. * passthru option to convert from big endian to little endian.
  771. */
  772. ret = ccp_init_dm_workarea(&ctx, cmd_q,
  773. CCP_AES_CTX_KSB_COUNT * CCP_KSB_BYTES,
  774. DMA_BIDIRECTIONAL);
  775. if (ret)
  776. goto e_key;
  777. dm_offset = CCP_KSB_BYTES - AES_BLOCK_SIZE;
  778. ccp_set_dm_area(&ctx, dm_offset, aes->iv, 0, aes->iv_len);
  779. ret = ccp_copy_to_ksb(cmd_q, &ctx, op.jobid, op.ksb_ctx,
  780. CCP_PASSTHRU_BYTESWAP_256BIT);
  781. if (ret) {
  782. cmd->engine_error = cmd_q->cmd_error;
  783. goto e_ctx;
  784. }
  785. /* Send data to the CCP AES engine */
  786. ret = ccp_init_data(&src, cmd_q, aes->src, aes->src_len,
  787. AES_BLOCK_SIZE, DMA_TO_DEVICE);
  788. if (ret)
  789. goto e_ctx;
  790. while (src.sg_wa.bytes_left) {
  791. ccp_prepare_data(&src, NULL, &op, AES_BLOCK_SIZE, true);
  792. if (aes->cmac_final && !src.sg_wa.bytes_left) {
  793. op.eom = 1;
  794. /* Push the K1/K2 key to the CCP now */
  795. ret = ccp_copy_from_ksb(cmd_q, &ctx, op.jobid,
  796. op.ksb_ctx,
  797. CCP_PASSTHRU_BYTESWAP_256BIT);
  798. if (ret) {
  799. cmd->engine_error = cmd_q->cmd_error;
  800. goto e_src;
  801. }
  802. ccp_set_dm_area(&ctx, 0, aes->cmac_key, 0,
  803. aes->cmac_key_len);
  804. ret = ccp_copy_to_ksb(cmd_q, &ctx, op.jobid, op.ksb_ctx,
  805. CCP_PASSTHRU_BYTESWAP_256BIT);
  806. if (ret) {
  807. cmd->engine_error = cmd_q->cmd_error;
  808. goto e_src;
  809. }
  810. }
  811. ret = ccp_perform_aes(&op);
  812. if (ret) {
  813. cmd->engine_error = cmd_q->cmd_error;
  814. goto e_src;
  815. }
  816. ccp_process_data(&src, NULL, &op);
  817. }
  818. /* Retrieve the AES context - convert from LE to BE using
  819. * 32-byte (256-bit) byteswapping
  820. */
  821. ret = ccp_copy_from_ksb(cmd_q, &ctx, op.jobid, op.ksb_ctx,
  822. CCP_PASSTHRU_BYTESWAP_256BIT);
  823. if (ret) {
  824. cmd->engine_error = cmd_q->cmd_error;
  825. goto e_src;
  826. }
  827. /* ...but we only need AES_BLOCK_SIZE bytes */
  828. dm_offset = CCP_KSB_BYTES - AES_BLOCK_SIZE;
  829. ccp_get_dm_area(&ctx, dm_offset, aes->iv, 0, aes->iv_len);
  830. e_src:
  831. ccp_free_data(&src, cmd_q);
  832. e_ctx:
  833. ccp_dm_free(&ctx);
  834. e_key:
  835. ccp_dm_free(&key);
  836. return ret;
  837. }
  838. static int ccp_run_aes_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
  839. {
  840. struct ccp_aes_engine *aes = &cmd->u.aes;
  841. struct ccp_dm_workarea key, ctx;
  842. struct ccp_data src, dst;
  843. struct ccp_op op;
  844. unsigned int dm_offset;
  845. bool in_place = false;
  846. int ret;
  847. if (aes->mode == CCP_AES_MODE_CMAC)
  848. return ccp_run_aes_cmac_cmd(cmd_q, cmd);
  849. if (!((aes->key_len == AES_KEYSIZE_128) ||
  850. (aes->key_len == AES_KEYSIZE_192) ||
  851. (aes->key_len == AES_KEYSIZE_256)))
  852. return -EINVAL;
  853. if (((aes->mode == CCP_AES_MODE_ECB) ||
  854. (aes->mode == CCP_AES_MODE_CBC) ||
  855. (aes->mode == CCP_AES_MODE_CFB)) &&
  856. (aes->src_len & (AES_BLOCK_SIZE - 1)))
  857. return -EINVAL;
  858. if (!aes->key || !aes->src || !aes->dst)
  859. return -EINVAL;
  860. if (aes->mode != CCP_AES_MODE_ECB) {
  861. if (aes->iv_len != AES_BLOCK_SIZE)
  862. return -EINVAL;
  863. if (!aes->iv)
  864. return -EINVAL;
  865. }
  866. BUILD_BUG_ON(CCP_AES_KEY_KSB_COUNT != 1);
  867. BUILD_BUG_ON(CCP_AES_CTX_KSB_COUNT != 1);
  868. ret = -EIO;
  869. memset(&op, 0, sizeof(op));
  870. op.cmd_q = cmd_q;
  871. op.jobid = ccp_gen_jobid(cmd_q->ccp);
  872. op.ksb_key = cmd_q->ksb_key;
  873. op.ksb_ctx = cmd_q->ksb_ctx;
  874. op.init = (aes->mode == CCP_AES_MODE_ECB) ? 0 : 1;
  875. op.u.aes.type = aes->type;
  876. op.u.aes.mode = aes->mode;
  877. op.u.aes.action = aes->action;
  878. /* All supported key sizes fit in a single (32-byte) KSB entry
  879. * and must be in little endian format. Use the 256-bit byte
  880. * swap passthru option to convert from big endian to little
  881. * endian.
  882. */
  883. ret = ccp_init_dm_workarea(&key, cmd_q,
  884. CCP_AES_KEY_KSB_COUNT * CCP_KSB_BYTES,
  885. DMA_TO_DEVICE);
  886. if (ret)
  887. return ret;
  888. dm_offset = CCP_KSB_BYTES - aes->key_len;
  889. ccp_set_dm_area(&key, dm_offset, aes->key, 0, aes->key_len);
  890. ret = ccp_copy_to_ksb(cmd_q, &key, op.jobid, op.ksb_key,
  891. CCP_PASSTHRU_BYTESWAP_256BIT);
  892. if (ret) {
  893. cmd->engine_error = cmd_q->cmd_error;
  894. goto e_key;
  895. }
  896. /* The AES context fits in a single (32-byte) KSB entry and
  897. * must be in little endian format. Use the 256-bit byte swap
  898. * passthru option to convert from big endian to little endian.
  899. */
  900. ret = ccp_init_dm_workarea(&ctx, cmd_q,
  901. CCP_AES_CTX_KSB_COUNT * CCP_KSB_BYTES,
  902. DMA_BIDIRECTIONAL);
  903. if (ret)
  904. goto e_key;
  905. if (aes->mode != CCP_AES_MODE_ECB) {
  906. /* Load the AES context - conver to LE */
  907. dm_offset = CCP_KSB_BYTES - AES_BLOCK_SIZE;
  908. ccp_set_dm_area(&ctx, dm_offset, aes->iv, 0, aes->iv_len);
  909. ret = ccp_copy_to_ksb(cmd_q, &ctx, op.jobid, op.ksb_ctx,
  910. CCP_PASSTHRU_BYTESWAP_256BIT);
  911. if (ret) {
  912. cmd->engine_error = cmd_q->cmd_error;
  913. goto e_ctx;
  914. }
  915. }
  916. /* Prepare the input and output data workareas. For in-place
  917. * operations we need to set the dma direction to BIDIRECTIONAL
  918. * and copy the src workarea to the dst workarea.
  919. */
  920. if (sg_virt(aes->src) == sg_virt(aes->dst))
  921. in_place = true;
  922. ret = ccp_init_data(&src, cmd_q, aes->src, aes->src_len,
  923. AES_BLOCK_SIZE,
  924. in_place ? DMA_BIDIRECTIONAL : DMA_TO_DEVICE);
  925. if (ret)
  926. goto e_ctx;
  927. if (in_place) {
  928. dst = src;
  929. } else {
  930. ret = ccp_init_data(&dst, cmd_q, aes->dst, aes->src_len,
  931. AES_BLOCK_SIZE, DMA_FROM_DEVICE);
  932. if (ret)
  933. goto e_src;
  934. }
  935. /* Send data to the CCP AES engine */
  936. while (src.sg_wa.bytes_left) {
  937. ccp_prepare_data(&src, &dst, &op, AES_BLOCK_SIZE, true);
  938. if (!src.sg_wa.bytes_left) {
  939. op.eom = 1;
  940. /* Since we don't retrieve the AES context in ECB
  941. * mode we have to wait for the operation to complete
  942. * on the last piece of data
  943. */
  944. if (aes->mode == CCP_AES_MODE_ECB)
  945. op.soc = 1;
  946. }
  947. ret = ccp_perform_aes(&op);
  948. if (ret) {
  949. cmd->engine_error = cmd_q->cmd_error;
  950. goto e_dst;
  951. }
  952. ccp_process_data(&src, &dst, &op);
  953. }
  954. if (aes->mode != CCP_AES_MODE_ECB) {
  955. /* Retrieve the AES context - convert from LE to BE using
  956. * 32-byte (256-bit) byteswapping
  957. */
  958. ret = ccp_copy_from_ksb(cmd_q, &ctx, op.jobid, op.ksb_ctx,
  959. CCP_PASSTHRU_BYTESWAP_256BIT);
  960. if (ret) {
  961. cmd->engine_error = cmd_q->cmd_error;
  962. goto e_dst;
  963. }
  964. /* ...but we only need AES_BLOCK_SIZE bytes */
  965. dm_offset = CCP_KSB_BYTES - AES_BLOCK_SIZE;
  966. ccp_get_dm_area(&ctx, dm_offset, aes->iv, 0, aes->iv_len);
  967. }
  968. e_dst:
  969. if (!in_place)
  970. ccp_free_data(&dst, cmd_q);
  971. e_src:
  972. ccp_free_data(&src, cmd_q);
  973. e_ctx:
  974. ccp_dm_free(&ctx);
  975. e_key:
  976. ccp_dm_free(&key);
  977. return ret;
  978. }
  979. static int ccp_run_xts_aes_cmd(struct ccp_cmd_queue *cmd_q,
  980. struct ccp_cmd *cmd)
  981. {
  982. struct ccp_xts_aes_engine *xts = &cmd->u.xts;
  983. struct ccp_dm_workarea key, ctx;
  984. struct ccp_data src, dst;
  985. struct ccp_op op;
  986. unsigned int unit_size, dm_offset;
  987. bool in_place = false;
  988. int ret;
  989. switch (xts->unit_size) {
  990. case CCP_XTS_AES_UNIT_SIZE_16:
  991. unit_size = 16;
  992. break;
  993. case CCP_XTS_AES_UNIT_SIZE_512:
  994. unit_size = 512;
  995. break;
  996. case CCP_XTS_AES_UNIT_SIZE_1024:
  997. unit_size = 1024;
  998. break;
  999. case CCP_XTS_AES_UNIT_SIZE_2048:
  1000. unit_size = 2048;
  1001. break;
  1002. case CCP_XTS_AES_UNIT_SIZE_4096:
  1003. unit_size = 4096;
  1004. break;
  1005. default:
  1006. return -EINVAL;
  1007. }
  1008. if (xts->key_len != AES_KEYSIZE_128)
  1009. return -EINVAL;
  1010. if (!xts->final && (xts->src_len & (AES_BLOCK_SIZE - 1)))
  1011. return -EINVAL;
  1012. if (xts->iv_len != AES_BLOCK_SIZE)
  1013. return -EINVAL;
  1014. if (!xts->key || !xts->iv || !xts->src || !xts->dst)
  1015. return -EINVAL;
  1016. BUILD_BUG_ON(CCP_XTS_AES_KEY_KSB_COUNT != 1);
  1017. BUILD_BUG_ON(CCP_XTS_AES_CTX_KSB_COUNT != 1);
  1018. ret = -EIO;
  1019. memset(&op, 0, sizeof(op));
  1020. op.cmd_q = cmd_q;
  1021. op.jobid = ccp_gen_jobid(cmd_q->ccp);
  1022. op.ksb_key = cmd_q->ksb_key;
  1023. op.ksb_ctx = cmd_q->ksb_ctx;
  1024. op.init = 1;
  1025. op.u.xts.action = xts->action;
  1026. op.u.xts.unit_size = xts->unit_size;
  1027. /* All supported key sizes fit in a single (32-byte) KSB entry
  1028. * and must be in little endian format. Use the 256-bit byte
  1029. * swap passthru option to convert from big endian to little
  1030. * endian.
  1031. */
  1032. ret = ccp_init_dm_workarea(&key, cmd_q,
  1033. CCP_XTS_AES_KEY_KSB_COUNT * CCP_KSB_BYTES,
  1034. DMA_TO_DEVICE);
  1035. if (ret)
  1036. return ret;
  1037. dm_offset = CCP_KSB_BYTES - AES_KEYSIZE_128;
  1038. ccp_set_dm_area(&key, dm_offset, xts->key, 0, xts->key_len);
  1039. ccp_set_dm_area(&key, 0, xts->key, dm_offset, xts->key_len);
  1040. ret = ccp_copy_to_ksb(cmd_q, &key, op.jobid, op.ksb_key,
  1041. CCP_PASSTHRU_BYTESWAP_256BIT);
  1042. if (ret) {
  1043. cmd->engine_error = cmd_q->cmd_error;
  1044. goto e_key;
  1045. }
  1046. /* The AES context fits in a single (32-byte) KSB entry and
  1047. * for XTS is already in little endian format so no byte swapping
  1048. * is needed.
  1049. */
  1050. ret = ccp_init_dm_workarea(&ctx, cmd_q,
  1051. CCP_XTS_AES_CTX_KSB_COUNT * CCP_KSB_BYTES,
  1052. DMA_BIDIRECTIONAL);
  1053. if (ret)
  1054. goto e_key;
  1055. ccp_set_dm_area(&ctx, 0, xts->iv, 0, xts->iv_len);
  1056. ret = ccp_copy_to_ksb(cmd_q, &ctx, op.jobid, op.ksb_ctx,
  1057. CCP_PASSTHRU_BYTESWAP_NOOP);
  1058. if (ret) {
  1059. cmd->engine_error = cmd_q->cmd_error;
  1060. goto e_ctx;
  1061. }
  1062. /* Prepare the input and output data workareas. For in-place
  1063. * operations we need to set the dma direction to BIDIRECTIONAL
  1064. * and copy the src workarea to the dst workarea.
  1065. */
  1066. if (sg_virt(xts->src) == sg_virt(xts->dst))
  1067. in_place = true;
  1068. ret = ccp_init_data(&src, cmd_q, xts->src, xts->src_len,
  1069. unit_size,
  1070. in_place ? DMA_BIDIRECTIONAL : DMA_TO_DEVICE);
  1071. if (ret)
  1072. goto e_ctx;
  1073. if (in_place) {
  1074. dst = src;
  1075. } else {
  1076. ret = ccp_init_data(&dst, cmd_q, xts->dst, xts->src_len,
  1077. unit_size, DMA_FROM_DEVICE);
  1078. if (ret)
  1079. goto e_src;
  1080. }
  1081. /* Send data to the CCP AES engine */
  1082. while (src.sg_wa.bytes_left) {
  1083. ccp_prepare_data(&src, &dst, &op, unit_size, true);
  1084. if (!src.sg_wa.bytes_left)
  1085. op.eom = 1;
  1086. ret = ccp_perform_xts_aes(&op);
  1087. if (ret) {
  1088. cmd->engine_error = cmd_q->cmd_error;
  1089. goto e_dst;
  1090. }
  1091. ccp_process_data(&src, &dst, &op);
  1092. }
  1093. /* Retrieve the AES context - convert from LE to BE using
  1094. * 32-byte (256-bit) byteswapping
  1095. */
  1096. ret = ccp_copy_from_ksb(cmd_q, &ctx, op.jobid, op.ksb_ctx,
  1097. CCP_PASSTHRU_BYTESWAP_256BIT);
  1098. if (ret) {
  1099. cmd->engine_error = cmd_q->cmd_error;
  1100. goto e_dst;
  1101. }
  1102. /* ...but we only need AES_BLOCK_SIZE bytes */
  1103. dm_offset = CCP_KSB_BYTES - AES_BLOCK_SIZE;
  1104. ccp_get_dm_area(&ctx, dm_offset, xts->iv, 0, xts->iv_len);
  1105. e_dst:
  1106. if (!in_place)
  1107. ccp_free_data(&dst, cmd_q);
  1108. e_src:
  1109. ccp_free_data(&src, cmd_q);
  1110. e_ctx:
  1111. ccp_dm_free(&ctx);
  1112. e_key:
  1113. ccp_dm_free(&key);
  1114. return ret;
  1115. }
  1116. static int ccp_run_sha_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
  1117. {
  1118. struct ccp_sha_engine *sha = &cmd->u.sha;
  1119. struct ccp_dm_workarea ctx;
  1120. struct ccp_data src;
  1121. struct ccp_op op;
  1122. int ret;
  1123. if (sha->ctx_len != CCP_SHA_CTXSIZE)
  1124. return -EINVAL;
  1125. if (!sha->ctx)
  1126. return -EINVAL;
  1127. if (!sha->final && (sha->src_len & (CCP_SHA_BLOCKSIZE - 1)))
  1128. return -EINVAL;
  1129. if (!sha->src_len) {
  1130. const u8 *sha_zero;
  1131. /* Not final, just return */
  1132. if (!sha->final)
  1133. return 0;
  1134. /* CCP can't do a zero length sha operation so the caller
  1135. * must buffer the data.
  1136. */
  1137. if (sha->msg_bits)
  1138. return -EINVAL;
  1139. /* A sha operation for a message with a total length of zero,
  1140. * return known result.
  1141. */
  1142. switch (sha->type) {
  1143. case CCP_SHA_TYPE_1:
  1144. sha_zero = ccp_sha1_zero;
  1145. break;
  1146. case CCP_SHA_TYPE_224:
  1147. sha_zero = ccp_sha224_zero;
  1148. break;
  1149. case CCP_SHA_TYPE_256:
  1150. sha_zero = ccp_sha256_zero;
  1151. break;
  1152. default:
  1153. return -EINVAL;
  1154. }
  1155. scatterwalk_map_and_copy((void *)sha_zero, sha->ctx, 0,
  1156. sha->ctx_len, 1);
  1157. return 0;
  1158. }
  1159. if (!sha->src)
  1160. return -EINVAL;
  1161. BUILD_BUG_ON(CCP_SHA_KSB_COUNT != 1);
  1162. memset(&op, 0, sizeof(op));
  1163. op.cmd_q = cmd_q;
  1164. op.jobid = ccp_gen_jobid(cmd_q->ccp);
  1165. op.ksb_ctx = cmd_q->ksb_ctx;
  1166. op.u.sha.type = sha->type;
  1167. op.u.sha.msg_bits = sha->msg_bits;
  1168. /* The SHA context fits in a single (32-byte) KSB entry and
  1169. * must be in little endian format. Use the 256-bit byte swap
  1170. * passthru option to convert from big endian to little endian.
  1171. */
  1172. ret = ccp_init_dm_workarea(&ctx, cmd_q,
  1173. CCP_SHA_KSB_COUNT * CCP_KSB_BYTES,
  1174. DMA_BIDIRECTIONAL);
  1175. if (ret)
  1176. return ret;
  1177. if (sha->first) {
  1178. const __be32 *init;
  1179. switch (sha->type) {
  1180. case CCP_SHA_TYPE_1:
  1181. init = ccp_sha1_init;
  1182. break;
  1183. case CCP_SHA_TYPE_224:
  1184. init = ccp_sha224_init;
  1185. break;
  1186. case CCP_SHA_TYPE_256:
  1187. init = ccp_sha256_init;
  1188. break;
  1189. default:
  1190. ret = -EINVAL;
  1191. goto e_ctx;
  1192. }
  1193. memcpy(ctx.address, init, CCP_SHA_CTXSIZE);
  1194. } else {
  1195. ccp_set_dm_area(&ctx, 0, sha->ctx, 0, sha->ctx_len);
  1196. }
  1197. ret = ccp_copy_to_ksb(cmd_q, &ctx, op.jobid, op.ksb_ctx,
  1198. CCP_PASSTHRU_BYTESWAP_256BIT);
  1199. if (ret) {
  1200. cmd->engine_error = cmd_q->cmd_error;
  1201. goto e_ctx;
  1202. }
  1203. /* Send data to the CCP SHA engine */
  1204. ret = ccp_init_data(&src, cmd_q, sha->src, sha->src_len,
  1205. CCP_SHA_BLOCKSIZE, DMA_TO_DEVICE);
  1206. if (ret)
  1207. goto e_ctx;
  1208. while (src.sg_wa.bytes_left) {
  1209. ccp_prepare_data(&src, NULL, &op, CCP_SHA_BLOCKSIZE, false);
  1210. if (sha->final && !src.sg_wa.bytes_left)
  1211. op.eom = 1;
  1212. ret = ccp_perform_sha(&op);
  1213. if (ret) {
  1214. cmd->engine_error = cmd_q->cmd_error;
  1215. goto e_data;
  1216. }
  1217. ccp_process_data(&src, NULL, &op);
  1218. }
  1219. /* Retrieve the SHA context - convert from LE to BE using
  1220. * 32-byte (256-bit) byteswapping to BE
  1221. */
  1222. ret = ccp_copy_from_ksb(cmd_q, &ctx, op.jobid, op.ksb_ctx,
  1223. CCP_PASSTHRU_BYTESWAP_256BIT);
  1224. if (ret) {
  1225. cmd->engine_error = cmd_q->cmd_error;
  1226. goto e_data;
  1227. }
  1228. ccp_get_dm_area(&ctx, 0, sha->ctx, 0, sha->ctx_len);
  1229. if (sha->final && sha->opad) {
  1230. /* HMAC operation, recursively perform final SHA */
  1231. struct ccp_cmd hmac_cmd;
  1232. struct scatterlist sg;
  1233. u64 block_size, digest_size;
  1234. u8 *hmac_buf;
  1235. switch (sha->type) {
  1236. case CCP_SHA_TYPE_1:
  1237. block_size = SHA1_BLOCK_SIZE;
  1238. digest_size = SHA1_DIGEST_SIZE;
  1239. break;
  1240. case CCP_SHA_TYPE_224:
  1241. block_size = SHA224_BLOCK_SIZE;
  1242. digest_size = SHA224_DIGEST_SIZE;
  1243. break;
  1244. case CCP_SHA_TYPE_256:
  1245. block_size = SHA256_BLOCK_SIZE;
  1246. digest_size = SHA256_DIGEST_SIZE;
  1247. break;
  1248. default:
  1249. ret = -EINVAL;
  1250. goto e_data;
  1251. }
  1252. if (sha->opad_len != block_size) {
  1253. ret = -EINVAL;
  1254. goto e_data;
  1255. }
  1256. hmac_buf = kmalloc(block_size + digest_size, GFP_KERNEL);
  1257. if (!hmac_buf) {
  1258. ret = -ENOMEM;
  1259. goto e_data;
  1260. }
  1261. sg_init_one(&sg, hmac_buf, block_size + digest_size);
  1262. scatterwalk_map_and_copy(hmac_buf, sha->opad, 0, block_size, 0);
  1263. memcpy(hmac_buf + block_size, ctx.address, digest_size);
  1264. memset(&hmac_cmd, 0, sizeof(hmac_cmd));
  1265. hmac_cmd.engine = CCP_ENGINE_SHA;
  1266. hmac_cmd.u.sha.type = sha->type;
  1267. hmac_cmd.u.sha.ctx = sha->ctx;
  1268. hmac_cmd.u.sha.ctx_len = sha->ctx_len;
  1269. hmac_cmd.u.sha.src = &sg;
  1270. hmac_cmd.u.sha.src_len = block_size + digest_size;
  1271. hmac_cmd.u.sha.opad = NULL;
  1272. hmac_cmd.u.sha.opad_len = 0;
  1273. hmac_cmd.u.sha.first = 1;
  1274. hmac_cmd.u.sha.final = 1;
  1275. hmac_cmd.u.sha.msg_bits = (block_size + digest_size) << 3;
  1276. ret = ccp_run_sha_cmd(cmd_q, &hmac_cmd);
  1277. if (ret)
  1278. cmd->engine_error = hmac_cmd.engine_error;
  1279. kfree(hmac_buf);
  1280. }
  1281. e_data:
  1282. ccp_free_data(&src, cmd_q);
  1283. e_ctx:
  1284. ccp_dm_free(&ctx);
  1285. return ret;
  1286. }
  1287. static int ccp_run_rsa_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
  1288. {
  1289. struct ccp_rsa_engine *rsa = &cmd->u.rsa;
  1290. struct ccp_dm_workarea exp, src;
  1291. struct ccp_data dst;
  1292. struct ccp_op op;
  1293. unsigned int ksb_count, i_len, o_len;
  1294. int ret;
  1295. if (rsa->key_size > CCP_RSA_MAX_WIDTH)
  1296. return -EINVAL;
  1297. if (!rsa->exp || !rsa->mod || !rsa->src || !rsa->dst)
  1298. return -EINVAL;
  1299. /* The RSA modulus must precede the message being acted upon, so
  1300. * it must be copied to a DMA area where the message and the
  1301. * modulus can be concatenated. Therefore the input buffer
  1302. * length required is twice the output buffer length (which
  1303. * must be a multiple of 256-bits).
  1304. */
  1305. o_len = ((rsa->key_size + 255) / 256) * 32;
  1306. i_len = o_len * 2;
  1307. ksb_count = o_len / CCP_KSB_BYTES;
  1308. memset(&op, 0, sizeof(op));
  1309. op.cmd_q = cmd_q;
  1310. op.jobid = ccp_gen_jobid(cmd_q->ccp);
  1311. op.ksb_key = ccp_alloc_ksb(cmd_q->ccp, ksb_count);
  1312. if (!op.ksb_key)
  1313. return -EIO;
  1314. /* The RSA exponent may span multiple (32-byte) KSB entries and must
  1315. * be in little endian format. Reverse copy each 32-byte chunk
  1316. * of the exponent (En chunk to E0 chunk, E(n-1) chunk to E1 chunk)
  1317. * and each byte within that chunk and do not perform any byte swap
  1318. * operations on the passthru operation.
  1319. */
  1320. ret = ccp_init_dm_workarea(&exp, cmd_q, o_len, DMA_TO_DEVICE);
  1321. if (ret)
  1322. goto e_ksb;
  1323. ccp_reverse_set_dm_area(&exp, rsa->exp, rsa->exp_len, CCP_KSB_BYTES,
  1324. false);
  1325. ret = ccp_copy_to_ksb(cmd_q, &exp, op.jobid, op.ksb_key,
  1326. CCP_PASSTHRU_BYTESWAP_NOOP);
  1327. if (ret) {
  1328. cmd->engine_error = cmd_q->cmd_error;
  1329. goto e_exp;
  1330. }
  1331. /* Concatenate the modulus and the message. Both the modulus and
  1332. * the operands must be in little endian format. Since the input
  1333. * is in big endian format it must be converted.
  1334. */
  1335. ret = ccp_init_dm_workarea(&src, cmd_q, i_len, DMA_TO_DEVICE);
  1336. if (ret)
  1337. goto e_exp;
  1338. ccp_reverse_set_dm_area(&src, rsa->mod, rsa->mod_len, CCP_KSB_BYTES,
  1339. false);
  1340. src.address += o_len; /* Adjust the address for the copy operation */
  1341. ccp_reverse_set_dm_area(&src, rsa->src, rsa->src_len, CCP_KSB_BYTES,
  1342. false);
  1343. src.address -= o_len; /* Reset the address to original value */
  1344. /* Prepare the output area for the operation */
  1345. ret = ccp_init_data(&dst, cmd_q, rsa->dst, rsa->mod_len,
  1346. o_len, DMA_FROM_DEVICE);
  1347. if (ret)
  1348. goto e_src;
  1349. op.soc = 1;
  1350. op.src.u.dma.address = src.dma.address;
  1351. op.src.u.dma.offset = 0;
  1352. op.src.u.dma.length = i_len;
  1353. op.dst.u.dma.address = dst.dm_wa.dma.address;
  1354. op.dst.u.dma.offset = 0;
  1355. op.dst.u.dma.length = o_len;
  1356. op.u.rsa.mod_size = rsa->key_size;
  1357. op.u.rsa.input_len = i_len;
  1358. ret = ccp_perform_rsa(&op);
  1359. if (ret) {
  1360. cmd->engine_error = cmd_q->cmd_error;
  1361. goto e_dst;
  1362. }
  1363. ccp_reverse_get_dm_area(&dst.dm_wa, rsa->dst, rsa->mod_len);
  1364. e_dst:
  1365. ccp_free_data(&dst, cmd_q);
  1366. e_src:
  1367. ccp_dm_free(&src);
  1368. e_exp:
  1369. ccp_dm_free(&exp);
  1370. e_ksb:
  1371. ccp_free_ksb(cmd_q->ccp, op.ksb_key, ksb_count);
  1372. return ret;
  1373. }
  1374. static int ccp_run_passthru_cmd(struct ccp_cmd_queue *cmd_q,
  1375. struct ccp_cmd *cmd)
  1376. {
  1377. struct ccp_passthru_engine *pt = &cmd->u.passthru;
  1378. struct ccp_dm_workarea mask;
  1379. struct ccp_data src, dst;
  1380. struct ccp_op op;
  1381. bool in_place = false;
  1382. unsigned int i;
  1383. int ret;
  1384. if (!pt->final && (pt->src_len & (CCP_PASSTHRU_BLOCKSIZE - 1)))
  1385. return -EINVAL;
  1386. if (!pt->src || !pt->dst)
  1387. return -EINVAL;
  1388. if (pt->bit_mod != CCP_PASSTHRU_BITWISE_NOOP) {
  1389. if (pt->mask_len != CCP_PASSTHRU_MASKSIZE)
  1390. return -EINVAL;
  1391. if (!pt->mask)
  1392. return -EINVAL;
  1393. }
  1394. BUILD_BUG_ON(CCP_PASSTHRU_KSB_COUNT != 1);
  1395. memset(&op, 0, sizeof(op));
  1396. op.cmd_q = cmd_q;
  1397. op.jobid = ccp_gen_jobid(cmd_q->ccp);
  1398. if (pt->bit_mod != CCP_PASSTHRU_BITWISE_NOOP) {
  1399. /* Load the mask */
  1400. op.ksb_key = cmd_q->ksb_key;
  1401. ret = ccp_init_dm_workarea(&mask, cmd_q,
  1402. CCP_PASSTHRU_KSB_COUNT *
  1403. CCP_KSB_BYTES,
  1404. DMA_TO_DEVICE);
  1405. if (ret)
  1406. return ret;
  1407. ccp_set_dm_area(&mask, 0, pt->mask, 0, pt->mask_len);
  1408. ret = ccp_copy_to_ksb(cmd_q, &mask, op.jobid, op.ksb_key,
  1409. CCP_PASSTHRU_BYTESWAP_NOOP);
  1410. if (ret) {
  1411. cmd->engine_error = cmd_q->cmd_error;
  1412. goto e_mask;
  1413. }
  1414. }
  1415. /* Prepare the input and output data workareas. For in-place
  1416. * operations we need to set the dma direction to BIDIRECTIONAL
  1417. * and copy the src workarea to the dst workarea.
  1418. */
  1419. if (sg_virt(pt->src) == sg_virt(pt->dst))
  1420. in_place = true;
  1421. ret = ccp_init_data(&src, cmd_q, pt->src, pt->src_len,
  1422. CCP_PASSTHRU_MASKSIZE,
  1423. in_place ? DMA_BIDIRECTIONAL : DMA_TO_DEVICE);
  1424. if (ret)
  1425. goto e_mask;
  1426. if (in_place) {
  1427. dst = src;
  1428. } else {
  1429. ret = ccp_init_data(&dst, cmd_q, pt->dst, pt->src_len,
  1430. CCP_PASSTHRU_MASKSIZE, DMA_FROM_DEVICE);
  1431. if (ret)
  1432. goto e_src;
  1433. }
  1434. /* Send data to the CCP Passthru engine
  1435. * Because the CCP engine works on a single source and destination
  1436. * dma address at a time, each entry in the source scatterlist
  1437. * (after the dma_map_sg call) must be less than or equal to the
  1438. * (remaining) length in the destination scatterlist entry and the
  1439. * length must be a multiple of CCP_PASSTHRU_BLOCKSIZE
  1440. */
  1441. dst.sg_wa.sg_used = 0;
  1442. for (i = 1; i <= src.sg_wa.dma_count; i++) {
  1443. if (!dst.sg_wa.sg ||
  1444. (dst.sg_wa.sg->length < src.sg_wa.sg->length)) {
  1445. ret = -EINVAL;
  1446. goto e_dst;
  1447. }
  1448. if (i == src.sg_wa.dma_count) {
  1449. op.eom = 1;
  1450. op.soc = 1;
  1451. }
  1452. op.src.type = CCP_MEMTYPE_SYSTEM;
  1453. op.src.u.dma.address = sg_dma_address(src.sg_wa.sg);
  1454. op.src.u.dma.offset = 0;
  1455. op.src.u.dma.length = sg_dma_len(src.sg_wa.sg);
  1456. op.dst.type = CCP_MEMTYPE_SYSTEM;
  1457. op.dst.u.dma.address = sg_dma_address(dst.sg_wa.sg);
  1458. op.dst.u.dma.offset = dst.sg_wa.sg_used;
  1459. op.dst.u.dma.length = op.src.u.dma.length;
  1460. ret = ccp_perform_passthru(&op);
  1461. if (ret) {
  1462. cmd->engine_error = cmd_q->cmd_error;
  1463. goto e_dst;
  1464. }
  1465. dst.sg_wa.sg_used += src.sg_wa.sg->length;
  1466. if (dst.sg_wa.sg_used == dst.sg_wa.sg->length) {
  1467. dst.sg_wa.sg = sg_next(dst.sg_wa.sg);
  1468. dst.sg_wa.sg_used = 0;
  1469. }
  1470. src.sg_wa.sg = sg_next(src.sg_wa.sg);
  1471. }
  1472. e_dst:
  1473. if (!in_place)
  1474. ccp_free_data(&dst, cmd_q);
  1475. e_src:
  1476. ccp_free_data(&src, cmd_q);
  1477. e_mask:
  1478. if (pt->bit_mod != CCP_PASSTHRU_BITWISE_NOOP)
  1479. ccp_dm_free(&mask);
  1480. return ret;
  1481. }
  1482. static int ccp_run_ecc_mm_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
  1483. {
  1484. struct ccp_ecc_engine *ecc = &cmd->u.ecc;
  1485. struct ccp_dm_workarea src, dst;
  1486. struct ccp_op op;
  1487. int ret;
  1488. u8 *save;
  1489. if (!ecc->u.mm.operand_1 ||
  1490. (ecc->u.mm.operand_1_len > CCP_ECC_MODULUS_BYTES))
  1491. return -EINVAL;
  1492. if (ecc->function != CCP_ECC_FUNCTION_MINV_384BIT)
  1493. if (!ecc->u.mm.operand_2 ||
  1494. (ecc->u.mm.operand_2_len > CCP_ECC_MODULUS_BYTES))
  1495. return -EINVAL;
  1496. if (!ecc->u.mm.result ||
  1497. (ecc->u.mm.result_len < CCP_ECC_MODULUS_BYTES))
  1498. return -EINVAL;
  1499. memset(&op, 0, sizeof(op));
  1500. op.cmd_q = cmd_q;
  1501. op.jobid = ccp_gen_jobid(cmd_q->ccp);
  1502. /* Concatenate the modulus and the operands. Both the modulus and
  1503. * the operands must be in little endian format. Since the input
  1504. * is in big endian format it must be converted and placed in a
  1505. * fixed length buffer.
  1506. */
  1507. ret = ccp_init_dm_workarea(&src, cmd_q, CCP_ECC_SRC_BUF_SIZE,
  1508. DMA_TO_DEVICE);
  1509. if (ret)
  1510. return ret;
  1511. /* Save the workarea address since it is updated in order to perform
  1512. * the concatenation
  1513. */
  1514. save = src.address;
  1515. /* Copy the ECC modulus */
  1516. ccp_reverse_set_dm_area(&src, ecc->mod, ecc->mod_len,
  1517. CCP_ECC_OPERAND_SIZE, false);
  1518. src.address += CCP_ECC_OPERAND_SIZE;
  1519. /* Copy the first operand */
  1520. ccp_reverse_set_dm_area(&src, ecc->u.mm.operand_1,
  1521. ecc->u.mm.operand_1_len,
  1522. CCP_ECC_OPERAND_SIZE, false);
  1523. src.address += CCP_ECC_OPERAND_SIZE;
  1524. if (ecc->function != CCP_ECC_FUNCTION_MINV_384BIT) {
  1525. /* Copy the second operand */
  1526. ccp_reverse_set_dm_area(&src, ecc->u.mm.operand_2,
  1527. ecc->u.mm.operand_2_len,
  1528. CCP_ECC_OPERAND_SIZE, false);
  1529. src.address += CCP_ECC_OPERAND_SIZE;
  1530. }
  1531. /* Restore the workarea address */
  1532. src.address = save;
  1533. /* Prepare the output area for the operation */
  1534. ret = ccp_init_dm_workarea(&dst, cmd_q, CCP_ECC_DST_BUF_SIZE,
  1535. DMA_FROM_DEVICE);
  1536. if (ret)
  1537. goto e_src;
  1538. op.soc = 1;
  1539. op.src.u.dma.address = src.dma.address;
  1540. op.src.u.dma.offset = 0;
  1541. op.src.u.dma.length = src.length;
  1542. op.dst.u.dma.address = dst.dma.address;
  1543. op.dst.u.dma.offset = 0;
  1544. op.dst.u.dma.length = dst.length;
  1545. op.u.ecc.function = cmd->u.ecc.function;
  1546. ret = ccp_perform_ecc(&op);
  1547. if (ret) {
  1548. cmd->engine_error = cmd_q->cmd_error;
  1549. goto e_dst;
  1550. }
  1551. ecc->ecc_result = le16_to_cpup(
  1552. (const __le16 *)(dst.address + CCP_ECC_RESULT_OFFSET));
  1553. if (!(ecc->ecc_result & CCP_ECC_RESULT_SUCCESS)) {
  1554. ret = -EIO;
  1555. goto e_dst;
  1556. }
  1557. /* Save the ECC result */
  1558. ccp_reverse_get_dm_area(&dst, ecc->u.mm.result, CCP_ECC_MODULUS_BYTES);
  1559. e_dst:
  1560. ccp_dm_free(&dst);
  1561. e_src:
  1562. ccp_dm_free(&src);
  1563. return ret;
  1564. }
  1565. static int ccp_run_ecc_pm_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
  1566. {
  1567. struct ccp_ecc_engine *ecc = &cmd->u.ecc;
  1568. struct ccp_dm_workarea src, dst;
  1569. struct ccp_op op;
  1570. int ret;
  1571. u8 *save;
  1572. if (!ecc->u.pm.point_1.x ||
  1573. (ecc->u.pm.point_1.x_len > CCP_ECC_MODULUS_BYTES) ||
  1574. !ecc->u.pm.point_1.y ||
  1575. (ecc->u.pm.point_1.y_len > CCP_ECC_MODULUS_BYTES))
  1576. return -EINVAL;
  1577. if (ecc->function == CCP_ECC_FUNCTION_PADD_384BIT) {
  1578. if (!ecc->u.pm.point_2.x ||
  1579. (ecc->u.pm.point_2.x_len > CCP_ECC_MODULUS_BYTES) ||
  1580. !ecc->u.pm.point_2.y ||
  1581. (ecc->u.pm.point_2.y_len > CCP_ECC_MODULUS_BYTES))
  1582. return -EINVAL;
  1583. } else {
  1584. if (!ecc->u.pm.domain_a ||
  1585. (ecc->u.pm.domain_a_len > CCP_ECC_MODULUS_BYTES))
  1586. return -EINVAL;
  1587. if (ecc->function == CCP_ECC_FUNCTION_PMUL_384BIT)
  1588. if (!ecc->u.pm.scalar ||
  1589. (ecc->u.pm.scalar_len > CCP_ECC_MODULUS_BYTES))
  1590. return -EINVAL;
  1591. }
  1592. if (!ecc->u.pm.result.x ||
  1593. (ecc->u.pm.result.x_len < CCP_ECC_MODULUS_BYTES) ||
  1594. !ecc->u.pm.result.y ||
  1595. (ecc->u.pm.result.y_len < CCP_ECC_MODULUS_BYTES))
  1596. return -EINVAL;
  1597. memset(&op, 0, sizeof(op));
  1598. op.cmd_q = cmd_q;
  1599. op.jobid = ccp_gen_jobid(cmd_q->ccp);
  1600. /* Concatenate the modulus and the operands. Both the modulus and
  1601. * the operands must be in little endian format. Since the input
  1602. * is in big endian format it must be converted and placed in a
  1603. * fixed length buffer.
  1604. */
  1605. ret = ccp_init_dm_workarea(&src, cmd_q, CCP_ECC_SRC_BUF_SIZE,
  1606. DMA_TO_DEVICE);
  1607. if (ret)
  1608. return ret;
  1609. /* Save the workarea address since it is updated in order to perform
  1610. * the concatenation
  1611. */
  1612. save = src.address;
  1613. /* Copy the ECC modulus */
  1614. ccp_reverse_set_dm_area(&src, ecc->mod, ecc->mod_len,
  1615. CCP_ECC_OPERAND_SIZE, false);
  1616. src.address += CCP_ECC_OPERAND_SIZE;
  1617. /* Copy the first point X and Y coordinate */
  1618. ccp_reverse_set_dm_area(&src, ecc->u.pm.point_1.x,
  1619. ecc->u.pm.point_1.x_len,
  1620. CCP_ECC_OPERAND_SIZE, false);
  1621. src.address += CCP_ECC_OPERAND_SIZE;
  1622. ccp_reverse_set_dm_area(&src, ecc->u.pm.point_1.y,
  1623. ecc->u.pm.point_1.y_len,
  1624. CCP_ECC_OPERAND_SIZE, false);
  1625. src.address += CCP_ECC_OPERAND_SIZE;
  1626. /* Set the first point Z coordianate to 1 */
  1627. *src.address = 0x01;
  1628. src.address += CCP_ECC_OPERAND_SIZE;
  1629. if (ecc->function == CCP_ECC_FUNCTION_PADD_384BIT) {
  1630. /* Copy the second point X and Y coordinate */
  1631. ccp_reverse_set_dm_area(&src, ecc->u.pm.point_2.x,
  1632. ecc->u.pm.point_2.x_len,
  1633. CCP_ECC_OPERAND_SIZE, false);
  1634. src.address += CCP_ECC_OPERAND_SIZE;
  1635. ccp_reverse_set_dm_area(&src, ecc->u.pm.point_2.y,
  1636. ecc->u.pm.point_2.y_len,
  1637. CCP_ECC_OPERAND_SIZE, false);
  1638. src.address += CCP_ECC_OPERAND_SIZE;
  1639. /* Set the second point Z coordianate to 1 */
  1640. *src.address = 0x01;
  1641. src.address += CCP_ECC_OPERAND_SIZE;
  1642. } else {
  1643. /* Copy the Domain "a" parameter */
  1644. ccp_reverse_set_dm_area(&src, ecc->u.pm.domain_a,
  1645. ecc->u.pm.domain_a_len,
  1646. CCP_ECC_OPERAND_SIZE, false);
  1647. src.address += CCP_ECC_OPERAND_SIZE;
  1648. if (ecc->function == CCP_ECC_FUNCTION_PMUL_384BIT) {
  1649. /* Copy the scalar value */
  1650. ccp_reverse_set_dm_area(&src, ecc->u.pm.scalar,
  1651. ecc->u.pm.scalar_len,
  1652. CCP_ECC_OPERAND_SIZE, false);
  1653. src.address += CCP_ECC_OPERAND_SIZE;
  1654. }
  1655. }
  1656. /* Restore the workarea address */
  1657. src.address = save;
  1658. /* Prepare the output area for the operation */
  1659. ret = ccp_init_dm_workarea(&dst, cmd_q, CCP_ECC_DST_BUF_SIZE,
  1660. DMA_FROM_DEVICE);
  1661. if (ret)
  1662. goto e_src;
  1663. op.soc = 1;
  1664. op.src.u.dma.address = src.dma.address;
  1665. op.src.u.dma.offset = 0;
  1666. op.src.u.dma.length = src.length;
  1667. op.dst.u.dma.address = dst.dma.address;
  1668. op.dst.u.dma.offset = 0;
  1669. op.dst.u.dma.length = dst.length;
  1670. op.u.ecc.function = cmd->u.ecc.function;
  1671. ret = ccp_perform_ecc(&op);
  1672. if (ret) {
  1673. cmd->engine_error = cmd_q->cmd_error;
  1674. goto e_dst;
  1675. }
  1676. ecc->ecc_result = le16_to_cpup(
  1677. (const __le16 *)(dst.address + CCP_ECC_RESULT_OFFSET));
  1678. if (!(ecc->ecc_result & CCP_ECC_RESULT_SUCCESS)) {
  1679. ret = -EIO;
  1680. goto e_dst;
  1681. }
  1682. /* Save the workarea address since it is updated as we walk through
  1683. * to copy the point math result
  1684. */
  1685. save = dst.address;
  1686. /* Save the ECC result X and Y coordinates */
  1687. ccp_reverse_get_dm_area(&dst, ecc->u.pm.result.x,
  1688. CCP_ECC_MODULUS_BYTES);
  1689. dst.address += CCP_ECC_OUTPUT_SIZE;
  1690. ccp_reverse_get_dm_area(&dst, ecc->u.pm.result.y,
  1691. CCP_ECC_MODULUS_BYTES);
  1692. dst.address += CCP_ECC_OUTPUT_SIZE;
  1693. /* Restore the workarea address */
  1694. dst.address = save;
  1695. e_dst:
  1696. ccp_dm_free(&dst);
  1697. e_src:
  1698. ccp_dm_free(&src);
  1699. return ret;
  1700. }
  1701. static int ccp_run_ecc_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
  1702. {
  1703. struct ccp_ecc_engine *ecc = &cmd->u.ecc;
  1704. ecc->ecc_result = 0;
  1705. if (!ecc->mod ||
  1706. (ecc->mod_len > CCP_ECC_MODULUS_BYTES))
  1707. return -EINVAL;
  1708. switch (ecc->function) {
  1709. case CCP_ECC_FUNCTION_MMUL_384BIT:
  1710. case CCP_ECC_FUNCTION_MADD_384BIT:
  1711. case CCP_ECC_FUNCTION_MINV_384BIT:
  1712. return ccp_run_ecc_mm_cmd(cmd_q, cmd);
  1713. case CCP_ECC_FUNCTION_PADD_384BIT:
  1714. case CCP_ECC_FUNCTION_PMUL_384BIT:
  1715. case CCP_ECC_FUNCTION_PDBL_384BIT:
  1716. return ccp_run_ecc_pm_cmd(cmd_q, cmd);
  1717. default:
  1718. return -EINVAL;
  1719. }
  1720. }
  1721. int ccp_run_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
  1722. {
  1723. int ret;
  1724. cmd->engine_error = 0;
  1725. cmd_q->cmd_error = 0;
  1726. cmd_q->int_rcvd = 0;
  1727. cmd_q->free_slots = CMD_Q_DEPTH(ioread32(cmd_q->reg_status));
  1728. switch (cmd->engine) {
  1729. case CCP_ENGINE_AES:
  1730. ret = ccp_run_aes_cmd(cmd_q, cmd);
  1731. break;
  1732. case CCP_ENGINE_XTS_AES_128:
  1733. ret = ccp_run_xts_aes_cmd(cmd_q, cmd);
  1734. break;
  1735. case CCP_ENGINE_SHA:
  1736. ret = ccp_run_sha_cmd(cmd_q, cmd);
  1737. break;
  1738. case CCP_ENGINE_RSA:
  1739. ret = ccp_run_rsa_cmd(cmd_q, cmd);
  1740. break;
  1741. case CCP_ENGINE_PASSTHRU:
  1742. ret = ccp_run_passthru_cmd(cmd_q, cmd);
  1743. break;
  1744. case CCP_ENGINE_ECC:
  1745. ret = ccp_run_ecc_cmd(cmd_q, cmd);
  1746. break;
  1747. default:
  1748. ret = -EINVAL;
  1749. }
  1750. return ret;
  1751. }