Linux Audio

Check our new training course

Loading...
v6.8
  1// SPDX-License-Identifier: GPL-2.0-only
  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.
 
 
 
 
 
 
 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>
 23#include <linux/major.h>
 24#include <linux/tpm_eventlog.h>
 25#include <linux/hw_random.h>
 26#include "tpm.h"
 
 27
 28DEFINE_IDR(dev_nums_idr);
 29static DEFINE_MUTEX(idr_lock);
 30
 31const struct class tpm_class = {
 32	.name = "tpm",
 33	.shutdown_pre = tpm_class_shutdown,
 34};
 35const struct class tpmrm_class = {
 36	.name = "tpmrm",
 37};
 38dev_t tpm_devt;
 39
 40static int tpm_request_locality(struct tpm_chip *chip)
 41{
 42	int rc;
 43
 44	if (!chip->ops->request_locality)
 45		return 0;
 46
 47	rc = chip->ops->request_locality(chip, 0);
 48	if (rc < 0)
 49		return rc;
 50
 51	chip->locality = rc;
 52	return 0;
 53}
 54
 55static void tpm_relinquish_locality(struct tpm_chip *chip)
 56{
 57	int rc;
 58
 59	if (!chip->ops->relinquish_locality)
 60		return;
 61
 62	rc = chip->ops->relinquish_locality(chip, chip->locality);
 63	if (rc)
 64		dev_err(&chip->dev, "%s: : error %d\n", __func__, rc);
 65
 66	chip->locality = -1;
 67}
 68
 69static int tpm_cmd_ready(struct tpm_chip *chip)
 70{
 71	if (!chip->ops->cmd_ready)
 72		return 0;
 73
 74	return chip->ops->cmd_ready(chip);
 75}
 76
 77static int tpm_go_idle(struct tpm_chip *chip)
 78{
 79	if (!chip->ops->go_idle)
 80		return 0;
 81
 82	return chip->ops->go_idle(chip);
 83}
 84
 85static void tpm_clk_enable(struct tpm_chip *chip)
 86{
 87	if (chip->ops->clk_enable)
 88		chip->ops->clk_enable(chip, true);
 89}
 90
 91static void tpm_clk_disable(struct tpm_chip *chip)
 92{
 93	if (chip->ops->clk_enable)
 94		chip->ops->clk_enable(chip, false);
 95}
 96
 97/**
 98 * tpm_chip_start() - power on the TPM
 99 * @chip:	a TPM chip to use
100 *
101 * Return:
102 * * The response length	- OK
103 * * -errno			- A system error
104 */
105int tpm_chip_start(struct tpm_chip *chip)
106{
107	int ret;
108
109	tpm_clk_enable(chip);
110
111	if (chip->locality == -1) {
112		ret = tpm_request_locality(chip);
113		if (ret) {
114			tpm_clk_disable(chip);
115			return ret;
116		}
117	}
118
119	ret = tpm_cmd_ready(chip);
120	if (ret) {
121		tpm_relinquish_locality(chip);
122		tpm_clk_disable(chip);
123		return ret;
124	}
125
126	return 0;
127}
128EXPORT_SYMBOL_GPL(tpm_chip_start);
129
130/**
131 * tpm_chip_stop() - power off the TPM
132 * @chip:	a TPM chip to use
133 *
134 * Return:
135 * * The response length	- OK
136 * * -errno			- A system error
137 */
138void tpm_chip_stop(struct tpm_chip *chip)
139{
140	tpm_go_idle(chip);
141	tpm_relinquish_locality(chip);
142	tpm_clk_disable(chip);
143}
144EXPORT_SYMBOL_GPL(tpm_chip_stop);
145
146/**
147 * tpm_try_get_ops() - Get a ref to the tpm_chip
148 * @chip: Chip to ref
149 *
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
152 * accessed safely. The locking prevents tpm_chip_unregister from
153 * completing, so it should not be held for long periods.
154 *
155 * Returns -ERRNO if the chip could not be got.
156 */
157int tpm_try_get_ops(struct tpm_chip *chip)
158{
159	int rc = -EIO;
160
161	get_device(&chip->dev);
162
163	down_read(&chip->ops_sem);
164	if (!chip->ops)
165		goto out_ops;
166
167	mutex_lock(&chip->tpm_mutex);
168	rc = tpm_chip_start(chip);
169	if (rc)
170		goto out_lock;
171
172	return 0;
173out_lock:
174	mutex_unlock(&chip->tpm_mutex);
175out_ops:
176	up_read(&chip->ops_sem);
177	put_device(&chip->dev);
178	return rc;
179}
180EXPORT_SYMBOL_GPL(tpm_try_get_ops);
181
182/**
183 * tpm_put_ops() - Release a ref to the tpm_chip
184 * @chip: Chip to put
185 *
186 * This is the opposite pair to tpm_try_get_ops(). After this returns chip may
187 * be kfree'd.
188 */
189void tpm_put_ops(struct tpm_chip *chip)
190{
191	tpm_chip_stop(chip);
192	mutex_unlock(&chip->tpm_mutex);
193	up_read(&chip->ops_sem);
194	put_device(&chip->dev);
195}
196EXPORT_SYMBOL_GPL(tpm_put_ops);
197
198/**
199 * tpm_default_chip() - find a TPM chip and get a reference to it
 
 
 
 
200 */
201struct tpm_chip *tpm_default_chip(void)
202{
203	struct tpm_chip *chip, *res = NULL;
204	int chip_num = 0;
205	int chip_prev;
206
207	mutex_lock(&idr_lock);
208
209	do {
210		chip_prev = chip_num;
211		chip = idr_get_next(&dev_nums_idr, &chip_num);
212		if (chip) {
213			get_device(&chip->dev);
 
 
 
 
 
 
 
 
214			res = chip;
215			break;
216		}
217	} while (chip_prev != chip_num);
218
219	mutex_unlock(&idr_lock);
220
221	return res;
222}
223EXPORT_SYMBOL_GPL(tpm_default_chip);
224
225/**
226 * tpm_find_get_ops() - find and reserve a TPM chip
227 * @chip:	a &struct tpm_chip instance, %NULL for the default chip
228 *
229 * Finds a TPM chip and reserves its class device and operations. The chip must
230 * be released with tpm_put_ops() after use.
231 * This function is for internal use only. It supports existing TPM callers
232 * by accepting NULL, but those callers should be converted to pass in a chip
233 * directly.
234 *
235 * Return:
236 * A reserved &struct tpm_chip instance.
237 * %NULL if a chip is not found.
238 * %NULL if the chip is not available.
239 */
240struct tpm_chip *tpm_find_get_ops(struct tpm_chip *chip)
241{
242	int rc;
243
244	if (chip) {
245		if (!tpm_try_get_ops(chip))
246			return chip;
247		return NULL;
248	}
249
250	chip = tpm_default_chip();
251	if (!chip)
252		return NULL;
253	rc = tpm_try_get_ops(chip);
254	/* release additional reference we got from tpm_default_chip() */
255	put_device(&chip->dev);
256	if (rc)
257		return NULL;
258	return chip;
259}
260
261/**
262 * tpm_dev_release() - free chip memory and the device number
263 * @dev: the character device for the TPM chip
264 *
265 * This is used as the release function for the character device.
266 */
267static void tpm_dev_release(struct device *dev)
268{
269	struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev);
270
271	mutex_lock(&idr_lock);
272	idr_remove(&dev_nums_idr, chip->dev_num);
273	mutex_unlock(&idr_lock);
274
275	kfree(chip->work_space.context_buf);
276	kfree(chip->work_space.session_buf);
277	kfree(chip->allocated_banks);
278	kfree(chip);
279}
280
281/**
282 * tpm_class_shutdown() - prepare the TPM device for loss of power.
283 * @dev: device to which the chip is associated.
284 *
285 * Issues a TPM2_Shutdown command prior to loss of power, as required by the
286 * TPM 2.0 spec. Then, calls bus- and device- specific shutdown code.
287 *
288 * Return: always 0 (i.e. success)
289 */
290int tpm_class_shutdown(struct device *dev)
291{
292	struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev);
293
294	down_write(&chip->ops_sem);
295	if (chip->flags & TPM_CHIP_FLAG_TPM2) {
296		if (!tpm_chip_start(chip)) {
297			tpm2_shutdown(chip, TPM2_SU_CLEAR);
298			tpm_chip_stop(chip);
299		}
300	}
301	chip->ops = NULL;
302	up_write(&chip->ops_sem);
303
304	return 0;
305}
306
307/**
308 * tpm_chip_alloc() - allocate a new struct tpm_chip instance
309 * @pdev: device to which the chip is associated
310 *        At this point pdev mst be initialized, but does not have to
311 *        be registered
312 * @ops: struct tpm_class_ops instance
313 *
314 * Allocates a new struct tpm_chip instance and assigns a free
315 * device number for it. Must be paired with put_device(&chip->dev).
316 */
317struct tpm_chip *tpm_chip_alloc(struct device *pdev,
318				const struct tpm_class_ops *ops)
319{
320	struct tpm_chip *chip;
321	int rc;
322
323	chip = kzalloc(sizeof(*chip), GFP_KERNEL);
324	if (chip == NULL)
325		return ERR_PTR(-ENOMEM);
326
327	mutex_init(&chip->tpm_mutex);
328	init_rwsem(&chip->ops_sem);
329
330	chip->ops = ops;
331
332	mutex_lock(&idr_lock);
333	rc = idr_alloc(&dev_nums_idr, NULL, 0, TPM_NUM_DEVICES, GFP_KERNEL);
334	mutex_unlock(&idr_lock);
335	if (rc < 0) {
336		dev_err(pdev, "No available tpm device numbers\n");
337		kfree(chip);
338		return ERR_PTR(rc);
339	}
340	chip->dev_num = rc;
341
342	device_initialize(&chip->dev);
343
344	chip->dev.class = &tpm_class;
345	chip->dev.release = tpm_dev_release;
346	chip->dev.parent = pdev;
347	chip->dev.groups = chip->groups;
348
349	if (chip->dev_num == 0)
350		chip->dev.devt = MKDEV(MISC_MAJOR, TPM_MINOR);
351	else
352		chip->dev.devt = MKDEV(MAJOR(tpm_devt), chip->dev_num);
353
354	rc = dev_set_name(&chip->dev, "tpm%d", chip->dev_num);
355	if (rc)
356		goto out;
357
358	if (!pdev)
359		chip->flags |= TPM_CHIP_FLAG_VIRTUAL;
360
361	cdev_init(&chip->cdev, &tpm_fops);
362	chip->cdev.owner = THIS_MODULE;
 
363
364	rc = tpm2_init_space(&chip->work_space, TPM2_SPACE_BUFFER_SIZE);
365	if (rc) {
366		rc = -ENOMEM;
367		goto out;
368	}
369
370	chip->locality = -1;
371	return chip;
372
373out:
374	put_device(&chip->dev);
375	return ERR_PTR(rc);
376}
377EXPORT_SYMBOL_GPL(tpm_chip_alloc);
378
379static void tpm_put_device(void *dev)
380{
381	put_device(dev);
382}
383
384/**
385 * tpmm_chip_alloc() - allocate a new struct tpm_chip instance
386 * @pdev: parent device to which the chip is associated
387 * @ops: struct tpm_class_ops instance
388 *
389 * Same as tpm_chip_alloc except devm is used to do the put_device
390 */
391struct tpm_chip *tpmm_chip_alloc(struct device *pdev,
392				 const struct tpm_class_ops *ops)
393{
394	struct tpm_chip *chip;
395	int rc;
396
397	chip = tpm_chip_alloc(pdev, ops);
398	if (IS_ERR(chip))
399		return chip;
400
401	rc = devm_add_action_or_reset(pdev,
402				      tpm_put_device,
403				      &chip->dev);
404	if (rc)
405		return ERR_PTR(rc);
406
407	dev_set_drvdata(pdev, chip);
408
409	return chip;
410}
411EXPORT_SYMBOL_GPL(tpmm_chip_alloc);
412
413static int tpm_add_char_device(struct tpm_chip *chip)
414{
415	int rc;
416
417	rc = cdev_device_add(&chip->cdev, &chip->dev);
418	if (rc) {
419		dev_err(&chip->dev,
420			"unable to cdev_device_add() %s, major %d, minor %d, err=%d\n",
421			dev_name(&chip->dev), MAJOR(chip->dev.devt),
422			MINOR(chip->dev.devt), rc);
 
423		return rc;
424	}
425
426	if (chip->flags & TPM_CHIP_FLAG_TPM2 && !tpm_is_firmware_upgrade(chip)) {
427		rc = tpm_devs_add(chip);
428		if (rc)
429			goto err_del_cdev;
 
 
 
 
 
430	}
431
432	/* Make the chip available. */
433	mutex_lock(&idr_lock);
434	idr_replace(&dev_nums_idr, chip, chip->dev_num);
435	mutex_unlock(&idr_lock);
436
437	return 0;
438
439err_del_cdev:
440	cdev_device_del(&chip->cdev, &chip->dev);
441	return rc;
442}
443
444static void tpm_del_char_device(struct tpm_chip *chip)
445{
446	cdev_device_del(&chip->cdev, &chip->dev);
 
447
448	/* Make the chip unavailable. */
449	mutex_lock(&idr_lock);
450	idr_replace(&dev_nums_idr, NULL, chip->dev_num);
451	mutex_unlock(&idr_lock);
452
453	/* Make the driver uncallable. */
454	down_write(&chip->ops_sem);
455
456	/*
457	 * Check if chip->ops is still valid: In case that the controller
458	 * drivers shutdown handler unregisters the controller in its
459	 * shutdown handler we are called twice and chip->ops to NULL.
460	 */
461	if (chip->ops) {
462		if (chip->flags & TPM_CHIP_FLAG_TPM2) {
463			if (!tpm_chip_start(chip)) {
464				tpm2_shutdown(chip, TPM2_SU_CLEAR);
465				tpm_chip_stop(chip);
466			}
467		}
468		chip->ops = NULL;
469	}
470	up_write(&chip->ops_sem);
471}
472
473static void tpm_del_legacy_sysfs(struct tpm_chip *chip)
474{
475	struct attribute **i;
476
477	if (chip->flags & (TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_VIRTUAL) ||
478	    tpm_is_firmware_upgrade(chip))
479		return;
480
481	sysfs_remove_link(&chip->dev.parent->kobj, "ppi");
482
483	for (i = chip->groups[0]->attrs; *i != NULL; ++i)
484		sysfs_remove_link(&chip->dev.parent->kobj, (*i)->name);
485}
486
487/* For compatibility with legacy sysfs paths we provide symlinks from the
488 * parent dev directory to selected names within the tpm chip directory. Old
489 * kernel versions created these files directly under the parent.
490 */
491static int tpm_add_legacy_sysfs(struct tpm_chip *chip)
492{
493	struct attribute **i;
494	int rc;
495
496	if (chip->flags & (TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_VIRTUAL) ||
497		tpm_is_firmware_upgrade(chip))
498		return 0;
499
500	rc = compat_only_sysfs_link_entry_to_kobj(
501		    &chip->dev.parent->kobj, &chip->dev.kobj, "ppi", NULL);
502	if (rc && rc != -ENOENT)
503		return rc;
504
505	/* All the names from tpm-sysfs */
506	for (i = chip->groups[0]->attrs; *i != NULL; ++i) {
507		rc = compat_only_sysfs_link_entry_to_kobj(
508		    &chip->dev.parent->kobj, &chip->dev.kobj, (*i)->name, NULL);
509		if (rc) {
510			tpm_del_legacy_sysfs(chip);
511			return rc;
512		}
513	}
514
515	return 0;
516}
517
518static int tpm_hwrng_read(struct hwrng *rng, void *data, size_t max, bool wait)
519{
520	struct tpm_chip *chip = container_of(rng, struct tpm_chip, hwrng);
521
522	/* Give back zero bytes, as TPM chip has not yet fully resumed: */
523	if (chip->flags & TPM_CHIP_FLAG_SUSPENDED)
524		return 0;
525
526	return tpm_get_random(chip, data, max);
527}
528
529static bool tpm_is_hwrng_enabled(struct tpm_chip *chip)
530{
531	if (!IS_ENABLED(CONFIG_HW_RANDOM_TPM))
532		return false;
533	if (tpm_is_firmware_upgrade(chip))
534		return false;
535	if (chip->flags & TPM_CHIP_FLAG_HWRNG_DISABLED)
536		return false;
537	return true;
538}
539
540static int tpm_add_hwrng(struct tpm_chip *chip)
541{
542	if (!tpm_is_hwrng_enabled(chip))
543		return 0;
544
545	snprintf(chip->hwrng_name, sizeof(chip->hwrng_name),
546		 "tpm-rng-%d", chip->dev_num);
547	chip->hwrng.name = chip->hwrng_name;
548	chip->hwrng.read = tpm_hwrng_read;
549	return hwrng_register(&chip->hwrng);
550}
551
552static int tpm_get_pcr_allocation(struct tpm_chip *chip)
553{
554	int rc;
555
556	if (tpm_is_firmware_upgrade(chip))
557		return 0;
558
559	rc = (chip->flags & TPM_CHIP_FLAG_TPM2) ?
560	     tpm2_get_pcr_allocation(chip) :
561	     tpm1_get_pcr_allocation(chip);
562
563	if (rc > 0)
564		return -ENODEV;
565
566	return rc;
567}
568
569/*
570 * tpm_chip_bootstrap() - Boostrap TPM chip after power on
571 * @chip: TPM chip to use.
572 *
573 * Initialize TPM chip after power on. This a one-shot function: subsequent
574 * calls will have no effect.
575 */
576int tpm_chip_bootstrap(struct tpm_chip *chip)
577{
578	int rc;
579
580	if (chip->flags & TPM_CHIP_FLAG_BOOTSTRAPPED)
581		return 0;
582
583	rc = tpm_chip_start(chip);
584	if (rc)
585		return rc;
586
587	rc = tpm_auto_startup(chip);
588	if (rc)
589		goto stop;
590
591	rc = tpm_get_pcr_allocation(chip);
592stop:
593	tpm_chip_stop(chip);
594
595	/*
596	 * Unconditionally set, as driver initialization should cease, when the
597	 * boostrapping process fails.
598	 */
599	chip->flags |= TPM_CHIP_FLAG_BOOTSTRAPPED;
600
601	return rc;
602}
603EXPORT_SYMBOL_GPL(tpm_chip_bootstrap);
604
605/*
606 * tpm_chip_register() - create a character device for the TPM chip
607 * @chip: TPM chip to use.
608 *
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
611 * chips available for in-kernel use.
612 *
613 * This function should be only called after the chip initialization is
614 * complete.
615 */
616int tpm_chip_register(struct tpm_chip *chip)
617{
618	int rc;
619
620	rc = tpm_chip_bootstrap(chip);
621	if (rc)
622		return rc;
 
 
 
 
 
623
624	tpm_sysfs_add_device(chip);
625
626	tpm_bios_log_setup(chip);
 
 
627
628	tpm_add_ppi(chip);
629
630	rc = tpm_add_hwrng(chip);
631	if (rc)
632		goto out_ppi;
633
634	rc = tpm_add_char_device(chip);
635	if (rc)
636		goto out_hwrng;
 
 
637
638	rc = tpm_add_legacy_sysfs(chip);
639	if (rc) {
640		tpm_chip_unregister(chip);
641		return rc;
642	}
643
644	return 0;
645
646out_hwrng:
647	if (tpm_is_hwrng_enabled(chip))
648		hwrng_unregister(&chip->hwrng);
649out_ppi:
650	tpm_bios_log_teardown(chip);
651
652	return rc;
653}
654EXPORT_SYMBOL_GPL(tpm_chip_register);
655
656/*
657 * tpm_chip_unregister() - release the TPM driver
658 * @chip: TPM chip to use.
659 *
660 * Takes the chip first away from the list of available TPM chips and then
661 * cleans up all the resources reserved by tpm_chip_register().
662 *
663 * Once this function returns the driver call backs in 'op's will not be
664 * running and will no longer start.
665 *
666 * NOTE: This function should be only called before deinitializing chip
667 * resources.
668 */
669void tpm_chip_unregister(struct tpm_chip *chip)
670{
671	tpm_del_legacy_sysfs(chip);
672	if (tpm_is_hwrng_enabled(chip))
673		hwrng_unregister(&chip->hwrng);
674	tpm_bios_log_teardown(chip);
675	if (chip->flags & TPM_CHIP_FLAG_TPM2 && !tpm_is_firmware_upgrade(chip))
676		tpm_devs_remove(chip);
677	tpm_del_char_device(chip);
678}
679EXPORT_SYMBOL_GPL(tpm_chip_unregister);
v4.10.11
 
  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
 32DEFINE_IDR(dev_nums_idr);
 33static DEFINE_MUTEX(idr_lock);
 34
 35struct class *tpm_class;
 
 
 
 
 
 
 36dev_t tpm_devt;
 37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 38/**
 39 * tpm_try_get_ops() - Get a ref to the tpm_chip
 40 * @chip: Chip to ref
 41 *
 42 * The caller must already have some kind of locking to ensure that chip is
 43 * valid. This function will lock the chip so that the ops member can be
 44 * accessed safely. The locking prevents tpm_chip_unregister from
 45 * completing, so it should not be held for long periods.
 46 *
 47 * Returns -ERRNO if the chip could not be got.
 48 */
 49int tpm_try_get_ops(struct tpm_chip *chip)
 50{
 51	int rc = -EIO;
 52
 53	get_device(&chip->dev);
 54
 55	down_read(&chip->ops_sem);
 56	if (!chip->ops)
 
 
 
 
 
 57		goto out_lock;
 58
 59	return 0;
 60out_lock:
 
 
 61	up_read(&chip->ops_sem);
 62	put_device(&chip->dev);
 63	return rc;
 64}
 65EXPORT_SYMBOL_GPL(tpm_try_get_ops);
 66
 67/**
 68 * tpm_put_ops() - Release a ref to the tpm_chip
 69 * @chip: Chip to put
 70 *
 71 * This is the opposite pair to tpm_try_get_ops(). After this returns chip may
 72 * be kfree'd.
 73 */
 74void tpm_put_ops(struct tpm_chip *chip)
 75{
 
 
 76	up_read(&chip->ops_sem);
 77	put_device(&chip->dev);
 78}
 79EXPORT_SYMBOL_GPL(tpm_put_ops);
 80
 81/**
 82 * tpm_chip_find_get() - return tpm_chip for a given chip number
 83 * @chip_num: id to find
 84 *
 85 * The return'd chip has been tpm_try_get_ops'd and must be released via
 86 * tpm_put_ops
 87 */
 88struct tpm_chip *tpm_chip_find_get(int chip_num)
 89{
 90	struct tpm_chip *chip, *res = NULL;
 
 91	int chip_prev;
 92
 93	mutex_lock(&idr_lock);
 94
 95	if (chip_num == TPM_ANY_NUM) {
 96		chip_num = 0;
 97		do {
 98			chip_prev = chip_num;
 99			chip = idr_get_next(&dev_nums_idr, &chip_num);
100			if (chip && !tpm_try_get_ops(chip)) {
101				res = chip;
102				break;
103			}
104		} while (chip_prev != chip_num);
105	} else {
106		chip = idr_find(&dev_nums_idr, chip_num);
107		if (chip && !tpm_try_get_ops(chip))
108			res = chip;
109	}
 
 
110
111	mutex_unlock(&idr_lock);
112
113	return res;
114}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
115
116/**
117 * tpm_dev_release() - free chip memory and the device number
118 * @dev: the character device for the TPM chip
119 *
120 * This is used as the release function for the character device.
121 */
122static void tpm_dev_release(struct device *dev)
123{
124	struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev);
125
126	mutex_lock(&idr_lock);
127	idr_remove(&dev_nums_idr, chip->dev_num);
128	mutex_unlock(&idr_lock);
129
130	kfree(chip->log.bios_event_log);
 
 
131	kfree(chip);
132}
133
134/**
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
135 * tpm_chip_alloc() - allocate a new struct tpm_chip instance
136 * @pdev: device to which the chip is associated
137 *        At this point pdev mst be initialized, but does not have to
138 *        be registered
139 * @ops: struct tpm_class_ops instance
140 *
141 * Allocates a new struct tpm_chip instance and assigns a free
142 * device number for it. Must be paired with put_device(&chip->dev).
143 */
144struct tpm_chip *tpm_chip_alloc(struct device *dev,
145				const struct tpm_class_ops *ops)
146{
147	struct tpm_chip *chip;
148	int rc;
149
150	chip = kzalloc(sizeof(*chip), GFP_KERNEL);
151	if (chip == NULL)
152		return ERR_PTR(-ENOMEM);
153
154	mutex_init(&chip->tpm_mutex);
155	init_rwsem(&chip->ops_sem);
156
157	chip->ops = ops;
158
159	mutex_lock(&idr_lock);
160	rc = idr_alloc(&dev_nums_idr, NULL, 0, TPM_NUM_DEVICES, GFP_KERNEL);
161	mutex_unlock(&idr_lock);
162	if (rc < 0) {
163		dev_err(dev, "No available tpm device numbers\n");
164		kfree(chip);
165		return ERR_PTR(rc);
166	}
167	chip->dev_num = rc;
168
169	device_initialize(&chip->dev);
170
171	chip->dev.class = tpm_class;
172	chip->dev.release = tpm_dev_release;
173	chip->dev.parent = dev;
174	chip->dev.groups = chip->groups;
175
176	if (chip->dev_num == 0)
177		chip->dev.devt = MKDEV(MISC_MAJOR, TPM_MINOR);
178	else
179		chip->dev.devt = MKDEV(MAJOR(tpm_devt), chip->dev_num);
180
181	rc = dev_set_name(&chip->dev, "tpm%d", chip->dev_num);
182	if (rc)
183		goto out;
184
185	if (!dev)
186		chip->flags |= TPM_CHIP_FLAG_VIRTUAL;
187
188	cdev_init(&chip->cdev, &tpm_fops);
189	chip->cdev.owner = THIS_MODULE;
190	chip->cdev.kobj.parent = &chip->dev.kobj;
191
 
 
 
 
 
 
 
192	return chip;
193
194out:
195	put_device(&chip->dev);
196	return ERR_PTR(rc);
197}
198EXPORT_SYMBOL_GPL(tpm_chip_alloc);
199
 
 
 
 
 
200/**
201 * tpmm_chip_alloc() - allocate a new struct tpm_chip instance
202 * @pdev: parent device to which the chip is associated
203 * @ops: struct tpm_class_ops instance
204 *
205 * Same as tpm_chip_alloc except devm is used to do the put_device
206 */
207struct tpm_chip *tpmm_chip_alloc(struct device *pdev,
208				 const struct tpm_class_ops *ops)
209{
210	struct tpm_chip *chip;
211	int rc;
212
213	chip = tpm_chip_alloc(pdev, ops);
214	if (IS_ERR(chip))
215		return chip;
216
217	rc = devm_add_action_or_reset(pdev,
218				      (void (*)(void *)) put_device,
219				      &chip->dev);
220	if (rc)
221		return ERR_PTR(rc);
222
223	dev_set_drvdata(pdev, chip);
224
225	return chip;
226}
227EXPORT_SYMBOL_GPL(tpmm_chip_alloc);
228
229static int tpm_add_char_device(struct tpm_chip *chip)
230{
231	int rc;
232
233	rc = cdev_add(&chip->cdev, chip->dev.devt, 1);
234	if (rc) {
235		dev_err(&chip->dev,
236			"unable to cdev_add() %s, major %d, minor %d, err=%d\n",
237			dev_name(&chip->dev), MAJOR(chip->dev.devt),
238			MINOR(chip->dev.devt), rc);
239
240		return rc;
241	}
242
243	rc = device_add(&chip->dev);
244	if (rc) {
245		dev_err(&chip->dev,
246			"unable to device_register() %s, major %d, minor %d, err=%d\n",
247			dev_name(&chip->dev), MAJOR(chip->dev.devt),
248			MINOR(chip->dev.devt), rc);
249
250		cdev_del(&chip->cdev);
251		return rc;
252	}
253
254	/* Make the chip available. */
255	mutex_lock(&idr_lock);
256	idr_replace(&dev_nums_idr, chip, chip->dev_num);
257	mutex_unlock(&idr_lock);
258
 
 
 
 
259	return rc;
260}
261
262static void tpm_del_char_device(struct tpm_chip *chip)
263{
264	cdev_del(&chip->cdev);
265	device_del(&chip->dev);
266
267	/* Make the chip unavailable. */
268	mutex_lock(&idr_lock);
269	idr_replace(&dev_nums_idr, NULL, chip->dev_num);
270	mutex_unlock(&idr_lock);
271
272	/* Make the driver uncallable. */
273	down_write(&chip->ops_sem);
274	if (chip->flags & TPM_CHIP_FLAG_TPM2)
275		tpm2_shutdown(chip, TPM2_SU_CLEAR);
276	chip->ops = NULL;
 
 
 
 
 
 
 
 
 
 
 
 
277	up_write(&chip->ops_sem);
278}
279
280static void tpm_del_legacy_sysfs(struct tpm_chip *chip)
281{
282	struct attribute **i;
283
284	if (chip->flags & (TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_VIRTUAL))
 
285		return;
286
287	sysfs_remove_link(&chip->dev.parent->kobj, "ppi");
288
289	for (i = chip->groups[0]->attrs; *i != NULL; ++i)
290		sysfs_remove_link(&chip->dev.parent->kobj, (*i)->name);
291}
292
293/* For compatibility with legacy sysfs paths we provide symlinks from the
294 * parent dev directory to selected names within the tpm chip directory. Old
295 * kernel versions created these files directly under the parent.
296 */
297static int tpm_add_legacy_sysfs(struct tpm_chip *chip)
298{
299	struct attribute **i;
300	int rc;
301
302	if (chip->flags & (TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_VIRTUAL))
 
303		return 0;
304
305	rc = __compat_only_sysfs_link_entry_to_kobj(
306		    &chip->dev.parent->kobj, &chip->dev.kobj, "ppi");
307	if (rc && rc != -ENOENT)
308		return rc;
309
310	/* All the names from tpm-sysfs */
311	for (i = chip->groups[0]->attrs; *i != NULL; ++i) {
312		rc = __compat_only_sysfs_link_entry_to_kobj(
313		    &chip->dev.parent->kobj, &chip->dev.kobj, (*i)->name);
314		if (rc) {
315			tpm_del_legacy_sysfs(chip);
316			return rc;
317		}
318	}
319
320	return 0;
321}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
322/*
323 * tpm_chip_register() - create a character device for the TPM chip
324 * @chip: TPM chip to use.
325 *
326 * Creates a character device for the TPM chip and adds sysfs attributes for
327 * the device. As the last step this function adds the chip to the list of TPM
328 * chips available for in-kernel use.
329 *
330 * This function should be only called after the chip initialization is
331 * complete.
332 */
333int tpm_chip_register(struct tpm_chip *chip)
334{
335	int rc;
336
337	if (chip->ops->flags & TPM_OPS_AUTO_STARTUP) {
338		if (chip->flags & TPM_CHIP_FLAG_TPM2)
339			rc = tpm2_auto_startup(chip);
340		else
341			rc = tpm1_auto_startup(chip);
342		if (rc)
343			return rc;
344	}
345
346	tpm_sysfs_add_device(chip);
347
348	rc = tpm_bios_log_setup(chip);
349	if (rc != 0 && rc != -ENODEV)
350		return rc;
351
352	tpm_add_ppi(chip);
353
 
 
 
 
354	rc = tpm_add_char_device(chip);
355	if (rc) {
356		tpm_bios_log_teardown(chip);
357		return rc;
358	}
359
360	rc = tpm_add_legacy_sysfs(chip);
361	if (rc) {
362		tpm_chip_unregister(chip);
363		return rc;
364	}
365
366	return 0;
 
 
 
 
 
 
 
 
367}
368EXPORT_SYMBOL_GPL(tpm_chip_register);
369
370/*
371 * tpm_chip_unregister() - release the TPM driver
372 * @chip: TPM chip to use.
373 *
374 * Takes the chip first away from the list of available TPM chips and then
375 * cleans up all the resources reserved by tpm_chip_register().
376 *
377 * Once this function returns the driver call backs in 'op's will not be
378 * running and will no longer start.
379 *
380 * NOTE: This function should be only called before deinitializing chip
381 * resources.
382 */
383void tpm_chip_unregister(struct tpm_chip *chip)
384{
385	tpm_del_legacy_sysfs(chip);
 
 
386	tpm_bios_log_teardown(chip);
 
 
387	tpm_del_char_device(chip);
388}
389EXPORT_SYMBOL_GPL(tpm_chip_unregister);