]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - drivers/char/tpm/xen-tpmfront.c
tpm: drop 'irq' from struct tpm_vendor_specific
[mirror_ubuntu-hirsute-kernel.git] / drivers / char / tpm / xen-tpmfront.c
CommitLineData
e2683957
DDG
1/*
2 * Implementation of the Xen vTPM device frontend
3 *
4 * Author: Daniel De Graaf <dgdegra@tycho.nsa.gov>
5 *
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.
9 */
10#include <linux/errno.h>
11#include <linux/err.h>
12#include <linux/interrupt.h>
8702c675 13#include <xen/xen.h>
e2683957
DDG
14#include <xen/events.h>
15#include <xen/interface/io/tpmif.h>
16#include <xen/grant_table.h>
17#include <xen/xenbus.h>
18#include <xen/page.h>
19#include "tpm.h"
51c71a3b 20#include <xen/platform_pci.h>
e2683957
DDG
21
22struct tpm_private {
23 struct tpm_chip *chip;
24 struct xenbus_device *dev;
25
26 struct vtpm_shared_page *shr;
27
28 unsigned int evtchn;
29 int ring_ref;
30 domid_t backend_id;
570a3609 31 int irq;
e2683957
DDG
32};
33
34enum status_bits {
35 VTPM_STATUS_RUNNING = 0x1,
36 VTPM_STATUS_IDLE = 0x2,
37 VTPM_STATUS_RESULT = 0x4,
38 VTPM_STATUS_CANCELED = 0x8,
39};
40
41static u8 vtpm_status(struct tpm_chip *chip)
42{
43 struct tpm_private *priv = TPM_VPRIV(chip);
44 switch (priv->shr->state) {
45 case VTPM_STATE_IDLE:
46 return VTPM_STATUS_IDLE | VTPM_STATUS_CANCELED;
47 case VTPM_STATE_FINISH:
48 return VTPM_STATUS_IDLE | VTPM_STATUS_RESULT;
49 case VTPM_STATE_SUBMIT:
50 case VTPM_STATE_CANCEL: /* cancel requested, not yet canceled */
51 return VTPM_STATUS_RUNNING;
52 default:
53 return 0;
54 }
55}
56
57static bool vtpm_req_canceled(struct tpm_chip *chip, u8 status)
58{
59 return status & VTPM_STATUS_CANCELED;
60}
61
62static void vtpm_cancel(struct tpm_chip *chip)
63{
64 struct tpm_private *priv = TPM_VPRIV(chip);
65 priv->shr->state = VTPM_STATE_CANCEL;
66 wmb();
67 notify_remote_via_evtchn(priv->evtchn);
68}
69
70static unsigned int shr_data_offset(struct vtpm_shared_page *shr)
71{
72 return sizeof(*shr) + sizeof(u32) * shr->nr_extra_pages;
73}
74
75static int vtpm_send(struct tpm_chip *chip, u8 *buf, size_t count)
76{
77 struct tpm_private *priv = TPM_VPRIV(chip);
78 struct vtpm_shared_page *shr = priv->shr;
79 unsigned int offset = shr_data_offset(shr);
80
81 u32 ordinal;
82 unsigned long duration;
83
84 if (offset > PAGE_SIZE)
85 return -EINVAL;
86
87 if (offset + count > PAGE_SIZE)
88 return -EINVAL;
89
90 /* Wait for completion of any existing command or cancellation */
91 if (wait_for_tpm_stat(chip, VTPM_STATUS_IDLE, chip->vendor.timeout_c,
92 &chip->vendor.read_queue, true) < 0) {
93 vtpm_cancel(chip);
94 return -ETIME;
95 }
96
97 memcpy(offset + (u8 *)shr, buf, count);
98 shr->length = count;
99 barrier();
100 shr->state = VTPM_STATE_SUBMIT;
101 wmb();
102 notify_remote_via_evtchn(priv->evtchn);
103
104 ordinal = be32_to_cpu(((struct tpm_input_header*)buf)->ordinal);
105 duration = tpm_calc_ordinal_duration(chip, ordinal);
106
107 if (wait_for_tpm_stat(chip, VTPM_STATUS_IDLE, duration,
108 &chip->vendor.read_queue, true) < 0) {
109 /* got a signal or timeout, try to cancel */
110 vtpm_cancel(chip);
111 return -ETIME;
112 }
113
114 return count;
115}
116
117static int vtpm_recv(struct tpm_chip *chip, u8 *buf, size_t count)
118{
119 struct tpm_private *priv = TPM_VPRIV(chip);
120 struct vtpm_shared_page *shr = priv->shr;
121 unsigned int offset = shr_data_offset(shr);
122 size_t length = shr->length;
123
124 if (shr->state == VTPM_STATE_IDLE)
125 return -ECANCELED;
126
127 /* In theory the wait at the end of _send makes this one unnecessary */
128 if (wait_for_tpm_stat(chip, VTPM_STATUS_RESULT, chip->vendor.timeout_c,
129 &chip->vendor.read_queue, true) < 0) {
130 vtpm_cancel(chip);
131 return -ETIME;
132 }
133
134 if (offset > PAGE_SIZE)
135 return -EIO;
136
137 if (offset + length > PAGE_SIZE)
138 length = PAGE_SIZE - offset;
139
140 if (length > count)
141 length = count;
142
143 memcpy(buf, offset + (u8 *)shr, length);
144
145 return length;
146}
147
01ad1fa7 148static const struct tpm_class_ops tpm_vtpm = {
e2683957
DDG
149 .status = vtpm_status,
150 .recv = vtpm_recv,
151 .send = vtpm_send,
152 .cancel = vtpm_cancel,
153 .req_complete_mask = VTPM_STATUS_IDLE | VTPM_STATUS_RESULT,
154 .req_complete_val = VTPM_STATUS_IDLE | VTPM_STATUS_RESULT,
155 .req_canceled = vtpm_req_canceled,
e2683957
DDG
156};
157
158static irqreturn_t tpmif_interrupt(int dummy, void *dev_id)
159{
160 struct tpm_private *priv = dev_id;
161
162 switch (priv->shr->state) {
163 case VTPM_STATE_IDLE:
164 case VTPM_STATE_FINISH:
165 wake_up_interruptible(&priv->chip->vendor.read_queue);
166 break;
167 case VTPM_STATE_SUBMIT:
168 case VTPM_STATE_CANCEL:
169 default:
170 break;
171 }
172 return IRQ_HANDLED;
173}
174
175static int setup_chip(struct device *dev, struct tpm_private *priv)
176{
177 struct tpm_chip *chip;
178
afb5abc2
JS
179 chip = tpmm_chip_alloc(dev, &tpm_vtpm);
180 if (IS_ERR(chip))
181 return PTR_ERR(chip);
e2683957
DDG
182
183 init_waitqueue_head(&chip->vendor.read_queue);
184
185 priv->chip = chip;
186 TPM_VPRIV(chip) = priv;
187
188 return 0;
189}
190
191/* caller must clean up in case of errors */
192static int setup_ring(struct xenbus_device *dev, struct tpm_private *priv)
193{
194 struct xenbus_transaction xbt;
195 const char *message = NULL;
196 int rv;
ccc9d90a 197 grant_ref_t gref;
e2683957
DDG
198
199 priv->shr = (void *)__get_free_page(GFP_KERNEL|__GFP_ZERO);
200 if (!priv->shr) {
201 xenbus_dev_fatal(dev, -ENOMEM, "allocating shared ring");
202 return -ENOMEM;
203 }
204
ccc9d90a 205 rv = xenbus_grant_ring(dev, &priv->shr, 1, &gref);
e2683957
DDG
206 if (rv < 0)
207 return rv;
208
ccc9d90a 209 priv->ring_ref = gref;
e2683957
DDG
210
211 rv = xenbus_alloc_evtchn(dev, &priv->evtchn);
212 if (rv)
213 return rv;
214
215 rv = bind_evtchn_to_irqhandler(priv->evtchn, tpmif_interrupt, 0,
216 "tpmif", priv);
217 if (rv <= 0) {
218 xenbus_dev_fatal(dev, rv, "allocating TPM irq");
219 return rv;
220 }
570a3609 221 priv->irq = rv;
e2683957
DDG
222
223 again:
224 rv = xenbus_transaction_start(&xbt);
225 if (rv) {
226 xenbus_dev_fatal(dev, rv, "starting transaction");
227 return rv;
228 }
229
230 rv = xenbus_printf(xbt, dev->nodename,
231 "ring-ref", "%u", priv->ring_ref);
232 if (rv) {
233 message = "writing ring-ref";
234 goto abort_transaction;
235 }
236
237 rv = xenbus_printf(xbt, dev->nodename, "event-channel", "%u",
238 priv->evtchn);
239 if (rv) {
240 message = "writing event-channel";
241 goto abort_transaction;
242 }
243
244 rv = xenbus_printf(xbt, dev->nodename, "feature-protocol-v2", "1");
245 if (rv) {
246 message = "writing feature-protocol-v2";
247 goto abort_transaction;
248 }
249
250 rv = xenbus_transaction_end(xbt, 0);
251 if (rv == -EAGAIN)
252 goto again;
253 if (rv) {
254 xenbus_dev_fatal(dev, rv, "completing transaction");
255 return rv;
256 }
257
258 xenbus_switch_state(dev, XenbusStateInitialised);
259
260 return 0;
261
262 abort_transaction:
263 xenbus_transaction_end(xbt, 1);
264 if (message)
265 xenbus_dev_error(dev, rv, "%s", message);
266
267 return rv;
268}
269
270static void ring_free(struct tpm_private *priv)
271{
272 if (!priv)
273 return;
274
275 if (priv->ring_ref)
276 gnttab_end_foreign_access(priv->ring_ref, 0,
277 (unsigned long)priv->shr);
278 else
279 free_page((unsigned long)priv->shr);
280
570a3609
CR
281 if (priv->irq)
282 unbind_from_irqhandler(priv->irq, priv);
e2683957
DDG
283
284 kfree(priv);
285}
286
287static int tpmfront_probe(struct xenbus_device *dev,
288 const struct xenbus_device_id *id)
289{
290 struct tpm_private *priv;
afb5abc2 291 struct tpm_chip *chip;
e2683957
DDG
292 int rv;
293
294 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
295 if (!priv) {
296 xenbus_dev_fatal(dev, -ENOMEM, "allocating priv structure");
297 return -ENOMEM;
298 }
299
300 rv = setup_chip(&dev->dev, priv);
301 if (rv) {
302 kfree(priv);
303 return rv;
304 }
305
306 rv = setup_ring(dev, priv);
307 if (rv) {
afb5abc2
JS
308 chip = dev_get_drvdata(&dev->dev);
309 tpm_chip_unregister(chip);
e2683957
DDG
310 ring_free(priv);
311 return rv;
312 }
313
314 tpm_get_timeouts(priv->chip);
315
afb5abc2 316 return tpm_chip_register(priv->chip);
e2683957
DDG
317}
318
319static int tpmfront_remove(struct xenbus_device *dev)
320{
321 struct tpm_chip *chip = dev_get_drvdata(&dev->dev);
322 struct tpm_private *priv = TPM_VPRIV(chip);
afb5abc2 323 tpm_chip_unregister(chip);
e2683957
DDG
324 ring_free(priv);
325 TPM_VPRIV(chip) = NULL;
326 return 0;
327}
328
329static int tpmfront_resume(struct xenbus_device *dev)
330{
331 /* A suspend/resume/migrate will interrupt a vTPM anyway */
332 tpmfront_remove(dev);
333 return tpmfront_probe(dev, NULL);
334}
335
336static void backend_changed(struct xenbus_device *dev,
337 enum xenbus_state backend_state)
338{
339 int val;
340
341 switch (backend_state) {
342 case XenbusStateInitialised:
343 case XenbusStateConnected:
344 if (dev->state == XenbusStateConnected)
345 break;
346
347 if (xenbus_scanf(XBT_NIL, dev->otherend,
348 "feature-protocol-v2", "%d", &val) < 0)
349 val = 0;
350 if (!val) {
351 xenbus_dev_fatal(dev, -EINVAL,
352 "vTPM protocol 2 required");
353 return;
354 }
355 xenbus_switch_state(dev, XenbusStateConnected);
356 break;
357
358 case XenbusStateClosing:
359 case XenbusStateClosed:
360 device_unregister(&dev->dev);
361 xenbus_frontend_closed(dev);
362 break;
363 default:
364 break;
365 }
366}
367
368static const struct xenbus_device_id tpmfront_ids[] = {
369 { "vtpm" },
370 { "" }
371};
372MODULE_ALIAS("xen:vtpm");
373
95afae48
DV
374static struct xenbus_driver tpmfront_driver = {
375 .ids = tpmfront_ids,
376 .probe = tpmfront_probe,
377 .remove = tpmfront_remove,
378 .resume = tpmfront_resume,
379 .otherend_changed = backend_changed,
380};
e2683957
DDG
381
382static int __init xen_tpmfront_init(void)
383{
384 if (!xen_domain())
385 return -ENODEV;
386
51c71a3b
KRW
387 if (!xen_has_pv_devices())
388 return -ENODEV;
389
e2683957
DDG
390 return xenbus_register_frontend(&tpmfront_driver);
391}
392module_init(xen_tpmfront_init);
393
394static void __exit xen_tpmfront_exit(void)
395{
396 xenbus_unregister_driver(&tpmfront_driver);
397}
398module_exit(xen_tpmfront_exit);
399
400MODULE_AUTHOR("Daniel De Graaf <dgdegra@tycho.nsa.gov>");
401MODULE_DESCRIPTION("Xen vTPM Driver");
402MODULE_LICENSE("GPL");