blob: efd24bbb5cb1ccc5bae48d6bca5f135b34ee0f8b [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Matthias Kaehlcked081d472007-05-08 00:32:02 -070028#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/spinlock.h>
Rajiv Andradefd048862011-09-16 14:39:40 -030030#include <linux/freezer.h>
Matthias Kaehlcked081d472007-05-08 00:32:02 -070031
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "tpm.h"
33
Kylene Hall3122a882005-06-23 22:01:48 -070034enum tpm_const {
35 TPM_MINOR = 224, /* officially assigned */
Rajiv Andrade7f366782009-09-24 16:27:46 -030036 TPM_BUFSIZE = 4096,
Kylene Hall3122a882005-06-23 22:01:48 -070037 TPM_NUM_DEVICES = 256,
Kylene Hall3122a882005-06-23 22:01:48 -070038};
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Kylene Jo Hall9e18ee12006-04-22 02:37:38 -070040enum tpm_duration {
41 TPM_SHORT = 0,
42 TPM_MEDIUM = 1,
43 TPM_LONG = 2,
44 TPM_UNDEFINED,
45};
46
47#define TPM_MAX_ORDINAL 243
48#define TPM_MAX_PROTECTED_ORDINAL 12
49#define TPM_PROTECTED_ORDINAL_MASK 0xFF
50
Dmitry Torokhov9b3056c2010-10-01 14:16:39 -070051/*
52 * Bug workaround - some TPM's don't flush the most
53 * recently changed pcr on suspend, so force the flush
54 * with an extend to the selected _unused_ non-volatile pcr.
55 */
56static int tpm_suspend_pcr;
57module_param_named(suspend_pcr, tpm_suspend_pcr, uint, 0644);
58MODULE_PARM_DESC(suspend_pcr,
59 "PCR to use for dummy writes to faciltate flush on suspend.");
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061static LIST_HEAD(tpm_chip_list);
62static DEFINE_SPINLOCK(driver_lock);
Kylene Jo Hall10685a92006-04-22 02:38:32 -070063static DECLARE_BITMAP(dev_mask, TPM_NUM_DEVICES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Kylene Jo Hall9e18ee12006-04-22 02:37:38 -070065/*
66 * Array with one entry per ordinal defining the maximum amount
67 * of time the chip could take to return the result. The ordinal
68 * designation of short, medium or long is defined in a table in
69 * TCG Specification TPM Main Part 2 TPM Structures Section 17. The
70 * values of the SHORT, MEDIUM, and LONG durations are retrieved
71 * from the chip during initialization with a call to tpm_get_timeouts.
72 */
73static const u8 tpm_protected_ordinal_duration[TPM_MAX_PROTECTED_ORDINAL] = {
74 TPM_UNDEFINED, /* 0 */
75 TPM_UNDEFINED,
76 TPM_UNDEFINED,
77 TPM_UNDEFINED,
78 TPM_UNDEFINED,
79 TPM_UNDEFINED, /* 5 */
80 TPM_UNDEFINED,
81 TPM_UNDEFINED,
82 TPM_UNDEFINED,
83 TPM_UNDEFINED,
84 TPM_SHORT, /* 10 */
85 TPM_SHORT,
86};
87
88static const u8 tpm_ordinal_duration[TPM_MAX_ORDINAL] = {
89 TPM_UNDEFINED, /* 0 */
90 TPM_UNDEFINED,
91 TPM_UNDEFINED,
92 TPM_UNDEFINED,
93 TPM_UNDEFINED,
94 TPM_UNDEFINED, /* 5 */
95 TPM_UNDEFINED,
96 TPM_UNDEFINED,
97 TPM_UNDEFINED,
98 TPM_UNDEFINED,
99 TPM_SHORT, /* 10 */
100 TPM_SHORT,
101 TPM_MEDIUM,
102 TPM_LONG,
103 TPM_LONG,
104 TPM_MEDIUM, /* 15 */
105 TPM_SHORT,
106 TPM_SHORT,
107 TPM_MEDIUM,
108 TPM_LONG,
109 TPM_SHORT, /* 20 */
110 TPM_SHORT,
111 TPM_MEDIUM,
112 TPM_MEDIUM,
113 TPM_MEDIUM,
114 TPM_SHORT, /* 25 */
115 TPM_SHORT,
116 TPM_MEDIUM,
117 TPM_SHORT,
118 TPM_SHORT,
119 TPM_MEDIUM, /* 30 */
120 TPM_LONG,
121 TPM_MEDIUM,
122 TPM_SHORT,
123 TPM_SHORT,
124 TPM_SHORT, /* 35 */
125 TPM_MEDIUM,
126 TPM_MEDIUM,
127 TPM_UNDEFINED,
128 TPM_UNDEFINED,
129 TPM_MEDIUM, /* 40 */
130 TPM_LONG,
131 TPM_MEDIUM,
132 TPM_SHORT,
133 TPM_SHORT,
134 TPM_SHORT, /* 45 */
135 TPM_SHORT,
136 TPM_SHORT,
137 TPM_SHORT,
138 TPM_LONG,
139 TPM_MEDIUM, /* 50 */
140 TPM_MEDIUM,
141 TPM_UNDEFINED,
142 TPM_UNDEFINED,
143 TPM_UNDEFINED,
144 TPM_UNDEFINED, /* 55 */
145 TPM_UNDEFINED,
146 TPM_UNDEFINED,
147 TPM_UNDEFINED,
148 TPM_UNDEFINED,
149 TPM_MEDIUM, /* 60 */
150 TPM_MEDIUM,
151 TPM_MEDIUM,
152 TPM_SHORT,
153 TPM_SHORT,
154 TPM_MEDIUM, /* 65 */
155 TPM_UNDEFINED,
156 TPM_UNDEFINED,
157 TPM_UNDEFINED,
158 TPM_UNDEFINED,
159 TPM_SHORT, /* 70 */
160 TPM_SHORT,
161 TPM_UNDEFINED,
162 TPM_UNDEFINED,
163 TPM_UNDEFINED,
164 TPM_UNDEFINED, /* 75 */
165 TPM_UNDEFINED,
166 TPM_UNDEFINED,
167 TPM_UNDEFINED,
168 TPM_UNDEFINED,
169 TPM_LONG, /* 80 */
170 TPM_UNDEFINED,
171 TPM_MEDIUM,
172 TPM_LONG,
173 TPM_SHORT,
174 TPM_UNDEFINED, /* 85 */
175 TPM_UNDEFINED,
176 TPM_UNDEFINED,
177 TPM_UNDEFINED,
178 TPM_UNDEFINED,
179 TPM_SHORT, /* 90 */
180 TPM_SHORT,
181 TPM_SHORT,
182 TPM_SHORT,
183 TPM_SHORT,
184 TPM_UNDEFINED, /* 95 */
185 TPM_UNDEFINED,
186 TPM_UNDEFINED,
187 TPM_UNDEFINED,
188 TPM_UNDEFINED,
189 TPM_MEDIUM, /* 100 */
190 TPM_SHORT,
191 TPM_SHORT,
192 TPM_UNDEFINED,
193 TPM_UNDEFINED,
194 TPM_UNDEFINED, /* 105 */
195 TPM_UNDEFINED,
196 TPM_UNDEFINED,
197 TPM_UNDEFINED,
198 TPM_UNDEFINED,
199 TPM_SHORT, /* 110 */
200 TPM_SHORT,
201 TPM_SHORT,
202 TPM_SHORT,
203 TPM_SHORT,
204 TPM_SHORT, /* 115 */
205 TPM_SHORT,
206 TPM_SHORT,
207 TPM_UNDEFINED,
208 TPM_UNDEFINED,
209 TPM_LONG, /* 120 */
210 TPM_LONG,
211 TPM_MEDIUM,
212 TPM_UNDEFINED,
213 TPM_SHORT,
214 TPM_SHORT, /* 125 */
215 TPM_SHORT,
216 TPM_LONG,
217 TPM_SHORT,
218 TPM_SHORT,
219 TPM_SHORT, /* 130 */
220 TPM_MEDIUM,
221 TPM_UNDEFINED,
222 TPM_SHORT,
223 TPM_MEDIUM,
224 TPM_UNDEFINED, /* 135 */
225 TPM_UNDEFINED,
226 TPM_UNDEFINED,
227 TPM_UNDEFINED,
228 TPM_UNDEFINED,
229 TPM_SHORT, /* 140 */
230 TPM_SHORT,
231 TPM_UNDEFINED,
232 TPM_UNDEFINED,
233 TPM_UNDEFINED,
234 TPM_UNDEFINED, /* 145 */
235 TPM_UNDEFINED,
236 TPM_UNDEFINED,
237 TPM_UNDEFINED,
238 TPM_UNDEFINED,
239 TPM_SHORT, /* 150 */
240 TPM_MEDIUM,
241 TPM_MEDIUM,
242 TPM_SHORT,
243 TPM_SHORT,
244 TPM_UNDEFINED, /* 155 */
245 TPM_UNDEFINED,
246 TPM_UNDEFINED,
247 TPM_UNDEFINED,
248 TPM_UNDEFINED,
249 TPM_SHORT, /* 160 */
250 TPM_SHORT,
251 TPM_SHORT,
252 TPM_SHORT,
253 TPM_UNDEFINED,
254 TPM_UNDEFINED, /* 165 */
255 TPM_UNDEFINED,
256 TPM_UNDEFINED,
257 TPM_UNDEFINED,
258 TPM_UNDEFINED,
259 TPM_LONG, /* 170 */
260 TPM_UNDEFINED,
261 TPM_UNDEFINED,
262 TPM_UNDEFINED,
263 TPM_UNDEFINED,
264 TPM_UNDEFINED, /* 175 */
265 TPM_UNDEFINED,
266 TPM_UNDEFINED,
267 TPM_UNDEFINED,
268 TPM_UNDEFINED,
269 TPM_MEDIUM, /* 180 */
270 TPM_SHORT,
271 TPM_MEDIUM,
272 TPM_MEDIUM,
273 TPM_MEDIUM,
274 TPM_MEDIUM, /* 185 */
275 TPM_SHORT,
276 TPM_UNDEFINED,
277 TPM_UNDEFINED,
278 TPM_UNDEFINED,
279 TPM_UNDEFINED, /* 190 */
280 TPM_UNDEFINED,
281 TPM_UNDEFINED,
282 TPM_UNDEFINED,
283 TPM_UNDEFINED,
284 TPM_UNDEFINED, /* 195 */
285 TPM_UNDEFINED,
286 TPM_UNDEFINED,
287 TPM_UNDEFINED,
288 TPM_UNDEFINED,
289 TPM_SHORT, /* 200 */
290 TPM_UNDEFINED,
291 TPM_UNDEFINED,
292 TPM_UNDEFINED,
293 TPM_SHORT,
294 TPM_SHORT, /* 205 */
295 TPM_SHORT,
296 TPM_SHORT,
297 TPM_SHORT,
298 TPM_SHORT,
299 TPM_MEDIUM, /* 210 */
300 TPM_UNDEFINED,
301 TPM_MEDIUM,
302 TPM_MEDIUM,
303 TPM_MEDIUM,
304 TPM_UNDEFINED, /* 215 */
305 TPM_MEDIUM,
306 TPM_UNDEFINED,
307 TPM_UNDEFINED,
308 TPM_SHORT,
309 TPM_SHORT, /* 220 */
310 TPM_SHORT,
311 TPM_SHORT,
312 TPM_SHORT,
313 TPM_SHORT,
314 TPM_UNDEFINED, /* 225 */
315 TPM_UNDEFINED,
316 TPM_UNDEFINED,
317 TPM_UNDEFINED,
318 TPM_UNDEFINED,
319 TPM_SHORT, /* 230 */
320 TPM_LONG,
321 TPM_MEDIUM,
322 TPM_UNDEFINED,
323 TPM_UNDEFINED,
324 TPM_UNDEFINED, /* 235 */
325 TPM_UNDEFINED,
326 TPM_UNDEFINED,
327 TPM_UNDEFINED,
328 TPM_UNDEFINED,
329 TPM_SHORT, /* 240 */
330 TPM_UNDEFINED,
331 TPM_MEDIUM,
332};
333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334static void user_reader_timeout(unsigned long ptr)
335{
336 struct tpm_chip *chip = (struct tpm_chip *) ptr;
337
Kylene Jo Hall09e12f92005-11-13 16:07:43 -0800338 schedule_work(&chip->work);
339}
340
David Howellsc4028952006-11-22 14:57:56 +0000341static void timeout_work(struct work_struct *work)
Kylene Jo Hall09e12f92005-11-13 16:07:43 -0800342{
David Howellsc4028952006-11-22 14:57:56 +0000343 struct tpm_chip *chip = container_of(work, struct tpm_chip, work);
Kylene Jo Hall09e12f92005-11-13 16:07:43 -0800344
Matthias Kaehlcked081d472007-05-08 00:32:02 -0700345 mutex_lock(&chip->buffer_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 atomic_set(&chip->data_pending, 0);
347 memset(chip->data_buffer, 0, TPM_BUFSIZE);
Matthias Kaehlcked081d472007-05-08 00:32:02 -0700348 mutex_unlock(&chip->buffer_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349}
350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351/*
Kylene Jo Hall9e18ee12006-04-22 02:37:38 -0700352 * Returns max number of jiffies to wait
353 */
354unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip,
355 u32 ordinal)
356{
357 int duration_idx = TPM_UNDEFINED;
358 int duration = 0;
359
360 if (ordinal < TPM_MAX_ORDINAL)
361 duration_idx = tpm_ordinal_duration[ordinal];
362 else if ((ordinal & TPM_PROTECTED_ORDINAL_MASK) <
363 TPM_MAX_PROTECTED_ORDINAL)
364 duration_idx =
365 tpm_protected_ordinal_duration[ordinal &
366 TPM_PROTECTED_ORDINAL_MASK];
367
Linus Torvalds8d1dc202011-03-01 13:23:27 -0800368 if (duration_idx != TPM_UNDEFINED)
Kylene Jo Hall36b20022006-04-22 02:38:19 -0700369 duration = chip->vendor.duration[duration_idx];
Linus Torvalds8d1dc202011-03-01 13:23:27 -0800370 if (duration <= 0)
Kylene Jo Hall9e18ee12006-04-22 02:37:38 -0700371 return 2 * 60 * HZ;
Linus Torvalds8d1dc202011-03-01 13:23:27 -0800372 else
373 return duration;
Kylene Jo Hall9e18ee12006-04-22 02:37:38 -0700374}
375EXPORT_SYMBOL_GPL(tpm_calc_ordinal_duration);
376
377/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 * Internal kernel interface to transmit TPM commands
379 */
380static ssize_t tpm_transmit(struct tpm_chip *chip, const char *buf,
381 size_t bufsiz)
382{
Kylene Halld9e5b6b2005-06-23 22:02:02 -0700383 ssize_t rc;
Kylene Jo Hall9e18ee12006-04-22 02:37:38 -0700384 u32 count, ordinal;
Nishanth Aravamudan700d8bd2005-06-23 22:01:47 -0700385 unsigned long stop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Kylene Hall81179bb2005-06-23 22:01:59 -0700387 count = be32_to_cpu(*((__be32 *) (buf + 2)));
Kylene Jo Hall9e18ee12006-04-22 02:37:38 -0700388 ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 if (count == 0)
390 return -ENODATA;
391 if (count > bufsiz) {
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800392 dev_err(chip->dev,
Al Virob76be682005-04-26 07:43:41 -0700393 "invalid count value %x %zx \n", count, bufsiz);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 return -E2BIG;
395 }
396
Matthias Kaehlcked081d472007-05-08 00:32:02 -0700397 mutex_lock(&chip->tpm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Kylene Jo Hall90dda522006-04-22 02:37:15 -0700399 if ((rc = chip->vendor.send(chip, (u8 *) buf, count)) < 0) {
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800400 dev_err(chip->dev,
Kylene Halld9e5b6b2005-06-23 22:02:02 -0700401 "tpm_transmit: tpm_send: error %zd\n", rc);
402 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 }
404
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700405 if (chip->vendor.irq)
406 goto out_recv;
407
Kylene Jo Hall9e18ee12006-04-22 02:37:38 -0700408 stop = jiffies + tpm_calc_ordinal_duration(chip, ordinal);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 do {
Kylene Jo Hall90dda522006-04-22 02:37:15 -0700410 u8 status = chip->vendor.status(chip);
411 if ((status & chip->vendor.req_complete_mask) ==
412 chip->vendor.req_complete_val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 goto out_recv;
Kylene Halld9e5b6b2005-06-23 22:02:02 -0700414
Kylene Jo Hall90dda522006-04-22 02:37:15 -0700415 if ((status == chip->vendor.req_canceled)) {
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800416 dev_err(chip->dev, "Operation Canceled\n");
Kylene Halld9e5b6b2005-06-23 22:02:02 -0700417 rc = -ECANCELED;
418 goto out;
419 }
420
421 msleep(TPM_TIMEOUT); /* CHECK */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 rmb();
Nishanth Aravamudan700d8bd2005-06-23 22:01:47 -0700423 } while (time_before(jiffies, stop));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Kylene Jo Hall90dda522006-04-22 02:37:15 -0700425 chip->vendor.cancel(chip);
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800426 dev_err(chip->dev, "Operation Timed out\n");
Kylene Halld9e5b6b2005-06-23 22:02:02 -0700427 rc = -ETIME;
428 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430out_recv:
Kylene Jo Hall90dda522006-04-22 02:37:15 -0700431 rc = chip->vendor.recv(chip, (u8 *) buf, bufsiz);
Kylene Halld9e5b6b2005-06-23 22:02:02 -0700432 if (rc < 0)
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800433 dev_err(chip->dev,
Kylene Halld9e5b6b2005-06-23 22:02:02 -0700434 "tpm_transmit: tpm_recv: error %zd\n", rc);
435out:
Matthias Kaehlcked081d472007-05-08 00:32:02 -0700436 mutex_unlock(&chip->tpm_mutex);
Kylene Halld9e5b6b2005-06-23 22:02:02 -0700437 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438}
439
440#define TPM_DIGEST_SIZE 20
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700441#define TPM_ERROR_SIZE 10
442#define TPM_RET_CODE_IDX 6
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700443
444enum tpm_capabilities {
Rajiv Andrade08837432009-02-02 15:23:43 -0200445 TPM_CAP_FLAG = cpu_to_be32(4),
446 TPM_CAP_PROP = cpu_to_be32(5),
447 CAP_VERSION_1_1 = cpu_to_be32(0x06),
448 CAP_VERSION_1_2 = cpu_to_be32(0x1A)
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700449};
450
451enum tpm_sub_capabilities {
Rajiv Andrade08837432009-02-02 15:23:43 -0200452 TPM_CAP_PROP_PCR = cpu_to_be32(0x101),
453 TPM_CAP_PROP_MANUFACTURER = cpu_to_be32(0x103),
454 TPM_CAP_FLAG_PERM = cpu_to_be32(0x108),
455 TPM_CAP_FLAG_VOL = cpu_to_be32(0x109),
456 TPM_CAP_PROP_OWNER = cpu_to_be32(0x111),
457 TPM_CAP_PROP_TIS_TIMEOUT = cpu_to_be32(0x115),
458 TPM_CAP_PROP_TIS_DURATION = cpu_to_be32(0x120),
459
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700460};
461
Rajiv Andrade08837432009-02-02 15:23:43 -0200462static ssize_t transmit_cmd(struct tpm_chip *chip, struct tpm_cmd_t *cmd,
463 int len, const char *desc)
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700464{
465 int err;
466
Rajiv Andrade08837432009-02-02 15:23:43 -0200467 len = tpm_transmit(chip,(u8 *) cmd, len);
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700468 if (len < 0)
469 return len;
470 if (len == TPM_ERROR_SIZE) {
Rajiv Andrade08837432009-02-02 15:23:43 -0200471 err = be32_to_cpu(cmd->header.out.return_code);
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700472 dev_dbg(chip->dev, "A TPM error (%d) occurred %s\n", err, desc);
473 return err;
474 }
475 return 0;
476}
477
Rajiv Andrade08837432009-02-02 15:23:43 -0200478#define TPM_INTERNAL_RESULT_SIZE 200
479#define TPM_TAG_RQU_COMMAND cpu_to_be16(193)
480#define TPM_ORD_GET_CAP cpu_to_be32(101)
481
482static const struct tpm_input_header tpm_getcap_header = {
483 .tag = TPM_TAG_RQU_COMMAND,
484 .length = cpu_to_be32(22),
485 .ordinal = TPM_ORD_GET_CAP
486};
487
488ssize_t tpm_getcap(struct device *dev, __be32 subcap_id, cap_t *cap,
489 const char *desc)
490{
491 struct tpm_cmd_t tpm_cmd;
492 int rc;
493 struct tpm_chip *chip = dev_get_drvdata(dev);
494
495 tpm_cmd.header.in = tpm_getcap_header;
496 if (subcap_id == CAP_VERSION_1_1 || subcap_id == CAP_VERSION_1_2) {
497 tpm_cmd.params.getcap_in.cap = subcap_id;
498 /*subcap field not necessary */
499 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(0);
500 tpm_cmd.header.in.length -= cpu_to_be32(sizeof(__be32));
501 } else {
502 if (subcap_id == TPM_CAP_FLAG_PERM ||
503 subcap_id == TPM_CAP_FLAG_VOL)
504 tpm_cmd.params.getcap_in.cap = TPM_CAP_FLAG;
505 else
506 tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
507 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
508 tpm_cmd.params.getcap_in.subcap = subcap_id;
509 }
510 rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE, desc);
511 if (!rc)
512 *cap = tpm_cmd.params.getcap_out.cap;
513 return rc;
514}
515
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700516void tpm_gen_interrupt(struct tpm_chip *chip)
517{
Rajiv Andrade08837432009-02-02 15:23:43 -0200518 struct tpm_cmd_t tpm_cmd;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700519 ssize_t rc;
520
Rajiv Andrade08837432009-02-02 15:23:43 -0200521 tpm_cmd.header.in = tpm_getcap_header;
522 tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
523 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
524 tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_TIMEOUT;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700525
Rajiv Andrade08837432009-02-02 15:23:43 -0200526 rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE,
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700527 "attempting to determine the timeouts");
528}
529EXPORT_SYMBOL_GPL(tpm_gen_interrupt);
530
Stefan Berger2b30a902011-11-11 12:57:02 -0500531int tpm_get_timeouts(struct tpm_chip *chip)
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700532{
Rajiv Andrade08837432009-02-02 15:23:43 -0200533 struct tpm_cmd_t tpm_cmd;
534 struct timeout_t *timeout_cap;
535 struct duration_t *duration_cap;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700536 ssize_t rc;
537 u32 timeout;
Stefan Bergere3e1a1e2011-03-30 12:13:27 -0400538 unsigned int scale = 1;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700539
Rajiv Andrade08837432009-02-02 15:23:43 -0200540 tpm_cmd.header.in = tpm_getcap_header;
541 tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
542 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
543 tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_TIMEOUT;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700544
Rajiv Andrade08837432009-02-02 15:23:43 -0200545 rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE,
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700546 "attempting to determine the timeouts");
547 if (rc)
548 goto duration;
549
Stefan Berger829bf062011-03-30 12:13:26 -0400550 if (be32_to_cpu(tpm_cmd.header.out.return_code) != 0 ||
551 be32_to_cpu(tpm_cmd.header.out.length)
552 != sizeof(tpm_cmd.header.out) + sizeof(u32) + 4 * sizeof(u32))
Stefan Berger2b30a902011-11-11 12:57:02 -0500553 return -EINVAL;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700554
Rajiv Andrade08837432009-02-02 15:23:43 -0200555 timeout_cap = &tpm_cmd.params.getcap_out.cap.timeout;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700556 /* Don't overwrite default if value is 0 */
Rajiv Andrade08837432009-02-02 15:23:43 -0200557 timeout = be32_to_cpu(timeout_cap->a);
Stefan Bergere3e1a1e2011-03-30 12:13:27 -0400558 if (timeout && timeout < 1000) {
559 /* timeouts in msec rather usec */
560 scale = 1000;
Stefan Berger62592102011-03-30 12:13:28 -0400561 chip->vendor.timeout_adjusted = true;
Stefan Bergere3e1a1e2011-03-30 12:13:27 -0400562 }
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700563 if (timeout)
Stefan Bergere3e1a1e2011-03-30 12:13:27 -0400564 chip->vendor.timeout_a = usecs_to_jiffies(timeout * scale);
Rajiv Andrade08837432009-02-02 15:23:43 -0200565 timeout = be32_to_cpu(timeout_cap->b);
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700566 if (timeout)
Stefan Bergere3e1a1e2011-03-30 12:13:27 -0400567 chip->vendor.timeout_b = usecs_to_jiffies(timeout * scale);
Rajiv Andrade08837432009-02-02 15:23:43 -0200568 timeout = be32_to_cpu(timeout_cap->c);
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700569 if (timeout)
Stefan Bergere3e1a1e2011-03-30 12:13:27 -0400570 chip->vendor.timeout_c = usecs_to_jiffies(timeout * scale);
Rajiv Andrade08837432009-02-02 15:23:43 -0200571 timeout = be32_to_cpu(timeout_cap->d);
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700572 if (timeout)
Stefan Bergere3e1a1e2011-03-30 12:13:27 -0400573 chip->vendor.timeout_d = usecs_to_jiffies(timeout * scale);
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700574
575duration:
Rajiv Andrade08837432009-02-02 15:23:43 -0200576 tpm_cmd.header.in = tpm_getcap_header;
577 tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
578 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
579 tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_DURATION;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700580
Rajiv Andrade08837432009-02-02 15:23:43 -0200581 rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE,
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700582 "attempting to determine the durations");
583 if (rc)
Stefan Berger2b30a902011-11-11 12:57:02 -0500584 return rc;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700585
Stefan Berger979b1402011-03-30 12:13:23 -0400586 if (be32_to_cpu(tpm_cmd.header.out.return_code) != 0 ||
587 be32_to_cpu(tpm_cmd.header.out.length)
588 != sizeof(tpm_cmd.header.out) + sizeof(u32) + 3 * sizeof(u32))
Stefan Berger2b30a902011-11-11 12:57:02 -0500589 return -EINVAL;
Stefan Berger979b1402011-03-30 12:13:23 -0400590
Rajiv Andrade08837432009-02-02 15:23:43 -0200591 duration_cap = &tpm_cmd.params.getcap_out.cap.duration;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700592 chip->vendor.duration[TPM_SHORT] =
Rajiv Andrade08837432009-02-02 15:23:43 -0200593 usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_short));
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700594 chip->vendor.duration[TPM_MEDIUM] =
Rajiv Andrade08837432009-02-02 15:23:43 -0200595 usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_medium));
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700596 chip->vendor.duration[TPM_LONG] =
Rajiv Andrade08837432009-02-02 15:23:43 -0200597 usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_long));
Stefan Bergere934acc2011-03-30 12:13:24 -0400598
599 /* The Broadcom BCM0102 chipset in a Dell Latitude D820 gets the above
600 * value wrong and apparently reports msecs rather than usecs. So we
601 * fix up the resulting too-small TPM_SHORT value to make things work.
602 * We also scale the TPM_MEDIUM and -_LONG values by 1000.
603 */
604 if (chip->vendor.duration[TPM_SHORT] < (HZ / 100)) {
605 chip->vendor.duration[TPM_SHORT] = HZ;
606 chip->vendor.duration[TPM_MEDIUM] *= 1000;
607 chip->vendor.duration[TPM_LONG] *= 1000;
Stefan Berger04ab2292011-03-30 12:13:25 -0400608 chip->vendor.duration_adjusted = true;
Stefan Bergere934acc2011-03-30 12:13:24 -0400609 dev_info(chip->dev, "Adjusting TPM timeout parameters.");
610 }
Stefan Berger2b30a902011-11-11 12:57:02 -0500611 return 0;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700612}
613EXPORT_SYMBOL_GPL(tpm_get_timeouts);
614
Stefan Bergerd97c6ad2011-11-11 12:57:03 -0500615#define TPM_ORD_CONTINUE_SELFTEST 83
616#define CONTINUE_SELFTEST_RESULT_SIZE 10
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700617
Stefan Bergerd97c6ad2011-11-11 12:57:03 -0500618static struct tpm_input_header continue_selftest_header = {
619 .tag = TPM_TAG_RQU_COMMAND,
620 .length = cpu_to_be32(10),
621 .ordinal = cpu_to_be32(TPM_ORD_CONTINUE_SELFTEST),
622};
623
624/**
625 * tpm_continue_selftest -- run TPM's selftest
626 * @chip: TPM chip to use
627 *
628 * Returns 0 on success, < 0 in case of fatal error or a value > 0 representing
629 * a TPM error code.
630 */
Stefan Berger68d6e672011-11-11 12:57:04 -0500631static int tpm_continue_selftest(struct tpm_chip *chip)
Stefan Bergerd97c6ad2011-11-11 12:57:03 -0500632{
633 int rc;
634 struct tpm_cmd_t cmd;
635
636 cmd.header.in = continue_selftest_header;
637 rc = transmit_cmd(chip, &cmd, CONTINUE_SELFTEST_RESULT_SIZE,
638 "continue selftest");
639 return rc;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700640}
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700641
642ssize_t tpm_show_enabled(struct device * dev, struct device_attribute * attr,
643 char *buf)
644{
Rajiv Andrade08837432009-02-02 15:23:43 -0200645 cap_t cap;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700646 ssize_t rc;
647
Rajiv Andrade08837432009-02-02 15:23:43 -0200648 rc = tpm_getcap(dev, TPM_CAP_FLAG_PERM, &cap,
649 "attempting to determine the permanent enabled state");
650 if (rc)
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700651 return 0;
Marcin Obaraec288bd2008-07-25 19:44:59 -0700652
Rajiv Andrade08837432009-02-02 15:23:43 -0200653 rc = sprintf(buf, "%d\n", !cap.perm_flags.disable);
Marcin Obaraec288bd2008-07-25 19:44:59 -0700654 return rc;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700655}
656EXPORT_SYMBOL_GPL(tpm_show_enabled);
657
658ssize_t tpm_show_active(struct device * dev, struct device_attribute * attr,
659 char *buf)
660{
Rajiv Andrade08837432009-02-02 15:23:43 -0200661 cap_t cap;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700662 ssize_t rc;
663
Rajiv Andrade08837432009-02-02 15:23:43 -0200664 rc = tpm_getcap(dev, TPM_CAP_FLAG_PERM, &cap,
665 "attempting to determine the permanent active state");
666 if (rc)
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700667 return 0;
Marcin Obaraec288bd2008-07-25 19:44:59 -0700668
Rajiv Andrade08837432009-02-02 15:23:43 -0200669 rc = sprintf(buf, "%d\n", !cap.perm_flags.deactivated);
Marcin Obaraec288bd2008-07-25 19:44:59 -0700670 return rc;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700671}
672EXPORT_SYMBOL_GPL(tpm_show_active);
673
674ssize_t tpm_show_owned(struct device * dev, struct device_attribute * attr,
675 char *buf)
676{
Rajiv Andrade08837432009-02-02 15:23:43 -0200677 cap_t cap;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700678 ssize_t rc;
679
Rajiv Andrade08837432009-02-02 15:23:43 -0200680 rc = tpm_getcap(dev, TPM_CAP_PROP_OWNER, &cap,
681 "attempting to determine the owner state");
682 if (rc)
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700683 return 0;
Marcin Obaraec288bd2008-07-25 19:44:59 -0700684
Rajiv Andrade08837432009-02-02 15:23:43 -0200685 rc = sprintf(buf, "%d\n", cap.owned);
Marcin Obaraec288bd2008-07-25 19:44:59 -0700686 return rc;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700687}
688EXPORT_SYMBOL_GPL(tpm_show_owned);
689
690ssize_t tpm_show_temp_deactivated(struct device * dev,
691 struct device_attribute * attr, char *buf)
692{
Rajiv Andrade08837432009-02-02 15:23:43 -0200693 cap_t cap;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700694 ssize_t rc;
695
Rajiv Andrade08837432009-02-02 15:23:43 -0200696 rc = tpm_getcap(dev, TPM_CAP_FLAG_VOL, &cap,
697 "attempting to determine the temporary state");
698 if (rc)
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700699 return 0;
Marcin Obaraec288bd2008-07-25 19:44:59 -0700700
Rajiv Andrade08837432009-02-02 15:23:43 -0200701 rc = sprintf(buf, "%d\n", cap.stclear_flags.deactivated);
Marcin Obaraec288bd2008-07-25 19:44:59 -0700702 return rc;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700703}
704EXPORT_SYMBOL_GPL(tpm_show_temp_deactivated);
705
Rajiv Andrade659aaf22009-02-02 15:23:44 -0200706/*
707 * tpm_chip_find_get - return tpm_chip for given chip number
708 */
709static struct tpm_chip *tpm_chip_find_get(int chip_num)
710{
Rajiv Andrade8920d5a2009-02-05 13:06:30 -0200711 struct tpm_chip *pos, *chip = NULL;
Rajiv Andrade659aaf22009-02-02 15:23:44 -0200712
713 rcu_read_lock();
714 list_for_each_entry_rcu(pos, &tpm_chip_list, list) {
715 if (chip_num != TPM_ANY_NUM && chip_num != pos->dev_num)
716 continue;
717
Rajiv Andrade8920d5a2009-02-05 13:06:30 -0200718 if (try_module_get(pos->dev->driver->owner)) {
719 chip = pos;
Rajiv Andrade659aaf22009-02-02 15:23:44 -0200720 break;
Rajiv Andrade8920d5a2009-02-05 13:06:30 -0200721 }
Rajiv Andrade659aaf22009-02-02 15:23:44 -0200722 }
723 rcu_read_unlock();
Rajiv Andrade8920d5a2009-02-05 13:06:30 -0200724 return chip;
Rajiv Andrade659aaf22009-02-02 15:23:44 -0200725}
726
727#define TPM_ORDINAL_PCRREAD cpu_to_be32(21)
728#define READ_PCR_RESULT_SIZE 30
729static struct tpm_input_header pcrread_header = {
730 .tag = TPM_TAG_RQU_COMMAND,
731 .length = cpu_to_be32(14),
732 .ordinal = TPM_ORDINAL_PCRREAD
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733};
734
Stefan Berger68d6e672011-11-11 12:57:04 -0500735static int __tpm_pcr_read(struct tpm_chip *chip, int pcr_idx, u8 *res_buf)
Rajiv Andrade659aaf22009-02-02 15:23:44 -0200736{
737 int rc;
738 struct tpm_cmd_t cmd;
739
740 cmd.header.in = pcrread_header;
741 cmd.params.pcrread_in.pcr_idx = cpu_to_be32(pcr_idx);
Rajiv Andrade23acb982009-09-30 12:26:55 -0300742 rc = transmit_cmd(chip, &cmd, READ_PCR_RESULT_SIZE,
Rajiv Andrade659aaf22009-02-02 15:23:44 -0200743 "attempting to read a pcr value");
744
745 if (rc == 0)
746 memcpy(res_buf, cmd.params.pcrread_out.pcr_result,
747 TPM_DIGEST_SIZE);
748 return rc;
749}
750
751/**
752 * tpm_pcr_read - read a pcr value
753 * @chip_num: tpm idx # or ANY
754 * @pcr_idx: pcr idx to retrieve
755 * @res_buf: TPM_PCR value
756 * size of res_buf is 20 bytes (or NULL if you don't care)
757 *
758 * The TPM driver should be built-in, but for whatever reason it
759 * isn't, protect against the chip disappearing, by incrementing
760 * the module usage count.
761 */
762int tpm_pcr_read(u32 chip_num, int pcr_idx, u8 *res_buf)
763{
764 struct tpm_chip *chip;
765 int rc;
766
767 chip = tpm_chip_find_get(chip_num);
768 if (chip == NULL)
769 return -ENODEV;
770 rc = __tpm_pcr_read(chip, pcr_idx, res_buf);
Mimi Zohara0e39342010-11-23 17:50:32 -0500771 tpm_chip_put(chip);
Rajiv Andrade659aaf22009-02-02 15:23:44 -0200772 return rc;
773}
774EXPORT_SYMBOL_GPL(tpm_pcr_read);
775
776/**
777 * tpm_pcr_extend - extend pcr value with hash
778 * @chip_num: tpm idx # or AN&
779 * @pcr_idx: pcr idx to extend
780 * @hash: hash value used to extend pcr value
781 *
782 * The TPM driver should be built-in, but for whatever reason it
783 * isn't, protect against the chip disappearing, by incrementing
784 * the module usage count.
785 */
786#define TPM_ORD_PCR_EXTEND cpu_to_be32(20)
Jason Gunthorpe0afd9052009-09-18 12:54:24 -0700787#define EXTEND_PCR_RESULT_SIZE 34
Rajiv Andrade659aaf22009-02-02 15:23:44 -0200788static struct tpm_input_header pcrextend_header = {
789 .tag = TPM_TAG_RQU_COMMAND,
790 .length = cpu_to_be32(34),
791 .ordinal = TPM_ORD_PCR_EXTEND
792};
793
794int tpm_pcr_extend(u32 chip_num, int pcr_idx, const u8 *hash)
795{
796 struct tpm_cmd_t cmd;
797 int rc;
798 struct tpm_chip *chip;
799
800 chip = tpm_chip_find_get(chip_num);
801 if (chip == NULL)
802 return -ENODEV;
803
804 cmd.header.in = pcrextend_header;
Rajiv Andrade659aaf22009-02-02 15:23:44 -0200805 cmd.params.pcrextend_in.pcr_idx = cpu_to_be32(pcr_idx);
806 memcpy(cmd.params.pcrextend_in.hash, hash, TPM_DIGEST_SIZE);
Jason Gunthorpe0afd9052009-09-18 12:54:24 -0700807 rc = transmit_cmd(chip, &cmd, EXTEND_PCR_RESULT_SIZE,
Rajiv Andrade659aaf22009-02-02 15:23:44 -0200808 "attempting extend a PCR value");
809
Mimi Zohara0e39342010-11-23 17:50:32 -0500810 tpm_chip_put(chip);
Rajiv Andrade659aaf22009-02-02 15:23:44 -0200811 return rc;
812}
813EXPORT_SYMBOL_GPL(tpm_pcr_extend);
814
Stefan Berger68d6e672011-11-11 12:57:04 -0500815/**
816 * tpm_do_selftest - have the TPM continue its selftest and wait until it
817 * can receive further commands
818 * @chip: TPM chip to use
819 *
820 * Returns 0 on success, < 0 in case of fatal error or a value > 0 representing
821 * a TPM error code.
822 */
823int tpm_do_selftest(struct tpm_chip *chip)
824{
825 int rc;
826 u8 digest[TPM_DIGEST_SIZE];
827 unsigned int loops;
828 unsigned int delay_msec = 1000;
829 unsigned long duration;
830
831 duration = tpm_calc_ordinal_duration(chip,
832 TPM_ORD_CONTINUE_SELFTEST);
833
834 loops = jiffies_to_msecs(duration) / delay_msec;
835
836 rc = tpm_continue_selftest(chip);
837 /* This may fail if there was no TPM driver during a suspend/resume
838 * cycle; some may return 10 (BAD_ORDINAL), others 28 (FAILEDSELFTEST)
839 */
840 if (rc)
841 return rc;
842
843 do {
844 rc = __tpm_pcr_read(chip, 0, digest);
845 if (rc != TPM_WARN_DOING_SELFTEST)
846 return rc;
847 msleep(delay_msec);
848 } while (--loops > 0);
849
850 return rc;
851}
852EXPORT_SYMBOL_GPL(tpm_do_selftest);
853
Mimi Zoharc749ba92010-11-23 18:54:16 -0500854int tpm_send(u32 chip_num, void *cmd, size_t buflen)
855{
856 struct tpm_chip *chip;
857 int rc;
858
859 chip = tpm_chip_find_get(chip_num);
860 if (chip == NULL)
861 return -ENODEV;
862
863 rc = transmit_cmd(chip, cmd, buflen, "attempting tpm_cmd");
864
865 tpm_chip_put(chip);
866 return rc;
867}
868EXPORT_SYMBOL_GPL(tpm_send);
869
Kylene Hall6659ca22005-06-23 22:02:00 -0700870ssize_t tpm_show_pcrs(struct device *dev, struct device_attribute *attr,
871 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872{
Rajiv Andrade08837432009-02-02 15:23:43 -0200873 cap_t cap;
Rajiv Andrade659aaf22009-02-02 15:23:44 -0200874 u8 digest[TPM_DIGEST_SIZE];
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700875 ssize_t rc;
Kylene Hall81179bb2005-06-23 22:01:59 -0700876 int i, j, num_pcrs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 char *str = buf;
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800878 struct tpm_chip *chip = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
Rajiv Andrade08837432009-02-02 15:23:43 -0200880 rc = tpm_getcap(dev, TPM_CAP_PROP_PCR, &cap,
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700881 "attempting to determine the number of PCRS");
Rajiv Andrade08837432009-02-02 15:23:43 -0200882 if (rc)
Kylene Halle234bc92005-06-23 22:02:08 -0700883 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Rajiv Andrade08837432009-02-02 15:23:43 -0200885 num_pcrs = be32_to_cpu(cap.num_pcrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 for (i = 0; i < num_pcrs; i++) {
Rajiv Andrade659aaf22009-02-02 15:23:44 -0200887 rc = __tpm_pcr_read(chip, i, digest);
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700888 if (rc)
Rajiv Andrade08837432009-02-02 15:23:43 -0200889 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 str += sprintf(str, "PCR-%02d: ", i);
891 for (j = 0; j < TPM_DIGEST_SIZE; j++)
Rajiv Andrade659aaf22009-02-02 15:23:44 -0200892 str += sprintf(str, "%02X ", digest[j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 str += sprintf(str, "\n");
894 }
895 return str - buf;
896}
Kylene Hall6659ca22005-06-23 22:02:00 -0700897EXPORT_SYMBOL_GPL(tpm_show_pcrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
899#define READ_PUBEK_RESULT_SIZE 314
Rajiv Andrade08837432009-02-02 15:23:43 -0200900#define TPM_ORD_READPUBEK cpu_to_be32(124)
901struct tpm_input_header tpm_readpubek_header = {
902 .tag = TPM_TAG_RQU_COMMAND,
903 .length = cpu_to_be32(30),
904 .ordinal = TPM_ORD_READPUBEK
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905};
906
Kylene Hall6659ca22005-06-23 22:02:00 -0700907ssize_t tpm_show_pubek(struct device *dev, struct device_attribute *attr,
908 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909{
Kylene Hall2df71112005-06-23 22:01:54 -0700910 u8 *data;
Rajiv Andrade08837432009-02-02 15:23:43 -0200911 struct tpm_cmd_t tpm_cmd;
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700912 ssize_t err;
Kylene Hall81179bb2005-06-23 22:01:59 -0700913 int i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 char *str = buf;
915
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800916 struct tpm_chip *chip = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
Rajiv Andrade08837432009-02-02 15:23:43 -0200918 tpm_cmd.header.in = tpm_readpubek_header;
919 err = transmit_cmd(chip, &tpm_cmd, READ_PUBEK_RESULT_SIZE,
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700920 "attempting to read the PUBEK");
921 if (err)
Kylene Jo Hall34d6e072005-06-23 22:02:10 -0700922 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
924 /*
925 ignore header 10 bytes
926 algorithm 32 bits (1 == RSA )
927 encscheme 16 bits
928 sigscheme 16 bits
929 parameters (RSA 12->bytes: keybit, #primes, expbit)
930 keylenbytes 32 bits
931 256 byte modulus
932 ignore checksum 20 bytes
933 */
Rajiv Andrade08837432009-02-02 15:23:43 -0200934 data = tpm_cmd.params.readpubek_out_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 str +=
936 sprintf(str,
Stefan Berger5a794442011-03-30 12:13:29 -0400937 "Algorithm: %02X %02X %02X %02X\n"
938 "Encscheme: %02X %02X\n"
939 "Sigscheme: %02X %02X\n"
940 "Parameters: %02X %02X %02X %02X "
941 "%02X %02X %02X %02X "
942 "%02X %02X %02X %02X\n"
943 "Modulus length: %d\n"
944 "Modulus:\n",
945 data[0], data[1], data[2], data[3],
946 data[4], data[5],
947 data[6], data[7],
948 data[12], data[13], data[14], data[15],
949 data[16], data[17], data[18], data[19],
950 data[20], data[21], data[22], data[23],
951 be32_to_cpu(*((__be32 *) (data + 24))));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
953 for (i = 0; i < 256; i++) {
Stefan Berger5a794442011-03-30 12:13:29 -0400954 str += sprintf(str, "%02X ", data[i + 28]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 if ((i + 1) % 16 == 0)
956 str += sprintf(str, "\n");
957 }
Kylene Jo Hall34d6e072005-06-23 22:02:10 -0700958out:
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700959 rc = str - buf;
Kylene Hall2df71112005-06-23 22:01:54 -0700960 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961}
Kylene Hall6659ca22005-06-23 22:02:00 -0700962EXPORT_SYMBOL_GPL(tpm_show_pubek);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
Kylene Hall6659ca22005-06-23 22:02:00 -0700965ssize_t tpm_show_caps(struct device *dev, struct device_attribute *attr,
966 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967{
Rajiv Andrade08837432009-02-02 15:23:43 -0200968 cap_t cap;
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700969 ssize_t rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 char *str = buf;
971
Rajiv Andrade08837432009-02-02 15:23:43 -0200972 rc = tpm_getcap(dev, TPM_CAP_PROP_MANUFACTURER, &cap,
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700973 "attempting to determine the manufacturer");
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700974 if (rc)
Rajiv Andrade08837432009-02-02 15:23:43 -0200975 return 0;
976 str += sprintf(str, "Manufacturer: 0x%x\n",
977 be32_to_cpu(cap.manufacturer_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
Rajiv Andrade08837432009-02-02 15:23:43 -0200979 rc = tpm_getcap(dev, CAP_VERSION_1_1, &cap,
980 "attempting to determine the 1.1 version");
981 if (rc)
982 return 0;
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700983 str += sprintf(str,
984 "TCG version: %d.%d\nFirmware version: %d.%d\n",
Rajiv Andrade08837432009-02-02 15:23:43 -0200985 cap.tpm_version.Major, cap.tpm_version.Minor,
986 cap.tpm_version.revMajor, cap.tpm_version.revMinor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 return str - buf;
988}
Kylene Hall6659ca22005-06-23 22:02:00 -0700989EXPORT_SYMBOL_GPL(tpm_show_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700991ssize_t tpm_show_caps_1_2(struct device * dev,
992 struct device_attribute * attr, char *buf)
993{
Rajiv Andrade08837432009-02-02 15:23:43 -0200994 cap_t cap;
995 ssize_t rc;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700996 char *str = buf;
997
Rajiv Andrade08837432009-02-02 15:23:43 -0200998 rc = tpm_getcap(dev, TPM_CAP_PROP_MANUFACTURER, &cap,
999 "attempting to determine the manufacturer");
1000 if (rc)
Kylene Jo Hall08e96e42006-04-22 02:37:50 -07001001 return 0;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -07001002 str += sprintf(str, "Manufacturer: 0x%x\n",
Rajiv Andrade08837432009-02-02 15:23:43 -02001003 be32_to_cpu(cap.manufacturer_id));
1004 rc = tpm_getcap(dev, CAP_VERSION_1_2, &cap,
1005 "attempting to determine the 1.2 version");
1006 if (rc)
1007 return 0;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -07001008 str += sprintf(str,
1009 "TCG version: %d.%d\nFirmware version: %d.%d\n",
Rajiv Andrade08837432009-02-02 15:23:43 -02001010 cap.tpm_version_1_2.Major, cap.tpm_version_1_2.Minor,
1011 cap.tpm_version_1_2.revMajor,
1012 cap.tpm_version_1_2.revMinor);
Kylene Jo Hall08e96e42006-04-22 02:37:50 -07001013 return str - buf;
1014}
1015EXPORT_SYMBOL_GPL(tpm_show_caps_1_2);
1016
Stefan Berger04ab2292011-03-30 12:13:25 -04001017ssize_t tpm_show_durations(struct device *dev, struct device_attribute *attr,
1018 char *buf)
1019{
1020 struct tpm_chip *chip = dev_get_drvdata(dev);
1021
Stefan Berger403d1d02011-08-23 08:52:10 -04001022 if (chip->vendor.duration[TPM_LONG] == 0)
1023 return 0;
1024
Stefan Berger04ab2292011-03-30 12:13:25 -04001025 return sprintf(buf, "%d %d %d [%s]\n",
1026 jiffies_to_usecs(chip->vendor.duration[TPM_SHORT]),
1027 jiffies_to_usecs(chip->vendor.duration[TPM_MEDIUM]),
1028 jiffies_to_usecs(chip->vendor.duration[TPM_LONG]),
1029 chip->vendor.duration_adjusted
1030 ? "adjusted" : "original");
1031}
1032EXPORT_SYMBOL_GPL(tpm_show_durations);
1033
Stefan Berger62592102011-03-30 12:13:28 -04001034ssize_t tpm_show_timeouts(struct device *dev, struct device_attribute *attr,
1035 char *buf)
1036{
1037 struct tpm_chip *chip = dev_get_drvdata(dev);
1038
1039 return sprintf(buf, "%d %d %d %d [%s]\n",
1040 jiffies_to_usecs(chip->vendor.timeout_a),
1041 jiffies_to_usecs(chip->vendor.timeout_b),
1042 jiffies_to_usecs(chip->vendor.timeout_c),
1043 jiffies_to_usecs(chip->vendor.timeout_d),
1044 chip->vendor.timeout_adjusted
1045 ? "adjusted" : "original");
1046}
1047EXPORT_SYMBOL_GPL(tpm_show_timeouts);
1048
Kylene Hall6659ca22005-06-23 22:02:00 -07001049ssize_t tpm_store_cancel(struct device *dev, struct device_attribute *attr,
1050 const char *buf, size_t count)
1051{
1052 struct tpm_chip *chip = dev_get_drvdata(dev);
1053 if (chip == NULL)
1054 return 0;
1055
Kylene Jo Hall90dda522006-04-22 02:37:15 -07001056 chip->vendor.cancel(chip);
Kylene Hall6659ca22005-06-23 22:02:00 -07001057 return count;
1058}
1059EXPORT_SYMBOL_GPL(tpm_store_cancel);
1060
Rajiv Andradefd048862011-09-16 14:39:40 -03001061int wait_for_tpm_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout,
1062 wait_queue_head_t *queue)
1063{
1064 unsigned long stop;
1065 long rc;
1066 u8 status;
1067
1068 /* check current status */
1069 status = chip->vendor.status(chip);
1070 if ((status & mask) == mask)
1071 return 0;
1072
1073 stop = jiffies + timeout;
1074
1075 if (chip->vendor.irq) {
1076again:
1077 timeout = stop - jiffies;
1078 if ((long)timeout <= 0)
1079 return -ETIME;
1080 rc = wait_event_interruptible_timeout(*queue,
1081 ((chip->vendor.status(chip)
1082 & mask) == mask),
1083 timeout);
1084 if (rc > 0)
1085 return 0;
1086 if (rc == -ERESTARTSYS && freezing(current)) {
1087 clear_thread_flag(TIF_SIGPENDING);
1088 goto again;
1089 }
1090 } else {
1091 do {
1092 msleep(TPM_TIMEOUT);
1093 status = chip->vendor.status(chip);
1094 if ((status & mask) == mask)
1095 return 0;
1096 } while (time_before(jiffies, stop));
1097 }
1098 return -ETIME;
1099}
1100EXPORT_SYMBOL_GPL(wait_for_tpm_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101/*
1102 * Device file system interface to the TPM
Rajiv Andradef89c5ed2008-10-11 09:03:39 +11001103 *
1104 * It's assured that the chip will be opened just once,
1105 * by the check of is_open variable, which is protected
1106 * by driver_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 */
1108int tpm_open(struct inode *inode, struct file *file)
1109{
Rajiv Andradef02a9362008-10-11 09:04:23 +11001110 int minor = iminor(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 struct tpm_chip *chip = NULL, *pos;
1112
Rajiv Andradef02a9362008-10-11 09:04:23 +11001113 rcu_read_lock();
1114 list_for_each_entry_rcu(pos, &tpm_chip_list, list) {
Kylene Jo Hall90dda522006-04-22 02:37:15 -07001115 if (pos->vendor.miscdev.minor == minor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 chip = pos;
Rajiv Andradef02a9362008-10-11 09:04:23 +11001117 get_device(chip->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 break;
1119 }
1120 }
Rajiv Andradef02a9362008-10-11 09:04:23 +11001121 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
Rajiv Andradef02a9362008-10-11 09:04:23 +11001123 if (!chip)
1124 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
Rajiv Andradedc36d322008-10-11 09:04:02 +11001126 if (test_and_set_bit(0, &chip->is_open)) {
Andrew Mortonb888c872005-10-30 15:03:28 -08001127 dev_dbg(chip->dev, "Another process owns this TPM\n");
Rajiv Andradef02a9362008-10-11 09:04:23 +11001128 put_device(chip->dev);
1129 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 }
1131
Peter Huewe1309d7a2011-03-29 13:31:25 +02001132 chip->data_buffer = kzalloc(TPM_BUFSIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 if (chip->data_buffer == NULL) {
Rajiv Andradedc36d322008-10-11 09:04:02 +11001134 clear_bit(0, &chip->is_open);
Kylene Jo Halle659a3f2005-10-30 15:03:24 -08001135 put_device(chip->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 return -ENOMEM;
1137 }
1138
1139 atomic_set(&chip->data_pending, 0);
1140
1141 file->private_data = chip;
1142 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144EXPORT_SYMBOL_GPL(tpm_open);
1145
Rajiv Andradef02a9362008-10-11 09:04:23 +11001146/*
1147 * Called on file close
1148 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149int tpm_release(struct inode *inode, struct file *file)
1150{
1151 struct tpm_chip *chip = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
Rajiv Andrade4bdec112008-10-11 09:05:20 +11001153 del_singleshot_timer_sync(&chip->user_read_timer);
Tejun Heo2e5c44c2010-12-24 15:59:07 +01001154 flush_work_sync(&chip->work);
Kylene Hall5e976d52005-06-23 22:02:03 -07001155 file->private_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 atomic_set(&chip->data_pending, 0);
Rajiv Andradef02a9362008-10-11 09:04:23 +11001157 kfree(chip->data_buffer);
Rajiv Andradedc36d322008-10-11 09:04:02 +11001158 clear_bit(0, &chip->is_open);
Kylene Jo Halle659a3f2005-10-30 15:03:24 -08001159 put_device(chip->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 return 0;
1161}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162EXPORT_SYMBOL_GPL(tpm_release);
1163
Andrew Mortonb888c872005-10-30 15:03:28 -08001164ssize_t tpm_write(struct file *file, const char __user *buf,
Kylene Jo Hall3c2f6062006-04-22 02:36:56 -07001165 size_t size, loff_t *off)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166{
1167 struct tpm_chip *chip = file->private_data;
Michael Halcrow01476002008-07-25 19:45:00 -07001168 size_t in_size = size, out_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
1170 /* cannot perform a write until the read has cleared
1171 either via tpm_read or a user_read_timer timeout */
Nishanth Aravamudan700d8bd2005-06-23 22:01:47 -07001172 while (atomic_read(&chip->data_pending) != 0)
1173 msleep(TPM_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
Matthias Kaehlcked081d472007-05-08 00:32:02 -07001175 mutex_lock(&chip->buffer_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
1177 if (in_size > TPM_BUFSIZE)
1178 in_size = TPM_BUFSIZE;
1179
1180 if (copy_from_user
1181 (chip->data_buffer, (void __user *) buf, in_size)) {
Matthias Kaehlcked081d472007-05-08 00:32:02 -07001182 mutex_unlock(&chip->buffer_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 return -EFAULT;
1184 }
1185
1186 /* atomic tpm command send and result receive */
1187 out_size = tpm_transmit(chip, chip->data_buffer, TPM_BUFSIZE);
1188
1189 atomic_set(&chip->data_pending, out_size);
Matthias Kaehlcked081d472007-05-08 00:32:02 -07001190 mutex_unlock(&chip->buffer_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
1192 /* Set a timeout by which the reader must come claim the result */
Kylene Hallfe3fd4832005-06-23 22:01:56 -07001193 mod_timer(&chip->user_read_timer, jiffies + (60 * HZ));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
1195 return in_size;
1196}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197EXPORT_SYMBOL_GPL(tpm_write);
1198
Kylene Jo Hall3c2f6062006-04-22 02:36:56 -07001199ssize_t tpm_read(struct file *file, char __user *buf,
1200 size_t size, loff_t *off)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201{
1202 struct tpm_chip *chip = file->private_data;
Michael Halcrow01476002008-07-25 19:45:00 -07001203 ssize_t ret_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
Kylene Hall5b44bd52005-06-23 22:01:53 -07001205 del_singleshot_timer_sync(&chip->user_read_timer);
Tejun Heo2e5c44c2010-12-24 15:59:07 +01001206 flush_work_sync(&chip->work);
Kylene Hall5b44bd52005-06-23 22:01:53 -07001207 ret_size = atomic_read(&chip->data_pending);
1208 atomic_set(&chip->data_pending, 0);
1209 if (ret_size > 0) { /* relay data */
1210 if (size < ret_size)
1211 ret_size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
Matthias Kaehlcked081d472007-05-08 00:32:02 -07001213 mutex_lock(&chip->buffer_mutex);
Kylene Jo Hallf6a23822005-11-13 16:07:42 -08001214 if (copy_to_user(buf, chip->data_buffer, ret_size))
Kylene Hall5b44bd52005-06-23 22:01:53 -07001215 ret_size = -EFAULT;
Matthias Kaehlcked081d472007-05-08 00:32:02 -07001216 mutex_unlock(&chip->buffer_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 }
1218
1219 return ret_size;
1220}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221EXPORT_SYMBOL_GPL(tpm_read);
1222
Kylene Jo Halle659a3f2005-10-30 15:03:24 -08001223void tpm_remove_hardware(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224{
Kylene Jo Halle659a3f2005-10-30 15:03:24 -08001225 struct tpm_chip *chip = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
1227 if (chip == NULL) {
Kylene Jo Halle659a3f2005-10-30 15:03:24 -08001228 dev_err(dev, "No device data found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 return;
1230 }
1231
1232 spin_lock(&driver_lock);
Rajiv Andradef02a9362008-10-11 09:04:23 +11001233 list_del_rcu(&chip->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 spin_unlock(&driver_lock);
Rajiv Andradef02a9362008-10-11 09:04:23 +11001235 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
Kylene Jo Hall90dda522006-04-22 02:37:15 -07001237 misc_deregister(&chip->vendor.miscdev);
Kylene Jo Hall90dda522006-04-22 02:37:15 -07001238 sysfs_remove_group(&dev->kobj, chip->vendor.attr_group);
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -08001239 tpm_bios_log_teardown(chip->bios_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
Richard MUSIL5bd91f12008-02-06 01:37:02 -08001241 /* write it this way to be explicit (chip->dev == dev) */
1242 put_device(chip->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243}
Kylene Jo Halle659a3f2005-10-30 15:03:24 -08001244EXPORT_SYMBOL_GPL(tpm_remove_hardware);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
Rajiv Andrade225a9be2010-03-25 00:55:32 -03001246#define TPM_ORD_SAVESTATE cpu_to_be32(152)
1247#define SAVESTATE_RESULT_SIZE 10
1248
1249static struct tpm_input_header savestate_header = {
1250 .tag = TPM_TAG_RQU_COMMAND,
1251 .length = cpu_to_be32(10),
1252 .ordinal = TPM_ORD_SAVESTATE
1253};
1254
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255/*
1256 * We are about to suspend. Save the TPM state
1257 * so that it can be restored.
1258 */
Kylene Jo Hallce2c87d2005-10-30 15:03:25 -08001259int tpm_pm_suspend(struct device *dev, pm_message_t pm_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260{
Kylene Jo Hallce2c87d2005-10-30 15:03:25 -08001261 struct tpm_chip *chip = dev_get_drvdata(dev);
Rajiv Andrade225a9be2010-03-25 00:55:32 -03001262 struct tpm_cmd_t cmd;
1263 int rc;
1264
1265 u8 dummy_hash[TPM_DIGEST_SIZE] = { 0 };
David Smith2490c682008-01-14 00:55:12 -08001266
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 if (chip == NULL)
1268 return -ENODEV;
1269
Rajiv Andrade225a9be2010-03-25 00:55:32 -03001270 /* for buggy tpm, flush pcrs with extend to selected dummy */
1271 if (tpm_suspend_pcr) {
1272 cmd.header.in = pcrextend_header;
1273 cmd.params.pcrextend_in.pcr_idx = cpu_to_be32(tpm_suspend_pcr);
1274 memcpy(cmd.params.pcrextend_in.hash, dummy_hash,
1275 TPM_DIGEST_SIZE);
1276 rc = transmit_cmd(chip, &cmd, EXTEND_PCR_RESULT_SIZE,
1277 "extending dummy pcr before suspend");
1278 }
1279
1280 /* now do the actual savestate */
1281 cmd.header.in = savestate_header;
1282 rc = transmit_cmd(chip, &cmd, SAVESTATE_RESULT_SIZE,
1283 "sending savestate before suspend");
1284 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286EXPORT_SYMBOL_GPL(tpm_pm_suspend);
1287
1288/*
1289 * Resume from a power safe. The BIOS already restored
1290 * the TPM state.
1291 */
Kylene Jo Hallce2c87d2005-10-30 15:03:25 -08001292int tpm_pm_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293{
Kylene Jo Hallce2c87d2005-10-30 15:03:25 -08001294 struct tpm_chip *chip = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
1296 if (chip == NULL)
1297 return -ENODEV;
1298
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 return 0;
1300}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301EXPORT_SYMBOL_GPL(tpm_pm_resume);
1302
Rajiv Andrade253115b2008-10-11 09:04:39 +11001303/* In case vendor provided release function, call it too.*/
1304
1305void tpm_dev_vendor_release(struct tpm_chip *chip)
1306{
1307 if (chip->vendor.release)
1308 chip->vendor.release(chip->dev);
1309
1310 clear_bit(chip->dev_num, dev_mask);
1311 kfree(chip->vendor.miscdev.name);
1312}
1313EXPORT_SYMBOL_GPL(tpm_dev_vendor_release);
1314
1315
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316/*
Richard MUSIL5bd91f12008-02-06 01:37:02 -08001317 * Once all references to platform device are down to 0,
1318 * release all allocated structures.
Richard MUSIL5bd91f12008-02-06 01:37:02 -08001319 */
Stephen Rothwellcbb2ed42008-10-18 20:25:46 -07001320void tpm_dev_release(struct device *dev)
Richard MUSIL5bd91f12008-02-06 01:37:02 -08001321{
1322 struct tpm_chip *chip = dev_get_drvdata(dev);
1323
Rajiv Andrade253115b2008-10-11 09:04:39 +11001324 tpm_dev_vendor_release(chip);
Richard MUSIL5bd91f12008-02-06 01:37:02 -08001325
Rajiv Andrade253115b2008-10-11 09:04:39 +11001326 chip->release(dev);
Richard MUSIL5bd91f12008-02-06 01:37:02 -08001327 kfree(chip);
1328}
Rajiv Andrade253115b2008-10-11 09:04:39 +11001329EXPORT_SYMBOL_GPL(tpm_dev_release);
Richard MUSIL5bd91f12008-02-06 01:37:02 -08001330
1331/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 * Called from tpm_<specific>.c probe function only for devices
1333 * the driver has determined it should claim. Prior to calling
1334 * this function the specific probe function has called pci_enable_device
1335 * upon errant exit from this function specific probe function should call
1336 * pci_disable_device
1337 */
Rajiv Andradef02a9362008-10-11 09:04:23 +11001338struct tpm_chip *tpm_register_hardware(struct device *dev,
1339 const struct tpm_vendor_specific *entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340{
Kylene Jo Hall6f9becc2005-06-25 14:55:41 -07001341#define DEVNAME_SIZE 7
1342
1343 char *devname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 struct tpm_chip *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
1346 /* Driver specific per-device data */
Andrew Mortonb888c872005-10-30 15:03:28 -08001347 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
Parag Warudkar8e39c932007-05-08 00:31:09 -07001348 devname = kmalloc(DEVNAME_SIZE, GFP_KERNEL);
1349
Andrew Mortondd78c942008-10-15 22:04:37 -07001350 if (chip == NULL || devname == NULL)
1351 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
Matthias Kaehlcked081d472007-05-08 00:32:02 -07001353 mutex_init(&chip->buffer_mutex);
1354 mutex_init(&chip->tpm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 INIT_LIST_HEAD(&chip->list);
1356
David Howellsc4028952006-11-22 14:57:56 +00001357 INIT_WORK(&chip->work, timeout_work);
Kylene Jo Hall09e12f92005-11-13 16:07:43 -08001358
Jiri Slaby40565f12007-02-12 00:52:31 -08001359 setup_timer(&chip->user_read_timer, user_reader_timeout,
1360 (unsigned long)chip);
Kylene Hallfe3fd4832005-06-23 22:01:56 -07001361
Kylene Jo Hall90dda522006-04-22 02:37:15 -07001362 memcpy(&chip->vendor, entry, sizeof(struct tpm_vendor_specific));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
Kylene Jo Hall10685a92006-04-22 02:38:32 -07001364 chip->dev_num = find_first_zero_bit(dev_mask, TPM_NUM_DEVICES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
Kylene Jo Hall10685a92006-04-22 02:38:32 -07001366 if (chip->dev_num >= TPM_NUM_DEVICES) {
Andrew Mortonb888c872005-10-30 15:03:28 -08001367 dev_err(dev, "No available tpm device numbers\n");
Andrew Mortondd78c942008-10-15 22:04:37 -07001368 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 } else if (chip->dev_num == 0)
Kylene Jo Hall90dda522006-04-22 02:37:15 -07001370 chip->vendor.miscdev.minor = TPM_MINOR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 else
Kylene Jo Hall90dda522006-04-22 02:37:15 -07001372 chip->vendor.miscdev.minor = MISC_DYNAMIC_MINOR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
Kylene Jo Hall10685a92006-04-22 02:38:32 -07001374 set_bit(chip->dev_num, dev_mask);
1375
Kylene Jo Hall6f9becc2005-06-25 14:55:41 -07001376 scnprintf(devname, DEVNAME_SIZE, "%s%d", "tpm", chip->dev_num);
Kylene Jo Hall90dda522006-04-22 02:37:15 -07001377 chip->vendor.miscdev.name = devname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
Greg Kroah-Hartman94fbcde2006-07-27 16:16:04 -07001379 chip->vendor.miscdev.parent = dev;
Kylene Jo Halle659a3f2005-10-30 15:03:24 -08001380 chip->dev = get_device(dev);
Richard MUSIL5bd91f12008-02-06 01:37:02 -08001381 chip->release = dev->release;
1382 dev->release = tpm_dev_release;
1383 dev_set_drvdata(dev, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
Kylene Jo Hall90dda522006-04-22 02:37:15 -07001385 if (misc_register(&chip->vendor.miscdev)) {
Kylene Jo Halle659a3f2005-10-30 15:03:24 -08001386 dev_err(chip->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 "unable to misc_register %s, minor %d\n",
Kylene Jo Hall90dda522006-04-22 02:37:15 -07001388 chip->vendor.miscdev.name,
1389 chip->vendor.miscdev.minor);
Richard MUSIL5bd91f12008-02-06 01:37:02 -08001390 put_device(chip->dev);
Kylene Jo Halle0dd03c2006-04-22 02:37:26 -07001391 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 }
1393
Jeff Garzikf33d9bd2006-10-11 01:21:51 -07001394 if (sysfs_create_group(&dev->kobj, chip->vendor.attr_group)) {
Neil Horman5d469ec02006-12-06 20:37:08 -08001395 misc_deregister(&chip->vendor.miscdev);
Richard MUSIL5bd91f12008-02-06 01:37:02 -08001396 put_device(chip->dev);
Rajiv Andradef02a9362008-10-11 09:04:23 +11001397
Jeff Garzikf33d9bd2006-10-11 01:21:51 -07001398 return NULL;
1399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -08001401 chip->bios_dir = tpm_bios_log_setup(devname);
1402
Rajiv Andradef02a9362008-10-11 09:04:23 +11001403 /* Make chip available */
1404 spin_lock(&driver_lock);
1405 list_add_rcu(&chip->list, &tpm_chip_list);
1406 spin_unlock(&driver_lock);
1407
Kylene Jo Halle0dd03c2006-04-22 02:37:26 -07001408 return chip;
Andrew Mortondd78c942008-10-15 22:04:37 -07001409
1410out_free:
1411 kfree(chip);
1412 kfree(devname);
1413 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415EXPORT_SYMBOL_GPL(tpm_register_hardware);
1416
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
1418MODULE_DESCRIPTION("TPM Driver");
1419MODULE_VERSION("2.0");
1420MODULE_LICENSE("GPL");