xen-tpmfront.c 8.6 KB

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