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 65095531f8SJavier Martinez Canillas static int tpm_validate_command(struct tpm_chip *chip, 66745b361eSJarkko Sakkinen struct tpm_space *space, 67745b361eSJarkko Sakkinen const u8 *cmd, 6858472f5cSJarkko Sakkinen size_t len) 6958472f5cSJarkko Sakkinen { 7058472f5cSJarkko Sakkinen const struct tpm_input_header *header = (const void *)cmd; 7158472f5cSJarkko Sakkinen int i; 7258472f5cSJarkko Sakkinen u32 cc; 7358472f5cSJarkko Sakkinen u32 attrs; 7458472f5cSJarkko Sakkinen unsigned int nr_handles; 7558472f5cSJarkko Sakkinen 7658472f5cSJarkko Sakkinen if (len < TPM_HEADER_SIZE) 77095531f8SJavier Martinez Canillas return -EINVAL; 7858472f5cSJarkko Sakkinen 79745b361eSJarkko Sakkinen if (!space) 80095531f8SJavier Martinez Canillas return 0; 81745b361eSJarkko Sakkinen 8258472f5cSJarkko Sakkinen if (chip->flags & TPM_CHIP_FLAG_TPM2 && chip->nr_commands) { 8358472f5cSJarkko Sakkinen cc = be32_to_cpu(header->ordinal); 8458472f5cSJarkko Sakkinen 8558472f5cSJarkko Sakkinen i = tpm2_find_cc(chip, cc); 8658472f5cSJarkko Sakkinen if (i < 0) { 8758472f5cSJarkko Sakkinen dev_dbg(&chip->dev, "0x%04X is an invalid command\n", 8858472f5cSJarkko Sakkinen cc); 89095531f8SJavier Martinez Canillas return -EOPNOTSUPP; 9058472f5cSJarkko Sakkinen } 9158472f5cSJarkko Sakkinen 9258472f5cSJarkko Sakkinen attrs = chip->cc_attrs_tbl[i]; 9358472f5cSJarkko Sakkinen nr_handles = 9458472f5cSJarkko Sakkinen 4 * ((attrs >> TPM2_CC_ATTR_CHANDLES) & GENMASK(2, 0)); 9558472f5cSJarkko Sakkinen if (len < TPM_HEADER_SIZE + 4 * nr_handles) 9658472f5cSJarkko Sakkinen goto err_len; 9758472f5cSJarkko Sakkinen } 9858472f5cSJarkko Sakkinen 99095531f8SJavier Martinez Canillas return 0; 10058472f5cSJarkko Sakkinen err_len: 10158472f5cSJarkko Sakkinen dev_dbg(&chip->dev, 10258472f5cSJarkko Sakkinen "%s: insufficient command length %zu", __func__, len); 103095531f8SJavier Martinez Canillas return -EINVAL; 10458472f5cSJarkko Sakkinen } 10558472f5cSJarkko Sakkinen 106627448e8STomas Winkler static int tpm_request_locality(struct tpm_chip *chip, unsigned int flags) 107888d867dSTomas Winkler { 108888d867dSTomas Winkler int rc; 109888d867dSTomas Winkler 11058bac8ccSJarkko Sakkinen if (flags & TPM_TRANSMIT_NESTED) 111627448e8STomas Winkler return 0; 112627448e8STomas Winkler 113888d867dSTomas Winkler if (!chip->ops->request_locality) 114888d867dSTomas Winkler return 0; 115888d867dSTomas Winkler 116888d867dSTomas Winkler rc = chip->ops->request_locality(chip, 0); 117888d867dSTomas Winkler if (rc < 0) 118888d867dSTomas Winkler return rc; 119888d867dSTomas Winkler 120888d867dSTomas Winkler chip->locality = rc; 121888d867dSTomas Winkler 122888d867dSTomas Winkler return 0; 123888d867dSTomas Winkler } 124888d867dSTomas Winkler 125627448e8STomas Winkler static void tpm_relinquish_locality(struct tpm_chip *chip, unsigned int flags) 126888d867dSTomas Winkler { 127888d867dSTomas Winkler int rc; 128888d867dSTomas Winkler 12958bac8ccSJarkko Sakkinen if (flags & TPM_TRANSMIT_NESTED) 130627448e8STomas Winkler return; 131627448e8STomas Winkler 132888d867dSTomas Winkler if (!chip->ops->relinquish_locality) 133888d867dSTomas Winkler return; 134888d867dSTomas Winkler 135888d867dSTomas Winkler rc = chip->ops->relinquish_locality(chip, chip->locality); 136888d867dSTomas Winkler if (rc) 137888d867dSTomas Winkler dev_err(&chip->dev, "%s: : error %d\n", __func__, rc); 138888d867dSTomas Winkler 139888d867dSTomas Winkler chip->locality = -1; 140888d867dSTomas Winkler } 141888d867dSTomas Winkler 142627448e8STomas Winkler static int tpm_cmd_ready(struct tpm_chip *chip, unsigned int flags) 143627448e8STomas Winkler { 14458bac8ccSJarkko Sakkinen if (flags & TPM_TRANSMIT_NESTED) 145627448e8STomas Winkler return 0; 146627448e8STomas Winkler 147627448e8STomas Winkler if (!chip->ops->cmd_ready) 148627448e8STomas Winkler return 0; 149627448e8STomas Winkler 150627448e8STomas Winkler return chip->ops->cmd_ready(chip); 151627448e8STomas Winkler } 152627448e8STomas Winkler 153627448e8STomas Winkler static int tpm_go_idle(struct tpm_chip *chip, unsigned int flags) 154627448e8STomas Winkler { 15558bac8ccSJarkko Sakkinen if (flags & TPM_TRANSMIT_NESTED) 156627448e8STomas Winkler return 0; 157627448e8STomas Winkler 158627448e8STomas Winkler if (!chip->ops->go_idle) 159627448e8STomas Winkler return 0; 160627448e8STomas Winkler 161627448e8STomas Winkler return chip->ops->go_idle(chip); 162627448e8STomas Winkler } 163627448e8STomas Winkler 164e2fb992dSJames Bottomley static ssize_t tpm_try_transmit(struct tpm_chip *chip, 165e2fb992dSJames Bottomley struct tpm_space *space, 166e2fb992dSJames Bottomley u8 *buf, size_t bufsiz, 167e2fb992dSJames Bottomley unsigned int flags) 1689deb0eb7SJason Gunthorpe { 169745b361eSJarkko Sakkinen struct tpm_output_header *header = (void *)buf; 170745b361eSJarkko Sakkinen int rc; 171745b361eSJarkko Sakkinen ssize_t len = 0; 1729deb0eb7SJason Gunthorpe u32 count, ordinal; 1739deb0eb7SJason Gunthorpe unsigned long stop; 174877c57d0SJarkko Sakkinen bool need_locality; 1759deb0eb7SJason Gunthorpe 176095531f8SJavier Martinez Canillas rc = tpm_validate_command(chip, space, buf, bufsiz); 177095531f8SJavier Martinez Canillas if (rc == -EINVAL) 178095531f8SJavier Martinez Canillas return rc; 179095531f8SJavier Martinez Canillas /* 180095531f8SJavier Martinez Canillas * If the command is not implemented by the TPM, synthesize a 181095531f8SJavier Martinez Canillas * response with a TPM2_RC_COMMAND_CODE return for user-space. 182095531f8SJavier Martinez Canillas */ 183095531f8SJavier Martinez Canillas if (rc == -EOPNOTSUPP) { 184095531f8SJavier Martinez Canillas header->length = cpu_to_be32(sizeof(*header)); 185095531f8SJavier Martinez Canillas header->tag = cpu_to_be16(TPM2_ST_NO_SESSIONS); 186095531f8SJavier Martinez Canillas header->return_code = cpu_to_be32(TPM2_RC_COMMAND_CODE | 187095531f8SJavier Martinez Canillas TSS2_RESMGR_TPM_RC_LAYER); 18836a11029SRicardo Schwarzmeier return sizeof(*header); 189095531f8SJavier Martinez Canillas } 190ebfd7532SJarkko Sakkinen 1919deb0eb7SJason Gunthorpe if (bufsiz > TPM_BUFSIZE) 1929deb0eb7SJason Gunthorpe bufsiz = TPM_BUFSIZE; 1939deb0eb7SJason Gunthorpe 1949deb0eb7SJason Gunthorpe count = be32_to_cpu(*((__be32 *) (buf + 2))); 1959deb0eb7SJason Gunthorpe ordinal = be32_to_cpu(*((__be32 *) (buf + 6))); 1969deb0eb7SJason Gunthorpe if (count == 0) 1979deb0eb7SJason Gunthorpe return -ENODATA; 1989deb0eb7SJason Gunthorpe if (count > bufsiz) { 1998cfffc9dSJason Gunthorpe dev_err(&chip->dev, 2009deb0eb7SJason Gunthorpe "invalid count value %x %zx\n", count, bufsiz); 2019deb0eb7SJason Gunthorpe return -E2BIG; 2029deb0eb7SJason Gunthorpe } 2039deb0eb7SJason Gunthorpe 20458bac8ccSJarkko Sakkinen if (!(flags & TPM_TRANSMIT_UNLOCKED) && !(flags & TPM_TRANSMIT_NESTED)) 2059deb0eb7SJason Gunthorpe mutex_lock(&chip->tpm_mutex); 2069deb0eb7SJason Gunthorpe 207b3e958ceSAzhar Shaikh if (chip->ops->clk_enable != NULL) 208b3e958ceSAzhar Shaikh chip->ops->clk_enable(chip, true); 209b3e958ceSAzhar Shaikh 210877c57d0SJarkko Sakkinen /* Store the decision as chip->locality will be changed. */ 211877c57d0SJarkko Sakkinen need_locality = chip->locality == -1; 212877c57d0SJarkko Sakkinen 213627448e8STomas Winkler if (need_locality) { 214627448e8STomas Winkler rc = tpm_request_locality(chip, flags); 21501f54664STomas Winkler if (rc < 0) { 21601f54664STomas Winkler need_locality = false; 21701f54664STomas Winkler goto out_locality; 21801f54664STomas Winkler } 219877c57d0SJarkko Sakkinen } 220877c57d0SJarkko Sakkinen 221627448e8STomas Winkler rc = tpm_cmd_ready(chip, flags); 222627448e8STomas Winkler if (rc) 22301f54664STomas Winkler goto out_locality; 224888d867dSTomas Winkler 225745b361eSJarkko Sakkinen rc = tpm2_prepare_space(chip, space, ordinal, buf); 226745b361eSJarkko Sakkinen if (rc) 227745b361eSJarkko Sakkinen goto out; 228745b361eSJarkko Sakkinen 22962c09e12SWinkler, Tomas rc = chip->ops->send(chip, buf, count); 2309deb0eb7SJason Gunthorpe if (rc < 0) { 231402149c6SStefan Berger if (rc != -EPIPE) 2328cfffc9dSJason Gunthorpe dev_err(&chip->dev, 233f5595f5bSJarkko Sakkinen "%s: send(): error %d\n", __func__, rc); 2349deb0eb7SJason Gunthorpe goto out; 2359deb0eb7SJason Gunthorpe } 2369deb0eb7SJason Gunthorpe 237f5595f5bSJarkko Sakkinen /* A sanity check. send() should just return zero on success e.g. 238f5595f5bSJarkko Sakkinen * not the command length. 239f5595f5bSJarkko Sakkinen */ 240f5595f5bSJarkko Sakkinen if (rc > 0) { 241f5595f5bSJarkko Sakkinen dev_warn(&chip->dev, 242f5595f5bSJarkko Sakkinen "%s: send(): invalid value %d\n", __func__, rc); 243f5595f5bSJarkko Sakkinen rc = 0; 244f5595f5bSJarkko Sakkinen } 245f5595f5bSJarkko Sakkinen 246570a3609SChristophe Ricard if (chip->flags & TPM_CHIP_FLAG_IRQ) 2479deb0eb7SJason Gunthorpe goto out_recv; 2489deb0eb7SJason Gunthorpe 249d856c00fSTomas Winkler stop = jiffies + tpm_calc_ordinal_duration(chip, ordinal); 2509deb0eb7SJason Gunthorpe do { 2515f82e9f0SJason Gunthorpe u8 status = chip->ops->status(chip); 2525f82e9f0SJason Gunthorpe if ((status & chip->ops->req_complete_mask) == 2535f82e9f0SJason Gunthorpe chip->ops->req_complete_val) 2549deb0eb7SJason Gunthorpe goto out_recv; 2559deb0eb7SJason Gunthorpe 2565f82e9f0SJason Gunthorpe if (chip->ops->req_canceled(chip, status)) { 2578cfffc9dSJason Gunthorpe dev_err(&chip->dev, "Operation Canceled\n"); 2589deb0eb7SJason Gunthorpe rc = -ECANCELED; 2599deb0eb7SJason Gunthorpe goto out; 2609deb0eb7SJason Gunthorpe } 2619deb0eb7SJason Gunthorpe 26259f5a6b0SNayna Jain tpm_msleep(TPM_TIMEOUT_POLL); 2639deb0eb7SJason Gunthorpe rmb(); 2649deb0eb7SJason Gunthorpe } while (time_before(jiffies, stop)); 2659deb0eb7SJason Gunthorpe 2665f82e9f0SJason Gunthorpe chip->ops->cancel(chip); 2678cfffc9dSJason Gunthorpe dev_err(&chip->dev, "Operation Timed out\n"); 2689deb0eb7SJason Gunthorpe rc = -ETIME; 2699deb0eb7SJason Gunthorpe goto out; 2709deb0eb7SJason Gunthorpe 2719deb0eb7SJason Gunthorpe out_recv: 27262c09e12SWinkler, Tomas len = chip->ops->recv(chip, buf, bufsiz); 273745b361eSJarkko Sakkinen if (len < 0) { 274745b361eSJarkko Sakkinen rc = len; 2758cfffc9dSJason Gunthorpe dev_err(&chip->dev, 276745b361eSJarkko Sakkinen "tpm_transmit: tpm_recv: error %d\n", rc); 277a147918eSJarkko Sakkinen goto out; 278745b361eSJarkko Sakkinen } else if (len < TPM_HEADER_SIZE) { 279a147918eSJarkko Sakkinen rc = -EFAULT; 280a147918eSJarkko Sakkinen goto out; 281a147918eSJarkko Sakkinen } 282a147918eSJarkko Sakkinen 283745b361eSJarkko Sakkinen if (len != be32_to_cpu(header->length)) { 284745b361eSJarkko Sakkinen rc = -EFAULT; 285a147918eSJarkko Sakkinen goto out; 286745b361eSJarkko Sakkinen } 287745b361eSJarkko Sakkinen 288745b361eSJarkko Sakkinen rc = tpm2_commit_space(chip, space, ordinal, buf, &len); 289627448e8STomas Winkler if (rc) 290627448e8STomas Winkler dev_err(&chip->dev, "tpm2_commit_space: error %d\n", rc); 291a147918eSJarkko Sakkinen 2929deb0eb7SJason Gunthorpe out: 29301f54664STomas Winkler /* may fail but do not override previous error value in rc */ 29401f54664STomas Winkler tpm_go_idle(chip, flags); 295888d867dSTomas Winkler 29601f54664STomas Winkler out_locality: 297888d867dSTomas Winkler if (need_locality) 298627448e8STomas Winkler tpm_relinquish_locality(chip, flags); 299888d867dSTomas Winkler 300b3e958ceSAzhar Shaikh if (chip->ops->clk_enable != NULL) 301b3e958ceSAzhar Shaikh chip->ops->clk_enable(chip, false); 302b3e958ceSAzhar Shaikh 30358bac8ccSJarkko Sakkinen if (!(flags & TPM_TRANSMIT_UNLOCKED) && !(flags & TPM_TRANSMIT_NESTED)) 3049deb0eb7SJason Gunthorpe mutex_unlock(&chip->tpm_mutex); 305745b361eSJarkko Sakkinen return rc ? rc : len; 3069deb0eb7SJason Gunthorpe } 3079deb0eb7SJason Gunthorpe 308f865c196SWinkler, Tomas /** 309e2fb992dSJames Bottomley * tpm_transmit - Internal kernel interface to transmit TPM commands. 310*412eb585SJarkko Sakkinen * @chip: a TPM chip to use 311*412eb585SJarkko Sakkinen * @space: a TPM space 312*412eb585SJarkko Sakkinen * @buf: a TPM command buffer 313e2fb992dSJames Bottomley * @bufsiz: length of the TPM command buffer 314*412eb585SJarkko Sakkinen * @flags: TPM transmit flags 315e2fb992dSJames Bottomley * 316*412eb585SJarkko Sakkinen * A wrapper around tpm_try_transmit() that handles TPM2_RC_RETRY returns from 317*412eb585SJarkko Sakkinen * the TPM and retransmits the command after a delay up to a maximum wait of 318*412eb585SJarkko Sakkinen * TPM2_DURATION_LONG. 319e2fb992dSJames Bottomley * 320*412eb585SJarkko Sakkinen * Note that TPM 1.x never returns TPM2_RC_RETRY so the retry logic is TPM 2.0 321*412eb585SJarkko Sakkinen * only. 322e2fb992dSJames Bottomley * 323e2fb992dSJames Bottomley * Return: 324*412eb585SJarkko Sakkinen * * The response length - OK 325*412eb585SJarkko Sakkinen * * -errno - A system error 326e2fb992dSJames Bottomley */ 327e2fb992dSJames Bottomley ssize_t tpm_transmit(struct tpm_chip *chip, struct tpm_space *space, 328e2fb992dSJames Bottomley u8 *buf, size_t bufsiz, unsigned int flags) 329e2fb992dSJames Bottomley { 330e2fb992dSJames Bottomley struct tpm_output_header *header = (struct tpm_output_header *)buf; 331e2fb992dSJames Bottomley /* space for header and handles */ 332e2fb992dSJames Bottomley u8 save[TPM_HEADER_SIZE + 3*sizeof(u32)]; 333e2fb992dSJames Bottomley unsigned int delay_msec = TPM2_DURATION_SHORT; 334e2fb992dSJames Bottomley u32 rc = 0; 335e2fb992dSJames Bottomley ssize_t ret; 336e2fb992dSJames Bottomley const size_t save_size = min(space ? sizeof(save) : TPM_HEADER_SIZE, 337e2fb992dSJames Bottomley bufsiz); 3382be8ffedSJames Bottomley /* the command code is where the return code will be */ 3392be8ffedSJames Bottomley u32 cc = be32_to_cpu(header->return_code); 340e2fb992dSJames Bottomley 341e2fb992dSJames Bottomley /* 342e2fb992dSJames Bottomley * Subtlety here: if we have a space, the handles will be 343e2fb992dSJames Bottomley * transformed, so when we restore the header we also have to 344e2fb992dSJames Bottomley * restore the handles. 345e2fb992dSJames Bottomley */ 346e2fb992dSJames Bottomley memcpy(save, buf, save_size); 347e2fb992dSJames Bottomley 348e2fb992dSJames Bottomley for (;;) { 349e2fb992dSJames Bottomley ret = tpm_try_transmit(chip, space, buf, bufsiz, flags); 350e2fb992dSJames Bottomley if (ret < 0) 351e2fb992dSJames Bottomley break; 352e2fb992dSJames Bottomley rc = be32_to_cpu(header->return_code); 3532be8ffedSJames Bottomley if (rc != TPM2_RC_RETRY && rc != TPM2_RC_TESTING) 3542be8ffedSJames Bottomley break; 3552be8ffedSJames Bottomley /* 3562be8ffedSJames Bottomley * return immediately if self test returns test 3572be8ffedSJames Bottomley * still running to shorten boot time. 3582be8ffedSJames Bottomley */ 3592be8ffedSJames Bottomley if (rc == TPM2_RC_TESTING && cc == TPM2_CC_SELF_TEST) 360e2fb992dSJames Bottomley break; 36192980756SNayna Jain 362e2fb992dSJames Bottomley if (delay_msec > TPM2_DURATION_LONG) { 3632be8ffedSJames Bottomley if (rc == TPM2_RC_RETRY) 3642be8ffedSJames Bottomley dev_err(&chip->dev, "in retry loop\n"); 3652be8ffedSJames Bottomley else 3662be8ffedSJames Bottomley dev_err(&chip->dev, 3672be8ffedSJames Bottomley "self test is still running\n"); 368e2fb992dSJames Bottomley break; 369e2fb992dSJames Bottomley } 370e2fb992dSJames Bottomley tpm_msleep(delay_msec); 37192980756SNayna Jain delay_msec *= 2; 372e2fb992dSJames Bottomley memcpy(buf, save, save_size); 373e2fb992dSJames Bottomley } 374e2fb992dSJames Bottomley return ret; 375e2fb992dSJames Bottomley } 376*412eb585SJarkko Sakkinen 377e2fb992dSJames Bottomley /** 37865520d46SWinkler, Tomas * tpm_transmit_cmd - send a tpm command to the device 379*412eb585SJarkko Sakkinen * @chip: a TPM chip to use 380*412eb585SJarkko Sakkinen * @space: a TPM space 381*412eb585SJarkko Sakkinen * @buf: a TPM command buffer 382c659af78SStefan Berger * @min_rsp_body_length: minimum expected length of response body 383*412eb585SJarkko Sakkinen * @flags: TPM transmit flags 384f865c196SWinkler, Tomas * @desc: command description used in the error message 385f865c196SWinkler, Tomas * 386f865c196SWinkler, Tomas * Return: 387*412eb585SJarkko Sakkinen * * 0 - OK 388*412eb585SJarkko Sakkinen * * -errno - A system error 389*412eb585SJarkko Sakkinen * * TPM_RC - A TPM error 390f865c196SWinkler, Tomas */ 391745b361eSJarkko Sakkinen ssize_t tpm_transmit_cmd(struct tpm_chip *chip, struct tpm_space *space, 392*412eb585SJarkko Sakkinen struct tpm_buf *buf, size_t min_rsp_body_length, 393*412eb585SJarkko Sakkinen unsigned int flags, const char *desc) 3949deb0eb7SJason Gunthorpe { 395*412eb585SJarkko Sakkinen const struct tpm_output_header *header = 396*412eb585SJarkko Sakkinen (struct tpm_output_header *)buf->data; 3979deb0eb7SJason Gunthorpe int err; 398c659af78SStefan Berger ssize_t len; 3999deb0eb7SJason Gunthorpe 400*412eb585SJarkko Sakkinen len = tpm_transmit(chip, space, buf->data, PAGE_SIZE, flags); 4019deb0eb7SJason Gunthorpe if (len < 0) 4029deb0eb7SJason Gunthorpe return len; 40387155b73SJarkko Sakkinen 40487155b73SJarkko Sakkinen err = be32_to_cpu(header->return_code); 4050d6d0d62SJavier Martinez Canillas if (err != 0 && err != TPM_ERR_DISABLED && err != TPM_ERR_DEACTIVATED 40608a8112aSJerry Snitselaar && err != TPM2_RC_TESTING && desc) 4078cfffc9dSJason Gunthorpe dev_err(&chip->dev, "A TPM error (%d) occurred %s\n", err, 40871ed848fSJarkko Sakkinen desc); 409c659af78SStefan Berger if (err) 4109deb0eb7SJason Gunthorpe return err; 411c659af78SStefan Berger 412c659af78SStefan Berger if (len < min_rsp_body_length + TPM_HEADER_SIZE) 413c659af78SStefan Berger return -EFAULT; 414c659af78SStefan Berger 415c659af78SStefan Berger return 0; 4169deb0eb7SJason Gunthorpe } 417be4c9acfSStefan Berger EXPORT_SYMBOL_GPL(tpm_transmit_cmd); 4189deb0eb7SJason Gunthorpe 4199deb0eb7SJason Gunthorpe int tpm_get_timeouts(struct tpm_chip *chip) 4209deb0eb7SJason Gunthorpe { 421d1d253cfSJason Gunthorpe if (chip->flags & TPM_CHIP_FLAG_HAVE_TIMEOUTS) 422d1d253cfSJason Gunthorpe return 0; 423d1d253cfSJason Gunthorpe 42470a3199aSTomas Winkler if (chip->flags & TPM_CHIP_FLAG_TPM2) 42570a3199aSTomas Winkler return tpm2_get_timeouts(chip); 42670a3199aSTomas Winkler else 42770a3199aSTomas Winkler return tpm1_get_timeouts(chip); 4289deb0eb7SJason Gunthorpe } 4299deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_get_timeouts); 4309deb0eb7SJason Gunthorpe 4319deb0eb7SJason Gunthorpe /** 432aad887f6SJarkko Sakkinen * tpm_is_tpm2 - do we a have a TPM2 chip? 433aad887f6SJarkko Sakkinen * @chip: a &struct tpm_chip instance, %NULL for the default chip 434954650efSJarkko Sakkinen * 435aad887f6SJarkko Sakkinen * Return: 436aad887f6SJarkko Sakkinen * 1 if we have a TPM2 chip. 437aad887f6SJarkko Sakkinen * 0 if we don't have a TPM2 chip. 438aad887f6SJarkko Sakkinen * A negative number for system errors (errno). 439954650efSJarkko Sakkinen */ 440aad887f6SJarkko Sakkinen int tpm_is_tpm2(struct tpm_chip *chip) 441954650efSJarkko Sakkinen { 442954650efSJarkko Sakkinen int rc; 443954650efSJarkko Sakkinen 444fc1d52b7SStefan Berger chip = tpm_find_get_ops(chip); 445aad887f6SJarkko Sakkinen if (!chip) 446954650efSJarkko Sakkinen return -ENODEV; 447954650efSJarkko Sakkinen 448954650efSJarkko Sakkinen rc = (chip->flags & TPM_CHIP_FLAG_TPM2) != 0; 449954650efSJarkko Sakkinen 4504e26195fSJason Gunthorpe tpm_put_ops(chip); 451954650efSJarkko Sakkinen 452954650efSJarkko Sakkinen return rc; 453954650efSJarkko Sakkinen } 454954650efSJarkko Sakkinen EXPORT_SYMBOL_GPL(tpm_is_tpm2); 455954650efSJarkko Sakkinen 456954650efSJarkko Sakkinen /** 457aad887f6SJarkko Sakkinen * tpm_pcr_read - read a PCR value from SHA1 bank 458aad887f6SJarkko Sakkinen * @chip: a &struct tpm_chip instance, %NULL for the default chip 459aad887f6SJarkko Sakkinen * @pcr_idx: the PCR to be retrieved 460aad887f6SJarkko Sakkinen * @res_buf: the value of the PCR 4619deb0eb7SJason Gunthorpe * 462aad887f6SJarkko Sakkinen * Return: same as with tpm_transmit_cmd() 4639deb0eb7SJason Gunthorpe */ 46495adc6b4STomas Winkler int tpm_pcr_read(struct tpm_chip *chip, u32 pcr_idx, u8 *res_buf) 4659deb0eb7SJason Gunthorpe { 4669deb0eb7SJason Gunthorpe int rc; 4679deb0eb7SJason Gunthorpe 468fc1d52b7SStefan Berger chip = tpm_find_get_ops(chip); 469aad887f6SJarkko Sakkinen if (!chip) 4709deb0eb7SJason Gunthorpe return -ENODEV; 471d4a31756STomas Winkler 4727a1d7e6dSJarkko Sakkinen if (chip->flags & TPM_CHIP_FLAG_TPM2) 4737a1d7e6dSJarkko Sakkinen rc = tpm2_pcr_read(chip, pcr_idx, res_buf); 4747a1d7e6dSJarkko Sakkinen else 475cfddcb05STomas Winkler rc = tpm1_pcr_read(chip, pcr_idx, res_buf); 476d4a31756STomas Winkler 4774e26195fSJason Gunthorpe tpm_put_ops(chip); 4789deb0eb7SJason Gunthorpe return rc; 4799deb0eb7SJason Gunthorpe } 4809deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_pcr_read); 4819deb0eb7SJason Gunthorpe 4829deb0eb7SJason Gunthorpe /** 483aad887f6SJarkko Sakkinen * tpm_pcr_extend - extend a PCR value in SHA1 bank. 484aad887f6SJarkko Sakkinen * @chip: a &struct tpm_chip instance, %NULL for the default chip 485aad887f6SJarkko Sakkinen * @pcr_idx: the PCR to be retrieved 486aad887f6SJarkko Sakkinen * @hash: the hash value used to extend the PCR value 4879deb0eb7SJason Gunthorpe * 488aad887f6SJarkko Sakkinen * Note: with TPM 2.0 extends also those banks with a known digest size to the 489aad887f6SJarkko Sakkinen * cryto subsystem in order to prevent malicious use of those PCR banks. In the 490aad887f6SJarkko Sakkinen * future we should dynamically determine digest sizes. 491aad887f6SJarkko Sakkinen * 492aad887f6SJarkko Sakkinen * Return: same as with tpm_transmit_cmd() 4939deb0eb7SJason Gunthorpe */ 49495adc6b4STomas Winkler int tpm_pcr_extend(struct tpm_chip *chip, u32 pcr_idx, const u8 *hash) 4959deb0eb7SJason Gunthorpe { 4969deb0eb7SJason Gunthorpe int rc; 497c1f92b4bSNayna Jain struct tpm2_digest digest_list[ARRAY_SIZE(chip->active_banks)]; 498c1f92b4bSNayna Jain u32 count = 0; 499c1f92b4bSNayna Jain int i; 5009deb0eb7SJason Gunthorpe 501fc1d52b7SStefan Berger chip = tpm_find_get_ops(chip); 502aad887f6SJarkko Sakkinen if (!chip) 5039deb0eb7SJason Gunthorpe return -ENODEV; 5049deb0eb7SJason Gunthorpe 5057a1d7e6dSJarkko Sakkinen if (chip->flags & TPM_CHIP_FLAG_TPM2) { 506c1f92b4bSNayna Jain memset(digest_list, 0, sizeof(digest_list)); 507c1f92b4bSNayna Jain 50870ea1636SDan Carpenter for (i = 0; i < ARRAY_SIZE(chip->active_banks) && 50970ea1636SDan Carpenter chip->active_banks[i] != TPM2_ALG_ERROR; i++) { 510c1f92b4bSNayna Jain digest_list[i].alg_id = chip->active_banks[i]; 511c1f92b4bSNayna Jain memcpy(digest_list[i].digest, hash, TPM_DIGEST_SIZE); 512c1f92b4bSNayna Jain count++; 513c1f92b4bSNayna Jain } 514c1f92b4bSNayna Jain 515c1f92b4bSNayna Jain rc = tpm2_pcr_extend(chip, pcr_idx, count, digest_list); 5164e26195fSJason Gunthorpe tpm_put_ops(chip); 5177a1d7e6dSJarkko Sakkinen return rc; 5187a1d7e6dSJarkko Sakkinen } 5197a1d7e6dSJarkko Sakkinen 520175d5b2aSRoberto Sassu rc = tpm1_pcr_extend(chip, pcr_idx, hash, 5219deb0eb7SJason Gunthorpe "attempting extend a PCR value"); 5224e26195fSJason Gunthorpe tpm_put_ops(chip); 5239deb0eb7SJason Gunthorpe return rc; 5249deb0eb7SJason Gunthorpe } 5259deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_pcr_extend); 5269deb0eb7SJason Gunthorpe 5279deb0eb7SJason Gunthorpe /** 528aad887f6SJarkko Sakkinen * tpm_send - send a TPM command 529aad887f6SJarkko Sakkinen * @chip: a &struct tpm_chip instance, %NULL for the default chip 530aad887f6SJarkko Sakkinen * @cmd: a TPM command buffer 531aad887f6SJarkko Sakkinen * @buflen: the length of the TPM command buffer 532aad887f6SJarkko Sakkinen * 533aad887f6SJarkko Sakkinen * Return: same as with tpm_transmit_cmd() 534aad887f6SJarkko Sakkinen */ 535aad887f6SJarkko Sakkinen int tpm_send(struct tpm_chip *chip, void *cmd, size_t buflen) 5369deb0eb7SJason Gunthorpe { 537*412eb585SJarkko Sakkinen struct tpm_buf buf; 5389deb0eb7SJason Gunthorpe int rc; 5399deb0eb7SJason Gunthorpe 540fc1d52b7SStefan Berger chip = tpm_find_get_ops(chip); 541aad887f6SJarkko Sakkinen if (!chip) 5429deb0eb7SJason Gunthorpe return -ENODEV; 5439deb0eb7SJason Gunthorpe 544*412eb585SJarkko Sakkinen rc = tpm_buf_init(&buf, 0, 0); 545*412eb585SJarkko Sakkinen if (rc) 546*412eb585SJarkko Sakkinen goto out; 547*412eb585SJarkko Sakkinen 548*412eb585SJarkko Sakkinen memcpy(buf.data, cmd, buflen); 549*412eb585SJarkko Sakkinen rc = tpm_transmit_cmd(chip, NULL, &buf, 0, 0, 550aad887f6SJarkko Sakkinen "attempting to a send a command"); 551*412eb585SJarkko Sakkinen tpm_buf_destroy(&buf); 552*412eb585SJarkko Sakkinen out: 5534e26195fSJason Gunthorpe tpm_put_ops(chip); 5549deb0eb7SJason Gunthorpe return rc; 5559deb0eb7SJason Gunthorpe } 5569deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_send); 5579deb0eb7SJason Gunthorpe 558b03c4370STomas Winkler int tpm_auto_startup(struct tpm_chip *chip) 559b03c4370STomas Winkler { 560b03c4370STomas Winkler int rc; 561b03c4370STomas Winkler 562b03c4370STomas Winkler if (!(chip->ops->flags & TPM_OPS_AUTO_STARTUP)) 563b03c4370STomas Winkler return 0; 564b03c4370STomas Winkler 565b03c4370STomas Winkler if (chip->flags & TPM_CHIP_FLAG_TPM2) 566b03c4370STomas Winkler rc = tpm2_auto_startup(chip); 567b03c4370STomas Winkler else 568b03c4370STomas Winkler rc = tpm1_auto_startup(chip); 569b03c4370STomas Winkler 570b03c4370STomas Winkler return rc; 571b03c4370STomas Winkler } 572b03c4370STomas Winkler 5739deb0eb7SJason Gunthorpe /* 5749deb0eb7SJason Gunthorpe * We are about to suspend. Save the TPM state 5759deb0eb7SJason Gunthorpe * so that it can be restored. 5769deb0eb7SJason Gunthorpe */ 5779deb0eb7SJason Gunthorpe int tpm_pm_suspend(struct device *dev) 5789deb0eb7SJason Gunthorpe { 579ec03c50bSStefan Berger struct tpm_chip *chip = dev_get_drvdata(dev); 580c82a330cSTomas Winkler int rc = 0; 5819deb0eb7SJason Gunthorpe 582c82a330cSTomas Winkler if (!chip) 5839deb0eb7SJason Gunthorpe return -ENODEV; 5849deb0eb7SJason Gunthorpe 585b5d0ebc9SEnric Balletbo i Serra if (chip->flags & TPM_CHIP_FLAG_ALWAYS_POWERED) 586b5d0ebc9SEnric Balletbo i Serra return 0; 587b5d0ebc9SEnric Balletbo i Serra 588c82a330cSTomas Winkler if (chip->flags & TPM_CHIP_FLAG_TPM2) 58974d6b3ceSJarkko Sakkinen tpm2_shutdown(chip, TPM2_SU_STATE); 590c82a330cSTomas Winkler else 591c82a330cSTomas Winkler rc = tpm1_pm_suspend(chip, tpm_suspend_pcr); 5929deb0eb7SJason Gunthorpe 5939deb0eb7SJason Gunthorpe return rc; 5949deb0eb7SJason Gunthorpe } 5959deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_pm_suspend); 5969deb0eb7SJason Gunthorpe 5979deb0eb7SJason Gunthorpe /* 5989deb0eb7SJason Gunthorpe * Resume from a power safe. The BIOS already restored 5999deb0eb7SJason Gunthorpe * the TPM state. 6009deb0eb7SJason Gunthorpe */ 6019deb0eb7SJason Gunthorpe int tpm_pm_resume(struct device *dev) 6029deb0eb7SJason Gunthorpe { 603ec03c50bSStefan Berger struct tpm_chip *chip = dev_get_drvdata(dev); 6049deb0eb7SJason Gunthorpe 6059deb0eb7SJason Gunthorpe if (chip == NULL) 6069deb0eb7SJason Gunthorpe return -ENODEV; 6079deb0eb7SJason Gunthorpe 6089deb0eb7SJason Gunthorpe return 0; 6099deb0eb7SJason Gunthorpe } 6109deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_pm_resume); 6119deb0eb7SJason Gunthorpe 6129deb0eb7SJason Gunthorpe /** 613aad887f6SJarkko Sakkinen * tpm_get_random() - get random bytes from the TPM's RNG 614aad887f6SJarkko Sakkinen * @chip: a &struct tpm_chip instance, %NULL for the default chip 6159deb0eb7SJason Gunthorpe * @out: destination buffer for the random bytes 6169deb0eb7SJason Gunthorpe * @max: the max number of bytes to write to @out 6179deb0eb7SJason Gunthorpe * 6187aee9c52STomas Winkler * Return: number of random bytes read or a negative error value. 6199deb0eb7SJason Gunthorpe */ 620aad887f6SJarkko Sakkinen int tpm_get_random(struct tpm_chip *chip, u8 *out, size_t max) 6219deb0eb7SJason Gunthorpe { 622433d390fSTomas Winkler int rc; 6239deb0eb7SJason Gunthorpe 624433d390fSTomas Winkler if (!out || max > TPM_MAX_RNG_DATA) 6253e14d83eSJarkko Sakkinen return -EINVAL; 6263e14d83eSJarkko Sakkinen 627fc1d52b7SStefan Berger chip = tpm_find_get_ops(chip); 628aad887f6SJarkko Sakkinen if (!chip) 6299deb0eb7SJason Gunthorpe return -ENODEV; 6309deb0eb7SJason Gunthorpe 631433d390fSTomas Winkler if (chip->flags & TPM_CHIP_FLAG_TPM2) 632433d390fSTomas Winkler rc = tpm2_get_random(chip, out, max); 633433d390fSTomas Winkler else 634433d390fSTomas Winkler rc = tpm1_get_random(chip, out, max); 6359deb0eb7SJason Gunthorpe 6364e26195fSJason Gunthorpe tpm_put_ops(chip); 637433d390fSTomas Winkler return rc; 6389deb0eb7SJason Gunthorpe } 6399deb0eb7SJason Gunthorpe EXPORT_SYMBOL_GPL(tpm_get_random); 6409deb0eb7SJason Gunthorpe 641954650efSJarkko Sakkinen /** 642aad887f6SJarkko Sakkinen * tpm_seal_trusted() - seal a trusted key payload 643aad887f6SJarkko Sakkinen * @chip: a &struct tpm_chip instance, %NULL for the default chip 644954650efSJarkko Sakkinen * @options: authentication values and other options 645954650efSJarkko Sakkinen * @payload: the key data in clear and encrypted form 646954650efSJarkko Sakkinen * 647aad887f6SJarkko Sakkinen * Note: only TPM 2.0 chip are supported. TPM 1.x implementation is located in 648aad887f6SJarkko Sakkinen * the keyring subsystem. 649aad887f6SJarkko Sakkinen * 650aad887f6SJarkko Sakkinen * Return: same as with tpm_transmit_cmd() 651954650efSJarkko Sakkinen */ 652aad887f6SJarkko Sakkinen int tpm_seal_trusted(struct tpm_chip *chip, struct trusted_key_payload *payload, 653954650efSJarkko Sakkinen struct trusted_key_options *options) 654954650efSJarkko Sakkinen { 655954650efSJarkko Sakkinen int rc; 656954650efSJarkko Sakkinen 657fc1d52b7SStefan Berger chip = tpm_find_get_ops(chip); 658aad887f6SJarkko Sakkinen if (!chip || !(chip->flags & TPM_CHIP_FLAG_TPM2)) 659954650efSJarkko Sakkinen return -ENODEV; 660954650efSJarkko Sakkinen 661954650efSJarkko Sakkinen rc = tpm2_seal_trusted(chip, payload, options); 662954650efSJarkko Sakkinen 6634e26195fSJason Gunthorpe tpm_put_ops(chip); 664954650efSJarkko Sakkinen return rc; 665954650efSJarkko Sakkinen } 666954650efSJarkko Sakkinen EXPORT_SYMBOL_GPL(tpm_seal_trusted); 667954650efSJarkko Sakkinen 668954650efSJarkko Sakkinen /** 669954650efSJarkko Sakkinen * tpm_unseal_trusted() - unseal a trusted key 670aad887f6SJarkko Sakkinen * @chip: a &struct tpm_chip instance, %NULL for the default chip 671954650efSJarkko Sakkinen * @options: authentication values and other options 672954650efSJarkko Sakkinen * @payload: the key data in clear and encrypted form 673954650efSJarkko Sakkinen * 674aad887f6SJarkko Sakkinen * Note: only TPM 2.0 chip are supported. TPM 1.x implementation is located in 675aad887f6SJarkko Sakkinen * the keyring subsystem. 676aad887f6SJarkko Sakkinen * 677aad887f6SJarkko Sakkinen * Return: same as with tpm_transmit_cmd() 678954650efSJarkko Sakkinen */ 679aad887f6SJarkko Sakkinen int tpm_unseal_trusted(struct tpm_chip *chip, 680aad887f6SJarkko Sakkinen struct trusted_key_payload *payload, 681954650efSJarkko Sakkinen struct trusted_key_options *options) 682954650efSJarkko Sakkinen { 683954650efSJarkko Sakkinen int rc; 684954650efSJarkko Sakkinen 685fc1d52b7SStefan Berger chip = tpm_find_get_ops(chip); 686aad887f6SJarkko Sakkinen if (!chip || !(chip->flags & TPM_CHIP_FLAG_TPM2)) 687954650efSJarkko Sakkinen return -ENODEV; 688954650efSJarkko Sakkinen 689954650efSJarkko Sakkinen rc = tpm2_unseal_trusted(chip, payload, options); 690954650efSJarkko Sakkinen 6914e26195fSJason Gunthorpe tpm_put_ops(chip); 6924e26195fSJason Gunthorpe 693954650efSJarkko Sakkinen return rc; 694954650efSJarkko Sakkinen } 695954650efSJarkko Sakkinen EXPORT_SYMBOL_GPL(tpm_unseal_trusted); 696954650efSJarkko Sakkinen 697313d21eeSJarkko Sakkinen static int __init tpm_init(void) 698313d21eeSJarkko Sakkinen { 699313d21eeSJarkko Sakkinen int rc; 700313d21eeSJarkko Sakkinen 701313d21eeSJarkko Sakkinen tpm_class = class_create(THIS_MODULE, "tpm"); 702313d21eeSJarkko Sakkinen if (IS_ERR(tpm_class)) { 703313d21eeSJarkko Sakkinen pr_err("couldn't create tpm class\n"); 704313d21eeSJarkko Sakkinen return PTR_ERR(tpm_class); 705313d21eeSJarkko Sakkinen } 706313d21eeSJarkko Sakkinen 707fdc915f7SJames Bottomley tpmrm_class = class_create(THIS_MODULE, "tpmrm"); 708fdc915f7SJames Bottomley if (IS_ERR(tpmrm_class)) { 709fdc915f7SJames Bottomley pr_err("couldn't create tpmrm class\n"); 7109e1b74a6STadeusz Struk rc = PTR_ERR(tpmrm_class); 7119e1b74a6STadeusz Struk goto out_destroy_tpm_class; 712fdc915f7SJames Bottomley } 713fdc915f7SJames Bottomley 714fdc915f7SJames Bottomley rc = alloc_chrdev_region(&tpm_devt, 0, 2*TPM_NUM_DEVICES, "tpm"); 715313d21eeSJarkko Sakkinen if (rc < 0) { 716313d21eeSJarkko Sakkinen pr_err("tpm: failed to allocate char dev region\n"); 7179e1b74a6STadeusz Struk goto out_destroy_tpmrm_class; 7189e1b74a6STadeusz Struk } 7199e1b74a6STadeusz Struk 7209e1b74a6STadeusz Struk rc = tpm_dev_common_init(); 7219e1b74a6STadeusz Struk if (rc) { 7229e1b74a6STadeusz Struk pr_err("tpm: failed to allocate char dev region\n"); 7239e1b74a6STadeusz Struk goto out_unreg_chrdev; 724313d21eeSJarkko Sakkinen } 725313d21eeSJarkko Sakkinen 726313d21eeSJarkko Sakkinen return 0; 7279e1b74a6STadeusz Struk 7289e1b74a6STadeusz Struk out_unreg_chrdev: 7299e1b74a6STadeusz Struk unregister_chrdev_region(tpm_devt, 2 * TPM_NUM_DEVICES); 7309e1b74a6STadeusz Struk out_destroy_tpmrm_class: 7319e1b74a6STadeusz Struk class_destroy(tpmrm_class); 7329e1b74a6STadeusz Struk out_destroy_tpm_class: 7339e1b74a6STadeusz Struk class_destroy(tpm_class); 7349e1b74a6STadeusz Struk 7359e1b74a6STadeusz Struk return rc; 736313d21eeSJarkko Sakkinen } 737313d21eeSJarkko Sakkinen 738313d21eeSJarkko Sakkinen static void __exit tpm_exit(void) 739313d21eeSJarkko Sakkinen { 74015516788SStefan Berger idr_destroy(&dev_nums_idr); 741313d21eeSJarkko Sakkinen class_destroy(tpm_class); 742fdc915f7SJames Bottomley class_destroy(tpmrm_class); 743fdc915f7SJames Bottomley unregister_chrdev_region(tpm_devt, 2*TPM_NUM_DEVICES); 7449e1b74a6STadeusz Struk tpm_dev_common_exit(); 745313d21eeSJarkko Sakkinen } 746313d21eeSJarkko Sakkinen 747313d21eeSJarkko Sakkinen subsys_initcall(tpm_init); 748313d21eeSJarkko Sakkinen module_exit(tpm_exit); 749313d21eeSJarkko Sakkinen 7509deb0eb7SJason Gunthorpe MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)"); 7519deb0eb7SJason Gunthorpe MODULE_DESCRIPTION("TPM Driver"); 7529deb0eb7SJason Gunthorpe MODULE_VERSION("2.0"); 7539deb0eb7SJason Gunthorpe MODULE_LICENSE("GPL"); 754