tpm-chip.c 11 KB

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