ccp-dev.c 14 KB

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