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