tpm-chip.c 8.2 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
/**
 * tpm_try_get_ops() - Get a ref to the tpm_chip
 * @chip: Chip to ref
 *
 * The caller must already have some kind of locking to ensure that chip is
 * valid. This function will lock the chip so that the ops member can be
 * accessed safely. The locking prevents tpm_chip_unregister from
 * completing, so it should not be held for long periods.
 *
 * Returns -ERRNO if the chip could not be got.
49
 */
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
int tpm_try_get_ops(struct tpm_chip *chip)
{
	int rc = -EIO;

	get_device(&chip->dev);

	down_read(&chip->ops_sem);
	if (!chip->ops)
		goto out_lock;

	return 0;
out_lock:
	up_read(&chip->ops_sem);
	put_device(&chip->dev);
	return rc;
}
EXPORT_SYMBOL_GPL(tpm_try_get_ops);

/**
 * tpm_put_ops() - Release a ref to the tpm_chip
 * @chip: Chip to put
 *
 * This is the opposite pair to tpm_try_get_ops(). After this returns chip may
 * be kfree'd.
 */
void tpm_put_ops(struct tpm_chip *chip)
{
	up_read(&chip->ops_sem);
	put_device(&chip->dev);
}
EXPORT_SYMBOL_GPL(tpm_put_ops);

/**
 * tpm_chip_find_get() - return tpm_chip for a given chip number
 * @chip_num: id to find
 *
 * The return'd chip has been tpm_try_get_ops'd and must be released via
 * tpm_put_ops
  */
89 90 91 92 93 94 95 96 97
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;

98 99
		/* rcu prevents chip from being free'd */
		if (!tpm_try_get_ops(pos))
100
			chip = pos;
101
		break;
102 103 104 105 106 107
	}
	rcu_read_unlock();
	return chip;
}

/**
J
Jarkko Sakkinen 已提交
108 109
 * tpm_dev_release() - free chip memory and the device number
 * @dev: the character device for the TPM chip
110
 *
J
Jarkko Sakkinen 已提交
111
 * This is used as the release function for the character device.
112
 */
J
Jarkko Sakkinen 已提交
113
static void tpm_dev_release(struct device *dev)
114
{
J
Jarkko Sakkinen 已提交
115
	struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev);
116 117 118 119 120 121 122 123

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

/**
124 125 126 127
 * tpm_chip_alloc() - allocate a new struct tpm_chip instance
 * @pdev: device to which the chip is associated
 *        At this point pdev mst be initialized, but does not have to
 *        be registered
128 129 130
 * @ops: struct tpm_class_ops instance
 *
 * Allocates a new struct tpm_chip instance and assigns a free
131
 * device number for it. Must be paired with put_device(&chip->dev).
132
 */
133 134
struct tpm_chip *tpm_chip_alloc(struct device *dev,
				const struct tpm_class_ops *ops)
135 136
{
	struct tpm_chip *chip;
137
	int rc;
138 139 140 141 142 143

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

	mutex_init(&chip->tpm_mutex);
144
	init_rwsem(&chip->ops_sem);
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
	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 已提交
161
	device_initialize(&chip->dev);
162

J
Jarkko Sakkinen 已提交
163 164
	chip->dev.class = tpm_class;
	chip->dev.release = tpm_dev_release;
J
Jason Gunthorpe 已提交
165
	chip->dev.parent = dev;
166 167 168
#ifdef CONFIG_ACPI
	chip->dev.groups = chip->groups;
#endif
J
Jarkko Sakkinen 已提交
169 170 171 172 173 174

	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 已提交
175 176 177
	rc = dev_set_name(&chip->dev, "tpm%d", chip->dev_num);
	if (rc)
		goto out;
J
Jarkko Sakkinen 已提交
178 179

	cdev_init(&chip->cdev, &tpm_fops);
S
Stefan Berger 已提交
180
	chip->cdev.owner = THIS_MODULE;
181
	chip->cdev.kobj.parent = &chip->dev.kobj;
J
Jarkko Sakkinen 已提交
182

183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
	return chip;

out:
	put_device(&chip->dev);
	return ERR_PTR(rc);
}
EXPORT_SYMBOL_GPL(tpm_chip_alloc);

/**
 * tpmm_chip_alloc() - allocate a new struct tpm_chip instance
 * @pdev: parent device to which the chip is associated
 * @ops: struct tpm_class_ops instance
 *
 * Same as tpm_chip_alloc except devm is used to do the put_device
 */
struct tpm_chip *tpmm_chip_alloc(struct device *pdev,
				 const struct tpm_class_ops *ops)
{
	struct tpm_chip *chip;
	int rc;

	chip = tpm_chip_alloc(pdev, ops);
	if (IS_ERR(chip))
		return chip;

	rc = devm_add_action(pdev, (void (*)(void *)) put_device, &chip->dev);
209 210 211 212
	if (rc) {
		put_device(&chip->dev);
		return ERR_PTR(rc);
	}
213

214
	dev_set_drvdata(pdev, chip);
J
Jason Gunthorpe 已提交
215

216
	return chip;
217 218 219
}
EXPORT_SYMBOL_GPL(tpmm_chip_alloc);

220
static int tpm_add_char_device(struct tpm_chip *chip)
J
Jarkko Sakkinen 已提交
221 222 223
{
	int rc;

224
	rc = cdev_add(&chip->cdev, chip->dev.devt, 1);
J
Jarkko Sakkinen 已提交
225 226
	if (rc) {
		dev_err(&chip->dev,
227
			"unable to cdev_add() %s, major %d, minor %d, err=%d\n",
J
Jason Gunthorpe 已提交
228
			dev_name(&chip->dev), MAJOR(chip->dev.devt),
J
Jarkko Sakkinen 已提交
229 230 231 232 233
			MINOR(chip->dev.devt), rc);

		return rc;
	}

234
	rc = device_add(&chip->dev);
J
Jarkko Sakkinen 已提交
235 236
	if (rc) {
		dev_err(&chip->dev,
237
			"unable to device_register() %s, major %d, minor %d, err=%d\n",
J
Jason Gunthorpe 已提交
238
			dev_name(&chip->dev), MAJOR(chip->dev.devt),
J
Jarkko Sakkinen 已提交
239 240
			MINOR(chip->dev.devt), rc);

241
		cdev_del(&chip->cdev);
J
Jarkko Sakkinen 已提交
242 243 244 245 246 247
		return rc;
	}

	return rc;
}

248
static void tpm_del_char_device(struct tpm_chip *chip)
J
Jarkko Sakkinen 已提交
249 250
{
	cdev_del(&chip->cdev);
251 252 253 254 255 256

	/* Make the driver uncallable. */
	down_write(&chip->ops_sem);
	chip->ops = NULL;
	up_write(&chip->ops_sem);

257
	device_del(&chip->dev);
J
Jarkko Sakkinen 已提交
258 259
}

260 261 262 263 264 265 266 267 268 269 270
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 已提交
271
	chip->bios_dir = tpm_bios_log_setup(dev_name(&chip->dev));
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286

	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);
}

287
/*
J
Jarkko Sakkinen 已提交
288
 * tpm_chip_register() - create a character device for the TPM chip
289 290
 * @chip: TPM chip to use.
 *
291 292 293
 * 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.
294
 *
295 296
 * This function should be only called after the chip initialization is
 * complete.
297 298 299 300 301
 */
int tpm_chip_register(struct tpm_chip *chip)
{
	int rc;

302 303 304
	rc = tpm1_chip_register(chip);
	if (rc)
		return rc;
305

306 307
	tpm_add_ppi(chip);

308
	rc = tpm_add_char_device(chip);
309
	if (rc)
310
		goto out_err;
311

312 313
	/* Make the chip available. */
	spin_lock(&driver_lock);
314
	list_add_tail_rcu(&chip->list, &tpm_chip_list);
315 316 317 318
	spin_unlock(&driver_lock);

	chip->flags |= TPM_CHIP_FLAG_REGISTERED;

319
	if (!(chip->flags & TPM_CHIP_FLAG_TPM2)) {
J
Jason Gunthorpe 已提交
320 321
		rc = __compat_only_sysfs_link_entry_to_kobj(
		    &chip->dev.parent->kobj, &chip->dev.kobj, "ppi");
322 323 324 325 326 327
		if (rc && rc != -ENOENT) {
			tpm_chip_unregister(chip);
			return rc;
		}
	}

328
	return 0;
329 330
out_err:
	tpm1_chip_unregister(chip);
331 332 333 334 335 336 337 338 339 340 341
	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().
 *
342 343 344
 * Once this function returns the driver call backs in 'op's will not be
 * running and will no longer start.
 *
345 346 347 348 349 350 351 352 353 354 355 356 357
 * 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();

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

361
	tpm1_chip_unregister(chip);
362
	tpm_del_char_device(chip);
363 364
}
EXPORT_SYMBOL_GPL(tpm_chip_unregister);