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