tpm-chip.c 6.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*
 * Copyright (C) 2004 IBM Corporation
 * Copyright (C) 2014 Intel Corporation
 *
 * Authors:
 * Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
 * Leendert van Doorn <leendert@watson.ibm.com>
 * Dave Safford <safford@watson.ibm.com>
 * Reiner Sailer <sailer@watson.ibm.com>
 * Kylene Hall <kjhall@us.ibm.com>
 *
 * Maintained by: <tpmdd-devel@lists.sourceforge.net>
 *
 * TPM chip management routines.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, version 2 of the
 * License.
 *
 */

#include <linux/poll.h>
#include <linux/slab.h>
#include <linux/mutex.h>
#include <linux/spinlock.h>
#include <linux/freezer.h>
J
Jarkko Sakkinen 已提交
28
#include <linux/major.h>
29 30 31 32 33 34 35
#include "tpm.h"
#include "tpm_eventlog.h"

static DECLARE_BITMAP(dev_mask, TPM_NUM_DEVICES);
static LIST_HEAD(tpm_chip_list);
static DEFINE_SPINLOCK(driver_lock);

J
Jarkko Sakkinen 已提交
36 37 38
struct class *tpm_class;
dev_t tpm_devt;

39 40 41 42 43 44 45 46 47 48 49 50 51
/*
 * tpm_chip_find_get - return tpm_chip for a given chip number
 * @chip_num the device number for the chip
 */
struct tpm_chip *tpm_chip_find_get(int chip_num)
{
	struct tpm_chip *pos, *chip = NULL;

	rcu_read_lock();
	list_for_each_entry_rcu(pos, &tpm_chip_list, list) {
		if (chip_num != TPM_ANY_NUM && chip_num != pos->dev_num)
			continue;

J
Jason Gunthorpe 已提交
52
		if (try_module_get(pos->dev.parent->driver->owner)) {
53 54 55 56 57 58 59 60 61
			chip = pos;
			break;
		}
	}
	rcu_read_unlock();
	return chip;
}

/**
J
Jarkko Sakkinen 已提交
62 63
 * tpm_dev_release() - free chip memory and the device number
 * @dev: the character device for the TPM chip
64
 *
J
Jarkko Sakkinen 已提交
65
 * This is used as the release function for the character device.
66
 */
J
Jarkko Sakkinen 已提交
67
static void tpm_dev_release(struct device *dev)
68
{
J
Jarkko Sakkinen 已提交
69
	struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev);
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90

	spin_lock(&driver_lock);
	clear_bit(chip->dev_num, dev_mask);
	spin_unlock(&driver_lock);
	kfree(chip);
}

/**
 * tpmm_chip_alloc() - allocate a new struct tpm_chip instance
 * @dev: device to which the chip is associated
 * @ops: struct tpm_class_ops instance
 *
 * Allocates a new struct tpm_chip instance and assigns a free
 * device number for it. Caller does not have to worry about
 * freeing the allocated resources. When the devices is removed
 * devres calls tpmm_chip_remove() to do the job.
 */
struct tpm_chip *tpmm_chip_alloc(struct device *dev,
				 const struct tpm_class_ops *ops)
{
	struct tpm_chip *chip;
91
	int rc;
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113

	chip = kzalloc(sizeof(*chip), GFP_KERNEL);
	if (chip == NULL)
		return ERR_PTR(-ENOMEM);

	mutex_init(&chip->tpm_mutex);
	INIT_LIST_HEAD(&chip->list);

	chip->ops = ops;

	spin_lock(&driver_lock);
	chip->dev_num = find_first_zero_bit(dev_mask, TPM_NUM_DEVICES);
	spin_unlock(&driver_lock);

	if (chip->dev_num >= TPM_NUM_DEVICES) {
		dev_err(dev, "No available tpm device numbers\n");
		kfree(chip);
		return ERR_PTR(-ENOMEM);
	}

	set_bit(chip->dev_num, dev_mask);

J
Jason Gunthorpe 已提交
114
	device_initialize(&chip->dev);
115 116 117

	dev_set_drvdata(dev, chip);

J
Jarkko Sakkinen 已提交
118 119
	chip->dev.class = tpm_class;
	chip->dev.release = tpm_dev_release;
J
Jason Gunthorpe 已提交
120
	chip->dev.parent = dev;
121 122 123
#ifdef CONFIG_ACPI
	chip->dev.groups = chip->groups;
#endif
J
Jarkko Sakkinen 已提交
124 125 126 127 128 129

	if (chip->dev_num == 0)
		chip->dev.devt = MKDEV(MISC_MAJOR, TPM_MINOR);
	else
		chip->dev.devt = MKDEV(MAJOR(tpm_devt), chip->dev_num);

J
Jason Gunthorpe 已提交
130 131 132
	rc = dev_set_name(&chip->dev, "tpm%d", chip->dev_num);
	if (rc)
		goto out;
J
Jarkko Sakkinen 已提交
133 134

	cdev_init(&chip->cdev, &tpm_fops);
J
Jason Gunthorpe 已提交
135
	chip->cdev.owner = dev->driver->owner;
136
	chip->cdev.kobj.parent = &chip->dev.kobj;
J
Jarkko Sakkinen 已提交
137

138 139 140 141 142
	rc = devm_add_action(dev, (void (*)(void *)) put_device, &chip->dev);
	if (rc) {
		put_device(&chip->dev);
		return ERR_PTR(rc);
	}
143

144
	return chip;
J
Jason Gunthorpe 已提交
145 146 147 148

out:
	put_device(&chip->dev);
	return ERR_PTR(rc);
149 150 151
}
EXPORT_SYMBOL_GPL(tpmm_chip_alloc);

152
static int tpm_add_char_device(struct tpm_chip *chip)
J
Jarkko Sakkinen 已提交
153 154 155
{
	int rc;

156
	rc = cdev_add(&chip->cdev, chip->dev.devt, 1);
J
Jarkko Sakkinen 已提交
157 158
	if (rc) {
		dev_err(&chip->dev,
159
			"unable to cdev_add() %s, major %d, minor %d, err=%d\n",
J
Jason Gunthorpe 已提交
160
			dev_name(&chip->dev), MAJOR(chip->dev.devt),
J
Jarkko Sakkinen 已提交
161 162 163 164 165
			MINOR(chip->dev.devt), rc);

		return rc;
	}

166
	rc = device_add(&chip->dev);
J
Jarkko Sakkinen 已提交
167 168
	if (rc) {
		dev_err(&chip->dev,
169
			"unable to device_register() %s, major %d, minor %d, err=%d\n",
J
Jason Gunthorpe 已提交
170
			dev_name(&chip->dev), MAJOR(chip->dev.devt),
J
Jarkko Sakkinen 已提交
171 172
			MINOR(chip->dev.devt), rc);

173
		cdev_del(&chip->cdev);
J
Jarkko Sakkinen 已提交
174 175 176 177 178 179
		return rc;
	}

	return rc;
}

180
static void tpm_del_char_device(struct tpm_chip *chip)
J
Jarkko Sakkinen 已提交
181 182
{
	cdev_del(&chip->cdev);
183
	device_del(&chip->dev);
J
Jarkko Sakkinen 已提交
184 185
}

186 187 188 189 190 191 192 193 194 195 196
static int tpm1_chip_register(struct tpm_chip *chip)
{
	int rc;

	if (chip->flags & TPM_CHIP_FLAG_TPM2)
		return 0;

	rc = tpm_sysfs_add_device(chip);
	if (rc)
		return rc;

J
Jason Gunthorpe 已提交
197
	chip->bios_dir = tpm_bios_log_setup(dev_name(&chip->dev));
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212

	return 0;
}

static void tpm1_chip_unregister(struct tpm_chip *chip)
{
	if (chip->flags & TPM_CHIP_FLAG_TPM2)
		return;

	if (chip->bios_dir)
		tpm_bios_log_teardown(chip->bios_dir);

	tpm_sysfs_del_device(chip);
}

213
/*
J
Jarkko Sakkinen 已提交
214
 * tpm_chip_register() - create a character device for the TPM chip
215 216
 * @chip: TPM chip to use.
 *
217 218 219
 * Creates a character device for the TPM chip and adds sysfs attributes for
 * the device. As the last step this function adds the chip to the list of TPM
 * chips available for in-kernel use.
220
 *
221 222
 * This function should be only called after the chip initialization is
 * complete.
223 224 225 226 227
 */
int tpm_chip_register(struct tpm_chip *chip)
{
	int rc;

228 229 230
	rc = tpm1_chip_register(chip);
	if (rc)
		return rc;
231

232 233
	tpm_add_ppi(chip);

234
	rc = tpm_add_char_device(chip);
235
	if (rc)
236
		goto out_err;
237

238 239
	/* Make the chip available. */
	spin_lock(&driver_lock);
240
	list_add_tail_rcu(&chip->list, &tpm_chip_list);
241 242 243 244
	spin_unlock(&driver_lock);

	chip->flags |= TPM_CHIP_FLAG_REGISTERED;

245
	if (!(chip->flags & TPM_CHIP_FLAG_TPM2)) {
J
Jason Gunthorpe 已提交
246 247
		rc = __compat_only_sysfs_link_entry_to_kobj(
		    &chip->dev.parent->kobj, &chip->dev.kobj, "ppi");
248 249 250 251 252 253
		if (rc && rc != -ENOENT) {
			tpm_chip_unregister(chip);
			return rc;
		}
	}

254
	return 0;
255 256
out_err:
	tpm1_chip_unregister(chip);
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
	return rc;
}
EXPORT_SYMBOL_GPL(tpm_chip_register);

/*
 * tpm_chip_unregister() - release the TPM driver
 * @chip: TPM chip to use.
 *
 * Takes the chip first away from the list of available TPM chips and then
 * cleans up all the resources reserved by tpm_chip_register().
 *
 * NOTE: This function should be only called before deinitializing chip
 * resources.
 */
void tpm_chip_unregister(struct tpm_chip *chip)
{
	if (!(chip->flags & TPM_CHIP_FLAG_REGISTERED))
		return;

	spin_lock(&driver_lock);
	list_del_rcu(&chip->list);
	spin_unlock(&driver_lock);
	synchronize_rcu();

281
	if (!(chip->flags & TPM_CHIP_FLAG_TPM2))
J
Jason Gunthorpe 已提交
282
		sysfs_remove_link(&chip->dev.parent->kobj, "ppi");
283

284
	tpm1_chip_unregister(chip);
285
	tpm_del_char_device(chip);
286 287
}
EXPORT_SYMBOL_GPL(tpm_chip_unregister);