xref: /openbmc/linux/drivers/char/tpm/tpm-chip.c (revision 80483c3a)
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 
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 "tpm.h"
30 #include "tpm_eventlog.h"
31 
32 DEFINE_IDR(dev_nums_idr);
33 static DEFINE_MUTEX(idr_lock);
34 
35 struct class *tpm_class;
36 dev_t tpm_devt;
37 
38 /**
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.
48  */
49 int 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 
59 	return 0;
60 out_lock:
61 	up_read(&chip->ops_sem);
62 	put_device(&chip->dev);
63 	return rc;
64 }
65 EXPORT_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  */
74 void tpm_put_ops(struct tpm_chip *chip)
75 {
76 	up_read(&chip->ops_sem);
77 	put_device(&chip->dev);
78 }
79 EXPORT_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   */
88 struct tpm_chip *tpm_chip_find_get(int chip_num)
89 {
90 	struct tpm_chip *chip, *res = NULL;
91 	int chip_prev;
92 
93 	mutex_lock(&idr_lock);
94 
95 	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;
109 	}
110 
111 	mutex_unlock(&idr_lock);
112 
113 	return res;
114 }
115 
116 /**
117  * tpm_dev_release() - free chip memory and the device number
118  * @dev: the character device for the TPM chip
119  *
120  * This is used as the release function for the character device.
121  */
122 static void tpm_dev_release(struct device *dev)
123 {
124 	struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev);
125 
126 	mutex_lock(&idr_lock);
127 	idr_remove(&dev_nums_idr, chip->dev_num);
128 	mutex_unlock(&idr_lock);
129 
130 	kfree(chip);
131 }
132 
133 /**
134  * 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
138  * @ops: struct tpm_class_ops instance
139  *
140  * Allocates a new struct tpm_chip instance and assigns a free
141  * device number for it. Must be paired with put_device(&chip->dev).
142  */
143 struct tpm_chip *tpm_chip_alloc(struct device *dev,
144 				const struct tpm_class_ops *ops)
145 {
146 	struct tpm_chip *chip;
147 	int rc;
148 
149 	chip = kzalloc(sizeof(*chip), GFP_KERNEL);
150 	if (chip == NULL)
151 		return ERR_PTR(-ENOMEM);
152 
153 	mutex_init(&chip->tpm_mutex);
154 	init_rwsem(&chip->ops_sem);
155 
156 	chip->ops = ops;
157 
158 	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) {
162 		dev_err(dev, "No available tpm device numbers\n");
163 		kfree(chip);
164 		return ERR_PTR(rc);
165 	}
166 	chip->dev_num = rc;
167 
168 	device_initialize(&chip->dev);
169 
170 	chip->dev.class = tpm_class;
171 	chip->dev.release = tpm_dev_release;
172 	chip->dev.parent = dev;
173 	chip->dev.groups = chip->groups;
174 
175 	if (chip->dev_num == 0)
176 		chip->dev.devt = MKDEV(MISC_MAJOR, TPM_MINOR);
177 	else
178 		chip->dev.devt = MKDEV(MAJOR(tpm_devt), chip->dev_num);
179 
180 	rc = dev_set_name(&chip->dev, "tpm%d", chip->dev_num);
181 	if (rc)
182 		goto out;
183 
184 	if (!dev)
185 		chip->flags |= TPM_CHIP_FLAG_VIRTUAL;
186 
187 	cdev_init(&chip->cdev, &tpm_fops);
188 	chip->cdev.owner = THIS_MODULE;
189 	chip->cdev.kobj.parent = &chip->dev.kobj;
190 
191 	return chip;
192 
193 out:
194 	put_device(&chip->dev);
195 	return ERR_PTR(rc);
196 }
197 EXPORT_SYMBOL_GPL(tpm_chip_alloc);
198 
199 /**
200  * tpmm_chip_alloc() - allocate a new struct tpm_chip instance
201  * @pdev: parent device to which the chip is associated
202  * @ops: struct tpm_class_ops instance
203  *
204  * Same as tpm_chip_alloc except devm is used to do the put_device
205  */
206 struct tpm_chip *tpmm_chip_alloc(struct device *pdev,
207 				 const struct tpm_class_ops *ops)
208 {
209 	struct tpm_chip *chip;
210 	int rc;
211 
212 	chip = tpm_chip_alloc(pdev, ops);
213 	if (IS_ERR(chip))
214 		return chip;
215 
216 	rc = devm_add_action_or_reset(pdev,
217 				      (void (*)(void *)) put_device,
218 				      &chip->dev);
219 	if (rc)
220 		return ERR_PTR(rc);
221 
222 	dev_set_drvdata(pdev, chip);
223 
224 	return chip;
225 }
226 EXPORT_SYMBOL_GPL(tpmm_chip_alloc);
227 
228 static int tpm_add_char_device(struct tpm_chip *chip)
229 {
230 	int rc;
231 
232 	rc = cdev_add(&chip->cdev, chip->dev.devt, 1);
233 	if (rc) {
234 		dev_err(&chip->dev,
235 			"unable to cdev_add() %s, major %d, minor %d, err=%d\n",
236 			dev_name(&chip->dev), MAJOR(chip->dev.devt),
237 			MINOR(chip->dev.devt), rc);
238 
239 		return rc;
240 	}
241 
242 	rc = device_add(&chip->dev);
243 	if (rc) {
244 		dev_err(&chip->dev,
245 			"unable to device_register() %s, major %d, minor %d, err=%d\n",
246 			dev_name(&chip->dev), MAJOR(chip->dev.devt),
247 			MINOR(chip->dev.devt), rc);
248 
249 		cdev_del(&chip->cdev);
250 		return rc;
251 	}
252 
253 	/* Make the chip available. */
254 	mutex_lock(&idr_lock);
255 	idr_replace(&dev_nums_idr, chip, chip->dev_num);
256 	mutex_unlock(&idr_lock);
257 
258 	return rc;
259 }
260 
261 static void tpm_del_char_device(struct tpm_chip *chip)
262 {
263 	cdev_del(&chip->cdev);
264 	device_del(&chip->dev);
265 
266 	/* Make the chip unavailable. */
267 	mutex_lock(&idr_lock);
268 	idr_replace(&dev_nums_idr, NULL, chip->dev_num);
269 	mutex_unlock(&idr_lock);
270 
271 	/* Make the driver uncallable. */
272 	down_write(&chip->ops_sem);
273 	if (chip->flags & TPM_CHIP_FLAG_TPM2)
274 		tpm2_shutdown(chip, TPM2_SU_CLEAR);
275 	chip->ops = NULL;
276 	up_write(&chip->ops_sem);
277 }
278 
279 static int tpm1_chip_register(struct tpm_chip *chip)
280 {
281 	if (chip->flags & TPM_CHIP_FLAG_TPM2)
282 		return 0;
283 
284 	tpm_sysfs_add_device(chip);
285 
286 	chip->bios_dir = tpm_bios_log_setup(dev_name(&chip->dev));
287 
288 	return 0;
289 }
290 
291 static void tpm1_chip_unregister(struct tpm_chip *chip)
292 {
293 	if (chip->flags & TPM_CHIP_FLAG_TPM2)
294 		return;
295 
296 	if (chip->bios_dir)
297 		tpm_bios_log_teardown(chip->bios_dir);
298 }
299 
300 static void tpm_del_legacy_sysfs(struct tpm_chip *chip)
301 {
302 	struct attribute **i;
303 
304 	if (chip->flags & (TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_VIRTUAL))
305 		return;
306 
307 	sysfs_remove_link(&chip->dev.parent->kobj, "ppi");
308 
309 	for (i = chip->groups[0]->attrs; *i != NULL; ++i)
310 		sysfs_remove_link(&chip->dev.parent->kobj, (*i)->name);
311 }
312 
313 /* For compatibility with legacy sysfs paths we provide symlinks from the
314  * parent dev directory to selected names within the tpm chip directory. Old
315  * kernel versions created these files directly under the parent.
316  */
317 static int tpm_add_legacy_sysfs(struct tpm_chip *chip)
318 {
319 	struct attribute **i;
320 	int rc;
321 
322 	if (chip->flags & (TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_VIRTUAL))
323 		return 0;
324 
325 	rc = __compat_only_sysfs_link_entry_to_kobj(
326 		    &chip->dev.parent->kobj, &chip->dev.kobj, "ppi");
327 	if (rc && rc != -ENOENT)
328 		return rc;
329 
330 	/* All the names from tpm-sysfs */
331 	for (i = chip->groups[0]->attrs; *i != NULL; ++i) {
332 		rc = __compat_only_sysfs_link_entry_to_kobj(
333 		    &chip->dev.parent->kobj, &chip->dev.kobj, (*i)->name);
334 		if (rc) {
335 			tpm_del_legacy_sysfs(chip);
336 			return rc;
337 		}
338 	}
339 
340 	return 0;
341 }
342 /*
343  * tpm_chip_register() - create a character device for the TPM chip
344  * @chip: TPM chip to use.
345  *
346  * Creates a character device for the TPM chip and adds sysfs attributes for
347  * the device. As the last step this function adds the chip to the list of TPM
348  * chips available for in-kernel use.
349  *
350  * This function should be only called after the chip initialization is
351  * complete.
352  */
353 int tpm_chip_register(struct tpm_chip *chip)
354 {
355 	int rc;
356 
357 	if (chip->ops->flags & TPM_OPS_AUTO_STARTUP) {
358 		if (chip->flags & TPM_CHIP_FLAG_TPM2)
359 			rc = tpm2_auto_startup(chip);
360 		else
361 			rc = tpm1_auto_startup(chip);
362 		if (rc)
363 			return rc;
364 	}
365 
366 	rc = tpm1_chip_register(chip);
367 	if (rc)
368 		return rc;
369 
370 	tpm_add_ppi(chip);
371 
372 	rc = tpm_add_char_device(chip);
373 	if (rc) {
374 		tpm1_chip_unregister(chip);
375 		return rc;
376 	}
377 
378 	chip->flags |= TPM_CHIP_FLAG_REGISTERED;
379 
380 	rc = tpm_add_legacy_sysfs(chip);
381 	if (rc) {
382 		tpm_chip_unregister(chip);
383 		return rc;
384 	}
385 
386 	return 0;
387 }
388 EXPORT_SYMBOL_GPL(tpm_chip_register);
389 
390 /*
391  * tpm_chip_unregister() - release the TPM driver
392  * @chip: TPM chip to use.
393  *
394  * Takes the chip first away from the list of available TPM chips and then
395  * cleans up all the resources reserved by tpm_chip_register().
396  *
397  * Once this function returns the driver call backs in 'op's will not be
398  * running and will no longer start.
399  *
400  * NOTE: This function should be only called before deinitializing chip
401  * resources.
402  */
403 void tpm_chip_unregister(struct tpm_chip *chip)
404 {
405 	if (!(chip->flags & TPM_CHIP_FLAG_REGISTERED))
406 		return;
407 
408 	tpm_del_legacy_sysfs(chip);
409 
410 	tpm1_chip_unregister(chip);
411 	tpm_del_char_device(chip);
412 }
413 EXPORT_SYMBOL_GPL(tpm_chip_unregister);
414