ccp-dev-v5.c 30 KB

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