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 */
tpm_try_get_ops(struct tpm_chip * chip)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 */
tpm_put_ops(struct tpm_chip * chip)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 */
tpm_chip_find_get(int chip_num)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 */
tpm_dev_release(struct device * dev)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 /**
135 * tpm_class_shutdown() - prepare the TPM device for loss of power.
136 * @dev: device to which the chip is associated.
137 *
138 * Issues a TPM2_Shutdown command prior to loss of power, as required by the
139 * TPM 2.0 spec.
140 * Then, calls bus- and device- specific shutdown code.
141 *
142 * XXX: This codepath relies on the fact that sysfs is not enabled for
143 * TPM2: sysfs uses an implicit lock on chip->ops, so this could race if TPM2
144 * has sysfs support enabled before TPM sysfs's implicit locking is fixed.
145 */
tpm_class_shutdown(struct device * dev)146 static int tpm_class_shutdown(struct device *dev)
147 {
148 struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev);
149
150 if (chip->flags & TPM_CHIP_FLAG_TPM2) {
151 down_write(&chip->ops_sem);
152 tpm2_shutdown(chip, TPM2_SU_CLEAR);
153 chip->ops = NULL;
154 up_write(&chip->ops_sem);
155 }
156 /* Allow bus- and device-specific code to run. Note: since chip->ops
157 * is NULL, more-specific shutdown code will not be able to issue TPM
158 * commands.
159 */
160 if (dev->bus && dev->bus->shutdown)
161 dev->bus->shutdown(dev);
162 else if (dev->driver && dev->driver->shutdown)
163 dev->driver->shutdown(dev);
164 return 0;
165 }
166
167
168 /**
169 * tpm_chip_alloc() - allocate a new struct tpm_chip instance
170 * @pdev: device to which the chip is associated
171 * At this point pdev mst be initialized, but does not have to
172 * be registered
173 * @ops: struct tpm_class_ops instance
174 *
175 * Allocates a new struct tpm_chip instance and assigns a free
176 * device number for it. Must be paired with put_device(&chip->dev).
177 */
tpm_chip_alloc(struct device * pdev,const struct tpm_class_ops * ops)178 struct tpm_chip *tpm_chip_alloc(struct device *pdev,
179 const struct tpm_class_ops *ops)
180 {
181 struct tpm_chip *chip;
182 int rc;
183
184 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
185 if (chip == NULL)
186 return ERR_PTR(-ENOMEM);
187
188 mutex_init(&chip->tpm_mutex);
189 init_rwsem(&chip->ops_sem);
190
191 chip->ops = ops;
192
193 mutex_lock(&idr_lock);
194 rc = idr_alloc(&dev_nums_idr, NULL, 0, TPM_NUM_DEVICES, GFP_KERNEL);
195 mutex_unlock(&idr_lock);
196 if (rc < 0) {
197 dev_err(pdev, "No available tpm device numbers\n");
198 kfree(chip);
199 return ERR_PTR(rc);
200 }
201 chip->dev_num = rc;
202
203 device_initialize(&chip->dev);
204
205 chip->dev.class = tpm_class;
206 chip->dev.class->shutdown = tpm_class_shutdown;
207 chip->dev.release = tpm_dev_release;
208 chip->dev.parent = pdev;
209 chip->dev.groups = chip->groups;
210
211 if (chip->dev_num == 0)
212 chip->dev.devt = MKDEV(MISC_MAJOR, TPM_MINOR);
213 else
214 chip->dev.devt = MKDEV(MAJOR(tpm_devt), chip->dev_num);
215
216 rc = dev_set_name(&chip->dev, "tpm%d", chip->dev_num);
217 if (rc)
218 goto out;
219
220 if (!pdev)
221 chip->flags |= TPM_CHIP_FLAG_VIRTUAL;
222
223 cdev_init(&chip->cdev, &tpm_fops);
224 chip->cdev.owner = THIS_MODULE;
225 chip->cdev.kobj.parent = &chip->dev.kobj;
226
227 return chip;
228
229 out:
230 put_device(&chip->dev);
231 return ERR_PTR(rc);
232 }
233 EXPORT_SYMBOL_GPL(tpm_chip_alloc);
234
235 /**
236 * tpmm_chip_alloc() - allocate a new struct tpm_chip instance
237 * @pdev: parent device to which the chip is associated
238 * @ops: struct tpm_class_ops instance
239 *
240 * Same as tpm_chip_alloc except devm is used to do the put_device
241 */
tpmm_chip_alloc(struct device * pdev,const struct tpm_class_ops * ops)242 struct tpm_chip *tpmm_chip_alloc(struct device *pdev,
243 const struct tpm_class_ops *ops)
244 {
245 struct tpm_chip *chip;
246 int rc;
247
248 chip = tpm_chip_alloc(pdev, ops);
249 if (IS_ERR(chip))
250 return chip;
251
252 rc = devm_add_action_or_reset(pdev,
253 (void (*)(void *)) put_device,
254 &chip->dev);
255 if (rc)
256 return ERR_PTR(rc);
257
258 dev_set_drvdata(pdev, chip);
259
260 return chip;
261 }
262 EXPORT_SYMBOL_GPL(tpmm_chip_alloc);
263
tpm_add_char_device(struct tpm_chip * chip)264 static int tpm_add_char_device(struct tpm_chip *chip)
265 {
266 int rc;
267
268 rc = cdev_add(&chip->cdev, chip->dev.devt, 1);
269 if (rc) {
270 dev_err(&chip->dev,
271 "unable to cdev_add() %s, major %d, minor %d, err=%d\n",
272 dev_name(&chip->dev), MAJOR(chip->dev.devt),
273 MINOR(chip->dev.devt), rc);
274
275 return rc;
276 }
277
278 rc = device_add(&chip->dev);
279 if (rc) {
280 dev_err(&chip->dev,
281 "unable to device_register() %s, major %d, minor %d, err=%d\n",
282 dev_name(&chip->dev), MAJOR(chip->dev.devt),
283 MINOR(chip->dev.devt), rc);
284
285 cdev_del(&chip->cdev);
286 return rc;
287 }
288
289 /* Make the chip available. */
290 mutex_lock(&idr_lock);
291 idr_replace(&dev_nums_idr, chip, chip->dev_num);
292 mutex_unlock(&idr_lock);
293
294 return rc;
295 }
296
tpm_del_char_device(struct tpm_chip * chip)297 static void tpm_del_char_device(struct tpm_chip *chip)
298 {
299 cdev_del(&chip->cdev);
300 device_del(&chip->dev);
301
302 /* Make the chip unavailable. */
303 mutex_lock(&idr_lock);
304 idr_replace(&dev_nums_idr, NULL, chip->dev_num);
305 mutex_unlock(&idr_lock);
306
307 /* Make the driver uncallable. */
308 down_write(&chip->ops_sem);
309 if (chip->flags & TPM_CHIP_FLAG_TPM2)
310 tpm2_shutdown(chip, TPM2_SU_CLEAR);
311 chip->ops = NULL;
312 up_write(&chip->ops_sem);
313 }
314
tpm1_chip_register(struct tpm_chip * chip)315 static int tpm1_chip_register(struct tpm_chip *chip)
316 {
317 if (chip->flags & TPM_CHIP_FLAG_TPM2)
318 return 0;
319
320 tpm_sysfs_add_device(chip);
321
322 chip->bios_dir = tpm_bios_log_setup(dev_name(&chip->dev));
323
324 return 0;
325 }
326
tpm1_chip_unregister(struct tpm_chip * chip)327 static void tpm1_chip_unregister(struct tpm_chip *chip)
328 {
329 if (chip->flags & TPM_CHIP_FLAG_TPM2)
330 return;
331
332 if (chip->bios_dir)
333 tpm_bios_log_teardown(chip->bios_dir);
334 }
335
tpm_del_legacy_sysfs(struct tpm_chip * chip)336 static void tpm_del_legacy_sysfs(struct tpm_chip *chip)
337 {
338 struct attribute **i;
339
340 if (chip->flags & (TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_VIRTUAL))
341 return;
342
343 sysfs_remove_link(&chip->dev.parent->kobj, "ppi");
344
345 for (i = chip->groups[0]->attrs; *i != NULL; ++i)
346 sysfs_remove_link(&chip->dev.parent->kobj, (*i)->name);
347 }
348
349 /* For compatibility with legacy sysfs paths we provide symlinks from the
350 * parent dev directory to selected names within the tpm chip directory. Old
351 * kernel versions created these files directly under the parent.
352 */
tpm_add_legacy_sysfs(struct tpm_chip * chip)353 static int tpm_add_legacy_sysfs(struct tpm_chip *chip)
354 {
355 struct attribute **i;
356 int rc;
357
358 if (chip->flags & (TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_VIRTUAL))
359 return 0;
360
361 rc = __compat_only_sysfs_link_entry_to_kobj(
362 &chip->dev.parent->kobj, &chip->dev.kobj, "ppi");
363 if (rc && rc != -ENOENT)
364 return rc;
365
366 /* All the names from tpm-sysfs */
367 for (i = chip->groups[0]->attrs; *i != NULL; ++i) {
368 rc = __compat_only_sysfs_link_entry_to_kobj(
369 &chip->dev.parent->kobj, &chip->dev.kobj, (*i)->name);
370 if (rc) {
371 tpm_del_legacy_sysfs(chip);
372 return rc;
373 }
374 }
375
376 return 0;
377 }
378 /*
379 * tpm_chip_register() - create a character device for the TPM chip
380 * @chip: TPM chip to use.
381 *
382 * Creates a character device for the TPM chip and adds sysfs attributes for
383 * the device. As the last step this function adds the chip to the list of TPM
384 * chips available for in-kernel use.
385 *
386 * This function should be only called after the chip initialization is
387 * complete.
388 */
tpm_chip_register(struct tpm_chip * chip)389 int tpm_chip_register(struct tpm_chip *chip)
390 {
391 int rc;
392
393 if (chip->ops->flags & TPM_OPS_AUTO_STARTUP) {
394 if (chip->flags & TPM_CHIP_FLAG_TPM2)
395 rc = tpm2_auto_startup(chip);
396 else
397 rc = tpm1_auto_startup(chip);
398 if (rc)
399 return rc;
400 }
401
402 rc = tpm1_chip_register(chip);
403 if (rc)
404 return rc;
405
406 tpm_add_ppi(chip);
407
408 rc = tpm_add_char_device(chip);
409 if (rc) {
410 tpm1_chip_unregister(chip);
411 return rc;
412 }
413
414 chip->flags |= TPM_CHIP_FLAG_REGISTERED;
415
416 rc = tpm_add_legacy_sysfs(chip);
417 if (rc) {
418 tpm_chip_unregister(chip);
419 return rc;
420 }
421
422 return 0;
423 }
424 EXPORT_SYMBOL_GPL(tpm_chip_register);
425
426 /*
427 * tpm_chip_unregister() - release the TPM driver
428 * @chip: TPM chip to use.
429 *
430 * Takes the chip first away from the list of available TPM chips and then
431 * cleans up all the resources reserved by tpm_chip_register().
432 *
433 * Once this function returns the driver call backs in 'op's will not be
434 * running and will no longer start.
435 *
436 * NOTE: This function should be only called before deinitializing chip
437 * resources.
438 */
tpm_chip_unregister(struct tpm_chip * chip)439 void tpm_chip_unregister(struct tpm_chip *chip)
440 {
441 if (!(chip->flags & TPM_CHIP_FLAG_REGISTERED))
442 return;
443
444 tpm_del_legacy_sysfs(chip);
445
446 tpm1_chip_unregister(chip);
447 tpm_del_char_device(chip);
448 }
449 EXPORT_SYMBOL_GPL(tpm_chip_unregister);
450