blob: 62a5682578cace94bd6dcca7306a0a5a47df9a4a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Yoder8e81cc12007-08-22 14:01:04 -070010 * Maintained by: <tpmdd-devel@lists.sourceforge.net>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
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 Aravamudan700d8bd2005-06-23 22:01:47 -070022 * calls to msleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 *
24 */
25
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/poll.h>
Matthias Kaehlcked081d472007-05-08 00:32:02 -070027#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/spinlock.h>
Arnd Bergmann25368ca2008-05-20 19:16:51 +020029#include <linux/smp_lock.h>
Matthias Kaehlcked081d472007-05-08 00:32:02 -070030
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "tpm.h"
32
Kylene Hall3122a882005-06-23 22:01:48 -070033enum tpm_const {
34 TPM_MINOR = 224, /* officially assigned */
35 TPM_BUFSIZE = 2048,
36 TPM_NUM_DEVICES = 256,
Kylene Hall3122a882005-06-23 22:01:48 -070037};
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Kylene Jo Hall9e18ee12006-04-22 02:37:38 -070039enum tpm_duration {
40 TPM_SHORT = 0,
41 TPM_MEDIUM = 1,
42 TPM_LONG = 2,
43 TPM_UNDEFINED,
44};
45
46#define TPM_MAX_ORDINAL 243
47#define TPM_MAX_PROTECTED_ORDINAL 12
48#define TPM_PROTECTED_ORDINAL_MASK 0xFF
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050static LIST_HEAD(tpm_chip_list);
51static DEFINE_SPINLOCK(driver_lock);
Kylene Jo Hall10685a92006-04-22 02:38:32 -070052static DECLARE_BITMAP(dev_mask, TPM_NUM_DEVICES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Kylene Jo Hall9e18ee12006-04-22 02:37:38 -070054/*
55 * Array with one entry per ordinal defining the maximum amount
56 * of time the chip could take to return the result. The ordinal
57 * designation of short, medium or long is defined in a table in
58 * TCG Specification TPM Main Part 2 TPM Structures Section 17. The
59 * values of the SHORT, MEDIUM, and LONG durations are retrieved
60 * from the chip during initialization with a call to tpm_get_timeouts.
61 */
62static const u8 tpm_protected_ordinal_duration[TPM_MAX_PROTECTED_ORDINAL] = {
63 TPM_UNDEFINED, /* 0 */
64 TPM_UNDEFINED,
65 TPM_UNDEFINED,
66 TPM_UNDEFINED,
67 TPM_UNDEFINED,
68 TPM_UNDEFINED, /* 5 */
69 TPM_UNDEFINED,
70 TPM_UNDEFINED,
71 TPM_UNDEFINED,
72 TPM_UNDEFINED,
73 TPM_SHORT, /* 10 */
74 TPM_SHORT,
75};
76
77static const u8 tpm_ordinal_duration[TPM_MAX_ORDINAL] = {
78 TPM_UNDEFINED, /* 0 */
79 TPM_UNDEFINED,
80 TPM_UNDEFINED,
81 TPM_UNDEFINED,
82 TPM_UNDEFINED,
83 TPM_UNDEFINED, /* 5 */
84 TPM_UNDEFINED,
85 TPM_UNDEFINED,
86 TPM_UNDEFINED,
87 TPM_UNDEFINED,
88 TPM_SHORT, /* 10 */
89 TPM_SHORT,
90 TPM_MEDIUM,
91 TPM_LONG,
92 TPM_LONG,
93 TPM_MEDIUM, /* 15 */
94 TPM_SHORT,
95 TPM_SHORT,
96 TPM_MEDIUM,
97 TPM_LONG,
98 TPM_SHORT, /* 20 */
99 TPM_SHORT,
100 TPM_MEDIUM,
101 TPM_MEDIUM,
102 TPM_MEDIUM,
103 TPM_SHORT, /* 25 */
104 TPM_SHORT,
105 TPM_MEDIUM,
106 TPM_SHORT,
107 TPM_SHORT,
108 TPM_MEDIUM, /* 30 */
109 TPM_LONG,
110 TPM_MEDIUM,
111 TPM_SHORT,
112 TPM_SHORT,
113 TPM_SHORT, /* 35 */
114 TPM_MEDIUM,
115 TPM_MEDIUM,
116 TPM_UNDEFINED,
117 TPM_UNDEFINED,
118 TPM_MEDIUM, /* 40 */
119 TPM_LONG,
120 TPM_MEDIUM,
121 TPM_SHORT,
122 TPM_SHORT,
123 TPM_SHORT, /* 45 */
124 TPM_SHORT,
125 TPM_SHORT,
126 TPM_SHORT,
127 TPM_LONG,
128 TPM_MEDIUM, /* 50 */
129 TPM_MEDIUM,
130 TPM_UNDEFINED,
131 TPM_UNDEFINED,
132 TPM_UNDEFINED,
133 TPM_UNDEFINED, /* 55 */
134 TPM_UNDEFINED,
135 TPM_UNDEFINED,
136 TPM_UNDEFINED,
137 TPM_UNDEFINED,
138 TPM_MEDIUM, /* 60 */
139 TPM_MEDIUM,
140 TPM_MEDIUM,
141 TPM_SHORT,
142 TPM_SHORT,
143 TPM_MEDIUM, /* 65 */
144 TPM_UNDEFINED,
145 TPM_UNDEFINED,
146 TPM_UNDEFINED,
147 TPM_UNDEFINED,
148 TPM_SHORT, /* 70 */
149 TPM_SHORT,
150 TPM_UNDEFINED,
151 TPM_UNDEFINED,
152 TPM_UNDEFINED,
153 TPM_UNDEFINED, /* 75 */
154 TPM_UNDEFINED,
155 TPM_UNDEFINED,
156 TPM_UNDEFINED,
157 TPM_UNDEFINED,
158 TPM_LONG, /* 80 */
159 TPM_UNDEFINED,
160 TPM_MEDIUM,
161 TPM_LONG,
162 TPM_SHORT,
163 TPM_UNDEFINED, /* 85 */
164 TPM_UNDEFINED,
165 TPM_UNDEFINED,
166 TPM_UNDEFINED,
167 TPM_UNDEFINED,
168 TPM_SHORT, /* 90 */
169 TPM_SHORT,
170 TPM_SHORT,
171 TPM_SHORT,
172 TPM_SHORT,
173 TPM_UNDEFINED, /* 95 */
174 TPM_UNDEFINED,
175 TPM_UNDEFINED,
176 TPM_UNDEFINED,
177 TPM_UNDEFINED,
178 TPM_MEDIUM, /* 100 */
179 TPM_SHORT,
180 TPM_SHORT,
181 TPM_UNDEFINED,
182 TPM_UNDEFINED,
183 TPM_UNDEFINED, /* 105 */
184 TPM_UNDEFINED,
185 TPM_UNDEFINED,
186 TPM_UNDEFINED,
187 TPM_UNDEFINED,
188 TPM_SHORT, /* 110 */
189 TPM_SHORT,
190 TPM_SHORT,
191 TPM_SHORT,
192 TPM_SHORT,
193 TPM_SHORT, /* 115 */
194 TPM_SHORT,
195 TPM_SHORT,
196 TPM_UNDEFINED,
197 TPM_UNDEFINED,
198 TPM_LONG, /* 120 */
199 TPM_LONG,
200 TPM_MEDIUM,
201 TPM_UNDEFINED,
202 TPM_SHORT,
203 TPM_SHORT, /* 125 */
204 TPM_SHORT,
205 TPM_LONG,
206 TPM_SHORT,
207 TPM_SHORT,
208 TPM_SHORT, /* 130 */
209 TPM_MEDIUM,
210 TPM_UNDEFINED,
211 TPM_SHORT,
212 TPM_MEDIUM,
213 TPM_UNDEFINED, /* 135 */
214 TPM_UNDEFINED,
215 TPM_UNDEFINED,
216 TPM_UNDEFINED,
217 TPM_UNDEFINED,
218 TPM_SHORT, /* 140 */
219 TPM_SHORT,
220 TPM_UNDEFINED,
221 TPM_UNDEFINED,
222 TPM_UNDEFINED,
223 TPM_UNDEFINED, /* 145 */
224 TPM_UNDEFINED,
225 TPM_UNDEFINED,
226 TPM_UNDEFINED,
227 TPM_UNDEFINED,
228 TPM_SHORT, /* 150 */
229 TPM_MEDIUM,
230 TPM_MEDIUM,
231 TPM_SHORT,
232 TPM_SHORT,
233 TPM_UNDEFINED, /* 155 */
234 TPM_UNDEFINED,
235 TPM_UNDEFINED,
236 TPM_UNDEFINED,
237 TPM_UNDEFINED,
238 TPM_SHORT, /* 160 */
239 TPM_SHORT,
240 TPM_SHORT,
241 TPM_SHORT,
242 TPM_UNDEFINED,
243 TPM_UNDEFINED, /* 165 */
244 TPM_UNDEFINED,
245 TPM_UNDEFINED,
246 TPM_UNDEFINED,
247 TPM_UNDEFINED,
248 TPM_LONG, /* 170 */
249 TPM_UNDEFINED,
250 TPM_UNDEFINED,
251 TPM_UNDEFINED,
252 TPM_UNDEFINED,
253 TPM_UNDEFINED, /* 175 */
254 TPM_UNDEFINED,
255 TPM_UNDEFINED,
256 TPM_UNDEFINED,
257 TPM_UNDEFINED,
258 TPM_MEDIUM, /* 180 */
259 TPM_SHORT,
260 TPM_MEDIUM,
261 TPM_MEDIUM,
262 TPM_MEDIUM,
263 TPM_MEDIUM, /* 185 */
264 TPM_SHORT,
265 TPM_UNDEFINED,
266 TPM_UNDEFINED,
267 TPM_UNDEFINED,
268 TPM_UNDEFINED, /* 190 */
269 TPM_UNDEFINED,
270 TPM_UNDEFINED,
271 TPM_UNDEFINED,
272 TPM_UNDEFINED,
273 TPM_UNDEFINED, /* 195 */
274 TPM_UNDEFINED,
275 TPM_UNDEFINED,
276 TPM_UNDEFINED,
277 TPM_UNDEFINED,
278 TPM_SHORT, /* 200 */
279 TPM_UNDEFINED,
280 TPM_UNDEFINED,
281 TPM_UNDEFINED,
282 TPM_SHORT,
283 TPM_SHORT, /* 205 */
284 TPM_SHORT,
285 TPM_SHORT,
286 TPM_SHORT,
287 TPM_SHORT,
288 TPM_MEDIUM, /* 210 */
289 TPM_UNDEFINED,
290 TPM_MEDIUM,
291 TPM_MEDIUM,
292 TPM_MEDIUM,
293 TPM_UNDEFINED, /* 215 */
294 TPM_MEDIUM,
295 TPM_UNDEFINED,
296 TPM_UNDEFINED,
297 TPM_SHORT,
298 TPM_SHORT, /* 220 */
299 TPM_SHORT,
300 TPM_SHORT,
301 TPM_SHORT,
302 TPM_SHORT,
303 TPM_UNDEFINED, /* 225 */
304 TPM_UNDEFINED,
305 TPM_UNDEFINED,
306 TPM_UNDEFINED,
307 TPM_UNDEFINED,
308 TPM_SHORT, /* 230 */
309 TPM_LONG,
310 TPM_MEDIUM,
311 TPM_UNDEFINED,
312 TPM_UNDEFINED,
313 TPM_UNDEFINED, /* 235 */
314 TPM_UNDEFINED,
315 TPM_UNDEFINED,
316 TPM_UNDEFINED,
317 TPM_UNDEFINED,
318 TPM_SHORT, /* 240 */
319 TPM_UNDEFINED,
320 TPM_MEDIUM,
321};
322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323static void user_reader_timeout(unsigned long ptr)
324{
325 struct tpm_chip *chip = (struct tpm_chip *) ptr;
326
Kylene Jo Hall09e12f92005-11-13 16:07:43 -0800327 schedule_work(&chip->work);
328}
329
David Howellsc4028952006-11-22 14:57:56 +0000330static void timeout_work(struct work_struct *work)
Kylene Jo Hall09e12f92005-11-13 16:07:43 -0800331{
David Howellsc4028952006-11-22 14:57:56 +0000332 struct tpm_chip *chip = container_of(work, struct tpm_chip, work);
Kylene Jo Hall09e12f92005-11-13 16:07:43 -0800333
Matthias Kaehlcked081d472007-05-08 00:32:02 -0700334 mutex_lock(&chip->buffer_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 atomic_set(&chip->data_pending, 0);
336 memset(chip->data_buffer, 0, TPM_BUFSIZE);
Matthias Kaehlcked081d472007-05-08 00:32:02 -0700337 mutex_unlock(&chip->buffer_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338}
339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340/*
Kylene Jo Hall9e18ee12006-04-22 02:37:38 -0700341 * Returns max number of jiffies to wait
342 */
343unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip,
344 u32 ordinal)
345{
346 int duration_idx = TPM_UNDEFINED;
347 int duration = 0;
348
349 if (ordinal < TPM_MAX_ORDINAL)
350 duration_idx = tpm_ordinal_duration[ordinal];
351 else if ((ordinal & TPM_PROTECTED_ORDINAL_MASK) <
352 TPM_MAX_PROTECTED_ORDINAL)
353 duration_idx =
354 tpm_protected_ordinal_duration[ordinal &
355 TPM_PROTECTED_ORDINAL_MASK];
356
357 if (duration_idx != TPM_UNDEFINED)
Kylene Jo Hall36b20022006-04-22 02:38:19 -0700358 duration = chip->vendor.duration[duration_idx];
Kylene Jo Hall9e18ee12006-04-22 02:37:38 -0700359 if (duration <= 0)
360 return 2 * 60 * HZ;
361 else
362 return duration;
363}
364EXPORT_SYMBOL_GPL(tpm_calc_ordinal_duration);
365
366/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 * Internal kernel interface to transmit TPM commands
368 */
369static ssize_t tpm_transmit(struct tpm_chip *chip, const char *buf,
370 size_t bufsiz)
371{
Kylene Halld9e5b6b2005-06-23 22:02:02 -0700372 ssize_t rc;
Kylene Jo Hall9e18ee12006-04-22 02:37:38 -0700373 u32 count, ordinal;
Nishanth Aravamudan700d8bd2005-06-23 22:01:47 -0700374 unsigned long stop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Kylene Hall81179bb2005-06-23 22:01:59 -0700376 count = be32_to_cpu(*((__be32 *) (buf + 2)));
Kylene Jo Hall9e18ee12006-04-22 02:37:38 -0700377 ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 if (count == 0)
379 return -ENODATA;
380 if (count > bufsiz) {
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800381 dev_err(chip->dev,
Al Virob76be682005-04-26 07:43:41 -0700382 "invalid count value %x %zx \n", count, bufsiz);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 return -E2BIG;
384 }
385
Matthias Kaehlcked081d472007-05-08 00:32:02 -0700386 mutex_lock(&chip->tpm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Kylene Jo Hall90dda522006-04-22 02:37:15 -0700388 if ((rc = chip->vendor.send(chip, (u8 *) buf, count)) < 0) {
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800389 dev_err(chip->dev,
Kylene Halld9e5b6b2005-06-23 22:02:02 -0700390 "tpm_transmit: tpm_send: error %zd\n", rc);
391 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 }
393
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700394 if (chip->vendor.irq)
395 goto out_recv;
396
Kylene Jo Hall9e18ee12006-04-22 02:37:38 -0700397 stop = jiffies + tpm_calc_ordinal_duration(chip, ordinal);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 do {
Kylene Jo Hall90dda522006-04-22 02:37:15 -0700399 u8 status = chip->vendor.status(chip);
400 if ((status & chip->vendor.req_complete_mask) ==
401 chip->vendor.req_complete_val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 goto out_recv;
Kylene Halld9e5b6b2005-06-23 22:02:02 -0700403
Kylene Jo Hall90dda522006-04-22 02:37:15 -0700404 if ((status == chip->vendor.req_canceled)) {
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800405 dev_err(chip->dev, "Operation Canceled\n");
Kylene Halld9e5b6b2005-06-23 22:02:02 -0700406 rc = -ECANCELED;
407 goto out;
408 }
409
410 msleep(TPM_TIMEOUT); /* CHECK */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 rmb();
Nishanth Aravamudan700d8bd2005-06-23 22:01:47 -0700412 } while (time_before(jiffies, stop));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Kylene Jo Hall90dda522006-04-22 02:37:15 -0700414 chip->vendor.cancel(chip);
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800415 dev_err(chip->dev, "Operation Timed out\n");
Kylene Halld9e5b6b2005-06-23 22:02:02 -0700416 rc = -ETIME;
417 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
419out_recv:
Kylene Jo Hall90dda522006-04-22 02:37:15 -0700420 rc = chip->vendor.recv(chip, (u8 *) buf, bufsiz);
Kylene Halld9e5b6b2005-06-23 22:02:02 -0700421 if (rc < 0)
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800422 dev_err(chip->dev,
Kylene Halld9e5b6b2005-06-23 22:02:02 -0700423 "tpm_transmit: tpm_recv: error %zd\n", rc);
424out:
Matthias Kaehlcked081d472007-05-08 00:32:02 -0700425 mutex_unlock(&chip->tpm_mutex);
Kylene Halld9e5b6b2005-06-23 22:02:02 -0700426 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427}
428
429#define TPM_DIGEST_SIZE 20
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700430#define TPM_ERROR_SIZE 10
431#define TPM_RET_CODE_IDX 6
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700432
433enum tpm_capabilities {
Rajiv Andrade08837432009-02-02 15:23:43 -0200434 TPM_CAP_FLAG = cpu_to_be32(4),
435 TPM_CAP_PROP = cpu_to_be32(5),
436 CAP_VERSION_1_1 = cpu_to_be32(0x06),
437 CAP_VERSION_1_2 = cpu_to_be32(0x1A)
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700438};
439
440enum tpm_sub_capabilities {
Rajiv Andrade08837432009-02-02 15:23:43 -0200441 TPM_CAP_PROP_PCR = cpu_to_be32(0x101),
442 TPM_CAP_PROP_MANUFACTURER = cpu_to_be32(0x103),
443 TPM_CAP_FLAG_PERM = cpu_to_be32(0x108),
444 TPM_CAP_FLAG_VOL = cpu_to_be32(0x109),
445 TPM_CAP_PROP_OWNER = cpu_to_be32(0x111),
446 TPM_CAP_PROP_TIS_TIMEOUT = cpu_to_be32(0x115),
447 TPM_CAP_PROP_TIS_DURATION = cpu_to_be32(0x120),
448
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700449};
450
Rajiv Andrade08837432009-02-02 15:23:43 -0200451static ssize_t transmit_cmd(struct tpm_chip *chip, struct tpm_cmd_t *cmd,
452 int len, const char *desc)
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700453{
454 int err;
455
Rajiv Andrade08837432009-02-02 15:23:43 -0200456 len = tpm_transmit(chip,(u8 *) cmd, len);
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700457 if (len < 0)
458 return len;
459 if (len == TPM_ERROR_SIZE) {
Rajiv Andrade08837432009-02-02 15:23:43 -0200460 err = be32_to_cpu(cmd->header.out.return_code);
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700461 dev_dbg(chip->dev, "A TPM error (%d) occurred %s\n", err, desc);
462 return err;
463 }
464 return 0;
465}
466
Rajiv Andrade08837432009-02-02 15:23:43 -0200467#define TPM_INTERNAL_RESULT_SIZE 200
468#define TPM_TAG_RQU_COMMAND cpu_to_be16(193)
469#define TPM_ORD_GET_CAP cpu_to_be32(101)
470
471static const struct tpm_input_header tpm_getcap_header = {
472 .tag = TPM_TAG_RQU_COMMAND,
473 .length = cpu_to_be32(22),
474 .ordinal = TPM_ORD_GET_CAP
475};
476
477ssize_t tpm_getcap(struct device *dev, __be32 subcap_id, cap_t *cap,
478 const char *desc)
479{
480 struct tpm_cmd_t tpm_cmd;
481 int rc;
482 struct tpm_chip *chip = dev_get_drvdata(dev);
483
484 tpm_cmd.header.in = tpm_getcap_header;
485 if (subcap_id == CAP_VERSION_1_1 || subcap_id == CAP_VERSION_1_2) {
486 tpm_cmd.params.getcap_in.cap = subcap_id;
487 /*subcap field not necessary */
488 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(0);
489 tpm_cmd.header.in.length -= cpu_to_be32(sizeof(__be32));
490 } else {
491 if (subcap_id == TPM_CAP_FLAG_PERM ||
492 subcap_id == TPM_CAP_FLAG_VOL)
493 tpm_cmd.params.getcap_in.cap = TPM_CAP_FLAG;
494 else
495 tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
496 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
497 tpm_cmd.params.getcap_in.subcap = subcap_id;
498 }
499 rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE, desc);
500 if (!rc)
501 *cap = tpm_cmd.params.getcap_out.cap;
502 return rc;
503}
504
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700505void tpm_gen_interrupt(struct tpm_chip *chip)
506{
Rajiv Andrade08837432009-02-02 15:23:43 -0200507 struct tpm_cmd_t tpm_cmd;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700508 ssize_t rc;
509
Rajiv Andrade08837432009-02-02 15:23:43 -0200510 tpm_cmd.header.in = tpm_getcap_header;
511 tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
512 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
513 tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_TIMEOUT;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700514
Rajiv Andrade08837432009-02-02 15:23:43 -0200515 rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE,
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700516 "attempting to determine the timeouts");
517}
518EXPORT_SYMBOL_GPL(tpm_gen_interrupt);
519
520void tpm_get_timeouts(struct tpm_chip *chip)
521{
Rajiv Andrade08837432009-02-02 15:23:43 -0200522 struct tpm_cmd_t tpm_cmd;
523 struct timeout_t *timeout_cap;
524 struct duration_t *duration_cap;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700525 ssize_t rc;
526 u32 timeout;
527
Rajiv Andrade08837432009-02-02 15:23:43 -0200528 tpm_cmd.header.in = tpm_getcap_header;
529 tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
530 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
531 tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_TIMEOUT;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700532
Rajiv Andrade08837432009-02-02 15:23:43 -0200533 rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE,
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700534 "attempting to determine the timeouts");
535 if (rc)
536 goto duration;
537
Rajiv Andrade08837432009-02-02 15:23:43 -0200538 if (be32_to_cpu(tpm_cmd.header.out.length)
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700539 != 4 * sizeof(u32))
540 goto duration;
541
Rajiv Andrade08837432009-02-02 15:23:43 -0200542 timeout_cap = &tpm_cmd.params.getcap_out.cap.timeout;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700543 /* Don't overwrite default if value is 0 */
Rajiv Andrade08837432009-02-02 15:23:43 -0200544 timeout = be32_to_cpu(timeout_cap->a);
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700545 if (timeout)
Marcin Obara9e5b1b12008-10-15 22:04:34 -0700546 chip->vendor.timeout_a = usecs_to_jiffies(timeout);
Rajiv Andrade08837432009-02-02 15:23:43 -0200547 timeout = be32_to_cpu(timeout_cap->b);
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700548 if (timeout)
Marcin Obara9e5b1b12008-10-15 22:04:34 -0700549 chip->vendor.timeout_b = usecs_to_jiffies(timeout);
Rajiv Andrade08837432009-02-02 15:23:43 -0200550 timeout = be32_to_cpu(timeout_cap->c);
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700551 if (timeout)
Marcin Obara9e5b1b12008-10-15 22:04:34 -0700552 chip->vendor.timeout_c = usecs_to_jiffies(timeout);
Rajiv Andrade08837432009-02-02 15:23:43 -0200553 timeout = be32_to_cpu(timeout_cap->d);
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700554 if (timeout)
Marcin Obara9e5b1b12008-10-15 22:04:34 -0700555 chip->vendor.timeout_d = usecs_to_jiffies(timeout);
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700556
557duration:
Rajiv Andrade08837432009-02-02 15:23:43 -0200558 tpm_cmd.header.in = tpm_getcap_header;
559 tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
560 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
561 tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_DURATION;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700562
Rajiv Andrade08837432009-02-02 15:23:43 -0200563 rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE,
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700564 "attempting to determine the durations");
565 if (rc)
566 return;
567
Rajiv Andrade08837432009-02-02 15:23:43 -0200568 if (be32_to_cpu(tpm_cmd.header.out.return_code)
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700569 != 3 * sizeof(u32))
570 return;
Rajiv Andrade08837432009-02-02 15:23:43 -0200571 duration_cap = &tpm_cmd.params.getcap_out.cap.duration;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700572 chip->vendor.duration[TPM_SHORT] =
Rajiv Andrade08837432009-02-02 15:23:43 -0200573 usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_short));
Valdis Kletnieks292cf4a2008-10-15 22:04:35 -0700574 /* The Broadcom BCM0102 chipset in a Dell Latitude D820 gets the above
575 * value wrong and apparently reports msecs rather than usecs. So we
576 * fix up the resulting too-small TPM_SHORT value to make things work.
577 */
578 if (chip->vendor.duration[TPM_SHORT] < (HZ/100))
579 chip->vendor.duration[TPM_SHORT] = HZ;
580
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700581 chip->vendor.duration[TPM_MEDIUM] =
Rajiv Andrade08837432009-02-02 15:23:43 -0200582 usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_medium));
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700583 chip->vendor.duration[TPM_LONG] =
Rajiv Andrade08837432009-02-02 15:23:43 -0200584 usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_long));
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700585}
586EXPORT_SYMBOL_GPL(tpm_get_timeouts);
587
588void tpm_continue_selftest(struct tpm_chip *chip)
589{
590 u8 data[] = {
591 0, 193, /* TPM_TAG_RQU_COMMAND */
592 0, 0, 0, 10, /* length */
593 0, 0, 0, 83, /* TPM_ORD_GetCapability */
594 };
595
596 tpm_transmit(chip, data, sizeof(data));
597}
598EXPORT_SYMBOL_GPL(tpm_continue_selftest);
599
600ssize_t tpm_show_enabled(struct device * dev, struct device_attribute * attr,
601 char *buf)
602{
Rajiv Andrade08837432009-02-02 15:23:43 -0200603 cap_t cap;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700604 ssize_t rc;
605
Rajiv Andrade08837432009-02-02 15:23:43 -0200606 rc = tpm_getcap(dev, TPM_CAP_FLAG_PERM, &cap,
607 "attempting to determine the permanent enabled state");
608 if (rc)
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700609 return 0;
Marcin Obaraec288bd2008-07-25 19:44:59 -0700610
Rajiv Andrade08837432009-02-02 15:23:43 -0200611 rc = sprintf(buf, "%d\n", !cap.perm_flags.disable);
Marcin Obaraec288bd2008-07-25 19:44:59 -0700612 return rc;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700613}
614EXPORT_SYMBOL_GPL(tpm_show_enabled);
615
616ssize_t tpm_show_active(struct device * dev, struct device_attribute * attr,
617 char *buf)
618{
Rajiv Andrade08837432009-02-02 15:23:43 -0200619 cap_t cap;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700620 ssize_t rc;
621
Rajiv Andrade08837432009-02-02 15:23:43 -0200622 rc = tpm_getcap(dev, TPM_CAP_FLAG_PERM, &cap,
623 "attempting to determine the permanent active state");
624 if (rc)
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700625 return 0;
Marcin Obaraec288bd2008-07-25 19:44:59 -0700626
Rajiv Andrade08837432009-02-02 15:23:43 -0200627 rc = sprintf(buf, "%d\n", !cap.perm_flags.deactivated);
Marcin Obaraec288bd2008-07-25 19:44:59 -0700628 return rc;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700629}
630EXPORT_SYMBOL_GPL(tpm_show_active);
631
632ssize_t tpm_show_owned(struct device * dev, struct device_attribute * attr,
633 char *buf)
634{
Rajiv Andrade08837432009-02-02 15:23:43 -0200635 cap_t cap;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700636 ssize_t rc;
637
Rajiv Andrade08837432009-02-02 15:23:43 -0200638 rc = tpm_getcap(dev, TPM_CAP_PROP_OWNER, &cap,
639 "attempting to determine the owner state");
640 if (rc)
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700641 return 0;
Marcin Obaraec288bd2008-07-25 19:44:59 -0700642
Rajiv Andrade08837432009-02-02 15:23:43 -0200643 rc = sprintf(buf, "%d\n", cap.owned);
Marcin Obaraec288bd2008-07-25 19:44:59 -0700644 return rc;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700645}
646EXPORT_SYMBOL_GPL(tpm_show_owned);
647
648ssize_t tpm_show_temp_deactivated(struct device * dev,
649 struct device_attribute * attr, char *buf)
650{
Rajiv Andrade08837432009-02-02 15:23:43 -0200651 cap_t cap;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700652 ssize_t rc;
653
Rajiv Andrade08837432009-02-02 15:23:43 -0200654 rc = tpm_getcap(dev, TPM_CAP_FLAG_VOL, &cap,
655 "attempting to determine the temporary state");
656 if (rc)
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700657 return 0;
Marcin Obaraec288bd2008-07-25 19:44:59 -0700658
Rajiv Andrade08837432009-02-02 15:23:43 -0200659 rc = sprintf(buf, "%d\n", cap.stclear_flags.deactivated);
Marcin Obaraec288bd2008-07-25 19:44:59 -0700660 return rc;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700661}
662EXPORT_SYMBOL_GPL(tpm_show_temp_deactivated);
663
Rajiv Andrade659aaf22009-02-02 15:23:44 -0200664/*
665 * tpm_chip_find_get - return tpm_chip for given chip number
666 */
667static struct tpm_chip *tpm_chip_find_get(int chip_num)
668{
669 struct tpm_chip *pos;
670
671 rcu_read_lock();
672 list_for_each_entry_rcu(pos, &tpm_chip_list, list) {
673 if (chip_num != TPM_ANY_NUM && chip_num != pos->dev_num)
674 continue;
675
676 if (try_module_get(pos->dev->driver->owner))
677 break;
678 }
679 rcu_read_unlock();
680 return pos;
681}
682
683#define TPM_ORDINAL_PCRREAD cpu_to_be32(21)
684#define READ_PCR_RESULT_SIZE 30
685static struct tpm_input_header pcrread_header = {
686 .tag = TPM_TAG_RQU_COMMAND,
687 .length = cpu_to_be32(14),
688 .ordinal = TPM_ORDINAL_PCRREAD
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689};
690
Rajiv Andrade659aaf22009-02-02 15:23:44 -0200691int __tpm_pcr_read(struct tpm_chip *chip, int pcr_idx, u8 *res_buf)
692{
693 int rc;
694 struct tpm_cmd_t cmd;
695
696 cmd.header.in = pcrread_header;
697 cmd.params.pcrread_in.pcr_idx = cpu_to_be32(pcr_idx);
698 BUILD_BUG_ON(cmd.header.in.length > READ_PCR_RESULT_SIZE);
699 rc = transmit_cmd(chip, &cmd, cmd.header.in.length,
700 "attempting to read a pcr value");
701
702 if (rc == 0)
703 memcpy(res_buf, cmd.params.pcrread_out.pcr_result,
704 TPM_DIGEST_SIZE);
705 return rc;
706}
707
708/**
709 * tpm_pcr_read - read a pcr value
710 * @chip_num: tpm idx # or ANY
711 * @pcr_idx: pcr idx to retrieve
712 * @res_buf: TPM_PCR value
713 * size of res_buf is 20 bytes (or NULL if you don't care)
714 *
715 * The TPM driver should be built-in, but for whatever reason it
716 * isn't, protect against the chip disappearing, by incrementing
717 * the module usage count.
718 */
719int tpm_pcr_read(u32 chip_num, int pcr_idx, u8 *res_buf)
720{
721 struct tpm_chip *chip;
722 int rc;
723
724 chip = tpm_chip_find_get(chip_num);
725 if (chip == NULL)
726 return -ENODEV;
727 rc = __tpm_pcr_read(chip, pcr_idx, res_buf);
728 module_put(chip->dev->driver->owner);
729 return rc;
730}
731EXPORT_SYMBOL_GPL(tpm_pcr_read);
732
733/**
734 * tpm_pcr_extend - extend pcr value with hash
735 * @chip_num: tpm idx # or AN&
736 * @pcr_idx: pcr idx to extend
737 * @hash: hash value used to extend pcr value
738 *
739 * The TPM driver should be built-in, but for whatever reason it
740 * isn't, protect against the chip disappearing, by incrementing
741 * the module usage count.
742 */
743#define TPM_ORD_PCR_EXTEND cpu_to_be32(20)
744#define EXTEND_PCR_SIZE 34
745static struct tpm_input_header pcrextend_header = {
746 .tag = TPM_TAG_RQU_COMMAND,
747 .length = cpu_to_be32(34),
748 .ordinal = TPM_ORD_PCR_EXTEND
749};
750
751int tpm_pcr_extend(u32 chip_num, int pcr_idx, const u8 *hash)
752{
753 struct tpm_cmd_t cmd;
754 int rc;
755 struct tpm_chip *chip;
756
757 chip = tpm_chip_find_get(chip_num);
758 if (chip == NULL)
759 return -ENODEV;
760
761 cmd.header.in = pcrextend_header;
762 BUILD_BUG_ON(be32_to_cpu(cmd.header.in.length) > EXTEND_PCR_SIZE);
763 cmd.params.pcrextend_in.pcr_idx = cpu_to_be32(pcr_idx);
764 memcpy(cmd.params.pcrextend_in.hash, hash, TPM_DIGEST_SIZE);
765 rc = transmit_cmd(chip, &cmd, cmd.header.in.length,
766 "attempting extend a PCR value");
767
768 module_put(chip->dev->driver->owner);
769 return rc;
770}
771EXPORT_SYMBOL_GPL(tpm_pcr_extend);
772
Kylene Hall6659ca22005-06-23 22:02:00 -0700773ssize_t tpm_show_pcrs(struct device *dev, struct device_attribute *attr,
774 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
Rajiv Andrade08837432009-02-02 15:23:43 -0200776 cap_t cap;
Rajiv Andrade659aaf22009-02-02 15:23:44 -0200777 u8 digest[TPM_DIGEST_SIZE];
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700778 ssize_t rc;
Kylene Hall81179bb2005-06-23 22:01:59 -0700779 int i, j, num_pcrs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 char *str = buf;
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800781 struct tpm_chip *chip = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Rajiv Andrade08837432009-02-02 15:23:43 -0200783 rc = tpm_getcap(dev, TPM_CAP_PROP_PCR, &cap,
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700784 "attempting to determine the number of PCRS");
Rajiv Andrade08837432009-02-02 15:23:43 -0200785 if (rc)
Kylene Halle234bc92005-06-23 22:02:08 -0700786 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Rajiv Andrade08837432009-02-02 15:23:43 -0200788 num_pcrs = be32_to_cpu(cap.num_pcrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 for (i = 0; i < num_pcrs; i++) {
Rajiv Andrade659aaf22009-02-02 15:23:44 -0200790 rc = __tpm_pcr_read(chip, i, digest);
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700791 if (rc)
Rajiv Andrade08837432009-02-02 15:23:43 -0200792 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 str += sprintf(str, "PCR-%02d: ", i);
794 for (j = 0; j < TPM_DIGEST_SIZE; j++)
Rajiv Andrade659aaf22009-02-02 15:23:44 -0200795 str += sprintf(str, "%02X ", digest[j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 str += sprintf(str, "\n");
797 }
798 return str - buf;
799}
Kylene Hall6659ca22005-06-23 22:02:00 -0700800EXPORT_SYMBOL_GPL(tpm_show_pcrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
802#define READ_PUBEK_RESULT_SIZE 314
Rajiv Andrade08837432009-02-02 15:23:43 -0200803#define TPM_ORD_READPUBEK cpu_to_be32(124)
804struct tpm_input_header tpm_readpubek_header = {
805 .tag = TPM_TAG_RQU_COMMAND,
806 .length = cpu_to_be32(30),
807 .ordinal = TPM_ORD_READPUBEK
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808};
809
Kylene Hall6659ca22005-06-23 22:02:00 -0700810ssize_t tpm_show_pubek(struct device *dev, struct device_attribute *attr,
811 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812{
Kylene Hall2df71112005-06-23 22:01:54 -0700813 u8 *data;
Rajiv Andrade08837432009-02-02 15:23:43 -0200814 struct tpm_cmd_t tpm_cmd;
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700815 ssize_t err;
Kylene Hall81179bb2005-06-23 22:01:59 -0700816 int i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 char *str = buf;
818
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800819 struct tpm_chip *chip = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Rajiv Andrade08837432009-02-02 15:23:43 -0200821 tpm_cmd.header.in = tpm_readpubek_header;
822 err = transmit_cmd(chip, &tpm_cmd, READ_PUBEK_RESULT_SIZE,
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700823 "attempting to read the PUBEK");
824 if (err)
Kylene Jo Hall34d6e072005-06-23 22:02:10 -0700825 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
827 /*
828 ignore header 10 bytes
829 algorithm 32 bits (1 == RSA )
830 encscheme 16 bits
831 sigscheme 16 bits
832 parameters (RSA 12->bytes: keybit, #primes, expbit)
833 keylenbytes 32 bits
834 256 byte modulus
835 ignore checksum 20 bytes
836 */
Rajiv Andrade08837432009-02-02 15:23:43 -0200837 data = tpm_cmd.params.readpubek_out_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 str +=
839 sprintf(str,
840 "Algorithm: %02X %02X %02X %02X\nEncscheme: %02X %02X\n"
841 "Sigscheme: %02X %02X\nParameters: %02X %02X %02X %02X"
842 " %02X %02X %02X %02X %02X %02X %02X %02X\n"
843 "Modulus length: %d\nModulus: \n",
844 data[10], data[11], data[12], data[13], data[14],
845 data[15], data[16], data[17], data[22], data[23],
846 data[24], data[25], data[26], data[27], data[28],
847 data[29], data[30], data[31], data[32], data[33],
Kylene Jo Hall1dda8ab2005-06-25 14:55:40 -0700848 be32_to_cpu(*((__be32 *) (data + 34))));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
850 for (i = 0; i < 256; i++) {
Kylene Jo Hall1dda8ab2005-06-25 14:55:40 -0700851 str += sprintf(str, "%02X ", data[i + 38]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 if ((i + 1) % 16 == 0)
853 str += sprintf(str, "\n");
854 }
Kylene Jo Hall34d6e072005-06-23 22:02:10 -0700855out:
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700856 rc = str - buf;
Kylene Hall2df71112005-06-23 22:01:54 -0700857 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858}
Kylene Hall6659ca22005-06-23 22:02:00 -0700859EXPORT_SYMBOL_GPL(tpm_show_pubek);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
Kylene Hall6659ca22005-06-23 22:02:00 -0700862ssize_t tpm_show_caps(struct device *dev, struct device_attribute *attr,
863 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864{
Rajiv Andrade08837432009-02-02 15:23:43 -0200865 cap_t cap;
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700866 ssize_t rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 char *str = buf;
868
Rajiv Andrade08837432009-02-02 15:23:43 -0200869 rc = tpm_getcap(dev, TPM_CAP_PROP_MANUFACTURER, &cap,
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700870 "attempting to determine the manufacturer");
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700871 if (rc)
Rajiv Andrade08837432009-02-02 15:23:43 -0200872 return 0;
873 str += sprintf(str, "Manufacturer: 0x%x\n",
874 be32_to_cpu(cap.manufacturer_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Rajiv Andrade08837432009-02-02 15:23:43 -0200876 rc = tpm_getcap(dev, CAP_VERSION_1_1, &cap,
877 "attempting to determine the 1.1 version");
878 if (rc)
879 return 0;
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700880 str += sprintf(str,
881 "TCG version: %d.%d\nFirmware version: %d.%d\n",
Rajiv Andrade08837432009-02-02 15:23:43 -0200882 cap.tpm_version.Major, cap.tpm_version.Minor,
883 cap.tpm_version.revMajor, cap.tpm_version.revMinor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 return str - buf;
885}
Kylene Hall6659ca22005-06-23 22:02:00 -0700886EXPORT_SYMBOL_GPL(tpm_show_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700888ssize_t tpm_show_caps_1_2(struct device * dev,
889 struct device_attribute * attr, char *buf)
890{
Rajiv Andrade08837432009-02-02 15:23:43 -0200891 cap_t cap;
892 ssize_t rc;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700893 char *str = buf;
894
Rajiv Andrade08837432009-02-02 15:23:43 -0200895 rc = tpm_getcap(dev, TPM_CAP_PROP_MANUFACTURER, &cap,
896 "attempting to determine the manufacturer");
897 if (rc)
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700898 return 0;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700899 str += sprintf(str, "Manufacturer: 0x%x\n",
Rajiv Andrade08837432009-02-02 15:23:43 -0200900 be32_to_cpu(cap.manufacturer_id));
901 rc = tpm_getcap(dev, CAP_VERSION_1_2, &cap,
902 "attempting to determine the 1.2 version");
903 if (rc)
904 return 0;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700905 str += sprintf(str,
906 "TCG version: %d.%d\nFirmware version: %d.%d\n",
Rajiv Andrade08837432009-02-02 15:23:43 -0200907 cap.tpm_version_1_2.Major, cap.tpm_version_1_2.Minor,
908 cap.tpm_version_1_2.revMajor,
909 cap.tpm_version_1_2.revMinor);
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700910 return str - buf;
911}
912EXPORT_SYMBOL_GPL(tpm_show_caps_1_2);
913
Kylene Hall6659ca22005-06-23 22:02:00 -0700914ssize_t tpm_store_cancel(struct device *dev, struct device_attribute *attr,
915 const char *buf, size_t count)
916{
917 struct tpm_chip *chip = dev_get_drvdata(dev);
918 if (chip == NULL)
919 return 0;
920
Kylene Jo Hall90dda522006-04-22 02:37:15 -0700921 chip->vendor.cancel(chip);
Kylene Hall6659ca22005-06-23 22:02:00 -0700922 return count;
923}
924EXPORT_SYMBOL_GPL(tpm_store_cancel);
925
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926/*
927 * Device file system interface to the TPM
Rajiv Andradef89c5ed2008-10-11 09:03:39 +1100928 *
929 * It's assured that the chip will be opened just once,
930 * by the check of is_open variable, which is protected
931 * by driver_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 */
933int tpm_open(struct inode *inode, struct file *file)
934{
Rajiv Andradef02a9362008-10-11 09:04:23 +1100935 int minor = iminor(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 struct tpm_chip *chip = NULL, *pos;
937
Rajiv Andradef02a9362008-10-11 09:04:23 +1100938 rcu_read_lock();
939 list_for_each_entry_rcu(pos, &tpm_chip_list, list) {
Kylene Jo Hall90dda522006-04-22 02:37:15 -0700940 if (pos->vendor.miscdev.minor == minor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 chip = pos;
Rajiv Andradef02a9362008-10-11 09:04:23 +1100942 get_device(chip->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 break;
944 }
945 }
Rajiv Andradef02a9362008-10-11 09:04:23 +1100946 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Rajiv Andradef02a9362008-10-11 09:04:23 +1100948 if (!chip)
949 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
Rajiv Andradedc36d322008-10-11 09:04:02 +1100951 if (test_and_set_bit(0, &chip->is_open)) {
Andrew Mortonb888c872005-10-30 15:03:28 -0800952 dev_dbg(chip->dev, "Another process owns this TPM\n");
Rajiv Andradef02a9362008-10-11 09:04:23 +1100953 put_device(chip->dev);
954 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 }
956
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 chip->data_buffer = kmalloc(TPM_BUFSIZE * sizeof(u8), GFP_KERNEL);
958 if (chip->data_buffer == NULL) {
Rajiv Andradedc36d322008-10-11 09:04:02 +1100959 clear_bit(0, &chip->is_open);
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800960 put_device(chip->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 return -ENOMEM;
962 }
963
964 atomic_set(&chip->data_pending, 0);
965
966 file->private_data = chip;
967 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969EXPORT_SYMBOL_GPL(tpm_open);
970
Rajiv Andradef02a9362008-10-11 09:04:23 +1100971/*
972 * Called on file close
973 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974int tpm_release(struct inode *inode, struct file *file)
975{
976 struct tpm_chip *chip = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
Rajiv Andrade4bdec112008-10-11 09:05:20 +1100978 del_singleshot_timer_sync(&chip->user_read_timer);
Parag Warudkar8e39c932007-05-08 00:31:09 -0700979 flush_scheduled_work();
Kylene Hall5e976d52005-06-23 22:02:03 -0700980 file->private_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 atomic_set(&chip->data_pending, 0);
Rajiv Andradef02a9362008-10-11 09:04:23 +1100982 kfree(chip->data_buffer);
Rajiv Andradedc36d322008-10-11 09:04:02 +1100983 clear_bit(0, &chip->is_open);
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800984 put_device(chip->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 return 0;
986}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987EXPORT_SYMBOL_GPL(tpm_release);
988
Andrew Mortonb888c872005-10-30 15:03:28 -0800989ssize_t tpm_write(struct file *file, const char __user *buf,
Kylene Jo Hall3c2f6062006-04-22 02:36:56 -0700990 size_t size, loff_t *off)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991{
992 struct tpm_chip *chip = file->private_data;
Michael Halcrow01476002008-07-25 19:45:00 -0700993 size_t in_size = size, out_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
995 /* cannot perform a write until the read has cleared
996 either via tpm_read or a user_read_timer timeout */
Nishanth Aravamudan700d8bd2005-06-23 22:01:47 -0700997 while (atomic_read(&chip->data_pending) != 0)
998 msleep(TPM_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
Matthias Kaehlcked081d472007-05-08 00:32:02 -07001000 mutex_lock(&chip->buffer_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
1002 if (in_size > TPM_BUFSIZE)
1003 in_size = TPM_BUFSIZE;
1004
1005 if (copy_from_user
1006 (chip->data_buffer, (void __user *) buf, in_size)) {
Matthias Kaehlcked081d472007-05-08 00:32:02 -07001007 mutex_unlock(&chip->buffer_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 return -EFAULT;
1009 }
1010
1011 /* atomic tpm command send and result receive */
1012 out_size = tpm_transmit(chip, chip->data_buffer, TPM_BUFSIZE);
1013
1014 atomic_set(&chip->data_pending, out_size);
Matthias Kaehlcked081d472007-05-08 00:32:02 -07001015 mutex_unlock(&chip->buffer_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
1017 /* Set a timeout by which the reader must come claim the result */
Kylene Hallfe3fd4832005-06-23 22:01:56 -07001018 mod_timer(&chip->user_read_timer, jiffies + (60 * HZ));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
1020 return in_size;
1021}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022EXPORT_SYMBOL_GPL(tpm_write);
1023
Kylene Jo Hall3c2f6062006-04-22 02:36:56 -07001024ssize_t tpm_read(struct file *file, char __user *buf,
1025 size_t size, loff_t *off)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026{
1027 struct tpm_chip *chip = file->private_data;
Michael Halcrow01476002008-07-25 19:45:00 -07001028 ssize_t ret_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
Kylene Hall5b44bd52005-06-23 22:01:53 -07001030 del_singleshot_timer_sync(&chip->user_read_timer);
Kylene Jo Hallba396112005-11-18 01:10:57 -08001031 flush_scheduled_work();
Kylene Hall5b44bd52005-06-23 22:01:53 -07001032 ret_size = atomic_read(&chip->data_pending);
1033 atomic_set(&chip->data_pending, 0);
1034 if (ret_size > 0) { /* relay data */
1035 if (size < ret_size)
1036 ret_size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
Matthias Kaehlcked081d472007-05-08 00:32:02 -07001038 mutex_lock(&chip->buffer_mutex);
Kylene Jo Hallf6a23822005-11-13 16:07:42 -08001039 if (copy_to_user(buf, chip->data_buffer, ret_size))
Kylene Hall5b44bd52005-06-23 22:01:53 -07001040 ret_size = -EFAULT;
Matthias Kaehlcked081d472007-05-08 00:32:02 -07001041 mutex_unlock(&chip->buffer_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 }
1043
1044 return ret_size;
1045}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046EXPORT_SYMBOL_GPL(tpm_read);
1047
Kylene Jo Halle659a3f2005-10-30 15:03:24 -08001048void tpm_remove_hardware(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049{
Kylene Jo Halle659a3f2005-10-30 15:03:24 -08001050 struct tpm_chip *chip = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
1052 if (chip == NULL) {
Kylene Jo Halle659a3f2005-10-30 15:03:24 -08001053 dev_err(dev, "No device data found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 return;
1055 }
1056
1057 spin_lock(&driver_lock);
Rajiv Andradef02a9362008-10-11 09:04:23 +11001058 list_del_rcu(&chip->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 spin_unlock(&driver_lock);
Rajiv Andradef02a9362008-10-11 09:04:23 +11001060 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
Kylene Jo Hall90dda522006-04-22 02:37:15 -07001062 misc_deregister(&chip->vendor.miscdev);
Kylene Jo Hall90dda522006-04-22 02:37:15 -07001063 sysfs_remove_group(&dev->kobj, chip->vendor.attr_group);
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -08001064 tpm_bios_log_teardown(chip->bios_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
Richard MUSIL5bd91f12008-02-06 01:37:02 -08001066 /* write it this way to be explicit (chip->dev == dev) */
1067 put_device(chip->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068}
Kylene Jo Halle659a3f2005-10-30 15:03:24 -08001069EXPORT_SYMBOL_GPL(tpm_remove_hardware);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071/*
1072 * We are about to suspend. Save the TPM state
1073 * so that it can be restored.
1074 */
Kylene Jo Hallce2c87d2005-10-30 15:03:25 -08001075int tpm_pm_suspend(struct device *dev, pm_message_t pm_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076{
Kylene Jo Hallce2c87d2005-10-30 15:03:25 -08001077 struct tpm_chip *chip = dev_get_drvdata(dev);
David Smith2490c682008-01-14 00:55:12 -08001078 u8 savestate[] = {
1079 0, 193, /* TPM_TAG_RQU_COMMAND */
1080 0, 0, 0, 10, /* blob length (in bytes) */
1081 0, 0, 0, 152 /* TPM_ORD_SaveState */
1082 };
1083
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 if (chip == NULL)
1085 return -ENODEV;
1086
1087 tpm_transmit(chip, savestate, sizeof(savestate));
1088 return 0;
1089}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090EXPORT_SYMBOL_GPL(tpm_pm_suspend);
1091
1092/*
1093 * Resume from a power safe. The BIOS already restored
1094 * the TPM state.
1095 */
Kylene Jo Hallce2c87d2005-10-30 15:03:25 -08001096int tpm_pm_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097{
Kylene Jo Hallce2c87d2005-10-30 15:03:25 -08001098 struct tpm_chip *chip = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
1100 if (chip == NULL)
1101 return -ENODEV;
1102
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 return 0;
1104}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105EXPORT_SYMBOL_GPL(tpm_pm_resume);
1106
Rajiv Andrade253115b2008-10-11 09:04:39 +11001107/* In case vendor provided release function, call it too.*/
1108
1109void tpm_dev_vendor_release(struct tpm_chip *chip)
1110{
1111 if (chip->vendor.release)
1112 chip->vendor.release(chip->dev);
1113
1114 clear_bit(chip->dev_num, dev_mask);
1115 kfree(chip->vendor.miscdev.name);
1116}
1117EXPORT_SYMBOL_GPL(tpm_dev_vendor_release);
1118
1119
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120/*
Richard MUSIL5bd91f12008-02-06 01:37:02 -08001121 * Once all references to platform device are down to 0,
1122 * release all allocated structures.
Richard MUSIL5bd91f12008-02-06 01:37:02 -08001123 */
Stephen Rothwellcbb2ed42008-10-18 20:25:46 -07001124void tpm_dev_release(struct device *dev)
Richard MUSIL5bd91f12008-02-06 01:37:02 -08001125{
1126 struct tpm_chip *chip = dev_get_drvdata(dev);
1127
Rajiv Andrade253115b2008-10-11 09:04:39 +11001128 tpm_dev_vendor_release(chip);
Richard MUSIL5bd91f12008-02-06 01:37:02 -08001129
Rajiv Andrade253115b2008-10-11 09:04:39 +11001130 chip->release(dev);
Richard MUSIL5bd91f12008-02-06 01:37:02 -08001131 kfree(chip);
1132}
Rajiv Andrade253115b2008-10-11 09:04:39 +11001133EXPORT_SYMBOL_GPL(tpm_dev_release);
Richard MUSIL5bd91f12008-02-06 01:37:02 -08001134
1135/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 * Called from tpm_<specific>.c probe function only for devices
1137 * the driver has determined it should claim. Prior to calling
1138 * this function the specific probe function has called pci_enable_device
1139 * upon errant exit from this function specific probe function should call
1140 * pci_disable_device
1141 */
Rajiv Andradef02a9362008-10-11 09:04:23 +11001142struct tpm_chip *tpm_register_hardware(struct device *dev,
1143 const struct tpm_vendor_specific *entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144{
Kylene Jo Hall6f9becc2005-06-25 14:55:41 -07001145#define DEVNAME_SIZE 7
1146
1147 char *devname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 struct tpm_chip *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
1150 /* Driver specific per-device data */
Andrew Mortonb888c872005-10-30 15:03:28 -08001151 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
Parag Warudkar8e39c932007-05-08 00:31:09 -07001152 devname = kmalloc(DEVNAME_SIZE, GFP_KERNEL);
1153
Andrew Mortondd78c942008-10-15 22:04:37 -07001154 if (chip == NULL || devname == NULL)
1155 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
Matthias Kaehlcked081d472007-05-08 00:32:02 -07001157 mutex_init(&chip->buffer_mutex);
1158 mutex_init(&chip->tpm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 INIT_LIST_HEAD(&chip->list);
1160
David Howellsc4028952006-11-22 14:57:56 +00001161 INIT_WORK(&chip->work, timeout_work);
Kylene Jo Hall09e12f92005-11-13 16:07:43 -08001162
Jiri Slaby40565f12007-02-12 00:52:31 -08001163 setup_timer(&chip->user_read_timer, user_reader_timeout,
1164 (unsigned long)chip);
Kylene Hallfe3fd4832005-06-23 22:01:56 -07001165
Kylene Jo Hall90dda522006-04-22 02:37:15 -07001166 memcpy(&chip->vendor, entry, sizeof(struct tpm_vendor_specific));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
Kylene Jo Hall10685a92006-04-22 02:38:32 -07001168 chip->dev_num = find_first_zero_bit(dev_mask, TPM_NUM_DEVICES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
Kylene Jo Hall10685a92006-04-22 02:38:32 -07001170 if (chip->dev_num >= TPM_NUM_DEVICES) {
Andrew Mortonb888c872005-10-30 15:03:28 -08001171 dev_err(dev, "No available tpm device numbers\n");
Andrew Mortondd78c942008-10-15 22:04:37 -07001172 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 } else if (chip->dev_num == 0)
Kylene Jo Hall90dda522006-04-22 02:37:15 -07001174 chip->vendor.miscdev.minor = TPM_MINOR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 else
Kylene Jo Hall90dda522006-04-22 02:37:15 -07001176 chip->vendor.miscdev.minor = MISC_DYNAMIC_MINOR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
Kylene Jo Hall10685a92006-04-22 02:38:32 -07001178 set_bit(chip->dev_num, dev_mask);
1179
Kylene Jo Hall6f9becc2005-06-25 14:55:41 -07001180 scnprintf(devname, DEVNAME_SIZE, "%s%d", "tpm", chip->dev_num);
Kylene Jo Hall90dda522006-04-22 02:37:15 -07001181 chip->vendor.miscdev.name = devname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
Greg Kroah-Hartman94fbcde2006-07-27 16:16:04 -07001183 chip->vendor.miscdev.parent = dev;
Kylene Jo Halle659a3f2005-10-30 15:03:24 -08001184 chip->dev = get_device(dev);
Richard MUSIL5bd91f12008-02-06 01:37:02 -08001185 chip->release = dev->release;
1186 dev->release = tpm_dev_release;
1187 dev_set_drvdata(dev, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
Kylene Jo Hall90dda522006-04-22 02:37:15 -07001189 if (misc_register(&chip->vendor.miscdev)) {
Kylene Jo Halle659a3f2005-10-30 15:03:24 -08001190 dev_err(chip->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 "unable to misc_register %s, minor %d\n",
Kylene Jo Hall90dda522006-04-22 02:37:15 -07001192 chip->vendor.miscdev.name,
1193 chip->vendor.miscdev.minor);
Richard MUSIL5bd91f12008-02-06 01:37:02 -08001194 put_device(chip->dev);
Kylene Jo Halle0dd03c2006-04-22 02:37:26 -07001195 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 }
1197
Jeff Garzikf33d9bd2006-10-11 01:21:51 -07001198 if (sysfs_create_group(&dev->kobj, chip->vendor.attr_group)) {
Neil Horman5d469ec02006-12-06 20:37:08 -08001199 misc_deregister(&chip->vendor.miscdev);
Richard MUSIL5bd91f12008-02-06 01:37:02 -08001200 put_device(chip->dev);
Rajiv Andradef02a9362008-10-11 09:04:23 +11001201
Jeff Garzikf33d9bd2006-10-11 01:21:51 -07001202 return NULL;
1203 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -08001205 chip->bios_dir = tpm_bios_log_setup(devname);
1206
Rajiv Andradef02a9362008-10-11 09:04:23 +11001207 /* Make chip available */
1208 spin_lock(&driver_lock);
1209 list_add_rcu(&chip->list, &tpm_chip_list);
1210 spin_unlock(&driver_lock);
1211
Kylene Jo Halle0dd03c2006-04-22 02:37:26 -07001212 return chip;
Andrew Mortondd78c942008-10-15 22:04:37 -07001213
1214out_free:
1215 kfree(chip);
1216 kfree(devname);
1217 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219EXPORT_SYMBOL_GPL(tpm_register_hardware);
1220
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
1222MODULE_DESCRIPTION("TPM Driver");
1223MODULE_VERSION("2.0");
1224MODULE_LICENSE("GPL");