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.
10 #include <linux/errno.h>
11 #include <linux/err.h>
12 #include <linux/interrupt.h>
14 #include <xen/events.h>
15 #include <xen/interface/io/tpmif.h>
16 #include <xen/grant_table.h>
17 #include <xen/xenbus.h>
20 #include <xen/platform_pci.h>
23 struct tpm_chip *chip;
24 struct xenbus_device *dev;
26 struct vtpm_shared_page *shr;
32 wait_queue_head_t read_queue;
36 VTPM_STATUS_RUNNING = 0x1,
37 VTPM_STATUS_IDLE = 0x2,
38 VTPM_STATUS_RESULT = 0x4,
39 VTPM_STATUS_CANCELED = 0x8,
42 static u8 vtpm_status(struct tpm_chip *chip)
44 struct tpm_private *priv = dev_get_drvdata(&chip->dev);
45 switch (priv->shr->state) {
47 return VTPM_STATUS_IDLE | VTPM_STATUS_CANCELED;
48 case VTPM_STATE_FINISH:
49 return VTPM_STATUS_IDLE | VTPM_STATUS_RESULT;
50 case VTPM_STATE_SUBMIT:
51 case VTPM_STATE_CANCEL: /* cancel requested, not yet canceled */
52 return VTPM_STATUS_RUNNING;
58 static bool vtpm_req_canceled(struct tpm_chip *chip, u8 status)
60 return status & VTPM_STATUS_CANCELED;
63 static void vtpm_cancel(struct tpm_chip *chip)
65 struct tpm_private *priv = dev_get_drvdata(&chip->dev);
66 priv->shr->state = VTPM_STATE_CANCEL;
68 notify_remote_via_evtchn(priv->evtchn);
71 static unsigned int shr_data_offset(struct vtpm_shared_page *shr)
73 return sizeof(*shr) + sizeof(u32) * shr->nr_extra_pages;
76 static int vtpm_send(struct tpm_chip *chip, u8 *buf, size_t count)
78 struct tpm_private *priv = dev_get_drvdata(&chip->dev);
79 struct vtpm_shared_page *shr = priv->shr;
80 unsigned int offset = shr_data_offset(shr);
83 unsigned long duration;
85 if (offset > PAGE_SIZE)
88 if (offset + count > PAGE_SIZE)
91 /* Wait for completion of any existing command or cancellation */
92 if (wait_for_tpm_stat(chip, VTPM_STATUS_IDLE, chip->timeout_c,
93 &priv->read_queue, true) < 0) {
98 memcpy(offset + (u8 *)shr, buf, count);
101 shr->state = VTPM_STATE_SUBMIT;
103 notify_remote_via_evtchn(priv->evtchn);
105 ordinal = be32_to_cpu(((struct tpm_input_header*)buf)->ordinal);
106 duration = tpm_calc_ordinal_duration(chip, ordinal);
108 if (wait_for_tpm_stat(chip, VTPM_STATUS_IDLE, duration,
109 &priv->read_queue, true) < 0) {
110 /* got a signal or timeout, try to cancel */
118 static int vtpm_recv(struct tpm_chip *chip, u8 *buf, size_t count)
120 struct tpm_private *priv = dev_get_drvdata(&chip->dev);
121 struct vtpm_shared_page *shr = priv->shr;
122 unsigned int offset = shr_data_offset(shr);
123 size_t length = shr->length;
125 if (shr->state == VTPM_STATE_IDLE)
128 /* In theory the wait at the end of _send makes this one unnecessary */
129 if (wait_for_tpm_stat(chip, VTPM_STATUS_RESULT, chip->timeout_c,
130 &priv->read_queue, true) < 0) {
135 if (offset > PAGE_SIZE)
138 if (offset + length > PAGE_SIZE)
139 length = PAGE_SIZE - offset;
144 memcpy(buf, offset + (u8 *)shr, length);
149 static const struct tpm_class_ops tpm_vtpm = {
150 .status = vtpm_status,
153 .cancel = vtpm_cancel,
154 .req_complete_mask = VTPM_STATUS_IDLE | VTPM_STATUS_RESULT,
155 .req_complete_val = VTPM_STATUS_IDLE | VTPM_STATUS_RESULT,
156 .req_canceled = vtpm_req_canceled,
159 static irqreturn_t tpmif_interrupt(int dummy, void *dev_id)
161 struct tpm_private *priv = dev_id;
163 switch (priv->shr->state) {
164 case VTPM_STATE_IDLE:
165 case VTPM_STATE_FINISH:
166 wake_up_interruptible(&priv->read_queue);
168 case VTPM_STATE_SUBMIT:
169 case VTPM_STATE_CANCEL:
176 static int setup_chip(struct device *dev, struct tpm_private *priv)
178 struct tpm_chip *chip;
180 chip = tpmm_chip_alloc(dev, &tpm_vtpm);
182 return PTR_ERR(chip);
184 init_waitqueue_head(&priv->read_queue);
187 dev_set_drvdata(&chip->dev, priv);
192 /* caller must clean up in case of errors */
193 static int setup_ring(struct xenbus_device *dev, struct tpm_private *priv)
195 struct xenbus_transaction xbt;
196 const char *message = NULL;
200 priv->shr = (void *)__get_free_page(GFP_KERNEL|__GFP_ZERO);
202 xenbus_dev_fatal(dev, -ENOMEM, "allocating shared ring");
206 rv = xenbus_grant_ring(dev, priv->shr, 1, &gref);
210 priv->ring_ref = gref;
212 rv = xenbus_alloc_evtchn(dev, &priv->evtchn);
216 rv = bind_evtchn_to_irqhandler(priv->evtchn, tpmif_interrupt, 0,
219 xenbus_dev_fatal(dev, rv, "allocating TPM irq");
225 rv = xenbus_transaction_start(&xbt);
227 xenbus_dev_fatal(dev, rv, "starting transaction");
231 rv = xenbus_printf(xbt, dev->nodename,
232 "ring-ref", "%u", priv->ring_ref);
234 message = "writing ring-ref";
235 goto abort_transaction;
238 rv = xenbus_printf(xbt, dev->nodename, "event-channel", "%u",
241 message = "writing event-channel";
242 goto abort_transaction;
245 rv = xenbus_printf(xbt, dev->nodename, "feature-protocol-v2", "1");
247 message = "writing feature-protocol-v2";
248 goto abort_transaction;
251 rv = xenbus_transaction_end(xbt, 0);
255 xenbus_dev_fatal(dev, rv, "completing transaction");
259 xenbus_switch_state(dev, XenbusStateInitialised);
264 xenbus_transaction_end(xbt, 1);
266 xenbus_dev_error(dev, rv, "%s", message);
271 static void ring_free(struct tpm_private *priv)
277 gnttab_end_foreign_access(priv->ring_ref, 0,
278 (unsigned long)priv->shr);
280 free_page((unsigned long)priv->shr);
283 unbind_from_irqhandler(priv->irq, priv);
288 static int tpmfront_probe(struct xenbus_device *dev,
289 const struct xenbus_device_id *id)
291 struct tpm_private *priv;
294 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
296 xenbus_dev_fatal(dev, -ENOMEM, "allocating priv structure");
300 rv = setup_chip(&dev->dev, priv);
306 rv = setup_ring(dev, priv);
312 tpm_get_timeouts(priv->chip);
314 return tpm_chip_register(priv->chip);
317 static int tpmfront_remove(struct xenbus_device *dev)
319 struct tpm_chip *chip = dev_get_drvdata(&dev->dev);
320 struct tpm_private *priv = dev_get_drvdata(&chip->dev);
321 tpm_chip_unregister(chip);
323 dev_set_drvdata(&chip->dev, NULL);
327 static int tpmfront_resume(struct xenbus_device *dev)
329 /* A suspend/resume/migrate will interrupt a vTPM anyway */
330 tpmfront_remove(dev);
331 return tpmfront_probe(dev, NULL);
334 static void backend_changed(struct xenbus_device *dev,
335 enum xenbus_state backend_state)
337 switch (backend_state) {
338 case XenbusStateInitialised:
339 case XenbusStateConnected:
340 if (dev->state == XenbusStateConnected)
343 if (!xenbus_read_unsigned(dev->otherend, "feature-protocol-v2",
345 xenbus_dev_fatal(dev, -EINVAL,
346 "vTPM protocol 2 required");
349 xenbus_switch_state(dev, XenbusStateConnected);
352 case XenbusStateClosing:
353 case XenbusStateClosed:
354 device_unregister(&dev->dev);
355 xenbus_frontend_closed(dev);
362 static const struct xenbus_device_id tpmfront_ids[] = {
366 MODULE_ALIAS("xen:vtpm");
368 static struct xenbus_driver tpmfront_driver = {
370 .probe = tpmfront_probe,
371 .remove = tpmfront_remove,
372 .resume = tpmfront_resume,
373 .otherend_changed = backend_changed,
376 static int __init xen_tpmfront_init(void)
381 if (!xen_has_pv_devices())
384 return xenbus_register_frontend(&tpmfront_driver);
386 module_init(xen_tpmfront_init);
388 static void __exit xen_tpmfront_exit(void)
390 xenbus_unregister_driver(&tpmfront_driver);
392 module_exit(xen_tpmfront_exit);
394 MODULE_AUTHOR("Daniel De Graaf <dgdegra@tycho.nsa.gov>");
395 MODULE_DESCRIPTION("Xen vTPM Driver");
396 MODULE_LICENSE("GPL");