ccp-dev.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. /*
  2. * AMD Cryptographic Coprocessor (CCP) driver
  3. *
  4. * Copyright (C) 2013,2017 Advanced Micro Devices, Inc.
  5. *
  6. * Author: Tom Lendacky <thomas.lendacky@amd.com>
  7. * Author: Gary R Hook <gary.hook@amd.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/kernel.h>
  15. #include <linux/kthread.h>
  16. #include <linux/sched.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/spinlock.h>
  19. #include <linux/spinlock_types.h>
  20. #include <linux/types.h>
  21. #include <linux/mutex.h>
  22. #include <linux/delay.h>
  23. #include <linux/hw_random.h>
  24. #include <linux/cpu.h>
  25. #ifdef CONFIG_X86
  26. #include <asm/cpu_device_id.h>
  27. #endif
  28. #include <linux/ccp.h>
  29. #include "ccp-dev.h"
  30. MODULE_AUTHOR("Tom Lendacky <thomas.lendacky@amd.com>");
  31. MODULE_AUTHOR("Gary R Hook <gary.hook@amd.com>");
  32. MODULE_LICENSE("GPL");
  33. MODULE_VERSION("1.1.0");
  34. MODULE_DESCRIPTION("AMD Cryptographic Coprocessor driver");
  35. struct ccp_tasklet_data {
  36. struct completion completion;
  37. struct ccp_cmd *cmd;
  38. };
  39. /* Human-readable error strings */
  40. static char *ccp_error_codes[] = {
  41. "",
  42. "ERR 01: ILLEGAL_ENGINE",
  43. "ERR 02: ILLEGAL_KEY_ID",
  44. "ERR 03: ILLEGAL_FUNCTION_TYPE",
  45. "ERR 04: ILLEGAL_FUNCTION_MODE",
  46. "ERR 05: ILLEGAL_FUNCTION_ENCRYPT",
  47. "ERR 06: ILLEGAL_FUNCTION_SIZE",
  48. "ERR 07: Zlib_MISSING_INIT_EOM",
  49. "ERR 08: ILLEGAL_FUNCTION_RSVD",
  50. "ERR 09: ILLEGAL_BUFFER_LENGTH",
  51. "ERR 10: VLSB_FAULT",
  52. "ERR 11: ILLEGAL_MEM_ADDR",
  53. "ERR 12: ILLEGAL_MEM_SEL",
  54. "ERR 13: ILLEGAL_CONTEXT_ID",
  55. "ERR 14: ILLEGAL_KEY_ADDR",
  56. "ERR 15: 0xF Reserved",
  57. "ERR 16: Zlib_ILLEGAL_MULTI_QUEUE",
  58. "ERR 17: Zlib_ILLEGAL_JOBID_CHANGE",
  59. "ERR 18: CMD_TIMEOUT",
  60. "ERR 19: IDMA0_AXI_SLVERR",
  61. "ERR 20: IDMA0_AXI_DECERR",
  62. "ERR 21: 0x15 Reserved",
  63. "ERR 22: IDMA1_AXI_SLAVE_FAULT",
  64. "ERR 23: IDMA1_AIXI_DECERR",
  65. "ERR 24: 0x18 Reserved",
  66. "ERR 25: ZLIBVHB_AXI_SLVERR",
  67. "ERR 26: ZLIBVHB_AXI_DECERR",
  68. "ERR 27: 0x1B Reserved",
  69. "ERR 27: ZLIB_UNEXPECTED_EOM",
  70. "ERR 27: ZLIB_EXTRA_DATA",
  71. "ERR 30: ZLIB_BTYPE",
  72. "ERR 31: ZLIB_UNDEFINED_SYMBOL",
  73. "ERR 32: ZLIB_UNDEFINED_DISTANCE_S",
  74. "ERR 33: ZLIB_CODE_LENGTH_SYMBOL",
  75. "ERR 34: ZLIB _VHB_ILLEGAL_FETCH",
  76. "ERR 35: ZLIB_UNCOMPRESSED_LEN",
  77. "ERR 36: ZLIB_LIMIT_REACHED",
  78. "ERR 37: ZLIB_CHECKSUM_MISMATCH0",
  79. "ERR 38: ODMA0_AXI_SLVERR",
  80. "ERR 39: ODMA0_AXI_DECERR",
  81. "ERR 40: 0x28 Reserved",
  82. "ERR 41: ODMA1_AXI_SLVERR",
  83. "ERR 42: ODMA1_AXI_DECERR",
  84. "ERR 43: LSB_PARITY_ERR",
  85. };
  86. void ccp_log_error(struct ccp_device *d, int e)
  87. {
  88. dev_err(d->dev, "CCP error: %s (0x%x)\n", ccp_error_codes[e], e);
  89. }
  90. /* List of CCPs, CCP count, read-write access lock, and access functions
  91. *
  92. * Lock structure: get ccp_unit_lock for reading whenever we need to
  93. * examine the CCP list. While holding it for reading we can acquire
  94. * the RR lock to update the round-robin next-CCP pointer. The unit lock
  95. * must be acquired before the RR lock.
  96. *
  97. * If the unit-lock is acquired for writing, we have total control over
  98. * the list, so there's no value in getting the RR lock.
  99. */
  100. static DEFINE_RWLOCK(ccp_unit_lock);
  101. static LIST_HEAD(ccp_units);
  102. /* Round-robin counter */
  103. static DEFINE_SPINLOCK(ccp_rr_lock);
  104. static struct ccp_device *ccp_rr;
  105. /**
  106. * ccp_add_device - add a CCP device to the list
  107. *
  108. * @ccp: ccp_device struct pointer
  109. *
  110. * Put this CCP on the unit list, which makes it available
  111. * for use.
  112. *
  113. * Returns zero if a CCP device is present, -ENODEV otherwise.
  114. */
  115. void ccp_add_device(struct ccp_device *ccp)
  116. {
  117. unsigned long flags;
  118. write_lock_irqsave(&ccp_unit_lock, flags);
  119. list_add_tail(&ccp->entry, &ccp_units);
  120. if (!ccp_rr)
  121. /* We already have the list lock (we're first) so this
  122. * pointer can't change on us. Set its initial value.
  123. */
  124. ccp_rr = ccp;
  125. write_unlock_irqrestore(&ccp_unit_lock, flags);
  126. }
  127. /**
  128. * ccp_del_device - remove a CCP device from the list
  129. *
  130. * @ccp: ccp_device struct pointer
  131. *
  132. * Remove this unit from the list of devices. If the next device
  133. * up for use is this one, adjust the pointer. If this is the last
  134. * device, NULL the pointer.
  135. */
  136. void ccp_del_device(struct ccp_device *ccp)
  137. {
  138. unsigned long flags;
  139. write_lock_irqsave(&ccp_unit_lock, flags);
  140. if (ccp_rr == ccp) {
  141. /* ccp_unit_lock is read/write; any read access
  142. * will be suspended while we make changes to the
  143. * list and RR pointer.
  144. */
  145. if (list_is_last(&ccp_rr->entry, &ccp_units))
  146. ccp_rr = list_first_entry(&ccp_units, struct ccp_device,
  147. entry);
  148. else
  149. ccp_rr = list_next_entry(ccp_rr, entry);
  150. }
  151. list_del(&ccp->entry);
  152. if (list_empty(&ccp_units))
  153. ccp_rr = NULL;
  154. write_unlock_irqrestore(&ccp_unit_lock, flags);
  155. }
  156. int ccp_register_rng(struct ccp_device *ccp)
  157. {
  158. int ret = 0;
  159. dev_dbg(ccp->dev, "Registering RNG...\n");
  160. /* Register an RNG */
  161. ccp->hwrng.name = ccp->rngname;
  162. ccp->hwrng.read = ccp_trng_read;
  163. ret = hwrng_register(&ccp->hwrng);
  164. if (ret)
  165. dev_err(ccp->dev, "error registering hwrng (%d)\n", ret);
  166. return ret;
  167. }
  168. void ccp_unregister_rng(struct ccp_device *ccp)
  169. {
  170. if (ccp->hwrng.name)
  171. hwrng_unregister(&ccp->hwrng);
  172. }
  173. static struct ccp_device *ccp_get_device(void)
  174. {
  175. unsigned long flags;
  176. struct ccp_device *dp = NULL;
  177. /* We round-robin through the unit list.
  178. * The (ccp_rr) pointer refers to the next unit to use.
  179. */
  180. read_lock_irqsave(&ccp_unit_lock, flags);
  181. if (!list_empty(&ccp_units)) {
  182. spin_lock(&ccp_rr_lock);
  183. dp = ccp_rr;
  184. if (list_is_last(&ccp_rr->entry, &ccp_units))
  185. ccp_rr = list_first_entry(&ccp_units, struct ccp_device,
  186. entry);
  187. else
  188. ccp_rr = list_next_entry(ccp_rr, entry);
  189. spin_unlock(&ccp_rr_lock);
  190. }
  191. read_unlock_irqrestore(&ccp_unit_lock, flags);
  192. return dp;
  193. }
  194. /**
  195. * ccp_present - check if a CCP device is present
  196. *
  197. * Returns zero if a CCP device is present, -ENODEV otherwise.
  198. */
  199. int ccp_present(void)
  200. {
  201. unsigned long flags;
  202. int ret;
  203. read_lock_irqsave(&ccp_unit_lock, flags);
  204. ret = list_empty(&ccp_units);
  205. read_unlock_irqrestore(&ccp_unit_lock, flags);
  206. return ret ? -ENODEV : 0;
  207. }
  208. EXPORT_SYMBOL_GPL(ccp_present);
  209. /**
  210. * ccp_version - get the version of the CCP device
  211. *
  212. * Returns the version from the first unit on the list;
  213. * otherwise a zero if no CCP device is present
  214. */
  215. unsigned int ccp_version(void)
  216. {
  217. struct ccp_device *dp;
  218. unsigned long flags;
  219. int ret = 0;
  220. read_lock_irqsave(&ccp_unit_lock, flags);
  221. if (!list_empty(&ccp_units)) {
  222. dp = list_first_entry(&ccp_units, struct ccp_device, entry);
  223. ret = dp->vdata->version;
  224. }
  225. read_unlock_irqrestore(&ccp_unit_lock, flags);
  226. return ret;
  227. }
  228. EXPORT_SYMBOL_GPL(ccp_version);
  229. /**
  230. * ccp_enqueue_cmd - queue an operation for processing by the CCP
  231. *
  232. * @cmd: ccp_cmd struct to be processed
  233. *
  234. * Queue a cmd to be processed by the CCP. If queueing the cmd
  235. * would exceed the defined length of the cmd queue the cmd will
  236. * only be queued if the CCP_CMD_MAY_BACKLOG flag is set and will
  237. * result in a return code of -EBUSY.
  238. *
  239. * The callback routine specified in the ccp_cmd struct will be
  240. * called to notify the caller of completion (if the cmd was not
  241. * backlogged) or advancement out of the backlog. If the cmd has
  242. * advanced out of the backlog the "err" value of the callback
  243. * will be -EINPROGRESS. Any other "err" value during callback is
  244. * the result of the operation.
  245. *
  246. * The cmd has been successfully queued if:
  247. * the return code is -EINPROGRESS or
  248. * the return code is -EBUSY and CCP_CMD_MAY_BACKLOG flag is set
  249. */
  250. int ccp_enqueue_cmd(struct ccp_cmd *cmd)
  251. {
  252. struct ccp_device *ccp;
  253. unsigned long flags;
  254. unsigned int i;
  255. int ret;
  256. /* Some commands might need to be sent to a specific device */
  257. ccp = cmd->ccp ? cmd->ccp : ccp_get_device();
  258. if (!ccp)
  259. return -ENODEV;
  260. /* Caller must supply a callback routine */
  261. if (!cmd->callback)
  262. return -EINVAL;
  263. cmd->ccp = ccp;
  264. spin_lock_irqsave(&ccp->cmd_lock, flags);
  265. i = ccp->cmd_q_count;
  266. if (ccp->cmd_count >= MAX_CMD_QLEN) {
  267. ret = -EBUSY;
  268. if (cmd->flags & CCP_CMD_MAY_BACKLOG)
  269. list_add_tail(&cmd->entry, &ccp->backlog);
  270. } else {
  271. ret = -EINPROGRESS;
  272. ccp->cmd_count++;
  273. list_add_tail(&cmd->entry, &ccp->cmd);
  274. /* Find an idle queue */
  275. if (!ccp->suspending) {
  276. for (i = 0; i < ccp->cmd_q_count; i++) {
  277. if (ccp->cmd_q[i].active)
  278. continue;
  279. break;
  280. }
  281. }
  282. }
  283. spin_unlock_irqrestore(&ccp->cmd_lock, flags);
  284. /* If we found an idle queue, wake it up */
  285. if (i < ccp->cmd_q_count)
  286. wake_up_process(ccp->cmd_q[i].kthread);
  287. return ret;
  288. }
  289. EXPORT_SYMBOL_GPL(ccp_enqueue_cmd);
  290. static void ccp_do_cmd_backlog(struct work_struct *work)
  291. {
  292. struct ccp_cmd *cmd = container_of(work, struct ccp_cmd, work);
  293. struct ccp_device *ccp = cmd->ccp;
  294. unsigned long flags;
  295. unsigned int i;
  296. cmd->callback(cmd->data, -EINPROGRESS);
  297. spin_lock_irqsave(&ccp->cmd_lock, flags);
  298. ccp->cmd_count++;
  299. list_add_tail(&cmd->entry, &ccp->cmd);
  300. /* Find an idle queue */
  301. for (i = 0; i < ccp->cmd_q_count; i++) {
  302. if (ccp->cmd_q[i].active)
  303. continue;
  304. break;
  305. }
  306. spin_unlock_irqrestore(&ccp->cmd_lock, flags);
  307. /* If we found an idle queue, wake it up */
  308. if (i < ccp->cmd_q_count)
  309. wake_up_process(ccp->cmd_q[i].kthread);
  310. }
  311. static struct ccp_cmd *ccp_dequeue_cmd(struct ccp_cmd_queue *cmd_q)
  312. {
  313. struct ccp_device *ccp = cmd_q->ccp;
  314. struct ccp_cmd *cmd = NULL;
  315. struct ccp_cmd *backlog = NULL;
  316. unsigned long flags;
  317. spin_lock_irqsave(&ccp->cmd_lock, flags);
  318. cmd_q->active = 0;
  319. if (ccp->suspending) {
  320. cmd_q->suspended = 1;
  321. spin_unlock_irqrestore(&ccp->cmd_lock, flags);
  322. wake_up_interruptible(&ccp->suspend_queue);
  323. return NULL;
  324. }
  325. if (ccp->cmd_count) {
  326. cmd_q->active = 1;
  327. cmd = list_first_entry(&ccp->cmd, struct ccp_cmd, entry);
  328. list_del(&cmd->entry);
  329. ccp->cmd_count--;
  330. }
  331. if (!list_empty(&ccp->backlog)) {
  332. backlog = list_first_entry(&ccp->backlog, struct ccp_cmd,
  333. entry);
  334. list_del(&backlog->entry);
  335. }
  336. spin_unlock_irqrestore(&ccp->cmd_lock, flags);
  337. if (backlog) {
  338. INIT_WORK(&backlog->work, ccp_do_cmd_backlog);
  339. schedule_work(&backlog->work);
  340. }
  341. return cmd;
  342. }
  343. static void ccp_do_cmd_complete(unsigned long data)
  344. {
  345. struct ccp_tasklet_data *tdata = (struct ccp_tasklet_data *)data;
  346. struct ccp_cmd *cmd = tdata->cmd;
  347. cmd->callback(cmd->data, cmd->ret);
  348. complete(&tdata->completion);
  349. }
  350. /**
  351. * ccp_cmd_queue_thread - create a kernel thread to manage a CCP queue
  352. *
  353. * @data: thread-specific data
  354. */
  355. int ccp_cmd_queue_thread(void *data)
  356. {
  357. struct ccp_cmd_queue *cmd_q = (struct ccp_cmd_queue *)data;
  358. struct ccp_cmd *cmd;
  359. struct ccp_tasklet_data tdata;
  360. struct tasklet_struct tasklet;
  361. tasklet_init(&tasklet, ccp_do_cmd_complete, (unsigned long)&tdata);
  362. set_current_state(TASK_INTERRUPTIBLE);
  363. while (!kthread_should_stop()) {
  364. schedule();
  365. set_current_state(TASK_INTERRUPTIBLE);
  366. cmd = ccp_dequeue_cmd(cmd_q);
  367. if (!cmd)
  368. continue;
  369. __set_current_state(TASK_RUNNING);
  370. /* Execute the command */
  371. cmd->ret = ccp_run_cmd(cmd_q, cmd);
  372. /* Schedule the completion callback */
  373. tdata.cmd = cmd;
  374. init_completion(&tdata.completion);
  375. tasklet_schedule(&tasklet);
  376. wait_for_completion(&tdata.completion);
  377. }
  378. __set_current_state(TASK_RUNNING);
  379. return 0;
  380. }
  381. /**
  382. * ccp_alloc_struct - allocate and initialize the ccp_device struct
  383. *
  384. * @dev: device struct of the CCP
  385. */
  386. struct ccp_device *ccp_alloc_struct(struct sp_device *sp)
  387. {
  388. struct device *dev = sp->dev;
  389. struct ccp_device *ccp;
  390. ccp = devm_kzalloc(dev, sizeof(*ccp), GFP_KERNEL);
  391. if (!ccp)
  392. return NULL;
  393. ccp->dev = dev;
  394. ccp->sp = sp;
  395. ccp->axcache = sp->axcache;
  396. INIT_LIST_HEAD(&ccp->cmd);
  397. INIT_LIST_HEAD(&ccp->backlog);
  398. spin_lock_init(&ccp->cmd_lock);
  399. mutex_init(&ccp->req_mutex);
  400. mutex_init(&ccp->sb_mutex);
  401. ccp->sb_count = KSB_COUNT;
  402. ccp->sb_start = 0;
  403. /* Initialize the wait queues */
  404. init_waitqueue_head(&ccp->sb_queue);
  405. init_waitqueue_head(&ccp->suspend_queue);
  406. snprintf(ccp->name, MAX_CCP_NAME_LEN, "ccp-%u", sp->ord);
  407. snprintf(ccp->rngname, MAX_CCP_NAME_LEN, "ccp-%u-rng", sp->ord);
  408. return ccp;
  409. }
  410. int ccp_trng_read(struct hwrng *rng, void *data, size_t max, bool wait)
  411. {
  412. struct ccp_device *ccp = container_of(rng, struct ccp_device, hwrng);
  413. u32 trng_value;
  414. int len = min_t(int, sizeof(trng_value), max);
  415. /* Locking is provided by the caller so we can update device
  416. * hwrng-related fields safely
  417. */
  418. trng_value = ioread32(ccp->io_regs + TRNG_OUT_REG);
  419. if (!trng_value) {
  420. /* Zero is returned if not data is available or if a
  421. * bad-entropy error is present. Assume an error if
  422. * we exceed TRNG_RETRIES reads of zero.
  423. */
  424. if (ccp->hwrng_retries++ > TRNG_RETRIES)
  425. return -EIO;
  426. return 0;
  427. }
  428. /* Reset the counter and save the rng value */
  429. ccp->hwrng_retries = 0;
  430. memcpy(data, &trng_value, len);
  431. return len;
  432. }
  433. #ifdef CONFIG_PM
  434. bool ccp_queues_suspended(struct ccp_device *ccp)
  435. {
  436. unsigned int suspended = 0;
  437. unsigned long flags;
  438. unsigned int i;
  439. spin_lock_irqsave(&ccp->cmd_lock, flags);
  440. for (i = 0; i < ccp->cmd_q_count; i++)
  441. if (ccp->cmd_q[i].suspended)
  442. suspended++;
  443. spin_unlock_irqrestore(&ccp->cmd_lock, flags);
  444. return ccp->cmd_q_count == suspended;
  445. }
  446. int ccp_dev_suspend(struct sp_device *sp, pm_message_t state)
  447. {
  448. struct ccp_device *ccp = sp->ccp_data;
  449. unsigned long flags;
  450. unsigned int i;
  451. spin_lock_irqsave(&ccp->cmd_lock, flags);
  452. ccp->suspending = 1;
  453. /* Wake all the queue kthreads to prepare for suspend */
  454. for (i = 0; i < ccp->cmd_q_count; i++)
  455. wake_up_process(ccp->cmd_q[i].kthread);
  456. spin_unlock_irqrestore(&ccp->cmd_lock, flags);
  457. /* Wait for all queue kthreads to say they're done */
  458. while (!ccp_queues_suspended(ccp))
  459. wait_event_interruptible(ccp->suspend_queue,
  460. ccp_queues_suspended(ccp));
  461. return 0;
  462. }
  463. int ccp_dev_resume(struct sp_device *sp)
  464. {
  465. struct ccp_device *ccp = sp->ccp_data;
  466. unsigned long flags;
  467. unsigned int i;
  468. spin_lock_irqsave(&ccp->cmd_lock, flags);
  469. ccp->suspending = 0;
  470. /* Wake up all the kthreads */
  471. for (i = 0; i < ccp->cmd_q_count; i++) {
  472. ccp->cmd_q[i].suspended = 0;
  473. wake_up_process(ccp->cmd_q[i].kthread);
  474. }
  475. spin_unlock_irqrestore(&ccp->cmd_lock, flags);
  476. return 0;
  477. }
  478. #endif
  479. int ccp_dev_init(struct sp_device *sp)
  480. {
  481. struct device *dev = sp->dev;
  482. struct ccp_device *ccp;
  483. int ret;
  484. ret = -ENOMEM;
  485. ccp = ccp_alloc_struct(sp);
  486. if (!ccp)
  487. goto e_err;
  488. sp->ccp_data = ccp;
  489. ccp->vdata = (struct ccp_vdata *)sp->dev_vdata->ccp_vdata;
  490. if (!ccp->vdata || !ccp->vdata->version) {
  491. ret = -ENODEV;
  492. dev_err(dev, "missing driver data\n");
  493. goto e_err;
  494. }
  495. ccp->use_tasklet = sp->use_tasklet;
  496. ccp->io_regs = sp->io_map + ccp->vdata->offset;
  497. if (ccp->vdata->setup)
  498. ccp->vdata->setup(ccp);
  499. ret = ccp->vdata->perform->init(ccp);
  500. if (ret)
  501. goto e_err;
  502. dev_notice(dev, "ccp enabled\n");
  503. return 0;
  504. e_err:
  505. sp->ccp_data = NULL;
  506. dev_notice(dev, "ccp initialization failed\n");
  507. return ret;
  508. }
  509. void ccp_dev_destroy(struct sp_device *sp)
  510. {
  511. struct ccp_device *ccp = sp->ccp_data;
  512. if (!ccp)
  513. return;
  514. ccp->vdata->perform->destroy(ccp);
  515. }