Thomas Gleixner | b886d83c | 2019-06-01 10:08:55 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Jarkko Sakkinen | afb5abc | 2014-12-12 11:46:34 -0800 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2004 IBM Corporation |
| 4 | * Copyright (C) 2014 Intel Corporation |
| 5 | * |
| 6 | * Authors: |
| 7 | * Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com> |
| 8 | * Leendert van Doorn <leendert@watson.ibm.com> |
| 9 | * Dave Safford <safford@watson.ibm.com> |
| 10 | * Reiner Sailer <sailer@watson.ibm.com> |
| 11 | * Kylene Hall <kjhall@us.ibm.com> |
| 12 | * |
| 13 | * Maintained by: <tpmdd-devel@lists.sourceforge.net> |
| 14 | * |
| 15 | * TPM chip management routines. |
Jarkko Sakkinen | afb5abc | 2014-12-12 11:46:34 -0800 | [diff] [blame] | 16 | */ |
| 17 | |
| 18 | #include <linux/poll.h> |
| 19 | #include <linux/slab.h> |
| 20 | #include <linux/mutex.h> |
| 21 | #include <linux/spinlock.h> |
| 22 | #include <linux/freezer.h> |
Jarkko Sakkinen | 313d21e | 2014-12-12 11:46:37 -0800 | [diff] [blame] | 23 | #include <linux/major.h> |
Thiebaud Weksteen | fd3ec36 | 2017-09-20 10:13:36 +0200 | [diff] [blame] | 24 | #include <linux/tpm_eventlog.h> |
Jason Gunthorpe | 6e592a0 | 2017-11-17 15:24:03 +0200 | [diff] [blame] | 25 | #include <linux/hw_random.h> |
Jarkko Sakkinen | afb5abc | 2014-12-12 11:46:34 -0800 | [diff] [blame] | 26 | #include "tpm.h" |
Jarkko Sakkinen | afb5abc | 2014-12-12 11:46:34 -0800 | [diff] [blame] | 27 | |
Stefan Berger | 1551678 | 2016-02-29 08:53:02 -0500 | [diff] [blame] | 28 | DEFINE_IDR(dev_nums_idr); |
| 29 | static DEFINE_MUTEX(idr_lock); |
Jarkko Sakkinen | afb5abc | 2014-12-12 11:46:34 -0800 | [diff] [blame] | 30 | |
Jarkko Sakkinen | 313d21e | 2014-12-12 11:46:37 -0800 | [diff] [blame] | 31 | struct class *tpm_class; |
James Bottomley | fdc915f | 2017-01-03 09:07:32 -0800 | [diff] [blame] | 32 | struct class *tpmrm_class; |
Jarkko Sakkinen | 313d21e | 2014-12-12 11:46:37 -0800 | [diff] [blame] | 33 | dev_t tpm_devt; |
| 34 | |
Jarkko Sakkinen | 47a6c28 | 2018-11-05 03:02:38 +0200 | [diff] [blame] | 35 | static int tpm_request_locality(struct tpm_chip *chip) |
Jarkko Sakkinen | 719b7d8 | 2018-11-04 21:18:46 +0200 | [diff] [blame] | 36 | { |
| 37 | int rc; |
| 38 | |
Jarkko Sakkinen | 719b7d8 | 2018-11-04 21:18:46 +0200 | [diff] [blame] | 39 | if (!chip->ops->request_locality) |
| 40 | return 0; |
| 41 | |
| 42 | rc = chip->ops->request_locality(chip, 0); |
| 43 | if (rc < 0) |
| 44 | return rc; |
| 45 | |
| 46 | chip->locality = rc; |
| 47 | return 0; |
| 48 | } |
| 49 | |
Jarkko Sakkinen | 47a6c28 | 2018-11-05 03:02:38 +0200 | [diff] [blame] | 50 | static void tpm_relinquish_locality(struct tpm_chip *chip) |
Jarkko Sakkinen | 719b7d8 | 2018-11-04 21:18:46 +0200 | [diff] [blame] | 51 | { |
| 52 | int rc; |
| 53 | |
Jarkko Sakkinen | 719b7d8 | 2018-11-04 21:18:46 +0200 | [diff] [blame] | 54 | if (!chip->ops->relinquish_locality) |
| 55 | return; |
| 56 | |
| 57 | rc = chip->ops->relinquish_locality(chip, chip->locality); |
| 58 | if (rc) |
| 59 | dev_err(&chip->dev, "%s: : error %d\n", __func__, rc); |
| 60 | |
| 61 | chip->locality = -1; |
| 62 | } |
| 63 | |
Jarkko Sakkinen | 47a6c28 | 2018-11-05 03:02:38 +0200 | [diff] [blame] | 64 | static int tpm_cmd_ready(struct tpm_chip *chip) |
Jarkko Sakkinen | 719b7d8 | 2018-11-04 21:18:46 +0200 | [diff] [blame] | 65 | { |
Jarkko Sakkinen | 719b7d8 | 2018-11-04 21:18:46 +0200 | [diff] [blame] | 66 | if (!chip->ops->cmd_ready) |
| 67 | return 0; |
| 68 | |
| 69 | return chip->ops->cmd_ready(chip); |
| 70 | } |
| 71 | |
Jarkko Sakkinen | 47a6c28 | 2018-11-05 03:02:38 +0200 | [diff] [blame] | 72 | static int tpm_go_idle(struct tpm_chip *chip) |
Jarkko Sakkinen | 719b7d8 | 2018-11-04 21:18:46 +0200 | [diff] [blame] | 73 | { |
Jarkko Sakkinen | 719b7d8 | 2018-11-04 21:18:46 +0200 | [diff] [blame] | 74 | if (!chip->ops->go_idle) |
| 75 | return 0; |
| 76 | |
| 77 | return chip->ops->go_idle(chip); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * tpm_chip_start() - power on the TPM |
| 82 | * @chip: a TPM chip to use |
Jarkko Sakkinen | 719b7d8 | 2018-11-04 21:18:46 +0200 | [diff] [blame] | 83 | * |
| 84 | * Return: |
| 85 | * * The response length - OK |
| 86 | * * -errno - A system error |
| 87 | */ |
Jarkko Sakkinen | 47a6c28 | 2018-11-05 03:02:38 +0200 | [diff] [blame] | 88 | int tpm_chip_start(struct tpm_chip *chip) |
Jarkko Sakkinen | 719b7d8 | 2018-11-04 21:18:46 +0200 | [diff] [blame] | 89 | { |
| 90 | int ret; |
| 91 | |
| 92 | if (chip->ops->clk_enable) |
| 93 | chip->ops->clk_enable(chip, true); |
| 94 | |
| 95 | if (chip->locality == -1) { |
Jarkko Sakkinen | 47a6c28 | 2018-11-05 03:02:38 +0200 | [diff] [blame] | 96 | ret = tpm_request_locality(chip); |
Jarkko Sakkinen | 719b7d8 | 2018-11-04 21:18:46 +0200 | [diff] [blame] | 97 | if (ret) { |
| 98 | chip->ops->clk_enable(chip, false); |
| 99 | return ret; |
| 100 | } |
| 101 | } |
| 102 | |
Jarkko Sakkinen | 47a6c28 | 2018-11-05 03:02:38 +0200 | [diff] [blame] | 103 | ret = tpm_cmd_ready(chip); |
Jarkko Sakkinen | 719b7d8 | 2018-11-04 21:18:46 +0200 | [diff] [blame] | 104 | if (ret) { |
Jarkko Sakkinen | 47a6c28 | 2018-11-05 03:02:38 +0200 | [diff] [blame] | 105 | tpm_relinquish_locality(chip); |
Jarkko Sakkinen | 719b7d8 | 2018-11-04 21:18:46 +0200 | [diff] [blame] | 106 | if (chip->ops->clk_enable) |
| 107 | chip->ops->clk_enable(chip, false); |
| 108 | return ret; |
| 109 | } |
| 110 | |
| 111 | return 0; |
| 112 | } |
| 113 | EXPORT_SYMBOL_GPL(tpm_chip_start); |
| 114 | |
| 115 | /** |
| 116 | * tpm_chip_stop() - power off the TPM |
| 117 | * @chip: a TPM chip to use |
Jarkko Sakkinen | 719b7d8 | 2018-11-04 21:18:46 +0200 | [diff] [blame] | 118 | * |
| 119 | * Return: |
| 120 | * * The response length - OK |
| 121 | * * -errno - A system error |
| 122 | */ |
Jarkko Sakkinen | 47a6c28 | 2018-11-05 03:02:38 +0200 | [diff] [blame] | 123 | void tpm_chip_stop(struct tpm_chip *chip) |
Jarkko Sakkinen | 719b7d8 | 2018-11-04 21:18:46 +0200 | [diff] [blame] | 124 | { |
Jarkko Sakkinen | 47a6c28 | 2018-11-05 03:02:38 +0200 | [diff] [blame] | 125 | tpm_go_idle(chip); |
| 126 | tpm_relinquish_locality(chip); |
Jarkko Sakkinen | 719b7d8 | 2018-11-04 21:18:46 +0200 | [diff] [blame] | 127 | if (chip->ops->clk_enable) |
| 128 | chip->ops->clk_enable(chip, false); |
| 129 | } |
| 130 | EXPORT_SYMBOL_GPL(tpm_chip_stop); |
| 131 | |
Jason Gunthorpe | 4e26195 | 2016-02-12 20:29:53 -0700 | [diff] [blame] | 132 | /** |
| 133 | * tpm_try_get_ops() - Get a ref to the tpm_chip |
| 134 | * @chip: Chip to ref |
| 135 | * |
| 136 | * The caller must already have some kind of locking to ensure that chip is |
| 137 | * valid. This function will lock the chip so that the ops member can be |
| 138 | * accessed safely. The locking prevents tpm_chip_unregister from |
| 139 | * completing, so it should not be held for long periods. |
| 140 | * |
| 141 | * Returns -ERRNO if the chip could not be got. |
Jarkko Sakkinen | afb5abc | 2014-12-12 11:46:34 -0800 | [diff] [blame] | 142 | */ |
Jason Gunthorpe | 4e26195 | 2016-02-12 20:29:53 -0700 | [diff] [blame] | 143 | int tpm_try_get_ops(struct tpm_chip *chip) |
| 144 | { |
| 145 | int rc = -EIO; |
| 146 | |
| 147 | get_device(&chip->dev); |
| 148 | |
| 149 | down_read(&chip->ops_sem); |
| 150 | if (!chip->ops) |
Jarkko Sakkinen | a3fbfae | 2018-11-05 02:07:56 +0200 | [diff] [blame] | 151 | goto out_ops; |
Jason Gunthorpe | 4e26195 | 2016-02-12 20:29:53 -0700 | [diff] [blame] | 152 | |
Jarkko Sakkinen | 2f25740 | 2018-11-04 20:01:42 +0200 | [diff] [blame] | 153 | mutex_lock(&chip->tpm_mutex); |
Jarkko Sakkinen | 47a6c28 | 2018-11-05 03:02:38 +0200 | [diff] [blame] | 154 | rc = tpm_chip_start(chip); |
Jarkko Sakkinen | a3fbfae | 2018-11-05 02:07:56 +0200 | [diff] [blame] | 155 | if (rc) |
| 156 | goto out_lock; |
| 157 | |
Jason Gunthorpe | 4e26195 | 2016-02-12 20:29:53 -0700 | [diff] [blame] | 158 | return 0; |
| 159 | out_lock: |
Jarkko Sakkinen | a3fbfae | 2018-11-05 02:07:56 +0200 | [diff] [blame] | 160 | mutex_unlock(&chip->tpm_mutex); |
| 161 | out_ops: |
Jason Gunthorpe | 4e26195 | 2016-02-12 20:29:53 -0700 | [diff] [blame] | 162 | up_read(&chip->ops_sem); |
| 163 | put_device(&chip->dev); |
| 164 | return rc; |
| 165 | } |
| 166 | EXPORT_SYMBOL_GPL(tpm_try_get_ops); |
| 167 | |
| 168 | /** |
| 169 | * tpm_put_ops() - Release a ref to the tpm_chip |
| 170 | * @chip: Chip to put |
| 171 | * |
| 172 | * This is the opposite pair to tpm_try_get_ops(). After this returns chip may |
| 173 | * be kfree'd. |
| 174 | */ |
| 175 | void tpm_put_ops(struct tpm_chip *chip) |
| 176 | { |
Jarkko Sakkinen | 47a6c28 | 2018-11-05 03:02:38 +0200 | [diff] [blame] | 177 | tpm_chip_stop(chip); |
Jarkko Sakkinen | 2f25740 | 2018-11-04 20:01:42 +0200 | [diff] [blame] | 178 | mutex_unlock(&chip->tpm_mutex); |
Jason Gunthorpe | 4e26195 | 2016-02-12 20:29:53 -0700 | [diff] [blame] | 179 | up_read(&chip->ops_sem); |
| 180 | put_device(&chip->dev); |
| 181 | } |
| 182 | EXPORT_SYMBOL_GPL(tpm_put_ops); |
| 183 | |
| 184 | /** |
Stefan Berger | aaae815 | 2018-06-26 15:09:30 -0400 | [diff] [blame] | 185 | * tpm_default_chip() - find a TPM chip and get a reference to it |
| 186 | */ |
| 187 | struct tpm_chip *tpm_default_chip(void) |
| 188 | { |
| 189 | struct tpm_chip *chip, *res = NULL; |
| 190 | int chip_num = 0; |
| 191 | int chip_prev; |
| 192 | |
| 193 | mutex_lock(&idr_lock); |
| 194 | |
| 195 | do { |
| 196 | chip_prev = chip_num; |
| 197 | chip = idr_get_next(&dev_nums_idr, &chip_num); |
| 198 | if (chip) { |
| 199 | get_device(&chip->dev); |
| 200 | res = chip; |
| 201 | break; |
| 202 | } |
| 203 | } while (chip_prev != chip_num); |
| 204 | |
| 205 | mutex_unlock(&idr_lock); |
| 206 | |
| 207 | return res; |
| 208 | } |
| 209 | EXPORT_SYMBOL_GPL(tpm_default_chip); |
| 210 | |
| 211 | /** |
Stefan Berger | fc1d52b | 2018-06-26 07:06:15 -0400 | [diff] [blame] | 212 | * tpm_find_get_ops() - find and reserve a TPM chip |
Jarkko Sakkinen | aad887f | 2017-11-05 13:16:26 +0200 | [diff] [blame] | 213 | * @chip: a &struct tpm_chip instance, %NULL for the default chip |
Jason Gunthorpe | 4e26195 | 2016-02-12 20:29:53 -0700 | [diff] [blame] | 214 | * |
Jarkko Sakkinen | aad887f | 2017-11-05 13:16:26 +0200 | [diff] [blame] | 215 | * Finds a TPM chip and reserves its class device and operations. The chip must |
Stefan Berger | fc1d52b | 2018-06-26 07:06:15 -0400 | [diff] [blame] | 216 | * be released with tpm_put_ops() after use. |
| 217 | * This function is for internal use only. It supports existing TPM callers |
| 218 | * by accepting NULL, but those callers should be converted to pass in a chip |
| 219 | * directly. |
Jarkko Sakkinen | aad887f | 2017-11-05 13:16:26 +0200 | [diff] [blame] | 220 | * |
| 221 | * Return: |
| 222 | * A reserved &struct tpm_chip instance. |
| 223 | * %NULL if a chip is not found. |
| 224 | * %NULL if the chip is not available. |
Matthew Wilcox | 37f4915 | 2016-12-14 15:09:16 -0800 | [diff] [blame] | 225 | */ |
Stefan Berger | fc1d52b | 2018-06-26 07:06:15 -0400 | [diff] [blame] | 226 | struct tpm_chip *tpm_find_get_ops(struct tpm_chip *chip) |
Jarkko Sakkinen | afb5abc | 2014-12-12 11:46:34 -0800 | [diff] [blame] | 227 | { |
Stefan Berger | eccc9bb | 2018-06-26 15:09:31 -0400 | [diff] [blame] | 228 | int rc; |
Jarkko Sakkinen | afb5abc | 2014-12-12 11:46:34 -0800 | [diff] [blame] | 229 | |
Stefan Berger | eccc9bb | 2018-06-26 15:09:31 -0400 | [diff] [blame] | 230 | if (chip) { |
Jarkko Sakkinen | aad887f | 2017-11-05 13:16:26 +0200 | [diff] [blame] | 231 | if (!tpm_try_get_ops(chip)) |
Stefan Berger | eccc9bb | 2018-06-26 15:09:31 -0400 | [diff] [blame] | 232 | return chip; |
| 233 | return NULL; |
Jarkko Sakkinen | afb5abc | 2014-12-12 11:46:34 -0800 | [diff] [blame] | 234 | } |
Stefan Berger | 1551678 | 2016-02-29 08:53:02 -0500 | [diff] [blame] | 235 | |
Stefan Berger | eccc9bb | 2018-06-26 15:09:31 -0400 | [diff] [blame] | 236 | chip = tpm_default_chip(); |
| 237 | if (!chip) |
| 238 | return NULL; |
| 239 | rc = tpm_try_get_ops(chip); |
| 240 | /* release additional reference we got from tpm_default_chip() */ |
| 241 | put_device(&chip->dev); |
| 242 | if (rc) |
| 243 | return NULL; |
| 244 | return chip; |
Jarkko Sakkinen | afb5abc | 2014-12-12 11:46:34 -0800 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | /** |
Jarkko Sakkinen | 313d21e | 2014-12-12 11:46:37 -0800 | [diff] [blame] | 248 | * tpm_dev_release() - free chip memory and the device number |
| 249 | * @dev: the character device for the TPM chip |
Jarkko Sakkinen | afb5abc | 2014-12-12 11:46:34 -0800 | [diff] [blame] | 250 | * |
Jarkko Sakkinen | 313d21e | 2014-12-12 11:46:37 -0800 | [diff] [blame] | 251 | * This is used as the release function for the character device. |
Jarkko Sakkinen | afb5abc | 2014-12-12 11:46:34 -0800 | [diff] [blame] | 252 | */ |
Jarkko Sakkinen | 313d21e | 2014-12-12 11:46:37 -0800 | [diff] [blame] | 253 | static void tpm_dev_release(struct device *dev) |
Jarkko Sakkinen | afb5abc | 2014-12-12 11:46:34 -0800 | [diff] [blame] | 254 | { |
Jarkko Sakkinen | 313d21e | 2014-12-12 11:46:37 -0800 | [diff] [blame] | 255 | struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev); |
Jarkko Sakkinen | afb5abc | 2014-12-12 11:46:34 -0800 | [diff] [blame] | 256 | |
Stefan Berger | 1551678 | 2016-02-29 08:53:02 -0500 | [diff] [blame] | 257 | mutex_lock(&idr_lock); |
| 258 | idr_remove(&dev_nums_idr, chip->dev_num); |
| 259 | mutex_unlock(&idr_lock); |
| 260 | |
Nayna Jain | 748935e | 2016-11-14 05:00:52 -0500 | [diff] [blame] | 261 | kfree(chip->log.bios_event_log); |
Jarkko Sakkinen | 745b361 | 2017-01-06 14:03:45 +0200 | [diff] [blame] | 262 | kfree(chip->work_space.context_buf); |
James Bottomley | 4d57856 | 2017-01-31 15:47:31 -0800 | [diff] [blame] | 263 | kfree(chip->work_space.session_buf); |
Roberto Sassu | bcfff83 | 2019-02-06 17:24:47 +0100 | [diff] [blame] | 264 | kfree(chip->allocated_banks); |
Jarkko Sakkinen | afb5abc | 2014-12-12 11:46:34 -0800 | [diff] [blame] | 265 | kfree(chip); |
| 266 | } |
| 267 | |
James Bottomley | fdc915f | 2017-01-03 09:07:32 -0800 | [diff] [blame] | 268 | static void tpm_devs_release(struct device *dev) |
| 269 | { |
| 270 | struct tpm_chip *chip = container_of(dev, struct tpm_chip, devs); |
| 271 | |
| 272 | /* release the master device reference */ |
| 273 | put_device(&chip->dev); |
| 274 | } |
| 275 | |
Jarkko Sakkinen | afb5abc | 2014-12-12 11:46:34 -0800 | [diff] [blame] | 276 | /** |
Josh Zimmerman | d1bd4a7 | 2017-06-25 14:53:24 -0700 | [diff] [blame] | 277 | * tpm_class_shutdown() - prepare the TPM device for loss of power. |
| 278 | * @dev: device to which the chip is associated. |
| 279 | * |
| 280 | * Issues a TPM2_Shutdown command prior to loss of power, as required by the |
| 281 | * TPM 2.0 spec. |
| 282 | * Then, calls bus- and device- specific shutdown code. |
| 283 | * |
| 284 | * XXX: This codepath relies on the fact that sysfs is not enabled for |
| 285 | * TPM2: sysfs uses an implicit lock on chip->ops, so this could race if TPM2 |
| 286 | * has sysfs support enabled before TPM sysfs's implicit locking is fixed. |
| 287 | */ |
| 288 | static int tpm_class_shutdown(struct device *dev) |
| 289 | { |
| 290 | struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev); |
| 291 | |
Vadim Sukhomlinov | db4d8cb | 2019-06-10 15:01:18 -0700 | [diff] [blame^] | 292 | down_write(&chip->ops_sem); |
Josh Zimmerman | d1bd4a7 | 2017-06-25 14:53:24 -0700 | [diff] [blame] | 293 | if (chip->flags & TPM_CHIP_FLAG_TPM2) { |
Jarkko Sakkinen | 47a6c28 | 2018-11-05 03:02:38 +0200 | [diff] [blame] | 294 | if (!tpm_chip_start(chip)) { |
Jarkko Sakkinen | a3fbfae | 2018-11-05 02:07:56 +0200 | [diff] [blame] | 295 | tpm2_shutdown(chip, TPM2_SU_CLEAR); |
Jarkko Sakkinen | 47a6c28 | 2018-11-05 03:02:38 +0200 | [diff] [blame] | 296 | tpm_chip_stop(chip); |
Jarkko Sakkinen | a3fbfae | 2018-11-05 02:07:56 +0200 | [diff] [blame] | 297 | } |
Josh Zimmerman | d1bd4a7 | 2017-06-25 14:53:24 -0700 | [diff] [blame] | 298 | } |
Vadim Sukhomlinov | db4d8cb | 2019-06-10 15:01:18 -0700 | [diff] [blame^] | 299 | chip->ops = NULL; |
| 300 | up_write(&chip->ops_sem); |
Michal Suchanek | 7521621 | 2017-08-11 15:44:43 +0200 | [diff] [blame] | 301 | |
Josh Zimmerman | d1bd4a7 | 2017-06-25 14:53:24 -0700 | [diff] [blame] | 302 | return 0; |
| 303 | } |
| 304 | |
| 305 | /** |
Jason Gunthorpe | 3897cd9 | 2016-02-11 12:45:48 -0700 | [diff] [blame] | 306 | * tpm_chip_alloc() - allocate a new struct tpm_chip instance |
| 307 | * @pdev: device to which the chip is associated |
| 308 | * At this point pdev mst be initialized, but does not have to |
| 309 | * be registered |
Jarkko Sakkinen | afb5abc | 2014-12-12 11:46:34 -0800 | [diff] [blame] | 310 | * @ops: struct tpm_class_ops instance |
| 311 | * |
| 312 | * Allocates a new struct tpm_chip instance and assigns a free |
Jason Gunthorpe | 3897cd9 | 2016-02-11 12:45:48 -0700 | [diff] [blame] | 313 | * device number for it. Must be paired with put_device(&chip->dev). |
Jarkko Sakkinen | afb5abc | 2014-12-12 11:46:34 -0800 | [diff] [blame] | 314 | */ |
Winkler, Tomas | 2998b02 | 2016-11-23 12:04:13 +0200 | [diff] [blame] | 315 | struct tpm_chip *tpm_chip_alloc(struct device *pdev, |
Jason Gunthorpe | 3897cd9 | 2016-02-11 12:45:48 -0700 | [diff] [blame] | 316 | const struct tpm_class_ops *ops) |
Jarkko Sakkinen | afb5abc | 2014-12-12 11:46:34 -0800 | [diff] [blame] | 317 | { |
| 318 | struct tpm_chip *chip; |
Jarkko Sakkinen | 4f3b193 | 2016-02-13 11:58:16 +0200 | [diff] [blame] | 319 | int rc; |
Jarkko Sakkinen | afb5abc | 2014-12-12 11:46:34 -0800 | [diff] [blame] | 320 | |
| 321 | chip = kzalloc(sizeof(*chip), GFP_KERNEL); |
| 322 | if (chip == NULL) |
| 323 | return ERR_PTR(-ENOMEM); |
| 324 | |
| 325 | mutex_init(&chip->tpm_mutex); |
Jason Gunthorpe | 4e26195 | 2016-02-12 20:29:53 -0700 | [diff] [blame] | 326 | init_rwsem(&chip->ops_sem); |
Jarkko Sakkinen | afb5abc | 2014-12-12 11:46:34 -0800 | [diff] [blame] | 327 | |
| 328 | chip->ops = ops; |
| 329 | |
Stefan Berger | 1551678 | 2016-02-29 08:53:02 -0500 | [diff] [blame] | 330 | mutex_lock(&idr_lock); |
| 331 | rc = idr_alloc(&dev_nums_idr, NULL, 0, TPM_NUM_DEVICES, GFP_KERNEL); |
| 332 | mutex_unlock(&idr_lock); |
| 333 | if (rc < 0) { |
Winkler, Tomas | 2998b02 | 2016-11-23 12:04:13 +0200 | [diff] [blame] | 334 | dev_err(pdev, "No available tpm device numbers\n"); |
Jarkko Sakkinen | afb5abc | 2014-12-12 11:46:34 -0800 | [diff] [blame] | 335 | kfree(chip); |
Stefan Berger | 1551678 | 2016-02-29 08:53:02 -0500 | [diff] [blame] | 336 | return ERR_PTR(rc); |
Jarkko Sakkinen | afb5abc | 2014-12-12 11:46:34 -0800 | [diff] [blame] | 337 | } |
Stefan Berger | 1551678 | 2016-02-29 08:53:02 -0500 | [diff] [blame] | 338 | chip->dev_num = rc; |
Jarkko Sakkinen | afb5abc | 2014-12-12 11:46:34 -0800 | [diff] [blame] | 339 | |
Jason Gunthorpe | 3635e2e | 2016-02-29 12:29:48 -0500 | [diff] [blame] | 340 | device_initialize(&chip->dev); |
James Bottomley | fdc915f | 2017-01-03 09:07:32 -0800 | [diff] [blame] | 341 | device_initialize(&chip->devs); |
Jarkko Sakkinen | afb5abc | 2014-12-12 11:46:34 -0800 | [diff] [blame] | 342 | |
Jarkko Sakkinen | 313d21e | 2014-12-12 11:46:37 -0800 | [diff] [blame] | 343 | chip->dev.class = tpm_class; |
Michal Suchanek | 7521621 | 2017-08-11 15:44:43 +0200 | [diff] [blame] | 344 | chip->dev.class->shutdown_pre = tpm_class_shutdown; |
Jarkko Sakkinen | 313d21e | 2014-12-12 11:46:37 -0800 | [diff] [blame] | 345 | chip->dev.release = tpm_dev_release; |
Winkler, Tomas | 2998b02 | 2016-11-23 12:04:13 +0200 | [diff] [blame] | 346 | chip->dev.parent = pdev; |
Jarkko Sakkinen | 9b774d5 | 2015-04-14 17:56:48 +0300 | [diff] [blame] | 347 | chip->dev.groups = chip->groups; |
Jarkko Sakkinen | 313d21e | 2014-12-12 11:46:37 -0800 | [diff] [blame] | 348 | |
James Bottomley | fdc915f | 2017-01-03 09:07:32 -0800 | [diff] [blame] | 349 | chip->devs.parent = pdev; |
| 350 | chip->devs.class = tpmrm_class; |
| 351 | chip->devs.release = tpm_devs_release; |
| 352 | /* get extra reference on main device to hold on |
| 353 | * behalf of devs. This holds the chip structure |
| 354 | * while cdevs is in use. The corresponding put |
Stefan Berger | 8979b02 | 2017-04-17 21:58:26 -0400 | [diff] [blame] | 355 | * is in the tpm_devs_release (TPM2 only) |
James Bottomley | fdc915f | 2017-01-03 09:07:32 -0800 | [diff] [blame] | 356 | */ |
Stefan Berger | 8979b02 | 2017-04-17 21:58:26 -0400 | [diff] [blame] | 357 | if (chip->flags & TPM_CHIP_FLAG_TPM2) |
| 358 | get_device(&chip->dev); |
James Bottomley | fdc915f | 2017-01-03 09:07:32 -0800 | [diff] [blame] | 359 | |
Jarkko Sakkinen | 313d21e | 2014-12-12 11:46:37 -0800 | [diff] [blame] | 360 | if (chip->dev_num == 0) |
| 361 | chip->dev.devt = MKDEV(MISC_MAJOR, TPM_MINOR); |
| 362 | else |
| 363 | chip->dev.devt = MKDEV(MAJOR(tpm_devt), chip->dev_num); |
| 364 | |
James Bottomley | fdc915f | 2017-01-03 09:07:32 -0800 | [diff] [blame] | 365 | chip->devs.devt = |
| 366 | MKDEV(MAJOR(tpm_devt), chip->dev_num + TPM_NUM_DEVICES); |
| 367 | |
Jason Gunthorpe | 3635e2e | 2016-02-29 12:29:48 -0500 | [diff] [blame] | 368 | rc = dev_set_name(&chip->dev, "tpm%d", chip->dev_num); |
| 369 | if (rc) |
| 370 | goto out; |
James Bottomley | fdc915f | 2017-01-03 09:07:32 -0800 | [diff] [blame] | 371 | rc = dev_set_name(&chip->devs, "tpmrm%d", chip->dev_num); |
| 372 | if (rc) |
| 373 | goto out; |
Jarkko Sakkinen | 313d21e | 2014-12-12 11:46:37 -0800 | [diff] [blame] | 374 | |
Winkler, Tomas | 2998b02 | 2016-11-23 12:04:13 +0200 | [diff] [blame] | 375 | if (!pdev) |
Stefan Berger | 2f9f537 | 2016-04-18 13:26:14 -0400 | [diff] [blame] | 376 | chip->flags |= TPM_CHIP_FLAG_VIRTUAL; |
| 377 | |
Jarkko Sakkinen | 313d21e | 2014-12-12 11:46:37 -0800 | [diff] [blame] | 378 | cdev_init(&chip->cdev, &tpm_fops); |
James Bottomley | fdc915f | 2017-01-03 09:07:32 -0800 | [diff] [blame] | 379 | cdev_init(&chip->cdevs, &tpmrm_fops); |
Stefan Berger | 2072df4 | 2016-02-29 08:53:01 -0500 | [diff] [blame] | 380 | chip->cdev.owner = THIS_MODULE; |
James Bottomley | fdc915f | 2017-01-03 09:07:32 -0800 | [diff] [blame] | 381 | chip->cdevs.owner = THIS_MODULE; |
Jarkko Sakkinen | 313d21e | 2014-12-12 11:46:37 -0800 | [diff] [blame] | 382 | |
Jarkko Sakkinen | 745b361 | 2017-01-06 14:03:45 +0200 | [diff] [blame] | 383 | chip->work_space.context_buf = kzalloc(PAGE_SIZE, GFP_KERNEL); |
| 384 | if (!chip->work_space.context_buf) { |
| 385 | rc = -ENOMEM; |
| 386 | goto out; |
| 387 | } |
James Bottomley | 4d57856 | 2017-01-31 15:47:31 -0800 | [diff] [blame] | 388 | chip->work_space.session_buf = kzalloc(PAGE_SIZE, GFP_KERNEL); |
| 389 | if (!chip->work_space.session_buf) { |
| 390 | rc = -ENOMEM; |
| 391 | goto out; |
| 392 | } |
Jarkko Sakkinen | 745b361 | 2017-01-06 14:03:45 +0200 | [diff] [blame] | 393 | |
Jarkko Sakkinen | 877c57d | 2017-03-24 11:45:49 +0200 | [diff] [blame] | 394 | chip->locality = -1; |
Jarkko Sakkinen | afb5abc | 2014-12-12 11:46:34 -0800 | [diff] [blame] | 395 | return chip; |
Jason Gunthorpe | 3635e2e | 2016-02-29 12:29:48 -0500 | [diff] [blame] | 396 | |
| 397 | out: |
James Bottomley | fdc915f | 2017-01-03 09:07:32 -0800 | [diff] [blame] | 398 | put_device(&chip->devs); |
Jason Gunthorpe | 3635e2e | 2016-02-29 12:29:48 -0500 | [diff] [blame] | 399 | put_device(&chip->dev); |
| 400 | return ERR_PTR(rc); |
Jarkko Sakkinen | afb5abc | 2014-12-12 11:46:34 -0800 | [diff] [blame] | 401 | } |
Jason Gunthorpe | 3897cd9 | 2016-02-11 12:45:48 -0700 | [diff] [blame] | 402 | EXPORT_SYMBOL_GPL(tpm_chip_alloc); |
| 403 | |
| 404 | /** |
| 405 | * tpmm_chip_alloc() - allocate a new struct tpm_chip instance |
| 406 | * @pdev: parent device to which the chip is associated |
| 407 | * @ops: struct tpm_class_ops instance |
| 408 | * |
| 409 | * Same as tpm_chip_alloc except devm is used to do the put_device |
| 410 | */ |
| 411 | struct tpm_chip *tpmm_chip_alloc(struct device *pdev, |
| 412 | const struct tpm_class_ops *ops) |
| 413 | { |
| 414 | struct tpm_chip *chip; |
| 415 | int rc; |
| 416 | |
| 417 | chip = tpm_chip_alloc(pdev, ops); |
| 418 | if (IS_ERR(chip)) |
| 419 | return chip; |
| 420 | |
Sudip Mukherjee | 2b88cd9 | 2016-06-12 15:05:29 +0100 | [diff] [blame] | 421 | rc = devm_add_action_or_reset(pdev, |
| 422 | (void (*)(void *)) put_device, |
| 423 | &chip->dev); |
| 424 | if (rc) |
Jason Gunthorpe | 3897cd9 | 2016-02-11 12:45:48 -0700 | [diff] [blame] | 425 | return ERR_PTR(rc); |
Jason Gunthorpe | 3897cd9 | 2016-02-11 12:45:48 -0700 | [diff] [blame] | 426 | |
| 427 | dev_set_drvdata(pdev, chip); |
| 428 | |
| 429 | return chip; |
| 430 | } |
Jarkko Sakkinen | afb5abc | 2014-12-12 11:46:34 -0800 | [diff] [blame] | 431 | EXPORT_SYMBOL_GPL(tpmm_chip_alloc); |
| 432 | |
Jarkko Sakkinen | 72c91ce | 2016-01-29 09:47:22 -0800 | [diff] [blame] | 433 | static int tpm_add_char_device(struct tpm_chip *chip) |
Jarkko Sakkinen | 313d21e | 2014-12-12 11:46:37 -0800 | [diff] [blame] | 434 | { |
| 435 | int rc; |
| 436 | |
Logan Gunthorpe | 8dbbf58 | 2017-03-17 12:48:13 -0600 | [diff] [blame] | 437 | rc = cdev_device_add(&chip->cdev, &chip->dev); |
Jarkko Sakkinen | 313d21e | 2014-12-12 11:46:37 -0800 | [diff] [blame] | 438 | if (rc) { |
| 439 | dev_err(&chip->dev, |
Logan Gunthorpe | 8dbbf58 | 2017-03-17 12:48:13 -0600 | [diff] [blame] | 440 | "unable to cdev_device_add() %s, major %d, minor %d, err=%d\n", |
Jason Gunthorpe | 3635e2e | 2016-02-29 12:29:48 -0500 | [diff] [blame] | 441 | dev_name(&chip->dev), MAJOR(chip->dev.devt), |
Jarkko Sakkinen | 313d21e | 2014-12-12 11:46:37 -0800 | [diff] [blame] | 442 | MINOR(chip->dev.devt), rc); |
Jarkko Sakkinen | 313d21e | 2014-12-12 11:46:37 -0800 | [diff] [blame] | 443 | return rc; |
| 444 | } |
| 445 | |
Linus Torvalds | af82455 | 2017-05-04 19:07:10 -0700 | [diff] [blame] | 446 | if (chip->flags & TPM_CHIP_FLAG_TPM2) { |
| 447 | rc = cdev_device_add(&chip->cdevs, &chip->devs); |
| 448 | if (rc) { |
| 449 | dev_err(&chip->devs, |
| 450 | "unable to cdev_device_add() %s, major %d, minor %d, err=%d\n", |
| 451 | dev_name(&chip->devs), MAJOR(chip->devs.devt), |
| 452 | MINOR(chip->devs.devt), rc); |
| 453 | return rc; |
| 454 | } |
James Bottomley | fdc915f | 2017-01-03 09:07:32 -0800 | [diff] [blame] | 455 | } |
| 456 | |
Stefan Berger | 1551678 | 2016-02-29 08:53:02 -0500 | [diff] [blame] | 457 | /* Make the chip available. */ |
| 458 | mutex_lock(&idr_lock); |
| 459 | idr_replace(&dev_nums_idr, chip, chip->dev_num); |
| 460 | mutex_unlock(&idr_lock); |
| 461 | |
Jarkko Sakkinen | 313d21e | 2014-12-12 11:46:37 -0800 | [diff] [blame] | 462 | return rc; |
| 463 | } |
| 464 | |
Jarkko Sakkinen | 72c91ce | 2016-01-29 09:47:22 -0800 | [diff] [blame] | 465 | static void tpm_del_char_device(struct tpm_chip *chip) |
Jarkko Sakkinen | 313d21e | 2014-12-12 11:46:37 -0800 | [diff] [blame] | 466 | { |
Logan Gunthorpe | 8dbbf58 | 2017-03-17 12:48:13 -0600 | [diff] [blame] | 467 | cdev_device_del(&chip->cdev, &chip->dev); |
Stefan Berger | 1551678 | 2016-02-29 08:53:02 -0500 | [diff] [blame] | 468 | |
| 469 | /* Make the chip unavailable. */ |
| 470 | mutex_lock(&idr_lock); |
| 471 | idr_replace(&dev_nums_idr, NULL, chip->dev_num); |
| 472 | mutex_unlock(&idr_lock); |
Jason Gunthorpe | 4e26195 | 2016-02-12 20:29:53 -0700 | [diff] [blame] | 473 | |
| 474 | /* Make the driver uncallable. */ |
| 475 | down_write(&chip->ops_sem); |
Jarkko Sakkinen | a3fbfae | 2018-11-05 02:07:56 +0200 | [diff] [blame] | 476 | if (chip->flags & TPM_CHIP_FLAG_TPM2) { |
Jarkko Sakkinen | 47a6c28 | 2018-11-05 03:02:38 +0200 | [diff] [blame] | 477 | if (!tpm_chip_start(chip)) { |
Jarkko Sakkinen | a3fbfae | 2018-11-05 02:07:56 +0200 | [diff] [blame] | 478 | tpm2_shutdown(chip, TPM2_SU_CLEAR); |
Jarkko Sakkinen | 47a6c28 | 2018-11-05 03:02:38 +0200 | [diff] [blame] | 479 | tpm_chip_stop(chip); |
Jarkko Sakkinen | a3fbfae | 2018-11-05 02:07:56 +0200 | [diff] [blame] | 480 | } |
| 481 | } |
Jason Gunthorpe | 4e26195 | 2016-02-12 20:29:53 -0700 | [diff] [blame] | 482 | chip->ops = NULL; |
| 483 | up_write(&chip->ops_sem); |
Jarkko Sakkinen | 313d21e | 2014-12-12 11:46:37 -0800 | [diff] [blame] | 484 | } |
| 485 | |
Jason Gunthorpe | 062807f | 2016-04-18 13:26:13 -0400 | [diff] [blame] | 486 | static void tpm_del_legacy_sysfs(struct tpm_chip *chip) |
| 487 | { |
| 488 | struct attribute **i; |
| 489 | |
Stefan Berger | 2f9f537 | 2016-04-18 13:26:14 -0400 | [diff] [blame] | 490 | if (chip->flags & (TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_VIRTUAL)) |
Jason Gunthorpe | 062807f | 2016-04-18 13:26:13 -0400 | [diff] [blame] | 491 | return; |
| 492 | |
| 493 | sysfs_remove_link(&chip->dev.parent->kobj, "ppi"); |
| 494 | |
| 495 | for (i = chip->groups[0]->attrs; *i != NULL; ++i) |
| 496 | sysfs_remove_link(&chip->dev.parent->kobj, (*i)->name); |
| 497 | } |
| 498 | |
| 499 | /* For compatibility with legacy sysfs paths we provide symlinks from the |
| 500 | * parent dev directory to selected names within the tpm chip directory. Old |
| 501 | * kernel versions created these files directly under the parent. |
| 502 | */ |
| 503 | static int tpm_add_legacy_sysfs(struct tpm_chip *chip) |
| 504 | { |
| 505 | struct attribute **i; |
| 506 | int rc; |
| 507 | |
Stefan Berger | 2f9f537 | 2016-04-18 13:26:14 -0400 | [diff] [blame] | 508 | if (chip->flags & (TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_VIRTUAL)) |
Jason Gunthorpe | 062807f | 2016-04-18 13:26:13 -0400 | [diff] [blame] | 509 | return 0; |
| 510 | |
| 511 | rc = __compat_only_sysfs_link_entry_to_kobj( |
| 512 | &chip->dev.parent->kobj, &chip->dev.kobj, "ppi"); |
| 513 | if (rc && rc != -ENOENT) |
| 514 | return rc; |
| 515 | |
| 516 | /* All the names from tpm-sysfs */ |
| 517 | for (i = chip->groups[0]->attrs; *i != NULL; ++i) { |
| 518 | rc = __compat_only_sysfs_link_entry_to_kobj( |
| 519 | &chip->dev.parent->kobj, &chip->dev.kobj, (*i)->name); |
| 520 | if (rc) { |
| 521 | tpm_del_legacy_sysfs(chip); |
| 522 | return rc; |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | return 0; |
| 527 | } |
Jason Gunthorpe | 6e592a0 | 2017-11-17 15:24:03 +0200 | [diff] [blame] | 528 | |
| 529 | static int tpm_hwrng_read(struct hwrng *rng, void *data, size_t max, bool wait) |
| 530 | { |
| 531 | struct tpm_chip *chip = container_of(rng, struct tpm_chip, hwrng); |
| 532 | |
| 533 | return tpm_get_random(chip, data, max); |
| 534 | } |
| 535 | |
| 536 | static int tpm_add_hwrng(struct tpm_chip *chip) |
| 537 | { |
| 538 | if (!IS_ENABLED(CONFIG_HW_RANDOM_TPM)) |
| 539 | return 0; |
| 540 | |
| 541 | snprintf(chip->hwrng_name, sizeof(chip->hwrng_name), |
| 542 | "tpm-rng-%d", chip->dev_num); |
| 543 | chip->hwrng.name = chip->hwrng_name; |
| 544 | chip->hwrng.read = tpm_hwrng_read; |
| 545 | return hwrng_register(&chip->hwrng); |
| 546 | } |
| 547 | |
Jarkko Sakkinen | afb5abc | 2014-12-12 11:46:34 -0800 | [diff] [blame] | 548 | /* |
Jarkko Sakkinen | 313d21e | 2014-12-12 11:46:37 -0800 | [diff] [blame] | 549 | * tpm_chip_register() - create a character device for the TPM chip |
Jarkko Sakkinen | afb5abc | 2014-12-12 11:46:34 -0800 | [diff] [blame] | 550 | * @chip: TPM chip to use. |
| 551 | * |
Jarkko Sakkinen | d972b05 | 2015-03-01 23:55:47 +0200 | [diff] [blame] | 552 | * Creates a character device for the TPM chip and adds sysfs attributes for |
| 553 | * the device. As the last step this function adds the chip to the list of TPM |
| 554 | * chips available for in-kernel use. |
Jarkko Sakkinen | afb5abc | 2014-12-12 11:46:34 -0800 | [diff] [blame] | 555 | * |
Jarkko Sakkinen | d972b05 | 2015-03-01 23:55:47 +0200 | [diff] [blame] | 556 | * This function should be only called after the chip initialization is |
| 557 | * complete. |
Jarkko Sakkinen | afb5abc | 2014-12-12 11:46:34 -0800 | [diff] [blame] | 558 | */ |
| 559 | int tpm_chip_register(struct tpm_chip *chip) |
| 560 | { |
| 561 | int rc; |
| 562 | |
Jarkko Sakkinen | 47a6c28 | 2018-11-05 03:02:38 +0200 | [diff] [blame] | 563 | rc = tpm_chip_start(chip); |
Jarkko Sakkinen | a3fbfae | 2018-11-05 02:07:56 +0200 | [diff] [blame] | 564 | if (rc) |
| 565 | return rc; |
Tomas Winkler | b03c437 | 2018-10-19 21:22:59 +0300 | [diff] [blame] | 566 | rc = tpm_auto_startup(chip); |
Jarkko Sakkinen | 47a6c28 | 2018-11-05 03:02:38 +0200 | [diff] [blame] | 567 | tpm_chip_stop(chip); |
Tomas Winkler | b03c437 | 2018-10-19 21:22:59 +0300 | [diff] [blame] | 568 | if (rc) |
| 569 | return rc; |
Jason Gunthorpe | cae8b44 | 2016-07-12 11:41:49 -0600 | [diff] [blame] | 570 | |
Jarkko Sakkinen | 7518a21 | 2016-11-14 05:00:51 -0500 | [diff] [blame] | 571 | tpm_sysfs_add_device(chip); |
| 572 | |
| 573 | rc = tpm_bios_log_setup(chip); |
Jason Gunthorpe | 0cf577a | 2016-11-19 11:18:28 -0700 | [diff] [blame] | 574 | if (rc != 0 && rc != -ENODEV) |
Jarkko Sakkinen | 34d47b6 | 2015-03-18 08:17:14 +0200 | [diff] [blame] | 575 | return rc; |
Jarkko Sakkinen | afb5abc | 2014-12-12 11:46:34 -0800 | [diff] [blame] | 576 | |
Jarkko Sakkinen | 9b774d5 | 2015-04-14 17:56:48 +0300 | [diff] [blame] | 577 | tpm_add_ppi(chip); |
| 578 | |
Jason Gunthorpe | 6e592a0 | 2017-11-17 15:24:03 +0200 | [diff] [blame] | 579 | rc = tpm_add_hwrng(chip); |
| 580 | if (rc) |
| 581 | goto out_ppi; |
| 582 | |
Jarkko Sakkinen | 72c91ce | 2016-01-29 09:47:22 -0800 | [diff] [blame] | 583 | rc = tpm_add_char_device(chip); |
Jason Gunthorpe | 6e592a0 | 2017-11-17 15:24:03 +0200 | [diff] [blame] | 584 | if (rc) |
| 585 | goto out_hwrng; |
Jarkko Sakkinen | d972b05 | 2015-03-01 23:55:47 +0200 | [diff] [blame] | 586 | |
Jason Gunthorpe | 062807f | 2016-04-18 13:26:13 -0400 | [diff] [blame] | 587 | rc = tpm_add_legacy_sysfs(chip); |
| 588 | if (rc) { |
| 589 | tpm_chip_unregister(chip); |
| 590 | return rc; |
Jarkko Sakkinen | d56e4f7 | 2015-11-07 13:33:25 +0200 | [diff] [blame] | 591 | } |
| 592 | |
Jarkko Sakkinen | afb5abc | 2014-12-12 11:46:34 -0800 | [diff] [blame] | 593 | return 0; |
Jason Gunthorpe | 6e592a0 | 2017-11-17 15:24:03 +0200 | [diff] [blame] | 594 | |
| 595 | out_hwrng: |
| 596 | if (IS_ENABLED(CONFIG_HW_RANDOM_TPM)) |
| 597 | hwrng_unregister(&chip->hwrng); |
| 598 | out_ppi: |
| 599 | tpm_bios_log_teardown(chip); |
| 600 | |
| 601 | return rc; |
Jarkko Sakkinen | afb5abc | 2014-12-12 11:46:34 -0800 | [diff] [blame] | 602 | } |
| 603 | EXPORT_SYMBOL_GPL(tpm_chip_register); |
| 604 | |
| 605 | /* |
| 606 | * tpm_chip_unregister() - release the TPM driver |
| 607 | * @chip: TPM chip to use. |
| 608 | * |
| 609 | * Takes the chip first away from the list of available TPM chips and then |
| 610 | * cleans up all the resources reserved by tpm_chip_register(). |
| 611 | * |
Jason Gunthorpe | 4e26195 | 2016-02-12 20:29:53 -0700 | [diff] [blame] | 612 | * Once this function returns the driver call backs in 'op's will not be |
| 613 | * running and will no longer start. |
| 614 | * |
Jarkko Sakkinen | afb5abc | 2014-12-12 11:46:34 -0800 | [diff] [blame] | 615 | * NOTE: This function should be only called before deinitializing chip |
| 616 | * resources. |
| 617 | */ |
| 618 | void tpm_chip_unregister(struct tpm_chip *chip) |
| 619 | { |
Jason Gunthorpe | 062807f | 2016-04-18 13:26:13 -0400 | [diff] [blame] | 620 | tpm_del_legacy_sysfs(chip); |
Jason Gunthorpe | 6e592a0 | 2017-11-17 15:24:03 +0200 | [diff] [blame] | 621 | if (IS_ENABLED(CONFIG_HW_RANDOM_TPM)) |
| 622 | hwrng_unregister(&chip->hwrng); |
Jarkko Sakkinen | 7518a21 | 2016-11-14 05:00:51 -0500 | [diff] [blame] | 623 | tpm_bios_log_teardown(chip); |
Linus Torvalds | af82455 | 2017-05-04 19:07:10 -0700 | [diff] [blame] | 624 | if (chip->flags & TPM_CHIP_FLAG_TPM2) |
| 625 | cdev_device_del(&chip->cdevs, &chip->devs); |
Jarkko Sakkinen | 72c91ce | 2016-01-29 09:47:22 -0800 | [diff] [blame] | 626 | tpm_del_char_device(chip); |
Jarkko Sakkinen | afb5abc | 2014-12-12 11:46:34 -0800 | [diff] [blame] | 627 | } |
| 628 | EXPORT_SYMBOL_GPL(tpm_chip_unregister); |