xen-tpmfront.c 8.7 KB

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