1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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/tpm_eventlog.h>
30#include <linux/hw_random.h>
31#include "tpm.h"
32
33DEFINE_IDR(dev_nums_idr);
34static DEFINE_MUTEX(idr_lock);
35
36struct class *tpm_class;
37struct class *tpmrm_class;
38dev_t tpm_devt;
39
40
41
42
43
44
45
46
47
48
49
50
51int tpm_try_get_ops(struct tpm_chip *chip)
52{
53 int rc = -EIO;
54
55 get_device(&chip->dev);
56
57 down_read(&chip->ops_sem);
58 if (!chip->ops)
59 goto out_lock;
60
61 return 0;
62out_lock:
63 up_read(&chip->ops_sem);
64 put_device(&chip->dev);
65 return rc;
66}
67EXPORT_SYMBOL_GPL(tpm_try_get_ops);
68
69
70
71
72
73
74
75
76void tpm_put_ops(struct tpm_chip *chip)
77{
78 up_read(&chip->ops_sem);
79 put_device(&chip->dev);
80}
81EXPORT_SYMBOL_GPL(tpm_put_ops);
82
83
84
85
86
87
88
89
90
91
92
93
94
95struct tpm_chip *tpm_chip_find_get(struct tpm_chip *chip)
96{
97 struct tpm_chip *res = NULL;
98 int chip_num = 0;
99 int chip_prev;
100
101 mutex_lock(&idr_lock);
102
103 if (!chip) {
104 do {
105 chip_prev = chip_num;
106 chip = idr_get_next(&dev_nums_idr, &chip_num);
107 if (chip && !tpm_try_get_ops(chip)) {
108 res = chip;
109 break;
110 }
111 } while (chip_prev != chip_num);
112 } else {
113 if (!tpm_try_get_ops(chip))
114 res = chip;
115 }
116
117 mutex_unlock(&idr_lock);
118
119 return res;
120}
121
122
123
124
125
126
127
128static void tpm_dev_release(struct device *dev)
129{
130 struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev);
131
132 mutex_lock(&idr_lock);
133 idr_remove(&dev_nums_idr, chip->dev_num);
134 mutex_unlock(&idr_lock);
135
136 kfree(chip->log.bios_event_log);
137 kfree(chip->work_space.context_buf);
138 kfree(chip->work_space.session_buf);
139 kfree(chip);
140}
141
142static void tpm_devs_release(struct device *dev)
143{
144 struct tpm_chip *chip = container_of(dev, struct tpm_chip, devs);
145
146
147 put_device(&chip->dev);
148}
149
150
151
152
153
154
155
156
157
158
159
160
161
162static int tpm_class_shutdown(struct device *dev)
163{
164 struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev);
165
166 if (chip->flags & TPM_CHIP_FLAG_TPM2) {
167 down_write(&chip->ops_sem);
168 tpm2_shutdown(chip, TPM2_SU_CLEAR);
169 chip->ops = NULL;
170 up_write(&chip->ops_sem);
171 }
172
173 return 0;
174}
175
176
177
178
179
180
181
182
183
184
185
186struct tpm_chip *tpm_chip_alloc(struct device *pdev,
187 const struct tpm_class_ops *ops)
188{
189 struct tpm_chip *chip;
190 int rc;
191
192 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
193 if (chip == NULL)
194 return ERR_PTR(-ENOMEM);
195
196 mutex_init(&chip->tpm_mutex);
197 init_rwsem(&chip->ops_sem);
198
199 chip->ops = ops;
200
201 mutex_lock(&idr_lock);
202 rc = idr_alloc(&dev_nums_idr, NULL, 0, TPM_NUM_DEVICES, GFP_KERNEL);
203 mutex_unlock(&idr_lock);
204 if (rc < 0) {
205 dev_err(pdev, "No available tpm device numbers\n");
206 kfree(chip);
207 return ERR_PTR(rc);
208 }
209 chip->dev_num = rc;
210
211 device_initialize(&chip->dev);
212 device_initialize(&chip->devs);
213
214 chip->dev.class = tpm_class;
215 chip->dev.class->shutdown_pre = tpm_class_shutdown;
216 chip->dev.release = tpm_dev_release;
217 chip->dev.parent = pdev;
218 chip->dev.groups = chip->groups;
219
220 chip->devs.parent = pdev;
221 chip->devs.class = tpmrm_class;
222 chip->devs.release = tpm_devs_release;
223
224
225
226
227
228 if (chip->flags & TPM_CHIP_FLAG_TPM2)
229 get_device(&chip->dev);
230
231 if (chip->dev_num == 0)
232 chip->dev.devt = MKDEV(MISC_MAJOR, TPM_MINOR);
233 else
234 chip->dev.devt = MKDEV(MAJOR(tpm_devt), chip->dev_num);
235
236 chip->devs.devt =
237 MKDEV(MAJOR(tpm_devt), chip->dev_num + TPM_NUM_DEVICES);
238
239 rc = dev_set_name(&chip->dev, "tpm%d", chip->dev_num);
240 if (rc)
241 goto out;
242 rc = dev_set_name(&chip->devs, "tpmrm%d", chip->dev_num);
243 if (rc)
244 goto out;
245
246 if (!pdev)
247 chip->flags |= TPM_CHIP_FLAG_VIRTUAL;
248
249 cdev_init(&chip->cdev, &tpm_fops);
250 cdev_init(&chip->cdevs, &tpmrm_fops);
251 chip->cdev.owner = THIS_MODULE;
252 chip->cdevs.owner = THIS_MODULE;
253
254 chip->work_space.context_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
255 if (!chip->work_space.context_buf) {
256 rc = -ENOMEM;
257 goto out;
258 }
259 chip->work_space.session_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
260 if (!chip->work_space.session_buf) {
261 rc = -ENOMEM;
262 goto out;
263 }
264
265 chip->locality = -1;
266 return chip;
267
268out:
269 put_device(&chip->devs);
270 put_device(&chip->dev);
271 return ERR_PTR(rc);
272}
273EXPORT_SYMBOL_GPL(tpm_chip_alloc);
274
275
276
277
278
279
280
281
282struct tpm_chip *tpmm_chip_alloc(struct device *pdev,
283 const struct tpm_class_ops *ops)
284{
285 struct tpm_chip *chip;
286 int rc;
287
288 chip = tpm_chip_alloc(pdev, ops);
289 if (IS_ERR(chip))
290 return chip;
291
292 rc = devm_add_action_or_reset(pdev,
293 (void (*)(void *)) put_device,
294 &chip->dev);
295 if (rc)
296 return ERR_PTR(rc);
297
298 dev_set_drvdata(pdev, chip);
299
300 return chip;
301}
302EXPORT_SYMBOL_GPL(tpmm_chip_alloc);
303
304static int tpm_add_char_device(struct tpm_chip *chip)
305{
306 int rc;
307
308 rc = cdev_device_add(&chip->cdev, &chip->dev);
309 if (rc) {
310 dev_err(&chip->dev,
311 "unable to cdev_device_add() %s, major %d, minor %d, err=%d\n",
312 dev_name(&chip->dev), MAJOR(chip->dev.devt),
313 MINOR(chip->dev.devt), rc);
314 return rc;
315 }
316
317 if (chip->flags & TPM_CHIP_FLAG_TPM2) {
318 rc = cdev_device_add(&chip->cdevs, &chip->devs);
319 if (rc) {
320 dev_err(&chip->devs,
321 "unable to cdev_device_add() %s, major %d, minor %d, err=%d\n",
322 dev_name(&chip->devs), MAJOR(chip->devs.devt),
323 MINOR(chip->devs.devt), rc);
324 return rc;
325 }
326 }
327
328
329 mutex_lock(&idr_lock);
330 idr_replace(&dev_nums_idr, chip, chip->dev_num);
331 mutex_unlock(&idr_lock);
332
333 return rc;
334}
335
336static void tpm_del_char_device(struct tpm_chip *chip)
337{
338 cdev_device_del(&chip->cdev, &chip->dev);
339
340
341 mutex_lock(&idr_lock);
342 idr_replace(&dev_nums_idr, NULL, chip->dev_num);
343 mutex_unlock(&idr_lock);
344
345
346 down_write(&chip->ops_sem);
347 if (chip->flags & TPM_CHIP_FLAG_TPM2)
348 tpm2_shutdown(chip, TPM2_SU_CLEAR);
349 chip->ops = NULL;
350 up_write(&chip->ops_sem);
351}
352
353static void tpm_del_legacy_sysfs(struct tpm_chip *chip)
354{
355 struct attribute **i;
356
357 if (chip->flags & (TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_VIRTUAL))
358 return;
359
360 sysfs_remove_link(&chip->dev.parent->kobj, "ppi");
361
362 for (i = chip->groups[0]->attrs; *i != NULL; ++i)
363 sysfs_remove_link(&chip->dev.parent->kobj, (*i)->name);
364}
365
366
367
368
369
370static int tpm_add_legacy_sysfs(struct tpm_chip *chip)
371{
372 struct attribute **i;
373 int rc;
374
375 if (chip->flags & (TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_VIRTUAL))
376 return 0;
377
378 rc = __compat_only_sysfs_link_entry_to_kobj(
379 &chip->dev.parent->kobj, &chip->dev.kobj, "ppi");
380 if (rc && rc != -ENOENT)
381 return rc;
382
383
384 for (i = chip->groups[0]->attrs; *i != NULL; ++i) {
385 rc = __compat_only_sysfs_link_entry_to_kobj(
386 &chip->dev.parent->kobj, &chip->dev.kobj, (*i)->name);
387 if (rc) {
388 tpm_del_legacy_sysfs(chip);
389 return rc;
390 }
391 }
392
393 return 0;
394}
395
396static int tpm_hwrng_read(struct hwrng *rng, void *data, size_t max, bool wait)
397{
398 struct tpm_chip *chip = container_of(rng, struct tpm_chip, hwrng);
399
400 return tpm_get_random(chip, data, max);
401}
402
403static int tpm_add_hwrng(struct tpm_chip *chip)
404{
405 if (!IS_ENABLED(CONFIG_HW_RANDOM_TPM))
406 return 0;
407
408 snprintf(chip->hwrng_name, sizeof(chip->hwrng_name),
409 "tpm-rng-%d", chip->dev_num);
410 chip->hwrng.name = chip->hwrng_name;
411 chip->hwrng.read = tpm_hwrng_read;
412 return hwrng_register(&chip->hwrng);
413}
414
415
416
417
418
419
420
421
422
423
424
425
426int tpm_chip_register(struct tpm_chip *chip)
427{
428 int rc;
429
430 if (chip->ops->flags & TPM_OPS_AUTO_STARTUP) {
431 if (chip->flags & TPM_CHIP_FLAG_TPM2)
432 rc = tpm2_auto_startup(chip);
433 else
434 rc = tpm1_auto_startup(chip);
435 if (rc)
436 return rc;
437 }
438
439 tpm_sysfs_add_device(chip);
440
441 rc = tpm_bios_log_setup(chip);
442 if (rc != 0 && rc != -ENODEV)
443 return rc;
444
445 tpm_add_ppi(chip);
446
447 rc = tpm_add_hwrng(chip);
448 if (rc)
449 goto out_ppi;
450
451 rc = tpm_add_char_device(chip);
452 if (rc)
453 goto out_hwrng;
454
455 rc = tpm_add_legacy_sysfs(chip);
456 if (rc) {
457 tpm_chip_unregister(chip);
458 return rc;
459 }
460
461 return 0;
462
463out_hwrng:
464 if (IS_ENABLED(CONFIG_HW_RANDOM_TPM))
465 hwrng_unregister(&chip->hwrng);
466out_ppi:
467 tpm_bios_log_teardown(chip);
468
469 return rc;
470}
471EXPORT_SYMBOL_GPL(tpm_chip_register);
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486void tpm_chip_unregister(struct tpm_chip *chip)
487{
488 tpm_del_legacy_sysfs(chip);
489 if (IS_ENABLED(CONFIG_HW_RANDOM_TPM))
490 hwrng_unregister(&chip->hwrng);
491 tpm_bios_log_teardown(chip);
492 if (chip->flags & TPM_CHIP_FLAG_TPM2)
493 cdev_device_del(&chip->cdevs, &chip->devs);
494 tpm_del_char_device(chip);
495}
496EXPORT_SYMBOL_GPL(tpm_chip_unregister);
497