PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/char/tpm/tpm-chip.c

https://github.com/kvaneesh/linux
C | 647 lines | 404 code | 104 blank | 139 comment | 61 complexity | a11aaff36f2ac77a67f08a0564efa6c6 MD5 | raw file
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2004 IBM Corporation
  4. * Copyright (C) 2014 Intel Corporation
  5. *
  6. * Authors:
  7. * Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
  8. * Leendert van Doorn <leendert@watson.ibm.com>
  9. * Dave Safford <safford@watson.ibm.com>
  10. * Reiner Sailer <sailer@watson.ibm.com>
  11. * Kylene Hall <kjhall@us.ibm.com>
  12. *
  13. * Maintained by: <tpmdd-devel@lists.sourceforge.net>
  14. *
  15. * TPM chip management routines.
  16. */
  17. #include <linux/poll.h>
  18. #include <linux/slab.h>
  19. #include <linux/mutex.h>
  20. #include <linux/spinlock.h>
  21. #include <linux/freezer.h>
  22. #include <linux/major.h>
  23. #include <linux/tpm_eventlog.h>
  24. #include <linux/hw_random.h>
  25. #include "tpm.h"
  26. DEFINE_IDR(dev_nums_idr);
  27. static DEFINE_MUTEX(idr_lock);
  28. struct class *tpm_class;
  29. struct class *tpmrm_class;
  30. dev_t tpm_devt;
  31. static int tpm_request_locality(struct tpm_chip *chip)
  32. {
  33. int rc;
  34. if (!chip->ops->request_locality)
  35. return 0;
  36. rc = chip->ops->request_locality(chip, 0);
  37. if (rc < 0)
  38. return rc;
  39. chip->locality = rc;
  40. return 0;
  41. }
  42. static void tpm_relinquish_locality(struct tpm_chip *chip)
  43. {
  44. int rc;
  45. if (!chip->ops->relinquish_locality)
  46. return;
  47. rc = chip->ops->relinquish_locality(chip, chip->locality);
  48. if (rc)
  49. dev_err(&chip->dev, "%s: : error %d\n", __func__, rc);
  50. chip->locality = -1;
  51. }
  52. static int tpm_cmd_ready(struct tpm_chip *chip)
  53. {
  54. if (!chip->ops->cmd_ready)
  55. return 0;
  56. return chip->ops->cmd_ready(chip);
  57. }
  58. static int tpm_go_idle(struct tpm_chip *chip)
  59. {
  60. if (!chip->ops->go_idle)
  61. return 0;
  62. return chip->ops->go_idle(chip);
  63. }
  64. static void tpm_clk_enable(struct tpm_chip *chip)
  65. {
  66. if (chip->ops->clk_enable)
  67. chip->ops->clk_enable(chip, true);
  68. }
  69. static void tpm_clk_disable(struct tpm_chip *chip)
  70. {
  71. if (chip->ops->clk_enable)
  72. chip->ops->clk_enable(chip, false);
  73. }
  74. /**
  75. * tpm_chip_start() - power on the TPM
  76. * @chip: a TPM chip to use
  77. *
  78. * Return:
  79. * * The response length - OK
  80. * * -errno - A system error
  81. */
  82. int tpm_chip_start(struct tpm_chip *chip)
  83. {
  84. int ret;
  85. tpm_clk_enable(chip);
  86. if (chip->locality == -1) {
  87. ret = tpm_request_locality(chip);
  88. if (ret) {
  89. tpm_clk_disable(chip);
  90. return ret;
  91. }
  92. }
  93. ret = tpm_cmd_ready(chip);
  94. if (ret) {
  95. tpm_relinquish_locality(chip);
  96. tpm_clk_disable(chip);
  97. return ret;
  98. }
  99. return 0;
  100. }
  101. EXPORT_SYMBOL_GPL(tpm_chip_start);
  102. /**
  103. * tpm_chip_stop() - power off the TPM
  104. * @chip: a TPM chip to use
  105. *
  106. * Return:
  107. * * The response length - OK
  108. * * -errno - A system error
  109. */
  110. void tpm_chip_stop(struct tpm_chip *chip)
  111. {
  112. tpm_go_idle(chip);
  113. tpm_relinquish_locality(chip);
  114. tpm_clk_disable(chip);
  115. }
  116. EXPORT_SYMBOL_GPL(tpm_chip_stop);
  117. /**
  118. * tpm_try_get_ops() - Get a ref to the tpm_chip
  119. * @chip: Chip to ref
  120. *
  121. * The caller must already have some kind of locking to ensure that chip is
  122. * valid. This function will lock the chip so that the ops member can be
  123. * accessed safely. The locking prevents tpm_chip_unregister from
  124. * completing, so it should not be held for long periods.
  125. *
  126. * Returns -ERRNO if the chip could not be got.
  127. */
  128. int tpm_try_get_ops(struct tpm_chip *chip)
  129. {
  130. int rc = -EIO;
  131. get_device(&chip->dev);
  132. down_read(&chip->ops_sem);
  133. if (!chip->ops)
  134. goto out_ops;
  135. mutex_lock(&chip->tpm_mutex);
  136. rc = tpm_chip_start(chip);
  137. if (rc)
  138. goto out_lock;
  139. return 0;
  140. out_lock:
  141. mutex_unlock(&chip->tpm_mutex);
  142. out_ops:
  143. up_read(&chip->ops_sem);
  144. put_device(&chip->dev);
  145. return rc;
  146. }
  147. EXPORT_SYMBOL_GPL(tpm_try_get_ops);
  148. /**
  149. * tpm_put_ops() - Release a ref to the tpm_chip
  150. * @chip: Chip to put
  151. *
  152. * This is the opposite pair to tpm_try_get_ops(). After this returns chip may
  153. * be kfree'd.
  154. */
  155. void tpm_put_ops(struct tpm_chip *chip)
  156. {
  157. tpm_chip_stop(chip);
  158. mutex_unlock(&chip->tpm_mutex);
  159. up_read(&chip->ops_sem);
  160. put_device(&chip->dev);
  161. }
  162. EXPORT_SYMBOL_GPL(tpm_put_ops);
  163. /**
  164. * tpm_default_chip() - find a TPM chip and get a reference to it
  165. */
  166. struct tpm_chip *tpm_default_chip(void)
  167. {
  168. struct tpm_chip *chip, *res = NULL;
  169. int chip_num = 0;
  170. int chip_prev;
  171. mutex_lock(&idr_lock);
  172. do {
  173. chip_prev = chip_num;
  174. chip = idr_get_next(&dev_nums_idr, &chip_num);
  175. if (chip) {
  176. get_device(&chip->dev);
  177. res = chip;
  178. break;
  179. }
  180. } while (chip_prev != chip_num);
  181. mutex_unlock(&idr_lock);
  182. return res;
  183. }
  184. EXPORT_SYMBOL_GPL(tpm_default_chip);
  185. /**
  186. * tpm_find_get_ops() - find and reserve a TPM chip
  187. * @chip: a &struct tpm_chip instance, %NULL for the default chip
  188. *
  189. * Finds a TPM chip and reserves its class device and operations. The chip must
  190. * be released with tpm_put_ops() after use.
  191. * This function is for internal use only. It supports existing TPM callers
  192. * by accepting NULL, but those callers should be converted to pass in a chip
  193. * directly.
  194. *
  195. * Return:
  196. * A reserved &struct tpm_chip instance.
  197. * %NULL if a chip is not found.
  198. * %NULL if the chip is not available.
  199. */
  200. struct tpm_chip *tpm_find_get_ops(struct tpm_chip *chip)
  201. {
  202. int rc;
  203. if (chip) {
  204. if (!tpm_try_get_ops(chip))
  205. return chip;
  206. return NULL;
  207. }
  208. chip = tpm_default_chip();
  209. if (!chip)
  210. return NULL;
  211. rc = tpm_try_get_ops(chip);
  212. /* release additional reference we got from tpm_default_chip() */
  213. put_device(&chip->dev);
  214. if (rc)
  215. return NULL;
  216. return chip;
  217. }
  218. /**
  219. * tpm_dev_release() - free chip memory and the device number
  220. * @dev: the character device for the TPM chip
  221. *
  222. * This is used as the release function for the character device.
  223. */
  224. static void tpm_dev_release(struct device *dev)
  225. {
  226. struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev);
  227. mutex_lock(&idr_lock);
  228. idr_remove(&dev_nums_idr, chip->dev_num);
  229. mutex_unlock(&idr_lock);
  230. kfree(chip->log.bios_event_log);
  231. kfree(chip->work_space.context_buf);
  232. kfree(chip->work_space.session_buf);
  233. kfree(chip->allocated_banks);
  234. kfree(chip);
  235. }
  236. static void tpm_devs_release(struct device *dev)
  237. {
  238. struct tpm_chip *chip = container_of(dev, struct tpm_chip, devs);
  239. /* release the master device reference */
  240. put_device(&chip->dev);
  241. }
  242. /**
  243. * tpm_class_shutdown() - prepare the TPM device for loss of power.
  244. * @dev: device to which the chip is associated.
  245. *
  246. * Issues a TPM2_Shutdown command prior to loss of power, as required by the
  247. * TPM 2.0 spec. Then, calls bus- and device- specific shutdown code.
  248. *
  249. * Return: always 0 (i.e. success)
  250. */
  251. static int tpm_class_shutdown(struct device *dev)
  252. {
  253. struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev);
  254. down_write(&chip->ops_sem);
  255. if (chip->flags & TPM_CHIP_FLAG_TPM2) {
  256. if (!tpm_chip_start(chip)) {
  257. tpm2_shutdown(chip, TPM2_SU_CLEAR);
  258. tpm_chip_stop(chip);
  259. }
  260. }
  261. chip->ops = NULL;
  262. up_write(&chip->ops_sem);
  263. return 0;
  264. }
  265. /**
  266. * tpm_chip_alloc() - allocate a new struct tpm_chip instance
  267. * @pdev: device to which the chip is associated
  268. * At this point pdev mst be initialized, but does not have to
  269. * be registered
  270. * @ops: struct tpm_class_ops instance
  271. *
  272. * Allocates a new struct tpm_chip instance and assigns a free
  273. * device number for it. Must be paired with put_device(&chip->dev).
  274. */
  275. struct tpm_chip *tpm_chip_alloc(struct device *pdev,
  276. const struct tpm_class_ops *ops)
  277. {
  278. struct tpm_chip *chip;
  279. int rc;
  280. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  281. if (chip == NULL)
  282. return ERR_PTR(-ENOMEM);
  283. mutex_init(&chip->tpm_mutex);
  284. init_rwsem(&chip->ops_sem);
  285. chip->ops = ops;
  286. mutex_lock(&idr_lock);
  287. rc = idr_alloc(&dev_nums_idr, NULL, 0, TPM_NUM_DEVICES, GFP_KERNEL);
  288. mutex_unlock(&idr_lock);
  289. if (rc < 0) {
  290. dev_err(pdev, "No available tpm device numbers\n");
  291. kfree(chip);
  292. return ERR_PTR(rc);
  293. }
  294. chip->dev_num = rc;
  295. device_initialize(&chip->dev);
  296. device_initialize(&chip->devs);
  297. chip->dev.class = tpm_class;
  298. chip->dev.class->shutdown_pre = tpm_class_shutdown;
  299. chip->dev.release = tpm_dev_release;
  300. chip->dev.parent = pdev;
  301. chip->dev.groups = chip->groups;
  302. chip->devs.parent = pdev;
  303. chip->devs.class = tpmrm_class;
  304. chip->devs.release = tpm_devs_release;
  305. /* get extra reference on main device to hold on
  306. * behalf of devs. This holds the chip structure
  307. * while cdevs is in use. The corresponding put
  308. * is in the tpm_devs_release (TPM2 only)
  309. */
  310. if (chip->flags & TPM_CHIP_FLAG_TPM2)
  311. get_device(&chip->dev);
  312. if (chip->dev_num == 0)
  313. chip->dev.devt = MKDEV(MISC_MAJOR, TPM_MINOR);
  314. else
  315. chip->dev.devt = MKDEV(MAJOR(tpm_devt), chip->dev_num);
  316. chip->devs.devt =
  317. MKDEV(MAJOR(tpm_devt), chip->dev_num + TPM_NUM_DEVICES);
  318. rc = dev_set_name(&chip->dev, "tpm%d", chip->dev_num);
  319. if (rc)
  320. goto out;
  321. rc = dev_set_name(&chip->devs, "tpmrm%d", chip->dev_num);
  322. if (rc)
  323. goto out;
  324. if (!pdev)
  325. chip->flags |= TPM_CHIP_FLAG_VIRTUAL;
  326. cdev_init(&chip->cdev, &tpm_fops);
  327. cdev_init(&chip->cdevs, &tpmrm_fops);
  328. chip->cdev.owner = THIS_MODULE;
  329. chip->cdevs.owner = THIS_MODULE;
  330. rc = tpm2_init_space(&chip->work_space, TPM2_SPACE_BUFFER_SIZE);
  331. if (rc) {
  332. rc = -ENOMEM;
  333. goto out;
  334. }
  335. chip->locality = -1;
  336. return chip;
  337. out:
  338. put_device(&chip->devs);
  339. put_device(&chip->dev);
  340. return ERR_PTR(rc);
  341. }
  342. EXPORT_SYMBOL_GPL(tpm_chip_alloc);
  343. /**
  344. * tpmm_chip_alloc() - allocate a new struct tpm_chip instance
  345. * @pdev: parent device to which the chip is associated
  346. * @ops: struct tpm_class_ops instance
  347. *
  348. * Same as tpm_chip_alloc except devm is used to do the put_device
  349. */
  350. struct tpm_chip *tpmm_chip_alloc(struct device *pdev,
  351. const struct tpm_class_ops *ops)
  352. {
  353. struct tpm_chip *chip;
  354. int rc;
  355. chip = tpm_chip_alloc(pdev, ops);
  356. if (IS_ERR(chip))
  357. return chip;
  358. rc = devm_add_action_or_reset(pdev,
  359. (void (*)(void *)) put_device,
  360. &chip->dev);
  361. if (rc)
  362. return ERR_PTR(rc);
  363. dev_set_drvdata(pdev, chip);
  364. return chip;
  365. }
  366. EXPORT_SYMBOL_GPL(tpmm_chip_alloc);
  367. static int tpm_add_char_device(struct tpm_chip *chip)
  368. {
  369. int rc;
  370. rc = cdev_device_add(&chip->cdev, &chip->dev);
  371. if (rc) {
  372. dev_err(&chip->dev,
  373. "unable to cdev_device_add() %s, major %d, minor %d, err=%d\n",
  374. dev_name(&chip->dev), MAJOR(chip->dev.devt),
  375. MINOR(chip->dev.devt), rc);
  376. return rc;
  377. }
  378. if (chip->flags & TPM_CHIP_FLAG_TPM2) {
  379. rc = cdev_device_add(&chip->cdevs, &chip->devs);
  380. if (rc) {
  381. dev_err(&chip->devs,
  382. "unable to cdev_device_add() %s, major %d, minor %d, err=%d\n",
  383. dev_name(&chip->devs), MAJOR(chip->devs.devt),
  384. MINOR(chip->devs.devt), rc);
  385. return rc;
  386. }
  387. }
  388. /* Make the chip available. */
  389. mutex_lock(&idr_lock);
  390. idr_replace(&dev_nums_idr, chip, chip->dev_num);
  391. mutex_unlock(&idr_lock);
  392. return rc;
  393. }
  394. static void tpm_del_char_device(struct tpm_chip *chip)
  395. {
  396. cdev_device_del(&chip->cdev, &chip->dev);
  397. /* Make the chip unavailable. */
  398. mutex_lock(&idr_lock);
  399. idr_replace(&dev_nums_idr, NULL, chip->dev_num);
  400. mutex_unlock(&idr_lock);
  401. /* Make the driver uncallable. */
  402. down_write(&chip->ops_sem);
  403. if (chip->flags & TPM_CHIP_FLAG_TPM2) {
  404. if (!tpm_chip_start(chip)) {
  405. tpm2_shutdown(chip, TPM2_SU_CLEAR);
  406. tpm_chip_stop(chip);
  407. }
  408. }
  409. chip->ops = NULL;
  410. up_write(&chip->ops_sem);
  411. }
  412. static void tpm_del_legacy_sysfs(struct tpm_chip *chip)
  413. {
  414. struct attribute **i;
  415. if (chip->flags & (TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_VIRTUAL))
  416. return;
  417. sysfs_remove_link(&chip->dev.parent->kobj, "ppi");
  418. for (i = chip->groups[0]->attrs; *i != NULL; ++i)
  419. sysfs_remove_link(&chip->dev.parent->kobj, (*i)->name);
  420. }
  421. /* For compatibility with legacy sysfs paths we provide symlinks from the
  422. * parent dev directory to selected names within the tpm chip directory. Old
  423. * kernel versions created these files directly under the parent.
  424. */
  425. static int tpm_add_legacy_sysfs(struct tpm_chip *chip)
  426. {
  427. struct attribute **i;
  428. int rc;
  429. if (chip->flags & (TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_VIRTUAL))
  430. return 0;
  431. rc = compat_only_sysfs_link_entry_to_kobj(
  432. &chip->dev.parent->kobj, &chip->dev.kobj, "ppi", NULL);
  433. if (rc && rc != -ENOENT)
  434. return rc;
  435. /* All the names from tpm-sysfs */
  436. for (i = chip->groups[0]->attrs; *i != NULL; ++i) {
  437. rc = compat_only_sysfs_link_entry_to_kobj(
  438. &chip->dev.parent->kobj, &chip->dev.kobj, (*i)->name, NULL);
  439. if (rc) {
  440. tpm_del_legacy_sysfs(chip);
  441. return rc;
  442. }
  443. }
  444. return 0;
  445. }
  446. static int tpm_hwrng_read(struct hwrng *rng, void *data, size_t max, bool wait)
  447. {
  448. struct tpm_chip *chip = container_of(rng, struct tpm_chip, hwrng);
  449. return tpm_get_random(chip, data, max);
  450. }
  451. static int tpm_add_hwrng(struct tpm_chip *chip)
  452. {
  453. if (!IS_ENABLED(CONFIG_HW_RANDOM_TPM))
  454. return 0;
  455. snprintf(chip->hwrng_name, sizeof(chip->hwrng_name),
  456. "tpm-rng-%d", chip->dev_num);
  457. chip->hwrng.name = chip->hwrng_name;
  458. chip->hwrng.read = tpm_hwrng_read;
  459. return hwrng_register(&chip->hwrng);
  460. }
  461. static int tpm_get_pcr_allocation(struct tpm_chip *chip)
  462. {
  463. int rc;
  464. rc = (chip->flags & TPM_CHIP_FLAG_TPM2) ?
  465. tpm2_get_pcr_allocation(chip) :
  466. tpm1_get_pcr_allocation(chip);
  467. if (rc > 0)
  468. return -ENODEV;
  469. return rc;
  470. }
  471. /*
  472. * tpm_chip_register() - create a character device for the TPM chip
  473. * @chip: TPM chip to use.
  474. *
  475. * Creates a character device for the TPM chip and adds sysfs attributes for
  476. * the device. As the last step this function adds the chip to the list of TPM
  477. * chips available for in-kernel use.
  478. *
  479. * This function should be only called after the chip initialization is
  480. * complete.
  481. */
  482. int tpm_chip_register(struct tpm_chip *chip)
  483. {
  484. int rc;
  485. rc = tpm_chip_start(chip);
  486. if (rc)
  487. return rc;
  488. rc = tpm_auto_startup(chip);
  489. if (rc) {
  490. tpm_chip_stop(chip);
  491. return rc;
  492. }
  493. rc = tpm_get_pcr_allocation(chip);
  494. tpm_chip_stop(chip);
  495. if (rc)
  496. return rc;
  497. tpm_sysfs_add_device(chip);
  498. tpm_bios_log_setup(chip);
  499. tpm_add_ppi(chip);
  500. rc = tpm_add_hwrng(chip);
  501. if (rc)
  502. goto out_ppi;
  503. rc = tpm_add_char_device(chip);
  504. if (rc)
  505. goto out_hwrng;
  506. rc = tpm_add_legacy_sysfs(chip);
  507. if (rc) {
  508. tpm_chip_unregister(chip);
  509. return rc;
  510. }
  511. return 0;
  512. out_hwrng:
  513. if (IS_ENABLED(CONFIG_HW_RANDOM_TPM))
  514. hwrng_unregister(&chip->hwrng);
  515. out_ppi:
  516. tpm_bios_log_teardown(chip);
  517. return rc;
  518. }
  519. EXPORT_SYMBOL_GPL(tpm_chip_register);
  520. /*
  521. * tpm_chip_unregister() - release the TPM driver
  522. * @chip: TPM chip to use.
  523. *
  524. * Takes the chip first away from the list of available TPM chips and then
  525. * cleans up all the resources reserved by tpm_chip_register().
  526. *
  527. * Once this function returns the driver call backs in 'op's will not be
  528. * running and will no longer start.
  529. *
  530. * NOTE: This function should be only called before deinitializing chip
  531. * resources.
  532. */
  533. void tpm_chip_unregister(struct tpm_chip *chip)
  534. {
  535. tpm_del_legacy_sysfs(chip);
  536. if (IS_ENABLED(CONFIG_HW_RANDOM_TPM))
  537. hwrng_unregister(&chip->hwrng);
  538. tpm_bios_log_teardown(chip);
  539. if (chip->flags & TPM_CHIP_FLAG_TPM2)
  540. cdev_device_del(&chip->cdevs, &chip->devs);
  541. tpm_del_char_device(chip);
  542. }
  543. EXPORT_SYMBOL_GPL(tpm_chip_unregister);