tpm-chip.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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. if (chip->flags & TPM_CHIP_FLAG_TPM2) {
  168. down_write(&chip->ops_sem);
  169. tpm2_shutdown(chip, TPM2_SU_CLEAR);
  170. chip->ops = NULL;
  171. up_write(&chip->ops_sem);
  172. }
  173. return 0;
  174. }
  175. /**
  176. * tpm_chip_alloc() - allocate a new struct tpm_chip instance
  177. * @pdev: device to which the chip is associated
  178. * At this point pdev mst be initialized, but does not have to
  179. * be registered
  180. * @ops: struct tpm_class_ops instance
  181. *
  182. * Allocates a new struct tpm_chip instance and assigns a free
  183. * device number for it. Must be paired with put_device(&chip->dev).
  184. */
  185. struct tpm_chip *tpm_chip_alloc(struct device *pdev,
  186. const struct tpm_class_ops *ops)
  187. {
  188. struct tpm_chip *chip;
  189. int rc;
  190. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  191. if (chip == NULL)
  192. return ERR_PTR(-ENOMEM);
  193. mutex_init(&chip->tpm_mutex);
  194. init_rwsem(&chip->ops_sem);
  195. chip->ops = ops;
  196. mutex_lock(&idr_lock);
  197. rc = idr_alloc(&dev_nums_idr, NULL, 0, TPM_NUM_DEVICES, GFP_KERNEL);
  198. mutex_unlock(&idr_lock);
  199. if (rc < 0) {
  200. dev_err(pdev, "No available tpm device numbers\n");
  201. kfree(chip);
  202. return ERR_PTR(rc);
  203. }
  204. chip->dev_num = rc;
  205. device_initialize(&chip->dev);
  206. device_initialize(&chip->devs);
  207. chip->dev.class = tpm_class;
  208. chip->dev.class->shutdown_pre = tpm_class_shutdown;
  209. chip->dev.release = tpm_dev_release;
  210. chip->dev.parent = pdev;
  211. chip->dev.groups = chip->groups;
  212. chip->devs.parent = pdev;
  213. chip->devs.class = tpmrm_class;
  214. chip->devs.release = tpm_devs_release;
  215. /* get extra reference on main device to hold on
  216. * behalf of devs. This holds the chip structure
  217. * while cdevs is in use. The corresponding put
  218. * is in the tpm_devs_release (TPM2 only)
  219. */
  220. if (chip->flags & TPM_CHIP_FLAG_TPM2)
  221. get_device(&chip->dev);
  222. if (chip->dev_num == 0)
  223. chip->dev.devt = MKDEV(MISC_MAJOR, TPM_MINOR);
  224. else
  225. chip->dev.devt = MKDEV(MAJOR(tpm_devt), chip->dev_num);
  226. chip->devs.devt =
  227. MKDEV(MAJOR(tpm_devt), chip->dev_num + TPM_NUM_DEVICES);
  228. rc = dev_set_name(&chip->dev, "tpm%d", chip->dev_num);
  229. if (rc)
  230. goto out;
  231. rc = dev_set_name(&chip->devs, "tpmrm%d", chip->dev_num);
  232. if (rc)
  233. goto out;
  234. if (!pdev)
  235. chip->flags |= TPM_CHIP_FLAG_VIRTUAL;
  236. cdev_init(&chip->cdev, &tpm_fops);
  237. cdev_init(&chip->cdevs, &tpmrm_fops);
  238. chip->cdev.owner = THIS_MODULE;
  239. chip->cdevs.owner = THIS_MODULE;
  240. chip->work_space.context_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
  241. if (!chip->work_space.context_buf) {
  242. rc = -ENOMEM;
  243. goto out;
  244. }
  245. chip->work_space.session_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
  246. if (!chip->work_space.session_buf) {
  247. rc = -ENOMEM;
  248. goto out;
  249. }
  250. chip->locality = -1;
  251. return chip;
  252. out:
  253. put_device(&chip->devs);
  254. put_device(&chip->dev);
  255. return ERR_PTR(rc);
  256. }
  257. EXPORT_SYMBOL_GPL(tpm_chip_alloc);
  258. /**
  259. * tpmm_chip_alloc() - allocate a new struct tpm_chip instance
  260. * @pdev: parent device to which the chip is associated
  261. * @ops: struct tpm_class_ops instance
  262. *
  263. * Same as tpm_chip_alloc except devm is used to do the put_device
  264. */
  265. struct tpm_chip *tpmm_chip_alloc(struct device *pdev,
  266. const struct tpm_class_ops *ops)
  267. {
  268. struct tpm_chip *chip;
  269. int rc;
  270. chip = tpm_chip_alloc(pdev, ops);
  271. if (IS_ERR(chip))
  272. return chip;
  273. rc = devm_add_action_or_reset(pdev,
  274. (void (*)(void *)) put_device,
  275. &chip->dev);
  276. if (rc)
  277. return ERR_PTR(rc);
  278. dev_set_drvdata(pdev, chip);
  279. return chip;
  280. }
  281. EXPORT_SYMBOL_GPL(tpmm_chip_alloc);
  282. static int tpm_add_char_device(struct tpm_chip *chip)
  283. {
  284. int rc;
  285. rc = cdev_device_add(&chip->cdev, &chip->dev);
  286. if (rc) {
  287. dev_err(&chip->dev,
  288. "unable to cdev_device_add() %s, major %d, minor %d, err=%d\n",
  289. dev_name(&chip->dev), MAJOR(chip->dev.devt),
  290. MINOR(chip->dev.devt), rc);
  291. return rc;
  292. }
  293. if (chip->flags & TPM_CHIP_FLAG_TPM2) {
  294. rc = cdev_device_add(&chip->cdevs, &chip->devs);
  295. if (rc) {
  296. dev_err(&chip->devs,
  297. "unable to cdev_device_add() %s, major %d, minor %d, err=%d\n",
  298. dev_name(&chip->devs), MAJOR(chip->devs.devt),
  299. MINOR(chip->devs.devt), rc);
  300. return rc;
  301. }
  302. }
  303. /* Make the chip available. */
  304. mutex_lock(&idr_lock);
  305. idr_replace(&dev_nums_idr, chip, chip->dev_num);
  306. mutex_unlock(&idr_lock);
  307. return rc;
  308. }
  309. static void tpm_del_char_device(struct tpm_chip *chip)
  310. {
  311. cdev_device_del(&chip->cdev, &chip->dev);
  312. /* Make the chip unavailable. */
  313. mutex_lock(&idr_lock);
  314. idr_replace(&dev_nums_idr, NULL, chip->dev_num);
  315. mutex_unlock(&idr_lock);
  316. /* Make the driver uncallable. */
  317. down_write(&chip->ops_sem);
  318. if (chip->flags & TPM_CHIP_FLAG_TPM2)
  319. tpm2_shutdown(chip, TPM2_SU_CLEAR);
  320. chip->ops = NULL;
  321. up_write(&chip->ops_sem);
  322. }
  323. static void tpm_del_legacy_sysfs(struct tpm_chip *chip)
  324. {
  325. struct attribute **i;
  326. if (chip->flags & (TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_VIRTUAL))
  327. return;
  328. sysfs_remove_link(&chip->dev.parent->kobj, "ppi");
  329. for (i = chip->groups[0]->attrs; *i != NULL; ++i)
  330. sysfs_remove_link(&chip->dev.parent->kobj, (*i)->name);
  331. }
  332. /* For compatibility with legacy sysfs paths we provide symlinks from the
  333. * parent dev directory to selected names within the tpm chip directory. Old
  334. * kernel versions created these files directly under the parent.
  335. */
  336. static int tpm_add_legacy_sysfs(struct tpm_chip *chip)
  337. {
  338. struct attribute **i;
  339. int rc;
  340. if (chip->flags & (TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_VIRTUAL))
  341. return 0;
  342. rc = __compat_only_sysfs_link_entry_to_kobj(
  343. &chip->dev.parent->kobj, &chip->dev.kobj, "ppi");
  344. if (rc && rc != -ENOENT)
  345. return rc;
  346. /* All the names from tpm-sysfs */
  347. for (i = chip->groups[0]->attrs; *i != NULL; ++i) {
  348. rc = __compat_only_sysfs_link_entry_to_kobj(
  349. &chip->dev.parent->kobj, &chip->dev.kobj, (*i)->name);
  350. if (rc) {
  351. tpm_del_legacy_sysfs(chip);
  352. return rc;
  353. }
  354. }
  355. return 0;
  356. }
  357. static int tpm_hwrng_read(struct hwrng *rng, void *data, size_t max, bool wait)
  358. {
  359. struct tpm_chip *chip = container_of(rng, struct tpm_chip, hwrng);
  360. return tpm_get_random(chip, data, max);
  361. }
  362. static int tpm_add_hwrng(struct tpm_chip *chip)
  363. {
  364. if (!IS_ENABLED(CONFIG_HW_RANDOM_TPM))
  365. return 0;
  366. snprintf(chip->hwrng_name, sizeof(chip->hwrng_name),
  367. "tpm-rng-%d", chip->dev_num);
  368. chip->hwrng.name = chip->hwrng_name;
  369. chip->hwrng.read = tpm_hwrng_read;
  370. return hwrng_register(&chip->hwrng);
  371. }
  372. /*
  373. * tpm_chip_register() - create a character device for the TPM chip
  374. * @chip: TPM chip to use.
  375. *
  376. * Creates a character device for the TPM chip and adds sysfs attributes for
  377. * the device. As the last step this function adds the chip to the list of TPM
  378. * chips available for in-kernel use.
  379. *
  380. * This function should be only called after the chip initialization is
  381. * complete.
  382. */
  383. int tpm_chip_register(struct tpm_chip *chip)
  384. {
  385. int rc;
  386. if (chip->ops->flags & TPM_OPS_AUTO_STARTUP) {
  387. if (chip->flags & TPM_CHIP_FLAG_TPM2)
  388. rc = tpm2_auto_startup(chip);
  389. else
  390. rc = tpm1_auto_startup(chip);
  391. if (rc)
  392. return rc;
  393. }
  394. tpm_sysfs_add_device(chip);
  395. rc = tpm_bios_log_setup(chip);
  396. if (rc != 0 && rc != -ENODEV)
  397. return rc;
  398. tpm_add_ppi(chip);
  399. rc = tpm_add_hwrng(chip);
  400. if (rc)
  401. goto out_ppi;
  402. rc = tpm_add_char_device(chip);
  403. if (rc)
  404. goto out_hwrng;
  405. rc = tpm_add_legacy_sysfs(chip);
  406. if (rc) {
  407. tpm_chip_unregister(chip);
  408. return rc;
  409. }
  410. return 0;
  411. out_hwrng:
  412. if (IS_ENABLED(CONFIG_HW_RANDOM_TPM))
  413. hwrng_unregister(&chip->hwrng);
  414. out_ppi:
  415. tpm_bios_log_teardown(chip);
  416. return rc;
  417. }
  418. EXPORT_SYMBOL_GPL(tpm_chip_register);
  419. /*
  420. * tpm_chip_unregister() - release the TPM driver
  421. * @chip: TPM chip to use.
  422. *
  423. * Takes the chip first away from the list of available TPM chips and then
  424. * cleans up all the resources reserved by tpm_chip_register().
  425. *
  426. * Once this function returns the driver call backs in 'op's will not be
  427. * running and will no longer start.
  428. *
  429. * NOTE: This function should be only called before deinitializing chip
  430. * resources.
  431. */
  432. void tpm_chip_unregister(struct tpm_chip *chip)
  433. {
  434. tpm_del_legacy_sysfs(chip);
  435. if (IS_ENABLED(CONFIG_HW_RANDOM_TPM))
  436. hwrng_unregister(&chip->hwrng);
  437. tpm_bios_log_teardown(chip);
  438. if (chip->flags & TPM_CHIP_FLAG_TPM2)
  439. cdev_device_del(&chip->cdevs, &chip->devs);
  440. tpm_del_char_device(chip);
  441. }
  442. EXPORT_SYMBOL_GPL(tpm_chip_unregister);