mfd: wm8350-i2c: Make sure the i2c regmap functions are compiled
[linux/fpc-iii.git] / drivers / char / tpm / xen-tpmfront.c
blobafa9362f4f4d35a5e1f8aa7506771a479dbeea12
1 /*
2 * Implementation of the Xen vTPM device frontend
4 * Author: Daniel De Graaf <dgdegra@tycho.nsa.gov>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2,
8 * as published by the Free Software Foundation.
9 */
10 #include <linux/errno.h>
11 #include <linux/err.h>
12 #include <linux/interrupt.h>
13 #include <xen/xen.h>
14 #include <xen/events.h>
15 #include <xen/interface/io/tpmif.h>
16 #include <xen/grant_table.h>
17 #include <xen/xenbus.h>
18 #include <xen/page.h>
19 #include "tpm.h"
20 #include <xen/platform_pci.h>
22 struct tpm_private {
23 struct tpm_chip *chip;
24 struct xenbus_device *dev;
26 struct vtpm_shared_page *shr;
28 unsigned int evtchn;
29 int ring_ref;
30 domid_t backend_id;
33 enum status_bits {
34 VTPM_STATUS_RUNNING = 0x1,
35 VTPM_STATUS_IDLE = 0x2,
36 VTPM_STATUS_RESULT = 0x4,
37 VTPM_STATUS_CANCELED = 0x8,
40 static u8 vtpm_status(struct tpm_chip *chip)
42 struct tpm_private *priv = TPM_VPRIV(chip);
43 switch (priv->shr->state) {
44 case VTPM_STATE_IDLE:
45 return VTPM_STATUS_IDLE | VTPM_STATUS_CANCELED;
46 case VTPM_STATE_FINISH:
47 return VTPM_STATUS_IDLE | VTPM_STATUS_RESULT;
48 case VTPM_STATE_SUBMIT:
49 case VTPM_STATE_CANCEL: /* cancel requested, not yet canceled */
50 return VTPM_STATUS_RUNNING;
51 default:
52 return 0;
56 static bool vtpm_req_canceled(struct tpm_chip *chip, u8 status)
58 return status & VTPM_STATUS_CANCELED;
61 static void vtpm_cancel(struct tpm_chip *chip)
63 struct tpm_private *priv = TPM_VPRIV(chip);
64 priv->shr->state = VTPM_STATE_CANCEL;
65 wmb();
66 notify_remote_via_evtchn(priv->evtchn);
69 static unsigned int shr_data_offset(struct vtpm_shared_page *shr)
71 return sizeof(*shr) + sizeof(u32) * shr->nr_extra_pages;
74 static int vtpm_send(struct tpm_chip *chip, u8 *buf, size_t count)
76 struct tpm_private *priv = TPM_VPRIV(chip);
77 struct vtpm_shared_page *shr = priv->shr;
78 unsigned int offset = shr_data_offset(shr);
80 u32 ordinal;
81 unsigned long duration;
83 if (offset > PAGE_SIZE)
84 return -EINVAL;
86 if (offset + count > PAGE_SIZE)
87 return -EINVAL;
89 /* Wait for completion of any existing command or cancellation */
90 if (wait_for_tpm_stat(chip, VTPM_STATUS_IDLE, chip->vendor.timeout_c,
91 &chip->vendor.read_queue, true) < 0) {
92 vtpm_cancel(chip);
93 return -ETIME;
96 memcpy(offset + (u8 *)shr, buf, count);
97 shr->length = count;
98 barrier();
99 shr->state = VTPM_STATE_SUBMIT;
100 wmb();
101 notify_remote_via_evtchn(priv->evtchn);
103 ordinal = be32_to_cpu(((struct tpm_input_header*)buf)->ordinal);
104 duration = tpm_calc_ordinal_duration(chip, ordinal);
106 if (wait_for_tpm_stat(chip, VTPM_STATUS_IDLE, duration,
107 &chip->vendor.read_queue, true) < 0) {
108 /* got a signal or timeout, try to cancel */
109 vtpm_cancel(chip);
110 return -ETIME;
113 return count;
116 static int vtpm_recv(struct tpm_chip *chip, u8 *buf, size_t count)
118 struct tpm_private *priv = TPM_VPRIV(chip);
119 struct vtpm_shared_page *shr = priv->shr;
120 unsigned int offset = shr_data_offset(shr);
121 size_t length = shr->length;
123 if (shr->state == VTPM_STATE_IDLE)
124 return -ECANCELED;
126 /* In theory the wait at the end of _send makes this one unnecessary */
127 if (wait_for_tpm_stat(chip, VTPM_STATUS_RESULT, chip->vendor.timeout_c,
128 &chip->vendor.read_queue, true) < 0) {
129 vtpm_cancel(chip);
130 return -ETIME;
133 if (offset > PAGE_SIZE)
134 return -EIO;
136 if (offset + length > PAGE_SIZE)
137 length = PAGE_SIZE - offset;
139 if (length > count)
140 length = count;
142 memcpy(buf, offset + (u8 *)shr, length);
144 return length;
147 static const struct file_operations vtpm_ops = {
148 .owner = THIS_MODULE,
149 .llseek = no_llseek,
150 .open = tpm_open,
151 .read = tpm_read,
152 .write = tpm_write,
153 .release = tpm_release,
156 static DEVICE_ATTR(pubek, S_IRUGO, tpm_show_pubek, NULL);
157 static DEVICE_ATTR(pcrs, S_IRUGO, tpm_show_pcrs, NULL);
158 static DEVICE_ATTR(enabled, S_IRUGO, tpm_show_enabled, NULL);
159 static DEVICE_ATTR(active, S_IRUGO, tpm_show_active, NULL);
160 static DEVICE_ATTR(owned, S_IRUGO, tpm_show_owned, NULL);
161 static DEVICE_ATTR(temp_deactivated, S_IRUGO, tpm_show_temp_deactivated,
162 NULL);
163 static DEVICE_ATTR(caps, S_IRUGO, tpm_show_caps, NULL);
164 static DEVICE_ATTR(cancel, S_IWUSR | S_IWGRP, NULL, tpm_store_cancel);
165 static DEVICE_ATTR(durations, S_IRUGO, tpm_show_durations, NULL);
166 static DEVICE_ATTR(timeouts, S_IRUGO, tpm_show_timeouts, NULL);
168 static struct attribute *vtpm_attrs[] = {
169 &dev_attr_pubek.attr,
170 &dev_attr_pcrs.attr,
171 &dev_attr_enabled.attr,
172 &dev_attr_active.attr,
173 &dev_attr_owned.attr,
174 &dev_attr_temp_deactivated.attr,
175 &dev_attr_caps.attr,
176 &dev_attr_cancel.attr,
177 &dev_attr_durations.attr,
178 &dev_attr_timeouts.attr,
179 NULL,
182 static struct attribute_group vtpm_attr_grp = {
183 .attrs = vtpm_attrs,
186 static const struct tpm_vendor_specific tpm_vtpm = {
187 .status = vtpm_status,
188 .recv = vtpm_recv,
189 .send = vtpm_send,
190 .cancel = vtpm_cancel,
191 .req_complete_mask = VTPM_STATUS_IDLE | VTPM_STATUS_RESULT,
192 .req_complete_val = VTPM_STATUS_IDLE | VTPM_STATUS_RESULT,
193 .req_canceled = vtpm_req_canceled,
194 .attr_group = &vtpm_attr_grp,
195 .miscdev = {
196 .fops = &vtpm_ops,
200 static irqreturn_t tpmif_interrupt(int dummy, void *dev_id)
202 struct tpm_private *priv = dev_id;
204 switch (priv->shr->state) {
205 case VTPM_STATE_IDLE:
206 case VTPM_STATE_FINISH:
207 wake_up_interruptible(&priv->chip->vendor.read_queue);
208 break;
209 case VTPM_STATE_SUBMIT:
210 case VTPM_STATE_CANCEL:
211 default:
212 break;
214 return IRQ_HANDLED;
217 static int setup_chip(struct device *dev, struct tpm_private *priv)
219 struct tpm_chip *chip;
221 chip = tpm_register_hardware(dev, &tpm_vtpm);
222 if (!chip)
223 return -ENODEV;
225 init_waitqueue_head(&chip->vendor.read_queue);
227 priv->chip = chip;
228 TPM_VPRIV(chip) = priv;
230 return 0;
233 /* caller must clean up in case of errors */
234 static int setup_ring(struct xenbus_device *dev, struct tpm_private *priv)
236 struct xenbus_transaction xbt;
237 const char *message = NULL;
238 int rv;
240 priv->shr = (void *)__get_free_page(GFP_KERNEL|__GFP_ZERO);
241 if (!priv->shr) {
242 xenbus_dev_fatal(dev, -ENOMEM, "allocating shared ring");
243 return -ENOMEM;
246 rv = xenbus_grant_ring(dev, virt_to_mfn(priv->shr));
247 if (rv < 0)
248 return rv;
250 priv->ring_ref = rv;
252 rv = xenbus_alloc_evtchn(dev, &priv->evtchn);
253 if (rv)
254 return rv;
256 rv = bind_evtchn_to_irqhandler(priv->evtchn, tpmif_interrupt, 0,
257 "tpmif", priv);
258 if (rv <= 0) {
259 xenbus_dev_fatal(dev, rv, "allocating TPM irq");
260 return rv;
262 priv->chip->vendor.irq = rv;
264 again:
265 rv = xenbus_transaction_start(&xbt);
266 if (rv) {
267 xenbus_dev_fatal(dev, rv, "starting transaction");
268 return rv;
271 rv = xenbus_printf(xbt, dev->nodename,
272 "ring-ref", "%u", priv->ring_ref);
273 if (rv) {
274 message = "writing ring-ref";
275 goto abort_transaction;
278 rv = xenbus_printf(xbt, dev->nodename, "event-channel", "%u",
279 priv->evtchn);
280 if (rv) {
281 message = "writing event-channel";
282 goto abort_transaction;
285 rv = xenbus_printf(xbt, dev->nodename, "feature-protocol-v2", "1");
286 if (rv) {
287 message = "writing feature-protocol-v2";
288 goto abort_transaction;
291 rv = xenbus_transaction_end(xbt, 0);
292 if (rv == -EAGAIN)
293 goto again;
294 if (rv) {
295 xenbus_dev_fatal(dev, rv, "completing transaction");
296 return rv;
299 xenbus_switch_state(dev, XenbusStateInitialised);
301 return 0;
303 abort_transaction:
304 xenbus_transaction_end(xbt, 1);
305 if (message)
306 xenbus_dev_error(dev, rv, "%s", message);
308 return rv;
311 static void ring_free(struct tpm_private *priv)
313 if (!priv)
314 return;
316 if (priv->ring_ref)
317 gnttab_end_foreign_access(priv->ring_ref, 0,
318 (unsigned long)priv->shr);
319 else
320 free_page((unsigned long)priv->shr);
322 if (priv->chip && priv->chip->vendor.irq)
323 unbind_from_irqhandler(priv->chip->vendor.irq, priv);
325 kfree(priv);
328 static int tpmfront_probe(struct xenbus_device *dev,
329 const struct xenbus_device_id *id)
331 struct tpm_private *priv;
332 int rv;
334 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
335 if (!priv) {
336 xenbus_dev_fatal(dev, -ENOMEM, "allocating priv structure");
337 return -ENOMEM;
340 rv = setup_chip(&dev->dev, priv);
341 if (rv) {
342 kfree(priv);
343 return rv;
346 rv = setup_ring(dev, priv);
347 if (rv) {
348 tpm_remove_hardware(&dev->dev);
349 ring_free(priv);
350 return rv;
353 tpm_get_timeouts(priv->chip);
355 dev_set_drvdata(&dev->dev, priv->chip);
357 return rv;
360 static int tpmfront_remove(struct xenbus_device *dev)
362 struct tpm_chip *chip = dev_get_drvdata(&dev->dev);
363 struct tpm_private *priv = TPM_VPRIV(chip);
364 tpm_remove_hardware(&dev->dev);
365 ring_free(priv);
366 TPM_VPRIV(chip) = NULL;
367 return 0;
370 static int tpmfront_resume(struct xenbus_device *dev)
372 /* A suspend/resume/migrate will interrupt a vTPM anyway */
373 tpmfront_remove(dev);
374 return tpmfront_probe(dev, NULL);
377 static void backend_changed(struct xenbus_device *dev,
378 enum xenbus_state backend_state)
380 int val;
382 switch (backend_state) {
383 case XenbusStateInitialised:
384 case XenbusStateConnected:
385 if (dev->state == XenbusStateConnected)
386 break;
388 if (xenbus_scanf(XBT_NIL, dev->otherend,
389 "feature-protocol-v2", "%d", &val) < 0)
390 val = 0;
391 if (!val) {
392 xenbus_dev_fatal(dev, -EINVAL,
393 "vTPM protocol 2 required");
394 return;
396 xenbus_switch_state(dev, XenbusStateConnected);
397 break;
399 case XenbusStateClosing:
400 case XenbusStateClosed:
401 device_unregister(&dev->dev);
402 xenbus_frontend_closed(dev);
403 break;
404 default:
405 break;
409 static const struct xenbus_device_id tpmfront_ids[] = {
410 { "vtpm" },
411 { "" }
413 MODULE_ALIAS("xen:vtpm");
415 static DEFINE_XENBUS_DRIVER(tpmfront, ,
416 .probe = tpmfront_probe,
417 .remove = tpmfront_remove,
418 .resume = tpmfront_resume,
419 .otherend_changed = backend_changed,
422 static int __init xen_tpmfront_init(void)
424 if (!xen_domain())
425 return -ENODEV;
427 if (!xen_has_pv_devices())
428 return -ENODEV;
430 return xenbus_register_frontend(&tpmfront_driver);
432 module_init(xen_tpmfront_init);
434 static void __exit xen_tpmfront_exit(void)
436 xenbus_unregister_driver(&tpmfront_driver);
438 module_exit(xen_tpmfront_exit);
440 MODULE_AUTHOR("Daniel De Graaf <dgdegra@tycho.nsa.gov>");
441 MODULE_DESCRIPTION("Xen vTPM Driver");
442 MODULE_LICENSE("GPL");