2 * Copyright (C) 2004 IBM Corporation
5 * Leendert van Doorn <leendert@watson.ibm.com>
6 * Dave Safford <safford@watson.ibm.com>
7 * Reiner Sailer <sailer@watson.ibm.com>
8 * Kylene Hall <kjhall@us.ibm.com>
10 * Maintained by: <tpmdd_devel@lists.sourceforge.net>
12 * Device driver for TCG/TCPA TPM (trusted platform module).
13 * Specifications at www.trustedcomputinggroup.org
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License as
17 * published by the Free Software Foundation, version 2 of the
20 * Note, the TPM chip is not interrupt driven (only polling)
21 * and can have very long timeouts (minutes!). Hence the unusual
26 #include <linux/sched.h>
27 #include <linux/poll.h>
28 #include <linux/spinlock.h>
32 TPM_MINOR = 224, /* officially assigned */
34 TPM_NUM_DEVICES = 256,
35 TPM_NUM_MASK_ENTRIES = TPM_NUM_DEVICES / (8 * sizeof(int))
45 #define TPM_MAX_ORDINAL 243
46 #define TPM_MAX_PROTECTED_ORDINAL 12
47 #define TPM_PROTECTED_ORDINAL_MASK 0xFF
49 static LIST_HEAD(tpm_chip_list);
50 static DEFINE_SPINLOCK(driver_lock);
51 static int dev_mask[TPM_NUM_MASK_ENTRIES];
54 * Array with one entry per ordinal defining the maximum amount
55 * of time the chip could take to return the result. The ordinal
56 * designation of short, medium or long is defined in a table in
57 * TCG Specification TPM Main Part 2 TPM Structures Section 17. The
58 * values of the SHORT, MEDIUM, and LONG durations are retrieved
59 * from the chip during initialization with a call to tpm_get_timeouts.
61 static const u8 tpm_protected_ordinal_duration[TPM_MAX_PROTECTED_ORDINAL] = {
62 TPM_UNDEFINED, /* 0 */
67 TPM_UNDEFINED, /* 5 */
76 static const u8 tpm_ordinal_duration[TPM_MAX_ORDINAL] = {
77 TPM_UNDEFINED, /* 0 */
82 TPM_UNDEFINED, /* 5 */
132 TPM_UNDEFINED, /* 55 */
152 TPM_UNDEFINED, /* 75 */
162 TPM_UNDEFINED, /* 85 */
172 TPM_UNDEFINED, /* 95 */
177 TPM_MEDIUM, /* 100 */
182 TPM_UNDEFINED, /* 105 */
212 TPM_UNDEFINED, /* 135 */
222 TPM_UNDEFINED, /* 145 */
232 TPM_UNDEFINED, /* 155 */
242 TPM_UNDEFINED, /* 165 */
252 TPM_UNDEFINED, /* 175 */
257 TPM_MEDIUM, /* 180 */
262 TPM_MEDIUM, /* 185 */
267 TPM_UNDEFINED, /* 190 */
272 TPM_UNDEFINED, /* 195 */
287 TPM_MEDIUM, /* 210 */
292 TPM_UNDEFINED, /* 215 */
302 TPM_UNDEFINED, /* 225 */
312 TPM_UNDEFINED, /* 235 */
322 static void user_reader_timeout(unsigned long ptr)
324 struct tpm_chip *chip = (struct tpm_chip *) ptr;
326 schedule_work(&chip->work);
329 static void timeout_work(void *ptr)
331 struct tpm_chip *chip = ptr;
333 down(&chip->buffer_mutex);
334 atomic_set(&chip->data_pending, 0);
335 memset(chip->data_buffer, 0, TPM_BUFSIZE);
336 up(&chip->buffer_mutex);
340 * Returns max number of jiffies to wait
342 unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip,
345 int duration_idx = TPM_UNDEFINED;
348 if (ordinal < TPM_MAX_ORDINAL)
349 duration_idx = tpm_ordinal_duration[ordinal];
350 else if ((ordinal & TPM_PROTECTED_ORDINAL_MASK) <
351 TPM_MAX_PROTECTED_ORDINAL)
353 tpm_protected_ordinal_duration[ordinal &
354 TPM_PROTECTED_ORDINAL_MASK];
356 if (duration_idx != TPM_UNDEFINED)
357 duration = chip->vendor.duration[duration_idx] * HZ / 1000;
363 EXPORT_SYMBOL_GPL(tpm_calc_ordinal_duration);
366 * Internal kernel interface to transmit TPM commands
368 static ssize_t tpm_transmit(struct tpm_chip *chip, const char *buf,
375 count = be32_to_cpu(*((__be32 *) (buf + 2)));
376 ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
379 if (count > bufsiz) {
381 "invalid count value %x %zx \n", count, bufsiz);
385 down(&chip->tpm_mutex);
387 if ((rc = chip->vendor.send(chip, (u8 *) buf, count)) < 0) {
389 "tpm_transmit: tpm_send: error %zd\n", rc);
393 stop = jiffies + tpm_calc_ordinal_duration(chip, ordinal);
395 u8 status = chip->vendor.status(chip);
396 if ((status & chip->vendor.req_complete_mask) ==
397 chip->vendor.req_complete_val)
400 if ((status == chip->vendor.req_canceled)) {
401 dev_err(chip->dev, "Operation Canceled\n");
406 msleep(TPM_TIMEOUT); /* CHECK */
408 } while (time_before(jiffies, stop));
410 chip->vendor.cancel(chip);
411 dev_err(chip->dev, "Operation Timed out\n");
416 rc = chip->vendor.recv(chip, (u8 *) buf, bufsiz);
419 "tpm_transmit: tpm_recv: error %zd\n", rc);
421 up(&chip->tpm_mutex);
425 #define TPM_DIGEST_SIZE 20
426 #define TPM_ERROR_SIZE 10
427 #define TPM_RET_CODE_IDX 6
428 #define TPM_GET_CAP_RET_SIZE_IDX 10
429 #define TPM_GET_CAP_RET_UINT32_1_IDX 14
430 #define TPM_GET_CAP_RET_UINT32_2_IDX 18
431 #define TPM_GET_CAP_RET_UINT32_3_IDX 22
432 #define TPM_GET_CAP_RET_UINT32_4_IDX 26
434 #define TPM_CAP_IDX 13
435 #define TPM_CAP_SUBCAP_IDX 21
437 enum tpm_capabilities {
441 enum tpm_sub_capabilities {
442 TPM_CAP_PROP_PCR = 0x1,
443 TPM_CAP_PROP_MANUFACTURER = 0x3,
447 * This is a semi generic GetCapability command for use
448 * with the capability type TPM_CAP_PROP or TPM_CAP_FLAG
449 * and their associated sub_capabilities.
452 static const u8 tpm_cap[] = {
453 0, 193, /* TPM_TAG_RQU_COMMAND */
454 0, 0, 0, 22, /* length */
455 0, 0, 0, 101, /* TPM_ORD_GetCapability */
456 0, 0, 0, 0, /* TPM_CAP_<TYPE> */
457 0, 0, 0, 4, /* TPM_CAP_SUB_<TYPE> size */
458 0, 0, 1, 0 /* TPM_CAP_SUB_<TYPE> */
461 static ssize_t transmit_cmd(struct tpm_chip *chip, u8 *data, int len,
466 len = tpm_transmit(chip, data, len);
469 if (len == TPM_ERROR_SIZE) {
470 err = be32_to_cpu(*((__be32 *) (data + TPM_RET_CODE_IDX)));
471 dev_dbg(chip->dev, "A TPM error (%d) occurred %s\n", err, desc);
477 static const u8 pcrread[] = {
478 0, 193, /* TPM_TAG_RQU_COMMAND */
479 0, 0, 0, 14, /* length */
480 0, 0, 0, 21, /* TPM_ORD_PcrRead */
481 0, 0, 0, 0 /* PCR index */
484 ssize_t tpm_show_pcrs(struct device *dev, struct device_attribute *attr,
493 struct tpm_chip *chip = dev_get_drvdata(dev);
497 memcpy(data, tpm_cap, sizeof(tpm_cap));
498 data[TPM_CAP_IDX] = TPM_CAP_PROP;
499 data[TPM_CAP_SUBCAP_IDX] = TPM_CAP_PROP_PCR;
501 rc = transmit_cmd(chip, data, sizeof(data),
502 "attempting to determine the number of PCRS");
506 num_pcrs = be32_to_cpu(*((__be32 *) (data + 14)));
507 for (i = 0; i < num_pcrs; i++) {
508 memcpy(data, pcrread, sizeof(pcrread));
509 index = cpu_to_be32(i);
510 memcpy(data + 10, &index, 4);
511 rc = transmit_cmd(chip, data, sizeof(data),
512 "attempting to read a PCR");
515 str += sprintf(str, "PCR-%02d: ", i);
516 for (j = 0; j < TPM_DIGEST_SIZE; j++)
517 str += sprintf(str, "%02X ", *(data + 10 + j));
518 str += sprintf(str, "\n");
523 EXPORT_SYMBOL_GPL(tpm_show_pcrs);
525 #define READ_PUBEK_RESULT_SIZE 314
526 static const u8 readpubek[] = {
527 0, 193, /* TPM_TAG_RQU_COMMAND */
528 0, 0, 0, 30, /* length */
529 0, 0, 0, 124, /* TPM_ORD_ReadPubek */
532 ssize_t tpm_show_pubek(struct device *dev, struct device_attribute *attr,
540 struct tpm_chip *chip = dev_get_drvdata(dev);
544 data = kzalloc(READ_PUBEK_RESULT_SIZE, GFP_KERNEL);
548 memcpy(data, readpubek, sizeof(readpubek));
550 err = transmit_cmd(chip, data, READ_PUBEK_RESULT_SIZE,
551 "attempting to read the PUBEK");
556 ignore header 10 bytes
557 algorithm 32 bits (1 == RSA )
560 parameters (RSA 12->bytes: keybit, #primes, expbit)
563 ignore checksum 20 bytes
568 "Algorithm: %02X %02X %02X %02X\nEncscheme: %02X %02X\n"
569 "Sigscheme: %02X %02X\nParameters: %02X %02X %02X %02X"
570 " %02X %02X %02X %02X %02X %02X %02X %02X\n"
571 "Modulus length: %d\nModulus: \n",
572 data[10], data[11], data[12], data[13], data[14],
573 data[15], data[16], data[17], data[22], data[23],
574 data[24], data[25], data[26], data[27], data[28],
575 data[29], data[30], data[31], data[32], data[33],
576 be32_to_cpu(*((__be32 *) (data + 34))));
578 for (i = 0; i < 256; i++) {
579 str += sprintf(str, "%02X ", data[i + 38]);
580 if ((i + 1) % 16 == 0)
581 str += sprintf(str, "\n");
588 EXPORT_SYMBOL_GPL(tpm_show_pubek);
590 #define CAP_VERSION_1_1 6
591 #define CAP_VERSION_IDX 13
592 static const u8 cap_version[] = {
593 0, 193, /* TPM_TAG_RQU_COMMAND */
594 0, 0, 0, 18, /* length */
595 0, 0, 0, 101, /* TPM_ORD_GetCapability */
600 ssize_t tpm_show_caps(struct device *dev, struct device_attribute *attr,
607 struct tpm_chip *chip = dev_get_drvdata(dev);
611 memcpy(data, tpm_cap, sizeof(tpm_cap));
612 data[TPM_CAP_IDX] = TPM_CAP_PROP;
613 data[TPM_CAP_SUBCAP_IDX] = TPM_CAP_PROP_MANUFACTURER;
615 rc = transmit_cmd(chip, data, sizeof(data),
616 "attempting to determine the manufacturer");
620 str += sprintf(str, "Manufacturer: 0x%x\n",
621 be32_to_cpu(*((__be32 *) (data + TPM_GET_CAP_RET_UINT32_1_IDX))));
623 memcpy(data, cap_version, sizeof(cap_version));
624 data[CAP_VERSION_IDX] = CAP_VERSION_1_1;
625 rc = transmit_cmd(chip, data, sizeof(data),
626 "attempting to determine the 1.1 version");
631 "TCG version: %d.%d\nFirmware version: %d.%d\n",
632 (int) data[14], (int) data[15], (int) data[16],
638 EXPORT_SYMBOL_GPL(tpm_show_caps);
640 ssize_t tpm_store_cancel(struct device *dev, struct device_attribute *attr,
641 const char *buf, size_t count)
643 struct tpm_chip *chip = dev_get_drvdata(dev);
647 chip->vendor.cancel(chip);
650 EXPORT_SYMBOL_GPL(tpm_store_cancel);
653 * Device file system interface to the TPM
655 int tpm_open(struct inode *inode, struct file *file)
657 int rc = 0, minor = iminor(inode);
658 struct tpm_chip *chip = NULL, *pos;
660 spin_lock(&driver_lock);
662 list_for_each_entry(pos, &tpm_chip_list, list) {
663 if (pos->vendor.miscdev.minor == minor) {
674 if (chip->num_opens) {
675 dev_dbg(chip->dev, "Another process owns this TPM\n");
681 get_device(chip->dev);
683 spin_unlock(&driver_lock);
685 chip->data_buffer = kmalloc(TPM_BUFSIZE * sizeof(u8), GFP_KERNEL);
686 if (chip->data_buffer == NULL) {
688 put_device(chip->dev);
692 atomic_set(&chip->data_pending, 0);
694 file->private_data = chip;
698 spin_unlock(&driver_lock);
701 EXPORT_SYMBOL_GPL(tpm_open);
703 int tpm_release(struct inode *inode, struct file *file)
705 struct tpm_chip *chip = file->private_data;
707 spin_lock(&driver_lock);
708 file->private_data = NULL;
710 del_singleshot_timer_sync(&chip->user_read_timer);
711 flush_scheduled_work();
712 atomic_set(&chip->data_pending, 0);
713 put_device(chip->dev);
714 kfree(chip->data_buffer);
715 spin_unlock(&driver_lock);
718 EXPORT_SYMBOL_GPL(tpm_release);
720 ssize_t tpm_write(struct file *file, const char __user *buf,
721 size_t size, loff_t *off)
723 struct tpm_chip *chip = file->private_data;
724 int in_size = size, out_size;
726 /* cannot perform a write until the read has cleared
727 either via tpm_read or a user_read_timer timeout */
728 while (atomic_read(&chip->data_pending) != 0)
731 down(&chip->buffer_mutex);
733 if (in_size > TPM_BUFSIZE)
734 in_size = TPM_BUFSIZE;
737 (chip->data_buffer, (void __user *) buf, in_size)) {
738 up(&chip->buffer_mutex);
742 /* atomic tpm command send and result receive */
743 out_size = tpm_transmit(chip, chip->data_buffer, TPM_BUFSIZE);
745 atomic_set(&chip->data_pending, out_size);
746 up(&chip->buffer_mutex);
748 /* Set a timeout by which the reader must come claim the result */
749 mod_timer(&chip->user_read_timer, jiffies + (60 * HZ));
753 EXPORT_SYMBOL_GPL(tpm_write);
755 ssize_t tpm_read(struct file *file, char __user *buf,
756 size_t size, loff_t *off)
758 struct tpm_chip *chip = file->private_data;
761 del_singleshot_timer_sync(&chip->user_read_timer);
762 flush_scheduled_work();
763 ret_size = atomic_read(&chip->data_pending);
764 atomic_set(&chip->data_pending, 0);
765 if (ret_size > 0) { /* relay data */
769 down(&chip->buffer_mutex);
770 if (copy_to_user(buf, chip->data_buffer, ret_size))
772 up(&chip->buffer_mutex);
777 EXPORT_SYMBOL_GPL(tpm_read);
779 void tpm_remove_hardware(struct device *dev)
781 struct tpm_chip *chip = dev_get_drvdata(dev);
784 dev_err(dev, "No device data found\n");
788 spin_lock(&driver_lock);
790 list_del(&chip->list);
792 spin_unlock(&driver_lock);
794 dev_set_drvdata(dev, NULL);
795 misc_deregister(&chip->vendor.miscdev);
796 kfree(chip->vendor.miscdev.name);
798 sysfs_remove_group(&dev->kobj, chip->vendor.attr_group);
799 tpm_bios_log_teardown(chip->bios_dir);
801 dev_mask[chip->dev_num / TPM_NUM_MASK_ENTRIES] &=
802 ~(1 << (chip->dev_num % TPM_NUM_MASK_ENTRIES));
808 EXPORT_SYMBOL_GPL(tpm_remove_hardware);
810 static u8 savestate[] = {
811 0, 193, /* TPM_TAG_RQU_COMMAND */
812 0, 0, 0, 10, /* blob length (in bytes) */
813 0, 0, 0, 152 /* TPM_ORD_SaveState */
817 * We are about to suspend. Save the TPM state
818 * so that it can be restored.
820 int tpm_pm_suspend(struct device *dev, pm_message_t pm_state)
822 struct tpm_chip *chip = dev_get_drvdata(dev);
826 tpm_transmit(chip, savestate, sizeof(savestate));
829 EXPORT_SYMBOL_GPL(tpm_pm_suspend);
832 * Resume from a power safe. The BIOS already restored
835 int tpm_pm_resume(struct device *dev)
837 struct tpm_chip *chip = dev_get_drvdata(dev);
844 EXPORT_SYMBOL_GPL(tpm_pm_resume);
847 * Called from tpm_<specific>.c probe function only for devices
848 * the driver has determined it should claim. Prior to calling
849 * this function the specific probe function has called pci_enable_device
850 * upon errant exit from this function specific probe function should call
853 struct tpm_chip *tpm_register_hardware(struct device *dev, const struct tpm_vendor_specific
856 #define DEVNAME_SIZE 7
859 struct tpm_chip *chip;
862 /* Driver specific per-device data */
863 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
867 init_MUTEX(&chip->buffer_mutex);
868 init_MUTEX(&chip->tpm_mutex);
869 INIT_LIST_HEAD(&chip->list);
871 INIT_WORK(&chip->work, timeout_work, chip);
873 init_timer(&chip->user_read_timer);
874 chip->user_read_timer.function = user_reader_timeout;
875 chip->user_read_timer.data = (unsigned long) chip;
877 memcpy(&chip->vendor, entry, sizeof(struct tpm_vendor_specific));
881 for (i = 0; i < TPM_NUM_MASK_ENTRIES; i++)
882 for (j = 0; j < 8 * sizeof(int); j++)
883 if ((dev_mask[i] & (1 << j)) == 0) {
885 i * TPM_NUM_MASK_ENTRIES + j;
886 dev_mask[i] |= 1 << j;
887 goto dev_num_search_complete;
890 dev_num_search_complete:
891 if (chip->dev_num < 0) {
892 dev_err(dev, "No available tpm device numbers\n");
895 } else if (chip->dev_num == 0)
896 chip->vendor.miscdev.minor = TPM_MINOR;
898 chip->vendor.miscdev.minor = MISC_DYNAMIC_MINOR;
900 devname = kmalloc(DEVNAME_SIZE, GFP_KERNEL);
901 scnprintf(devname, DEVNAME_SIZE, "%s%d", "tpm", chip->dev_num);
902 chip->vendor.miscdev.name = devname;
904 chip->vendor.miscdev.dev = dev;
905 chip->dev = get_device(dev);
907 if (misc_register(&chip->vendor.miscdev)) {
909 "unable to misc_register %s, minor %d\n",
910 chip->vendor.miscdev.name,
911 chip->vendor.miscdev.minor);
914 dev_mask[i] &= !(1 << j);
918 spin_lock(&driver_lock);
920 dev_set_drvdata(dev, chip);
922 list_add(&chip->list, &tpm_chip_list);
924 spin_unlock(&driver_lock);
926 sysfs_create_group(&dev->kobj, chip->vendor.attr_group);
928 chip->bios_dir = tpm_bios_log_setup(devname);
932 EXPORT_SYMBOL_GPL(tpm_register_hardware);
934 MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
935 MODULE_DESCRIPTION("TPM Driver");
936 MODULE_VERSION("2.0");
937 MODULE_LICENSE("GPL");