19deb0eb7SJason Gunthorpe /* 29deb0eb7SJason Gunthorpe * Copyright (C) 2004 IBM Corporation 3afb5abc2SJarkko Sakkinen * Copyright (C) 2014 Intel Corporation 49deb0eb7SJason Gunthorpe * 59deb0eb7SJason Gunthorpe * Authors: 69deb0eb7SJason Gunthorpe * Leendert van Doorn <leendert@watson.ibm.com> 79deb0eb7SJason Gunthorpe * Dave Safford <safford@watson.ibm.com> 89deb0eb7SJason Gunthorpe * Reiner Sailer <sailer@watson.ibm.com> 99deb0eb7SJason Gunthorpe * Kylene Hall <kjhall@us.ibm.com> 109deb0eb7SJason Gunthorpe * 119deb0eb7SJason Gunthorpe * Maintained by: <tpmdd-devel@lists.sourceforge.net> 129deb0eb7SJason Gunthorpe * 139deb0eb7SJason Gunthorpe * Device driver for TCG/TCPA TPM (trusted platform module). 149deb0eb7SJason Gunthorpe * Specifications at www.trustedcomputinggroup.org 159deb0eb7SJason Gunthorpe * 169deb0eb7SJason Gunthorpe * This program is free software; you can redistribute it and/or 179deb0eb7SJason Gunthorpe * modify it under the terms of the GNU General Public License as 189deb0eb7SJason Gunthorpe * published by the Free Software Foundation, version 2 of the 199deb0eb7SJason Gunthorpe * License. 209deb0eb7SJason Gunthorpe * 219deb0eb7SJason Gunthorpe * Note, the TPM chip is not interrupt driven (only polling) 229deb0eb7SJason Gunthorpe * and can have very long timeouts (minutes!). Hence the unusual 239deb0eb7SJason Gunthorpe * calls to msleep. 249deb0eb7SJason Gunthorpe * 259deb0eb7SJason Gunthorpe */ 269deb0eb7SJason Gunthorpe 279deb0eb7SJason Gunthorpe #include <linux/poll.h> 289deb0eb7SJason Gunthorpe #include <linux/slab.h> 299deb0eb7SJason Gunthorpe #include <linux/mutex.h> 309deb0eb7SJason Gunthorpe #include <linux/spinlock.h> 319deb0eb7SJason Gunthorpe #include <linux/freezer.h> 32e74f2f76SWinkler, Tomas #include <linux/pm_runtime.h> 339deb0eb7SJason Gunthorpe 349deb0eb7SJason Gunthorpe #include "tpm.h" 359deb0eb7SJason Gunthorpe #include "tpm_eventlog.h" 369deb0eb7SJason Gunthorpe 379deb0eb7SJason Gunthorpe #define TPM_MAX_ORDINAL 243 389deb0eb7SJason Gunthorpe #define TSC_MAX_ORDINAL 12 399deb0eb7SJason Gunthorpe #define TPM_PROTECTED_COMMAND 0x00 409deb0eb7SJason Gunthorpe #define TPM_CONNECTION_COMMAND 0x40 419deb0eb7SJason Gunthorpe 429deb0eb7SJason Gunthorpe /* 439deb0eb7SJason Gunthorpe * Bug workaround - some TPM's don't flush the most 449deb0eb7SJason Gunthorpe * recently changed pcr on suspend, so force the flush 459deb0eb7SJason Gunthorpe * with an extend to the selected _unused_ non-volatile pcr. 469deb0eb7SJason Gunthorpe */ 479deb0eb7SJason Gunthorpe static int tpm_suspend_pcr; 489deb0eb7SJason Gunthorpe module_param_named(suspend_pcr, tpm_suspend_pcr, uint, 0644); 499deb0eb7SJason Gunthorpe MODULE_PARM_DESC(suspend_pcr, 5039f5712bSDmitry Torokhov "PCR to use for dummy writes to facilitate flush on suspend."); 519deb0eb7SJason Gunthorpe 529deb0eb7SJason Gunthorpe /* 539deb0eb7SJason Gunthorpe * Array with one entry per ordinal defining the maximum amount 549deb0eb7SJason Gunthorpe * of time the chip could take to return the result. The ordinal 559deb0eb7SJason Gunthorpe * designation of short, medium or long is defined in a table in 569deb0eb7SJason Gunthorpe * TCG Specification TPM Main Part 2 TPM Structures Section 17. The 579deb0eb7SJason Gunthorpe * values of the SHORT, MEDIUM, and LONG durations are retrieved 589deb0eb7SJason Gunthorpe * from the chip during initialization with a call to tpm_get_timeouts. 599deb0eb7SJason Gunthorpe */ 609deb0eb7SJason Gunthorpe static const u8 tpm_ordinal_duration[TPM_MAX_ORDINAL] = { 619deb0eb7SJason Gunthorpe TPM_UNDEFINED, /* 0 */ 629deb0eb7SJason Gunthorpe TPM_UNDEFINED, 639deb0eb7SJason Gunthorpe TPM_UNDEFINED, 649deb0eb7SJason Gunthorpe TPM_UNDEFINED, 659deb0eb7SJason Gunthorpe TPM_UNDEFINED, 669deb0eb7SJason Gunthorpe TPM_UNDEFINED, /* 5 */ 679deb0eb7SJason Gunthorpe TPM_UNDEFINED, 689deb0eb7SJason Gunthorpe TPM_UNDEFINED, 699deb0eb7SJason Gunthorpe TPM_UNDEFINED, 709deb0eb7SJason Gunthorpe TPM_UNDEFINED, 719deb0eb7SJason Gunthorpe TPM_SHORT, /* 10 */ 729deb0eb7SJason Gunthorpe TPM_SHORT, 739deb0eb7SJason Gunthorpe TPM_MEDIUM, 749deb0eb7SJason Gunthorpe TPM_LONG, 759deb0eb7SJason Gunthorpe TPM_LONG, 769deb0eb7SJason Gunthorpe TPM_MEDIUM, /* 15 */ 779deb0eb7SJason Gunthorpe TPM_SHORT, 789deb0eb7SJason Gunthorpe TPM_SHORT, 799deb0eb7SJason Gunthorpe TPM_MEDIUM, 809deb0eb7SJason Gunthorpe TPM_LONG, 819deb0eb7SJason Gunthorpe TPM_SHORT, /* 20 */ 829deb0eb7SJason Gunthorpe TPM_SHORT, 839deb0eb7SJason Gunthorpe TPM_MEDIUM, 849deb0eb7SJason Gunthorpe TPM_MEDIUM, 859deb0eb7SJason Gunthorpe TPM_MEDIUM, 869deb0eb7SJason Gunthorpe TPM_SHORT, /* 25 */ 879deb0eb7SJason Gunthorpe TPM_SHORT, 889deb0eb7SJason Gunthorpe TPM_MEDIUM, 899deb0eb7SJason Gunthorpe TPM_SHORT, 909deb0eb7SJason Gunthorpe TPM_SHORT, 919deb0eb7SJason Gunthorpe TPM_MEDIUM, /* 30 */ 929deb0eb7SJason Gunthorpe TPM_LONG, 939deb0eb7SJason Gunthorpe TPM_MEDIUM, 949deb0eb7SJason Gunthorpe TPM_SHORT, 959deb0eb7SJason Gunthorpe TPM_SHORT, 969deb0eb7SJason Gunthorpe TPM_SHORT, /* 35 */ 979deb0eb7SJason Gunthorpe TPM_MEDIUM, 989deb0eb7SJason Gunthorpe TPM_MEDIUM, 999deb0eb7SJason Gunthorpe TPM_UNDEFINED, 1009deb0eb7SJason Gunthorpe TPM_UNDEFINED, 1019deb0eb7SJason Gunthorpe TPM_MEDIUM, /* 40 */ 1029deb0eb7SJason Gunthorpe TPM_LONG, 1039deb0eb7SJason Gunthorpe TPM_MEDIUM, 1049deb0eb7SJason Gunthorpe TPM_SHORT, 1059deb0eb7SJason Gunthorpe TPM_SHORT, 1069deb0eb7SJason Gunthorpe TPM_SHORT, /* 45 */ 1079deb0eb7SJason Gunthorpe TPM_SHORT, 1089deb0eb7SJason Gunthorpe TPM_SHORT, 1099deb0eb7SJason Gunthorpe TPM_SHORT, 1109deb0eb7SJason Gunthorpe TPM_LONG, 1119deb0eb7SJason Gunthorpe TPM_MEDIUM, /* 50 */ 1129deb0eb7SJason Gunthorpe TPM_MEDIUM, 1139deb0eb7SJason Gunthorpe TPM_UNDEFINED, 1149deb0eb7SJason Gunthorpe TPM_UNDEFINED, 1159deb0eb7SJason Gunthorpe TPM_UNDEFINED, 1169deb0eb7SJason Gunthorpe TPM_UNDEFINED, /* 55 */ 1179deb0eb7SJason Gunthorpe TPM_UNDEFINED, 1189deb0eb7SJason Gunthorpe TPM_UNDEFINED, 1199deb0eb7SJason Gunthorpe TPM_UNDEFINED, 1209deb0eb7SJason Gunthorpe TPM_UNDEFINED, 1219deb0eb7SJason Gunthorpe TPM_MEDIUM, /* 60 */ 1229deb0eb7SJason Gunthorpe TPM_MEDIUM, 1239deb0eb7SJason Gunthorpe TPM_MEDIUM, 1249deb0eb7SJason Gunthorpe TPM_SHORT, 1259deb0eb7SJason Gunthorpe TPM_SHORT, 1269deb0eb7SJason Gunthorpe TPM_MEDIUM, /* 65 */ 1279deb0eb7SJason Gunthorpe TPM_UNDEFINED, 1289deb0eb7SJason Gunthorpe TPM_UNDEFINED, 1299deb0eb7SJason Gunthorpe TPM_UNDEFINED, 1309deb0eb7SJason Gunthorpe TPM_UNDEFINED, 1319deb0eb7SJason Gunthorpe TPM_SHORT, /* 70 */ 1329deb0eb7SJason Gunthorpe TPM_SHORT, 1339deb0eb7SJason Gunthorpe TPM_UNDEFINED, 1349deb0eb7SJason Gunthorpe TPM_UNDEFINED, 1359deb0eb7SJason Gunthorpe TPM_UNDEFINED, 1369deb0eb7SJason Gunthorpe TPM_UNDEFINED, /* 75 */ 1379deb0eb7SJason Gunthorpe TPM_UNDEFINED, 1389deb0eb7SJason Gunthorpe TPM_UNDEFINED, 1399deb0eb7SJason Gunthorpe TPM_UNDEFINED, 1409deb0eb7SJason Gunthorpe TPM_UNDEFINED, 1419deb0eb7SJason Gunthorpe TPM_LONG, /* 80 */ 1429deb0eb7SJason Gunthorpe TPM_UNDEFINED, 1439deb0eb7SJason Gunthorpe TPM_MEDIUM, 1449deb0eb7SJason Gunthorpe TPM_LONG, 1459deb0eb7SJason Gunthorpe TPM_SHORT, 1469deb0eb7SJason Gunthorpe TPM_UNDEFINED, /* 85 */ 1479deb0eb7SJason Gunthorpe TPM_UNDEFINED, 1489deb0eb7SJason Gunthorpe TPM_UNDEFINED, 1499deb0eb7SJason Gunthorpe TPM_UNDEFINED, 1509deb0eb7SJason Gunthorpe TPM_UNDEFINED, 1519deb0eb7SJason Gunthorpe TPM_SHORT, /* 90 */ 1529deb0eb7SJason Gunthorpe TPM_SHORT, 1539deb0eb7SJason Gunthorpe TPM_SHORT, 1549deb0eb7SJason Gunthorpe TPM_SHORT, 1559deb0eb7SJason Gunthorpe TPM_SHORT, 1569deb0eb7SJason Gunthorpe TPM_UNDEFINED, /* 95 */ 1579deb0eb7SJason Gunthorpe TPM_UNDEFINED, 1589deb0eb7SJason Gunthorpe TPM_UNDEFINED, 1599deb0eb7SJason Gunthorpe TPM_UNDEFINED, 1609deb0eb7SJason Gunthorpe TPM_UNDEFINED, 1619deb0eb7SJason Gunthorpe TPM_MEDIUM, /* 100 */ 1629deb0eb7SJason Gunthorpe TPM_SHORT, 1639deb0eb7SJason Gunthorpe TPM_SHORT, 1649deb0eb7SJason Gunthorpe TPM_UNDEFINED, 1659deb0eb7SJason Gunthorpe TPM_UNDEFINED, 1669deb0eb7SJason Gunthorpe TPM_UNDEFINED, /* 105 */ 1679deb0eb7SJason Gunthorpe TPM_UNDEFINED, 1689deb0eb7SJason Gunthorpe TPM_UNDEFINED, 1699deb0eb7SJason Gunthorpe TPM_UNDEFINED, 1709deb0eb7SJason Gunthorpe TPM_UNDEFINED, 1719deb0eb7SJason Gunthorpe TPM_SHORT, /* 110 */ 1729deb0eb7SJason Gunthorpe TPM_SHORT, 1739deb0eb7SJason Gunthorpe TPM_SHORT, 1749deb0eb7SJason Gunthorpe TPM_SHORT, 1759deb0eb7SJason Gunthorpe TPM_SHORT, 1769deb0eb7SJason Gunthorpe TPM_SHORT, /* 115 */ 1779deb0eb7SJason Gunthorpe TPM_SHORT, 1789deb0eb7SJason Gunthorpe TPM_SHORT, 1799deb0eb7SJason Gunthorpe TPM_UNDEFINED, 1809deb0eb7SJason Gunthorpe TPM_UNDEFINED, 1819deb0eb7SJason Gunthorpe TPM_LONG, /* 120 */ 1829deb0eb7SJason Gunthorpe TPM_LONG, 1839deb0eb7SJason Gunthorpe TPM_MEDIUM, 1849deb0eb7SJason Gunthorpe TPM_UNDEFINED, 1859deb0eb7SJason Gunthorpe TPM_SHORT, 1869deb0eb7SJason Gunthorpe TPM_SHORT, /* 125 */ 1879deb0eb7SJason Gunthorpe TPM_SHORT, 1889deb0eb7SJason Gunthorpe TPM_LONG, 1899deb0eb7SJason Gunthorpe TPM_SHORT, 1909deb0eb7SJason Gunthorpe TPM_SHORT, 1919deb0eb7SJason Gunthorpe TPM_SHORT, /* 130 */ 1929deb0eb7SJason Gunthorpe TPM_MEDIUM, 1939deb0eb7SJason Gunthorpe TPM_UNDEFINED, 1949deb0eb7SJason Gunthorpe TPM_SHORT, 1959deb0eb7SJason Gunthorpe TPM_MEDIUM, 1969deb0eb7SJason Gunthorpe TPM_UNDEFINED, /* 135 */ 1979deb0eb7SJason Gunthorpe TPM_UNDEFINED, 1989deb0eb7SJason Gunthorpe TPM_UNDEFINED, 1999deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2009deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2019deb0eb7SJason Gunthorpe TPM_SHORT, /* 140 */ 2029deb0eb7SJason Gunthorpe TPM_SHORT, 2039deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2049deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2059deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2069deb0eb7SJason Gunthorpe TPM_UNDEFINED, /* 145 */ 2079deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2089deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2099deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2109deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2119deb0eb7SJason Gunthorpe TPM_SHORT, /* 150 */ 2129deb0eb7SJason Gunthorpe TPM_MEDIUM, 2139deb0eb7SJason Gunthorpe TPM_MEDIUM, 2149deb0eb7SJason Gunthorpe TPM_SHORT, 2159deb0eb7SJason Gunthorpe TPM_SHORT, 2169deb0eb7SJason Gunthorpe TPM_UNDEFINED, /* 155 */ 2179deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2189deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2199deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2209deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2219deb0eb7SJason Gunthorpe TPM_SHORT, /* 160 */ 2229deb0eb7SJason Gunthorpe TPM_SHORT, 2239deb0eb7SJason Gunthorpe TPM_SHORT, 2249deb0eb7SJason Gunthorpe TPM_SHORT, 2259deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2269deb0eb7SJason Gunthorpe TPM_UNDEFINED, /* 165 */ 2279deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2289deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2299deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2309deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2319deb0eb7SJason Gunthorpe TPM_LONG, /* 170 */ 2329deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2339deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2349deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2359deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2369deb0eb7SJason Gunthorpe TPM_UNDEFINED, /* 175 */ 2379deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2389deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2399deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2409deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2419deb0eb7SJason Gunthorpe TPM_MEDIUM, /* 180 */ 2429deb0eb7SJason Gunthorpe TPM_SHORT, 2439deb0eb7SJason Gunthorpe TPM_MEDIUM, 2449deb0eb7SJason Gunthorpe TPM_MEDIUM, 2459deb0eb7SJason Gunthorpe TPM_MEDIUM, 2469deb0eb7SJason Gunthorpe TPM_MEDIUM, /* 185 */ 2479deb0eb7SJason Gunthorpe TPM_SHORT, 2489deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2499deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2509deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2519deb0eb7SJason Gunthorpe TPM_UNDEFINED, /* 190 */ 2529deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2539deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2549deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2559deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2569deb0eb7SJason Gunthorpe TPM_UNDEFINED, /* 195 */ 2579deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2589deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2599deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2609deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2619deb0eb7SJason Gunthorpe TPM_SHORT, /* 200 */ 2629deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2639deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2649deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2659deb0eb7SJason Gunthorpe TPM_SHORT, 2669deb0eb7SJason Gunthorpe TPM_SHORT, /* 205 */ 2679deb0eb7SJason Gunthorpe TPM_SHORT, 2689deb0eb7SJason Gunthorpe TPM_SHORT, 2699deb0eb7SJason Gunthorpe TPM_SHORT, 2709deb0eb7SJason Gunthorpe TPM_SHORT, 2719deb0eb7SJason Gunthorpe TPM_MEDIUM, /* 210 */ 2729deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2739deb0eb7SJason Gunthorpe TPM_MEDIUM, 2749deb0eb7SJason Gunthorpe TPM_MEDIUM, 2759deb0eb7SJason Gunthorpe TPM_MEDIUM, 2769deb0eb7SJason Gunthorpe TPM_UNDEFINED, /* 215 */ 2779deb0eb7SJason Gunthorpe TPM_MEDIUM, 2789deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2799deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2809deb0eb7SJason Gunthorpe TPM_SHORT, 2819deb0eb7SJason Gunthorpe TPM_SHORT, /* 220 */ 2829deb0eb7SJason Gunthorpe TPM_SHORT, 2839deb0eb7SJason Gunthorpe TPM_SHORT, 2849deb0eb7SJason Gunthorpe TPM_SHORT, 2859deb0eb7SJason Gunthorpe TPM_SHORT, 2869deb0eb7SJason Gunthorpe TPM_UNDEFINED, /* 225 */ 2879deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2889deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2899deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2909deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2919deb0eb7SJason Gunthorpe TPM_SHORT, /* 230 */ 2929deb0eb7SJason Gunthorpe TPM_LONG, 2939deb0eb7SJason Gunthorpe TPM_MEDIUM, 2949deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2959deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2969deb0eb7SJason Gunthorpe TPM_UNDEFINED, /* 235 */ 2979deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2989deb0eb7SJason Gunthorpe TPM_UNDEFINED, 2999deb0eb7SJason Gunthorpe TPM_UNDEFINED, 3009deb0eb7SJason Gunthorpe TPM_UNDEFINED, 3019deb0eb7SJason Gunthorpe TPM_SHORT, /* 240 */ 3029deb0eb7SJason Gunthorpe TPM_UNDEFINED, 3039deb0eb7SJason Gunthorpe TPM_MEDIUM, 3049deb0eb7SJason Gunthorpe }; 3059deb0eb7SJason Gunthorpe 3069deb0eb7SJason Gunthorpe /* 3079deb0eb7SJason Gunthorpe * Returns max number of jiffies to wait 3089deb0eb7SJason Gunthorpe */ 3099deb0eb7SJason Gunthorpe unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip, 3109deb0eb7SJason Gunthorpe u32 ordinal) 3119deb0eb7SJason Gunthorpe { 3129deb0eb7SJason Gunthorpe int duration_idx = TPM_UNDEFINED; 3139deb0eb7SJason Gunthorpe int duration = 0; 3149deb0eb7SJason Gunthorpe 315f7286430SMartin Wilck /* 316f7286430SMartin Wilck * We only have a duration table for protected commands, where the upper 317f7286430SMartin Wilck * 16 bits are 0. For the few other ordinals the fallback will be used. 318f7286430SMartin Wilck */ 319f7286430SMartin Wilck if (ordinal < TPM_MAX_ORDINAL) 3209deb0eb7SJason Gunthorpe duration_idx = tpm_ordinal_duration[ordinal]; 3219deb0eb7SJason Gunthorpe 3229deb0eb7SJason Gunthorpe if (duration_idx != TPM_UNDEFINED) 323af782f33SChristophe Ricard duration = chip->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 331f865c196SWinkler, Tomas /** 332f865c196SWinkler, Tomas * tmp_transmit - Internal kernel interface to transmit TPM commands. 333f865c196SWinkler, Tomas * 334f865c196SWinkler, Tomas * @chip: TPM chip to use 335f865c196SWinkler, Tomas * @buf: TPM command buffer 336f865c196SWinkler, Tomas * @bufsiz: length of the TPM command buffer 337f865c196SWinkler, Tomas * @flags: tpm transmit flags - bitmap 338f865c196SWinkler, Tomas * 339f865c196SWinkler, Tomas * Return: 340f865c196SWinkler, Tomas * 0 when the operation is successful. 341f865c196SWinkler, Tomas * A negative number for system errors (errno). 3429deb0eb7SJason Gunthorpe */ 343d4816edfSJarkko Sakkinen ssize_t tpm_transmit(struct tpm_chip *chip, const u8 *buf, size_t bufsiz, 344d4816edfSJarkko Sakkinen unsigned int flags) 3459deb0eb7SJason Gunthorpe { 3469deb0eb7SJason Gunthorpe ssize_t rc; 3479deb0eb7SJason Gunthorpe u32 count, ordinal; 3489deb0eb7SJason Gunthorpe unsigned long stop; 3499deb0eb7SJason Gunthorpe 350ebfd7532SJarkko Sakkinen if (bufsiz < TPM_HEADER_SIZE) 351ebfd7532SJarkko Sakkinen return -EINVAL; 352ebfd7532SJarkko Sakkinen 3539deb0eb7SJason Gunthorpe if (bufsiz > TPM_BUFSIZE) 3549deb0eb7SJason Gunthorpe bufsiz = TPM_BUFSIZE; 3559deb0eb7SJason Gunthorpe 3569deb0eb7SJason Gunthorpe count = be32_to_cpu(*((__be32 *) (buf + 2))); 3579deb0eb7SJason Gunthorpe ordinal = be32_to_cpu(*((__be32 *) (buf + 6))); 3589deb0eb7SJason Gunthorpe if (count == 0) 3599deb0eb7SJason Gunthorpe return -ENODATA; 3609deb0eb7SJason Gunthorpe if (count > bufsiz) { 3618cfffc9dSJason Gunthorpe dev_err(&chip->dev, 3629deb0eb7SJason Gunthorpe "invalid count value %x %zx\n", count, bufsiz); 3639deb0eb7SJason Gunthorpe return -E2BIG; 3649deb0eb7SJason Gunthorpe } 3659deb0eb7SJason Gunthorpe 366d4816edfSJarkko Sakkinen if (!(flags & TPM_TRANSMIT_UNLOCKED)) 3679deb0eb7SJason Gunthorpe mutex_lock(&chip->tpm_mutex); 3689deb0eb7SJason Gunthorpe 3696804f6bbSStefan Berger if (chip->dev.parent) 370e74f2f76SWinkler, Tomas pm_runtime_get_sync(chip->dev.parent); 371e74f2f76SWinkler, Tomas 3725f82e9f0SJason Gunthorpe rc = chip->ops->send(chip, (u8 *) buf, count); 3739deb0eb7SJason Gunthorpe if (rc < 0) { 3748cfffc9dSJason Gunthorpe dev_err(&chip->dev, 3759deb0eb7SJason Gunthorpe "tpm_transmit: tpm_send: error %zd\n", rc); 3769deb0eb7SJason Gunthorpe goto out; 3779deb0eb7SJason Gunthorpe } 3789deb0eb7SJason Gunthorpe 379570a3609SChristophe Ricard if (chip->flags & TPM_CHIP_FLAG_IRQ) 3809deb0eb7SJason Gunthorpe goto out_recv; 3819deb0eb7SJason Gunthorpe 3827a1d7e6dSJarkko Sakkinen if (chip->flags & TPM_CHIP_FLAG_TPM2) 3837a1d7e6dSJarkko Sakkinen stop = jiffies + tpm2_calc_ordinal_duration(chip, ordinal); 3847a1d7e6dSJarkko Sakkinen else 3859deb0eb7SJason Gunthorpe stop = jiffies + tpm_calc_ordinal_duration(chip, ordinal); 3869deb0eb7SJason Gunthorpe do { 3875f82e9f0SJason Gunthorpe u8 status = chip->ops->status(chip); 3885f82e9f0SJason Gunthorpe if ((status & chip->ops->req_complete_mask) == 3895f82e9f0SJason Gunthorpe chip->ops->req_complete_val) 3909deb0eb7SJason Gunthorpe goto out_recv; 3919deb0eb7SJason Gunthorpe 3925f82e9f0SJason Gunthorpe if (chip->ops->req_canceled(chip, status)) { 3938cfffc9dSJason Gunthorpe dev_err(&chip->dev, "Operation Canceled\n"); 3949deb0eb7SJason Gunthorpe rc = -ECANCELED; 3959deb0eb7SJason Gunthorpe goto out; 3969deb0eb7SJason Gunthorpe } 3979deb0eb7SJason Gunthorpe 3989deb0eb7SJason Gunthorpe msleep(TPM_TIMEOUT); /* CHECK */ 3999deb0eb7SJason Gunthorpe rmb(); 4009deb0eb7SJason Gunthorpe } while (time_before(jiffies, stop)); 4019deb0eb7SJason Gunthorpe 4025f82e9f0SJason Gunthorpe chip->ops->cancel(chip); 4038cfffc9dSJason Gunthorpe dev_err(&chip->dev, "Operation Timed out\n"); 4049deb0eb7SJason Gunthorpe rc = -ETIME; 4059deb0eb7SJason Gunthorpe goto out; 4069deb0eb7SJason Gunthorpe 4079deb0eb7SJason Gunthorpe out_recv: 4085f82e9f0SJason Gunthorpe rc = chip->ops->recv(chip, (u8 *) buf, bufsiz); 4099deb0eb7SJason Gunthorpe if (rc < 0) 4108cfffc9dSJason Gunthorpe dev_err(&chip->dev, 4119deb0eb7SJason Gunthorpe "tpm_transmit: tpm_recv: error %zd\n", rc); 4129deb0eb7SJason Gunthorpe out: 4136804f6bbSStefan Berger if (chip->dev.parent) 414e74f2f76SWinkler, Tomas pm_runtime_put_sync(chip->dev.parent); 415e74f2f76SWinkler, Tomas 416d4816edfSJarkko Sakkinen if (!(flags & TPM_TRANSMIT_UNLOCKED)) 4179deb0eb7SJason Gunthorpe mutex_unlock(&chip->tpm_mutex); 4189deb0eb7SJason Gunthorpe return rc; 4199deb0eb7SJason Gunthorpe } 4209deb0eb7SJason Gunthorpe 421f865c196SWinkler, Tomas /** 422f865c196SWinkler, Tomas * tmp_transmit_cmd - send a tpm command to the device 423f865c196SWinkler, Tomas * The function extracts tpm out header return code 424f865c196SWinkler, Tomas * 425f865c196SWinkler, Tomas * @chip: TPM chip to use 426c659af78SStefan Berger * @buf: TPM command buffer 427c659af78SStefan Berger * @bufsiz: length of the buffer 428c659af78SStefan Berger * @min_rsp_body_length: minimum expected length of response body 429f865c196SWinkler, Tomas * @flags: tpm transmit flags - bitmap 430f865c196SWinkler, Tomas * @desc: command description used in the error message 431f865c196SWinkler, Tomas * 432f865c196SWinkler, Tomas * Return: 433f865c196SWinkler, Tomas * 0 when the operation is successful. 434f865c196SWinkler, Tomas * A negative number for system errors (errno). 435f865c196SWinkler, Tomas * A positive number for a TPM error. 436f865c196SWinkler, Tomas */ 437c659af78SStefan Berger ssize_t tpm_transmit_cmd(struct tpm_chip *chip, const void *buf, 438c659af78SStefan Berger size_t bufsiz, size_t min_rsp_body_length, 439c659af78SStefan Berger unsigned int flags, const char *desc) 4409deb0eb7SJason Gunthorpe { 441d4816edfSJarkko Sakkinen const struct tpm_output_header *header; 4429deb0eb7SJason Gunthorpe int err; 443c659af78SStefan Berger ssize_t len; 4449deb0eb7SJason Gunthorpe 445c659af78SStefan Berger len = tpm_transmit(chip, (const u8 *)buf, bufsiz, flags); 4469deb0eb7SJason Gunthorpe if (len < 0) 4479deb0eb7SJason Gunthorpe return len; 4489deb0eb7SJason Gunthorpe else if (len < TPM_HEADER_SIZE) 4499deb0eb7SJason Gunthorpe return -EFAULT; 4509deb0eb7SJason Gunthorpe 451c659af78SStefan Berger header = buf; 452c659af78SStefan Berger if (len != be32_to_cpu(header->length)) 453c659af78SStefan Berger return -EFAULT; 45487155b73SJarkko Sakkinen 45587155b73SJarkko Sakkinen err = be32_to_cpu(header->return_code); 4569deb0eb7SJason Gunthorpe if (err != 0 && desc) 4578cfffc9dSJason Gunthorpe dev_err(&chip->dev, "A TPM error (%d) occurred %s\n", err, 45871ed848fSJarkko Sakkinen desc); 459c659af78SStefan Berger if (err) 4609deb0eb7SJason Gunthorpe return err; 461c659af78SStefan Berger 462c659af78SStefan Berger if (len < min_rsp_body_length + TPM_HEADER_SIZE) 463c659af78SStefan Berger return -EFAULT; 464c659af78SStefan Berger 465c659af78SStefan Berger return 0; 4669deb0eb7SJason Gunthorpe } 4679deb0eb7SJason Gunthorpe 468f865c196SWinkler, Tomas #define TPM_DIGEST_SIZE 20 469f865c196SWinkler, Tomas #define TPM_RET_CODE_IDX 6 4709deb0eb7SJason Gunthorpe #define TPM_INTERNAL_RESULT_SIZE 200 4719deb0eb7SJason Gunthorpe #define TPM_ORD_GET_CAP cpu_to_be32(101) 4729deb0eb7SJason Gunthorpe #define TPM_ORD_GET_RANDOM cpu_to_be32(70) 4739deb0eb7SJason Gunthorpe 4749deb0eb7SJason Gunthorpe static const struct tpm_input_header tpm_getcap_header = { 4759deb0eb7SJason Gunthorpe .tag = TPM_TAG_RQU_COMMAND, 4769deb0eb7SJason Gunthorpe .length = cpu_to_be32(22), 4779deb0eb7SJason Gunthorpe .ordinal = TPM_ORD_GET_CAP 4789deb0eb7SJason Gunthorpe }; 4799deb0eb7SJason Gunthorpe 48084fda152SJarkko Sakkinen ssize_t tpm_getcap(struct tpm_chip *chip, u32 subcap_id, cap_t *cap, 481c659af78SStefan Berger const char *desc, size_t min_cap_length) 4829deb0eb7SJason Gunthorpe { 4839deb0eb7SJason Gunthorpe struct tpm_cmd_t tpm_cmd; 4849deb0eb7SJason Gunthorpe int rc; 4859deb0eb7SJason Gunthorpe 4869deb0eb7SJason Gunthorpe tpm_cmd.header.in = tpm_getcap_header; 48784fda152SJarkko Sakkinen if (subcap_id == TPM_CAP_VERSION_1_1 || 48884fda152SJarkko Sakkinen subcap_id == TPM_CAP_VERSION_1_2) { 48984fda152SJarkko Sakkinen tpm_cmd.params.getcap_in.cap = cpu_to_be32(subcap_id); 4909deb0eb7SJason Gunthorpe /*subcap field not necessary */ 4919deb0eb7SJason Gunthorpe tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(0); 4929deb0eb7SJason Gunthorpe tpm_cmd.header.in.length -= cpu_to_be32(sizeof(__be32)); 4939deb0eb7SJason Gunthorpe } else { 4949deb0eb7SJason Gunthorpe if (subcap_id == TPM_CAP_FLAG_PERM || 4959deb0eb7SJason Gunthorpe subcap_id == TPM_CAP_FLAG_VOL) 49684fda152SJarkko Sakkinen tpm_cmd.params.getcap_in.cap = 49784fda152SJarkko Sakkinen cpu_to_be32(TPM_CAP_FLAG); 4989deb0eb7SJason Gunthorpe else 49984fda152SJarkko Sakkinen tpm_cmd.params.getcap_in.cap = 50084fda152SJarkko Sakkinen cpu_to_be32(TPM_CAP_PROP); 5019deb0eb7SJason Gunthorpe tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4); 50284fda152SJarkko Sakkinen tpm_cmd.params.getcap_in.subcap = cpu_to_be32(subcap_id); 5039deb0eb7SJason Gunthorpe } 504c659af78SStefan Berger rc = tpm_transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE, 505c659af78SStefan Berger min_cap_length, 0, desc); 5069deb0eb7SJason Gunthorpe if (!rc) 5079deb0eb7SJason Gunthorpe *cap = tpm_cmd.params.getcap_out.cap; 5089deb0eb7SJason Gunthorpe return rc; 5099deb0eb7SJason Gunthorpe } 510eb5854e7SJarkko Sakkinen EXPORT_SYMBOL_GPL(tpm_getcap); 5119deb0eb7SJason Gunthorpe 5129deb0eb7SJason Gunthorpe #define TPM_ORD_STARTUP cpu_to_be32(153) 5139deb0eb7SJason Gunthorpe #define TPM_ST_CLEAR cpu_to_be16(1) 5149deb0eb7SJason Gunthorpe #define TPM_ST_STATE cpu_to_be16(2) 5159deb0eb7SJason Gunthorpe #define TPM_ST_DEACTIVATED cpu_to_be16(3) 5169deb0eb7SJason Gunthorpe static const struct tpm_input_header tpm_startup_header = { 5179deb0eb7SJason Gunthorpe .tag = TPM_TAG_RQU_COMMAND, 5189deb0eb7SJason Gunthorpe .length = cpu_to_be32(12), 5199deb0eb7SJason Gunthorpe .ordinal = TPM_ORD_STARTUP 5209deb0eb7SJason Gunthorpe }; 5219deb0eb7SJason Gunthorpe 5229deb0eb7SJason Gunthorpe static int tpm_startup(struct tpm_chip *chip, __be16 startup_type) 5239deb0eb7SJason Gunthorpe { 5249deb0eb7SJason Gunthorpe struct tpm_cmd_t start_cmd; 5259deb0eb7SJason Gunthorpe start_cmd.header.in = tpm_startup_header; 5267a1d7e6dSJarkko Sakkinen 5279deb0eb7SJason Gunthorpe start_cmd.params.startup_in.startup_type = startup_type; 528d4816edfSJarkko Sakkinen return tpm_transmit_cmd(chip, &start_cmd, TPM_INTERNAL_RESULT_SIZE, 0, 529c659af78SStefan Berger 0, "attempting to start the TPM"); 5309deb0eb7SJason Gunthorpe } 5319deb0eb7SJason Gunthorpe 5329deb0eb7SJason Gunthorpe int tpm_get_timeouts(struct tpm_chip *chip) 5339deb0eb7SJason Gunthorpe { 534aaa6f7f6SEd Swierk cap_t cap; 5351d70fe9dSMaciej S. Szmigiero unsigned long timeout_old[4], timeout_chip[4], timeout_eff[4]; 5369deb0eb7SJason Gunthorpe ssize_t rc; 5379deb0eb7SJason Gunthorpe 538d1d253cfSJason Gunthorpe if (chip->flags & TPM_CHIP_FLAG_HAVE_TIMEOUTS) 539d1d253cfSJason Gunthorpe return 0; 540d1d253cfSJason Gunthorpe 54125112048SJason Gunthorpe if (chip->flags & TPM_CHIP_FLAG_TPM2) { 54225112048SJason Gunthorpe /* Fixed timeouts for TPM2 */ 543af782f33SChristophe Ricard chip->timeout_a = msecs_to_jiffies(TPM2_TIMEOUT_A); 544af782f33SChristophe Ricard chip->timeout_b = msecs_to_jiffies(TPM2_TIMEOUT_B); 545af782f33SChristophe Ricard chip->timeout_c = msecs_to_jiffies(TPM2_TIMEOUT_C); 546af782f33SChristophe Ricard chip->timeout_d = msecs_to_jiffies(TPM2_TIMEOUT_D); 547af782f33SChristophe Ricard chip->duration[TPM_SHORT] = 54825112048SJason Gunthorpe msecs_to_jiffies(TPM2_DURATION_SHORT); 549af782f33SChristophe Ricard chip->duration[TPM_MEDIUM] = 55025112048SJason Gunthorpe msecs_to_jiffies(TPM2_DURATION_MEDIUM); 551af782f33SChristophe Ricard chip->duration[TPM_LONG] = 55225112048SJason Gunthorpe msecs_to_jiffies(TPM2_DURATION_LONG); 553d1d253cfSJason Gunthorpe 554d1d253cfSJason Gunthorpe chip->flags |= TPM_CHIP_FLAG_HAVE_TIMEOUTS; 55525112048SJason Gunthorpe return 0; 55625112048SJason Gunthorpe } 55725112048SJason Gunthorpe 558c659af78SStefan Berger rc = tpm_getcap(chip, TPM_CAP_PROP_TIS_TIMEOUT, &cap, NULL, 559c659af78SStefan Berger sizeof(cap.timeout)); 5609deb0eb7SJason Gunthorpe if (rc == TPM_ERR_INVALID_POSTINIT) { 5619deb0eb7SJason Gunthorpe /* The TPM is not started, we are the first to talk to it. 5629deb0eb7SJason Gunthorpe Execute a startup command. */ 563aaa6f7f6SEd Swierk dev_info(&chip->dev, "Issuing TPM_STARTUP\n"); 5649deb0eb7SJason Gunthorpe if (tpm_startup(chip, TPM_ST_CLEAR)) 5659deb0eb7SJason Gunthorpe return rc; 5669deb0eb7SJason Gunthorpe 567aaa6f7f6SEd Swierk rc = tpm_getcap(chip, TPM_CAP_PROP_TIS_TIMEOUT, &cap, 568c659af78SStefan Berger "attempting to determine the timeouts", 569c659af78SStefan Berger sizeof(cap.timeout)); 5709deb0eb7SJason Gunthorpe } 571c659af78SStefan Berger 57262bfdacbSJason Gunthorpe if (rc) { 57362bfdacbSJason Gunthorpe dev_err(&chip->dev, 57462bfdacbSJason Gunthorpe "A TPM error (%zd) occurred attempting to determine the timeouts\n", 57562bfdacbSJason Gunthorpe rc); 576aaa6f7f6SEd Swierk return rc; 57762bfdacbSJason Gunthorpe } 5789deb0eb7SJason Gunthorpe 5791d70fe9dSMaciej S. Szmigiero timeout_old[0] = jiffies_to_usecs(chip->timeout_a); 5801d70fe9dSMaciej S. Szmigiero timeout_old[1] = jiffies_to_usecs(chip->timeout_b); 5811d70fe9dSMaciej S. Szmigiero timeout_old[2] = jiffies_to_usecs(chip->timeout_c); 5821d70fe9dSMaciej S. Szmigiero timeout_old[3] = jiffies_to_usecs(chip->timeout_d); 5831d70fe9dSMaciej S. Szmigiero timeout_chip[0] = be32_to_cpu(cap.timeout.a); 5841d70fe9dSMaciej S. Szmigiero timeout_chip[1] = be32_to_cpu(cap.timeout.b); 5851d70fe9dSMaciej S. Szmigiero timeout_chip[2] = be32_to_cpu(cap.timeout.c); 5861d70fe9dSMaciej S. Szmigiero timeout_chip[3] = be32_to_cpu(cap.timeout.d); 5871d70fe9dSMaciej S. Szmigiero memcpy(timeout_eff, timeout_chip, sizeof(timeout_eff)); 5888e54caf4SJason Gunthorpe 5898e54caf4SJason Gunthorpe /* 5908e54caf4SJason Gunthorpe * Provide ability for vendor overrides of timeout values in case 5918e54caf4SJason Gunthorpe * of misreporting. 5928e54caf4SJason Gunthorpe */ 5938e54caf4SJason Gunthorpe if (chip->ops->update_timeouts != NULL) 594af782f33SChristophe Ricard chip->timeout_adjusted = 5951d70fe9dSMaciej S. Szmigiero chip->ops->update_timeouts(chip, timeout_eff); 5968e54caf4SJason Gunthorpe 597af782f33SChristophe Ricard if (!chip->timeout_adjusted) { 5981d70fe9dSMaciej S. Szmigiero /* Restore default if chip reported 0 */ 5998e54caf4SJason Gunthorpe int i; 6008e54caf4SJason Gunthorpe 6011d70fe9dSMaciej S. Szmigiero for (i = 0; i < ARRAY_SIZE(timeout_eff); i++) { 6021d70fe9dSMaciej S. Szmigiero if (timeout_eff[i]) 6031d70fe9dSMaciej S. Szmigiero continue; 6041d70fe9dSMaciej S. Szmigiero 6051d70fe9dSMaciej S. Szmigiero timeout_eff[i] = timeout_old[i]; 6061d70fe9dSMaciej S. Szmigiero chip->timeout_adjusted = true; 6071d70fe9dSMaciej S. Szmigiero } 6081d70fe9dSMaciej S. Szmigiero 6091d70fe9dSMaciej S. Szmigiero if (timeout_eff[0] != 0 && timeout_eff[0] < 1000) { 6109deb0eb7SJason Gunthorpe /* timeouts in msec rather usec */ 6111d70fe9dSMaciej S. Szmigiero for (i = 0; i != ARRAY_SIZE(timeout_eff); i++) 6121d70fe9dSMaciej S. Szmigiero timeout_eff[i] *= 1000; 613af782f33SChristophe Ricard chip->timeout_adjusted = true; 6149deb0eb7SJason Gunthorpe } 6158e54caf4SJason Gunthorpe } 6168e54caf4SJason Gunthorpe 6178e54caf4SJason Gunthorpe /* Report adjusted timeouts */ 618af782f33SChristophe Ricard if (chip->timeout_adjusted) { 6198cfffc9dSJason Gunthorpe dev_info(&chip->dev, 6208e54caf4SJason Gunthorpe HW_ERR "Adjusting reported timeouts: A %lu->%luus B %lu->%luus C %lu->%luus D %lu->%luus\n", 6211d70fe9dSMaciej S. Szmigiero timeout_chip[0], timeout_eff[0], 6221d70fe9dSMaciej S. Szmigiero timeout_chip[1], timeout_eff[1], 6231d70fe9dSMaciej S. Szmigiero timeout_chip[2], timeout_eff[2], 6241d70fe9dSMaciej S. Szmigiero timeout_chip[3], timeout_eff[3]); 6258e54caf4SJason Gunthorpe } 6268e54caf4SJason Gunthorpe 6271d70fe9dSMaciej S. Szmigiero chip->timeout_a = usecs_to_jiffies(timeout_eff[0]); 6281d70fe9dSMaciej S. Szmigiero chip->timeout_b = usecs_to_jiffies(timeout_eff[1]); 6291d70fe9dSMaciej S. Szmigiero chip->timeout_c = usecs_to_jiffies(timeout_eff[2]); 6301d70fe9dSMaciej S. Szmigiero chip->timeout_d = usecs_to_jiffies(timeout_eff[3]); 6319deb0eb7SJason Gunthorpe 632aaa6f7f6SEd Swierk rc = tpm_getcap(chip, TPM_CAP_PROP_TIS_DURATION, &cap, 633c659af78SStefan Berger "attempting to determine the durations", 634c659af78SStefan Berger sizeof(cap.duration)); 6359deb0eb7SJason Gunthorpe if (rc) 6369deb0eb7SJason Gunthorpe return rc; 6379deb0eb7SJason Gunthorpe 638af782f33SChristophe Ricard chip->duration[TPM_SHORT] = 639aaa6f7f6SEd Swierk usecs_to_jiffies(be32_to_cpu(cap.duration.tpm_short)); 640af782f33SChristophe Ricard chip->duration[TPM_MEDIUM] = 641aaa6f7f6SEd Swierk usecs_to_jiffies(be32_to_cpu(cap.duration.tpm_medium)); 642af782f33SChristophe Ricard chip->duration[TPM_LONG] = 643aaa6f7f6SEd Swierk usecs_to_jiffies(be32_to_cpu(cap.duration.tpm_long)); 6449deb0eb7SJason Gunthorpe 6459deb0eb7SJason Gunthorpe /* The Broadcom BCM0102 chipset in a Dell Latitude D820 gets the above 6469deb0eb7SJason Gunthorpe * value wrong and apparently reports msecs rather than usecs. So we 6479deb0eb7SJason Gunthorpe * fix up the resulting too-small TPM_SHORT value to make things work. 6489deb0eb7SJason Gunthorpe * We also scale the TPM_MEDIUM and -_LONG values by 1000. 6499deb0eb7SJason Gunthorpe */ 650af782f33SChristophe Ricard if (chip->duration[TPM_SHORT] < (HZ / 100)) { 651af782f33SChristophe Ricard chip->duration[TPM_SHORT] = HZ; 652af782f33SChristophe Ricard chip->duration[TPM_MEDIUM] *= 1000; 653af782f33SChristophe Ricard chip->duration[TPM_LONG] *= 1000; 654af782f33SChristophe Ricard chip->duration_adjusted = true; 6558cfffc9dSJason Gunthorpe dev_info(&chip->dev, "Adjusting TPM timeout parameters."); 6569deb0eb7SJason Gunthorpe } 657d1d253cfSJason Gunthorpe 658d1d253cfSJason Gunthorpe chip->flags |= TPM_CHIP_FLAG_HAVE_TIMEOUTS; 6599deb0eb7SJason Gunthorpe return 0; 6609deb0eb7SJason Gunthorpe } 6619deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_get_timeouts); 6629deb0eb7SJason Gunthorpe 6639deb0eb7SJason Gunthorpe #define TPM_ORD_CONTINUE_SELFTEST 83 6649deb0eb7SJason Gunthorpe #define CONTINUE_SELFTEST_RESULT_SIZE 10 6659deb0eb7SJason Gunthorpe 6660014777fSJulia Lawall static const struct tpm_input_header continue_selftest_header = { 6679deb0eb7SJason Gunthorpe .tag = TPM_TAG_RQU_COMMAND, 6689deb0eb7SJason Gunthorpe .length = cpu_to_be32(10), 6699deb0eb7SJason Gunthorpe .ordinal = cpu_to_be32(TPM_ORD_CONTINUE_SELFTEST), 6709deb0eb7SJason Gunthorpe }; 6719deb0eb7SJason Gunthorpe 6729deb0eb7SJason Gunthorpe /** 6739deb0eb7SJason Gunthorpe * tpm_continue_selftest -- run TPM's selftest 6749deb0eb7SJason Gunthorpe * @chip: TPM chip to use 6759deb0eb7SJason Gunthorpe * 6769deb0eb7SJason Gunthorpe * Returns 0 on success, < 0 in case of fatal error or a value > 0 representing 6779deb0eb7SJason Gunthorpe * a TPM error code. 6789deb0eb7SJason Gunthorpe */ 6799deb0eb7SJason Gunthorpe static int tpm_continue_selftest(struct tpm_chip *chip) 6809deb0eb7SJason Gunthorpe { 6819deb0eb7SJason Gunthorpe int rc; 6829deb0eb7SJason Gunthorpe struct tpm_cmd_t cmd; 6839deb0eb7SJason Gunthorpe 6849deb0eb7SJason Gunthorpe cmd.header.in = continue_selftest_header; 685c659af78SStefan Berger rc = tpm_transmit_cmd(chip, &cmd, CONTINUE_SELFTEST_RESULT_SIZE, 0, 0, 6869deb0eb7SJason Gunthorpe "continue selftest"); 6879deb0eb7SJason Gunthorpe return rc; 6889deb0eb7SJason Gunthorpe } 6899deb0eb7SJason Gunthorpe 6909deb0eb7SJason Gunthorpe #define TPM_ORDINAL_PCRREAD cpu_to_be32(21) 6919deb0eb7SJason Gunthorpe #define READ_PCR_RESULT_SIZE 30 692c659af78SStefan Berger #define READ_PCR_RESULT_BODY_SIZE 20 6930014777fSJulia Lawall static const struct tpm_input_header pcrread_header = { 6949deb0eb7SJason Gunthorpe .tag = TPM_TAG_RQU_COMMAND, 6959deb0eb7SJason Gunthorpe .length = cpu_to_be32(14), 6969deb0eb7SJason Gunthorpe .ordinal = TPM_ORDINAL_PCRREAD 6979deb0eb7SJason Gunthorpe }; 6989deb0eb7SJason Gunthorpe 699000a07b0SJason Gunthorpe int tpm_pcr_read_dev(struct tpm_chip *chip, int pcr_idx, u8 *res_buf) 7009deb0eb7SJason Gunthorpe { 7019deb0eb7SJason Gunthorpe int rc; 7029deb0eb7SJason Gunthorpe struct tpm_cmd_t cmd; 7039deb0eb7SJason Gunthorpe 7049deb0eb7SJason Gunthorpe cmd.header.in = pcrread_header; 7059deb0eb7SJason Gunthorpe cmd.params.pcrread_in.pcr_idx = cpu_to_be32(pcr_idx); 706c659af78SStefan Berger rc = tpm_transmit_cmd(chip, &cmd, READ_PCR_RESULT_SIZE, 707c659af78SStefan Berger READ_PCR_RESULT_BODY_SIZE, 0, 7089deb0eb7SJason Gunthorpe "attempting to read a pcr value"); 7099deb0eb7SJason Gunthorpe 7109deb0eb7SJason Gunthorpe if (rc == 0) 7119deb0eb7SJason Gunthorpe memcpy(res_buf, cmd.params.pcrread_out.pcr_result, 7129deb0eb7SJason Gunthorpe TPM_DIGEST_SIZE); 7139deb0eb7SJason Gunthorpe return rc; 7149deb0eb7SJason Gunthorpe } 7159deb0eb7SJason Gunthorpe 7169deb0eb7SJason Gunthorpe /** 717954650efSJarkko Sakkinen * tpm_is_tpm2 - is the chip a TPM2 chip? 718954650efSJarkko Sakkinen * @chip_num: tpm idx # or ANY 719954650efSJarkko Sakkinen * 720954650efSJarkko Sakkinen * Returns < 0 on error, and 1 or 0 on success depending whether the chip 721954650efSJarkko Sakkinen * is a TPM2 chip. 722954650efSJarkko Sakkinen */ 723954650efSJarkko Sakkinen int tpm_is_tpm2(u32 chip_num) 724954650efSJarkko Sakkinen { 725954650efSJarkko Sakkinen struct tpm_chip *chip; 726954650efSJarkko Sakkinen int rc; 727954650efSJarkko Sakkinen 728954650efSJarkko Sakkinen chip = tpm_chip_find_get(chip_num); 729954650efSJarkko Sakkinen if (chip == NULL) 730954650efSJarkko Sakkinen return -ENODEV; 731954650efSJarkko Sakkinen 732954650efSJarkko Sakkinen rc = (chip->flags & TPM_CHIP_FLAG_TPM2) != 0; 733954650efSJarkko Sakkinen 7344e26195fSJason Gunthorpe tpm_put_ops(chip); 735954650efSJarkko Sakkinen 736954650efSJarkko Sakkinen return rc; 737954650efSJarkko Sakkinen } 738954650efSJarkko Sakkinen EXPORT_SYMBOL_GPL(tpm_is_tpm2); 739954650efSJarkko Sakkinen 740954650efSJarkko Sakkinen /** 7419deb0eb7SJason Gunthorpe * tpm_pcr_read - read a pcr value 7429deb0eb7SJason Gunthorpe * @chip_num: tpm idx # or ANY 7439deb0eb7SJason Gunthorpe * @pcr_idx: pcr idx to retrieve 7449deb0eb7SJason Gunthorpe * @res_buf: TPM_PCR value 7459deb0eb7SJason Gunthorpe * size of res_buf is 20 bytes (or NULL if you don't care) 7469deb0eb7SJason Gunthorpe * 7479deb0eb7SJason Gunthorpe * The TPM driver should be built-in, but for whatever reason it 7489deb0eb7SJason Gunthorpe * isn't, protect against the chip disappearing, by incrementing 7499deb0eb7SJason Gunthorpe * the module usage count. 7509deb0eb7SJason Gunthorpe */ 7519deb0eb7SJason Gunthorpe int tpm_pcr_read(u32 chip_num, int pcr_idx, u8 *res_buf) 7529deb0eb7SJason Gunthorpe { 7539deb0eb7SJason Gunthorpe struct tpm_chip *chip; 7549deb0eb7SJason Gunthorpe int rc; 7559deb0eb7SJason Gunthorpe 7569deb0eb7SJason Gunthorpe chip = tpm_chip_find_get(chip_num); 7579deb0eb7SJason Gunthorpe if (chip == NULL) 7589deb0eb7SJason Gunthorpe return -ENODEV; 7597a1d7e6dSJarkko Sakkinen if (chip->flags & TPM_CHIP_FLAG_TPM2) 7607a1d7e6dSJarkko Sakkinen rc = tpm2_pcr_read(chip, pcr_idx, res_buf); 7617a1d7e6dSJarkko Sakkinen else 762000a07b0SJason Gunthorpe rc = tpm_pcr_read_dev(chip, pcr_idx, res_buf); 7634e26195fSJason Gunthorpe tpm_put_ops(chip); 7649deb0eb7SJason Gunthorpe return rc; 7659deb0eb7SJason Gunthorpe } 7669deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_pcr_read); 7679deb0eb7SJason Gunthorpe 768ca6d4580SWinkler, Tomas #define TPM_ORD_PCR_EXTEND cpu_to_be32(20) 769ca6d4580SWinkler, Tomas #define EXTEND_PCR_RESULT_SIZE 34 770*51b0be64SStefan Berger #define EXTEND_PCR_RESULT_BODY_SIZE 20 771ca6d4580SWinkler, Tomas static const struct tpm_input_header pcrextend_header = { 772ca6d4580SWinkler, Tomas .tag = TPM_TAG_RQU_COMMAND, 773ca6d4580SWinkler, Tomas .length = cpu_to_be32(34), 774ca6d4580SWinkler, Tomas .ordinal = TPM_ORD_PCR_EXTEND 775ca6d4580SWinkler, Tomas }; 776ca6d4580SWinkler, Tomas 7779deb0eb7SJason Gunthorpe /** 7789deb0eb7SJason Gunthorpe * tpm_pcr_extend - extend pcr value with hash 7799deb0eb7SJason Gunthorpe * @chip_num: tpm idx # or AN& 7809deb0eb7SJason Gunthorpe * @pcr_idx: pcr idx to extend 7819deb0eb7SJason Gunthorpe * @hash: hash value used to extend pcr value 7829deb0eb7SJason Gunthorpe * 7839deb0eb7SJason Gunthorpe * The TPM driver should be built-in, but for whatever reason it 7849deb0eb7SJason Gunthorpe * isn't, protect against the chip disappearing, by incrementing 7859deb0eb7SJason Gunthorpe * the module usage count. 7869deb0eb7SJason Gunthorpe */ 7879deb0eb7SJason Gunthorpe int tpm_pcr_extend(u32 chip_num, int pcr_idx, const u8 *hash) 7889deb0eb7SJason Gunthorpe { 7899deb0eb7SJason Gunthorpe struct tpm_cmd_t cmd; 7909deb0eb7SJason Gunthorpe int rc; 7919deb0eb7SJason Gunthorpe struct tpm_chip *chip; 792c1f92b4bSNayna Jain struct tpm2_digest digest_list[ARRAY_SIZE(chip->active_banks)]; 793c1f92b4bSNayna Jain u32 count = 0; 794c1f92b4bSNayna Jain int i; 7959deb0eb7SJason Gunthorpe 7969deb0eb7SJason Gunthorpe chip = tpm_chip_find_get(chip_num); 7979deb0eb7SJason Gunthorpe if (chip == NULL) 7989deb0eb7SJason Gunthorpe return -ENODEV; 7999deb0eb7SJason Gunthorpe 8007a1d7e6dSJarkko Sakkinen if (chip->flags & TPM_CHIP_FLAG_TPM2) { 801c1f92b4bSNayna Jain memset(digest_list, 0, sizeof(digest_list)); 802c1f92b4bSNayna Jain 80370ea1636SDan Carpenter for (i = 0; i < ARRAY_SIZE(chip->active_banks) && 80470ea1636SDan Carpenter chip->active_banks[i] != TPM2_ALG_ERROR; i++) { 805c1f92b4bSNayna Jain digest_list[i].alg_id = chip->active_banks[i]; 806c1f92b4bSNayna Jain memcpy(digest_list[i].digest, hash, TPM_DIGEST_SIZE); 807c1f92b4bSNayna Jain count++; 808c1f92b4bSNayna Jain } 809c1f92b4bSNayna Jain 810c1f92b4bSNayna Jain rc = tpm2_pcr_extend(chip, pcr_idx, count, digest_list); 8114e26195fSJason Gunthorpe tpm_put_ops(chip); 8127a1d7e6dSJarkko Sakkinen return rc; 8137a1d7e6dSJarkko Sakkinen } 8147a1d7e6dSJarkko Sakkinen 8159deb0eb7SJason Gunthorpe cmd.header.in = pcrextend_header; 8169deb0eb7SJason Gunthorpe cmd.params.pcrextend_in.pcr_idx = cpu_to_be32(pcr_idx); 8179deb0eb7SJason Gunthorpe memcpy(cmd.params.pcrextend_in.hash, hash, TPM_DIGEST_SIZE); 818c659af78SStefan Berger rc = tpm_transmit_cmd(chip, &cmd, EXTEND_PCR_RESULT_SIZE, 819c659af78SStefan Berger EXTEND_PCR_RESULT_BODY_SIZE, 0, 8209deb0eb7SJason Gunthorpe "attempting extend a PCR value"); 8219deb0eb7SJason Gunthorpe 8224e26195fSJason Gunthorpe tpm_put_ops(chip); 8239deb0eb7SJason Gunthorpe return rc; 8249deb0eb7SJason Gunthorpe } 8259deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_pcr_extend); 8269deb0eb7SJason Gunthorpe 8279deb0eb7SJason Gunthorpe /** 8289deb0eb7SJason Gunthorpe * tpm_do_selftest - have the TPM continue its selftest and wait until it 8299deb0eb7SJason Gunthorpe * can receive further commands 8309deb0eb7SJason Gunthorpe * @chip: TPM chip to use 8319deb0eb7SJason Gunthorpe * 8329deb0eb7SJason Gunthorpe * Returns 0 on success, < 0 in case of fatal error or a value > 0 representing 8339deb0eb7SJason Gunthorpe * a TPM error code. 8349deb0eb7SJason Gunthorpe */ 8359deb0eb7SJason Gunthorpe int tpm_do_selftest(struct tpm_chip *chip) 8369deb0eb7SJason Gunthorpe { 8379deb0eb7SJason Gunthorpe int rc; 8389deb0eb7SJason Gunthorpe unsigned int loops; 8399deb0eb7SJason Gunthorpe unsigned int delay_msec = 100; 8409deb0eb7SJason Gunthorpe unsigned long duration; 8410c541332SJarkko Sakkinen u8 dummy[TPM_DIGEST_SIZE]; 8429deb0eb7SJason Gunthorpe 8439deb0eb7SJason Gunthorpe duration = tpm_calc_ordinal_duration(chip, TPM_ORD_CONTINUE_SELFTEST); 8449deb0eb7SJason Gunthorpe 8459deb0eb7SJason Gunthorpe loops = jiffies_to_msecs(duration) / delay_msec; 8469deb0eb7SJason Gunthorpe 8479deb0eb7SJason Gunthorpe rc = tpm_continue_selftest(chip); 8489deb0eb7SJason Gunthorpe /* This may fail if there was no TPM driver during a suspend/resume 8499deb0eb7SJason Gunthorpe * cycle; some may return 10 (BAD_ORDINAL), others 28 (FAILEDSELFTEST) 8509deb0eb7SJason Gunthorpe */ 8519deb0eb7SJason Gunthorpe if (rc) 8529deb0eb7SJason Gunthorpe return rc; 8539deb0eb7SJason Gunthorpe 8549deb0eb7SJason Gunthorpe do { 8559deb0eb7SJason Gunthorpe /* Attempt to read a PCR value */ 8560c541332SJarkko Sakkinen rc = tpm_pcr_read_dev(chip, 0, dummy); 8570c541332SJarkko Sakkinen 8589deb0eb7SJason Gunthorpe /* Some buggy TPMs will not respond to tpm_tis_ready() for 8599deb0eb7SJason Gunthorpe * around 300ms while the self test is ongoing, keep trying 8609deb0eb7SJason Gunthorpe * until the self test duration expires. */ 8619deb0eb7SJason Gunthorpe if (rc == -ETIME) { 8628cfffc9dSJason Gunthorpe dev_info( 8638cfffc9dSJason Gunthorpe &chip->dev, HW_ERR 8648cfffc9dSJason Gunthorpe "TPM command timed out during continue self test"); 8659deb0eb7SJason Gunthorpe msleep(delay_msec); 8669deb0eb7SJason Gunthorpe continue; 8679deb0eb7SJason Gunthorpe } 8689deb0eb7SJason Gunthorpe 8699deb0eb7SJason Gunthorpe if (rc == TPM_ERR_DISABLED || rc == TPM_ERR_DEACTIVATED) { 8708cfffc9dSJason Gunthorpe dev_info(&chip->dev, 8719deb0eb7SJason Gunthorpe "TPM is disabled/deactivated (0x%X)\n", rc); 8729deb0eb7SJason Gunthorpe /* TPM is disabled and/or deactivated; driver can 8739deb0eb7SJason Gunthorpe * proceed and TPM does handle commands for 8749deb0eb7SJason Gunthorpe * suspend/resume correctly 8759deb0eb7SJason Gunthorpe */ 8769deb0eb7SJason Gunthorpe return 0; 8779deb0eb7SJason Gunthorpe } 8789deb0eb7SJason Gunthorpe if (rc != TPM_WARN_DOING_SELFTEST) 8799deb0eb7SJason Gunthorpe return rc; 8809deb0eb7SJason Gunthorpe msleep(delay_msec); 8819deb0eb7SJason Gunthorpe } while (--loops > 0); 8829deb0eb7SJason Gunthorpe 8839deb0eb7SJason Gunthorpe return rc; 8849deb0eb7SJason Gunthorpe } 8859deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_do_selftest); 8869deb0eb7SJason Gunthorpe 887cae8b441SJason Gunthorpe /** 888cae8b441SJason Gunthorpe * tpm1_auto_startup - Perform the standard automatic TPM initialization 889cae8b441SJason Gunthorpe * sequence 890cae8b441SJason Gunthorpe * @chip: TPM chip to use 891cae8b441SJason Gunthorpe * 892cae8b441SJason Gunthorpe * Returns 0 on success, < 0 in case of fatal error. 893cae8b441SJason Gunthorpe */ 894cae8b441SJason Gunthorpe int tpm1_auto_startup(struct tpm_chip *chip) 895cae8b441SJason Gunthorpe { 896cae8b441SJason Gunthorpe int rc; 897cae8b441SJason Gunthorpe 898cae8b441SJason Gunthorpe rc = tpm_get_timeouts(chip); 899cae8b441SJason Gunthorpe if (rc) 900cae8b441SJason Gunthorpe goto out; 901cae8b441SJason Gunthorpe rc = tpm_do_selftest(chip); 902cae8b441SJason Gunthorpe if (rc) { 903cae8b441SJason Gunthorpe dev_err(&chip->dev, "TPM self test failed\n"); 904cae8b441SJason Gunthorpe goto out; 905cae8b441SJason Gunthorpe } 906cae8b441SJason Gunthorpe 907cae8b441SJason Gunthorpe return rc; 908cae8b441SJason Gunthorpe out: 909cae8b441SJason Gunthorpe if (rc > 0) 910cae8b441SJason Gunthorpe rc = -ENODEV; 911cae8b441SJason Gunthorpe return rc; 912cae8b441SJason Gunthorpe } 913cae8b441SJason Gunthorpe 9149deb0eb7SJason Gunthorpe int tpm_send(u32 chip_num, void *cmd, size_t buflen) 9159deb0eb7SJason Gunthorpe { 9169deb0eb7SJason Gunthorpe struct tpm_chip *chip; 9179deb0eb7SJason Gunthorpe int rc; 9189deb0eb7SJason Gunthorpe 9199deb0eb7SJason Gunthorpe chip = tpm_chip_find_get(chip_num); 9209deb0eb7SJason Gunthorpe if (chip == NULL) 9219deb0eb7SJason Gunthorpe return -ENODEV; 9229deb0eb7SJason Gunthorpe 923c659af78SStefan Berger rc = tpm_transmit_cmd(chip, cmd, buflen, 0, 0, "attempting tpm_cmd"); 9249deb0eb7SJason Gunthorpe 9254e26195fSJason Gunthorpe tpm_put_ops(chip); 9269deb0eb7SJason Gunthorpe return rc; 9279deb0eb7SJason Gunthorpe } 9289deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_send); 9299deb0eb7SJason Gunthorpe 9309deb0eb7SJason Gunthorpe static bool wait_for_tpm_stat_cond(struct tpm_chip *chip, u8 mask, 9319deb0eb7SJason Gunthorpe bool check_cancel, bool *canceled) 9329deb0eb7SJason Gunthorpe { 9335f82e9f0SJason Gunthorpe u8 status = chip->ops->status(chip); 9349deb0eb7SJason Gunthorpe 9359deb0eb7SJason Gunthorpe *canceled = false; 9369deb0eb7SJason Gunthorpe if ((status & mask) == mask) 9379deb0eb7SJason Gunthorpe return true; 9385f82e9f0SJason Gunthorpe if (check_cancel && chip->ops->req_canceled(chip, status)) { 9399deb0eb7SJason Gunthorpe *canceled = true; 9409deb0eb7SJason Gunthorpe return true; 9419deb0eb7SJason Gunthorpe } 9429deb0eb7SJason Gunthorpe return false; 9439deb0eb7SJason Gunthorpe } 9449deb0eb7SJason Gunthorpe 9459deb0eb7SJason Gunthorpe int wait_for_tpm_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout, 9469deb0eb7SJason Gunthorpe wait_queue_head_t *queue, bool check_cancel) 9479deb0eb7SJason Gunthorpe { 9489deb0eb7SJason Gunthorpe unsigned long stop; 9499deb0eb7SJason Gunthorpe long rc; 9509deb0eb7SJason Gunthorpe u8 status; 9519deb0eb7SJason Gunthorpe bool canceled = false; 9529deb0eb7SJason Gunthorpe 9539deb0eb7SJason Gunthorpe /* check current status */ 9545f82e9f0SJason Gunthorpe status = chip->ops->status(chip); 9559deb0eb7SJason Gunthorpe if ((status & mask) == mask) 9569deb0eb7SJason Gunthorpe return 0; 9579deb0eb7SJason Gunthorpe 9589deb0eb7SJason Gunthorpe stop = jiffies + timeout; 9599deb0eb7SJason Gunthorpe 960570a3609SChristophe Ricard if (chip->flags & TPM_CHIP_FLAG_IRQ) { 9619deb0eb7SJason Gunthorpe again: 9629deb0eb7SJason Gunthorpe timeout = stop - jiffies; 9639deb0eb7SJason Gunthorpe if ((long)timeout <= 0) 9649deb0eb7SJason Gunthorpe return -ETIME; 9659deb0eb7SJason Gunthorpe rc = wait_event_interruptible_timeout(*queue, 9669deb0eb7SJason Gunthorpe wait_for_tpm_stat_cond(chip, mask, check_cancel, 9679deb0eb7SJason Gunthorpe &canceled), 9689deb0eb7SJason Gunthorpe timeout); 9699deb0eb7SJason Gunthorpe if (rc > 0) { 9709deb0eb7SJason Gunthorpe if (canceled) 9719deb0eb7SJason Gunthorpe return -ECANCELED; 9729deb0eb7SJason Gunthorpe return 0; 9739deb0eb7SJason Gunthorpe } 9749deb0eb7SJason Gunthorpe if (rc == -ERESTARTSYS && freezing(current)) { 9759deb0eb7SJason Gunthorpe clear_thread_flag(TIF_SIGPENDING); 9769deb0eb7SJason Gunthorpe goto again; 9779deb0eb7SJason Gunthorpe } 9789deb0eb7SJason Gunthorpe } else { 9799deb0eb7SJason Gunthorpe do { 9809deb0eb7SJason Gunthorpe msleep(TPM_TIMEOUT); 9815f82e9f0SJason Gunthorpe status = chip->ops->status(chip); 9829deb0eb7SJason Gunthorpe if ((status & mask) == mask) 9839deb0eb7SJason Gunthorpe return 0; 9849deb0eb7SJason Gunthorpe } while (time_before(jiffies, stop)); 9859deb0eb7SJason Gunthorpe } 9869deb0eb7SJason Gunthorpe return -ETIME; 9879deb0eb7SJason Gunthorpe } 9889deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(wait_for_tpm_stat); 9899deb0eb7SJason Gunthorpe 9909deb0eb7SJason Gunthorpe #define TPM_ORD_SAVESTATE cpu_to_be32(152) 9919deb0eb7SJason Gunthorpe #define SAVESTATE_RESULT_SIZE 10 9929deb0eb7SJason Gunthorpe 9930014777fSJulia Lawall static const struct tpm_input_header savestate_header = { 9949deb0eb7SJason Gunthorpe .tag = TPM_TAG_RQU_COMMAND, 9959deb0eb7SJason Gunthorpe .length = cpu_to_be32(10), 9969deb0eb7SJason Gunthorpe .ordinal = TPM_ORD_SAVESTATE 9979deb0eb7SJason Gunthorpe }; 9989deb0eb7SJason Gunthorpe 9999deb0eb7SJason Gunthorpe /* 10009deb0eb7SJason Gunthorpe * We are about to suspend. Save the TPM state 10019deb0eb7SJason Gunthorpe * so that it can be restored. 10029deb0eb7SJason Gunthorpe */ 10039deb0eb7SJason Gunthorpe int tpm_pm_suspend(struct device *dev) 10049deb0eb7SJason Gunthorpe { 1005ec03c50bSStefan Berger struct tpm_chip *chip = dev_get_drvdata(dev); 10069deb0eb7SJason Gunthorpe struct tpm_cmd_t cmd; 10079deb0eb7SJason Gunthorpe int rc, try; 10089deb0eb7SJason Gunthorpe 10099deb0eb7SJason Gunthorpe u8 dummy_hash[TPM_DIGEST_SIZE] = { 0 }; 10109deb0eb7SJason Gunthorpe 10119deb0eb7SJason Gunthorpe if (chip == NULL) 10129deb0eb7SJason Gunthorpe return -ENODEV; 10139deb0eb7SJason Gunthorpe 101474d6b3ceSJarkko Sakkinen if (chip->flags & TPM_CHIP_FLAG_TPM2) { 101574d6b3ceSJarkko Sakkinen tpm2_shutdown(chip, TPM2_SU_STATE); 101674d6b3ceSJarkko Sakkinen return 0; 101774d6b3ceSJarkko Sakkinen } 101830fc8d13SJarkko Sakkinen 10199deb0eb7SJason Gunthorpe /* for buggy tpm, flush pcrs with extend to selected dummy */ 10209deb0eb7SJason Gunthorpe if (tpm_suspend_pcr) { 10219deb0eb7SJason Gunthorpe cmd.header.in = pcrextend_header; 10229deb0eb7SJason Gunthorpe cmd.params.pcrextend_in.pcr_idx = cpu_to_be32(tpm_suspend_pcr); 10239deb0eb7SJason Gunthorpe memcpy(cmd.params.pcrextend_in.hash, dummy_hash, 10249deb0eb7SJason Gunthorpe TPM_DIGEST_SIZE); 1025c659af78SStefan Berger rc = tpm_transmit_cmd(chip, &cmd, EXTEND_PCR_RESULT_SIZE, 1026c659af78SStefan Berger EXTEND_PCR_RESULT_BODY_SIZE, 0, 10279deb0eb7SJason Gunthorpe "extending dummy pcr before suspend"); 10289deb0eb7SJason Gunthorpe } 10299deb0eb7SJason Gunthorpe 10309deb0eb7SJason Gunthorpe /* now do the actual savestate */ 10319deb0eb7SJason Gunthorpe for (try = 0; try < TPM_RETRY; try++) { 10329deb0eb7SJason Gunthorpe cmd.header.in = savestate_header; 1033d4816edfSJarkko Sakkinen rc = tpm_transmit_cmd(chip, &cmd, SAVESTATE_RESULT_SIZE, 0, 1034c659af78SStefan Berger 0, NULL); 10359deb0eb7SJason Gunthorpe 10369deb0eb7SJason Gunthorpe /* 10379deb0eb7SJason Gunthorpe * If the TPM indicates that it is too busy to respond to 10389deb0eb7SJason Gunthorpe * this command then retry before giving up. It can take 10399deb0eb7SJason Gunthorpe * several seconds for this TPM to be ready. 10409deb0eb7SJason Gunthorpe * 10419deb0eb7SJason Gunthorpe * This can happen if the TPM has already been sent the 10429deb0eb7SJason Gunthorpe * SaveState command before the driver has loaded. TCG 1.2 10439deb0eb7SJason Gunthorpe * specification states that any communication after SaveState 10449deb0eb7SJason Gunthorpe * may cause the TPM to invalidate previously saved state. 10459deb0eb7SJason Gunthorpe */ 10469deb0eb7SJason Gunthorpe if (rc != TPM_WARN_RETRY) 10479deb0eb7SJason Gunthorpe break; 10489deb0eb7SJason Gunthorpe msleep(TPM_TIMEOUT_RETRY); 10499deb0eb7SJason Gunthorpe } 10509deb0eb7SJason Gunthorpe 10519deb0eb7SJason Gunthorpe if (rc) 10528cfffc9dSJason Gunthorpe dev_err(&chip->dev, 10539deb0eb7SJason Gunthorpe "Error (%d) sending savestate before suspend\n", rc); 10549deb0eb7SJason Gunthorpe else if (try > 0) 10558cfffc9dSJason Gunthorpe dev_warn(&chip->dev, "TPM savestate took %dms\n", 10569deb0eb7SJason Gunthorpe try * TPM_TIMEOUT_RETRY); 10579deb0eb7SJason Gunthorpe 10589deb0eb7SJason Gunthorpe return rc; 10599deb0eb7SJason Gunthorpe } 10609deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_pm_suspend); 10619deb0eb7SJason Gunthorpe 10629deb0eb7SJason Gunthorpe /* 10639deb0eb7SJason Gunthorpe * Resume from a power safe. The BIOS already restored 10649deb0eb7SJason Gunthorpe * the TPM state. 10659deb0eb7SJason Gunthorpe */ 10669deb0eb7SJason Gunthorpe int tpm_pm_resume(struct device *dev) 10679deb0eb7SJason Gunthorpe { 1068ec03c50bSStefan Berger struct tpm_chip *chip = dev_get_drvdata(dev); 10699deb0eb7SJason Gunthorpe 10709deb0eb7SJason Gunthorpe if (chip == NULL) 10719deb0eb7SJason Gunthorpe return -ENODEV; 10729deb0eb7SJason Gunthorpe 10739deb0eb7SJason Gunthorpe return 0; 10749deb0eb7SJason Gunthorpe } 10759deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_pm_resume); 10769deb0eb7SJason Gunthorpe 10779deb0eb7SJason Gunthorpe #define TPM_GETRANDOM_RESULT_SIZE 18 10780014777fSJulia Lawall static const struct tpm_input_header tpm_getrandom_header = { 10799deb0eb7SJason Gunthorpe .tag = TPM_TAG_RQU_COMMAND, 10809deb0eb7SJason Gunthorpe .length = cpu_to_be32(14), 10819deb0eb7SJason Gunthorpe .ordinal = TPM_ORD_GET_RANDOM 10829deb0eb7SJason Gunthorpe }; 10839deb0eb7SJason Gunthorpe 10849deb0eb7SJason Gunthorpe /** 10859deb0eb7SJason Gunthorpe * tpm_get_random() - Get random bytes from the tpm's RNG 10869deb0eb7SJason Gunthorpe * @chip_num: A specific chip number for the request or TPM_ANY_NUM 10879deb0eb7SJason Gunthorpe * @out: destination buffer for the random bytes 10889deb0eb7SJason Gunthorpe * @max: the max number of bytes to write to @out 10899deb0eb7SJason Gunthorpe * 10909deb0eb7SJason Gunthorpe * Returns < 0 on error and the number of bytes read on success 10919deb0eb7SJason Gunthorpe */ 10929deb0eb7SJason Gunthorpe int tpm_get_random(u32 chip_num, u8 *out, size_t max) 10939deb0eb7SJason Gunthorpe { 10949deb0eb7SJason Gunthorpe struct tpm_chip *chip; 10959deb0eb7SJason Gunthorpe struct tpm_cmd_t tpm_cmd; 1096c659af78SStefan Berger u32 recd, num_bytes = min_t(u32, max, TPM_MAX_RNG_DATA), rlength; 10979deb0eb7SJason Gunthorpe int err, total = 0, retries = 5; 10989deb0eb7SJason Gunthorpe u8 *dest = out; 10999deb0eb7SJason Gunthorpe 11003e14d83eSJarkko Sakkinen if (!out || !num_bytes || max > TPM_MAX_RNG_DATA) 11013e14d83eSJarkko Sakkinen return -EINVAL; 11023e14d83eSJarkko Sakkinen 11039deb0eb7SJason Gunthorpe chip = tpm_chip_find_get(chip_num); 11049deb0eb7SJason Gunthorpe if (chip == NULL) 11059deb0eb7SJason Gunthorpe return -ENODEV; 11069deb0eb7SJason Gunthorpe 11077a1d7e6dSJarkko Sakkinen if (chip->flags & TPM_CHIP_FLAG_TPM2) { 11087a1d7e6dSJarkko Sakkinen err = tpm2_get_random(chip, out, max); 11094e26195fSJason Gunthorpe tpm_put_ops(chip); 11107a1d7e6dSJarkko Sakkinen return err; 11117a1d7e6dSJarkko Sakkinen } 11127a1d7e6dSJarkko Sakkinen 11139deb0eb7SJason Gunthorpe do { 11149deb0eb7SJason Gunthorpe tpm_cmd.header.in = tpm_getrandom_header; 11159deb0eb7SJason Gunthorpe tpm_cmd.params.getrandom_in.num_bytes = cpu_to_be32(num_bytes); 11169deb0eb7SJason Gunthorpe 111787155b73SJarkko Sakkinen err = tpm_transmit_cmd(chip, &tpm_cmd, 11189deb0eb7SJason Gunthorpe TPM_GETRANDOM_RESULT_SIZE + num_bytes, 1119c659af78SStefan Berger offsetof(struct tpm_getrandom_out, 1120c659af78SStefan Berger rng_data), 1121d4816edfSJarkko Sakkinen 0, "attempting get random"); 11229deb0eb7SJason Gunthorpe if (err) 11239deb0eb7SJason Gunthorpe break; 11249deb0eb7SJason Gunthorpe 11259deb0eb7SJason Gunthorpe recd = be32_to_cpu(tpm_cmd.params.getrandom_out.rng_data_len); 1126c659af78SStefan Berger 1127c659af78SStefan Berger rlength = be32_to_cpu(tpm_cmd.header.out.length); 1128c659af78SStefan Berger if (rlength < offsetof(struct tpm_getrandom_out, rng_data) + 1129c659af78SStefan Berger recd) { 1130c659af78SStefan Berger total = -EFAULT; 1131c659af78SStefan Berger break; 1132c659af78SStefan Berger } 11339deb0eb7SJason Gunthorpe memcpy(dest, tpm_cmd.params.getrandom_out.rng_data, recd); 11349deb0eb7SJason Gunthorpe 11359deb0eb7SJason Gunthorpe dest += recd; 11369deb0eb7SJason Gunthorpe total += recd; 11379deb0eb7SJason Gunthorpe num_bytes -= recd; 11389deb0eb7SJason Gunthorpe } while (retries-- && total < max); 11399deb0eb7SJason Gunthorpe 11404e26195fSJason Gunthorpe tpm_put_ops(chip); 11419deb0eb7SJason Gunthorpe return total ? total : -EIO; 11429deb0eb7SJason Gunthorpe } 11439deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_get_random); 11449deb0eb7SJason Gunthorpe 1145954650efSJarkko Sakkinen /** 1146954650efSJarkko Sakkinen * tpm_seal_trusted() - seal a trusted key 1147954650efSJarkko Sakkinen * @chip_num: A specific chip number for the request or TPM_ANY_NUM 1148954650efSJarkko Sakkinen * @options: authentication values and other options 1149954650efSJarkko Sakkinen * @payload: the key data in clear and encrypted form 1150954650efSJarkko Sakkinen * 1151954650efSJarkko Sakkinen * Returns < 0 on error and 0 on success. At the moment, only TPM 2.0 chips 1152954650efSJarkko Sakkinen * are supported. 1153954650efSJarkko Sakkinen */ 1154954650efSJarkko Sakkinen int tpm_seal_trusted(u32 chip_num, struct trusted_key_payload *payload, 1155954650efSJarkko Sakkinen struct trusted_key_options *options) 1156954650efSJarkko Sakkinen { 1157954650efSJarkko Sakkinen struct tpm_chip *chip; 1158954650efSJarkko Sakkinen int rc; 1159954650efSJarkko Sakkinen 1160954650efSJarkko Sakkinen chip = tpm_chip_find_get(chip_num); 1161954650efSJarkko Sakkinen if (chip == NULL || !(chip->flags & TPM_CHIP_FLAG_TPM2)) 1162954650efSJarkko Sakkinen return -ENODEV; 1163954650efSJarkko Sakkinen 1164954650efSJarkko Sakkinen rc = tpm2_seal_trusted(chip, payload, options); 1165954650efSJarkko Sakkinen 11664e26195fSJason Gunthorpe tpm_put_ops(chip); 1167954650efSJarkko Sakkinen return rc; 1168954650efSJarkko Sakkinen } 1169954650efSJarkko Sakkinen EXPORT_SYMBOL_GPL(tpm_seal_trusted); 1170954650efSJarkko Sakkinen 1171954650efSJarkko Sakkinen /** 1172954650efSJarkko Sakkinen * tpm_unseal_trusted() - unseal a trusted key 1173954650efSJarkko Sakkinen * @chip_num: A specific chip number for the request or TPM_ANY_NUM 1174954650efSJarkko Sakkinen * @options: authentication values and other options 1175954650efSJarkko Sakkinen * @payload: the key data in clear and encrypted form 1176954650efSJarkko Sakkinen * 1177954650efSJarkko Sakkinen * Returns < 0 on error and 0 on success. At the moment, only TPM 2.0 chips 1178954650efSJarkko Sakkinen * are supported. 1179954650efSJarkko Sakkinen */ 1180954650efSJarkko Sakkinen int tpm_unseal_trusted(u32 chip_num, struct trusted_key_payload *payload, 1181954650efSJarkko Sakkinen struct trusted_key_options *options) 1182954650efSJarkko Sakkinen { 1183954650efSJarkko Sakkinen struct tpm_chip *chip; 1184954650efSJarkko Sakkinen int rc; 1185954650efSJarkko Sakkinen 1186954650efSJarkko Sakkinen chip = tpm_chip_find_get(chip_num); 1187954650efSJarkko Sakkinen if (chip == NULL || !(chip->flags & TPM_CHIP_FLAG_TPM2)) 1188954650efSJarkko Sakkinen return -ENODEV; 1189954650efSJarkko Sakkinen 1190954650efSJarkko Sakkinen rc = tpm2_unseal_trusted(chip, payload, options); 1191954650efSJarkko Sakkinen 11924e26195fSJason Gunthorpe tpm_put_ops(chip); 11934e26195fSJason Gunthorpe 1194954650efSJarkko Sakkinen return rc; 1195954650efSJarkko Sakkinen } 1196954650efSJarkko Sakkinen EXPORT_SYMBOL_GPL(tpm_unseal_trusted); 1197954650efSJarkko Sakkinen 1198313d21eeSJarkko Sakkinen static int __init tpm_init(void) 1199313d21eeSJarkko Sakkinen { 1200313d21eeSJarkko Sakkinen int rc; 1201313d21eeSJarkko Sakkinen 1202313d21eeSJarkko Sakkinen tpm_class = class_create(THIS_MODULE, "tpm"); 1203313d21eeSJarkko Sakkinen if (IS_ERR(tpm_class)) { 1204313d21eeSJarkko Sakkinen pr_err("couldn't create tpm class\n"); 1205313d21eeSJarkko Sakkinen return PTR_ERR(tpm_class); 1206313d21eeSJarkko Sakkinen } 1207313d21eeSJarkko Sakkinen 1208313d21eeSJarkko Sakkinen rc = alloc_chrdev_region(&tpm_devt, 0, TPM_NUM_DEVICES, "tpm"); 1209313d21eeSJarkko Sakkinen if (rc < 0) { 1210313d21eeSJarkko Sakkinen pr_err("tpm: failed to allocate char dev region\n"); 1211313d21eeSJarkko Sakkinen class_destroy(tpm_class); 1212313d21eeSJarkko Sakkinen return rc; 1213313d21eeSJarkko Sakkinen } 1214313d21eeSJarkko Sakkinen 1215313d21eeSJarkko Sakkinen return 0; 1216313d21eeSJarkko Sakkinen } 1217313d21eeSJarkko Sakkinen 1218313d21eeSJarkko Sakkinen static void __exit tpm_exit(void) 1219313d21eeSJarkko Sakkinen { 122015516788SStefan Berger idr_destroy(&dev_nums_idr); 1221313d21eeSJarkko Sakkinen class_destroy(tpm_class); 1222313d21eeSJarkko Sakkinen unregister_chrdev_region(tpm_devt, TPM_NUM_DEVICES); 1223313d21eeSJarkko Sakkinen } 1224313d21eeSJarkko Sakkinen 1225313d21eeSJarkko Sakkinen subsys_initcall(tpm_init); 1226313d21eeSJarkko Sakkinen module_exit(tpm_exit); 1227313d21eeSJarkko Sakkinen 12289deb0eb7SJason Gunthorpe MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)"); 12299deb0eb7SJason Gunthorpe MODULE_DESCRIPTION("TPM Driver"); 12309deb0eb7SJason Gunthorpe MODULE_VERSION("2.0"); 12319deb0eb7SJason Gunthorpe MODULE_LICENSE("GPL"); 1232