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