tpm-chip.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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_chip_alloc() - allocate a new struct tpm_chip instance
  118. * @pdev: device to which the chip is associated
  119. * At this point pdev mst be initialized, but does not have to
  120. * be registered
  121. * @ops: struct tpm_class_ops instance
  122. *
  123. * Allocates a new struct tpm_chip instance and assigns a free
  124. * device number for it. Must be paired with put_device(&chip->dev).
  125. */
  126. struct tpm_chip *tpm_chip_alloc(struct device *dev,
  127. const struct tpm_class_ops *ops)
  128. {
  129. struct tpm_chip *chip;
  130. int rc;
  131. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  132. if (chip == NULL)
  133. return ERR_PTR(-ENOMEM);
  134. mutex_init(&chip->tpm_mutex);
  135. init_rwsem(&chip->ops_sem);
  136. chip->ops = ops;
  137. mutex_lock(&idr_lock);
  138. rc = idr_alloc(&dev_nums_idr, NULL, 0, TPM_NUM_DEVICES, GFP_KERNEL);
  139. mutex_unlock(&idr_lock);
  140. if (rc < 0) {
  141. dev_err(dev, "No available tpm device numbers\n");
  142. kfree(chip);
  143. return ERR_PTR(rc);
  144. }
  145. chip->dev_num = rc;
  146. device_initialize(&chip->dev);
  147. chip->dev.class = tpm_class;
  148. chip->dev.release = tpm_dev_release;
  149. chip->dev.parent = dev;
  150. chip->dev.groups = chip->groups;
  151. if (chip->dev_num == 0)
  152. chip->dev.devt = MKDEV(MISC_MAJOR, TPM_MINOR);
  153. else
  154. chip->dev.devt = MKDEV(MAJOR(tpm_devt), chip->dev_num);
  155. rc = dev_set_name(&chip->dev, "tpm%d", chip->dev_num);
  156. if (rc)
  157. goto out;
  158. if (!dev)
  159. chip->flags |= TPM_CHIP_FLAG_VIRTUAL;
  160. cdev_init(&chip->cdev, &tpm_fops);
  161. chip->cdev.owner = THIS_MODULE;
  162. chip->cdev.kobj.parent = &chip->dev.kobj;
  163. return chip;
  164. out:
  165. put_device(&chip->dev);
  166. return ERR_PTR(rc);
  167. }
  168. EXPORT_SYMBOL_GPL(tpm_chip_alloc);
  169. /**
  170. * tpmm_chip_alloc() - allocate a new struct tpm_chip instance
  171. * @pdev: parent device to which the chip is associated
  172. * @ops: struct tpm_class_ops instance
  173. *
  174. * Same as tpm_chip_alloc except devm is used to do the put_device
  175. */
  176. struct tpm_chip *tpmm_chip_alloc(struct device *pdev,
  177. const struct tpm_class_ops *ops)
  178. {
  179. struct tpm_chip *chip;
  180. int rc;
  181. chip = tpm_chip_alloc(pdev, ops);
  182. if (IS_ERR(chip))
  183. return chip;
  184. rc = devm_add_action_or_reset(pdev,
  185. (void (*)(void *)) put_device,
  186. &chip->dev);
  187. if (rc)
  188. return ERR_PTR(rc);
  189. dev_set_drvdata(pdev, chip);
  190. return chip;
  191. }
  192. EXPORT_SYMBOL_GPL(tpmm_chip_alloc);
  193. static int tpm_add_char_device(struct tpm_chip *chip)
  194. {
  195. int rc;
  196. rc = cdev_add(&chip->cdev, chip->dev.devt, 1);
  197. if (rc) {
  198. dev_err(&chip->dev,
  199. "unable to cdev_add() %s, major %d, minor %d, err=%d\n",
  200. dev_name(&chip->dev), MAJOR(chip->dev.devt),
  201. MINOR(chip->dev.devt), rc);
  202. return rc;
  203. }
  204. rc = device_add(&chip->dev);
  205. if (rc) {
  206. dev_err(&chip->dev,
  207. "unable to device_register() %s, major %d, minor %d, err=%d\n",
  208. dev_name(&chip->dev), MAJOR(chip->dev.devt),
  209. MINOR(chip->dev.devt), rc);
  210. cdev_del(&chip->cdev);
  211. return rc;
  212. }
  213. /* Make the chip available. */
  214. mutex_lock(&idr_lock);
  215. idr_replace(&dev_nums_idr, chip, chip->dev_num);
  216. mutex_unlock(&idr_lock);
  217. return rc;
  218. }
  219. static void tpm_del_char_device(struct tpm_chip *chip)
  220. {
  221. cdev_del(&chip->cdev);
  222. device_del(&chip->dev);
  223. /* Make the chip unavailable. */
  224. mutex_lock(&idr_lock);
  225. idr_replace(&dev_nums_idr, NULL, chip->dev_num);
  226. mutex_unlock(&idr_lock);
  227. /* Make the driver uncallable. */
  228. down_write(&chip->ops_sem);
  229. if (chip->flags & TPM_CHIP_FLAG_TPM2)
  230. tpm2_shutdown(chip, TPM2_SU_CLEAR);
  231. chip->ops = NULL;
  232. up_write(&chip->ops_sem);
  233. }
  234. static int tpm1_chip_register(struct tpm_chip *chip)
  235. {
  236. if (chip->flags & TPM_CHIP_FLAG_TPM2)
  237. return 0;
  238. tpm_sysfs_add_device(chip);
  239. chip->bios_dir = tpm_bios_log_setup(dev_name(&chip->dev));
  240. return 0;
  241. }
  242. static void tpm1_chip_unregister(struct tpm_chip *chip)
  243. {
  244. if (chip->flags & TPM_CHIP_FLAG_TPM2)
  245. return;
  246. if (chip->bios_dir)
  247. tpm_bios_log_teardown(chip->bios_dir);
  248. }
  249. static void tpm_del_legacy_sysfs(struct tpm_chip *chip)
  250. {
  251. struct attribute **i;
  252. if (chip->flags & (TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_VIRTUAL))
  253. return;
  254. sysfs_remove_link(&chip->dev.parent->kobj, "ppi");
  255. for (i = chip->groups[0]->attrs; *i != NULL; ++i)
  256. sysfs_remove_link(&chip->dev.parent->kobj, (*i)->name);
  257. }
  258. /* For compatibility with legacy sysfs paths we provide symlinks from the
  259. * parent dev directory to selected names within the tpm chip directory. Old
  260. * kernel versions created these files directly under the parent.
  261. */
  262. static int tpm_add_legacy_sysfs(struct tpm_chip *chip)
  263. {
  264. struct attribute **i;
  265. int rc;
  266. if (chip->flags & (TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_VIRTUAL))
  267. return 0;
  268. rc = __compat_only_sysfs_link_entry_to_kobj(
  269. &chip->dev.parent->kobj, &chip->dev.kobj, "ppi");
  270. if (rc && rc != -ENOENT)
  271. return rc;
  272. /* All the names from tpm-sysfs */
  273. for (i = chip->groups[0]->attrs; *i != NULL; ++i) {
  274. rc = __compat_only_sysfs_link_entry_to_kobj(
  275. &chip->dev.parent->kobj, &chip->dev.kobj, (*i)->name);
  276. if (rc) {
  277. tpm_del_legacy_sysfs(chip);
  278. return rc;
  279. }
  280. }
  281. return 0;
  282. }
  283. /*
  284. * tpm_chip_register() - create a character device for the TPM chip
  285. * @chip: TPM chip to use.
  286. *
  287. * Creates a character device for the TPM chip and adds sysfs attributes for
  288. * the device. As the last step this function adds the chip to the list of TPM
  289. * chips available for in-kernel use.
  290. *
  291. * This function should be only called after the chip initialization is
  292. * complete.
  293. */
  294. int tpm_chip_register(struct tpm_chip *chip)
  295. {
  296. int rc;
  297. if (chip->ops->flags & TPM_OPS_AUTO_STARTUP) {
  298. if (chip->flags & TPM_CHIP_FLAG_TPM2)
  299. rc = tpm2_auto_startup(chip);
  300. else
  301. rc = tpm1_auto_startup(chip);
  302. if (rc)
  303. return rc;
  304. }
  305. rc = tpm1_chip_register(chip);
  306. if (rc)
  307. return rc;
  308. tpm_add_ppi(chip);
  309. rc = tpm_add_char_device(chip);
  310. if (rc) {
  311. tpm1_chip_unregister(chip);
  312. return rc;
  313. }
  314. chip->flags |= TPM_CHIP_FLAG_REGISTERED;
  315. rc = tpm_add_legacy_sysfs(chip);
  316. if (rc) {
  317. tpm_chip_unregister(chip);
  318. return rc;
  319. }
  320. return 0;
  321. }
  322. EXPORT_SYMBOL_GPL(tpm_chip_register);
  323. /*
  324. * tpm_chip_unregister() - release the TPM driver
  325. * @chip: TPM chip to use.
  326. *
  327. * Takes the chip first away from the list of available TPM chips and then
  328. * cleans up all the resources reserved by tpm_chip_register().
  329. *
  330. * Once this function returns the driver call backs in 'op's will not be
  331. * running and will no longer start.
  332. *
  333. * NOTE: This function should be only called before deinitializing chip
  334. * resources.
  335. */
  336. void tpm_chip_unregister(struct tpm_chip *chip)
  337. {
  338. if (!(chip->flags & TPM_CHIP_FLAG_REGISTERED))
  339. return;
  340. tpm_del_legacy_sysfs(chip);
  341. tpm1_chip_unregister(chip);
  342. tpm_del_char_device(chip);
  343. }
  344. EXPORT_SYMBOL_GPL(tpm_chip_unregister);