blob: d47ad10a35fe3362156fbdb15038eb4fda661a97 [file] [log] [blame]
Thomas Gleixnerb886d83c2019-06-01 10:08:55 +02001// SPDX-License-Identifier: GPL-2.0-only
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -08002/*
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 Sakkinenafb5abc2014-12-12 11:46:34 -080016 */
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 Sakkinen313d21e2014-12-12 11:46:37 -080023#include <linux/major.h>
Thiebaud Weksteenfd3ec362017-09-20 10:13:36 +020024#include <linux/tpm_eventlog.h>
Jason Gunthorpe6e592a02017-11-17 15:24:03 +020025#include <linux/hw_random.h>
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -080026#include "tpm.h"
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -080027
Stefan Berger15516782016-02-29 08:53:02 -050028DEFINE_IDR(dev_nums_idr);
29static DEFINE_MUTEX(idr_lock);
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -080030
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -080031struct class *tpm_class;
James Bottomleyfdc915f2017-01-03 09:07:32 -080032struct class *tpmrm_class;
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -080033dev_t tpm_devt;
34
Jarkko Sakkinen47a6c282018-11-05 03:02:38 +020035static int tpm_request_locality(struct tpm_chip *chip)
Jarkko Sakkinen719b7d82018-11-04 21:18:46 +020036{
37 int rc;
38
Jarkko Sakkinen719b7d82018-11-04 21:18:46 +020039 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 Sakkinen47a6c282018-11-05 03:02:38 +020050static void tpm_relinquish_locality(struct tpm_chip *chip)
Jarkko Sakkinen719b7d82018-11-04 21:18:46 +020051{
52 int rc;
53
Jarkko Sakkinen719b7d82018-11-04 21:18:46 +020054 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 Sakkinen47a6c282018-11-05 03:02:38 +020064static int tpm_cmd_ready(struct tpm_chip *chip)
Jarkko Sakkinen719b7d82018-11-04 21:18:46 +020065{
Jarkko Sakkinen719b7d82018-11-04 21:18:46 +020066 if (!chip->ops->cmd_ready)
67 return 0;
68
69 return chip->ops->cmd_ready(chip);
70}
71
Jarkko Sakkinen47a6c282018-11-05 03:02:38 +020072static int tpm_go_idle(struct tpm_chip *chip)
Jarkko Sakkinen719b7d82018-11-04 21:18:46 +020073{
Jarkko Sakkinen719b7d82018-11-04 21:18:46 +020074 if (!chip->ops->go_idle)
75 return 0;
76
77 return chip->ops->go_idle(chip);
78}
79
80/**
81 * tpm_chip_start() - power on the TPM
82 * @chip: a TPM chip to use
Jarkko Sakkinen719b7d82018-11-04 21:18:46 +020083 *
84 * Return:
85 * * The response length - OK
86 * * -errno - A system error
87 */
Jarkko Sakkinen47a6c282018-11-05 03:02:38 +020088int tpm_chip_start(struct tpm_chip *chip)
Jarkko Sakkinen719b7d82018-11-04 21:18:46 +020089{
90 int ret;
91
92 if (chip->ops->clk_enable)
93 chip->ops->clk_enable(chip, true);
94
95 if (chip->locality == -1) {
Jarkko Sakkinen47a6c282018-11-05 03:02:38 +020096 ret = tpm_request_locality(chip);
Jarkko Sakkinen719b7d82018-11-04 21:18:46 +020097 if (ret) {
98 chip->ops->clk_enable(chip, false);
99 return ret;
100 }
101 }
102
Jarkko Sakkinen47a6c282018-11-05 03:02:38 +0200103 ret = tpm_cmd_ready(chip);
Jarkko Sakkinen719b7d82018-11-04 21:18:46 +0200104 if (ret) {
Jarkko Sakkinen47a6c282018-11-05 03:02:38 +0200105 tpm_relinquish_locality(chip);
Jarkko Sakkinen719b7d82018-11-04 21:18:46 +0200106 if (chip->ops->clk_enable)
107 chip->ops->clk_enable(chip, false);
108 return ret;
109 }
110
111 return 0;
112}
113EXPORT_SYMBOL_GPL(tpm_chip_start);
114
115/**
116 * tpm_chip_stop() - power off the TPM
117 * @chip: a TPM chip to use
Jarkko Sakkinen719b7d82018-11-04 21:18:46 +0200118 *
119 * Return:
120 * * The response length - OK
121 * * -errno - A system error
122 */
Jarkko Sakkinen47a6c282018-11-05 03:02:38 +0200123void tpm_chip_stop(struct tpm_chip *chip)
Jarkko Sakkinen719b7d82018-11-04 21:18:46 +0200124{
Jarkko Sakkinen47a6c282018-11-05 03:02:38 +0200125 tpm_go_idle(chip);
126 tpm_relinquish_locality(chip);
Jarkko Sakkinen719b7d82018-11-04 21:18:46 +0200127 if (chip->ops->clk_enable)
128 chip->ops->clk_enable(chip, false);
129}
130EXPORT_SYMBOL_GPL(tpm_chip_stop);
131
Jason Gunthorpe4e261952016-02-12 20:29:53 -0700132/**
133 * tpm_try_get_ops() - Get a ref to the tpm_chip
134 * @chip: Chip to ref
135 *
136 * The caller must already have some kind of locking to ensure that chip is
137 * valid. This function will lock the chip so that the ops member can be
138 * accessed safely. The locking prevents tpm_chip_unregister from
139 * completing, so it should not be held for long periods.
140 *
141 * Returns -ERRNO if the chip could not be got.
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800142 */
Jason Gunthorpe4e261952016-02-12 20:29:53 -0700143int tpm_try_get_ops(struct tpm_chip *chip)
144{
145 int rc = -EIO;
146
147 get_device(&chip->dev);
148
149 down_read(&chip->ops_sem);
150 if (!chip->ops)
Jarkko Sakkinena3fbfae2018-11-05 02:07:56 +0200151 goto out_ops;
Jason Gunthorpe4e261952016-02-12 20:29:53 -0700152
Jarkko Sakkinen2f257402018-11-04 20:01:42 +0200153 mutex_lock(&chip->tpm_mutex);
Jarkko Sakkinen47a6c282018-11-05 03:02:38 +0200154 rc = tpm_chip_start(chip);
Jarkko Sakkinena3fbfae2018-11-05 02:07:56 +0200155 if (rc)
156 goto out_lock;
157
Jason Gunthorpe4e261952016-02-12 20:29:53 -0700158 return 0;
159out_lock:
Jarkko Sakkinena3fbfae2018-11-05 02:07:56 +0200160 mutex_unlock(&chip->tpm_mutex);
161out_ops:
Jason Gunthorpe4e261952016-02-12 20:29:53 -0700162 up_read(&chip->ops_sem);
163 put_device(&chip->dev);
164 return rc;
165}
166EXPORT_SYMBOL_GPL(tpm_try_get_ops);
167
168/**
169 * tpm_put_ops() - Release a ref to the tpm_chip
170 * @chip: Chip to put
171 *
172 * This is the opposite pair to tpm_try_get_ops(). After this returns chip may
173 * be kfree'd.
174 */
175void tpm_put_ops(struct tpm_chip *chip)
176{
Jarkko Sakkinen47a6c282018-11-05 03:02:38 +0200177 tpm_chip_stop(chip);
Jarkko Sakkinen2f257402018-11-04 20:01:42 +0200178 mutex_unlock(&chip->tpm_mutex);
Jason Gunthorpe4e261952016-02-12 20:29:53 -0700179 up_read(&chip->ops_sem);
180 put_device(&chip->dev);
181}
182EXPORT_SYMBOL_GPL(tpm_put_ops);
183
184/**
Stefan Bergeraaae8152018-06-26 15:09:30 -0400185 * tpm_default_chip() - find a TPM chip and get a reference to it
186 */
187struct tpm_chip *tpm_default_chip(void)
188{
189 struct tpm_chip *chip, *res = NULL;
190 int chip_num = 0;
191 int chip_prev;
192
193 mutex_lock(&idr_lock);
194
195 do {
196 chip_prev = chip_num;
197 chip = idr_get_next(&dev_nums_idr, &chip_num);
198 if (chip) {
199 get_device(&chip->dev);
200 res = chip;
201 break;
202 }
203 } while (chip_prev != chip_num);
204
205 mutex_unlock(&idr_lock);
206
207 return res;
208}
209EXPORT_SYMBOL_GPL(tpm_default_chip);
210
211/**
Stefan Bergerfc1d52b2018-06-26 07:06:15 -0400212 * tpm_find_get_ops() - find and reserve a TPM chip
Jarkko Sakkinenaad887f2017-11-05 13:16:26 +0200213 * @chip: a &struct tpm_chip instance, %NULL for the default chip
Jason Gunthorpe4e261952016-02-12 20:29:53 -0700214 *
Jarkko Sakkinenaad887f2017-11-05 13:16:26 +0200215 * Finds a TPM chip and reserves its class device and operations. The chip must
Stefan Bergerfc1d52b2018-06-26 07:06:15 -0400216 * be released with tpm_put_ops() after use.
217 * This function is for internal use only. It supports existing TPM callers
218 * by accepting NULL, but those callers should be converted to pass in a chip
219 * directly.
Jarkko Sakkinenaad887f2017-11-05 13:16:26 +0200220 *
221 * Return:
222 * A reserved &struct tpm_chip instance.
223 * %NULL if a chip is not found.
224 * %NULL if the chip is not available.
Matthew Wilcox37f49152016-12-14 15:09:16 -0800225 */
Stefan Bergerfc1d52b2018-06-26 07:06:15 -0400226struct tpm_chip *tpm_find_get_ops(struct tpm_chip *chip)
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800227{
Stefan Bergereccc9bb2018-06-26 15:09:31 -0400228 int rc;
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800229
Stefan Bergereccc9bb2018-06-26 15:09:31 -0400230 if (chip) {
Jarkko Sakkinenaad887f2017-11-05 13:16:26 +0200231 if (!tpm_try_get_ops(chip))
Stefan Bergereccc9bb2018-06-26 15:09:31 -0400232 return chip;
233 return NULL;
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800234 }
Stefan Berger15516782016-02-29 08:53:02 -0500235
Stefan Bergereccc9bb2018-06-26 15:09:31 -0400236 chip = tpm_default_chip();
237 if (!chip)
238 return NULL;
239 rc = tpm_try_get_ops(chip);
240 /* release additional reference we got from tpm_default_chip() */
241 put_device(&chip->dev);
242 if (rc)
243 return NULL;
244 return chip;
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800245}
246
247/**
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -0800248 * tpm_dev_release() - free chip memory and the device number
249 * @dev: the character device for the TPM chip
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800250 *
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -0800251 * This is used as the release function for the character device.
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800252 */
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -0800253static void tpm_dev_release(struct device *dev)
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800254{
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -0800255 struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev);
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800256
Stefan Berger15516782016-02-29 08:53:02 -0500257 mutex_lock(&idr_lock);
258 idr_remove(&dev_nums_idr, chip->dev_num);
259 mutex_unlock(&idr_lock);
260
Nayna Jain748935e2016-11-14 05:00:52 -0500261 kfree(chip->log.bios_event_log);
Jarkko Sakkinen745b3612017-01-06 14:03:45 +0200262 kfree(chip->work_space.context_buf);
James Bottomley4d578562017-01-31 15:47:31 -0800263 kfree(chip->work_space.session_buf);
Roberto Sassubcfff832019-02-06 17:24:47 +0100264 kfree(chip->allocated_banks);
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800265 kfree(chip);
266}
267
James Bottomleyfdc915f2017-01-03 09:07:32 -0800268static void tpm_devs_release(struct device *dev)
269{
270 struct tpm_chip *chip = container_of(dev, struct tpm_chip, devs);
271
272 /* release the master device reference */
273 put_device(&chip->dev);
274}
275
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800276/**
Josh Zimmermand1bd4a72017-06-25 14:53:24 -0700277 * tpm_class_shutdown() - prepare the TPM device for loss of power.
278 * @dev: device to which the chip is associated.
279 *
280 * Issues a TPM2_Shutdown command prior to loss of power, as required by the
281 * TPM 2.0 spec.
282 * Then, calls bus- and device- specific shutdown code.
283 *
284 * XXX: This codepath relies on the fact that sysfs is not enabled for
285 * TPM2: sysfs uses an implicit lock on chip->ops, so this could race if TPM2
286 * has sysfs support enabled before TPM sysfs's implicit locking is fixed.
287 */
288static int tpm_class_shutdown(struct device *dev)
289{
290 struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev);
291
Vadim Sukhomlinovdb4d8cb2019-06-10 15:01:18 -0700292 down_write(&chip->ops_sem);
Josh Zimmermand1bd4a72017-06-25 14:53:24 -0700293 if (chip->flags & TPM_CHIP_FLAG_TPM2) {
Jarkko Sakkinen47a6c282018-11-05 03:02:38 +0200294 if (!tpm_chip_start(chip)) {
Jarkko Sakkinena3fbfae2018-11-05 02:07:56 +0200295 tpm2_shutdown(chip, TPM2_SU_CLEAR);
Jarkko Sakkinen47a6c282018-11-05 03:02:38 +0200296 tpm_chip_stop(chip);
Jarkko Sakkinena3fbfae2018-11-05 02:07:56 +0200297 }
Josh Zimmermand1bd4a72017-06-25 14:53:24 -0700298 }
Vadim Sukhomlinovdb4d8cb2019-06-10 15:01:18 -0700299 chip->ops = NULL;
300 up_write(&chip->ops_sem);
Michal Suchanek75216212017-08-11 15:44:43 +0200301
Josh Zimmermand1bd4a72017-06-25 14:53:24 -0700302 return 0;
303}
304
305/**
Jason Gunthorpe3897cd92016-02-11 12:45:48 -0700306 * tpm_chip_alloc() - allocate a new struct tpm_chip instance
307 * @pdev: device to which the chip is associated
308 * At this point pdev mst be initialized, but does not have to
309 * be registered
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800310 * @ops: struct tpm_class_ops instance
311 *
312 * Allocates a new struct tpm_chip instance and assigns a free
Jason Gunthorpe3897cd92016-02-11 12:45:48 -0700313 * device number for it. Must be paired with put_device(&chip->dev).
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800314 */
Winkler, Tomas2998b022016-11-23 12:04:13 +0200315struct tpm_chip *tpm_chip_alloc(struct device *pdev,
Jason Gunthorpe3897cd92016-02-11 12:45:48 -0700316 const struct tpm_class_ops *ops)
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800317{
318 struct tpm_chip *chip;
Jarkko Sakkinen4f3b1932016-02-13 11:58:16 +0200319 int rc;
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800320
321 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
322 if (chip == NULL)
323 return ERR_PTR(-ENOMEM);
324
325 mutex_init(&chip->tpm_mutex);
Jason Gunthorpe4e261952016-02-12 20:29:53 -0700326 init_rwsem(&chip->ops_sem);
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800327
328 chip->ops = ops;
329
Stefan Berger15516782016-02-29 08:53:02 -0500330 mutex_lock(&idr_lock);
331 rc = idr_alloc(&dev_nums_idr, NULL, 0, TPM_NUM_DEVICES, GFP_KERNEL);
332 mutex_unlock(&idr_lock);
333 if (rc < 0) {
Winkler, Tomas2998b022016-11-23 12:04:13 +0200334 dev_err(pdev, "No available tpm device numbers\n");
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800335 kfree(chip);
Stefan Berger15516782016-02-29 08:53:02 -0500336 return ERR_PTR(rc);
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800337 }
Stefan Berger15516782016-02-29 08:53:02 -0500338 chip->dev_num = rc;
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800339
Jason Gunthorpe3635e2e2016-02-29 12:29:48 -0500340 device_initialize(&chip->dev);
James Bottomleyfdc915f2017-01-03 09:07:32 -0800341 device_initialize(&chip->devs);
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800342
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -0800343 chip->dev.class = tpm_class;
Michal Suchanek75216212017-08-11 15:44:43 +0200344 chip->dev.class->shutdown_pre = tpm_class_shutdown;
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -0800345 chip->dev.release = tpm_dev_release;
Winkler, Tomas2998b022016-11-23 12:04:13 +0200346 chip->dev.parent = pdev;
Jarkko Sakkinen9b774d52015-04-14 17:56:48 +0300347 chip->dev.groups = chip->groups;
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -0800348
James Bottomleyfdc915f2017-01-03 09:07:32 -0800349 chip->devs.parent = pdev;
350 chip->devs.class = tpmrm_class;
351 chip->devs.release = tpm_devs_release;
352 /* get extra reference on main device to hold on
353 * behalf of devs. This holds the chip structure
354 * while cdevs is in use. The corresponding put
Stefan Berger8979b022017-04-17 21:58:26 -0400355 * is in the tpm_devs_release (TPM2 only)
James Bottomleyfdc915f2017-01-03 09:07:32 -0800356 */
Stefan Berger8979b022017-04-17 21:58:26 -0400357 if (chip->flags & TPM_CHIP_FLAG_TPM2)
358 get_device(&chip->dev);
James Bottomleyfdc915f2017-01-03 09:07:32 -0800359
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -0800360 if (chip->dev_num == 0)
361 chip->dev.devt = MKDEV(MISC_MAJOR, TPM_MINOR);
362 else
363 chip->dev.devt = MKDEV(MAJOR(tpm_devt), chip->dev_num);
364
James Bottomleyfdc915f2017-01-03 09:07:32 -0800365 chip->devs.devt =
366 MKDEV(MAJOR(tpm_devt), chip->dev_num + TPM_NUM_DEVICES);
367
Jason Gunthorpe3635e2e2016-02-29 12:29:48 -0500368 rc = dev_set_name(&chip->dev, "tpm%d", chip->dev_num);
369 if (rc)
370 goto out;
James Bottomleyfdc915f2017-01-03 09:07:32 -0800371 rc = dev_set_name(&chip->devs, "tpmrm%d", chip->dev_num);
372 if (rc)
373 goto out;
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -0800374
Winkler, Tomas2998b022016-11-23 12:04:13 +0200375 if (!pdev)
Stefan Berger2f9f5372016-04-18 13:26:14 -0400376 chip->flags |= TPM_CHIP_FLAG_VIRTUAL;
377
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -0800378 cdev_init(&chip->cdev, &tpm_fops);
James Bottomleyfdc915f2017-01-03 09:07:32 -0800379 cdev_init(&chip->cdevs, &tpmrm_fops);
Stefan Berger2072df42016-02-29 08:53:01 -0500380 chip->cdev.owner = THIS_MODULE;
James Bottomleyfdc915f2017-01-03 09:07:32 -0800381 chip->cdevs.owner = THIS_MODULE;
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -0800382
Jarkko Sakkinen745b3612017-01-06 14:03:45 +0200383 chip->work_space.context_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
384 if (!chip->work_space.context_buf) {
385 rc = -ENOMEM;
386 goto out;
387 }
James Bottomley4d578562017-01-31 15:47:31 -0800388 chip->work_space.session_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
389 if (!chip->work_space.session_buf) {
390 rc = -ENOMEM;
391 goto out;
392 }
Jarkko Sakkinen745b3612017-01-06 14:03:45 +0200393
Jarkko Sakkinen877c57d2017-03-24 11:45:49 +0200394 chip->locality = -1;
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800395 return chip;
Jason Gunthorpe3635e2e2016-02-29 12:29:48 -0500396
397out:
James Bottomleyfdc915f2017-01-03 09:07:32 -0800398 put_device(&chip->devs);
Jason Gunthorpe3635e2e2016-02-29 12:29:48 -0500399 put_device(&chip->dev);
400 return ERR_PTR(rc);
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800401}
Jason Gunthorpe3897cd92016-02-11 12:45:48 -0700402EXPORT_SYMBOL_GPL(tpm_chip_alloc);
403
404/**
405 * tpmm_chip_alloc() - allocate a new struct tpm_chip instance
406 * @pdev: parent device to which the chip is associated
407 * @ops: struct tpm_class_ops instance
408 *
409 * Same as tpm_chip_alloc except devm is used to do the put_device
410 */
411struct tpm_chip *tpmm_chip_alloc(struct device *pdev,
412 const struct tpm_class_ops *ops)
413{
414 struct tpm_chip *chip;
415 int rc;
416
417 chip = tpm_chip_alloc(pdev, ops);
418 if (IS_ERR(chip))
419 return chip;
420
Sudip Mukherjee2b88cd92016-06-12 15:05:29 +0100421 rc = devm_add_action_or_reset(pdev,
422 (void (*)(void *)) put_device,
423 &chip->dev);
424 if (rc)
Jason Gunthorpe3897cd92016-02-11 12:45:48 -0700425 return ERR_PTR(rc);
Jason Gunthorpe3897cd92016-02-11 12:45:48 -0700426
427 dev_set_drvdata(pdev, chip);
428
429 return chip;
430}
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800431EXPORT_SYMBOL_GPL(tpmm_chip_alloc);
432
Jarkko Sakkinen72c91ce2016-01-29 09:47:22 -0800433static int tpm_add_char_device(struct tpm_chip *chip)
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -0800434{
435 int rc;
436
Logan Gunthorpe8dbbf582017-03-17 12:48:13 -0600437 rc = cdev_device_add(&chip->cdev, &chip->dev);
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -0800438 if (rc) {
439 dev_err(&chip->dev,
Logan Gunthorpe8dbbf582017-03-17 12:48:13 -0600440 "unable to cdev_device_add() %s, major %d, minor %d, err=%d\n",
Jason Gunthorpe3635e2e2016-02-29 12:29:48 -0500441 dev_name(&chip->dev), MAJOR(chip->dev.devt),
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -0800442 MINOR(chip->dev.devt), rc);
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -0800443 return rc;
444 }
445
Linus Torvaldsaf824552017-05-04 19:07:10 -0700446 if (chip->flags & TPM_CHIP_FLAG_TPM2) {
447 rc = cdev_device_add(&chip->cdevs, &chip->devs);
448 if (rc) {
449 dev_err(&chip->devs,
450 "unable to cdev_device_add() %s, major %d, minor %d, err=%d\n",
451 dev_name(&chip->devs), MAJOR(chip->devs.devt),
452 MINOR(chip->devs.devt), rc);
453 return rc;
454 }
James Bottomleyfdc915f2017-01-03 09:07:32 -0800455 }
456
Stefan Berger15516782016-02-29 08:53:02 -0500457 /* Make the chip available. */
458 mutex_lock(&idr_lock);
459 idr_replace(&dev_nums_idr, chip, chip->dev_num);
460 mutex_unlock(&idr_lock);
461
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -0800462 return rc;
463}
464
Jarkko Sakkinen72c91ce2016-01-29 09:47:22 -0800465static void tpm_del_char_device(struct tpm_chip *chip)
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -0800466{
Logan Gunthorpe8dbbf582017-03-17 12:48:13 -0600467 cdev_device_del(&chip->cdev, &chip->dev);
Stefan Berger15516782016-02-29 08:53:02 -0500468
469 /* Make the chip unavailable. */
470 mutex_lock(&idr_lock);
471 idr_replace(&dev_nums_idr, NULL, chip->dev_num);
472 mutex_unlock(&idr_lock);
Jason Gunthorpe4e261952016-02-12 20:29:53 -0700473
474 /* Make the driver uncallable. */
475 down_write(&chip->ops_sem);
Jarkko Sakkinena3fbfae2018-11-05 02:07:56 +0200476 if (chip->flags & TPM_CHIP_FLAG_TPM2) {
Jarkko Sakkinen47a6c282018-11-05 03:02:38 +0200477 if (!tpm_chip_start(chip)) {
Jarkko Sakkinena3fbfae2018-11-05 02:07:56 +0200478 tpm2_shutdown(chip, TPM2_SU_CLEAR);
Jarkko Sakkinen47a6c282018-11-05 03:02:38 +0200479 tpm_chip_stop(chip);
Jarkko Sakkinena3fbfae2018-11-05 02:07:56 +0200480 }
481 }
Jason Gunthorpe4e261952016-02-12 20:29:53 -0700482 chip->ops = NULL;
483 up_write(&chip->ops_sem);
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -0800484}
485
Jason Gunthorpe062807f2016-04-18 13:26:13 -0400486static void tpm_del_legacy_sysfs(struct tpm_chip *chip)
487{
488 struct attribute **i;
489
Stefan Berger2f9f5372016-04-18 13:26:14 -0400490 if (chip->flags & (TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_VIRTUAL))
Jason Gunthorpe062807f2016-04-18 13:26:13 -0400491 return;
492
493 sysfs_remove_link(&chip->dev.parent->kobj, "ppi");
494
495 for (i = chip->groups[0]->attrs; *i != NULL; ++i)
496 sysfs_remove_link(&chip->dev.parent->kobj, (*i)->name);
497}
498
499/* For compatibility with legacy sysfs paths we provide symlinks from the
500 * parent dev directory to selected names within the tpm chip directory. Old
501 * kernel versions created these files directly under the parent.
502 */
503static int tpm_add_legacy_sysfs(struct tpm_chip *chip)
504{
505 struct attribute **i;
506 int rc;
507
Stefan Berger2f9f5372016-04-18 13:26:14 -0400508 if (chip->flags & (TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_VIRTUAL))
Jason Gunthorpe062807f2016-04-18 13:26:13 -0400509 return 0;
510
511 rc = __compat_only_sysfs_link_entry_to_kobj(
512 &chip->dev.parent->kobj, &chip->dev.kobj, "ppi");
513 if (rc && rc != -ENOENT)
514 return rc;
515
516 /* All the names from tpm-sysfs */
517 for (i = chip->groups[0]->attrs; *i != NULL; ++i) {
518 rc = __compat_only_sysfs_link_entry_to_kobj(
519 &chip->dev.parent->kobj, &chip->dev.kobj, (*i)->name);
520 if (rc) {
521 tpm_del_legacy_sysfs(chip);
522 return rc;
523 }
524 }
525
526 return 0;
527}
Jason Gunthorpe6e592a02017-11-17 15:24:03 +0200528
529static int tpm_hwrng_read(struct hwrng *rng, void *data, size_t max, bool wait)
530{
531 struct tpm_chip *chip = container_of(rng, struct tpm_chip, hwrng);
532
533 return tpm_get_random(chip, data, max);
534}
535
536static int tpm_add_hwrng(struct tpm_chip *chip)
537{
538 if (!IS_ENABLED(CONFIG_HW_RANDOM_TPM))
539 return 0;
540
541 snprintf(chip->hwrng_name, sizeof(chip->hwrng_name),
542 "tpm-rng-%d", chip->dev_num);
543 chip->hwrng.name = chip->hwrng_name;
544 chip->hwrng.read = tpm_hwrng_read;
545 return hwrng_register(&chip->hwrng);
546}
547
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800548/*
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -0800549 * tpm_chip_register() - create a character device for the TPM chip
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800550 * @chip: TPM chip to use.
551 *
Jarkko Sakkinend972b052015-03-01 23:55:47 +0200552 * Creates a character device for the TPM chip and adds sysfs attributes for
553 * the device. As the last step this function adds the chip to the list of TPM
554 * chips available for in-kernel use.
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800555 *
Jarkko Sakkinend972b052015-03-01 23:55:47 +0200556 * This function should be only called after the chip initialization is
557 * complete.
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800558 */
559int tpm_chip_register(struct tpm_chip *chip)
560{
561 int rc;
562
Jarkko Sakkinen47a6c282018-11-05 03:02:38 +0200563 rc = tpm_chip_start(chip);
Jarkko Sakkinena3fbfae2018-11-05 02:07:56 +0200564 if (rc)
565 return rc;
Tomas Winklerb03c4372018-10-19 21:22:59 +0300566 rc = tpm_auto_startup(chip);
Jarkko Sakkinen47a6c282018-11-05 03:02:38 +0200567 tpm_chip_stop(chip);
Tomas Winklerb03c4372018-10-19 21:22:59 +0300568 if (rc)
569 return rc;
Jason Gunthorpecae8b442016-07-12 11:41:49 -0600570
Jarkko Sakkinen7518a212016-11-14 05:00:51 -0500571 tpm_sysfs_add_device(chip);
572
573 rc = tpm_bios_log_setup(chip);
Jason Gunthorpe0cf577a2016-11-19 11:18:28 -0700574 if (rc != 0 && rc != -ENODEV)
Jarkko Sakkinen34d47b62015-03-18 08:17:14 +0200575 return rc;
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800576
Jarkko Sakkinen9b774d52015-04-14 17:56:48 +0300577 tpm_add_ppi(chip);
578
Jason Gunthorpe6e592a02017-11-17 15:24:03 +0200579 rc = tpm_add_hwrng(chip);
580 if (rc)
581 goto out_ppi;
582
Jarkko Sakkinen72c91ce2016-01-29 09:47:22 -0800583 rc = tpm_add_char_device(chip);
Jason Gunthorpe6e592a02017-11-17 15:24:03 +0200584 if (rc)
585 goto out_hwrng;
Jarkko Sakkinend972b052015-03-01 23:55:47 +0200586
Jason Gunthorpe062807f2016-04-18 13:26:13 -0400587 rc = tpm_add_legacy_sysfs(chip);
588 if (rc) {
589 tpm_chip_unregister(chip);
590 return rc;
Jarkko Sakkinend56e4f72015-11-07 13:33:25 +0200591 }
592
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800593 return 0;
Jason Gunthorpe6e592a02017-11-17 15:24:03 +0200594
595out_hwrng:
596 if (IS_ENABLED(CONFIG_HW_RANDOM_TPM))
597 hwrng_unregister(&chip->hwrng);
598out_ppi:
599 tpm_bios_log_teardown(chip);
600
601 return rc;
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800602}
603EXPORT_SYMBOL_GPL(tpm_chip_register);
604
605/*
606 * tpm_chip_unregister() - release the TPM driver
607 * @chip: TPM chip to use.
608 *
609 * Takes the chip first away from the list of available TPM chips and then
610 * cleans up all the resources reserved by tpm_chip_register().
611 *
Jason Gunthorpe4e261952016-02-12 20:29:53 -0700612 * Once this function returns the driver call backs in 'op's will not be
613 * running and will no longer start.
614 *
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800615 * NOTE: This function should be only called before deinitializing chip
616 * resources.
617 */
618void tpm_chip_unregister(struct tpm_chip *chip)
619{
Jason Gunthorpe062807f2016-04-18 13:26:13 -0400620 tpm_del_legacy_sysfs(chip);
Jason Gunthorpe6e592a02017-11-17 15:24:03 +0200621 if (IS_ENABLED(CONFIG_HW_RANDOM_TPM))
622 hwrng_unregister(&chip->hwrng);
Jarkko Sakkinen7518a212016-11-14 05:00:51 -0500623 tpm_bios_log_teardown(chip);
Linus Torvaldsaf824552017-05-04 19:07:10 -0700624 if (chip->flags & TPM_CHIP_FLAG_TPM2)
625 cdev_device_del(&chip->cdevs, &chip->devs);
Jarkko Sakkinen72c91ce2016-01-29 09:47:22 -0800626 tpm_del_char_device(chip);
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800627}
628EXPORT_SYMBOL_GPL(tpm_chip_unregister);