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