Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2004 IBM Corporation |
| 3 | * |
| 4 | * Authors: |
| 5 | * Leendert van Doorn <leendert@watson.ibm.com> |
| 6 | * Dave Safford <safford@watson.ibm.com> |
| 7 | * Reiner Sailer <sailer@watson.ibm.com> |
| 8 | * Kylene Hall <kjhall@us.ibm.com> |
| 9 | * |
Kent Yoder | 8e81cc1 | 2007-08-22 14:01:04 -0700 | [diff] [blame] | 10 | * Maintained by: <tpmdd-devel@lists.sourceforge.net> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | * |
| 12 | * Device driver for TCG/TCPA TPM (trusted platform module). |
Peter Huewe | 0a41826 | 2013-10-22 00:29:14 +0200 | [diff] [blame] | 13 | * Specifications at www.trustedcomputinggroup.org |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | * |
| 15 | * This program is free software; you can redistribute it and/or |
| 16 | * modify it under the terms of the GNU General Public License as |
| 17 | * published by the Free Software Foundation, version 2 of the |
| 18 | * License. |
Peter Huewe | 0a41826 | 2013-10-22 00:29:14 +0200 | [diff] [blame] | 19 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | * Note, the TPM chip is not interrupt driven (only polling) |
| 21 | * and can have very long timeouts (minutes!). Hence the unusual |
Nishanth Aravamudan | 700d8bd | 2005-06-23 22:01:47 -0700 | [diff] [blame] | 22 | * calls to msleep. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | * |
| 24 | */ |
| 25 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <linux/poll.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 27 | #include <linux/slab.h> |
Matthias Kaehlcke | d081d47 | 2007-05-08 00:32:02 -0700 | [diff] [blame] | 28 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <linux/spinlock.h> |
Rajiv Andrade | fd04886 | 2011-09-16 14:39:40 -0300 | [diff] [blame] | 30 | #include <linux/freezer.h> |
Matthias Kaehlcke | d081d47 | 2007-05-08 00:32:02 -0700 | [diff] [blame] | 31 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include "tpm.h" |
Kent Yoder | e5dcd87 | 2012-07-11 10:08:12 -0500 | [diff] [blame] | 33 | #include "tpm_eventlog.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
Kylene Jo Hall | 9e18ee1 | 2006-04-22 02:37:38 -0700 | [diff] [blame] | 35 | enum tpm_duration { |
| 36 | TPM_SHORT = 0, |
| 37 | TPM_MEDIUM = 1, |
| 38 | TPM_LONG = 2, |
| 39 | TPM_UNDEFINED, |
| 40 | }; |
| 41 | |
| 42 | #define TPM_MAX_ORDINAL 243 |
Peter Huewe | 07b133e | 2012-11-16 00:31:29 +0100 | [diff] [blame] | 43 | #define TSC_MAX_ORDINAL 12 |
| 44 | #define TPM_PROTECTED_COMMAND 0x00 |
| 45 | #define TPM_CONNECTION_COMMAND 0x40 |
Kylene Jo Hall | 9e18ee1 | 2006-04-22 02:37:38 -0700 | [diff] [blame] | 46 | |
Dmitry Torokhov | 9b3056c | 2010-10-01 14:16:39 -0700 | [diff] [blame] | 47 | /* |
| 48 | * Bug workaround - some TPM's don't flush the most |
| 49 | * recently changed pcr on suspend, so force the flush |
| 50 | * with an extend to the selected _unused_ non-volatile pcr. |
| 51 | */ |
| 52 | static int tpm_suspend_pcr; |
| 53 | module_param_named(suspend_pcr, tpm_suspend_pcr, uint, 0644); |
| 54 | MODULE_PARM_DESC(suspend_pcr, |
| 55 | "PCR to use for dummy writes to faciltate flush on suspend."); |
| 56 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | static LIST_HEAD(tpm_chip_list); |
| 58 | static DEFINE_SPINLOCK(driver_lock); |
Kylene Jo Hall | 10685a9 | 2006-04-22 02:38:32 -0700 | [diff] [blame] | 59 | static DECLARE_BITMAP(dev_mask, TPM_NUM_DEVICES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | |
Kylene Jo Hall | 9e18ee1 | 2006-04-22 02:37:38 -0700 | [diff] [blame] | 61 | /* |
| 62 | * Array with one entry per ordinal defining the maximum amount |
| 63 | * of time the chip could take to return the result. The ordinal |
| 64 | * designation of short, medium or long is defined in a table in |
| 65 | * TCG Specification TPM Main Part 2 TPM Structures Section 17. The |
| 66 | * values of the SHORT, MEDIUM, and LONG durations are retrieved |
| 67 | * from the chip during initialization with a call to tpm_get_timeouts. |
| 68 | */ |
Kylene Jo Hall | 9e18ee1 | 2006-04-22 02:37:38 -0700 | [diff] [blame] | 69 | static const u8 tpm_ordinal_duration[TPM_MAX_ORDINAL] = { |
| 70 | TPM_UNDEFINED, /* 0 */ |
| 71 | TPM_UNDEFINED, |
| 72 | TPM_UNDEFINED, |
| 73 | TPM_UNDEFINED, |
| 74 | TPM_UNDEFINED, |
| 75 | TPM_UNDEFINED, /* 5 */ |
| 76 | TPM_UNDEFINED, |
| 77 | TPM_UNDEFINED, |
| 78 | TPM_UNDEFINED, |
| 79 | TPM_UNDEFINED, |
| 80 | TPM_SHORT, /* 10 */ |
| 81 | TPM_SHORT, |
| 82 | TPM_MEDIUM, |
| 83 | TPM_LONG, |
| 84 | TPM_LONG, |
| 85 | TPM_MEDIUM, /* 15 */ |
| 86 | TPM_SHORT, |
| 87 | TPM_SHORT, |
| 88 | TPM_MEDIUM, |
| 89 | TPM_LONG, |
| 90 | TPM_SHORT, /* 20 */ |
| 91 | TPM_SHORT, |
| 92 | TPM_MEDIUM, |
| 93 | TPM_MEDIUM, |
| 94 | TPM_MEDIUM, |
| 95 | TPM_SHORT, /* 25 */ |
| 96 | TPM_SHORT, |
| 97 | TPM_MEDIUM, |
| 98 | TPM_SHORT, |
| 99 | TPM_SHORT, |
| 100 | TPM_MEDIUM, /* 30 */ |
| 101 | TPM_LONG, |
| 102 | TPM_MEDIUM, |
| 103 | TPM_SHORT, |
| 104 | TPM_SHORT, |
| 105 | TPM_SHORT, /* 35 */ |
| 106 | TPM_MEDIUM, |
| 107 | TPM_MEDIUM, |
| 108 | TPM_UNDEFINED, |
| 109 | TPM_UNDEFINED, |
| 110 | TPM_MEDIUM, /* 40 */ |
| 111 | TPM_LONG, |
| 112 | TPM_MEDIUM, |
| 113 | TPM_SHORT, |
| 114 | TPM_SHORT, |
| 115 | TPM_SHORT, /* 45 */ |
| 116 | TPM_SHORT, |
| 117 | TPM_SHORT, |
| 118 | TPM_SHORT, |
| 119 | TPM_LONG, |
| 120 | TPM_MEDIUM, /* 50 */ |
| 121 | TPM_MEDIUM, |
| 122 | TPM_UNDEFINED, |
| 123 | TPM_UNDEFINED, |
| 124 | TPM_UNDEFINED, |
| 125 | TPM_UNDEFINED, /* 55 */ |
| 126 | TPM_UNDEFINED, |
| 127 | TPM_UNDEFINED, |
| 128 | TPM_UNDEFINED, |
| 129 | TPM_UNDEFINED, |
| 130 | TPM_MEDIUM, /* 60 */ |
| 131 | TPM_MEDIUM, |
| 132 | TPM_MEDIUM, |
| 133 | TPM_SHORT, |
| 134 | TPM_SHORT, |
| 135 | TPM_MEDIUM, /* 65 */ |
| 136 | TPM_UNDEFINED, |
| 137 | TPM_UNDEFINED, |
| 138 | TPM_UNDEFINED, |
| 139 | TPM_UNDEFINED, |
| 140 | TPM_SHORT, /* 70 */ |
| 141 | TPM_SHORT, |
| 142 | TPM_UNDEFINED, |
| 143 | TPM_UNDEFINED, |
| 144 | TPM_UNDEFINED, |
| 145 | TPM_UNDEFINED, /* 75 */ |
| 146 | TPM_UNDEFINED, |
| 147 | TPM_UNDEFINED, |
| 148 | TPM_UNDEFINED, |
| 149 | TPM_UNDEFINED, |
| 150 | TPM_LONG, /* 80 */ |
| 151 | TPM_UNDEFINED, |
| 152 | TPM_MEDIUM, |
| 153 | TPM_LONG, |
| 154 | TPM_SHORT, |
| 155 | TPM_UNDEFINED, /* 85 */ |
| 156 | TPM_UNDEFINED, |
| 157 | TPM_UNDEFINED, |
| 158 | TPM_UNDEFINED, |
| 159 | TPM_UNDEFINED, |
| 160 | TPM_SHORT, /* 90 */ |
| 161 | TPM_SHORT, |
| 162 | TPM_SHORT, |
| 163 | TPM_SHORT, |
| 164 | TPM_SHORT, |
| 165 | TPM_UNDEFINED, /* 95 */ |
| 166 | TPM_UNDEFINED, |
| 167 | TPM_UNDEFINED, |
| 168 | TPM_UNDEFINED, |
| 169 | TPM_UNDEFINED, |
| 170 | TPM_MEDIUM, /* 100 */ |
| 171 | TPM_SHORT, |
| 172 | TPM_SHORT, |
| 173 | TPM_UNDEFINED, |
| 174 | TPM_UNDEFINED, |
| 175 | TPM_UNDEFINED, /* 105 */ |
| 176 | TPM_UNDEFINED, |
| 177 | TPM_UNDEFINED, |
| 178 | TPM_UNDEFINED, |
| 179 | TPM_UNDEFINED, |
| 180 | TPM_SHORT, /* 110 */ |
| 181 | TPM_SHORT, |
| 182 | TPM_SHORT, |
| 183 | TPM_SHORT, |
| 184 | TPM_SHORT, |
| 185 | TPM_SHORT, /* 115 */ |
| 186 | TPM_SHORT, |
| 187 | TPM_SHORT, |
| 188 | TPM_UNDEFINED, |
| 189 | TPM_UNDEFINED, |
| 190 | TPM_LONG, /* 120 */ |
| 191 | TPM_LONG, |
| 192 | TPM_MEDIUM, |
| 193 | TPM_UNDEFINED, |
| 194 | TPM_SHORT, |
| 195 | TPM_SHORT, /* 125 */ |
| 196 | TPM_SHORT, |
| 197 | TPM_LONG, |
| 198 | TPM_SHORT, |
| 199 | TPM_SHORT, |
| 200 | TPM_SHORT, /* 130 */ |
| 201 | TPM_MEDIUM, |
| 202 | TPM_UNDEFINED, |
| 203 | TPM_SHORT, |
| 204 | TPM_MEDIUM, |
| 205 | TPM_UNDEFINED, /* 135 */ |
| 206 | TPM_UNDEFINED, |
| 207 | TPM_UNDEFINED, |
| 208 | TPM_UNDEFINED, |
| 209 | TPM_UNDEFINED, |
| 210 | TPM_SHORT, /* 140 */ |
| 211 | TPM_SHORT, |
| 212 | TPM_UNDEFINED, |
| 213 | TPM_UNDEFINED, |
| 214 | TPM_UNDEFINED, |
| 215 | TPM_UNDEFINED, /* 145 */ |
| 216 | TPM_UNDEFINED, |
| 217 | TPM_UNDEFINED, |
| 218 | TPM_UNDEFINED, |
| 219 | TPM_UNDEFINED, |
| 220 | TPM_SHORT, /* 150 */ |
| 221 | TPM_MEDIUM, |
| 222 | TPM_MEDIUM, |
| 223 | TPM_SHORT, |
| 224 | TPM_SHORT, |
| 225 | TPM_UNDEFINED, /* 155 */ |
| 226 | TPM_UNDEFINED, |
| 227 | TPM_UNDEFINED, |
| 228 | TPM_UNDEFINED, |
| 229 | TPM_UNDEFINED, |
| 230 | TPM_SHORT, /* 160 */ |
| 231 | TPM_SHORT, |
| 232 | TPM_SHORT, |
| 233 | TPM_SHORT, |
| 234 | TPM_UNDEFINED, |
| 235 | TPM_UNDEFINED, /* 165 */ |
| 236 | TPM_UNDEFINED, |
| 237 | TPM_UNDEFINED, |
| 238 | TPM_UNDEFINED, |
| 239 | TPM_UNDEFINED, |
| 240 | TPM_LONG, /* 170 */ |
| 241 | TPM_UNDEFINED, |
| 242 | TPM_UNDEFINED, |
| 243 | TPM_UNDEFINED, |
| 244 | TPM_UNDEFINED, |
| 245 | TPM_UNDEFINED, /* 175 */ |
| 246 | TPM_UNDEFINED, |
| 247 | TPM_UNDEFINED, |
| 248 | TPM_UNDEFINED, |
| 249 | TPM_UNDEFINED, |
| 250 | TPM_MEDIUM, /* 180 */ |
| 251 | TPM_SHORT, |
| 252 | TPM_MEDIUM, |
| 253 | TPM_MEDIUM, |
| 254 | TPM_MEDIUM, |
| 255 | TPM_MEDIUM, /* 185 */ |
| 256 | TPM_SHORT, |
| 257 | TPM_UNDEFINED, |
| 258 | TPM_UNDEFINED, |
| 259 | TPM_UNDEFINED, |
| 260 | TPM_UNDEFINED, /* 190 */ |
| 261 | TPM_UNDEFINED, |
| 262 | TPM_UNDEFINED, |
| 263 | TPM_UNDEFINED, |
| 264 | TPM_UNDEFINED, |
| 265 | TPM_UNDEFINED, /* 195 */ |
| 266 | TPM_UNDEFINED, |
| 267 | TPM_UNDEFINED, |
| 268 | TPM_UNDEFINED, |
| 269 | TPM_UNDEFINED, |
| 270 | TPM_SHORT, /* 200 */ |
| 271 | TPM_UNDEFINED, |
| 272 | TPM_UNDEFINED, |
| 273 | TPM_UNDEFINED, |
| 274 | TPM_SHORT, |
| 275 | TPM_SHORT, /* 205 */ |
| 276 | TPM_SHORT, |
| 277 | TPM_SHORT, |
| 278 | TPM_SHORT, |
| 279 | TPM_SHORT, |
| 280 | TPM_MEDIUM, /* 210 */ |
| 281 | TPM_UNDEFINED, |
| 282 | TPM_MEDIUM, |
| 283 | TPM_MEDIUM, |
| 284 | TPM_MEDIUM, |
| 285 | TPM_UNDEFINED, /* 215 */ |
| 286 | TPM_MEDIUM, |
| 287 | TPM_UNDEFINED, |
| 288 | TPM_UNDEFINED, |
| 289 | TPM_SHORT, |
| 290 | TPM_SHORT, /* 220 */ |
| 291 | TPM_SHORT, |
| 292 | TPM_SHORT, |
| 293 | TPM_SHORT, |
| 294 | TPM_SHORT, |
| 295 | TPM_UNDEFINED, /* 225 */ |
| 296 | TPM_UNDEFINED, |
| 297 | TPM_UNDEFINED, |
| 298 | TPM_UNDEFINED, |
| 299 | TPM_UNDEFINED, |
| 300 | TPM_SHORT, /* 230 */ |
| 301 | TPM_LONG, |
| 302 | TPM_MEDIUM, |
| 303 | TPM_UNDEFINED, |
| 304 | TPM_UNDEFINED, |
| 305 | TPM_UNDEFINED, /* 235 */ |
| 306 | TPM_UNDEFINED, |
| 307 | TPM_UNDEFINED, |
| 308 | TPM_UNDEFINED, |
| 309 | TPM_UNDEFINED, |
| 310 | TPM_SHORT, /* 240 */ |
| 311 | TPM_UNDEFINED, |
| 312 | TPM_MEDIUM, |
| 313 | }; |
| 314 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | /* |
Kylene Jo Hall | 9e18ee1 | 2006-04-22 02:37:38 -0700 | [diff] [blame] | 316 | * Returns max number of jiffies to wait |
| 317 | */ |
| 318 | unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip, |
| 319 | u32 ordinal) |
| 320 | { |
| 321 | int duration_idx = TPM_UNDEFINED; |
| 322 | int duration = 0; |
Peter Huewe | 07b133e | 2012-11-16 00:31:29 +0100 | [diff] [blame] | 323 | u8 category = (ordinal >> 24) & 0xFF; |
Kylene Jo Hall | 9e18ee1 | 2006-04-22 02:37:38 -0700 | [diff] [blame] | 324 | |
Peter Huewe | 07b133e | 2012-11-16 00:31:29 +0100 | [diff] [blame] | 325 | if ((category == TPM_PROTECTED_COMMAND && ordinal < TPM_MAX_ORDINAL) || |
| 326 | (category == TPM_CONNECTION_COMMAND && ordinal < TSC_MAX_ORDINAL)) |
Kylene Jo Hall | 9e18ee1 | 2006-04-22 02:37:38 -0700 | [diff] [blame] | 327 | duration_idx = tpm_ordinal_duration[ordinal]; |
Kylene Jo Hall | 9e18ee1 | 2006-04-22 02:37:38 -0700 | [diff] [blame] | 328 | |
Linus Torvalds | 8d1dc20 | 2011-03-01 13:23:27 -0800 | [diff] [blame] | 329 | if (duration_idx != TPM_UNDEFINED) |
Kylene Jo Hall | 36b2002 | 2006-04-22 02:38:19 -0700 | [diff] [blame] | 330 | duration = chip->vendor.duration[duration_idx]; |
Linus Torvalds | 8d1dc20 | 2011-03-01 13:23:27 -0800 | [diff] [blame] | 331 | if (duration <= 0) |
Kylene Jo Hall | 9e18ee1 | 2006-04-22 02:37:38 -0700 | [diff] [blame] | 332 | return 2 * 60 * HZ; |
Linus Torvalds | 8d1dc20 | 2011-03-01 13:23:27 -0800 | [diff] [blame] | 333 | else |
| 334 | return duration; |
Kylene Jo Hall | 9e18ee1 | 2006-04-22 02:37:38 -0700 | [diff] [blame] | 335 | } |
| 336 | EXPORT_SYMBOL_GPL(tpm_calc_ordinal_duration); |
| 337 | |
| 338 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | * Internal kernel interface to transmit TPM commands |
| 340 | */ |
Jason Gunthorpe | afdba32 | 2013-11-26 13:30:40 -0700 | [diff] [blame^] | 341 | ssize_t tpm_transmit(struct tpm_chip *chip, const char *buf, |
| 342 | size_t bufsiz) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | { |
Kylene Hall | d9e5b6b | 2005-06-23 22:02:02 -0700 | [diff] [blame] | 344 | ssize_t rc; |
Kylene Jo Hall | 9e18ee1 | 2006-04-22 02:37:38 -0700 | [diff] [blame] | 345 | u32 count, ordinal; |
Nishanth Aravamudan | 700d8bd | 2005-06-23 22:01:47 -0700 | [diff] [blame] | 346 | unsigned long stop; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | |
Peter Huewe | 6b07d30 | 2011-09-15 14:37:43 -0300 | [diff] [blame] | 348 | if (bufsiz > TPM_BUFSIZE) |
| 349 | bufsiz = TPM_BUFSIZE; |
| 350 | |
Kylene Hall | 81179bb | 2005-06-23 22:01:59 -0700 | [diff] [blame] | 351 | count = be32_to_cpu(*((__be32 *) (buf + 2))); |
Kylene Jo Hall | 9e18ee1 | 2006-04-22 02:37:38 -0700 | [diff] [blame] | 352 | ordinal = be32_to_cpu(*((__be32 *) (buf + 6))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | if (count == 0) |
| 354 | return -ENODATA; |
| 355 | if (count > bufsiz) { |
Kylene Jo Hall | e659a3f | 2005-10-30 15:03:24 -0800 | [diff] [blame] | 356 | dev_err(chip->dev, |
Peter Huewe | 0a41826 | 2013-10-22 00:29:14 +0200 | [diff] [blame] | 357 | "invalid count value %x %zx\n", count, bufsiz); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | return -E2BIG; |
| 359 | } |
| 360 | |
Matthias Kaehlcke | d081d47 | 2007-05-08 00:32:02 -0700 | [diff] [blame] | 361 | mutex_lock(&chip->tpm_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | |
Peter Huewe | 0a41826 | 2013-10-22 00:29:14 +0200 | [diff] [blame] | 363 | rc = chip->vendor.send(chip, (u8 *) buf, count); |
| 364 | if (rc < 0) { |
Kylene Jo Hall | e659a3f | 2005-10-30 15:03:24 -0800 | [diff] [blame] | 365 | dev_err(chip->dev, |
Kylene Hall | d9e5b6b | 2005-06-23 22:02:02 -0700 | [diff] [blame] | 366 | "tpm_transmit: tpm_send: error %zd\n", rc); |
| 367 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | } |
| 369 | |
Leendert van Doorn | 27084ef | 2006-04-22 02:38:03 -0700 | [diff] [blame] | 370 | if (chip->vendor.irq) |
| 371 | goto out_recv; |
| 372 | |
Kylene Jo Hall | 9e18ee1 | 2006-04-22 02:37:38 -0700 | [diff] [blame] | 373 | stop = jiffies + tpm_calc_ordinal_duration(chip, ordinal); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | do { |
Kylene Jo Hall | 90dda52 | 2006-04-22 02:37:15 -0700 | [diff] [blame] | 375 | u8 status = chip->vendor.status(chip); |
| 376 | if ((status & chip->vendor.req_complete_mask) == |
| 377 | chip->vendor.req_complete_val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | goto out_recv; |
Kylene Hall | d9e5b6b | 2005-06-23 22:02:02 -0700 | [diff] [blame] | 379 | |
Stefan Berger | 1f86605 | 2013-01-22 13:52:35 -0600 | [diff] [blame] | 380 | if (chip->vendor.req_canceled(chip, status)) { |
Kylene Jo Hall | e659a3f | 2005-10-30 15:03:24 -0800 | [diff] [blame] | 381 | dev_err(chip->dev, "Operation Canceled\n"); |
Kylene Hall | d9e5b6b | 2005-06-23 22:02:02 -0700 | [diff] [blame] | 382 | rc = -ECANCELED; |
| 383 | goto out; |
| 384 | } |
| 385 | |
| 386 | msleep(TPM_TIMEOUT); /* CHECK */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | rmb(); |
Nishanth Aravamudan | 700d8bd | 2005-06-23 22:01:47 -0700 | [diff] [blame] | 388 | } while (time_before(jiffies, stop)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | |
Kylene Jo Hall | 90dda52 | 2006-04-22 02:37:15 -0700 | [diff] [blame] | 390 | chip->vendor.cancel(chip); |
Kylene Jo Hall | e659a3f | 2005-10-30 15:03:24 -0800 | [diff] [blame] | 391 | dev_err(chip->dev, "Operation Timed out\n"); |
Kylene Hall | d9e5b6b | 2005-06-23 22:02:02 -0700 | [diff] [blame] | 392 | rc = -ETIME; |
| 393 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | |
| 395 | out_recv: |
Kylene Jo Hall | 90dda52 | 2006-04-22 02:37:15 -0700 | [diff] [blame] | 396 | rc = chip->vendor.recv(chip, (u8 *) buf, bufsiz); |
Kylene Hall | d9e5b6b | 2005-06-23 22:02:02 -0700 | [diff] [blame] | 397 | if (rc < 0) |
Kylene Jo Hall | e659a3f | 2005-10-30 15:03:24 -0800 | [diff] [blame] | 398 | dev_err(chip->dev, |
Kylene Hall | d9e5b6b | 2005-06-23 22:02:02 -0700 | [diff] [blame] | 399 | "tpm_transmit: tpm_recv: error %zd\n", rc); |
| 400 | out: |
Matthias Kaehlcke | d081d47 | 2007-05-08 00:32:02 -0700 | [diff] [blame] | 401 | mutex_unlock(&chip->tpm_mutex); |
Kylene Hall | d9e5b6b | 2005-06-23 22:02:02 -0700 | [diff] [blame] | 402 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | #define TPM_DIGEST_SIZE 20 |
Kylene Jo Hall | beed53a | 2006-04-22 02:37:05 -0700 | [diff] [blame] | 406 | #define TPM_RET_CODE_IDX 6 |
Kylene Jo Hall | beed53a | 2006-04-22 02:37:05 -0700 | [diff] [blame] | 407 | |
| 408 | enum tpm_capabilities { |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame] | 409 | TPM_CAP_FLAG = cpu_to_be32(4), |
| 410 | TPM_CAP_PROP = cpu_to_be32(5), |
| 411 | CAP_VERSION_1_1 = cpu_to_be32(0x06), |
| 412 | CAP_VERSION_1_2 = cpu_to_be32(0x1A) |
Kylene Jo Hall | beed53a | 2006-04-22 02:37:05 -0700 | [diff] [blame] | 413 | }; |
| 414 | |
| 415 | enum tpm_sub_capabilities { |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame] | 416 | TPM_CAP_PROP_PCR = cpu_to_be32(0x101), |
| 417 | TPM_CAP_PROP_MANUFACTURER = cpu_to_be32(0x103), |
| 418 | TPM_CAP_FLAG_PERM = cpu_to_be32(0x108), |
| 419 | TPM_CAP_FLAG_VOL = cpu_to_be32(0x109), |
| 420 | TPM_CAP_PROP_OWNER = cpu_to_be32(0x111), |
| 421 | TPM_CAP_PROP_TIS_TIMEOUT = cpu_to_be32(0x115), |
| 422 | TPM_CAP_PROP_TIS_DURATION = cpu_to_be32(0x120), |
| 423 | |
Kylene Jo Hall | beed53a | 2006-04-22 02:37:05 -0700 | [diff] [blame] | 424 | }; |
| 425 | |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame] | 426 | static ssize_t transmit_cmd(struct tpm_chip *chip, struct tpm_cmd_t *cmd, |
| 427 | int len, const char *desc) |
Kylene Jo Hall | beed53a | 2006-04-22 02:37:05 -0700 | [diff] [blame] | 428 | { |
| 429 | int err; |
| 430 | |
Peter Huewe | 0a41826 | 2013-10-22 00:29:14 +0200 | [diff] [blame] | 431 | len = tpm_transmit(chip, (u8 *) cmd, len); |
Kylene Jo Hall | beed53a | 2006-04-22 02:37:05 -0700 | [diff] [blame] | 432 | if (len < 0) |
| 433 | return len; |
Rajiv Andrade | b9e3238 | 2011-11-01 17:00:52 -0200 | [diff] [blame] | 434 | else if (len < TPM_HEADER_SIZE) |
| 435 | return -EFAULT; |
| 436 | |
| 437 | err = be32_to_cpu(cmd->header.out.return_code); |
Jason Gunthorpe | c584af1 | 2012-11-21 13:54:33 -0700 | [diff] [blame] | 438 | if (err != 0 && desc) |
Rajiv Andrade | b9e3238 | 2011-11-01 17:00:52 -0200 | [diff] [blame] | 439 | dev_err(chip->dev, "A TPM error (%d) occurred %s\n", err, desc); |
| 440 | |
| 441 | return err; |
Kylene Jo Hall | beed53a | 2006-04-22 02:37:05 -0700 | [diff] [blame] | 442 | } |
| 443 | |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame] | 444 | #define TPM_INTERNAL_RESULT_SIZE 200 |
| 445 | #define TPM_TAG_RQU_COMMAND cpu_to_be16(193) |
| 446 | #define TPM_ORD_GET_CAP cpu_to_be32(101) |
Kent Yoder | 41ab999 | 2012-06-07 13:47:14 -0500 | [diff] [blame] | 447 | #define TPM_ORD_GET_RANDOM cpu_to_be32(70) |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame] | 448 | |
| 449 | static const struct tpm_input_header tpm_getcap_header = { |
| 450 | .tag = TPM_TAG_RQU_COMMAND, |
| 451 | .length = cpu_to_be32(22), |
| 452 | .ordinal = TPM_ORD_GET_CAP |
| 453 | }; |
| 454 | |
| 455 | ssize_t tpm_getcap(struct device *dev, __be32 subcap_id, cap_t *cap, |
| 456 | const char *desc) |
| 457 | { |
| 458 | struct tpm_cmd_t tpm_cmd; |
| 459 | int rc; |
| 460 | struct tpm_chip *chip = dev_get_drvdata(dev); |
| 461 | |
| 462 | tpm_cmd.header.in = tpm_getcap_header; |
| 463 | if (subcap_id == CAP_VERSION_1_1 || subcap_id == CAP_VERSION_1_2) { |
| 464 | tpm_cmd.params.getcap_in.cap = subcap_id; |
| 465 | /*subcap field not necessary */ |
| 466 | tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(0); |
| 467 | tpm_cmd.header.in.length -= cpu_to_be32(sizeof(__be32)); |
| 468 | } else { |
| 469 | if (subcap_id == TPM_CAP_FLAG_PERM || |
| 470 | subcap_id == TPM_CAP_FLAG_VOL) |
| 471 | tpm_cmd.params.getcap_in.cap = TPM_CAP_FLAG; |
| 472 | else |
| 473 | tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP; |
| 474 | tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4); |
| 475 | tpm_cmd.params.getcap_in.subcap = subcap_id; |
| 476 | } |
| 477 | rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE, desc); |
| 478 | if (!rc) |
| 479 | *cap = tpm_cmd.params.getcap_out.cap; |
| 480 | return rc; |
| 481 | } |
| 482 | |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 483 | void tpm_gen_interrupt(struct tpm_chip *chip) |
| 484 | { |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame] | 485 | struct tpm_cmd_t tpm_cmd; |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 486 | ssize_t rc; |
| 487 | |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame] | 488 | tpm_cmd.header.in = tpm_getcap_header; |
| 489 | tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP; |
| 490 | tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4); |
| 491 | tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_TIMEOUT; |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 492 | |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame] | 493 | rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE, |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 494 | "attempting to determine the timeouts"); |
| 495 | } |
| 496 | EXPORT_SYMBOL_GPL(tpm_gen_interrupt); |
| 497 | |
Jason Gunthorpe | c584af1 | 2012-11-21 13:54:33 -0700 | [diff] [blame] | 498 | #define TPM_ORD_STARTUP cpu_to_be32(153) |
| 499 | #define TPM_ST_CLEAR cpu_to_be16(1) |
| 500 | #define TPM_ST_STATE cpu_to_be16(2) |
| 501 | #define TPM_ST_DEACTIVATED cpu_to_be16(3) |
| 502 | static const struct tpm_input_header tpm_startup_header = { |
| 503 | .tag = TPM_TAG_RQU_COMMAND, |
| 504 | .length = cpu_to_be32(12), |
| 505 | .ordinal = TPM_ORD_STARTUP |
| 506 | }; |
| 507 | |
| 508 | static int tpm_startup(struct tpm_chip *chip, __be16 startup_type) |
| 509 | { |
| 510 | struct tpm_cmd_t start_cmd; |
| 511 | start_cmd.header.in = tpm_startup_header; |
| 512 | start_cmd.params.startup_in.startup_type = startup_type; |
| 513 | return transmit_cmd(chip, &start_cmd, TPM_INTERNAL_RESULT_SIZE, |
| 514 | "attempting to start the TPM"); |
| 515 | } |
| 516 | |
Stefan Berger | 2b30a90 | 2011-11-11 12:57:02 -0500 | [diff] [blame] | 517 | int tpm_get_timeouts(struct tpm_chip *chip) |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 518 | { |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame] | 519 | struct tpm_cmd_t tpm_cmd; |
| 520 | struct timeout_t *timeout_cap; |
| 521 | struct duration_t *duration_cap; |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 522 | ssize_t rc; |
| 523 | u32 timeout; |
Stefan Berger | e3e1a1e | 2011-03-30 12:13:27 -0400 | [diff] [blame] | 524 | unsigned int scale = 1; |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 525 | |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame] | 526 | tpm_cmd.header.in = tpm_getcap_header; |
| 527 | tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP; |
| 528 | tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4); |
| 529 | tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_TIMEOUT; |
Jason Gunthorpe | c584af1 | 2012-11-21 13:54:33 -0700 | [diff] [blame] | 530 | rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE, NULL); |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 531 | |
Jason Gunthorpe | c584af1 | 2012-11-21 13:54:33 -0700 | [diff] [blame] | 532 | if (rc == TPM_ERR_INVALID_POSTINIT) { |
| 533 | /* The TPM is not started, we are the first to talk to it. |
| 534 | Execute a startup command. */ |
| 535 | dev_info(chip->dev, "Issuing TPM_STARTUP"); |
| 536 | if (tpm_startup(chip, TPM_ST_CLEAR)) |
| 537 | return rc; |
| 538 | |
| 539 | tpm_cmd.header.in = tpm_getcap_header; |
| 540 | tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP; |
| 541 | tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4); |
| 542 | tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_TIMEOUT; |
| 543 | rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE, |
| 544 | NULL); |
| 545 | } |
| 546 | if (rc) { |
| 547 | dev_err(chip->dev, |
| 548 | "A TPM error (%zd) occurred attempting to determine the timeouts\n", |
| 549 | rc); |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 550 | goto duration; |
Jason Gunthorpe | c584af1 | 2012-11-21 13:54:33 -0700 | [diff] [blame] | 551 | } |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 552 | |
Stefan Berger | 829bf06 | 2011-03-30 12:13:26 -0400 | [diff] [blame] | 553 | if (be32_to_cpu(tpm_cmd.header.out.return_code) != 0 || |
| 554 | be32_to_cpu(tpm_cmd.header.out.length) |
| 555 | != sizeof(tpm_cmd.header.out) + sizeof(u32) + 4 * sizeof(u32)) |
Stefan Berger | 2b30a90 | 2011-11-11 12:57:02 -0500 | [diff] [blame] | 556 | return -EINVAL; |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 557 | |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame] | 558 | timeout_cap = &tpm_cmd.params.getcap_out.cap.timeout; |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 559 | /* Don't overwrite default if value is 0 */ |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame] | 560 | timeout = be32_to_cpu(timeout_cap->a); |
Stefan Berger | e3e1a1e | 2011-03-30 12:13:27 -0400 | [diff] [blame] | 561 | if (timeout && timeout < 1000) { |
| 562 | /* timeouts in msec rather usec */ |
| 563 | scale = 1000; |
Stefan Berger | 6259210 | 2011-03-30 12:13:28 -0400 | [diff] [blame] | 564 | chip->vendor.timeout_adjusted = true; |
Stefan Berger | e3e1a1e | 2011-03-30 12:13:27 -0400 | [diff] [blame] | 565 | } |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 566 | if (timeout) |
Stefan Berger | e3e1a1e | 2011-03-30 12:13:27 -0400 | [diff] [blame] | 567 | chip->vendor.timeout_a = usecs_to_jiffies(timeout * scale); |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame] | 568 | timeout = be32_to_cpu(timeout_cap->b); |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 569 | if (timeout) |
Stefan Berger | e3e1a1e | 2011-03-30 12:13:27 -0400 | [diff] [blame] | 570 | chip->vendor.timeout_b = usecs_to_jiffies(timeout * scale); |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame] | 571 | timeout = be32_to_cpu(timeout_cap->c); |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 572 | if (timeout) |
Stefan Berger | e3e1a1e | 2011-03-30 12:13:27 -0400 | [diff] [blame] | 573 | chip->vendor.timeout_c = usecs_to_jiffies(timeout * scale); |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame] | 574 | timeout = be32_to_cpu(timeout_cap->d); |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 575 | if (timeout) |
Stefan Berger | e3e1a1e | 2011-03-30 12:13:27 -0400 | [diff] [blame] | 576 | chip->vendor.timeout_d = usecs_to_jiffies(timeout * scale); |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 577 | |
| 578 | duration: |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame] | 579 | tpm_cmd.header.in = tpm_getcap_header; |
| 580 | tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP; |
| 581 | tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4); |
| 582 | tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_DURATION; |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 583 | |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame] | 584 | rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE, |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 585 | "attempting to determine the durations"); |
| 586 | if (rc) |
Stefan Berger | 2b30a90 | 2011-11-11 12:57:02 -0500 | [diff] [blame] | 587 | return rc; |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 588 | |
Stefan Berger | 979b140 | 2011-03-30 12:13:23 -0400 | [diff] [blame] | 589 | if (be32_to_cpu(tpm_cmd.header.out.return_code) != 0 || |
| 590 | be32_to_cpu(tpm_cmd.header.out.length) |
| 591 | != sizeof(tpm_cmd.header.out) + sizeof(u32) + 3 * sizeof(u32)) |
Stefan Berger | 2b30a90 | 2011-11-11 12:57:02 -0500 | [diff] [blame] | 592 | return -EINVAL; |
Stefan Berger | 979b140 | 2011-03-30 12:13:23 -0400 | [diff] [blame] | 593 | |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame] | 594 | duration_cap = &tpm_cmd.params.getcap_out.cap.duration; |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 595 | chip->vendor.duration[TPM_SHORT] = |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame] | 596 | usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_short)); |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 597 | chip->vendor.duration[TPM_MEDIUM] = |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame] | 598 | usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_medium)); |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 599 | chip->vendor.duration[TPM_LONG] = |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame] | 600 | usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_long)); |
Stefan Berger | e934acc | 2011-03-30 12:13:24 -0400 | [diff] [blame] | 601 | |
| 602 | /* The Broadcom BCM0102 chipset in a Dell Latitude D820 gets the above |
| 603 | * value wrong and apparently reports msecs rather than usecs. So we |
| 604 | * fix up the resulting too-small TPM_SHORT value to make things work. |
| 605 | * We also scale the TPM_MEDIUM and -_LONG values by 1000. |
| 606 | */ |
| 607 | if (chip->vendor.duration[TPM_SHORT] < (HZ / 100)) { |
| 608 | chip->vendor.duration[TPM_SHORT] = HZ; |
| 609 | chip->vendor.duration[TPM_MEDIUM] *= 1000; |
| 610 | chip->vendor.duration[TPM_LONG] *= 1000; |
Stefan Berger | 04ab229 | 2011-03-30 12:13:25 -0400 | [diff] [blame] | 611 | chip->vendor.duration_adjusted = true; |
Stefan Berger | e934acc | 2011-03-30 12:13:24 -0400 | [diff] [blame] | 612 | dev_info(chip->dev, "Adjusting TPM timeout parameters."); |
| 613 | } |
Stefan Berger | 2b30a90 | 2011-11-11 12:57:02 -0500 | [diff] [blame] | 614 | return 0; |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 615 | } |
| 616 | EXPORT_SYMBOL_GPL(tpm_get_timeouts); |
| 617 | |
Stefan Berger | d97c6ad | 2011-11-11 12:57:03 -0500 | [diff] [blame] | 618 | #define TPM_ORD_CONTINUE_SELFTEST 83 |
| 619 | #define CONTINUE_SELFTEST_RESULT_SIZE 10 |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 620 | |
Stefan Berger | d97c6ad | 2011-11-11 12:57:03 -0500 | [diff] [blame] | 621 | static struct tpm_input_header continue_selftest_header = { |
| 622 | .tag = TPM_TAG_RQU_COMMAND, |
| 623 | .length = cpu_to_be32(10), |
| 624 | .ordinal = cpu_to_be32(TPM_ORD_CONTINUE_SELFTEST), |
| 625 | }; |
| 626 | |
| 627 | /** |
| 628 | * tpm_continue_selftest -- run TPM's selftest |
| 629 | * @chip: TPM chip to use |
| 630 | * |
| 631 | * Returns 0 on success, < 0 in case of fatal error or a value > 0 representing |
| 632 | * a TPM error code. |
| 633 | */ |
Stefan Berger | 68d6e67 | 2011-11-11 12:57:04 -0500 | [diff] [blame] | 634 | static int tpm_continue_selftest(struct tpm_chip *chip) |
Stefan Berger | d97c6ad | 2011-11-11 12:57:03 -0500 | [diff] [blame] | 635 | { |
| 636 | int rc; |
| 637 | struct tpm_cmd_t cmd; |
| 638 | |
| 639 | cmd.header.in = continue_selftest_header; |
| 640 | rc = transmit_cmd(chip, &cmd, CONTINUE_SELFTEST_RESULT_SIZE, |
| 641 | "continue selftest"); |
| 642 | return rc; |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 643 | } |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 644 | |
Peter Huewe | 0a41826 | 2013-10-22 00:29:14 +0200 | [diff] [blame] | 645 | ssize_t tpm_show_enabled(struct device *dev, struct device_attribute *attr, |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 646 | char *buf) |
| 647 | { |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame] | 648 | cap_t cap; |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 649 | ssize_t rc; |
| 650 | |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame] | 651 | rc = tpm_getcap(dev, TPM_CAP_FLAG_PERM, &cap, |
| 652 | "attempting to determine the permanent enabled state"); |
| 653 | if (rc) |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 654 | return 0; |
Marcin Obara | ec288bd | 2008-07-25 19:44:59 -0700 | [diff] [blame] | 655 | |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame] | 656 | rc = sprintf(buf, "%d\n", !cap.perm_flags.disable); |
Marcin Obara | ec288bd | 2008-07-25 19:44:59 -0700 | [diff] [blame] | 657 | return rc; |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 658 | } |
| 659 | EXPORT_SYMBOL_GPL(tpm_show_enabled); |
| 660 | |
Peter Huewe | 0a41826 | 2013-10-22 00:29:14 +0200 | [diff] [blame] | 661 | ssize_t tpm_show_active(struct device *dev, struct device_attribute *attr, |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 662 | char *buf) |
| 663 | { |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame] | 664 | cap_t cap; |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 665 | ssize_t rc; |
| 666 | |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame] | 667 | rc = tpm_getcap(dev, TPM_CAP_FLAG_PERM, &cap, |
| 668 | "attempting to determine the permanent active state"); |
| 669 | if (rc) |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 670 | return 0; |
Marcin Obara | ec288bd | 2008-07-25 19:44:59 -0700 | [diff] [blame] | 671 | |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame] | 672 | rc = sprintf(buf, "%d\n", !cap.perm_flags.deactivated); |
Marcin Obara | ec288bd | 2008-07-25 19:44:59 -0700 | [diff] [blame] | 673 | return rc; |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 674 | } |
| 675 | EXPORT_SYMBOL_GPL(tpm_show_active); |
| 676 | |
Peter Huewe | 0a41826 | 2013-10-22 00:29:14 +0200 | [diff] [blame] | 677 | ssize_t tpm_show_owned(struct device *dev, struct device_attribute *attr, |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 678 | char *buf) |
| 679 | { |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame] | 680 | cap_t cap; |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 681 | ssize_t rc; |
| 682 | |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame] | 683 | rc = tpm_getcap(dev, TPM_CAP_PROP_OWNER, &cap, |
| 684 | "attempting to determine the owner state"); |
| 685 | if (rc) |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 686 | return 0; |
Marcin Obara | ec288bd | 2008-07-25 19:44:59 -0700 | [diff] [blame] | 687 | |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame] | 688 | rc = sprintf(buf, "%d\n", cap.owned); |
Marcin Obara | ec288bd | 2008-07-25 19:44:59 -0700 | [diff] [blame] | 689 | return rc; |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 690 | } |
| 691 | EXPORT_SYMBOL_GPL(tpm_show_owned); |
| 692 | |
Peter Huewe | 0a41826 | 2013-10-22 00:29:14 +0200 | [diff] [blame] | 693 | ssize_t tpm_show_temp_deactivated(struct device *dev, |
| 694 | struct device_attribute *attr, char *buf) |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 695 | { |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame] | 696 | cap_t cap; |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 697 | ssize_t rc; |
| 698 | |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame] | 699 | rc = tpm_getcap(dev, TPM_CAP_FLAG_VOL, &cap, |
| 700 | "attempting to determine the temporary state"); |
| 701 | if (rc) |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 702 | return 0; |
Marcin Obara | ec288bd | 2008-07-25 19:44:59 -0700 | [diff] [blame] | 703 | |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame] | 704 | rc = sprintf(buf, "%d\n", cap.stclear_flags.deactivated); |
Marcin Obara | ec288bd | 2008-07-25 19:44:59 -0700 | [diff] [blame] | 705 | return rc; |
Kylene Jo Hall | 08e96e4 | 2006-04-22 02:37:50 -0700 | [diff] [blame] | 706 | } |
| 707 | EXPORT_SYMBOL_GPL(tpm_show_temp_deactivated); |
| 708 | |
Rajiv Andrade | 659aaf2 | 2009-02-02 15:23:44 -0200 | [diff] [blame] | 709 | /* |
| 710 | * tpm_chip_find_get - return tpm_chip for given chip number |
| 711 | */ |
| 712 | static struct tpm_chip *tpm_chip_find_get(int chip_num) |
| 713 | { |
Rajiv Andrade | 8920d5a | 2009-02-05 13:06:30 -0200 | [diff] [blame] | 714 | struct tpm_chip *pos, *chip = NULL; |
Rajiv Andrade | 659aaf2 | 2009-02-02 15:23:44 -0200 | [diff] [blame] | 715 | |
| 716 | rcu_read_lock(); |
| 717 | list_for_each_entry_rcu(pos, &tpm_chip_list, list) { |
| 718 | if (chip_num != TPM_ANY_NUM && chip_num != pos->dev_num) |
| 719 | continue; |
| 720 | |
Rajiv Andrade | 8920d5a | 2009-02-05 13:06:30 -0200 | [diff] [blame] | 721 | if (try_module_get(pos->dev->driver->owner)) { |
| 722 | chip = pos; |
Rajiv Andrade | 659aaf2 | 2009-02-02 15:23:44 -0200 | [diff] [blame] | 723 | break; |
Rajiv Andrade | 8920d5a | 2009-02-05 13:06:30 -0200 | [diff] [blame] | 724 | } |
Rajiv Andrade | 659aaf2 | 2009-02-02 15:23:44 -0200 | [diff] [blame] | 725 | } |
| 726 | rcu_read_unlock(); |
Rajiv Andrade | 8920d5a | 2009-02-05 13:06:30 -0200 | [diff] [blame] | 727 | return chip; |
Rajiv Andrade | 659aaf2 | 2009-02-02 15:23:44 -0200 | [diff] [blame] | 728 | } |
| 729 | |
| 730 | #define TPM_ORDINAL_PCRREAD cpu_to_be32(21) |
| 731 | #define READ_PCR_RESULT_SIZE 30 |
| 732 | static struct tpm_input_header pcrread_header = { |
| 733 | .tag = TPM_TAG_RQU_COMMAND, |
| 734 | .length = cpu_to_be32(14), |
| 735 | .ordinal = TPM_ORDINAL_PCRREAD |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 736 | }; |
| 737 | |
Stefan Berger | 68d6e67 | 2011-11-11 12:57:04 -0500 | [diff] [blame] | 738 | static int __tpm_pcr_read(struct tpm_chip *chip, int pcr_idx, u8 *res_buf) |
Rajiv Andrade | 659aaf2 | 2009-02-02 15:23:44 -0200 | [diff] [blame] | 739 | { |
| 740 | int rc; |
| 741 | struct tpm_cmd_t cmd; |
| 742 | |
| 743 | cmd.header.in = pcrread_header; |
| 744 | cmd.params.pcrread_in.pcr_idx = cpu_to_be32(pcr_idx); |
Rajiv Andrade | 23acb98 | 2009-09-30 12:26:55 -0300 | [diff] [blame] | 745 | rc = transmit_cmd(chip, &cmd, READ_PCR_RESULT_SIZE, |
Rajiv Andrade | 659aaf2 | 2009-02-02 15:23:44 -0200 | [diff] [blame] | 746 | "attempting to read a pcr value"); |
| 747 | |
| 748 | if (rc == 0) |
| 749 | memcpy(res_buf, cmd.params.pcrread_out.pcr_result, |
| 750 | TPM_DIGEST_SIZE); |
| 751 | return rc; |
| 752 | } |
| 753 | |
| 754 | /** |
| 755 | * tpm_pcr_read - read a pcr value |
Peter Huewe | 0a41826 | 2013-10-22 00:29:14 +0200 | [diff] [blame] | 756 | * @chip_num: tpm idx # or ANY |
Rajiv Andrade | 659aaf2 | 2009-02-02 15:23:44 -0200 | [diff] [blame] | 757 | * @pcr_idx: pcr idx to retrieve |
Peter Huewe | 0a41826 | 2013-10-22 00:29:14 +0200 | [diff] [blame] | 758 | * @res_buf: TPM_PCR value |
| 759 | * size of res_buf is 20 bytes (or NULL if you don't care) |
Rajiv Andrade | 659aaf2 | 2009-02-02 15:23:44 -0200 | [diff] [blame] | 760 | * |
| 761 | * The TPM driver should be built-in, but for whatever reason it |
| 762 | * isn't, protect against the chip disappearing, by incrementing |
| 763 | * the module usage count. |
| 764 | */ |
| 765 | int tpm_pcr_read(u32 chip_num, int pcr_idx, u8 *res_buf) |
| 766 | { |
| 767 | struct tpm_chip *chip; |
| 768 | int rc; |
| 769 | |
| 770 | chip = tpm_chip_find_get(chip_num); |
| 771 | if (chip == NULL) |
| 772 | return -ENODEV; |
| 773 | rc = __tpm_pcr_read(chip, pcr_idx, res_buf); |
Mimi Zohar | a0e3934 | 2010-11-23 17:50:32 -0500 | [diff] [blame] | 774 | tpm_chip_put(chip); |
Rajiv Andrade | 659aaf2 | 2009-02-02 15:23:44 -0200 | [diff] [blame] | 775 | return rc; |
| 776 | } |
| 777 | EXPORT_SYMBOL_GPL(tpm_pcr_read); |
| 778 | |
| 779 | /** |
| 780 | * tpm_pcr_extend - extend pcr value with hash |
Peter Huewe | 0a41826 | 2013-10-22 00:29:14 +0200 | [diff] [blame] | 781 | * @chip_num: tpm idx # or AN& |
Rajiv Andrade | 659aaf2 | 2009-02-02 15:23:44 -0200 | [diff] [blame] | 782 | * @pcr_idx: pcr idx to extend |
Peter Huewe | 0a41826 | 2013-10-22 00:29:14 +0200 | [diff] [blame] | 783 | * @hash: hash value used to extend pcr value |
Rajiv Andrade | 659aaf2 | 2009-02-02 15:23:44 -0200 | [diff] [blame] | 784 | * |
| 785 | * The TPM driver should be built-in, but for whatever reason it |
| 786 | * isn't, protect against the chip disappearing, by incrementing |
| 787 | * the module usage count. |
| 788 | */ |
| 789 | #define TPM_ORD_PCR_EXTEND cpu_to_be32(20) |
Jason Gunthorpe | 0afd905 | 2009-09-18 12:54:24 -0700 | [diff] [blame] | 790 | #define EXTEND_PCR_RESULT_SIZE 34 |
Rajiv Andrade | 659aaf2 | 2009-02-02 15:23:44 -0200 | [diff] [blame] | 791 | static struct tpm_input_header pcrextend_header = { |
| 792 | .tag = TPM_TAG_RQU_COMMAND, |
| 793 | .length = cpu_to_be32(34), |
| 794 | .ordinal = TPM_ORD_PCR_EXTEND |
| 795 | }; |
| 796 | |
| 797 | int tpm_pcr_extend(u32 chip_num, int pcr_idx, const u8 *hash) |
| 798 | { |
| 799 | struct tpm_cmd_t cmd; |
| 800 | int rc; |
| 801 | struct tpm_chip *chip; |
| 802 | |
| 803 | chip = tpm_chip_find_get(chip_num); |
| 804 | if (chip == NULL) |
| 805 | return -ENODEV; |
| 806 | |
| 807 | cmd.header.in = pcrextend_header; |
Rajiv Andrade | 659aaf2 | 2009-02-02 15:23:44 -0200 | [diff] [blame] | 808 | cmd.params.pcrextend_in.pcr_idx = cpu_to_be32(pcr_idx); |
| 809 | memcpy(cmd.params.pcrextend_in.hash, hash, TPM_DIGEST_SIZE); |
Jason Gunthorpe | 0afd905 | 2009-09-18 12:54:24 -0700 | [diff] [blame] | 810 | rc = transmit_cmd(chip, &cmd, EXTEND_PCR_RESULT_SIZE, |
Rajiv Andrade | 659aaf2 | 2009-02-02 15:23:44 -0200 | [diff] [blame] | 811 | "attempting extend a PCR value"); |
| 812 | |
Mimi Zohar | a0e3934 | 2010-11-23 17:50:32 -0500 | [diff] [blame] | 813 | tpm_chip_put(chip); |
Rajiv Andrade | 659aaf2 | 2009-02-02 15:23:44 -0200 | [diff] [blame] | 814 | return rc; |
| 815 | } |
| 816 | EXPORT_SYMBOL_GPL(tpm_pcr_extend); |
| 817 | |
Stefan Berger | 68d6e67 | 2011-11-11 12:57:04 -0500 | [diff] [blame] | 818 | /** |
| 819 | * tpm_do_selftest - have the TPM continue its selftest and wait until it |
| 820 | * can receive further commands |
| 821 | * @chip: TPM chip to use |
| 822 | * |
| 823 | * Returns 0 on success, < 0 in case of fatal error or a value > 0 representing |
| 824 | * a TPM error code. |
| 825 | */ |
| 826 | int tpm_do_selftest(struct tpm_chip *chip) |
| 827 | { |
| 828 | int rc; |
Stefan Berger | 68d6e67 | 2011-11-11 12:57:04 -0500 | [diff] [blame] | 829 | unsigned int loops; |
Jason Gunthorpe | 4643826 | 2012-11-21 13:15:54 -0800 | [diff] [blame] | 830 | unsigned int delay_msec = 100; |
Stefan Berger | 68d6e67 | 2011-11-11 12:57:04 -0500 | [diff] [blame] | 831 | unsigned long duration; |
Rajiv Andrade | 24ebe66 | 2012-04-24 17:38:17 -0300 | [diff] [blame] | 832 | struct tpm_cmd_t cmd; |
Stefan Berger | 68d6e67 | 2011-11-11 12:57:04 -0500 | [diff] [blame] | 833 | |
Peter Huewe | 0a41826 | 2013-10-22 00:29:14 +0200 | [diff] [blame] | 834 | duration = tpm_calc_ordinal_duration(chip, TPM_ORD_CONTINUE_SELFTEST); |
Stefan Berger | 68d6e67 | 2011-11-11 12:57:04 -0500 | [diff] [blame] | 835 | |
| 836 | loops = jiffies_to_msecs(duration) / delay_msec; |
| 837 | |
| 838 | rc = tpm_continue_selftest(chip); |
| 839 | /* This may fail if there was no TPM driver during a suspend/resume |
| 840 | * cycle; some may return 10 (BAD_ORDINAL), others 28 (FAILEDSELFTEST) |
| 841 | */ |
| 842 | if (rc) |
| 843 | return rc; |
| 844 | |
| 845 | do { |
Rajiv Andrade | 24ebe66 | 2012-04-24 17:38:17 -0300 | [diff] [blame] | 846 | /* Attempt to read a PCR value */ |
| 847 | cmd.header.in = pcrread_header; |
| 848 | cmd.params.pcrread_in.pcr_idx = cpu_to_be32(0); |
| 849 | rc = tpm_transmit(chip, (u8 *) &cmd, READ_PCR_RESULT_SIZE); |
Jason Gunthorpe | 4643826 | 2012-11-21 13:15:54 -0800 | [diff] [blame] | 850 | /* Some buggy TPMs will not respond to tpm_tis_ready() for |
| 851 | * around 300ms while the self test is ongoing, keep trying |
| 852 | * until the self test duration expires. */ |
| 853 | if (rc == -ETIME) { |
| 854 | dev_info(chip->dev, HW_ERR "TPM command timed out during continue self test"); |
| 855 | msleep(delay_msec); |
| 856 | continue; |
| 857 | } |
Rajiv Andrade | 24ebe66 | 2012-04-24 17:38:17 -0300 | [diff] [blame] | 858 | |
| 859 | if (rc < TPM_HEADER_SIZE) |
| 860 | return -EFAULT; |
| 861 | |
| 862 | rc = be32_to_cpu(cmd.header.out.return_code); |
Stefan Berger | be40541 | 2012-01-17 22:07:30 -0500 | [diff] [blame] | 863 | if (rc == TPM_ERR_DISABLED || rc == TPM_ERR_DEACTIVATED) { |
| 864 | dev_info(chip->dev, |
| 865 | "TPM is disabled/deactivated (0x%X)\n", rc); |
| 866 | /* TPM is disabled and/or deactivated; driver can |
| 867 | * proceed and TPM does handle commands for |
| 868 | * suspend/resume correctly |
| 869 | */ |
| 870 | return 0; |
| 871 | } |
Stefan Berger | 68d6e67 | 2011-11-11 12:57:04 -0500 | [diff] [blame] | 872 | if (rc != TPM_WARN_DOING_SELFTEST) |
| 873 | return rc; |
| 874 | msleep(delay_msec); |
| 875 | } while (--loops > 0); |
| 876 | |
| 877 | return rc; |
| 878 | } |
| 879 | EXPORT_SYMBOL_GPL(tpm_do_selftest); |
| 880 | |
Mimi Zohar | c749ba9 | 2010-11-23 18:54:16 -0500 | [diff] [blame] | 881 | int tpm_send(u32 chip_num, void *cmd, size_t buflen) |
| 882 | { |
| 883 | struct tpm_chip *chip; |
| 884 | int rc; |
| 885 | |
| 886 | chip = tpm_chip_find_get(chip_num); |
| 887 | if (chip == NULL) |
| 888 | return -ENODEV; |
| 889 | |
| 890 | rc = transmit_cmd(chip, cmd, buflen, "attempting tpm_cmd"); |
| 891 | |
| 892 | tpm_chip_put(chip); |
| 893 | return rc; |
| 894 | } |
| 895 | EXPORT_SYMBOL_GPL(tpm_send); |
| 896 | |
Kylene Hall | 6659ca2 | 2005-06-23 22:02:00 -0700 | [diff] [blame] | 897 | ssize_t tpm_show_pcrs(struct device *dev, struct device_attribute *attr, |
| 898 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 899 | { |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame] | 900 | cap_t cap; |
Rajiv Andrade | 659aaf2 | 2009-02-02 15:23:44 -0200 | [diff] [blame] | 901 | u8 digest[TPM_DIGEST_SIZE]; |
Kylene Jo Hall | beed53a | 2006-04-22 02:37:05 -0700 | [diff] [blame] | 902 | ssize_t rc; |
Kylene Hall | 81179bb | 2005-06-23 22:01:59 -0700 | [diff] [blame] | 903 | int i, j, num_pcrs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 904 | char *str = buf; |
Kylene Jo Hall | e659a3f | 2005-10-30 15:03:24 -0800 | [diff] [blame] | 905 | struct tpm_chip *chip = dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame] | 907 | rc = tpm_getcap(dev, TPM_CAP_PROP_PCR, &cap, |
Kylene Jo Hall | beed53a | 2006-04-22 02:37:05 -0700 | [diff] [blame] | 908 | "attempting to determine the number of PCRS"); |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame] | 909 | if (rc) |
Kylene Hall | e234bc9 | 2005-06-23 22:02:08 -0700 | [diff] [blame] | 910 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 911 | |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame] | 912 | num_pcrs = be32_to_cpu(cap.num_pcrs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 913 | for (i = 0; i < num_pcrs; i++) { |
Rajiv Andrade | 659aaf2 | 2009-02-02 15:23:44 -0200 | [diff] [blame] | 914 | rc = __tpm_pcr_read(chip, i, digest); |
Kylene Jo Hall | beed53a | 2006-04-22 02:37:05 -0700 | [diff] [blame] | 915 | if (rc) |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame] | 916 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | str += sprintf(str, "PCR-%02d: ", i); |
| 918 | for (j = 0; j < TPM_DIGEST_SIZE; j++) |
Rajiv Andrade | 659aaf2 | 2009-02-02 15:23:44 -0200 | [diff] [blame] | 919 | str += sprintf(str, "%02X ", digest[j]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 920 | str += sprintf(str, "\n"); |
| 921 | } |
| 922 | return str - buf; |
| 923 | } |
Kylene Hall | 6659ca2 | 2005-06-23 22:02:00 -0700 | [diff] [blame] | 924 | EXPORT_SYMBOL_GPL(tpm_show_pcrs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 925 | |
| 926 | #define READ_PUBEK_RESULT_SIZE 314 |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame] | 927 | #define TPM_ORD_READPUBEK cpu_to_be32(124) |
Xiaoyan Zhang | 8119807 | 2013-08-29 20:39:11 +0800 | [diff] [blame] | 928 | static struct tpm_input_header tpm_readpubek_header = { |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame] | 929 | .tag = TPM_TAG_RQU_COMMAND, |
| 930 | .length = cpu_to_be32(30), |
| 931 | .ordinal = TPM_ORD_READPUBEK |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 932 | }; |
| 933 | |
Kylene Hall | 6659ca2 | 2005-06-23 22:02:00 -0700 | [diff] [blame] | 934 | ssize_t tpm_show_pubek(struct device *dev, struct device_attribute *attr, |
| 935 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 936 | { |
Kylene Hall | 2df7111 | 2005-06-23 22:01:54 -0700 | [diff] [blame] | 937 | u8 *data; |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame] | 938 | struct tpm_cmd_t tpm_cmd; |
Kylene Jo Hall | beed53a | 2006-04-22 02:37:05 -0700 | [diff] [blame] | 939 | ssize_t err; |
Kylene Hall | 81179bb | 2005-06-23 22:01:59 -0700 | [diff] [blame] | 940 | int i, rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 941 | char *str = buf; |
| 942 | |
Kylene Jo Hall | e659a3f | 2005-10-30 15:03:24 -0800 | [diff] [blame] | 943 | struct tpm_chip *chip = dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 944 | |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame] | 945 | tpm_cmd.header.in = tpm_readpubek_header; |
| 946 | err = transmit_cmd(chip, &tpm_cmd, READ_PUBEK_RESULT_SIZE, |
Kylene Jo Hall | beed53a | 2006-04-22 02:37:05 -0700 | [diff] [blame] | 947 | "attempting to read the PUBEK"); |
| 948 | if (err) |
Kylene Jo Hall | 34d6e07 | 2005-06-23 22:02:10 -0700 | [diff] [blame] | 949 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 950 | |
Peter Huewe | 0a41826 | 2013-10-22 00:29:14 +0200 | [diff] [blame] | 951 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 952 | ignore header 10 bytes |
| 953 | algorithm 32 bits (1 == RSA ) |
| 954 | encscheme 16 bits |
| 955 | sigscheme 16 bits |
Peter Huewe | 0a41826 | 2013-10-22 00:29:14 +0200 | [diff] [blame] | 956 | parameters (RSA 12->bytes: keybit, #primes, expbit) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 957 | keylenbytes 32 bits |
| 958 | 256 byte modulus |
| 959 | ignore checksum 20 bytes |
| 960 | */ |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame] | 961 | data = tpm_cmd.params.readpubek_out_buffer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 962 | str += |
| 963 | sprintf(str, |
Stefan Berger | 5a79444 | 2011-03-30 12:13:29 -0400 | [diff] [blame] | 964 | "Algorithm: %02X %02X %02X %02X\n" |
| 965 | "Encscheme: %02X %02X\n" |
| 966 | "Sigscheme: %02X %02X\n" |
| 967 | "Parameters: %02X %02X %02X %02X " |
| 968 | "%02X %02X %02X %02X " |
| 969 | "%02X %02X %02X %02X\n" |
| 970 | "Modulus length: %d\n" |
| 971 | "Modulus:\n", |
| 972 | data[0], data[1], data[2], data[3], |
| 973 | data[4], data[5], |
| 974 | data[6], data[7], |
| 975 | data[12], data[13], data[14], data[15], |
| 976 | data[16], data[17], data[18], data[19], |
| 977 | data[20], data[21], data[22], data[23], |
| 978 | be32_to_cpu(*((__be32 *) (data + 24)))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 979 | |
| 980 | for (i = 0; i < 256; i++) { |
Stefan Berger | 5a79444 | 2011-03-30 12:13:29 -0400 | [diff] [blame] | 981 | str += sprintf(str, "%02X ", data[i + 28]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 982 | if ((i + 1) % 16 == 0) |
| 983 | str += sprintf(str, "\n"); |
| 984 | } |
Kylene Jo Hall | 34d6e07 | 2005-06-23 22:02:10 -0700 | [diff] [blame] | 985 | out: |
Kylene Jo Hall | beed53a | 2006-04-22 02:37:05 -0700 | [diff] [blame] | 986 | rc = str - buf; |
Kylene Hall | 2df7111 | 2005-06-23 22:01:54 -0700 | [diff] [blame] | 987 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 988 | } |
Kylene Hall | 6659ca2 | 2005-06-23 22:02:00 -0700 | [diff] [blame] | 989 | EXPORT_SYMBOL_GPL(tpm_show_pubek); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 990 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 991 | |
Kylene Hall | 6659ca2 | 2005-06-23 22:02:00 -0700 | [diff] [blame] | 992 | ssize_t tpm_show_caps(struct device *dev, struct device_attribute *attr, |
| 993 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 994 | { |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame] | 995 | cap_t cap; |
Kylene Jo Hall | beed53a | 2006-04-22 02:37:05 -0700 | [diff] [blame] | 996 | ssize_t rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 997 | char *str = buf; |
| 998 | |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame] | 999 | rc = tpm_getcap(dev, TPM_CAP_PROP_MANUFACTURER, &cap, |
Kylene Jo Hall | beed53a | 2006-04-22 02:37:05 -0700 | [diff] [blame] | 1000 | "attempting to determine the manufacturer"); |
Kylene Jo Hall | beed53a | 2006-04-22 02:37:05 -0700 | [diff] [blame] | 1001 | if (rc) |
Rajiv Andrade | 0883743 | 2009-02-02 15:23:43 -0200 | [diff] [blame] | 1002 | return 0; |
| 1003 | str += sprintf(str, "Manufacturer: 0x%x\n", |
| 1004 | be32_to_cpu(cap.manufacturer_id)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1005 | |
Jason Gunthorpe | e907481 | 2013-09-17 18:19:44 -0600 | [diff] [blame] | 1006 | /* Try to get a TPM version 1.2 TPM_CAP_VERSION_INFO */ |
| 1007 | rc = tpm_getcap(dev, CAP_VERSION_1_2, &cap, |
| 1008 | "attempting to determine the 1.2 version"); |
| 1009 | if (!rc) { |
| 1010 | str += sprintf(str, |
| 1011 | "TCG version: %d.%d\nFirmware version: %d.%d\n", |
| 1012 | cap.tpm_version_1_2.Major, |
| 1013 | cap.tpm_version_1_2.Minor, |
| 1014 | cap.tpm_version_1_2.revMajor, |
| 1015 | cap.tpm_version_1_2.revMinor); |
| 1016 | } else { |
| 1017 | /* Otherwise just use TPM_STRUCT_VER */ |
| 1018 | rc = tpm_getcap(dev, CAP_VERSION_1_1, &cap, |
| 1019 | "attempting to determine the 1.1 version"); |
| 1020 | if (rc) |
| 1021 | return 0; |
| 1022 | str += sprintf(str, |
| 1023 | "TCG version: %d.%d\nFirmware version: %d.%d\n", |
| 1024 | cap.tpm_version.Major, |
| 1025 | cap.tpm_version.Minor, |
| 1026 | cap.tpm_version.revMajor, |
| 1027 | cap.tpm_version.revMinor); |
| 1028 | } |
| 1029 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1030 | return str - buf; |
| 1031 | } |
Kylene Hall | 6659ca2 | 2005-06-23 22:02:00 -0700 | [diff] [blame] | 1032 | EXPORT_SYMBOL_GPL(tpm_show_caps); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1033 | |
Stefan Berger | 04ab229 | 2011-03-30 12:13:25 -0400 | [diff] [blame] | 1034 | ssize_t tpm_show_durations(struct device *dev, struct device_attribute *attr, |
| 1035 | char *buf) |
| 1036 | { |
| 1037 | struct tpm_chip *chip = dev_get_drvdata(dev); |
| 1038 | |
Stefan Berger | 403d1d0 | 2011-08-23 08:52:10 -0400 | [diff] [blame] | 1039 | if (chip->vendor.duration[TPM_LONG] == 0) |
| 1040 | return 0; |
| 1041 | |
Stefan Berger | 04ab229 | 2011-03-30 12:13:25 -0400 | [diff] [blame] | 1042 | return sprintf(buf, "%d %d %d [%s]\n", |
| 1043 | jiffies_to_usecs(chip->vendor.duration[TPM_SHORT]), |
| 1044 | jiffies_to_usecs(chip->vendor.duration[TPM_MEDIUM]), |
| 1045 | jiffies_to_usecs(chip->vendor.duration[TPM_LONG]), |
| 1046 | chip->vendor.duration_adjusted |
| 1047 | ? "adjusted" : "original"); |
| 1048 | } |
| 1049 | EXPORT_SYMBOL_GPL(tpm_show_durations); |
| 1050 | |
Stefan Berger | 6259210 | 2011-03-30 12:13:28 -0400 | [diff] [blame] | 1051 | ssize_t tpm_show_timeouts(struct device *dev, struct device_attribute *attr, |
| 1052 | char *buf) |
| 1053 | { |
| 1054 | struct tpm_chip *chip = dev_get_drvdata(dev); |
| 1055 | |
| 1056 | return sprintf(buf, "%d %d %d %d [%s]\n", |
| 1057 | jiffies_to_usecs(chip->vendor.timeout_a), |
| 1058 | jiffies_to_usecs(chip->vendor.timeout_b), |
| 1059 | jiffies_to_usecs(chip->vendor.timeout_c), |
| 1060 | jiffies_to_usecs(chip->vendor.timeout_d), |
| 1061 | chip->vendor.timeout_adjusted |
| 1062 | ? "adjusted" : "original"); |
| 1063 | } |
| 1064 | EXPORT_SYMBOL_GPL(tpm_show_timeouts); |
| 1065 | |
Kylene Hall | 6659ca2 | 2005-06-23 22:02:00 -0700 | [diff] [blame] | 1066 | ssize_t tpm_store_cancel(struct device *dev, struct device_attribute *attr, |
| 1067 | const char *buf, size_t count) |
| 1068 | { |
| 1069 | struct tpm_chip *chip = dev_get_drvdata(dev); |
| 1070 | if (chip == NULL) |
| 1071 | return 0; |
| 1072 | |
Kylene Jo Hall | 90dda52 | 2006-04-22 02:37:15 -0700 | [diff] [blame] | 1073 | chip->vendor.cancel(chip); |
Kylene Hall | 6659ca2 | 2005-06-23 22:02:00 -0700 | [diff] [blame] | 1074 | return count; |
| 1075 | } |
| 1076 | EXPORT_SYMBOL_GPL(tpm_store_cancel); |
| 1077 | |
Peter Huewe | 0a41826 | 2013-10-22 00:29:14 +0200 | [diff] [blame] | 1078 | static bool wait_for_tpm_stat_cond(struct tpm_chip *chip, u8 mask, |
| 1079 | bool check_cancel, bool *canceled) |
Stefan Berger | 78f09cc | 2013-01-22 13:57:53 -0600 | [diff] [blame] | 1080 | { |
| 1081 | u8 status = chip->vendor.status(chip); |
| 1082 | |
| 1083 | *canceled = false; |
| 1084 | if ((status & mask) == mask) |
| 1085 | return true; |
| 1086 | if (check_cancel && chip->vendor.req_canceled(chip, status)) { |
| 1087 | *canceled = true; |
| 1088 | return true; |
| 1089 | } |
| 1090 | return false; |
| 1091 | } |
| 1092 | |
Rajiv Andrade | fd04886 | 2011-09-16 14:39:40 -0300 | [diff] [blame] | 1093 | int wait_for_tpm_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout, |
Stefan Berger | 78f09cc | 2013-01-22 13:57:53 -0600 | [diff] [blame] | 1094 | wait_queue_head_t *queue, bool check_cancel) |
Rajiv Andrade | fd04886 | 2011-09-16 14:39:40 -0300 | [diff] [blame] | 1095 | { |
| 1096 | unsigned long stop; |
| 1097 | long rc; |
| 1098 | u8 status; |
Stefan Berger | 78f09cc | 2013-01-22 13:57:53 -0600 | [diff] [blame] | 1099 | bool canceled = false; |
Rajiv Andrade | fd04886 | 2011-09-16 14:39:40 -0300 | [diff] [blame] | 1100 | |
| 1101 | /* check current status */ |
| 1102 | status = chip->vendor.status(chip); |
| 1103 | if ((status & mask) == mask) |
| 1104 | return 0; |
| 1105 | |
| 1106 | stop = jiffies + timeout; |
| 1107 | |
| 1108 | if (chip->vendor.irq) { |
| 1109 | again: |
| 1110 | timeout = stop - jiffies; |
| 1111 | if ((long)timeout <= 0) |
| 1112 | return -ETIME; |
| 1113 | rc = wait_event_interruptible_timeout(*queue, |
Stefan Berger | 78f09cc | 2013-01-22 13:57:53 -0600 | [diff] [blame] | 1114 | wait_for_tpm_stat_cond(chip, mask, check_cancel, |
| 1115 | &canceled), |
| 1116 | timeout); |
| 1117 | if (rc > 0) { |
| 1118 | if (canceled) |
| 1119 | return -ECANCELED; |
Rajiv Andrade | fd04886 | 2011-09-16 14:39:40 -0300 | [diff] [blame] | 1120 | return 0; |
Stefan Berger | 78f09cc | 2013-01-22 13:57:53 -0600 | [diff] [blame] | 1121 | } |
Rajiv Andrade | fd04886 | 2011-09-16 14:39:40 -0300 | [diff] [blame] | 1122 | if (rc == -ERESTARTSYS && freezing(current)) { |
| 1123 | clear_thread_flag(TIF_SIGPENDING); |
| 1124 | goto again; |
| 1125 | } |
| 1126 | } else { |
| 1127 | do { |
| 1128 | msleep(TPM_TIMEOUT); |
| 1129 | status = chip->vendor.status(chip); |
| 1130 | if ((status & mask) == mask) |
| 1131 | return 0; |
| 1132 | } while (time_before(jiffies, stop)); |
| 1133 | } |
| 1134 | return -ETIME; |
| 1135 | } |
| 1136 | EXPORT_SYMBOL_GPL(wait_for_tpm_stat); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1137 | |
Kylene Jo Hall | e659a3f | 2005-10-30 15:03:24 -0800 | [diff] [blame] | 1138 | void tpm_remove_hardware(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1139 | { |
Kylene Jo Hall | e659a3f | 2005-10-30 15:03:24 -0800 | [diff] [blame] | 1140 | struct tpm_chip *chip = dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1141 | |
| 1142 | if (chip == NULL) { |
Kylene Jo Hall | e659a3f | 2005-10-30 15:03:24 -0800 | [diff] [blame] | 1143 | dev_err(dev, "No device data found\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1144 | return; |
| 1145 | } |
| 1146 | |
| 1147 | spin_lock(&driver_lock); |
Rajiv Andrade | f02a936 | 2008-10-11 09:04:23 +1100 | [diff] [blame] | 1148 | list_del_rcu(&chip->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1149 | spin_unlock(&driver_lock); |
Rajiv Andrade | f02a936 | 2008-10-11 09:04:23 +1100 | [diff] [blame] | 1150 | synchronize_rcu(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1151 | |
Jason Gunthorpe | afdba32 | 2013-11-26 13:30:40 -0700 | [diff] [blame^] | 1152 | tpm_dev_del_device(chip); |
Kylene Jo Hall | 90dda52 | 2006-04-22 02:37:15 -0700 | [diff] [blame] | 1153 | sysfs_remove_group(&dev->kobj, chip->vendor.attr_group); |
Gang Wei | 1631cfb | 2012-10-09 17:35:22 +0800 | [diff] [blame] | 1154 | tpm_remove_ppi(&dev->kobj); |
Kylene Jo Hall | 55a82ab | 2006-01-08 01:03:15 -0800 | [diff] [blame] | 1155 | tpm_bios_log_teardown(chip->bios_dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1156 | |
Richard MUSIL | 5bd91f1 | 2008-02-06 01:37:02 -0800 | [diff] [blame] | 1157 | /* write it this way to be explicit (chip->dev == dev) */ |
| 1158 | put_device(chip->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1159 | } |
Kylene Jo Hall | e659a3f | 2005-10-30 15:03:24 -0800 | [diff] [blame] | 1160 | EXPORT_SYMBOL_GPL(tpm_remove_hardware); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1161 | |
Rajiv Andrade | 225a9be | 2010-03-25 00:55:32 -0300 | [diff] [blame] | 1162 | #define TPM_ORD_SAVESTATE cpu_to_be32(152) |
| 1163 | #define SAVESTATE_RESULT_SIZE 10 |
| 1164 | |
| 1165 | static struct tpm_input_header savestate_header = { |
| 1166 | .tag = TPM_TAG_RQU_COMMAND, |
| 1167 | .length = cpu_to_be32(10), |
| 1168 | .ordinal = TPM_ORD_SAVESTATE |
| 1169 | }; |
| 1170 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1171 | /* |
| 1172 | * We are about to suspend. Save the TPM state |
| 1173 | * so that it can be restored. |
| 1174 | */ |
Rafael J. Wysocki | 035e2ce | 2012-07-06 19:09:01 +0200 | [diff] [blame] | 1175 | int tpm_pm_suspend(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1176 | { |
Kylene Jo Hall | ce2c87d | 2005-10-30 15:03:25 -0800 | [diff] [blame] | 1177 | struct tpm_chip *chip = dev_get_drvdata(dev); |
Rajiv Andrade | 225a9be | 2010-03-25 00:55:32 -0300 | [diff] [blame] | 1178 | struct tpm_cmd_t cmd; |
Duncan Laurie | 32d33b2 | 2013-03-17 14:56:39 -0700 | [diff] [blame] | 1179 | int rc, try; |
Rajiv Andrade | 225a9be | 2010-03-25 00:55:32 -0300 | [diff] [blame] | 1180 | |
| 1181 | u8 dummy_hash[TPM_DIGEST_SIZE] = { 0 }; |
David Smith | 2490c68 | 2008-01-14 00:55:12 -0800 | [diff] [blame] | 1182 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1183 | if (chip == NULL) |
| 1184 | return -ENODEV; |
| 1185 | |
Rajiv Andrade | 225a9be | 2010-03-25 00:55:32 -0300 | [diff] [blame] | 1186 | /* for buggy tpm, flush pcrs with extend to selected dummy */ |
| 1187 | if (tpm_suspend_pcr) { |
| 1188 | cmd.header.in = pcrextend_header; |
| 1189 | cmd.params.pcrextend_in.pcr_idx = cpu_to_be32(tpm_suspend_pcr); |
| 1190 | memcpy(cmd.params.pcrextend_in.hash, dummy_hash, |
| 1191 | TPM_DIGEST_SIZE); |
| 1192 | rc = transmit_cmd(chip, &cmd, EXTEND_PCR_RESULT_SIZE, |
| 1193 | "extending dummy pcr before suspend"); |
| 1194 | } |
| 1195 | |
| 1196 | /* now do the actual savestate */ |
Duncan Laurie | 32d33b2 | 2013-03-17 14:56:39 -0700 | [diff] [blame] | 1197 | for (try = 0; try < TPM_RETRY; try++) { |
| 1198 | cmd.header.in = savestate_header; |
| 1199 | rc = transmit_cmd(chip, &cmd, SAVESTATE_RESULT_SIZE, NULL); |
| 1200 | |
| 1201 | /* |
| 1202 | * If the TPM indicates that it is too busy to respond to |
| 1203 | * this command then retry before giving up. It can take |
| 1204 | * several seconds for this TPM to be ready. |
| 1205 | * |
| 1206 | * This can happen if the TPM has already been sent the |
| 1207 | * SaveState command before the driver has loaded. TCG 1.2 |
| 1208 | * specification states that any communication after SaveState |
| 1209 | * may cause the TPM to invalidate previously saved state. |
| 1210 | */ |
| 1211 | if (rc != TPM_WARN_RETRY) |
| 1212 | break; |
| 1213 | msleep(TPM_TIMEOUT_RETRY); |
| 1214 | } |
| 1215 | |
| 1216 | if (rc) |
| 1217 | dev_err(chip->dev, |
| 1218 | "Error (%d) sending savestate before suspend\n", rc); |
| 1219 | else if (try > 0) |
| 1220 | dev_warn(chip->dev, "TPM savestate took %dms\n", |
| 1221 | try * TPM_TIMEOUT_RETRY); |
| 1222 | |
Rajiv Andrade | 225a9be | 2010-03-25 00:55:32 -0300 | [diff] [blame] | 1223 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1224 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1225 | EXPORT_SYMBOL_GPL(tpm_pm_suspend); |
| 1226 | |
| 1227 | /* |
| 1228 | * Resume from a power safe. The BIOS already restored |
| 1229 | * the TPM state. |
| 1230 | */ |
Kylene Jo Hall | ce2c87d | 2005-10-30 15:03:25 -0800 | [diff] [blame] | 1231 | int tpm_pm_resume(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1232 | { |
Kylene Jo Hall | ce2c87d | 2005-10-30 15:03:25 -0800 | [diff] [blame] | 1233 | struct tpm_chip *chip = dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1234 | |
| 1235 | if (chip == NULL) |
| 1236 | return -ENODEV; |
| 1237 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1238 | return 0; |
| 1239 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1240 | EXPORT_SYMBOL_GPL(tpm_pm_resume); |
| 1241 | |
Kent Yoder | 41ab999 | 2012-06-07 13:47:14 -0500 | [diff] [blame] | 1242 | #define TPM_GETRANDOM_RESULT_SIZE 18 |
| 1243 | static struct tpm_input_header tpm_getrandom_header = { |
| 1244 | .tag = TPM_TAG_RQU_COMMAND, |
| 1245 | .length = cpu_to_be32(14), |
| 1246 | .ordinal = TPM_ORD_GET_RANDOM |
| 1247 | }; |
| 1248 | |
| 1249 | /** |
| 1250 | * tpm_get_random() - Get random bytes from the tpm's RNG |
| 1251 | * @chip_num: A specific chip number for the request or TPM_ANY_NUM |
| 1252 | * @out: destination buffer for the random bytes |
| 1253 | * @max: the max number of bytes to write to @out |
| 1254 | * |
| 1255 | * Returns < 0 on error and the number of bytes read on success |
| 1256 | */ |
| 1257 | int tpm_get_random(u32 chip_num, u8 *out, size_t max) |
| 1258 | { |
| 1259 | struct tpm_chip *chip; |
| 1260 | struct tpm_cmd_t tpm_cmd; |
| 1261 | u32 recd, num_bytes = min_t(u32, max, TPM_MAX_RNG_DATA); |
| 1262 | int err, total = 0, retries = 5; |
| 1263 | u8 *dest = out; |
| 1264 | |
| 1265 | chip = tpm_chip_find_get(chip_num); |
| 1266 | if (chip == NULL) |
| 1267 | return -ENODEV; |
| 1268 | |
| 1269 | if (!out || !num_bytes || max > TPM_MAX_RNG_DATA) |
| 1270 | return -EINVAL; |
| 1271 | |
| 1272 | do { |
| 1273 | tpm_cmd.header.in = tpm_getrandom_header; |
| 1274 | tpm_cmd.params.getrandom_in.num_bytes = cpu_to_be32(num_bytes); |
| 1275 | |
| 1276 | err = transmit_cmd(chip, &tpm_cmd, |
| 1277 | TPM_GETRANDOM_RESULT_SIZE + num_bytes, |
| 1278 | "attempting get random"); |
| 1279 | if (err) |
| 1280 | break; |
| 1281 | |
| 1282 | recd = be32_to_cpu(tpm_cmd.params.getrandom_out.rng_data_len); |
| 1283 | memcpy(dest, tpm_cmd.params.getrandom_out.rng_data, recd); |
| 1284 | |
| 1285 | dest += recd; |
| 1286 | total += recd; |
| 1287 | num_bytes -= recd; |
| 1288 | } while (retries-- && total < max); |
| 1289 | |
| 1290 | return total ? total : -EIO; |
| 1291 | } |
| 1292 | EXPORT_SYMBOL_GPL(tpm_get_random); |
| 1293 | |
Rajiv Andrade | 253115b | 2008-10-11 09:04:39 +1100 | [diff] [blame] | 1294 | /* In case vendor provided release function, call it too.*/ |
| 1295 | |
| 1296 | void tpm_dev_vendor_release(struct tpm_chip *chip) |
| 1297 | { |
Andi Shyti | 3072928 | 2012-05-28 18:42:45 +0200 | [diff] [blame] | 1298 | if (!chip) |
| 1299 | return; |
| 1300 | |
Rajiv Andrade | 253115b | 2008-10-11 09:04:39 +1100 | [diff] [blame] | 1301 | if (chip->vendor.release) |
| 1302 | chip->vendor.release(chip->dev); |
| 1303 | |
| 1304 | clear_bit(chip->dev_num, dev_mask); |
Rajiv Andrade | 253115b | 2008-10-11 09:04:39 +1100 | [diff] [blame] | 1305 | } |
| 1306 | EXPORT_SYMBOL_GPL(tpm_dev_vendor_release); |
| 1307 | |
| 1308 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1309 | /* |
Richard MUSIL | 5bd91f1 | 2008-02-06 01:37:02 -0800 | [diff] [blame] | 1310 | * Once all references to platform device are down to 0, |
| 1311 | * release all allocated structures. |
Richard MUSIL | 5bd91f1 | 2008-02-06 01:37:02 -0800 | [diff] [blame] | 1312 | */ |
Jason Gunthorpe | afdba32 | 2013-11-26 13:30:40 -0700 | [diff] [blame^] | 1313 | static void tpm_dev_release(struct device *dev) |
Richard MUSIL | 5bd91f1 | 2008-02-06 01:37:02 -0800 | [diff] [blame] | 1314 | { |
| 1315 | struct tpm_chip *chip = dev_get_drvdata(dev); |
| 1316 | |
Andi Shyti | 3072928 | 2012-05-28 18:42:45 +0200 | [diff] [blame] | 1317 | if (!chip) |
| 1318 | return; |
| 1319 | |
Rajiv Andrade | 253115b | 2008-10-11 09:04:39 +1100 | [diff] [blame] | 1320 | tpm_dev_vendor_release(chip); |
Richard MUSIL | 5bd91f1 | 2008-02-06 01:37:02 -0800 | [diff] [blame] | 1321 | |
Rajiv Andrade | 253115b | 2008-10-11 09:04:39 +1100 | [diff] [blame] | 1322 | chip->release(dev); |
Richard MUSIL | 5bd91f1 | 2008-02-06 01:37:02 -0800 | [diff] [blame] | 1323 | kfree(chip); |
| 1324 | } |
| 1325 | |
| 1326 | /* |
Peter Huewe | 0a41826 | 2013-10-22 00:29:14 +0200 | [diff] [blame] | 1327 | * Called from tpm_<specific>.c probe function only for devices |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1328 | * the driver has determined it should claim. Prior to calling |
| 1329 | * this function the specific probe function has called pci_enable_device |
| 1330 | * upon errant exit from this function specific probe function should call |
| 1331 | * pci_disable_device |
| 1332 | */ |
Rajiv Andrade | f02a936 | 2008-10-11 09:04:23 +1100 | [diff] [blame] | 1333 | struct tpm_chip *tpm_register_hardware(struct device *dev, |
| 1334 | const struct tpm_vendor_specific *entry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1335 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1336 | struct tpm_chip *chip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1337 | |
| 1338 | /* Driver specific per-device data */ |
Andrew Morton | b888c87 | 2005-10-30 15:03:28 -0800 | [diff] [blame] | 1339 | chip = kzalloc(sizeof(*chip), GFP_KERNEL); |
Parag Warudkar | 8e39c93 | 2007-05-08 00:31:09 -0700 | [diff] [blame] | 1340 | |
Jason Gunthorpe | 6aff1fd | 2013-09-22 14:19:18 -0600 | [diff] [blame] | 1341 | if (chip == NULL) |
| 1342 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1343 | |
Matthias Kaehlcke | d081d47 | 2007-05-08 00:32:02 -0700 | [diff] [blame] | 1344 | mutex_init(&chip->tpm_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1345 | INIT_LIST_HEAD(&chip->list); |
| 1346 | |
Kylene Jo Hall | 90dda52 | 2006-04-22 02:37:15 -0700 | [diff] [blame] | 1347 | memcpy(&chip->vendor, entry, sizeof(struct tpm_vendor_specific)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1348 | |
Kylene Jo Hall | 10685a9 | 2006-04-22 02:38:32 -0700 | [diff] [blame] | 1349 | chip->dev_num = find_first_zero_bit(dev_mask, TPM_NUM_DEVICES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1350 | |
Kylene Jo Hall | 10685a9 | 2006-04-22 02:38:32 -0700 | [diff] [blame] | 1351 | if (chip->dev_num >= TPM_NUM_DEVICES) { |
Andrew Morton | b888c87 | 2005-10-30 15:03:28 -0800 | [diff] [blame] | 1352 | dev_err(dev, "No available tpm device numbers\n"); |
Andrew Morton | dd78c94 | 2008-10-15 22:04:37 -0700 | [diff] [blame] | 1353 | goto out_free; |
Jason Gunthorpe | afdba32 | 2013-11-26 13:30:40 -0700 | [diff] [blame^] | 1354 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1355 | |
Kylene Jo Hall | 10685a9 | 2006-04-22 02:38:32 -0700 | [diff] [blame] | 1356 | set_bit(chip->dev_num, dev_mask); |
| 1357 | |
Jason Gunthorpe | 6aff1fd | 2013-09-22 14:19:18 -0600 | [diff] [blame] | 1358 | scnprintf(chip->devname, sizeof(chip->devname), "%s%d", "tpm", |
| 1359 | chip->dev_num); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1360 | |
Kylene Jo Hall | e659a3f | 2005-10-30 15:03:24 -0800 | [diff] [blame] | 1361 | chip->dev = get_device(dev); |
Richard MUSIL | 5bd91f1 | 2008-02-06 01:37:02 -0800 | [diff] [blame] | 1362 | chip->release = dev->release; |
| 1363 | dev->release = tpm_dev_release; |
| 1364 | dev_set_drvdata(dev, chip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1365 | |
Jason Gunthorpe | afdba32 | 2013-11-26 13:30:40 -0700 | [diff] [blame^] | 1366 | if (tpm_dev_add_device(chip)) |
Wanlong Gao | dad79cb | 2012-05-16 16:24:28 +0800 | [diff] [blame] | 1367 | goto put_device; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1368 | |
Jason Gunthorpe | afdba32 | 2013-11-26 13:30:40 -0700 | [diff] [blame^] | 1369 | if (sysfs_create_group(&dev->kobj, chip->vendor.attr_group)) |
| 1370 | goto del_misc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1371 | |
Jason Gunthorpe | afdba32 | 2013-11-26 13:30:40 -0700 | [diff] [blame^] | 1372 | if (tpm_add_ppi(&dev->kobj)) |
| 1373 | goto del_misc; |
Xiaoyan Zhang | f84fdff | 2012-08-22 18:47:22 +0800 | [diff] [blame] | 1374 | |
Jason Gunthorpe | 6aff1fd | 2013-09-22 14:19:18 -0600 | [diff] [blame] | 1375 | chip->bios_dir = tpm_bios_log_setup(chip->devname); |
Kylene Jo Hall | 55a82ab | 2006-01-08 01:03:15 -0800 | [diff] [blame] | 1376 | |
Rajiv Andrade | f02a936 | 2008-10-11 09:04:23 +1100 | [diff] [blame] | 1377 | /* Make chip available */ |
| 1378 | spin_lock(&driver_lock); |
| 1379 | list_add_rcu(&chip->list, &tpm_chip_list); |
| 1380 | spin_unlock(&driver_lock); |
| 1381 | |
Kylene Jo Hall | e0dd03c | 2006-04-22 02:37:26 -0700 | [diff] [blame] | 1382 | return chip; |
Andrew Morton | dd78c94 | 2008-10-15 22:04:37 -0700 | [diff] [blame] | 1383 | |
Jason Gunthorpe | afdba32 | 2013-11-26 13:30:40 -0700 | [diff] [blame^] | 1384 | del_misc: |
| 1385 | tpm_dev_del_device(chip); |
Wanlong Gao | dad79cb | 2012-05-16 16:24:28 +0800 | [diff] [blame] | 1386 | put_device: |
| 1387 | put_device(chip->dev); |
Andrew Morton | dd78c94 | 2008-10-15 22:04:37 -0700 | [diff] [blame] | 1388 | out_free: |
| 1389 | kfree(chip); |
Andrew Morton | dd78c94 | 2008-10-15 22:04:37 -0700 | [diff] [blame] | 1390 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1391 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1392 | EXPORT_SYMBOL_GPL(tpm_register_hardware); |
| 1393 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1394 | MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)"); |
| 1395 | MODULE_DESCRIPTION("TPM Driver"); |
| 1396 | MODULE_VERSION("2.0"); |
| 1397 | MODULE_LICENSE("GPL"); |