aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJames Morris <james.morris@microsoft.com>2018-07-30 12:30:19 -0400
committerJames Morris <james.morris@microsoft.com>2018-07-30 12:30:19 -0400
commit5da08f7d01693433ff6bea0c3462a5173d577540 (patch)
treea2a302177adbd31edcba2f9cd254f963853711e2
parent87ea58433208d17295e200d56be5e2a4fe4ce7d6 (diff)
parentec403d8ed08c8272cfeeeea154fdebcd289988c8 (diff)
Merge tag 'tpmdd-next-20180728' of git://git.infradead.org/users/jjs/linux-tpmdd into next-tpm
tpmdd updates for Linux 4.19 From Jarkko: * Migrated away from PM runtime as explicit cmdReady/goIdle trasactions for every command is a spec requirement. PM runtime adds only a layer of complexity on our case. * tpm_tis drivers can now specify the hwrng quality. * TPM 2.0 code uses now tpm_buf for constructing messages. I think Tomas Winkler has done the same for TPM 1.2. I'll start digging those changes from the patchwork in the near future. * Bug fixes and clean ups.
-rw-r--r--drivers/char/tpm/tpm-chip.c68
-rw-r--r--drivers/char/tpm/tpm-interface.c72
-rw-r--r--drivers/char/tpm/tpm.h31
-rw-r--r--drivers/char/tpm/tpm2-cmd.c258
-rw-r--r--drivers/char/tpm/tpm2-space.c12
-rw-r--r--drivers/char/tpm/tpm_crb.c101
-rw-r--r--drivers/char/tpm/tpm_i2c_infineon.c8
-rw-r--r--drivers/char/tpm/tpm_tis_core.c2
-rw-r--r--drivers/char/tpm/tpm_tis_core.h1
-rw-r--r--drivers/char/tpm/tpm_tis_spi.c9
-rw-r--r--drivers/char/tpm/tpm_vtpm_proxy.c2
-rw-r--r--include/linux/tpm.h7
-rw-r--r--security/integrity/ima/ima.h2
-rw-r--r--security/integrity/ima/ima_crypto.c4
-rw-r--r--security/integrity/ima/ima_init.c16
-rw-r--r--security/integrity/ima/ima_queue.c4
16 files changed, 289 insertions, 308 deletions
diff --git a/drivers/char/tpm/tpm-chip.c b/drivers/char/tpm/tpm-chip.c
index 0a62c19937b6..46caadca916a 100644
--- a/drivers/char/tpm/tpm-chip.c
+++ b/drivers/char/tpm/tpm-chip.c
@@ -81,42 +81,66 @@ void tpm_put_ops(struct tpm_chip *chip)
81EXPORT_SYMBOL_GPL(tpm_put_ops); 81EXPORT_SYMBOL_GPL(tpm_put_ops);
82 82
83/** 83/**
84 * tpm_chip_find_get() - find and reserve a TPM chip 84 * tpm_default_chip() - find a TPM chip and get a reference to it
85 */
86struct tpm_chip *tpm_default_chip(void)
87{
88 struct tpm_chip *chip, *res = NULL;
89 int chip_num = 0;
90 int chip_prev;
91
92 mutex_lock(&idr_lock);
93
94 do {
95 chip_prev = chip_num;
96 chip = idr_get_next(&dev_nums_idr, &chip_num);
97 if (chip) {
98 get_device(&chip->dev);
99 res = chip;
100 break;
101 }
102 } while (chip_prev != chip_num);
103
104 mutex_unlock(&idr_lock);
105
106 return res;
107}
108EXPORT_SYMBOL_GPL(tpm_default_chip);
109
110/**
111 * tpm_find_get_ops() - find and reserve a TPM chip
85 * @chip: a &struct tpm_chip instance, %NULL for the default chip 112 * @chip: a &struct tpm_chip instance, %NULL for the default chip
86 * 113 *
87 * Finds a TPM chip and reserves its class device and operations. The chip must 114 * Finds a TPM chip and reserves its class device and operations. The chip must
88 * be released with tpm_chip_put_ops() after use. 115 * be released with tpm_put_ops() after use.
116 * This function is for internal use only. It supports existing TPM callers
117 * by accepting NULL, but those callers should be converted to pass in a chip
118 * directly.
89 * 119 *
90 * Return: 120 * Return:
91 * A reserved &struct tpm_chip instance. 121 * A reserved &struct tpm_chip instance.
92 * %NULL if a chip is not found. 122 * %NULL if a chip is not found.
93 * %NULL if the chip is not available. 123 * %NULL if the chip is not available.
94 */ 124 */
95struct tpm_chip *tpm_chip_find_get(struct tpm_chip *chip) 125struct tpm_chip *tpm_find_get_ops(struct tpm_chip *chip)
96{ 126{
97 struct tpm_chip *res = NULL; 127 int rc;
98 int chip_num = 0;
99 int chip_prev;
100
101 mutex_lock(&idr_lock);
102 128
103 if (!chip) { 129 if (chip) {
104 do {
105 chip_prev = chip_num;
106 chip = idr_get_next(&dev_nums_idr, &chip_num);
107 if (chip && !tpm_try_get_ops(chip)) {
108 res = chip;
109 break;
110 }
111 } while (chip_prev != chip_num);
112 } else {
113 if (!tpm_try_get_ops(chip)) 130 if (!tpm_try_get_ops(chip))
114 res = chip; 131 return chip;
132 return NULL;
115 } 133 }
116 134
117 mutex_unlock(&idr_lock); 135 chip = tpm_default_chip();
118 136 if (!chip)
119 return res; 137 return NULL;
138 rc = tpm_try_get_ops(chip);
139 /* release additional reference we got from tpm_default_chip() */
140 put_device(&chip->dev);
141 if (rc)
142 return NULL;
143 return chip;
120} 144}
121 145
122/** 146/**
diff --git a/drivers/char/tpm/tpm-interface.c b/drivers/char/tpm/tpm-interface.c
index e32f6e85dc6d..1a803b0cf980 100644
--- a/drivers/char/tpm/tpm-interface.c
+++ b/drivers/char/tpm/tpm-interface.c
@@ -29,7 +29,6 @@
29#include <linux/mutex.h> 29#include <linux/mutex.h>
30#include <linux/spinlock.h> 30#include <linux/spinlock.h>
31#include <linux/freezer.h> 31#include <linux/freezer.h>
32#include <linux/pm_runtime.h>
33#include <linux/tpm_eventlog.h> 32#include <linux/tpm_eventlog.h>
34 33
35#include "tpm.h" 34#include "tpm.h"
@@ -369,10 +368,13 @@ err_len:
369 return -EINVAL; 368 return -EINVAL;
370} 369}
371 370
372static int tpm_request_locality(struct tpm_chip *chip) 371static int tpm_request_locality(struct tpm_chip *chip, unsigned int flags)
373{ 372{
374 int rc; 373 int rc;
375 374
375 if (flags & TPM_TRANSMIT_NESTED)
376 return 0;
377
376 if (!chip->ops->request_locality) 378 if (!chip->ops->request_locality)
377 return 0; 379 return 0;
378 380
@@ -385,10 +387,13 @@ static int tpm_request_locality(struct tpm_chip *chip)
385 return 0; 387 return 0;
386} 388}
387 389
388static void tpm_relinquish_locality(struct tpm_chip *chip) 390static void tpm_relinquish_locality(struct tpm_chip *chip, unsigned int flags)
389{ 391{
390 int rc; 392 int rc;
391 393
394 if (flags & TPM_TRANSMIT_NESTED)
395 return;
396
392 if (!chip->ops->relinquish_locality) 397 if (!chip->ops->relinquish_locality)
393 return; 398 return;
394 399
@@ -399,6 +404,28 @@ static void tpm_relinquish_locality(struct tpm_chip *chip)
399 chip->locality = -1; 404 chip->locality = -1;
400} 405}
401 406
407static int tpm_cmd_ready(struct tpm_chip *chip, unsigned int flags)
408{
409 if (flags & TPM_TRANSMIT_NESTED)
410 return 0;
411
412 if (!chip->ops->cmd_ready)
413 return 0;
414
415 return chip->ops->cmd_ready(chip);
416}
417
418static int tpm_go_idle(struct tpm_chip *chip, unsigned int flags)
419{
420 if (flags & TPM_TRANSMIT_NESTED)
421 return 0;
422
423 if (!chip->ops->go_idle)
424 return 0;
425
426 return chip->ops->go_idle(chip);
427}
428
402static ssize_t tpm_try_transmit(struct tpm_chip *chip, 429static ssize_t tpm_try_transmit(struct tpm_chip *chip,
403 struct tpm_space *space, 430 struct tpm_space *space,
404 u8 *buf, size_t bufsiz, 431 u8 *buf, size_t bufsiz,
@@ -423,7 +450,7 @@ static ssize_t tpm_try_transmit(struct tpm_chip *chip,
423 header->tag = cpu_to_be16(TPM2_ST_NO_SESSIONS); 450 header->tag = cpu_to_be16(TPM2_ST_NO_SESSIONS);
424 header->return_code = cpu_to_be32(TPM2_RC_COMMAND_CODE | 451 header->return_code = cpu_to_be32(TPM2_RC_COMMAND_CODE |
425 TSS2_RESMGR_TPM_RC_LAYER); 452 TSS2_RESMGR_TPM_RC_LAYER);
426 return bufsiz; 453 return sizeof(*header);
427 } 454 }
428 455
429 if (bufsiz > TPM_BUFSIZE) 456 if (bufsiz > TPM_BUFSIZE)
@@ -439,24 +466,24 @@ static ssize_t tpm_try_transmit(struct tpm_chip *chip,
439 return -E2BIG; 466 return -E2BIG;
440 } 467 }
441 468
442 if (!(flags & TPM_TRANSMIT_UNLOCKED)) 469 if (!(flags & TPM_TRANSMIT_UNLOCKED) && !(flags & TPM_TRANSMIT_NESTED))
443 mutex_lock(&chip->tpm_mutex); 470 mutex_lock(&chip->tpm_mutex);
444 471
445
446 if (chip->ops->clk_enable != NULL) 472 if (chip->ops->clk_enable != NULL)
447 chip->ops->clk_enable(chip, true); 473 chip->ops->clk_enable(chip, true);
448 474
449 /* Store the decision as chip->locality will be changed. */ 475 /* Store the decision as chip->locality will be changed. */
450 need_locality = chip->locality == -1; 476 need_locality = chip->locality == -1;
451 477
452 if (!(flags & TPM_TRANSMIT_RAW) && need_locality) { 478 if (need_locality) {
453 rc = tpm_request_locality(chip); 479 rc = tpm_request_locality(chip, flags);
454 if (rc < 0) 480 if (rc < 0)
455 goto out_no_locality; 481 goto out_no_locality;
456 } 482 }
457 483
458 if (chip->dev.parent) 484 rc = tpm_cmd_ready(chip, flags);
459 pm_runtime_get_sync(chip->dev.parent); 485 if (rc)
486 goto out;
460 487
461 rc = tpm2_prepare_space(chip, space, ordinal, buf); 488 rc = tpm2_prepare_space(chip, space, ordinal, buf);
462 if (rc) 489 if (rc)
@@ -516,19 +543,22 @@ out_recv:
516 } 543 }
517 544
518 rc = tpm2_commit_space(chip, space, ordinal, buf, &len); 545 rc = tpm2_commit_space(chip, space, ordinal, buf, &len);
546 if (rc)
547 dev_err(&chip->dev, "tpm2_commit_space: error %d\n", rc);
519 548
520out: 549out:
521 if (chip->dev.parent) 550 rc = tpm_go_idle(chip, flags);
522 pm_runtime_put_sync(chip->dev.parent); 551 if (rc)
552 goto out;
523 553
524 if (need_locality) 554 if (need_locality)
525 tpm_relinquish_locality(chip); 555 tpm_relinquish_locality(chip, flags);
526 556
527out_no_locality: 557out_no_locality:
528 if (chip->ops->clk_enable != NULL) 558 if (chip->ops->clk_enable != NULL)
529 chip->ops->clk_enable(chip, false); 559 chip->ops->clk_enable(chip, false);
530 560
531 if (!(flags & TPM_TRANSMIT_UNLOCKED)) 561 if (!(flags & TPM_TRANSMIT_UNLOCKED) && !(flags & TPM_TRANSMIT_NESTED))
532 mutex_unlock(&chip->tpm_mutex); 562 mutex_unlock(&chip->tpm_mutex);
533 return rc ? rc : len; 563 return rc ? rc : len;
534} 564}
@@ -930,7 +960,7 @@ int tpm_is_tpm2(struct tpm_chip *chip)
930{ 960{
931 int rc; 961 int rc;
932 962
933 chip = tpm_chip_find_get(chip); 963 chip = tpm_find_get_ops(chip);
934 if (!chip) 964 if (!chip)
935 return -ENODEV; 965 return -ENODEV;
936 966
@@ -954,7 +984,7 @@ int tpm_pcr_read(struct tpm_chip *chip, int pcr_idx, u8 *res_buf)
954{ 984{
955 int rc; 985 int rc;
956 986
957 chip = tpm_chip_find_get(chip); 987 chip = tpm_find_get_ops(chip);
958 if (!chip) 988 if (!chip)
959 return -ENODEV; 989 return -ENODEV;
960 if (chip->flags & TPM_CHIP_FLAG_TPM2) 990 if (chip->flags & TPM_CHIP_FLAG_TPM2)
@@ -1013,7 +1043,7 @@ int tpm_pcr_extend(struct tpm_chip *chip, int pcr_idx, const u8 *hash)
1013 u32 count = 0; 1043 u32 count = 0;
1014 int i; 1044 int i;
1015 1045
1016 chip = tpm_chip_find_get(chip); 1046 chip = tpm_find_get_ops(chip);
1017 if (!chip) 1047 if (!chip)
1018 return -ENODEV; 1048 return -ENODEV;
1019 1049
@@ -1142,7 +1172,7 @@ int tpm_send(struct tpm_chip *chip, void *cmd, size_t buflen)
1142{ 1172{
1143 int rc; 1173 int rc;
1144 1174
1145 chip = tpm_chip_find_get(chip); 1175 chip = tpm_find_get_ops(chip);
1146 if (!chip) 1176 if (!chip)
1147 return -ENODEV; 1177 return -ENODEV;
1148 1178
@@ -1262,7 +1292,7 @@ int tpm_get_random(struct tpm_chip *chip, u8 *out, size_t max)
1262 if (!out || !num_bytes || max > TPM_MAX_RNG_DATA) 1292 if (!out || !num_bytes || max > TPM_MAX_RNG_DATA)
1263 return -EINVAL; 1293 return -EINVAL;
1264 1294
1265 chip = tpm_chip_find_get(chip); 1295 chip = tpm_find_get_ops(chip);
1266 if (!chip) 1296 if (!chip)
1267 return -ENODEV; 1297 return -ENODEV;
1268 1298
@@ -1324,7 +1354,7 @@ int tpm_seal_trusted(struct tpm_chip *chip, struct trusted_key_payload *payload,
1324{ 1354{
1325 int rc; 1355 int rc;
1326 1356
1327 chip = tpm_chip_find_get(chip); 1357 chip = tpm_find_get_ops(chip);
1328 if (!chip || !(chip->flags & TPM_CHIP_FLAG_TPM2)) 1358 if (!chip || !(chip->flags & TPM_CHIP_FLAG_TPM2))
1329 return -ENODEV; 1359 return -ENODEV;
1330 1360
@@ -1352,7 +1382,7 @@ int tpm_unseal_trusted(struct tpm_chip *chip,
1352{ 1382{
1353 int rc; 1383 int rc;
1354 1384
1355 chip = tpm_chip_find_get(chip); 1385 chip = tpm_find_get_ops(chip);
1356 if (!chip || !(chip->flags & TPM_CHIP_FLAG_TPM2)) 1386 if (!chip || !(chip->flags & TPM_CHIP_FLAG_TPM2))
1357 return -ENODEV; 1387 return -ENODEV;
1358 1388
diff --git a/drivers/char/tpm/tpm.h b/drivers/char/tpm/tpm.h
index 4426649e431c..f3501d05264f 100644
--- a/drivers/char/tpm/tpm.h
+++ b/drivers/char/tpm/tpm.h
@@ -424,23 +424,24 @@ struct tpm_buf {
424 u8 *data; 424 u8 *data;
425}; 425};
426 426
427static inline int tpm_buf_init(struct tpm_buf *buf, u16 tag, u32 ordinal) 427static inline void tpm_buf_reset(struct tpm_buf *buf, u16 tag, u32 ordinal)
428{ 428{
429 struct tpm_input_header *head; 429 struct tpm_input_header *head;
430 head = (struct tpm_input_header *)buf->data;
431 head->tag = cpu_to_be16(tag);
432 head->length = cpu_to_be32(sizeof(*head));
433 head->ordinal = cpu_to_be32(ordinal);
434}
430 435
436static inline int tpm_buf_init(struct tpm_buf *buf, u16 tag, u32 ordinal)
437{
431 buf->data_page = alloc_page(GFP_HIGHUSER); 438 buf->data_page = alloc_page(GFP_HIGHUSER);
432 if (!buf->data_page) 439 if (!buf->data_page)
433 return -ENOMEM; 440 return -ENOMEM;
434 441
435 buf->flags = 0; 442 buf->flags = 0;
436 buf->data = kmap(buf->data_page); 443 buf->data = kmap(buf->data_page);
437 444 tpm_buf_reset(buf, tag, ordinal);
438 head = (struct tpm_input_header *) buf->data;
439
440 head->tag = cpu_to_be16(tag);
441 head->length = cpu_to_be32(sizeof(*head));
442 head->ordinal = cpu_to_be32(ordinal);
443
444 return 0; 445 return 0;
445} 446}
446 447
@@ -511,9 +512,17 @@ extern const struct file_operations tpm_fops;
511extern const struct file_operations tpmrm_fops; 512extern const struct file_operations tpmrm_fops;
512extern struct idr dev_nums_idr; 513extern struct idr dev_nums_idr;
513 514
515/**
516 * enum tpm_transmit_flags - flags for tpm_transmit()
517 *
518 * @TPM_TRANSMIT_UNLOCKED: do not lock the chip
519 * @TPM_TRANSMIT_NESTED: discard setup steps (power management,
520 * locality) including locking (i.e. implicit
521 * UNLOCKED)
522 */
514enum tpm_transmit_flags { 523enum tpm_transmit_flags {
515 TPM_TRANSMIT_UNLOCKED = BIT(0), 524 TPM_TRANSMIT_UNLOCKED = BIT(0),
516 TPM_TRANSMIT_RAW = BIT(1), 525 TPM_TRANSMIT_NESTED = BIT(1),
517}; 526};
518 527
519ssize_t tpm_transmit(struct tpm_chip *chip, struct tpm_space *space, 528ssize_t tpm_transmit(struct tpm_chip *chip, struct tpm_space *space,
@@ -538,7 +547,7 @@ static inline void tpm_msleep(unsigned int delay_msec)
538 delay_msec * 1000); 547 delay_msec * 1000);
539}; 548};
540 549
541struct tpm_chip *tpm_chip_find_get(struct tpm_chip *chip); 550struct tpm_chip *tpm_find_get_ops(struct tpm_chip *chip);
542__must_check int tpm_try_get_ops(struct tpm_chip *chip); 551__must_check int tpm_try_get_ops(struct tpm_chip *chip);
543void tpm_put_ops(struct tpm_chip *chip); 552void tpm_put_ops(struct tpm_chip *chip);
544 553
@@ -569,7 +578,7 @@ static inline u32 tpm2_rc_value(u32 rc)
569int tpm2_pcr_read(struct tpm_chip *chip, int pcr_idx, u8 *res_buf); 578int tpm2_pcr_read(struct tpm_chip *chip, int pcr_idx, u8 *res_buf);
570int tpm2_pcr_extend(struct tpm_chip *chip, int pcr_idx, u32 count, 579int tpm2_pcr_extend(struct tpm_chip *chip, int pcr_idx, u32 count,
571 struct tpm2_digest *digests); 580 struct tpm2_digest *digests);
572int tpm2_get_random(struct tpm_chip *chip, u8 *out, size_t max); 581int tpm2_get_random(struct tpm_chip *chip, u8 *dest, size_t max);
573void tpm2_flush_context_cmd(struct tpm_chip *chip, u32 handle, 582void tpm2_flush_context_cmd(struct tpm_chip *chip, u32 handle,
574 unsigned int flags); 583 unsigned int flags);
575int tpm2_seal_trusted(struct tpm_chip *chip, 584int tpm2_seal_trusted(struct tpm_chip *chip,
diff --git a/drivers/char/tpm/tpm2-cmd.c b/drivers/char/tpm/tpm2-cmd.c
index d31b09099216..c31b490bd41d 100644
--- a/drivers/char/tpm/tpm2-cmd.c
+++ b/drivers/char/tpm/tpm2-cmd.c
@@ -27,46 +27,6 @@ enum tpm2_session_attributes {
27 TPM2_SA_CONTINUE_SESSION = BIT(0), 27 TPM2_SA_CONTINUE_SESSION = BIT(0),
28}; 28};
29 29
30struct tpm2_startup_in {
31 __be16 startup_type;
32} __packed;
33
34struct tpm2_get_tpm_pt_in {
35 __be32 cap_id;
36 __be32 property_id;
37 __be32 property_cnt;
38} __packed;
39
40struct tpm2_get_tpm_pt_out {
41 u8 more_data;
42 __be32 subcap_id;
43 __be32 property_cnt;
44 __be32 property_id;
45 __be32 value;
46} __packed;
47
48struct tpm2_get_random_in {
49 __be16 size;
50} __packed;
51
52struct tpm2_get_random_out {
53 __be16 size;
54 u8 buffer[TPM_MAX_RNG_DATA];
55} __packed;
56
57union tpm2_cmd_params {
58 struct tpm2_startup_in startup_in;
59 struct tpm2_get_tpm_pt_in get_tpm_pt_in;
60 struct tpm2_get_tpm_pt_out get_tpm_pt_out;
61 struct tpm2_get_random_in getrandom_in;
62 struct tpm2_get_random_out getrandom_out;
63};
64
65struct tpm2_cmd {
66 tpm_cmd_header header;
67 union tpm2_cmd_params params;
68} __packed;
69
70struct tpm2_hash { 30struct tpm2_hash {
71 unsigned int crypto_id; 31 unsigned int crypto_id;
72 unsigned int tpm_id; 32 unsigned int tpm_id;
@@ -321,82 +281,72 @@ int tpm2_pcr_extend(struct tpm_chip *chip, int pcr_idx, u32 count,
321} 281}
322 282
323 283
324#define TPM2_GETRANDOM_IN_SIZE \ 284struct tpm2_get_random_out {
325 (sizeof(struct tpm_input_header) + \ 285 __be16 size;
326 sizeof(struct tpm2_get_random_in)) 286 u8 buffer[TPM_MAX_RNG_DATA];
327 287} __packed;
328static const struct tpm_input_header tpm2_getrandom_header = {
329 .tag = cpu_to_be16(TPM2_ST_NO_SESSIONS),
330 .length = cpu_to_be32(TPM2_GETRANDOM_IN_SIZE),
331 .ordinal = cpu_to_be32(TPM2_CC_GET_RANDOM)
332};
333 288
334/** 289/**
335 * tpm2_get_random() - get random bytes from the TPM RNG 290 * tpm2_get_random() - get random bytes from the TPM RNG
336 * 291 *
337 * @chip: TPM chip to use 292 * @chip: a &tpm_chip instance
338 * @out: destination buffer for the random bytes 293 * @dest: destination buffer
339 * @max: the max number of bytes to write to @out 294 * @max: the max number of random bytes to pull
340 * 295 *
341 * Return: 296 * Return:
342 * Size of the output buffer, or -EIO on error. 297 * size of the buffer on success,
298 * -errno otherwise
343 */ 299 */
344int tpm2_get_random(struct tpm_chip *chip, u8 *out, size_t max) 300int tpm2_get_random(struct tpm_chip *chip, u8 *dest, size_t max)
345{ 301{
346 struct tpm2_cmd cmd; 302 struct tpm2_get_random_out *out;
347 u32 recd, rlength; 303 struct tpm_buf buf;
348 u32 num_bytes; 304 u32 recd;
305 u32 num_bytes = max;
349 int err; 306 int err;
350 int total = 0; 307 int total = 0;
351 int retries = 5; 308 int retries = 5;
352 u8 *dest = out; 309 u8 *dest_ptr = dest;
353 310
354 num_bytes = min_t(u32, max, sizeof(cmd.params.getrandom_out.buffer)); 311 if (!num_bytes || max > TPM_MAX_RNG_DATA)
355
356 if (!out || !num_bytes ||
357 max > sizeof(cmd.params.getrandom_out.buffer))
358 return -EINVAL; 312 return -EINVAL;
359 313
360 do { 314 err = tpm_buf_init(&buf, 0, 0);
361 cmd.header.in = tpm2_getrandom_header; 315 if (err)
362 cmd.params.getrandom_in.size = cpu_to_be16(num_bytes); 316 return err;
363 317
364 err = tpm_transmit_cmd(chip, NULL, &cmd, sizeof(cmd), 318 do {
319 tpm_buf_reset(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_RANDOM);
320 tpm_buf_append_u16(&buf, num_bytes);
321 err = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE,
365 offsetof(struct tpm2_get_random_out, 322 offsetof(struct tpm2_get_random_out,
366 buffer), 323 buffer),
367 0, "attempting get random"); 324 0, "attempting get random");
368 if (err) 325 if (err)
369 break; 326 goto out;
370 327
371 recd = min_t(u32, be16_to_cpu(cmd.params.getrandom_out.size), 328 out = (struct tpm2_get_random_out *)
372 num_bytes); 329 &buf.data[TPM_HEADER_SIZE];
373 rlength = be32_to_cpu(cmd.header.out.length); 330 recd = min_t(u32, be16_to_cpu(out->size), num_bytes);
374 if (rlength < offsetof(struct tpm2_get_random_out, buffer) + 331 if (tpm_buf_length(&buf) <
375 recd) 332 offsetof(struct tpm2_get_random_out, buffer) + recd) {
376 return -EFAULT; 333 err = -EFAULT;
377 memcpy(dest, cmd.params.getrandom_out.buffer, recd); 334 goto out;
335 }
336 memcpy(dest_ptr, out->buffer, recd);
378 337
379 dest += recd; 338 dest_ptr += recd;
380 total += recd; 339 total += recd;
381 num_bytes -= recd; 340 num_bytes -= recd;
382 } while (retries-- && total < max); 341 } while (retries-- && total < max);
383 342
343 tpm_buf_destroy(&buf);
384 return total ? total : -EIO; 344 return total ? total : -EIO;
345out:
346 tpm_buf_destroy(&buf);
347 return err;
385} 348}
386 349
387#define TPM2_GET_TPM_PT_IN_SIZE \
388 (sizeof(struct tpm_input_header) + \
389 sizeof(struct tpm2_get_tpm_pt_in))
390
391#define TPM2_GET_TPM_PT_OUT_BODY_SIZE \
392 sizeof(struct tpm2_get_tpm_pt_out)
393
394static const struct tpm_input_header tpm2_get_tpm_pt_header = {
395 .tag = cpu_to_be16(TPM2_ST_NO_SESSIONS),
396 .length = cpu_to_be32(TPM2_GET_TPM_PT_IN_SIZE),
397 .ordinal = cpu_to_be32(TPM2_CC_GET_CAPABILITY)
398};
399
400/** 350/**
401 * tpm2_flush_context_cmd() - execute a TPM2_FlushContext command 351 * tpm2_flush_context_cmd() - execute a TPM2_FlushContext command
402 * @chip: TPM chip to use 352 * @chip: TPM chip to use
@@ -471,7 +421,7 @@ int tpm2_seal_trusted(struct tpm_chip *chip,
471{ 421{
472 unsigned int blob_len; 422 unsigned int blob_len;
473 struct tpm_buf buf; 423 struct tpm_buf buf;
474 u32 hash, rlength; 424 u32 hash;
475 int i; 425 int i;
476 int rc; 426 int rc;
477 427
@@ -546,8 +496,7 @@ int tpm2_seal_trusted(struct tpm_chip *chip,
546 rc = -E2BIG; 496 rc = -E2BIG;
547 goto out; 497 goto out;
548 } 498 }
549 rlength = be32_to_cpu(((struct tpm2_cmd *)&buf)->header.out.length); 499 if (tpm_buf_length(&buf) < TPM_HEADER_SIZE + 4 + blob_len) {
550 if (rlength < TPM_HEADER_SIZE + 4 + blob_len) {
551 rc = -EFAULT; 500 rc = -EFAULT;
552 goto out; 501 goto out;
553 } 502 }
@@ -657,7 +606,6 @@ static int tpm2_unseal_cmd(struct tpm_chip *chip,
657 u16 data_len; 606 u16 data_len;
658 u8 *data; 607 u8 *data;
659 int rc; 608 int rc;
660 u32 rlength;
661 609
662 rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS, TPM2_CC_UNSEAL); 610 rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS, TPM2_CC_UNSEAL);
663 if (rc) 611 if (rc)
@@ -685,9 +633,7 @@ static int tpm2_unseal_cmd(struct tpm_chip *chip,
685 goto out; 633 goto out;
686 } 634 }
687 635
688 rlength = be32_to_cpu(((struct tpm2_cmd *)&buf) 636 if (tpm_buf_length(&buf) < TPM_HEADER_SIZE + 6 + data_len) {
689 ->header.out.length);
690 if (rlength < TPM_HEADER_SIZE + 6 + data_len) {
691 rc = -EFAULT; 637 rc = -EFAULT;
692 goto out; 638 goto out;
693 } 639 }
@@ -733,69 +679,71 @@ out:
733 return rc; 679 return rc;
734} 680}
735 681
682struct tpm2_get_cap_out {
683 u8 more_data;
684 __be32 subcap_id;
685 __be32 property_cnt;
686 __be32 property_id;
687 __be32 value;
688} __packed;
689
736/** 690/**
737 * tpm2_get_tpm_pt() - get value of a TPM_CAP_TPM_PROPERTIES type property 691 * tpm2_get_tpm_pt() - get value of a TPM_CAP_TPM_PROPERTIES type property
738 * @chip: TPM chip to use. 692 * @chip: a &tpm_chip instance
739 * @property_id: property ID. 693 * @property_id: property ID.
740 * @value: output variable. 694 * @value: output variable.
741 * @desc: passed to tpm_transmit_cmd() 695 * @desc: passed to tpm_transmit_cmd()
742 * 696 *
743 * Return: Same as with tpm_transmit_cmd. 697 * Return:
698 * 0 on success,
699 * -errno or a TPM return code otherwise
744 */ 700 */
745ssize_t tpm2_get_tpm_pt(struct tpm_chip *chip, u32 property_id, u32 *value, 701ssize_t tpm2_get_tpm_pt(struct tpm_chip *chip, u32 property_id, u32 *value,
746 const char *desc) 702 const char *desc)
747{ 703{
748 struct tpm2_cmd cmd; 704 struct tpm2_get_cap_out *out;
705 struct tpm_buf buf;
749 int rc; 706 int rc;
750 707
751 cmd.header.in = tpm2_get_tpm_pt_header; 708 rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_CAPABILITY);
752 cmd.params.get_tpm_pt_in.cap_id = cpu_to_be32(TPM2_CAP_TPM_PROPERTIES); 709 if (rc)
753 cmd.params.get_tpm_pt_in.property_id = cpu_to_be32(property_id); 710 return rc;
754 cmd.params.get_tpm_pt_in.property_cnt = cpu_to_be32(1); 711 tpm_buf_append_u32(&buf, TPM2_CAP_TPM_PROPERTIES);
755 712 tpm_buf_append_u32(&buf, property_id);
756 rc = tpm_transmit_cmd(chip, NULL, &cmd, sizeof(cmd), 713 tpm_buf_append_u32(&buf, 1);
757 TPM2_GET_TPM_PT_OUT_BODY_SIZE, 0, desc); 714 rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 0, 0, NULL);
758 if (!rc) 715 if (!rc) {
759 *value = be32_to_cpu(cmd.params.get_tpm_pt_out.value); 716 out = (struct tpm2_get_cap_out *)
760 717 &buf.data[TPM_HEADER_SIZE];
718 *value = be32_to_cpu(out->value);
719 }
720 tpm_buf_destroy(&buf);
761 return rc; 721 return rc;
762} 722}
763EXPORT_SYMBOL_GPL(tpm2_get_tpm_pt); 723EXPORT_SYMBOL_GPL(tpm2_get_tpm_pt);
764 724
765#define TPM2_SHUTDOWN_IN_SIZE \
766 (sizeof(struct tpm_input_header) + \
767 sizeof(struct tpm2_startup_in))
768
769static const struct tpm_input_header tpm2_shutdown_header = {
770 .tag = cpu_to_be16(TPM2_ST_NO_SESSIONS),
771 .length = cpu_to_be32(TPM2_SHUTDOWN_IN_SIZE),
772 .ordinal = cpu_to_be32(TPM2_CC_SHUTDOWN)
773};
774
775/** 725/**
776 * tpm2_shutdown() - send shutdown command to the TPM chip 726 * tpm2_shutdown() - send a TPM shutdown command
727 *
728 * Sends a TPM shutdown command. The shutdown command is used in call
729 * sites where the system is going down. If it fails, there is not much
730 * that can be done except print an error message.
777 * 731 *
778 * @chip: TPM chip to use. 732 * @chip: a &tpm_chip instance
779 * @shutdown_type: shutdown type. The value is either 733 * @shutdown_type: TPM_SU_CLEAR or TPM_SU_STATE.
780 * TPM_SU_CLEAR or TPM_SU_STATE.
781 */ 734 */
782void tpm2_shutdown(struct tpm_chip *chip, u16 shutdown_type) 735void tpm2_shutdown(struct tpm_chip *chip, u16 shutdown_type)
783{ 736{
784 struct tpm2_cmd cmd; 737 struct tpm_buf buf;
785 int rc; 738 int rc;
786 739
787 cmd.header.in = tpm2_shutdown_header; 740 rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_SHUTDOWN);
788 cmd.params.startup_in.startup_type = cpu_to_be16(shutdown_type); 741 if (rc)
789 742 return;
790 rc = tpm_transmit_cmd(chip, NULL, &cmd, sizeof(cmd), 0, 0, 743 tpm_buf_append_u16(&buf, shutdown_type);
791 "stopping the TPM"); 744 tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 0, 0,
792 745 "stopping the TPM");
793 /* In places where shutdown command is sent there's no much we can do 746 tpm_buf_destroy(&buf);
794 * except print the error code on a system failure.
795 */
796 if (rc < 0 && rc != -EPIPE)
797 dev_warn(&chip->dev, "transmit returned %d while stopping the TPM",
798 rc);
799} 747}
800 748
801/* 749/*
@@ -863,31 +811,37 @@ static int tpm2_do_selftest(struct tpm_chip *chip)
863} 811}
864 812
865/** 813/**
866 * tpm2_probe() - probe TPM 2.0 814 * tpm2_probe() - probe for the TPM 2.0 protocol
867 * @chip: TPM chip to use 815 * @chip: a &tpm_chip instance
868 * 816 *
869 * Return: < 0 error and 0 on success. 817 * Send an idempotent TPM 2.0 command and see whether there is TPM2 chip in the
818 * other end based on the response tag. The flag TPM_CHIP_FLAG_TPM2 is set by
819 * this function if this is the case.
870 * 820 *
871 * Send idempotent TPM 2.0 command and see whether TPM 2.0 chip replied based on 821 * Return:
872 * the reply tag. 822 * 0 on success,
823 * -errno otherwise
873 */ 824 */
874int tpm2_probe(struct tpm_chip *chip) 825int tpm2_probe(struct tpm_chip *chip)
875{ 826{
876 struct tpm2_cmd cmd; 827 struct tpm_output_header *out;
828 struct tpm_buf buf;
877 int rc; 829 int rc;
878 830
879 cmd.header.in = tpm2_get_tpm_pt_header; 831 rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_CAPABILITY);
880 cmd.params.get_tpm_pt_in.cap_id = cpu_to_be32(TPM2_CAP_TPM_PROPERTIES); 832 if (rc)
881 cmd.params.get_tpm_pt_in.property_id = cpu_to_be32(0x100);
882 cmd.params.get_tpm_pt_in.property_cnt = cpu_to_be32(1);
883
884 rc = tpm_transmit_cmd(chip, NULL, &cmd, sizeof(cmd), 0, 0, NULL);
885 if (rc < 0)
886 return rc; 833 return rc;
887 834 tpm_buf_append_u32(&buf, TPM2_CAP_TPM_PROPERTIES);
888 if (be16_to_cpu(cmd.header.out.tag) == TPM2_ST_NO_SESSIONS) 835 tpm_buf_append_u32(&buf, TPM_PT_TOTAL_COMMANDS);
889 chip->flags |= TPM_CHIP_FLAG_TPM2; 836 tpm_buf_append_u32(&buf, 1);
890 837 rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 0, 0, NULL);
838 /* We ignore TPM return codes on purpose. */
839 if (rc >= 0) {
840 out = (struct tpm_output_header *)buf.data;
841 if (be16_to_cpu(out->tag) == TPM2_ST_NO_SESSIONS)
842 chip->flags |= TPM_CHIP_FLAG_TPM2;
843 }
844 tpm_buf_destroy(&buf);
891 return 0; 845 return 0;
892} 846}
893EXPORT_SYMBOL_GPL(tpm2_probe); 847EXPORT_SYMBOL_GPL(tpm2_probe);
diff --git a/drivers/char/tpm/tpm2-space.c b/drivers/char/tpm/tpm2-space.c
index 6122d3276f72..d2e101b32482 100644
--- a/drivers/char/tpm/tpm2-space.c
+++ b/drivers/char/tpm/tpm2-space.c
@@ -39,7 +39,7 @@ static void tpm2_flush_sessions(struct tpm_chip *chip, struct tpm_space *space)
39 for (i = 0; i < ARRAY_SIZE(space->session_tbl); i++) { 39 for (i = 0; i < ARRAY_SIZE(space->session_tbl); i++) {
40 if (space->session_tbl[i]) 40 if (space->session_tbl[i])
41 tpm2_flush_context_cmd(chip, space->session_tbl[i], 41 tpm2_flush_context_cmd(chip, space->session_tbl[i],
42 TPM_TRANSMIT_UNLOCKED); 42 TPM_TRANSMIT_NESTED);
43 } 43 }
44} 44}
45 45
@@ -84,7 +84,7 @@ static int tpm2_load_context(struct tpm_chip *chip, u8 *buf,
84 tpm_buf_append(&tbuf, &buf[*offset], body_size); 84 tpm_buf_append(&tbuf, &buf[*offset], body_size);
85 85
86 rc = tpm_transmit_cmd(chip, NULL, tbuf.data, PAGE_SIZE, 4, 86 rc = tpm_transmit_cmd(chip, NULL, tbuf.data, PAGE_SIZE, 4,
87 TPM_TRANSMIT_UNLOCKED, NULL); 87 TPM_TRANSMIT_NESTED, NULL);
88 if (rc < 0) { 88 if (rc < 0) {
89 dev_warn(&chip->dev, "%s: failed with a system error %d\n", 89 dev_warn(&chip->dev, "%s: failed with a system error %d\n",
90 __func__, rc); 90 __func__, rc);
@@ -133,7 +133,7 @@ static int tpm2_save_context(struct tpm_chip *chip, u32 handle, u8 *buf,
133 tpm_buf_append_u32(&tbuf, handle); 133 tpm_buf_append_u32(&tbuf, handle);
134 134
135 rc = tpm_transmit_cmd(chip, NULL, tbuf.data, PAGE_SIZE, 0, 135 rc = tpm_transmit_cmd(chip, NULL, tbuf.data, PAGE_SIZE, 0,
136 TPM_TRANSMIT_UNLOCKED, NULL); 136 TPM_TRANSMIT_NESTED, NULL);
137 if (rc < 0) { 137 if (rc < 0) {
138 dev_warn(&chip->dev, "%s: failed with a system error %d\n", 138 dev_warn(&chip->dev, "%s: failed with a system error %d\n",
139 __func__, rc); 139 __func__, rc);
@@ -170,7 +170,7 @@ static void tpm2_flush_space(struct tpm_chip *chip)
170 for (i = 0; i < ARRAY_SIZE(space->context_tbl); i++) 170 for (i = 0; i < ARRAY_SIZE(space->context_tbl); i++)
171 if (space->context_tbl[i] && ~space->context_tbl[i]) 171 if (space->context_tbl[i] && ~space->context_tbl[i])
172 tpm2_flush_context_cmd(chip, space->context_tbl[i], 172 tpm2_flush_context_cmd(chip, space->context_tbl[i],
173 TPM_TRANSMIT_UNLOCKED); 173 TPM_TRANSMIT_NESTED);
174 174
175 tpm2_flush_sessions(chip, space); 175 tpm2_flush_sessions(chip, space);
176} 176}
@@ -377,7 +377,7 @@ static int tpm2_map_response_header(struct tpm_chip *chip, u32 cc, u8 *rsp,
377 377
378 return 0; 378 return 0;
379out_no_slots: 379out_no_slots:
380 tpm2_flush_context_cmd(chip, phandle, TPM_TRANSMIT_UNLOCKED); 380 tpm2_flush_context_cmd(chip, phandle, TPM_TRANSMIT_NESTED);
381 dev_warn(&chip->dev, "%s: out of slots for 0x%08X\n", __func__, 381 dev_warn(&chip->dev, "%s: out of slots for 0x%08X\n", __func__,
382 phandle); 382 phandle);
383 return -ENOMEM; 383 return -ENOMEM;
@@ -465,7 +465,7 @@ static int tpm2_save_space(struct tpm_chip *chip)
465 return rc; 465 return rc;
466 466
467 tpm2_flush_context_cmd(chip, space->context_tbl[i], 467 tpm2_flush_context_cmd(chip, space->context_tbl[i],
468 TPM_TRANSMIT_UNLOCKED); 468 TPM_TRANSMIT_NESTED);
469 space->context_tbl[i] = ~0; 469 space->context_tbl[i] = ~0;
470 } 470 }
471 471
diff --git a/drivers/char/tpm/tpm_crb.c b/drivers/char/tpm/tpm_crb.c
index 34fbc6cb097b..36952ef98f90 100644
--- a/drivers/char/tpm/tpm_crb.c
+++ b/drivers/char/tpm/tpm_crb.c
@@ -132,7 +132,7 @@ static bool crb_wait_for_reg_32(u32 __iomem *reg, u32 mask, u32 value,
132} 132}
133 133
134/** 134/**
135 * crb_go_idle - request tpm crb device to go the idle state 135 * __crb_go_idle - request tpm crb device to go the idle state
136 * 136 *
137 * @dev: crb device 137 * @dev: crb device
138 * @priv: crb private data 138 * @priv: crb private data
@@ -147,7 +147,7 @@ static bool crb_wait_for_reg_32(u32 __iomem *reg, u32 mask, u32 value,
147 * 147 *
148 * Return: 0 always 148 * Return: 0 always
149 */ 149 */
150static int crb_go_idle(struct device *dev, struct crb_priv *priv) 150static int __crb_go_idle(struct device *dev, struct crb_priv *priv)
151{ 151{
152 if ((priv->sm == ACPI_TPM2_START_METHOD) || 152 if ((priv->sm == ACPI_TPM2_START_METHOD) ||
153 (priv->sm == ACPI_TPM2_COMMAND_BUFFER_WITH_START_METHOD) || 153 (priv->sm == ACPI_TPM2_COMMAND_BUFFER_WITH_START_METHOD) ||
@@ -163,11 +163,20 @@ static int crb_go_idle(struct device *dev, struct crb_priv *priv)
163 dev_warn(dev, "goIdle timed out\n"); 163 dev_warn(dev, "goIdle timed out\n");
164 return -ETIME; 164 return -ETIME;
165 } 165 }
166
166 return 0; 167 return 0;
167} 168}
168 169
170static int crb_go_idle(struct tpm_chip *chip)
171{
172 struct device *dev = &chip->dev;
173 struct crb_priv *priv = dev_get_drvdata(dev);
174
175 return __crb_go_idle(dev, priv);
176}
177
169/** 178/**
170 * crb_cmd_ready - request tpm crb device to enter ready state 179 * __crb_cmd_ready - request tpm crb device to enter ready state
171 * 180 *
172 * @dev: crb device 181 * @dev: crb device
173 * @priv: crb private data 182 * @priv: crb private data
@@ -181,7 +190,7 @@ static int crb_go_idle(struct device *dev, struct crb_priv *priv)
181 * 190 *
182 * Return: 0 on success -ETIME on timeout; 191 * Return: 0 on success -ETIME on timeout;
183 */ 192 */
184static int crb_cmd_ready(struct device *dev, struct crb_priv *priv) 193static int __crb_cmd_ready(struct device *dev, struct crb_priv *priv)
185{ 194{
186 if ((priv->sm == ACPI_TPM2_START_METHOD) || 195 if ((priv->sm == ACPI_TPM2_START_METHOD) ||
187 (priv->sm == ACPI_TPM2_COMMAND_BUFFER_WITH_START_METHOD) || 196 (priv->sm == ACPI_TPM2_COMMAND_BUFFER_WITH_START_METHOD) ||
@@ -200,6 +209,14 @@ static int crb_cmd_ready(struct device *dev, struct crb_priv *priv)
200 return 0; 209 return 0;
201} 210}
202 211
212static int crb_cmd_ready(struct tpm_chip *chip)
213{
214 struct device *dev = &chip->dev;
215 struct crb_priv *priv = dev_get_drvdata(dev);
216
217 return __crb_cmd_ready(dev, priv);
218}
219
203static int __crb_request_locality(struct device *dev, 220static int __crb_request_locality(struct device *dev,
204 struct crb_priv *priv, int loc) 221 struct crb_priv *priv, int loc)
205{ 222{
@@ -401,6 +418,8 @@ static const struct tpm_class_ops tpm_crb = {
401 .send = crb_send, 418 .send = crb_send,
402 .cancel = crb_cancel, 419 .cancel = crb_cancel,
403 .req_canceled = crb_req_canceled, 420 .req_canceled = crb_req_canceled,
421 .go_idle = crb_go_idle,
422 .cmd_ready = crb_cmd_ready,
404 .request_locality = crb_request_locality, 423 .request_locality = crb_request_locality,
405 .relinquish_locality = crb_relinquish_locality, 424 .relinquish_locality = crb_relinquish_locality,
406 .req_complete_mask = CRB_DRV_STS_COMPLETE, 425 .req_complete_mask = CRB_DRV_STS_COMPLETE,
@@ -520,7 +539,7 @@ static int crb_map_io(struct acpi_device *device, struct crb_priv *priv,
520 * PTT HW bug w/a: wake up the device to access 539 * PTT HW bug w/a: wake up the device to access
521 * possibly not retained registers. 540 * possibly not retained registers.
522 */ 541 */
523 ret = crb_cmd_ready(dev, priv); 542 ret = __crb_cmd_ready(dev, priv);
524 if (ret) 543 if (ret)
525 goto out_relinquish_locality; 544 goto out_relinquish_locality;
526 545
@@ -565,7 +584,7 @@ out:
565 if (!ret) 584 if (!ret)
566 priv->cmd_size = cmd_size; 585 priv->cmd_size = cmd_size;
567 586
568 crb_go_idle(dev, priv); 587 __crb_go_idle(dev, priv);
569 588
570out_relinquish_locality: 589out_relinquish_locality:
571 590
@@ -628,32 +647,7 @@ static int crb_acpi_add(struct acpi_device *device)
628 chip->acpi_dev_handle = device->handle; 647 chip->acpi_dev_handle = device->handle;
629 chip->flags = TPM_CHIP_FLAG_TPM2; 648 chip->flags = TPM_CHIP_FLAG_TPM2;
630 649
631 rc = __crb_request_locality(dev, priv, 0); 650 return tpm_chip_register(chip);
632 if (rc)
633 return rc;
634
635 rc = crb_cmd_ready(dev, priv);
636 if (rc)
637 goto out;
638
639 pm_runtime_get_noresume(dev);
640 pm_runtime_set_active(dev);
641 pm_runtime_enable(dev);
642
643 rc = tpm_chip_register(chip);
644 if (rc) {
645 crb_go_idle(dev, priv);
646 pm_runtime_put_noidle(dev);
647 pm_runtime_disable(dev);
648 goto out;
649 }
650
651 pm_runtime_put_sync(dev);
652
653out:
654 __crb_relinquish_locality(dev, priv, 0);
655
656 return rc;
657} 651}
658 652
659static int crb_acpi_remove(struct acpi_device *device) 653static int crb_acpi_remove(struct acpi_device *device)
@@ -663,52 +657,11 @@ static int crb_acpi_remove(struct acpi_device *device)
663 657
664 tpm_chip_unregister(chip); 658 tpm_chip_unregister(chip);
665 659
666 pm_runtime_disable(dev);
667
668 return 0; 660 return 0;
669} 661}
670 662
671static int __maybe_unused crb_pm_runtime_suspend(struct device *dev)
672{
673 struct tpm_chip *chip = dev_get_drvdata(dev);
674 struct crb_priv *priv = dev_get_drvdata(&chip->dev);
675
676 return crb_go_idle(dev, priv);
677}
678
679static int __maybe_unused crb_pm_runtime_resume(struct device *dev)
680{
681 struct tpm_chip *chip = dev_get_drvdata(dev);
682 struct crb_priv *priv = dev_get_drvdata(&chip->dev);
683
684 return crb_cmd_ready(dev, priv);
685}
686
687static int __maybe_unused crb_pm_suspend(struct device *dev)
688{
689 int ret;
690
691 ret = tpm_pm_suspend(dev);
692 if (ret)
693 return ret;
694
695 return crb_pm_runtime_suspend(dev);
696}
697
698static int __maybe_unused crb_pm_resume(struct device *dev)
699{
700 int ret;
701
702 ret = crb_pm_runtime_resume(dev);
703 if (ret)
704 return ret;
705
706 return tpm_pm_resume(dev);
707}
708
709static const struct dev_pm_ops crb_pm = { 663static const struct dev_pm_ops crb_pm = {
710 SET_SYSTEM_SLEEP_PM_OPS(crb_pm_suspend, crb_pm_resume) 664 SET_SYSTEM_SLEEP_PM_OPS(tpm_pm_suspend, tpm_pm_resume)
711 SET_RUNTIME_PM_OPS(crb_pm_runtime_suspend, crb_pm_runtime_resume, NULL)
712}; 665};
713 666
714static const struct acpi_device_id crb_device_ids[] = { 667static const struct acpi_device_id crb_device_ids[] = {
diff --git a/drivers/char/tpm/tpm_i2c_infineon.c b/drivers/char/tpm/tpm_i2c_infineon.c
index 6116cd05e228..9086edc9066b 100644
--- a/drivers/char/tpm/tpm_i2c_infineon.c
+++ b/drivers/char/tpm/tpm_i2c_infineon.c
@@ -117,7 +117,7 @@ static int iic_tpm_read(u8 addr, u8 *buffer, size_t len)
117 /* Lock the adapter for the duration of the whole sequence. */ 117 /* Lock the adapter for the duration of the whole sequence. */
118 if (!tpm_dev.client->adapter->algo->master_xfer) 118 if (!tpm_dev.client->adapter->algo->master_xfer)
119 return -EOPNOTSUPP; 119 return -EOPNOTSUPP;
120 i2c_lock_adapter(tpm_dev.client->adapter); 120 i2c_lock_bus(tpm_dev.client->adapter, I2C_LOCK_SEGMENT);
121 121
122 if (tpm_dev.chip_type == SLB9645) { 122 if (tpm_dev.chip_type == SLB9645) {
123 /* use a combined read for newer chips 123 /* use a combined read for newer chips
@@ -192,7 +192,7 @@ static int iic_tpm_read(u8 addr, u8 *buffer, size_t len)
192 } 192 }
193 193
194out: 194out:
195 i2c_unlock_adapter(tpm_dev.client->adapter); 195 i2c_unlock_bus(tpm_dev.client->adapter, I2C_LOCK_SEGMENT);
196 /* take care of 'guard time' */ 196 /* take care of 'guard time' */
197 usleep_range(SLEEP_DURATION_LOW, SLEEP_DURATION_HI); 197 usleep_range(SLEEP_DURATION_LOW, SLEEP_DURATION_HI);
198 198
@@ -224,7 +224,7 @@ static int iic_tpm_write_generic(u8 addr, u8 *buffer, size_t len,
224 224
225 if (!tpm_dev.client->adapter->algo->master_xfer) 225 if (!tpm_dev.client->adapter->algo->master_xfer)
226 return -EOPNOTSUPP; 226 return -EOPNOTSUPP;
227 i2c_lock_adapter(tpm_dev.client->adapter); 227 i2c_lock_bus(tpm_dev.client->adapter, I2C_LOCK_SEGMENT);
228 228
229 /* prepend the 'register address' to the buffer */ 229 /* prepend the 'register address' to the buffer */
230 tpm_dev.buf[0] = addr; 230 tpm_dev.buf[0] = addr;
@@ -243,7 +243,7 @@ static int iic_tpm_write_generic(u8 addr, u8 *buffer, size_t len,
243 usleep_range(sleep_low, sleep_hi); 243 usleep_range(sleep_low, sleep_hi);
244 } 244 }
245 245
246 i2c_unlock_adapter(tpm_dev.client->adapter); 246 i2c_unlock_bus(tpm_dev.client->adapter, I2C_LOCK_SEGMENT);
247 /* take care of 'guard time' */ 247 /* take care of 'guard time' */
248 usleep_range(SLEEP_DURATION_LOW, SLEEP_DURATION_HI); 248 usleep_range(SLEEP_DURATION_LOW, SLEEP_DURATION_HI);
249 249
diff --git a/drivers/char/tpm/tpm_tis_core.c b/drivers/char/tpm/tpm_tis_core.c
index 8b46aaa9e049..d2345d9fd7b5 100644
--- a/drivers/char/tpm/tpm_tis_core.c
+++ b/drivers/char/tpm/tpm_tis_core.c
@@ -875,6 +875,8 @@ int tpm_tis_core_init(struct device *dev, struct tpm_tis_data *priv, int irq,
875 chip->acpi_dev_handle = acpi_dev_handle; 875 chip->acpi_dev_handle = acpi_dev_handle;
876#endif 876#endif
877 877
878 chip->hwrng.quality = priv->rng_quality;
879
878 /* Maximum timeouts */ 880 /* Maximum timeouts */
879 chip->timeout_a = msecs_to_jiffies(TIS_TIMEOUT_A_MAX); 881 chip->timeout_a = msecs_to_jiffies(TIS_TIMEOUT_A_MAX);
880 chip->timeout_b = msecs_to_jiffies(TIS_TIMEOUT_B_MAX); 882 chip->timeout_b = msecs_to_jiffies(TIS_TIMEOUT_B_MAX);
diff --git a/drivers/char/tpm/tpm_tis_core.h b/drivers/char/tpm/tpm_tis_core.h
index f6e1dbe212a7..f48125f1e6e0 100644
--- a/drivers/char/tpm/tpm_tis_core.h
+++ b/drivers/char/tpm/tpm_tis_core.h
@@ -99,6 +99,7 @@ struct tpm_tis_data {
99 wait_queue_head_t int_queue; 99 wait_queue_head_t int_queue;
100 wait_queue_head_t read_queue; 100 wait_queue_head_t read_queue;
101 const struct tpm_tis_phy_ops *phy_ops; 101 const struct tpm_tis_phy_ops *phy_ops;
102 unsigned short rng_quality;
102}; 103};
103 104
104struct tpm_tis_phy_ops { 105struct tpm_tis_phy_ops {
diff --git a/drivers/char/tpm/tpm_tis_spi.c b/drivers/char/tpm/tpm_tis_spi.c
index 424ff2fde1f2..9914f6973463 100644
--- a/drivers/char/tpm/tpm_tis_spi.c
+++ b/drivers/char/tpm/tpm_tis_spi.c
@@ -199,6 +199,7 @@ static const struct tpm_tis_phy_ops tpm_spi_phy_ops = {
199static int tpm_tis_spi_probe(struct spi_device *dev) 199static int tpm_tis_spi_probe(struct spi_device *dev)
200{ 200{
201 struct tpm_tis_spi_phy *phy; 201 struct tpm_tis_spi_phy *phy;
202 int irq;
202 203
203 phy = devm_kzalloc(&dev->dev, sizeof(struct tpm_tis_spi_phy), 204 phy = devm_kzalloc(&dev->dev, sizeof(struct tpm_tis_spi_phy),
204 GFP_KERNEL); 205 GFP_KERNEL);
@@ -211,7 +212,13 @@ static int tpm_tis_spi_probe(struct spi_device *dev)
211 if (!phy->iobuf) 212 if (!phy->iobuf)
212 return -ENOMEM; 213 return -ENOMEM;
213 214
214 return tpm_tis_core_init(&dev->dev, &phy->priv, -1, &tpm_spi_phy_ops, 215 /* If the SPI device has an IRQ then use that */
216 if (dev->irq > 0)
217 irq = dev->irq;
218 else
219 irq = -1;
220
221 return tpm_tis_core_init(&dev->dev, &phy->priv, irq, &tpm_spi_phy_ops,
215 NULL); 222 NULL);
216} 223}
217 224
diff --git a/drivers/char/tpm/tpm_vtpm_proxy.c b/drivers/char/tpm/tpm_vtpm_proxy.c
index e4f79f920450..87a0ce47f201 100644
--- a/drivers/char/tpm/tpm_vtpm_proxy.c
+++ b/drivers/char/tpm/tpm_vtpm_proxy.c
@@ -418,7 +418,7 @@ static int vtpm_proxy_request_locality(struct tpm_chip *chip, int locality)
418 proxy_dev->state |= STATE_DRIVER_COMMAND; 418 proxy_dev->state |= STATE_DRIVER_COMMAND;
419 419
420 rc = tpm_transmit_cmd(chip, NULL, buf.data, tpm_buf_length(&buf), 0, 420 rc = tpm_transmit_cmd(chip, NULL, buf.data, tpm_buf_length(&buf), 0,
421 TPM_TRANSMIT_UNLOCKED | TPM_TRANSMIT_RAW, 421 TPM_TRANSMIT_NESTED,
422 "attempting to set locality"); 422 "attempting to set locality");
423 423
424 proxy_dev->state &= ~STATE_DRIVER_COMMAND; 424 proxy_dev->state &= ~STATE_DRIVER_COMMAND;
diff --git a/include/linux/tpm.h b/include/linux/tpm.h
index 06639fb6ab85..4609b94142d4 100644
--- a/include/linux/tpm.h
+++ b/include/linux/tpm.h
@@ -43,6 +43,8 @@ struct tpm_class_ops {
43 u8 (*status) (struct tpm_chip *chip); 43 u8 (*status) (struct tpm_chip *chip);
44 bool (*update_timeouts)(struct tpm_chip *chip, 44 bool (*update_timeouts)(struct tpm_chip *chip,
45 unsigned long *timeout_cap); 45 unsigned long *timeout_cap);
46 int (*go_idle)(struct tpm_chip *chip);
47 int (*cmd_ready)(struct tpm_chip *chip);
46 int (*request_locality)(struct tpm_chip *chip, int loc); 48 int (*request_locality)(struct tpm_chip *chip, int loc);
47 int (*relinquish_locality)(struct tpm_chip *chip, int loc); 49 int (*relinquish_locality)(struct tpm_chip *chip, int loc);
48 void (*clk_enable)(struct tpm_chip *chip, bool value); 50 void (*clk_enable)(struct tpm_chip *chip, bool value);
@@ -61,6 +63,7 @@ extern int tpm_seal_trusted(struct tpm_chip *chip,
61extern int tpm_unseal_trusted(struct tpm_chip *chip, 63extern int tpm_unseal_trusted(struct tpm_chip *chip,
62 struct trusted_key_payload *payload, 64 struct trusted_key_payload *payload,
63 struct trusted_key_options *options); 65 struct trusted_key_options *options);
66extern struct tpm_chip *tpm_default_chip(void);
64#else 67#else
65static inline int tpm_is_tpm2(struct tpm_chip *chip) 68static inline int tpm_is_tpm2(struct tpm_chip *chip)
66{ 69{
@@ -96,5 +99,9 @@ static inline int tpm_unseal_trusted(struct tpm_chip *chip,
96{ 99{
97 return -ENODEV; 100 return -ENODEV;
98} 101}
102static inline struct tpm_chip *tpm_default_chip(void)
103{
104 return NULL;
105}
99#endif 106#endif
100#endif 107#endif
diff --git a/security/integrity/ima/ima.h b/security/integrity/ima/ima.h
index 78c15264b17b..588e4813370c 100644
--- a/security/integrity/ima/ima.h
+++ b/security/integrity/ima/ima.h
@@ -53,9 +53,9 @@ enum tpm_pcrs { TPM_PCR0 = 0, TPM_PCR8 = 8 };
53extern int ima_policy_flag; 53extern int ima_policy_flag;
54 54
55/* set during initialization */ 55/* set during initialization */
56extern int ima_used_chip;
57extern int ima_hash_algo; 56extern int ima_hash_algo;
58extern int ima_appraise; 57extern int ima_appraise;
58extern struct tpm_chip *ima_tpm_chip;
59 59
60/* IMA event related data */ 60/* IMA event related data */
61struct ima_event_data { 61struct ima_event_data {
diff --git a/security/integrity/ima/ima_crypto.c b/security/integrity/ima/ima_crypto.c
index 4e085a17124f..7e7e7e7c250a 100644
--- a/security/integrity/ima/ima_crypto.c
+++ b/security/integrity/ima/ima_crypto.c
@@ -631,10 +631,10 @@ int ima_calc_buffer_hash(const void *buf, loff_t len,
631 631
632static void __init ima_pcrread(int idx, u8 *pcr) 632static void __init ima_pcrread(int idx, u8 *pcr)
633{ 633{
634 if (!ima_used_chip) 634 if (!ima_tpm_chip)
635 return; 635 return;
636 636
637 if (tpm_pcr_read(NULL, idx, pcr) != 0) 637 if (tpm_pcr_read(ima_tpm_chip, idx, pcr) != 0)
638 pr_err("Error Communicating to TPM chip\n"); 638 pr_err("Error Communicating to TPM chip\n");
639} 639}
640 640
diff --git a/security/integrity/ima/ima_init.c b/security/integrity/ima/ima_init.c
index 29b72cd2502e..faac9ecaa0ae 100644
--- a/security/integrity/ima/ima_init.c
+++ b/security/integrity/ima/ima_init.c
@@ -26,7 +26,7 @@
26 26
27/* name for boot aggregate entry */ 27/* name for boot aggregate entry */
28static const char *boot_aggregate_name = "boot_aggregate"; 28static const char *boot_aggregate_name = "boot_aggregate";
29int ima_used_chip; 29struct tpm_chip *ima_tpm_chip;
30 30
31/* Add the boot aggregate to the IMA measurement list and extend 31/* Add the boot aggregate to the IMA measurement list and extend
32 * the PCR register. 32 * the PCR register.
@@ -64,7 +64,7 @@ static int __init ima_add_boot_aggregate(void)
64 iint->ima_hash->algo = HASH_ALGO_SHA1; 64 iint->ima_hash->algo = HASH_ALGO_SHA1;
65 iint->ima_hash->length = SHA1_DIGEST_SIZE; 65 iint->ima_hash->length = SHA1_DIGEST_SIZE;
66 66
67 if (ima_used_chip) { 67 if (ima_tpm_chip) {
68 result = ima_calc_boot_aggregate(&hash.hdr); 68 result = ima_calc_boot_aggregate(&hash.hdr);
69 if (result < 0) { 69 if (result < 0) {
70 audit_cause = "hashing_error"; 70 audit_cause = "hashing_error";
@@ -106,17 +106,11 @@ void __init ima_load_x509(void)
106 106
107int __init ima_init(void) 107int __init ima_init(void)
108{ 108{
109 u8 pcr_i[TPM_DIGEST_SIZE];
110 int rc; 109 int rc;
111 110
112 ima_used_chip = 0; 111 ima_tpm_chip = tpm_default_chip();
113 rc = tpm_pcr_read(NULL, 0, pcr_i); 112 if (!ima_tpm_chip)
114 if (rc == 0) 113 pr_info("No TPM chip found, activating TPM-bypass!\n");
115 ima_used_chip = 1;
116
117 if (!ima_used_chip)
118 pr_info("No TPM chip found, activating TPM-bypass! (rc=%d)\n",
119 rc);
120 114
121 rc = integrity_init_keyring(INTEGRITY_KEYRING_IMA); 115 rc = integrity_init_keyring(INTEGRITY_KEYRING_IMA);
122 if (rc) 116 if (rc)
diff --git a/security/integrity/ima/ima_queue.c b/security/integrity/ima/ima_queue.c
index 418f35e38015..b186819bd5aa 100644
--- a/security/integrity/ima/ima_queue.c
+++ b/security/integrity/ima/ima_queue.c
@@ -142,10 +142,10 @@ static int ima_pcr_extend(const u8 *hash, int pcr)
142{ 142{
143 int result = 0; 143 int result = 0;
144 144
145 if (!ima_used_chip) 145 if (!ima_tpm_chip)
146 return result; 146 return result;
147 147
148 result = tpm_pcr_extend(NULL, pcr, hash); 148 result = tpm_pcr_extend(ima_tpm_chip, pcr, hash);
149 if (result != 0) 149 if (result != 0)
150 pr_err("Error Communicating to TPM chip, result: %d\n", result); 150 pr_err("Error Communicating to TPM chip, result: %d\n", result);
151 return result; 151 return result;