Commit | Line | Data |
---|---|---|
b886d83c | 1 | // SPDX-License-Identifier: GPL-2.0-only |
afb5abc2 JS |
2 | /* |
3 | * Copyright (C) 2004 IBM Corporation | |
4 | * Copyright (C) 2014 Intel Corporation | |
5 | * | |
6 | * Authors: | |
7 | * Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com> | |
8 | * Leendert van Doorn <leendert@watson.ibm.com> | |
9 | * Dave Safford <safford@watson.ibm.com> | |
10 | * Reiner Sailer <sailer@watson.ibm.com> | |
11 | * Kylene Hall <kjhall@us.ibm.com> | |
12 | * | |
13 | * Maintained by: <tpmdd-devel@lists.sourceforge.net> | |
14 | * | |
15 | * TPM chip management routines. | |
afb5abc2 JS |
16 | */ |
17 | ||
18 | #include <linux/poll.h> | |
19 | #include <linux/slab.h> | |
20 | #include <linux/mutex.h> | |
21 | #include <linux/spinlock.h> | |
22 | #include <linux/freezer.h> | |
313d21ee | 23 | #include <linux/major.h> |
fd3ec366 | 24 | #include <linux/tpm_eventlog.h> |
6e592a06 | 25 | #include <linux/hw_random.h> |
afb5abc2 | 26 | #include "tpm.h" |
afb5abc2 | 27 | |
15516788 SB |
28 | DEFINE_IDR(dev_nums_idr); |
29 | static DEFINE_MUTEX(idr_lock); | |
afb5abc2 | 30 | |
313d21ee | 31 | struct class *tpm_class; |
fdc915f7 | 32 | struct class *tpmrm_class; |
313d21ee JS |
33 | dev_t tpm_devt; |
34 | ||
47a6c28b | 35 | static int tpm_request_locality(struct tpm_chip *chip) |
719b7d81 JS |
36 | { |
37 | int rc; | |
38 | ||
719b7d81 JS |
39 | if (!chip->ops->request_locality) |
40 | return 0; | |
41 | ||
42 | rc = chip->ops->request_locality(chip, 0); | |
43 | if (rc < 0) | |
44 | return rc; | |
45 | ||
46 | chip->locality = rc; | |
47 | return 0; | |
48 | } | |
49 | ||
47a6c28b | 50 | static void tpm_relinquish_locality(struct tpm_chip *chip) |
719b7d81 JS |
51 | { |
52 | int rc; | |
53 | ||
719b7d81 JS |
54 | if (!chip->ops->relinquish_locality) |
55 | return; | |
56 | ||
57 | rc = chip->ops->relinquish_locality(chip, chip->locality); | |
58 | if (rc) | |
59 | dev_err(&chip->dev, "%s: : error %d\n", __func__, rc); | |
60 | ||
61 | chip->locality = -1; | |
62 | } | |
63 | ||
47a6c28b | 64 | static int tpm_cmd_ready(struct tpm_chip *chip) |
719b7d81 | 65 | { |
719b7d81 JS |
66 | if (!chip->ops->cmd_ready) |
67 | return 0; | |
68 | ||
69 | return chip->ops->cmd_ready(chip); | |
70 | } | |
71 | ||
47a6c28b | 72 | static int tpm_go_idle(struct tpm_chip *chip) |
719b7d81 | 73 | { |
719b7d81 JS |
74 | if (!chip->ops->go_idle) |
75 | return 0; | |
76 | ||
77 | return chip->ops->go_idle(chip); | |
78 | } | |
79 | ||
1e5ac630 MB |
80 | static void tpm_clk_enable(struct tpm_chip *chip) |
81 | { | |
82 | if (chip->ops->clk_enable) | |
83 | chip->ops->clk_enable(chip, true); | |
84 | } | |
85 | ||
86 | static void tpm_clk_disable(struct tpm_chip *chip) | |
87 | { | |
88 | if (chip->ops->clk_enable) | |
89 | chip->ops->clk_enable(chip, false); | |
90 | } | |
91 | ||
719b7d81 JS |
92 | /** |
93 | * tpm_chip_start() - power on the TPM | |
94 | * @chip: a TPM chip to use | |
719b7d81 JS |
95 | * |
96 | * Return: | |
97 | * * The response length - OK | |
98 | * * -errno - A system error | |
99 | */ | |
47a6c28b | 100 | int tpm_chip_start(struct tpm_chip *chip) |
719b7d81 JS |
101 | { |
102 | int ret; | |
103 | ||
1e5ac630 | 104 | tpm_clk_enable(chip); |
719b7d81 JS |
105 | |
106 | if (chip->locality == -1) { | |
47a6c28b | 107 | ret = tpm_request_locality(chip); |
719b7d81 | 108 | if (ret) { |
1e5ac630 | 109 | tpm_clk_disable(chip); |
719b7d81 JS |
110 | return ret; |
111 | } | |
112 | } | |
113 | ||
47a6c28b | 114 | ret = tpm_cmd_ready(chip); |
719b7d81 | 115 | if (ret) { |
47a6c28b | 116 | tpm_relinquish_locality(chip); |
1e5ac630 | 117 | tpm_clk_disable(chip); |
719b7d81 JS |
118 | return ret; |
119 | } | |
120 | ||
121 | return 0; | |
122 | } | |
123 | EXPORT_SYMBOL_GPL(tpm_chip_start); | |
124 | ||
125 | /** | |
126 | * tpm_chip_stop() - power off the TPM | |
127 | * @chip: a TPM chip to use | |
719b7d81 JS |
128 | * |
129 | * Return: | |
130 | * * The response length - OK | |
131 | * * -errno - A system error | |
132 | */ | |
47a6c28b | 133 | void tpm_chip_stop(struct tpm_chip *chip) |
719b7d81 | 134 | { |
47a6c28b JS |
135 | tpm_go_idle(chip); |
136 | tpm_relinquish_locality(chip); | |
1e5ac630 | 137 | tpm_clk_disable(chip); |
719b7d81 JS |
138 | } |
139 | EXPORT_SYMBOL_GPL(tpm_chip_stop); | |
140 | ||
4e26195f JG |
141 | /** |
142 | * tpm_try_get_ops() - Get a ref to the tpm_chip | |
143 | * @chip: Chip to ref | |
144 | * | |
145 | * The caller must already have some kind of locking to ensure that chip is | |
146 | * valid. This function will lock the chip so that the ops member can be | |
147 | * accessed safely. The locking prevents tpm_chip_unregister from | |
148 | * completing, so it should not be held for long periods. | |
149 | * | |
150 | * Returns -ERRNO if the chip could not be got. | |
afb5abc2 | 151 | */ |
4e26195f JG |
152 | int tpm_try_get_ops(struct tpm_chip *chip) |
153 | { | |
154 | int rc = -EIO; | |
155 | ||
156 | get_device(&chip->dev); | |
157 | ||
158 | down_read(&chip->ops_sem); | |
159 | if (!chip->ops) | |
a3fbfae8 | 160 | goto out_ops; |
4e26195f | 161 | |
2f257402 | 162 | mutex_lock(&chip->tpm_mutex); |
47a6c28b | 163 | rc = tpm_chip_start(chip); |
a3fbfae8 JS |
164 | if (rc) |
165 | goto out_lock; | |
166 | ||
4e26195f JG |
167 | return 0; |
168 | out_lock: | |
a3fbfae8 JS |
169 | mutex_unlock(&chip->tpm_mutex); |
170 | out_ops: | |
4e26195f JG |
171 | up_read(&chip->ops_sem); |
172 | put_device(&chip->dev); | |
173 | return rc; | |
174 | } | |
175 | EXPORT_SYMBOL_GPL(tpm_try_get_ops); | |
176 | ||
177 | /** | |
178 | * tpm_put_ops() - Release a ref to the tpm_chip | |
179 | * @chip: Chip to put | |
180 | * | |
181 | * This is the opposite pair to tpm_try_get_ops(). After this returns chip may | |
182 | * be kfree'd. | |
183 | */ | |
184 | void tpm_put_ops(struct tpm_chip *chip) | |
185 | { | |
47a6c28b | 186 | tpm_chip_stop(chip); |
2f257402 | 187 | mutex_unlock(&chip->tpm_mutex); |
4e26195f JG |
188 | up_read(&chip->ops_sem); |
189 | put_device(&chip->dev); | |
190 | } | |
191 | EXPORT_SYMBOL_GPL(tpm_put_ops); | |
192 | ||
aaae8153 SB |
193 | /** |
194 | * tpm_default_chip() - find a TPM chip and get a reference to it | |
195 | */ | |
196 | struct tpm_chip *tpm_default_chip(void) | |
197 | { | |
198 | struct tpm_chip *chip, *res = NULL; | |
199 | int chip_num = 0; | |
200 | int chip_prev; | |
201 | ||
202 | mutex_lock(&idr_lock); | |
203 | ||
204 | do { | |
205 | chip_prev = chip_num; | |
206 | chip = idr_get_next(&dev_nums_idr, &chip_num); | |
207 | if (chip) { | |
208 | get_device(&chip->dev); | |
209 | res = chip; | |
210 | break; | |
211 | } | |
212 | } while (chip_prev != chip_num); | |
213 | ||
214 | mutex_unlock(&idr_lock); | |
215 | ||
216 | return res; | |
217 | } | |
218 | EXPORT_SYMBOL_GPL(tpm_default_chip); | |
219 | ||
4e26195f | 220 | /** |
fc1d52b7 | 221 | * tpm_find_get_ops() - find and reserve a TPM chip |
aad887f6 | 222 | * @chip: a &struct tpm_chip instance, %NULL for the default chip |
4e26195f | 223 | * |
aad887f6 | 224 | * Finds a TPM chip and reserves its class device and operations. The chip must |
fc1d52b7 SB |
225 | * be released with tpm_put_ops() after use. |
226 | * This function is for internal use only. It supports existing TPM callers | |
227 | * by accepting NULL, but those callers should be converted to pass in a chip | |
228 | * directly. | |
aad887f6 JS |
229 | * |
230 | * Return: | |
231 | * A reserved &struct tpm_chip instance. | |
232 | * %NULL if a chip is not found. | |
233 | * %NULL if the chip is not available. | |
37f4915f | 234 | */ |
fc1d52b7 | 235 | struct tpm_chip *tpm_find_get_ops(struct tpm_chip *chip) |
afb5abc2 | 236 | { |
eccc9bb8 | 237 | int rc; |
15516788 | 238 | |
eccc9bb8 | 239 | if (chip) { |
aad887f6 | 240 | if (!tpm_try_get_ops(chip)) |
eccc9bb8 SB |
241 | return chip; |
242 | return NULL; | |
15516788 | 243 | } |
afb5abc2 | 244 | |
eccc9bb8 SB |
245 | chip = tpm_default_chip(); |
246 | if (!chip) | |
247 | return NULL; | |
248 | rc = tpm_try_get_ops(chip); | |
249 | /* release additional reference we got from tpm_default_chip() */ | |
250 | put_device(&chip->dev); | |
251 | if (rc) | |
252 | return NULL; | |
253 | return chip; | |
afb5abc2 JS |
254 | } |
255 | ||
256 | /** | |
313d21ee JS |
257 | * tpm_dev_release() - free chip memory and the device number |
258 | * @dev: the character device for the TPM chip | |
afb5abc2 | 259 | * |
313d21ee | 260 | * This is used as the release function for the character device. |
afb5abc2 | 261 | */ |
313d21ee | 262 | static void tpm_dev_release(struct device *dev) |
afb5abc2 | 263 | { |
313d21ee | 264 | struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev); |
afb5abc2 | 265 | |
15516788 SB |
266 | mutex_lock(&idr_lock); |
267 | idr_remove(&dev_nums_idr, chip->dev_num); | |
268 | mutex_unlock(&idr_lock); | |
269 | ||
748935ee | 270 | kfree(chip->log.bios_event_log); |
745b361e | 271 | kfree(chip->work_space.context_buf); |
4d57856a | 272 | kfree(chip->work_space.session_buf); |
bcfff838 | 273 | kfree(chip->allocated_banks); |
afb5abc2 JS |
274 | kfree(chip); |
275 | } | |
276 | ||
fdc915f7 JB |
277 | static void tpm_devs_release(struct device *dev) |
278 | { | |
279 | struct tpm_chip *chip = container_of(dev, struct tpm_chip, devs); | |
280 | ||
281 | /* release the master device reference */ | |
282 | put_device(&chip->dev); | |
283 | } | |
284 | ||
d1bd4a79 JZ |
285 | /** |
286 | * tpm_class_shutdown() - prepare the TPM device for loss of power. | |
287 | * @dev: device to which the chip is associated. | |
288 | * | |
289 | * Issues a TPM2_Shutdown command prior to loss of power, as required by the | |
290 | * TPM 2.0 spec. | |
291 | * Then, calls bus- and device- specific shutdown code. | |
292 | * | |
293 | * XXX: This codepath relies on the fact that sysfs is not enabled for | |
294 | * TPM2: sysfs uses an implicit lock on chip->ops, so this could race if TPM2 | |
295 | * has sysfs support enabled before TPM sysfs's implicit locking is fixed. | |
296 | */ | |
297 | static int tpm_class_shutdown(struct device *dev) | |
298 | { | |
299 | struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev); | |
300 | ||
db4d8cb9 | 301 | down_write(&chip->ops_sem); |
d1bd4a79 | 302 | if (chip->flags & TPM_CHIP_FLAG_TPM2) { |
47a6c28b | 303 | if (!tpm_chip_start(chip)) { |
a3fbfae8 | 304 | tpm2_shutdown(chip, TPM2_SU_CLEAR); |
47a6c28b | 305 | tpm_chip_stop(chip); |
a3fbfae8 | 306 | } |
d1bd4a79 | 307 | } |
db4d8cb9 VS |
308 | chip->ops = NULL; |
309 | up_write(&chip->ops_sem); | |
7521621e | 310 | |
d1bd4a79 JZ |
311 | return 0; |
312 | } | |
313 | ||
afb5abc2 | 314 | /** |
3897cd9c JG |
315 | * tpm_chip_alloc() - allocate a new struct tpm_chip instance |
316 | * @pdev: device to which the chip is associated | |
317 | * At this point pdev mst be initialized, but does not have to | |
318 | * be registered | |
afb5abc2 JS |
319 | * @ops: struct tpm_class_ops instance |
320 | * | |
321 | * Allocates a new struct tpm_chip instance and assigns a free | |
3897cd9c | 322 | * device number for it. Must be paired with put_device(&chip->dev). |
afb5abc2 | 323 | */ |
2998b02b | 324 | struct tpm_chip *tpm_chip_alloc(struct device *pdev, |
3897cd9c | 325 | const struct tpm_class_ops *ops) |
afb5abc2 JS |
326 | { |
327 | struct tpm_chip *chip; | |
4f3b193d | 328 | int rc; |
afb5abc2 JS |
329 | |
330 | chip = kzalloc(sizeof(*chip), GFP_KERNEL); | |
331 | if (chip == NULL) | |
332 | return ERR_PTR(-ENOMEM); | |
333 | ||
334 | mutex_init(&chip->tpm_mutex); | |
4e26195f | 335 | init_rwsem(&chip->ops_sem); |
afb5abc2 JS |
336 | |
337 | chip->ops = ops; | |
338 | ||
15516788 SB |
339 | mutex_lock(&idr_lock); |
340 | rc = idr_alloc(&dev_nums_idr, NULL, 0, TPM_NUM_DEVICES, GFP_KERNEL); | |
341 | mutex_unlock(&idr_lock); | |
342 | if (rc < 0) { | |
2998b02b | 343 | dev_err(pdev, "No available tpm device numbers\n"); |
afb5abc2 | 344 | kfree(chip); |
15516788 | 345 | return ERR_PTR(rc); |
afb5abc2 | 346 | } |
15516788 | 347 | chip->dev_num = rc; |
afb5abc2 | 348 | |
3635e2ec | 349 | device_initialize(&chip->dev); |
fdc915f7 | 350 | device_initialize(&chip->devs); |
afb5abc2 | 351 | |
313d21ee | 352 | chip->dev.class = tpm_class; |
7521621e | 353 | chip->dev.class->shutdown_pre = tpm_class_shutdown; |
313d21ee | 354 | chip->dev.release = tpm_dev_release; |
2998b02b | 355 | chip->dev.parent = pdev; |
9b774d5c | 356 | chip->dev.groups = chip->groups; |
313d21ee | 357 | |
fdc915f7 JB |
358 | chip->devs.parent = pdev; |
359 | chip->devs.class = tpmrm_class; | |
360 | chip->devs.release = tpm_devs_release; | |
361 | /* get extra reference on main device to hold on | |
362 | * behalf of devs. This holds the chip structure | |
363 | * while cdevs is in use. The corresponding put | |
8979b02a | 364 | * is in the tpm_devs_release (TPM2 only) |
fdc915f7 | 365 | */ |
8979b02a SB |
366 | if (chip->flags & TPM_CHIP_FLAG_TPM2) |
367 | get_device(&chip->dev); | |
fdc915f7 | 368 | |
313d21ee JS |
369 | if (chip->dev_num == 0) |
370 | chip->dev.devt = MKDEV(MISC_MAJOR, TPM_MINOR); | |
371 | else | |
372 | chip->dev.devt = MKDEV(MAJOR(tpm_devt), chip->dev_num); | |
373 | ||
fdc915f7 JB |
374 | chip->devs.devt = |
375 | MKDEV(MAJOR(tpm_devt), chip->dev_num + TPM_NUM_DEVICES); | |
376 | ||
3635e2ec | 377 | rc = dev_set_name(&chip->dev, "tpm%d", chip->dev_num); |
fdc915f7 JB |
378 | if (rc) |
379 | goto out; | |
380 | rc = dev_set_name(&chip->devs, "tpmrm%d", chip->dev_num); | |
3635e2ec JG |
381 | if (rc) |
382 | goto out; | |
313d21ee | 383 | |
2998b02b | 384 | if (!pdev) |
2f9f5377 SB |
385 | chip->flags |= TPM_CHIP_FLAG_VIRTUAL; |
386 | ||
313d21ee | 387 | cdev_init(&chip->cdev, &tpm_fops); |
fdc915f7 | 388 | cdev_init(&chip->cdevs, &tpmrm_fops); |
2072df40 | 389 | chip->cdev.owner = THIS_MODULE; |
fdc915f7 | 390 | chip->cdevs.owner = THIS_MODULE; |
313d21ee | 391 | |
745b361e JS |
392 | chip->work_space.context_buf = kzalloc(PAGE_SIZE, GFP_KERNEL); |
393 | if (!chip->work_space.context_buf) { | |
394 | rc = -ENOMEM; | |
395 | goto out; | |
396 | } | |
4d57856a JB |
397 | chip->work_space.session_buf = kzalloc(PAGE_SIZE, GFP_KERNEL); |
398 | if (!chip->work_space.session_buf) { | |
399 | rc = -ENOMEM; | |
400 | goto out; | |
401 | } | |
745b361e | 402 | |
877c57d0 | 403 | chip->locality = -1; |
3897cd9c JG |
404 | return chip; |
405 | ||
406 | out: | |
fdc915f7 | 407 | put_device(&chip->devs); |
3897cd9c JG |
408 | put_device(&chip->dev); |
409 | return ERR_PTR(rc); | |
410 | } | |
411 | EXPORT_SYMBOL_GPL(tpm_chip_alloc); | |
412 | ||
413 | /** | |
414 | * tpmm_chip_alloc() - allocate a new struct tpm_chip instance | |
415 | * @pdev: parent device to which the chip is associated | |
416 | * @ops: struct tpm_class_ops instance | |
417 | * | |
418 | * Same as tpm_chip_alloc except devm is used to do the put_device | |
419 | */ | |
420 | struct tpm_chip *tpmm_chip_alloc(struct device *pdev, | |
421 | const struct tpm_class_ops *ops) | |
422 | { | |
423 | struct tpm_chip *chip; | |
424 | int rc; | |
425 | ||
426 | chip = tpm_chip_alloc(pdev, ops); | |
427 | if (IS_ERR(chip)) | |
428 | return chip; | |
429 | ||
2b88cd96 SM |
430 | rc = devm_add_action_or_reset(pdev, |
431 | (void (*)(void *)) put_device, | |
432 | &chip->dev); | |
433 | if (rc) | |
4f3b193d | 434 | return ERR_PTR(rc); |
8e0ee3c9 | 435 | |
3897cd9c | 436 | dev_set_drvdata(pdev, chip); |
3635e2ec | 437 | |
3897cd9c | 438 | return chip; |
afb5abc2 JS |
439 | } |
440 | EXPORT_SYMBOL_GPL(tpmm_chip_alloc); | |
441 | ||
72c91ce8 | 442 | static int tpm_add_char_device(struct tpm_chip *chip) |
313d21ee JS |
443 | { |
444 | int rc; | |
445 | ||
8dbbf582 | 446 | rc = cdev_device_add(&chip->cdev, &chip->dev); |
313d21ee JS |
447 | if (rc) { |
448 | dev_err(&chip->dev, | |
8dbbf582 | 449 | "unable to cdev_device_add() %s, major %d, minor %d, err=%d\n", |
3635e2ec | 450 | dev_name(&chip->dev), MAJOR(chip->dev.devt), |
313d21ee | 451 | MINOR(chip->dev.devt), rc); |
313d21ee JS |
452 | return rc; |
453 | } | |
454 | ||
af82455f LT |
455 | if (chip->flags & TPM_CHIP_FLAG_TPM2) { |
456 | rc = cdev_device_add(&chip->cdevs, &chip->devs); | |
457 | if (rc) { | |
458 | dev_err(&chip->devs, | |
459 | "unable to cdev_device_add() %s, major %d, minor %d, err=%d\n", | |
460 | dev_name(&chip->devs), MAJOR(chip->devs.devt), | |
461 | MINOR(chip->devs.devt), rc); | |
462 | return rc; | |
463 | } | |
fdc915f7 JB |
464 | } |
465 | ||
15516788 SB |
466 | /* Make the chip available. */ |
467 | mutex_lock(&idr_lock); | |
468 | idr_replace(&dev_nums_idr, chip, chip->dev_num); | |
469 | mutex_unlock(&idr_lock); | |
470 | ||
313d21ee JS |
471 | return rc; |
472 | } | |
473 | ||
72c91ce8 | 474 | static void tpm_del_char_device(struct tpm_chip *chip) |
313d21ee | 475 | { |
8dbbf582 | 476 | cdev_device_del(&chip->cdev, &chip->dev); |
15516788 SB |
477 | |
478 | /* Make the chip unavailable. */ | |
479 | mutex_lock(&idr_lock); | |
480 | idr_replace(&dev_nums_idr, NULL, chip->dev_num); | |
481 | mutex_unlock(&idr_lock); | |
4e26195f JG |
482 | |
483 | /* Make the driver uncallable. */ | |
484 | down_write(&chip->ops_sem); | |
a3fbfae8 | 485 | if (chip->flags & TPM_CHIP_FLAG_TPM2) { |
47a6c28b | 486 | if (!tpm_chip_start(chip)) { |
a3fbfae8 | 487 | tpm2_shutdown(chip, TPM2_SU_CLEAR); |
47a6c28b | 488 | tpm_chip_stop(chip); |
a3fbfae8 JS |
489 | } |
490 | } | |
4e26195f JG |
491 | chip->ops = NULL; |
492 | up_write(&chip->ops_sem); | |
313d21ee JS |
493 | } |
494 | ||
062807f2 JG |
495 | static void tpm_del_legacy_sysfs(struct tpm_chip *chip) |
496 | { | |
497 | struct attribute **i; | |
498 | ||
2f9f5377 | 499 | if (chip->flags & (TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_VIRTUAL)) |
062807f2 | 500 | return; |
34d47b63 | 501 | |
062807f2 JG |
502 | sysfs_remove_link(&chip->dev.parent->kobj, "ppi"); |
503 | ||
504 | for (i = chip->groups[0]->attrs; *i != NULL; ++i) | |
505 | sysfs_remove_link(&chip->dev.parent->kobj, (*i)->name); | |
34d47b63 JS |
506 | } |
507 | ||
062807f2 JG |
508 | /* For compatibility with legacy sysfs paths we provide symlinks from the |
509 | * parent dev directory to selected names within the tpm chip directory. Old | |
510 | * kernel versions created these files directly under the parent. | |
511 | */ | |
512 | static int tpm_add_legacy_sysfs(struct tpm_chip *chip) | |
513 | { | |
514 | struct attribute **i; | |
515 | int rc; | |
516 | ||
2f9f5377 | 517 | if (chip->flags & (TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_VIRTUAL)) |
062807f2 JG |
518 | return 0; |
519 | ||
520 | rc = __compat_only_sysfs_link_entry_to_kobj( | |
521 | &chip->dev.parent->kobj, &chip->dev.kobj, "ppi"); | |
522 | if (rc && rc != -ENOENT) | |
523 | return rc; | |
524 | ||
525 | /* All the names from tpm-sysfs */ | |
526 | for (i = chip->groups[0]->attrs; *i != NULL; ++i) { | |
527 | rc = __compat_only_sysfs_link_entry_to_kobj( | |
528 | &chip->dev.parent->kobj, &chip->dev.kobj, (*i)->name); | |
529 | if (rc) { | |
530 | tpm_del_legacy_sysfs(chip); | |
531 | return rc; | |
532 | } | |
533 | } | |
534 | ||
535 | return 0; | |
536 | } | |
6e592a06 JG |
537 | |
538 | static int tpm_hwrng_read(struct hwrng *rng, void *data, size_t max, bool wait) | |
539 | { | |
540 | struct tpm_chip *chip = container_of(rng, struct tpm_chip, hwrng); | |
541 | ||
542 | return tpm_get_random(chip, data, max); | |
543 | } | |
544 | ||
545 | static int tpm_add_hwrng(struct tpm_chip *chip) | |
546 | { | |
547 | if (!IS_ENABLED(CONFIG_HW_RANDOM_TPM)) | |
548 | return 0; | |
549 | ||
550 | snprintf(chip->hwrng_name, sizeof(chip->hwrng_name), | |
551 | "tpm-rng-%d", chip->dev_num); | |
552 | chip->hwrng.name = chip->hwrng_name; | |
553 | chip->hwrng.read = tpm_hwrng_read; | |
554 | return hwrng_register(&chip->hwrng); | |
555 | } | |
556 | ||
afb5abc2 | 557 | /* |
313d21ee | 558 | * tpm_chip_register() - create a character device for the TPM chip |
afb5abc2 JS |
559 | * @chip: TPM chip to use. |
560 | * | |
d972b052 JS |
561 | * Creates a character device for the TPM chip and adds sysfs attributes for |
562 | * the device. As the last step this function adds the chip to the list of TPM | |
563 | * chips available for in-kernel use. | |
afb5abc2 | 564 | * |
d972b052 JS |
565 | * This function should be only called after the chip initialization is |
566 | * complete. | |
afb5abc2 JS |
567 | */ |
568 | int tpm_chip_register(struct tpm_chip *chip) | |
569 | { | |
570 | int rc; | |
571 | ||
47a6c28b | 572 | rc = tpm_chip_start(chip); |
a3fbfae8 JS |
573 | if (rc) |
574 | return rc; | |
b03c4370 | 575 | rc = tpm_auto_startup(chip); |
47a6c28b | 576 | tpm_chip_stop(chip); |
b03c4370 TW |
577 | if (rc) |
578 | return rc; | |
cae8b441 | 579 | |
7518a21a JS |
580 | tpm_sysfs_add_device(chip); |
581 | ||
582 | rc = tpm_bios_log_setup(chip); | |
0cf577a0 | 583 | if (rc != 0 && rc != -ENODEV) |
34d47b63 | 584 | return rc; |
afb5abc2 | 585 | |
9b774d5c JS |
586 | tpm_add_ppi(chip); |
587 | ||
6e592a06 JG |
588 | rc = tpm_add_hwrng(chip); |
589 | if (rc) | |
590 | goto out_ppi; | |
591 | ||
72c91ce8 | 592 | rc = tpm_add_char_device(chip); |
6e592a06 JG |
593 | if (rc) |
594 | goto out_hwrng; | |
d972b052 | 595 | |
062807f2 JG |
596 | rc = tpm_add_legacy_sysfs(chip); |
597 | if (rc) { | |
598 | tpm_chip_unregister(chip); | |
599 | return rc; | |
d56e4f75 JS |
600 | } |
601 | ||
afb5abc2 | 602 | return 0; |
6e592a06 JG |
603 | |
604 | out_hwrng: | |
605 | if (IS_ENABLED(CONFIG_HW_RANDOM_TPM)) | |
606 | hwrng_unregister(&chip->hwrng); | |
607 | out_ppi: | |
608 | tpm_bios_log_teardown(chip); | |
609 | ||
610 | return rc; | |
afb5abc2 JS |
611 | } |
612 | EXPORT_SYMBOL_GPL(tpm_chip_register); | |
613 | ||
614 | /* | |
615 | * tpm_chip_unregister() - release the TPM driver | |
616 | * @chip: TPM chip to use. | |
617 | * | |
618 | * Takes the chip first away from the list of available TPM chips and then | |
619 | * cleans up all the resources reserved by tpm_chip_register(). | |
620 | * | |
4e26195f JG |
621 | * Once this function returns the driver call backs in 'op's will not be |
622 | * running and will no longer start. | |
623 | * | |
afb5abc2 JS |
624 | * NOTE: This function should be only called before deinitializing chip |
625 | * resources. | |
626 | */ | |
627 | void tpm_chip_unregister(struct tpm_chip *chip) | |
628 | { | |
062807f2 | 629 | tpm_del_legacy_sysfs(chip); |
6e592a06 JG |
630 | if (IS_ENABLED(CONFIG_HW_RANDOM_TPM)) |
631 | hwrng_unregister(&chip->hwrng); | |
7518a21a | 632 | tpm_bios_log_teardown(chip); |
af82455f LT |
633 | if (chip->flags & TPM_CHIP_FLAG_TPM2) |
634 | cdev_device_del(&chip->cdevs, &chip->devs); | |
72c91ce8 | 635 | tpm_del_char_device(chip); |
afb5abc2 JS |
636 | } |
637 | EXPORT_SYMBOL_GPL(tpm_chip_unregister); |