19deb0eb7SJason Gunthorpe /* 29deb0eb7SJason Gunthorpe * Copyright (C) 2004 IBM Corporation 39deb0eb7SJason Gunthorpe * 49deb0eb7SJason Gunthorpe * Authors: 59deb0eb7SJason Gunthorpe * Leendert van Doorn <leendert@watson.ibm.com> 69deb0eb7SJason Gunthorpe * Dave Safford <safford@watson.ibm.com> 79deb0eb7SJason Gunthorpe * Reiner Sailer <sailer@watson.ibm.com> 89deb0eb7SJason Gunthorpe * Kylene Hall <kjhall@us.ibm.com> 99deb0eb7SJason Gunthorpe * 109deb0eb7SJason Gunthorpe * Maintained by: <tpmdd-devel@lists.sourceforge.net> 119deb0eb7SJason Gunthorpe * 129deb0eb7SJason Gunthorpe * Device driver for TCG/TCPA TPM (trusted platform module). 139deb0eb7SJason Gunthorpe * Specifications at www.trustedcomputinggroup.org 149deb0eb7SJason Gunthorpe * 159deb0eb7SJason Gunthorpe * This program is free software; you can redistribute it and/or 169deb0eb7SJason Gunthorpe * modify it under the terms of the GNU General Public License as 179deb0eb7SJason Gunthorpe * published by the Free Software Foundation, version 2 of the 189deb0eb7SJason Gunthorpe * License. 199deb0eb7SJason Gunthorpe * 209deb0eb7SJason Gunthorpe * Note, the TPM chip is not interrupt driven (only polling) 219deb0eb7SJason Gunthorpe * and can have very long timeouts (minutes!). Hence the unusual 229deb0eb7SJason Gunthorpe * calls to msleep. 239deb0eb7SJason Gunthorpe * 249deb0eb7SJason Gunthorpe */ 259deb0eb7SJason Gunthorpe 269deb0eb7SJason Gunthorpe #include <linux/poll.h> 279deb0eb7SJason Gunthorpe #include <linux/slab.h> 289deb0eb7SJason Gunthorpe #include <linux/mutex.h> 299deb0eb7SJason Gunthorpe #include <linux/spinlock.h> 309deb0eb7SJason Gunthorpe #include <linux/freezer.h> 319deb0eb7SJason Gunthorpe 329deb0eb7SJason Gunthorpe #include "tpm.h" 339deb0eb7SJason Gunthorpe #include "tpm_eventlog.h" 349deb0eb7SJason Gunthorpe 359deb0eb7SJason Gunthorpe #define TPM_MAX_ORDINAL 243 369deb0eb7SJason Gunthorpe #define TSC_MAX_ORDINAL 12 379deb0eb7SJason Gunthorpe #define TPM_PROTECTED_COMMAND 0x00 389deb0eb7SJason Gunthorpe #define TPM_CONNECTION_COMMAND 0x40 399deb0eb7SJason Gunthorpe 409deb0eb7SJason Gunthorpe /* 419deb0eb7SJason Gunthorpe * Bug workaround - some TPM's don't flush the most 429deb0eb7SJason Gunthorpe * recently changed pcr on suspend, so force the flush 439deb0eb7SJason Gunthorpe * with an extend to the selected _unused_ non-volatile pcr. 449deb0eb7SJason Gunthorpe */ 459deb0eb7SJason Gunthorpe static int tpm_suspend_pcr; 469deb0eb7SJason Gunthorpe module_param_named(suspend_pcr, tpm_suspend_pcr, uint, 0644); 479deb0eb7SJason Gunthorpe MODULE_PARM_DESC(suspend_pcr, 489deb0eb7SJason Gunthorpe "PCR to use for dummy writes to faciltate flush on suspend."); 499deb0eb7SJason Gunthorpe 509deb0eb7SJason Gunthorpe static LIST_HEAD(tpm_chip_list); 519deb0eb7SJason Gunthorpe static DEFINE_SPINLOCK(driver_lock); 529deb0eb7SJason Gunthorpe static DECLARE_BITMAP(dev_mask, TPM_NUM_DEVICES); 539deb0eb7SJason Gunthorpe 549deb0eb7SJason Gunthorpe /* 559deb0eb7SJason Gunthorpe * Array with one entry per ordinal defining the maximum amount 569deb0eb7SJason Gunthorpe * of time the chip could take to return the result. The ordinal 579deb0eb7SJason Gunthorpe * designation of short, medium or long is defined in a table in 589deb0eb7SJason Gunthorpe * TCG Specification TPM Main Part 2 TPM Structures Section 17. The 599deb0eb7SJason Gunthorpe * values of the SHORT, MEDIUM, and LONG durations are retrieved 609deb0eb7SJason Gunthorpe * from the chip during initialization with a call to tpm_get_timeouts. 619deb0eb7SJason Gunthorpe */ 629deb0eb7SJason Gunthorpe static const u8 tpm_ordinal_duration[TPM_MAX_ORDINAL] = { 639deb0eb7SJason Gunthorpe TPM_UNDEFINED, /* 0 */ 649deb0eb7SJason Gunthorpe TPM_UNDEFINED, 659deb0eb7SJason Gunthorpe TPM_UNDEFINED, 669deb0eb7SJason Gunthorpe TPM_UNDEFINED, 679deb0eb7SJason Gunthorpe TPM_UNDEFINED, 689deb0eb7SJason Gunthorpe TPM_UNDEFINED, /* 5 */ 699deb0eb7SJason Gunthorpe TPM_UNDEFINED, 709deb0eb7SJason Gunthorpe TPM_UNDEFINED, 719deb0eb7SJason Gunthorpe TPM_UNDEFINED, 729deb0eb7SJason Gunthorpe TPM_UNDEFINED, 739deb0eb7SJason Gunthorpe TPM_SHORT, /* 10 */ 749deb0eb7SJason Gunthorpe TPM_SHORT, 759deb0eb7SJason Gunthorpe TPM_MEDIUM, 769deb0eb7SJason Gunthorpe TPM_LONG, 779deb0eb7SJason Gunthorpe TPM_LONG, 789deb0eb7SJason Gunthorpe TPM_MEDIUM, /* 15 */ 799deb0eb7SJason Gunthorpe TPM_SHORT, 809deb0eb7SJason Gunthorpe TPM_SHORT, 819deb0eb7SJason Gunthorpe TPM_MEDIUM, 829deb0eb7SJason Gunthorpe TPM_LONG, 839deb0eb7SJason Gunthorpe TPM_SHORT, /* 20 */ 849deb0eb7SJason Gunthorpe TPM_SHORT, 859deb0eb7SJason Gunthorpe TPM_MEDIUM, 869deb0eb7SJason Gunthorpe TPM_MEDIUM, 879deb0eb7SJason Gunthorpe TPM_MEDIUM, 889deb0eb7SJason Gunthorpe TPM_SHORT, /* 25 */ 899deb0eb7SJason Gunthorpe TPM_SHORT, 909deb0eb7SJason Gunthorpe TPM_MEDIUM, 919deb0eb7SJason Gunthorpe TPM_SHORT, 929deb0eb7SJason Gunthorpe TPM_SHORT, 939deb0eb7SJason Gunthorpe TPM_MEDIUM, /* 30 */ 949deb0eb7SJason Gunthorpe TPM_LONG, 959deb0eb7SJason Gunthorpe TPM_MEDIUM, 969deb0eb7SJason Gunthorpe TPM_SHORT, 979deb0eb7SJason Gunthorpe TPM_SHORT, 989deb0eb7SJason Gunthorpe TPM_SHORT, /* 35 */ 999deb0eb7SJason Gunthorpe TPM_MEDIUM, 1009deb0eb7SJason Gunthorpe TPM_MEDIUM, 1019deb0eb7SJason Gunthorpe TPM_UNDEFINED, 1029deb0eb7SJason Gunthorpe TPM_UNDEFINED, 1039deb0eb7SJason Gunthorpe TPM_MEDIUM, /* 40 */ 1049deb0eb7SJason Gunthorpe TPM_LONG, 1059deb0eb7SJason Gunthorpe TPM_MEDIUM, 1069deb0eb7SJason Gunthorpe TPM_SHORT, 1079deb0eb7SJason Gunthorpe TPM_SHORT, 1089deb0eb7SJason Gunthorpe TPM_SHORT, /* 45 */ 1099deb0eb7SJason Gunthorpe TPM_SHORT, 1109deb0eb7SJason Gunthorpe TPM_SHORT, 1119deb0eb7SJason Gunthorpe TPM_SHORT, 1129deb0eb7SJason Gunthorpe TPM_LONG, 1139deb0eb7SJason Gunthorpe TPM_MEDIUM, /* 50 */ 1149deb0eb7SJason Gunthorpe TPM_MEDIUM, 1159deb0eb7SJason Gunthorpe TPM_UNDEFINED, 1169deb0eb7SJason Gunthorpe TPM_UNDEFINED, 1179deb0eb7SJason Gunthorpe TPM_UNDEFINED, 1189deb0eb7SJason Gunthorpe TPM_UNDEFINED, /* 55 */ 1199deb0eb7SJason Gunthorpe TPM_UNDEFINED, 1209deb0eb7SJason Gunthorpe TPM_UNDEFINED, 1219deb0eb7SJason Gunthorpe TPM_UNDEFINED, 1229deb0eb7SJason Gunthorpe TPM_UNDEFINED, 1239deb0eb7SJason Gunthorpe TPM_MEDIUM, /* 60 */ 1249deb0eb7SJason Gunthorpe TPM_MEDIUM, 1259deb0eb7SJason Gunthorpe TPM_MEDIUM, 1269deb0eb7SJason Gunthorpe TPM_SHORT, 1279deb0eb7SJason Gunthorpe TPM_SHORT, 1289deb0eb7SJason Gunthorpe TPM_MEDIUM, /* 65 */ 1299deb0eb7SJason Gunthorpe TPM_UNDEFINED, 1309deb0eb7SJason Gunthorpe TPM_UNDEFINED, 1319deb0eb7SJason Gunthorpe TPM_UNDEFINED, 1329deb0eb7SJason Gunthorpe TPM_UNDEFINED, 1339deb0eb7SJason Gunthorpe TPM_SHORT, /* 70 */ 1349deb0eb7SJason Gunthorpe TPM_SHORT, 1359deb0eb7SJason Gunthorpe TPM_UNDEFINED, 1369deb0eb7SJason Gunthorpe TPM_UNDEFINED, 1379deb0eb7SJason Gunthorpe TPM_UNDEFINED, 1389deb0eb7SJason Gunthorpe TPM_UNDEFINED, /* 75 */ 1399deb0eb7SJason Gunthorpe TPM_UNDEFINED, 1409deb0eb7SJason Gunthorpe TPM_UNDEFINED, 1419deb0eb7SJason Gunthorpe TPM_UNDEFINED, 1429deb0eb7SJason Gunthorpe TPM_UNDEFINED, 1439deb0eb7SJason Gunthorpe TPM_LONG, /* 80 */ 1449deb0eb7SJason Gunthorpe TPM_UNDEFINED, 1459deb0eb7SJason Gunthorpe TPM_MEDIUM, 1469deb0eb7SJason Gunthorpe TPM_LONG, 1479deb0eb7SJason Gunthorpe TPM_SHORT, 1489deb0eb7SJason Gunthorpe TPM_UNDEFINED, /* 85 */ 1499deb0eb7SJason Gunthorpe TPM_UNDEFINED, 1509deb0eb7SJason Gunthorpe TPM_UNDEFINED, 1519deb0eb7SJason Gunthorpe TPM_UNDEFINED, 1529deb0eb7SJason Gunthorpe TPM_UNDEFINED, 1539deb0eb7SJason Gunthorpe TPM_SHORT, /* 90 */ 1549deb0eb7SJason Gunthorpe TPM_SHORT, 1559deb0eb7SJason Gunthorpe TPM_SHORT, 1569deb0eb7SJason Gunthorpe TPM_SHORT, 1579deb0eb7SJason Gunthorpe TPM_SHORT, 1589deb0eb7SJason Gunthorpe TPM_UNDEFINED, /* 95 */ 1599deb0eb7SJason Gunthorpe TPM_UNDEFINED, 1609deb0eb7SJason Gunthorpe TPM_UNDEFINED, 1619deb0eb7SJason Gunthorpe TPM_UNDEFINED, 1629deb0eb7SJason Gunthorpe TPM_UNDEFINED, 1639deb0eb7SJason Gunthorpe TPM_MEDIUM, /* 100 */ 1649deb0eb7SJason Gunthorpe TPM_SHORT, 1659deb0eb7SJason Gunthorpe TPM_SHORT, 1669deb0eb7SJason Gunthorpe TPM_UNDEFINED, 1679deb0eb7SJason Gunthorpe TPM_UNDEFINED, 1689deb0eb7SJason Gunthorpe TPM_UNDEFINED, /* 105 */ 1699deb0eb7SJason Gunthorpe TPM_UNDEFINED, 1709deb0eb7SJason Gunthorpe TPM_UNDEFINED, 1719deb0eb7SJason Gunthorpe TPM_UNDEFINED, 1729deb0eb7SJason Gunthorpe TPM_UNDEFINED, 1739deb0eb7SJason Gunthorpe TPM_SHORT, /* 110 */ 1749deb0eb7SJason Gunthorpe TPM_SHORT, 1759deb0eb7SJason Gunthorpe TPM_SHORT, 1769deb0eb7SJason Gunthorpe TPM_SHORT, 1779deb0eb7SJason Gunthorpe TPM_SHORT, 1789deb0eb7SJason Gunthorpe TPM_SHORT, /* 115 */ 1799deb0eb7SJason Gunthorpe TPM_SHORT, 1809deb0eb7SJason Gunthorpe TPM_SHORT, 1819deb0eb7SJason Gunthorpe TPM_UNDEFINED, 1829deb0eb7SJason Gunthorpe TPM_UNDEFINED, 1839deb0eb7SJason Gunthorpe TPM_LONG, /* 120 */ 1849deb0eb7SJason Gunthorpe TPM_LONG, 1859deb0eb7SJason Gunthorpe TPM_MEDIUM, 1869deb0eb7SJason Gunthorpe TPM_UNDEFINED, 1879deb0eb7SJason Gunthorpe TPM_SHORT, 1889deb0eb7SJason Gunthorpe TPM_SHORT, /* 125 */ 1899deb0eb7SJason Gunthorpe TPM_SHORT, 1909deb0eb7SJason Gunthorpe TPM_LONG, 1919deb0eb7SJason Gunthorpe TPM_SHORT, 1929deb0eb7SJason Gunthorpe TPM_SHORT, 1939deb0eb7SJason Gunthorpe TPM_SHORT, /* 130 */ 1949deb0eb7SJason Gunthorpe TPM_MEDIUM, 1959deb0eb7SJason Gunthorpe TPM_UNDEFINED, 1969deb0eb7SJason Gunthorpe TPM_SHORT, 1979deb0eb7SJason Gunthorpe TPM_MEDIUM, 1989deb0eb7SJason Gunthorpe TPM_UNDEFINED, /* 135 */ 1999deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2009deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2019deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2029deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2039deb0eb7SJason Gunthorpe TPM_SHORT, /* 140 */ 2049deb0eb7SJason Gunthorpe TPM_SHORT, 2059deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2069deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2079deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2089deb0eb7SJason Gunthorpe TPM_UNDEFINED, /* 145 */ 2099deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2109deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2119deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2129deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2139deb0eb7SJason Gunthorpe TPM_SHORT, /* 150 */ 2149deb0eb7SJason Gunthorpe TPM_MEDIUM, 2159deb0eb7SJason Gunthorpe TPM_MEDIUM, 2169deb0eb7SJason Gunthorpe TPM_SHORT, 2179deb0eb7SJason Gunthorpe TPM_SHORT, 2189deb0eb7SJason Gunthorpe TPM_UNDEFINED, /* 155 */ 2199deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2209deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2219deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2229deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2239deb0eb7SJason Gunthorpe TPM_SHORT, /* 160 */ 2249deb0eb7SJason Gunthorpe TPM_SHORT, 2259deb0eb7SJason Gunthorpe TPM_SHORT, 2269deb0eb7SJason Gunthorpe TPM_SHORT, 2279deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2289deb0eb7SJason Gunthorpe TPM_UNDEFINED, /* 165 */ 2299deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2309deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2319deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2329deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2339deb0eb7SJason Gunthorpe TPM_LONG, /* 170 */ 2349deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2359deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2369deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2379deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2389deb0eb7SJason Gunthorpe TPM_UNDEFINED, /* 175 */ 2399deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2409deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2419deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2429deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2439deb0eb7SJason Gunthorpe TPM_MEDIUM, /* 180 */ 2449deb0eb7SJason Gunthorpe TPM_SHORT, 2459deb0eb7SJason Gunthorpe TPM_MEDIUM, 2469deb0eb7SJason Gunthorpe TPM_MEDIUM, 2479deb0eb7SJason Gunthorpe TPM_MEDIUM, 2489deb0eb7SJason Gunthorpe TPM_MEDIUM, /* 185 */ 2499deb0eb7SJason Gunthorpe TPM_SHORT, 2509deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2519deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2529deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2539deb0eb7SJason Gunthorpe TPM_UNDEFINED, /* 190 */ 2549deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2559deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2569deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2579deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2589deb0eb7SJason Gunthorpe TPM_UNDEFINED, /* 195 */ 2599deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2609deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2619deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2629deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2639deb0eb7SJason Gunthorpe TPM_SHORT, /* 200 */ 2649deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2659deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2669deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2679deb0eb7SJason Gunthorpe TPM_SHORT, 2689deb0eb7SJason Gunthorpe TPM_SHORT, /* 205 */ 2699deb0eb7SJason Gunthorpe TPM_SHORT, 2709deb0eb7SJason Gunthorpe TPM_SHORT, 2719deb0eb7SJason Gunthorpe TPM_SHORT, 2729deb0eb7SJason Gunthorpe TPM_SHORT, 2739deb0eb7SJason Gunthorpe TPM_MEDIUM, /* 210 */ 2749deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2759deb0eb7SJason Gunthorpe TPM_MEDIUM, 2769deb0eb7SJason Gunthorpe TPM_MEDIUM, 2779deb0eb7SJason Gunthorpe TPM_MEDIUM, 2789deb0eb7SJason Gunthorpe TPM_UNDEFINED, /* 215 */ 2799deb0eb7SJason Gunthorpe TPM_MEDIUM, 2809deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2819deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2829deb0eb7SJason Gunthorpe TPM_SHORT, 2839deb0eb7SJason Gunthorpe TPM_SHORT, /* 220 */ 2849deb0eb7SJason Gunthorpe TPM_SHORT, 2859deb0eb7SJason Gunthorpe TPM_SHORT, 2869deb0eb7SJason Gunthorpe TPM_SHORT, 2879deb0eb7SJason Gunthorpe TPM_SHORT, 2889deb0eb7SJason Gunthorpe TPM_UNDEFINED, /* 225 */ 2899deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2909deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2919deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2929deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2939deb0eb7SJason Gunthorpe TPM_SHORT, /* 230 */ 2949deb0eb7SJason Gunthorpe TPM_LONG, 2959deb0eb7SJason Gunthorpe TPM_MEDIUM, 2969deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2979deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2989deb0eb7SJason Gunthorpe TPM_UNDEFINED, /* 235 */ 2999deb0eb7SJason Gunthorpe TPM_UNDEFINED, 3009deb0eb7SJason Gunthorpe TPM_UNDEFINED, 3019deb0eb7SJason Gunthorpe TPM_UNDEFINED, 3029deb0eb7SJason Gunthorpe TPM_UNDEFINED, 3039deb0eb7SJason Gunthorpe TPM_SHORT, /* 240 */ 3049deb0eb7SJason Gunthorpe TPM_UNDEFINED, 3059deb0eb7SJason Gunthorpe TPM_MEDIUM, 3069deb0eb7SJason Gunthorpe }; 3079deb0eb7SJason Gunthorpe 3089deb0eb7SJason Gunthorpe /* 3099deb0eb7SJason Gunthorpe * Returns max number of jiffies to wait 3109deb0eb7SJason Gunthorpe */ 3119deb0eb7SJason Gunthorpe unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip, 3129deb0eb7SJason Gunthorpe u32 ordinal) 3139deb0eb7SJason Gunthorpe { 3149deb0eb7SJason Gunthorpe int duration_idx = TPM_UNDEFINED; 3159deb0eb7SJason Gunthorpe int duration = 0; 3169deb0eb7SJason Gunthorpe u8 category = (ordinal >> 24) & 0xFF; 3179deb0eb7SJason Gunthorpe 3189deb0eb7SJason Gunthorpe if ((category == TPM_PROTECTED_COMMAND && ordinal < TPM_MAX_ORDINAL) || 3199deb0eb7SJason Gunthorpe (category == TPM_CONNECTION_COMMAND && ordinal < TSC_MAX_ORDINAL)) 3209deb0eb7SJason Gunthorpe duration_idx = tpm_ordinal_duration[ordinal]; 3219deb0eb7SJason Gunthorpe 3229deb0eb7SJason Gunthorpe if (duration_idx != TPM_UNDEFINED) 3239deb0eb7SJason Gunthorpe duration = chip->vendor.duration[duration_idx]; 3249deb0eb7SJason Gunthorpe if (duration <= 0) 3259deb0eb7SJason Gunthorpe return 2 * 60 * HZ; 3269deb0eb7SJason Gunthorpe else 3279deb0eb7SJason Gunthorpe return duration; 3289deb0eb7SJason Gunthorpe } 3299deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_calc_ordinal_duration); 3309deb0eb7SJason Gunthorpe 3319deb0eb7SJason Gunthorpe /* 3329deb0eb7SJason Gunthorpe * Internal kernel interface to transmit TPM commands 3339deb0eb7SJason Gunthorpe */ 334afdba32eSJason Gunthorpe ssize_t tpm_transmit(struct tpm_chip *chip, const char *buf, 3359deb0eb7SJason Gunthorpe size_t bufsiz) 3369deb0eb7SJason Gunthorpe { 3379deb0eb7SJason Gunthorpe ssize_t rc; 3389deb0eb7SJason Gunthorpe u32 count, ordinal; 3399deb0eb7SJason Gunthorpe unsigned long stop; 3409deb0eb7SJason Gunthorpe 3419deb0eb7SJason Gunthorpe if (bufsiz > TPM_BUFSIZE) 3429deb0eb7SJason Gunthorpe bufsiz = TPM_BUFSIZE; 3439deb0eb7SJason Gunthorpe 3449deb0eb7SJason Gunthorpe count = be32_to_cpu(*((__be32 *) (buf + 2))); 3459deb0eb7SJason Gunthorpe ordinal = be32_to_cpu(*((__be32 *) (buf + 6))); 3469deb0eb7SJason Gunthorpe if (count == 0) 3479deb0eb7SJason Gunthorpe return -ENODATA; 3489deb0eb7SJason Gunthorpe if (count > bufsiz) { 3499deb0eb7SJason Gunthorpe dev_err(chip->dev, 3509deb0eb7SJason Gunthorpe "invalid count value %x %zx\n", count, bufsiz); 3519deb0eb7SJason Gunthorpe return -E2BIG; 3529deb0eb7SJason Gunthorpe } 3539deb0eb7SJason Gunthorpe 3549deb0eb7SJason Gunthorpe mutex_lock(&chip->tpm_mutex); 3559deb0eb7SJason Gunthorpe 3565f82e9f0SJason Gunthorpe rc = chip->ops->send(chip, (u8 *) buf, count); 3579deb0eb7SJason Gunthorpe if (rc < 0) { 3589deb0eb7SJason Gunthorpe dev_err(chip->dev, 3599deb0eb7SJason Gunthorpe "tpm_transmit: tpm_send: error %zd\n", rc); 3609deb0eb7SJason Gunthorpe goto out; 3619deb0eb7SJason Gunthorpe } 3629deb0eb7SJason Gunthorpe 3639deb0eb7SJason Gunthorpe if (chip->vendor.irq) 3649deb0eb7SJason Gunthorpe goto out_recv; 3659deb0eb7SJason Gunthorpe 3669deb0eb7SJason Gunthorpe stop = jiffies + tpm_calc_ordinal_duration(chip, ordinal); 3679deb0eb7SJason Gunthorpe do { 3685f82e9f0SJason Gunthorpe u8 status = chip->ops->status(chip); 3695f82e9f0SJason Gunthorpe if ((status & chip->ops->req_complete_mask) == 3705f82e9f0SJason Gunthorpe chip->ops->req_complete_val) 3719deb0eb7SJason Gunthorpe goto out_recv; 3729deb0eb7SJason Gunthorpe 3735f82e9f0SJason Gunthorpe if (chip->ops->req_canceled(chip, status)) { 3749deb0eb7SJason Gunthorpe dev_err(chip->dev, "Operation Canceled\n"); 3759deb0eb7SJason Gunthorpe rc = -ECANCELED; 3769deb0eb7SJason Gunthorpe goto out; 3779deb0eb7SJason Gunthorpe } 3789deb0eb7SJason Gunthorpe 3799deb0eb7SJason Gunthorpe msleep(TPM_TIMEOUT); /* CHECK */ 3809deb0eb7SJason Gunthorpe rmb(); 3819deb0eb7SJason Gunthorpe } while (time_before(jiffies, stop)); 3829deb0eb7SJason Gunthorpe 3835f82e9f0SJason Gunthorpe chip->ops->cancel(chip); 3849deb0eb7SJason Gunthorpe dev_err(chip->dev, "Operation Timed out\n"); 3859deb0eb7SJason Gunthorpe rc = -ETIME; 3869deb0eb7SJason Gunthorpe goto out; 3879deb0eb7SJason Gunthorpe 3889deb0eb7SJason Gunthorpe out_recv: 3895f82e9f0SJason Gunthorpe rc = chip->ops->recv(chip, (u8 *) buf, bufsiz); 3909deb0eb7SJason Gunthorpe if (rc < 0) 3919deb0eb7SJason Gunthorpe dev_err(chip->dev, 3929deb0eb7SJason Gunthorpe "tpm_transmit: tpm_recv: error %zd\n", rc); 3939deb0eb7SJason Gunthorpe out: 3949deb0eb7SJason Gunthorpe mutex_unlock(&chip->tpm_mutex); 3959deb0eb7SJason Gunthorpe return rc; 3969deb0eb7SJason Gunthorpe } 3979deb0eb7SJason Gunthorpe 3989deb0eb7SJason Gunthorpe #define TPM_DIGEST_SIZE 20 3999deb0eb7SJason Gunthorpe #define TPM_RET_CODE_IDX 6 4009deb0eb7SJason Gunthorpe 4019deb0eb7SJason Gunthorpe static ssize_t transmit_cmd(struct tpm_chip *chip, struct tpm_cmd_t *cmd, 4029deb0eb7SJason Gunthorpe int len, const char *desc) 4039deb0eb7SJason Gunthorpe { 4049deb0eb7SJason Gunthorpe int err; 4059deb0eb7SJason Gunthorpe 4069deb0eb7SJason Gunthorpe len = tpm_transmit(chip, (u8 *) cmd, len); 4079deb0eb7SJason Gunthorpe if (len < 0) 4089deb0eb7SJason Gunthorpe return len; 4099deb0eb7SJason Gunthorpe else if (len < TPM_HEADER_SIZE) 4109deb0eb7SJason Gunthorpe return -EFAULT; 4119deb0eb7SJason Gunthorpe 4129deb0eb7SJason Gunthorpe err = be32_to_cpu(cmd->header.out.return_code); 4139deb0eb7SJason Gunthorpe if (err != 0 && desc) 4149deb0eb7SJason Gunthorpe dev_err(chip->dev, "A TPM error (%d) occurred %s\n", err, desc); 4159deb0eb7SJason Gunthorpe 4169deb0eb7SJason Gunthorpe return err; 4179deb0eb7SJason Gunthorpe } 4189deb0eb7SJason Gunthorpe 4199deb0eb7SJason Gunthorpe #define TPM_INTERNAL_RESULT_SIZE 200 4209deb0eb7SJason Gunthorpe #define TPM_ORD_GET_CAP cpu_to_be32(101) 4219deb0eb7SJason Gunthorpe #define TPM_ORD_GET_RANDOM cpu_to_be32(70) 4229deb0eb7SJason Gunthorpe 4239deb0eb7SJason Gunthorpe static const struct tpm_input_header tpm_getcap_header = { 4249deb0eb7SJason Gunthorpe .tag = TPM_TAG_RQU_COMMAND, 4259deb0eb7SJason Gunthorpe .length = cpu_to_be32(22), 4269deb0eb7SJason Gunthorpe .ordinal = TPM_ORD_GET_CAP 4279deb0eb7SJason Gunthorpe }; 4289deb0eb7SJason Gunthorpe 4299deb0eb7SJason Gunthorpe ssize_t tpm_getcap(struct device *dev, __be32 subcap_id, cap_t *cap, 4309deb0eb7SJason Gunthorpe const char *desc) 4319deb0eb7SJason Gunthorpe { 4329deb0eb7SJason Gunthorpe struct tpm_cmd_t tpm_cmd; 4339deb0eb7SJason Gunthorpe int rc; 4349deb0eb7SJason Gunthorpe struct tpm_chip *chip = dev_get_drvdata(dev); 4359deb0eb7SJason Gunthorpe 4369deb0eb7SJason Gunthorpe tpm_cmd.header.in = tpm_getcap_header; 4379deb0eb7SJason Gunthorpe if (subcap_id == CAP_VERSION_1_1 || subcap_id == CAP_VERSION_1_2) { 4389deb0eb7SJason Gunthorpe tpm_cmd.params.getcap_in.cap = subcap_id; 4399deb0eb7SJason Gunthorpe /*subcap field not necessary */ 4409deb0eb7SJason Gunthorpe tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(0); 4419deb0eb7SJason Gunthorpe tpm_cmd.header.in.length -= cpu_to_be32(sizeof(__be32)); 4429deb0eb7SJason Gunthorpe } else { 4439deb0eb7SJason Gunthorpe if (subcap_id == TPM_CAP_FLAG_PERM || 4449deb0eb7SJason Gunthorpe subcap_id == TPM_CAP_FLAG_VOL) 4459deb0eb7SJason Gunthorpe tpm_cmd.params.getcap_in.cap = TPM_CAP_FLAG; 4469deb0eb7SJason Gunthorpe else 4479deb0eb7SJason Gunthorpe tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP; 4489deb0eb7SJason Gunthorpe tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4); 4499deb0eb7SJason Gunthorpe tpm_cmd.params.getcap_in.subcap = subcap_id; 4509deb0eb7SJason Gunthorpe } 4519deb0eb7SJason Gunthorpe rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE, desc); 4529deb0eb7SJason Gunthorpe if (!rc) 4539deb0eb7SJason Gunthorpe *cap = tpm_cmd.params.getcap_out.cap; 4549deb0eb7SJason Gunthorpe return rc; 4559deb0eb7SJason Gunthorpe } 4569deb0eb7SJason Gunthorpe 4579deb0eb7SJason Gunthorpe void tpm_gen_interrupt(struct tpm_chip *chip) 4589deb0eb7SJason Gunthorpe { 4599deb0eb7SJason Gunthorpe struct tpm_cmd_t tpm_cmd; 4609deb0eb7SJason Gunthorpe ssize_t rc; 4619deb0eb7SJason Gunthorpe 4629deb0eb7SJason Gunthorpe tpm_cmd.header.in = tpm_getcap_header; 4639deb0eb7SJason Gunthorpe tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP; 4649deb0eb7SJason Gunthorpe tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4); 4659deb0eb7SJason Gunthorpe tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_TIMEOUT; 4669deb0eb7SJason Gunthorpe 4679deb0eb7SJason Gunthorpe rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE, 4689deb0eb7SJason Gunthorpe "attempting to determine the timeouts"); 4699deb0eb7SJason Gunthorpe } 4709deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_gen_interrupt); 4719deb0eb7SJason Gunthorpe 4729deb0eb7SJason Gunthorpe #define TPM_ORD_STARTUP cpu_to_be32(153) 4739deb0eb7SJason Gunthorpe #define TPM_ST_CLEAR cpu_to_be16(1) 4749deb0eb7SJason Gunthorpe #define TPM_ST_STATE cpu_to_be16(2) 4759deb0eb7SJason Gunthorpe #define TPM_ST_DEACTIVATED cpu_to_be16(3) 4769deb0eb7SJason Gunthorpe static const struct tpm_input_header tpm_startup_header = { 4779deb0eb7SJason Gunthorpe .tag = TPM_TAG_RQU_COMMAND, 4789deb0eb7SJason Gunthorpe .length = cpu_to_be32(12), 4799deb0eb7SJason Gunthorpe .ordinal = TPM_ORD_STARTUP 4809deb0eb7SJason Gunthorpe }; 4819deb0eb7SJason Gunthorpe 4829deb0eb7SJason Gunthorpe static int tpm_startup(struct tpm_chip *chip, __be16 startup_type) 4839deb0eb7SJason Gunthorpe { 4849deb0eb7SJason Gunthorpe struct tpm_cmd_t start_cmd; 4859deb0eb7SJason Gunthorpe start_cmd.header.in = tpm_startup_header; 4869deb0eb7SJason Gunthorpe start_cmd.params.startup_in.startup_type = startup_type; 4879deb0eb7SJason Gunthorpe return transmit_cmd(chip, &start_cmd, TPM_INTERNAL_RESULT_SIZE, 4889deb0eb7SJason Gunthorpe "attempting to start the TPM"); 4899deb0eb7SJason Gunthorpe } 4909deb0eb7SJason Gunthorpe 4919deb0eb7SJason Gunthorpe int tpm_get_timeouts(struct tpm_chip *chip) 4929deb0eb7SJason Gunthorpe { 4939deb0eb7SJason Gunthorpe struct tpm_cmd_t tpm_cmd; 494*8e54caf4SJason Gunthorpe unsigned long new_timeout[4]; 495*8e54caf4SJason Gunthorpe unsigned long old_timeout[4]; 4969deb0eb7SJason Gunthorpe struct duration_t *duration_cap; 4979deb0eb7SJason Gunthorpe ssize_t rc; 4989deb0eb7SJason Gunthorpe 4999deb0eb7SJason Gunthorpe tpm_cmd.header.in = tpm_getcap_header; 5009deb0eb7SJason Gunthorpe tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP; 5019deb0eb7SJason Gunthorpe tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4); 5029deb0eb7SJason Gunthorpe tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_TIMEOUT; 5039deb0eb7SJason Gunthorpe rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE, NULL); 5049deb0eb7SJason Gunthorpe 5059deb0eb7SJason Gunthorpe if (rc == TPM_ERR_INVALID_POSTINIT) { 5069deb0eb7SJason Gunthorpe /* The TPM is not started, we are the first to talk to it. 5079deb0eb7SJason Gunthorpe Execute a startup command. */ 5089deb0eb7SJason Gunthorpe dev_info(chip->dev, "Issuing TPM_STARTUP"); 5099deb0eb7SJason Gunthorpe if (tpm_startup(chip, TPM_ST_CLEAR)) 5109deb0eb7SJason Gunthorpe return rc; 5119deb0eb7SJason Gunthorpe 5129deb0eb7SJason Gunthorpe tpm_cmd.header.in = tpm_getcap_header; 5139deb0eb7SJason Gunthorpe tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP; 5149deb0eb7SJason Gunthorpe tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4); 5159deb0eb7SJason Gunthorpe tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_TIMEOUT; 5169deb0eb7SJason Gunthorpe rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE, 5179deb0eb7SJason Gunthorpe NULL); 5189deb0eb7SJason Gunthorpe } 5199deb0eb7SJason Gunthorpe if (rc) { 5209deb0eb7SJason Gunthorpe dev_err(chip->dev, 5219deb0eb7SJason Gunthorpe "A TPM error (%zd) occurred attempting to determine the timeouts\n", 5229deb0eb7SJason Gunthorpe rc); 5239deb0eb7SJason Gunthorpe goto duration; 5249deb0eb7SJason Gunthorpe } 5259deb0eb7SJason Gunthorpe 5269deb0eb7SJason Gunthorpe if (be32_to_cpu(tpm_cmd.header.out.return_code) != 0 || 5279deb0eb7SJason Gunthorpe be32_to_cpu(tpm_cmd.header.out.length) 5289deb0eb7SJason Gunthorpe != sizeof(tpm_cmd.header.out) + sizeof(u32) + 4 * sizeof(u32)) 5299deb0eb7SJason Gunthorpe return -EINVAL; 5309deb0eb7SJason Gunthorpe 531*8e54caf4SJason Gunthorpe old_timeout[0] = be32_to_cpu(tpm_cmd.params.getcap_out.cap.timeout.a); 532*8e54caf4SJason Gunthorpe old_timeout[1] = be32_to_cpu(tpm_cmd.params.getcap_out.cap.timeout.b); 533*8e54caf4SJason Gunthorpe old_timeout[2] = be32_to_cpu(tpm_cmd.params.getcap_out.cap.timeout.c); 534*8e54caf4SJason Gunthorpe old_timeout[3] = be32_to_cpu(tpm_cmd.params.getcap_out.cap.timeout.d); 535*8e54caf4SJason Gunthorpe memcpy(new_timeout, old_timeout, sizeof(new_timeout)); 536*8e54caf4SJason Gunthorpe 537*8e54caf4SJason Gunthorpe /* 538*8e54caf4SJason Gunthorpe * Provide ability for vendor overrides of timeout values in case 539*8e54caf4SJason Gunthorpe * of misreporting. 540*8e54caf4SJason Gunthorpe */ 541*8e54caf4SJason Gunthorpe if (chip->ops->update_timeouts != NULL) 542*8e54caf4SJason Gunthorpe chip->vendor.timeout_adjusted = 543*8e54caf4SJason Gunthorpe chip->ops->update_timeouts(chip, new_timeout); 544*8e54caf4SJason Gunthorpe 545*8e54caf4SJason Gunthorpe if (!chip->vendor.timeout_adjusted) { 5469deb0eb7SJason Gunthorpe /* Don't overwrite default if value is 0 */ 547*8e54caf4SJason Gunthorpe if (new_timeout[0] != 0 && new_timeout[0] < 1000) { 548*8e54caf4SJason Gunthorpe int i; 549*8e54caf4SJason Gunthorpe 5509deb0eb7SJason Gunthorpe /* timeouts in msec rather usec */ 551*8e54caf4SJason Gunthorpe for (i = 0; i != ARRAY_SIZE(new_timeout); i++) 552*8e54caf4SJason Gunthorpe new_timeout[i] *= 1000; 5539deb0eb7SJason Gunthorpe chip->vendor.timeout_adjusted = true; 5549deb0eb7SJason Gunthorpe } 555*8e54caf4SJason Gunthorpe } 556*8e54caf4SJason Gunthorpe 557*8e54caf4SJason Gunthorpe /* Report adjusted timeouts */ 558*8e54caf4SJason Gunthorpe if (chip->vendor.timeout_adjusted) { 559*8e54caf4SJason Gunthorpe dev_info(chip->dev, 560*8e54caf4SJason Gunthorpe HW_ERR "Adjusting reported timeouts: A %lu->%luus B %lu->%luus C %lu->%luus D %lu->%luus\n", 561*8e54caf4SJason Gunthorpe old_timeout[0], new_timeout[0], 562*8e54caf4SJason Gunthorpe old_timeout[1], new_timeout[1], 563*8e54caf4SJason Gunthorpe old_timeout[2], new_timeout[2], 564*8e54caf4SJason Gunthorpe old_timeout[3], new_timeout[3]); 565*8e54caf4SJason Gunthorpe } 566*8e54caf4SJason Gunthorpe 567*8e54caf4SJason Gunthorpe chip->vendor.timeout_a = usecs_to_jiffies(new_timeout[0]); 568*8e54caf4SJason Gunthorpe chip->vendor.timeout_b = usecs_to_jiffies(new_timeout[1]); 569*8e54caf4SJason Gunthorpe chip->vendor.timeout_c = usecs_to_jiffies(new_timeout[2]); 570*8e54caf4SJason Gunthorpe chip->vendor.timeout_d = usecs_to_jiffies(new_timeout[3]); 5719deb0eb7SJason Gunthorpe 5729deb0eb7SJason Gunthorpe duration: 5739deb0eb7SJason Gunthorpe tpm_cmd.header.in = tpm_getcap_header; 5749deb0eb7SJason Gunthorpe tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP; 5759deb0eb7SJason Gunthorpe tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4); 5769deb0eb7SJason Gunthorpe tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_DURATION; 5779deb0eb7SJason Gunthorpe 5789deb0eb7SJason Gunthorpe rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE, 5799deb0eb7SJason Gunthorpe "attempting to determine the durations"); 5809deb0eb7SJason Gunthorpe if (rc) 5819deb0eb7SJason Gunthorpe return rc; 5829deb0eb7SJason Gunthorpe 5839deb0eb7SJason Gunthorpe if (be32_to_cpu(tpm_cmd.header.out.return_code) != 0 || 5849deb0eb7SJason Gunthorpe be32_to_cpu(tpm_cmd.header.out.length) 5859deb0eb7SJason Gunthorpe != sizeof(tpm_cmd.header.out) + sizeof(u32) + 3 * sizeof(u32)) 5869deb0eb7SJason Gunthorpe return -EINVAL; 5879deb0eb7SJason Gunthorpe 5889deb0eb7SJason Gunthorpe duration_cap = &tpm_cmd.params.getcap_out.cap.duration; 5899deb0eb7SJason Gunthorpe chip->vendor.duration[TPM_SHORT] = 5909deb0eb7SJason Gunthorpe usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_short)); 5919deb0eb7SJason Gunthorpe chip->vendor.duration[TPM_MEDIUM] = 5929deb0eb7SJason Gunthorpe usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_medium)); 5939deb0eb7SJason Gunthorpe chip->vendor.duration[TPM_LONG] = 5949deb0eb7SJason Gunthorpe usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_long)); 5959deb0eb7SJason Gunthorpe 5969deb0eb7SJason Gunthorpe /* The Broadcom BCM0102 chipset in a Dell Latitude D820 gets the above 5979deb0eb7SJason Gunthorpe * value wrong and apparently reports msecs rather than usecs. So we 5989deb0eb7SJason Gunthorpe * fix up the resulting too-small TPM_SHORT value to make things work. 5999deb0eb7SJason Gunthorpe * We also scale the TPM_MEDIUM and -_LONG values by 1000. 6009deb0eb7SJason Gunthorpe */ 6019deb0eb7SJason Gunthorpe if (chip->vendor.duration[TPM_SHORT] < (HZ / 100)) { 6029deb0eb7SJason Gunthorpe chip->vendor.duration[TPM_SHORT] = HZ; 6039deb0eb7SJason Gunthorpe chip->vendor.duration[TPM_MEDIUM] *= 1000; 6049deb0eb7SJason Gunthorpe chip->vendor.duration[TPM_LONG] *= 1000; 6059deb0eb7SJason Gunthorpe chip->vendor.duration_adjusted = true; 6069deb0eb7SJason Gunthorpe dev_info(chip->dev, "Adjusting TPM timeout parameters."); 6079deb0eb7SJason Gunthorpe } 6089deb0eb7SJason Gunthorpe return 0; 6099deb0eb7SJason Gunthorpe } 6109deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_get_timeouts); 6119deb0eb7SJason Gunthorpe 6129deb0eb7SJason Gunthorpe #define TPM_ORD_CONTINUE_SELFTEST 83 6139deb0eb7SJason Gunthorpe #define CONTINUE_SELFTEST_RESULT_SIZE 10 6149deb0eb7SJason Gunthorpe 6159deb0eb7SJason Gunthorpe static struct tpm_input_header continue_selftest_header = { 6169deb0eb7SJason Gunthorpe .tag = TPM_TAG_RQU_COMMAND, 6179deb0eb7SJason Gunthorpe .length = cpu_to_be32(10), 6189deb0eb7SJason Gunthorpe .ordinal = cpu_to_be32(TPM_ORD_CONTINUE_SELFTEST), 6199deb0eb7SJason Gunthorpe }; 6209deb0eb7SJason Gunthorpe 6219deb0eb7SJason Gunthorpe /** 6229deb0eb7SJason Gunthorpe * tpm_continue_selftest -- run TPM's selftest 6239deb0eb7SJason Gunthorpe * @chip: TPM chip to use 6249deb0eb7SJason Gunthorpe * 6259deb0eb7SJason Gunthorpe * Returns 0 on success, < 0 in case of fatal error or a value > 0 representing 6269deb0eb7SJason Gunthorpe * a TPM error code. 6279deb0eb7SJason Gunthorpe */ 6289deb0eb7SJason Gunthorpe static int tpm_continue_selftest(struct tpm_chip *chip) 6299deb0eb7SJason Gunthorpe { 6309deb0eb7SJason Gunthorpe int rc; 6319deb0eb7SJason Gunthorpe struct tpm_cmd_t cmd; 6329deb0eb7SJason Gunthorpe 6339deb0eb7SJason Gunthorpe cmd.header.in = continue_selftest_header; 6349deb0eb7SJason Gunthorpe rc = transmit_cmd(chip, &cmd, CONTINUE_SELFTEST_RESULT_SIZE, 6359deb0eb7SJason Gunthorpe "continue selftest"); 6369deb0eb7SJason Gunthorpe return rc; 6379deb0eb7SJason Gunthorpe } 6389deb0eb7SJason Gunthorpe 6399deb0eb7SJason Gunthorpe /* 6409deb0eb7SJason Gunthorpe * tpm_chip_find_get - return tpm_chip for given chip number 6419deb0eb7SJason Gunthorpe */ 6429deb0eb7SJason Gunthorpe static struct tpm_chip *tpm_chip_find_get(int chip_num) 6439deb0eb7SJason Gunthorpe { 6449deb0eb7SJason Gunthorpe struct tpm_chip *pos, *chip = NULL; 6459deb0eb7SJason Gunthorpe 6469deb0eb7SJason Gunthorpe rcu_read_lock(); 6479deb0eb7SJason Gunthorpe list_for_each_entry_rcu(pos, &tpm_chip_list, list) { 6489deb0eb7SJason Gunthorpe if (chip_num != TPM_ANY_NUM && chip_num != pos->dev_num) 6499deb0eb7SJason Gunthorpe continue; 6509deb0eb7SJason Gunthorpe 6519deb0eb7SJason Gunthorpe if (try_module_get(pos->dev->driver->owner)) { 6529deb0eb7SJason Gunthorpe chip = pos; 6539deb0eb7SJason Gunthorpe break; 6549deb0eb7SJason Gunthorpe } 6559deb0eb7SJason Gunthorpe } 6569deb0eb7SJason Gunthorpe rcu_read_unlock(); 6579deb0eb7SJason Gunthorpe return chip; 6589deb0eb7SJason Gunthorpe } 6599deb0eb7SJason Gunthorpe 6609deb0eb7SJason Gunthorpe #define TPM_ORDINAL_PCRREAD cpu_to_be32(21) 6619deb0eb7SJason Gunthorpe #define READ_PCR_RESULT_SIZE 30 6629deb0eb7SJason Gunthorpe static struct tpm_input_header pcrread_header = { 6639deb0eb7SJason Gunthorpe .tag = TPM_TAG_RQU_COMMAND, 6649deb0eb7SJason Gunthorpe .length = cpu_to_be32(14), 6659deb0eb7SJason Gunthorpe .ordinal = TPM_ORDINAL_PCRREAD 6669deb0eb7SJason Gunthorpe }; 6679deb0eb7SJason Gunthorpe 668000a07b0SJason Gunthorpe int tpm_pcr_read_dev(struct tpm_chip *chip, int pcr_idx, u8 *res_buf) 6699deb0eb7SJason Gunthorpe { 6709deb0eb7SJason Gunthorpe int rc; 6719deb0eb7SJason Gunthorpe struct tpm_cmd_t cmd; 6729deb0eb7SJason Gunthorpe 6739deb0eb7SJason Gunthorpe cmd.header.in = pcrread_header; 6749deb0eb7SJason Gunthorpe cmd.params.pcrread_in.pcr_idx = cpu_to_be32(pcr_idx); 6759deb0eb7SJason Gunthorpe rc = transmit_cmd(chip, &cmd, READ_PCR_RESULT_SIZE, 6769deb0eb7SJason Gunthorpe "attempting to read a pcr value"); 6779deb0eb7SJason Gunthorpe 6789deb0eb7SJason Gunthorpe if (rc == 0) 6799deb0eb7SJason Gunthorpe memcpy(res_buf, cmd.params.pcrread_out.pcr_result, 6809deb0eb7SJason Gunthorpe TPM_DIGEST_SIZE); 6819deb0eb7SJason Gunthorpe return rc; 6829deb0eb7SJason Gunthorpe } 6839deb0eb7SJason Gunthorpe 6849deb0eb7SJason Gunthorpe /** 6859deb0eb7SJason Gunthorpe * tpm_pcr_read - read a pcr value 6869deb0eb7SJason Gunthorpe * @chip_num: tpm idx # or ANY 6879deb0eb7SJason Gunthorpe * @pcr_idx: pcr idx to retrieve 6889deb0eb7SJason Gunthorpe * @res_buf: TPM_PCR value 6899deb0eb7SJason Gunthorpe * size of res_buf is 20 bytes (or NULL if you don't care) 6909deb0eb7SJason Gunthorpe * 6919deb0eb7SJason Gunthorpe * The TPM driver should be built-in, but for whatever reason it 6929deb0eb7SJason Gunthorpe * isn't, protect against the chip disappearing, by incrementing 6939deb0eb7SJason Gunthorpe * the module usage count. 6949deb0eb7SJason Gunthorpe */ 6959deb0eb7SJason Gunthorpe int tpm_pcr_read(u32 chip_num, int pcr_idx, u8 *res_buf) 6969deb0eb7SJason Gunthorpe { 6979deb0eb7SJason Gunthorpe struct tpm_chip *chip; 6989deb0eb7SJason Gunthorpe int rc; 6999deb0eb7SJason Gunthorpe 7009deb0eb7SJason Gunthorpe chip = tpm_chip_find_get(chip_num); 7019deb0eb7SJason Gunthorpe if (chip == NULL) 7029deb0eb7SJason Gunthorpe return -ENODEV; 703000a07b0SJason Gunthorpe rc = tpm_pcr_read_dev(chip, pcr_idx, res_buf); 7049deb0eb7SJason Gunthorpe tpm_chip_put(chip); 7059deb0eb7SJason Gunthorpe return rc; 7069deb0eb7SJason Gunthorpe } 7079deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_pcr_read); 7089deb0eb7SJason Gunthorpe 7099deb0eb7SJason Gunthorpe /** 7109deb0eb7SJason Gunthorpe * tpm_pcr_extend - extend pcr value with hash 7119deb0eb7SJason Gunthorpe * @chip_num: tpm idx # or AN& 7129deb0eb7SJason Gunthorpe * @pcr_idx: pcr idx to extend 7139deb0eb7SJason Gunthorpe * @hash: hash value used to extend pcr value 7149deb0eb7SJason Gunthorpe * 7159deb0eb7SJason Gunthorpe * The TPM driver should be built-in, but for whatever reason it 7169deb0eb7SJason Gunthorpe * isn't, protect against the chip disappearing, by incrementing 7179deb0eb7SJason Gunthorpe * the module usage count. 7189deb0eb7SJason Gunthorpe */ 7199deb0eb7SJason Gunthorpe #define TPM_ORD_PCR_EXTEND cpu_to_be32(20) 7209deb0eb7SJason Gunthorpe #define EXTEND_PCR_RESULT_SIZE 34 7219deb0eb7SJason Gunthorpe static struct tpm_input_header pcrextend_header = { 7229deb0eb7SJason Gunthorpe .tag = TPM_TAG_RQU_COMMAND, 7239deb0eb7SJason Gunthorpe .length = cpu_to_be32(34), 7249deb0eb7SJason Gunthorpe .ordinal = TPM_ORD_PCR_EXTEND 7259deb0eb7SJason Gunthorpe }; 7269deb0eb7SJason Gunthorpe 7279deb0eb7SJason Gunthorpe int tpm_pcr_extend(u32 chip_num, int pcr_idx, const u8 *hash) 7289deb0eb7SJason Gunthorpe { 7299deb0eb7SJason Gunthorpe struct tpm_cmd_t cmd; 7309deb0eb7SJason Gunthorpe int rc; 7319deb0eb7SJason Gunthorpe struct tpm_chip *chip; 7329deb0eb7SJason Gunthorpe 7339deb0eb7SJason Gunthorpe chip = tpm_chip_find_get(chip_num); 7349deb0eb7SJason Gunthorpe if (chip == NULL) 7359deb0eb7SJason Gunthorpe return -ENODEV; 7369deb0eb7SJason Gunthorpe 7379deb0eb7SJason Gunthorpe cmd.header.in = pcrextend_header; 7389deb0eb7SJason Gunthorpe cmd.params.pcrextend_in.pcr_idx = cpu_to_be32(pcr_idx); 7399deb0eb7SJason Gunthorpe memcpy(cmd.params.pcrextend_in.hash, hash, TPM_DIGEST_SIZE); 7409deb0eb7SJason Gunthorpe rc = transmit_cmd(chip, &cmd, EXTEND_PCR_RESULT_SIZE, 7419deb0eb7SJason Gunthorpe "attempting extend a PCR value"); 7429deb0eb7SJason Gunthorpe 7439deb0eb7SJason Gunthorpe tpm_chip_put(chip); 7449deb0eb7SJason Gunthorpe return rc; 7459deb0eb7SJason Gunthorpe } 7469deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_pcr_extend); 7479deb0eb7SJason Gunthorpe 7489deb0eb7SJason Gunthorpe /** 7499deb0eb7SJason Gunthorpe * tpm_do_selftest - have the TPM continue its selftest and wait until it 7509deb0eb7SJason Gunthorpe * can receive further commands 7519deb0eb7SJason Gunthorpe * @chip: TPM chip to use 7529deb0eb7SJason Gunthorpe * 7539deb0eb7SJason Gunthorpe * Returns 0 on success, < 0 in case of fatal error or a value > 0 representing 7549deb0eb7SJason Gunthorpe * a TPM error code. 7559deb0eb7SJason Gunthorpe */ 7569deb0eb7SJason Gunthorpe int tpm_do_selftest(struct tpm_chip *chip) 7579deb0eb7SJason Gunthorpe { 7589deb0eb7SJason Gunthorpe int rc; 7599deb0eb7SJason Gunthorpe unsigned int loops; 7609deb0eb7SJason Gunthorpe unsigned int delay_msec = 100; 7619deb0eb7SJason Gunthorpe unsigned long duration; 7629deb0eb7SJason Gunthorpe struct tpm_cmd_t cmd; 7639deb0eb7SJason Gunthorpe 7649deb0eb7SJason Gunthorpe duration = tpm_calc_ordinal_duration(chip, TPM_ORD_CONTINUE_SELFTEST); 7659deb0eb7SJason Gunthorpe 7669deb0eb7SJason Gunthorpe loops = jiffies_to_msecs(duration) / delay_msec; 7679deb0eb7SJason Gunthorpe 7689deb0eb7SJason Gunthorpe rc = tpm_continue_selftest(chip); 7699deb0eb7SJason Gunthorpe /* This may fail if there was no TPM driver during a suspend/resume 7709deb0eb7SJason Gunthorpe * cycle; some may return 10 (BAD_ORDINAL), others 28 (FAILEDSELFTEST) 7719deb0eb7SJason Gunthorpe */ 7729deb0eb7SJason Gunthorpe if (rc) 7739deb0eb7SJason Gunthorpe return rc; 7749deb0eb7SJason Gunthorpe 7759deb0eb7SJason Gunthorpe do { 7769deb0eb7SJason Gunthorpe /* Attempt to read a PCR value */ 7779deb0eb7SJason Gunthorpe cmd.header.in = pcrread_header; 7789deb0eb7SJason Gunthorpe cmd.params.pcrread_in.pcr_idx = cpu_to_be32(0); 7799deb0eb7SJason Gunthorpe rc = tpm_transmit(chip, (u8 *) &cmd, READ_PCR_RESULT_SIZE); 7809deb0eb7SJason Gunthorpe /* Some buggy TPMs will not respond to tpm_tis_ready() for 7819deb0eb7SJason Gunthorpe * around 300ms while the self test is ongoing, keep trying 7829deb0eb7SJason Gunthorpe * until the self test duration expires. */ 7839deb0eb7SJason Gunthorpe if (rc == -ETIME) { 7849deb0eb7SJason Gunthorpe dev_info(chip->dev, HW_ERR "TPM command timed out during continue self test"); 7859deb0eb7SJason Gunthorpe msleep(delay_msec); 7869deb0eb7SJason Gunthorpe continue; 7879deb0eb7SJason Gunthorpe } 7889deb0eb7SJason Gunthorpe 7899deb0eb7SJason Gunthorpe if (rc < TPM_HEADER_SIZE) 7909deb0eb7SJason Gunthorpe return -EFAULT; 7919deb0eb7SJason Gunthorpe 7929deb0eb7SJason Gunthorpe rc = be32_to_cpu(cmd.header.out.return_code); 7939deb0eb7SJason Gunthorpe if (rc == TPM_ERR_DISABLED || rc == TPM_ERR_DEACTIVATED) { 7949deb0eb7SJason Gunthorpe dev_info(chip->dev, 7959deb0eb7SJason Gunthorpe "TPM is disabled/deactivated (0x%X)\n", rc); 7969deb0eb7SJason Gunthorpe /* TPM is disabled and/or deactivated; driver can 7979deb0eb7SJason Gunthorpe * proceed and TPM does handle commands for 7989deb0eb7SJason Gunthorpe * suspend/resume correctly 7999deb0eb7SJason Gunthorpe */ 8009deb0eb7SJason Gunthorpe return 0; 8019deb0eb7SJason Gunthorpe } 8029deb0eb7SJason Gunthorpe if (rc != TPM_WARN_DOING_SELFTEST) 8039deb0eb7SJason Gunthorpe return rc; 8049deb0eb7SJason Gunthorpe msleep(delay_msec); 8059deb0eb7SJason Gunthorpe } while (--loops > 0); 8069deb0eb7SJason Gunthorpe 8079deb0eb7SJason Gunthorpe return rc; 8089deb0eb7SJason Gunthorpe } 8099deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_do_selftest); 8109deb0eb7SJason Gunthorpe 8119deb0eb7SJason Gunthorpe int tpm_send(u32 chip_num, void *cmd, size_t buflen) 8129deb0eb7SJason Gunthorpe { 8139deb0eb7SJason Gunthorpe struct tpm_chip *chip; 8149deb0eb7SJason Gunthorpe int rc; 8159deb0eb7SJason Gunthorpe 8169deb0eb7SJason Gunthorpe chip = tpm_chip_find_get(chip_num); 8179deb0eb7SJason Gunthorpe if (chip == NULL) 8189deb0eb7SJason Gunthorpe return -ENODEV; 8199deb0eb7SJason Gunthorpe 8209deb0eb7SJason Gunthorpe rc = transmit_cmd(chip, cmd, buflen, "attempting tpm_cmd"); 8219deb0eb7SJason Gunthorpe 8229deb0eb7SJason Gunthorpe tpm_chip_put(chip); 8239deb0eb7SJason Gunthorpe return rc; 8249deb0eb7SJason Gunthorpe } 8259deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_send); 8269deb0eb7SJason Gunthorpe 8279deb0eb7SJason Gunthorpe static bool wait_for_tpm_stat_cond(struct tpm_chip *chip, u8 mask, 8289deb0eb7SJason Gunthorpe bool check_cancel, bool *canceled) 8299deb0eb7SJason Gunthorpe { 8305f82e9f0SJason Gunthorpe u8 status = chip->ops->status(chip); 8319deb0eb7SJason Gunthorpe 8329deb0eb7SJason Gunthorpe *canceled = false; 8339deb0eb7SJason Gunthorpe if ((status & mask) == mask) 8349deb0eb7SJason Gunthorpe return true; 8355f82e9f0SJason Gunthorpe if (check_cancel && chip->ops->req_canceled(chip, status)) { 8369deb0eb7SJason Gunthorpe *canceled = true; 8379deb0eb7SJason Gunthorpe return true; 8389deb0eb7SJason Gunthorpe } 8399deb0eb7SJason Gunthorpe return false; 8409deb0eb7SJason Gunthorpe } 8419deb0eb7SJason Gunthorpe 8429deb0eb7SJason Gunthorpe int wait_for_tpm_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout, 8439deb0eb7SJason Gunthorpe wait_queue_head_t *queue, bool check_cancel) 8449deb0eb7SJason Gunthorpe { 8459deb0eb7SJason Gunthorpe unsigned long stop; 8469deb0eb7SJason Gunthorpe long rc; 8479deb0eb7SJason Gunthorpe u8 status; 8489deb0eb7SJason Gunthorpe bool canceled = false; 8499deb0eb7SJason Gunthorpe 8509deb0eb7SJason Gunthorpe /* check current status */ 8515f82e9f0SJason Gunthorpe status = chip->ops->status(chip); 8529deb0eb7SJason Gunthorpe if ((status & mask) == mask) 8539deb0eb7SJason Gunthorpe return 0; 8549deb0eb7SJason Gunthorpe 8559deb0eb7SJason Gunthorpe stop = jiffies + timeout; 8569deb0eb7SJason Gunthorpe 8579deb0eb7SJason Gunthorpe if (chip->vendor.irq) { 8589deb0eb7SJason Gunthorpe again: 8599deb0eb7SJason Gunthorpe timeout = stop - jiffies; 8609deb0eb7SJason Gunthorpe if ((long)timeout <= 0) 8619deb0eb7SJason Gunthorpe return -ETIME; 8629deb0eb7SJason Gunthorpe rc = wait_event_interruptible_timeout(*queue, 8639deb0eb7SJason Gunthorpe wait_for_tpm_stat_cond(chip, mask, check_cancel, 8649deb0eb7SJason Gunthorpe &canceled), 8659deb0eb7SJason Gunthorpe timeout); 8669deb0eb7SJason Gunthorpe if (rc > 0) { 8679deb0eb7SJason Gunthorpe if (canceled) 8689deb0eb7SJason Gunthorpe return -ECANCELED; 8699deb0eb7SJason Gunthorpe return 0; 8709deb0eb7SJason Gunthorpe } 8719deb0eb7SJason Gunthorpe if (rc == -ERESTARTSYS && freezing(current)) { 8729deb0eb7SJason Gunthorpe clear_thread_flag(TIF_SIGPENDING); 8739deb0eb7SJason Gunthorpe goto again; 8749deb0eb7SJason Gunthorpe } 8759deb0eb7SJason Gunthorpe } else { 8769deb0eb7SJason Gunthorpe do { 8779deb0eb7SJason Gunthorpe msleep(TPM_TIMEOUT); 8785f82e9f0SJason Gunthorpe status = chip->ops->status(chip); 8799deb0eb7SJason Gunthorpe if ((status & mask) == mask) 8809deb0eb7SJason Gunthorpe return 0; 8819deb0eb7SJason Gunthorpe } while (time_before(jiffies, stop)); 8829deb0eb7SJason Gunthorpe } 8839deb0eb7SJason Gunthorpe return -ETIME; 8849deb0eb7SJason Gunthorpe } 8859deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(wait_for_tpm_stat); 8869deb0eb7SJason Gunthorpe 8879deb0eb7SJason Gunthorpe void tpm_remove_hardware(struct device *dev) 8889deb0eb7SJason Gunthorpe { 8899deb0eb7SJason Gunthorpe struct tpm_chip *chip = dev_get_drvdata(dev); 8909deb0eb7SJason Gunthorpe 8919deb0eb7SJason Gunthorpe if (chip == NULL) { 8929deb0eb7SJason Gunthorpe dev_err(dev, "No device data found\n"); 8939deb0eb7SJason Gunthorpe return; 8949deb0eb7SJason Gunthorpe } 8959deb0eb7SJason Gunthorpe 8969deb0eb7SJason Gunthorpe spin_lock(&driver_lock); 8979deb0eb7SJason Gunthorpe list_del_rcu(&chip->list); 8989deb0eb7SJason Gunthorpe spin_unlock(&driver_lock); 8999deb0eb7SJason Gunthorpe synchronize_rcu(); 9009deb0eb7SJason Gunthorpe 901afdba32eSJason Gunthorpe tpm_dev_del_device(chip); 9021e3b73a9SJason Gunthorpe tpm_sysfs_del_device(chip); 9039deb0eb7SJason Gunthorpe tpm_remove_ppi(&dev->kobj); 9049deb0eb7SJason Gunthorpe tpm_bios_log_teardown(chip->bios_dir); 9059deb0eb7SJason Gunthorpe 9069deb0eb7SJason Gunthorpe /* write it this way to be explicit (chip->dev == dev) */ 9079deb0eb7SJason Gunthorpe put_device(chip->dev); 9089deb0eb7SJason Gunthorpe } 9099deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_remove_hardware); 9109deb0eb7SJason Gunthorpe 9119deb0eb7SJason Gunthorpe #define TPM_ORD_SAVESTATE cpu_to_be32(152) 9129deb0eb7SJason Gunthorpe #define SAVESTATE_RESULT_SIZE 10 9139deb0eb7SJason Gunthorpe 9149deb0eb7SJason Gunthorpe static struct tpm_input_header savestate_header = { 9159deb0eb7SJason Gunthorpe .tag = TPM_TAG_RQU_COMMAND, 9169deb0eb7SJason Gunthorpe .length = cpu_to_be32(10), 9179deb0eb7SJason Gunthorpe .ordinal = TPM_ORD_SAVESTATE 9189deb0eb7SJason Gunthorpe }; 9199deb0eb7SJason Gunthorpe 9209deb0eb7SJason Gunthorpe /* 9219deb0eb7SJason Gunthorpe * We are about to suspend. Save the TPM state 9229deb0eb7SJason Gunthorpe * so that it can be restored. 9239deb0eb7SJason Gunthorpe */ 9249deb0eb7SJason Gunthorpe int tpm_pm_suspend(struct device *dev) 9259deb0eb7SJason Gunthorpe { 9269deb0eb7SJason Gunthorpe struct tpm_chip *chip = dev_get_drvdata(dev); 9279deb0eb7SJason Gunthorpe struct tpm_cmd_t cmd; 9289deb0eb7SJason Gunthorpe int rc, try; 9299deb0eb7SJason Gunthorpe 9309deb0eb7SJason Gunthorpe u8 dummy_hash[TPM_DIGEST_SIZE] = { 0 }; 9319deb0eb7SJason Gunthorpe 9329deb0eb7SJason Gunthorpe if (chip == NULL) 9339deb0eb7SJason Gunthorpe return -ENODEV; 9349deb0eb7SJason Gunthorpe 9359deb0eb7SJason Gunthorpe /* for buggy tpm, flush pcrs with extend to selected dummy */ 9369deb0eb7SJason Gunthorpe if (tpm_suspend_pcr) { 9379deb0eb7SJason Gunthorpe cmd.header.in = pcrextend_header; 9389deb0eb7SJason Gunthorpe cmd.params.pcrextend_in.pcr_idx = cpu_to_be32(tpm_suspend_pcr); 9399deb0eb7SJason Gunthorpe memcpy(cmd.params.pcrextend_in.hash, dummy_hash, 9409deb0eb7SJason Gunthorpe TPM_DIGEST_SIZE); 9419deb0eb7SJason Gunthorpe rc = transmit_cmd(chip, &cmd, EXTEND_PCR_RESULT_SIZE, 9429deb0eb7SJason Gunthorpe "extending dummy pcr before suspend"); 9439deb0eb7SJason Gunthorpe } 9449deb0eb7SJason Gunthorpe 9459deb0eb7SJason Gunthorpe /* now do the actual savestate */ 9469deb0eb7SJason Gunthorpe for (try = 0; try < TPM_RETRY; try++) { 9479deb0eb7SJason Gunthorpe cmd.header.in = savestate_header; 9489deb0eb7SJason Gunthorpe rc = transmit_cmd(chip, &cmd, SAVESTATE_RESULT_SIZE, NULL); 9499deb0eb7SJason Gunthorpe 9509deb0eb7SJason Gunthorpe /* 9519deb0eb7SJason Gunthorpe * If the TPM indicates that it is too busy to respond to 9529deb0eb7SJason Gunthorpe * this command then retry before giving up. It can take 9539deb0eb7SJason Gunthorpe * several seconds for this TPM to be ready. 9549deb0eb7SJason Gunthorpe * 9559deb0eb7SJason Gunthorpe * This can happen if the TPM has already been sent the 9569deb0eb7SJason Gunthorpe * SaveState command before the driver has loaded. TCG 1.2 9579deb0eb7SJason Gunthorpe * specification states that any communication after SaveState 9589deb0eb7SJason Gunthorpe * may cause the TPM to invalidate previously saved state. 9599deb0eb7SJason Gunthorpe */ 9609deb0eb7SJason Gunthorpe if (rc != TPM_WARN_RETRY) 9619deb0eb7SJason Gunthorpe break; 9629deb0eb7SJason Gunthorpe msleep(TPM_TIMEOUT_RETRY); 9639deb0eb7SJason Gunthorpe } 9649deb0eb7SJason Gunthorpe 9659deb0eb7SJason Gunthorpe if (rc) 9669deb0eb7SJason Gunthorpe dev_err(chip->dev, 9679deb0eb7SJason Gunthorpe "Error (%d) sending savestate before suspend\n", rc); 9689deb0eb7SJason Gunthorpe else if (try > 0) 9699deb0eb7SJason Gunthorpe dev_warn(chip->dev, "TPM savestate took %dms\n", 9709deb0eb7SJason Gunthorpe try * TPM_TIMEOUT_RETRY); 9719deb0eb7SJason Gunthorpe 9729deb0eb7SJason Gunthorpe return rc; 9739deb0eb7SJason Gunthorpe } 9749deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_pm_suspend); 9759deb0eb7SJason Gunthorpe 9769deb0eb7SJason Gunthorpe /* 9779deb0eb7SJason Gunthorpe * Resume from a power safe. The BIOS already restored 9789deb0eb7SJason Gunthorpe * the TPM state. 9799deb0eb7SJason Gunthorpe */ 9809deb0eb7SJason Gunthorpe int tpm_pm_resume(struct device *dev) 9819deb0eb7SJason Gunthorpe { 9829deb0eb7SJason Gunthorpe struct tpm_chip *chip = dev_get_drvdata(dev); 9839deb0eb7SJason Gunthorpe 9849deb0eb7SJason Gunthorpe if (chip == NULL) 9859deb0eb7SJason Gunthorpe return -ENODEV; 9869deb0eb7SJason Gunthorpe 9879deb0eb7SJason Gunthorpe return 0; 9889deb0eb7SJason Gunthorpe } 9899deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_pm_resume); 9909deb0eb7SJason Gunthorpe 9919deb0eb7SJason Gunthorpe #define TPM_GETRANDOM_RESULT_SIZE 18 9929deb0eb7SJason Gunthorpe static struct tpm_input_header tpm_getrandom_header = { 9939deb0eb7SJason Gunthorpe .tag = TPM_TAG_RQU_COMMAND, 9949deb0eb7SJason Gunthorpe .length = cpu_to_be32(14), 9959deb0eb7SJason Gunthorpe .ordinal = TPM_ORD_GET_RANDOM 9969deb0eb7SJason Gunthorpe }; 9979deb0eb7SJason Gunthorpe 9989deb0eb7SJason Gunthorpe /** 9999deb0eb7SJason Gunthorpe * tpm_get_random() - Get random bytes from the tpm's RNG 10009deb0eb7SJason Gunthorpe * @chip_num: A specific chip number for the request or TPM_ANY_NUM 10019deb0eb7SJason Gunthorpe * @out: destination buffer for the random bytes 10029deb0eb7SJason Gunthorpe * @max: the max number of bytes to write to @out 10039deb0eb7SJason Gunthorpe * 10049deb0eb7SJason Gunthorpe * Returns < 0 on error and the number of bytes read on success 10059deb0eb7SJason Gunthorpe */ 10069deb0eb7SJason Gunthorpe int tpm_get_random(u32 chip_num, u8 *out, size_t max) 10079deb0eb7SJason Gunthorpe { 10089deb0eb7SJason Gunthorpe struct tpm_chip *chip; 10099deb0eb7SJason Gunthorpe struct tpm_cmd_t tpm_cmd; 10109deb0eb7SJason Gunthorpe u32 recd, num_bytes = min_t(u32, max, TPM_MAX_RNG_DATA); 10119deb0eb7SJason Gunthorpe int err, total = 0, retries = 5; 10129deb0eb7SJason Gunthorpe u8 *dest = out; 10139deb0eb7SJason Gunthorpe 10143e14d83eSJarkko Sakkinen if (!out || !num_bytes || max > TPM_MAX_RNG_DATA) 10153e14d83eSJarkko Sakkinen return -EINVAL; 10163e14d83eSJarkko Sakkinen 10179deb0eb7SJason Gunthorpe chip = tpm_chip_find_get(chip_num); 10189deb0eb7SJason Gunthorpe if (chip == NULL) 10199deb0eb7SJason Gunthorpe return -ENODEV; 10209deb0eb7SJason Gunthorpe 10219deb0eb7SJason Gunthorpe do { 10229deb0eb7SJason Gunthorpe tpm_cmd.header.in = tpm_getrandom_header; 10239deb0eb7SJason Gunthorpe tpm_cmd.params.getrandom_in.num_bytes = cpu_to_be32(num_bytes); 10249deb0eb7SJason Gunthorpe 10259deb0eb7SJason Gunthorpe err = transmit_cmd(chip, &tpm_cmd, 10269deb0eb7SJason Gunthorpe TPM_GETRANDOM_RESULT_SIZE + num_bytes, 10279deb0eb7SJason Gunthorpe "attempting get random"); 10289deb0eb7SJason Gunthorpe if (err) 10299deb0eb7SJason Gunthorpe break; 10309deb0eb7SJason Gunthorpe 10319deb0eb7SJason Gunthorpe recd = be32_to_cpu(tpm_cmd.params.getrandom_out.rng_data_len); 10329deb0eb7SJason Gunthorpe memcpy(dest, tpm_cmd.params.getrandom_out.rng_data, recd); 10339deb0eb7SJason Gunthorpe 10349deb0eb7SJason Gunthorpe dest += recd; 10359deb0eb7SJason Gunthorpe total += recd; 10369deb0eb7SJason Gunthorpe num_bytes -= recd; 10379deb0eb7SJason Gunthorpe } while (retries-- && total < max); 10389deb0eb7SJason Gunthorpe 10393e14d83eSJarkko Sakkinen tpm_chip_put(chip); 10409deb0eb7SJason Gunthorpe return total ? total : -EIO; 10419deb0eb7SJason Gunthorpe } 10429deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_get_random); 10439deb0eb7SJason Gunthorpe 10449deb0eb7SJason Gunthorpe /* In case vendor provided release function, call it too.*/ 10459deb0eb7SJason Gunthorpe 10469deb0eb7SJason Gunthorpe void tpm_dev_vendor_release(struct tpm_chip *chip) 10479deb0eb7SJason Gunthorpe { 10489deb0eb7SJason Gunthorpe if (!chip) 10499deb0eb7SJason Gunthorpe return; 10509deb0eb7SJason Gunthorpe 10519deb0eb7SJason Gunthorpe clear_bit(chip->dev_num, dev_mask); 10529deb0eb7SJason Gunthorpe } 10539deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_dev_vendor_release); 10549deb0eb7SJason Gunthorpe 10559deb0eb7SJason Gunthorpe 10569deb0eb7SJason Gunthorpe /* 10579deb0eb7SJason Gunthorpe * Once all references to platform device are down to 0, 10589deb0eb7SJason Gunthorpe * release all allocated structures. 10599deb0eb7SJason Gunthorpe */ 1060afdba32eSJason Gunthorpe static void tpm_dev_release(struct device *dev) 10619deb0eb7SJason Gunthorpe { 10629deb0eb7SJason Gunthorpe struct tpm_chip *chip = dev_get_drvdata(dev); 10639deb0eb7SJason Gunthorpe 10649deb0eb7SJason Gunthorpe if (!chip) 10659deb0eb7SJason Gunthorpe return; 10669deb0eb7SJason Gunthorpe 10679deb0eb7SJason Gunthorpe tpm_dev_vendor_release(chip); 10689deb0eb7SJason Gunthorpe 10699deb0eb7SJason Gunthorpe chip->release(dev); 10709deb0eb7SJason Gunthorpe kfree(chip); 10719deb0eb7SJason Gunthorpe } 10729deb0eb7SJason Gunthorpe 10739deb0eb7SJason Gunthorpe /* 10749deb0eb7SJason Gunthorpe * Called from tpm_<specific>.c probe function only for devices 10759deb0eb7SJason Gunthorpe * the driver has determined it should claim. Prior to calling 10769deb0eb7SJason Gunthorpe * this function the specific probe function has called pci_enable_device 10779deb0eb7SJason Gunthorpe * upon errant exit from this function specific probe function should call 10789deb0eb7SJason Gunthorpe * pci_disable_device 10799deb0eb7SJason Gunthorpe */ 10809deb0eb7SJason Gunthorpe struct tpm_chip *tpm_register_hardware(struct device *dev, 108101ad1fa7SJason Gunthorpe const struct tpm_class_ops *ops) 10829deb0eb7SJason Gunthorpe { 10839deb0eb7SJason Gunthorpe struct tpm_chip *chip; 10849deb0eb7SJason Gunthorpe 10859deb0eb7SJason Gunthorpe /* Driver specific per-device data */ 10869deb0eb7SJason Gunthorpe chip = kzalloc(sizeof(*chip), GFP_KERNEL); 10879deb0eb7SJason Gunthorpe 10889deb0eb7SJason Gunthorpe if (chip == NULL) 10899deb0eb7SJason Gunthorpe return NULL; 10909deb0eb7SJason Gunthorpe 10919deb0eb7SJason Gunthorpe mutex_init(&chip->tpm_mutex); 10929deb0eb7SJason Gunthorpe INIT_LIST_HEAD(&chip->list); 10939deb0eb7SJason Gunthorpe 10945f82e9f0SJason Gunthorpe chip->ops = ops; 10959deb0eb7SJason Gunthorpe chip->dev_num = find_first_zero_bit(dev_mask, TPM_NUM_DEVICES); 10969deb0eb7SJason Gunthorpe 10979deb0eb7SJason Gunthorpe if (chip->dev_num >= TPM_NUM_DEVICES) { 10989deb0eb7SJason Gunthorpe dev_err(dev, "No available tpm device numbers\n"); 10999deb0eb7SJason Gunthorpe goto out_free; 1100afdba32eSJason Gunthorpe } 11019deb0eb7SJason Gunthorpe 11029deb0eb7SJason Gunthorpe set_bit(chip->dev_num, dev_mask); 11039deb0eb7SJason Gunthorpe 11049deb0eb7SJason Gunthorpe scnprintf(chip->devname, sizeof(chip->devname), "%s%d", "tpm", 11059deb0eb7SJason Gunthorpe chip->dev_num); 11069deb0eb7SJason Gunthorpe 11079deb0eb7SJason Gunthorpe chip->dev = get_device(dev); 11089deb0eb7SJason Gunthorpe chip->release = dev->release; 11099deb0eb7SJason Gunthorpe dev->release = tpm_dev_release; 11109deb0eb7SJason Gunthorpe dev_set_drvdata(dev, chip); 11119deb0eb7SJason Gunthorpe 1112afdba32eSJason Gunthorpe if (tpm_dev_add_device(chip)) 11139deb0eb7SJason Gunthorpe goto put_device; 11149deb0eb7SJason Gunthorpe 11151e3b73a9SJason Gunthorpe if (tpm_sysfs_add_device(chip)) 1116afdba32eSJason Gunthorpe goto del_misc; 11179deb0eb7SJason Gunthorpe 1118afdba32eSJason Gunthorpe if (tpm_add_ppi(&dev->kobj)) 1119b49e1043SStefan Berger goto del_sysfs; 11209deb0eb7SJason Gunthorpe 11219deb0eb7SJason Gunthorpe chip->bios_dir = tpm_bios_log_setup(chip->devname); 11229deb0eb7SJason Gunthorpe 11239deb0eb7SJason Gunthorpe /* Make chip available */ 11249deb0eb7SJason Gunthorpe spin_lock(&driver_lock); 11259deb0eb7SJason Gunthorpe list_add_rcu(&chip->list, &tpm_chip_list); 11269deb0eb7SJason Gunthorpe spin_unlock(&driver_lock); 11279deb0eb7SJason Gunthorpe 11289deb0eb7SJason Gunthorpe return chip; 11299deb0eb7SJason Gunthorpe 1130b49e1043SStefan Berger del_sysfs: 1131b49e1043SStefan Berger tpm_sysfs_del_device(chip); 1132afdba32eSJason Gunthorpe del_misc: 1133afdba32eSJason Gunthorpe tpm_dev_del_device(chip); 11349deb0eb7SJason Gunthorpe put_device: 11359deb0eb7SJason Gunthorpe put_device(chip->dev); 11369deb0eb7SJason Gunthorpe out_free: 11379deb0eb7SJason Gunthorpe kfree(chip); 11389deb0eb7SJason Gunthorpe return NULL; 11399deb0eb7SJason Gunthorpe } 11409deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_register_hardware); 11419deb0eb7SJason Gunthorpe 11429deb0eb7SJason Gunthorpe MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)"); 11439deb0eb7SJason Gunthorpe MODULE_DESCRIPTION("TPM Driver"); 11449deb0eb7SJason Gunthorpe MODULE_VERSION("2.0"); 11459deb0eb7SJason Gunthorpe MODULE_LICENSE("GPL"); 1146