tpm-chip.c 10.9 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
#include "tpm.h"
#include "tpm_eventlog.h"

32 33
DEFINE_IDR(dev_nums_idr);
static DEFINE_MUTEX(idr_lock);
34

J
Jarkko Sakkinen 已提交
35
struct class *tpm_class;
36
struct class *tpmrm_class;
J
Jarkko Sakkinen 已提交
37 38
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
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
88
 */
89 90
struct tpm_chip *tpm_chip_find_get(int chip_num)
{
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
	struct tpm_chip *chip, *res = NULL;
	int chip_prev;

	mutex_lock(&idr_lock);

	if (chip_num == TPM_ANY_NUM) {
		chip_num = 0;
		do {
			chip_prev = chip_num;
			chip = idr_get_next(&dev_nums_idr, &chip_num);
			if (chip && !tpm_try_get_ops(chip)) {
				res = chip;
				break;
			}
		} while (chip_prev != chip_num);
	} else {
107
		chip = idr_find(&dev_nums_idr, chip_num);
108 109 110
		if (chip && !tpm_try_get_ops(chip))
			res = chip;
	}
111

112
	mutex_unlock(&idr_lock);
113

114
	return res;
115 116 117
}

/**
J
Jarkko Sakkinen 已提交
118 119
 * tpm_dev_release() - free chip memory and the device number
 * @dev: the character device for the TPM chip
120
 *
J
Jarkko Sakkinen 已提交
121
 * This is used as the release function for the character device.
122
 */
J
Jarkko Sakkinen 已提交
123
static void tpm_dev_release(struct device *dev)
124
{
J
Jarkko Sakkinen 已提交
125
	struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev);
126

127 128 129 130
	mutex_lock(&idr_lock);
	idr_remove(&dev_nums_idr, chip->dev_num);
	mutex_unlock(&idr_lock);

N
Nayna Jain 已提交
131
	kfree(chip->log.bios_event_log);
132
	kfree(chip->work_space.context_buf);
133
	kfree(chip->work_space.session_buf);
134 135 136
	kfree(chip);
}

137 138 139 140 141 142 143 144
static void tpm_devs_release(struct device *dev)
{
	struct tpm_chip *chip = container_of(dev, struct tpm_chip, devs);

	/* release the master device reference */
	put_device(&chip->dev);
}

145
/**
146 147 148 149
 * 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
150 151 152
 * @ops: struct tpm_class_ops instance
 *
 * Allocates a new struct tpm_chip instance and assigns a free
153
 * device number for it. Must be paired with put_device(&chip->dev).
154
 */
155
struct tpm_chip *tpm_chip_alloc(struct device *pdev,
156
				const struct tpm_class_ops *ops)
157 158
{
	struct tpm_chip *chip;
159
	int rc;
160 161 162 163 164 165

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

	mutex_init(&chip->tpm_mutex);
166
	init_rwsem(&chip->ops_sem);
167 168 169

	chip->ops = ops;

170 171 172 173
	mutex_lock(&idr_lock);
	rc = idr_alloc(&dev_nums_idr, NULL, 0, TPM_NUM_DEVICES, GFP_KERNEL);
	mutex_unlock(&idr_lock);
	if (rc < 0) {
174
		dev_err(pdev, "No available tpm device numbers\n");
175
		kfree(chip);
176
		return ERR_PTR(rc);
177
	}
178
	chip->dev_num = rc;
179

J
Jason Gunthorpe 已提交
180
	device_initialize(&chip->dev);
181
	device_initialize(&chip->devs);
182

J
Jarkko Sakkinen 已提交
183 184
	chip->dev.class = tpm_class;
	chip->dev.release = tpm_dev_release;
185
	chip->dev.parent = pdev;
186
	chip->dev.groups = chip->groups;
J
Jarkko Sakkinen 已提交
187

188 189 190 191 192 193
	chip->devs.parent = pdev;
	chip->devs.class = tpmrm_class;
	chip->devs.release = tpm_devs_release;
	/* get extra reference on main device to hold on
	 * behalf of devs.  This holds the chip structure
	 * while cdevs is in use.  The corresponding put
194
	 * is in the tpm_devs_release (TPM2 only)
195
	 */
196 197
	if (chip->flags & TPM_CHIP_FLAG_TPM2)
		get_device(&chip->dev);
198

J
Jarkko Sakkinen 已提交
199 200 201 202 203
	if (chip->dev_num == 0)
		chip->dev.devt = MKDEV(MISC_MAJOR, TPM_MINOR);
	else
		chip->dev.devt = MKDEV(MAJOR(tpm_devt), chip->dev_num);

204 205 206
	chip->devs.devt =
		MKDEV(MAJOR(tpm_devt), chip->dev_num + TPM_NUM_DEVICES);

J
Jason Gunthorpe 已提交
207
	rc = dev_set_name(&chip->dev, "tpm%d", chip->dev_num);
208 209 210
	if (rc)
		goto out;
	rc = dev_set_name(&chip->devs, "tpmrm%d", chip->dev_num);
J
Jason Gunthorpe 已提交
211 212
	if (rc)
		goto out;
J
Jarkko Sakkinen 已提交
213

214
	if (!pdev)
215 216
		chip->flags |= TPM_CHIP_FLAG_VIRTUAL;

J
Jarkko Sakkinen 已提交
217
	cdev_init(&chip->cdev, &tpm_fops);
218
	cdev_init(&chip->cdevs, &tpmrm_fops);
S
Stefan Berger 已提交
219
	chip->cdev.owner = THIS_MODULE;
220
	chip->cdevs.owner = THIS_MODULE;
221
	chip->cdev.kobj.parent = &chip->dev.kobj;
222
	chip->cdevs.kobj.parent = &chip->devs.kobj;
J
Jarkko Sakkinen 已提交
223

224 225 226 227 228
	chip->work_space.context_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
	if (!chip->work_space.context_buf) {
		rc = -ENOMEM;
		goto out;
	}
229 230 231 232 233
	chip->work_space.session_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
	if (!chip->work_space.session_buf) {
		rc = -ENOMEM;
		goto out;
	}
234

235
	chip->locality = -1;
236 237 238
	return chip;

out:
239
	put_device(&chip->devs);
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
	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;

262 263 264 265
	rc = devm_add_action_or_reset(pdev,
				      (void (*)(void *)) put_device,
				      &chip->dev);
	if (rc)
266
		return ERR_PTR(rc);
267

268
	dev_set_drvdata(pdev, chip);
J
Jason Gunthorpe 已提交
269

270
	return chip;
271 272 273
}
EXPORT_SYMBOL_GPL(tpmm_chip_alloc);

274
static int tpm_add_char_device(struct tpm_chip *chip)
J
Jarkko Sakkinen 已提交
275 276 277
{
	int rc;

278
	rc = cdev_add(&chip->cdev, chip->dev.devt, 1);
J
Jarkko Sakkinen 已提交
279 280
	if (rc) {
		dev_err(&chip->dev,
281
			"unable to cdev_add() %s, major %d, minor %d, err=%d\n",
J
Jason Gunthorpe 已提交
282
			dev_name(&chip->dev), MAJOR(chip->dev.devt),
J
Jarkko Sakkinen 已提交
283 284 285 286
			MINOR(chip->dev.devt), rc);
		return rc;
	}

287
	rc = device_add(&chip->dev);
J
Jarkko Sakkinen 已提交
288 289
	if (rc) {
		dev_err(&chip->dev,
290
			"unable to device_register() %s, major %d, minor %d, err=%d\n",
J
Jason Gunthorpe 已提交
291
			dev_name(&chip->dev), MAJOR(chip->dev.devt),
J
Jarkko Sakkinen 已提交
292 293
			MINOR(chip->dev.devt), rc);

294
		cdev_del(&chip->cdev);
J
Jarkko Sakkinen 已提交
295 296 297
		return rc;
	}

298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
	if (chip->flags & TPM_CHIP_FLAG_TPM2)
		rc = cdev_add(&chip->cdevs, chip->devs.devt, 1);
	if (rc) {
		dev_err(&chip->dev,
			"unable to cdev_add() %s, major %d, minor %d, err=%d\n",
			dev_name(&chip->devs), MAJOR(chip->devs.devt),
			MINOR(chip->devs.devt), rc);
		return rc;
	}

	if (chip->flags & TPM_CHIP_FLAG_TPM2)
		rc = device_add(&chip->devs);
	if (rc) {
		dev_err(&chip->dev,
			"unable to device_register() %s, major %d, minor %d, err=%d\n",
			dev_name(&chip->devs), MAJOR(chip->devs.devt),
			MINOR(chip->devs.devt), rc);
		cdev_del(&chip->cdevs);
		return rc;
	}

319 320 321 322 323
	/* Make the chip available. */
	mutex_lock(&idr_lock);
	idr_replace(&dev_nums_idr, chip, chip->dev_num);
	mutex_unlock(&idr_lock);

J
Jarkko Sakkinen 已提交
324 325 326
	return rc;
}

327
static void tpm_del_char_device(struct tpm_chip *chip)
J
Jarkko Sakkinen 已提交
328 329
{
	cdev_del(&chip->cdev);
330 331 332 333 334 335
	device_del(&chip->dev);

	/* Make the chip unavailable. */
	mutex_lock(&idr_lock);
	idr_replace(&dev_nums_idr, NULL, chip->dev_num);
	mutex_unlock(&idr_lock);
336 337 338

	/* Make the driver uncallable. */
	down_write(&chip->ops_sem);
339 340
	if (chip->flags & TPM_CHIP_FLAG_TPM2)
		tpm2_shutdown(chip, TPM2_SU_CLEAR);
341 342
	chip->ops = NULL;
	up_write(&chip->ops_sem);
J
Jarkko Sakkinen 已提交
343 344
}

345 346 347 348
static void tpm_del_legacy_sysfs(struct tpm_chip *chip)
{
	struct attribute **i;

349
	if (chip->flags & (TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_VIRTUAL))
350
		return;
351

352 353 354 355
	sysfs_remove_link(&chip->dev.parent->kobj, "ppi");

	for (i = chip->groups[0]->attrs; *i != NULL; ++i)
		sysfs_remove_link(&chip->dev.parent->kobj, (*i)->name);
356 357
}

358 359 360 361 362 363 364 365 366
/* For compatibility with legacy sysfs paths we provide symlinks from the
 * parent dev directory to selected names within the tpm chip directory. Old
 * kernel versions created these files directly under the parent.
 */
static int tpm_add_legacy_sysfs(struct tpm_chip *chip)
{
	struct attribute **i;
	int rc;

367
	if (chip->flags & (TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_VIRTUAL))
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
		return 0;

	rc = __compat_only_sysfs_link_entry_to_kobj(
		    &chip->dev.parent->kobj, &chip->dev.kobj, "ppi");
	if (rc && rc != -ENOENT)
		return rc;

	/* All the names from tpm-sysfs */
	for (i = chip->groups[0]->attrs; *i != NULL; ++i) {
		rc = __compat_only_sysfs_link_entry_to_kobj(
		    &chip->dev.parent->kobj, &chip->dev.kobj, (*i)->name);
		if (rc) {
			tpm_del_legacy_sysfs(chip);
			return rc;
		}
	}

	return 0;
}
387
/*
J
Jarkko Sakkinen 已提交
388
 * tpm_chip_register() - create a character device for the TPM chip
389 390
 * @chip: TPM chip to use.
 *
391 392 393
 * 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.
394
 *
395 396
 * This function should be only called after the chip initialization is
 * complete.
397 398 399 400 401
 */
int tpm_chip_register(struct tpm_chip *chip)
{
	int rc;

402 403 404 405 406 407 408 409 410
	if (chip->ops->flags & TPM_OPS_AUTO_STARTUP) {
		if (chip->flags & TPM_CHIP_FLAG_TPM2)
			rc = tpm2_auto_startup(chip);
		else
			rc = tpm1_auto_startup(chip);
		if (rc)
			return rc;
	}

411 412 413
	tpm_sysfs_add_device(chip);

	rc = tpm_bios_log_setup(chip);
414
	if (rc != 0 && rc != -ENODEV)
415
		return rc;
416

417 418
	tpm_add_ppi(chip);

419
	rc = tpm_add_char_device(chip);
420
	if (rc) {
421
		tpm_bios_log_teardown(chip);
422 423
		return rc;
	}
424

425 426 427 428
	rc = tpm_add_legacy_sysfs(chip);
	if (rc) {
		tpm_chip_unregister(chip);
		return rc;
429 430
	}

431 432 433 434 435 436 437 438 439 440 441
	return 0;
}
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().
 *
442 443 444
 * Once this function returns the driver call backs in 'op's will not be
 * running and will no longer start.
 *
445 446 447 448 449
 * NOTE: This function should be only called before deinitializing chip
 * resources.
 */
void tpm_chip_unregister(struct tpm_chip *chip)
{
450
	tpm_del_legacy_sysfs(chip);
451
	tpm_bios_log_teardown(chip);
452 453 454 455
	if (chip->flags & TPM_CHIP_FLAG_TPM2) {
		cdev_del(&chip->cdevs);
		device_del(&chip->devs);
	}
456
	tpm_del_char_device(chip);
457 458
}
EXPORT_SYMBOL_GPL(tpm_chip_unregister);