Lines Matching full:chip

15  * TPM chip management routines.
40 static int tpm_request_locality(struct tpm_chip *chip) in tpm_request_locality() argument
44 if (!chip->ops->request_locality) in tpm_request_locality()
47 rc = chip->ops->request_locality(chip, 0); in tpm_request_locality()
51 chip->locality = rc; in tpm_request_locality()
55 static void tpm_relinquish_locality(struct tpm_chip *chip) in tpm_relinquish_locality() argument
59 if (!chip->ops->relinquish_locality) in tpm_relinquish_locality()
62 rc = chip->ops->relinquish_locality(chip, chip->locality); in tpm_relinquish_locality()
64 dev_err(&chip->dev, "%s: : error %d\n", __func__, rc); in tpm_relinquish_locality()
66 chip->locality = -1; in tpm_relinquish_locality()
69 static int tpm_cmd_ready(struct tpm_chip *chip) in tpm_cmd_ready() argument
71 if (!chip->ops->cmd_ready) in tpm_cmd_ready()
74 return chip->ops->cmd_ready(chip); in tpm_cmd_ready()
77 static int tpm_go_idle(struct tpm_chip *chip) in tpm_go_idle() argument
79 if (!chip->ops->go_idle) in tpm_go_idle()
82 return chip->ops->go_idle(chip); in tpm_go_idle()
85 static void tpm_clk_enable(struct tpm_chip *chip) in tpm_clk_enable() argument
87 if (chip->ops->clk_enable) in tpm_clk_enable()
88 chip->ops->clk_enable(chip, true); in tpm_clk_enable()
91 static void tpm_clk_disable(struct tpm_chip *chip) in tpm_clk_disable() argument
93 if (chip->ops->clk_enable) in tpm_clk_disable()
94 chip->ops->clk_enable(chip, false); in tpm_clk_disable()
99 * @chip: a TPM chip to use
105 int tpm_chip_start(struct tpm_chip *chip) in tpm_chip_start() argument
109 tpm_clk_enable(chip); in tpm_chip_start()
111 if (chip->locality == -1) { in tpm_chip_start()
112 ret = tpm_request_locality(chip); in tpm_chip_start()
114 tpm_clk_disable(chip); in tpm_chip_start()
119 ret = tpm_cmd_ready(chip); in tpm_chip_start()
121 tpm_relinquish_locality(chip); in tpm_chip_start()
122 tpm_clk_disable(chip); in tpm_chip_start()
132 * @chip: a TPM chip to use
138 void tpm_chip_stop(struct tpm_chip *chip) in tpm_chip_stop() argument
140 tpm_go_idle(chip); in tpm_chip_stop()
141 tpm_relinquish_locality(chip); in tpm_chip_stop()
142 tpm_clk_disable(chip); in tpm_chip_stop()
148 * @chip: Chip to ref
150 * The caller must already have some kind of locking to ensure that chip is
151 * valid. This function will lock the chip so that the ops member can be
155 * Returns -ERRNO if the chip could not be got.
157 int tpm_try_get_ops(struct tpm_chip *chip) in tpm_try_get_ops() argument
161 if (chip->flags & TPM_CHIP_FLAG_DISABLE) in tpm_try_get_ops()
164 get_device(&chip->dev); in tpm_try_get_ops()
166 down_read(&chip->ops_sem); in tpm_try_get_ops()
167 if (!chip->ops) in tpm_try_get_ops()
170 mutex_lock(&chip->tpm_mutex); in tpm_try_get_ops()
171 rc = tpm_chip_start(chip); in tpm_try_get_ops()
177 mutex_unlock(&chip->tpm_mutex); in tpm_try_get_ops()
179 up_read(&chip->ops_sem); in tpm_try_get_ops()
180 put_device(&chip->dev); in tpm_try_get_ops()
187 * @chip: Chip to put
189 * This is the opposite pair to tpm_try_get_ops(). After this returns chip may
192 void tpm_put_ops(struct tpm_chip *chip) in tpm_put_ops() argument
194 tpm_chip_stop(chip); in tpm_put_ops()
195 mutex_unlock(&chip->tpm_mutex); in tpm_put_ops()
196 up_read(&chip->ops_sem); in tpm_put_ops()
197 put_device(&chip->dev); in tpm_put_ops()
202 * tpm_default_chip() - find a TPM chip and get a reference to it
206 struct tpm_chip *chip, *res = NULL; in tpm_default_chip() local
214 chip = idr_get_next(&dev_nums_idr, &chip_num); in tpm_default_chip()
215 if (chip) { in tpm_default_chip()
216 get_device(&chip->dev); in tpm_default_chip()
217 res = chip; in tpm_default_chip()
229 * tpm_find_get_ops() - find and reserve a TPM chip
230 * @chip: a &struct tpm_chip instance, %NULL for the default chip
232 * Finds a TPM chip and reserves its class device and operations. The chip must
235 * by accepting NULL, but those callers should be converted to pass in a chip
240 * %NULL if a chip is not found.
241 * %NULL if the chip is not available.
243 struct tpm_chip *tpm_find_get_ops(struct tpm_chip *chip) in tpm_find_get_ops() argument
247 if (chip) { in tpm_find_get_ops()
248 if (!tpm_try_get_ops(chip)) in tpm_find_get_ops()
249 return chip; in tpm_find_get_ops()
253 chip = tpm_default_chip(); in tpm_find_get_ops()
254 if (!chip) in tpm_find_get_ops()
256 rc = tpm_try_get_ops(chip); in tpm_find_get_ops()
258 put_device(&chip->dev); in tpm_find_get_ops()
261 return chip; in tpm_find_get_ops()
265 * tpm_dev_release() - free chip memory and the device number
266 * @dev: the character device for the TPM chip
272 struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev); in tpm_dev_release() local
275 idr_remove(&dev_nums_idr, chip->dev_num); in tpm_dev_release()
278 kfree(chip->work_space.context_buf); in tpm_dev_release()
279 kfree(chip->work_space.session_buf); in tpm_dev_release()
280 kfree(chip->allocated_banks); in tpm_dev_release()
282 kfree(chip->auth); in tpm_dev_release()
284 kfree(chip); in tpm_dev_release()
289 * @dev: device to which the chip is associated.
298 struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev); in tpm_class_shutdown() local
300 down_write(&chip->ops_sem); in tpm_class_shutdown()
301 if (chip->flags & TPM_CHIP_FLAG_TPM2) { in tpm_class_shutdown()
302 if (!tpm_chip_start(chip)) { in tpm_class_shutdown()
303 tpm2_shutdown(chip, TPM2_SU_CLEAR); in tpm_class_shutdown()
304 tpm_chip_stop(chip); in tpm_class_shutdown()
307 chip->ops = NULL; in tpm_class_shutdown()
308 up_write(&chip->ops_sem); in tpm_class_shutdown()
315 * @pdev: device to which the chip is associated
321 * device number for it. Must be paired with put_device(&chip->dev).
326 struct tpm_chip *chip; in tpm_chip_alloc() local
329 chip = kzalloc(sizeof(*chip), GFP_KERNEL); in tpm_chip_alloc()
330 if (chip == NULL) in tpm_chip_alloc()
333 mutex_init(&chip->tpm_mutex); in tpm_chip_alloc()
334 init_rwsem(&chip->ops_sem); in tpm_chip_alloc()
336 chip->ops = ops; in tpm_chip_alloc()
343 kfree(chip); in tpm_chip_alloc()
346 chip->dev_num = rc; in tpm_chip_alloc()
348 device_initialize(&chip->dev); in tpm_chip_alloc()
350 chip->dev.class = &tpm_class; in tpm_chip_alloc()
351 chip->dev.release = tpm_dev_release; in tpm_chip_alloc()
352 chip->dev.parent = pdev; in tpm_chip_alloc()
353 chip->dev.groups = chip->groups; in tpm_chip_alloc()
355 if (chip->dev_num == 0) in tpm_chip_alloc()
356 chip->dev.devt = MKDEV(MISC_MAJOR, TPM_MINOR); in tpm_chip_alloc()
358 chip->dev.devt = MKDEV(MAJOR(tpm_devt), chip->dev_num); in tpm_chip_alloc()
360 rc = dev_set_name(&chip->dev, "tpm%d", chip->dev_num); in tpm_chip_alloc()
365 chip->flags |= TPM_CHIP_FLAG_VIRTUAL; in tpm_chip_alloc()
367 cdev_init(&chip->cdev, &tpm_fops); in tpm_chip_alloc()
368 chip->cdev.owner = THIS_MODULE; in tpm_chip_alloc()
370 rc = tpm2_init_space(&chip->work_space, TPM2_SPACE_BUFFER_SIZE); in tpm_chip_alloc()
376 chip->locality = -1; in tpm_chip_alloc()
377 return chip; in tpm_chip_alloc()
380 put_device(&chip->dev); in tpm_chip_alloc()
392 * @pdev: parent device to which the chip is associated
400 struct tpm_chip *chip; in tpmm_chip_alloc() local
403 chip = tpm_chip_alloc(pdev, ops); in tpmm_chip_alloc()
404 if (IS_ERR(chip)) in tpmm_chip_alloc()
405 return chip; in tpmm_chip_alloc()
409 &chip->dev); in tpmm_chip_alloc()
413 dev_set_drvdata(pdev, chip); in tpmm_chip_alloc()
415 return chip; in tpmm_chip_alloc()
419 static int tpm_add_char_device(struct tpm_chip *chip) in tpm_add_char_device() argument
423 rc = cdev_device_add(&chip->cdev, &chip->dev); in tpm_add_char_device()
425 dev_err(&chip->dev, in tpm_add_char_device()
427 dev_name(&chip->dev), MAJOR(chip->dev.devt), in tpm_add_char_device()
428 MINOR(chip->dev.devt), rc); in tpm_add_char_device()
432 if (chip->flags & TPM_CHIP_FLAG_TPM2 && !tpm_is_firmware_upgrade(chip)) { in tpm_add_char_device()
433 rc = tpm_devs_add(chip); in tpm_add_char_device()
438 /* Make the chip available. */ in tpm_add_char_device()
440 idr_replace(&dev_nums_idr, chip, chip->dev_num); in tpm_add_char_device()
446 cdev_device_del(&chip->cdev, &chip->dev); in tpm_add_char_device()
450 static void tpm_del_char_device(struct tpm_chip *chip) in tpm_del_char_device() argument
452 cdev_device_del(&chip->cdev, &chip->dev); in tpm_del_char_device()
454 /* Make the chip unavailable. */ in tpm_del_char_device()
456 idr_replace(&dev_nums_idr, NULL, chip->dev_num); in tpm_del_char_device()
460 down_write(&chip->ops_sem); in tpm_del_char_device()
463 * Check if chip->ops is still valid: In case that the controller in tpm_del_char_device()
465 * shutdown handler we are called twice and chip->ops to NULL. in tpm_del_char_device()
467 if (chip->ops) { in tpm_del_char_device()
468 if (chip->flags & TPM_CHIP_FLAG_TPM2) { in tpm_del_char_device()
469 if (!tpm_chip_start(chip)) { in tpm_del_char_device()
470 tpm2_shutdown(chip, TPM2_SU_CLEAR); in tpm_del_char_device()
471 tpm_chip_stop(chip); in tpm_del_char_device()
474 chip->ops = NULL; in tpm_del_char_device()
476 up_write(&chip->ops_sem); in tpm_del_char_device()
479 static void tpm_del_legacy_sysfs(struct tpm_chip *chip) in tpm_del_legacy_sysfs() argument
483 if (chip->flags & (TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_VIRTUAL) || in tpm_del_legacy_sysfs()
484 tpm_is_firmware_upgrade(chip)) in tpm_del_legacy_sysfs()
487 sysfs_remove_link(&chip->dev.parent->kobj, "ppi"); in tpm_del_legacy_sysfs()
489 for (i = chip->groups[0]->attrs; *i != NULL; ++i) in tpm_del_legacy_sysfs()
490 sysfs_remove_link(&chip->dev.parent->kobj, (*i)->name); in tpm_del_legacy_sysfs()
494 * parent dev directory to selected names within the tpm chip directory. Old
497 static int tpm_add_legacy_sysfs(struct tpm_chip *chip) in tpm_add_legacy_sysfs() argument
502 if (chip->flags & (TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_VIRTUAL) || in tpm_add_legacy_sysfs()
503 tpm_is_firmware_upgrade(chip)) in tpm_add_legacy_sysfs()
507 &chip->dev.parent->kobj, &chip->dev.kobj, "ppi", NULL); in tpm_add_legacy_sysfs()
512 for (i = chip->groups[0]->attrs; *i != NULL; ++i) { in tpm_add_legacy_sysfs()
514 &chip->dev.parent->kobj, &chip->dev.kobj, (*i)->name, NULL); in tpm_add_legacy_sysfs()
516 tpm_del_legacy_sysfs(chip); in tpm_add_legacy_sysfs()
526 struct tpm_chip *chip = container_of(rng, struct tpm_chip, hwrng); in tpm_hwrng_read() local
528 return tpm_get_random(chip, data, max); in tpm_hwrng_read()
531 static bool tpm_is_hwrng_enabled(struct tpm_chip *chip) in tpm_is_hwrng_enabled() argument
535 if (tpm_is_firmware_upgrade(chip)) in tpm_is_hwrng_enabled()
537 if (chip->flags & TPM_CHIP_FLAG_HWRNG_DISABLED) in tpm_is_hwrng_enabled()
542 static int tpm_add_hwrng(struct tpm_chip *chip) in tpm_add_hwrng() argument
544 if (!tpm_is_hwrng_enabled(chip)) in tpm_add_hwrng()
547 snprintf(chip->hwrng_name, sizeof(chip->hwrng_name), in tpm_add_hwrng()
548 "tpm-rng-%d", chip->dev_num); in tpm_add_hwrng()
549 chip->hwrng.name = chip->hwrng_name; in tpm_add_hwrng()
550 chip->hwrng.read = tpm_hwrng_read; in tpm_add_hwrng()
551 return hwrng_register(&chip->hwrng); in tpm_add_hwrng()
554 static int tpm_get_pcr_allocation(struct tpm_chip *chip) in tpm_get_pcr_allocation() argument
558 if (tpm_is_firmware_upgrade(chip)) in tpm_get_pcr_allocation()
561 rc = (chip->flags & TPM_CHIP_FLAG_TPM2) ? in tpm_get_pcr_allocation()
562 tpm2_get_pcr_allocation(chip) : in tpm_get_pcr_allocation()
563 tpm1_get_pcr_allocation(chip); in tpm_get_pcr_allocation()
572 * tpm_chip_bootstrap() - Boostrap TPM chip after power on
573 * @chip: TPM chip to use.
575 * Initialize TPM chip after power on. This a one-shot function: subsequent
578 int tpm_chip_bootstrap(struct tpm_chip *chip) in tpm_chip_bootstrap() argument
582 if (chip->flags & TPM_CHIP_FLAG_BOOTSTRAPPED) in tpm_chip_bootstrap()
585 rc = tpm_chip_start(chip); in tpm_chip_bootstrap()
589 rc = tpm_auto_startup(chip); in tpm_chip_bootstrap()
593 rc = tpm_get_pcr_allocation(chip); in tpm_chip_bootstrap()
595 tpm_chip_stop(chip); in tpm_chip_bootstrap()
601 chip->flags |= TPM_CHIP_FLAG_BOOTSTRAPPED; in tpm_chip_bootstrap()
608 * tpm_chip_register() - create a character device for the TPM chip
609 * @chip: TPM chip to use.
611 * Creates a character device for the TPM chip and adds sysfs attributes for
612 * the device. As the last step this function adds the chip to the list of TPM
615 * This function should be only called after the chip initialization is
618 int tpm_chip_register(struct tpm_chip *chip) in tpm_chip_register() argument
622 rc = tpm_chip_bootstrap(chip); in tpm_chip_register()
626 tpm_sysfs_add_device(chip); in tpm_chip_register()
628 tpm_bios_log_setup(chip); in tpm_chip_register()
630 tpm_add_ppi(chip); in tpm_chip_register()
632 rc = tpm_add_hwrng(chip); in tpm_chip_register()
636 rc = tpm_add_char_device(chip); in tpm_chip_register()
640 rc = tpm_add_legacy_sysfs(chip); in tpm_chip_register()
642 tpm_chip_unregister(chip); in tpm_chip_register()
649 if (tpm_is_hwrng_enabled(chip)) in tpm_chip_register()
650 hwrng_unregister(&chip->hwrng); in tpm_chip_register()
652 tpm_bios_log_teardown(chip); in tpm_chip_register()
660 * @chip: TPM chip to use.
662 * Takes the chip first away from the list of available TPM chips and then
668 * NOTE: This function should be only called before deinitializing chip
671 void tpm_chip_unregister(struct tpm_chip *chip) in tpm_chip_unregister() argument
676 rc = tpm_try_get_ops(chip); in tpm_chip_unregister()
678 tpm2_end_auth_session(chip); in tpm_chip_unregister()
679 tpm_put_ops(chip); in tpm_chip_unregister()
683 tpm_del_legacy_sysfs(chip); in tpm_chip_unregister()
684 if (tpm_is_hwrng_enabled(chip)) in tpm_chip_unregister()
685 hwrng_unregister(&chip->hwrng); in tpm_chip_unregister()
686 tpm_bios_log_teardown(chip); in tpm_chip_unregister()
687 if (chip->flags & TPM_CHIP_FLAG_TPM2 && !tpm_is_firmware_upgrade(chip)) in tpm_chip_unregister()
688 tpm_devs_remove(chip); in tpm_chip_unregister()
689 tpm_del_char_device(chip); in tpm_chip_unregister()