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 331745b361eSJarkko Sakkinen static bool tpm_validate_command(struct tpm_chip *chip, 332745b361eSJarkko Sakkinen struct tpm_space *space, 333745b361eSJarkko Sakkinen const u8 *cmd, 33458472f5cSJarkko Sakkinen size_t len) 33558472f5cSJarkko Sakkinen { 33658472f5cSJarkko Sakkinen const struct tpm_input_header *header = (const void *)cmd; 33758472f5cSJarkko Sakkinen int i; 33858472f5cSJarkko Sakkinen u32 cc; 33958472f5cSJarkko Sakkinen u32 attrs; 34058472f5cSJarkko Sakkinen unsigned int nr_handles; 34158472f5cSJarkko Sakkinen 34258472f5cSJarkko Sakkinen if (len < TPM_HEADER_SIZE) 34358472f5cSJarkko Sakkinen return false; 34458472f5cSJarkko Sakkinen 345745b361eSJarkko Sakkinen if (!space) 346745b361eSJarkko Sakkinen return true; 347745b361eSJarkko Sakkinen 34858472f5cSJarkko Sakkinen if (chip->flags & TPM_CHIP_FLAG_TPM2 && chip->nr_commands) { 34958472f5cSJarkko Sakkinen cc = be32_to_cpu(header->ordinal); 35058472f5cSJarkko Sakkinen 35158472f5cSJarkko Sakkinen i = tpm2_find_cc(chip, cc); 35258472f5cSJarkko Sakkinen if (i < 0) { 35358472f5cSJarkko Sakkinen dev_dbg(&chip->dev, "0x%04X is an invalid command\n", 35458472f5cSJarkko Sakkinen cc); 35558472f5cSJarkko Sakkinen return false; 35658472f5cSJarkko Sakkinen } 35758472f5cSJarkko Sakkinen 35858472f5cSJarkko Sakkinen attrs = chip->cc_attrs_tbl[i]; 35958472f5cSJarkko Sakkinen nr_handles = 36058472f5cSJarkko Sakkinen 4 * ((attrs >> TPM2_CC_ATTR_CHANDLES) & GENMASK(2, 0)); 36158472f5cSJarkko Sakkinen if (len < TPM_HEADER_SIZE + 4 * nr_handles) 36258472f5cSJarkko Sakkinen goto err_len; 36358472f5cSJarkko Sakkinen } 36458472f5cSJarkko Sakkinen 36558472f5cSJarkko Sakkinen return true; 36658472f5cSJarkko Sakkinen err_len: 36758472f5cSJarkko Sakkinen dev_dbg(&chip->dev, 36858472f5cSJarkko Sakkinen "%s: insufficient command length %zu", __func__, len); 36958472f5cSJarkko Sakkinen return false; 37058472f5cSJarkko Sakkinen } 37158472f5cSJarkko Sakkinen 372f865c196SWinkler, Tomas /** 373f865c196SWinkler, Tomas * tmp_transmit - Internal kernel interface to transmit TPM commands. 374f865c196SWinkler, Tomas * 375f865c196SWinkler, Tomas * @chip: TPM chip to use 376f865c196SWinkler, Tomas * @buf: TPM command buffer 377f865c196SWinkler, Tomas * @bufsiz: length of the TPM command buffer 378f865c196SWinkler, Tomas * @flags: tpm transmit flags - bitmap 379f865c196SWinkler, Tomas * 380f865c196SWinkler, Tomas * Return: 381f865c196SWinkler, Tomas * 0 when the operation is successful. 382f865c196SWinkler, Tomas * A negative number for system errors (errno). 3839deb0eb7SJason Gunthorpe */ 384745b361eSJarkko Sakkinen ssize_t tpm_transmit(struct tpm_chip *chip, struct tpm_space *space, 385745b361eSJarkko Sakkinen u8 *buf, size_t bufsiz, unsigned int flags) 3869deb0eb7SJason Gunthorpe { 387745b361eSJarkko Sakkinen struct tpm_output_header *header = (void *)buf; 388745b361eSJarkko Sakkinen int rc; 389745b361eSJarkko Sakkinen ssize_t len = 0; 3909deb0eb7SJason Gunthorpe u32 count, ordinal; 3919deb0eb7SJason Gunthorpe unsigned long stop; 3929deb0eb7SJason Gunthorpe 393745b361eSJarkko Sakkinen if (!tpm_validate_command(chip, space, buf, bufsiz)) 394ebfd7532SJarkko Sakkinen return -EINVAL; 395ebfd7532SJarkko Sakkinen 3969deb0eb7SJason Gunthorpe if (bufsiz > TPM_BUFSIZE) 3979deb0eb7SJason Gunthorpe bufsiz = TPM_BUFSIZE; 3989deb0eb7SJason Gunthorpe 3999deb0eb7SJason Gunthorpe count = be32_to_cpu(*((__be32 *) (buf + 2))); 4009deb0eb7SJason Gunthorpe ordinal = be32_to_cpu(*((__be32 *) (buf + 6))); 4019deb0eb7SJason Gunthorpe if (count == 0) 4029deb0eb7SJason Gunthorpe return -ENODATA; 4039deb0eb7SJason Gunthorpe if (count > bufsiz) { 4048cfffc9dSJason Gunthorpe dev_err(&chip->dev, 4059deb0eb7SJason Gunthorpe "invalid count value %x %zx\n", count, bufsiz); 4069deb0eb7SJason Gunthorpe return -E2BIG; 4079deb0eb7SJason Gunthorpe } 4089deb0eb7SJason Gunthorpe 409d4816edfSJarkko Sakkinen if (!(flags & TPM_TRANSMIT_UNLOCKED)) 4109deb0eb7SJason Gunthorpe mutex_lock(&chip->tpm_mutex); 4119deb0eb7SJason Gunthorpe 4126804f6bbSStefan Berger if (chip->dev.parent) 413e74f2f76SWinkler, Tomas pm_runtime_get_sync(chip->dev.parent); 414e74f2f76SWinkler, Tomas 415745b361eSJarkko Sakkinen rc = tpm2_prepare_space(chip, space, ordinal, buf); 416745b361eSJarkko Sakkinen if (rc) 417745b361eSJarkko Sakkinen goto out; 418745b361eSJarkko Sakkinen 4195f82e9f0SJason Gunthorpe rc = chip->ops->send(chip, (u8 *) buf, count); 4209deb0eb7SJason Gunthorpe if (rc < 0) { 4218cfffc9dSJason Gunthorpe dev_err(&chip->dev, 422745b361eSJarkko Sakkinen "tpm_transmit: tpm_send: error %d\n", rc); 4239deb0eb7SJason Gunthorpe goto out; 4249deb0eb7SJason Gunthorpe } 4259deb0eb7SJason Gunthorpe 426570a3609SChristophe Ricard if (chip->flags & TPM_CHIP_FLAG_IRQ) 4279deb0eb7SJason Gunthorpe goto out_recv; 4289deb0eb7SJason Gunthorpe 4297a1d7e6dSJarkko Sakkinen if (chip->flags & TPM_CHIP_FLAG_TPM2) 4307a1d7e6dSJarkko Sakkinen stop = jiffies + tpm2_calc_ordinal_duration(chip, ordinal); 4317a1d7e6dSJarkko Sakkinen else 4329deb0eb7SJason Gunthorpe stop = jiffies + tpm_calc_ordinal_duration(chip, ordinal); 4339deb0eb7SJason Gunthorpe do { 4345f82e9f0SJason Gunthorpe u8 status = chip->ops->status(chip); 4355f82e9f0SJason Gunthorpe if ((status & chip->ops->req_complete_mask) == 4365f82e9f0SJason Gunthorpe chip->ops->req_complete_val) 4379deb0eb7SJason Gunthorpe goto out_recv; 4389deb0eb7SJason Gunthorpe 4395f82e9f0SJason Gunthorpe if (chip->ops->req_canceled(chip, status)) { 4408cfffc9dSJason Gunthorpe dev_err(&chip->dev, "Operation Canceled\n"); 4419deb0eb7SJason Gunthorpe rc = -ECANCELED; 4429deb0eb7SJason Gunthorpe goto out; 4439deb0eb7SJason Gunthorpe } 4449deb0eb7SJason Gunthorpe 4459deb0eb7SJason Gunthorpe msleep(TPM_TIMEOUT); /* CHECK */ 4469deb0eb7SJason Gunthorpe rmb(); 4479deb0eb7SJason Gunthorpe } while (time_before(jiffies, stop)); 4489deb0eb7SJason Gunthorpe 4495f82e9f0SJason Gunthorpe chip->ops->cancel(chip); 4508cfffc9dSJason Gunthorpe dev_err(&chip->dev, "Operation Timed out\n"); 4519deb0eb7SJason Gunthorpe rc = -ETIME; 4529deb0eb7SJason Gunthorpe goto out; 4539deb0eb7SJason Gunthorpe 4549deb0eb7SJason Gunthorpe out_recv: 455745b361eSJarkko Sakkinen len = chip->ops->recv(chip, (u8 *) buf, bufsiz); 456745b361eSJarkko Sakkinen if (len < 0) { 457745b361eSJarkko Sakkinen rc = len; 4588cfffc9dSJason Gunthorpe dev_err(&chip->dev, 459745b361eSJarkko Sakkinen "tpm_transmit: tpm_recv: error %d\n", rc); 460a147918eSJarkko Sakkinen goto out; 461745b361eSJarkko Sakkinen } else if (len < TPM_HEADER_SIZE) { 462a147918eSJarkko Sakkinen rc = -EFAULT; 463a147918eSJarkko Sakkinen goto out; 464a147918eSJarkko Sakkinen } 465a147918eSJarkko Sakkinen 466745b361eSJarkko Sakkinen if (len != be32_to_cpu(header->length)) { 467745b361eSJarkko Sakkinen rc = -EFAULT; 468a147918eSJarkko Sakkinen goto out; 469745b361eSJarkko Sakkinen } 470745b361eSJarkko Sakkinen 471745b361eSJarkko Sakkinen rc = tpm2_commit_space(chip, space, ordinal, buf, &len); 472a147918eSJarkko Sakkinen 4739deb0eb7SJason Gunthorpe out: 4746804f6bbSStefan Berger if (chip->dev.parent) 475e74f2f76SWinkler, Tomas pm_runtime_put_sync(chip->dev.parent); 476e74f2f76SWinkler, Tomas 477d4816edfSJarkko Sakkinen if (!(flags & TPM_TRANSMIT_UNLOCKED)) 4789deb0eb7SJason Gunthorpe mutex_unlock(&chip->tpm_mutex); 479745b361eSJarkko Sakkinen return rc ? rc : len; 4809deb0eb7SJason Gunthorpe } 4819deb0eb7SJason Gunthorpe 482f865c196SWinkler, Tomas /** 483f865c196SWinkler, Tomas * tmp_transmit_cmd - send a tpm command to the device 484f865c196SWinkler, Tomas * The function extracts tpm out header return code 485f865c196SWinkler, Tomas * 486f865c196SWinkler, Tomas * @chip: TPM chip to use 487c659af78SStefan Berger * @buf: TPM command buffer 488c659af78SStefan Berger * @bufsiz: length of the buffer 489c659af78SStefan Berger * @min_rsp_body_length: minimum expected length of response body 490f865c196SWinkler, Tomas * @flags: tpm transmit flags - bitmap 491f865c196SWinkler, Tomas * @desc: command description used in the error message 492f865c196SWinkler, Tomas * 493f865c196SWinkler, Tomas * Return: 494f865c196SWinkler, Tomas * 0 when the operation is successful. 495f865c196SWinkler, Tomas * A negative number for system errors (errno). 496f865c196SWinkler, Tomas * A positive number for a TPM error. 497f865c196SWinkler, Tomas */ 498745b361eSJarkko Sakkinen ssize_t tpm_transmit_cmd(struct tpm_chip *chip, struct tpm_space *space, 499745b361eSJarkko Sakkinen const void *buf, size_t bufsiz, 500745b361eSJarkko Sakkinen size_t min_rsp_body_length, unsigned int flags, 501745b361eSJarkko Sakkinen const char *desc) 5029deb0eb7SJason Gunthorpe { 503a147918eSJarkko Sakkinen const struct tpm_output_header *header = buf; 5049deb0eb7SJason Gunthorpe int err; 505c659af78SStefan Berger ssize_t len; 5069deb0eb7SJason Gunthorpe 507745b361eSJarkko Sakkinen len = tpm_transmit(chip, space, (u8 *)buf, bufsiz, flags); 5089deb0eb7SJason Gunthorpe if (len < 0) 5099deb0eb7SJason Gunthorpe return len; 51087155b73SJarkko Sakkinen 51187155b73SJarkko Sakkinen err = be32_to_cpu(header->return_code); 5129deb0eb7SJason Gunthorpe if (err != 0 && desc) 5138cfffc9dSJason Gunthorpe dev_err(&chip->dev, "A TPM error (%d) occurred %s\n", err, 51471ed848fSJarkko Sakkinen desc); 515c659af78SStefan Berger if (err) 5169deb0eb7SJason Gunthorpe return err; 517c659af78SStefan Berger 518c659af78SStefan Berger if (len < min_rsp_body_length + TPM_HEADER_SIZE) 519c659af78SStefan Berger return -EFAULT; 520c659af78SStefan Berger 521c659af78SStefan Berger return 0; 5229deb0eb7SJason Gunthorpe } 5239deb0eb7SJason Gunthorpe 524f865c196SWinkler, Tomas #define TPM_DIGEST_SIZE 20 525f865c196SWinkler, Tomas #define TPM_RET_CODE_IDX 6 5269deb0eb7SJason Gunthorpe #define TPM_INTERNAL_RESULT_SIZE 200 5279deb0eb7SJason Gunthorpe #define TPM_ORD_GET_CAP cpu_to_be32(101) 5289deb0eb7SJason Gunthorpe #define TPM_ORD_GET_RANDOM cpu_to_be32(70) 5299deb0eb7SJason Gunthorpe 5309deb0eb7SJason Gunthorpe static const struct tpm_input_header tpm_getcap_header = { 5319deb0eb7SJason Gunthorpe .tag = TPM_TAG_RQU_COMMAND, 5329deb0eb7SJason Gunthorpe .length = cpu_to_be32(22), 5339deb0eb7SJason Gunthorpe .ordinal = TPM_ORD_GET_CAP 5349deb0eb7SJason Gunthorpe }; 5359deb0eb7SJason Gunthorpe 53684fda152SJarkko Sakkinen ssize_t tpm_getcap(struct tpm_chip *chip, u32 subcap_id, cap_t *cap, 537c659af78SStefan Berger const char *desc, size_t min_cap_length) 5389deb0eb7SJason Gunthorpe { 5399deb0eb7SJason Gunthorpe struct tpm_cmd_t tpm_cmd; 5409deb0eb7SJason Gunthorpe int rc; 5419deb0eb7SJason Gunthorpe 5429deb0eb7SJason Gunthorpe tpm_cmd.header.in = tpm_getcap_header; 54384fda152SJarkko Sakkinen if (subcap_id == TPM_CAP_VERSION_1_1 || 54484fda152SJarkko Sakkinen subcap_id == TPM_CAP_VERSION_1_2) { 54584fda152SJarkko Sakkinen tpm_cmd.params.getcap_in.cap = cpu_to_be32(subcap_id); 5469deb0eb7SJason Gunthorpe /*subcap field not necessary */ 5479deb0eb7SJason Gunthorpe tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(0); 5489deb0eb7SJason Gunthorpe tpm_cmd.header.in.length -= cpu_to_be32(sizeof(__be32)); 5499deb0eb7SJason Gunthorpe } else { 5509deb0eb7SJason Gunthorpe if (subcap_id == TPM_CAP_FLAG_PERM || 5519deb0eb7SJason Gunthorpe subcap_id == TPM_CAP_FLAG_VOL) 55284fda152SJarkko Sakkinen tpm_cmd.params.getcap_in.cap = 55384fda152SJarkko Sakkinen cpu_to_be32(TPM_CAP_FLAG); 5549deb0eb7SJason Gunthorpe else 55584fda152SJarkko Sakkinen tpm_cmd.params.getcap_in.cap = 55684fda152SJarkko Sakkinen cpu_to_be32(TPM_CAP_PROP); 5579deb0eb7SJason Gunthorpe tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4); 55884fda152SJarkko Sakkinen tpm_cmd.params.getcap_in.subcap = cpu_to_be32(subcap_id); 5599deb0eb7SJason Gunthorpe } 560745b361eSJarkko Sakkinen rc = tpm_transmit_cmd(chip, NULL, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE, 561c659af78SStefan Berger min_cap_length, 0, desc); 5629deb0eb7SJason Gunthorpe if (!rc) 5639deb0eb7SJason Gunthorpe *cap = tpm_cmd.params.getcap_out.cap; 5649deb0eb7SJason Gunthorpe return rc; 5659deb0eb7SJason Gunthorpe } 566eb5854e7SJarkko Sakkinen EXPORT_SYMBOL_GPL(tpm_getcap); 5679deb0eb7SJason Gunthorpe 5689deb0eb7SJason Gunthorpe #define TPM_ORD_STARTUP cpu_to_be32(153) 5699deb0eb7SJason Gunthorpe #define TPM_ST_CLEAR cpu_to_be16(1) 5709deb0eb7SJason Gunthorpe #define TPM_ST_STATE cpu_to_be16(2) 5719deb0eb7SJason Gunthorpe #define TPM_ST_DEACTIVATED cpu_to_be16(3) 5729deb0eb7SJason Gunthorpe static const struct tpm_input_header tpm_startup_header = { 5739deb0eb7SJason Gunthorpe .tag = TPM_TAG_RQU_COMMAND, 5749deb0eb7SJason Gunthorpe .length = cpu_to_be32(12), 5759deb0eb7SJason Gunthorpe .ordinal = TPM_ORD_STARTUP 5769deb0eb7SJason Gunthorpe }; 5779deb0eb7SJason Gunthorpe 5789deb0eb7SJason Gunthorpe static int tpm_startup(struct tpm_chip *chip, __be16 startup_type) 5799deb0eb7SJason Gunthorpe { 5809deb0eb7SJason Gunthorpe struct tpm_cmd_t start_cmd; 5819deb0eb7SJason Gunthorpe start_cmd.header.in = tpm_startup_header; 5827a1d7e6dSJarkko Sakkinen 5839deb0eb7SJason Gunthorpe start_cmd.params.startup_in.startup_type = startup_type; 584745b361eSJarkko Sakkinen return tpm_transmit_cmd(chip, NULL, &start_cmd, 585745b361eSJarkko Sakkinen TPM_INTERNAL_RESULT_SIZE, 0, 586c659af78SStefan Berger 0, "attempting to start the TPM"); 5879deb0eb7SJason Gunthorpe } 5889deb0eb7SJason Gunthorpe 5899deb0eb7SJason Gunthorpe int tpm_get_timeouts(struct tpm_chip *chip) 5909deb0eb7SJason Gunthorpe { 591aaa6f7f6SEd Swierk cap_t cap; 5921d70fe9dSMaciej S. Szmigiero unsigned long timeout_old[4], timeout_chip[4], timeout_eff[4]; 5939deb0eb7SJason Gunthorpe ssize_t rc; 5949deb0eb7SJason Gunthorpe 595d1d253cfSJason Gunthorpe if (chip->flags & TPM_CHIP_FLAG_HAVE_TIMEOUTS) 596d1d253cfSJason Gunthorpe return 0; 597d1d253cfSJason Gunthorpe 59825112048SJason Gunthorpe if (chip->flags & TPM_CHIP_FLAG_TPM2) { 59925112048SJason Gunthorpe /* Fixed timeouts for TPM2 */ 600af782f33SChristophe Ricard chip->timeout_a = msecs_to_jiffies(TPM2_TIMEOUT_A); 601af782f33SChristophe Ricard chip->timeout_b = msecs_to_jiffies(TPM2_TIMEOUT_B); 602af782f33SChristophe Ricard chip->timeout_c = msecs_to_jiffies(TPM2_TIMEOUT_C); 603af782f33SChristophe Ricard chip->timeout_d = msecs_to_jiffies(TPM2_TIMEOUT_D); 604af782f33SChristophe Ricard chip->duration[TPM_SHORT] = 60525112048SJason Gunthorpe msecs_to_jiffies(TPM2_DURATION_SHORT); 606af782f33SChristophe Ricard chip->duration[TPM_MEDIUM] = 60725112048SJason Gunthorpe msecs_to_jiffies(TPM2_DURATION_MEDIUM); 608af782f33SChristophe Ricard chip->duration[TPM_LONG] = 60925112048SJason Gunthorpe msecs_to_jiffies(TPM2_DURATION_LONG); 610d1d253cfSJason Gunthorpe 611d1d253cfSJason Gunthorpe chip->flags |= TPM_CHIP_FLAG_HAVE_TIMEOUTS; 61225112048SJason Gunthorpe return 0; 61325112048SJason Gunthorpe } 61425112048SJason Gunthorpe 615c659af78SStefan Berger rc = tpm_getcap(chip, TPM_CAP_PROP_TIS_TIMEOUT, &cap, NULL, 616c659af78SStefan Berger sizeof(cap.timeout)); 6179deb0eb7SJason Gunthorpe if (rc == TPM_ERR_INVALID_POSTINIT) { 6189deb0eb7SJason Gunthorpe /* The TPM is not started, we are the first to talk to it. 6199deb0eb7SJason Gunthorpe Execute a startup command. */ 620aaa6f7f6SEd Swierk dev_info(&chip->dev, "Issuing TPM_STARTUP\n"); 6219deb0eb7SJason Gunthorpe if (tpm_startup(chip, TPM_ST_CLEAR)) 6229deb0eb7SJason Gunthorpe return rc; 6239deb0eb7SJason Gunthorpe 624aaa6f7f6SEd Swierk rc = tpm_getcap(chip, TPM_CAP_PROP_TIS_TIMEOUT, &cap, 625c659af78SStefan Berger "attempting to determine the timeouts", 626c659af78SStefan Berger sizeof(cap.timeout)); 6279deb0eb7SJason Gunthorpe } 628c659af78SStefan Berger 62962bfdacbSJason Gunthorpe if (rc) { 63062bfdacbSJason Gunthorpe dev_err(&chip->dev, 63162bfdacbSJason Gunthorpe "A TPM error (%zd) occurred attempting to determine the timeouts\n", 63262bfdacbSJason Gunthorpe rc); 633aaa6f7f6SEd Swierk return rc; 63462bfdacbSJason Gunthorpe } 6359deb0eb7SJason Gunthorpe 6361d70fe9dSMaciej S. Szmigiero timeout_old[0] = jiffies_to_usecs(chip->timeout_a); 6371d70fe9dSMaciej S. Szmigiero timeout_old[1] = jiffies_to_usecs(chip->timeout_b); 6381d70fe9dSMaciej S. Szmigiero timeout_old[2] = jiffies_to_usecs(chip->timeout_c); 6391d70fe9dSMaciej S. Szmigiero timeout_old[3] = jiffies_to_usecs(chip->timeout_d); 6401d70fe9dSMaciej S. Szmigiero timeout_chip[0] = be32_to_cpu(cap.timeout.a); 6411d70fe9dSMaciej S. Szmigiero timeout_chip[1] = be32_to_cpu(cap.timeout.b); 6421d70fe9dSMaciej S. Szmigiero timeout_chip[2] = be32_to_cpu(cap.timeout.c); 6431d70fe9dSMaciej S. Szmigiero timeout_chip[3] = be32_to_cpu(cap.timeout.d); 6441d70fe9dSMaciej S. Szmigiero memcpy(timeout_eff, timeout_chip, sizeof(timeout_eff)); 6458e54caf4SJason Gunthorpe 6468e54caf4SJason Gunthorpe /* 6478e54caf4SJason Gunthorpe * Provide ability for vendor overrides of timeout values in case 6488e54caf4SJason Gunthorpe * of misreporting. 6498e54caf4SJason Gunthorpe */ 6508e54caf4SJason Gunthorpe if (chip->ops->update_timeouts != NULL) 651af782f33SChristophe Ricard chip->timeout_adjusted = 6521d70fe9dSMaciej S. Szmigiero chip->ops->update_timeouts(chip, timeout_eff); 6538e54caf4SJason Gunthorpe 654af782f33SChristophe Ricard if (!chip->timeout_adjusted) { 6551d70fe9dSMaciej S. Szmigiero /* Restore default if chip reported 0 */ 6568e54caf4SJason Gunthorpe int i; 6578e54caf4SJason Gunthorpe 6581d70fe9dSMaciej S. Szmigiero for (i = 0; i < ARRAY_SIZE(timeout_eff); i++) { 6591d70fe9dSMaciej S. Szmigiero if (timeout_eff[i]) 6601d70fe9dSMaciej S. Szmigiero continue; 6611d70fe9dSMaciej S. Szmigiero 6621d70fe9dSMaciej S. Szmigiero timeout_eff[i] = timeout_old[i]; 6631d70fe9dSMaciej S. Szmigiero chip->timeout_adjusted = true; 6641d70fe9dSMaciej S. Szmigiero } 6651d70fe9dSMaciej S. Szmigiero 6661d70fe9dSMaciej S. Szmigiero if (timeout_eff[0] != 0 && timeout_eff[0] < 1000) { 6679deb0eb7SJason Gunthorpe /* timeouts in msec rather usec */ 6681d70fe9dSMaciej S. Szmigiero for (i = 0; i != ARRAY_SIZE(timeout_eff); i++) 6691d70fe9dSMaciej S. Szmigiero timeout_eff[i] *= 1000; 670af782f33SChristophe Ricard chip->timeout_adjusted = true; 6719deb0eb7SJason Gunthorpe } 6728e54caf4SJason Gunthorpe } 6738e54caf4SJason Gunthorpe 6748e54caf4SJason Gunthorpe /* Report adjusted timeouts */ 675af782f33SChristophe Ricard if (chip->timeout_adjusted) { 6768cfffc9dSJason Gunthorpe dev_info(&chip->dev, 6778e54caf4SJason Gunthorpe HW_ERR "Adjusting reported timeouts: A %lu->%luus B %lu->%luus C %lu->%luus D %lu->%luus\n", 6781d70fe9dSMaciej S. Szmigiero timeout_chip[0], timeout_eff[0], 6791d70fe9dSMaciej S. Szmigiero timeout_chip[1], timeout_eff[1], 6801d70fe9dSMaciej S. Szmigiero timeout_chip[2], timeout_eff[2], 6811d70fe9dSMaciej S. Szmigiero timeout_chip[3], timeout_eff[3]); 6828e54caf4SJason Gunthorpe } 6838e54caf4SJason Gunthorpe 6841d70fe9dSMaciej S. Szmigiero chip->timeout_a = usecs_to_jiffies(timeout_eff[0]); 6851d70fe9dSMaciej S. Szmigiero chip->timeout_b = usecs_to_jiffies(timeout_eff[1]); 6861d70fe9dSMaciej S. Szmigiero chip->timeout_c = usecs_to_jiffies(timeout_eff[2]); 6871d70fe9dSMaciej S. Szmigiero chip->timeout_d = usecs_to_jiffies(timeout_eff[3]); 6889deb0eb7SJason Gunthorpe 689aaa6f7f6SEd Swierk rc = tpm_getcap(chip, TPM_CAP_PROP_TIS_DURATION, &cap, 690c659af78SStefan Berger "attempting to determine the durations", 691c659af78SStefan Berger sizeof(cap.duration)); 6929deb0eb7SJason Gunthorpe if (rc) 6939deb0eb7SJason Gunthorpe return rc; 6949deb0eb7SJason Gunthorpe 695af782f33SChristophe Ricard chip->duration[TPM_SHORT] = 696aaa6f7f6SEd Swierk usecs_to_jiffies(be32_to_cpu(cap.duration.tpm_short)); 697af782f33SChristophe Ricard chip->duration[TPM_MEDIUM] = 698aaa6f7f6SEd Swierk usecs_to_jiffies(be32_to_cpu(cap.duration.tpm_medium)); 699af782f33SChristophe Ricard chip->duration[TPM_LONG] = 700aaa6f7f6SEd Swierk usecs_to_jiffies(be32_to_cpu(cap.duration.tpm_long)); 7019deb0eb7SJason Gunthorpe 7029deb0eb7SJason Gunthorpe /* The Broadcom BCM0102 chipset in a Dell Latitude D820 gets the above 7039deb0eb7SJason Gunthorpe * value wrong and apparently reports msecs rather than usecs. So we 7049deb0eb7SJason Gunthorpe * fix up the resulting too-small TPM_SHORT value to make things work. 7059deb0eb7SJason Gunthorpe * We also scale the TPM_MEDIUM and -_LONG values by 1000. 7069deb0eb7SJason Gunthorpe */ 707af782f33SChristophe Ricard if (chip->duration[TPM_SHORT] < (HZ / 100)) { 708af782f33SChristophe Ricard chip->duration[TPM_SHORT] = HZ; 709af782f33SChristophe Ricard chip->duration[TPM_MEDIUM] *= 1000; 710af782f33SChristophe Ricard chip->duration[TPM_LONG] *= 1000; 711af782f33SChristophe Ricard chip->duration_adjusted = true; 7128cfffc9dSJason Gunthorpe dev_info(&chip->dev, "Adjusting TPM timeout parameters."); 7139deb0eb7SJason Gunthorpe } 714d1d253cfSJason Gunthorpe 715d1d253cfSJason Gunthorpe chip->flags |= TPM_CHIP_FLAG_HAVE_TIMEOUTS; 7169deb0eb7SJason Gunthorpe return 0; 7179deb0eb7SJason Gunthorpe } 7189deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_get_timeouts); 7199deb0eb7SJason Gunthorpe 7209deb0eb7SJason Gunthorpe #define TPM_ORD_CONTINUE_SELFTEST 83 7219deb0eb7SJason Gunthorpe #define CONTINUE_SELFTEST_RESULT_SIZE 10 7229deb0eb7SJason Gunthorpe 7230014777fSJulia Lawall static const struct tpm_input_header continue_selftest_header = { 7249deb0eb7SJason Gunthorpe .tag = TPM_TAG_RQU_COMMAND, 7259deb0eb7SJason Gunthorpe .length = cpu_to_be32(10), 7269deb0eb7SJason Gunthorpe .ordinal = cpu_to_be32(TPM_ORD_CONTINUE_SELFTEST), 7279deb0eb7SJason Gunthorpe }; 7289deb0eb7SJason Gunthorpe 7299deb0eb7SJason Gunthorpe /** 7309deb0eb7SJason Gunthorpe * tpm_continue_selftest -- run TPM's selftest 7319deb0eb7SJason Gunthorpe * @chip: TPM chip to use 7329deb0eb7SJason Gunthorpe * 7339deb0eb7SJason Gunthorpe * Returns 0 on success, < 0 in case of fatal error or a value > 0 representing 7349deb0eb7SJason Gunthorpe * a TPM error code. 7359deb0eb7SJason Gunthorpe */ 7369deb0eb7SJason Gunthorpe static int tpm_continue_selftest(struct tpm_chip *chip) 7379deb0eb7SJason Gunthorpe { 7389deb0eb7SJason Gunthorpe int rc; 7399deb0eb7SJason Gunthorpe struct tpm_cmd_t cmd; 7409deb0eb7SJason Gunthorpe 7419deb0eb7SJason Gunthorpe cmd.header.in = continue_selftest_header; 742745b361eSJarkko Sakkinen rc = tpm_transmit_cmd(chip, NULL, &cmd, CONTINUE_SELFTEST_RESULT_SIZE, 743745b361eSJarkko Sakkinen 0, 0, "continue selftest"); 7449deb0eb7SJason Gunthorpe return rc; 7459deb0eb7SJason Gunthorpe } 7469deb0eb7SJason Gunthorpe 7479deb0eb7SJason Gunthorpe #define TPM_ORDINAL_PCRREAD cpu_to_be32(21) 7489deb0eb7SJason Gunthorpe #define READ_PCR_RESULT_SIZE 30 749c659af78SStefan Berger #define READ_PCR_RESULT_BODY_SIZE 20 7500014777fSJulia Lawall static const struct tpm_input_header pcrread_header = { 7519deb0eb7SJason Gunthorpe .tag = TPM_TAG_RQU_COMMAND, 7529deb0eb7SJason Gunthorpe .length = cpu_to_be32(14), 7539deb0eb7SJason Gunthorpe .ordinal = TPM_ORDINAL_PCRREAD 7549deb0eb7SJason Gunthorpe }; 7559deb0eb7SJason Gunthorpe 756000a07b0SJason Gunthorpe int tpm_pcr_read_dev(struct tpm_chip *chip, int pcr_idx, u8 *res_buf) 7579deb0eb7SJason Gunthorpe { 7589deb0eb7SJason Gunthorpe int rc; 7599deb0eb7SJason Gunthorpe struct tpm_cmd_t cmd; 7609deb0eb7SJason Gunthorpe 7619deb0eb7SJason Gunthorpe cmd.header.in = pcrread_header; 7629deb0eb7SJason Gunthorpe cmd.params.pcrread_in.pcr_idx = cpu_to_be32(pcr_idx); 763745b361eSJarkko Sakkinen rc = tpm_transmit_cmd(chip, NULL, &cmd, READ_PCR_RESULT_SIZE, 764c659af78SStefan Berger READ_PCR_RESULT_BODY_SIZE, 0, 7659deb0eb7SJason Gunthorpe "attempting to read a pcr value"); 7669deb0eb7SJason Gunthorpe 7679deb0eb7SJason Gunthorpe if (rc == 0) 7689deb0eb7SJason Gunthorpe memcpy(res_buf, cmd.params.pcrread_out.pcr_result, 7699deb0eb7SJason Gunthorpe TPM_DIGEST_SIZE); 7709deb0eb7SJason Gunthorpe return rc; 7719deb0eb7SJason Gunthorpe } 7729deb0eb7SJason Gunthorpe 7739deb0eb7SJason Gunthorpe /** 774954650efSJarkko Sakkinen * tpm_is_tpm2 - is the chip a TPM2 chip? 775954650efSJarkko Sakkinen * @chip_num: tpm idx # or ANY 776954650efSJarkko Sakkinen * 777954650efSJarkko Sakkinen * Returns < 0 on error, and 1 or 0 on success depending whether the chip 778954650efSJarkko Sakkinen * is a TPM2 chip. 779954650efSJarkko Sakkinen */ 780954650efSJarkko Sakkinen int tpm_is_tpm2(u32 chip_num) 781954650efSJarkko Sakkinen { 782954650efSJarkko Sakkinen struct tpm_chip *chip; 783954650efSJarkko Sakkinen int rc; 784954650efSJarkko Sakkinen 785954650efSJarkko Sakkinen chip = tpm_chip_find_get(chip_num); 786954650efSJarkko Sakkinen if (chip == NULL) 787954650efSJarkko Sakkinen return -ENODEV; 788954650efSJarkko Sakkinen 789954650efSJarkko Sakkinen rc = (chip->flags & TPM_CHIP_FLAG_TPM2) != 0; 790954650efSJarkko Sakkinen 7914e26195fSJason Gunthorpe tpm_put_ops(chip); 792954650efSJarkko Sakkinen 793954650efSJarkko Sakkinen return rc; 794954650efSJarkko Sakkinen } 795954650efSJarkko Sakkinen EXPORT_SYMBOL_GPL(tpm_is_tpm2); 796954650efSJarkko Sakkinen 797954650efSJarkko Sakkinen /** 7989deb0eb7SJason Gunthorpe * tpm_pcr_read - read a pcr value 7999deb0eb7SJason Gunthorpe * @chip_num: tpm idx # or ANY 8009deb0eb7SJason Gunthorpe * @pcr_idx: pcr idx to retrieve 8019deb0eb7SJason Gunthorpe * @res_buf: TPM_PCR value 8029deb0eb7SJason Gunthorpe * size of res_buf is 20 bytes (or NULL if you don't care) 8039deb0eb7SJason Gunthorpe * 8049deb0eb7SJason Gunthorpe * The TPM driver should be built-in, but for whatever reason it 8059deb0eb7SJason Gunthorpe * isn't, protect against the chip disappearing, by incrementing 8069deb0eb7SJason Gunthorpe * the module usage count. 8079deb0eb7SJason Gunthorpe */ 8089deb0eb7SJason Gunthorpe int tpm_pcr_read(u32 chip_num, int pcr_idx, u8 *res_buf) 8099deb0eb7SJason Gunthorpe { 8109deb0eb7SJason Gunthorpe struct tpm_chip *chip; 8119deb0eb7SJason Gunthorpe int rc; 8129deb0eb7SJason Gunthorpe 8139deb0eb7SJason Gunthorpe chip = tpm_chip_find_get(chip_num); 8149deb0eb7SJason Gunthorpe if (chip == NULL) 8159deb0eb7SJason Gunthorpe return -ENODEV; 8167a1d7e6dSJarkko Sakkinen if (chip->flags & TPM_CHIP_FLAG_TPM2) 8177a1d7e6dSJarkko Sakkinen rc = tpm2_pcr_read(chip, pcr_idx, res_buf); 8187a1d7e6dSJarkko Sakkinen else 819000a07b0SJason Gunthorpe rc = tpm_pcr_read_dev(chip, pcr_idx, res_buf); 8204e26195fSJason Gunthorpe tpm_put_ops(chip); 8219deb0eb7SJason Gunthorpe return rc; 8229deb0eb7SJason Gunthorpe } 8239deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_pcr_read); 8249deb0eb7SJason Gunthorpe 825ca6d4580SWinkler, Tomas #define TPM_ORD_PCR_EXTEND cpu_to_be32(20) 826ca6d4580SWinkler, Tomas #define EXTEND_PCR_RESULT_SIZE 34 82751b0be64SStefan Berger #define EXTEND_PCR_RESULT_BODY_SIZE 20 828ca6d4580SWinkler, Tomas static const struct tpm_input_header pcrextend_header = { 829ca6d4580SWinkler, Tomas .tag = TPM_TAG_RQU_COMMAND, 830ca6d4580SWinkler, Tomas .length = cpu_to_be32(34), 831ca6d4580SWinkler, Tomas .ordinal = TPM_ORD_PCR_EXTEND 832ca6d4580SWinkler, Tomas }; 833ca6d4580SWinkler, Tomas 8349deb0eb7SJason Gunthorpe /** 8359deb0eb7SJason Gunthorpe * tpm_pcr_extend - extend pcr value with hash 8369deb0eb7SJason Gunthorpe * @chip_num: tpm idx # or AN& 8379deb0eb7SJason Gunthorpe * @pcr_idx: pcr idx to extend 8389deb0eb7SJason Gunthorpe * @hash: hash value used to extend pcr value 8399deb0eb7SJason Gunthorpe * 8409deb0eb7SJason Gunthorpe * The TPM driver should be built-in, but for whatever reason it 8419deb0eb7SJason Gunthorpe * isn't, protect against the chip disappearing, by incrementing 8429deb0eb7SJason Gunthorpe * the module usage count. 8439deb0eb7SJason Gunthorpe */ 8449deb0eb7SJason Gunthorpe int tpm_pcr_extend(u32 chip_num, int pcr_idx, const u8 *hash) 8459deb0eb7SJason Gunthorpe { 8469deb0eb7SJason Gunthorpe struct tpm_cmd_t cmd; 8479deb0eb7SJason Gunthorpe int rc; 8489deb0eb7SJason Gunthorpe struct tpm_chip *chip; 849c1f92b4bSNayna Jain struct tpm2_digest digest_list[ARRAY_SIZE(chip->active_banks)]; 850c1f92b4bSNayna Jain u32 count = 0; 851c1f92b4bSNayna Jain int i; 8529deb0eb7SJason Gunthorpe 8539deb0eb7SJason Gunthorpe chip = tpm_chip_find_get(chip_num); 8549deb0eb7SJason Gunthorpe if (chip == NULL) 8559deb0eb7SJason Gunthorpe return -ENODEV; 8569deb0eb7SJason Gunthorpe 8577a1d7e6dSJarkko Sakkinen if (chip->flags & TPM_CHIP_FLAG_TPM2) { 858c1f92b4bSNayna Jain memset(digest_list, 0, sizeof(digest_list)); 859c1f92b4bSNayna Jain 86070ea1636SDan Carpenter for (i = 0; i < ARRAY_SIZE(chip->active_banks) && 86170ea1636SDan Carpenter chip->active_banks[i] != TPM2_ALG_ERROR; i++) { 862c1f92b4bSNayna Jain digest_list[i].alg_id = chip->active_banks[i]; 863c1f92b4bSNayna Jain memcpy(digest_list[i].digest, hash, TPM_DIGEST_SIZE); 864c1f92b4bSNayna Jain count++; 865c1f92b4bSNayna Jain } 866c1f92b4bSNayna Jain 867c1f92b4bSNayna Jain rc = tpm2_pcr_extend(chip, pcr_idx, count, digest_list); 8684e26195fSJason Gunthorpe tpm_put_ops(chip); 8697a1d7e6dSJarkko Sakkinen return rc; 8707a1d7e6dSJarkko Sakkinen } 8717a1d7e6dSJarkko Sakkinen 8729deb0eb7SJason Gunthorpe cmd.header.in = pcrextend_header; 8739deb0eb7SJason Gunthorpe cmd.params.pcrextend_in.pcr_idx = cpu_to_be32(pcr_idx); 8749deb0eb7SJason Gunthorpe memcpy(cmd.params.pcrextend_in.hash, hash, TPM_DIGEST_SIZE); 875745b361eSJarkko Sakkinen rc = tpm_transmit_cmd(chip, NULL, &cmd, EXTEND_PCR_RESULT_SIZE, 876c659af78SStefan Berger EXTEND_PCR_RESULT_BODY_SIZE, 0, 8779deb0eb7SJason Gunthorpe "attempting extend a PCR value"); 8789deb0eb7SJason Gunthorpe 8794e26195fSJason Gunthorpe tpm_put_ops(chip); 8809deb0eb7SJason Gunthorpe return rc; 8819deb0eb7SJason Gunthorpe } 8829deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_pcr_extend); 8839deb0eb7SJason Gunthorpe 8849deb0eb7SJason Gunthorpe /** 8859deb0eb7SJason Gunthorpe * tpm_do_selftest - have the TPM continue its selftest and wait until it 8869deb0eb7SJason Gunthorpe * can receive further commands 8879deb0eb7SJason Gunthorpe * @chip: TPM chip to use 8889deb0eb7SJason Gunthorpe * 8899deb0eb7SJason Gunthorpe * Returns 0 on success, < 0 in case of fatal error or a value > 0 representing 8909deb0eb7SJason Gunthorpe * a TPM error code. 8919deb0eb7SJason Gunthorpe */ 8929deb0eb7SJason Gunthorpe int tpm_do_selftest(struct tpm_chip *chip) 8939deb0eb7SJason Gunthorpe { 8949deb0eb7SJason Gunthorpe int rc; 8959deb0eb7SJason Gunthorpe unsigned int loops; 8969deb0eb7SJason Gunthorpe unsigned int delay_msec = 100; 8979deb0eb7SJason Gunthorpe unsigned long duration; 8980c541332SJarkko Sakkinen u8 dummy[TPM_DIGEST_SIZE]; 8999deb0eb7SJason Gunthorpe 9009deb0eb7SJason Gunthorpe duration = tpm_calc_ordinal_duration(chip, TPM_ORD_CONTINUE_SELFTEST); 9019deb0eb7SJason Gunthorpe 9029deb0eb7SJason Gunthorpe loops = jiffies_to_msecs(duration) / delay_msec; 9039deb0eb7SJason Gunthorpe 9049deb0eb7SJason Gunthorpe rc = tpm_continue_selftest(chip); 9059deb0eb7SJason Gunthorpe /* This may fail if there was no TPM driver during a suspend/resume 9069deb0eb7SJason Gunthorpe * cycle; some may return 10 (BAD_ORDINAL), others 28 (FAILEDSELFTEST) 9079deb0eb7SJason Gunthorpe */ 9089deb0eb7SJason Gunthorpe if (rc) 9099deb0eb7SJason Gunthorpe return rc; 9109deb0eb7SJason Gunthorpe 9119deb0eb7SJason Gunthorpe do { 9129deb0eb7SJason Gunthorpe /* Attempt to read a PCR value */ 9130c541332SJarkko Sakkinen rc = tpm_pcr_read_dev(chip, 0, dummy); 9140c541332SJarkko Sakkinen 9159deb0eb7SJason Gunthorpe /* Some buggy TPMs will not respond to tpm_tis_ready() for 9169deb0eb7SJason Gunthorpe * around 300ms while the self test is ongoing, keep trying 9179deb0eb7SJason Gunthorpe * until the self test duration expires. */ 9189deb0eb7SJason Gunthorpe if (rc == -ETIME) { 9198cfffc9dSJason Gunthorpe dev_info( 9208cfffc9dSJason Gunthorpe &chip->dev, HW_ERR 9218cfffc9dSJason Gunthorpe "TPM command timed out during continue self test"); 9229deb0eb7SJason Gunthorpe msleep(delay_msec); 9239deb0eb7SJason Gunthorpe continue; 9249deb0eb7SJason Gunthorpe } 9259deb0eb7SJason Gunthorpe 9269deb0eb7SJason Gunthorpe if (rc == TPM_ERR_DISABLED || rc == TPM_ERR_DEACTIVATED) { 9278cfffc9dSJason Gunthorpe dev_info(&chip->dev, 9289deb0eb7SJason Gunthorpe "TPM is disabled/deactivated (0x%X)\n", rc); 9299deb0eb7SJason Gunthorpe /* TPM is disabled and/or deactivated; driver can 9309deb0eb7SJason Gunthorpe * proceed and TPM does handle commands for 9319deb0eb7SJason Gunthorpe * suspend/resume correctly 9329deb0eb7SJason Gunthorpe */ 9339deb0eb7SJason Gunthorpe return 0; 9349deb0eb7SJason Gunthorpe } 9359deb0eb7SJason Gunthorpe if (rc != TPM_WARN_DOING_SELFTEST) 9369deb0eb7SJason Gunthorpe return rc; 9379deb0eb7SJason Gunthorpe msleep(delay_msec); 9389deb0eb7SJason Gunthorpe } while (--loops > 0); 9399deb0eb7SJason Gunthorpe 9409deb0eb7SJason Gunthorpe return rc; 9419deb0eb7SJason Gunthorpe } 9429deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_do_selftest); 9439deb0eb7SJason Gunthorpe 944cae8b441SJason Gunthorpe /** 945cae8b441SJason Gunthorpe * tpm1_auto_startup - Perform the standard automatic TPM initialization 946cae8b441SJason Gunthorpe * sequence 947cae8b441SJason Gunthorpe * @chip: TPM chip to use 948cae8b441SJason Gunthorpe * 949cae8b441SJason Gunthorpe * Returns 0 on success, < 0 in case of fatal error. 950cae8b441SJason Gunthorpe */ 951cae8b441SJason Gunthorpe int tpm1_auto_startup(struct tpm_chip *chip) 952cae8b441SJason Gunthorpe { 953cae8b441SJason Gunthorpe int rc; 954cae8b441SJason Gunthorpe 955cae8b441SJason Gunthorpe rc = tpm_get_timeouts(chip); 956cae8b441SJason Gunthorpe if (rc) 957cae8b441SJason Gunthorpe goto out; 958cae8b441SJason Gunthorpe rc = tpm_do_selftest(chip); 959cae8b441SJason Gunthorpe if (rc) { 960cae8b441SJason Gunthorpe dev_err(&chip->dev, "TPM self test failed\n"); 961cae8b441SJason Gunthorpe goto out; 962cae8b441SJason Gunthorpe } 963cae8b441SJason Gunthorpe 964cae8b441SJason Gunthorpe return rc; 965cae8b441SJason Gunthorpe out: 966cae8b441SJason Gunthorpe if (rc > 0) 967cae8b441SJason Gunthorpe rc = -ENODEV; 968cae8b441SJason Gunthorpe return rc; 969cae8b441SJason Gunthorpe } 970cae8b441SJason Gunthorpe 9719deb0eb7SJason Gunthorpe int tpm_send(u32 chip_num, void *cmd, size_t buflen) 9729deb0eb7SJason Gunthorpe { 9739deb0eb7SJason Gunthorpe struct tpm_chip *chip; 9749deb0eb7SJason Gunthorpe int rc; 9759deb0eb7SJason Gunthorpe 9769deb0eb7SJason Gunthorpe chip = tpm_chip_find_get(chip_num); 9779deb0eb7SJason Gunthorpe if (chip == NULL) 9789deb0eb7SJason Gunthorpe return -ENODEV; 9799deb0eb7SJason Gunthorpe 980745b361eSJarkko Sakkinen rc = tpm_transmit_cmd(chip, NULL, cmd, buflen, 0, 0, 981745b361eSJarkko Sakkinen "attempting tpm_cmd"); 9824e26195fSJason Gunthorpe tpm_put_ops(chip); 9839deb0eb7SJason Gunthorpe return rc; 9849deb0eb7SJason Gunthorpe } 9859deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_send); 9869deb0eb7SJason Gunthorpe 9879deb0eb7SJason Gunthorpe static bool wait_for_tpm_stat_cond(struct tpm_chip *chip, u8 mask, 9889deb0eb7SJason Gunthorpe bool check_cancel, bool *canceled) 9899deb0eb7SJason Gunthorpe { 9905f82e9f0SJason Gunthorpe u8 status = chip->ops->status(chip); 9919deb0eb7SJason Gunthorpe 9929deb0eb7SJason Gunthorpe *canceled = false; 9939deb0eb7SJason Gunthorpe if ((status & mask) == mask) 9949deb0eb7SJason Gunthorpe return true; 9955f82e9f0SJason Gunthorpe if (check_cancel && chip->ops->req_canceled(chip, status)) { 9969deb0eb7SJason Gunthorpe *canceled = true; 9979deb0eb7SJason Gunthorpe return true; 9989deb0eb7SJason Gunthorpe } 9999deb0eb7SJason Gunthorpe return false; 10009deb0eb7SJason Gunthorpe } 10019deb0eb7SJason Gunthorpe 10029deb0eb7SJason Gunthorpe int wait_for_tpm_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout, 10039deb0eb7SJason Gunthorpe wait_queue_head_t *queue, bool check_cancel) 10049deb0eb7SJason Gunthorpe { 10059deb0eb7SJason Gunthorpe unsigned long stop; 10069deb0eb7SJason Gunthorpe long rc; 10079deb0eb7SJason Gunthorpe u8 status; 10089deb0eb7SJason Gunthorpe bool canceled = false; 10099deb0eb7SJason Gunthorpe 10109deb0eb7SJason Gunthorpe /* check current status */ 10115f82e9f0SJason Gunthorpe status = chip->ops->status(chip); 10129deb0eb7SJason Gunthorpe if ((status & mask) == mask) 10139deb0eb7SJason Gunthorpe return 0; 10149deb0eb7SJason Gunthorpe 10159deb0eb7SJason Gunthorpe stop = jiffies + timeout; 10169deb0eb7SJason Gunthorpe 1017570a3609SChristophe Ricard if (chip->flags & TPM_CHIP_FLAG_IRQ) { 10189deb0eb7SJason Gunthorpe again: 10199deb0eb7SJason Gunthorpe timeout = stop - jiffies; 10209deb0eb7SJason Gunthorpe if ((long)timeout <= 0) 10219deb0eb7SJason Gunthorpe return -ETIME; 10229deb0eb7SJason Gunthorpe rc = wait_event_interruptible_timeout(*queue, 10239deb0eb7SJason Gunthorpe wait_for_tpm_stat_cond(chip, mask, check_cancel, 10249deb0eb7SJason Gunthorpe &canceled), 10259deb0eb7SJason Gunthorpe timeout); 10269deb0eb7SJason Gunthorpe if (rc > 0) { 10279deb0eb7SJason Gunthorpe if (canceled) 10289deb0eb7SJason Gunthorpe return -ECANCELED; 10299deb0eb7SJason Gunthorpe return 0; 10309deb0eb7SJason Gunthorpe } 10319deb0eb7SJason Gunthorpe if (rc == -ERESTARTSYS && freezing(current)) { 10329deb0eb7SJason Gunthorpe clear_thread_flag(TIF_SIGPENDING); 10339deb0eb7SJason Gunthorpe goto again; 10349deb0eb7SJason Gunthorpe } 10359deb0eb7SJason Gunthorpe } else { 10369deb0eb7SJason Gunthorpe do { 10379deb0eb7SJason Gunthorpe msleep(TPM_TIMEOUT); 10385f82e9f0SJason Gunthorpe status = chip->ops->status(chip); 10399deb0eb7SJason Gunthorpe if ((status & mask) == mask) 10409deb0eb7SJason Gunthorpe return 0; 10419deb0eb7SJason Gunthorpe } while (time_before(jiffies, stop)); 10429deb0eb7SJason Gunthorpe } 10439deb0eb7SJason Gunthorpe return -ETIME; 10449deb0eb7SJason Gunthorpe } 10459deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(wait_for_tpm_stat); 10469deb0eb7SJason Gunthorpe 10479deb0eb7SJason Gunthorpe #define TPM_ORD_SAVESTATE cpu_to_be32(152) 10489deb0eb7SJason Gunthorpe #define SAVESTATE_RESULT_SIZE 10 10499deb0eb7SJason Gunthorpe 10500014777fSJulia Lawall static const struct tpm_input_header savestate_header = { 10519deb0eb7SJason Gunthorpe .tag = TPM_TAG_RQU_COMMAND, 10529deb0eb7SJason Gunthorpe .length = cpu_to_be32(10), 10539deb0eb7SJason Gunthorpe .ordinal = TPM_ORD_SAVESTATE 10549deb0eb7SJason Gunthorpe }; 10559deb0eb7SJason Gunthorpe 10569deb0eb7SJason Gunthorpe /* 10579deb0eb7SJason Gunthorpe * We are about to suspend. Save the TPM state 10589deb0eb7SJason Gunthorpe * so that it can be restored. 10599deb0eb7SJason Gunthorpe */ 10609deb0eb7SJason Gunthorpe int tpm_pm_suspend(struct device *dev) 10619deb0eb7SJason Gunthorpe { 1062ec03c50bSStefan Berger struct tpm_chip *chip = dev_get_drvdata(dev); 10639deb0eb7SJason Gunthorpe struct tpm_cmd_t cmd; 10649deb0eb7SJason Gunthorpe int rc, try; 10659deb0eb7SJason Gunthorpe 10669deb0eb7SJason Gunthorpe u8 dummy_hash[TPM_DIGEST_SIZE] = { 0 }; 10679deb0eb7SJason Gunthorpe 10689deb0eb7SJason Gunthorpe if (chip == NULL) 10699deb0eb7SJason Gunthorpe return -ENODEV; 10709deb0eb7SJason Gunthorpe 107174d6b3ceSJarkko Sakkinen if (chip->flags & TPM_CHIP_FLAG_TPM2) { 107274d6b3ceSJarkko Sakkinen tpm2_shutdown(chip, TPM2_SU_STATE); 107374d6b3ceSJarkko Sakkinen return 0; 107474d6b3ceSJarkko Sakkinen } 107530fc8d13SJarkko Sakkinen 10769deb0eb7SJason Gunthorpe /* for buggy tpm, flush pcrs with extend to selected dummy */ 10779deb0eb7SJason Gunthorpe if (tpm_suspend_pcr) { 10789deb0eb7SJason Gunthorpe cmd.header.in = pcrextend_header; 10799deb0eb7SJason Gunthorpe cmd.params.pcrextend_in.pcr_idx = cpu_to_be32(tpm_suspend_pcr); 10809deb0eb7SJason Gunthorpe memcpy(cmd.params.pcrextend_in.hash, dummy_hash, 10819deb0eb7SJason Gunthorpe TPM_DIGEST_SIZE); 1082745b361eSJarkko Sakkinen rc = tpm_transmit_cmd(chip, NULL, &cmd, EXTEND_PCR_RESULT_SIZE, 1083c659af78SStefan Berger EXTEND_PCR_RESULT_BODY_SIZE, 0, 10849deb0eb7SJason Gunthorpe "extending dummy pcr before suspend"); 10859deb0eb7SJason Gunthorpe } 10869deb0eb7SJason Gunthorpe 10879deb0eb7SJason Gunthorpe /* now do the actual savestate */ 10889deb0eb7SJason Gunthorpe for (try = 0; try < TPM_RETRY; try++) { 10899deb0eb7SJason Gunthorpe cmd.header.in = savestate_header; 1090745b361eSJarkko Sakkinen rc = tpm_transmit_cmd(chip, NULL, &cmd, SAVESTATE_RESULT_SIZE, 1091745b361eSJarkko Sakkinen 0, 0, NULL); 10929deb0eb7SJason Gunthorpe 10939deb0eb7SJason Gunthorpe /* 10949deb0eb7SJason Gunthorpe * If the TPM indicates that it is too busy to respond to 10959deb0eb7SJason Gunthorpe * this command then retry before giving up. It can take 10969deb0eb7SJason Gunthorpe * several seconds for this TPM to be ready. 10979deb0eb7SJason Gunthorpe * 10989deb0eb7SJason Gunthorpe * This can happen if the TPM has already been sent the 10999deb0eb7SJason Gunthorpe * SaveState command before the driver has loaded. TCG 1.2 11009deb0eb7SJason Gunthorpe * specification states that any communication after SaveState 11019deb0eb7SJason Gunthorpe * may cause the TPM to invalidate previously saved state. 11029deb0eb7SJason Gunthorpe */ 11039deb0eb7SJason Gunthorpe if (rc != TPM_WARN_RETRY) 11049deb0eb7SJason Gunthorpe break; 11059deb0eb7SJason Gunthorpe msleep(TPM_TIMEOUT_RETRY); 11069deb0eb7SJason Gunthorpe } 11079deb0eb7SJason Gunthorpe 11089deb0eb7SJason Gunthorpe if (rc) 11098cfffc9dSJason Gunthorpe dev_err(&chip->dev, 11109deb0eb7SJason Gunthorpe "Error (%d) sending savestate before suspend\n", rc); 11119deb0eb7SJason Gunthorpe else if (try > 0) 11128cfffc9dSJason Gunthorpe dev_warn(&chip->dev, "TPM savestate took %dms\n", 11139deb0eb7SJason Gunthorpe try * TPM_TIMEOUT_RETRY); 11149deb0eb7SJason Gunthorpe 11159deb0eb7SJason Gunthorpe return rc; 11169deb0eb7SJason Gunthorpe } 11179deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_pm_suspend); 11189deb0eb7SJason Gunthorpe 11199deb0eb7SJason Gunthorpe /* 11209deb0eb7SJason Gunthorpe * Resume from a power safe. The BIOS already restored 11219deb0eb7SJason Gunthorpe * the TPM state. 11229deb0eb7SJason Gunthorpe */ 11239deb0eb7SJason Gunthorpe int tpm_pm_resume(struct device *dev) 11249deb0eb7SJason Gunthorpe { 1125ec03c50bSStefan Berger struct tpm_chip *chip = dev_get_drvdata(dev); 11269deb0eb7SJason Gunthorpe 11279deb0eb7SJason Gunthorpe if (chip == NULL) 11289deb0eb7SJason Gunthorpe return -ENODEV; 11299deb0eb7SJason Gunthorpe 11309deb0eb7SJason Gunthorpe return 0; 11319deb0eb7SJason Gunthorpe } 11329deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_pm_resume); 11339deb0eb7SJason Gunthorpe 11349deb0eb7SJason Gunthorpe #define TPM_GETRANDOM_RESULT_SIZE 18 11350014777fSJulia Lawall static const struct tpm_input_header tpm_getrandom_header = { 11369deb0eb7SJason Gunthorpe .tag = TPM_TAG_RQU_COMMAND, 11379deb0eb7SJason Gunthorpe .length = cpu_to_be32(14), 11389deb0eb7SJason Gunthorpe .ordinal = TPM_ORD_GET_RANDOM 11399deb0eb7SJason Gunthorpe }; 11409deb0eb7SJason Gunthorpe 11419deb0eb7SJason Gunthorpe /** 11429deb0eb7SJason Gunthorpe * tpm_get_random() - Get random bytes from the tpm's RNG 11439deb0eb7SJason Gunthorpe * @chip_num: A specific chip number for the request or TPM_ANY_NUM 11449deb0eb7SJason Gunthorpe * @out: destination buffer for the random bytes 11459deb0eb7SJason Gunthorpe * @max: the max number of bytes to write to @out 11469deb0eb7SJason Gunthorpe * 11479deb0eb7SJason Gunthorpe * Returns < 0 on error and the number of bytes read on success 11489deb0eb7SJason Gunthorpe */ 11499deb0eb7SJason Gunthorpe int tpm_get_random(u32 chip_num, u8 *out, size_t max) 11509deb0eb7SJason Gunthorpe { 11519deb0eb7SJason Gunthorpe struct tpm_chip *chip; 11529deb0eb7SJason Gunthorpe struct tpm_cmd_t tpm_cmd; 1153c659af78SStefan Berger u32 recd, num_bytes = min_t(u32, max, TPM_MAX_RNG_DATA), rlength; 11549deb0eb7SJason Gunthorpe int err, total = 0, retries = 5; 11559deb0eb7SJason Gunthorpe u8 *dest = out; 11569deb0eb7SJason Gunthorpe 11573e14d83eSJarkko Sakkinen if (!out || !num_bytes || max > TPM_MAX_RNG_DATA) 11583e14d83eSJarkko Sakkinen return -EINVAL; 11593e14d83eSJarkko Sakkinen 11609deb0eb7SJason Gunthorpe chip = tpm_chip_find_get(chip_num); 11619deb0eb7SJason Gunthorpe if (chip == NULL) 11629deb0eb7SJason Gunthorpe return -ENODEV; 11639deb0eb7SJason Gunthorpe 11647a1d7e6dSJarkko Sakkinen if (chip->flags & TPM_CHIP_FLAG_TPM2) { 11657a1d7e6dSJarkko Sakkinen err = tpm2_get_random(chip, out, max); 11664e26195fSJason Gunthorpe tpm_put_ops(chip); 11677a1d7e6dSJarkko Sakkinen return err; 11687a1d7e6dSJarkko Sakkinen } 11697a1d7e6dSJarkko Sakkinen 11709deb0eb7SJason Gunthorpe do { 11719deb0eb7SJason Gunthorpe tpm_cmd.header.in = tpm_getrandom_header; 11729deb0eb7SJason Gunthorpe tpm_cmd.params.getrandom_in.num_bytes = cpu_to_be32(num_bytes); 11739deb0eb7SJason Gunthorpe 1174745b361eSJarkko Sakkinen err = tpm_transmit_cmd(chip, NULL, &tpm_cmd, 11759deb0eb7SJason Gunthorpe TPM_GETRANDOM_RESULT_SIZE + num_bytes, 1176c659af78SStefan Berger offsetof(struct tpm_getrandom_out, 1177c659af78SStefan Berger rng_data), 1178d4816edfSJarkko Sakkinen 0, "attempting get random"); 11799deb0eb7SJason Gunthorpe if (err) 11809deb0eb7SJason Gunthorpe break; 11819deb0eb7SJason Gunthorpe 11829deb0eb7SJason Gunthorpe recd = be32_to_cpu(tpm_cmd.params.getrandom_out.rng_data_len); 1183c659af78SStefan Berger 1184c659af78SStefan Berger rlength = be32_to_cpu(tpm_cmd.header.out.length); 1185c659af78SStefan Berger if (rlength < offsetof(struct tpm_getrandom_out, rng_data) + 1186c659af78SStefan Berger recd) { 1187c659af78SStefan Berger total = -EFAULT; 1188c659af78SStefan Berger break; 1189c659af78SStefan Berger } 11909deb0eb7SJason Gunthorpe memcpy(dest, tpm_cmd.params.getrandom_out.rng_data, recd); 11919deb0eb7SJason Gunthorpe 11929deb0eb7SJason Gunthorpe dest += recd; 11939deb0eb7SJason Gunthorpe total += recd; 11949deb0eb7SJason Gunthorpe num_bytes -= recd; 11959deb0eb7SJason Gunthorpe } while (retries-- && total < max); 11969deb0eb7SJason Gunthorpe 11974e26195fSJason Gunthorpe tpm_put_ops(chip); 11989deb0eb7SJason Gunthorpe return total ? total : -EIO; 11999deb0eb7SJason Gunthorpe } 12009deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_get_random); 12019deb0eb7SJason Gunthorpe 1202954650efSJarkko Sakkinen /** 1203954650efSJarkko Sakkinen * tpm_seal_trusted() - seal a trusted key 1204954650efSJarkko Sakkinen * @chip_num: A specific chip number for the request or TPM_ANY_NUM 1205954650efSJarkko Sakkinen * @options: authentication values and other options 1206954650efSJarkko Sakkinen * @payload: the key data in clear and encrypted form 1207954650efSJarkko Sakkinen * 1208954650efSJarkko Sakkinen * Returns < 0 on error and 0 on success. At the moment, only TPM 2.0 chips 1209954650efSJarkko Sakkinen * are supported. 1210954650efSJarkko Sakkinen */ 1211954650efSJarkko Sakkinen int tpm_seal_trusted(u32 chip_num, struct trusted_key_payload *payload, 1212954650efSJarkko Sakkinen struct trusted_key_options *options) 1213954650efSJarkko Sakkinen { 1214954650efSJarkko Sakkinen struct tpm_chip *chip; 1215954650efSJarkko Sakkinen int rc; 1216954650efSJarkko Sakkinen 1217954650efSJarkko Sakkinen chip = tpm_chip_find_get(chip_num); 1218954650efSJarkko Sakkinen if (chip == NULL || !(chip->flags & TPM_CHIP_FLAG_TPM2)) 1219954650efSJarkko Sakkinen return -ENODEV; 1220954650efSJarkko Sakkinen 1221954650efSJarkko Sakkinen rc = tpm2_seal_trusted(chip, payload, options); 1222954650efSJarkko Sakkinen 12234e26195fSJason Gunthorpe tpm_put_ops(chip); 1224954650efSJarkko Sakkinen return rc; 1225954650efSJarkko Sakkinen } 1226954650efSJarkko Sakkinen EXPORT_SYMBOL_GPL(tpm_seal_trusted); 1227954650efSJarkko Sakkinen 1228954650efSJarkko Sakkinen /** 1229954650efSJarkko Sakkinen * tpm_unseal_trusted() - unseal a trusted key 1230954650efSJarkko Sakkinen * @chip_num: A specific chip number for the request or TPM_ANY_NUM 1231954650efSJarkko Sakkinen * @options: authentication values and other options 1232954650efSJarkko Sakkinen * @payload: the key data in clear and encrypted form 1233954650efSJarkko Sakkinen * 1234954650efSJarkko Sakkinen * Returns < 0 on error and 0 on success. At the moment, only TPM 2.0 chips 1235954650efSJarkko Sakkinen * are supported. 1236954650efSJarkko Sakkinen */ 1237954650efSJarkko Sakkinen int tpm_unseal_trusted(u32 chip_num, struct trusted_key_payload *payload, 1238954650efSJarkko Sakkinen struct trusted_key_options *options) 1239954650efSJarkko Sakkinen { 1240954650efSJarkko Sakkinen struct tpm_chip *chip; 1241954650efSJarkko Sakkinen int rc; 1242954650efSJarkko Sakkinen 1243954650efSJarkko Sakkinen chip = tpm_chip_find_get(chip_num); 1244954650efSJarkko Sakkinen if (chip == NULL || !(chip->flags & TPM_CHIP_FLAG_TPM2)) 1245954650efSJarkko Sakkinen return -ENODEV; 1246954650efSJarkko Sakkinen 1247954650efSJarkko Sakkinen rc = tpm2_unseal_trusted(chip, payload, options); 1248954650efSJarkko Sakkinen 12494e26195fSJason Gunthorpe tpm_put_ops(chip); 12504e26195fSJason Gunthorpe 1251954650efSJarkko Sakkinen return rc; 1252954650efSJarkko Sakkinen } 1253954650efSJarkko Sakkinen EXPORT_SYMBOL_GPL(tpm_unseal_trusted); 1254954650efSJarkko Sakkinen 1255313d21eeSJarkko Sakkinen static int __init tpm_init(void) 1256313d21eeSJarkko Sakkinen { 1257313d21eeSJarkko Sakkinen int rc; 1258313d21eeSJarkko Sakkinen 1259313d21eeSJarkko Sakkinen tpm_class = class_create(THIS_MODULE, "tpm"); 1260313d21eeSJarkko Sakkinen if (IS_ERR(tpm_class)) { 1261313d21eeSJarkko Sakkinen pr_err("couldn't create tpm class\n"); 1262313d21eeSJarkko Sakkinen return PTR_ERR(tpm_class); 1263313d21eeSJarkko Sakkinen } 1264313d21eeSJarkko Sakkinen 1265*fdc915f7SJames Bottomley tpmrm_class = class_create(THIS_MODULE, "tpmrm"); 1266*fdc915f7SJames Bottomley if (IS_ERR(tpmrm_class)) { 1267*fdc915f7SJames Bottomley pr_err("couldn't create tpmrm class\n"); 1268*fdc915f7SJames Bottomley class_destroy(tpm_class); 1269*fdc915f7SJames Bottomley return PTR_ERR(tpmrm_class); 1270*fdc915f7SJames Bottomley } 1271*fdc915f7SJames Bottomley 1272*fdc915f7SJames Bottomley rc = alloc_chrdev_region(&tpm_devt, 0, 2*TPM_NUM_DEVICES, "tpm"); 1273313d21eeSJarkko Sakkinen if (rc < 0) { 1274313d21eeSJarkko Sakkinen pr_err("tpm: failed to allocate char dev region\n"); 1275*fdc915f7SJames Bottomley class_destroy(tpmrm_class); 1276313d21eeSJarkko Sakkinen class_destroy(tpm_class); 1277313d21eeSJarkko Sakkinen return rc; 1278313d21eeSJarkko Sakkinen } 1279313d21eeSJarkko Sakkinen 1280313d21eeSJarkko Sakkinen return 0; 1281313d21eeSJarkko Sakkinen } 1282313d21eeSJarkko Sakkinen 1283313d21eeSJarkko Sakkinen static void __exit tpm_exit(void) 1284313d21eeSJarkko Sakkinen { 128515516788SStefan Berger idr_destroy(&dev_nums_idr); 1286313d21eeSJarkko Sakkinen class_destroy(tpm_class); 1287*fdc915f7SJames Bottomley class_destroy(tpmrm_class); 1288*fdc915f7SJames Bottomley unregister_chrdev_region(tpm_devt, 2*TPM_NUM_DEVICES); 1289313d21eeSJarkko Sakkinen } 1290313d21eeSJarkko Sakkinen 1291313d21eeSJarkko Sakkinen subsys_initcall(tpm_init); 1292313d21eeSJarkko Sakkinen module_exit(tpm_exit); 1293313d21eeSJarkko Sakkinen 12949deb0eb7SJason Gunthorpe MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)"); 12959deb0eb7SJason Gunthorpe MODULE_DESCRIPTION("TPM Driver"); 12969deb0eb7SJason Gunthorpe MODULE_VERSION("2.0"); 12979deb0eb7SJason Gunthorpe MODULE_LICENSE("GPL"); 1298