1b886d83cSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 29deb0eb7SJason Gunthorpe /* 39deb0eb7SJason Gunthorpe * Copyright (C) 2004 IBM Corporation 4afb5abc2SJarkko Sakkinen * Copyright (C) 2014 Intel Corporation 59deb0eb7SJason Gunthorpe * 69deb0eb7SJason Gunthorpe * Authors: 79deb0eb7SJason Gunthorpe * Leendert van Doorn <leendert@watson.ibm.com> 89deb0eb7SJason Gunthorpe * Dave Safford <safford@watson.ibm.com> 99deb0eb7SJason Gunthorpe * Reiner Sailer <sailer@watson.ibm.com> 109deb0eb7SJason Gunthorpe * Kylene Hall <kjhall@us.ibm.com> 119deb0eb7SJason Gunthorpe * 129deb0eb7SJason Gunthorpe * Maintained by: <tpmdd-devel@lists.sourceforge.net> 139deb0eb7SJason Gunthorpe * 149deb0eb7SJason Gunthorpe * Device driver for TCG/TCPA TPM (trusted platform module). 159deb0eb7SJason Gunthorpe * Specifications at www.trustedcomputinggroup.org 169deb0eb7SJason Gunthorpe * 179deb0eb7SJason Gunthorpe * Note, the TPM chip is not interrupt driven (only polling) 189deb0eb7SJason Gunthorpe * and can have very long timeouts (minutes!). Hence the unusual 199deb0eb7SJason Gunthorpe * calls to msleep. 209deb0eb7SJason Gunthorpe */ 219deb0eb7SJason Gunthorpe 229deb0eb7SJason Gunthorpe #include <linux/poll.h> 239deb0eb7SJason Gunthorpe #include <linux/slab.h> 249deb0eb7SJason Gunthorpe #include <linux/mutex.h> 259deb0eb7SJason Gunthorpe #include <linux/spinlock.h> 269deb0eb7SJason Gunthorpe #include <linux/freezer.h> 27fd3ec366SThiebaud Weksteen #include <linux/tpm_eventlog.h> 289deb0eb7SJason Gunthorpe 299deb0eb7SJason Gunthorpe #include "tpm.h" 309deb0eb7SJason Gunthorpe 319deb0eb7SJason Gunthorpe /* 329deb0eb7SJason Gunthorpe * Bug workaround - some TPM's don't flush the most 339deb0eb7SJason Gunthorpe * recently changed pcr on suspend, so force the flush 349deb0eb7SJason Gunthorpe * with an extend to the selected _unused_ non-volatile pcr. 359deb0eb7SJason Gunthorpe */ 3695adc6b4STomas Winkler static u32 tpm_suspend_pcr; 379deb0eb7SJason Gunthorpe module_param_named(suspend_pcr, tpm_suspend_pcr, uint, 0644); 389deb0eb7SJason Gunthorpe MODULE_PARM_DESC(suspend_pcr, 3939f5712bSDmitry Torokhov "PCR to use for dummy writes to facilitate flush on suspend."); 409deb0eb7SJason Gunthorpe 41d856c00fSTomas Winkler /** 42d856c00fSTomas Winkler * tpm_calc_ordinal_duration() - calculate the maximum command duration 43d856c00fSTomas Winkler * @chip: TPM chip to use. 44d856c00fSTomas Winkler * @ordinal: TPM command ordinal. 45d856c00fSTomas Winkler * 46d856c00fSTomas Winkler * The function returns the maximum amount of time the chip could take 47d856c00fSTomas Winkler * to return the result for a particular ordinal in jiffies. 48d856c00fSTomas Winkler * 49d856c00fSTomas Winkler * Return: A maximal duration time for an ordinal in jiffies. 50d856c00fSTomas Winkler */ 51d856c00fSTomas Winkler unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal) 52d856c00fSTomas Winkler { 53d856c00fSTomas Winkler if (chip->flags & TPM_CHIP_FLAG_TPM2) 54d856c00fSTomas Winkler return tpm2_calc_ordinal_duration(chip, ordinal); 55d856c00fSTomas Winkler else 56d856c00fSTomas Winkler return tpm1_calc_ordinal_duration(chip, ordinal); 57d856c00fSTomas Winkler } 58d856c00fSTomas Winkler EXPORT_SYMBOL_GPL(tpm_calc_ordinal_duration); 59d856c00fSTomas Winkler 6047a6c28bSJarkko Sakkinen static ssize_t tpm_try_transmit(struct tpm_chip *chip, void *buf, size_t bufsiz) 619deb0eb7SJason Gunthorpe { 62b34b77a9SJarkko Sakkinen struct tpm_header *header = buf; 63745b361eSJarkko Sakkinen int rc; 64745b361eSJarkko Sakkinen ssize_t len = 0; 659deb0eb7SJason Gunthorpe u32 count, ordinal; 669deb0eb7SJason Gunthorpe unsigned long stop; 679deb0eb7SJason Gunthorpe 68c3465a37SJarkko Sakkinen if (bufsiz < TPM_HEADER_SIZE) 69c3465a37SJarkko Sakkinen return -EINVAL; 70ebfd7532SJarkko Sakkinen 719deb0eb7SJason Gunthorpe if (bufsiz > TPM_BUFSIZE) 729deb0eb7SJason Gunthorpe bufsiz = TPM_BUFSIZE; 739deb0eb7SJason Gunthorpe 74720b0711SJarkko Sakkinen count = be32_to_cpu(header->length); 75720b0711SJarkko Sakkinen ordinal = be32_to_cpu(header->ordinal); 769deb0eb7SJason Gunthorpe if (count == 0) 779deb0eb7SJason Gunthorpe return -ENODATA; 789deb0eb7SJason Gunthorpe if (count > bufsiz) { 798cfffc9dSJason Gunthorpe dev_err(&chip->dev, 809deb0eb7SJason Gunthorpe "invalid count value %x %zx\n", count, bufsiz); 819deb0eb7SJason Gunthorpe return -E2BIG; 829deb0eb7SJason Gunthorpe } 839deb0eb7SJason Gunthorpe 8462c09e12SWinkler, Tomas rc = chip->ops->send(chip, buf, count); 859deb0eb7SJason Gunthorpe if (rc < 0) { 86402149c6SStefan Berger if (rc != -EPIPE) 878cfffc9dSJason Gunthorpe dev_err(&chip->dev, 88f5595f5bSJarkko Sakkinen "%s: send(): error %d\n", __func__, rc); 8929b47ce9SJarkko Sakkinen return rc; 909deb0eb7SJason Gunthorpe } 919deb0eb7SJason Gunthorpe 92f5595f5bSJarkko Sakkinen /* A sanity check. send() should just return zero on success e.g. 93f5595f5bSJarkko Sakkinen * not the command length. 94f5595f5bSJarkko Sakkinen */ 95f5595f5bSJarkko Sakkinen if (rc > 0) { 96f5595f5bSJarkko Sakkinen dev_warn(&chip->dev, 97f5595f5bSJarkko Sakkinen "%s: send(): invalid value %d\n", __func__, rc); 98f5595f5bSJarkko Sakkinen rc = 0; 99f5595f5bSJarkko Sakkinen } 100f5595f5bSJarkko Sakkinen 101570a3609SChristophe Ricard if (chip->flags & TPM_CHIP_FLAG_IRQ) 1029deb0eb7SJason Gunthorpe goto out_recv; 1039deb0eb7SJason Gunthorpe 104d856c00fSTomas Winkler stop = jiffies + tpm_calc_ordinal_duration(chip, ordinal); 1059deb0eb7SJason Gunthorpe do { 1065f82e9f0SJason Gunthorpe u8 status = chip->ops->status(chip); 1075f82e9f0SJason Gunthorpe if ((status & chip->ops->req_complete_mask) == 1085f82e9f0SJason Gunthorpe chip->ops->req_complete_val) 1099deb0eb7SJason Gunthorpe goto out_recv; 1109deb0eb7SJason Gunthorpe 1115f82e9f0SJason Gunthorpe if (chip->ops->req_canceled(chip, status)) { 1128cfffc9dSJason Gunthorpe dev_err(&chip->dev, "Operation Canceled\n"); 11329b47ce9SJarkko Sakkinen return -ECANCELED; 1149deb0eb7SJason Gunthorpe } 1159deb0eb7SJason Gunthorpe 11659f5a6b0SNayna Jain tpm_msleep(TPM_TIMEOUT_POLL); 1179deb0eb7SJason Gunthorpe rmb(); 1189deb0eb7SJason Gunthorpe } while (time_before(jiffies, stop)); 1199deb0eb7SJason Gunthorpe 1205f82e9f0SJason Gunthorpe chip->ops->cancel(chip); 1218cfffc9dSJason Gunthorpe dev_err(&chip->dev, "Operation Timed out\n"); 12229b47ce9SJarkko Sakkinen return -ETIME; 1239deb0eb7SJason Gunthorpe 1249deb0eb7SJason Gunthorpe out_recv: 12562c09e12SWinkler, Tomas len = chip->ops->recv(chip, buf, bufsiz); 126745b361eSJarkko Sakkinen if (len < 0) { 127745b361eSJarkko Sakkinen rc = len; 128304ff672SJarkko Sakkinen dev_err(&chip->dev, "tpm_transmit: tpm_recv: error %d\n", rc); 129304ff672SJarkko Sakkinen } else if (len < TPM_HEADER_SIZE || len != be32_to_cpu(header->length)) 130a147918eSJarkko Sakkinen rc = -EFAULT; 131a147918eSJarkko Sakkinen 132745b361eSJarkko Sakkinen return rc ? rc : len; 1339deb0eb7SJason Gunthorpe } 1349deb0eb7SJason Gunthorpe 135f865c196SWinkler, Tomas /** 136e2fb992dSJames Bottomley * tpm_transmit - Internal kernel interface to transmit TPM commands. 137412eb585SJarkko Sakkinen * @chip: a TPM chip to use 138412eb585SJarkko Sakkinen * @buf: a TPM command buffer 139e2fb992dSJames Bottomley * @bufsiz: length of the TPM command buffer 140e2fb992dSJames Bottomley * 141412eb585SJarkko Sakkinen * A wrapper around tpm_try_transmit() that handles TPM2_RC_RETRY returns from 142412eb585SJarkko Sakkinen * the TPM and retransmits the command after a delay up to a maximum wait of 143412eb585SJarkko Sakkinen * TPM2_DURATION_LONG. 144e2fb992dSJames Bottomley * 145412eb585SJarkko Sakkinen * Note that TPM 1.x never returns TPM2_RC_RETRY so the retry logic is TPM 2.0 146412eb585SJarkko Sakkinen * only. 147e2fb992dSJames Bottomley * 148e2fb992dSJames Bottomley * Return: 149412eb585SJarkko Sakkinen * * The response length - OK 150412eb585SJarkko Sakkinen * * -errno - A system error 151e2fb992dSJames Bottomley */ 15247a6c28bSJarkko Sakkinen ssize_t tpm_transmit(struct tpm_chip *chip, u8 *buf, size_t bufsiz) 153e2fb992dSJames Bottomley { 154b34b77a9SJarkko Sakkinen struct tpm_header *header = (struct tpm_header *)buf; 155e2fb992dSJames Bottomley /* space for header and handles */ 156e2fb992dSJames Bottomley u8 save[TPM_HEADER_SIZE + 3*sizeof(u32)]; 157e2fb992dSJames Bottomley unsigned int delay_msec = TPM2_DURATION_SHORT; 158e2fb992dSJames Bottomley u32 rc = 0; 159e2fb992dSJames Bottomley ssize_t ret; 1605faafbabSJarkko Sakkinen const size_t save_size = min(sizeof(save), bufsiz); 1612be8ffedSJames Bottomley /* the command code is where the return code will be */ 1622be8ffedSJames Bottomley u32 cc = be32_to_cpu(header->return_code); 163e2fb992dSJames Bottomley 164e2fb992dSJames Bottomley /* 165e2fb992dSJames Bottomley * Subtlety here: if we have a space, the handles will be 166e2fb992dSJames Bottomley * transformed, so when we restore the header we also have to 167e2fb992dSJames Bottomley * restore the handles. 168e2fb992dSJames Bottomley */ 169e2fb992dSJames Bottomley memcpy(save, buf, save_size); 170e2fb992dSJames Bottomley 171e2fb992dSJames Bottomley for (;;) { 17247a6c28bSJarkko Sakkinen ret = tpm_try_transmit(chip, buf, bufsiz); 173e2fb992dSJames Bottomley if (ret < 0) 174e2fb992dSJames Bottomley break; 175e2fb992dSJames Bottomley rc = be32_to_cpu(header->return_code); 1762be8ffedSJames Bottomley if (rc != TPM2_RC_RETRY && rc != TPM2_RC_TESTING) 1772be8ffedSJames Bottomley break; 1782be8ffedSJames Bottomley /* 1792be8ffedSJames Bottomley * return immediately if self test returns test 1802be8ffedSJames Bottomley * still running to shorten boot time. 1812be8ffedSJames Bottomley */ 1822be8ffedSJames Bottomley if (rc == TPM2_RC_TESTING && cc == TPM2_CC_SELF_TEST) 183e2fb992dSJames Bottomley break; 18492980756SNayna Jain 185e2fb992dSJames Bottomley if (delay_msec > TPM2_DURATION_LONG) { 1862be8ffedSJames Bottomley if (rc == TPM2_RC_RETRY) 1872be8ffedSJames Bottomley dev_err(&chip->dev, "in retry loop\n"); 1882be8ffedSJames Bottomley else 1892be8ffedSJames Bottomley dev_err(&chip->dev, 1902be8ffedSJames Bottomley "self test is still running\n"); 191e2fb992dSJames Bottomley break; 192e2fb992dSJames Bottomley } 193e2fb992dSJames Bottomley tpm_msleep(delay_msec); 19492980756SNayna Jain delay_msec *= 2; 195e2fb992dSJames Bottomley memcpy(buf, save, save_size); 196e2fb992dSJames Bottomley } 197e2fb992dSJames Bottomley return ret; 198e2fb992dSJames Bottomley } 199412eb585SJarkko Sakkinen 200e2fb992dSJames Bottomley /** 20165520d46SWinkler, Tomas * tpm_transmit_cmd - send a tpm command to the device 202412eb585SJarkko Sakkinen * @chip: a TPM chip to use 203412eb585SJarkko Sakkinen * @buf: a TPM command buffer 204c659af78SStefan Berger * @min_rsp_body_length: minimum expected length of response body 205f865c196SWinkler, Tomas * @desc: command description used in the error message 206f865c196SWinkler, Tomas * 207f865c196SWinkler, Tomas * Return: 208412eb585SJarkko Sakkinen * * 0 - OK 209412eb585SJarkko Sakkinen * * -errno - A system error 210412eb585SJarkko Sakkinen * * TPM_RC - A TPM error 211f865c196SWinkler, Tomas */ 2125faafbabSJarkko Sakkinen ssize_t tpm_transmit_cmd(struct tpm_chip *chip, struct tpm_buf *buf, 21347a6c28bSJarkko Sakkinen size_t min_rsp_body_length, const char *desc) 2149deb0eb7SJason Gunthorpe { 215b34b77a9SJarkko Sakkinen const struct tpm_header *header = (struct tpm_header *)buf->data; 2169deb0eb7SJason Gunthorpe int err; 217c659af78SStefan Berger ssize_t len; 2189deb0eb7SJason Gunthorpe 21947a6c28bSJarkko Sakkinen len = tpm_transmit(chip, buf->data, PAGE_SIZE); 2209deb0eb7SJason Gunthorpe if (len < 0) 2219deb0eb7SJason Gunthorpe return len; 22287155b73SJarkko Sakkinen 22387155b73SJarkko Sakkinen err = be32_to_cpu(header->return_code); 2240d6d0d62SJavier Martinez Canillas if (err != 0 && err != TPM_ERR_DISABLED && err != TPM_ERR_DEACTIVATED 22508a8112aSJerry Snitselaar && err != TPM2_RC_TESTING && desc) 2268cfffc9dSJason Gunthorpe dev_err(&chip->dev, "A TPM error (%d) occurred %s\n", err, 22771ed848fSJarkko Sakkinen desc); 228c659af78SStefan Berger if (err) 2299deb0eb7SJason Gunthorpe return err; 230c659af78SStefan Berger 231c659af78SStefan Berger if (len < min_rsp_body_length + TPM_HEADER_SIZE) 232c659af78SStefan Berger return -EFAULT; 233c659af78SStefan Berger 234c659af78SStefan Berger return 0; 2359deb0eb7SJason Gunthorpe } 236be4c9acfSStefan Berger EXPORT_SYMBOL_GPL(tpm_transmit_cmd); 2379deb0eb7SJason Gunthorpe 2389deb0eb7SJason Gunthorpe int tpm_get_timeouts(struct tpm_chip *chip) 2399deb0eb7SJason Gunthorpe { 240d1d253cfSJason Gunthorpe if (chip->flags & TPM_CHIP_FLAG_HAVE_TIMEOUTS) 241d1d253cfSJason Gunthorpe return 0; 242d1d253cfSJason Gunthorpe 24370a3199aSTomas Winkler if (chip->flags & TPM_CHIP_FLAG_TPM2) 24470a3199aSTomas Winkler return tpm2_get_timeouts(chip); 24570a3199aSTomas Winkler else 24670a3199aSTomas Winkler return tpm1_get_timeouts(chip); 2479deb0eb7SJason Gunthorpe } 2489deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_get_timeouts); 2499deb0eb7SJason Gunthorpe 2509deb0eb7SJason Gunthorpe /** 251aad887f6SJarkko Sakkinen * tpm_is_tpm2 - do we a have a TPM2 chip? 252aad887f6SJarkko Sakkinen * @chip: a &struct tpm_chip instance, %NULL for the default chip 253954650efSJarkko Sakkinen * 254aad887f6SJarkko Sakkinen * Return: 255aad887f6SJarkko Sakkinen * 1 if we have a TPM2 chip. 256aad887f6SJarkko Sakkinen * 0 if we don't have a TPM2 chip. 257aad887f6SJarkko Sakkinen * A negative number for system errors (errno). 258954650efSJarkko Sakkinen */ 259aad887f6SJarkko Sakkinen int tpm_is_tpm2(struct tpm_chip *chip) 260954650efSJarkko Sakkinen { 261954650efSJarkko Sakkinen int rc; 262954650efSJarkko Sakkinen 263fc1d52b7SStefan Berger chip = tpm_find_get_ops(chip); 264aad887f6SJarkko Sakkinen if (!chip) 265954650efSJarkko Sakkinen return -ENODEV; 266954650efSJarkko Sakkinen 267954650efSJarkko Sakkinen rc = (chip->flags & TPM_CHIP_FLAG_TPM2) != 0; 268954650efSJarkko Sakkinen 2694e26195fSJason Gunthorpe tpm_put_ops(chip); 270954650efSJarkko Sakkinen 271954650efSJarkko Sakkinen return rc; 272954650efSJarkko Sakkinen } 273954650efSJarkko Sakkinen EXPORT_SYMBOL_GPL(tpm_is_tpm2); 274954650efSJarkko Sakkinen 275954650efSJarkko Sakkinen /** 276aad887f6SJarkko Sakkinen * tpm_pcr_read - read a PCR value from SHA1 bank 277aad887f6SJarkko Sakkinen * @chip: a &struct tpm_chip instance, %NULL for the default chip 278aad887f6SJarkko Sakkinen * @pcr_idx: the PCR to be retrieved 279879b5892SRoberto Sassu * @digest: the PCR bank and buffer current PCR value is written to 2809deb0eb7SJason Gunthorpe * 281aad887f6SJarkko Sakkinen * Return: same as with tpm_transmit_cmd() 2829deb0eb7SJason Gunthorpe */ 283879b5892SRoberto Sassu int tpm_pcr_read(struct tpm_chip *chip, u32 pcr_idx, 284879b5892SRoberto Sassu struct tpm_digest *digest) 2859deb0eb7SJason Gunthorpe { 2869deb0eb7SJason Gunthorpe int rc; 2879deb0eb7SJason Gunthorpe 288fc1d52b7SStefan Berger chip = tpm_find_get_ops(chip); 289aad887f6SJarkko Sakkinen if (!chip) 2909deb0eb7SJason Gunthorpe return -ENODEV; 291d4a31756STomas Winkler 2927a1d7e6dSJarkko Sakkinen if (chip->flags & TPM_CHIP_FLAG_TPM2) 293879b5892SRoberto Sassu rc = tpm2_pcr_read(chip, pcr_idx, digest, NULL); 2947a1d7e6dSJarkko Sakkinen else 295879b5892SRoberto Sassu rc = tpm1_pcr_read(chip, pcr_idx, digest->digest); 296d4a31756STomas Winkler 2974e26195fSJason Gunthorpe tpm_put_ops(chip); 2989deb0eb7SJason Gunthorpe return rc; 2999deb0eb7SJason Gunthorpe } 3009deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_pcr_read); 3019deb0eb7SJason Gunthorpe 3029deb0eb7SJason Gunthorpe /** 303aad887f6SJarkko Sakkinen * tpm_pcr_extend - extend a PCR value in SHA1 bank. 304aad887f6SJarkko Sakkinen * @chip: a &struct tpm_chip instance, %NULL for the default chip 305aad887f6SJarkko Sakkinen * @pcr_idx: the PCR to be retrieved 3060b6cf6b9SRoberto Sassu * @digests: array of tpm_digest structures used to extend PCRs 3079deb0eb7SJason Gunthorpe * 3080b6cf6b9SRoberto Sassu * Note: callers must pass a digest for every allocated PCR bank, in the same 3090b6cf6b9SRoberto Sassu * order of the banks in chip->allocated_banks. 310aad887f6SJarkko Sakkinen * 311aad887f6SJarkko Sakkinen * Return: same as with tpm_transmit_cmd() 3129deb0eb7SJason Gunthorpe */ 3130b6cf6b9SRoberto Sassu int tpm_pcr_extend(struct tpm_chip *chip, u32 pcr_idx, 3140b6cf6b9SRoberto Sassu struct tpm_digest *digests) 3159deb0eb7SJason Gunthorpe { 3169deb0eb7SJason Gunthorpe int rc; 317c1f92b4bSNayna Jain int i; 3189deb0eb7SJason Gunthorpe 319fc1d52b7SStefan Berger chip = tpm_find_get_ops(chip); 320aad887f6SJarkko Sakkinen if (!chip) 3219deb0eb7SJason Gunthorpe return -ENODEV; 3229deb0eb7SJason Gunthorpe 323*9f75c822SRoberto Sassu for (i = 0; i < chip->nr_allocated_banks; i++) { 324*9f75c822SRoberto Sassu if (digests[i].alg_id != chip->allocated_banks[i].alg_id) { 325*9f75c822SRoberto Sassu rc = EINVAL; 326*9f75c822SRoberto Sassu goto out; 327*9f75c822SRoberto Sassu } 328*9f75c822SRoberto Sassu } 3290b6cf6b9SRoberto Sassu 3307a1d7e6dSJarkko Sakkinen if (chip->flags & TPM_CHIP_FLAG_TPM2) { 3310b6cf6b9SRoberto Sassu rc = tpm2_pcr_extend(chip, pcr_idx, digests); 332*9f75c822SRoberto Sassu goto out; 3337a1d7e6dSJarkko Sakkinen } 3347a1d7e6dSJarkko Sakkinen 3350b6cf6b9SRoberto Sassu rc = tpm1_pcr_extend(chip, pcr_idx, digests[0].digest, 3369deb0eb7SJason Gunthorpe "attempting extend a PCR value"); 337*9f75c822SRoberto Sassu 338*9f75c822SRoberto Sassu out: 3394e26195fSJason Gunthorpe tpm_put_ops(chip); 3409deb0eb7SJason Gunthorpe return rc; 3419deb0eb7SJason Gunthorpe } 3429deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_pcr_extend); 3439deb0eb7SJason Gunthorpe 3449deb0eb7SJason Gunthorpe /** 345aad887f6SJarkko Sakkinen * tpm_send - send a TPM command 346aad887f6SJarkko Sakkinen * @chip: a &struct tpm_chip instance, %NULL for the default chip 347aad887f6SJarkko Sakkinen * @cmd: a TPM command buffer 348aad887f6SJarkko Sakkinen * @buflen: the length of the TPM command buffer 349aad887f6SJarkko Sakkinen * 350aad887f6SJarkko Sakkinen * Return: same as with tpm_transmit_cmd() 351aad887f6SJarkko Sakkinen */ 352aad887f6SJarkko Sakkinen int tpm_send(struct tpm_chip *chip, void *cmd, size_t buflen) 3539deb0eb7SJason Gunthorpe { 354412eb585SJarkko Sakkinen struct tpm_buf buf; 3559deb0eb7SJason Gunthorpe int rc; 3569deb0eb7SJason Gunthorpe 357fc1d52b7SStefan Berger chip = tpm_find_get_ops(chip); 358aad887f6SJarkko Sakkinen if (!chip) 3599deb0eb7SJason Gunthorpe return -ENODEV; 3609deb0eb7SJason Gunthorpe 361412eb585SJarkko Sakkinen rc = tpm_buf_init(&buf, 0, 0); 362412eb585SJarkko Sakkinen if (rc) 363412eb585SJarkko Sakkinen goto out; 364412eb585SJarkko Sakkinen 365412eb585SJarkko Sakkinen memcpy(buf.data, cmd, buflen); 36647a6c28bSJarkko Sakkinen rc = tpm_transmit_cmd(chip, &buf, 0, "attempting to a send a command"); 367412eb585SJarkko Sakkinen tpm_buf_destroy(&buf); 368412eb585SJarkko Sakkinen out: 3694e26195fSJason Gunthorpe tpm_put_ops(chip); 3709deb0eb7SJason Gunthorpe return rc; 3719deb0eb7SJason Gunthorpe } 3729deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_send); 3739deb0eb7SJason Gunthorpe 374b03c4370STomas Winkler int tpm_auto_startup(struct tpm_chip *chip) 375b03c4370STomas Winkler { 376b03c4370STomas Winkler int rc; 377b03c4370STomas Winkler 378b03c4370STomas Winkler if (!(chip->ops->flags & TPM_OPS_AUTO_STARTUP)) 379b03c4370STomas Winkler return 0; 380b03c4370STomas Winkler 381b03c4370STomas Winkler if (chip->flags & TPM_CHIP_FLAG_TPM2) 382b03c4370STomas Winkler rc = tpm2_auto_startup(chip); 383b03c4370STomas Winkler else 384b03c4370STomas Winkler rc = tpm1_auto_startup(chip); 385b03c4370STomas Winkler 386b03c4370STomas Winkler return rc; 387b03c4370STomas Winkler } 388b03c4370STomas Winkler 3899deb0eb7SJason Gunthorpe /* 3909deb0eb7SJason Gunthorpe * We are about to suspend. Save the TPM state 3919deb0eb7SJason Gunthorpe * so that it can be restored. 3929deb0eb7SJason Gunthorpe */ 3939deb0eb7SJason Gunthorpe int tpm_pm_suspend(struct device *dev) 3949deb0eb7SJason Gunthorpe { 395ec03c50bSStefan Berger struct tpm_chip *chip = dev_get_drvdata(dev); 396c82a330cSTomas Winkler int rc = 0; 3979deb0eb7SJason Gunthorpe 398c82a330cSTomas Winkler if (!chip) 3999deb0eb7SJason Gunthorpe return -ENODEV; 4009deb0eb7SJason Gunthorpe 401b5d0ebc9SEnric Balletbo i Serra if (chip->flags & TPM_CHIP_FLAG_ALWAYS_POWERED) 402b5d0ebc9SEnric Balletbo i Serra return 0; 403b5d0ebc9SEnric Balletbo i Serra 40447a6c28bSJarkko Sakkinen if (!tpm_chip_start(chip)) { 405e891db1aSJarkko Sakkinen if (chip->flags & TPM_CHIP_FLAG_TPM2) 40674d6b3ceSJarkko Sakkinen tpm2_shutdown(chip, TPM2_SU_STATE); 407e891db1aSJarkko Sakkinen else 408c82a330cSTomas Winkler rc = tpm1_pm_suspend(chip, tpm_suspend_pcr); 409e891db1aSJarkko Sakkinen 410e891db1aSJarkko Sakkinen tpm_chip_stop(chip); 411a3fbfae8SJarkko Sakkinen } 4129deb0eb7SJason Gunthorpe 4139deb0eb7SJason Gunthorpe return rc; 4149deb0eb7SJason Gunthorpe } 4159deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_pm_suspend); 4169deb0eb7SJason Gunthorpe 4179deb0eb7SJason Gunthorpe /* 4189deb0eb7SJason Gunthorpe * Resume from a power safe. The BIOS already restored 4199deb0eb7SJason Gunthorpe * the TPM state. 4209deb0eb7SJason Gunthorpe */ 4219deb0eb7SJason Gunthorpe int tpm_pm_resume(struct device *dev) 4229deb0eb7SJason Gunthorpe { 423ec03c50bSStefan Berger struct tpm_chip *chip = dev_get_drvdata(dev); 4249deb0eb7SJason Gunthorpe 4259deb0eb7SJason Gunthorpe if (chip == NULL) 4269deb0eb7SJason Gunthorpe return -ENODEV; 4279deb0eb7SJason Gunthorpe 4289deb0eb7SJason Gunthorpe return 0; 4299deb0eb7SJason Gunthorpe } 4309deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_pm_resume); 4319deb0eb7SJason Gunthorpe 4329deb0eb7SJason Gunthorpe /** 433aad887f6SJarkko Sakkinen * tpm_get_random() - get random bytes from the TPM's RNG 434aad887f6SJarkko Sakkinen * @chip: a &struct tpm_chip instance, %NULL for the default chip 4359deb0eb7SJason Gunthorpe * @out: destination buffer for the random bytes 4369deb0eb7SJason Gunthorpe * @max: the max number of bytes to write to @out 4379deb0eb7SJason Gunthorpe * 4387aee9c52STomas Winkler * Return: number of random bytes read or a negative error value. 4399deb0eb7SJason Gunthorpe */ 440aad887f6SJarkko Sakkinen int tpm_get_random(struct tpm_chip *chip, u8 *out, size_t max) 4419deb0eb7SJason Gunthorpe { 442433d390fSTomas Winkler int rc; 4439deb0eb7SJason Gunthorpe 444433d390fSTomas Winkler if (!out || max > TPM_MAX_RNG_DATA) 4453e14d83eSJarkko Sakkinen return -EINVAL; 4463e14d83eSJarkko Sakkinen 447fc1d52b7SStefan Berger chip = tpm_find_get_ops(chip); 448aad887f6SJarkko Sakkinen if (!chip) 4499deb0eb7SJason Gunthorpe return -ENODEV; 4509deb0eb7SJason Gunthorpe 451433d390fSTomas Winkler if (chip->flags & TPM_CHIP_FLAG_TPM2) 452433d390fSTomas Winkler rc = tpm2_get_random(chip, out, max); 453433d390fSTomas Winkler else 454433d390fSTomas Winkler rc = tpm1_get_random(chip, out, max); 4559deb0eb7SJason Gunthorpe 4564e26195fSJason Gunthorpe tpm_put_ops(chip); 457433d390fSTomas Winkler return rc; 4589deb0eb7SJason Gunthorpe } 4599deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_get_random); 4609deb0eb7SJason Gunthorpe 461954650efSJarkko Sakkinen /** 462aad887f6SJarkko Sakkinen * tpm_seal_trusted() - seal a trusted key payload 463aad887f6SJarkko Sakkinen * @chip: a &struct tpm_chip instance, %NULL for the default chip 464954650efSJarkko Sakkinen * @options: authentication values and other options 465954650efSJarkko Sakkinen * @payload: the key data in clear and encrypted form 466954650efSJarkko Sakkinen * 467aad887f6SJarkko Sakkinen * Note: only TPM 2.0 chip are supported. TPM 1.x implementation is located in 468aad887f6SJarkko Sakkinen * the keyring subsystem. 469aad887f6SJarkko Sakkinen * 470aad887f6SJarkko Sakkinen * Return: same as with tpm_transmit_cmd() 471954650efSJarkko Sakkinen */ 472aad887f6SJarkko Sakkinen int tpm_seal_trusted(struct tpm_chip *chip, struct trusted_key_payload *payload, 473954650efSJarkko Sakkinen struct trusted_key_options *options) 474954650efSJarkko Sakkinen { 475954650efSJarkko Sakkinen int rc; 476954650efSJarkko Sakkinen 477fc1d52b7SStefan Berger chip = tpm_find_get_ops(chip); 478aad887f6SJarkko Sakkinen if (!chip || !(chip->flags & TPM_CHIP_FLAG_TPM2)) 479954650efSJarkko Sakkinen return -ENODEV; 480954650efSJarkko Sakkinen 481954650efSJarkko Sakkinen rc = tpm2_seal_trusted(chip, payload, options); 482954650efSJarkko Sakkinen 4834e26195fSJason Gunthorpe tpm_put_ops(chip); 484954650efSJarkko Sakkinen return rc; 485954650efSJarkko Sakkinen } 486954650efSJarkko Sakkinen EXPORT_SYMBOL_GPL(tpm_seal_trusted); 487954650efSJarkko Sakkinen 488954650efSJarkko Sakkinen /** 489954650efSJarkko Sakkinen * tpm_unseal_trusted() - unseal a trusted key 490aad887f6SJarkko Sakkinen * @chip: a &struct tpm_chip instance, %NULL for the default chip 491954650efSJarkko Sakkinen * @options: authentication values and other options 492954650efSJarkko Sakkinen * @payload: the key data in clear and encrypted form 493954650efSJarkko Sakkinen * 494aad887f6SJarkko Sakkinen * Note: only TPM 2.0 chip are supported. TPM 1.x implementation is located in 495aad887f6SJarkko Sakkinen * the keyring subsystem. 496aad887f6SJarkko Sakkinen * 497aad887f6SJarkko Sakkinen * Return: same as with tpm_transmit_cmd() 498954650efSJarkko Sakkinen */ 499aad887f6SJarkko Sakkinen int tpm_unseal_trusted(struct tpm_chip *chip, 500aad887f6SJarkko Sakkinen struct trusted_key_payload *payload, 501954650efSJarkko Sakkinen struct trusted_key_options *options) 502954650efSJarkko Sakkinen { 503954650efSJarkko Sakkinen int rc; 504954650efSJarkko Sakkinen 505fc1d52b7SStefan Berger chip = tpm_find_get_ops(chip); 506aad887f6SJarkko Sakkinen if (!chip || !(chip->flags & TPM_CHIP_FLAG_TPM2)) 507954650efSJarkko Sakkinen return -ENODEV; 508954650efSJarkko Sakkinen 509954650efSJarkko Sakkinen rc = tpm2_unseal_trusted(chip, payload, options); 510954650efSJarkko Sakkinen 5114e26195fSJason Gunthorpe tpm_put_ops(chip); 5124e26195fSJason Gunthorpe 513954650efSJarkko Sakkinen return rc; 514954650efSJarkko Sakkinen } 515954650efSJarkko Sakkinen EXPORT_SYMBOL_GPL(tpm_unseal_trusted); 516954650efSJarkko Sakkinen 517313d21eeSJarkko Sakkinen static int __init tpm_init(void) 518313d21eeSJarkko Sakkinen { 519313d21eeSJarkko Sakkinen int rc; 520313d21eeSJarkko Sakkinen 521313d21eeSJarkko Sakkinen tpm_class = class_create(THIS_MODULE, "tpm"); 522313d21eeSJarkko Sakkinen if (IS_ERR(tpm_class)) { 523313d21eeSJarkko Sakkinen pr_err("couldn't create tpm class\n"); 524313d21eeSJarkko Sakkinen return PTR_ERR(tpm_class); 525313d21eeSJarkko Sakkinen } 526313d21eeSJarkko Sakkinen 527fdc915f7SJames Bottomley tpmrm_class = class_create(THIS_MODULE, "tpmrm"); 528fdc915f7SJames Bottomley if (IS_ERR(tpmrm_class)) { 529fdc915f7SJames Bottomley pr_err("couldn't create tpmrm class\n"); 5309e1b74a6STadeusz Struk rc = PTR_ERR(tpmrm_class); 5319e1b74a6STadeusz Struk goto out_destroy_tpm_class; 532fdc915f7SJames Bottomley } 533fdc915f7SJames Bottomley 534fdc915f7SJames Bottomley rc = alloc_chrdev_region(&tpm_devt, 0, 2*TPM_NUM_DEVICES, "tpm"); 535313d21eeSJarkko Sakkinen if (rc < 0) { 536313d21eeSJarkko Sakkinen pr_err("tpm: failed to allocate char dev region\n"); 5379e1b74a6STadeusz Struk goto out_destroy_tpmrm_class; 5389e1b74a6STadeusz Struk } 5399e1b74a6STadeusz Struk 5409e1b74a6STadeusz Struk rc = tpm_dev_common_init(); 5419e1b74a6STadeusz Struk if (rc) { 5429e1b74a6STadeusz Struk pr_err("tpm: failed to allocate char dev region\n"); 5439e1b74a6STadeusz Struk goto out_unreg_chrdev; 544313d21eeSJarkko Sakkinen } 545313d21eeSJarkko Sakkinen 546313d21eeSJarkko Sakkinen return 0; 5479e1b74a6STadeusz Struk 5489e1b74a6STadeusz Struk out_unreg_chrdev: 5499e1b74a6STadeusz Struk unregister_chrdev_region(tpm_devt, 2 * TPM_NUM_DEVICES); 5509e1b74a6STadeusz Struk out_destroy_tpmrm_class: 5519e1b74a6STadeusz Struk class_destroy(tpmrm_class); 5529e1b74a6STadeusz Struk out_destroy_tpm_class: 5539e1b74a6STadeusz Struk class_destroy(tpm_class); 5549e1b74a6STadeusz Struk 5559e1b74a6STadeusz Struk return rc; 556313d21eeSJarkko Sakkinen } 557313d21eeSJarkko Sakkinen 558313d21eeSJarkko Sakkinen static void __exit tpm_exit(void) 559313d21eeSJarkko Sakkinen { 56015516788SStefan Berger idr_destroy(&dev_nums_idr); 561313d21eeSJarkko Sakkinen class_destroy(tpm_class); 562fdc915f7SJames Bottomley class_destroy(tpmrm_class); 563fdc915f7SJames Bottomley unregister_chrdev_region(tpm_devt, 2*TPM_NUM_DEVICES); 5649e1b74a6STadeusz Struk tpm_dev_common_exit(); 565313d21eeSJarkko Sakkinen } 566313d21eeSJarkko Sakkinen 567313d21eeSJarkko Sakkinen subsys_initcall(tpm_init); 568313d21eeSJarkko Sakkinen module_exit(tpm_exit); 569313d21eeSJarkko Sakkinen 5709deb0eb7SJason Gunthorpe MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)"); 5719deb0eb7SJason Gunthorpe MODULE_DESCRIPTION("TPM Driver"); 5729deb0eb7SJason Gunthorpe MODULE_VERSION("2.0"); 5739deb0eb7SJason Gunthorpe MODULE_LICENSE("GPL"); 574