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