ccp-dev.c 14 KB

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