blob: 2ea2f1561e59f9b01280cf8491f49832ed39b9fe [file] [log] [blame]
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -08001/*
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
23#include <linux/poll.h>
24#include <linux/slab.h>
25#include <linux/mutex.h>
26#include <linux/spinlock.h>
27#include <linux/freezer.h>
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -080028#include <linux/major.h>
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -080029#include "tpm.h"
30#include "tpm_eventlog.h"
31
Stefan Berger15516782016-02-29 08:53:02 -050032DEFINE_IDR(dev_nums_idr);
33static DEFINE_MUTEX(idr_lock);
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -080034
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -080035struct class *tpm_class;
36dev_t tpm_devt;
37
Jason Gunthorpe4e261952016-02-12 20:29:53 -070038/**
39 * tpm_try_get_ops() - Get a ref to the tpm_chip
40 * @chip: Chip to ref
41 *
42 * The caller must already have some kind of locking to ensure that chip is
43 * valid. This function will lock the chip so that the ops member can be
44 * accessed safely. The locking prevents tpm_chip_unregister from
45 * completing, so it should not be held for long periods.
46 *
47 * Returns -ERRNO if the chip could not be got.
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -080048 */
Jason Gunthorpe4e261952016-02-12 20:29:53 -070049int tpm_try_get_ops(struct tpm_chip *chip)
50{
51 int rc = -EIO;
52
53 get_device(&chip->dev);
54
55 down_read(&chip->ops_sem);
56 if (!chip->ops)
57 goto out_lock;
58
Jason Gunthorpe4e261952016-02-12 20:29:53 -070059 return 0;
60out_lock:
61 up_read(&chip->ops_sem);
62 put_device(&chip->dev);
63 return rc;
64}
65EXPORT_SYMBOL_GPL(tpm_try_get_ops);
66
67/**
68 * tpm_put_ops() - Release a ref to the tpm_chip
69 * @chip: Chip to put
70 *
71 * This is the opposite pair to tpm_try_get_ops(). After this returns chip may
72 * be kfree'd.
73 */
74void tpm_put_ops(struct tpm_chip *chip)
75{
Jason Gunthorpe4e261952016-02-12 20:29:53 -070076 up_read(&chip->ops_sem);
77 put_device(&chip->dev);
78}
79EXPORT_SYMBOL_GPL(tpm_put_ops);
80
81/**
82 * tpm_chip_find_get() - return tpm_chip for a given chip number
83 * @chip_num: id to find
84 *
85 * The return'd chip has been tpm_try_get_ops'd and must be released via
86 * tpm_put_ops
87 */
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -080088struct tpm_chip *tpm_chip_find_get(int chip_num)
89{
Stefan Berger15516782016-02-29 08:53:02 -050090 struct tpm_chip *chip, *res = NULL;
91 int chip_prev;
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -080092
Stefan Berger15516782016-02-29 08:53:02 -050093 mutex_lock(&idr_lock);
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -080094
Stefan Berger15516782016-02-29 08:53:02 -050095 if (chip_num == TPM_ANY_NUM) {
96 chip_num = 0;
97 do {
98 chip_prev = chip_num;
99 chip = idr_get_next(&dev_nums_idr, &chip_num);
100 if (chip && !tpm_try_get_ops(chip)) {
101 res = chip;
102 break;
103 }
104 } while (chip_prev != chip_num);
105 } else {
106 chip = idr_find_slowpath(&dev_nums_idr, chip_num);
107 if (chip && !tpm_try_get_ops(chip))
108 res = chip;
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800109 }
Stefan Berger15516782016-02-29 08:53:02 -0500110
111 mutex_unlock(&idr_lock);
112
113 return res;
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800114}
115
116/**
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -0800117 * tpm_dev_release() - free chip memory and the device number
118 * @dev: the character device for the TPM chip
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800119 *
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -0800120 * This is used as the release function for the character device.
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800121 */
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -0800122static void tpm_dev_release(struct device *dev)
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800123{
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -0800124 struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev);
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800125
Stefan Berger15516782016-02-29 08:53:02 -0500126 mutex_lock(&idr_lock);
127 idr_remove(&dev_nums_idr, chip->dev_num);
128 mutex_unlock(&idr_lock);
129
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800130 kfree(chip);
131}
132
133/**
Jason Gunthorpe3897cd92016-02-11 12:45:48 -0700134 * tpm_chip_alloc() - allocate a new struct tpm_chip instance
135 * @pdev: device to which the chip is associated
136 * At this point pdev mst be initialized, but does not have to
137 * be registered
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800138 * @ops: struct tpm_class_ops instance
139 *
140 * Allocates a new struct tpm_chip instance and assigns a free
Jason Gunthorpe3897cd92016-02-11 12:45:48 -0700141 * device number for it. Must be paired with put_device(&chip->dev).
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800142 */
Jason Gunthorpe3897cd92016-02-11 12:45:48 -0700143struct tpm_chip *tpm_chip_alloc(struct device *dev,
144 const struct tpm_class_ops *ops)
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800145{
146 struct tpm_chip *chip;
Jarkko Sakkinen4f3b1932016-02-13 11:58:16 +0200147 int rc;
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800148
149 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
150 if (chip == NULL)
151 return ERR_PTR(-ENOMEM);
152
153 mutex_init(&chip->tpm_mutex);
Jason Gunthorpe4e261952016-02-12 20:29:53 -0700154 init_rwsem(&chip->ops_sem);
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800155
156 chip->ops = ops;
157
Stefan Berger15516782016-02-29 08:53:02 -0500158 mutex_lock(&idr_lock);
159 rc = idr_alloc(&dev_nums_idr, NULL, 0, TPM_NUM_DEVICES, GFP_KERNEL);
160 mutex_unlock(&idr_lock);
161 if (rc < 0) {
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800162 dev_err(dev, "No available tpm device numbers\n");
163 kfree(chip);
Stefan Berger15516782016-02-29 08:53:02 -0500164 return ERR_PTR(rc);
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800165 }
Stefan Berger15516782016-02-29 08:53:02 -0500166 chip->dev_num = rc;
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800167
Jason Gunthorpe3635e2e2016-02-29 12:29:48 -0500168 device_initialize(&chip->dev);
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800169
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -0800170 chip->dev.class = tpm_class;
171 chip->dev.release = tpm_dev_release;
Jason Gunthorpe8cfffc92016-02-29 12:29:47 -0500172 chip->dev.parent = dev;
Jarkko Sakkinen9b774d52015-04-14 17:56:48 +0300173#ifdef CONFIG_ACPI
174 chip->dev.groups = chip->groups;
175#endif
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -0800176
177 if (chip->dev_num == 0)
178 chip->dev.devt = MKDEV(MISC_MAJOR, TPM_MINOR);
179 else
180 chip->dev.devt = MKDEV(MAJOR(tpm_devt), chip->dev_num);
181
Jason Gunthorpe3635e2e2016-02-29 12:29:48 -0500182 rc = dev_set_name(&chip->dev, "tpm%d", chip->dev_num);
183 if (rc)
184 goto out;
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -0800185
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -0800186 cdev_init(&chip->cdev, &tpm_fops);
Stefan Berger2072df42016-02-29 08:53:01 -0500187 chip->cdev.owner = THIS_MODULE;
Jason Gunthorpeba0ef852015-06-30 13:15:31 -0600188 chip->cdev.kobj.parent = &chip->dev.kobj;
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -0800189
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800190 return chip;
Jason Gunthorpe3635e2e2016-02-29 12:29:48 -0500191
192out:
193 put_device(&chip->dev);
194 return ERR_PTR(rc);
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800195}
Jason Gunthorpe3897cd92016-02-11 12:45:48 -0700196EXPORT_SYMBOL_GPL(tpm_chip_alloc);
197
198/**
199 * tpmm_chip_alloc() - allocate a new struct tpm_chip instance
200 * @pdev: parent device to which the chip is associated
201 * @ops: struct tpm_class_ops instance
202 *
203 * Same as tpm_chip_alloc except devm is used to do the put_device
204 */
205struct tpm_chip *tpmm_chip_alloc(struct device *pdev,
206 const struct tpm_class_ops *ops)
207{
208 struct tpm_chip *chip;
209 int rc;
210
211 chip = tpm_chip_alloc(pdev, ops);
212 if (IS_ERR(chip))
213 return chip;
214
215 rc = devm_add_action(pdev, (void (*)(void *)) put_device, &chip->dev);
216 if (rc) {
217 put_device(&chip->dev);
218 return ERR_PTR(rc);
219 }
220
221 dev_set_drvdata(pdev, chip);
222
223 return chip;
224}
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800225EXPORT_SYMBOL_GPL(tpmm_chip_alloc);
226
Jarkko Sakkinen72c91ce2016-01-29 09:47:22 -0800227static int tpm_add_char_device(struct tpm_chip *chip)
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -0800228{
229 int rc;
230
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -0800231 rc = cdev_add(&chip->cdev, chip->dev.devt, 1);
232 if (rc) {
233 dev_err(&chip->dev,
234 "unable to cdev_add() %s, major %d, minor %d, err=%d\n",
Jason Gunthorpe3635e2e2016-02-29 12:29:48 -0500235 dev_name(&chip->dev), MAJOR(chip->dev.devt),
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -0800236 MINOR(chip->dev.devt), rc);
237
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -0800238 return rc;
239 }
240
Jarkko Sakkinend972b052015-03-01 23:55:47 +0200241 rc = device_add(&chip->dev);
242 if (rc) {
243 dev_err(&chip->dev,
244 "unable to device_register() %s, major %d, minor %d, err=%d\n",
Jason Gunthorpe3635e2e2016-02-29 12:29:48 -0500245 dev_name(&chip->dev), MAJOR(chip->dev.devt),
Jarkko Sakkinend972b052015-03-01 23:55:47 +0200246 MINOR(chip->dev.devt), rc);
247
Jarkko Sakkinen72c91ce2016-01-29 09:47:22 -0800248 cdev_del(&chip->cdev);
Jarkko Sakkinend972b052015-03-01 23:55:47 +0200249 return rc;
250 }
251
Stefan Berger15516782016-02-29 08:53:02 -0500252 /* Make the chip available. */
253 mutex_lock(&idr_lock);
254 idr_replace(&dev_nums_idr, chip, chip->dev_num);
255 mutex_unlock(&idr_lock);
256
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -0800257 return rc;
258}
259
Jarkko Sakkinen72c91ce2016-01-29 09:47:22 -0800260static void tpm_del_char_device(struct tpm_chip *chip)
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -0800261{
262 cdev_del(&chip->cdev);
Stefan Berger15516782016-02-29 08:53:02 -0500263 device_del(&chip->dev);
264
265 /* Make the chip unavailable. */
266 mutex_lock(&idr_lock);
267 idr_replace(&dev_nums_idr, NULL, chip->dev_num);
268 mutex_unlock(&idr_lock);
Jason Gunthorpe4e261952016-02-12 20:29:53 -0700269
270 /* Make the driver uncallable. */
271 down_write(&chip->ops_sem);
Jarkko Sakkinenc0dff1f2016-04-25 12:20:07 +0300272 if (chip->flags & TPM_CHIP_FLAG_TPM2)
273 tpm2_shutdown(chip, TPM2_SU_CLEAR);
Jason Gunthorpe4e261952016-02-12 20:29:53 -0700274 chip->ops = NULL;
275 up_write(&chip->ops_sem);
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -0800276}
277
Jarkko Sakkinen34d47b62015-03-18 08:17:14 +0200278static int tpm1_chip_register(struct tpm_chip *chip)
279{
280 int rc;
281
282 if (chip->flags & TPM_CHIP_FLAG_TPM2)
283 return 0;
284
285 rc = tpm_sysfs_add_device(chip);
286 if (rc)
287 return rc;
288
Jason Gunthorpe3635e2e2016-02-29 12:29:48 -0500289 chip->bios_dir = tpm_bios_log_setup(dev_name(&chip->dev));
Jarkko Sakkinen34d47b62015-03-18 08:17:14 +0200290
291 return 0;
292}
293
294static void tpm1_chip_unregister(struct tpm_chip *chip)
295{
296 if (chip->flags & TPM_CHIP_FLAG_TPM2)
297 return;
298
299 if (chip->bios_dir)
300 tpm_bios_log_teardown(chip->bios_dir);
301
Jarkko Sakkinen34d47b62015-03-18 08:17:14 +0200302 tpm_sysfs_del_device(chip);
303}
304
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800305/*
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -0800306 * tpm_chip_register() - create a character device for the TPM chip
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800307 * @chip: TPM chip to use.
308 *
Jarkko Sakkinend972b052015-03-01 23:55:47 +0200309 * Creates a character device for the TPM chip and adds sysfs attributes for
310 * the device. As the last step this function adds the chip to the list of TPM
311 * chips available for in-kernel use.
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800312 *
Jarkko Sakkinend972b052015-03-01 23:55:47 +0200313 * This function should be only called after the chip initialization is
314 * complete.
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800315 */
316int tpm_chip_register(struct tpm_chip *chip)
317{
318 int rc;
319
Jarkko Sakkinen34d47b62015-03-18 08:17:14 +0200320 rc = tpm1_chip_register(chip);
321 if (rc)
322 return rc;
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800323
Jarkko Sakkinen9b774d52015-04-14 17:56:48 +0300324 tpm_add_ppi(chip);
325
Jarkko Sakkinen72c91ce2016-01-29 09:47:22 -0800326 rc = tpm_add_char_device(chip);
Jarkko Sakkinend972b052015-03-01 23:55:47 +0200327 if (rc)
Jarkko Sakkinen34d47b62015-03-18 08:17:14 +0200328 goto out_err;
Jarkko Sakkinend972b052015-03-01 23:55:47 +0200329
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800330 chip->flags |= TPM_CHIP_FLAG_REGISTERED;
331
Jarkko Sakkinend56e4f72015-11-07 13:33:25 +0200332 if (!(chip->flags & TPM_CHIP_FLAG_TPM2)) {
Jason Gunthorpe8cfffc92016-02-29 12:29:47 -0500333 rc = __compat_only_sysfs_link_entry_to_kobj(
334 &chip->dev.parent->kobj, &chip->dev.kobj, "ppi");
Jarkko Sakkinend56e4f72015-11-07 13:33:25 +0200335 if (rc && rc != -ENOENT) {
336 tpm_chip_unregister(chip);
337 return rc;
338 }
339 }
340
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800341 return 0;
Jarkko Sakkinen34d47b62015-03-18 08:17:14 +0200342out_err:
343 tpm1_chip_unregister(chip);
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800344 return rc;
345}
346EXPORT_SYMBOL_GPL(tpm_chip_register);
347
348/*
349 * tpm_chip_unregister() - release the TPM driver
350 * @chip: TPM chip to use.
351 *
352 * Takes the chip first away from the list of available TPM chips and then
353 * cleans up all the resources reserved by tpm_chip_register().
354 *
Jason Gunthorpe4e261952016-02-12 20:29:53 -0700355 * Once this function returns the driver call backs in 'op's will not be
356 * running and will no longer start.
357 *
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800358 * NOTE: This function should be only called before deinitializing chip
359 * resources.
360 */
361void tpm_chip_unregister(struct tpm_chip *chip)
362{
363 if (!(chip->flags & TPM_CHIP_FLAG_REGISTERED))
364 return;
365
Jarkko Sakkinen9b774d52015-04-14 17:56:48 +0300366 if (!(chip->flags & TPM_CHIP_FLAG_TPM2))
Jason Gunthorpe8cfffc92016-02-29 12:29:47 -0500367 sysfs_remove_link(&chip->dev.parent->kobj, "ppi");
Jarkko Sakkinen9b774d52015-04-14 17:56:48 +0300368
Jarkko Sakkinen34d47b62015-03-18 08:17:14 +0200369 tpm1_chip_unregister(chip);
Jarkko Sakkinen72c91ce2016-01-29 09:47:22 -0800370 tpm_del_char_device(chip);
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800371}
372EXPORT_SYMBOL_GPL(tpm_chip_unregister);