blob: 66367286deea703e3b765b84db7a23de5b17ef8b [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
32static DECLARE_BITMAP(dev_mask, TPM_NUM_DEVICES);
33static LIST_HEAD(tpm_chip_list);
34static DEFINE_SPINLOCK(driver_lock);
35
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -080036struct class *tpm_class;
37dev_t tpm_devt;
38
Jason Gunthorpe4e261952016-02-12 20:29:53 -070039/**
40 * tpm_try_get_ops() - Get a ref to the tpm_chip
41 * @chip: Chip to ref
42 *
43 * The caller must already have some kind of locking to ensure that chip is
44 * valid. This function will lock the chip so that the ops member can be
45 * accessed safely. The locking prevents tpm_chip_unregister from
46 * completing, so it should not be held for long periods.
47 *
48 * Returns -ERRNO if the chip could not be got.
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -080049 */
Jason Gunthorpe4e261952016-02-12 20:29:53 -070050int tpm_try_get_ops(struct tpm_chip *chip)
51{
52 int rc = -EIO;
53
54 get_device(&chip->dev);
55
56 down_read(&chip->ops_sem);
57 if (!chip->ops)
58 goto out_lock;
59
Jason Gunthorpe4e261952016-02-12 20:29:53 -070060 return 0;
61out_lock:
62 up_read(&chip->ops_sem);
63 put_device(&chip->dev);
64 return rc;
65}
66EXPORT_SYMBOL_GPL(tpm_try_get_ops);
67
68/**
69 * tpm_put_ops() - Release a ref to the tpm_chip
70 * @chip: Chip to put
71 *
72 * This is the opposite pair to tpm_try_get_ops(). After this returns chip may
73 * be kfree'd.
74 */
75void tpm_put_ops(struct tpm_chip *chip)
76{
Jason Gunthorpe4e261952016-02-12 20:29:53 -070077 up_read(&chip->ops_sem);
78 put_device(&chip->dev);
79}
80EXPORT_SYMBOL_GPL(tpm_put_ops);
81
82/**
83 * tpm_chip_find_get() - return tpm_chip for a given chip number
84 * @chip_num: id to find
85 *
86 * The return'd chip has been tpm_try_get_ops'd and must be released via
87 * tpm_put_ops
88 */
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -080089struct tpm_chip *tpm_chip_find_get(int chip_num)
90{
91 struct tpm_chip *pos, *chip = NULL;
92
93 rcu_read_lock();
94 list_for_each_entry_rcu(pos, &tpm_chip_list, list) {
95 if (chip_num != TPM_ANY_NUM && chip_num != pos->dev_num)
96 continue;
97
Jason Gunthorpe4e261952016-02-12 20:29:53 -070098 /* rcu prevents chip from being free'd */
99 if (!tpm_try_get_ops(pos))
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800100 chip = pos;
Jason Gunthorpe4e261952016-02-12 20:29:53 -0700101 break;
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800102 }
103 rcu_read_unlock();
104 return chip;
105}
106
107/**
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -0800108 * tpm_dev_release() - free chip memory and the device number
109 * @dev: the character device for the TPM chip
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800110 *
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -0800111 * This is used as the release function for the character device.
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800112 */
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -0800113static void tpm_dev_release(struct device *dev)
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800114{
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -0800115 struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev);
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800116
117 spin_lock(&driver_lock);
118 clear_bit(chip->dev_num, dev_mask);
119 spin_unlock(&driver_lock);
120 kfree(chip);
121}
122
123/**
124 * tpmm_chip_alloc() - allocate a new struct tpm_chip instance
125 * @dev: device to which the chip is associated
126 * @ops: struct tpm_class_ops instance
127 *
128 * Allocates a new struct tpm_chip instance and assigns a free
129 * device number for it. Caller does not have to worry about
130 * freeing the allocated resources. When the devices is removed
131 * devres calls tpmm_chip_remove() to do the job.
132 */
133struct tpm_chip *tpmm_chip_alloc(struct device *dev,
134 const struct tpm_class_ops *ops)
135{
136 struct tpm_chip *chip;
Jarkko Sakkinen4f3b1932016-02-13 11:58:16 +0200137 int rc;
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800138
139 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
140 if (chip == NULL)
141 return ERR_PTR(-ENOMEM);
142
143 mutex_init(&chip->tpm_mutex);
Jason Gunthorpe4e261952016-02-12 20:29:53 -0700144 init_rwsem(&chip->ops_sem);
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800145 INIT_LIST_HEAD(&chip->list);
146
147 chip->ops = ops;
148
149 spin_lock(&driver_lock);
150 chip->dev_num = find_first_zero_bit(dev_mask, TPM_NUM_DEVICES);
151 spin_unlock(&driver_lock);
152
153 if (chip->dev_num >= TPM_NUM_DEVICES) {
154 dev_err(dev, "No available tpm device numbers\n");
155 kfree(chip);
156 return ERR_PTR(-ENOMEM);
157 }
158
159 set_bit(chip->dev_num, dev_mask);
160
Jason Gunthorpe3635e2e2016-02-29 12:29:48 -0500161 device_initialize(&chip->dev);
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800162
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800163 dev_set_drvdata(dev, chip);
164
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -0800165 chip->dev.class = tpm_class;
166 chip->dev.release = tpm_dev_release;
Jason Gunthorpe8cfffc92016-02-29 12:29:47 -0500167 chip->dev.parent = dev;
Jarkko Sakkinen9b774d52015-04-14 17:56:48 +0300168#ifdef CONFIG_ACPI
169 chip->dev.groups = chip->groups;
170#endif
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -0800171
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
Jason Gunthorpe3635e2e2016-02-29 12:29:48 -0500177 rc = dev_set_name(&chip->dev, "tpm%d", chip->dev_num);
178 if (rc)
179 goto out;
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -0800180
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -0800181 cdev_init(&chip->cdev, &tpm_fops);
Stefan Berger2072df42016-02-29 08:53:01 -0500182 chip->cdev.owner = THIS_MODULE;
Jason Gunthorpeba0ef852015-06-30 13:15:31 -0600183 chip->cdev.kobj.parent = &chip->dev.kobj;
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -0800184
Jarkko Sakkinen4f3b1932016-02-13 11:58:16 +0200185 rc = devm_add_action(dev, (void (*)(void *)) put_device, &chip->dev);
186 if (rc) {
187 put_device(&chip->dev);
188 return ERR_PTR(rc);
189 }
Jarkko Sakkinen8e0ee3c2016-02-08 22:31:08 +0200190
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800191 return chip;
Jason Gunthorpe3635e2e2016-02-29 12:29:48 -0500192
193out:
194 put_device(&chip->dev);
195 return ERR_PTR(rc);
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800196}
197EXPORT_SYMBOL_GPL(tpmm_chip_alloc);
198
Jarkko Sakkinen72c91ce2016-01-29 09:47:22 -0800199static int tpm_add_char_device(struct tpm_chip *chip)
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -0800200{
201 int rc;
202
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -0800203 rc = cdev_add(&chip->cdev, chip->dev.devt, 1);
204 if (rc) {
205 dev_err(&chip->dev,
206 "unable to cdev_add() %s, major %d, minor %d, err=%d\n",
Jason Gunthorpe3635e2e2016-02-29 12:29:48 -0500207 dev_name(&chip->dev), MAJOR(chip->dev.devt),
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -0800208 MINOR(chip->dev.devt), rc);
209
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -0800210 return rc;
211 }
212
Jarkko Sakkinend972b052015-03-01 23:55:47 +0200213 rc = device_add(&chip->dev);
214 if (rc) {
215 dev_err(&chip->dev,
216 "unable to device_register() %s, major %d, minor %d, err=%d\n",
Jason Gunthorpe3635e2e2016-02-29 12:29:48 -0500217 dev_name(&chip->dev), MAJOR(chip->dev.devt),
Jarkko Sakkinend972b052015-03-01 23:55:47 +0200218 MINOR(chip->dev.devt), rc);
219
Jarkko Sakkinen72c91ce2016-01-29 09:47:22 -0800220 cdev_del(&chip->cdev);
Jarkko Sakkinend972b052015-03-01 23:55:47 +0200221 return rc;
222 }
223
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -0800224 return rc;
225}
226
Jarkko Sakkinen72c91ce2016-01-29 09:47:22 -0800227static void tpm_del_char_device(struct tpm_chip *chip)
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -0800228{
229 cdev_del(&chip->cdev);
Jason Gunthorpe4e261952016-02-12 20:29:53 -0700230
231 /* Make the driver uncallable. */
232 down_write(&chip->ops_sem);
233 chip->ops = NULL;
234 up_write(&chip->ops_sem);
235
Jarkko Sakkinen8e0ee3c2016-02-08 22:31:08 +0200236 device_del(&chip->dev);
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -0800237}
238
Jarkko Sakkinen34d47b62015-03-18 08:17:14 +0200239static int tpm1_chip_register(struct tpm_chip *chip)
240{
241 int rc;
242
243 if (chip->flags & TPM_CHIP_FLAG_TPM2)
244 return 0;
245
246 rc = tpm_sysfs_add_device(chip);
247 if (rc)
248 return rc;
249
Jason Gunthorpe3635e2e2016-02-29 12:29:48 -0500250 chip->bios_dir = tpm_bios_log_setup(dev_name(&chip->dev));
Jarkko Sakkinen34d47b62015-03-18 08:17:14 +0200251
252 return 0;
253}
254
255static void tpm1_chip_unregister(struct tpm_chip *chip)
256{
257 if (chip->flags & TPM_CHIP_FLAG_TPM2)
258 return;
259
260 if (chip->bios_dir)
261 tpm_bios_log_teardown(chip->bios_dir);
262
Jarkko Sakkinen34d47b62015-03-18 08:17:14 +0200263 tpm_sysfs_del_device(chip);
264}
265
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800266/*
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -0800267 * tpm_chip_register() - create a character device for the TPM chip
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800268 * @chip: TPM chip to use.
269 *
Jarkko Sakkinend972b052015-03-01 23:55:47 +0200270 * Creates a character device for the TPM chip and adds sysfs attributes for
271 * the device. As the last step this function adds the chip to the list of TPM
272 * chips available for in-kernel use.
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800273 *
Jarkko Sakkinend972b052015-03-01 23:55:47 +0200274 * This function should be only called after the chip initialization is
275 * complete.
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800276 */
277int tpm_chip_register(struct tpm_chip *chip)
278{
279 int rc;
280
Jarkko Sakkinen34d47b62015-03-18 08:17:14 +0200281 rc = tpm1_chip_register(chip);
282 if (rc)
283 return rc;
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800284
Jarkko Sakkinen9b774d52015-04-14 17:56:48 +0300285 tpm_add_ppi(chip);
286
Jarkko Sakkinen72c91ce2016-01-29 09:47:22 -0800287 rc = tpm_add_char_device(chip);
Jarkko Sakkinend972b052015-03-01 23:55:47 +0200288 if (rc)
Jarkko Sakkinen34d47b62015-03-18 08:17:14 +0200289 goto out_err;
Jarkko Sakkinend972b052015-03-01 23:55:47 +0200290
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800291 /* Make the chip available. */
292 spin_lock(&driver_lock);
Jarkko Sakkinenb1a41442015-11-02 19:55:29 +0200293 list_add_tail_rcu(&chip->list, &tpm_chip_list);
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800294 spin_unlock(&driver_lock);
295
296 chip->flags |= TPM_CHIP_FLAG_REGISTERED;
297
Jarkko Sakkinend56e4f72015-11-07 13:33:25 +0200298 if (!(chip->flags & TPM_CHIP_FLAG_TPM2)) {
Jason Gunthorpe8cfffc92016-02-29 12:29:47 -0500299 rc = __compat_only_sysfs_link_entry_to_kobj(
300 &chip->dev.parent->kobj, &chip->dev.kobj, "ppi");
Jarkko Sakkinend56e4f72015-11-07 13:33:25 +0200301 if (rc && rc != -ENOENT) {
302 tpm_chip_unregister(chip);
303 return rc;
304 }
305 }
306
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800307 return 0;
Jarkko Sakkinen34d47b62015-03-18 08:17:14 +0200308out_err:
309 tpm1_chip_unregister(chip);
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800310 return rc;
311}
312EXPORT_SYMBOL_GPL(tpm_chip_register);
313
314/*
315 * tpm_chip_unregister() - release the TPM driver
316 * @chip: TPM chip to use.
317 *
318 * Takes the chip first away from the list of available TPM chips and then
319 * cleans up all the resources reserved by tpm_chip_register().
320 *
Jason Gunthorpe4e261952016-02-12 20:29:53 -0700321 * Once this function returns the driver call backs in 'op's will not be
322 * running and will no longer start.
323 *
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800324 * NOTE: This function should be only called before deinitializing chip
325 * resources.
326 */
327void tpm_chip_unregister(struct tpm_chip *chip)
328{
329 if (!(chip->flags & TPM_CHIP_FLAG_REGISTERED))
330 return;
331
332 spin_lock(&driver_lock);
333 list_del_rcu(&chip->list);
334 spin_unlock(&driver_lock);
335 synchronize_rcu();
336
Jarkko Sakkinen9b774d52015-04-14 17:56:48 +0300337 if (!(chip->flags & TPM_CHIP_FLAG_TPM2))
Jason Gunthorpe8cfffc92016-02-29 12:29:47 -0500338 sysfs_remove_link(&chip->dev.parent->kobj, "ppi");
Jarkko Sakkinen9b774d52015-04-14 17:56:48 +0300339
Jarkko Sakkinen34d47b62015-03-18 08:17:14 +0200340 tpm1_chip_unregister(chip);
Jarkko Sakkinen72c91ce2016-01-29 09:47:22 -0800341 tpm_del_char_device(chip);
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800342}
343EXPORT_SYMBOL_GPL(tpm_chip_unregister);