0001
0002
0003
0004
0005
0006
0007 #include <linux/errno.h>
0008 #include <linux/err.h>
0009 #include <linux/interrupt.h>
0010 #include <linux/freezer.h>
0011 #include <xen/xen.h>
0012 #include <xen/events.h>
0013 #include <xen/interface/io/tpmif.h>
0014 #include <xen/grant_table.h>
0015 #include <xen/xenbus.h>
0016 #include <xen/page.h>
0017 #include "tpm.h"
0018 #include <xen/platform_pci.h>
0019
0020 struct tpm_private {
0021 struct tpm_chip *chip;
0022 struct xenbus_device *dev;
0023
0024 struct vtpm_shared_page *shr;
0025
0026 unsigned int evtchn;
0027 int ring_ref;
0028 domid_t backend_id;
0029 int irq;
0030 wait_queue_head_t read_queue;
0031 };
0032
0033 enum status_bits {
0034 VTPM_STATUS_RUNNING = 0x1,
0035 VTPM_STATUS_IDLE = 0x2,
0036 VTPM_STATUS_RESULT = 0x4,
0037 VTPM_STATUS_CANCELED = 0x8,
0038 };
0039
0040 static bool wait_for_tpm_stat_cond(struct tpm_chip *chip, u8 mask,
0041 bool check_cancel, bool *canceled)
0042 {
0043 u8 status = chip->ops->status(chip);
0044
0045 *canceled = false;
0046 if ((status & mask) == mask)
0047 return true;
0048 if (check_cancel && chip->ops->req_canceled(chip, status)) {
0049 *canceled = true;
0050 return true;
0051 }
0052 return false;
0053 }
0054
0055 static int wait_for_tpm_stat(struct tpm_chip *chip, u8 mask,
0056 unsigned long timeout, wait_queue_head_t *queue,
0057 bool check_cancel)
0058 {
0059 unsigned long stop;
0060 long rc;
0061 u8 status;
0062 bool canceled = false;
0063
0064
0065 status = chip->ops->status(chip);
0066 if ((status & mask) == mask)
0067 return 0;
0068
0069 stop = jiffies + timeout;
0070
0071 if (chip->flags & TPM_CHIP_FLAG_IRQ) {
0072 again:
0073 timeout = stop - jiffies;
0074 if ((long)timeout <= 0)
0075 return -ETIME;
0076 rc = wait_event_interruptible_timeout(*queue,
0077 wait_for_tpm_stat_cond(chip, mask, check_cancel,
0078 &canceled),
0079 timeout);
0080 if (rc > 0) {
0081 if (canceled)
0082 return -ECANCELED;
0083 return 0;
0084 }
0085 if (rc == -ERESTARTSYS && freezing(current)) {
0086 clear_thread_flag(TIF_SIGPENDING);
0087 goto again;
0088 }
0089 } else {
0090 do {
0091 tpm_msleep(TPM_TIMEOUT);
0092 status = chip->ops->status(chip);
0093 if ((status & mask) == mask)
0094 return 0;
0095 } while (time_before(jiffies, stop));
0096 }
0097 return -ETIME;
0098 }
0099
0100 static u8 vtpm_status(struct tpm_chip *chip)
0101 {
0102 struct tpm_private *priv = dev_get_drvdata(&chip->dev);
0103 switch (priv->shr->state) {
0104 case VTPM_STATE_IDLE:
0105 return VTPM_STATUS_IDLE | VTPM_STATUS_CANCELED;
0106 case VTPM_STATE_FINISH:
0107 return VTPM_STATUS_IDLE | VTPM_STATUS_RESULT;
0108 case VTPM_STATE_SUBMIT:
0109 case VTPM_STATE_CANCEL:
0110 return VTPM_STATUS_RUNNING;
0111 default:
0112 return 0;
0113 }
0114 }
0115
0116 static bool vtpm_req_canceled(struct tpm_chip *chip, u8 status)
0117 {
0118 return status & VTPM_STATUS_CANCELED;
0119 }
0120
0121 static void vtpm_cancel(struct tpm_chip *chip)
0122 {
0123 struct tpm_private *priv = dev_get_drvdata(&chip->dev);
0124 priv->shr->state = VTPM_STATE_CANCEL;
0125 wmb();
0126 notify_remote_via_evtchn(priv->evtchn);
0127 }
0128
0129 static size_t shr_data_offset(struct vtpm_shared_page *shr)
0130 {
0131 return struct_size(shr, extra_pages, shr->nr_extra_pages);
0132 }
0133
0134 static int vtpm_send(struct tpm_chip *chip, u8 *buf, size_t count)
0135 {
0136 struct tpm_private *priv = dev_get_drvdata(&chip->dev);
0137 struct vtpm_shared_page *shr = priv->shr;
0138 size_t offset = shr_data_offset(shr);
0139
0140 u32 ordinal;
0141 unsigned long duration;
0142
0143 if (offset > PAGE_SIZE)
0144 return -EINVAL;
0145
0146 if (offset + count > PAGE_SIZE)
0147 return -EINVAL;
0148
0149
0150 if (wait_for_tpm_stat(chip, VTPM_STATUS_IDLE, chip->timeout_c,
0151 &priv->read_queue, true) < 0) {
0152 vtpm_cancel(chip);
0153 return -ETIME;
0154 }
0155
0156 memcpy(offset + (u8 *)shr, buf, count);
0157 shr->length = count;
0158 barrier();
0159 shr->state = VTPM_STATE_SUBMIT;
0160 wmb();
0161 notify_remote_via_evtchn(priv->evtchn);
0162
0163 ordinal = be32_to_cpu(((struct tpm_header *)buf)->ordinal);
0164 duration = tpm_calc_ordinal_duration(chip, ordinal);
0165
0166 if (wait_for_tpm_stat(chip, VTPM_STATUS_IDLE, duration,
0167 &priv->read_queue, true) < 0) {
0168
0169 vtpm_cancel(chip);
0170 return -ETIME;
0171 }
0172
0173 return 0;
0174 }
0175
0176 static int vtpm_recv(struct tpm_chip *chip, u8 *buf, size_t count)
0177 {
0178 struct tpm_private *priv = dev_get_drvdata(&chip->dev);
0179 struct vtpm_shared_page *shr = priv->shr;
0180 size_t offset = shr_data_offset(shr);
0181 size_t length = shr->length;
0182
0183 if (shr->state == VTPM_STATE_IDLE)
0184 return -ECANCELED;
0185
0186
0187 if (wait_for_tpm_stat(chip, VTPM_STATUS_RESULT, chip->timeout_c,
0188 &priv->read_queue, true) < 0) {
0189 vtpm_cancel(chip);
0190 return -ETIME;
0191 }
0192
0193 if (offset > PAGE_SIZE)
0194 return -EIO;
0195
0196 if (offset + length > PAGE_SIZE)
0197 length = PAGE_SIZE - offset;
0198
0199 if (length > count)
0200 length = count;
0201
0202 memcpy(buf, offset + (u8 *)shr, length);
0203
0204 return length;
0205 }
0206
0207 static const struct tpm_class_ops tpm_vtpm = {
0208 .status = vtpm_status,
0209 .recv = vtpm_recv,
0210 .send = vtpm_send,
0211 .cancel = vtpm_cancel,
0212 .req_complete_mask = VTPM_STATUS_IDLE | VTPM_STATUS_RESULT,
0213 .req_complete_val = VTPM_STATUS_IDLE | VTPM_STATUS_RESULT,
0214 .req_canceled = vtpm_req_canceled,
0215 };
0216
0217 static irqreturn_t tpmif_interrupt(int dummy, void *dev_id)
0218 {
0219 struct tpm_private *priv = dev_id;
0220
0221 switch (priv->shr->state) {
0222 case VTPM_STATE_IDLE:
0223 case VTPM_STATE_FINISH:
0224 wake_up_interruptible(&priv->read_queue);
0225 break;
0226 case VTPM_STATE_SUBMIT:
0227 case VTPM_STATE_CANCEL:
0228 default:
0229 break;
0230 }
0231 return IRQ_HANDLED;
0232 }
0233
0234 static int setup_chip(struct device *dev, struct tpm_private *priv)
0235 {
0236 struct tpm_chip *chip;
0237
0238 chip = tpmm_chip_alloc(dev, &tpm_vtpm);
0239 if (IS_ERR(chip))
0240 return PTR_ERR(chip);
0241
0242 init_waitqueue_head(&priv->read_queue);
0243
0244 priv->chip = chip;
0245 dev_set_drvdata(&chip->dev, priv);
0246
0247 return 0;
0248 }
0249
0250
0251 static int setup_ring(struct xenbus_device *dev, struct tpm_private *priv)
0252 {
0253 struct xenbus_transaction xbt;
0254 const char *message = NULL;
0255 int rv;
0256
0257 rv = xenbus_setup_ring(dev, GFP_KERNEL, (void **)&priv->shr, 1,
0258 &priv->ring_ref);
0259 if (rv < 0)
0260 return rv;
0261
0262 rv = xenbus_alloc_evtchn(dev, &priv->evtchn);
0263 if (rv)
0264 return rv;
0265
0266 rv = bind_evtchn_to_irqhandler(priv->evtchn, tpmif_interrupt, 0,
0267 "tpmif", priv);
0268 if (rv <= 0) {
0269 xenbus_dev_fatal(dev, rv, "allocating TPM irq");
0270 return rv;
0271 }
0272 priv->irq = rv;
0273
0274 again:
0275 rv = xenbus_transaction_start(&xbt);
0276 if (rv) {
0277 xenbus_dev_fatal(dev, rv, "starting transaction");
0278 return rv;
0279 }
0280
0281 rv = xenbus_printf(xbt, dev->nodename,
0282 "ring-ref", "%u", priv->ring_ref);
0283 if (rv) {
0284 message = "writing ring-ref";
0285 goto abort_transaction;
0286 }
0287
0288 rv = xenbus_printf(xbt, dev->nodename, "event-channel", "%u",
0289 priv->evtchn);
0290 if (rv) {
0291 message = "writing event-channel";
0292 goto abort_transaction;
0293 }
0294
0295 rv = xenbus_printf(xbt, dev->nodename, "feature-protocol-v2", "1");
0296 if (rv) {
0297 message = "writing feature-protocol-v2";
0298 goto abort_transaction;
0299 }
0300
0301 rv = xenbus_transaction_end(xbt, 0);
0302 if (rv == -EAGAIN)
0303 goto again;
0304 if (rv) {
0305 xenbus_dev_fatal(dev, rv, "completing transaction");
0306 return rv;
0307 }
0308
0309 xenbus_switch_state(dev, XenbusStateInitialised);
0310
0311 return 0;
0312
0313 abort_transaction:
0314 xenbus_transaction_end(xbt, 1);
0315 if (message)
0316 xenbus_dev_error(dev, rv, "%s", message);
0317
0318 return rv;
0319 }
0320
0321 static void ring_free(struct tpm_private *priv)
0322 {
0323 if (!priv)
0324 return;
0325
0326 xenbus_teardown_ring((void **)&priv->shr, 1, &priv->ring_ref);
0327
0328 if (priv->irq)
0329 unbind_from_irqhandler(priv->irq, priv);
0330
0331 kfree(priv);
0332 }
0333
0334 static int tpmfront_probe(struct xenbus_device *dev,
0335 const struct xenbus_device_id *id)
0336 {
0337 struct tpm_private *priv;
0338 int rv;
0339
0340 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
0341 if (!priv) {
0342 xenbus_dev_fatal(dev, -ENOMEM, "allocating priv structure");
0343 return -ENOMEM;
0344 }
0345
0346 rv = setup_chip(&dev->dev, priv);
0347 if (rv) {
0348 kfree(priv);
0349 return rv;
0350 }
0351
0352 rv = setup_ring(dev, priv);
0353 if (rv) {
0354 ring_free(priv);
0355 return rv;
0356 }
0357
0358 tpm_get_timeouts(priv->chip);
0359
0360 return tpm_chip_register(priv->chip);
0361 }
0362
0363 static int tpmfront_remove(struct xenbus_device *dev)
0364 {
0365 struct tpm_chip *chip = dev_get_drvdata(&dev->dev);
0366 struct tpm_private *priv = dev_get_drvdata(&chip->dev);
0367 tpm_chip_unregister(chip);
0368 ring_free(priv);
0369 dev_set_drvdata(&chip->dev, NULL);
0370 return 0;
0371 }
0372
0373 static int tpmfront_resume(struct xenbus_device *dev)
0374 {
0375
0376 tpmfront_remove(dev);
0377 return tpmfront_probe(dev, NULL);
0378 }
0379
0380 static void backend_changed(struct xenbus_device *dev,
0381 enum xenbus_state backend_state)
0382 {
0383 switch (backend_state) {
0384 case XenbusStateInitialised:
0385 case XenbusStateConnected:
0386 if (dev->state == XenbusStateConnected)
0387 break;
0388
0389 if (!xenbus_read_unsigned(dev->otherend, "feature-protocol-v2",
0390 0)) {
0391 xenbus_dev_fatal(dev, -EINVAL,
0392 "vTPM protocol 2 required");
0393 return;
0394 }
0395 xenbus_switch_state(dev, XenbusStateConnected);
0396 break;
0397
0398 case XenbusStateClosing:
0399 case XenbusStateClosed:
0400 device_unregister(&dev->dev);
0401 xenbus_frontend_closed(dev);
0402 break;
0403 default:
0404 break;
0405 }
0406 }
0407
0408 static const struct xenbus_device_id tpmfront_ids[] = {
0409 { "vtpm" },
0410 { "" }
0411 };
0412 MODULE_ALIAS("xen:vtpm");
0413
0414 static struct xenbus_driver tpmfront_driver = {
0415 .ids = tpmfront_ids,
0416 .probe = tpmfront_probe,
0417 .remove = tpmfront_remove,
0418 .resume = tpmfront_resume,
0419 .otherend_changed = backend_changed,
0420 };
0421
0422 static int __init xen_tpmfront_init(void)
0423 {
0424 if (!xen_domain())
0425 return -ENODEV;
0426
0427 if (!xen_has_pv_devices())
0428 return -ENODEV;
0429
0430 return xenbus_register_frontend(&tpmfront_driver);
0431 }
0432 module_init(xen_tpmfront_init);
0433
0434 static void __exit xen_tpmfront_exit(void)
0435 {
0436 xenbus_unregister_driver(&tpmfront_driver);
0437 }
0438 module_exit(xen_tpmfront_exit);
0439
0440 MODULE_AUTHOR("Daniel De Graaf <dgdegra@tycho.nsa.gov>");
0441 MODULE_DESCRIPTION("Xen vTPM Driver");
0442 MODULE_LICENSE("GPL");