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