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/poll.h>
27 #include <linux/slab.h>
28 #include <linux/jiffies.h>
29 #include <linux/mutex.h>
30 #include <linux/spinlock.h>
31 #include <linux/freezer.h>
36 TPM_MINOR = 224, /* officially assigned */
38 TPM_NUM_DEVICES = 256,
48 #define TPM_MAX_ORDINAL 243
49 #define TPM_MAX_PROTECTED_ORDINAL 12
50 #define TPM_PROTECTED_ORDINAL_MASK 0xFF
53 * Bug workaround - some TPM's don't flush the most
54 * recently changed pcr on suspend, so force the flush
55 * with an extend to the selected _unused_ non-volatile pcr.
57 static int tpm_suspend_pcr;
58 module_param_named(suspend_pcr, tpm_suspend_pcr, uint, 0644);
59 MODULE_PARM_DESC(suspend_pcr,
60 "PCR to use for dummy writes to faciltate flush on suspend.");
62 static LIST_HEAD(tpm_chip_list);
63 static DEFINE_SPINLOCK(driver_lock);
64 static DECLARE_BITMAP(dev_mask, TPM_NUM_DEVICES);
67 * Array with one entry per ordinal defining the maximum amount
68 * of time the chip could take to return the result. The ordinal
69 * designation of short, medium or long is defined in a table in
70 * TCG Specification TPM Main Part 2 TPM Structures Section 17. The
71 * values of the SHORT, MEDIUM, and LONG durations are retrieved
72 * from the chip during initialization with a call to tpm_get_timeouts.
74 static const u8 tpm_protected_ordinal_duration[TPM_MAX_PROTECTED_ORDINAL] = {
75 TPM_UNDEFINED, /* 0 */
80 TPM_UNDEFINED, /* 5 */
89 static const u8 tpm_ordinal_duration[TPM_MAX_ORDINAL] = {
90 TPM_UNDEFINED, /* 0 */
95 TPM_UNDEFINED, /* 5 */
145 TPM_UNDEFINED, /* 55 */
165 TPM_UNDEFINED, /* 75 */
175 TPM_UNDEFINED, /* 85 */
185 TPM_UNDEFINED, /* 95 */
190 TPM_MEDIUM, /* 100 */
195 TPM_UNDEFINED, /* 105 */
225 TPM_UNDEFINED, /* 135 */
235 TPM_UNDEFINED, /* 145 */
245 TPM_UNDEFINED, /* 155 */
255 TPM_UNDEFINED, /* 165 */
265 TPM_UNDEFINED, /* 175 */
270 TPM_MEDIUM, /* 180 */
275 TPM_MEDIUM, /* 185 */
280 TPM_UNDEFINED, /* 190 */
285 TPM_UNDEFINED, /* 195 */
300 TPM_MEDIUM, /* 210 */
305 TPM_UNDEFINED, /* 215 */
315 TPM_UNDEFINED, /* 225 */
325 TPM_UNDEFINED, /* 235 */
335 static void user_reader_timeout(unsigned long ptr)
337 struct tpm_chip *chip = (struct tpm_chip *) ptr;
339 schedule_work(&chip->work);
342 static void timeout_work(struct work_struct *work)
344 struct tpm_chip *chip = container_of(work, struct tpm_chip, work);
346 mutex_lock(&chip->buffer_mutex);
347 atomic_set(&chip->data_pending, 0);
348 memset(chip->data_buffer, 0, TPM_BUFSIZE);
349 mutex_unlock(&chip->buffer_mutex);
352 static void set_needs_resume(struct tpm_chip *chip)
354 mutex_lock(&chip->resume_mutex);
355 chip->needs_resume = 1;
356 mutex_unlock(&chip->resume_mutex);
359 #define TPM_ORD_CONTINUE_SELFTEST 83
360 #define CONTINUE_SELFTEST_RESULT_SIZE 10
362 #define TPM_INTERNAL_RESULT_SIZE 200
363 #define TPM_TAG_RQU_COMMAND cpu_to_be16(193)
364 #define TPM_ORD_GET_CAP cpu_to_be32(101)
366 static const struct tpm_input_header tpm_getcap_header = {
367 .tag = TPM_TAG_RQU_COMMAND,
368 .length = cpu_to_be32(22),
369 .ordinal = TPM_ORD_GET_CAP
372 static struct tpm_input_header continue_selftest_header = {
373 .tag = TPM_TAG_RQU_COMMAND,
374 .length = cpu_to_be32(10),
375 .ordinal = cpu_to_be32(TPM_ORD_CONTINUE_SELFTEST),
379 * Internal kernel interface to transmit TPM commands
381 static ssize_t tpm_transmit(struct tpm_chip *chip, const char *buf,
388 if (bufsiz > TPM_BUFSIZE)
389 bufsiz = TPM_BUFSIZE;
391 count = be32_to_cpu(*((__be32 *) (buf + 2)));
392 ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
395 if (count > bufsiz) {
397 "invalid count value %x %zx \n", count, bufsiz);
401 mutex_lock(&chip->tpm_mutex);
403 if ((rc = chip->vendor.send(chip, (u8 *) buf, count)) < 0) {
405 "tpm_transmit: tpm_send: error %zd\n", rc);
409 if (chip->vendor.irq)
412 stop = jiffies + tpm_calc_ordinal_duration(chip, ordinal);
414 u8 status = chip->vendor.status(chip);
415 if ((status & chip->vendor.req_complete_mask) ==
416 chip->vendor.req_complete_val)
419 if ((status == chip->vendor.req_canceled)) {
420 dev_err(chip->dev, "Operation Canceled\n");
425 msleep(TPM_TIMEOUT); /* CHECK */
427 } while (time_before(jiffies, stop));
429 chip->vendor.cancel(chip);
430 dev_err(chip->dev, "Operation Timed out\n");
435 rc = chip->vendor.recv(chip, (u8 *) buf, bufsiz);
438 "tpm_transmit: tpm_recv: error %zd\n", rc);
440 mutex_unlock(&chip->tpm_mutex);
444 void tpm_continue_selftest_nocheck(struct tpm_chip *chip)
446 struct tpm_cmd_t cmd;
447 cmd.header.in = continue_selftest_header;
448 tpm_transmit(chip, (u8 *) &cmd, CONTINUE_SELFTEST_RESULT_SIZE);
450 EXPORT_SYMBOL_GPL(tpm_continue_selftest_nocheck);
452 /* The maximum time in milliseconds that the TPM self test will take to
453 * complete. TODO(semenzato): 1s should be plenty for all TPMs, but how can we
456 #define TPM_SELF_TEST_DURATION_MSEC 1000
458 /* We don't want to wait for the self test to complete at resume, because it
459 * impacts the resume speed. TPM commands are infrequent so the wait is
460 * usually not needed and is wasteful. Instead, before we send any command, we
461 * check that enough time has elapsed from the resume so that we are
462 * comfortable that the self test has completed. If not, we wait. Unlike at
463 * boot, here we don't check the return code of continue_self_test, so we can
464 * use a code path which avoids recursion. Furthermore, this only works when
465 * ContinueSelfTest is blocking, that is it returns only after the self test
466 * has completed, which is the case for the Infineon TPM.
468 static void resume_if_needed(struct tpm_chip *chip)
470 mutex_lock(&chip->resume_mutex);
471 if (chip->needs_resume) {
472 dev_info(chip->dev, "waiting for TPM self test\n");
473 tpm_continue_selftest_nocheck(chip);
474 chip->needs_resume = 0;
475 dev_info(chip->dev, "TPM delayed resume completed\n");
477 mutex_unlock(&chip->resume_mutex);
480 ssize_t tpm_transmit_cmd(struct tpm_chip *chip, struct tpm_cmd_t *cmd,
481 int len, const char *desc)
485 resume_if_needed(chip);
487 len = tpm_transmit(chip,(u8 *) cmd, len);
490 else if (len < TPM_HEADER_SIZE)
493 err = be32_to_cpu(cmd->header.out.return_code);
495 dev_err(chip->dev, "A TPM error (%d) occurred %s\n", err, desc);
499 EXPORT_SYMBOL_GPL(tpm_transmit_cmd);
502 * tpm_continue_selftest -- run TPM's selftest
503 * @chip: TPM chip to use
505 * Returns 0 on success, < 0 in case of fatal error or a value > 0 representing
508 int tpm_continue_selftest(struct tpm_chip *chip)
511 struct tpm_cmd_t cmd;
513 cmd.header.in = continue_selftest_header;
514 rc = tpm_transmit_cmd(chip, &cmd,
515 CONTINUE_SELFTEST_RESULT_SIZE, "continue selftest");
518 EXPORT_SYMBOL_GPL(tpm_continue_selftest);
521 * Returns max number of jiffies to wait
523 unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip,
526 int duration_idx = TPM_UNDEFINED;
529 if (ordinal < TPM_MAX_ORDINAL)
530 duration_idx = tpm_ordinal_duration[ordinal];
531 else if ((ordinal & TPM_PROTECTED_ORDINAL_MASK) <
532 TPM_MAX_PROTECTED_ORDINAL)
534 tpm_protected_ordinal_duration[ordinal &
535 TPM_PROTECTED_ORDINAL_MASK];
537 if (duration_idx != TPM_UNDEFINED)
538 duration = chip->vendor.duration[duration_idx];
544 EXPORT_SYMBOL_GPL(tpm_calc_ordinal_duration);
546 #define TPM_DIGEST_SIZE 20
547 #define TPM_RET_CODE_IDX 6
549 enum tpm_capabilities {
550 TPM_CAP_FLAG = cpu_to_be32(4),
551 TPM_CAP_PROP = cpu_to_be32(5),
552 CAP_VERSION_1_1 = cpu_to_be32(0x06),
553 CAP_VERSION_1_2 = cpu_to_be32(0x1A)
556 enum tpm_sub_capabilities {
557 TPM_CAP_PROP_PCR = cpu_to_be32(0x101),
558 TPM_CAP_PROP_MANUFACTURER = cpu_to_be32(0x103),
559 TPM_CAP_FLAG_PERM = cpu_to_be32(0x108),
560 TPM_CAP_FLAG_VOL = cpu_to_be32(0x109),
561 TPM_CAP_PROP_OWNER = cpu_to_be32(0x111),
562 TPM_CAP_PROP_TIS_TIMEOUT = cpu_to_be32(0x115),
563 TPM_CAP_PROP_TIS_DURATION = cpu_to_be32(0x120),
567 ssize_t tpm_getcap(struct device *dev, __be32 subcap_id, cap_t *cap,
570 struct tpm_cmd_t tpm_cmd;
572 struct tpm_chip *chip = dev_get_drvdata(dev);
574 tpm_cmd.header.in = tpm_getcap_header;
575 if (subcap_id == CAP_VERSION_1_1 || subcap_id == CAP_VERSION_1_2) {
576 tpm_cmd.params.getcap_in.cap = subcap_id;
577 /*subcap field not necessary */
578 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(0);
579 tpm_cmd.header.in.length -= cpu_to_be32(sizeof(__be32));
581 if (subcap_id == TPM_CAP_FLAG_PERM ||
582 subcap_id == TPM_CAP_FLAG_VOL)
583 tpm_cmd.params.getcap_in.cap = TPM_CAP_FLAG;
585 tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
586 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
587 tpm_cmd.params.getcap_in.subcap = subcap_id;
589 rc = tpm_transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE, desc);
591 *cap = tpm_cmd.params.getcap_out.cap;
595 void tpm_gen_interrupt(struct tpm_chip *chip)
597 struct tpm_cmd_t tpm_cmd;
600 tpm_cmd.header.in = tpm_getcap_header;
601 tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
602 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
603 tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_TIMEOUT;
605 rc = tpm_transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE,
606 "attempting to determine the timeouts");
608 EXPORT_SYMBOL_GPL(tpm_gen_interrupt);
610 int tpm_get_timeouts(struct tpm_chip *chip)
612 struct tpm_cmd_t tpm_cmd;
613 struct timeout_t *timeout_cap;
614 struct duration_t *duration_cap;
617 unsigned int scale = 1;
619 tpm_cmd.header.in = tpm_getcap_header;
620 tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
621 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
622 tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_TIMEOUT;
624 rc = tpm_transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE,
625 "attempting to determine the timeouts");
629 if (be32_to_cpu(tpm_cmd.header.out.return_code) != 0 ||
630 be32_to_cpu(tpm_cmd.header.out.length)
631 != sizeof(tpm_cmd.header.out) + sizeof(u32) + 4 * sizeof(u32))
634 timeout_cap = &tpm_cmd.params.getcap_out.cap.timeout;
635 /* Don't overwrite default if value is 0 */
636 timeout = be32_to_cpu(timeout_cap->a);
637 if (timeout && timeout < 1000) {
638 /* timeouts in msec rather usec */
640 chip->vendor.timeout_adjusted = true;
643 chip->vendor.timeout_a = usecs_to_jiffies(timeout * scale);
644 timeout = be32_to_cpu(timeout_cap->b);
646 chip->vendor.timeout_b = usecs_to_jiffies(timeout * scale);
647 timeout = be32_to_cpu(timeout_cap->c);
649 chip->vendor.timeout_c = usecs_to_jiffies(timeout * scale);
650 timeout = be32_to_cpu(timeout_cap->d);
652 chip->vendor.timeout_d = usecs_to_jiffies(timeout * scale);
655 tpm_cmd.header.in = tpm_getcap_header;
656 tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
657 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
658 tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_DURATION;
660 rc = tpm_transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE,
661 "attempting to determine the durations");
665 if (be32_to_cpu(tpm_cmd.header.out.return_code) != 0 ||
666 be32_to_cpu(tpm_cmd.header.out.length)
667 != sizeof(tpm_cmd.header.out) + sizeof(u32) + 3 * sizeof(u32))
670 duration_cap = &tpm_cmd.params.getcap_out.cap.duration;
671 chip->vendor.duration[TPM_SHORT] =
672 usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_short));
673 chip->vendor.duration[TPM_MEDIUM] =
674 usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_medium));
675 chip->vendor.duration[TPM_LONG] =
676 usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_long));
678 /* The Broadcom BCM0102 chipset in a Dell Latitude D820 gets the above
679 * value wrong and apparently reports msecs rather than usecs. So we
680 * fix up the resulting too-small TPM_SHORT value to make things work.
681 * We also scale the TPM_MEDIUM and -_LONG values by 1000.
683 if (chip->vendor.duration[TPM_SHORT] < (HZ / 100)) {
684 chip->vendor.duration[TPM_SHORT] = HZ;
685 chip->vendor.duration[TPM_MEDIUM] *= 1000;
686 chip->vendor.duration[TPM_LONG] *= 1000;
687 chip->vendor.duration_adjusted = true;
688 dev_info(chip->dev, "Adjusting TPM timeout parameters.\n");
692 EXPORT_SYMBOL_GPL(tpm_get_timeouts);
694 ssize_t tpm_show_enabled(struct device * dev, struct device_attribute * attr,
700 rc = tpm_getcap(dev, TPM_CAP_FLAG_PERM, &cap,
701 "attempting to determine the permanent enabled state");
705 rc = sprintf(buf, "%d\n", !cap.perm_flags.disable);
708 EXPORT_SYMBOL_GPL(tpm_show_enabled);
710 ssize_t tpm_show_active(struct device * dev, struct device_attribute * attr,
716 rc = tpm_getcap(dev, TPM_CAP_FLAG_PERM, &cap,
717 "attempting to determine the permanent active state");
721 rc = sprintf(buf, "%d\n", !cap.perm_flags.deactivated);
724 EXPORT_SYMBOL_GPL(tpm_show_active);
726 ssize_t tpm_show_owned(struct device * dev, struct device_attribute * attr,
732 rc = tpm_getcap(dev, TPM_CAP_PROP_OWNER, &cap,
733 "attempting to determine the owner state");
737 rc = sprintf(buf, "%d\n", cap.owned);
740 EXPORT_SYMBOL_GPL(tpm_show_owned);
742 ssize_t tpm_show_temp_deactivated(struct device * dev,
743 struct device_attribute * attr, char *buf)
748 rc = tpm_getcap(dev, TPM_CAP_FLAG_VOL, &cap,
749 "attempting to determine the temporary state");
753 rc = sprintf(buf, "%d\n", cap.stclear_flags.deactivated);
756 EXPORT_SYMBOL_GPL(tpm_show_temp_deactivated);
759 * tpm_chip_find_get - return tpm_chip for given chip number
761 static struct tpm_chip *tpm_chip_find_get(int chip_num)
763 struct tpm_chip *pos, *chip = NULL;
766 list_for_each_entry_rcu(pos, &tpm_chip_list, list) {
767 if (chip_num != TPM_ANY_NUM && chip_num != pos->dev_num)
770 if (try_module_get(pos->dev->driver->owner)) {
779 #define TPM_ORDINAL_PCRREAD cpu_to_be32(21)
780 #define READ_PCR_RESULT_SIZE 30
781 static struct tpm_input_header pcrread_header = {
782 .tag = TPM_TAG_RQU_COMMAND,
783 .length = cpu_to_be32(14),
784 .ordinal = TPM_ORDINAL_PCRREAD
787 static int __tpm_pcr_read(struct tpm_chip *chip, int pcr_idx, u8 *res_buf)
790 struct tpm_cmd_t cmd;
792 cmd.header.in = pcrread_header;
793 cmd.params.pcrread_in.pcr_idx = cpu_to_be32(pcr_idx);
794 rc = tpm_transmit_cmd(chip, &cmd, READ_PCR_RESULT_SIZE,
795 "attempting to read a pcr value");
798 memcpy(res_buf, cmd.params.pcrread_out.pcr_result,
804 * tpm_pcr_read - read a pcr value
805 * @chip_num: tpm idx # or ANY
806 * @pcr_idx: pcr idx to retrieve
807 * @res_buf: TPM_PCR value
808 * size of res_buf is 20 bytes (or NULL if you don't care)
810 * The TPM driver should be built-in, but for whatever reason it
811 * isn't, protect against the chip disappearing, by incrementing
812 * the module usage count.
814 int tpm_pcr_read(u32 chip_num, int pcr_idx, u8 *res_buf)
816 struct tpm_chip *chip;
819 chip = tpm_chip_find_get(chip_num);
822 rc = __tpm_pcr_read(chip, pcr_idx, res_buf);
826 EXPORT_SYMBOL_GPL(tpm_pcr_read);
829 * tpm_pcr_extend - extend pcr value with hash
830 * @chip_num: tpm idx # or AN&
831 * @pcr_idx: pcr idx to extend
832 * @hash: hash value used to extend pcr value
834 * The TPM driver should be built-in, but for whatever reason it
835 * isn't, protect against the chip disappearing, by incrementing
836 * the module usage count.
838 #define TPM_ORD_PCR_EXTEND cpu_to_be32(20)
839 #define EXTEND_PCR_RESULT_SIZE 34
840 static struct tpm_input_header pcrextend_header = {
841 .tag = TPM_TAG_RQU_COMMAND,
842 .length = cpu_to_be32(34),
843 .ordinal = TPM_ORD_PCR_EXTEND
846 int tpm_pcr_extend(u32 chip_num, int pcr_idx, const u8 *hash)
848 struct tpm_cmd_t cmd;
850 struct tpm_chip *chip;
852 chip = tpm_chip_find_get(chip_num);
856 cmd.header.in = pcrextend_header;
857 cmd.params.pcrextend_in.pcr_idx = cpu_to_be32(pcr_idx);
858 memcpy(cmd.params.pcrextend_in.hash, hash, TPM_DIGEST_SIZE);
859 rc = tpm_transmit_cmd(chip, &cmd, EXTEND_PCR_RESULT_SIZE,
860 "attempting extend a PCR value");
865 EXPORT_SYMBOL_GPL(tpm_pcr_extend);
868 * tpm_do_selftest - have the TPM continue its selftest and wait until it
869 * can receive further commands
870 * @chip: TPM chip to use
872 * Returns 0 on success, < 0 in case of fatal error or a value > 0 representing
875 int tpm_do_selftest(struct tpm_chip *chip)
878 u8 digest[TPM_DIGEST_SIZE];
880 unsigned int delay_msec = 1000;
881 unsigned long duration;
883 duration = tpm_calc_ordinal_duration(chip,
884 TPM_ORD_CONTINUE_SELFTEST);
886 loops = jiffies_to_msecs(duration) / delay_msec;
888 rc = tpm_continue_selftest(chip);
889 /* This may fail if there was no TPM driver during a suspend/resume
890 * cycle; some may return 10 (BAD_ORDINAL), others 28 (FAILEDSELFTEST)
896 rc = __tpm_pcr_read(chip, 0, digest);
897 if (rc == TPM_ERR_DISABLED || rc == TPM_ERR_DEACTIVATED) {
899 "TPM is disabled/deactivated (0x%X)\n", rc);
900 /* TPM is disabled and/or deactivated; driver can
901 * proceed and TPM does handle commands for
902 * suspend/resume correctly
906 if (rc != TPM_WARN_DOING_SELFTEST)
909 } while (--loops > 0);
913 EXPORT_SYMBOL_GPL(tpm_do_selftest);
915 int tpm_send(u32 chip_num, void *cmd, size_t buflen)
917 struct tpm_chip *chip;
920 chip = tpm_chip_find_get(chip_num);
924 rc = tpm_transmit_cmd(chip, cmd, buflen, "attempting tpm_cmd");
929 EXPORT_SYMBOL_GPL(tpm_send);
931 ssize_t tpm_show_pcrs(struct device *dev, struct device_attribute *attr,
935 u8 digest[TPM_DIGEST_SIZE];
939 struct tpm_chip *chip = dev_get_drvdata(dev);
941 rc = tpm_getcap(dev, TPM_CAP_PROP_PCR, &cap,
942 "attempting to determine the number of PCRS");
946 num_pcrs = be32_to_cpu(cap.num_pcrs);
947 for (i = 0; i < num_pcrs; i++) {
948 rc = __tpm_pcr_read(chip, i, digest);
951 str += sprintf(str, "PCR-%02d: ", i);
952 for (j = 0; j < TPM_DIGEST_SIZE; j++)
953 str += sprintf(str, "%02X ", digest[j]);
954 str += sprintf(str, "\n");
958 EXPORT_SYMBOL_GPL(tpm_show_pcrs);
960 #define READ_PUBEK_RESULT_SIZE 314
961 #define TPM_ORD_READPUBEK cpu_to_be32(124)
962 struct tpm_input_header tpm_readpubek_header = {
963 .tag = TPM_TAG_RQU_COMMAND,
964 .length = cpu_to_be32(30),
965 .ordinal = TPM_ORD_READPUBEK
968 ssize_t tpm_show_pubek(struct device *dev, struct device_attribute *attr,
972 struct tpm_cmd_t tpm_cmd;
977 struct tpm_chip *chip = dev_get_drvdata(dev);
979 tpm_cmd.header.in = tpm_readpubek_header;
980 err = tpm_transmit_cmd(chip, &tpm_cmd, READ_PUBEK_RESULT_SIZE,
981 "attempting to read the PUBEK");
986 ignore header 10 bytes
987 algorithm 32 bits (1 == RSA )
990 parameters (RSA 12->bytes: keybit, #primes, expbit)
993 ignore checksum 20 bytes
995 data = tpm_cmd.params.readpubek_out_buffer;
998 "Algorithm: %02X %02X %02X %02X\n"
999 "Encscheme: %02X %02X\n"
1000 "Sigscheme: %02X %02X\n"
1001 "Parameters: %02X %02X %02X %02X "
1002 "%02X %02X %02X %02X "
1003 "%02X %02X %02X %02X\n"
1004 "Modulus length: %d\n"
1006 data[0], data[1], data[2], data[3],
1009 data[12], data[13], data[14], data[15],
1010 data[16], data[17], data[18], data[19],
1011 data[20], data[21], data[22], data[23],
1012 be32_to_cpu(*((__be32 *) (data + 24))));
1014 for (i = 0; i < 256; i++) {
1015 str += sprintf(str, "%02X ", data[i + 28]);
1016 if ((i + 1) % 16 == 0)
1017 str += sprintf(str, "\n");
1023 EXPORT_SYMBOL_GPL(tpm_show_pubek);
1026 ssize_t tpm_show_caps(struct device *dev, struct device_attribute *attr,
1033 rc = tpm_getcap(dev, TPM_CAP_PROP_MANUFACTURER, &cap,
1034 "attempting to determine the manufacturer");
1037 str += sprintf(str, "Manufacturer: 0x%x\n",
1038 be32_to_cpu(cap.manufacturer_id));
1040 rc = tpm_getcap(dev, CAP_VERSION_1_1, &cap,
1041 "attempting to determine the 1.1 version");
1045 "TCG version: %d.%d\nFirmware version: %d.%d\n",
1046 cap.tpm_version.Major, cap.tpm_version.Minor,
1047 cap.tpm_version.revMajor, cap.tpm_version.revMinor);
1050 EXPORT_SYMBOL_GPL(tpm_show_caps);
1052 ssize_t tpm_show_caps_1_2(struct device * dev,
1053 struct device_attribute * attr, char *buf)
1059 rc = tpm_getcap(dev, TPM_CAP_PROP_MANUFACTURER, &cap,
1060 "attempting to determine the manufacturer");
1063 str += sprintf(str, "Manufacturer: 0x%x\n",
1064 be32_to_cpu(cap.manufacturer_id));
1065 rc = tpm_getcap(dev, CAP_VERSION_1_2, &cap,
1066 "attempting to determine the 1.2 version");
1070 "TCG version: %d.%d\nFirmware version: %d.%d\n",
1071 cap.tpm_version_1_2.Major, cap.tpm_version_1_2.Minor,
1072 cap.tpm_version_1_2.revMajor,
1073 cap.tpm_version_1_2.revMinor);
1076 EXPORT_SYMBOL_GPL(tpm_show_caps_1_2);
1078 ssize_t tpm_show_durations(struct device *dev, struct device_attribute *attr,
1081 struct tpm_chip *chip = dev_get_drvdata(dev);
1083 if (chip->vendor.duration[TPM_LONG] == 0)
1086 return sprintf(buf, "%d %d %d [%s]\n",
1087 jiffies_to_usecs(chip->vendor.duration[TPM_SHORT]),
1088 jiffies_to_usecs(chip->vendor.duration[TPM_MEDIUM]),
1089 jiffies_to_usecs(chip->vendor.duration[TPM_LONG]),
1090 chip->vendor.duration_adjusted
1091 ? "adjusted" : "original");
1093 EXPORT_SYMBOL_GPL(tpm_show_durations);
1095 ssize_t tpm_show_timeouts(struct device *dev, struct device_attribute *attr,
1098 struct tpm_chip *chip = dev_get_drvdata(dev);
1100 return sprintf(buf, "%d %d %d %d [%s]\n",
1101 jiffies_to_usecs(chip->vendor.timeout_a),
1102 jiffies_to_usecs(chip->vendor.timeout_b),
1103 jiffies_to_usecs(chip->vendor.timeout_c),
1104 jiffies_to_usecs(chip->vendor.timeout_d),
1105 chip->vendor.timeout_adjusted
1106 ? "adjusted" : "original");
1108 EXPORT_SYMBOL_GPL(tpm_show_timeouts);
1110 ssize_t tpm_store_cancel(struct device *dev, struct device_attribute *attr,
1111 const char *buf, size_t count)
1113 struct tpm_chip *chip = dev_get_drvdata(dev);
1117 chip->vendor.cancel(chip);
1120 EXPORT_SYMBOL_GPL(tpm_store_cancel);
1122 int wait_for_tpm_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout,
1123 wait_queue_head_t *queue)
1129 /* check current status */
1130 status = chip->vendor.status(chip);
1131 if ((status & mask) == mask)
1134 stop = jiffies + timeout;
1136 if (chip->vendor.irq) {
1138 timeout = stop - jiffies;
1139 if ((long)timeout <= 0)
1141 rc = wait_event_interruptible_timeout(*queue,
1142 ((chip->vendor.status(chip)
1147 if (rc == -ERESTARTSYS && freezing(current)) {
1148 clear_thread_flag(TIF_SIGPENDING);
1153 msleep(TPM_TIMEOUT);
1154 status = chip->vendor.status(chip);
1155 if ((status & mask) == mask)
1157 } while (time_before(jiffies, stop));
1161 EXPORT_SYMBOL_GPL(wait_for_tpm_stat);
1163 * Device file system interface to the TPM
1165 * It's assured that the chip will be opened just once,
1166 * by the check of is_open variable, which is protected
1169 int tpm_open(struct inode *inode, struct file *file)
1171 int minor = iminor(inode);
1172 struct tpm_chip *chip = NULL, *pos;
1175 list_for_each_entry_rcu(pos, &tpm_chip_list, list) {
1176 if (pos->vendor.miscdev.minor == minor) {
1178 get_device(chip->dev);
1187 if (test_and_set_bit(0, &chip->is_open)) {
1188 dev_dbg(chip->dev, "Another process owns this TPM\n");
1189 put_device(chip->dev);
1193 chip->data_buffer = kzalloc(TPM_BUFSIZE, GFP_KERNEL);
1194 if (chip->data_buffer == NULL) {
1195 clear_bit(0, &chip->is_open);
1196 put_device(chip->dev);
1200 atomic_set(&chip->data_pending, 0);
1202 file->private_data = chip;
1205 EXPORT_SYMBOL_GPL(tpm_open);
1208 * Called on file close
1210 int tpm_release(struct inode *inode, struct file *file)
1212 struct tpm_chip *chip = file->private_data;
1214 del_singleshot_timer_sync(&chip->user_read_timer);
1215 flush_work_sync(&chip->work);
1216 file->private_data = NULL;
1217 atomic_set(&chip->data_pending, 0);
1218 kfree(chip->data_buffer);
1219 clear_bit(0, &chip->is_open);
1220 put_device(chip->dev);
1223 EXPORT_SYMBOL_GPL(tpm_release);
1225 ssize_t tpm_write(struct file *file, const char __user *buf,
1226 size_t size, loff_t *off)
1228 struct tpm_chip *chip = file->private_data;
1229 size_t in_size = size, out_size;
1231 /* cannot perform a write until the read has cleared
1232 either via tpm_read or a user_read_timer timeout */
1233 while (atomic_read(&chip->data_pending) != 0)
1234 msleep(TPM_TIMEOUT);
1236 resume_if_needed(chip);
1238 mutex_lock(&chip->buffer_mutex);
1240 if (in_size > TPM_BUFSIZE)
1241 in_size = TPM_BUFSIZE;
1244 (chip->data_buffer, (void __user *) buf, in_size)) {
1245 mutex_unlock(&chip->buffer_mutex);
1249 /* atomic tpm command send and result receive */
1250 out_size = tpm_transmit(chip, chip->data_buffer, TPM_BUFSIZE);
1252 atomic_set(&chip->data_pending, out_size);
1253 mutex_unlock(&chip->buffer_mutex);
1255 /* Set a timeout by which the reader must come claim the result */
1256 mod_timer(&chip->user_read_timer, jiffies + (60 * HZ));
1260 EXPORT_SYMBOL_GPL(tpm_write);
1262 ssize_t tpm_read(struct file *file, char __user *buf,
1263 size_t size, loff_t *off)
1265 struct tpm_chip *chip = file->private_data;
1269 del_singleshot_timer_sync(&chip->user_read_timer);
1270 flush_work_sync(&chip->work);
1271 ret_size = atomic_read(&chip->data_pending);
1272 /* TODO(wad): atomic_set should come AFTER the buffer is copied. */
1273 atomic_set(&chip->data_pending, 0);
1274 if (ret_size > 0) { /* relay data */
1275 ssize_t orig_ret_size = ret_size;
1276 if (size < ret_size)
1279 mutex_lock(&chip->buffer_mutex);
1280 rc = copy_to_user(buf, chip->data_buffer, ret_size);
1281 memset(chip->data_buffer, 0, orig_ret_size);
1285 mutex_unlock(&chip->buffer_mutex);
1290 EXPORT_SYMBOL_GPL(tpm_read);
1292 void tpm_remove_hardware(struct device *dev)
1294 struct tpm_chip *chip = dev_get_drvdata(dev);
1297 dev_err(dev, "No device data found\n");
1301 spin_lock(&driver_lock);
1302 list_del_rcu(&chip->list);
1303 spin_unlock(&driver_lock);
1306 misc_deregister(&chip->vendor.miscdev);
1307 sysfs_remove_group(&dev->kobj, chip->vendor.attr_group);
1308 tpm_bios_log_teardown(chip->bios_dir);
1310 /* write it this way to be explicit (chip->dev == dev) */
1311 put_device(chip->dev);
1313 EXPORT_SYMBOL_GPL(tpm_remove_hardware);
1315 #define TPM_ORD_SAVESTATE cpu_to_be32(152)
1316 #define SAVESTATE_RESULT_SIZE 10
1318 static struct tpm_input_header savestate_header = {
1319 .tag = TPM_TAG_RQU_COMMAND,
1320 .length = cpu_to_be32(10),
1321 .ordinal = TPM_ORD_SAVESTATE
1325 * We are about to suspend. Save the TPM state
1326 * so that it can be restored.
1328 int tpm_pm_suspend(struct device *dev, pm_message_t pm_state)
1330 struct tpm_chip *chip = dev_get_drvdata(dev);
1331 struct tpm_cmd_t cmd;
1334 u8 dummy_hash[TPM_DIGEST_SIZE] = { 0 };
1339 /* for buggy tpm, flush pcrs with extend to selected dummy */
1340 if (tpm_suspend_pcr) {
1341 cmd.header.in = pcrextend_header;
1342 cmd.params.pcrextend_in.pcr_idx = cpu_to_be32(tpm_suspend_pcr);
1343 memcpy(cmd.params.pcrextend_in.hash, dummy_hash,
1345 rc = tpm_transmit_cmd(chip, &cmd, EXTEND_PCR_RESULT_SIZE,
1346 "extending dummy pcr before suspend");
1349 /* now do the actual savestate */
1350 for (try = 0; try < TPM_RETRY; try++) {
1351 cmd.header.in = savestate_header;
1352 rc = tpm_transmit_cmd(chip, &cmd, SAVESTATE_RESULT_SIZE, NULL);
1355 * If the TPM indicates that it is too busy to respond to
1356 * this command then retry before giving up. It can take
1357 * several seconds for this TPM to be ready.
1359 * This can happen if the TPM has already been sent the
1360 * SaveState command before the driver has loaded. TCG 1.2
1361 * specification states that any communication after SaveState
1362 * may cause the TPM to invalidate previously saved state.
1364 if (rc != TPM_WARN_RETRY)
1366 msleep(TPM_TIMEOUT_RETRY);
1371 "Error (%d) sending savestate before suspend\n", rc);
1373 dev_warn(chip->dev, "TPM savestate took %dms\n",
1374 try * TPM_TIMEOUT_RETRY);
1378 EXPORT_SYMBOL_GPL(tpm_pm_suspend);
1381 * Resume from a power safe. The BIOS already restored
1384 int tpm_pm_resume(struct device *dev)
1386 struct tpm_chip *chip = dev_get_drvdata(dev);
1391 set_needs_resume(chip);
1394 EXPORT_SYMBOL_GPL(tpm_pm_resume);
1396 /* In case vendor provided release function, call it too.*/
1398 void tpm_dev_vendor_release(struct tpm_chip *chip)
1400 if (chip->vendor.release)
1401 chip->vendor.release(chip->dev);
1403 clear_bit(chip->dev_num, dev_mask);
1404 kfree(chip->vendor.miscdev.name);
1406 EXPORT_SYMBOL_GPL(tpm_dev_vendor_release);
1410 * Once all references to platform device are down to 0,
1411 * release all allocated structures.
1413 void tpm_dev_release(struct device *dev)
1415 struct tpm_chip *chip = dev_get_drvdata(dev);
1417 tpm_dev_vendor_release(chip);
1422 EXPORT_SYMBOL_GPL(tpm_dev_release);
1425 * Called from tpm_<specific>.c probe function only for devices
1426 * the driver has determined it should claim. Prior to calling
1427 * this function the specific probe function has called pci_enable_device
1428 * upon errant exit from this function specific probe function should call
1429 * pci_disable_device
1431 struct tpm_chip *tpm_register_hardware(struct device *dev,
1432 const struct tpm_vendor_specific *entry)
1434 #define DEVNAME_SIZE 7
1437 struct tpm_chip *chip;
1439 /* Driver specific per-device data */
1440 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
1441 devname = kmalloc(DEVNAME_SIZE, GFP_KERNEL);
1443 if (chip == NULL || devname == NULL)
1446 mutex_init(&chip->buffer_mutex);
1447 mutex_init(&chip->tpm_mutex);
1448 mutex_init(&chip->resume_mutex);
1449 INIT_LIST_HEAD(&chip->list);
1451 INIT_WORK(&chip->work, timeout_work);
1453 setup_timer(&chip->user_read_timer, user_reader_timeout,
1454 (unsigned long)chip);
1456 memcpy(&chip->vendor, entry, sizeof(struct tpm_vendor_specific));
1458 chip->dev_num = find_first_zero_bit(dev_mask, TPM_NUM_DEVICES);
1460 if (chip->dev_num >= TPM_NUM_DEVICES) {
1461 dev_err(dev, "No available tpm device numbers\n");
1463 } else if (chip->dev_num == 0)
1464 chip->vendor.miscdev.minor = TPM_MINOR;
1466 chip->vendor.miscdev.minor = MISC_DYNAMIC_MINOR;
1468 set_bit(chip->dev_num, dev_mask);
1470 scnprintf(devname, DEVNAME_SIZE, "%s%d", "tpm", chip->dev_num);
1471 chip->vendor.miscdev.name = devname;
1473 chip->vendor.miscdev.parent = dev;
1474 chip->dev = get_device(dev);
1475 chip->release = dev->release;
1476 dev->release = tpm_dev_release;
1477 dev_set_drvdata(dev, chip);
1479 if (misc_register(&chip->vendor.miscdev)) {
1481 "unable to misc_register %s, minor %d\n",
1482 chip->vendor.miscdev.name,
1483 chip->vendor.miscdev.minor);
1484 put_device(chip->dev);
1488 if (sysfs_create_group(&dev->kobj, chip->vendor.attr_group)) {
1489 misc_deregister(&chip->vendor.miscdev);
1490 put_device(chip->dev);
1495 chip->bios_dir = tpm_bios_log_setup(devname);
1497 /* Make chip available */
1498 spin_lock(&driver_lock);
1499 list_add_rcu(&chip->list, &tpm_chip_list);
1500 spin_unlock(&driver_lock);
1509 EXPORT_SYMBOL_GPL(tpm_register_hardware);
1511 MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
1512 MODULE_DESCRIPTION("TPM Driver");
1513 MODULE_VERSION("2.0");
1514 MODULE_LICENSE("GPL");