19deb0eb7SJason Gunthorpe /* 29deb0eb7SJason Gunthorpe * Copyright (C) 2004 IBM Corporation 3afb5abc2SJarkko Sakkinen * Copyright (C) 2014 Intel Corporation 49deb0eb7SJason Gunthorpe * 59deb0eb7SJason Gunthorpe * Authors: 69deb0eb7SJason Gunthorpe * Leendert van Doorn <leendert@watson.ibm.com> 79deb0eb7SJason Gunthorpe * Dave Safford <safford@watson.ibm.com> 89deb0eb7SJason Gunthorpe * Reiner Sailer <sailer@watson.ibm.com> 99deb0eb7SJason Gunthorpe * Kylene Hall <kjhall@us.ibm.com> 109deb0eb7SJason Gunthorpe * 119deb0eb7SJason Gunthorpe * Maintained by: <tpmdd-devel@lists.sourceforge.net> 129deb0eb7SJason Gunthorpe * 139deb0eb7SJason Gunthorpe * Device driver for TCG/TCPA TPM (trusted platform module). 149deb0eb7SJason Gunthorpe * Specifications at www.trustedcomputinggroup.org 159deb0eb7SJason Gunthorpe * 169deb0eb7SJason Gunthorpe * This program is free software; you can redistribute it and/or 179deb0eb7SJason Gunthorpe * modify it under the terms of the GNU General Public License as 189deb0eb7SJason Gunthorpe * published by the Free Software Foundation, version 2 of the 199deb0eb7SJason Gunthorpe * License. 209deb0eb7SJason Gunthorpe * 219deb0eb7SJason Gunthorpe * Note, the TPM chip is not interrupt driven (only polling) 229deb0eb7SJason Gunthorpe * and can have very long timeouts (minutes!). Hence the unusual 239deb0eb7SJason Gunthorpe * calls to msleep. 249deb0eb7SJason Gunthorpe * 259deb0eb7SJason Gunthorpe */ 269deb0eb7SJason Gunthorpe 279deb0eb7SJason Gunthorpe #include <linux/poll.h> 289deb0eb7SJason Gunthorpe #include <linux/slab.h> 299deb0eb7SJason Gunthorpe #include <linux/mutex.h> 309deb0eb7SJason Gunthorpe #include <linux/spinlock.h> 319deb0eb7SJason Gunthorpe #include <linux/freezer.h> 32fd3ec366SThiebaud Weksteen #include <linux/tpm_eventlog.h> 339deb0eb7SJason Gunthorpe 349deb0eb7SJason Gunthorpe #include "tpm.h" 359deb0eb7SJason Gunthorpe 369deb0eb7SJason Gunthorpe /* 379deb0eb7SJason Gunthorpe * Bug workaround - some TPM's don't flush the most 389deb0eb7SJason Gunthorpe * recently changed pcr on suspend, so force the flush 399deb0eb7SJason Gunthorpe * with an extend to the selected _unused_ non-volatile pcr. 409deb0eb7SJason Gunthorpe */ 4195adc6b4STomas Winkler static u32 tpm_suspend_pcr; 429deb0eb7SJason Gunthorpe module_param_named(suspend_pcr, tpm_suspend_pcr, uint, 0644); 439deb0eb7SJason Gunthorpe MODULE_PARM_DESC(suspend_pcr, 4439f5712bSDmitry Torokhov "PCR to use for dummy writes to facilitate flush on suspend."); 459deb0eb7SJason Gunthorpe 46d856c00fSTomas Winkler /** 47d856c00fSTomas Winkler * tpm_calc_ordinal_duration() - calculate the maximum command duration 48d856c00fSTomas Winkler * @chip: TPM chip to use. 49d856c00fSTomas Winkler * @ordinal: TPM command ordinal. 50d856c00fSTomas Winkler * 51d856c00fSTomas Winkler * The function returns the maximum amount of time the chip could take 52d856c00fSTomas Winkler * to return the result for a particular ordinal in jiffies. 53d856c00fSTomas Winkler * 54d856c00fSTomas Winkler * Return: A maximal duration time for an ordinal in jiffies. 55d856c00fSTomas Winkler */ 56d856c00fSTomas Winkler unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal) 57d856c00fSTomas Winkler { 58d856c00fSTomas Winkler if (chip->flags & TPM_CHIP_FLAG_TPM2) 59d856c00fSTomas Winkler return tpm2_calc_ordinal_duration(chip, ordinal); 60d856c00fSTomas Winkler else 61d856c00fSTomas Winkler return tpm1_calc_ordinal_duration(chip, ordinal); 62d856c00fSTomas Winkler } 63d856c00fSTomas Winkler EXPORT_SYMBOL_GPL(tpm_calc_ordinal_duration); 64d856c00fSTomas Winkler 65627448e8STomas Winkler static int tpm_request_locality(struct tpm_chip *chip, unsigned int flags) 66888d867dSTomas Winkler { 67888d867dSTomas Winkler int rc; 68888d867dSTomas Winkler 6958bac8ccSJarkko Sakkinen if (flags & TPM_TRANSMIT_NESTED) 70627448e8STomas Winkler return 0; 71627448e8STomas Winkler 72888d867dSTomas Winkler if (!chip->ops->request_locality) 73888d867dSTomas Winkler return 0; 74888d867dSTomas Winkler 75888d867dSTomas Winkler rc = chip->ops->request_locality(chip, 0); 76888d867dSTomas Winkler if (rc < 0) 77888d867dSTomas Winkler return rc; 78888d867dSTomas Winkler 79888d867dSTomas Winkler chip->locality = rc; 80888d867dSTomas Winkler 81888d867dSTomas Winkler return 0; 82888d867dSTomas Winkler } 83888d867dSTomas Winkler 84627448e8STomas Winkler static void tpm_relinquish_locality(struct tpm_chip *chip, unsigned int flags) 85888d867dSTomas Winkler { 86888d867dSTomas Winkler int rc; 87888d867dSTomas Winkler 8858bac8ccSJarkko Sakkinen if (flags & TPM_TRANSMIT_NESTED) 89627448e8STomas Winkler return; 90627448e8STomas Winkler 91888d867dSTomas Winkler if (!chip->ops->relinquish_locality) 92888d867dSTomas Winkler return; 93888d867dSTomas Winkler 94888d867dSTomas Winkler rc = chip->ops->relinquish_locality(chip, chip->locality); 95888d867dSTomas Winkler if (rc) 96888d867dSTomas Winkler dev_err(&chip->dev, "%s: : error %d\n", __func__, rc); 97888d867dSTomas Winkler 98888d867dSTomas Winkler chip->locality = -1; 99888d867dSTomas Winkler } 100888d867dSTomas Winkler 101627448e8STomas Winkler static int tpm_cmd_ready(struct tpm_chip *chip, unsigned int flags) 102627448e8STomas Winkler { 10358bac8ccSJarkko Sakkinen if (flags & TPM_TRANSMIT_NESTED) 104627448e8STomas Winkler return 0; 105627448e8STomas Winkler 106627448e8STomas Winkler if (!chip->ops->cmd_ready) 107627448e8STomas Winkler return 0; 108627448e8STomas Winkler 109627448e8STomas Winkler return chip->ops->cmd_ready(chip); 110627448e8STomas Winkler } 111627448e8STomas Winkler 112627448e8STomas Winkler static int tpm_go_idle(struct tpm_chip *chip, unsigned int flags) 113627448e8STomas Winkler { 11458bac8ccSJarkko Sakkinen if (flags & TPM_TRANSMIT_NESTED) 115627448e8STomas Winkler return 0; 116627448e8STomas Winkler 117627448e8STomas Winkler if (!chip->ops->go_idle) 118627448e8STomas Winkler return 0; 119627448e8STomas Winkler 120627448e8STomas Winkler return chip->ops->go_idle(chip); 121627448e8STomas Winkler } 122627448e8STomas Winkler 123*5faafbabSJarkko Sakkinen static ssize_t tpm_try_transmit(struct tpm_chip *chip, void *buf, size_t bufsiz, 124*5faafbabSJarkko Sakkinen unsigned int flags) 1259deb0eb7SJason Gunthorpe { 126b34b77a9SJarkko Sakkinen struct tpm_header *header = buf; 127745b361eSJarkko Sakkinen int rc; 128745b361eSJarkko Sakkinen ssize_t len = 0; 1299deb0eb7SJason Gunthorpe u32 count, ordinal; 1309deb0eb7SJason Gunthorpe unsigned long stop; 1319deb0eb7SJason Gunthorpe 132c3465a37SJarkko Sakkinen if (bufsiz < TPM_HEADER_SIZE) 133c3465a37SJarkko Sakkinen return -EINVAL; 134ebfd7532SJarkko Sakkinen 1359deb0eb7SJason Gunthorpe if (bufsiz > TPM_BUFSIZE) 1369deb0eb7SJason Gunthorpe bufsiz = TPM_BUFSIZE; 1379deb0eb7SJason Gunthorpe 138720b0711SJarkko Sakkinen count = be32_to_cpu(header->length); 139720b0711SJarkko Sakkinen ordinal = be32_to_cpu(header->ordinal); 1409deb0eb7SJason Gunthorpe if (count == 0) 1419deb0eb7SJason Gunthorpe return -ENODATA; 1429deb0eb7SJason Gunthorpe if (count > bufsiz) { 1438cfffc9dSJason Gunthorpe dev_err(&chip->dev, 1449deb0eb7SJason Gunthorpe "invalid count value %x %zx\n", count, bufsiz); 1459deb0eb7SJason Gunthorpe return -E2BIG; 1469deb0eb7SJason Gunthorpe } 1479deb0eb7SJason Gunthorpe 14862c09e12SWinkler, Tomas rc = chip->ops->send(chip, buf, count); 1499deb0eb7SJason Gunthorpe if (rc < 0) { 150402149c6SStefan Berger if (rc != -EPIPE) 1518cfffc9dSJason Gunthorpe dev_err(&chip->dev, 152f5595f5bSJarkko Sakkinen "%s: send(): error %d\n", __func__, rc); 15329b47ce9SJarkko Sakkinen return rc; 1549deb0eb7SJason Gunthorpe } 1559deb0eb7SJason Gunthorpe 156f5595f5bSJarkko Sakkinen /* A sanity check. send() should just return zero on success e.g. 157f5595f5bSJarkko Sakkinen * not the command length. 158f5595f5bSJarkko Sakkinen */ 159f5595f5bSJarkko Sakkinen if (rc > 0) { 160f5595f5bSJarkko Sakkinen dev_warn(&chip->dev, 161f5595f5bSJarkko Sakkinen "%s: send(): invalid value %d\n", __func__, rc); 162f5595f5bSJarkko Sakkinen rc = 0; 163f5595f5bSJarkko Sakkinen } 164f5595f5bSJarkko Sakkinen 165570a3609SChristophe Ricard if (chip->flags & TPM_CHIP_FLAG_IRQ) 1669deb0eb7SJason Gunthorpe goto out_recv; 1679deb0eb7SJason Gunthorpe 168d856c00fSTomas Winkler stop = jiffies + tpm_calc_ordinal_duration(chip, ordinal); 1699deb0eb7SJason Gunthorpe do { 1705f82e9f0SJason Gunthorpe u8 status = chip->ops->status(chip); 1715f82e9f0SJason Gunthorpe if ((status & chip->ops->req_complete_mask) == 1725f82e9f0SJason Gunthorpe chip->ops->req_complete_val) 1739deb0eb7SJason Gunthorpe goto out_recv; 1749deb0eb7SJason Gunthorpe 1755f82e9f0SJason Gunthorpe if (chip->ops->req_canceled(chip, status)) { 1768cfffc9dSJason Gunthorpe dev_err(&chip->dev, "Operation Canceled\n"); 17729b47ce9SJarkko Sakkinen return -ECANCELED; 1789deb0eb7SJason Gunthorpe } 1799deb0eb7SJason Gunthorpe 18059f5a6b0SNayna Jain tpm_msleep(TPM_TIMEOUT_POLL); 1819deb0eb7SJason Gunthorpe rmb(); 1829deb0eb7SJason Gunthorpe } while (time_before(jiffies, stop)); 1839deb0eb7SJason Gunthorpe 1845f82e9f0SJason Gunthorpe chip->ops->cancel(chip); 1858cfffc9dSJason Gunthorpe dev_err(&chip->dev, "Operation Timed out\n"); 18629b47ce9SJarkko Sakkinen return -ETIME; 1879deb0eb7SJason Gunthorpe 1889deb0eb7SJason Gunthorpe out_recv: 18962c09e12SWinkler, Tomas len = chip->ops->recv(chip, buf, bufsiz); 190745b361eSJarkko Sakkinen if (len < 0) { 191745b361eSJarkko Sakkinen rc = len; 192304ff672SJarkko Sakkinen dev_err(&chip->dev, "tpm_transmit: tpm_recv: error %d\n", rc); 193304ff672SJarkko Sakkinen } else if (len < TPM_HEADER_SIZE || len != be32_to_cpu(header->length)) 194a147918eSJarkko Sakkinen rc = -EFAULT; 195a147918eSJarkko Sakkinen 196745b361eSJarkko Sakkinen return rc ? rc : len; 1979deb0eb7SJason Gunthorpe } 1989deb0eb7SJason Gunthorpe 199f865c196SWinkler, Tomas /** 200e2fb992dSJames Bottomley * tpm_transmit - Internal kernel interface to transmit TPM commands. 201412eb585SJarkko Sakkinen * @chip: a TPM chip to use 202412eb585SJarkko Sakkinen * @buf: a TPM command buffer 203e2fb992dSJames Bottomley * @bufsiz: length of the TPM command buffer 204412eb585SJarkko Sakkinen * @flags: TPM transmit flags 205e2fb992dSJames Bottomley * 206412eb585SJarkko Sakkinen * A wrapper around tpm_try_transmit() that handles TPM2_RC_RETRY returns from 207412eb585SJarkko Sakkinen * the TPM and retransmits the command after a delay up to a maximum wait of 208412eb585SJarkko Sakkinen * TPM2_DURATION_LONG. 209e2fb992dSJames Bottomley * 210412eb585SJarkko Sakkinen * Note that TPM 1.x never returns TPM2_RC_RETRY so the retry logic is TPM 2.0 211412eb585SJarkko Sakkinen * only. 212e2fb992dSJames Bottomley * 213e2fb992dSJames Bottomley * Return: 214412eb585SJarkko Sakkinen * * The response length - OK 215412eb585SJarkko Sakkinen * * -errno - A system error 216e2fb992dSJames Bottomley */ 217*5faafbabSJarkko Sakkinen ssize_t tpm_transmit(struct tpm_chip *chip, u8 *buf, size_t bufsiz, 218*5faafbabSJarkko Sakkinen unsigned int flags) 219e2fb992dSJames Bottomley { 220b34b77a9SJarkko Sakkinen struct tpm_header *header = (struct tpm_header *)buf; 221e2fb992dSJames Bottomley /* space for header and handles */ 222e2fb992dSJames Bottomley u8 save[TPM_HEADER_SIZE + 3*sizeof(u32)]; 223e2fb992dSJames Bottomley unsigned int delay_msec = TPM2_DURATION_SHORT; 224304ff672SJarkko Sakkinen bool has_locality = false; 225e2fb992dSJames Bottomley u32 rc = 0; 226e2fb992dSJames Bottomley ssize_t ret; 227*5faafbabSJarkko Sakkinen const size_t save_size = min(sizeof(save), bufsiz); 2282be8ffedSJames Bottomley /* the command code is where the return code will be */ 2292be8ffedSJames Bottomley u32 cc = be32_to_cpu(header->return_code); 230e2fb992dSJames Bottomley 231e2fb992dSJames Bottomley /* 232e2fb992dSJames Bottomley * Subtlety here: if we have a space, the handles will be 233e2fb992dSJames Bottomley * transformed, so when we restore the header we also have to 234e2fb992dSJames Bottomley * restore the handles. 235e2fb992dSJames Bottomley */ 236e2fb992dSJames Bottomley memcpy(save, buf, save_size); 237e2fb992dSJames Bottomley 238e2fb992dSJames Bottomley for (;;) { 239304ff672SJarkko Sakkinen if (!(flags & TPM_TRANSMIT_UNLOCKED) && 240304ff672SJarkko Sakkinen !(flags & TPM_TRANSMIT_NESTED)) 241304ff672SJarkko Sakkinen mutex_lock(&chip->tpm_mutex); 242304ff672SJarkko Sakkinen 243304ff672SJarkko Sakkinen if (chip->ops->clk_enable != NULL) 244304ff672SJarkko Sakkinen chip->ops->clk_enable(chip, true); 245304ff672SJarkko Sakkinen 246304ff672SJarkko Sakkinen if (chip->locality == -1) { 247304ff672SJarkko Sakkinen ret = tpm_request_locality(chip, flags); 248304ff672SJarkko Sakkinen if (ret) 249304ff672SJarkko Sakkinen goto out_locality; 250304ff672SJarkko Sakkinen has_locality = true; 251304ff672SJarkko Sakkinen } 252304ff672SJarkko Sakkinen 253304ff672SJarkko Sakkinen ret = tpm_cmd_ready(chip, flags); 254304ff672SJarkko Sakkinen if (ret) 255304ff672SJarkko Sakkinen goto out_locality; 256304ff672SJarkko Sakkinen 257*5faafbabSJarkko Sakkinen ret = tpm_try_transmit(chip, buf, bufsiz, flags); 258304ff672SJarkko Sakkinen 259304ff672SJarkko Sakkinen /* This may fail but do not override ret. */ 260304ff672SJarkko Sakkinen tpm_go_idle(chip, flags); 261304ff672SJarkko Sakkinen 262304ff672SJarkko Sakkinen out_locality: 263304ff672SJarkko Sakkinen if (has_locality) 264304ff672SJarkko Sakkinen tpm_relinquish_locality(chip, flags); 265304ff672SJarkko Sakkinen 266304ff672SJarkko Sakkinen if (chip->ops->clk_enable != NULL) 267304ff672SJarkko Sakkinen chip->ops->clk_enable(chip, false); 268304ff672SJarkko Sakkinen 269304ff672SJarkko Sakkinen if (!(flags & TPM_TRANSMIT_UNLOCKED) && 270304ff672SJarkko Sakkinen !(flags & TPM_TRANSMIT_NESTED)) 271304ff672SJarkko Sakkinen mutex_unlock(&chip->tpm_mutex); 272304ff672SJarkko Sakkinen 273e2fb992dSJames Bottomley if (ret < 0) 274e2fb992dSJames Bottomley break; 275e2fb992dSJames Bottomley rc = be32_to_cpu(header->return_code); 2762be8ffedSJames Bottomley if (rc != TPM2_RC_RETRY && rc != TPM2_RC_TESTING) 2772be8ffedSJames Bottomley break; 2782be8ffedSJames Bottomley /* 2792be8ffedSJames Bottomley * return immediately if self test returns test 2802be8ffedSJames Bottomley * still running to shorten boot time. 2812be8ffedSJames Bottomley */ 2822be8ffedSJames Bottomley if (rc == TPM2_RC_TESTING && cc == TPM2_CC_SELF_TEST) 283e2fb992dSJames Bottomley break; 28492980756SNayna Jain 285e2fb992dSJames Bottomley if (delay_msec > TPM2_DURATION_LONG) { 2862be8ffedSJames Bottomley if (rc == TPM2_RC_RETRY) 2872be8ffedSJames Bottomley dev_err(&chip->dev, "in retry loop\n"); 2882be8ffedSJames Bottomley else 2892be8ffedSJames Bottomley dev_err(&chip->dev, 2902be8ffedSJames Bottomley "self test is still running\n"); 291e2fb992dSJames Bottomley break; 292e2fb992dSJames Bottomley } 293e2fb992dSJames Bottomley tpm_msleep(delay_msec); 29492980756SNayna Jain delay_msec *= 2; 295e2fb992dSJames Bottomley memcpy(buf, save, save_size); 296e2fb992dSJames Bottomley } 297e2fb992dSJames Bottomley return ret; 298e2fb992dSJames Bottomley } 299412eb585SJarkko Sakkinen 300e2fb992dSJames Bottomley /** 30165520d46SWinkler, Tomas * tpm_transmit_cmd - send a tpm command to the device 302412eb585SJarkko Sakkinen * @chip: a TPM chip to use 303412eb585SJarkko Sakkinen * @buf: a TPM command buffer 304c659af78SStefan Berger * @min_rsp_body_length: minimum expected length of response body 305412eb585SJarkko Sakkinen * @flags: TPM transmit flags 306f865c196SWinkler, Tomas * @desc: command description used in the error message 307f865c196SWinkler, Tomas * 308f865c196SWinkler, Tomas * Return: 309412eb585SJarkko Sakkinen * * 0 - OK 310412eb585SJarkko Sakkinen * * -errno - A system error 311412eb585SJarkko Sakkinen * * TPM_RC - A TPM error 312f865c196SWinkler, Tomas */ 313*5faafbabSJarkko Sakkinen ssize_t tpm_transmit_cmd(struct tpm_chip *chip, struct tpm_buf *buf, 314*5faafbabSJarkko Sakkinen size_t min_rsp_body_length, unsigned int flags, 315*5faafbabSJarkko Sakkinen const char *desc) 3169deb0eb7SJason Gunthorpe { 317b34b77a9SJarkko Sakkinen const struct tpm_header *header = (struct tpm_header *)buf->data; 3189deb0eb7SJason Gunthorpe int err; 319c659af78SStefan Berger ssize_t len; 3209deb0eb7SJason Gunthorpe 321*5faafbabSJarkko Sakkinen len = tpm_transmit(chip, buf->data, PAGE_SIZE, flags); 3229deb0eb7SJason Gunthorpe if (len < 0) 3239deb0eb7SJason Gunthorpe return len; 32487155b73SJarkko Sakkinen 32587155b73SJarkko Sakkinen err = be32_to_cpu(header->return_code); 3260d6d0d62SJavier Martinez Canillas if (err != 0 && err != TPM_ERR_DISABLED && err != TPM_ERR_DEACTIVATED 32708a8112aSJerry Snitselaar && err != TPM2_RC_TESTING && desc) 3288cfffc9dSJason Gunthorpe dev_err(&chip->dev, "A TPM error (%d) occurred %s\n", err, 32971ed848fSJarkko Sakkinen desc); 330c659af78SStefan Berger if (err) 3319deb0eb7SJason Gunthorpe return err; 332c659af78SStefan Berger 333c659af78SStefan Berger if (len < min_rsp_body_length + TPM_HEADER_SIZE) 334c659af78SStefan Berger return -EFAULT; 335c659af78SStefan Berger 336c659af78SStefan Berger return 0; 3379deb0eb7SJason Gunthorpe } 338be4c9acfSStefan Berger EXPORT_SYMBOL_GPL(tpm_transmit_cmd); 3399deb0eb7SJason Gunthorpe 3409deb0eb7SJason Gunthorpe int tpm_get_timeouts(struct tpm_chip *chip) 3419deb0eb7SJason Gunthorpe { 342d1d253cfSJason Gunthorpe if (chip->flags & TPM_CHIP_FLAG_HAVE_TIMEOUTS) 343d1d253cfSJason Gunthorpe return 0; 344d1d253cfSJason Gunthorpe 34570a3199aSTomas Winkler if (chip->flags & TPM_CHIP_FLAG_TPM2) 34670a3199aSTomas Winkler return tpm2_get_timeouts(chip); 34770a3199aSTomas Winkler else 34870a3199aSTomas Winkler return tpm1_get_timeouts(chip); 3499deb0eb7SJason Gunthorpe } 3509deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_get_timeouts); 3519deb0eb7SJason Gunthorpe 3529deb0eb7SJason Gunthorpe /** 353aad887f6SJarkko Sakkinen * tpm_is_tpm2 - do we a have a TPM2 chip? 354aad887f6SJarkko Sakkinen * @chip: a &struct tpm_chip instance, %NULL for the default chip 355954650efSJarkko Sakkinen * 356aad887f6SJarkko Sakkinen * Return: 357aad887f6SJarkko Sakkinen * 1 if we have a TPM2 chip. 358aad887f6SJarkko Sakkinen * 0 if we don't have a TPM2 chip. 359aad887f6SJarkko Sakkinen * A negative number for system errors (errno). 360954650efSJarkko Sakkinen */ 361aad887f6SJarkko Sakkinen int tpm_is_tpm2(struct tpm_chip *chip) 362954650efSJarkko Sakkinen { 363954650efSJarkko Sakkinen int rc; 364954650efSJarkko Sakkinen 365fc1d52b7SStefan Berger chip = tpm_find_get_ops(chip); 366aad887f6SJarkko Sakkinen if (!chip) 367954650efSJarkko Sakkinen return -ENODEV; 368954650efSJarkko Sakkinen 369954650efSJarkko Sakkinen rc = (chip->flags & TPM_CHIP_FLAG_TPM2) != 0; 370954650efSJarkko Sakkinen 3714e26195fSJason Gunthorpe tpm_put_ops(chip); 372954650efSJarkko Sakkinen 373954650efSJarkko Sakkinen return rc; 374954650efSJarkko Sakkinen } 375954650efSJarkko Sakkinen EXPORT_SYMBOL_GPL(tpm_is_tpm2); 376954650efSJarkko Sakkinen 377954650efSJarkko Sakkinen /** 378aad887f6SJarkko Sakkinen * tpm_pcr_read - read a PCR value from SHA1 bank 379aad887f6SJarkko Sakkinen * @chip: a &struct tpm_chip instance, %NULL for the default chip 380aad887f6SJarkko Sakkinen * @pcr_idx: the PCR to be retrieved 381aad887f6SJarkko Sakkinen * @res_buf: the value of the PCR 3829deb0eb7SJason Gunthorpe * 383aad887f6SJarkko Sakkinen * Return: same as with tpm_transmit_cmd() 3849deb0eb7SJason Gunthorpe */ 38595adc6b4STomas Winkler int tpm_pcr_read(struct tpm_chip *chip, u32 pcr_idx, u8 *res_buf) 3869deb0eb7SJason Gunthorpe { 3879deb0eb7SJason Gunthorpe int rc; 3889deb0eb7SJason Gunthorpe 389fc1d52b7SStefan Berger chip = tpm_find_get_ops(chip); 390aad887f6SJarkko Sakkinen if (!chip) 3919deb0eb7SJason Gunthorpe return -ENODEV; 392d4a31756STomas Winkler 3937a1d7e6dSJarkko Sakkinen if (chip->flags & TPM_CHIP_FLAG_TPM2) 3947a1d7e6dSJarkko Sakkinen rc = tpm2_pcr_read(chip, pcr_idx, res_buf); 3957a1d7e6dSJarkko Sakkinen else 396cfddcb05STomas Winkler rc = tpm1_pcr_read(chip, pcr_idx, res_buf); 397d4a31756STomas Winkler 3984e26195fSJason Gunthorpe tpm_put_ops(chip); 3999deb0eb7SJason Gunthorpe return rc; 4009deb0eb7SJason Gunthorpe } 4019deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_pcr_read); 4029deb0eb7SJason Gunthorpe 4039deb0eb7SJason Gunthorpe /** 404aad887f6SJarkko Sakkinen * tpm_pcr_extend - extend a PCR value in SHA1 bank. 405aad887f6SJarkko Sakkinen * @chip: a &struct tpm_chip instance, %NULL for the default chip 406aad887f6SJarkko Sakkinen * @pcr_idx: the PCR to be retrieved 407aad887f6SJarkko Sakkinen * @hash: the hash value used to extend the PCR value 4089deb0eb7SJason Gunthorpe * 409aad887f6SJarkko Sakkinen * Note: with TPM 2.0 extends also those banks with a known digest size to the 410aad887f6SJarkko Sakkinen * cryto subsystem in order to prevent malicious use of those PCR banks. In the 411aad887f6SJarkko Sakkinen * future we should dynamically determine digest sizes. 412aad887f6SJarkko Sakkinen * 413aad887f6SJarkko Sakkinen * Return: same as with tpm_transmit_cmd() 4149deb0eb7SJason Gunthorpe */ 41595adc6b4STomas Winkler int tpm_pcr_extend(struct tpm_chip *chip, u32 pcr_idx, const u8 *hash) 4169deb0eb7SJason Gunthorpe { 4179deb0eb7SJason Gunthorpe int rc; 418c1f92b4bSNayna Jain struct tpm2_digest digest_list[ARRAY_SIZE(chip->active_banks)]; 419c1f92b4bSNayna Jain u32 count = 0; 420c1f92b4bSNayna Jain int i; 4219deb0eb7SJason Gunthorpe 422fc1d52b7SStefan Berger chip = tpm_find_get_ops(chip); 423aad887f6SJarkko Sakkinen if (!chip) 4249deb0eb7SJason Gunthorpe return -ENODEV; 4259deb0eb7SJason Gunthorpe 4267a1d7e6dSJarkko Sakkinen if (chip->flags & TPM_CHIP_FLAG_TPM2) { 427c1f92b4bSNayna Jain memset(digest_list, 0, sizeof(digest_list)); 428c1f92b4bSNayna Jain 42970ea1636SDan Carpenter for (i = 0; i < ARRAY_SIZE(chip->active_banks) && 43070ea1636SDan Carpenter chip->active_banks[i] != TPM2_ALG_ERROR; i++) { 431c1f92b4bSNayna Jain digest_list[i].alg_id = chip->active_banks[i]; 432c1f92b4bSNayna Jain memcpy(digest_list[i].digest, hash, TPM_DIGEST_SIZE); 433c1f92b4bSNayna Jain count++; 434c1f92b4bSNayna Jain } 435c1f92b4bSNayna Jain 436c1f92b4bSNayna Jain rc = tpm2_pcr_extend(chip, pcr_idx, count, digest_list); 4374e26195fSJason Gunthorpe tpm_put_ops(chip); 4387a1d7e6dSJarkko Sakkinen return rc; 4397a1d7e6dSJarkko Sakkinen } 4407a1d7e6dSJarkko Sakkinen 441175d5b2aSRoberto Sassu rc = tpm1_pcr_extend(chip, pcr_idx, hash, 4429deb0eb7SJason Gunthorpe "attempting extend a PCR value"); 4434e26195fSJason Gunthorpe tpm_put_ops(chip); 4449deb0eb7SJason Gunthorpe return rc; 4459deb0eb7SJason Gunthorpe } 4469deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_pcr_extend); 4479deb0eb7SJason Gunthorpe 4489deb0eb7SJason Gunthorpe /** 449aad887f6SJarkko Sakkinen * tpm_send - send a TPM command 450aad887f6SJarkko Sakkinen * @chip: a &struct tpm_chip instance, %NULL for the default chip 451aad887f6SJarkko Sakkinen * @cmd: a TPM command buffer 452aad887f6SJarkko Sakkinen * @buflen: the length of the TPM command buffer 453aad887f6SJarkko Sakkinen * 454aad887f6SJarkko Sakkinen * Return: same as with tpm_transmit_cmd() 455aad887f6SJarkko Sakkinen */ 456aad887f6SJarkko Sakkinen int tpm_send(struct tpm_chip *chip, void *cmd, size_t buflen) 4579deb0eb7SJason Gunthorpe { 458412eb585SJarkko Sakkinen struct tpm_buf buf; 4599deb0eb7SJason Gunthorpe int rc; 4609deb0eb7SJason Gunthorpe 461fc1d52b7SStefan Berger chip = tpm_find_get_ops(chip); 462aad887f6SJarkko Sakkinen if (!chip) 4639deb0eb7SJason Gunthorpe return -ENODEV; 4649deb0eb7SJason Gunthorpe 465412eb585SJarkko Sakkinen rc = tpm_buf_init(&buf, 0, 0); 466412eb585SJarkko Sakkinen if (rc) 467412eb585SJarkko Sakkinen goto out; 468412eb585SJarkko Sakkinen 469412eb585SJarkko Sakkinen memcpy(buf.data, cmd, buflen); 470*5faafbabSJarkko Sakkinen rc = tpm_transmit_cmd(chip, &buf, 0, 0, 471aad887f6SJarkko Sakkinen "attempting to a send a command"); 472412eb585SJarkko Sakkinen tpm_buf_destroy(&buf); 473412eb585SJarkko Sakkinen out: 4744e26195fSJason Gunthorpe tpm_put_ops(chip); 4759deb0eb7SJason Gunthorpe return rc; 4769deb0eb7SJason Gunthorpe } 4779deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_send); 4789deb0eb7SJason Gunthorpe 479b03c4370STomas Winkler int tpm_auto_startup(struct tpm_chip *chip) 480b03c4370STomas Winkler { 481b03c4370STomas Winkler int rc; 482b03c4370STomas Winkler 483b03c4370STomas Winkler if (!(chip->ops->flags & TPM_OPS_AUTO_STARTUP)) 484b03c4370STomas Winkler return 0; 485b03c4370STomas Winkler 486b03c4370STomas Winkler if (chip->flags & TPM_CHIP_FLAG_TPM2) 487b03c4370STomas Winkler rc = tpm2_auto_startup(chip); 488b03c4370STomas Winkler else 489b03c4370STomas Winkler rc = tpm1_auto_startup(chip); 490b03c4370STomas Winkler 491b03c4370STomas Winkler return rc; 492b03c4370STomas Winkler } 493b03c4370STomas Winkler 4949deb0eb7SJason Gunthorpe /* 4959deb0eb7SJason Gunthorpe * We are about to suspend. Save the TPM state 4969deb0eb7SJason Gunthorpe * so that it can be restored. 4979deb0eb7SJason Gunthorpe */ 4989deb0eb7SJason Gunthorpe int tpm_pm_suspend(struct device *dev) 4999deb0eb7SJason Gunthorpe { 500ec03c50bSStefan Berger struct tpm_chip *chip = dev_get_drvdata(dev); 501c82a330cSTomas Winkler int rc = 0; 5029deb0eb7SJason Gunthorpe 503c82a330cSTomas Winkler if (!chip) 5049deb0eb7SJason Gunthorpe return -ENODEV; 5059deb0eb7SJason Gunthorpe 506b5d0ebc9SEnric Balletbo i Serra if (chip->flags & TPM_CHIP_FLAG_ALWAYS_POWERED) 507b5d0ebc9SEnric Balletbo i Serra return 0; 508b5d0ebc9SEnric Balletbo i Serra 509c82a330cSTomas Winkler if (chip->flags & TPM_CHIP_FLAG_TPM2) 51074d6b3ceSJarkko Sakkinen tpm2_shutdown(chip, TPM2_SU_STATE); 511c82a330cSTomas Winkler else 512c82a330cSTomas Winkler rc = tpm1_pm_suspend(chip, tpm_suspend_pcr); 5139deb0eb7SJason Gunthorpe 5149deb0eb7SJason Gunthorpe return rc; 5159deb0eb7SJason Gunthorpe } 5169deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_pm_suspend); 5179deb0eb7SJason Gunthorpe 5189deb0eb7SJason Gunthorpe /* 5199deb0eb7SJason Gunthorpe * Resume from a power safe. The BIOS already restored 5209deb0eb7SJason Gunthorpe * the TPM state. 5219deb0eb7SJason Gunthorpe */ 5229deb0eb7SJason Gunthorpe int tpm_pm_resume(struct device *dev) 5239deb0eb7SJason Gunthorpe { 524ec03c50bSStefan Berger struct tpm_chip *chip = dev_get_drvdata(dev); 5259deb0eb7SJason Gunthorpe 5269deb0eb7SJason Gunthorpe if (chip == NULL) 5279deb0eb7SJason Gunthorpe return -ENODEV; 5289deb0eb7SJason Gunthorpe 5299deb0eb7SJason Gunthorpe return 0; 5309deb0eb7SJason Gunthorpe } 5319deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_pm_resume); 5329deb0eb7SJason Gunthorpe 5339deb0eb7SJason Gunthorpe /** 534aad887f6SJarkko Sakkinen * tpm_get_random() - get random bytes from the TPM's RNG 535aad887f6SJarkko Sakkinen * @chip: a &struct tpm_chip instance, %NULL for the default chip 5369deb0eb7SJason Gunthorpe * @out: destination buffer for the random bytes 5379deb0eb7SJason Gunthorpe * @max: the max number of bytes to write to @out 5389deb0eb7SJason Gunthorpe * 5397aee9c52STomas Winkler * Return: number of random bytes read or a negative error value. 5409deb0eb7SJason Gunthorpe */ 541aad887f6SJarkko Sakkinen int tpm_get_random(struct tpm_chip *chip, u8 *out, size_t max) 5429deb0eb7SJason Gunthorpe { 543433d390fSTomas Winkler int rc; 5449deb0eb7SJason Gunthorpe 545433d390fSTomas Winkler if (!out || max > TPM_MAX_RNG_DATA) 5463e14d83eSJarkko Sakkinen return -EINVAL; 5473e14d83eSJarkko Sakkinen 548fc1d52b7SStefan Berger chip = tpm_find_get_ops(chip); 549aad887f6SJarkko Sakkinen if (!chip) 5509deb0eb7SJason Gunthorpe return -ENODEV; 5519deb0eb7SJason Gunthorpe 552433d390fSTomas Winkler if (chip->flags & TPM_CHIP_FLAG_TPM2) 553433d390fSTomas Winkler rc = tpm2_get_random(chip, out, max); 554433d390fSTomas Winkler else 555433d390fSTomas Winkler rc = tpm1_get_random(chip, out, max); 5569deb0eb7SJason Gunthorpe 5574e26195fSJason Gunthorpe tpm_put_ops(chip); 558433d390fSTomas Winkler return rc; 5599deb0eb7SJason Gunthorpe } 5609deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_get_random); 5619deb0eb7SJason Gunthorpe 562954650efSJarkko Sakkinen /** 563aad887f6SJarkko Sakkinen * tpm_seal_trusted() - seal a trusted key payload 564aad887f6SJarkko Sakkinen * @chip: a &struct tpm_chip instance, %NULL for the default chip 565954650efSJarkko Sakkinen * @options: authentication values and other options 566954650efSJarkko Sakkinen * @payload: the key data in clear and encrypted form 567954650efSJarkko Sakkinen * 568aad887f6SJarkko Sakkinen * Note: only TPM 2.0 chip are supported. TPM 1.x implementation is located in 569aad887f6SJarkko Sakkinen * the keyring subsystem. 570aad887f6SJarkko Sakkinen * 571aad887f6SJarkko Sakkinen * Return: same as with tpm_transmit_cmd() 572954650efSJarkko Sakkinen */ 573aad887f6SJarkko Sakkinen int tpm_seal_trusted(struct tpm_chip *chip, struct trusted_key_payload *payload, 574954650efSJarkko Sakkinen struct trusted_key_options *options) 575954650efSJarkko Sakkinen { 576954650efSJarkko Sakkinen int rc; 577954650efSJarkko Sakkinen 578fc1d52b7SStefan Berger chip = tpm_find_get_ops(chip); 579aad887f6SJarkko Sakkinen if (!chip || !(chip->flags & TPM_CHIP_FLAG_TPM2)) 580954650efSJarkko Sakkinen return -ENODEV; 581954650efSJarkko Sakkinen 582954650efSJarkko Sakkinen rc = tpm2_seal_trusted(chip, payload, options); 583954650efSJarkko Sakkinen 5844e26195fSJason Gunthorpe tpm_put_ops(chip); 585954650efSJarkko Sakkinen return rc; 586954650efSJarkko Sakkinen } 587954650efSJarkko Sakkinen EXPORT_SYMBOL_GPL(tpm_seal_trusted); 588954650efSJarkko Sakkinen 589954650efSJarkko Sakkinen /** 590954650efSJarkko Sakkinen * tpm_unseal_trusted() - unseal a trusted key 591aad887f6SJarkko Sakkinen * @chip: a &struct tpm_chip instance, %NULL for the default chip 592954650efSJarkko Sakkinen * @options: authentication values and other options 593954650efSJarkko Sakkinen * @payload: the key data in clear and encrypted form 594954650efSJarkko Sakkinen * 595aad887f6SJarkko Sakkinen * Note: only TPM 2.0 chip are supported. TPM 1.x implementation is located in 596aad887f6SJarkko Sakkinen * the keyring subsystem. 597aad887f6SJarkko Sakkinen * 598aad887f6SJarkko Sakkinen * Return: same as with tpm_transmit_cmd() 599954650efSJarkko Sakkinen */ 600aad887f6SJarkko Sakkinen int tpm_unseal_trusted(struct tpm_chip *chip, 601aad887f6SJarkko Sakkinen struct trusted_key_payload *payload, 602954650efSJarkko Sakkinen struct trusted_key_options *options) 603954650efSJarkko Sakkinen { 604954650efSJarkko Sakkinen int rc; 605954650efSJarkko Sakkinen 606fc1d52b7SStefan Berger chip = tpm_find_get_ops(chip); 607aad887f6SJarkko Sakkinen if (!chip || !(chip->flags & TPM_CHIP_FLAG_TPM2)) 608954650efSJarkko Sakkinen return -ENODEV; 609954650efSJarkko Sakkinen 610954650efSJarkko Sakkinen rc = tpm2_unseal_trusted(chip, payload, options); 611954650efSJarkko Sakkinen 6124e26195fSJason Gunthorpe tpm_put_ops(chip); 6134e26195fSJason Gunthorpe 614954650efSJarkko Sakkinen return rc; 615954650efSJarkko Sakkinen } 616954650efSJarkko Sakkinen EXPORT_SYMBOL_GPL(tpm_unseal_trusted); 617954650efSJarkko Sakkinen 618313d21eeSJarkko Sakkinen static int __init tpm_init(void) 619313d21eeSJarkko Sakkinen { 620313d21eeSJarkko Sakkinen int rc; 621313d21eeSJarkko Sakkinen 622313d21eeSJarkko Sakkinen tpm_class = class_create(THIS_MODULE, "tpm"); 623313d21eeSJarkko Sakkinen if (IS_ERR(tpm_class)) { 624313d21eeSJarkko Sakkinen pr_err("couldn't create tpm class\n"); 625313d21eeSJarkko Sakkinen return PTR_ERR(tpm_class); 626313d21eeSJarkko Sakkinen } 627313d21eeSJarkko Sakkinen 628fdc915f7SJames Bottomley tpmrm_class = class_create(THIS_MODULE, "tpmrm"); 629fdc915f7SJames Bottomley if (IS_ERR(tpmrm_class)) { 630fdc915f7SJames Bottomley pr_err("couldn't create tpmrm class\n"); 6319e1b74a6STadeusz Struk rc = PTR_ERR(tpmrm_class); 6329e1b74a6STadeusz Struk goto out_destroy_tpm_class; 633fdc915f7SJames Bottomley } 634fdc915f7SJames Bottomley 635fdc915f7SJames Bottomley rc = alloc_chrdev_region(&tpm_devt, 0, 2*TPM_NUM_DEVICES, "tpm"); 636313d21eeSJarkko Sakkinen if (rc < 0) { 637313d21eeSJarkko Sakkinen pr_err("tpm: failed to allocate char dev region\n"); 6389e1b74a6STadeusz Struk goto out_destroy_tpmrm_class; 6399e1b74a6STadeusz Struk } 6409e1b74a6STadeusz Struk 6419e1b74a6STadeusz Struk rc = tpm_dev_common_init(); 6429e1b74a6STadeusz Struk if (rc) { 6439e1b74a6STadeusz Struk pr_err("tpm: failed to allocate char dev region\n"); 6449e1b74a6STadeusz Struk goto out_unreg_chrdev; 645313d21eeSJarkko Sakkinen } 646313d21eeSJarkko Sakkinen 647313d21eeSJarkko Sakkinen return 0; 6489e1b74a6STadeusz Struk 6499e1b74a6STadeusz Struk out_unreg_chrdev: 6509e1b74a6STadeusz Struk unregister_chrdev_region(tpm_devt, 2 * TPM_NUM_DEVICES); 6519e1b74a6STadeusz Struk out_destroy_tpmrm_class: 6529e1b74a6STadeusz Struk class_destroy(tpmrm_class); 6539e1b74a6STadeusz Struk out_destroy_tpm_class: 6549e1b74a6STadeusz Struk class_destroy(tpm_class); 6559e1b74a6STadeusz Struk 6569e1b74a6STadeusz Struk return rc; 657313d21eeSJarkko Sakkinen } 658313d21eeSJarkko Sakkinen 659313d21eeSJarkko Sakkinen static void __exit tpm_exit(void) 660313d21eeSJarkko Sakkinen { 66115516788SStefan Berger idr_destroy(&dev_nums_idr); 662313d21eeSJarkko Sakkinen class_destroy(tpm_class); 663fdc915f7SJames Bottomley class_destroy(tpmrm_class); 664fdc915f7SJames Bottomley unregister_chrdev_region(tpm_devt, 2*TPM_NUM_DEVICES); 6659e1b74a6STadeusz Struk tpm_dev_common_exit(); 666313d21eeSJarkko Sakkinen } 667313d21eeSJarkko Sakkinen 668313d21eeSJarkko Sakkinen subsys_initcall(tpm_init); 669313d21eeSJarkko Sakkinen module_exit(tpm_exit); 670313d21eeSJarkko Sakkinen 6719deb0eb7SJason Gunthorpe MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)"); 6729deb0eb7SJason Gunthorpe MODULE_DESCRIPTION("TPM Driver"); 6739deb0eb7SJason Gunthorpe MODULE_VERSION("2.0"); 6749deb0eb7SJason Gunthorpe MODULE_LICENSE("GPL"); 675