blob: 023dfbaffe7395258139a2b33d9b883c53444c60 [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"
Kent Yodere5dcd872012-07-11 10:08:12 -050033#include "tpm_eventlog.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Kylene Jo Hall9e18ee12006-04-22 02:37:38 -070035enum 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
Peter Huewe07b133e2012-11-16 00:31:29 +010043#define TSC_MAX_ORDINAL 12
44#define TPM_PROTECTED_COMMAND 0x00
45#define TPM_CONNECTION_COMMAND 0x40
Kylene Jo Hall9e18ee12006-04-22 02:37:38 -070046
Dmitry Torokhov9b3056c2010-10-01 14:16:39 -070047/*
48 * Bug workaround - some TPM's don't flush the most
49 * recently changed pcr on suspend, so force the flush
50 * with an extend to the selected _unused_ non-volatile pcr.
51 */
52static int tpm_suspend_pcr;
53module_param_named(suspend_pcr, tpm_suspend_pcr, uint, 0644);
54MODULE_PARM_DESC(suspend_pcr,
55 "PCR to use for dummy writes to faciltate flush on suspend.");
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057static LIST_HEAD(tpm_chip_list);
58static DEFINE_SPINLOCK(driver_lock);
Kylene Jo Hall10685a92006-04-22 02:38:32 -070059static DECLARE_BITMAP(dev_mask, TPM_NUM_DEVICES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Kylene Jo Hall9e18ee12006-04-22 02:37:38 -070061/*
62 * Array with one entry per ordinal defining the maximum amount
63 * of time the chip could take to return the result. The ordinal
64 * designation of short, medium or long is defined in a table in
65 * TCG Specification TPM Main Part 2 TPM Structures Section 17. The
66 * values of the SHORT, MEDIUM, and LONG durations are retrieved
67 * from the chip during initialization with a call to tpm_get_timeouts.
68 */
Kylene Jo Hall9e18ee12006-04-22 02:37:38 -070069static const u8 tpm_ordinal_duration[TPM_MAX_ORDINAL] = {
70 TPM_UNDEFINED, /* 0 */
71 TPM_UNDEFINED,
72 TPM_UNDEFINED,
73 TPM_UNDEFINED,
74 TPM_UNDEFINED,
75 TPM_UNDEFINED, /* 5 */
76 TPM_UNDEFINED,
77 TPM_UNDEFINED,
78 TPM_UNDEFINED,
79 TPM_UNDEFINED,
80 TPM_SHORT, /* 10 */
81 TPM_SHORT,
82 TPM_MEDIUM,
83 TPM_LONG,
84 TPM_LONG,
85 TPM_MEDIUM, /* 15 */
86 TPM_SHORT,
87 TPM_SHORT,
88 TPM_MEDIUM,
89 TPM_LONG,
90 TPM_SHORT, /* 20 */
91 TPM_SHORT,
92 TPM_MEDIUM,
93 TPM_MEDIUM,
94 TPM_MEDIUM,
95 TPM_SHORT, /* 25 */
96 TPM_SHORT,
97 TPM_MEDIUM,
98 TPM_SHORT,
99 TPM_SHORT,
100 TPM_MEDIUM, /* 30 */
101 TPM_LONG,
102 TPM_MEDIUM,
103 TPM_SHORT,
104 TPM_SHORT,
105 TPM_SHORT, /* 35 */
106 TPM_MEDIUM,
107 TPM_MEDIUM,
108 TPM_UNDEFINED,
109 TPM_UNDEFINED,
110 TPM_MEDIUM, /* 40 */
111 TPM_LONG,
112 TPM_MEDIUM,
113 TPM_SHORT,
114 TPM_SHORT,
115 TPM_SHORT, /* 45 */
116 TPM_SHORT,
117 TPM_SHORT,
118 TPM_SHORT,
119 TPM_LONG,
120 TPM_MEDIUM, /* 50 */
121 TPM_MEDIUM,
122 TPM_UNDEFINED,
123 TPM_UNDEFINED,
124 TPM_UNDEFINED,
125 TPM_UNDEFINED, /* 55 */
126 TPM_UNDEFINED,
127 TPM_UNDEFINED,
128 TPM_UNDEFINED,
129 TPM_UNDEFINED,
130 TPM_MEDIUM, /* 60 */
131 TPM_MEDIUM,
132 TPM_MEDIUM,
133 TPM_SHORT,
134 TPM_SHORT,
135 TPM_MEDIUM, /* 65 */
136 TPM_UNDEFINED,
137 TPM_UNDEFINED,
138 TPM_UNDEFINED,
139 TPM_UNDEFINED,
140 TPM_SHORT, /* 70 */
141 TPM_SHORT,
142 TPM_UNDEFINED,
143 TPM_UNDEFINED,
144 TPM_UNDEFINED,
145 TPM_UNDEFINED, /* 75 */
146 TPM_UNDEFINED,
147 TPM_UNDEFINED,
148 TPM_UNDEFINED,
149 TPM_UNDEFINED,
150 TPM_LONG, /* 80 */
151 TPM_UNDEFINED,
152 TPM_MEDIUM,
153 TPM_LONG,
154 TPM_SHORT,
155 TPM_UNDEFINED, /* 85 */
156 TPM_UNDEFINED,
157 TPM_UNDEFINED,
158 TPM_UNDEFINED,
159 TPM_UNDEFINED,
160 TPM_SHORT, /* 90 */
161 TPM_SHORT,
162 TPM_SHORT,
163 TPM_SHORT,
164 TPM_SHORT,
165 TPM_UNDEFINED, /* 95 */
166 TPM_UNDEFINED,
167 TPM_UNDEFINED,
168 TPM_UNDEFINED,
169 TPM_UNDEFINED,
170 TPM_MEDIUM, /* 100 */
171 TPM_SHORT,
172 TPM_SHORT,
173 TPM_UNDEFINED,
174 TPM_UNDEFINED,
175 TPM_UNDEFINED, /* 105 */
176 TPM_UNDEFINED,
177 TPM_UNDEFINED,
178 TPM_UNDEFINED,
179 TPM_UNDEFINED,
180 TPM_SHORT, /* 110 */
181 TPM_SHORT,
182 TPM_SHORT,
183 TPM_SHORT,
184 TPM_SHORT,
185 TPM_SHORT, /* 115 */
186 TPM_SHORT,
187 TPM_SHORT,
188 TPM_UNDEFINED,
189 TPM_UNDEFINED,
190 TPM_LONG, /* 120 */
191 TPM_LONG,
192 TPM_MEDIUM,
193 TPM_UNDEFINED,
194 TPM_SHORT,
195 TPM_SHORT, /* 125 */
196 TPM_SHORT,
197 TPM_LONG,
198 TPM_SHORT,
199 TPM_SHORT,
200 TPM_SHORT, /* 130 */
201 TPM_MEDIUM,
202 TPM_UNDEFINED,
203 TPM_SHORT,
204 TPM_MEDIUM,
205 TPM_UNDEFINED, /* 135 */
206 TPM_UNDEFINED,
207 TPM_UNDEFINED,
208 TPM_UNDEFINED,
209 TPM_UNDEFINED,
210 TPM_SHORT, /* 140 */
211 TPM_SHORT,
212 TPM_UNDEFINED,
213 TPM_UNDEFINED,
214 TPM_UNDEFINED,
215 TPM_UNDEFINED, /* 145 */
216 TPM_UNDEFINED,
217 TPM_UNDEFINED,
218 TPM_UNDEFINED,
219 TPM_UNDEFINED,
220 TPM_SHORT, /* 150 */
221 TPM_MEDIUM,
222 TPM_MEDIUM,
223 TPM_SHORT,
224 TPM_SHORT,
225 TPM_UNDEFINED, /* 155 */
226 TPM_UNDEFINED,
227 TPM_UNDEFINED,
228 TPM_UNDEFINED,
229 TPM_UNDEFINED,
230 TPM_SHORT, /* 160 */
231 TPM_SHORT,
232 TPM_SHORT,
233 TPM_SHORT,
234 TPM_UNDEFINED,
235 TPM_UNDEFINED, /* 165 */
236 TPM_UNDEFINED,
237 TPM_UNDEFINED,
238 TPM_UNDEFINED,
239 TPM_UNDEFINED,
240 TPM_LONG, /* 170 */
241 TPM_UNDEFINED,
242 TPM_UNDEFINED,
243 TPM_UNDEFINED,
244 TPM_UNDEFINED,
245 TPM_UNDEFINED, /* 175 */
246 TPM_UNDEFINED,
247 TPM_UNDEFINED,
248 TPM_UNDEFINED,
249 TPM_UNDEFINED,
250 TPM_MEDIUM, /* 180 */
251 TPM_SHORT,
252 TPM_MEDIUM,
253 TPM_MEDIUM,
254 TPM_MEDIUM,
255 TPM_MEDIUM, /* 185 */
256 TPM_SHORT,
257 TPM_UNDEFINED,
258 TPM_UNDEFINED,
259 TPM_UNDEFINED,
260 TPM_UNDEFINED, /* 190 */
261 TPM_UNDEFINED,
262 TPM_UNDEFINED,
263 TPM_UNDEFINED,
264 TPM_UNDEFINED,
265 TPM_UNDEFINED, /* 195 */
266 TPM_UNDEFINED,
267 TPM_UNDEFINED,
268 TPM_UNDEFINED,
269 TPM_UNDEFINED,
270 TPM_SHORT, /* 200 */
271 TPM_UNDEFINED,
272 TPM_UNDEFINED,
273 TPM_UNDEFINED,
274 TPM_SHORT,
275 TPM_SHORT, /* 205 */
276 TPM_SHORT,
277 TPM_SHORT,
278 TPM_SHORT,
279 TPM_SHORT,
280 TPM_MEDIUM, /* 210 */
281 TPM_UNDEFINED,
282 TPM_MEDIUM,
283 TPM_MEDIUM,
284 TPM_MEDIUM,
285 TPM_UNDEFINED, /* 215 */
286 TPM_MEDIUM,
287 TPM_UNDEFINED,
288 TPM_UNDEFINED,
289 TPM_SHORT,
290 TPM_SHORT, /* 220 */
291 TPM_SHORT,
292 TPM_SHORT,
293 TPM_SHORT,
294 TPM_SHORT,
295 TPM_UNDEFINED, /* 225 */
296 TPM_UNDEFINED,
297 TPM_UNDEFINED,
298 TPM_UNDEFINED,
299 TPM_UNDEFINED,
300 TPM_SHORT, /* 230 */
301 TPM_LONG,
302 TPM_MEDIUM,
303 TPM_UNDEFINED,
304 TPM_UNDEFINED,
305 TPM_UNDEFINED, /* 235 */
306 TPM_UNDEFINED,
307 TPM_UNDEFINED,
308 TPM_UNDEFINED,
309 TPM_UNDEFINED,
310 TPM_SHORT, /* 240 */
311 TPM_UNDEFINED,
312 TPM_MEDIUM,
313};
314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315static void user_reader_timeout(unsigned long ptr)
316{
317 struct tpm_chip *chip = (struct tpm_chip *) ptr;
318
Kylene Jo Hall09e12f92005-11-13 16:07:43 -0800319 schedule_work(&chip->work);
320}
321
David Howellsc4028952006-11-22 14:57:56 +0000322static void timeout_work(struct work_struct *work)
Kylene Jo Hall09e12f92005-11-13 16:07:43 -0800323{
David Howellsc4028952006-11-22 14:57:56 +0000324 struct tpm_chip *chip = container_of(work, struct tpm_chip, work);
Kylene Jo Hall09e12f92005-11-13 16:07:43 -0800325
Matthias Kaehlcked081d472007-05-08 00:32:02 -0700326 mutex_lock(&chip->buffer_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 atomic_set(&chip->data_pending, 0);
328 memset(chip->data_buffer, 0, TPM_BUFSIZE);
Matthias Kaehlcked081d472007-05-08 00:32:02 -0700329 mutex_unlock(&chip->buffer_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330}
331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332/*
Kylene Jo Hall9e18ee12006-04-22 02:37:38 -0700333 * Returns max number of jiffies to wait
334 */
335unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip,
336 u32 ordinal)
337{
338 int duration_idx = TPM_UNDEFINED;
339 int duration = 0;
Peter Huewe07b133e2012-11-16 00:31:29 +0100340 u8 category = (ordinal >> 24) & 0xFF;
Kylene Jo Hall9e18ee12006-04-22 02:37:38 -0700341
Peter Huewe07b133e2012-11-16 00:31:29 +0100342 if ((category == TPM_PROTECTED_COMMAND && ordinal < TPM_MAX_ORDINAL) ||
343 (category == TPM_CONNECTION_COMMAND && ordinal < TSC_MAX_ORDINAL))
Kylene Jo Hall9e18ee12006-04-22 02:37:38 -0700344 duration_idx = tpm_ordinal_duration[ordinal];
Kylene Jo Hall9e18ee12006-04-22 02:37:38 -0700345
Linus Torvalds8d1dc202011-03-01 13:23:27 -0800346 if (duration_idx != TPM_UNDEFINED)
Kylene Jo Hall36b20022006-04-22 02:38:19 -0700347 duration = chip->vendor.duration[duration_idx];
Linus Torvalds8d1dc202011-03-01 13:23:27 -0800348 if (duration <= 0)
Kylene Jo Hall9e18ee12006-04-22 02:37:38 -0700349 return 2 * 60 * HZ;
Linus Torvalds8d1dc202011-03-01 13:23:27 -0800350 else
351 return duration;
Kylene Jo Hall9e18ee12006-04-22 02:37:38 -0700352}
353EXPORT_SYMBOL_GPL(tpm_calc_ordinal_duration);
354
355/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 * Internal kernel interface to transmit TPM commands
357 */
358static ssize_t tpm_transmit(struct tpm_chip *chip, const char *buf,
359 size_t bufsiz)
360{
Kylene Halld9e5b6b2005-06-23 22:02:02 -0700361 ssize_t rc;
Kylene Jo Hall9e18ee12006-04-22 02:37:38 -0700362 u32 count, ordinal;
Nishanth Aravamudan700d8bd2005-06-23 22:01:47 -0700363 unsigned long stop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Peter Huewe6b07d302011-09-15 14:37:43 -0300365 if (bufsiz > TPM_BUFSIZE)
366 bufsiz = TPM_BUFSIZE;
367
Kylene Hall81179bb2005-06-23 22:01:59 -0700368 count = be32_to_cpu(*((__be32 *) (buf + 2)));
Kylene Jo Hall9e18ee12006-04-22 02:37:38 -0700369 ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 if (count == 0)
371 return -ENODATA;
372 if (count > bufsiz) {
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800373 dev_err(chip->dev,
Al Virob76be682005-04-26 07:43:41 -0700374 "invalid count value %x %zx \n", count, bufsiz);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 return -E2BIG;
376 }
377
Matthias Kaehlcked081d472007-05-08 00:32:02 -0700378 mutex_lock(&chip->tpm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Kylene Jo Hall90dda522006-04-22 02:37:15 -0700380 if ((rc = chip->vendor.send(chip, (u8 *) buf, count)) < 0) {
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800381 dev_err(chip->dev,
Kylene Halld9e5b6b2005-06-23 22:02:02 -0700382 "tpm_transmit: tpm_send: error %zd\n", rc);
383 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 }
385
Leendert van Doorn27084ef2006-04-22 02:38:03 -0700386 if (chip->vendor.irq)
387 goto out_recv;
388
Kylene Jo Hall9e18ee12006-04-22 02:37:38 -0700389 stop = jiffies + tpm_calc_ordinal_duration(chip, ordinal);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 do {
Kylene Jo Hall90dda522006-04-22 02:37:15 -0700391 u8 status = chip->vendor.status(chip);
392 if ((status & chip->vendor.req_complete_mask) ==
393 chip->vendor.req_complete_val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 goto out_recv;
Kylene Halld9e5b6b2005-06-23 22:02:02 -0700395
Kylene Jo Hall90dda522006-04-22 02:37:15 -0700396 if ((status == chip->vendor.req_canceled)) {
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800397 dev_err(chip->dev, "Operation Canceled\n");
Kylene Halld9e5b6b2005-06-23 22:02:02 -0700398 rc = -ECANCELED;
399 goto out;
400 }
401
402 msleep(TPM_TIMEOUT); /* CHECK */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 rmb();
Nishanth Aravamudan700d8bd2005-06-23 22:01:47 -0700404 } while (time_before(jiffies, stop));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Kylene Jo Hall90dda522006-04-22 02:37:15 -0700406 chip->vendor.cancel(chip);
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800407 dev_err(chip->dev, "Operation Timed out\n");
Kylene Halld9e5b6b2005-06-23 22:02:02 -0700408 rc = -ETIME;
409 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
411out_recv:
Kylene Jo Hall90dda522006-04-22 02:37:15 -0700412 rc = chip->vendor.recv(chip, (u8 *) buf, bufsiz);
Kylene Halld9e5b6b2005-06-23 22:02:02 -0700413 if (rc < 0)
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800414 dev_err(chip->dev,
Kylene Halld9e5b6b2005-06-23 22:02:02 -0700415 "tpm_transmit: tpm_recv: error %zd\n", rc);
416out:
Matthias Kaehlcked081d472007-05-08 00:32:02 -0700417 mutex_unlock(&chip->tpm_mutex);
Kylene Halld9e5b6b2005-06-23 22:02:02 -0700418 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419}
420
421#define TPM_DIGEST_SIZE 20
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700422#define TPM_RET_CODE_IDX 6
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700423
424enum tpm_capabilities {
Rajiv Andrade08837432009-02-02 15:23:43 -0200425 TPM_CAP_FLAG = cpu_to_be32(4),
426 TPM_CAP_PROP = cpu_to_be32(5),
427 CAP_VERSION_1_1 = cpu_to_be32(0x06),
428 CAP_VERSION_1_2 = cpu_to_be32(0x1A)
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700429};
430
431enum tpm_sub_capabilities {
Rajiv Andrade08837432009-02-02 15:23:43 -0200432 TPM_CAP_PROP_PCR = cpu_to_be32(0x101),
433 TPM_CAP_PROP_MANUFACTURER = cpu_to_be32(0x103),
434 TPM_CAP_FLAG_PERM = cpu_to_be32(0x108),
435 TPM_CAP_FLAG_VOL = cpu_to_be32(0x109),
436 TPM_CAP_PROP_OWNER = cpu_to_be32(0x111),
437 TPM_CAP_PROP_TIS_TIMEOUT = cpu_to_be32(0x115),
438 TPM_CAP_PROP_TIS_DURATION = cpu_to_be32(0x120),
439
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700440};
441
Rajiv Andrade08837432009-02-02 15:23:43 -0200442static ssize_t transmit_cmd(struct tpm_chip *chip, struct tpm_cmd_t *cmd,
443 int len, const char *desc)
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700444{
445 int err;
446
Rajiv Andrade08837432009-02-02 15:23:43 -0200447 len = tpm_transmit(chip,(u8 *) cmd, len);
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700448 if (len < 0)
449 return len;
Rajiv Andradeb9e32382011-11-01 17:00:52 -0200450 else if (len < TPM_HEADER_SIZE)
451 return -EFAULT;
452
453 err = be32_to_cpu(cmd->header.out.return_code);
Jason Gunthorpec584af12012-11-21 13:54:33 -0700454 if (err != 0 && desc)
Rajiv Andradeb9e32382011-11-01 17:00:52 -0200455 dev_err(chip->dev, "A TPM error (%d) occurred %s\n", err, desc);
456
457 return err;
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700458}
459
Rajiv Andrade08837432009-02-02 15:23:43 -0200460#define TPM_INTERNAL_RESULT_SIZE 200
461#define TPM_TAG_RQU_COMMAND cpu_to_be16(193)
462#define TPM_ORD_GET_CAP cpu_to_be32(101)
Kent Yoder41ab9992012-06-07 13:47:14 -0500463#define TPM_ORD_GET_RANDOM cpu_to_be32(70)
Rajiv Andrade08837432009-02-02 15:23:43 -0200464
465static const struct tpm_input_header tpm_getcap_header = {
466 .tag = TPM_TAG_RQU_COMMAND,
467 .length = cpu_to_be32(22),
468 .ordinal = TPM_ORD_GET_CAP
469};
470
471ssize_t tpm_getcap(struct device *dev, __be32 subcap_id, cap_t *cap,
472 const char *desc)
473{
474 struct tpm_cmd_t tpm_cmd;
475 int rc;
476 struct tpm_chip *chip = dev_get_drvdata(dev);
477
478 tpm_cmd.header.in = tpm_getcap_header;
479 if (subcap_id == CAP_VERSION_1_1 || subcap_id == CAP_VERSION_1_2) {
480 tpm_cmd.params.getcap_in.cap = subcap_id;
481 /*subcap field not necessary */
482 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(0);
483 tpm_cmd.header.in.length -= cpu_to_be32(sizeof(__be32));
484 } else {
485 if (subcap_id == TPM_CAP_FLAG_PERM ||
486 subcap_id == TPM_CAP_FLAG_VOL)
487 tpm_cmd.params.getcap_in.cap = TPM_CAP_FLAG;
488 else
489 tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
490 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
491 tpm_cmd.params.getcap_in.subcap = subcap_id;
492 }
493 rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE, desc);
494 if (!rc)
495 *cap = tpm_cmd.params.getcap_out.cap;
496 return rc;
497}
498
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700499void tpm_gen_interrupt(struct tpm_chip *chip)
500{
Rajiv Andrade08837432009-02-02 15:23:43 -0200501 struct tpm_cmd_t tpm_cmd;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700502 ssize_t rc;
503
Rajiv Andrade08837432009-02-02 15:23:43 -0200504 tpm_cmd.header.in = tpm_getcap_header;
505 tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
506 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
507 tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_TIMEOUT;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700508
Rajiv Andrade08837432009-02-02 15:23:43 -0200509 rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE,
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700510 "attempting to determine the timeouts");
511}
512EXPORT_SYMBOL_GPL(tpm_gen_interrupt);
513
Jason Gunthorpec584af12012-11-21 13:54:33 -0700514#define TPM_ORD_STARTUP cpu_to_be32(153)
515#define TPM_ST_CLEAR cpu_to_be16(1)
516#define TPM_ST_STATE cpu_to_be16(2)
517#define TPM_ST_DEACTIVATED cpu_to_be16(3)
518static const struct tpm_input_header tpm_startup_header = {
519 .tag = TPM_TAG_RQU_COMMAND,
520 .length = cpu_to_be32(12),
521 .ordinal = TPM_ORD_STARTUP
522};
523
524static int tpm_startup(struct tpm_chip *chip, __be16 startup_type)
525{
526 struct tpm_cmd_t start_cmd;
527 start_cmd.header.in = tpm_startup_header;
528 start_cmd.params.startup_in.startup_type = startup_type;
529 return transmit_cmd(chip, &start_cmd, TPM_INTERNAL_RESULT_SIZE,
530 "attempting to start the TPM");
531}
532
Stefan Berger2b30a902011-11-11 12:57:02 -0500533int tpm_get_timeouts(struct tpm_chip *chip)
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700534{
Rajiv Andrade08837432009-02-02 15:23:43 -0200535 struct tpm_cmd_t tpm_cmd;
536 struct timeout_t *timeout_cap;
537 struct duration_t *duration_cap;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700538 ssize_t rc;
539 u32 timeout;
Stefan Bergere3e1a1e2011-03-30 12:13:27 -0400540 unsigned int scale = 1;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700541
Rajiv Andrade08837432009-02-02 15:23:43 -0200542 tpm_cmd.header.in = tpm_getcap_header;
543 tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
544 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
545 tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_TIMEOUT;
Jason Gunthorpec584af12012-11-21 13:54:33 -0700546 rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE, NULL);
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700547
Jason Gunthorpec584af12012-11-21 13:54:33 -0700548 if (rc == TPM_ERR_INVALID_POSTINIT) {
549 /* The TPM is not started, we are the first to talk to it.
550 Execute a startup command. */
551 dev_info(chip->dev, "Issuing TPM_STARTUP");
552 if (tpm_startup(chip, TPM_ST_CLEAR))
553 return rc;
554
555 tpm_cmd.header.in = tpm_getcap_header;
556 tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
557 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
558 tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_TIMEOUT;
559 rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE,
560 NULL);
561 }
562 if (rc) {
563 dev_err(chip->dev,
564 "A TPM error (%zd) occurred attempting to determine the timeouts\n",
565 rc);
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700566 goto duration;
Jason Gunthorpec584af12012-11-21 13:54:33 -0700567 }
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700568
Stefan Berger829bf062011-03-30 12:13:26 -0400569 if (be32_to_cpu(tpm_cmd.header.out.return_code) != 0 ||
570 be32_to_cpu(tpm_cmd.header.out.length)
571 != sizeof(tpm_cmd.header.out) + sizeof(u32) + 4 * sizeof(u32))
Stefan Berger2b30a902011-11-11 12:57:02 -0500572 return -EINVAL;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700573
Rajiv Andrade08837432009-02-02 15:23:43 -0200574 timeout_cap = &tpm_cmd.params.getcap_out.cap.timeout;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700575 /* Don't overwrite default if value is 0 */
Rajiv Andrade08837432009-02-02 15:23:43 -0200576 timeout = be32_to_cpu(timeout_cap->a);
Stefan Bergere3e1a1e2011-03-30 12:13:27 -0400577 if (timeout && timeout < 1000) {
578 /* timeouts in msec rather usec */
579 scale = 1000;
Stefan Berger62592102011-03-30 12:13:28 -0400580 chip->vendor.timeout_adjusted = true;
Stefan Bergere3e1a1e2011-03-30 12:13:27 -0400581 }
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700582 if (timeout)
Stefan Bergere3e1a1e2011-03-30 12:13:27 -0400583 chip->vendor.timeout_a = usecs_to_jiffies(timeout * scale);
Rajiv Andrade08837432009-02-02 15:23:43 -0200584 timeout = be32_to_cpu(timeout_cap->b);
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700585 if (timeout)
Stefan Bergere3e1a1e2011-03-30 12:13:27 -0400586 chip->vendor.timeout_b = usecs_to_jiffies(timeout * scale);
Rajiv Andrade08837432009-02-02 15:23:43 -0200587 timeout = be32_to_cpu(timeout_cap->c);
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700588 if (timeout)
Stefan Bergere3e1a1e2011-03-30 12:13:27 -0400589 chip->vendor.timeout_c = usecs_to_jiffies(timeout * scale);
Rajiv Andrade08837432009-02-02 15:23:43 -0200590 timeout = be32_to_cpu(timeout_cap->d);
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700591 if (timeout)
Stefan Bergere3e1a1e2011-03-30 12:13:27 -0400592 chip->vendor.timeout_d = usecs_to_jiffies(timeout * scale);
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700593
594duration:
Rajiv Andrade08837432009-02-02 15:23:43 -0200595 tpm_cmd.header.in = tpm_getcap_header;
596 tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
597 tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
598 tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_DURATION;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700599
Rajiv Andrade08837432009-02-02 15:23:43 -0200600 rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE,
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700601 "attempting to determine the durations");
602 if (rc)
Stefan Berger2b30a902011-11-11 12:57:02 -0500603 return rc;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700604
Stefan Berger979b1402011-03-30 12:13:23 -0400605 if (be32_to_cpu(tpm_cmd.header.out.return_code) != 0 ||
606 be32_to_cpu(tpm_cmd.header.out.length)
607 != sizeof(tpm_cmd.header.out) + sizeof(u32) + 3 * sizeof(u32))
Stefan Berger2b30a902011-11-11 12:57:02 -0500608 return -EINVAL;
Stefan Berger979b1402011-03-30 12:13:23 -0400609
Rajiv Andrade08837432009-02-02 15:23:43 -0200610 duration_cap = &tpm_cmd.params.getcap_out.cap.duration;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700611 chip->vendor.duration[TPM_SHORT] =
Rajiv Andrade08837432009-02-02 15:23:43 -0200612 usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_short));
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700613 chip->vendor.duration[TPM_MEDIUM] =
Rajiv Andrade08837432009-02-02 15:23:43 -0200614 usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_medium));
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700615 chip->vendor.duration[TPM_LONG] =
Rajiv Andrade08837432009-02-02 15:23:43 -0200616 usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_long));
Stefan Bergere934acc2011-03-30 12:13:24 -0400617
618 /* The Broadcom BCM0102 chipset in a Dell Latitude D820 gets the above
619 * value wrong and apparently reports msecs rather than usecs. So we
620 * fix up the resulting too-small TPM_SHORT value to make things work.
621 * We also scale the TPM_MEDIUM and -_LONG values by 1000.
622 */
623 if (chip->vendor.duration[TPM_SHORT] < (HZ / 100)) {
624 chip->vendor.duration[TPM_SHORT] = HZ;
625 chip->vendor.duration[TPM_MEDIUM] *= 1000;
626 chip->vendor.duration[TPM_LONG] *= 1000;
Stefan Berger04ab2292011-03-30 12:13:25 -0400627 chip->vendor.duration_adjusted = true;
Stefan Bergere934acc2011-03-30 12:13:24 -0400628 dev_info(chip->dev, "Adjusting TPM timeout parameters.");
629 }
Stefan Berger2b30a902011-11-11 12:57:02 -0500630 return 0;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700631}
632EXPORT_SYMBOL_GPL(tpm_get_timeouts);
633
Stefan Bergerd97c6ad2011-11-11 12:57:03 -0500634#define TPM_ORD_CONTINUE_SELFTEST 83
635#define CONTINUE_SELFTEST_RESULT_SIZE 10
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700636
Stefan Bergerd97c6ad2011-11-11 12:57:03 -0500637static struct tpm_input_header continue_selftest_header = {
638 .tag = TPM_TAG_RQU_COMMAND,
639 .length = cpu_to_be32(10),
640 .ordinal = cpu_to_be32(TPM_ORD_CONTINUE_SELFTEST),
641};
642
643/**
644 * tpm_continue_selftest -- run TPM's selftest
645 * @chip: TPM chip to use
646 *
647 * Returns 0 on success, < 0 in case of fatal error or a value > 0 representing
648 * a TPM error code.
649 */
Stefan Berger68d6e672011-11-11 12:57:04 -0500650static int tpm_continue_selftest(struct tpm_chip *chip)
Stefan Bergerd97c6ad2011-11-11 12:57:03 -0500651{
652 int rc;
653 struct tpm_cmd_t cmd;
654
655 cmd.header.in = continue_selftest_header;
656 rc = transmit_cmd(chip, &cmd, CONTINUE_SELFTEST_RESULT_SIZE,
657 "continue selftest");
658 return rc;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700659}
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700660
661ssize_t tpm_show_enabled(struct device * dev, struct device_attribute * attr,
662 char *buf)
663{
Rajiv Andrade08837432009-02-02 15:23:43 -0200664 cap_t cap;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700665 ssize_t rc;
666
Rajiv Andrade08837432009-02-02 15:23:43 -0200667 rc = tpm_getcap(dev, TPM_CAP_FLAG_PERM, &cap,
668 "attempting to determine the permanent enabled state");
669 if (rc)
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700670 return 0;
Marcin Obaraec288bd2008-07-25 19:44:59 -0700671
Rajiv Andrade08837432009-02-02 15:23:43 -0200672 rc = sprintf(buf, "%d\n", !cap.perm_flags.disable);
Marcin Obaraec288bd2008-07-25 19:44:59 -0700673 return rc;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700674}
675EXPORT_SYMBOL_GPL(tpm_show_enabled);
676
677ssize_t tpm_show_active(struct device * dev, struct device_attribute * attr,
678 char *buf)
679{
Rajiv Andrade08837432009-02-02 15:23:43 -0200680 cap_t cap;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700681 ssize_t rc;
682
Rajiv Andrade08837432009-02-02 15:23:43 -0200683 rc = tpm_getcap(dev, TPM_CAP_FLAG_PERM, &cap,
684 "attempting to determine the permanent active state");
685 if (rc)
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700686 return 0;
Marcin Obaraec288bd2008-07-25 19:44:59 -0700687
Rajiv Andrade08837432009-02-02 15:23:43 -0200688 rc = sprintf(buf, "%d\n", !cap.perm_flags.deactivated);
Marcin Obaraec288bd2008-07-25 19:44:59 -0700689 return rc;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700690}
691EXPORT_SYMBOL_GPL(tpm_show_active);
692
693ssize_t tpm_show_owned(struct device * dev, struct device_attribute * attr,
694 char *buf)
695{
Rajiv Andrade08837432009-02-02 15:23:43 -0200696 cap_t cap;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700697 ssize_t rc;
698
Rajiv Andrade08837432009-02-02 15:23:43 -0200699 rc = tpm_getcap(dev, TPM_CAP_PROP_OWNER, &cap,
700 "attempting to determine the owner state");
701 if (rc)
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700702 return 0;
Marcin Obaraec288bd2008-07-25 19:44:59 -0700703
Rajiv Andrade08837432009-02-02 15:23:43 -0200704 rc = sprintf(buf, "%d\n", cap.owned);
Marcin Obaraec288bd2008-07-25 19:44:59 -0700705 return rc;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700706}
707EXPORT_SYMBOL_GPL(tpm_show_owned);
708
709ssize_t tpm_show_temp_deactivated(struct device * dev,
710 struct device_attribute * attr, char *buf)
711{
Rajiv Andrade08837432009-02-02 15:23:43 -0200712 cap_t cap;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700713 ssize_t rc;
714
Rajiv Andrade08837432009-02-02 15:23:43 -0200715 rc = tpm_getcap(dev, TPM_CAP_FLAG_VOL, &cap,
716 "attempting to determine the temporary state");
717 if (rc)
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700718 return 0;
Marcin Obaraec288bd2008-07-25 19:44:59 -0700719
Rajiv Andrade08837432009-02-02 15:23:43 -0200720 rc = sprintf(buf, "%d\n", cap.stclear_flags.deactivated);
Marcin Obaraec288bd2008-07-25 19:44:59 -0700721 return rc;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -0700722}
723EXPORT_SYMBOL_GPL(tpm_show_temp_deactivated);
724
Rajiv Andrade659aaf22009-02-02 15:23:44 -0200725/*
726 * tpm_chip_find_get - return tpm_chip for given chip number
727 */
728static struct tpm_chip *tpm_chip_find_get(int chip_num)
729{
Rajiv Andrade8920d5a2009-02-05 13:06:30 -0200730 struct tpm_chip *pos, *chip = NULL;
Rajiv Andrade659aaf22009-02-02 15:23:44 -0200731
732 rcu_read_lock();
733 list_for_each_entry_rcu(pos, &tpm_chip_list, list) {
734 if (chip_num != TPM_ANY_NUM && chip_num != pos->dev_num)
735 continue;
736
Rajiv Andrade8920d5a2009-02-05 13:06:30 -0200737 if (try_module_get(pos->dev->driver->owner)) {
738 chip = pos;
Rajiv Andrade659aaf22009-02-02 15:23:44 -0200739 break;
Rajiv Andrade8920d5a2009-02-05 13:06:30 -0200740 }
Rajiv Andrade659aaf22009-02-02 15:23:44 -0200741 }
742 rcu_read_unlock();
Rajiv Andrade8920d5a2009-02-05 13:06:30 -0200743 return chip;
Rajiv Andrade659aaf22009-02-02 15:23:44 -0200744}
745
746#define TPM_ORDINAL_PCRREAD cpu_to_be32(21)
747#define READ_PCR_RESULT_SIZE 30
748static struct tpm_input_header pcrread_header = {
749 .tag = TPM_TAG_RQU_COMMAND,
750 .length = cpu_to_be32(14),
751 .ordinal = TPM_ORDINAL_PCRREAD
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752};
753
Stefan Berger68d6e672011-11-11 12:57:04 -0500754static int __tpm_pcr_read(struct tpm_chip *chip, int pcr_idx, u8 *res_buf)
Rajiv Andrade659aaf22009-02-02 15:23:44 -0200755{
756 int rc;
757 struct tpm_cmd_t cmd;
758
759 cmd.header.in = pcrread_header;
760 cmd.params.pcrread_in.pcr_idx = cpu_to_be32(pcr_idx);
Rajiv Andrade23acb982009-09-30 12:26:55 -0300761 rc = transmit_cmd(chip, &cmd, READ_PCR_RESULT_SIZE,
Rajiv Andrade659aaf22009-02-02 15:23:44 -0200762 "attempting to read a pcr value");
763
764 if (rc == 0)
765 memcpy(res_buf, cmd.params.pcrread_out.pcr_result,
766 TPM_DIGEST_SIZE);
767 return rc;
768}
769
770/**
771 * tpm_pcr_read - read a pcr value
772 * @chip_num: tpm idx # or ANY
773 * @pcr_idx: pcr idx to retrieve
774 * @res_buf: TPM_PCR value
775 * size of res_buf is 20 bytes (or NULL if you don't care)
776 *
777 * The TPM driver should be built-in, but for whatever reason it
778 * isn't, protect against the chip disappearing, by incrementing
779 * the module usage count.
780 */
781int tpm_pcr_read(u32 chip_num, int pcr_idx, u8 *res_buf)
782{
783 struct tpm_chip *chip;
784 int rc;
785
786 chip = tpm_chip_find_get(chip_num);
787 if (chip == NULL)
788 return -ENODEV;
789 rc = __tpm_pcr_read(chip, pcr_idx, res_buf);
Mimi Zohara0e39342010-11-23 17:50:32 -0500790 tpm_chip_put(chip);
Rajiv Andrade659aaf22009-02-02 15:23:44 -0200791 return rc;
792}
793EXPORT_SYMBOL_GPL(tpm_pcr_read);
794
795/**
796 * tpm_pcr_extend - extend pcr value with hash
797 * @chip_num: tpm idx # or AN&
798 * @pcr_idx: pcr idx to extend
799 * @hash: hash value used to extend pcr value
800 *
801 * The TPM driver should be built-in, but for whatever reason it
802 * isn't, protect against the chip disappearing, by incrementing
803 * the module usage count.
804 */
805#define TPM_ORD_PCR_EXTEND cpu_to_be32(20)
Jason Gunthorpe0afd9052009-09-18 12:54:24 -0700806#define EXTEND_PCR_RESULT_SIZE 34
Rajiv Andrade659aaf22009-02-02 15:23:44 -0200807static struct tpm_input_header pcrextend_header = {
808 .tag = TPM_TAG_RQU_COMMAND,
809 .length = cpu_to_be32(34),
810 .ordinal = TPM_ORD_PCR_EXTEND
811};
812
813int tpm_pcr_extend(u32 chip_num, int pcr_idx, const u8 *hash)
814{
815 struct tpm_cmd_t cmd;
816 int rc;
817 struct tpm_chip *chip;
818
819 chip = tpm_chip_find_get(chip_num);
820 if (chip == NULL)
821 return -ENODEV;
822
823 cmd.header.in = pcrextend_header;
Rajiv Andrade659aaf22009-02-02 15:23:44 -0200824 cmd.params.pcrextend_in.pcr_idx = cpu_to_be32(pcr_idx);
825 memcpy(cmd.params.pcrextend_in.hash, hash, TPM_DIGEST_SIZE);
Jason Gunthorpe0afd9052009-09-18 12:54:24 -0700826 rc = transmit_cmd(chip, &cmd, EXTEND_PCR_RESULT_SIZE,
Rajiv Andrade659aaf22009-02-02 15:23:44 -0200827 "attempting extend a PCR value");
828
Mimi Zohara0e39342010-11-23 17:50:32 -0500829 tpm_chip_put(chip);
Rajiv Andrade659aaf22009-02-02 15:23:44 -0200830 return rc;
831}
832EXPORT_SYMBOL_GPL(tpm_pcr_extend);
833
Stefan Berger68d6e672011-11-11 12:57:04 -0500834/**
835 * tpm_do_selftest - have the TPM continue its selftest and wait until it
836 * can receive further commands
837 * @chip: TPM chip to use
838 *
839 * Returns 0 on success, < 0 in case of fatal error or a value > 0 representing
840 * a TPM error code.
841 */
842int tpm_do_selftest(struct tpm_chip *chip)
843{
844 int rc;
Stefan Berger68d6e672011-11-11 12:57:04 -0500845 unsigned int loops;
846 unsigned int delay_msec = 1000;
847 unsigned long duration;
Rajiv Andrade24ebe662012-04-24 17:38:17 -0300848 struct tpm_cmd_t cmd;
Stefan Berger68d6e672011-11-11 12:57:04 -0500849
850 duration = tpm_calc_ordinal_duration(chip,
851 TPM_ORD_CONTINUE_SELFTEST);
852
853 loops = jiffies_to_msecs(duration) / delay_msec;
854
855 rc = tpm_continue_selftest(chip);
856 /* This may fail if there was no TPM driver during a suspend/resume
857 * cycle; some may return 10 (BAD_ORDINAL), others 28 (FAILEDSELFTEST)
858 */
859 if (rc)
860 return rc;
861
862 do {
Rajiv Andrade24ebe662012-04-24 17:38:17 -0300863 /* Attempt to read a PCR value */
864 cmd.header.in = pcrread_header;
865 cmd.params.pcrread_in.pcr_idx = cpu_to_be32(0);
866 rc = tpm_transmit(chip, (u8 *) &cmd, READ_PCR_RESULT_SIZE);
867
868 if (rc < TPM_HEADER_SIZE)
869 return -EFAULT;
870
871 rc = be32_to_cpu(cmd.header.out.return_code);
Stefan Bergerbe405412012-01-17 22:07:30 -0500872 if (rc == TPM_ERR_DISABLED || rc == TPM_ERR_DEACTIVATED) {
873 dev_info(chip->dev,
874 "TPM is disabled/deactivated (0x%X)\n", rc);
875 /* TPM is disabled and/or deactivated; driver can
876 * proceed and TPM does handle commands for
877 * suspend/resume correctly
878 */
879 return 0;
880 }
Stefan Berger68d6e672011-11-11 12:57:04 -0500881 if (rc != TPM_WARN_DOING_SELFTEST)
882 return rc;
883 msleep(delay_msec);
884 } while (--loops > 0);
885
886 return rc;
887}
888EXPORT_SYMBOL_GPL(tpm_do_selftest);
889
Mimi Zoharc749ba92010-11-23 18:54:16 -0500890int tpm_send(u32 chip_num, void *cmd, size_t buflen)
891{
892 struct tpm_chip *chip;
893 int rc;
894
895 chip = tpm_chip_find_get(chip_num);
896 if (chip == NULL)
897 return -ENODEV;
898
899 rc = transmit_cmd(chip, cmd, buflen, "attempting tpm_cmd");
900
901 tpm_chip_put(chip);
902 return rc;
903}
904EXPORT_SYMBOL_GPL(tpm_send);
905
Kylene Hall6659ca22005-06-23 22:02:00 -0700906ssize_t tpm_show_pcrs(struct device *dev, struct device_attribute *attr,
907 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908{
Rajiv Andrade08837432009-02-02 15:23:43 -0200909 cap_t cap;
Rajiv Andrade659aaf22009-02-02 15:23:44 -0200910 u8 digest[TPM_DIGEST_SIZE];
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700911 ssize_t rc;
Kylene Hall81179bb2005-06-23 22:01:59 -0700912 int i, j, num_pcrs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 char *str = buf;
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800914 struct tpm_chip *chip = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
Rajiv Andrade08837432009-02-02 15:23:43 -0200916 rc = tpm_getcap(dev, TPM_CAP_PROP_PCR, &cap,
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700917 "attempting to determine the number of PCRS");
Rajiv Andrade08837432009-02-02 15:23:43 -0200918 if (rc)
Kylene Halle234bc92005-06-23 22:02:08 -0700919 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
Rajiv Andrade08837432009-02-02 15:23:43 -0200921 num_pcrs = be32_to_cpu(cap.num_pcrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 for (i = 0; i < num_pcrs; i++) {
Rajiv Andrade659aaf22009-02-02 15:23:44 -0200923 rc = __tpm_pcr_read(chip, i, digest);
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700924 if (rc)
Rajiv Andrade08837432009-02-02 15:23:43 -0200925 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 str += sprintf(str, "PCR-%02d: ", i);
927 for (j = 0; j < TPM_DIGEST_SIZE; j++)
Rajiv Andrade659aaf22009-02-02 15:23:44 -0200928 str += sprintf(str, "%02X ", digest[j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 str += sprintf(str, "\n");
930 }
931 return str - buf;
932}
Kylene Hall6659ca22005-06-23 22:02:00 -0700933EXPORT_SYMBOL_GPL(tpm_show_pcrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
935#define READ_PUBEK_RESULT_SIZE 314
Rajiv Andrade08837432009-02-02 15:23:43 -0200936#define TPM_ORD_READPUBEK cpu_to_be32(124)
Xiaoyan Zhang81198072013-08-29 20:39:11 +0800937static struct tpm_input_header tpm_readpubek_header = {
Rajiv Andrade08837432009-02-02 15:23:43 -0200938 .tag = TPM_TAG_RQU_COMMAND,
939 .length = cpu_to_be32(30),
940 .ordinal = TPM_ORD_READPUBEK
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941};
942
Kylene Hall6659ca22005-06-23 22:02:00 -0700943ssize_t tpm_show_pubek(struct device *dev, struct device_attribute *attr,
944 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945{
Kylene Hall2df71112005-06-23 22:01:54 -0700946 u8 *data;
Rajiv Andrade08837432009-02-02 15:23:43 -0200947 struct tpm_cmd_t tpm_cmd;
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700948 ssize_t err;
Kylene Hall81179bb2005-06-23 22:01:59 -0700949 int i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 char *str = buf;
951
Kylene Jo Halle659a3f2005-10-30 15:03:24 -0800952 struct tpm_chip *chip = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
Rajiv Andrade08837432009-02-02 15:23:43 -0200954 tpm_cmd.header.in = tpm_readpubek_header;
955 err = transmit_cmd(chip, &tpm_cmd, READ_PUBEK_RESULT_SIZE,
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700956 "attempting to read the PUBEK");
957 if (err)
Kylene Jo Hall34d6e072005-06-23 22:02:10 -0700958 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
960 /*
961 ignore header 10 bytes
962 algorithm 32 bits (1 == RSA )
963 encscheme 16 bits
964 sigscheme 16 bits
965 parameters (RSA 12->bytes: keybit, #primes, expbit)
966 keylenbytes 32 bits
967 256 byte modulus
968 ignore checksum 20 bytes
969 */
Rajiv Andrade08837432009-02-02 15:23:43 -0200970 data = tpm_cmd.params.readpubek_out_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 str +=
972 sprintf(str,
Stefan Berger5a794442011-03-30 12:13:29 -0400973 "Algorithm: %02X %02X %02X %02X\n"
974 "Encscheme: %02X %02X\n"
975 "Sigscheme: %02X %02X\n"
976 "Parameters: %02X %02X %02X %02X "
977 "%02X %02X %02X %02X "
978 "%02X %02X %02X %02X\n"
979 "Modulus length: %d\n"
980 "Modulus:\n",
981 data[0], data[1], data[2], data[3],
982 data[4], data[5],
983 data[6], data[7],
984 data[12], data[13], data[14], data[15],
985 data[16], data[17], data[18], data[19],
986 data[20], data[21], data[22], data[23],
987 be32_to_cpu(*((__be32 *) (data + 24))));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
989 for (i = 0; i < 256; i++) {
Stefan Berger5a794442011-03-30 12:13:29 -0400990 str += sprintf(str, "%02X ", data[i + 28]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 if ((i + 1) % 16 == 0)
992 str += sprintf(str, "\n");
993 }
Kylene Jo Hall34d6e072005-06-23 22:02:10 -0700994out:
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -0700995 rc = str - buf;
Kylene Hall2df71112005-06-23 22:01:54 -0700996 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997}
Kylene Hall6659ca22005-06-23 22:02:00 -0700998EXPORT_SYMBOL_GPL(tpm_show_pubek);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
Kylene Hall6659ca22005-06-23 22:02:00 -07001001ssize_t tpm_show_caps(struct device *dev, struct device_attribute *attr,
1002 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003{
Rajiv Andrade08837432009-02-02 15:23:43 -02001004 cap_t cap;
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -07001005 ssize_t rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 char *str = buf;
1007
Rajiv Andrade08837432009-02-02 15:23:43 -02001008 rc = tpm_getcap(dev, TPM_CAP_PROP_MANUFACTURER, &cap,
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -07001009 "attempting to determine the manufacturer");
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -07001010 if (rc)
Rajiv Andrade08837432009-02-02 15:23:43 -02001011 return 0;
1012 str += sprintf(str, "Manufacturer: 0x%x\n",
1013 be32_to_cpu(cap.manufacturer_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
Rajiv Andrade08837432009-02-02 15:23:43 -02001015 rc = tpm_getcap(dev, CAP_VERSION_1_1, &cap,
1016 "attempting to determine the 1.1 version");
1017 if (rc)
1018 return 0;
Kylene Jo Hallbeed53a2006-04-22 02:37:05 -07001019 str += sprintf(str,
1020 "TCG version: %d.%d\nFirmware version: %d.%d\n",
Rajiv Andrade08837432009-02-02 15:23:43 -02001021 cap.tpm_version.Major, cap.tpm_version.Minor,
1022 cap.tpm_version.revMajor, cap.tpm_version.revMinor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 return str - buf;
1024}
Kylene Hall6659ca22005-06-23 22:02:00 -07001025EXPORT_SYMBOL_GPL(tpm_show_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026
Kylene Jo Hall08e96e42006-04-22 02:37:50 -07001027ssize_t tpm_show_caps_1_2(struct device * dev,
1028 struct device_attribute * attr, char *buf)
1029{
Rajiv Andrade08837432009-02-02 15:23:43 -02001030 cap_t cap;
1031 ssize_t rc;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -07001032 char *str = buf;
1033
Rajiv Andrade08837432009-02-02 15:23:43 -02001034 rc = tpm_getcap(dev, TPM_CAP_PROP_MANUFACTURER, &cap,
1035 "attempting to determine the manufacturer");
1036 if (rc)
Kylene Jo Hall08e96e42006-04-22 02:37:50 -07001037 return 0;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -07001038 str += sprintf(str, "Manufacturer: 0x%x\n",
Rajiv Andrade08837432009-02-02 15:23:43 -02001039 be32_to_cpu(cap.manufacturer_id));
1040 rc = tpm_getcap(dev, CAP_VERSION_1_2, &cap,
1041 "attempting to determine the 1.2 version");
1042 if (rc)
1043 return 0;
Kylene Jo Hall08e96e42006-04-22 02:37:50 -07001044 str += sprintf(str,
1045 "TCG version: %d.%d\nFirmware version: %d.%d\n",
Rajiv Andrade08837432009-02-02 15:23:43 -02001046 cap.tpm_version_1_2.Major, cap.tpm_version_1_2.Minor,
1047 cap.tpm_version_1_2.revMajor,
1048 cap.tpm_version_1_2.revMinor);
Kylene Jo Hall08e96e42006-04-22 02:37:50 -07001049 return str - buf;
1050}
1051EXPORT_SYMBOL_GPL(tpm_show_caps_1_2);
1052
Stefan Berger04ab2292011-03-30 12:13:25 -04001053ssize_t tpm_show_durations(struct device *dev, struct device_attribute *attr,
1054 char *buf)
1055{
1056 struct tpm_chip *chip = dev_get_drvdata(dev);
1057
Stefan Berger403d1d02011-08-23 08:52:10 -04001058 if (chip->vendor.duration[TPM_LONG] == 0)
1059 return 0;
1060
Stefan Berger04ab2292011-03-30 12:13:25 -04001061 return sprintf(buf, "%d %d %d [%s]\n",
1062 jiffies_to_usecs(chip->vendor.duration[TPM_SHORT]),
1063 jiffies_to_usecs(chip->vendor.duration[TPM_MEDIUM]),
1064 jiffies_to_usecs(chip->vendor.duration[TPM_LONG]),
1065 chip->vendor.duration_adjusted
1066 ? "adjusted" : "original");
1067}
1068EXPORT_SYMBOL_GPL(tpm_show_durations);
1069
Stefan Berger62592102011-03-30 12:13:28 -04001070ssize_t tpm_show_timeouts(struct device *dev, struct device_attribute *attr,
1071 char *buf)
1072{
1073 struct tpm_chip *chip = dev_get_drvdata(dev);
1074
1075 return sprintf(buf, "%d %d %d %d [%s]\n",
1076 jiffies_to_usecs(chip->vendor.timeout_a),
1077 jiffies_to_usecs(chip->vendor.timeout_b),
1078 jiffies_to_usecs(chip->vendor.timeout_c),
1079 jiffies_to_usecs(chip->vendor.timeout_d),
1080 chip->vendor.timeout_adjusted
1081 ? "adjusted" : "original");
1082}
1083EXPORT_SYMBOL_GPL(tpm_show_timeouts);
1084
Kylene Hall6659ca22005-06-23 22:02:00 -07001085ssize_t tpm_store_cancel(struct device *dev, struct device_attribute *attr,
1086 const char *buf, size_t count)
1087{
1088 struct tpm_chip *chip = dev_get_drvdata(dev);
1089 if (chip == NULL)
1090 return 0;
1091
Kylene Jo Hall90dda522006-04-22 02:37:15 -07001092 chip->vendor.cancel(chip);
Kylene Hall6659ca22005-06-23 22:02:00 -07001093 return count;
1094}
1095EXPORT_SYMBOL_GPL(tpm_store_cancel);
1096
Rajiv Andradefd048862011-09-16 14:39:40 -03001097int wait_for_tpm_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout,
1098 wait_queue_head_t *queue)
1099{
1100 unsigned long stop;
1101 long rc;
1102 u8 status;
1103
1104 /* check current status */
1105 status = chip->vendor.status(chip);
1106 if ((status & mask) == mask)
1107 return 0;
1108
1109 stop = jiffies + timeout;
1110
1111 if (chip->vendor.irq) {
1112again:
1113 timeout = stop - jiffies;
1114 if ((long)timeout <= 0)
1115 return -ETIME;
1116 rc = wait_event_interruptible_timeout(*queue,
1117 ((chip->vendor.status(chip)
1118 & mask) == mask),
1119 timeout);
1120 if (rc > 0)
1121 return 0;
1122 if (rc == -ERESTARTSYS && freezing(current)) {
1123 clear_thread_flag(TIF_SIGPENDING);
1124 goto again;
1125 }
1126 } else {
1127 do {
1128 msleep(TPM_TIMEOUT);
1129 status = chip->vendor.status(chip);
1130 if ((status & mask) == mask)
1131 return 0;
1132 } while (time_before(jiffies, stop));
1133 }
1134 return -ETIME;
1135}
1136EXPORT_SYMBOL_GPL(wait_for_tpm_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137/*
1138 * Device file system interface to the TPM
Rajiv Andradef89c5ed2008-10-11 09:03:39 +11001139 *
1140 * It's assured that the chip will be opened just once,
1141 * by the check of is_open variable, which is protected
1142 * by driver_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 */
1144int tpm_open(struct inode *inode, struct file *file)
1145{
Rajiv Andradef02a9362008-10-11 09:04:23 +11001146 int minor = iminor(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 struct tpm_chip *chip = NULL, *pos;
1148
Rajiv Andradef02a9362008-10-11 09:04:23 +11001149 rcu_read_lock();
1150 list_for_each_entry_rcu(pos, &tpm_chip_list, list) {
Kylene Jo Hall90dda522006-04-22 02:37:15 -07001151 if (pos->vendor.miscdev.minor == minor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 chip = pos;
Rajiv Andradef02a9362008-10-11 09:04:23 +11001153 get_device(chip->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 break;
1155 }
1156 }
Rajiv Andradef02a9362008-10-11 09:04:23 +11001157 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
Rajiv Andradef02a9362008-10-11 09:04:23 +11001159 if (!chip)
1160 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
Rajiv Andradedc36d322008-10-11 09:04:02 +11001162 if (test_and_set_bit(0, &chip->is_open)) {
Andrew Mortonb888c872005-10-30 15:03:28 -08001163 dev_dbg(chip->dev, "Another process owns this TPM\n");
Rajiv Andradef02a9362008-10-11 09:04:23 +11001164 put_device(chip->dev);
1165 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 }
1167
Peter Huewe1309d7a2011-03-29 13:31:25 +02001168 chip->data_buffer = kzalloc(TPM_BUFSIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 if (chip->data_buffer == NULL) {
Rajiv Andradedc36d322008-10-11 09:04:02 +11001170 clear_bit(0, &chip->is_open);
Kylene Jo Halle659a3f2005-10-30 15:03:24 -08001171 put_device(chip->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 return -ENOMEM;
1173 }
1174
1175 atomic_set(&chip->data_pending, 0);
1176
1177 file->private_data = chip;
1178 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180EXPORT_SYMBOL_GPL(tpm_open);
1181
Rajiv Andradef02a9362008-10-11 09:04:23 +11001182/*
1183 * Called on file close
1184 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185int tpm_release(struct inode *inode, struct file *file)
1186{
1187 struct tpm_chip *chip = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
Rajiv Andrade4bdec112008-10-11 09:05:20 +11001189 del_singleshot_timer_sync(&chip->user_read_timer);
Tejun Heo43829732012-08-20 14:51:24 -07001190 flush_work(&chip->work);
Kylene Hall5e976d52005-06-23 22:02:03 -07001191 file->private_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 atomic_set(&chip->data_pending, 0);
Kent Yoderdd7da132012-07-25 14:14:02 -05001193 kzfree(chip->data_buffer);
Rajiv Andradedc36d322008-10-11 09:04:02 +11001194 clear_bit(0, &chip->is_open);
Kylene Jo Halle659a3f2005-10-30 15:03:24 -08001195 put_device(chip->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 return 0;
1197}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198EXPORT_SYMBOL_GPL(tpm_release);
1199
Andrew Mortonb888c872005-10-30 15:03:28 -08001200ssize_t tpm_write(struct file *file, const char __user *buf,
Kylene Jo Hall3c2f6062006-04-22 02:36:56 -07001201 size_t size, loff_t *off)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202{
1203 struct tpm_chip *chip = file->private_data;
Peter Hueweabce9ac2012-09-27 16:09:33 +02001204 size_t in_size = size;
1205 ssize_t out_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
1207 /* cannot perform a write until the read has cleared
Peter Hueweabce9ac2012-09-27 16:09:33 +02001208 either via tpm_read or a user_read_timer timeout.
1209 This also prevents splitted buffered writes from blocking here.
1210 */
1211 if (atomic_read(&chip->data_pending) != 0)
1212 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
1214 if (in_size > TPM_BUFSIZE)
Peter Hueweabce9ac2012-09-27 16:09:33 +02001215 return -E2BIG;
1216
1217 mutex_lock(&chip->buffer_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
1219 if (copy_from_user
1220 (chip->data_buffer, (void __user *) buf, in_size)) {
Matthias Kaehlcked081d472007-05-08 00:32:02 -07001221 mutex_unlock(&chip->buffer_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 return -EFAULT;
1223 }
1224
1225 /* atomic tpm command send and result receive */
1226 out_size = tpm_transmit(chip, chip->data_buffer, TPM_BUFSIZE);
Peter Hueweabce9ac2012-09-27 16:09:33 +02001227 if (out_size < 0) {
1228 mutex_unlock(&chip->buffer_mutex);
1229 return out_size;
1230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
1232 atomic_set(&chip->data_pending, out_size);
Matthias Kaehlcked081d472007-05-08 00:32:02 -07001233 mutex_unlock(&chip->buffer_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
1235 /* Set a timeout by which the reader must come claim the result */
Kylene Hallfe3fd4832005-06-23 22:01:56 -07001236 mod_timer(&chip->user_read_timer, jiffies + (60 * HZ));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
1238 return in_size;
1239}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240EXPORT_SYMBOL_GPL(tpm_write);
1241
Kylene Jo Hall3c2f6062006-04-22 02:36:56 -07001242ssize_t tpm_read(struct file *file, char __user *buf,
1243 size_t size, loff_t *off)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244{
1245 struct tpm_chip *chip = file->private_data;
Michael Halcrow01476002008-07-25 19:45:00 -07001246 ssize_t ret_size;
Peter Huewe3321c072011-09-15 14:47:42 -03001247 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
Kylene Hall5b44bd52005-06-23 22:01:53 -07001249 del_singleshot_timer_sync(&chip->user_read_timer);
Tejun Heo43829732012-08-20 14:51:24 -07001250 flush_work(&chip->work);
Kylene Hall5b44bd52005-06-23 22:01:53 -07001251 ret_size = atomic_read(&chip->data_pending);
Kylene Hall5b44bd52005-06-23 22:01:53 -07001252 if (ret_size > 0) { /* relay data */
Tim Gardner3ab1aff2011-12-06 11:29:20 -07001253 ssize_t orig_ret_size = ret_size;
Kylene Hall5b44bd52005-06-23 22:01:53 -07001254 if (size < ret_size)
1255 ret_size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
Matthias Kaehlcked081d472007-05-08 00:32:02 -07001257 mutex_lock(&chip->buffer_mutex);
Peter Huewe3321c072011-09-15 14:47:42 -03001258 rc = copy_to_user(buf, chip->data_buffer, ret_size);
Tim Gardner3ab1aff2011-12-06 11:29:20 -07001259 memset(chip->data_buffer, 0, orig_ret_size);
Peter Huewe3321c072011-09-15 14:47:42 -03001260 if (rc)
Kylene Hall5b44bd52005-06-23 22:01:53 -07001261 ret_size = -EFAULT;
Peter Huewe3321c072011-09-15 14:47:42 -03001262
Matthias Kaehlcked081d472007-05-08 00:32:02 -07001263 mutex_unlock(&chip->buffer_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 }
1265
Kent Yoderdd7da132012-07-25 14:14:02 -05001266 atomic_set(&chip->data_pending, 0);
1267
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 return ret_size;
1269}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270EXPORT_SYMBOL_GPL(tpm_read);
1271
Kylene Jo Halle659a3f2005-10-30 15:03:24 -08001272void tpm_remove_hardware(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273{
Kylene Jo Halle659a3f2005-10-30 15:03:24 -08001274 struct tpm_chip *chip = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
1276 if (chip == NULL) {
Kylene Jo Halle659a3f2005-10-30 15:03:24 -08001277 dev_err(dev, "No device data found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 return;
1279 }
1280
1281 spin_lock(&driver_lock);
Rajiv Andradef02a9362008-10-11 09:04:23 +11001282 list_del_rcu(&chip->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 spin_unlock(&driver_lock);
Rajiv Andradef02a9362008-10-11 09:04:23 +11001284 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
Kylene Jo Hall90dda522006-04-22 02:37:15 -07001286 misc_deregister(&chip->vendor.miscdev);
Kylene Jo Hall90dda522006-04-22 02:37:15 -07001287 sysfs_remove_group(&dev->kobj, chip->vendor.attr_group);
Gang Wei1631cfb2012-10-09 17:35:22 +08001288 tpm_remove_ppi(&dev->kobj);
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -08001289 tpm_bios_log_teardown(chip->bios_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
Richard MUSIL5bd91f12008-02-06 01:37:02 -08001291 /* write it this way to be explicit (chip->dev == dev) */
1292 put_device(chip->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293}
Kylene Jo Halle659a3f2005-10-30 15:03:24 -08001294EXPORT_SYMBOL_GPL(tpm_remove_hardware);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
Rajiv Andrade225a9be2010-03-25 00:55:32 -03001296#define TPM_ORD_SAVESTATE cpu_to_be32(152)
1297#define SAVESTATE_RESULT_SIZE 10
1298
1299static struct tpm_input_header savestate_header = {
1300 .tag = TPM_TAG_RQU_COMMAND,
1301 .length = cpu_to_be32(10),
1302 .ordinal = TPM_ORD_SAVESTATE
1303};
1304
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305/*
1306 * We are about to suspend. Save the TPM state
1307 * so that it can be restored.
1308 */
Rafael J. Wysocki035e2ce2012-07-06 19:09:01 +02001309int tpm_pm_suspend(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310{
Kylene Jo Hallce2c87d2005-10-30 15:03:25 -08001311 struct tpm_chip *chip = dev_get_drvdata(dev);
Rajiv Andrade225a9be2010-03-25 00:55:32 -03001312 struct tpm_cmd_t cmd;
1313 int rc;
1314
1315 u8 dummy_hash[TPM_DIGEST_SIZE] = { 0 };
David Smith2490c682008-01-14 00:55:12 -08001316
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 if (chip == NULL)
1318 return -ENODEV;
1319
Rajiv Andrade225a9be2010-03-25 00:55:32 -03001320 /* for buggy tpm, flush pcrs with extend to selected dummy */
1321 if (tpm_suspend_pcr) {
1322 cmd.header.in = pcrextend_header;
1323 cmd.params.pcrextend_in.pcr_idx = cpu_to_be32(tpm_suspend_pcr);
1324 memcpy(cmd.params.pcrextend_in.hash, dummy_hash,
1325 TPM_DIGEST_SIZE);
1326 rc = transmit_cmd(chip, &cmd, EXTEND_PCR_RESULT_SIZE,
1327 "extending dummy pcr before suspend");
1328 }
1329
1330 /* now do the actual savestate */
1331 cmd.header.in = savestate_header;
1332 rc = transmit_cmd(chip, &cmd, SAVESTATE_RESULT_SIZE,
1333 "sending savestate before suspend");
1334 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336EXPORT_SYMBOL_GPL(tpm_pm_suspend);
1337
1338/*
1339 * Resume from a power safe. The BIOS already restored
1340 * the TPM state.
1341 */
Kylene Jo Hallce2c87d2005-10-30 15:03:25 -08001342int tpm_pm_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343{
Kylene Jo Hallce2c87d2005-10-30 15:03:25 -08001344 struct tpm_chip *chip = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
1346 if (chip == NULL)
1347 return -ENODEV;
1348
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 return 0;
1350}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351EXPORT_SYMBOL_GPL(tpm_pm_resume);
1352
Kent Yoder41ab9992012-06-07 13:47:14 -05001353#define TPM_GETRANDOM_RESULT_SIZE 18
1354static struct tpm_input_header tpm_getrandom_header = {
1355 .tag = TPM_TAG_RQU_COMMAND,
1356 .length = cpu_to_be32(14),
1357 .ordinal = TPM_ORD_GET_RANDOM
1358};
1359
1360/**
1361 * tpm_get_random() - Get random bytes from the tpm's RNG
1362 * @chip_num: A specific chip number for the request or TPM_ANY_NUM
1363 * @out: destination buffer for the random bytes
1364 * @max: the max number of bytes to write to @out
1365 *
1366 * Returns < 0 on error and the number of bytes read on success
1367 */
1368int tpm_get_random(u32 chip_num, u8 *out, size_t max)
1369{
1370 struct tpm_chip *chip;
1371 struct tpm_cmd_t tpm_cmd;
1372 u32 recd, num_bytes = min_t(u32, max, TPM_MAX_RNG_DATA);
1373 int err, total = 0, retries = 5;
1374 u8 *dest = out;
1375
1376 chip = tpm_chip_find_get(chip_num);
1377 if (chip == NULL)
1378 return -ENODEV;
1379
1380 if (!out || !num_bytes || max > TPM_MAX_RNG_DATA)
1381 return -EINVAL;
1382
1383 do {
1384 tpm_cmd.header.in = tpm_getrandom_header;
1385 tpm_cmd.params.getrandom_in.num_bytes = cpu_to_be32(num_bytes);
1386
1387 err = transmit_cmd(chip, &tpm_cmd,
1388 TPM_GETRANDOM_RESULT_SIZE + num_bytes,
1389 "attempting get random");
1390 if (err)
1391 break;
1392
1393 recd = be32_to_cpu(tpm_cmd.params.getrandom_out.rng_data_len);
1394 memcpy(dest, tpm_cmd.params.getrandom_out.rng_data, recd);
1395
1396 dest += recd;
1397 total += recd;
1398 num_bytes -= recd;
1399 } while (retries-- && total < max);
1400
1401 return total ? total : -EIO;
1402}
1403EXPORT_SYMBOL_GPL(tpm_get_random);
1404
Rajiv Andrade253115b2008-10-11 09:04:39 +11001405/* In case vendor provided release function, call it too.*/
1406
1407void tpm_dev_vendor_release(struct tpm_chip *chip)
1408{
Andi Shyti30729282012-05-28 18:42:45 +02001409 if (!chip)
1410 return;
1411
Rajiv Andrade253115b2008-10-11 09:04:39 +11001412 if (chip->vendor.release)
1413 chip->vendor.release(chip->dev);
1414
1415 clear_bit(chip->dev_num, dev_mask);
1416 kfree(chip->vendor.miscdev.name);
1417}
1418EXPORT_SYMBOL_GPL(tpm_dev_vendor_release);
1419
1420
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421/*
Richard MUSIL5bd91f12008-02-06 01:37:02 -08001422 * Once all references to platform device are down to 0,
1423 * release all allocated structures.
Richard MUSIL5bd91f12008-02-06 01:37:02 -08001424 */
Xiaoyan Zhang81198072013-08-29 20:39:11 +08001425static void tpm_dev_release(struct device *dev)
Richard MUSIL5bd91f12008-02-06 01:37:02 -08001426{
1427 struct tpm_chip *chip = dev_get_drvdata(dev);
1428
Andi Shyti30729282012-05-28 18:42:45 +02001429 if (!chip)
1430 return;
1431
Rajiv Andrade253115b2008-10-11 09:04:39 +11001432 tpm_dev_vendor_release(chip);
Richard MUSIL5bd91f12008-02-06 01:37:02 -08001433
Rajiv Andrade253115b2008-10-11 09:04:39 +11001434 chip->release(dev);
Richard MUSIL5bd91f12008-02-06 01:37:02 -08001435 kfree(chip);
1436}
Rajiv Andrade253115b2008-10-11 09:04:39 +11001437EXPORT_SYMBOL_GPL(tpm_dev_release);
Richard MUSIL5bd91f12008-02-06 01:37:02 -08001438
1439/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 * Called from tpm_<specific>.c probe function only for devices
1441 * the driver has determined it should claim. Prior to calling
1442 * this function the specific probe function has called pci_enable_device
1443 * upon errant exit from this function specific probe function should call
1444 * pci_disable_device
1445 */
Rajiv Andradef02a9362008-10-11 09:04:23 +11001446struct tpm_chip *tpm_register_hardware(struct device *dev,
1447 const struct tpm_vendor_specific *entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448{
Kylene Jo Hall6f9becc2005-06-25 14:55:41 -07001449#define DEVNAME_SIZE 7
1450
1451 char *devname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 struct tpm_chip *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
1454 /* Driver specific per-device data */
Andrew Mortonb888c872005-10-30 15:03:28 -08001455 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
Parag Warudkar8e39c932007-05-08 00:31:09 -07001456 devname = kmalloc(DEVNAME_SIZE, GFP_KERNEL);
1457
Andrew Mortondd78c942008-10-15 22:04:37 -07001458 if (chip == NULL || devname == NULL)
1459 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
Matthias Kaehlcked081d472007-05-08 00:32:02 -07001461 mutex_init(&chip->buffer_mutex);
1462 mutex_init(&chip->tpm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 INIT_LIST_HEAD(&chip->list);
1464
David Howellsc4028952006-11-22 14:57:56 +00001465 INIT_WORK(&chip->work, timeout_work);
Kylene Jo Hall09e12f92005-11-13 16:07:43 -08001466
Jiri Slaby40565f12007-02-12 00:52:31 -08001467 setup_timer(&chip->user_read_timer, user_reader_timeout,
1468 (unsigned long)chip);
Kylene Hallfe3fd4832005-06-23 22:01:56 -07001469
Kylene Jo Hall90dda522006-04-22 02:37:15 -07001470 memcpy(&chip->vendor, entry, sizeof(struct tpm_vendor_specific));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
Kylene Jo Hall10685a92006-04-22 02:38:32 -07001472 chip->dev_num = find_first_zero_bit(dev_mask, TPM_NUM_DEVICES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
Kylene Jo Hall10685a92006-04-22 02:38:32 -07001474 if (chip->dev_num >= TPM_NUM_DEVICES) {
Andrew Mortonb888c872005-10-30 15:03:28 -08001475 dev_err(dev, "No available tpm device numbers\n");
Andrew Mortondd78c942008-10-15 22:04:37 -07001476 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 } else if (chip->dev_num == 0)
Kylene Jo Hall90dda522006-04-22 02:37:15 -07001478 chip->vendor.miscdev.minor = TPM_MINOR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 else
Kylene Jo Hall90dda522006-04-22 02:37:15 -07001480 chip->vendor.miscdev.minor = MISC_DYNAMIC_MINOR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
Kylene Jo Hall10685a92006-04-22 02:38:32 -07001482 set_bit(chip->dev_num, dev_mask);
1483
Kylene Jo Hall6f9becc2005-06-25 14:55:41 -07001484 scnprintf(devname, DEVNAME_SIZE, "%s%d", "tpm", chip->dev_num);
Kylene Jo Hall90dda522006-04-22 02:37:15 -07001485 chip->vendor.miscdev.name = devname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
Greg Kroah-Hartman94fbcde2006-07-27 16:16:04 -07001487 chip->vendor.miscdev.parent = dev;
Kylene Jo Halle659a3f2005-10-30 15:03:24 -08001488 chip->dev = get_device(dev);
Richard MUSIL5bd91f12008-02-06 01:37:02 -08001489 chip->release = dev->release;
1490 dev->release = tpm_dev_release;
1491 dev_set_drvdata(dev, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
Kylene Jo Hall90dda522006-04-22 02:37:15 -07001493 if (misc_register(&chip->vendor.miscdev)) {
Kylene Jo Halle659a3f2005-10-30 15:03:24 -08001494 dev_err(chip->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 "unable to misc_register %s, minor %d\n",
Kylene Jo Hall90dda522006-04-22 02:37:15 -07001496 chip->vendor.miscdev.name,
1497 chip->vendor.miscdev.minor);
Wanlong Gaodad79cb2012-05-16 16:24:28 +08001498 goto put_device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 }
1500
Jeff Garzikf33d9bd2006-10-11 01:21:51 -07001501 if (sysfs_create_group(&dev->kobj, chip->vendor.attr_group)) {
Neil Horman5d469ec02006-12-06 20:37:08 -08001502 misc_deregister(&chip->vendor.miscdev);
Wanlong Gaodad79cb2012-05-16 16:24:28 +08001503 goto put_device;
Jeff Garzikf33d9bd2006-10-11 01:21:51 -07001504 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
Gang Wei1631cfb2012-10-09 17:35:22 +08001506 if (tpm_add_ppi(&dev->kobj)) {
Xiaoyan Zhangf84fdff2012-08-22 18:47:22 +08001507 misc_deregister(&chip->vendor.miscdev);
1508 goto put_device;
1509 }
1510
Kylene Jo Hall55a82ab2006-01-08 01:03:15 -08001511 chip->bios_dir = tpm_bios_log_setup(devname);
1512
Rajiv Andradef02a9362008-10-11 09:04:23 +11001513 /* Make chip available */
1514 spin_lock(&driver_lock);
1515 list_add_rcu(&chip->list, &tpm_chip_list);
1516 spin_unlock(&driver_lock);
1517
Kylene Jo Halle0dd03c2006-04-22 02:37:26 -07001518 return chip;
Andrew Mortondd78c942008-10-15 22:04:37 -07001519
Wanlong Gaodad79cb2012-05-16 16:24:28 +08001520put_device:
1521 put_device(chip->dev);
Andrew Mortondd78c942008-10-15 22:04:37 -07001522out_free:
1523 kfree(chip);
1524 kfree(devname);
1525 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527EXPORT_SYMBOL_GPL(tpm_register_hardware);
1528
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
1530MODULE_DESCRIPTION("TPM Driver");
1531MODULE_VERSION("2.0");
1532MODULE_LICENSE("GPL");