ccp-dev.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. /*
  2. * AMD Cryptographic Coprocessor (CCP) driver
  3. *
  4. * Copyright (C) 2013,2016 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_LICENSE("GPL");
  32. MODULE_VERSION("1.0.0");
  33. MODULE_DESCRIPTION("AMD Cryptographic Coprocessor driver");
  34. struct ccp_tasklet_data {
  35. struct completion completion;
  36. struct ccp_cmd *cmd;
  37. };
  38. /* Human-readable error strings */
  39. char *ccp_error_codes[] = {
  40. "",
  41. "ERR 01: ILLEGAL_ENGINE",
  42. "ERR 02: ILLEGAL_KEY_ID",
  43. "ERR 03: ILLEGAL_FUNCTION_TYPE",
  44. "ERR 04: ILLEGAL_FUNCTION_MODE",
  45. "ERR 05: ILLEGAL_FUNCTION_ENCRYPT",
  46. "ERR 06: ILLEGAL_FUNCTION_SIZE",
  47. "ERR 07: Zlib_MISSING_INIT_EOM",
  48. "ERR 08: ILLEGAL_FUNCTION_RSVD",
  49. "ERR 09: ILLEGAL_BUFFER_LENGTH",
  50. "ERR 10: VLSB_FAULT",
  51. "ERR 11: ILLEGAL_MEM_ADDR",
  52. "ERR 12: ILLEGAL_MEM_SEL",
  53. "ERR 13: ILLEGAL_CONTEXT_ID",
  54. "ERR 14: ILLEGAL_KEY_ADDR",
  55. "ERR 15: 0xF Reserved",
  56. "ERR 16: Zlib_ILLEGAL_MULTI_QUEUE",
  57. "ERR 17: Zlib_ILLEGAL_JOBID_CHANGE",
  58. "ERR 18: CMD_TIMEOUT",
  59. "ERR 19: IDMA0_AXI_SLVERR",
  60. "ERR 20: IDMA0_AXI_DECERR",
  61. "ERR 21: 0x15 Reserved",
  62. "ERR 22: IDMA1_AXI_SLAVE_FAULT",
  63. "ERR 23: IDMA1_AIXI_DECERR",
  64. "ERR 24: 0x18 Reserved",
  65. "ERR 25: ZLIBVHB_AXI_SLVERR",
  66. "ERR 26: ZLIBVHB_AXI_DECERR",
  67. "ERR 27: 0x1B Reserved",
  68. "ERR 27: ZLIB_UNEXPECTED_EOM",
  69. "ERR 27: ZLIB_EXTRA_DATA",
  70. "ERR 30: ZLIB_BTYPE",
  71. "ERR 31: ZLIB_UNDEFINED_SYMBOL",
  72. "ERR 32: ZLIB_UNDEFINED_DISTANCE_S",
  73. "ERR 33: ZLIB_CODE_LENGTH_SYMBOL",
  74. "ERR 34: ZLIB _VHB_ILLEGAL_FETCH",
  75. "ERR 35: ZLIB_UNCOMPRESSED_LEN",
  76. "ERR 36: ZLIB_LIMIT_REACHED",
  77. "ERR 37: ZLIB_CHECKSUM_MISMATCH0",
  78. "ERR 38: ODMA0_AXI_SLVERR",
  79. "ERR 39: ODMA0_AXI_DECERR",
  80. "ERR 40: 0x28 Reserved",
  81. "ERR 41: ODMA1_AXI_SLVERR",
  82. "ERR 42: ODMA1_AXI_DECERR",
  83. "ERR 43: LSB_PARITY_ERR",
  84. };
  85. void ccp_log_error(struct ccp_device *d, int e)
  86. {
  87. dev_err(d->dev, "CCP error: %s (0x%x)\n", ccp_error_codes[e], e);
  88. }
  89. /* List of CCPs, CCP count, read-write access lock, and access functions
  90. *
  91. * Lock structure: get ccp_unit_lock for reading whenever we need to
  92. * examine the CCP list. While holding it for reading we can acquire
  93. * the RR lock to update the round-robin next-CCP pointer. The unit lock
  94. * must be acquired before the RR lock.
  95. *
  96. * If the unit-lock is acquired for writing, we have total control over
  97. * the list, so there's no value in getting the RR lock.
  98. */
  99. static DEFINE_RWLOCK(ccp_unit_lock);
  100. static LIST_HEAD(ccp_units);
  101. /* Round-robin counter */
  102. static DEFINE_SPINLOCK(ccp_rr_lock);
  103. static struct ccp_device *ccp_rr;
  104. /* Ever-increasing value to produce unique unit numbers */
  105. static atomic_t ccp_unit_ordinal;
  106. static unsigned int ccp_increment_unit_ordinal(void)
  107. {
  108. return atomic_inc_return(&ccp_unit_ordinal);
  109. }
  110. /**
  111. * ccp_add_device - add a CCP device to the list
  112. *
  113. * @ccp: ccp_device struct pointer
  114. *
  115. * Put this CCP on the unit list, which makes it available
  116. * for use.
  117. *
  118. * Returns zero if a CCP device is present, -ENODEV otherwise.
  119. */
  120. void ccp_add_device(struct ccp_device *ccp)
  121. {
  122. unsigned long flags;
  123. write_lock_irqsave(&ccp_unit_lock, flags);
  124. list_add_tail(&ccp->entry, &ccp_units);
  125. if (!ccp_rr)
  126. /* We already have the list lock (we're first) so this
  127. * pointer can't change on us. Set its initial value.
  128. */
  129. ccp_rr = ccp;
  130. write_unlock_irqrestore(&ccp_unit_lock, flags);
  131. }
  132. /**
  133. * ccp_del_device - remove a CCP device from the list
  134. *
  135. * @ccp: ccp_device struct pointer
  136. *
  137. * Remove this unit from the list of devices. If the next device
  138. * up for use is this one, adjust the pointer. If this is the last
  139. * device, NULL the pointer.
  140. */
  141. void ccp_del_device(struct ccp_device *ccp)
  142. {
  143. unsigned long flags;
  144. write_lock_irqsave(&ccp_unit_lock, flags);
  145. if (ccp_rr == ccp) {
  146. /* ccp_unit_lock is read/write; any read access
  147. * will be suspended while we make changes to the
  148. * list and RR pointer.
  149. */
  150. if (list_is_last(&ccp_rr->entry, &ccp_units))
  151. ccp_rr = list_first_entry(&ccp_units, struct ccp_device,
  152. entry);
  153. else
  154. ccp_rr = list_next_entry(ccp_rr, entry);
  155. }
  156. list_del(&ccp->entry);
  157. if (list_empty(&ccp_units))
  158. ccp_rr = NULL;
  159. write_unlock_irqrestore(&ccp_unit_lock, flags);
  160. }
  161. int ccp_register_rng(struct ccp_device *ccp)
  162. {
  163. int ret = 0;
  164. dev_dbg(ccp->dev, "Registering RNG...\n");
  165. /* Register an RNG */
  166. ccp->hwrng.name = ccp->rngname;
  167. ccp->hwrng.read = ccp_trng_read;
  168. ret = hwrng_register(&ccp->hwrng);
  169. if (ret)
  170. dev_err(ccp->dev, "error registering hwrng (%d)\n", ret);
  171. return ret;
  172. }
  173. void ccp_unregister_rng(struct ccp_device *ccp)
  174. {
  175. if (ccp->hwrng.name)
  176. hwrng_unregister(&ccp->hwrng);
  177. }
  178. static struct ccp_device *ccp_get_device(void)
  179. {
  180. unsigned long flags;
  181. struct ccp_device *dp = NULL;
  182. /* We round-robin through the unit list.
  183. * The (ccp_rr) pointer refers to the next unit to use.
  184. */
  185. read_lock_irqsave(&ccp_unit_lock, flags);
  186. if (!list_empty(&ccp_units)) {
  187. spin_lock(&ccp_rr_lock);
  188. dp = ccp_rr;
  189. if (list_is_last(&ccp_rr->entry, &ccp_units))
  190. ccp_rr = list_first_entry(&ccp_units, struct ccp_device,
  191. entry);
  192. else
  193. ccp_rr = list_next_entry(ccp_rr, entry);
  194. spin_unlock(&ccp_rr_lock);
  195. }
  196. read_unlock_irqrestore(&ccp_unit_lock, flags);
  197. return dp;
  198. }
  199. /**
  200. * ccp_present - check if a CCP device is present
  201. *
  202. * Returns zero if a CCP device is present, -ENODEV otherwise.
  203. */
  204. int ccp_present(void)
  205. {
  206. unsigned long flags;
  207. int ret;
  208. read_lock_irqsave(&ccp_unit_lock, flags);
  209. ret = list_empty(&ccp_units);
  210. read_unlock_irqrestore(&ccp_unit_lock, flags);
  211. return ret ? -ENODEV : 0;
  212. }
  213. EXPORT_SYMBOL_GPL(ccp_present);
  214. /**
  215. * ccp_version - get the version of the CCP device
  216. *
  217. * Returns the version from the first unit on the list;
  218. * otherwise a zero if no CCP device is present
  219. */
  220. unsigned int ccp_version(void)
  221. {
  222. struct ccp_device *dp;
  223. unsigned long flags;
  224. int ret = 0;
  225. read_lock_irqsave(&ccp_unit_lock, flags);
  226. if (!list_empty(&ccp_units)) {
  227. dp = list_first_entry(&ccp_units, struct ccp_device, entry);
  228. ret = dp->vdata->version;
  229. }
  230. read_unlock_irqrestore(&ccp_unit_lock, flags);
  231. return ret;
  232. }
  233. EXPORT_SYMBOL_GPL(ccp_version);
  234. /**
  235. * ccp_enqueue_cmd - queue an operation for processing by the CCP
  236. *
  237. * @cmd: ccp_cmd struct to be processed
  238. *
  239. * Queue a cmd to be processed by the CCP. If queueing the cmd
  240. * would exceed the defined length of the cmd queue the cmd will
  241. * only be queued if the CCP_CMD_MAY_BACKLOG flag is set and will
  242. * result in a return code of -EBUSY.
  243. *
  244. * The callback routine specified in the ccp_cmd struct will be
  245. * called to notify the caller of completion (if the cmd was not
  246. * backlogged) or advancement out of the backlog. If the cmd has
  247. * advanced out of the backlog the "err" value of the callback
  248. * will be -EINPROGRESS. Any other "err" value during callback is
  249. * the result of the operation.
  250. *
  251. * The cmd has been successfully queued if:
  252. * the return code is -EINPROGRESS or
  253. * the return code is -EBUSY and CCP_CMD_MAY_BACKLOG flag is set
  254. */
  255. int ccp_enqueue_cmd(struct ccp_cmd *cmd)
  256. {
  257. struct ccp_device *ccp;
  258. unsigned long flags;
  259. unsigned int i;
  260. int ret;
  261. /* Some commands might need to be sent to a specific device */
  262. ccp = cmd->ccp ? cmd->ccp : ccp_get_device();
  263. if (!ccp)
  264. return -ENODEV;
  265. /* Caller must supply a callback routine */
  266. if (!cmd->callback)
  267. return -EINVAL;
  268. cmd->ccp = ccp;
  269. spin_lock_irqsave(&ccp->cmd_lock, flags);
  270. i = ccp->cmd_q_count;
  271. if (ccp->cmd_count >= MAX_CMD_QLEN) {
  272. ret = -EBUSY;
  273. if (cmd->flags & CCP_CMD_MAY_BACKLOG)
  274. list_add_tail(&cmd->entry, &ccp->backlog);
  275. } else {
  276. ret = -EINPROGRESS;
  277. ccp->cmd_count++;
  278. list_add_tail(&cmd->entry, &ccp->cmd);
  279. /* Find an idle queue */
  280. if (!ccp->suspending) {
  281. for (i = 0; i < ccp->cmd_q_count; i++) {
  282. if (ccp->cmd_q[i].active)
  283. continue;
  284. break;
  285. }
  286. }
  287. }
  288. spin_unlock_irqrestore(&ccp->cmd_lock, flags);
  289. /* If we found an idle queue, wake it up */
  290. if (i < ccp->cmd_q_count)
  291. wake_up_process(ccp->cmd_q[i].kthread);
  292. return ret;
  293. }
  294. EXPORT_SYMBOL_GPL(ccp_enqueue_cmd);
  295. static void ccp_do_cmd_backlog(struct work_struct *work)
  296. {
  297. struct ccp_cmd *cmd = container_of(work, struct ccp_cmd, work);
  298. struct ccp_device *ccp = cmd->ccp;
  299. unsigned long flags;
  300. unsigned int i;
  301. cmd->callback(cmd->data, -EINPROGRESS);
  302. spin_lock_irqsave(&ccp->cmd_lock, flags);
  303. ccp->cmd_count++;
  304. list_add_tail(&cmd->entry, &ccp->cmd);
  305. /* Find an idle queue */
  306. for (i = 0; i < ccp->cmd_q_count; i++) {
  307. if (ccp->cmd_q[i].active)
  308. continue;
  309. break;
  310. }
  311. spin_unlock_irqrestore(&ccp->cmd_lock, flags);
  312. /* If we found an idle queue, wake it up */
  313. if (i < ccp->cmd_q_count)
  314. wake_up_process(ccp->cmd_q[i].kthread);
  315. }
  316. static struct ccp_cmd *ccp_dequeue_cmd(struct ccp_cmd_queue *cmd_q)
  317. {
  318. struct ccp_device *ccp = cmd_q->ccp;
  319. struct ccp_cmd *cmd = NULL;
  320. struct ccp_cmd *backlog = NULL;
  321. unsigned long flags;
  322. spin_lock_irqsave(&ccp->cmd_lock, flags);
  323. cmd_q->active = 0;
  324. if (ccp->suspending) {
  325. cmd_q->suspended = 1;
  326. spin_unlock_irqrestore(&ccp->cmd_lock, flags);
  327. wake_up_interruptible(&ccp->suspend_queue);
  328. return NULL;
  329. }
  330. if (ccp->cmd_count) {
  331. cmd_q->active = 1;
  332. cmd = list_first_entry(&ccp->cmd, struct ccp_cmd, entry);
  333. list_del(&cmd->entry);
  334. ccp->cmd_count--;
  335. }
  336. if (!list_empty(&ccp->backlog)) {
  337. backlog = list_first_entry(&ccp->backlog, struct ccp_cmd,
  338. entry);
  339. list_del(&backlog->entry);
  340. }
  341. spin_unlock_irqrestore(&ccp->cmd_lock, flags);
  342. if (backlog) {
  343. INIT_WORK(&backlog->work, ccp_do_cmd_backlog);
  344. schedule_work(&backlog->work);
  345. }
  346. return cmd;
  347. }
  348. static void ccp_do_cmd_complete(unsigned long data)
  349. {
  350. struct ccp_tasklet_data *tdata = (struct ccp_tasklet_data *)data;
  351. struct ccp_cmd *cmd = tdata->cmd;
  352. cmd->callback(cmd->data, cmd->ret);
  353. complete(&tdata->completion);
  354. }
  355. /**
  356. * ccp_cmd_queue_thread - create a kernel thread to manage a CCP queue
  357. *
  358. * @data: thread-specific data
  359. */
  360. int ccp_cmd_queue_thread(void *data)
  361. {
  362. struct ccp_cmd_queue *cmd_q = (struct ccp_cmd_queue *)data;
  363. struct ccp_cmd *cmd;
  364. struct ccp_tasklet_data tdata;
  365. struct tasklet_struct tasklet;
  366. tasklet_init(&tasklet, ccp_do_cmd_complete, (unsigned long)&tdata);
  367. set_current_state(TASK_INTERRUPTIBLE);
  368. while (!kthread_should_stop()) {
  369. schedule();
  370. set_current_state(TASK_INTERRUPTIBLE);
  371. cmd = ccp_dequeue_cmd(cmd_q);
  372. if (!cmd)
  373. continue;
  374. __set_current_state(TASK_RUNNING);
  375. /* Execute the command */
  376. cmd->ret = ccp_run_cmd(cmd_q, cmd);
  377. /* Schedule the completion callback */
  378. tdata.cmd = cmd;
  379. init_completion(&tdata.completion);
  380. tasklet_schedule(&tasklet);
  381. wait_for_completion(&tdata.completion);
  382. }
  383. __set_current_state(TASK_RUNNING);
  384. return 0;
  385. }
  386. /**
  387. * ccp_alloc_struct - allocate and initialize the ccp_device struct
  388. *
  389. * @dev: device struct of the CCP
  390. */
  391. struct ccp_device *ccp_alloc_struct(struct device *dev)
  392. {
  393. struct ccp_device *ccp;
  394. ccp = devm_kzalloc(dev, sizeof(*ccp), GFP_KERNEL);
  395. if (!ccp)
  396. return NULL;
  397. ccp->dev = dev;
  398. INIT_LIST_HEAD(&ccp->cmd);
  399. INIT_LIST_HEAD(&ccp->backlog);
  400. spin_lock_init(&ccp->cmd_lock);
  401. mutex_init(&ccp->req_mutex);
  402. mutex_init(&ccp->sb_mutex);
  403. ccp->sb_count = KSB_COUNT;
  404. ccp->sb_start = 0;
  405. ccp->ord = ccp_increment_unit_ordinal();
  406. snprintf(ccp->name, MAX_CCP_NAME_LEN, "ccp-%u", ccp->ord);
  407. snprintf(ccp->rngname, MAX_CCP_NAME_LEN, "ccp-%u-rng", ccp->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. #endif
  447. static int __init ccp_mod_init(void)
  448. {
  449. #ifdef CONFIG_X86
  450. int ret;
  451. ret = ccp_pci_init();
  452. if (ret)
  453. return ret;
  454. /* Don't leave the driver loaded if init failed */
  455. if (ccp_present() != 0) {
  456. ccp_pci_exit();
  457. return -ENODEV;
  458. }
  459. return 0;
  460. #endif
  461. #ifdef CONFIG_ARM64
  462. int ret;
  463. ret = ccp_platform_init();
  464. if (ret)
  465. return ret;
  466. /* Don't leave the driver loaded if init failed */
  467. if (ccp_present() != 0) {
  468. ccp_platform_exit();
  469. return -ENODEV;
  470. }
  471. return 0;
  472. #endif
  473. return -ENODEV;
  474. }
  475. static void __exit ccp_mod_exit(void)
  476. {
  477. #ifdef CONFIG_X86
  478. ccp_pci_exit();
  479. #endif
  480. #ifdef CONFIG_ARM64
  481. ccp_platform_exit();
  482. #endif
  483. }
  484. module_init(ccp_mod_init);
  485. module_exit(ccp_mod_exit);