blob: 45cc39aabeeeb8fde7967971516a4efad50d2caa [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
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -080039/*
40 * tpm_chip_find_get - return tpm_chip for a given chip number
41 * @chip_num the device number for the chip
42 */
43struct tpm_chip *tpm_chip_find_get(int chip_num)
44{
45 struct tpm_chip *pos, *chip = NULL;
46
47 rcu_read_lock();
48 list_for_each_entry_rcu(pos, &tpm_chip_list, list) {
49 if (chip_num != TPM_ANY_NUM && chip_num != pos->dev_num)
50 continue;
51
Jarkko Sakkinen71ed8482014-12-12 11:46:36 -080052 if (try_module_get(pos->pdev->driver->owner)) {
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -080053 chip = pos;
54 break;
55 }
56 }
57 rcu_read_unlock();
58 return chip;
59}
60
61/**
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -080062 * tpm_dev_release() - free chip memory and the device number
63 * @dev: the character device for the TPM chip
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -080064 *
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -080065 * This is used as the release function for the character device.
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -080066 */
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -080067static void tpm_dev_release(struct device *dev)
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -080068{
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -080069 struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev);
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -080070
71 spin_lock(&driver_lock);
72 clear_bit(chip->dev_num, dev_mask);
73 spin_unlock(&driver_lock);
74 kfree(chip);
75}
76
77/**
78 * tpmm_chip_alloc() - allocate a new struct tpm_chip instance
79 * @dev: device to which the chip is associated
80 * @ops: struct tpm_class_ops instance
81 *
82 * Allocates a new struct tpm_chip instance and assigns a free
83 * device number for it. Caller does not have to worry about
84 * freeing the allocated resources. When the devices is removed
85 * devres calls tpmm_chip_remove() to do the job.
86 */
87struct tpm_chip *tpmm_chip_alloc(struct device *dev,
88 const struct tpm_class_ops *ops)
89{
90 struct tpm_chip *chip;
91
92 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
93 if (chip == NULL)
94 return ERR_PTR(-ENOMEM);
95
96 mutex_init(&chip->tpm_mutex);
97 INIT_LIST_HEAD(&chip->list);
98
99 chip->ops = ops;
100
101 spin_lock(&driver_lock);
102 chip->dev_num = find_first_zero_bit(dev_mask, TPM_NUM_DEVICES);
103 spin_unlock(&driver_lock);
104
105 if (chip->dev_num >= TPM_NUM_DEVICES) {
106 dev_err(dev, "No available tpm device numbers\n");
107 kfree(chip);
108 return ERR_PTR(-ENOMEM);
109 }
110
111 set_bit(chip->dev_num, dev_mask);
112
113 scnprintf(chip->devname, sizeof(chip->devname), "tpm%d", chip->dev_num);
114
Jarkko Sakkinen71ed8482014-12-12 11:46:36 -0800115 chip->pdev = dev;
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -0800116
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800117 dev_set_drvdata(dev, chip);
118
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -0800119 chip->dev.class = tpm_class;
120 chip->dev.release = tpm_dev_release;
121 chip->dev.parent = chip->pdev;
Jarkko Sakkinen9b774d52015-04-14 17:56:48 +0300122#ifdef CONFIG_ACPI
123 chip->dev.groups = chip->groups;
124#endif
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -0800125
126 if (chip->dev_num == 0)
127 chip->dev.devt = MKDEV(MISC_MAJOR, TPM_MINOR);
128 else
129 chip->dev.devt = MKDEV(MAJOR(tpm_devt), chip->dev_num);
130
Jarkko Sakkinen743410a2015-01-20 12:03:35 +0200131 dev_set_name(&chip->dev, "%s", chip->devname);
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -0800132
133 device_initialize(&chip->dev);
134
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -0800135 cdev_init(&chip->cdev, &tpm_fops);
Jason Gunthorpeba0ef852015-06-30 13:15:31 -0600136 chip->cdev.owner = chip->pdev->driver->owner;
137 chip->cdev.kobj.parent = &chip->dev.kobj;
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -0800138
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800139 return chip;
140}
141EXPORT_SYMBOL_GPL(tpmm_chip_alloc);
142
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -0800143static int tpm_dev_add_device(struct tpm_chip *chip)
144{
145 int rc;
146
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -0800147 rc = cdev_add(&chip->cdev, chip->dev.devt, 1);
148 if (rc) {
149 dev_err(&chip->dev,
150 "unable to cdev_add() %s, major %d, minor %d, err=%d\n",
151 chip->devname, MAJOR(chip->dev.devt),
152 MINOR(chip->dev.devt), rc);
153
154 device_unregister(&chip->dev);
155 return rc;
156 }
157
Jarkko Sakkinend972b052015-03-01 23:55:47 +0200158 rc = device_add(&chip->dev);
159 if (rc) {
160 dev_err(&chip->dev,
161 "unable to device_register() %s, major %d, minor %d, err=%d\n",
162 chip->devname, MAJOR(chip->dev.devt),
163 MINOR(chip->dev.devt), rc);
164
165 return rc;
166 }
167
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -0800168 return rc;
169}
170
171static void tpm_dev_del_device(struct tpm_chip *chip)
172{
173 cdev_del(&chip->cdev);
174 device_unregister(&chip->dev);
175}
176
Jarkko Sakkinen34d47b62015-03-18 08:17:14 +0200177static int tpm1_chip_register(struct tpm_chip *chip)
178{
179 int rc;
180
181 if (chip->flags & TPM_CHIP_FLAG_TPM2)
182 return 0;
183
184 rc = tpm_sysfs_add_device(chip);
185 if (rc)
186 return rc;
187
Jarkko Sakkinen34d47b62015-03-18 08:17:14 +0200188 chip->bios_dir = tpm_bios_log_setup(chip->devname);
189
190 return 0;
191}
192
193static void tpm1_chip_unregister(struct tpm_chip *chip)
194{
195 if (chip->flags & TPM_CHIP_FLAG_TPM2)
196 return;
197
198 if (chip->bios_dir)
199 tpm_bios_log_teardown(chip->bios_dir);
200
Jarkko Sakkinen34d47b62015-03-18 08:17:14 +0200201 tpm_sysfs_del_device(chip);
202}
203
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800204/*
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -0800205 * tpm_chip_register() - create a character device for the TPM chip
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800206 * @chip: TPM chip to use.
207 *
Jarkko Sakkinend972b052015-03-01 23:55:47 +0200208 * Creates a character device for the TPM chip and adds sysfs attributes for
209 * the device. As the last step this function adds the chip to the list of TPM
210 * chips available for in-kernel use.
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800211 *
Jarkko Sakkinend972b052015-03-01 23:55:47 +0200212 * This function should be only called after the chip initialization is
213 * complete.
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800214 */
215int tpm_chip_register(struct tpm_chip *chip)
216{
217 int rc;
218
Jarkko Sakkinen34d47b62015-03-18 08:17:14 +0200219 rc = tpm1_chip_register(chip);
220 if (rc)
221 return rc;
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800222
Jarkko Sakkinen9b774d52015-04-14 17:56:48 +0300223 tpm_add_ppi(chip);
224
Jarkko Sakkinend972b052015-03-01 23:55:47 +0200225 rc = tpm_dev_add_device(chip);
226 if (rc)
Jarkko Sakkinen34d47b62015-03-18 08:17:14 +0200227 goto out_err;
Jarkko Sakkinend972b052015-03-01 23:55:47 +0200228
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800229 /* Make the chip available. */
230 spin_lock(&driver_lock);
Jarkko Sakkinenb1a41442015-11-02 19:55:29 +0200231 list_add_tail_rcu(&chip->list, &tpm_chip_list);
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800232 spin_unlock(&driver_lock);
233
234 chip->flags |= TPM_CHIP_FLAG_REGISTERED;
235
Jarkko Sakkinend56e4f72015-11-07 13:33:25 +0200236 if (!(chip->flags & TPM_CHIP_FLAG_TPM2)) {
237 rc = __compat_only_sysfs_link_entry_to_kobj(&chip->pdev->kobj,
238 &chip->dev.kobj,
239 "ppi");
240 if (rc && rc != -ENOENT) {
241 tpm_chip_unregister(chip);
242 return rc;
243 }
244 }
245
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800246 return 0;
Jarkko Sakkinen34d47b62015-03-18 08:17:14 +0200247out_err:
248 tpm1_chip_unregister(chip);
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800249 return rc;
250}
251EXPORT_SYMBOL_GPL(tpm_chip_register);
252
253/*
254 * tpm_chip_unregister() - release the TPM driver
255 * @chip: TPM chip to use.
256 *
257 * Takes the chip first away from the list of available TPM chips and then
258 * cleans up all the resources reserved by tpm_chip_register().
259 *
260 * NOTE: This function should be only called before deinitializing chip
261 * resources.
262 */
263void tpm_chip_unregister(struct tpm_chip *chip)
264{
265 if (!(chip->flags & TPM_CHIP_FLAG_REGISTERED))
266 return;
267
268 spin_lock(&driver_lock);
269 list_del_rcu(&chip->list);
270 spin_unlock(&driver_lock);
271 synchronize_rcu();
272
Jarkko Sakkinen9b774d52015-04-14 17:56:48 +0300273 if (!(chip->flags & TPM_CHIP_FLAG_TPM2))
274 sysfs_remove_link(&chip->pdev->kobj, "ppi");
275
Jarkko Sakkinen34d47b62015-03-18 08:17:14 +0200276 tpm1_chip_unregister(chip);
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800277 tpm_dev_del_device(chip);
278}
279EXPORT_SYMBOL_GPL(tpm_chip_unregister);