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 file_operations vtpm_ops = {
147 .owner = THIS_MODULE,
152 .release = tpm_release,
155 static DEVICE_ATTR(pubek, S_IRUGO, tpm_show_pubek, NULL);
156 static DEVICE_ATTR(pcrs, S_IRUGO, tpm_show_pcrs, NULL);
157 static DEVICE_ATTR(enabled, S_IRUGO, tpm_show_enabled, NULL);
158 static DEVICE_ATTR(active, S_IRUGO, tpm_show_active, NULL);
159 static DEVICE_ATTR(owned, S_IRUGO, tpm_show_owned, NULL);
160 static DEVICE_ATTR(temp_deactivated, S_IRUGO, tpm_show_temp_deactivated,
162 static DEVICE_ATTR(caps, S_IRUGO, tpm_show_caps, NULL);
163 static DEVICE_ATTR(cancel, S_IWUSR | S_IWGRP, NULL, tpm_store_cancel);
164 static DEVICE_ATTR(durations, S_IRUGO, tpm_show_durations, NULL);
165 static DEVICE_ATTR(timeouts, S_IRUGO, tpm_show_timeouts, NULL);
167 static struct attribute *vtpm_attrs[] = {
168 &dev_attr_pubek.attr,
170 &dev_attr_enabled.attr,
171 &dev_attr_active.attr,
172 &dev_attr_owned.attr,
173 &dev_attr_temp_deactivated.attr,
175 &dev_attr_cancel.attr,
176 &dev_attr_durations.attr,
177 &dev_attr_timeouts.attr,
181 static struct attribute_group vtpm_attr_grp = {
185 static const struct tpm_vendor_specific tpm_vtpm = {
186 .status = vtpm_status,
189 .cancel = vtpm_cancel,
190 .req_complete_mask = VTPM_STATUS_IDLE | VTPM_STATUS_RESULT,
191 .req_complete_val = VTPM_STATUS_IDLE | VTPM_STATUS_RESULT,
192 .req_canceled = vtpm_req_canceled,
193 .attr_group = &vtpm_attr_grp,
199 static irqreturn_t tpmif_interrupt(int dummy, void *dev_id)
201 struct tpm_private *priv = dev_id;
203 switch (priv->shr->state) {
204 case VTPM_STATE_IDLE:
205 case VTPM_STATE_FINISH:
206 wake_up_interruptible(&priv->chip->vendor.read_queue);
208 case VTPM_STATE_SUBMIT:
209 case VTPM_STATE_CANCEL:
216 static int setup_chip(struct device *dev, struct tpm_private *priv)
218 struct tpm_chip *chip;
220 chip = tpm_register_hardware(dev, &tpm_vtpm);
224 init_waitqueue_head(&chip->vendor.read_queue);
227 TPM_VPRIV(chip) = priv;
232 /* caller must clean up in case of errors */
233 static int setup_ring(struct xenbus_device *dev, struct tpm_private *priv)
235 struct xenbus_transaction xbt;
236 const char *message = NULL;
239 priv->shr = (void *)__get_free_page(GFP_KERNEL|__GFP_ZERO);
241 xenbus_dev_fatal(dev, -ENOMEM, "allocating shared ring");
245 rv = xenbus_grant_ring(dev, virt_to_mfn(priv->shr));
251 rv = xenbus_alloc_evtchn(dev, &priv->evtchn);
255 rv = bind_evtchn_to_irqhandler(priv->evtchn, tpmif_interrupt, 0,
258 xenbus_dev_fatal(dev, rv, "allocating TPM irq");
261 priv->chip->vendor.irq = rv;
264 rv = xenbus_transaction_start(&xbt);
266 xenbus_dev_fatal(dev, rv, "starting transaction");
270 rv = xenbus_printf(xbt, dev->nodename,
271 "ring-ref", "%u", priv->ring_ref);
273 message = "writing ring-ref";
274 goto abort_transaction;
277 rv = xenbus_printf(xbt, dev->nodename, "event-channel", "%u",
280 message = "writing event-channel";
281 goto abort_transaction;
284 rv = xenbus_printf(xbt, dev->nodename, "feature-protocol-v2", "1");
286 message = "writing feature-protocol-v2";
287 goto abort_transaction;
290 rv = xenbus_transaction_end(xbt, 0);
294 xenbus_dev_fatal(dev, rv, "completing transaction");
298 xenbus_switch_state(dev, XenbusStateInitialised);
303 xenbus_transaction_end(xbt, 1);
305 xenbus_dev_error(dev, rv, "%s", message);
310 static void ring_free(struct tpm_private *priv)
316 gnttab_end_foreign_access(priv->ring_ref, 0,
317 (unsigned long)priv->shr);
319 free_page((unsigned long)priv->shr);
321 if (priv->chip && priv->chip->vendor.irq)
322 unbind_from_irqhandler(priv->chip->vendor.irq, priv);
327 static int tpmfront_probe(struct xenbus_device *dev,
328 const struct xenbus_device_id *id)
330 struct tpm_private *priv;
333 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
335 xenbus_dev_fatal(dev, -ENOMEM, "allocating priv structure");
339 rv = setup_chip(&dev->dev, priv);
345 rv = setup_ring(dev, priv);
347 tpm_remove_hardware(&dev->dev);
352 tpm_get_timeouts(priv->chip);
354 dev_set_drvdata(&dev->dev, priv->chip);
359 static int tpmfront_remove(struct xenbus_device *dev)
361 struct tpm_chip *chip = dev_get_drvdata(&dev->dev);
362 struct tpm_private *priv = TPM_VPRIV(chip);
363 tpm_remove_hardware(&dev->dev);
365 TPM_VPRIV(chip) = NULL;
369 static int tpmfront_resume(struct xenbus_device *dev)
371 /* A suspend/resume/migrate will interrupt a vTPM anyway */
372 tpmfront_remove(dev);
373 return tpmfront_probe(dev, NULL);
376 static void backend_changed(struct xenbus_device *dev,
377 enum xenbus_state backend_state)
381 switch (backend_state) {
382 case XenbusStateInitialised:
383 case XenbusStateConnected:
384 if (dev->state == XenbusStateConnected)
387 if (xenbus_scanf(XBT_NIL, dev->otherend,
388 "feature-protocol-v2", "%d", &val) < 0)
391 xenbus_dev_fatal(dev, -EINVAL,
392 "vTPM protocol 2 required");
395 xenbus_switch_state(dev, XenbusStateConnected);
398 case XenbusStateClosing:
399 case XenbusStateClosed:
400 device_unregister(&dev->dev);
401 xenbus_frontend_closed(dev);
408 static const struct xenbus_device_id tpmfront_ids[] = {
412 MODULE_ALIAS("xen:vtpm");
414 static DEFINE_XENBUS_DRIVER(tpmfront, ,
415 .probe = tpmfront_probe,
416 .remove = tpmfront_remove,
417 .resume = tpmfront_resume,
418 .otherend_changed = backend_changed,
421 static int __init xen_tpmfront_init(void)
426 return xenbus_register_frontend(&tpmfront_driver);
428 module_init(xen_tpmfront_init);
430 static void __exit xen_tpmfront_exit(void)
432 xenbus_unregister_driver(&tpmfront_driver);
434 module_exit(xen_tpmfront_exit);
436 MODULE_AUTHOR("Daniel De Graaf <dgdegra@tycho.nsa.gov>");
437 MODULE_DESCRIPTION("Xen vTPM Driver");
438 MODULE_LICENSE("GPL");