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