tpm-chip.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  83. if (chip == NULL)
  84. return ERR_PTR(-ENOMEM);
  85. mutex_init(&chip->tpm_mutex);
  86. INIT_LIST_HEAD(&chip->list);
  87. chip->ops = ops;
  88. spin_lock(&driver_lock);
  89. chip->dev_num = find_first_zero_bit(dev_mask, TPM_NUM_DEVICES);
  90. spin_unlock(&driver_lock);
  91. if (chip->dev_num >= TPM_NUM_DEVICES) {
  92. dev_err(dev, "No available tpm device numbers\n");
  93. kfree(chip);
  94. return ERR_PTR(-ENOMEM);
  95. }
  96. set_bit(chip->dev_num, dev_mask);
  97. scnprintf(chip->devname, sizeof(chip->devname), "tpm%d", chip->dev_num);
  98. chip->pdev = dev;
  99. dev_set_drvdata(dev, chip);
  100. chip->dev.class = tpm_class;
  101. chip->dev.release = tpm_dev_release;
  102. chip->dev.parent = chip->pdev;
  103. if (chip->dev_num == 0)
  104. chip->dev.devt = MKDEV(MISC_MAJOR, TPM_MINOR);
  105. else
  106. chip->dev.devt = MKDEV(MAJOR(tpm_devt), chip->dev_num);
  107. dev_set_name(&chip->dev, "%s", chip->devname);
  108. device_initialize(&chip->dev);
  109. chip->cdev.owner = chip->pdev->driver->owner;
  110. cdev_init(&chip->cdev, &tpm_fops);
  111. return chip;
  112. }
  113. EXPORT_SYMBOL_GPL(tpmm_chip_alloc);
  114. static int tpm_dev_add_device(struct tpm_chip *chip)
  115. {
  116. int rc;
  117. rc = cdev_add(&chip->cdev, chip->dev.devt, 1);
  118. if (rc) {
  119. dev_err(&chip->dev,
  120. "unable to cdev_add() %s, major %d, minor %d, err=%d\n",
  121. chip->devname, MAJOR(chip->dev.devt),
  122. MINOR(chip->dev.devt), rc);
  123. device_unregister(&chip->dev);
  124. return rc;
  125. }
  126. rc = device_add(&chip->dev);
  127. if (rc) {
  128. dev_err(&chip->dev,
  129. "unable to device_register() %s, major %d, minor %d, err=%d\n",
  130. chip->devname, MAJOR(chip->dev.devt),
  131. MINOR(chip->dev.devt), rc);
  132. return rc;
  133. }
  134. return rc;
  135. }
  136. static void tpm_dev_del_device(struct tpm_chip *chip)
  137. {
  138. cdev_del(&chip->cdev);
  139. device_unregister(&chip->dev);
  140. }
  141. static int tpm1_chip_register(struct tpm_chip *chip)
  142. {
  143. int rc;
  144. if (chip->flags & TPM_CHIP_FLAG_TPM2)
  145. return 0;
  146. rc = tpm_sysfs_add_device(chip);
  147. if (rc)
  148. return rc;
  149. rc = tpm_add_ppi(chip);
  150. if (rc) {
  151. tpm_sysfs_del_device(chip);
  152. return rc;
  153. }
  154. chip->bios_dir = tpm_bios_log_setup(chip->devname);
  155. return 0;
  156. }
  157. static void tpm1_chip_unregister(struct tpm_chip *chip)
  158. {
  159. if (chip->flags & TPM_CHIP_FLAG_TPM2)
  160. return;
  161. if (chip->bios_dir)
  162. tpm_bios_log_teardown(chip->bios_dir);
  163. tpm_remove_ppi(chip);
  164. tpm_sysfs_del_device(chip);
  165. }
  166. /*
  167. * tpm_chip_register() - create a character device for the TPM chip
  168. * @chip: TPM chip to use.
  169. *
  170. * Creates a character device for the TPM chip and adds sysfs attributes for
  171. * the device. As the last step this function adds the chip to the list of TPM
  172. * chips available for in-kernel use.
  173. *
  174. * This function should be only called after the chip initialization is
  175. * complete.
  176. */
  177. int tpm_chip_register(struct tpm_chip *chip)
  178. {
  179. int rc;
  180. rc = tpm1_chip_register(chip);
  181. if (rc)
  182. return rc;
  183. rc = tpm_dev_add_device(chip);
  184. if (rc)
  185. goto out_err;
  186. /* Make the chip available. */
  187. spin_lock(&driver_lock);
  188. list_add_rcu(&chip->list, &tpm_chip_list);
  189. spin_unlock(&driver_lock);
  190. chip->flags |= TPM_CHIP_FLAG_REGISTERED;
  191. return 0;
  192. out_err:
  193. tpm1_chip_unregister(chip);
  194. return rc;
  195. }
  196. EXPORT_SYMBOL_GPL(tpm_chip_register);
  197. /*
  198. * tpm_chip_unregister() - release the TPM driver
  199. * @chip: TPM chip to use.
  200. *
  201. * Takes the chip first away from the list of available TPM chips and then
  202. * cleans up all the resources reserved by tpm_chip_register().
  203. *
  204. * NOTE: This function should be only called before deinitializing chip
  205. * resources.
  206. */
  207. void tpm_chip_unregister(struct tpm_chip *chip)
  208. {
  209. if (!(chip->flags & TPM_CHIP_FLAG_REGISTERED))
  210. return;
  211. spin_lock(&driver_lock);
  212. list_del_rcu(&chip->list);
  213. spin_unlock(&driver_lock);
  214. synchronize_rcu();
  215. tpm1_chip_unregister(chip);
  216. tpm_dev_del_device(chip);
  217. }
  218. EXPORT_SYMBOL_GPL(tpm_chip_unregister);