tpm-chip.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. static DECLARE_BITMAP(dev_mask, TPM_NUM_DEVICES);
  31. static LIST_HEAD(tpm_chip_list);
  32. static DEFINE_SPINLOCK(driver_lock);
  33. struct class *tpm_class;
  34. dev_t tpm_devt;
  35. /*
  36. * tpm_chip_find_get - return tpm_chip for a given chip number
  37. * @chip_num the device number for the chip
  38. */
  39. struct tpm_chip *tpm_chip_find_get(int chip_num)
  40. {
  41. struct tpm_chip *pos, *chip = NULL;
  42. rcu_read_lock();
  43. list_for_each_entry_rcu(pos, &tpm_chip_list, list) {
  44. if (chip_num != TPM_ANY_NUM && chip_num != pos->dev_num)
  45. continue;
  46. if (try_module_get(pos->pdev->driver->owner)) {
  47. chip = pos;
  48. break;
  49. }
  50. }
  51. rcu_read_unlock();
  52. return chip;
  53. }
  54. /**
  55. * tpm_dev_release() - free chip memory and the device number
  56. * @dev: the character device for the TPM chip
  57. *
  58. * This is used as the release function for the character device.
  59. */
  60. static void tpm_dev_release(struct device *dev)
  61. {
  62. struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev);
  63. spin_lock(&driver_lock);
  64. clear_bit(chip->dev_num, dev_mask);
  65. spin_unlock(&driver_lock);
  66. kfree(chip);
  67. }
  68. /**
  69. * tpmm_chip_alloc() - allocate a new struct tpm_chip instance
  70. * @dev: device to which the chip is associated
  71. * @ops: struct tpm_class_ops instance
  72. *
  73. * Allocates a new struct tpm_chip instance and assigns a free
  74. * device number for it. Caller does not have to worry about
  75. * freeing the allocated resources. When the devices is removed
  76. * devres calls tpmm_chip_remove() to do the job.
  77. */
  78. struct tpm_chip *tpmm_chip_alloc(struct device *dev,
  79. const struct tpm_class_ops *ops)
  80. {
  81. struct tpm_chip *chip;
  82. int rc;
  83. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  84. if (chip == NULL)
  85. return ERR_PTR(-ENOMEM);
  86. mutex_init(&chip->tpm_mutex);
  87. INIT_LIST_HEAD(&chip->list);
  88. chip->ops = ops;
  89. spin_lock(&driver_lock);
  90. chip->dev_num = find_first_zero_bit(dev_mask, TPM_NUM_DEVICES);
  91. spin_unlock(&driver_lock);
  92. if (chip->dev_num >= TPM_NUM_DEVICES) {
  93. dev_err(dev, "No available tpm device numbers\n");
  94. kfree(chip);
  95. return ERR_PTR(-ENOMEM);
  96. }
  97. set_bit(chip->dev_num, dev_mask);
  98. scnprintf(chip->devname, sizeof(chip->devname), "tpm%d", chip->dev_num);
  99. chip->pdev = dev;
  100. dev_set_drvdata(dev, chip);
  101. chip->dev.class = tpm_class;
  102. chip->dev.release = tpm_dev_release;
  103. chip->dev.parent = chip->pdev;
  104. #ifdef CONFIG_ACPI
  105. chip->dev.groups = chip->groups;
  106. #endif
  107. if (chip->dev_num == 0)
  108. chip->dev.devt = MKDEV(MISC_MAJOR, TPM_MINOR);
  109. else
  110. chip->dev.devt = MKDEV(MAJOR(tpm_devt), chip->dev_num);
  111. dev_set_name(&chip->dev, "%s", chip->devname);
  112. device_initialize(&chip->dev);
  113. cdev_init(&chip->cdev, &tpm_fops);
  114. chip->cdev.owner = chip->pdev->driver->owner;
  115. chip->cdev.kobj.parent = &chip->dev.kobj;
  116. rc = devm_add_action(dev, (void (*)(void *)) put_device, &chip->dev);
  117. if (rc) {
  118. put_device(&chip->dev);
  119. return ERR_PTR(rc);
  120. }
  121. return chip;
  122. }
  123. EXPORT_SYMBOL_GPL(tpmm_chip_alloc);
  124. static int tpm_add_char_device(struct tpm_chip *chip)
  125. {
  126. int rc;
  127. rc = cdev_add(&chip->cdev, chip->dev.devt, 1);
  128. if (rc) {
  129. dev_err(&chip->dev,
  130. "unable to cdev_add() %s, major %d, minor %d, err=%d\n",
  131. chip->devname, MAJOR(chip->dev.devt),
  132. MINOR(chip->dev.devt), rc);
  133. return rc;
  134. }
  135. rc = device_add(&chip->dev);
  136. if (rc) {
  137. dev_err(&chip->dev,
  138. "unable to device_register() %s, major %d, minor %d, err=%d\n",
  139. chip->devname, MAJOR(chip->dev.devt),
  140. MINOR(chip->dev.devt), rc);
  141. cdev_del(&chip->cdev);
  142. return rc;
  143. }
  144. return rc;
  145. }
  146. static void tpm_del_char_device(struct tpm_chip *chip)
  147. {
  148. cdev_del(&chip->cdev);
  149. device_del(&chip->dev);
  150. }
  151. static int tpm1_chip_register(struct tpm_chip *chip)
  152. {
  153. int rc;
  154. if (chip->flags & TPM_CHIP_FLAG_TPM2)
  155. return 0;
  156. rc = tpm_sysfs_add_device(chip);
  157. if (rc)
  158. return rc;
  159. chip->bios_dir = tpm_bios_log_setup(chip->devname);
  160. return 0;
  161. }
  162. static void tpm1_chip_unregister(struct tpm_chip *chip)
  163. {
  164. if (chip->flags & TPM_CHIP_FLAG_TPM2)
  165. return;
  166. if (chip->bios_dir)
  167. tpm_bios_log_teardown(chip->bios_dir);
  168. tpm_sysfs_del_device(chip);
  169. }
  170. /*
  171. * tpm_chip_register() - create a character device for the TPM chip
  172. * @chip: TPM chip to use.
  173. *
  174. * Creates a character device for the TPM chip and adds sysfs attributes for
  175. * the device. As the last step this function adds the chip to the list of TPM
  176. * chips available for in-kernel use.
  177. *
  178. * This function should be only called after the chip initialization is
  179. * complete.
  180. */
  181. int tpm_chip_register(struct tpm_chip *chip)
  182. {
  183. int rc;
  184. rc = tpm1_chip_register(chip);
  185. if (rc)
  186. return rc;
  187. tpm_add_ppi(chip);
  188. rc = tpm_add_char_device(chip);
  189. if (rc)
  190. goto out_err;
  191. /* Make the chip available. */
  192. spin_lock(&driver_lock);
  193. list_add_tail_rcu(&chip->list, &tpm_chip_list);
  194. spin_unlock(&driver_lock);
  195. chip->flags |= TPM_CHIP_FLAG_REGISTERED;
  196. if (!(chip->flags & TPM_CHIP_FLAG_TPM2)) {
  197. rc = __compat_only_sysfs_link_entry_to_kobj(&chip->pdev->kobj,
  198. &chip->dev.kobj,
  199. "ppi");
  200. if (rc && rc != -ENOENT) {
  201. tpm_chip_unregister(chip);
  202. return rc;
  203. }
  204. }
  205. return 0;
  206. out_err:
  207. tpm1_chip_unregister(chip);
  208. return rc;
  209. }
  210. EXPORT_SYMBOL_GPL(tpm_chip_register);
  211. /*
  212. * tpm_chip_unregister() - release the TPM driver
  213. * @chip: TPM chip to use.
  214. *
  215. * Takes the chip first away from the list of available TPM chips and then
  216. * cleans up all the resources reserved by tpm_chip_register().
  217. *
  218. * NOTE: This function should be only called before deinitializing chip
  219. * resources.
  220. */
  221. void tpm_chip_unregister(struct tpm_chip *chip)
  222. {
  223. if (!(chip->flags & TPM_CHIP_FLAG_REGISTERED))
  224. return;
  225. spin_lock(&driver_lock);
  226. list_del_rcu(&chip->list);
  227. spin_unlock(&driver_lock);
  228. synchronize_rcu();
  229. if (!(chip->flags & TPM_CHIP_FLAG_TPM2))
  230. sysfs_remove_link(&chip->pdev->kobj, "ppi");
  231. tpm1_chip_unregister(chip);
  232. tpm_del_char_device(chip);
  233. }
  234. EXPORT_SYMBOL_GPL(tpm_chip_unregister);