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 get_device(&chip->dev); in tpm_try_get_ops()
163 down_read(&chip->ops_sem); in tpm_try_get_ops()
164 if (!chip->ops) in tpm_try_get_ops()
167 mutex_lock(&chip->tpm_mutex); in tpm_try_get_ops()
168 rc = tpm_chip_start(chip); in tpm_try_get_ops()
174 mutex_unlock(&chip->tpm_mutex); in tpm_try_get_ops()
176 up_read(&chip->ops_sem); in tpm_try_get_ops()
177 put_device(&chip->dev); in tpm_try_get_ops()
184 * @chip: Chip to put
186 * This is the opposite pair to tpm_try_get_ops(). After this returns chip may
189 void tpm_put_ops(struct tpm_chip *chip) in tpm_put_ops() argument
191 tpm_chip_stop(chip); in tpm_put_ops()
192 mutex_unlock(&chip->tpm_mutex); in tpm_put_ops()
193 up_read(&chip->ops_sem); in tpm_put_ops()
194 put_device(&chip->dev); in tpm_put_ops()
199 * tpm_default_chip() - find a TPM chip and get a reference to it
203 struct tpm_chip *chip, *res = NULL; in tpm_default_chip() local
211 chip = idr_get_next(&dev_nums_idr, &chip_num); in tpm_default_chip()
212 if (chip) { in tpm_default_chip()
213 get_device(&chip->dev); in tpm_default_chip()
214 res = chip; in tpm_default_chip()
226 * tpm_find_get_ops() - find and reserve a TPM chip
227 * @chip: a &struct tpm_chip instance, %NULL for the default chip
229 * Finds a TPM chip and reserves its class device and operations. The chip must
232 * by accepting NULL, but those callers should be converted to pass in a chip
237 * %NULL if a chip is not found.
238 * %NULL if the chip is not available.
240 struct tpm_chip *tpm_find_get_ops(struct tpm_chip *chip) in tpm_find_get_ops() argument
244 if (chip) { in tpm_find_get_ops()
245 if (!tpm_try_get_ops(chip)) in tpm_find_get_ops()
246 return chip; in tpm_find_get_ops()
250 chip = tpm_default_chip(); in tpm_find_get_ops()
251 if (!chip) in tpm_find_get_ops()
253 rc = tpm_try_get_ops(chip); in tpm_find_get_ops()
255 put_device(&chip->dev); in tpm_find_get_ops()
258 return chip; in tpm_find_get_ops()
262 * tpm_dev_release() - free chip memory and the device number
263 * @dev: the character device for the TPM chip
269 struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev); in tpm_dev_release() local
272 idr_remove(&dev_nums_idr, chip->dev_num); in tpm_dev_release()
275 kfree(chip->work_space.context_buf); in tpm_dev_release()
276 kfree(chip->work_space.session_buf); in tpm_dev_release()
277 kfree(chip->allocated_banks); in tpm_dev_release()
278 kfree(chip); in tpm_dev_release()
283 * @dev: device to which the chip is associated.
292 struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev); in tpm_class_shutdown() local
294 down_write(&chip->ops_sem); in tpm_class_shutdown()
295 if (chip->flags & TPM_CHIP_FLAG_TPM2) { in tpm_class_shutdown()
296 if (!tpm_chip_start(chip)) { in tpm_class_shutdown()
297 tpm2_shutdown(chip, TPM2_SU_CLEAR); in tpm_class_shutdown()
298 tpm_chip_stop(chip); in tpm_class_shutdown()
301 chip->ops = NULL; in tpm_class_shutdown()
302 up_write(&chip->ops_sem); in tpm_class_shutdown()
309 * @pdev: device to which the chip is associated
315 * device number for it. Must be paired with put_device(&chip->dev).
320 struct tpm_chip *chip; in tpm_chip_alloc() local
323 chip = kzalloc(sizeof(*chip), GFP_KERNEL); in tpm_chip_alloc()
324 if (chip == NULL) in tpm_chip_alloc()
327 mutex_init(&chip->tpm_mutex); in tpm_chip_alloc()
328 init_rwsem(&chip->ops_sem); in tpm_chip_alloc()
330 chip->ops = ops; in tpm_chip_alloc()
337 kfree(chip); in tpm_chip_alloc()
340 chip->dev_num = rc; in tpm_chip_alloc()
342 device_initialize(&chip->dev); in tpm_chip_alloc()
344 chip->dev.class = &tpm_class; in tpm_chip_alloc()
345 chip->dev.release = tpm_dev_release; in tpm_chip_alloc()
346 chip->dev.parent = pdev; in tpm_chip_alloc()
347 chip->dev.groups = chip->groups; in tpm_chip_alloc()
349 if (chip->dev_num == 0) in tpm_chip_alloc()
350 chip->dev.devt = MKDEV(MISC_MAJOR, TPM_MINOR); in tpm_chip_alloc()
352 chip->dev.devt = MKDEV(MAJOR(tpm_devt), chip->dev_num); in tpm_chip_alloc()
354 rc = dev_set_name(&chip->dev, "tpm%d", chip->dev_num); in tpm_chip_alloc()
359 chip->flags |= TPM_CHIP_FLAG_VIRTUAL; in tpm_chip_alloc()
361 cdev_init(&chip->cdev, &tpm_fops); in tpm_chip_alloc()
362 chip->cdev.owner = THIS_MODULE; in tpm_chip_alloc()
364 rc = tpm2_init_space(&chip->work_space, TPM2_SPACE_BUFFER_SIZE); in tpm_chip_alloc()
370 chip->locality = -1; in tpm_chip_alloc()
371 return chip; in tpm_chip_alloc()
374 put_device(&chip->dev); in tpm_chip_alloc()
386 * @pdev: parent device to which the chip is associated
394 struct tpm_chip *chip; in tpmm_chip_alloc() local
397 chip = tpm_chip_alloc(pdev, ops); in tpmm_chip_alloc()
398 if (IS_ERR(chip)) in tpmm_chip_alloc()
399 return chip; in tpmm_chip_alloc()
403 &chip->dev); in tpmm_chip_alloc()
407 dev_set_drvdata(pdev, chip); in tpmm_chip_alloc()
409 return chip; in tpmm_chip_alloc()
413 static int tpm_add_char_device(struct tpm_chip *chip) in tpm_add_char_device() argument
417 rc = cdev_device_add(&chip->cdev, &chip->dev); in tpm_add_char_device()
419 dev_err(&chip->dev, in tpm_add_char_device()
421 dev_name(&chip->dev), MAJOR(chip->dev.devt), in tpm_add_char_device()
422 MINOR(chip->dev.devt), rc); in tpm_add_char_device()
426 if (chip->flags & TPM_CHIP_FLAG_TPM2 && !tpm_is_firmware_upgrade(chip)) { in tpm_add_char_device()
427 rc = tpm_devs_add(chip); in tpm_add_char_device()
432 /* Make the chip available. */ in tpm_add_char_device()
434 idr_replace(&dev_nums_idr, chip, chip->dev_num); in tpm_add_char_device()
440 cdev_device_del(&chip->cdev, &chip->dev); in tpm_add_char_device()
444 static void tpm_del_char_device(struct tpm_chip *chip) in tpm_del_char_device() argument
446 cdev_device_del(&chip->cdev, &chip->dev); in tpm_del_char_device()
448 /* Make the chip unavailable. */ in tpm_del_char_device()
450 idr_replace(&dev_nums_idr, NULL, chip->dev_num); in tpm_del_char_device()
454 down_write(&chip->ops_sem); in tpm_del_char_device()
457 * Check if chip->ops is still valid: In case that the controller in tpm_del_char_device()
459 * shutdown handler we are called twice and chip->ops to NULL. in tpm_del_char_device()
461 if (chip->ops) { in tpm_del_char_device()
462 if (chip->flags & TPM_CHIP_FLAG_TPM2) { in tpm_del_char_device()
463 if (!tpm_chip_start(chip)) { in tpm_del_char_device()
464 tpm2_shutdown(chip, TPM2_SU_CLEAR); in tpm_del_char_device()
465 tpm_chip_stop(chip); in tpm_del_char_device()
468 chip->ops = NULL; in tpm_del_char_device()
470 up_write(&chip->ops_sem); in tpm_del_char_device()
473 static void tpm_del_legacy_sysfs(struct tpm_chip *chip) in tpm_del_legacy_sysfs() argument
477 if (chip->flags & (TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_VIRTUAL) || in tpm_del_legacy_sysfs()
478 tpm_is_firmware_upgrade(chip)) in tpm_del_legacy_sysfs()
481 sysfs_remove_link(&chip->dev.parent->kobj, "ppi"); in tpm_del_legacy_sysfs()
483 for (i = chip->groups[0]->attrs; *i != NULL; ++i) in tpm_del_legacy_sysfs()
484 sysfs_remove_link(&chip->dev.parent->kobj, (*i)->name); in tpm_del_legacy_sysfs()
488 * parent dev directory to selected names within the tpm chip directory. Old
491 static int tpm_add_legacy_sysfs(struct tpm_chip *chip) in tpm_add_legacy_sysfs() argument
496 if (chip->flags & (TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_VIRTUAL) || in tpm_add_legacy_sysfs()
497 tpm_is_firmware_upgrade(chip)) in tpm_add_legacy_sysfs()
501 &chip->dev.parent->kobj, &chip->dev.kobj, "ppi", NULL); in tpm_add_legacy_sysfs()
506 for (i = chip->groups[0]->attrs; *i != NULL; ++i) { in tpm_add_legacy_sysfs()
508 &chip->dev.parent->kobj, &chip->dev.kobj, (*i)->name, NULL); in tpm_add_legacy_sysfs()
510 tpm_del_legacy_sysfs(chip); in tpm_add_legacy_sysfs()
520 struct tpm_chip *chip = container_of(rng, struct tpm_chip, hwrng); in tpm_hwrng_read() local
522 /* Give back zero bytes, as TPM chip has not yet fully resumed: */ in tpm_hwrng_read()
523 if (chip->flags & TPM_CHIP_FLAG_SUSPENDED) in tpm_hwrng_read()
526 return tpm_get_random(chip, data, max); in tpm_hwrng_read()
529 static bool tpm_is_hwrng_enabled(struct tpm_chip *chip) in tpm_is_hwrng_enabled() argument
533 if (tpm_is_firmware_upgrade(chip)) in tpm_is_hwrng_enabled()
535 if (chip->flags & TPM_CHIP_FLAG_HWRNG_DISABLED) in tpm_is_hwrng_enabled()
540 static int tpm_add_hwrng(struct tpm_chip *chip) in tpm_add_hwrng() argument
542 if (!tpm_is_hwrng_enabled(chip)) in tpm_add_hwrng()
545 snprintf(chip->hwrng_name, sizeof(chip->hwrng_name), in tpm_add_hwrng()
546 "tpm-rng-%d", chip->dev_num); in tpm_add_hwrng()
547 chip->hwrng.name = chip->hwrng_name; in tpm_add_hwrng()
548 chip->hwrng.read = tpm_hwrng_read; in tpm_add_hwrng()
549 return hwrng_register(&chip->hwrng); in tpm_add_hwrng()
552 static int tpm_get_pcr_allocation(struct tpm_chip *chip) in tpm_get_pcr_allocation() argument
556 if (tpm_is_firmware_upgrade(chip)) in tpm_get_pcr_allocation()
559 rc = (chip->flags & TPM_CHIP_FLAG_TPM2) ? in tpm_get_pcr_allocation()
560 tpm2_get_pcr_allocation(chip) : in tpm_get_pcr_allocation()
561 tpm1_get_pcr_allocation(chip); in tpm_get_pcr_allocation()
570 * tpm_chip_bootstrap() - Boostrap TPM chip after power on
571 * @chip: TPM chip to use.
573 * Initialize TPM chip after power on. This a one-shot function: subsequent
576 int tpm_chip_bootstrap(struct tpm_chip *chip) in tpm_chip_bootstrap() argument
580 if (chip->flags & TPM_CHIP_FLAG_BOOTSTRAPPED) in tpm_chip_bootstrap()
583 rc = tpm_chip_start(chip); in tpm_chip_bootstrap()
587 rc = tpm_auto_startup(chip); in tpm_chip_bootstrap()
591 rc = tpm_get_pcr_allocation(chip); in tpm_chip_bootstrap()
593 tpm_chip_stop(chip); in tpm_chip_bootstrap()
599 chip->flags |= TPM_CHIP_FLAG_BOOTSTRAPPED; in tpm_chip_bootstrap()
606 * tpm_chip_register() - create a character device for the TPM chip
607 * @chip: TPM chip to use.
609 * Creates a character device for the TPM chip and adds sysfs attributes for
610 * the device. As the last step this function adds the chip to the list of TPM
613 * This function should be only called after the chip initialization is
616 int tpm_chip_register(struct tpm_chip *chip) in tpm_chip_register() argument
620 rc = tpm_chip_bootstrap(chip); in tpm_chip_register()
624 tpm_sysfs_add_device(chip); in tpm_chip_register()
626 tpm_bios_log_setup(chip); in tpm_chip_register()
628 tpm_add_ppi(chip); in tpm_chip_register()
630 rc = tpm_add_hwrng(chip); in tpm_chip_register()
634 rc = tpm_add_char_device(chip); in tpm_chip_register()
638 rc = tpm_add_legacy_sysfs(chip); in tpm_chip_register()
640 tpm_chip_unregister(chip); in tpm_chip_register()
647 if (tpm_is_hwrng_enabled(chip)) in tpm_chip_register()
648 hwrng_unregister(&chip->hwrng); in tpm_chip_register()
650 tpm_bios_log_teardown(chip); in tpm_chip_register()
658 * @chip: TPM chip to use.
660 * Takes the chip first away from the list of available TPM chips and then
666 * NOTE: This function should be only called before deinitializing chip
669 void tpm_chip_unregister(struct tpm_chip *chip) in tpm_chip_unregister() argument
671 tpm_del_legacy_sysfs(chip); in tpm_chip_unregister()
672 if (tpm_is_hwrng_enabled(chip)) in tpm_chip_unregister()
673 hwrng_unregister(&chip->hwrng); in tpm_chip_unregister()
674 tpm_bios_log_teardown(chip); in tpm_chip_unregister()
675 if (chip->flags & TPM_CHIP_FLAG_TPM2 && !tpm_is_firmware_upgrade(chip)) in tpm_chip_unregister()
676 tpm_devs_remove(chip); in tpm_chip_unregister()
677 tpm_del_char_device(chip); in tpm_chip_unregister()