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