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 | |
| 32 | static DECLARE_BITMAP(dev_mask, TPM_NUM_DEVICES); |
| 33 | static LIST_HEAD(tpm_chip_list); |
| 34 | static DEFINE_SPINLOCK(driver_lock); |
| 35 | |
Jarkko Sakkinen | 313d21e | 2014-12-12 11:46:37 -0800 | [diff] [blame] | 36 | struct class *tpm_class; |
| 37 | dev_t tpm_devt; |
| 38 | |
Jarkko Sakkinen | afb5abc | 2014-12-12 11:46:34 -0800 | [diff] [blame] | 39 | /* |
| 40 | * tpm_chip_find_get - return tpm_chip for a given chip number |
| 41 | * @chip_num the device number for the chip |
| 42 | */ |
| 43 | struct tpm_chip *tpm_chip_find_get(int chip_num) |
| 44 | { |
| 45 | struct tpm_chip *pos, *chip = NULL; |
| 46 | |
| 47 | rcu_read_lock(); |
| 48 | list_for_each_entry_rcu(pos, &tpm_chip_list, list) { |
| 49 | if (chip_num != TPM_ANY_NUM && chip_num != pos->dev_num) |
| 50 | continue; |
| 51 | |
Jarkko Sakkinen | 71ed848 | 2014-12-12 11:46:36 -0800 | [diff] [blame] | 52 | if (try_module_get(pos->pdev->driver->owner)) { |
Jarkko Sakkinen | afb5abc | 2014-12-12 11:46:34 -0800 | [diff] [blame] | 53 | chip = pos; |
| 54 | break; |
| 55 | } |
| 56 | } |
| 57 | rcu_read_unlock(); |
| 58 | return chip; |
| 59 | } |
| 60 | |
| 61 | /** |
Jarkko Sakkinen | 313d21e | 2014-12-12 11:46:37 -0800 | [diff] [blame] | 62 | * tpm_dev_release() - free chip memory and the device number |
| 63 | * @dev: the character device for the TPM chip |
Jarkko Sakkinen | afb5abc | 2014-12-12 11:46:34 -0800 | [diff] [blame] | 64 | * |
Jarkko Sakkinen | 313d21e | 2014-12-12 11:46:37 -0800 | [diff] [blame] | 65 | * This is used as the release function for the character device. |
Jarkko Sakkinen | afb5abc | 2014-12-12 11:46:34 -0800 | [diff] [blame] | 66 | */ |
Jarkko Sakkinen | 313d21e | 2014-12-12 11:46:37 -0800 | [diff] [blame] | 67 | static void tpm_dev_release(struct device *dev) |
Jarkko Sakkinen | afb5abc | 2014-12-12 11:46:34 -0800 | [diff] [blame] | 68 | { |
Jarkko Sakkinen | 313d21e | 2014-12-12 11:46:37 -0800 | [diff] [blame] | 69 | struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev); |
Jarkko Sakkinen | afb5abc | 2014-12-12 11:46:34 -0800 | [diff] [blame] | 70 | |
| 71 | spin_lock(&driver_lock); |
| 72 | clear_bit(chip->dev_num, dev_mask); |
| 73 | spin_unlock(&driver_lock); |
| 74 | kfree(chip); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * tpmm_chip_alloc() - allocate a new struct tpm_chip instance |
| 79 | * @dev: device to which the chip is associated |
| 80 | * @ops: struct tpm_class_ops instance |
| 81 | * |
| 82 | * Allocates a new struct tpm_chip instance and assigns a free |
| 83 | * device number for it. Caller does not have to worry about |
| 84 | * freeing the allocated resources. When the devices is removed |
| 85 | * devres calls tpmm_chip_remove() to do the job. |
| 86 | */ |
| 87 | struct tpm_chip *tpmm_chip_alloc(struct device *dev, |
| 88 | const struct tpm_class_ops *ops) |
| 89 | { |
| 90 | struct tpm_chip *chip; |
| 91 | |
| 92 | chip = kzalloc(sizeof(*chip), GFP_KERNEL); |
| 93 | if (chip == NULL) |
| 94 | return ERR_PTR(-ENOMEM); |
| 95 | |
| 96 | mutex_init(&chip->tpm_mutex); |
| 97 | INIT_LIST_HEAD(&chip->list); |
| 98 | |
| 99 | chip->ops = ops; |
| 100 | |
| 101 | spin_lock(&driver_lock); |
| 102 | chip->dev_num = find_first_zero_bit(dev_mask, TPM_NUM_DEVICES); |
| 103 | spin_unlock(&driver_lock); |
| 104 | |
| 105 | if (chip->dev_num >= TPM_NUM_DEVICES) { |
| 106 | dev_err(dev, "No available tpm device numbers\n"); |
| 107 | kfree(chip); |
| 108 | return ERR_PTR(-ENOMEM); |
| 109 | } |
| 110 | |
| 111 | set_bit(chip->dev_num, dev_mask); |
| 112 | |
| 113 | scnprintf(chip->devname, sizeof(chip->devname), "tpm%d", chip->dev_num); |
| 114 | |
Jarkko Sakkinen | 71ed848 | 2014-12-12 11:46:36 -0800 | [diff] [blame] | 115 | chip->pdev = dev; |
Jarkko Sakkinen | 313d21e | 2014-12-12 11:46:37 -0800 | [diff] [blame] | 116 | |
Jarkko Sakkinen | afb5abc | 2014-12-12 11:46:34 -0800 | [diff] [blame] | 117 | dev_set_drvdata(dev, chip); |
| 118 | |
Jarkko Sakkinen | 313d21e | 2014-12-12 11:46:37 -0800 | [diff] [blame] | 119 | chip->dev.class = tpm_class; |
| 120 | chip->dev.release = tpm_dev_release; |
| 121 | chip->dev.parent = chip->pdev; |
| 122 | |
| 123 | if (chip->dev_num == 0) |
| 124 | chip->dev.devt = MKDEV(MISC_MAJOR, TPM_MINOR); |
| 125 | else |
| 126 | chip->dev.devt = MKDEV(MAJOR(tpm_devt), chip->dev_num); |
| 127 | |
Jarkko Sakkinen | 743410a | 2015-01-20 12:03:35 +0200 | [diff] [blame] | 128 | dev_set_name(&chip->dev, "%s", chip->devname); |
Jarkko Sakkinen | 313d21e | 2014-12-12 11:46:37 -0800 | [diff] [blame] | 129 | |
| 130 | device_initialize(&chip->dev); |
| 131 | |
| 132 | chip->cdev.owner = chip->pdev->driver->owner; |
| 133 | cdev_init(&chip->cdev, &tpm_fops); |
| 134 | |
Jarkko Sakkinen | afb5abc | 2014-12-12 11:46:34 -0800 | [diff] [blame] | 135 | return chip; |
| 136 | } |
| 137 | EXPORT_SYMBOL_GPL(tpmm_chip_alloc); |
| 138 | |
Jarkko Sakkinen | 313d21e | 2014-12-12 11:46:37 -0800 | [diff] [blame] | 139 | static int tpm_dev_add_device(struct tpm_chip *chip) |
| 140 | { |
| 141 | int rc; |
| 142 | |
Jarkko Sakkinen | 313d21e | 2014-12-12 11:46:37 -0800 | [diff] [blame] | 143 | rc = cdev_add(&chip->cdev, chip->dev.devt, 1); |
| 144 | if (rc) { |
| 145 | dev_err(&chip->dev, |
| 146 | "unable to cdev_add() %s, major %d, minor %d, err=%d\n", |
| 147 | chip->devname, MAJOR(chip->dev.devt), |
| 148 | MINOR(chip->dev.devt), rc); |
| 149 | |
| 150 | device_unregister(&chip->dev); |
| 151 | return rc; |
| 152 | } |
| 153 | |
Jarkko Sakkinen | d972b05 | 2015-03-01 23:55:47 +0200 | [diff] [blame] | 154 | rc = device_add(&chip->dev); |
| 155 | if (rc) { |
| 156 | dev_err(&chip->dev, |
| 157 | "unable to device_register() %s, major %d, minor %d, err=%d\n", |
| 158 | chip->devname, MAJOR(chip->dev.devt), |
| 159 | MINOR(chip->dev.devt), rc); |
| 160 | |
| 161 | return rc; |
| 162 | } |
| 163 | |
Jarkko Sakkinen | 313d21e | 2014-12-12 11:46:37 -0800 | [diff] [blame] | 164 | return rc; |
| 165 | } |
| 166 | |
| 167 | static void tpm_dev_del_device(struct tpm_chip *chip) |
| 168 | { |
| 169 | cdev_del(&chip->cdev); |
| 170 | device_unregister(&chip->dev); |
| 171 | } |
| 172 | |
Jarkko Sakkinen | 34d47b6 | 2015-03-18 08:17:14 +0200 | [diff] [blame^] | 173 | static int tpm1_chip_register(struct tpm_chip *chip) |
| 174 | { |
| 175 | int rc; |
| 176 | |
| 177 | if (chip->flags & TPM_CHIP_FLAG_TPM2) |
| 178 | return 0; |
| 179 | |
| 180 | rc = tpm_sysfs_add_device(chip); |
| 181 | if (rc) |
| 182 | return rc; |
| 183 | |
| 184 | rc = tpm_add_ppi(chip); |
| 185 | if (rc) { |
| 186 | tpm_sysfs_del_device(chip); |
| 187 | return rc; |
| 188 | } |
| 189 | |
| 190 | chip->bios_dir = tpm_bios_log_setup(chip->devname); |
| 191 | |
| 192 | return 0; |
| 193 | } |
| 194 | |
| 195 | static void tpm1_chip_unregister(struct tpm_chip *chip) |
| 196 | { |
| 197 | if (chip->flags & TPM_CHIP_FLAG_TPM2) |
| 198 | return; |
| 199 | |
| 200 | if (chip->bios_dir) |
| 201 | tpm_bios_log_teardown(chip->bios_dir); |
| 202 | |
| 203 | tpm_remove_ppi(chip); |
| 204 | |
| 205 | tpm_sysfs_del_device(chip); |
| 206 | } |
| 207 | |
Jarkko Sakkinen | afb5abc | 2014-12-12 11:46:34 -0800 | [diff] [blame] | 208 | /* |
Jarkko Sakkinen | 313d21e | 2014-12-12 11:46:37 -0800 | [diff] [blame] | 209 | * tpm_chip_register() - create a character device for the TPM chip |
Jarkko Sakkinen | afb5abc | 2014-12-12 11:46:34 -0800 | [diff] [blame] | 210 | * @chip: TPM chip to use. |
| 211 | * |
Jarkko Sakkinen | d972b05 | 2015-03-01 23:55:47 +0200 | [diff] [blame] | 212 | * Creates a character device for the TPM chip and adds sysfs attributes for |
| 213 | * the device. As the last step this function adds the chip to the list of TPM |
| 214 | * chips available for in-kernel use. |
Jarkko Sakkinen | afb5abc | 2014-12-12 11:46:34 -0800 | [diff] [blame] | 215 | * |
Jarkko Sakkinen | d972b05 | 2015-03-01 23:55:47 +0200 | [diff] [blame] | 216 | * This function should be only called after the chip initialization is |
| 217 | * complete. |
Jarkko Sakkinen | afb5abc | 2014-12-12 11:46:34 -0800 | [diff] [blame] | 218 | */ |
| 219 | int tpm_chip_register(struct tpm_chip *chip) |
| 220 | { |
| 221 | int rc; |
| 222 | |
Jarkko Sakkinen | 34d47b6 | 2015-03-18 08:17:14 +0200 | [diff] [blame^] | 223 | rc = tpm1_chip_register(chip); |
| 224 | if (rc) |
| 225 | return rc; |
Jarkko Sakkinen | afb5abc | 2014-12-12 11:46:34 -0800 | [diff] [blame] | 226 | |
Jarkko Sakkinen | d972b05 | 2015-03-01 23:55:47 +0200 | [diff] [blame] | 227 | rc = tpm_dev_add_device(chip); |
| 228 | if (rc) |
Jarkko Sakkinen | 34d47b6 | 2015-03-18 08:17:14 +0200 | [diff] [blame^] | 229 | goto out_err; |
Jarkko Sakkinen | d972b05 | 2015-03-01 23:55:47 +0200 | [diff] [blame] | 230 | |
Jarkko Sakkinen | afb5abc | 2014-12-12 11:46:34 -0800 | [diff] [blame] | 231 | /* Make the chip available. */ |
| 232 | spin_lock(&driver_lock); |
| 233 | list_add_rcu(&chip->list, &tpm_chip_list); |
| 234 | spin_unlock(&driver_lock); |
| 235 | |
| 236 | chip->flags |= TPM_CHIP_FLAG_REGISTERED; |
| 237 | |
| 238 | return 0; |
Jarkko Sakkinen | 34d47b6 | 2015-03-18 08:17:14 +0200 | [diff] [blame^] | 239 | out_err: |
| 240 | tpm1_chip_unregister(chip); |
Jarkko Sakkinen | afb5abc | 2014-12-12 11:46:34 -0800 | [diff] [blame] | 241 | return rc; |
| 242 | } |
| 243 | EXPORT_SYMBOL_GPL(tpm_chip_register); |
| 244 | |
| 245 | /* |
| 246 | * tpm_chip_unregister() - release the TPM driver |
| 247 | * @chip: TPM chip to use. |
| 248 | * |
| 249 | * Takes the chip first away from the list of available TPM chips and then |
| 250 | * cleans up all the resources reserved by tpm_chip_register(). |
| 251 | * |
| 252 | * NOTE: This function should be only called before deinitializing chip |
| 253 | * resources. |
| 254 | */ |
| 255 | void tpm_chip_unregister(struct tpm_chip *chip) |
| 256 | { |
| 257 | if (!(chip->flags & TPM_CHIP_FLAG_REGISTERED)) |
| 258 | return; |
| 259 | |
| 260 | spin_lock(&driver_lock); |
| 261 | list_del_rcu(&chip->list); |
| 262 | spin_unlock(&driver_lock); |
| 263 | synchronize_rcu(); |
| 264 | |
Jarkko Sakkinen | 34d47b6 | 2015-03-18 08:17:14 +0200 | [diff] [blame^] | 265 | tpm1_chip_unregister(chip); |
Jarkko Sakkinen | afb5abc | 2014-12-12 11:46:34 -0800 | [diff] [blame] | 266 | tpm_dev_del_device(chip); |
| 267 | } |
| 268 | EXPORT_SYMBOL_GPL(tpm_chip_unregister); |