tpm-chip.c 8.9 KB

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