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>
13 #include <linux/freezer.h>
15 #include <xen/events.h>
16 #include <xen/interface/io/tpmif.h>
17 #include <xen/grant_table.h>
18 #include <xen/xenbus.h>
21 #include <xen/platform_pci.h>
24 struct tpm_chip
*chip
;
25 struct xenbus_device
*dev
;
27 struct vtpm_shared_page
*shr
;
33 wait_queue_head_t read_queue
;
37 VTPM_STATUS_RUNNING
= 0x1,
38 VTPM_STATUS_IDLE
= 0x2,
39 VTPM_STATUS_RESULT
= 0x4,
40 VTPM_STATUS_CANCELED
= 0x8,
43 static bool wait_for_tpm_stat_cond(struct tpm_chip
*chip
, u8 mask
,
44 bool check_cancel
, bool *canceled
)
46 u8 status
= chip
->ops
->status(chip
);
49 if ((status
& mask
) == mask
)
51 if (check_cancel
&& chip
->ops
->req_canceled(chip
, status
)) {
58 static int wait_for_tpm_stat(struct tpm_chip
*chip
, u8 mask
,
59 unsigned long timeout
, wait_queue_head_t
*queue
,
65 bool canceled
= false;
67 /* check current status */
68 status
= chip
->ops
->status(chip
);
69 if ((status
& mask
) == mask
)
72 stop
= jiffies
+ timeout
;
74 if (chip
->flags
& TPM_CHIP_FLAG_IRQ
) {
76 timeout
= stop
- jiffies
;
77 if ((long)timeout
<= 0)
79 rc
= wait_event_interruptible_timeout(*queue
,
80 wait_for_tpm_stat_cond(chip
, mask
, check_cancel
,
88 if (rc
== -ERESTARTSYS
&& freezing(current
)) {
89 clear_thread_flag(TIF_SIGPENDING
);
94 tpm_msleep(TPM_TIMEOUT
);
95 status
= chip
->ops
->status(chip
);
96 if ((status
& mask
) == mask
)
98 } while (time_before(jiffies
, stop
));
103 static u8
vtpm_status(struct tpm_chip
*chip
)
105 struct tpm_private
*priv
= dev_get_drvdata(&chip
->dev
);
106 switch (priv
->shr
->state
) {
107 case VTPM_STATE_IDLE
:
108 return VTPM_STATUS_IDLE
| VTPM_STATUS_CANCELED
;
109 case VTPM_STATE_FINISH
:
110 return VTPM_STATUS_IDLE
| VTPM_STATUS_RESULT
;
111 case VTPM_STATE_SUBMIT
:
112 case VTPM_STATE_CANCEL
: /* cancel requested, not yet canceled */
113 return VTPM_STATUS_RUNNING
;
119 static bool vtpm_req_canceled(struct tpm_chip
*chip
, u8 status
)
121 return status
& VTPM_STATUS_CANCELED
;
124 static void vtpm_cancel(struct tpm_chip
*chip
)
126 struct tpm_private
*priv
= dev_get_drvdata(&chip
->dev
);
127 priv
->shr
->state
= VTPM_STATE_CANCEL
;
129 notify_remote_via_evtchn(priv
->evtchn
);
132 static unsigned int shr_data_offset(struct vtpm_shared_page
*shr
)
134 return sizeof(*shr
) + sizeof(u32
) * shr
->nr_extra_pages
;
137 static int vtpm_send(struct tpm_chip
*chip
, u8
*buf
, size_t count
)
139 struct tpm_private
*priv
= dev_get_drvdata(&chip
->dev
);
140 struct vtpm_shared_page
*shr
= priv
->shr
;
141 unsigned int offset
= shr_data_offset(shr
);
144 unsigned long duration
;
146 if (offset
> PAGE_SIZE
)
149 if (offset
+ count
> PAGE_SIZE
)
152 /* Wait for completion of any existing command or cancellation */
153 if (wait_for_tpm_stat(chip
, VTPM_STATUS_IDLE
, chip
->timeout_c
,
154 &priv
->read_queue
, true) < 0) {
159 memcpy(offset
+ (u8
*)shr
, buf
, count
);
162 shr
->state
= VTPM_STATE_SUBMIT
;
164 notify_remote_via_evtchn(priv
->evtchn
);
166 ordinal
= be32_to_cpu(((struct tpm_input_header
*)buf
)->ordinal
);
167 duration
= tpm_calc_ordinal_duration(chip
, ordinal
);
169 if (wait_for_tpm_stat(chip
, VTPM_STATUS_IDLE
, duration
,
170 &priv
->read_queue
, true) < 0) {
171 /* got a signal or timeout, try to cancel */
179 static int vtpm_recv(struct tpm_chip
*chip
, u8
*buf
, size_t count
)
181 struct tpm_private
*priv
= dev_get_drvdata(&chip
->dev
);
182 struct vtpm_shared_page
*shr
= priv
->shr
;
183 unsigned int offset
= shr_data_offset(shr
);
184 size_t length
= shr
->length
;
186 if (shr
->state
== VTPM_STATE_IDLE
)
189 /* In theory the wait at the end of _send makes this one unnecessary */
190 if (wait_for_tpm_stat(chip
, VTPM_STATUS_RESULT
, chip
->timeout_c
,
191 &priv
->read_queue
, true) < 0) {
196 if (offset
> PAGE_SIZE
)
199 if (offset
+ length
> PAGE_SIZE
)
200 length
= PAGE_SIZE
- offset
;
205 memcpy(buf
, offset
+ (u8
*)shr
, length
);
210 static const struct tpm_class_ops tpm_vtpm
= {
211 .status
= vtpm_status
,
214 .cancel
= vtpm_cancel
,
215 .req_complete_mask
= VTPM_STATUS_IDLE
| VTPM_STATUS_RESULT
,
216 .req_complete_val
= VTPM_STATUS_IDLE
| VTPM_STATUS_RESULT
,
217 .req_canceled
= vtpm_req_canceled
,
220 static irqreturn_t
tpmif_interrupt(int dummy
, void *dev_id
)
222 struct tpm_private
*priv
= dev_id
;
224 switch (priv
->shr
->state
) {
225 case VTPM_STATE_IDLE
:
226 case VTPM_STATE_FINISH
:
227 wake_up_interruptible(&priv
->read_queue
);
229 case VTPM_STATE_SUBMIT
:
230 case VTPM_STATE_CANCEL
:
237 static int setup_chip(struct device
*dev
, struct tpm_private
*priv
)
239 struct tpm_chip
*chip
;
241 chip
= tpmm_chip_alloc(dev
, &tpm_vtpm
);
243 return PTR_ERR(chip
);
245 init_waitqueue_head(&priv
->read_queue
);
248 dev_set_drvdata(&chip
->dev
, priv
);
253 /* caller must clean up in case of errors */
254 static int setup_ring(struct xenbus_device
*dev
, struct tpm_private
*priv
)
256 struct xenbus_transaction xbt
;
257 const char *message
= NULL
;
261 priv
->shr
= (void *)__get_free_page(GFP_KERNEL
|__GFP_ZERO
);
263 xenbus_dev_fatal(dev
, -ENOMEM
, "allocating shared ring");
267 rv
= xenbus_grant_ring(dev
, &priv
->shr
, 1, &gref
);
271 priv
->ring_ref
= gref
;
273 rv
= xenbus_alloc_evtchn(dev
, &priv
->evtchn
);
277 rv
= bind_evtchn_to_irqhandler(priv
->evtchn
, tpmif_interrupt
, 0,
280 xenbus_dev_fatal(dev
, rv
, "allocating TPM irq");
286 rv
= xenbus_transaction_start(&xbt
);
288 xenbus_dev_fatal(dev
, rv
, "starting transaction");
292 rv
= xenbus_printf(xbt
, dev
->nodename
,
293 "ring-ref", "%u", priv
->ring_ref
);
295 message
= "writing ring-ref";
296 goto abort_transaction
;
299 rv
= xenbus_printf(xbt
, dev
->nodename
, "event-channel", "%u",
302 message
= "writing event-channel";
303 goto abort_transaction
;
306 rv
= xenbus_printf(xbt
, dev
->nodename
, "feature-protocol-v2", "1");
308 message
= "writing feature-protocol-v2";
309 goto abort_transaction
;
312 rv
= xenbus_transaction_end(xbt
, 0);
316 xenbus_dev_fatal(dev
, rv
, "completing transaction");
320 xenbus_switch_state(dev
, XenbusStateInitialised
);
325 xenbus_transaction_end(xbt
, 1);
327 xenbus_dev_error(dev
, rv
, "%s", message
);
332 static void ring_free(struct tpm_private
*priv
)
338 gnttab_end_foreign_access(priv
->ring_ref
, 0,
339 (unsigned long)priv
->shr
);
341 free_page((unsigned long)priv
->shr
);
344 unbind_from_irqhandler(priv
->irq
, priv
);
349 static int tpmfront_probe(struct xenbus_device
*dev
,
350 const struct xenbus_device_id
*id
)
352 struct tpm_private
*priv
;
355 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
357 xenbus_dev_fatal(dev
, -ENOMEM
, "allocating priv structure");
361 rv
= setup_chip(&dev
->dev
, priv
);
367 rv
= setup_ring(dev
, priv
);
373 tpm_get_timeouts(priv
->chip
);
375 return tpm_chip_register(priv
->chip
);
378 static int tpmfront_remove(struct xenbus_device
*dev
)
380 struct tpm_chip
*chip
= dev_get_drvdata(&dev
->dev
);
381 struct tpm_private
*priv
= dev_get_drvdata(&chip
->dev
);
382 tpm_chip_unregister(chip
);
384 dev_set_drvdata(&chip
->dev
, NULL
);
388 static int tpmfront_resume(struct xenbus_device
*dev
)
390 /* A suspend/resume/migrate will interrupt a vTPM anyway */
391 tpmfront_remove(dev
);
392 return tpmfront_probe(dev
, NULL
);
395 static void backend_changed(struct xenbus_device
*dev
,
396 enum xenbus_state backend_state
)
398 switch (backend_state
) {
399 case XenbusStateInitialised
:
400 case XenbusStateConnected
:
401 if (dev
->state
== XenbusStateConnected
)
404 if (!xenbus_read_unsigned(dev
->otherend
, "feature-protocol-v2",
406 xenbus_dev_fatal(dev
, -EINVAL
,
407 "vTPM protocol 2 required");
410 xenbus_switch_state(dev
, XenbusStateConnected
);
413 case XenbusStateClosing
:
414 case XenbusStateClosed
:
415 device_unregister(&dev
->dev
);
416 xenbus_frontend_closed(dev
);
423 static const struct xenbus_device_id tpmfront_ids
[] = {
427 MODULE_ALIAS("xen:vtpm");
429 static struct xenbus_driver tpmfront_driver
= {
431 .probe
= tpmfront_probe
,
432 .remove
= tpmfront_remove
,
433 .resume
= tpmfront_resume
,
434 .otherend_changed
= backend_changed
,
437 static int __init
xen_tpmfront_init(void)
442 if (!xen_has_pv_devices())
445 return xenbus_register_frontend(&tpmfront_driver
);
447 module_init(xen_tpmfront_init
);
449 static void __exit
xen_tpmfront_exit(void)
451 xenbus_unregister_driver(&tpmfront_driver
);
453 module_exit(xen_tpmfront_exit
);
455 MODULE_AUTHOR("Daniel De Graaf <dgdegra@tycho.nsa.gov>");
456 MODULE_DESCRIPTION("Xen vTPM Driver");
457 MODULE_LICENSE("GPL");