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