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
;
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
) {
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
;
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
;
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
);
81 unsigned long duration
;
83 if (offset
> PAGE_SIZE
)
86 if (offset
+ count
> PAGE_SIZE
)
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) {
96 memcpy(offset
+ (u8
*)shr
, buf
, count
);
99 shr
->state
= VTPM_STATE_SUBMIT
;
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 */
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
)
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) {
133 if (offset
> PAGE_SIZE
)
136 if (offset
+ length
> PAGE_SIZE
)
137 length
= PAGE_SIZE
- offset
;
142 memcpy(buf
, offset
+ (u8
*)shr
, length
);
147 static const struct file_operations vtpm_ops
= {
148 .owner
= THIS_MODULE
,
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
,
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
,
171 &dev_attr_enabled
.attr
,
172 &dev_attr_active
.attr
,
173 &dev_attr_owned
.attr
,
174 &dev_attr_temp_deactivated
.attr
,
176 &dev_attr_cancel
.attr
,
177 &dev_attr_durations
.attr
,
178 &dev_attr_timeouts
.attr
,
182 static struct attribute_group vtpm_attr_grp
= {
186 static const struct tpm_vendor_specific tpm_vtpm
= {
187 .status
= vtpm_status
,
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
,
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
);
209 case VTPM_STATE_SUBMIT
:
210 case VTPM_STATE_CANCEL
:
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
);
225 init_waitqueue_head(&chip
->vendor
.read_queue
);
228 TPM_VPRIV(chip
) = priv
;
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
;
240 priv
->shr
= (void *)__get_free_page(GFP_KERNEL
|__GFP_ZERO
);
242 xenbus_dev_fatal(dev
, -ENOMEM
, "allocating shared ring");
246 rv
= xenbus_grant_ring(dev
, virt_to_mfn(priv
->shr
));
252 rv
= xenbus_alloc_evtchn(dev
, &priv
->evtchn
);
256 rv
= bind_evtchn_to_irqhandler(priv
->evtchn
, tpmif_interrupt
, 0,
259 xenbus_dev_fatal(dev
, rv
, "allocating TPM irq");
262 priv
->chip
->vendor
.irq
= rv
;
265 rv
= xenbus_transaction_start(&xbt
);
267 xenbus_dev_fatal(dev
, rv
, "starting transaction");
271 rv
= xenbus_printf(xbt
, dev
->nodename
,
272 "ring-ref", "%u", priv
->ring_ref
);
274 message
= "writing ring-ref";
275 goto abort_transaction
;
278 rv
= xenbus_printf(xbt
, dev
->nodename
, "event-channel", "%u",
281 message
= "writing event-channel";
282 goto abort_transaction
;
285 rv
= xenbus_printf(xbt
, dev
->nodename
, "feature-protocol-v2", "1");
287 message
= "writing feature-protocol-v2";
288 goto abort_transaction
;
291 rv
= xenbus_transaction_end(xbt
, 0);
295 xenbus_dev_fatal(dev
, rv
, "completing transaction");
299 xenbus_switch_state(dev
, XenbusStateInitialised
);
304 xenbus_transaction_end(xbt
, 1);
306 xenbus_dev_error(dev
, rv
, "%s", message
);
311 static void ring_free(struct tpm_private
*priv
)
317 gnttab_end_foreign_access(priv
->ring_ref
, 0,
318 (unsigned long)priv
->shr
);
320 free_page((unsigned long)priv
->shr
);
322 if (priv
->chip
&& priv
->chip
->vendor
.irq
)
323 unbind_from_irqhandler(priv
->chip
->vendor
.irq
, priv
);
328 static int tpmfront_probe(struct xenbus_device
*dev
,
329 const struct xenbus_device_id
*id
)
331 struct tpm_private
*priv
;
334 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
336 xenbus_dev_fatal(dev
, -ENOMEM
, "allocating priv structure");
340 rv
= setup_chip(&dev
->dev
, priv
);
346 rv
= setup_ring(dev
, priv
);
348 tpm_remove_hardware(&dev
->dev
);
353 tpm_get_timeouts(priv
->chip
);
355 dev_set_drvdata(&dev
->dev
, priv
->chip
);
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
);
366 TPM_VPRIV(chip
) = NULL
;
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
)
382 switch (backend_state
) {
383 case XenbusStateInitialised
:
384 case XenbusStateConnected
:
385 if (dev
->state
== XenbusStateConnected
)
388 if (xenbus_scanf(XBT_NIL
, dev
->otherend
,
389 "feature-protocol-v2", "%d", &val
) < 0)
392 xenbus_dev_fatal(dev
, -EINVAL
,
393 "vTPM protocol 2 required");
396 xenbus_switch_state(dev
, XenbusStateConnected
);
399 case XenbusStateClosing
:
400 case XenbusStateClosed
:
401 device_unregister(&dev
->dev
);
402 xenbus_frontend_closed(dev
);
409 static const struct xenbus_device_id tpmfront_ids
[] = {
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)
427 if (!xen_has_pv_devices())
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");