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>
22 struct tpm_chip *chip;
23 struct xenbus_device *dev;
25 struct vtpm_shared_page *shr;
33 VTPM_STATUS_RUNNING = 0x1,
34 VTPM_STATUS_IDLE = 0x2,
35 VTPM_STATUS_RESULT = 0x4,
36 VTPM_STATUS_CANCELED = 0x8,
39 static u8 vtpm_status(struct tpm_chip *chip)
41 struct tpm_private *priv = TPM_VPRIV(chip);
42 switch (priv->shr->state) {
44 return VTPM_STATUS_IDLE | VTPM_STATUS_CANCELED;
45 case VTPM_STATE_FINISH:
46 return VTPM_STATUS_IDLE | VTPM_STATUS_RESULT;
47 case VTPM_STATE_SUBMIT:
48 case VTPM_STATE_CANCEL: /* cancel requested, not yet canceled */
49 return VTPM_STATUS_RUNNING;
55 static bool vtpm_req_canceled(struct tpm_chip *chip, u8 status)
57 return status & VTPM_STATUS_CANCELED;
60 static void vtpm_cancel(struct tpm_chip *chip)
62 struct tpm_private *priv = TPM_VPRIV(chip);
63 priv->shr->state = VTPM_STATE_CANCEL;
65 notify_remote_via_evtchn(priv->evtchn);
68 static unsigned int shr_data_offset(struct vtpm_shared_page *shr)
70 return sizeof(*shr) + sizeof(u32) * shr->nr_extra_pages;
73 static int vtpm_send(struct tpm_chip *chip, u8 *buf, size_t count)
75 struct tpm_private *priv = TPM_VPRIV(chip);
76 struct vtpm_shared_page *shr = priv->shr;
77 unsigned int offset = shr_data_offset(shr);
80 unsigned long duration;
82 if (offset > PAGE_SIZE)
85 if (offset + count > PAGE_SIZE)
88 /* Wait for completion of any existing command or cancellation */
89 if (wait_for_tpm_stat(chip, VTPM_STATUS_IDLE, chip->vendor.timeout_c,
90 &chip->vendor.read_queue, true) < 0) {
95 memcpy(offset + (u8 *)shr, buf, count);
98 shr->state = VTPM_STATE_SUBMIT;
100 notify_remote_via_evtchn(priv->evtchn);
102 ordinal = be32_to_cpu(((struct tpm_input_header*)buf)->ordinal);
103 duration = tpm_calc_ordinal_duration(chip, ordinal);
105 if (wait_for_tpm_stat(chip, VTPM_STATUS_IDLE, duration,
106 &chip->vendor.read_queue, true) < 0) {
107 /* got a signal or timeout, try to cancel */
115 static int vtpm_recv(struct tpm_chip *chip, u8 *buf, size_t count)
117 struct tpm_private *priv = TPM_VPRIV(chip);
118 struct vtpm_shared_page *shr = priv->shr;
119 unsigned int offset = shr_data_offset(shr);
120 size_t length = shr->length;
122 if (shr->state == VTPM_STATE_IDLE)
125 /* In theory the wait at the end of _send makes this one unnecessary */
126 if (wait_for_tpm_stat(chip, VTPM_STATUS_RESULT, chip->vendor.timeout_c,
127 &chip->vendor.read_queue, true) < 0) {
132 if (offset > PAGE_SIZE)
135 if (offset + length > PAGE_SIZE)
136 length = PAGE_SIZE - offset;
141 memcpy(buf, offset + (u8 *)shr, length);
146 static const struct tpm_vendor_specific tpm_vtpm = {
147 .status = vtpm_status,
150 .cancel = vtpm_cancel,
151 .req_complete_mask = VTPM_STATUS_IDLE | VTPM_STATUS_RESULT,
152 .req_complete_val = VTPM_STATUS_IDLE | VTPM_STATUS_RESULT,
153 .req_canceled = vtpm_req_canceled,
156 static irqreturn_t tpmif_interrupt(int dummy, void *dev_id)
158 struct tpm_private *priv = dev_id;
160 switch (priv->shr->state) {
161 case VTPM_STATE_IDLE:
162 case VTPM_STATE_FINISH:
163 wake_up_interruptible(&priv->chip->vendor.read_queue);
165 case VTPM_STATE_SUBMIT:
166 case VTPM_STATE_CANCEL:
173 static int setup_chip(struct device *dev, struct tpm_private *priv)
175 struct tpm_chip *chip;
177 chip = tpm_register_hardware(dev, &tpm_vtpm);
181 init_waitqueue_head(&chip->vendor.read_queue);
184 TPM_VPRIV(chip) = priv;
189 /* caller must clean up in case of errors */
190 static int setup_ring(struct xenbus_device *dev, struct tpm_private *priv)
192 struct xenbus_transaction xbt;
193 const char *message = NULL;
196 priv->shr = (void *)__get_free_page(GFP_KERNEL|__GFP_ZERO);
198 xenbus_dev_fatal(dev, -ENOMEM, "allocating shared ring");
202 rv = xenbus_grant_ring(dev, virt_to_mfn(priv->shr));
208 rv = xenbus_alloc_evtchn(dev, &priv->evtchn);
212 rv = bind_evtchn_to_irqhandler(priv->evtchn, tpmif_interrupt, 0,
215 xenbus_dev_fatal(dev, rv, "allocating TPM irq");
218 priv->chip->vendor.irq = rv;
221 rv = xenbus_transaction_start(&xbt);
223 xenbus_dev_fatal(dev, rv, "starting transaction");
227 rv = xenbus_printf(xbt, dev->nodename,
228 "ring-ref", "%u", priv->ring_ref);
230 message = "writing ring-ref";
231 goto abort_transaction;
234 rv = xenbus_printf(xbt, dev->nodename, "event-channel", "%u",
237 message = "writing event-channel";
238 goto abort_transaction;
241 rv = xenbus_printf(xbt, dev->nodename, "feature-protocol-v2", "1");
243 message = "writing feature-protocol-v2";
244 goto abort_transaction;
247 rv = xenbus_transaction_end(xbt, 0);
251 xenbus_dev_fatal(dev, rv, "completing transaction");
255 xenbus_switch_state(dev, XenbusStateInitialised);
260 xenbus_transaction_end(xbt, 1);
262 xenbus_dev_error(dev, rv, "%s", message);
267 static void ring_free(struct tpm_private *priv)
273 gnttab_end_foreign_access(priv->ring_ref, 0,
274 (unsigned long)priv->shr);
276 free_page((unsigned long)priv->shr);
278 if (priv->chip && priv->chip->vendor.irq)
279 unbind_from_irqhandler(priv->chip->vendor.irq, priv);
284 static int tpmfront_probe(struct xenbus_device *dev,
285 const struct xenbus_device_id *id)
287 struct tpm_private *priv;
290 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
292 xenbus_dev_fatal(dev, -ENOMEM, "allocating priv structure");
296 rv = setup_chip(&dev->dev, priv);
302 rv = setup_ring(dev, priv);
304 tpm_remove_hardware(&dev->dev);
309 tpm_get_timeouts(priv->chip);
314 static int tpmfront_remove(struct xenbus_device *dev)
316 struct tpm_chip *chip = dev_get_drvdata(&dev->dev);
317 struct tpm_private *priv = TPM_VPRIV(chip);
318 tpm_remove_hardware(&dev->dev);
320 TPM_VPRIV(chip) = NULL;
324 static int tpmfront_resume(struct xenbus_device *dev)
326 /* A suspend/resume/migrate will interrupt a vTPM anyway */
327 tpmfront_remove(dev);
328 return tpmfront_probe(dev, NULL);
331 static void backend_changed(struct xenbus_device *dev,
332 enum xenbus_state backend_state)
336 switch (backend_state) {
337 case XenbusStateInitialised:
338 case XenbusStateConnected:
339 if (dev->state == XenbusStateConnected)
342 if (xenbus_scanf(XBT_NIL, dev->otherend,
343 "feature-protocol-v2", "%d", &val) < 0)
346 xenbus_dev_fatal(dev, -EINVAL,
347 "vTPM protocol 2 required");
350 xenbus_switch_state(dev, XenbusStateConnected);
353 case XenbusStateClosing:
354 case XenbusStateClosed:
355 device_unregister(&dev->dev);
356 xenbus_frontend_closed(dev);
363 static const struct xenbus_device_id tpmfront_ids[] = {
367 MODULE_ALIAS("xen:vtpm");
369 static DEFINE_XENBUS_DRIVER(tpmfront, ,
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 return xenbus_register_frontend(&tpmfront_driver);
383 module_init(xen_tpmfront_init);
385 static void __exit xen_tpmfront_exit(void)
387 xenbus_unregister_driver(&tpmfront_driver);
389 module_exit(xen_tpmfront_exit);
391 MODULE_AUTHOR("Daniel De Graaf <dgdegra@tycho.nsa.gov>");
392 MODULE_DESCRIPTION("Xen vTPM Driver");
393 MODULE_LICENSE("GPL");