tpm: use struct tpm_chip for tpm_chip_find_get()
[linux/fpc-iii.git] / drivers / char / tpm / tpm-chip.c
blobbab9c14e040ccef92701878af0aa7143e23d4ed8
1 /*
2 * Copyright (C) 2004 IBM Corporation
3 * Copyright (C) 2014 Intel Corporation
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>
12 * Maintained by: <tpmdd-devel@lists.sourceforge.net>
14 * TPM chip management routines.
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.
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>
28 #include <linux/major.h>
29 #include <linux/tpm_eventlog.h>
31 #include "tpm.h"
33 DEFINE_IDR(dev_nums_idr);
34 static DEFINE_MUTEX(idr_lock);
36 struct class *tpm_class;
37 struct class *tpmrm_class;
38 dev_t tpm_devt;
40 /**
41 * tpm_try_get_ops() - Get a ref to the tpm_chip
42 * @chip: Chip to ref
44 * The caller must already have some kind of locking to ensure that chip is
45 * valid. This function will lock the chip so that the ops member can be
46 * accessed safely. The locking prevents tpm_chip_unregister from
47 * completing, so it should not be held for long periods.
49 * Returns -ERRNO if the chip could not be got.
51 int tpm_try_get_ops(struct tpm_chip *chip)
53 int rc = -EIO;
55 get_device(&chip->dev);
57 down_read(&chip->ops_sem);
58 if (!chip->ops)
59 goto out_lock;
61 return 0;
62 out_lock:
63 up_read(&chip->ops_sem);
64 put_device(&chip->dev);
65 return rc;
67 EXPORT_SYMBOL_GPL(tpm_try_get_ops);
69 /**
70 * tpm_put_ops() - Release a ref to the tpm_chip
71 * @chip: Chip to put
73 * This is the opposite pair to tpm_try_get_ops(). After this returns chip may
74 * be kfree'd.
76 void tpm_put_ops(struct tpm_chip *chip)
78 up_read(&chip->ops_sem);
79 put_device(&chip->dev);
81 EXPORT_SYMBOL_GPL(tpm_put_ops);
83 /**
84 * tpm_chip_find_get() - find and reserve a TPM chip
85 * @chip: a &struct tpm_chip instance, %NULL for the default chip
87 * Finds a TPM chip and reserves its class device and operations. The chip must
88 * be released with tpm_chip_put_ops() after use.
90 * Return:
91 * A reserved &struct tpm_chip instance.
92 * %NULL if a chip is not found.
93 * %NULL if the chip is not available.
95 struct tpm_chip *tpm_chip_find_get(struct tpm_chip *chip)
97 struct tpm_chip *res = NULL;
98 int chip_num = 0;
99 int chip_prev;
101 mutex_lock(&idr_lock);
103 if (!chip) {
104 do {
105 chip_prev = chip_num;
106 chip = idr_get_next(&dev_nums_idr, &chip_num);
107 if (chip && !tpm_try_get_ops(chip)) {
108 res = chip;
109 break;
111 } while (chip_prev != chip_num);
112 } else {
113 if (!tpm_try_get_ops(chip))
114 res = chip;
117 mutex_unlock(&idr_lock);
119 return res;
123 * tpm_dev_release() - free chip memory and the device number
124 * @dev: the character device for the TPM chip
126 * This is used as the release function for the character device.
128 static void tpm_dev_release(struct device *dev)
130 struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev);
132 mutex_lock(&idr_lock);
133 idr_remove(&dev_nums_idr, chip->dev_num);
134 mutex_unlock(&idr_lock);
136 kfree(chip->log.bios_event_log);
137 kfree(chip->work_space.context_buf);
138 kfree(chip->work_space.session_buf);
139 kfree(chip);
142 static void tpm_devs_release(struct device *dev)
144 struct tpm_chip *chip = container_of(dev, struct tpm_chip, devs);
146 /* release the master device reference */
147 put_device(&chip->dev);
151 * tpm_class_shutdown() - prepare the TPM device for loss of power.
152 * @dev: device to which the chip is associated.
154 * Issues a TPM2_Shutdown command prior to loss of power, as required by the
155 * TPM 2.0 spec.
156 * Then, calls bus- and device- specific shutdown code.
158 * XXX: This codepath relies on the fact that sysfs is not enabled for
159 * TPM2: sysfs uses an implicit lock on chip->ops, so this could race if TPM2
160 * has sysfs support enabled before TPM sysfs's implicit locking is fixed.
162 static int tpm_class_shutdown(struct device *dev)
164 struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev);
166 if (chip->flags & TPM_CHIP_FLAG_TPM2) {
167 down_write(&chip->ops_sem);
168 tpm2_shutdown(chip, TPM2_SU_CLEAR);
169 chip->ops = NULL;
170 up_write(&chip->ops_sem);
173 return 0;
177 * tpm_chip_alloc() - allocate a new struct tpm_chip instance
178 * @pdev: device to which the chip is associated
179 * At this point pdev mst be initialized, but does not have to
180 * be registered
181 * @ops: struct tpm_class_ops instance
183 * Allocates a new struct tpm_chip instance and assigns a free
184 * device number for it. Must be paired with put_device(&chip->dev).
186 struct tpm_chip *tpm_chip_alloc(struct device *pdev,
187 const struct tpm_class_ops *ops)
189 struct tpm_chip *chip;
190 int rc;
192 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
193 if (chip == NULL)
194 return ERR_PTR(-ENOMEM);
196 mutex_init(&chip->tpm_mutex);
197 init_rwsem(&chip->ops_sem);
199 chip->ops = ops;
201 mutex_lock(&idr_lock);
202 rc = idr_alloc(&dev_nums_idr, NULL, 0, TPM_NUM_DEVICES, GFP_KERNEL);
203 mutex_unlock(&idr_lock);
204 if (rc < 0) {
205 dev_err(pdev, "No available tpm device numbers\n");
206 kfree(chip);
207 return ERR_PTR(rc);
209 chip->dev_num = rc;
211 device_initialize(&chip->dev);
212 device_initialize(&chip->devs);
214 chip->dev.class = tpm_class;
215 chip->dev.class->shutdown_pre = tpm_class_shutdown;
216 chip->dev.release = tpm_dev_release;
217 chip->dev.parent = pdev;
218 chip->dev.groups = chip->groups;
220 chip->devs.parent = pdev;
221 chip->devs.class = tpmrm_class;
222 chip->devs.release = tpm_devs_release;
223 /* get extra reference on main device to hold on
224 * behalf of devs. This holds the chip structure
225 * while cdevs is in use. The corresponding put
226 * is in the tpm_devs_release (TPM2 only)
228 if (chip->flags & TPM_CHIP_FLAG_TPM2)
229 get_device(&chip->dev);
231 if (chip->dev_num == 0)
232 chip->dev.devt = MKDEV(MISC_MAJOR, TPM_MINOR);
233 else
234 chip->dev.devt = MKDEV(MAJOR(tpm_devt), chip->dev_num);
236 chip->devs.devt =
237 MKDEV(MAJOR(tpm_devt), chip->dev_num + TPM_NUM_DEVICES);
239 rc = dev_set_name(&chip->dev, "tpm%d", chip->dev_num);
240 if (rc)
241 goto out;
242 rc = dev_set_name(&chip->devs, "tpmrm%d", chip->dev_num);
243 if (rc)
244 goto out;
246 if (!pdev)
247 chip->flags |= TPM_CHIP_FLAG_VIRTUAL;
249 cdev_init(&chip->cdev, &tpm_fops);
250 cdev_init(&chip->cdevs, &tpmrm_fops);
251 chip->cdev.owner = THIS_MODULE;
252 chip->cdevs.owner = THIS_MODULE;
254 chip->work_space.context_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
255 if (!chip->work_space.context_buf) {
256 rc = -ENOMEM;
257 goto out;
259 chip->work_space.session_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
260 if (!chip->work_space.session_buf) {
261 rc = -ENOMEM;
262 goto out;
265 chip->locality = -1;
266 return chip;
268 out:
269 put_device(&chip->devs);
270 put_device(&chip->dev);
271 return ERR_PTR(rc);
273 EXPORT_SYMBOL_GPL(tpm_chip_alloc);
276 * tpmm_chip_alloc() - allocate a new struct tpm_chip instance
277 * @pdev: parent device to which the chip is associated
278 * @ops: struct tpm_class_ops instance
280 * Same as tpm_chip_alloc except devm is used to do the put_device
282 struct tpm_chip *tpmm_chip_alloc(struct device *pdev,
283 const struct tpm_class_ops *ops)
285 struct tpm_chip *chip;
286 int rc;
288 chip = tpm_chip_alloc(pdev, ops);
289 if (IS_ERR(chip))
290 return chip;
292 rc = devm_add_action_or_reset(pdev,
293 (void (*)(void *)) put_device,
294 &chip->dev);
295 if (rc)
296 return ERR_PTR(rc);
298 dev_set_drvdata(pdev, chip);
300 return chip;
302 EXPORT_SYMBOL_GPL(tpmm_chip_alloc);
304 static int tpm_add_char_device(struct tpm_chip *chip)
306 int rc;
308 rc = cdev_device_add(&chip->cdev, &chip->dev);
309 if (rc) {
310 dev_err(&chip->dev,
311 "unable to cdev_device_add() %s, major %d, minor %d, err=%d\n",
312 dev_name(&chip->dev), MAJOR(chip->dev.devt),
313 MINOR(chip->dev.devt), rc);
314 return rc;
317 if (chip->flags & TPM_CHIP_FLAG_TPM2) {
318 rc = cdev_device_add(&chip->cdevs, &chip->devs);
319 if (rc) {
320 dev_err(&chip->devs,
321 "unable to cdev_device_add() %s, major %d, minor %d, err=%d\n",
322 dev_name(&chip->devs), MAJOR(chip->devs.devt),
323 MINOR(chip->devs.devt), rc);
324 return rc;
328 /* Make the chip available. */
329 mutex_lock(&idr_lock);
330 idr_replace(&dev_nums_idr, chip, chip->dev_num);
331 mutex_unlock(&idr_lock);
333 return rc;
336 static void tpm_del_char_device(struct tpm_chip *chip)
338 cdev_device_del(&chip->cdev, &chip->dev);
340 /* Make the chip unavailable. */
341 mutex_lock(&idr_lock);
342 idr_replace(&dev_nums_idr, NULL, chip->dev_num);
343 mutex_unlock(&idr_lock);
345 /* Make the driver uncallable. */
346 down_write(&chip->ops_sem);
347 if (chip->flags & TPM_CHIP_FLAG_TPM2)
348 tpm2_shutdown(chip, TPM2_SU_CLEAR);
349 chip->ops = NULL;
350 up_write(&chip->ops_sem);
353 static void tpm_del_legacy_sysfs(struct tpm_chip *chip)
355 struct attribute **i;
357 if (chip->flags & (TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_VIRTUAL))
358 return;
360 sysfs_remove_link(&chip->dev.parent->kobj, "ppi");
362 for (i = chip->groups[0]->attrs; *i != NULL; ++i)
363 sysfs_remove_link(&chip->dev.parent->kobj, (*i)->name);
366 /* For compatibility with legacy sysfs paths we provide symlinks from the
367 * parent dev directory to selected names within the tpm chip directory. Old
368 * kernel versions created these files directly under the parent.
370 static int tpm_add_legacy_sysfs(struct tpm_chip *chip)
372 struct attribute **i;
373 int rc;
375 if (chip->flags & (TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_VIRTUAL))
376 return 0;
378 rc = __compat_only_sysfs_link_entry_to_kobj(
379 &chip->dev.parent->kobj, &chip->dev.kobj, "ppi");
380 if (rc && rc != -ENOENT)
381 return rc;
383 /* All the names from tpm-sysfs */
384 for (i = chip->groups[0]->attrs; *i != NULL; ++i) {
385 rc = __compat_only_sysfs_link_entry_to_kobj(
386 &chip->dev.parent->kobj, &chip->dev.kobj, (*i)->name);
387 if (rc) {
388 tpm_del_legacy_sysfs(chip);
389 return rc;
393 return 0;
396 * tpm_chip_register() - create a character device for the TPM chip
397 * @chip: TPM chip to use.
399 * Creates a character device for the TPM chip and adds sysfs attributes for
400 * the device. As the last step this function adds the chip to the list of TPM
401 * chips available for in-kernel use.
403 * This function should be only called after the chip initialization is
404 * complete.
406 int tpm_chip_register(struct tpm_chip *chip)
408 int rc;
410 if (chip->ops->flags & TPM_OPS_AUTO_STARTUP) {
411 if (chip->flags & TPM_CHIP_FLAG_TPM2)
412 rc = tpm2_auto_startup(chip);
413 else
414 rc = tpm1_auto_startup(chip);
415 if (rc)
416 return rc;
419 tpm_sysfs_add_device(chip);
421 rc = tpm_bios_log_setup(chip);
422 if (rc != 0 && rc != -ENODEV)
423 return rc;
425 tpm_add_ppi(chip);
427 rc = tpm_add_char_device(chip);
428 if (rc) {
429 tpm_bios_log_teardown(chip);
430 return rc;
433 rc = tpm_add_legacy_sysfs(chip);
434 if (rc) {
435 tpm_chip_unregister(chip);
436 return rc;
439 return 0;
441 EXPORT_SYMBOL_GPL(tpm_chip_register);
444 * tpm_chip_unregister() - release the TPM driver
445 * @chip: TPM chip to use.
447 * Takes the chip first away from the list of available TPM chips and then
448 * cleans up all the resources reserved by tpm_chip_register().
450 * Once this function returns the driver call backs in 'op's will not be
451 * running and will no longer start.
453 * NOTE: This function should be only called before deinitializing chip
454 * resources.
456 void tpm_chip_unregister(struct tpm_chip *chip)
458 tpm_del_legacy_sysfs(chip);
459 tpm_bios_log_teardown(chip);
460 if (chip->flags & TPM_CHIP_FLAG_TPM2)
461 cdev_device_del(&chip->cdevs, &chip->devs);
462 tpm_del_char_device(chip);
464 EXPORT_SYMBOL_GPL(tpm_chip_unregister);