tpm-chip.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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 "tpm.h"
  29. #include "tpm_eventlog.h"
  30. DEFINE_IDR(dev_nums_idr);
  31. static DEFINE_MUTEX(idr_lock);
  32. struct class *tpm_class;
  33. dev_t tpm_devt;
  34. /**
  35. * tpm_try_get_ops() - Get a ref to the tpm_chip
  36. * @chip: Chip to ref
  37. *
  38. * The caller must already have some kind of locking to ensure that chip is
  39. * valid. This function will lock the chip so that the ops member can be
  40. * accessed safely. The locking prevents tpm_chip_unregister from
  41. * completing, so it should not be held for long periods.
  42. *
  43. * Returns -ERRNO if the chip could not be got.
  44. */
  45. int tpm_try_get_ops(struct tpm_chip *chip)
  46. {
  47. int rc = -EIO;
  48. get_device(&chip->dev);
  49. down_read(&chip->ops_sem);
  50. if (!chip->ops)
  51. goto out_lock;
  52. return 0;
  53. out_lock:
  54. up_read(&chip->ops_sem);
  55. put_device(&chip->dev);
  56. return rc;
  57. }
  58. EXPORT_SYMBOL_GPL(tpm_try_get_ops);
  59. /**
  60. * tpm_put_ops() - Release a ref to the tpm_chip
  61. * @chip: Chip to put
  62. *
  63. * This is the opposite pair to tpm_try_get_ops(). After this returns chip may
  64. * be kfree'd.
  65. */
  66. void tpm_put_ops(struct tpm_chip *chip)
  67. {
  68. up_read(&chip->ops_sem);
  69. put_device(&chip->dev);
  70. }
  71. EXPORT_SYMBOL_GPL(tpm_put_ops);
  72. /**
  73. * tpm_chip_find_get() - return tpm_chip for a given chip number
  74. * @chip_num: id to find
  75. *
  76. * The return'd chip has been tpm_try_get_ops'd and must be released via
  77. * tpm_put_ops
  78. */
  79. struct tpm_chip *tpm_chip_find_get(int chip_num)
  80. {
  81. struct tpm_chip *chip, *res = NULL;
  82. int chip_prev;
  83. mutex_lock(&idr_lock);
  84. if (chip_num == TPM_ANY_NUM) {
  85. chip_num = 0;
  86. do {
  87. chip_prev = chip_num;
  88. chip = idr_get_next(&dev_nums_idr, &chip_num);
  89. if (chip && !tpm_try_get_ops(chip)) {
  90. res = chip;
  91. break;
  92. }
  93. } while (chip_prev != chip_num);
  94. } else {
  95. chip = idr_find_slowpath(&dev_nums_idr, chip_num);
  96. if (chip && !tpm_try_get_ops(chip))
  97. res = chip;
  98. }
  99. mutex_unlock(&idr_lock);
  100. return res;
  101. }
  102. /**
  103. * tpm_dev_release() - free chip memory and the device number
  104. * @dev: the character device for the TPM chip
  105. *
  106. * This is used as the release function for the character device.
  107. */
  108. static void tpm_dev_release(struct device *dev)
  109. {
  110. struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev);
  111. mutex_lock(&idr_lock);
  112. idr_remove(&dev_nums_idr, chip->dev_num);
  113. mutex_unlock(&idr_lock);
  114. kfree(chip);
  115. }
  116. /**
  117. * tpm_class_shutdown() - prepare the TPM device for loss of power.
  118. * @dev: device to which the chip is associated.
  119. *
  120. * Issues a TPM2_Shutdown command prior to loss of power, as required by the
  121. * TPM 2.0 spec.
  122. * Then, calls bus- and device- specific shutdown code.
  123. *
  124. * XXX: This codepath relies on the fact that sysfs is not enabled for
  125. * TPM2: sysfs uses an implicit lock on chip->ops, so this could race if TPM2
  126. * has sysfs support enabled before TPM sysfs's implicit locking is fixed.
  127. */
  128. static int tpm_class_shutdown(struct device *dev)
  129. {
  130. struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev);
  131. if (chip->flags & TPM_CHIP_FLAG_TPM2) {
  132. down_write(&chip->ops_sem);
  133. tpm2_shutdown(chip, TPM2_SU_CLEAR);
  134. chip->ops = NULL;
  135. up_write(&chip->ops_sem);
  136. }
  137. /* Allow bus- and device-specific code to run. Note: since chip->ops
  138. * is NULL, more-specific shutdown code will not be able to issue TPM
  139. * commands.
  140. */
  141. if (dev->bus && dev->bus->shutdown)
  142. dev->bus->shutdown(dev);
  143. else if (dev->driver && dev->driver->shutdown)
  144. dev->driver->shutdown(dev);
  145. return 0;
  146. }
  147. /**
  148. * tpm_chip_alloc() - allocate a new struct tpm_chip instance
  149. * @pdev: device to which the chip is associated
  150. * At this point pdev mst be initialized, but does not have to
  151. * be registered
  152. * @ops: struct tpm_class_ops instance
  153. *
  154. * Allocates a new struct tpm_chip instance and assigns a free
  155. * device number for it. Must be paired with put_device(&chip->dev).
  156. */
  157. struct tpm_chip *tpm_chip_alloc(struct device *pdev,
  158. const struct tpm_class_ops *ops)
  159. {
  160. struct tpm_chip *chip;
  161. int rc;
  162. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  163. if (chip == NULL)
  164. return ERR_PTR(-ENOMEM);
  165. mutex_init(&chip->tpm_mutex);
  166. init_rwsem(&chip->ops_sem);
  167. chip->ops = ops;
  168. mutex_lock(&idr_lock);
  169. rc = idr_alloc(&dev_nums_idr, NULL, 0, TPM_NUM_DEVICES, GFP_KERNEL);
  170. mutex_unlock(&idr_lock);
  171. if (rc < 0) {
  172. dev_err(pdev, "No available tpm device numbers\n");
  173. kfree(chip);
  174. return ERR_PTR(rc);
  175. }
  176. chip->dev_num = rc;
  177. device_initialize(&chip->dev);
  178. chip->dev.class = tpm_class;
  179. chip->dev.class->shutdown = tpm_class_shutdown;
  180. chip->dev.release = tpm_dev_release;
  181. chip->dev.parent = pdev;
  182. chip->dev.groups = chip->groups;
  183. if (chip->dev_num == 0)
  184. chip->dev.devt = MKDEV(MISC_MAJOR, TPM_MINOR);
  185. else
  186. chip->dev.devt = MKDEV(MAJOR(tpm_devt), chip->dev_num);
  187. rc = dev_set_name(&chip->dev, "tpm%d", chip->dev_num);
  188. if (rc)
  189. goto out;
  190. if (!pdev)
  191. chip->flags |= TPM_CHIP_FLAG_VIRTUAL;
  192. cdev_init(&chip->cdev, &tpm_fops);
  193. chip->cdev.owner = THIS_MODULE;
  194. chip->cdev.kobj.parent = &chip->dev.kobj;
  195. return chip;
  196. out:
  197. put_device(&chip->dev);
  198. return ERR_PTR(rc);
  199. }
  200. EXPORT_SYMBOL_GPL(tpm_chip_alloc);
  201. /**
  202. * tpmm_chip_alloc() - allocate a new struct tpm_chip instance
  203. * @pdev: parent device to which the chip is associated
  204. * @ops: struct tpm_class_ops instance
  205. *
  206. * Same as tpm_chip_alloc except devm is used to do the put_device
  207. */
  208. struct tpm_chip *tpmm_chip_alloc(struct device *pdev,
  209. const struct tpm_class_ops *ops)
  210. {
  211. struct tpm_chip *chip;
  212. int rc;
  213. chip = tpm_chip_alloc(pdev, ops);
  214. if (IS_ERR(chip))
  215. return chip;
  216. rc = devm_add_action_or_reset(pdev,
  217. (void (*)(void *)) put_device,
  218. &chip->dev);
  219. if (rc)
  220. return ERR_PTR(rc);
  221. dev_set_drvdata(pdev, chip);
  222. return chip;
  223. }
  224. EXPORT_SYMBOL_GPL(tpmm_chip_alloc);
  225. static int tpm_add_char_device(struct tpm_chip *chip)
  226. {
  227. int rc;
  228. rc = cdev_add(&chip->cdev, chip->dev.devt, 1);
  229. if (rc) {
  230. dev_err(&chip->dev,
  231. "unable to cdev_add() %s, major %d, minor %d, err=%d\n",
  232. dev_name(&chip->dev), MAJOR(chip->dev.devt),
  233. MINOR(chip->dev.devt), rc);
  234. return rc;
  235. }
  236. rc = device_add(&chip->dev);
  237. if (rc) {
  238. dev_err(&chip->dev,
  239. "unable to device_register() %s, major %d, minor %d, err=%d\n",
  240. dev_name(&chip->dev), MAJOR(chip->dev.devt),
  241. MINOR(chip->dev.devt), rc);
  242. cdev_del(&chip->cdev);
  243. return rc;
  244. }
  245. /* Make the chip available. */
  246. mutex_lock(&idr_lock);
  247. idr_replace(&dev_nums_idr, chip, chip->dev_num);
  248. mutex_unlock(&idr_lock);
  249. return rc;
  250. }
  251. static void tpm_del_char_device(struct tpm_chip *chip)
  252. {
  253. cdev_del(&chip->cdev);
  254. device_del(&chip->dev);
  255. /* Make the chip unavailable. */
  256. mutex_lock(&idr_lock);
  257. idr_replace(&dev_nums_idr, NULL, chip->dev_num);
  258. mutex_unlock(&idr_lock);
  259. /* Make the driver uncallable. */
  260. down_write(&chip->ops_sem);
  261. if (chip->flags & TPM_CHIP_FLAG_TPM2)
  262. tpm2_shutdown(chip, TPM2_SU_CLEAR);
  263. chip->ops = NULL;
  264. up_write(&chip->ops_sem);
  265. }
  266. static int tpm1_chip_register(struct tpm_chip *chip)
  267. {
  268. if (chip->flags & TPM_CHIP_FLAG_TPM2)
  269. return 0;
  270. tpm_sysfs_add_device(chip);
  271. chip->bios_dir = tpm_bios_log_setup(dev_name(&chip->dev));
  272. return 0;
  273. }
  274. static void tpm1_chip_unregister(struct tpm_chip *chip)
  275. {
  276. if (chip->flags & TPM_CHIP_FLAG_TPM2)
  277. return;
  278. if (chip->bios_dir)
  279. tpm_bios_log_teardown(chip->bios_dir);
  280. }
  281. static void tpm_del_legacy_sysfs(struct tpm_chip *chip)
  282. {
  283. struct attribute **i;
  284. if (chip->flags & (TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_VIRTUAL))
  285. return;
  286. sysfs_remove_link(&chip->dev.parent->kobj, "ppi");
  287. for (i = chip->groups[0]->attrs; *i != NULL; ++i)
  288. sysfs_remove_link(&chip->dev.parent->kobj, (*i)->name);
  289. }
  290. /* For compatibility with legacy sysfs paths we provide symlinks from the
  291. * parent dev directory to selected names within the tpm chip directory. Old
  292. * kernel versions created these files directly under the parent.
  293. */
  294. static int tpm_add_legacy_sysfs(struct tpm_chip *chip)
  295. {
  296. struct attribute **i;
  297. int rc;
  298. if (chip->flags & (TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_VIRTUAL))
  299. return 0;
  300. rc = __compat_only_sysfs_link_entry_to_kobj(
  301. &chip->dev.parent->kobj, &chip->dev.kobj, "ppi");
  302. if (rc && rc != -ENOENT)
  303. return rc;
  304. /* All the names from tpm-sysfs */
  305. for (i = chip->groups[0]->attrs; *i != NULL; ++i) {
  306. rc = __compat_only_sysfs_link_entry_to_kobj(
  307. &chip->dev.parent->kobj, &chip->dev.kobj, (*i)->name);
  308. if (rc) {
  309. tpm_del_legacy_sysfs(chip);
  310. return rc;
  311. }
  312. }
  313. return 0;
  314. }
  315. /*
  316. * tpm_chip_register() - create a character device for the TPM chip
  317. * @chip: TPM chip to use.
  318. *
  319. * Creates a character device for the TPM chip and adds sysfs attributes for
  320. * the device. As the last step this function adds the chip to the list of TPM
  321. * chips available for in-kernel use.
  322. *
  323. * This function should be only called after the chip initialization is
  324. * complete.
  325. */
  326. int tpm_chip_register(struct tpm_chip *chip)
  327. {
  328. int rc;
  329. if (chip->ops->flags & TPM_OPS_AUTO_STARTUP) {
  330. if (chip->flags & TPM_CHIP_FLAG_TPM2)
  331. rc = tpm2_auto_startup(chip);
  332. else
  333. rc = tpm1_auto_startup(chip);
  334. if (rc)
  335. return rc;
  336. }
  337. rc = tpm1_chip_register(chip);
  338. if (rc)
  339. return rc;
  340. tpm_add_ppi(chip);
  341. rc = tpm_add_char_device(chip);
  342. if (rc) {
  343. tpm1_chip_unregister(chip);
  344. return rc;
  345. }
  346. chip->flags |= TPM_CHIP_FLAG_REGISTERED;
  347. rc = tpm_add_legacy_sysfs(chip);
  348. if (rc) {
  349. tpm_chip_unregister(chip);
  350. return rc;
  351. }
  352. return 0;
  353. }
  354. EXPORT_SYMBOL_GPL(tpm_chip_register);
  355. /*
  356. * tpm_chip_unregister() - release the TPM driver
  357. * @chip: TPM chip to use.
  358. *
  359. * Takes the chip first away from the list of available TPM chips and then
  360. * cleans up all the resources reserved by tpm_chip_register().
  361. *
  362. * Once this function returns the driver call backs in 'op's will not be
  363. * running and will no longer start.
  364. *
  365. * NOTE: This function should be only called before deinitializing chip
  366. * resources.
  367. */
  368. void tpm_chip_unregister(struct tpm_chip *chip)
  369. {
  370. if (!(chip->flags & TPM_CHIP_FLAG_REGISTERED))
  371. return;
  372. tpm_del_legacy_sysfs(chip);
  373. tpm1_chip_unregister(chip);
  374. tpm_del_char_device(chip);
  375. }
  376. EXPORT_SYMBOL_GPL(tpm_chip_unregister);