tpm-chip.c 12 KB

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