ccp-dev-v5.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * AMD Cryptographic Coprocessor (CCP) driver
  4. *
  5. * Copyright (C) 2016,2019 Advanced Micro Devices, Inc.
  6. *
  7. * Author: Gary R Hook <gary.hook@amd.com>
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/kthread.h>
  11. #include <linux/dma-mapping.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/compiler.h>
  14. #include <linux/ccp.h>
  15. #include "ccp-dev.h"
  16. /* Allocate the requested number of contiguous LSB slots
  17. * from the LSB bitmap. Look in the private range for this
  18. * queue first; failing that, check the public area.
  19. * If no space is available, wait around.
  20. * Return: first slot number
  21. */
  22. static u32 ccp_lsb_alloc(struct ccp_cmd_queue *cmd_q, unsigned int count)
  23. {
  24. struct ccp_device *ccp;
  25. int start;
  26. /* First look at the map for the queue */
  27. if (cmd_q->lsb >= 0) {
  28. start = (u32)bitmap_find_next_zero_area(cmd_q->lsbmap,
  29. LSB_SIZE,
  30. 0, count, 0);
  31. if (start < LSB_SIZE) {
  32. bitmap_set(cmd_q->lsbmap, start, count);
  33. return start + cmd_q->lsb * LSB_SIZE;
  34. }
  35. }
  36. /* No joy; try to get an entry from the shared blocks */
  37. ccp = cmd_q->ccp;
  38. for (;;) {
  39. mutex_lock(&ccp->sb_mutex);
  40. start = (u32)bitmap_find_next_zero_area(ccp->lsbmap,
  41. MAX_LSB_CNT * LSB_SIZE,
  42. 0,
  43. count, 0);
  44. if (start <= MAX_LSB_CNT * LSB_SIZE) {
  45. bitmap_set(ccp->lsbmap, start, count);
  46. mutex_unlock(&ccp->sb_mutex);
  47. return start;
  48. }
  49. ccp->sb_avail = 0;
  50. mutex_unlock(&ccp->sb_mutex);
  51. /* Wait for KSB entries to become available */
  52. if (wait_event_interruptible(ccp->sb_queue, ccp->sb_avail))
  53. return 0;
  54. }
  55. }
  56. /* Free a number of LSB slots from the bitmap, starting at
  57. * the indicated starting slot number.
  58. */
  59. static void ccp_lsb_free(struct ccp_cmd_queue *cmd_q, unsigned int start,
  60. unsigned int count)
  61. {
  62. if (!start)
  63. return;
  64. if (cmd_q->lsb == start) {
  65. /* An entry from the private LSB */
  66. bitmap_clear(cmd_q->lsbmap, start, count);
  67. } else {
  68. /* From the shared LSBs */
  69. struct ccp_device *ccp = cmd_q->ccp;
  70. mutex_lock(&ccp->sb_mutex);
  71. bitmap_clear(ccp->lsbmap, start, count);
  72. ccp->sb_avail = 1;
  73. mutex_unlock(&ccp->sb_mutex);
  74. wake_up_interruptible_all(&ccp->sb_queue);
  75. }
  76. }
  77. /* CCP version 5: Union to define the function field (cmd_reg1/dword0) */
  78. union ccp_function {
  79. struct {
  80. u16 size:7;
  81. u16 encrypt:1;
  82. u16 mode:5;
  83. u16 type:2;
  84. } aes;
  85. struct {
  86. u16 size:7;
  87. u16 encrypt:1;
  88. u16 rsvd:5;
  89. u16 type:2;
  90. } aes_xts;
  91. struct {
  92. u16 size:7;
  93. u16 encrypt:1;
  94. u16 mode:5;
  95. u16 type:2;
  96. } des3;
  97. struct {
  98. u16 rsvd1:10;
  99. u16 type:4;
  100. u16 rsvd2:1;
  101. } sha;
  102. struct {
  103. u16 mode:3;
  104. u16 size:12;
  105. } rsa;
  106. struct {
  107. u16 byteswap:2;
  108. u16 bitwise:3;
  109. u16 reflect:2;
  110. u16 rsvd:8;
  111. } pt;
  112. struct {
  113. u16 rsvd:13;
  114. } zlib;
  115. struct {
  116. u16 size:10;
  117. u16 type:2;
  118. u16 mode:3;
  119. } ecc;
  120. u16 raw;
  121. };
  122. #define CCP_AES_SIZE(p) ((p)->aes.size)
  123. #define CCP_AES_ENCRYPT(p) ((p)->aes.encrypt)
  124. #define CCP_AES_MODE(p) ((p)->aes.mode)
  125. #define CCP_AES_TYPE(p) ((p)->aes.type)
  126. #define CCP_XTS_SIZE(p) ((p)->aes_xts.size)
  127. #define CCP_XTS_TYPE(p) ((p)->aes_xts.type)
  128. #define CCP_XTS_ENCRYPT(p) ((p)->aes_xts.encrypt)
  129. #define CCP_DES3_SIZE(p) ((p)->des3.size)
  130. #define CCP_DES3_ENCRYPT(p) ((p)->des3.encrypt)
  131. #define CCP_DES3_MODE(p) ((p)->des3.mode)
  132. #define CCP_DES3_TYPE(p) ((p)->des3.type)
  133. #define CCP_SHA_TYPE(p) ((p)->sha.type)
  134. #define CCP_RSA_SIZE(p) ((p)->rsa.size)
  135. #define CCP_PT_BYTESWAP(p) ((p)->pt.byteswap)
  136. #define CCP_PT_BITWISE(p) ((p)->pt.bitwise)
  137. #define CCP_ECC_MODE(p) ((p)->ecc.mode)
  138. #define CCP_ECC_AFFINE(p) ((p)->ecc.one)
  139. /* Word 0 */
  140. #define CCP5_CMD_DW0(p) ((p)->dw0)
  141. #define CCP5_CMD_SOC(p) (CCP5_CMD_DW0(p).soc)
  142. #define CCP5_CMD_IOC(p) (CCP5_CMD_DW0(p).ioc)
  143. #define CCP5_CMD_INIT(p) (CCP5_CMD_DW0(p).init)
  144. #define CCP5_CMD_EOM(p) (CCP5_CMD_DW0(p).eom)
  145. #define CCP5_CMD_FUNCTION(p) (CCP5_CMD_DW0(p).function)
  146. #define CCP5_CMD_ENGINE(p) (CCP5_CMD_DW0(p).engine)
  147. #define CCP5_CMD_PROT(p) (CCP5_CMD_DW0(p).prot)
  148. /* Word 1 */
  149. #define CCP5_CMD_DW1(p) ((p)->length)
  150. #define CCP5_CMD_LEN(p) (CCP5_CMD_DW1(p))
  151. /* Word 2 */
  152. #define CCP5_CMD_DW2(p) ((p)->src_lo)
  153. #define CCP5_CMD_SRC_LO(p) (CCP5_CMD_DW2(p))
  154. /* Word 3 */
  155. #define CCP5_CMD_DW3(p) ((p)->dw3)
  156. #define CCP5_CMD_SRC_MEM(p) ((p)->dw3.src_mem)
  157. #define CCP5_CMD_SRC_HI(p) ((p)->dw3.src_hi)
  158. #define CCP5_CMD_LSB_ID(p) ((p)->dw3.lsb_cxt_id)
  159. #define CCP5_CMD_FIX_SRC(p) ((p)->dw3.fixed)
  160. /* Words 4/5 */
  161. #define CCP5_CMD_DW4(p) ((p)->dw4)
  162. #define CCP5_CMD_DST_LO(p) (CCP5_CMD_DW4(p).dst_lo)
  163. #define CCP5_CMD_DW5(p) ((p)->dw5.fields.dst_hi)
  164. #define CCP5_CMD_DST_HI(p) (CCP5_CMD_DW5(p))
  165. #define CCP5_CMD_DST_MEM(p) ((p)->dw5.fields.dst_mem)
  166. #define CCP5_CMD_FIX_DST(p) ((p)->dw5.fields.fixed)
  167. #define CCP5_CMD_SHA_LO(p) ((p)->dw4.sha_len_lo)
  168. #define CCP5_CMD_SHA_HI(p) ((p)->dw5.sha_len_hi)
  169. /* Word 6/7 */
  170. #define CCP5_CMD_DW6(p) ((p)->key_lo)
  171. #define CCP5_CMD_KEY_LO(p) (CCP5_CMD_DW6(p))
  172. #define CCP5_CMD_DW7(p) ((p)->dw7)
  173. #define CCP5_CMD_KEY_HI(p) ((p)->dw7.key_hi)
  174. #define CCP5_CMD_KEY_MEM(p) ((p)->dw7.key_mem)
  175. static inline u32 low_address(unsigned long addr)
  176. {
  177. return (u64)addr & 0x0ffffffff;
  178. }
  179. static inline u32 high_address(unsigned long addr)
  180. {
  181. return ((u64)addr >> 32) & 0x00000ffff;
  182. }
  183. static unsigned int ccp5_get_free_slots(struct ccp_cmd_queue *cmd_q)
  184. {
  185. unsigned int head_idx, n;
  186. u32 head_lo, queue_start;
  187. queue_start = low_address(cmd_q->qdma_tail);
  188. head_lo = ioread32(cmd_q->reg_head_lo);
  189. head_idx = (head_lo - queue_start) / sizeof(struct ccp5_desc);
  190. n = head_idx + COMMANDS_PER_QUEUE - cmd_q->qidx - 1;
  191. return n % COMMANDS_PER_QUEUE; /* Always one unused spot */
  192. }
  193. static int ccp5_do_cmd(struct ccp5_desc *desc,
  194. struct ccp_cmd_queue *cmd_q)
  195. {
  196. u32 *mP;
  197. __le32 *dP;
  198. u32 tail;
  199. int i;
  200. int ret = 0;
  201. cmd_q->total_ops++;
  202. if (CCP5_CMD_SOC(desc)) {
  203. CCP5_CMD_IOC(desc) = 1;
  204. CCP5_CMD_SOC(desc) = 0;
  205. }
  206. mutex_lock(&cmd_q->q_mutex);
  207. mP = (u32 *) &cmd_q->qbase[cmd_q->qidx];
  208. dP = (__le32 *) desc;
  209. for (i = 0; i < 8; i++)
  210. mP[i] = cpu_to_le32(dP[i]); /* handle endianness */
  211. cmd_q->qidx = (cmd_q->qidx + 1) % COMMANDS_PER_QUEUE;
  212. /* The data used by this command must be flushed to memory */
  213. wmb();
  214. /* Write the new tail address back to the queue register */
  215. tail = low_address(cmd_q->qdma_tail + cmd_q->qidx * Q_DESC_SIZE);
  216. iowrite32(tail, cmd_q->reg_tail_lo);
  217. /* Turn the queue back on using our cached control register */
  218. iowrite32(cmd_q->qcontrol | CMD5_Q_RUN, cmd_q->reg_control);
  219. mutex_unlock(&cmd_q->q_mutex);
  220. if (CCP5_CMD_IOC(desc)) {
  221. /* Wait for the job to complete */
  222. ret = wait_event_interruptible(cmd_q->int_queue,
  223. cmd_q->int_rcvd);
  224. if (ret || cmd_q->cmd_error) {
  225. /* Log the error and flush the queue by
  226. * moving the head pointer
  227. */
  228. if (cmd_q->cmd_error)
  229. ccp_log_error(cmd_q->ccp,
  230. cmd_q->cmd_error);
  231. iowrite32(tail, cmd_q->reg_head_lo);
  232. if (!ret)
  233. ret = -EIO;
  234. }
  235. cmd_q->int_rcvd = 0;
  236. }
  237. return ret;
  238. }
  239. static int ccp5_perform_aes(struct ccp_op *op)
  240. {
  241. struct ccp5_desc desc;
  242. union ccp_function function;
  243. u32 key_addr = op->sb_key * LSB_ITEM_SIZE;
  244. op->cmd_q->total_aes_ops++;
  245. /* Zero out all the fields of the command desc */
  246. memset(&desc, 0, Q_DESC_SIZE);
  247. CCP5_CMD_ENGINE(&desc) = CCP_ENGINE_AES;
  248. CCP5_CMD_SOC(&desc) = op->soc;
  249. CCP5_CMD_IOC(&desc) = 1;
  250. CCP5_CMD_INIT(&desc) = op->init;
  251. CCP5_CMD_EOM(&desc) = op->eom;
  252. CCP5_CMD_PROT(&desc) = 0;
  253. function.raw = 0;
  254. CCP_AES_ENCRYPT(&function) = op->u.aes.action;
  255. CCP_AES_MODE(&function) = op->u.aes.mode;
  256. CCP_AES_TYPE(&function) = op->u.aes.type;
  257. CCP_AES_SIZE(&function) = op->u.aes.size;
  258. CCP5_CMD_FUNCTION(&desc) = function.raw;
  259. CCP5_CMD_LEN(&desc) = op->src.u.dma.length;
  260. CCP5_CMD_SRC_LO(&desc) = ccp_addr_lo(&op->src.u.dma);
  261. CCP5_CMD_SRC_HI(&desc) = ccp_addr_hi(&op->src.u.dma);
  262. CCP5_CMD_SRC_MEM(&desc) = CCP_MEMTYPE_SYSTEM;
  263. CCP5_CMD_DST_LO(&desc) = ccp_addr_lo(&op->dst.u.dma);
  264. CCP5_CMD_DST_HI(&desc) = ccp_addr_hi(&op->dst.u.dma);
  265. CCP5_CMD_DST_MEM(&desc) = CCP_MEMTYPE_SYSTEM;
  266. CCP5_CMD_KEY_LO(&desc) = lower_32_bits(key_addr);
  267. CCP5_CMD_KEY_HI(&desc) = 0;
  268. CCP5_CMD_KEY_MEM(&desc) = CCP_MEMTYPE_SB;
  269. CCP5_CMD_LSB_ID(&desc) = op->sb_ctx;
  270. return ccp5_do_cmd(&desc, op->cmd_q);
  271. }
  272. static int ccp5_perform_xts_aes(struct ccp_op *op)
  273. {
  274. struct ccp5_desc desc;
  275. union ccp_function function;
  276. u32 key_addr = op->sb_key * LSB_ITEM_SIZE;
  277. op->cmd_q->total_xts_aes_ops++;
  278. /* Zero out all the fields of the command desc */
  279. memset(&desc, 0, Q_DESC_SIZE);
  280. CCP5_CMD_ENGINE(&desc) = CCP_ENGINE_XTS_AES_128;
  281. CCP5_CMD_SOC(&desc) = op->soc;
  282. CCP5_CMD_IOC(&desc) = 1;
  283. CCP5_CMD_INIT(&desc) = op->init;
  284. CCP5_CMD_EOM(&desc) = op->eom;
  285. CCP5_CMD_PROT(&desc) = 0;
  286. function.raw = 0;
  287. CCP_XTS_TYPE(&function) = op->u.xts.type;
  288. CCP_XTS_ENCRYPT(&function) = op->u.xts.action;
  289. CCP_XTS_SIZE(&function) = op->u.xts.unit_size;
  290. CCP5_CMD_FUNCTION(&desc) = function.raw;
  291. CCP5_CMD_LEN(&desc) = op->src.u.dma.length;
  292. CCP5_CMD_SRC_LO(&desc) = ccp_addr_lo(&op->src.u.dma);
  293. CCP5_CMD_SRC_HI(&desc) = ccp_addr_hi(&op->src.u.dma);
  294. CCP5_CMD_SRC_MEM(&desc) = CCP_MEMTYPE_SYSTEM;
  295. CCP5_CMD_DST_LO(&desc) = ccp_addr_lo(&op->dst.u.dma);
  296. CCP5_CMD_DST_HI(&desc) = ccp_addr_hi(&op->dst.u.dma);
  297. CCP5_CMD_DST_MEM(&desc) = CCP_MEMTYPE_SYSTEM;
  298. CCP5_CMD_KEY_LO(&desc) = lower_32_bits(key_addr);
  299. CCP5_CMD_KEY_HI(&desc) = 0;
  300. CCP5_CMD_KEY_MEM(&desc) = CCP_MEMTYPE_SB;
  301. CCP5_CMD_LSB_ID(&desc) = op->sb_ctx;
  302. return ccp5_do_cmd(&desc, op->cmd_q);
  303. }
  304. static int ccp5_perform_sha(struct ccp_op *op)
  305. {
  306. struct ccp5_desc desc;
  307. union ccp_function function;
  308. op->cmd_q->total_sha_ops++;
  309. /* Zero out all the fields of the command desc */
  310. memset(&desc, 0, Q_DESC_SIZE);
  311. CCP5_CMD_ENGINE(&desc) = CCP_ENGINE_SHA;
  312. CCP5_CMD_SOC(&desc) = op->soc;
  313. CCP5_CMD_IOC(&desc) = 1;
  314. CCP5_CMD_INIT(&desc) = 1;
  315. CCP5_CMD_EOM(&desc) = op->eom;
  316. CCP5_CMD_PROT(&desc) = 0;
  317. function.raw = 0;
  318. CCP_SHA_TYPE(&function) = op->u.sha.type;
  319. CCP5_CMD_FUNCTION(&desc) = function.raw;
  320. CCP5_CMD_LEN(&desc) = op->src.u.dma.length;
  321. CCP5_CMD_SRC_LO(&desc) = ccp_addr_lo(&op->src.u.dma);
  322. CCP5_CMD_SRC_HI(&desc) = ccp_addr_hi(&op->src.u.dma);
  323. CCP5_CMD_SRC_MEM(&desc) = CCP_MEMTYPE_SYSTEM;
  324. CCP5_CMD_LSB_ID(&desc) = op->sb_ctx;
  325. if (op->eom) {
  326. CCP5_CMD_SHA_LO(&desc) = lower_32_bits(op->u.sha.msg_bits);
  327. CCP5_CMD_SHA_HI(&desc) = upper_32_bits(op->u.sha.msg_bits);
  328. } else {
  329. CCP5_CMD_SHA_LO(&desc) = 0;
  330. CCP5_CMD_SHA_HI(&desc) = 0;
  331. }
  332. return ccp5_do_cmd(&desc, op->cmd_q);
  333. }
  334. static int ccp5_perform_des3(struct ccp_op *op)
  335. {
  336. struct ccp5_desc desc;
  337. union ccp_function function;
  338. u32 key_addr = op->sb_key * LSB_ITEM_SIZE;
  339. op->cmd_q->total_3des_ops++;
  340. /* Zero out all the fields of the command desc */
  341. memset(&desc, 0, sizeof(struct ccp5_desc));
  342. CCP5_CMD_ENGINE(&desc) = CCP_ENGINE_DES3;
  343. CCP5_CMD_SOC(&desc) = op->soc;
  344. CCP5_CMD_IOC(&desc) = 1;
  345. CCP5_CMD_INIT(&desc) = op->init;
  346. CCP5_CMD_EOM(&desc) = op->eom;
  347. CCP5_CMD_PROT(&desc) = 0;
  348. function.raw = 0;
  349. CCP_DES3_ENCRYPT(&function) = op->u.des3.action;
  350. CCP_DES3_MODE(&function) = op->u.des3.mode;
  351. CCP_DES3_TYPE(&function) = op->u.des3.type;
  352. CCP5_CMD_FUNCTION(&desc) = function.raw;
  353. CCP5_CMD_LEN(&desc) = op->src.u.dma.length;
  354. CCP5_CMD_SRC_LO(&desc) = ccp_addr_lo(&op->src.u.dma);
  355. CCP5_CMD_SRC_HI(&desc) = ccp_addr_hi(&op->src.u.dma);
  356. CCP5_CMD_SRC_MEM(&desc) = CCP_MEMTYPE_SYSTEM;
  357. CCP5_CMD_DST_LO(&desc) = ccp_addr_lo(&op->dst.u.dma);
  358. CCP5_CMD_DST_HI(&desc) = ccp_addr_hi(&op->dst.u.dma);
  359. CCP5_CMD_DST_MEM(&desc) = CCP_MEMTYPE_SYSTEM;
  360. CCP5_CMD_KEY_LO(&desc) = lower_32_bits(key_addr);
  361. CCP5_CMD_KEY_HI(&desc) = 0;
  362. CCP5_CMD_KEY_MEM(&desc) = CCP_MEMTYPE_SB;
  363. CCP5_CMD_LSB_ID(&desc) = op->sb_ctx;
  364. return ccp5_do_cmd(&desc, op->cmd_q);
  365. }
  366. static int ccp5_perform_rsa(struct ccp_op *op)
  367. {
  368. struct ccp5_desc desc;
  369. union ccp_function function;
  370. op->cmd_q->total_rsa_ops++;
  371. /* Zero out all the fields of the command desc */
  372. memset(&desc, 0, Q_DESC_SIZE);
  373. CCP5_CMD_ENGINE(&desc) = CCP_ENGINE_RSA;
  374. CCP5_CMD_SOC(&desc) = op->soc;
  375. CCP5_CMD_IOC(&desc) = 1;
  376. CCP5_CMD_INIT(&desc) = 0;
  377. CCP5_CMD_EOM(&desc) = 1;
  378. CCP5_CMD_PROT(&desc) = 0;
  379. function.raw = 0;
  380. CCP_RSA_SIZE(&function) = (op->u.rsa.mod_size + 7) >> 3;
  381. CCP5_CMD_FUNCTION(&desc) = function.raw;
  382. CCP5_CMD_LEN(&desc) = op->u.rsa.input_len;
  383. /* Source is from external memory */
  384. CCP5_CMD_SRC_LO(&desc) = ccp_addr_lo(&op->src.u.dma);
  385. CCP5_CMD_SRC_HI(&desc) = ccp_addr_hi(&op->src.u.dma);
  386. CCP5_CMD_SRC_MEM(&desc) = CCP_MEMTYPE_SYSTEM;
  387. /* Destination is in external memory */
  388. CCP5_CMD_DST_LO(&desc) = ccp_addr_lo(&op->dst.u.dma);
  389. CCP5_CMD_DST_HI(&desc) = ccp_addr_hi(&op->dst.u.dma);
  390. CCP5_CMD_DST_MEM(&desc) = CCP_MEMTYPE_SYSTEM;
  391. /* Key (Exponent) is in external memory */
  392. CCP5_CMD_KEY_LO(&desc) = ccp_addr_lo(&op->exp.u.dma);
  393. CCP5_CMD_KEY_HI(&desc) = ccp_addr_hi(&op->exp.u.dma);
  394. CCP5_CMD_KEY_MEM(&desc) = CCP_MEMTYPE_SYSTEM;
  395. return ccp5_do_cmd(&desc, op->cmd_q);
  396. }
  397. static int ccp5_perform_passthru(struct ccp_op *op)
  398. {
  399. struct ccp5_desc desc;
  400. union ccp_function function;
  401. struct ccp_dma_info *saddr = &op->src.u.dma;
  402. struct ccp_dma_info *daddr = &op->dst.u.dma;
  403. op->cmd_q->total_pt_ops++;
  404. memset(&desc, 0, Q_DESC_SIZE);
  405. CCP5_CMD_ENGINE(&desc) = CCP_ENGINE_PASSTHRU;
  406. CCP5_CMD_SOC(&desc) = 0;
  407. CCP5_CMD_IOC(&desc) = 1;
  408. CCP5_CMD_INIT(&desc) = 0;
  409. CCP5_CMD_EOM(&desc) = op->eom;
  410. CCP5_CMD_PROT(&desc) = 0;
  411. function.raw = 0;
  412. CCP_PT_BYTESWAP(&function) = op->u.passthru.byte_swap;
  413. CCP_PT_BITWISE(&function) = op->u.passthru.bit_mod;
  414. CCP5_CMD_FUNCTION(&desc) = function.raw;
  415. /* Length of source data is always 256 bytes */
  416. if (op->src.type == CCP_MEMTYPE_SYSTEM)
  417. CCP5_CMD_LEN(&desc) = saddr->length;
  418. else
  419. CCP5_CMD_LEN(&desc) = daddr->length;
  420. if (op->src.type == CCP_MEMTYPE_SYSTEM) {
  421. CCP5_CMD_SRC_LO(&desc) = ccp_addr_lo(&op->src.u.dma);
  422. CCP5_CMD_SRC_HI(&desc) = ccp_addr_hi(&op->src.u.dma);
  423. CCP5_CMD_SRC_MEM(&desc) = CCP_MEMTYPE_SYSTEM;
  424. if (op->u.passthru.bit_mod != CCP_PASSTHRU_BITWISE_NOOP)
  425. CCP5_CMD_LSB_ID(&desc) = op->sb_key;
  426. } else {
  427. u32 key_addr = op->src.u.sb * CCP_SB_BYTES;
  428. CCP5_CMD_SRC_LO(&desc) = lower_32_bits(key_addr);
  429. CCP5_CMD_SRC_HI(&desc) = 0;
  430. CCP5_CMD_SRC_MEM(&desc) = CCP_MEMTYPE_SB;
  431. }
  432. if (op->dst.type == CCP_MEMTYPE_SYSTEM) {
  433. CCP5_CMD_DST_LO(&desc) = ccp_addr_lo(&op->dst.u.dma);
  434. CCP5_CMD_DST_HI(&desc) = ccp_addr_hi(&op->dst.u.dma);
  435. CCP5_CMD_DST_MEM(&desc) = CCP_MEMTYPE_SYSTEM;
  436. } else {
  437. u32 key_addr = op->dst.u.sb * CCP_SB_BYTES;
  438. CCP5_CMD_DST_LO(&desc) = lower_32_bits(key_addr);
  439. CCP5_CMD_DST_HI(&desc) = 0;
  440. CCP5_CMD_DST_MEM(&desc) = CCP_MEMTYPE_SB;
  441. }
  442. return ccp5_do_cmd(&desc, op->cmd_q);
  443. }
  444. static int ccp5_perform_ecc(struct ccp_op *op)
  445. {
  446. struct ccp5_desc desc;
  447. union ccp_function function;
  448. op->cmd_q->total_ecc_ops++;
  449. /* Zero out all the fields of the command desc */
  450. memset(&desc, 0, Q_DESC_SIZE);
  451. CCP5_CMD_ENGINE(&desc) = CCP_ENGINE_ECC;
  452. CCP5_CMD_SOC(&desc) = 0;
  453. CCP5_CMD_IOC(&desc) = 1;
  454. CCP5_CMD_INIT(&desc) = 0;
  455. CCP5_CMD_EOM(&desc) = 1;
  456. CCP5_CMD_PROT(&desc) = 0;
  457. function.raw = 0;
  458. function.ecc.mode = op->u.ecc.function;
  459. CCP5_CMD_FUNCTION(&desc) = function.raw;
  460. CCP5_CMD_LEN(&desc) = op->src.u.dma.length;
  461. CCP5_CMD_SRC_LO(&desc) = ccp_addr_lo(&op->src.u.dma);
  462. CCP5_CMD_SRC_HI(&desc) = ccp_addr_hi(&op->src.u.dma);
  463. CCP5_CMD_SRC_MEM(&desc) = CCP_MEMTYPE_SYSTEM;
  464. CCP5_CMD_DST_LO(&desc) = ccp_addr_lo(&op->dst.u.dma);
  465. CCP5_CMD_DST_HI(&desc) = ccp_addr_hi(&op->dst.u.dma);
  466. CCP5_CMD_DST_MEM(&desc) = CCP_MEMTYPE_SYSTEM;
  467. return ccp5_do_cmd(&desc, op->cmd_q);
  468. }
  469. static int ccp_find_lsb_regions(struct ccp_cmd_queue *cmd_q, u64 status)
  470. {
  471. int q_mask = 1 << cmd_q->id;
  472. int queues = 0;
  473. int j;
  474. /* Build a bit mask to know which LSBs this queue has access to.
  475. * Don't bother with segment 0 as it has special privileges.
  476. */
  477. for (j = 1; j < MAX_LSB_CNT; j++) {
  478. if (status & q_mask)
  479. bitmap_set(cmd_q->lsbmask, j, 1);
  480. status >>= LSB_REGION_WIDTH;
  481. }
  482. queues = bitmap_weight(cmd_q->lsbmask, MAX_LSB_CNT);
  483. dev_dbg(cmd_q->ccp->dev, "Queue %d can access %d LSB regions\n",
  484. cmd_q->id, queues);
  485. return queues ? 0 : -EINVAL;
  486. }
  487. static int ccp_find_and_assign_lsb_to_q(struct ccp_device *ccp,
  488. int lsb_cnt, int n_lsbs,
  489. unsigned long *lsb_pub)
  490. {
  491. DECLARE_BITMAP(qlsb, MAX_LSB_CNT);
  492. int bitno;
  493. int qlsb_wgt;
  494. int i;
  495. /* For each queue:
  496. * If the count of potential LSBs available to a queue matches the
  497. * ordinal given to us in lsb_cnt:
  498. * Copy the mask of possible LSBs for this queue into "qlsb";
  499. * For each bit in qlsb, see if the corresponding bit in the
  500. * aggregation mask is set; if so, we have a match.
  501. * If we have a match, clear the bit in the aggregation to
  502. * mark it as no longer available.
  503. * If there is no match, clear the bit in qlsb and keep looking.
  504. */
  505. for (i = 0; i < ccp->cmd_q_count; i++) {
  506. struct ccp_cmd_queue *cmd_q = &ccp->cmd_q[i];
  507. qlsb_wgt = bitmap_weight(cmd_q->lsbmask, MAX_LSB_CNT);
  508. if (qlsb_wgt == lsb_cnt) {
  509. bitmap_copy(qlsb, cmd_q->lsbmask, MAX_LSB_CNT);
  510. bitno = find_first_bit(qlsb, MAX_LSB_CNT);
  511. while (bitno < MAX_LSB_CNT) {
  512. if (test_bit(bitno, lsb_pub)) {
  513. /* We found an available LSB
  514. * that this queue can access
  515. */
  516. cmd_q->lsb = bitno;
  517. bitmap_clear(lsb_pub, bitno, 1);
  518. dev_dbg(ccp->dev,
  519. "Queue %d gets LSB %d\n",
  520. i, bitno);
  521. break;
  522. }
  523. bitmap_clear(qlsb, bitno, 1);
  524. bitno = find_first_bit(qlsb, MAX_LSB_CNT);
  525. }
  526. if (bitno >= MAX_LSB_CNT)
  527. return -EINVAL;
  528. n_lsbs--;
  529. }
  530. }
  531. return n_lsbs;
  532. }
  533. /* For each queue, from the most- to least-constrained:
  534. * find an LSB that can be assigned to the queue. If there are N queues that
  535. * can only use M LSBs, where N > M, fail; otherwise, every queue will get a
  536. * dedicated LSB. Remaining LSB regions become a shared resource.
  537. * If we have fewer LSBs than queues, all LSB regions become shared resources.
  538. */
  539. static int ccp_assign_lsbs(struct ccp_device *ccp)
  540. {
  541. DECLARE_BITMAP(lsb_pub, MAX_LSB_CNT);
  542. DECLARE_BITMAP(qlsb, MAX_LSB_CNT);
  543. int n_lsbs = 0;
  544. int bitno;
  545. int i, lsb_cnt;
  546. int rc = 0;
  547. bitmap_zero(lsb_pub, MAX_LSB_CNT);
  548. /* Create an aggregate bitmap to get a total count of available LSBs */
  549. for (i = 0; i < ccp->cmd_q_count; i++)
  550. bitmap_or(lsb_pub,
  551. lsb_pub, ccp->cmd_q[i].lsbmask,
  552. MAX_LSB_CNT);
  553. n_lsbs = bitmap_weight(lsb_pub, MAX_LSB_CNT);
  554. if (n_lsbs >= ccp->cmd_q_count) {
  555. /* We have enough LSBS to give every queue a private LSB.
  556. * Brute force search to start with the queues that are more
  557. * constrained in LSB choice. When an LSB is privately
  558. * assigned, it is removed from the public mask.
  559. * This is an ugly N squared algorithm with some optimization.
  560. */
  561. for (lsb_cnt = 1;
  562. n_lsbs && (lsb_cnt <= MAX_LSB_CNT);
  563. lsb_cnt++) {
  564. rc = ccp_find_and_assign_lsb_to_q(ccp, lsb_cnt, n_lsbs,
  565. lsb_pub);
  566. if (rc < 0)
  567. return -EINVAL;
  568. n_lsbs = rc;
  569. }
  570. }
  571. rc = 0;
  572. /* What's left of the LSBs, according to the public mask, now become
  573. * shared. Any zero bits in the lsb_pub mask represent an LSB region
  574. * that can't be used as a shared resource, so mark the LSB slots for
  575. * them as "in use".
  576. */
  577. bitmap_copy(qlsb, lsb_pub, MAX_LSB_CNT);
  578. bitno = find_first_zero_bit(qlsb, MAX_LSB_CNT);
  579. while (bitno < MAX_LSB_CNT) {
  580. bitmap_set(ccp->lsbmap, bitno * LSB_SIZE, LSB_SIZE);
  581. bitmap_set(qlsb, bitno, 1);
  582. bitno = find_first_zero_bit(qlsb, MAX_LSB_CNT);
  583. }
  584. return rc;
  585. }
  586. static void ccp5_disable_queue_interrupts(struct ccp_device *ccp)
  587. {
  588. unsigned int i;
  589. for (i = 0; i < ccp->cmd_q_count; i++)
  590. iowrite32(0x0, ccp->cmd_q[i].reg_int_enable);
  591. }
  592. static void ccp5_enable_queue_interrupts(struct ccp_device *ccp)
  593. {
  594. unsigned int i;
  595. for (i = 0; i < ccp->cmd_q_count; i++)
  596. iowrite32(SUPPORTED_INTERRUPTS, ccp->cmd_q[i].reg_int_enable);
  597. }
  598. static void ccp5_irq_bh(unsigned long data)
  599. {
  600. struct ccp_device *ccp = (struct ccp_device *)data;
  601. u32 status;
  602. unsigned int i;
  603. for (i = 0; i < ccp->cmd_q_count; i++) {
  604. struct ccp_cmd_queue *cmd_q = &ccp->cmd_q[i];
  605. status = ioread32(cmd_q->reg_interrupt_status);
  606. if (status) {
  607. cmd_q->int_status = status;
  608. cmd_q->q_status = ioread32(cmd_q->reg_status);
  609. cmd_q->q_int_status = ioread32(cmd_q->reg_int_status);
  610. /* On error, only save the first error value */
  611. if ((status & INT_ERROR) && !cmd_q->cmd_error)
  612. cmd_q->cmd_error = CMD_Q_ERROR(cmd_q->q_status);
  613. cmd_q->int_rcvd = 1;
  614. /* Acknowledge the interrupt and wake the kthread */
  615. iowrite32(status, cmd_q->reg_interrupt_status);
  616. wake_up_interruptible(&cmd_q->int_queue);
  617. }
  618. }
  619. ccp5_enable_queue_interrupts(ccp);
  620. }
  621. static irqreturn_t ccp5_irq_handler(int irq, void *data)
  622. {
  623. struct ccp_device *ccp = (struct ccp_device *)data;
  624. ccp5_disable_queue_interrupts(ccp);
  625. ccp->total_interrupts++;
  626. if (ccp->use_tasklet)
  627. tasklet_schedule(&ccp->irq_tasklet);
  628. else
  629. ccp5_irq_bh((unsigned long)ccp);
  630. return IRQ_HANDLED;
  631. }
  632. static int ccp5_init(struct ccp_device *ccp)
  633. {
  634. struct device *dev = ccp->dev;
  635. struct ccp_cmd_queue *cmd_q;
  636. struct dma_pool *dma_pool;
  637. char dma_pool_name[MAX_DMAPOOL_NAME_LEN];
  638. unsigned int qmr, i;
  639. u64 status;
  640. u32 status_lo, status_hi;
  641. int ret;
  642. /* Find available queues */
  643. qmr = ioread32(ccp->io_regs + Q_MASK_REG);
  644. for (i = 0; (i < MAX_HW_QUEUES) && (ccp->cmd_q_count < ccp->max_q_count); i++) {
  645. if (!(qmr & (1 << i)))
  646. continue;
  647. /* Allocate a dma pool for this queue */
  648. snprintf(dma_pool_name, sizeof(dma_pool_name), "%s_q%d",
  649. ccp->name, i);
  650. dma_pool = dma_pool_create(dma_pool_name, dev,
  651. CCP_DMAPOOL_MAX_SIZE,
  652. CCP_DMAPOOL_ALIGN, 0);
  653. if (!dma_pool) {
  654. dev_err(dev, "unable to allocate dma pool\n");
  655. ret = -ENOMEM;
  656. goto e_pool;
  657. }
  658. cmd_q = &ccp->cmd_q[ccp->cmd_q_count];
  659. ccp->cmd_q_count++;
  660. cmd_q->ccp = ccp;
  661. cmd_q->id = i;
  662. cmd_q->dma_pool = dma_pool;
  663. mutex_init(&cmd_q->q_mutex);
  664. /* Page alignment satisfies our needs for N <= 128 */
  665. BUILD_BUG_ON(COMMANDS_PER_QUEUE > 128);
  666. cmd_q->qsize = Q_SIZE(Q_DESC_SIZE);
  667. cmd_q->qbase = dmam_alloc_coherent(dev, cmd_q->qsize,
  668. &cmd_q->qbase_dma,
  669. GFP_KERNEL);
  670. if (!cmd_q->qbase) {
  671. dev_err(dev, "unable to allocate command queue\n");
  672. ret = -ENOMEM;
  673. goto e_pool;
  674. }
  675. cmd_q->qidx = 0;
  676. /* Preset some register values and masks that are queue
  677. * number dependent
  678. */
  679. cmd_q->reg_control = ccp->io_regs +
  680. CMD5_Q_STATUS_INCR * (i + 1);
  681. cmd_q->reg_tail_lo = cmd_q->reg_control + CMD5_Q_TAIL_LO_BASE;
  682. cmd_q->reg_head_lo = cmd_q->reg_control + CMD5_Q_HEAD_LO_BASE;
  683. cmd_q->reg_int_enable = cmd_q->reg_control +
  684. CMD5_Q_INT_ENABLE_BASE;
  685. cmd_q->reg_interrupt_status = cmd_q->reg_control +
  686. CMD5_Q_INTERRUPT_STATUS_BASE;
  687. cmd_q->reg_status = cmd_q->reg_control + CMD5_Q_STATUS_BASE;
  688. cmd_q->reg_int_status = cmd_q->reg_control +
  689. CMD5_Q_INT_STATUS_BASE;
  690. cmd_q->reg_dma_status = cmd_q->reg_control +
  691. CMD5_Q_DMA_STATUS_BASE;
  692. cmd_q->reg_dma_read_status = cmd_q->reg_control +
  693. CMD5_Q_DMA_READ_STATUS_BASE;
  694. cmd_q->reg_dma_write_status = cmd_q->reg_control +
  695. CMD5_Q_DMA_WRITE_STATUS_BASE;
  696. init_waitqueue_head(&cmd_q->int_queue);
  697. dev_dbg(dev, "queue #%u available\n", i);
  698. }
  699. if (ccp->cmd_q_count == 0) {
  700. dev_notice(dev, "no command queues available\n");
  701. ret = -EIO;
  702. goto e_pool;
  703. }
  704. /* Turn off the queues and disable interrupts until ready */
  705. ccp5_disable_queue_interrupts(ccp);
  706. for (i = 0; i < ccp->cmd_q_count; i++) {
  707. cmd_q = &ccp->cmd_q[i];
  708. cmd_q->qcontrol = 0; /* Start with nothing */
  709. iowrite32(cmd_q->qcontrol, cmd_q->reg_control);
  710. ioread32(cmd_q->reg_int_status);
  711. ioread32(cmd_q->reg_status);
  712. /* Clear the interrupt status */
  713. iowrite32(SUPPORTED_INTERRUPTS, cmd_q->reg_interrupt_status);
  714. }
  715. dev_dbg(dev, "Requesting an IRQ...\n");
  716. /* Request an irq */
  717. ret = sp_request_ccp_irq(ccp->sp, ccp5_irq_handler, ccp->name, ccp);
  718. if (ret) {
  719. dev_err(dev, "unable to allocate an IRQ\n");
  720. goto e_pool;
  721. }
  722. /* Initialize the ISR tasklet */
  723. if (ccp->use_tasklet)
  724. tasklet_init(&ccp->irq_tasklet, ccp5_irq_bh,
  725. (unsigned long)ccp);
  726. dev_dbg(dev, "Loading LSB map...\n");
  727. /* Copy the private LSB mask to the public registers */
  728. status_lo = ioread32(ccp->io_regs + LSB_PRIVATE_MASK_LO_OFFSET);
  729. status_hi = ioread32(ccp->io_regs + LSB_PRIVATE_MASK_HI_OFFSET);
  730. iowrite32(status_lo, ccp->io_regs + LSB_PUBLIC_MASK_LO_OFFSET);
  731. iowrite32(status_hi, ccp->io_regs + LSB_PUBLIC_MASK_HI_OFFSET);
  732. status = ((u64)status_hi<<30) | (u64)status_lo;
  733. dev_dbg(dev, "Configuring virtual queues...\n");
  734. /* Configure size of each virtual queue accessible to host */
  735. for (i = 0; i < ccp->cmd_q_count; i++) {
  736. u32 dma_addr_lo;
  737. u32 dma_addr_hi;
  738. cmd_q = &ccp->cmd_q[i];
  739. cmd_q->qcontrol &= ~(CMD5_Q_SIZE << CMD5_Q_SHIFT);
  740. cmd_q->qcontrol |= QUEUE_SIZE_VAL << CMD5_Q_SHIFT;
  741. cmd_q->qdma_tail = cmd_q->qbase_dma;
  742. dma_addr_lo = low_address(cmd_q->qdma_tail);
  743. iowrite32((u32)dma_addr_lo, cmd_q->reg_tail_lo);
  744. iowrite32((u32)dma_addr_lo, cmd_q->reg_head_lo);
  745. dma_addr_hi = high_address(cmd_q->qdma_tail);
  746. cmd_q->qcontrol |= (dma_addr_hi << 16);
  747. iowrite32(cmd_q->qcontrol, cmd_q->reg_control);
  748. /* Find the LSB regions accessible to the queue */
  749. ccp_find_lsb_regions(cmd_q, status);
  750. cmd_q->lsb = -1; /* Unassigned value */
  751. }
  752. dev_dbg(dev, "Assigning LSBs...\n");
  753. ret = ccp_assign_lsbs(ccp);
  754. if (ret) {
  755. dev_err(dev, "Unable to assign LSBs (%d)\n", ret);
  756. goto e_irq;
  757. }
  758. /* Optimization: pre-allocate LSB slots for each queue */
  759. for (i = 0; i < ccp->cmd_q_count; i++) {
  760. ccp->cmd_q[i].sb_key = ccp_lsb_alloc(&ccp->cmd_q[i], 2);
  761. ccp->cmd_q[i].sb_ctx = ccp_lsb_alloc(&ccp->cmd_q[i], 2);
  762. }
  763. dev_dbg(dev, "Starting threads...\n");
  764. /* Create a kthread for each queue */
  765. for (i = 0; i < ccp->cmd_q_count; i++) {
  766. struct task_struct *kthread;
  767. cmd_q = &ccp->cmd_q[i];
  768. kthread = kthread_create(ccp_cmd_queue_thread, cmd_q,
  769. "%s-q%u", ccp->name, cmd_q->id);
  770. if (IS_ERR(kthread)) {
  771. dev_err(dev, "error creating queue thread (%ld)\n",
  772. PTR_ERR(kthread));
  773. ret = PTR_ERR(kthread);
  774. goto e_kthread;
  775. }
  776. cmd_q->kthread = kthread;
  777. wake_up_process(kthread);
  778. }
  779. dev_dbg(dev, "Enabling interrupts...\n");
  780. ccp5_enable_queue_interrupts(ccp);
  781. dev_dbg(dev, "Registering device...\n");
  782. /* Put this on the unit list to make it available */
  783. ccp_add_device(ccp);
  784. ret = ccp_register_rng(ccp);
  785. if (ret)
  786. goto e_kthread;
  787. /* Register the DMA engine support */
  788. ret = ccp_dmaengine_register(ccp);
  789. if (ret)
  790. goto e_hwrng;
  791. #ifdef CONFIG_CRYPTO_DEV_CCP_DEBUGFS
  792. /* Set up debugfs entries */
  793. ccp5_debugfs_setup(ccp);
  794. #endif
  795. return 0;
  796. e_hwrng:
  797. ccp_unregister_rng(ccp);
  798. e_kthread:
  799. for (i = 0; i < ccp->cmd_q_count; i++)
  800. if (ccp->cmd_q[i].kthread)
  801. kthread_stop(ccp->cmd_q[i].kthread);
  802. e_irq:
  803. sp_free_ccp_irq(ccp->sp, ccp);
  804. e_pool:
  805. for (i = 0; i < ccp->cmd_q_count; i++)
  806. dma_pool_destroy(ccp->cmd_q[i].dma_pool);
  807. return ret;
  808. }
  809. static void ccp5_destroy(struct ccp_device *ccp)
  810. {
  811. struct ccp_cmd_queue *cmd_q;
  812. struct ccp_cmd *cmd;
  813. unsigned int i;
  814. /* Unregister the DMA engine */
  815. ccp_dmaengine_unregister(ccp);
  816. /* Unregister the RNG */
  817. ccp_unregister_rng(ccp);
  818. /* Remove this device from the list of available units first */
  819. ccp_del_device(ccp);
  820. #ifdef CONFIG_CRYPTO_DEV_CCP_DEBUGFS
  821. /* We're in the process of tearing down the entire driver;
  822. * when all the devices are gone clean up debugfs
  823. */
  824. if (ccp_present())
  825. ccp5_debugfs_destroy();
  826. #endif
  827. /* Disable and clear interrupts */
  828. ccp5_disable_queue_interrupts(ccp);
  829. for (i = 0; i < ccp->cmd_q_count; i++) {
  830. cmd_q = &ccp->cmd_q[i];
  831. /* Turn off the run bit */
  832. iowrite32(cmd_q->qcontrol & ~CMD5_Q_RUN, cmd_q->reg_control);
  833. /* Clear the interrupt status */
  834. iowrite32(SUPPORTED_INTERRUPTS, cmd_q->reg_interrupt_status);
  835. ioread32(cmd_q->reg_int_status);
  836. ioread32(cmd_q->reg_status);
  837. }
  838. /* Stop the queue kthreads */
  839. for (i = 0; i < ccp->cmd_q_count; i++)
  840. if (ccp->cmd_q[i].kthread)
  841. kthread_stop(ccp->cmd_q[i].kthread);
  842. sp_free_ccp_irq(ccp->sp, ccp);
  843. /* Flush the cmd and backlog queue */
  844. while (!list_empty(&ccp->cmd)) {
  845. /* Invoke the callback directly with an error code */
  846. cmd = list_first_entry(&ccp->cmd, struct ccp_cmd, entry);
  847. list_del(&cmd->entry);
  848. cmd->callback(cmd->data, -ENODEV);
  849. }
  850. while (!list_empty(&ccp->backlog)) {
  851. /* Invoke the callback directly with an error code */
  852. cmd = list_first_entry(&ccp->backlog, struct ccp_cmd, entry);
  853. list_del(&cmd->entry);
  854. cmd->callback(cmd->data, -ENODEV);
  855. }
  856. }
  857. static void ccp5_config(struct ccp_device *ccp)
  858. {
  859. /* Public side */
  860. iowrite32(0x0, ccp->io_regs + CMD5_REQID_CONFIG_OFFSET);
  861. }
  862. static void ccp5other_config(struct ccp_device *ccp)
  863. {
  864. int i;
  865. u32 rnd;
  866. /* We own all of the queues on the NTB CCP */
  867. iowrite32(0x00012D57, ccp->io_regs + CMD5_TRNG_CTL_OFFSET);
  868. iowrite32(0x00000003, ccp->io_regs + CMD5_CONFIG_0_OFFSET);
  869. for (i = 0; i < 12; i++) {
  870. rnd = ioread32(ccp->io_regs + TRNG_OUT_REG);
  871. iowrite32(rnd, ccp->io_regs + CMD5_AES_MASK_OFFSET);
  872. }
  873. iowrite32(0x0000001F, ccp->io_regs + CMD5_QUEUE_MASK_OFFSET);
  874. iowrite32(0x00005B6D, ccp->io_regs + CMD5_QUEUE_PRIO_OFFSET);
  875. iowrite32(0x00000000, ccp->io_regs + CMD5_CMD_TIMEOUT_OFFSET);
  876. iowrite32(0x3FFFFFFF, ccp->io_regs + LSB_PRIVATE_MASK_LO_OFFSET);
  877. iowrite32(0x000003FF, ccp->io_regs + LSB_PRIVATE_MASK_HI_OFFSET);
  878. iowrite32(0x00108823, ccp->io_regs + CMD5_CLK_GATE_CTL_OFFSET);
  879. ccp5_config(ccp);
  880. }
  881. /* Version 5 adds some function, but is essentially the same as v5 */
  882. static const struct ccp_actions ccp5_actions = {
  883. .aes = ccp5_perform_aes,
  884. .xts_aes = ccp5_perform_xts_aes,
  885. .sha = ccp5_perform_sha,
  886. .des3 = ccp5_perform_des3,
  887. .rsa = ccp5_perform_rsa,
  888. .passthru = ccp5_perform_passthru,
  889. .ecc = ccp5_perform_ecc,
  890. .sballoc = ccp_lsb_alloc,
  891. .sbfree = ccp_lsb_free,
  892. .init = ccp5_init,
  893. .destroy = ccp5_destroy,
  894. .get_free_slots = ccp5_get_free_slots,
  895. };
  896. const struct ccp_vdata ccpv5a = {
  897. .version = CCP_VERSION(5, 0),
  898. .setup = ccp5_config,
  899. .perform = &ccp5_actions,
  900. .offset = 0x0,
  901. .rsamax = CCP5_RSA_MAX_WIDTH,
  902. };
  903. const struct ccp_vdata ccpv5b = {
  904. .version = CCP_VERSION(5, 0),
  905. .dma_chan_attr = DMA_PRIVATE,
  906. .setup = ccp5other_config,
  907. .perform = &ccp5_actions,
  908. .offset = 0x0,
  909. .rsamax = CCP5_RSA_MAX_WIDTH,
  910. };