blob: 3d6d394a866188107f48495298dfe5a43d8e93f0 [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
Milan Broz1e5ac632019-07-04 09:26:15 +020080static void tpm_clk_enable(struct tpm_chip *chip)
81{
82 if (chip->ops->clk_enable)
83 chip->ops->clk_enable(chip, true);
84}
85
86static void tpm_clk_disable(struct tpm_chip *chip)
87{
88 if (chip->ops->clk_enable)
89 chip->ops->clk_enable(chip, false);
90}
91
Jarkko Sakkinen719b7d82018-11-04 21:18:46 +020092/**
93 * tpm_chip_start() - power on the TPM
94 * @chip: a TPM chip to use
Jarkko Sakkinen719b7d82018-11-04 21:18:46 +020095 *
96 * Return:
97 * * The response length - OK
98 * * -errno - A system error
99 */
Jarkko Sakkinen47a6c282018-11-05 03:02:38 +0200100int tpm_chip_start(struct tpm_chip *chip)
Jarkko Sakkinen719b7d82018-11-04 21:18:46 +0200101{
102 int ret;
103
Milan Broz1e5ac632019-07-04 09:26:15 +0200104 tpm_clk_enable(chip);
Jarkko Sakkinen719b7d82018-11-04 21:18:46 +0200105
106 if (chip->locality == -1) {
Jarkko Sakkinen47a6c282018-11-05 03:02:38 +0200107 ret = tpm_request_locality(chip);
Jarkko Sakkinen719b7d82018-11-04 21:18:46 +0200108 if (ret) {
Milan Broz1e5ac632019-07-04 09:26:15 +0200109 tpm_clk_disable(chip);
Jarkko Sakkinen719b7d82018-11-04 21:18:46 +0200110 return ret;
111 }
112 }
113
Jarkko Sakkinen47a6c282018-11-05 03:02:38 +0200114 ret = tpm_cmd_ready(chip);
Jarkko Sakkinen719b7d82018-11-04 21:18:46 +0200115 if (ret) {
Jarkko Sakkinen47a6c282018-11-05 03:02:38 +0200116 tpm_relinquish_locality(chip);
Milan Broz1e5ac632019-07-04 09:26:15 +0200117 tpm_clk_disable(chip);
Jarkko Sakkinen719b7d82018-11-04 21:18:46 +0200118 return ret;
119 }
120
121 return 0;
122}
123EXPORT_SYMBOL_GPL(tpm_chip_start);
124
125/**
126 * tpm_chip_stop() - power off the TPM
127 * @chip: a TPM chip to use
Jarkko Sakkinen719b7d82018-11-04 21:18:46 +0200128 *
129 * Return:
130 * * The response length - OK
131 * * -errno - A system error
132 */
Jarkko Sakkinen47a6c282018-11-05 03:02:38 +0200133void tpm_chip_stop(struct tpm_chip *chip)
Jarkko Sakkinen719b7d82018-11-04 21:18:46 +0200134{
Jarkko Sakkinen47a6c282018-11-05 03:02:38 +0200135 tpm_go_idle(chip);
136 tpm_relinquish_locality(chip);
Milan Broz1e5ac632019-07-04 09:26:15 +0200137 tpm_clk_disable(chip);
Jarkko Sakkinen719b7d82018-11-04 21:18:46 +0200138}
139EXPORT_SYMBOL_GPL(tpm_chip_stop);
140
Jason Gunthorpe4e261952016-02-12 20:29:53 -0700141/**
142 * tpm_try_get_ops() - Get a ref to the tpm_chip
143 * @chip: Chip to ref
144 *
145 * The caller must already have some kind of locking to ensure that chip is
146 * valid. This function will lock the chip so that the ops member can be
147 * accessed safely. The locking prevents tpm_chip_unregister from
148 * completing, so it should not be held for long periods.
149 *
150 * Returns -ERRNO if the chip could not be got.
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800151 */
Jason Gunthorpe4e261952016-02-12 20:29:53 -0700152int tpm_try_get_ops(struct tpm_chip *chip)
153{
154 int rc = -EIO;
155
156 get_device(&chip->dev);
157
158 down_read(&chip->ops_sem);
159 if (!chip->ops)
Jarkko Sakkinena3fbfae2018-11-05 02:07:56 +0200160 goto out_ops;
Jason Gunthorpe4e261952016-02-12 20:29:53 -0700161
Jarkko Sakkinen2f257402018-11-04 20:01:42 +0200162 mutex_lock(&chip->tpm_mutex);
Jarkko Sakkinen47a6c282018-11-05 03:02:38 +0200163 rc = tpm_chip_start(chip);
Jarkko Sakkinena3fbfae2018-11-05 02:07:56 +0200164 if (rc)
165 goto out_lock;
166
Jason Gunthorpe4e261952016-02-12 20:29:53 -0700167 return 0;
168out_lock:
Jarkko Sakkinena3fbfae2018-11-05 02:07:56 +0200169 mutex_unlock(&chip->tpm_mutex);
170out_ops:
Jason Gunthorpe4e261952016-02-12 20:29:53 -0700171 up_read(&chip->ops_sem);
172 put_device(&chip->dev);
173 return rc;
174}
175EXPORT_SYMBOL_GPL(tpm_try_get_ops);
176
177/**
178 * tpm_put_ops() - Release a ref to the tpm_chip
179 * @chip: Chip to put
180 *
181 * This is the opposite pair to tpm_try_get_ops(). After this returns chip may
182 * be kfree'd.
183 */
184void tpm_put_ops(struct tpm_chip *chip)
185{
Jarkko Sakkinen47a6c282018-11-05 03:02:38 +0200186 tpm_chip_stop(chip);
Jarkko Sakkinen2f257402018-11-04 20:01:42 +0200187 mutex_unlock(&chip->tpm_mutex);
Jason Gunthorpe4e261952016-02-12 20:29:53 -0700188 up_read(&chip->ops_sem);
189 put_device(&chip->dev);
190}
191EXPORT_SYMBOL_GPL(tpm_put_ops);
192
193/**
Stefan Bergeraaae8152018-06-26 15:09:30 -0400194 * tpm_default_chip() - find a TPM chip and get a reference to it
195 */
196struct tpm_chip *tpm_default_chip(void)
197{
198 struct tpm_chip *chip, *res = NULL;
199 int chip_num = 0;
200 int chip_prev;
201
202 mutex_lock(&idr_lock);
203
204 do {
205 chip_prev = chip_num;
206 chip = idr_get_next(&dev_nums_idr, &chip_num);
207 if (chip) {
208 get_device(&chip->dev);
209 res = chip;
210 break;
211 }
212 } while (chip_prev != chip_num);
213
214 mutex_unlock(&idr_lock);
215
216 return res;
217}
218EXPORT_SYMBOL_GPL(tpm_default_chip);
219
220/**
Stefan Bergerfc1d52b2018-06-26 07:06:15 -0400221 * tpm_find_get_ops() - find and reserve a TPM chip
Jarkko Sakkinenaad887f2017-11-05 13:16:26 +0200222 * @chip: a &struct tpm_chip instance, %NULL for the default chip
Jason Gunthorpe4e261952016-02-12 20:29:53 -0700223 *
Jarkko Sakkinenaad887f2017-11-05 13:16:26 +0200224 * Finds a TPM chip and reserves its class device and operations. The chip must
Stefan Bergerfc1d52b2018-06-26 07:06:15 -0400225 * be released with tpm_put_ops() after use.
226 * This function is for internal use only. It supports existing TPM callers
227 * by accepting NULL, but those callers should be converted to pass in a chip
228 * directly.
Jarkko Sakkinenaad887f2017-11-05 13:16:26 +0200229 *
230 * Return:
231 * A reserved &struct tpm_chip instance.
232 * %NULL if a chip is not found.
233 * %NULL if the chip is not available.
Matthew Wilcox37f49152016-12-14 15:09:16 -0800234 */
Stefan Bergerfc1d52b2018-06-26 07:06:15 -0400235struct tpm_chip *tpm_find_get_ops(struct tpm_chip *chip)
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800236{
Stefan Bergereccc9bb2018-06-26 15:09:31 -0400237 int rc;
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800238
Stefan Bergereccc9bb2018-06-26 15:09:31 -0400239 if (chip) {
Jarkko Sakkinenaad887f2017-11-05 13:16:26 +0200240 if (!tpm_try_get_ops(chip))
Stefan Bergereccc9bb2018-06-26 15:09:31 -0400241 return chip;
242 return NULL;
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800243 }
Stefan Berger15516782016-02-29 08:53:02 -0500244
Stefan Bergereccc9bb2018-06-26 15:09:31 -0400245 chip = tpm_default_chip();
246 if (!chip)
247 return NULL;
248 rc = tpm_try_get_ops(chip);
249 /* release additional reference we got from tpm_default_chip() */
250 put_device(&chip->dev);
251 if (rc)
252 return NULL;
253 return chip;
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800254}
255
256/**
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -0800257 * tpm_dev_release() - free chip memory and the device number
258 * @dev: the character device for the TPM chip
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800259 *
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -0800260 * This is used as the release function for the character device.
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800261 */
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -0800262static void tpm_dev_release(struct device *dev)
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800263{
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -0800264 struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev);
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800265
Stefan Berger15516782016-02-29 08:53:02 -0500266 mutex_lock(&idr_lock);
267 idr_remove(&dev_nums_idr, chip->dev_num);
268 mutex_unlock(&idr_lock);
269
Nayna Jain748935e2016-11-14 05:00:52 -0500270 kfree(chip->log.bios_event_log);
Jarkko Sakkinen745b3612017-01-06 14:03:45 +0200271 kfree(chip->work_space.context_buf);
James Bottomley4d578562017-01-31 15:47:31 -0800272 kfree(chip->work_space.session_buf);
Roberto Sassubcfff832019-02-06 17:24:47 +0100273 kfree(chip->allocated_banks);
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800274 kfree(chip);
275}
276
James Bottomleyfdc915f2017-01-03 09:07:32 -0800277static void tpm_devs_release(struct device *dev)
278{
279 struct tpm_chip *chip = container_of(dev, struct tpm_chip, devs);
280
281 /* release the master device reference */
282 put_device(&chip->dev);
283}
284
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800285/**
Josh Zimmermand1bd4a72017-06-25 14:53:24 -0700286 * tpm_class_shutdown() - prepare the TPM device for loss of power.
287 * @dev: device to which the chip is associated.
288 *
289 * Issues a TPM2_Shutdown command prior to loss of power, as required by the
Jarkko Sakkinen28eba2f2019-07-08 17:56:39 +0300290 * TPM 2.0 spec. Then, calls bus- and device- specific shutdown code.
Josh Zimmermand1bd4a72017-06-25 14:53:24 -0700291 *
Jarkko Sakkinen28eba2f2019-07-08 17:56:39 +0300292 * Return: always 0 (i.e. success)
Josh Zimmermand1bd4a72017-06-25 14:53:24 -0700293 */
294static int tpm_class_shutdown(struct device *dev)
295{
296 struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev);
297
Vadim Sukhomlinovdb4d8cb2019-06-10 15:01:18 -0700298 down_write(&chip->ops_sem);
Josh Zimmermand1bd4a72017-06-25 14:53:24 -0700299 if (chip->flags & TPM_CHIP_FLAG_TPM2) {
Jarkko Sakkinen47a6c282018-11-05 03:02:38 +0200300 if (!tpm_chip_start(chip)) {
Jarkko Sakkinena3fbfae2018-11-05 02:07:56 +0200301 tpm2_shutdown(chip, TPM2_SU_CLEAR);
Jarkko Sakkinen47a6c282018-11-05 03:02:38 +0200302 tpm_chip_stop(chip);
Jarkko Sakkinena3fbfae2018-11-05 02:07:56 +0200303 }
Josh Zimmermand1bd4a72017-06-25 14:53:24 -0700304 }
Vadim Sukhomlinovdb4d8cb2019-06-10 15:01:18 -0700305 chip->ops = NULL;
306 up_write(&chip->ops_sem);
Michal Suchanek75216212017-08-11 15:44:43 +0200307
Josh Zimmermand1bd4a72017-06-25 14:53:24 -0700308 return 0;
309}
310
311/**
Jason Gunthorpe3897cd92016-02-11 12:45:48 -0700312 * tpm_chip_alloc() - allocate a new struct tpm_chip instance
313 * @pdev: device to which the chip is associated
314 * At this point pdev mst be initialized, but does not have to
315 * be registered
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800316 * @ops: struct tpm_class_ops instance
317 *
318 * Allocates a new struct tpm_chip instance and assigns a free
Jason Gunthorpe3897cd92016-02-11 12:45:48 -0700319 * device number for it. Must be paired with put_device(&chip->dev).
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800320 */
Winkler, Tomas2998b022016-11-23 12:04:13 +0200321struct tpm_chip *tpm_chip_alloc(struct device *pdev,
Jason Gunthorpe3897cd92016-02-11 12:45:48 -0700322 const struct tpm_class_ops *ops)
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800323{
324 struct tpm_chip *chip;
Jarkko Sakkinen4f3b1932016-02-13 11:58:16 +0200325 int rc;
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800326
327 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
328 if (chip == NULL)
329 return ERR_PTR(-ENOMEM);
330
331 mutex_init(&chip->tpm_mutex);
Jason Gunthorpe4e261952016-02-12 20:29:53 -0700332 init_rwsem(&chip->ops_sem);
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800333
334 chip->ops = ops;
335
Stefan Berger15516782016-02-29 08:53:02 -0500336 mutex_lock(&idr_lock);
337 rc = idr_alloc(&dev_nums_idr, NULL, 0, TPM_NUM_DEVICES, GFP_KERNEL);
338 mutex_unlock(&idr_lock);
339 if (rc < 0) {
Winkler, Tomas2998b022016-11-23 12:04:13 +0200340 dev_err(pdev, "No available tpm device numbers\n");
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800341 kfree(chip);
Stefan Berger15516782016-02-29 08:53:02 -0500342 return ERR_PTR(rc);
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800343 }
Stefan Berger15516782016-02-29 08:53:02 -0500344 chip->dev_num = rc;
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800345
Jason Gunthorpe3635e2e2016-02-29 12:29:48 -0500346 device_initialize(&chip->dev);
James Bottomleyfdc915f2017-01-03 09:07:32 -0800347 device_initialize(&chip->devs);
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800348
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -0800349 chip->dev.class = tpm_class;
Michal Suchanek75216212017-08-11 15:44:43 +0200350 chip->dev.class->shutdown_pre = tpm_class_shutdown;
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -0800351 chip->dev.release = tpm_dev_release;
Winkler, Tomas2998b022016-11-23 12:04:13 +0200352 chip->dev.parent = pdev;
Jarkko Sakkinen9b774d52015-04-14 17:56:48 +0300353 chip->dev.groups = chip->groups;
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -0800354
James Bottomleyfdc915f2017-01-03 09:07:32 -0800355 chip->devs.parent = pdev;
356 chip->devs.class = tpmrm_class;
357 chip->devs.release = tpm_devs_release;
358 /* get extra reference on main device to hold on
359 * behalf of devs. This holds the chip structure
360 * while cdevs is in use. The corresponding put
Stefan Berger8979b022017-04-17 21:58:26 -0400361 * is in the tpm_devs_release (TPM2 only)
James Bottomleyfdc915f2017-01-03 09:07:32 -0800362 */
Stefan Berger8979b022017-04-17 21:58:26 -0400363 if (chip->flags & TPM_CHIP_FLAG_TPM2)
364 get_device(&chip->dev);
James Bottomleyfdc915f2017-01-03 09:07:32 -0800365
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -0800366 if (chip->dev_num == 0)
367 chip->dev.devt = MKDEV(MISC_MAJOR, TPM_MINOR);
368 else
369 chip->dev.devt = MKDEV(MAJOR(tpm_devt), chip->dev_num);
370
James Bottomleyfdc915f2017-01-03 09:07:32 -0800371 chip->devs.devt =
372 MKDEV(MAJOR(tpm_devt), chip->dev_num + TPM_NUM_DEVICES);
373
Jason Gunthorpe3635e2e2016-02-29 12:29:48 -0500374 rc = dev_set_name(&chip->dev, "tpm%d", chip->dev_num);
375 if (rc)
376 goto out;
James Bottomleyfdc915f2017-01-03 09:07:32 -0800377 rc = dev_set_name(&chip->devs, "tpmrm%d", chip->dev_num);
378 if (rc)
379 goto out;
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -0800380
Winkler, Tomas2998b022016-11-23 12:04:13 +0200381 if (!pdev)
Stefan Berger2f9f5372016-04-18 13:26:14 -0400382 chip->flags |= TPM_CHIP_FLAG_VIRTUAL;
383
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -0800384 cdev_init(&chip->cdev, &tpm_fops);
James Bottomleyfdc915f2017-01-03 09:07:32 -0800385 cdev_init(&chip->cdevs, &tpmrm_fops);
Stefan Berger2072df42016-02-29 08:53:01 -0500386 chip->cdev.owner = THIS_MODULE;
James Bottomleyfdc915f2017-01-03 09:07:32 -0800387 chip->cdevs.owner = THIS_MODULE;
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -0800388
Jarkko Sakkinen745b3612017-01-06 14:03:45 +0200389 chip->work_space.context_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
390 if (!chip->work_space.context_buf) {
391 rc = -ENOMEM;
392 goto out;
393 }
James Bottomley4d578562017-01-31 15:47:31 -0800394 chip->work_space.session_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
395 if (!chip->work_space.session_buf) {
396 rc = -ENOMEM;
397 goto out;
398 }
Jarkko Sakkinen745b3612017-01-06 14:03:45 +0200399
Jarkko Sakkinen877c57d2017-03-24 11:45:49 +0200400 chip->locality = -1;
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800401 return chip;
Jason Gunthorpe3635e2e2016-02-29 12:29:48 -0500402
403out:
James Bottomleyfdc915f2017-01-03 09:07:32 -0800404 put_device(&chip->devs);
Jason Gunthorpe3635e2e2016-02-29 12:29:48 -0500405 put_device(&chip->dev);
406 return ERR_PTR(rc);
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800407}
Jason Gunthorpe3897cd92016-02-11 12:45:48 -0700408EXPORT_SYMBOL_GPL(tpm_chip_alloc);
409
410/**
411 * tpmm_chip_alloc() - allocate a new struct tpm_chip instance
412 * @pdev: parent device to which the chip is associated
413 * @ops: struct tpm_class_ops instance
414 *
415 * Same as tpm_chip_alloc except devm is used to do the put_device
416 */
417struct tpm_chip *tpmm_chip_alloc(struct device *pdev,
418 const struct tpm_class_ops *ops)
419{
420 struct tpm_chip *chip;
421 int rc;
422
423 chip = tpm_chip_alloc(pdev, ops);
424 if (IS_ERR(chip))
425 return chip;
426
Sudip Mukherjee2b88cd92016-06-12 15:05:29 +0100427 rc = devm_add_action_or_reset(pdev,
428 (void (*)(void *)) put_device,
429 &chip->dev);
430 if (rc)
Jason Gunthorpe3897cd92016-02-11 12:45:48 -0700431 return ERR_PTR(rc);
Jason Gunthorpe3897cd92016-02-11 12:45:48 -0700432
433 dev_set_drvdata(pdev, chip);
434
435 return chip;
436}
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800437EXPORT_SYMBOL_GPL(tpmm_chip_alloc);
438
Jarkko Sakkinen72c91ce2016-01-29 09:47:22 -0800439static int tpm_add_char_device(struct tpm_chip *chip)
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -0800440{
441 int rc;
442
Logan Gunthorpe8dbbf582017-03-17 12:48:13 -0600443 rc = cdev_device_add(&chip->cdev, &chip->dev);
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -0800444 if (rc) {
445 dev_err(&chip->dev,
Logan Gunthorpe8dbbf582017-03-17 12:48:13 -0600446 "unable to cdev_device_add() %s, major %d, minor %d, err=%d\n",
Jason Gunthorpe3635e2e2016-02-29 12:29:48 -0500447 dev_name(&chip->dev), MAJOR(chip->dev.devt),
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -0800448 MINOR(chip->dev.devt), rc);
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -0800449 return rc;
450 }
451
Linus Torvaldsaf824552017-05-04 19:07:10 -0700452 if (chip->flags & TPM_CHIP_FLAG_TPM2) {
453 rc = cdev_device_add(&chip->cdevs, &chip->devs);
454 if (rc) {
455 dev_err(&chip->devs,
456 "unable to cdev_device_add() %s, major %d, minor %d, err=%d\n",
457 dev_name(&chip->devs), MAJOR(chip->devs.devt),
458 MINOR(chip->devs.devt), rc);
459 return rc;
460 }
James Bottomleyfdc915f2017-01-03 09:07:32 -0800461 }
462
Stefan Berger15516782016-02-29 08:53:02 -0500463 /* Make the chip available. */
464 mutex_lock(&idr_lock);
465 idr_replace(&dev_nums_idr, chip, chip->dev_num);
466 mutex_unlock(&idr_lock);
467
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -0800468 return rc;
469}
470
Jarkko Sakkinen72c91ce2016-01-29 09:47:22 -0800471static void tpm_del_char_device(struct tpm_chip *chip)
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -0800472{
Logan Gunthorpe8dbbf582017-03-17 12:48:13 -0600473 cdev_device_del(&chip->cdev, &chip->dev);
Stefan Berger15516782016-02-29 08:53:02 -0500474
475 /* Make the chip unavailable. */
476 mutex_lock(&idr_lock);
477 idr_replace(&dev_nums_idr, NULL, chip->dev_num);
478 mutex_unlock(&idr_lock);
Jason Gunthorpe4e261952016-02-12 20:29:53 -0700479
480 /* Make the driver uncallable. */
481 down_write(&chip->ops_sem);
Jarkko Sakkinena3fbfae2018-11-05 02:07:56 +0200482 if (chip->flags & TPM_CHIP_FLAG_TPM2) {
Jarkko Sakkinen47a6c282018-11-05 03:02:38 +0200483 if (!tpm_chip_start(chip)) {
Jarkko Sakkinena3fbfae2018-11-05 02:07:56 +0200484 tpm2_shutdown(chip, TPM2_SU_CLEAR);
Jarkko Sakkinen47a6c282018-11-05 03:02:38 +0200485 tpm_chip_stop(chip);
Jarkko Sakkinena3fbfae2018-11-05 02:07:56 +0200486 }
487 }
Jason Gunthorpe4e261952016-02-12 20:29:53 -0700488 chip->ops = NULL;
489 up_write(&chip->ops_sem);
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -0800490}
491
Jason Gunthorpe062807f2016-04-18 13:26:13 -0400492static void tpm_del_legacy_sysfs(struct tpm_chip *chip)
493{
494 struct attribute **i;
495
Stefan Berger2f9f5372016-04-18 13:26:14 -0400496 if (chip->flags & (TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_VIRTUAL))
Jason Gunthorpe062807f2016-04-18 13:26:13 -0400497 return;
498
499 sysfs_remove_link(&chip->dev.parent->kobj, "ppi");
500
501 for (i = chip->groups[0]->attrs; *i != NULL; ++i)
502 sysfs_remove_link(&chip->dev.parent->kobj, (*i)->name);
503}
504
505/* For compatibility with legacy sysfs paths we provide symlinks from the
506 * parent dev directory to selected names within the tpm chip directory. Old
507 * kernel versions created these files directly under the parent.
508 */
509static int tpm_add_legacy_sysfs(struct tpm_chip *chip)
510{
511 struct attribute **i;
512 int rc;
513
Stefan Berger2f9f5372016-04-18 13:26:14 -0400514 if (chip->flags & (TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_VIRTUAL))
Jason Gunthorpe062807f2016-04-18 13:26:13 -0400515 return 0;
516
517 rc = __compat_only_sysfs_link_entry_to_kobj(
518 &chip->dev.parent->kobj, &chip->dev.kobj, "ppi");
519 if (rc && rc != -ENOENT)
520 return rc;
521
522 /* All the names from tpm-sysfs */
523 for (i = chip->groups[0]->attrs; *i != NULL; ++i) {
524 rc = __compat_only_sysfs_link_entry_to_kobj(
525 &chip->dev.parent->kobj, &chip->dev.kobj, (*i)->name);
526 if (rc) {
527 tpm_del_legacy_sysfs(chip);
528 return rc;
529 }
530 }
531
532 return 0;
533}
Jason Gunthorpe6e592a02017-11-17 15:24:03 +0200534
535static int tpm_hwrng_read(struct hwrng *rng, void *data, size_t max, bool wait)
536{
537 struct tpm_chip *chip = container_of(rng, struct tpm_chip, hwrng);
538
539 return tpm_get_random(chip, data, max);
540}
541
542static int tpm_add_hwrng(struct tpm_chip *chip)
543{
544 if (!IS_ENABLED(CONFIG_HW_RANDOM_TPM))
545 return 0;
546
547 snprintf(chip->hwrng_name, sizeof(chip->hwrng_name),
548 "tpm-rng-%d", chip->dev_num);
549 chip->hwrng.name = chip->hwrng_name;
550 chip->hwrng.read = tpm_hwrng_read;
551 return hwrng_register(&chip->hwrng);
552}
553
Nayna Jainfa4f99c2019-07-11 12:13:35 -0400554static int tpm_get_pcr_allocation(struct tpm_chip *chip)
555{
556 int rc;
557
558 rc = (chip->flags & TPM_CHIP_FLAG_TPM2) ?
559 tpm2_get_pcr_allocation(chip) :
560 tpm1_get_pcr_allocation(chip);
561
562 if (rc > 0)
563 return -ENODEV;
564
565 return rc;
566}
567
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800568/*
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -0800569 * tpm_chip_register() - create a character device for the TPM chip
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800570 * @chip: TPM chip to use.
571 *
Jarkko Sakkinend972b052015-03-01 23:55:47 +0200572 * Creates a character device for the TPM chip and adds sysfs attributes for
573 * the device. As the last step this function adds the chip to the list of TPM
574 * chips available for in-kernel use.
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800575 *
Jarkko Sakkinend972b052015-03-01 23:55:47 +0200576 * This function should be only called after the chip initialization is
577 * complete.
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800578 */
579int tpm_chip_register(struct tpm_chip *chip)
580{
581 int rc;
582
Jarkko Sakkinen47a6c282018-11-05 03:02:38 +0200583 rc = tpm_chip_start(chip);
Jarkko Sakkinena3fbfae2018-11-05 02:07:56 +0200584 if (rc)
585 return rc;
Tomas Winklerb03c4372018-10-19 21:22:59 +0300586 rc = tpm_auto_startup(chip);
Nayna Jainfa4f99c2019-07-11 12:13:35 -0400587 if (rc) {
588 tpm_chip_stop(chip);
589 return rc;
590 }
591
592 rc = tpm_get_pcr_allocation(chip);
Jarkko Sakkinen47a6c282018-11-05 03:02:38 +0200593 tpm_chip_stop(chip);
Tomas Winklerb03c4372018-10-19 21:22:59 +0300594 if (rc)
595 return rc;
Jason Gunthorpecae8b442016-07-12 11:41:49 -0600596
Jarkko Sakkinen7518a212016-11-14 05:00:51 -0500597 tpm_sysfs_add_device(chip);
598
599 rc = tpm_bios_log_setup(chip);
Jason Gunthorpe0cf577a2016-11-19 11:18:28 -0700600 if (rc != 0 && rc != -ENODEV)
Jarkko Sakkinen34d47b62015-03-18 08:17:14 +0200601 return rc;
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800602
Jarkko Sakkinen9b774d52015-04-14 17:56:48 +0300603 tpm_add_ppi(chip);
604
Jason Gunthorpe6e592a02017-11-17 15:24:03 +0200605 rc = tpm_add_hwrng(chip);
606 if (rc)
607 goto out_ppi;
608
Jarkko Sakkinen72c91ce2016-01-29 09:47:22 -0800609 rc = tpm_add_char_device(chip);
Jason Gunthorpe6e592a02017-11-17 15:24:03 +0200610 if (rc)
611 goto out_hwrng;
Jarkko Sakkinend972b052015-03-01 23:55:47 +0200612
Jason Gunthorpe062807f2016-04-18 13:26:13 -0400613 rc = tpm_add_legacy_sysfs(chip);
614 if (rc) {
615 tpm_chip_unregister(chip);
616 return rc;
Jarkko Sakkinend56e4f72015-11-07 13:33:25 +0200617 }
618
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800619 return 0;
Jason Gunthorpe6e592a02017-11-17 15:24:03 +0200620
621out_hwrng:
622 if (IS_ENABLED(CONFIG_HW_RANDOM_TPM))
623 hwrng_unregister(&chip->hwrng);
624out_ppi:
625 tpm_bios_log_teardown(chip);
626
627 return rc;
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800628}
629EXPORT_SYMBOL_GPL(tpm_chip_register);
630
631/*
632 * tpm_chip_unregister() - release the TPM driver
633 * @chip: TPM chip to use.
634 *
635 * Takes the chip first away from the list of available TPM chips and then
636 * cleans up all the resources reserved by tpm_chip_register().
637 *
Jason Gunthorpe4e261952016-02-12 20:29:53 -0700638 * Once this function returns the driver call backs in 'op's will not be
639 * running and will no longer start.
640 *
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800641 * NOTE: This function should be only called before deinitializing chip
642 * resources.
643 */
644void tpm_chip_unregister(struct tpm_chip *chip)
645{
Jason Gunthorpe062807f2016-04-18 13:26:13 -0400646 tpm_del_legacy_sysfs(chip);
Jason Gunthorpe6e592a02017-11-17 15:24:03 +0200647 if (IS_ENABLED(CONFIG_HW_RANDOM_TPM))
648 hwrng_unregister(&chip->hwrng);
Jarkko Sakkinen7518a212016-11-14 05:00:51 -0500649 tpm_bios_log_teardown(chip);
Linus Torvaldsaf824552017-05-04 19:07:10 -0700650 if (chip->flags & TPM_CHIP_FLAG_TPM2)
651 cdev_device_del(&chip->cdevs, &chip->devs);
Jarkko Sakkinen72c91ce2016-01-29 09:47:22 -0800652 tpm_del_char_device(chip);
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800653}
654EXPORT_SYMBOL_GPL(tpm_chip_unregister);