64102de91ca3c852359b8369dcb1c698e129ec27
[cascardo/linux.git] / drivers / char / tpm / tpm-chip.c
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>
28 #include "tpm.h"
29 #include "tpm_eventlog.h"
30
31 static DECLARE_BITMAP(dev_mask, TPM_NUM_DEVICES);
32 static LIST_HEAD(tpm_chip_list);
33 static DEFINE_SPINLOCK(driver_lock);
34
35 /*
36  * tpm_chip_find_get - return tpm_chip for a given chip number
37  * @chip_num the device number for the chip
38  */
39 struct tpm_chip *tpm_chip_find_get(int chip_num)
40 {
41         struct tpm_chip *pos, *chip = NULL;
42
43         rcu_read_lock();
44         list_for_each_entry_rcu(pos, &tpm_chip_list, list) {
45                 if (chip_num != TPM_ANY_NUM && chip_num != pos->dev_num)
46                         continue;
47
48                 if (try_module_get(pos->dev->driver->owner)) {
49                         chip = pos;
50                         break;
51                 }
52         }
53         rcu_read_unlock();
54         return chip;
55 }
56
57 /**
58  * tpmm_chip_remove() - free chip memory and device number
59  * @data: points to struct tpm_chip instance
60  *
61  * This is used internally by tpmm_chip_alloc() and called by devres
62  * when the device is released. This function does the opposite of
63  * tpmm_chip_alloc() freeing memory and the device number.
64  */
65 static void tpmm_chip_remove(void *data)
66 {
67         struct tpm_chip *chip = (struct tpm_chip *) data;
68
69         spin_lock(&driver_lock);
70         clear_bit(chip->dev_num, dev_mask);
71         spin_unlock(&driver_lock);
72         kfree(chip);
73 }
74
75 /**
76  * tpmm_chip_alloc() - allocate a new struct tpm_chip instance
77  * @dev: device to which the chip is associated
78  * @ops: struct tpm_class_ops instance
79  *
80  * Allocates a new struct tpm_chip instance and assigns a free
81  * device number for it. Caller does not have to worry about
82  * freeing the allocated resources. When the devices is removed
83  * devres calls tpmm_chip_remove() to do the job.
84  */
85 struct tpm_chip *tpmm_chip_alloc(struct device *dev,
86                                  const struct tpm_class_ops *ops)
87 {
88         struct tpm_chip *chip;
89
90         chip = kzalloc(sizeof(*chip), GFP_KERNEL);
91         if (chip == NULL)
92                 return ERR_PTR(-ENOMEM);
93
94         mutex_init(&chip->tpm_mutex);
95         INIT_LIST_HEAD(&chip->list);
96
97         chip->ops = ops;
98
99         spin_lock(&driver_lock);
100         chip->dev_num = find_first_zero_bit(dev_mask, TPM_NUM_DEVICES);
101         spin_unlock(&driver_lock);
102
103         if (chip->dev_num >= TPM_NUM_DEVICES) {
104                 dev_err(dev, "No available tpm device numbers\n");
105                 kfree(chip);
106                 return ERR_PTR(-ENOMEM);
107         }
108
109         set_bit(chip->dev_num, dev_mask);
110
111         scnprintf(chip->devname, sizeof(chip->devname), "tpm%d", chip->dev_num);
112
113         chip->dev = dev;
114         devm_add_action(dev, tpmm_chip_remove, chip);
115         dev_set_drvdata(dev, chip);
116
117         return chip;
118 }
119 EXPORT_SYMBOL_GPL(tpmm_chip_alloc);
120
121 /*
122  * tpm_chip_register() - create a misc driver for the TPM chip
123  * @chip: TPM chip to use.
124  *
125  * Creates a misc driver for the TPM chip and adds sysfs interfaces for
126  * the device, PPI and TCPA. As the last step this function adds the
127  * chip to the list of TPM chips available for use.
128  *
129  * NOTE: This function should be only called after the chip initialization
130  * is complete.
131  *
132  * Called from tpm_<specific>.c probe function only for devices
133  * the driver has determined it should claim.  Prior to calling
134  * this function the specific probe function has called pci_enable_device
135  * upon errant exit from this function specific probe function should call
136  * pci_disable_device
137  */
138 int tpm_chip_register(struct tpm_chip *chip)
139 {
140         int rc;
141
142         rc = tpm_dev_add_device(chip);
143         if (rc)
144                 return rc;
145
146         rc = tpm_sysfs_add_device(chip);
147         if (rc)
148                 goto del_misc;
149
150         rc = tpm_add_ppi(chip);
151         if (rc)
152                 goto del_sysfs;
153
154         chip->bios_dir = tpm_bios_log_setup(chip->devname);
155
156         /* Make the chip available. */
157         spin_lock(&driver_lock);
158         list_add_rcu(&chip->list, &tpm_chip_list);
159         spin_unlock(&driver_lock);
160
161         chip->flags |= TPM_CHIP_FLAG_REGISTERED;
162
163         return 0;
164 del_sysfs:
165         tpm_sysfs_del_device(chip);
166 del_misc:
167         tpm_dev_del_device(chip);
168         return rc;
169 }
170 EXPORT_SYMBOL_GPL(tpm_chip_register);
171
172 /*
173  * tpm_chip_unregister() - release the TPM driver
174  * @chip: TPM chip to use.
175  *
176  * Takes the chip first away from the list of available TPM chips and then
177  * cleans up all the resources reserved by tpm_chip_register().
178  *
179  * NOTE: This function should be only called before deinitializing chip
180  * resources.
181  */
182 void tpm_chip_unregister(struct tpm_chip *chip)
183 {
184         if (!(chip->flags & TPM_CHIP_FLAG_REGISTERED))
185                 return;
186
187         spin_lock(&driver_lock);
188         list_del_rcu(&chip->list);
189         spin_unlock(&driver_lock);
190         synchronize_rcu();
191
192         if (chip->bios_dir)
193                 tpm_bios_log_teardown(chip->bios_dir);
194         tpm_remove_ppi(chip);
195         tpm_sysfs_del_device(chip);
196
197         tpm_dev_del_device(chip);
198 }
199 EXPORT_SYMBOL_GPL(tpm_chip_unregister);