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