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, 509deb0eb7SJason Gunthorpe "PCR to use for dummy writes to faciltate 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 331*f865c196SWinkler, Tomas /** 332*f865c196SWinkler, Tomas * tmp_transmit - Internal kernel interface to transmit TPM commands. 333*f865c196SWinkler, Tomas * 334*f865c196SWinkler, Tomas * @chip: TPM chip to use 335*f865c196SWinkler, Tomas * @buf: TPM command buffer 336*f865c196SWinkler, Tomas * @bufsiz: length of the TPM command buffer 337*f865c196SWinkler, Tomas * @flags: tpm transmit flags - bitmap 338*f865c196SWinkler, Tomas * 339*f865c196SWinkler, Tomas * Return: 340*f865c196SWinkler, Tomas * 0 when the operation is successful. 341*f865c196SWinkler, 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 421*f865c196SWinkler, Tomas /** 422*f865c196SWinkler, Tomas * tmp_transmit_cmd - send a tpm command to the device 423*f865c196SWinkler, Tomas * The function extracts tpm out header return code 424*f865c196SWinkler, Tomas * 425*f865c196SWinkler, Tomas * @chip: TPM chip to use 426*f865c196SWinkler, Tomas * @cmd: TPM command buffer 427*f865c196SWinkler, Tomas * @len: length of the TPM command 428*f865c196SWinkler, Tomas * @flags: tpm transmit flags - bitmap 429*f865c196SWinkler, Tomas * @desc: command description used in the error message 430*f865c196SWinkler, Tomas * 431*f865c196SWinkler, Tomas * Return: 432*f865c196SWinkler, Tomas * 0 when the operation is successful. 433*f865c196SWinkler, Tomas * A negative number for system errors (errno). 434*f865c196SWinkler, Tomas * A positive number for a TPM error. 435*f865c196SWinkler, Tomas */ 436d4816edfSJarkko Sakkinen ssize_t tpm_transmit_cmd(struct tpm_chip *chip, const void *cmd, 437d4816edfSJarkko Sakkinen int len, unsigned int flags, const char *desc) 4389deb0eb7SJason Gunthorpe { 439d4816edfSJarkko Sakkinen const struct tpm_output_header *header; 4409deb0eb7SJason Gunthorpe int err; 4419deb0eb7SJason Gunthorpe 442d4816edfSJarkko Sakkinen len = tpm_transmit(chip, (const u8 *)cmd, len, flags); 4439deb0eb7SJason Gunthorpe if (len < 0) 4449deb0eb7SJason Gunthorpe return len; 4459deb0eb7SJason Gunthorpe else if (len < TPM_HEADER_SIZE) 4469deb0eb7SJason Gunthorpe return -EFAULT; 4479deb0eb7SJason Gunthorpe 44887155b73SJarkko Sakkinen header = cmd; 44987155b73SJarkko Sakkinen 45087155b73SJarkko Sakkinen err = be32_to_cpu(header->return_code); 4519deb0eb7SJason Gunthorpe if (err != 0 && desc) 4528cfffc9dSJason Gunthorpe dev_err(&chip->dev, "A TPM error (%d) occurred %s\n", err, 45371ed848fSJarkko Sakkinen desc); 4549deb0eb7SJason Gunthorpe 4559deb0eb7SJason Gunthorpe return err; 4569deb0eb7SJason Gunthorpe } 4579deb0eb7SJason Gunthorpe 458*f865c196SWinkler, Tomas #define TPM_DIGEST_SIZE 20 459*f865c196SWinkler, Tomas #define TPM_RET_CODE_IDX 6 4609deb0eb7SJason Gunthorpe #define TPM_INTERNAL_RESULT_SIZE 200 4619deb0eb7SJason Gunthorpe #define TPM_ORD_GET_CAP cpu_to_be32(101) 4629deb0eb7SJason Gunthorpe #define TPM_ORD_GET_RANDOM cpu_to_be32(70) 4639deb0eb7SJason Gunthorpe 4649deb0eb7SJason Gunthorpe static const struct tpm_input_header tpm_getcap_header = { 4659deb0eb7SJason Gunthorpe .tag = TPM_TAG_RQU_COMMAND, 4669deb0eb7SJason Gunthorpe .length = cpu_to_be32(22), 4679deb0eb7SJason Gunthorpe .ordinal = TPM_ORD_GET_CAP 4689deb0eb7SJason Gunthorpe }; 4699deb0eb7SJason Gunthorpe 47084fda152SJarkko Sakkinen ssize_t tpm_getcap(struct tpm_chip *chip, u32 subcap_id, cap_t *cap, 4719deb0eb7SJason Gunthorpe const char *desc) 4729deb0eb7SJason Gunthorpe { 4739deb0eb7SJason Gunthorpe struct tpm_cmd_t tpm_cmd; 4749deb0eb7SJason Gunthorpe int rc; 4759deb0eb7SJason Gunthorpe 4769deb0eb7SJason Gunthorpe tpm_cmd.header.in = tpm_getcap_header; 47784fda152SJarkko Sakkinen if (subcap_id == TPM_CAP_VERSION_1_1 || 47884fda152SJarkko Sakkinen subcap_id == TPM_CAP_VERSION_1_2) { 47984fda152SJarkko Sakkinen tpm_cmd.params.getcap_in.cap = cpu_to_be32(subcap_id); 4809deb0eb7SJason Gunthorpe /*subcap field not necessary */ 4819deb0eb7SJason Gunthorpe tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(0); 4829deb0eb7SJason Gunthorpe tpm_cmd.header.in.length -= cpu_to_be32(sizeof(__be32)); 4839deb0eb7SJason Gunthorpe } else { 4849deb0eb7SJason Gunthorpe if (subcap_id == TPM_CAP_FLAG_PERM || 4859deb0eb7SJason Gunthorpe subcap_id == TPM_CAP_FLAG_VOL) 48684fda152SJarkko Sakkinen tpm_cmd.params.getcap_in.cap = 48784fda152SJarkko Sakkinen cpu_to_be32(TPM_CAP_FLAG); 4889deb0eb7SJason Gunthorpe else 48984fda152SJarkko Sakkinen tpm_cmd.params.getcap_in.cap = 49084fda152SJarkko Sakkinen cpu_to_be32(TPM_CAP_PROP); 4919deb0eb7SJason Gunthorpe tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4); 49284fda152SJarkko Sakkinen tpm_cmd.params.getcap_in.subcap = cpu_to_be32(subcap_id); 4939deb0eb7SJason Gunthorpe } 494d4816edfSJarkko Sakkinen rc = tpm_transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE, 0, 495d4816edfSJarkko Sakkinen desc); 4969deb0eb7SJason Gunthorpe if (!rc) 4979deb0eb7SJason Gunthorpe *cap = tpm_cmd.params.getcap_out.cap; 4989deb0eb7SJason Gunthorpe return rc; 4999deb0eb7SJason Gunthorpe } 500eb5854e7SJarkko Sakkinen EXPORT_SYMBOL_GPL(tpm_getcap); 5019deb0eb7SJason Gunthorpe 5029deb0eb7SJason Gunthorpe #define TPM_ORD_STARTUP cpu_to_be32(153) 5039deb0eb7SJason Gunthorpe #define TPM_ST_CLEAR cpu_to_be16(1) 5049deb0eb7SJason Gunthorpe #define TPM_ST_STATE cpu_to_be16(2) 5059deb0eb7SJason Gunthorpe #define TPM_ST_DEACTIVATED cpu_to_be16(3) 5069deb0eb7SJason Gunthorpe static const struct tpm_input_header tpm_startup_header = { 5079deb0eb7SJason Gunthorpe .tag = TPM_TAG_RQU_COMMAND, 5089deb0eb7SJason Gunthorpe .length = cpu_to_be32(12), 5099deb0eb7SJason Gunthorpe .ordinal = TPM_ORD_STARTUP 5109deb0eb7SJason Gunthorpe }; 5119deb0eb7SJason Gunthorpe 5129deb0eb7SJason Gunthorpe static int tpm_startup(struct tpm_chip *chip, __be16 startup_type) 5139deb0eb7SJason Gunthorpe { 5149deb0eb7SJason Gunthorpe struct tpm_cmd_t start_cmd; 5159deb0eb7SJason Gunthorpe start_cmd.header.in = tpm_startup_header; 5167a1d7e6dSJarkko Sakkinen 5179deb0eb7SJason Gunthorpe start_cmd.params.startup_in.startup_type = startup_type; 518d4816edfSJarkko Sakkinen return tpm_transmit_cmd(chip, &start_cmd, TPM_INTERNAL_RESULT_SIZE, 0, 5199deb0eb7SJason Gunthorpe "attempting to start the TPM"); 5209deb0eb7SJason Gunthorpe } 5219deb0eb7SJason Gunthorpe 5229deb0eb7SJason Gunthorpe int tpm_get_timeouts(struct tpm_chip *chip) 5239deb0eb7SJason Gunthorpe { 524aaa6f7f6SEd Swierk cap_t cap; 5258e54caf4SJason Gunthorpe unsigned long new_timeout[4]; 5268e54caf4SJason Gunthorpe unsigned long old_timeout[4]; 5279deb0eb7SJason Gunthorpe ssize_t rc; 5289deb0eb7SJason Gunthorpe 529d1d253cfSJason Gunthorpe if (chip->flags & TPM_CHIP_FLAG_HAVE_TIMEOUTS) 530d1d253cfSJason Gunthorpe return 0; 531d1d253cfSJason Gunthorpe 53225112048SJason Gunthorpe if (chip->flags & TPM_CHIP_FLAG_TPM2) { 53325112048SJason Gunthorpe /* Fixed timeouts for TPM2 */ 534af782f33SChristophe Ricard chip->timeout_a = msecs_to_jiffies(TPM2_TIMEOUT_A); 535af782f33SChristophe Ricard chip->timeout_b = msecs_to_jiffies(TPM2_TIMEOUT_B); 536af782f33SChristophe Ricard chip->timeout_c = msecs_to_jiffies(TPM2_TIMEOUT_C); 537af782f33SChristophe Ricard chip->timeout_d = msecs_to_jiffies(TPM2_TIMEOUT_D); 538af782f33SChristophe Ricard chip->duration[TPM_SHORT] = 53925112048SJason Gunthorpe msecs_to_jiffies(TPM2_DURATION_SHORT); 540af782f33SChristophe Ricard chip->duration[TPM_MEDIUM] = 54125112048SJason Gunthorpe msecs_to_jiffies(TPM2_DURATION_MEDIUM); 542af782f33SChristophe Ricard chip->duration[TPM_LONG] = 54325112048SJason Gunthorpe msecs_to_jiffies(TPM2_DURATION_LONG); 544d1d253cfSJason Gunthorpe 545d1d253cfSJason Gunthorpe chip->flags |= TPM_CHIP_FLAG_HAVE_TIMEOUTS; 54625112048SJason Gunthorpe return 0; 54725112048SJason Gunthorpe } 54825112048SJason Gunthorpe 549aaa6f7f6SEd Swierk rc = tpm_getcap(chip, TPM_CAP_PROP_TIS_TIMEOUT, &cap, 550aaa6f7f6SEd Swierk "attempting to determine the timeouts"); 5519deb0eb7SJason Gunthorpe if (rc == TPM_ERR_INVALID_POSTINIT) { 5529deb0eb7SJason Gunthorpe /* The TPM is not started, we are the first to talk to it. 5539deb0eb7SJason Gunthorpe Execute a startup command. */ 554aaa6f7f6SEd Swierk dev_info(&chip->dev, "Issuing TPM_STARTUP\n"); 5559deb0eb7SJason Gunthorpe if (tpm_startup(chip, TPM_ST_CLEAR)) 5569deb0eb7SJason Gunthorpe return rc; 5579deb0eb7SJason Gunthorpe 558aaa6f7f6SEd Swierk rc = tpm_getcap(chip, TPM_CAP_PROP_TIS_TIMEOUT, &cap, 559aaa6f7f6SEd Swierk "attempting to determine the timeouts"); 5609deb0eb7SJason Gunthorpe } 561aaa6f7f6SEd Swierk if (rc) 562aaa6f7f6SEd Swierk return rc; 5639deb0eb7SJason Gunthorpe 564aaa6f7f6SEd Swierk old_timeout[0] = be32_to_cpu(cap.timeout.a); 565aaa6f7f6SEd Swierk old_timeout[1] = be32_to_cpu(cap.timeout.b); 566aaa6f7f6SEd Swierk old_timeout[2] = be32_to_cpu(cap.timeout.c); 567aaa6f7f6SEd Swierk old_timeout[3] = be32_to_cpu(cap.timeout.d); 5688e54caf4SJason Gunthorpe memcpy(new_timeout, old_timeout, sizeof(new_timeout)); 5698e54caf4SJason Gunthorpe 5708e54caf4SJason Gunthorpe /* 5718e54caf4SJason Gunthorpe * Provide ability for vendor overrides of timeout values in case 5728e54caf4SJason Gunthorpe * of misreporting. 5738e54caf4SJason Gunthorpe */ 5748e54caf4SJason Gunthorpe if (chip->ops->update_timeouts != NULL) 575af782f33SChristophe Ricard chip->timeout_adjusted = 5768e54caf4SJason Gunthorpe chip->ops->update_timeouts(chip, new_timeout); 5778e54caf4SJason Gunthorpe 578af782f33SChristophe Ricard if (!chip->timeout_adjusted) { 5799deb0eb7SJason Gunthorpe /* Don't overwrite default if value is 0 */ 5808e54caf4SJason Gunthorpe if (new_timeout[0] != 0 && new_timeout[0] < 1000) { 5818e54caf4SJason Gunthorpe int i; 5828e54caf4SJason Gunthorpe 5839deb0eb7SJason Gunthorpe /* timeouts in msec rather usec */ 5848e54caf4SJason Gunthorpe for (i = 0; i != ARRAY_SIZE(new_timeout); i++) 5858e54caf4SJason Gunthorpe new_timeout[i] *= 1000; 586af782f33SChristophe Ricard chip->timeout_adjusted = true; 5879deb0eb7SJason Gunthorpe } 5888e54caf4SJason Gunthorpe } 5898e54caf4SJason Gunthorpe 5908e54caf4SJason Gunthorpe /* Report adjusted timeouts */ 591af782f33SChristophe Ricard if (chip->timeout_adjusted) { 5928cfffc9dSJason Gunthorpe dev_info(&chip->dev, 5938e54caf4SJason Gunthorpe HW_ERR "Adjusting reported timeouts: A %lu->%luus B %lu->%luus C %lu->%luus D %lu->%luus\n", 5948e54caf4SJason Gunthorpe old_timeout[0], new_timeout[0], 5958e54caf4SJason Gunthorpe old_timeout[1], new_timeout[1], 5968e54caf4SJason Gunthorpe old_timeout[2], new_timeout[2], 5978e54caf4SJason Gunthorpe old_timeout[3], new_timeout[3]); 5988e54caf4SJason Gunthorpe } 5998e54caf4SJason Gunthorpe 600af782f33SChristophe Ricard chip->timeout_a = usecs_to_jiffies(new_timeout[0]); 601af782f33SChristophe Ricard chip->timeout_b = usecs_to_jiffies(new_timeout[1]); 602af782f33SChristophe Ricard chip->timeout_c = usecs_to_jiffies(new_timeout[2]); 603af782f33SChristophe Ricard chip->timeout_d = usecs_to_jiffies(new_timeout[3]); 6049deb0eb7SJason Gunthorpe 605aaa6f7f6SEd Swierk rc = tpm_getcap(chip, TPM_CAP_PROP_TIS_DURATION, &cap, 6069deb0eb7SJason Gunthorpe "attempting to determine the durations"); 6079deb0eb7SJason Gunthorpe if (rc) 6089deb0eb7SJason Gunthorpe return rc; 6099deb0eb7SJason Gunthorpe 610af782f33SChristophe Ricard chip->duration[TPM_SHORT] = 611aaa6f7f6SEd Swierk usecs_to_jiffies(be32_to_cpu(cap.duration.tpm_short)); 612af782f33SChristophe Ricard chip->duration[TPM_MEDIUM] = 613aaa6f7f6SEd Swierk usecs_to_jiffies(be32_to_cpu(cap.duration.tpm_medium)); 614af782f33SChristophe Ricard chip->duration[TPM_LONG] = 615aaa6f7f6SEd Swierk usecs_to_jiffies(be32_to_cpu(cap.duration.tpm_long)); 6169deb0eb7SJason Gunthorpe 6179deb0eb7SJason Gunthorpe /* The Broadcom BCM0102 chipset in a Dell Latitude D820 gets the above 6189deb0eb7SJason Gunthorpe * value wrong and apparently reports msecs rather than usecs. So we 6199deb0eb7SJason Gunthorpe * fix up the resulting too-small TPM_SHORT value to make things work. 6209deb0eb7SJason Gunthorpe * We also scale the TPM_MEDIUM and -_LONG values by 1000. 6219deb0eb7SJason Gunthorpe */ 622af782f33SChristophe Ricard if (chip->duration[TPM_SHORT] < (HZ / 100)) { 623af782f33SChristophe Ricard chip->duration[TPM_SHORT] = HZ; 624af782f33SChristophe Ricard chip->duration[TPM_MEDIUM] *= 1000; 625af782f33SChristophe Ricard chip->duration[TPM_LONG] *= 1000; 626af782f33SChristophe Ricard chip->duration_adjusted = true; 6278cfffc9dSJason Gunthorpe dev_info(&chip->dev, "Adjusting TPM timeout parameters."); 6289deb0eb7SJason Gunthorpe } 629d1d253cfSJason Gunthorpe 630d1d253cfSJason Gunthorpe chip->flags |= TPM_CHIP_FLAG_HAVE_TIMEOUTS; 6319deb0eb7SJason Gunthorpe return 0; 6329deb0eb7SJason Gunthorpe } 6339deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_get_timeouts); 6349deb0eb7SJason Gunthorpe 6359deb0eb7SJason Gunthorpe #define TPM_ORD_CONTINUE_SELFTEST 83 6369deb0eb7SJason Gunthorpe #define CONTINUE_SELFTEST_RESULT_SIZE 10 6379deb0eb7SJason Gunthorpe 6380014777fSJulia Lawall static const struct tpm_input_header continue_selftest_header = { 6399deb0eb7SJason Gunthorpe .tag = TPM_TAG_RQU_COMMAND, 6409deb0eb7SJason Gunthorpe .length = cpu_to_be32(10), 6419deb0eb7SJason Gunthorpe .ordinal = cpu_to_be32(TPM_ORD_CONTINUE_SELFTEST), 6429deb0eb7SJason Gunthorpe }; 6439deb0eb7SJason Gunthorpe 6449deb0eb7SJason Gunthorpe /** 6459deb0eb7SJason Gunthorpe * tpm_continue_selftest -- run TPM's selftest 6469deb0eb7SJason Gunthorpe * @chip: TPM chip to use 6479deb0eb7SJason Gunthorpe * 6489deb0eb7SJason Gunthorpe * Returns 0 on success, < 0 in case of fatal error or a value > 0 representing 6499deb0eb7SJason Gunthorpe * a TPM error code. 6509deb0eb7SJason Gunthorpe */ 6519deb0eb7SJason Gunthorpe static int tpm_continue_selftest(struct tpm_chip *chip) 6529deb0eb7SJason Gunthorpe { 6539deb0eb7SJason Gunthorpe int rc; 6549deb0eb7SJason Gunthorpe struct tpm_cmd_t cmd; 6559deb0eb7SJason Gunthorpe 6569deb0eb7SJason Gunthorpe cmd.header.in = continue_selftest_header; 657d4816edfSJarkko Sakkinen rc = tpm_transmit_cmd(chip, &cmd, CONTINUE_SELFTEST_RESULT_SIZE, 0, 6589deb0eb7SJason Gunthorpe "continue selftest"); 6599deb0eb7SJason Gunthorpe return rc; 6609deb0eb7SJason Gunthorpe } 6619deb0eb7SJason Gunthorpe 6629deb0eb7SJason Gunthorpe #define TPM_ORDINAL_PCRREAD cpu_to_be32(21) 6639deb0eb7SJason Gunthorpe #define READ_PCR_RESULT_SIZE 30 6640014777fSJulia Lawall static const struct tpm_input_header pcrread_header = { 6659deb0eb7SJason Gunthorpe .tag = TPM_TAG_RQU_COMMAND, 6669deb0eb7SJason Gunthorpe .length = cpu_to_be32(14), 6679deb0eb7SJason Gunthorpe .ordinal = TPM_ORDINAL_PCRREAD 6689deb0eb7SJason Gunthorpe }; 6699deb0eb7SJason Gunthorpe 670000a07b0SJason Gunthorpe int tpm_pcr_read_dev(struct tpm_chip *chip, int pcr_idx, u8 *res_buf) 6719deb0eb7SJason Gunthorpe { 6729deb0eb7SJason Gunthorpe int rc; 6739deb0eb7SJason Gunthorpe struct tpm_cmd_t cmd; 6749deb0eb7SJason Gunthorpe 6759deb0eb7SJason Gunthorpe cmd.header.in = pcrread_header; 6769deb0eb7SJason Gunthorpe cmd.params.pcrread_in.pcr_idx = cpu_to_be32(pcr_idx); 677d4816edfSJarkko Sakkinen rc = tpm_transmit_cmd(chip, &cmd, READ_PCR_RESULT_SIZE, 0, 6789deb0eb7SJason Gunthorpe "attempting to read a pcr value"); 6799deb0eb7SJason Gunthorpe 6809deb0eb7SJason Gunthorpe if (rc == 0) 6819deb0eb7SJason Gunthorpe memcpy(res_buf, cmd.params.pcrread_out.pcr_result, 6829deb0eb7SJason Gunthorpe TPM_DIGEST_SIZE); 6839deb0eb7SJason Gunthorpe return rc; 6849deb0eb7SJason Gunthorpe } 6859deb0eb7SJason Gunthorpe 6869deb0eb7SJason Gunthorpe /** 687954650efSJarkko Sakkinen * tpm_is_tpm2 - is the chip a TPM2 chip? 688954650efSJarkko Sakkinen * @chip_num: tpm idx # or ANY 689954650efSJarkko Sakkinen * 690954650efSJarkko Sakkinen * Returns < 0 on error, and 1 or 0 on success depending whether the chip 691954650efSJarkko Sakkinen * is a TPM2 chip. 692954650efSJarkko Sakkinen */ 693954650efSJarkko Sakkinen int tpm_is_tpm2(u32 chip_num) 694954650efSJarkko Sakkinen { 695954650efSJarkko Sakkinen struct tpm_chip *chip; 696954650efSJarkko Sakkinen int rc; 697954650efSJarkko Sakkinen 698954650efSJarkko Sakkinen chip = tpm_chip_find_get(chip_num); 699954650efSJarkko Sakkinen if (chip == NULL) 700954650efSJarkko Sakkinen return -ENODEV; 701954650efSJarkko Sakkinen 702954650efSJarkko Sakkinen rc = (chip->flags & TPM_CHIP_FLAG_TPM2) != 0; 703954650efSJarkko Sakkinen 7044e26195fSJason Gunthorpe tpm_put_ops(chip); 705954650efSJarkko Sakkinen 706954650efSJarkko Sakkinen return rc; 707954650efSJarkko Sakkinen } 708954650efSJarkko Sakkinen EXPORT_SYMBOL_GPL(tpm_is_tpm2); 709954650efSJarkko Sakkinen 710954650efSJarkko Sakkinen /** 7119deb0eb7SJason Gunthorpe * tpm_pcr_read - read a pcr value 7129deb0eb7SJason Gunthorpe * @chip_num: tpm idx # or ANY 7139deb0eb7SJason Gunthorpe * @pcr_idx: pcr idx to retrieve 7149deb0eb7SJason Gunthorpe * @res_buf: TPM_PCR value 7159deb0eb7SJason Gunthorpe * size of res_buf is 20 bytes (or NULL if you don't care) 7169deb0eb7SJason Gunthorpe * 7179deb0eb7SJason Gunthorpe * The TPM driver should be built-in, but for whatever reason it 7189deb0eb7SJason Gunthorpe * isn't, protect against the chip disappearing, by incrementing 7199deb0eb7SJason Gunthorpe * the module usage count. 7209deb0eb7SJason Gunthorpe */ 7219deb0eb7SJason Gunthorpe int tpm_pcr_read(u32 chip_num, int pcr_idx, u8 *res_buf) 7229deb0eb7SJason Gunthorpe { 7239deb0eb7SJason Gunthorpe struct tpm_chip *chip; 7249deb0eb7SJason Gunthorpe int rc; 7259deb0eb7SJason Gunthorpe 7269deb0eb7SJason Gunthorpe chip = tpm_chip_find_get(chip_num); 7279deb0eb7SJason Gunthorpe if (chip == NULL) 7289deb0eb7SJason Gunthorpe return -ENODEV; 7297a1d7e6dSJarkko Sakkinen if (chip->flags & TPM_CHIP_FLAG_TPM2) 7307a1d7e6dSJarkko Sakkinen rc = tpm2_pcr_read(chip, pcr_idx, res_buf); 7317a1d7e6dSJarkko Sakkinen else 732000a07b0SJason Gunthorpe rc = tpm_pcr_read_dev(chip, pcr_idx, res_buf); 7334e26195fSJason Gunthorpe tpm_put_ops(chip); 7349deb0eb7SJason Gunthorpe return rc; 7359deb0eb7SJason Gunthorpe } 7369deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_pcr_read); 7379deb0eb7SJason Gunthorpe 738ca6d4580SWinkler, Tomas #define TPM_ORD_PCR_EXTEND cpu_to_be32(20) 739ca6d4580SWinkler, Tomas #define EXTEND_PCR_RESULT_SIZE 34 740ca6d4580SWinkler, Tomas static const struct tpm_input_header pcrextend_header = { 741ca6d4580SWinkler, Tomas .tag = TPM_TAG_RQU_COMMAND, 742ca6d4580SWinkler, Tomas .length = cpu_to_be32(34), 743ca6d4580SWinkler, Tomas .ordinal = TPM_ORD_PCR_EXTEND 744ca6d4580SWinkler, Tomas }; 745ca6d4580SWinkler, Tomas 7469deb0eb7SJason Gunthorpe /** 7479deb0eb7SJason Gunthorpe * tpm_pcr_extend - extend pcr value with hash 7489deb0eb7SJason Gunthorpe * @chip_num: tpm idx # or AN& 7499deb0eb7SJason Gunthorpe * @pcr_idx: pcr idx to extend 7509deb0eb7SJason Gunthorpe * @hash: hash value used to extend pcr value 7519deb0eb7SJason Gunthorpe * 7529deb0eb7SJason Gunthorpe * The TPM driver should be built-in, but for whatever reason it 7539deb0eb7SJason Gunthorpe * isn't, protect against the chip disappearing, by incrementing 7549deb0eb7SJason Gunthorpe * the module usage count. 7559deb0eb7SJason Gunthorpe */ 7569deb0eb7SJason Gunthorpe int tpm_pcr_extend(u32 chip_num, int pcr_idx, const u8 *hash) 7579deb0eb7SJason Gunthorpe { 7589deb0eb7SJason Gunthorpe struct tpm_cmd_t cmd; 7599deb0eb7SJason Gunthorpe int rc; 7609deb0eb7SJason Gunthorpe struct tpm_chip *chip; 7619deb0eb7SJason Gunthorpe 7629deb0eb7SJason Gunthorpe chip = tpm_chip_find_get(chip_num); 7639deb0eb7SJason Gunthorpe if (chip == NULL) 7649deb0eb7SJason Gunthorpe return -ENODEV; 7659deb0eb7SJason Gunthorpe 7667a1d7e6dSJarkko Sakkinen if (chip->flags & TPM_CHIP_FLAG_TPM2) { 7677a1d7e6dSJarkko Sakkinen rc = tpm2_pcr_extend(chip, pcr_idx, hash); 7684e26195fSJason Gunthorpe tpm_put_ops(chip); 7697a1d7e6dSJarkko Sakkinen return rc; 7707a1d7e6dSJarkko Sakkinen } 7717a1d7e6dSJarkko Sakkinen 7729deb0eb7SJason Gunthorpe cmd.header.in = pcrextend_header; 7739deb0eb7SJason Gunthorpe cmd.params.pcrextend_in.pcr_idx = cpu_to_be32(pcr_idx); 7749deb0eb7SJason Gunthorpe memcpy(cmd.params.pcrextend_in.hash, hash, TPM_DIGEST_SIZE); 775d4816edfSJarkko Sakkinen rc = tpm_transmit_cmd(chip, &cmd, EXTEND_PCR_RESULT_SIZE, 0, 7769deb0eb7SJason Gunthorpe "attempting extend a PCR value"); 7779deb0eb7SJason Gunthorpe 7784e26195fSJason Gunthorpe tpm_put_ops(chip); 7799deb0eb7SJason Gunthorpe return rc; 7809deb0eb7SJason Gunthorpe } 7819deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_pcr_extend); 7829deb0eb7SJason Gunthorpe 7839deb0eb7SJason Gunthorpe /** 7849deb0eb7SJason Gunthorpe * tpm_do_selftest - have the TPM continue its selftest and wait until it 7859deb0eb7SJason Gunthorpe * can receive further commands 7869deb0eb7SJason Gunthorpe * @chip: TPM chip to use 7879deb0eb7SJason Gunthorpe * 7889deb0eb7SJason Gunthorpe * Returns 0 on success, < 0 in case of fatal error or a value > 0 representing 7899deb0eb7SJason Gunthorpe * a TPM error code. 7909deb0eb7SJason Gunthorpe */ 7919deb0eb7SJason Gunthorpe int tpm_do_selftest(struct tpm_chip *chip) 7929deb0eb7SJason Gunthorpe { 7939deb0eb7SJason Gunthorpe int rc; 7949deb0eb7SJason Gunthorpe unsigned int loops; 7959deb0eb7SJason Gunthorpe unsigned int delay_msec = 100; 7969deb0eb7SJason Gunthorpe unsigned long duration; 7970c541332SJarkko Sakkinen u8 dummy[TPM_DIGEST_SIZE]; 7989deb0eb7SJason Gunthorpe 7999deb0eb7SJason Gunthorpe duration = tpm_calc_ordinal_duration(chip, TPM_ORD_CONTINUE_SELFTEST); 8009deb0eb7SJason Gunthorpe 8019deb0eb7SJason Gunthorpe loops = jiffies_to_msecs(duration) / delay_msec; 8029deb0eb7SJason Gunthorpe 8039deb0eb7SJason Gunthorpe rc = tpm_continue_selftest(chip); 8049deb0eb7SJason Gunthorpe /* This may fail if there was no TPM driver during a suspend/resume 8059deb0eb7SJason Gunthorpe * cycle; some may return 10 (BAD_ORDINAL), others 28 (FAILEDSELFTEST) 8069deb0eb7SJason Gunthorpe */ 8079deb0eb7SJason Gunthorpe if (rc) 8089deb0eb7SJason Gunthorpe return rc; 8099deb0eb7SJason Gunthorpe 8109deb0eb7SJason Gunthorpe do { 8119deb0eb7SJason Gunthorpe /* Attempt to read a PCR value */ 8120c541332SJarkko Sakkinen rc = tpm_pcr_read_dev(chip, 0, dummy); 8130c541332SJarkko Sakkinen 8149deb0eb7SJason Gunthorpe /* Some buggy TPMs will not respond to tpm_tis_ready() for 8159deb0eb7SJason Gunthorpe * around 300ms while the self test is ongoing, keep trying 8169deb0eb7SJason Gunthorpe * until the self test duration expires. */ 8179deb0eb7SJason Gunthorpe if (rc == -ETIME) { 8188cfffc9dSJason Gunthorpe dev_info( 8198cfffc9dSJason Gunthorpe &chip->dev, HW_ERR 8208cfffc9dSJason Gunthorpe "TPM command timed out during continue self test"); 8219deb0eb7SJason Gunthorpe msleep(delay_msec); 8229deb0eb7SJason Gunthorpe continue; 8239deb0eb7SJason Gunthorpe } 8249deb0eb7SJason Gunthorpe 8259deb0eb7SJason Gunthorpe if (rc == TPM_ERR_DISABLED || rc == TPM_ERR_DEACTIVATED) { 8268cfffc9dSJason Gunthorpe dev_info(&chip->dev, 8279deb0eb7SJason Gunthorpe "TPM is disabled/deactivated (0x%X)\n", rc); 8289deb0eb7SJason Gunthorpe /* TPM is disabled and/or deactivated; driver can 8299deb0eb7SJason Gunthorpe * proceed and TPM does handle commands for 8309deb0eb7SJason Gunthorpe * suspend/resume correctly 8319deb0eb7SJason Gunthorpe */ 8329deb0eb7SJason Gunthorpe return 0; 8339deb0eb7SJason Gunthorpe } 8349deb0eb7SJason Gunthorpe if (rc != TPM_WARN_DOING_SELFTEST) 8359deb0eb7SJason Gunthorpe return rc; 8369deb0eb7SJason Gunthorpe msleep(delay_msec); 8379deb0eb7SJason Gunthorpe } while (--loops > 0); 8389deb0eb7SJason Gunthorpe 8399deb0eb7SJason Gunthorpe return rc; 8409deb0eb7SJason Gunthorpe } 8419deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_do_selftest); 8429deb0eb7SJason Gunthorpe 843cae8b441SJason Gunthorpe /** 844cae8b441SJason Gunthorpe * tpm1_auto_startup - Perform the standard automatic TPM initialization 845cae8b441SJason Gunthorpe * sequence 846cae8b441SJason Gunthorpe * @chip: TPM chip to use 847cae8b441SJason Gunthorpe * 848cae8b441SJason Gunthorpe * Returns 0 on success, < 0 in case of fatal error. 849cae8b441SJason Gunthorpe */ 850cae8b441SJason Gunthorpe int tpm1_auto_startup(struct tpm_chip *chip) 851cae8b441SJason Gunthorpe { 852cae8b441SJason Gunthorpe int rc; 853cae8b441SJason Gunthorpe 854cae8b441SJason Gunthorpe rc = tpm_get_timeouts(chip); 855cae8b441SJason Gunthorpe if (rc) 856cae8b441SJason Gunthorpe goto out; 857cae8b441SJason Gunthorpe rc = tpm_do_selftest(chip); 858cae8b441SJason Gunthorpe if (rc) { 859cae8b441SJason Gunthorpe dev_err(&chip->dev, "TPM self test failed\n"); 860cae8b441SJason Gunthorpe goto out; 861cae8b441SJason Gunthorpe } 862cae8b441SJason Gunthorpe 863cae8b441SJason Gunthorpe return rc; 864cae8b441SJason Gunthorpe out: 865cae8b441SJason Gunthorpe if (rc > 0) 866cae8b441SJason Gunthorpe rc = -ENODEV; 867cae8b441SJason Gunthorpe return rc; 868cae8b441SJason Gunthorpe } 869cae8b441SJason Gunthorpe 8709deb0eb7SJason Gunthorpe int tpm_send(u32 chip_num, void *cmd, size_t buflen) 8719deb0eb7SJason Gunthorpe { 8729deb0eb7SJason Gunthorpe struct tpm_chip *chip; 8739deb0eb7SJason Gunthorpe int rc; 8749deb0eb7SJason Gunthorpe 8759deb0eb7SJason Gunthorpe chip = tpm_chip_find_get(chip_num); 8769deb0eb7SJason Gunthorpe if (chip == NULL) 8779deb0eb7SJason Gunthorpe return -ENODEV; 8789deb0eb7SJason Gunthorpe 879d4816edfSJarkko Sakkinen rc = tpm_transmit_cmd(chip, cmd, buflen, 0, "attempting tpm_cmd"); 8809deb0eb7SJason Gunthorpe 8814e26195fSJason Gunthorpe tpm_put_ops(chip); 8829deb0eb7SJason Gunthorpe return rc; 8839deb0eb7SJason Gunthorpe } 8849deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_send); 8859deb0eb7SJason Gunthorpe 8869deb0eb7SJason Gunthorpe static bool wait_for_tpm_stat_cond(struct tpm_chip *chip, u8 mask, 8879deb0eb7SJason Gunthorpe bool check_cancel, bool *canceled) 8889deb0eb7SJason Gunthorpe { 8895f82e9f0SJason Gunthorpe u8 status = chip->ops->status(chip); 8909deb0eb7SJason Gunthorpe 8919deb0eb7SJason Gunthorpe *canceled = false; 8929deb0eb7SJason Gunthorpe if ((status & mask) == mask) 8939deb0eb7SJason Gunthorpe return true; 8945f82e9f0SJason Gunthorpe if (check_cancel && chip->ops->req_canceled(chip, status)) { 8959deb0eb7SJason Gunthorpe *canceled = true; 8969deb0eb7SJason Gunthorpe return true; 8979deb0eb7SJason Gunthorpe } 8989deb0eb7SJason Gunthorpe return false; 8999deb0eb7SJason Gunthorpe } 9009deb0eb7SJason Gunthorpe 9019deb0eb7SJason Gunthorpe int wait_for_tpm_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout, 9029deb0eb7SJason Gunthorpe wait_queue_head_t *queue, bool check_cancel) 9039deb0eb7SJason Gunthorpe { 9049deb0eb7SJason Gunthorpe unsigned long stop; 9059deb0eb7SJason Gunthorpe long rc; 9069deb0eb7SJason Gunthorpe u8 status; 9079deb0eb7SJason Gunthorpe bool canceled = false; 9089deb0eb7SJason Gunthorpe 9099deb0eb7SJason Gunthorpe /* check current status */ 9105f82e9f0SJason Gunthorpe status = chip->ops->status(chip); 9119deb0eb7SJason Gunthorpe if ((status & mask) == mask) 9129deb0eb7SJason Gunthorpe return 0; 9139deb0eb7SJason Gunthorpe 9149deb0eb7SJason Gunthorpe stop = jiffies + timeout; 9159deb0eb7SJason Gunthorpe 916570a3609SChristophe Ricard if (chip->flags & TPM_CHIP_FLAG_IRQ) { 9179deb0eb7SJason Gunthorpe again: 9189deb0eb7SJason Gunthorpe timeout = stop - jiffies; 9199deb0eb7SJason Gunthorpe if ((long)timeout <= 0) 9209deb0eb7SJason Gunthorpe return -ETIME; 9219deb0eb7SJason Gunthorpe rc = wait_event_interruptible_timeout(*queue, 9229deb0eb7SJason Gunthorpe wait_for_tpm_stat_cond(chip, mask, check_cancel, 9239deb0eb7SJason Gunthorpe &canceled), 9249deb0eb7SJason Gunthorpe timeout); 9259deb0eb7SJason Gunthorpe if (rc > 0) { 9269deb0eb7SJason Gunthorpe if (canceled) 9279deb0eb7SJason Gunthorpe return -ECANCELED; 9289deb0eb7SJason Gunthorpe return 0; 9299deb0eb7SJason Gunthorpe } 9309deb0eb7SJason Gunthorpe if (rc == -ERESTARTSYS && freezing(current)) { 9319deb0eb7SJason Gunthorpe clear_thread_flag(TIF_SIGPENDING); 9329deb0eb7SJason Gunthorpe goto again; 9339deb0eb7SJason Gunthorpe } 9349deb0eb7SJason Gunthorpe } else { 9359deb0eb7SJason Gunthorpe do { 9369deb0eb7SJason Gunthorpe msleep(TPM_TIMEOUT); 9375f82e9f0SJason Gunthorpe status = chip->ops->status(chip); 9389deb0eb7SJason Gunthorpe if ((status & mask) == mask) 9399deb0eb7SJason Gunthorpe return 0; 9409deb0eb7SJason Gunthorpe } while (time_before(jiffies, stop)); 9419deb0eb7SJason Gunthorpe } 9429deb0eb7SJason Gunthorpe return -ETIME; 9439deb0eb7SJason Gunthorpe } 9449deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(wait_for_tpm_stat); 9459deb0eb7SJason Gunthorpe 9469deb0eb7SJason Gunthorpe #define TPM_ORD_SAVESTATE cpu_to_be32(152) 9479deb0eb7SJason Gunthorpe #define SAVESTATE_RESULT_SIZE 10 9489deb0eb7SJason Gunthorpe 9490014777fSJulia Lawall static const struct tpm_input_header savestate_header = { 9509deb0eb7SJason Gunthorpe .tag = TPM_TAG_RQU_COMMAND, 9519deb0eb7SJason Gunthorpe .length = cpu_to_be32(10), 9529deb0eb7SJason Gunthorpe .ordinal = TPM_ORD_SAVESTATE 9539deb0eb7SJason Gunthorpe }; 9549deb0eb7SJason Gunthorpe 9559deb0eb7SJason Gunthorpe /* 9569deb0eb7SJason Gunthorpe * We are about to suspend. Save the TPM state 9579deb0eb7SJason Gunthorpe * so that it can be restored. 9589deb0eb7SJason Gunthorpe */ 9599deb0eb7SJason Gunthorpe int tpm_pm_suspend(struct device *dev) 9609deb0eb7SJason Gunthorpe { 961ec03c50bSStefan Berger struct tpm_chip *chip = dev_get_drvdata(dev); 9629deb0eb7SJason Gunthorpe struct tpm_cmd_t cmd; 9639deb0eb7SJason Gunthorpe int rc, try; 9649deb0eb7SJason Gunthorpe 9659deb0eb7SJason Gunthorpe u8 dummy_hash[TPM_DIGEST_SIZE] = { 0 }; 9669deb0eb7SJason Gunthorpe 9679deb0eb7SJason Gunthorpe if (chip == NULL) 9689deb0eb7SJason Gunthorpe return -ENODEV; 9699deb0eb7SJason Gunthorpe 97074d6b3ceSJarkko Sakkinen if (chip->flags & TPM_CHIP_FLAG_TPM2) { 97174d6b3ceSJarkko Sakkinen tpm2_shutdown(chip, TPM2_SU_STATE); 97274d6b3ceSJarkko Sakkinen return 0; 97374d6b3ceSJarkko Sakkinen } 97430fc8d13SJarkko Sakkinen 9759deb0eb7SJason Gunthorpe /* for buggy tpm, flush pcrs with extend to selected dummy */ 9769deb0eb7SJason Gunthorpe if (tpm_suspend_pcr) { 9779deb0eb7SJason Gunthorpe cmd.header.in = pcrextend_header; 9789deb0eb7SJason Gunthorpe cmd.params.pcrextend_in.pcr_idx = cpu_to_be32(tpm_suspend_pcr); 9799deb0eb7SJason Gunthorpe memcpy(cmd.params.pcrextend_in.hash, dummy_hash, 9809deb0eb7SJason Gunthorpe TPM_DIGEST_SIZE); 981d4816edfSJarkko Sakkinen rc = tpm_transmit_cmd(chip, &cmd, EXTEND_PCR_RESULT_SIZE, 0, 9829deb0eb7SJason Gunthorpe "extending dummy pcr before suspend"); 9839deb0eb7SJason Gunthorpe } 9849deb0eb7SJason Gunthorpe 9859deb0eb7SJason Gunthorpe /* now do the actual savestate */ 9869deb0eb7SJason Gunthorpe for (try = 0; try < TPM_RETRY; try++) { 9879deb0eb7SJason Gunthorpe cmd.header.in = savestate_header; 988d4816edfSJarkko Sakkinen rc = tpm_transmit_cmd(chip, &cmd, SAVESTATE_RESULT_SIZE, 0, 989d4816edfSJarkko Sakkinen NULL); 9909deb0eb7SJason Gunthorpe 9919deb0eb7SJason Gunthorpe /* 9929deb0eb7SJason Gunthorpe * If the TPM indicates that it is too busy to respond to 9939deb0eb7SJason Gunthorpe * this command then retry before giving up. It can take 9949deb0eb7SJason Gunthorpe * several seconds for this TPM to be ready. 9959deb0eb7SJason Gunthorpe * 9969deb0eb7SJason Gunthorpe * This can happen if the TPM has already been sent the 9979deb0eb7SJason Gunthorpe * SaveState command before the driver has loaded. TCG 1.2 9989deb0eb7SJason Gunthorpe * specification states that any communication after SaveState 9999deb0eb7SJason Gunthorpe * may cause the TPM to invalidate previously saved state. 10009deb0eb7SJason Gunthorpe */ 10019deb0eb7SJason Gunthorpe if (rc != TPM_WARN_RETRY) 10029deb0eb7SJason Gunthorpe break; 10039deb0eb7SJason Gunthorpe msleep(TPM_TIMEOUT_RETRY); 10049deb0eb7SJason Gunthorpe } 10059deb0eb7SJason Gunthorpe 10069deb0eb7SJason Gunthorpe if (rc) 10078cfffc9dSJason Gunthorpe dev_err(&chip->dev, 10089deb0eb7SJason Gunthorpe "Error (%d) sending savestate before suspend\n", rc); 10099deb0eb7SJason Gunthorpe else if (try > 0) 10108cfffc9dSJason Gunthorpe dev_warn(&chip->dev, "TPM savestate took %dms\n", 10119deb0eb7SJason Gunthorpe try * TPM_TIMEOUT_RETRY); 10129deb0eb7SJason Gunthorpe 10139deb0eb7SJason Gunthorpe return rc; 10149deb0eb7SJason Gunthorpe } 10159deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_pm_suspend); 10169deb0eb7SJason Gunthorpe 10179deb0eb7SJason Gunthorpe /* 10189deb0eb7SJason Gunthorpe * Resume from a power safe. The BIOS already restored 10199deb0eb7SJason Gunthorpe * the TPM state. 10209deb0eb7SJason Gunthorpe */ 10219deb0eb7SJason Gunthorpe int tpm_pm_resume(struct device *dev) 10229deb0eb7SJason Gunthorpe { 1023ec03c50bSStefan Berger struct tpm_chip *chip = dev_get_drvdata(dev); 10249deb0eb7SJason Gunthorpe 10259deb0eb7SJason Gunthorpe if (chip == NULL) 10269deb0eb7SJason Gunthorpe return -ENODEV; 10279deb0eb7SJason Gunthorpe 10289deb0eb7SJason Gunthorpe return 0; 10299deb0eb7SJason Gunthorpe } 10309deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_pm_resume); 10319deb0eb7SJason Gunthorpe 10329deb0eb7SJason Gunthorpe #define TPM_GETRANDOM_RESULT_SIZE 18 10330014777fSJulia Lawall static const struct tpm_input_header tpm_getrandom_header = { 10349deb0eb7SJason Gunthorpe .tag = TPM_TAG_RQU_COMMAND, 10359deb0eb7SJason Gunthorpe .length = cpu_to_be32(14), 10369deb0eb7SJason Gunthorpe .ordinal = TPM_ORD_GET_RANDOM 10379deb0eb7SJason Gunthorpe }; 10389deb0eb7SJason Gunthorpe 10399deb0eb7SJason Gunthorpe /** 10409deb0eb7SJason Gunthorpe * tpm_get_random() - Get random bytes from the tpm's RNG 10419deb0eb7SJason Gunthorpe * @chip_num: A specific chip number for the request or TPM_ANY_NUM 10429deb0eb7SJason Gunthorpe * @out: destination buffer for the random bytes 10439deb0eb7SJason Gunthorpe * @max: the max number of bytes to write to @out 10449deb0eb7SJason Gunthorpe * 10459deb0eb7SJason Gunthorpe * Returns < 0 on error and the number of bytes read on success 10469deb0eb7SJason Gunthorpe */ 10479deb0eb7SJason Gunthorpe int tpm_get_random(u32 chip_num, u8 *out, size_t max) 10489deb0eb7SJason Gunthorpe { 10499deb0eb7SJason Gunthorpe struct tpm_chip *chip; 10509deb0eb7SJason Gunthorpe struct tpm_cmd_t tpm_cmd; 10519deb0eb7SJason Gunthorpe u32 recd, num_bytes = min_t(u32, max, TPM_MAX_RNG_DATA); 10529deb0eb7SJason Gunthorpe int err, total = 0, retries = 5; 10539deb0eb7SJason Gunthorpe u8 *dest = out; 10549deb0eb7SJason Gunthorpe 10553e14d83eSJarkko Sakkinen if (!out || !num_bytes || max > TPM_MAX_RNG_DATA) 10563e14d83eSJarkko Sakkinen return -EINVAL; 10573e14d83eSJarkko Sakkinen 10589deb0eb7SJason Gunthorpe chip = tpm_chip_find_get(chip_num); 10599deb0eb7SJason Gunthorpe if (chip == NULL) 10609deb0eb7SJason Gunthorpe return -ENODEV; 10619deb0eb7SJason Gunthorpe 10627a1d7e6dSJarkko Sakkinen if (chip->flags & TPM_CHIP_FLAG_TPM2) { 10637a1d7e6dSJarkko Sakkinen err = tpm2_get_random(chip, out, max); 10644e26195fSJason Gunthorpe tpm_put_ops(chip); 10657a1d7e6dSJarkko Sakkinen return err; 10667a1d7e6dSJarkko Sakkinen } 10677a1d7e6dSJarkko Sakkinen 10689deb0eb7SJason Gunthorpe do { 10699deb0eb7SJason Gunthorpe tpm_cmd.header.in = tpm_getrandom_header; 10709deb0eb7SJason Gunthorpe tpm_cmd.params.getrandom_in.num_bytes = cpu_to_be32(num_bytes); 10719deb0eb7SJason Gunthorpe 107287155b73SJarkko Sakkinen err = tpm_transmit_cmd(chip, &tpm_cmd, 10739deb0eb7SJason Gunthorpe TPM_GETRANDOM_RESULT_SIZE + num_bytes, 1074d4816edfSJarkko Sakkinen 0, "attempting get random"); 10759deb0eb7SJason Gunthorpe if (err) 10769deb0eb7SJason Gunthorpe break; 10779deb0eb7SJason Gunthorpe 10789deb0eb7SJason Gunthorpe recd = be32_to_cpu(tpm_cmd.params.getrandom_out.rng_data_len); 10799deb0eb7SJason Gunthorpe memcpy(dest, tpm_cmd.params.getrandom_out.rng_data, recd); 10809deb0eb7SJason Gunthorpe 10819deb0eb7SJason Gunthorpe dest += recd; 10829deb0eb7SJason Gunthorpe total += recd; 10839deb0eb7SJason Gunthorpe num_bytes -= recd; 10849deb0eb7SJason Gunthorpe } while (retries-- && total < max); 10859deb0eb7SJason Gunthorpe 10864e26195fSJason Gunthorpe tpm_put_ops(chip); 10879deb0eb7SJason Gunthorpe return total ? total : -EIO; 10889deb0eb7SJason Gunthorpe } 10899deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_get_random); 10909deb0eb7SJason Gunthorpe 1091954650efSJarkko Sakkinen /** 1092954650efSJarkko Sakkinen * tpm_seal_trusted() - seal a trusted key 1093954650efSJarkko Sakkinen * @chip_num: A specific chip number for the request or TPM_ANY_NUM 1094954650efSJarkko Sakkinen * @options: authentication values and other options 1095954650efSJarkko Sakkinen * @payload: the key data in clear and encrypted form 1096954650efSJarkko Sakkinen * 1097954650efSJarkko Sakkinen * Returns < 0 on error and 0 on success. At the moment, only TPM 2.0 chips 1098954650efSJarkko Sakkinen * are supported. 1099954650efSJarkko Sakkinen */ 1100954650efSJarkko Sakkinen int tpm_seal_trusted(u32 chip_num, struct trusted_key_payload *payload, 1101954650efSJarkko Sakkinen struct trusted_key_options *options) 1102954650efSJarkko Sakkinen { 1103954650efSJarkko Sakkinen struct tpm_chip *chip; 1104954650efSJarkko Sakkinen int rc; 1105954650efSJarkko Sakkinen 1106954650efSJarkko Sakkinen chip = tpm_chip_find_get(chip_num); 1107954650efSJarkko Sakkinen if (chip == NULL || !(chip->flags & TPM_CHIP_FLAG_TPM2)) 1108954650efSJarkko Sakkinen return -ENODEV; 1109954650efSJarkko Sakkinen 1110954650efSJarkko Sakkinen rc = tpm2_seal_trusted(chip, payload, options); 1111954650efSJarkko Sakkinen 11124e26195fSJason Gunthorpe tpm_put_ops(chip); 1113954650efSJarkko Sakkinen return rc; 1114954650efSJarkko Sakkinen } 1115954650efSJarkko Sakkinen EXPORT_SYMBOL_GPL(tpm_seal_trusted); 1116954650efSJarkko Sakkinen 1117954650efSJarkko Sakkinen /** 1118954650efSJarkko Sakkinen * tpm_unseal_trusted() - unseal a trusted key 1119954650efSJarkko Sakkinen * @chip_num: A specific chip number for the request or TPM_ANY_NUM 1120954650efSJarkko Sakkinen * @options: authentication values and other options 1121954650efSJarkko Sakkinen * @payload: the key data in clear and encrypted form 1122954650efSJarkko Sakkinen * 1123954650efSJarkko Sakkinen * Returns < 0 on error and 0 on success. At the moment, only TPM 2.0 chips 1124954650efSJarkko Sakkinen * are supported. 1125954650efSJarkko Sakkinen */ 1126954650efSJarkko Sakkinen int tpm_unseal_trusted(u32 chip_num, struct trusted_key_payload *payload, 1127954650efSJarkko Sakkinen struct trusted_key_options *options) 1128954650efSJarkko Sakkinen { 1129954650efSJarkko Sakkinen struct tpm_chip *chip; 1130954650efSJarkko Sakkinen int rc; 1131954650efSJarkko Sakkinen 1132954650efSJarkko Sakkinen chip = tpm_chip_find_get(chip_num); 1133954650efSJarkko Sakkinen if (chip == NULL || !(chip->flags & TPM_CHIP_FLAG_TPM2)) 1134954650efSJarkko Sakkinen return -ENODEV; 1135954650efSJarkko Sakkinen 1136954650efSJarkko Sakkinen rc = tpm2_unseal_trusted(chip, payload, options); 1137954650efSJarkko Sakkinen 11384e26195fSJason Gunthorpe tpm_put_ops(chip); 11394e26195fSJason Gunthorpe 1140954650efSJarkko Sakkinen return rc; 1141954650efSJarkko Sakkinen } 1142954650efSJarkko Sakkinen EXPORT_SYMBOL_GPL(tpm_unseal_trusted); 1143954650efSJarkko Sakkinen 1144313d21eeSJarkko Sakkinen static int __init tpm_init(void) 1145313d21eeSJarkko Sakkinen { 1146313d21eeSJarkko Sakkinen int rc; 1147313d21eeSJarkko Sakkinen 1148313d21eeSJarkko Sakkinen tpm_class = class_create(THIS_MODULE, "tpm"); 1149313d21eeSJarkko Sakkinen if (IS_ERR(tpm_class)) { 1150313d21eeSJarkko Sakkinen pr_err("couldn't create tpm class\n"); 1151313d21eeSJarkko Sakkinen return PTR_ERR(tpm_class); 1152313d21eeSJarkko Sakkinen } 1153313d21eeSJarkko Sakkinen 1154313d21eeSJarkko Sakkinen rc = alloc_chrdev_region(&tpm_devt, 0, TPM_NUM_DEVICES, "tpm"); 1155313d21eeSJarkko Sakkinen if (rc < 0) { 1156313d21eeSJarkko Sakkinen pr_err("tpm: failed to allocate char dev region\n"); 1157313d21eeSJarkko Sakkinen class_destroy(tpm_class); 1158313d21eeSJarkko Sakkinen return rc; 1159313d21eeSJarkko Sakkinen } 1160313d21eeSJarkko Sakkinen 1161313d21eeSJarkko Sakkinen return 0; 1162313d21eeSJarkko Sakkinen } 1163313d21eeSJarkko Sakkinen 1164313d21eeSJarkko Sakkinen static void __exit tpm_exit(void) 1165313d21eeSJarkko Sakkinen { 116615516788SStefan Berger idr_destroy(&dev_nums_idr); 1167313d21eeSJarkko Sakkinen class_destroy(tpm_class); 1168313d21eeSJarkko Sakkinen unregister_chrdev_region(tpm_devt, TPM_NUM_DEVICES); 1169313d21eeSJarkko Sakkinen } 1170313d21eeSJarkko Sakkinen 1171313d21eeSJarkko Sakkinen subsys_initcall(tpm_init); 1172313d21eeSJarkko Sakkinen module_exit(tpm_exit); 1173313d21eeSJarkko Sakkinen 11749deb0eb7SJason Gunthorpe MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)"); 11759deb0eb7SJason Gunthorpe MODULE_DESCRIPTION("TPM Driver"); 11769deb0eb7SJason Gunthorpe MODULE_VERSION("2.0"); 11779deb0eb7SJason Gunthorpe MODULE_LICENSE("GPL"); 1178