tpm_ibmvtpm.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  1. /*
  2. * Copyright (C) 2012 IBM Corporation
  3. *
  4. * Author: Ashley Lai <ashleydlai@gmail.com>
  5. *
  6. * Maintained by: <tpmdd-devel@lists.sourceforge.net>
  7. *
  8. * Device driver for TCG/TCPA TPM (trusted platform module).
  9. * Specifications at www.trustedcomputinggroup.org
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License as
  13. * published by the Free Software Foundation, version 2 of the
  14. * License.
  15. *
  16. */
  17. #include <linux/dma-mapping.h>
  18. #include <linux/dmapool.h>
  19. #include <linux/slab.h>
  20. #include <asm/vio.h>
  21. #include <asm/irq.h>
  22. #include <linux/types.h>
  23. #include <linux/list.h>
  24. #include <linux/spinlock.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/wait.h>
  27. #include <asm/prom.h>
  28. #include "tpm.h"
  29. #include "tpm_ibmvtpm.h"
  30. static const char tpm_ibmvtpm_driver_name[] = "tpm_ibmvtpm";
  31. static struct vio_device_id tpm_ibmvtpm_device_table[] = {
  32. { "IBM,vtpm", "IBM,vtpm"},
  33. { "", "" }
  34. };
  35. MODULE_DEVICE_TABLE(vio, tpm_ibmvtpm_device_table);
  36. /**
  37. * ibmvtpm_send_crq - Send a CRQ request
  38. *
  39. * @vdev: vio device struct
  40. * @w1: first word
  41. * @w2: second word
  42. *
  43. * Return:
  44. * 0 -Sucess
  45. * Non-zero - Failure
  46. */
  47. static int ibmvtpm_send_crq(struct vio_dev *vdev, u64 w1, u64 w2)
  48. {
  49. return plpar_hcall_norets(H_SEND_CRQ, vdev->unit_address, w1, w2);
  50. }
  51. /**
  52. * tpm_ibmvtpm_recv - Receive data after send
  53. *
  54. * @chip: tpm chip struct
  55. * @buf: buffer to read
  56. * @count: size of buffer
  57. *
  58. * Return:
  59. * Number of bytes read
  60. */
  61. static int tpm_ibmvtpm_recv(struct tpm_chip *chip, u8 *buf, size_t count)
  62. {
  63. struct ibmvtpm_dev *ibmvtpm = dev_get_drvdata(&chip->dev);
  64. u16 len;
  65. int sig;
  66. if (!ibmvtpm->rtce_buf) {
  67. dev_err(ibmvtpm->dev, "ibmvtpm device is not ready\n");
  68. return 0;
  69. }
  70. sig = wait_event_interruptible(ibmvtpm->wq, !ibmvtpm->tpm_processing_cmd);
  71. if (sig)
  72. return -EINTR;
  73. len = ibmvtpm->res_len;
  74. if (count < len) {
  75. dev_err(ibmvtpm->dev,
  76. "Invalid size in recv: count=%zd, crq_size=%d\n",
  77. count, len);
  78. return -EIO;
  79. }
  80. spin_lock(&ibmvtpm->rtce_lock);
  81. memcpy((void *)buf, (void *)ibmvtpm->rtce_buf, len);
  82. memset(ibmvtpm->rtce_buf, 0, len);
  83. ibmvtpm->res_len = 0;
  84. spin_unlock(&ibmvtpm->rtce_lock);
  85. return len;
  86. }
  87. /**
  88. * tpm_ibmvtpm_send - Send tpm request
  89. *
  90. * @chip: tpm chip struct
  91. * @buf: buffer contains data to send
  92. * @count: size of buffer
  93. *
  94. * Return:
  95. * Number of bytes sent or < 0 on error.
  96. */
  97. static int tpm_ibmvtpm_send(struct tpm_chip *chip, u8 *buf, size_t count)
  98. {
  99. struct ibmvtpm_dev *ibmvtpm = dev_get_drvdata(&chip->dev);
  100. struct ibmvtpm_crq crq;
  101. __be64 *word = (__be64 *)&crq;
  102. int rc, sig;
  103. if (!ibmvtpm->rtce_buf) {
  104. dev_err(ibmvtpm->dev, "ibmvtpm device is not ready\n");
  105. return 0;
  106. }
  107. if (count > ibmvtpm->rtce_size) {
  108. dev_err(ibmvtpm->dev,
  109. "Invalid size in send: count=%zd, rtce_size=%d\n",
  110. count, ibmvtpm->rtce_size);
  111. return -EIO;
  112. }
  113. if (ibmvtpm->tpm_processing_cmd) {
  114. dev_info(ibmvtpm->dev,
  115. "Need to wait for TPM to finish\n");
  116. /* wait for previous command to finish */
  117. sig = wait_event_interruptible(ibmvtpm->wq, !ibmvtpm->tpm_processing_cmd);
  118. if (sig)
  119. return -EINTR;
  120. }
  121. spin_lock(&ibmvtpm->rtce_lock);
  122. ibmvtpm->res_len = 0;
  123. memcpy((void *)ibmvtpm->rtce_buf, (void *)buf, count);
  124. crq.valid = (u8)IBMVTPM_VALID_CMD;
  125. crq.msg = (u8)VTPM_TPM_COMMAND;
  126. crq.len = cpu_to_be16(count);
  127. crq.data = cpu_to_be32(ibmvtpm->rtce_dma_handle);
  128. /*
  129. * set the processing flag before the Hcall, since we may get the
  130. * result (interrupt) before even being able to check rc.
  131. */
  132. ibmvtpm->tpm_processing_cmd = true;
  133. rc = ibmvtpm_send_crq(ibmvtpm->vdev, be64_to_cpu(word[0]),
  134. be64_to_cpu(word[1]));
  135. if (rc != H_SUCCESS) {
  136. dev_err(ibmvtpm->dev, "tpm_ibmvtpm_send failed rc=%d\n", rc);
  137. rc = 0;
  138. ibmvtpm->tpm_processing_cmd = false;
  139. } else
  140. rc = count;
  141. spin_unlock(&ibmvtpm->rtce_lock);
  142. return rc;
  143. }
  144. static void tpm_ibmvtpm_cancel(struct tpm_chip *chip)
  145. {
  146. return;
  147. }
  148. static u8 tpm_ibmvtpm_status(struct tpm_chip *chip)
  149. {
  150. return 0;
  151. }
  152. /**
  153. * ibmvtpm_crq_get_rtce_size - Send a CRQ request to get rtce size
  154. *
  155. * @ibmvtpm: vtpm device struct
  156. *
  157. * Return:
  158. * 0 on success.
  159. * Non-zero on failure.
  160. */
  161. static int ibmvtpm_crq_get_rtce_size(struct ibmvtpm_dev *ibmvtpm)
  162. {
  163. struct ibmvtpm_crq crq;
  164. u64 *buf = (u64 *) &crq;
  165. int rc;
  166. crq.valid = (u8)IBMVTPM_VALID_CMD;
  167. crq.msg = (u8)VTPM_GET_RTCE_BUFFER_SIZE;
  168. rc = ibmvtpm_send_crq(ibmvtpm->vdev, cpu_to_be64(buf[0]),
  169. cpu_to_be64(buf[1]));
  170. if (rc != H_SUCCESS)
  171. dev_err(ibmvtpm->dev,
  172. "ibmvtpm_crq_get_rtce_size failed rc=%d\n", rc);
  173. return rc;
  174. }
  175. /**
  176. * ibmvtpm_crq_get_version - Send a CRQ request to get vtpm version
  177. * - Note that this is vtpm version and not tpm version
  178. *
  179. * @ibmvtpm: vtpm device struct
  180. *
  181. * Return:
  182. * 0 on success.
  183. * Non-zero on failure.
  184. */
  185. static int ibmvtpm_crq_get_version(struct ibmvtpm_dev *ibmvtpm)
  186. {
  187. struct ibmvtpm_crq crq;
  188. u64 *buf = (u64 *) &crq;
  189. int rc;
  190. crq.valid = (u8)IBMVTPM_VALID_CMD;
  191. crq.msg = (u8)VTPM_GET_VERSION;
  192. rc = ibmvtpm_send_crq(ibmvtpm->vdev, cpu_to_be64(buf[0]),
  193. cpu_to_be64(buf[1]));
  194. if (rc != H_SUCCESS)
  195. dev_err(ibmvtpm->dev,
  196. "ibmvtpm_crq_get_version failed rc=%d\n", rc);
  197. return rc;
  198. }
  199. /**
  200. * ibmvtpm_crq_send_init_complete - Send a CRQ initialize complete message
  201. * @ibmvtpm: vtpm device struct
  202. *
  203. * Return:
  204. * 0 on success.
  205. * Non-zero on failure.
  206. */
  207. static int ibmvtpm_crq_send_init_complete(struct ibmvtpm_dev *ibmvtpm)
  208. {
  209. int rc;
  210. rc = ibmvtpm_send_crq(ibmvtpm->vdev, INIT_CRQ_COMP_CMD, 0);
  211. if (rc != H_SUCCESS)
  212. dev_err(ibmvtpm->dev,
  213. "ibmvtpm_crq_send_init_complete failed rc=%d\n", rc);
  214. return rc;
  215. }
  216. /**
  217. * ibmvtpm_crq_send_init - Send a CRQ initialize message
  218. * @ibmvtpm: vtpm device struct
  219. *
  220. * Return:
  221. * 0 on success.
  222. * Non-zero on failure.
  223. */
  224. static int ibmvtpm_crq_send_init(struct ibmvtpm_dev *ibmvtpm)
  225. {
  226. int rc;
  227. rc = ibmvtpm_send_crq(ibmvtpm->vdev, INIT_CRQ_CMD, 0);
  228. if (rc != H_SUCCESS)
  229. dev_err(ibmvtpm->dev,
  230. "ibmvtpm_crq_send_init failed rc=%d\n", rc);
  231. return rc;
  232. }
  233. /**
  234. * tpm_ibmvtpm_remove - ibm vtpm remove entry point
  235. * @vdev: vio device struct
  236. *
  237. * Return: Always 0.
  238. */
  239. static int tpm_ibmvtpm_remove(struct vio_dev *vdev)
  240. {
  241. struct tpm_chip *chip = dev_get_drvdata(&vdev->dev);
  242. struct ibmvtpm_dev *ibmvtpm = dev_get_drvdata(&chip->dev);
  243. int rc = 0;
  244. tpm_chip_unregister(chip);
  245. free_irq(vdev->irq, ibmvtpm);
  246. do {
  247. if (rc)
  248. msleep(100);
  249. rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
  250. } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
  251. dma_unmap_single(ibmvtpm->dev, ibmvtpm->crq_dma_handle,
  252. CRQ_RES_BUF_SIZE, DMA_BIDIRECTIONAL);
  253. free_page((unsigned long)ibmvtpm->crq_queue.crq_addr);
  254. if (ibmvtpm->rtce_buf) {
  255. dma_unmap_single(ibmvtpm->dev, ibmvtpm->rtce_dma_handle,
  256. ibmvtpm->rtce_size, DMA_BIDIRECTIONAL);
  257. kfree(ibmvtpm->rtce_buf);
  258. }
  259. kfree(ibmvtpm);
  260. return 0;
  261. }
  262. /**
  263. * tpm_ibmvtpm_get_desired_dma - Get DMA size needed by this driver
  264. * @vdev: vio device struct
  265. *
  266. * Return:
  267. * Number of bytes the driver needs to DMA map.
  268. */
  269. static unsigned long tpm_ibmvtpm_get_desired_dma(struct vio_dev *vdev)
  270. {
  271. struct tpm_chip *chip = dev_get_drvdata(&vdev->dev);
  272. struct ibmvtpm_dev *ibmvtpm = dev_get_drvdata(&chip->dev);
  273. /*
  274. * ibmvtpm initializes at probe time, so the data we are
  275. * asking for may not be set yet. Estimate that 4K required
  276. * for TCE-mapped buffer in addition to CRQ.
  277. */
  278. if (!ibmvtpm)
  279. return CRQ_RES_BUF_SIZE + PAGE_SIZE;
  280. return CRQ_RES_BUF_SIZE + ibmvtpm->rtce_size;
  281. }
  282. /**
  283. * tpm_ibmvtpm_suspend - Suspend
  284. * @dev: device struct
  285. *
  286. * Return: Always 0.
  287. */
  288. static int tpm_ibmvtpm_suspend(struct device *dev)
  289. {
  290. struct tpm_chip *chip = dev_get_drvdata(dev);
  291. struct ibmvtpm_dev *ibmvtpm = dev_get_drvdata(&chip->dev);
  292. struct ibmvtpm_crq crq;
  293. u64 *buf = (u64 *) &crq;
  294. int rc = 0;
  295. crq.valid = (u8)IBMVTPM_VALID_CMD;
  296. crq.msg = (u8)VTPM_PREPARE_TO_SUSPEND;
  297. rc = ibmvtpm_send_crq(ibmvtpm->vdev, cpu_to_be64(buf[0]),
  298. cpu_to_be64(buf[1]));
  299. if (rc != H_SUCCESS)
  300. dev_err(ibmvtpm->dev,
  301. "tpm_ibmvtpm_suspend failed rc=%d\n", rc);
  302. return rc;
  303. }
  304. /**
  305. * ibmvtpm_reset_crq - Reset CRQ
  306. *
  307. * @ibmvtpm: ibm vtpm struct
  308. *
  309. * Return:
  310. * 0 on success.
  311. * Non-zero on failure.
  312. */
  313. static int ibmvtpm_reset_crq(struct ibmvtpm_dev *ibmvtpm)
  314. {
  315. int rc = 0;
  316. do {
  317. if (rc)
  318. msleep(100);
  319. rc = plpar_hcall_norets(H_FREE_CRQ,
  320. ibmvtpm->vdev->unit_address);
  321. } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
  322. memset(ibmvtpm->crq_queue.crq_addr, 0, CRQ_RES_BUF_SIZE);
  323. ibmvtpm->crq_queue.index = 0;
  324. return plpar_hcall_norets(H_REG_CRQ, ibmvtpm->vdev->unit_address,
  325. ibmvtpm->crq_dma_handle, CRQ_RES_BUF_SIZE);
  326. }
  327. /**
  328. * tpm_ibmvtpm_resume - Resume from suspend
  329. *
  330. * @dev: device struct
  331. *
  332. * Return: Always 0.
  333. */
  334. static int tpm_ibmvtpm_resume(struct device *dev)
  335. {
  336. struct tpm_chip *chip = dev_get_drvdata(dev);
  337. struct ibmvtpm_dev *ibmvtpm = dev_get_drvdata(&chip->dev);
  338. int rc = 0;
  339. do {
  340. if (rc)
  341. msleep(100);
  342. rc = plpar_hcall_norets(H_ENABLE_CRQ,
  343. ibmvtpm->vdev->unit_address);
  344. } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
  345. if (rc) {
  346. dev_err(dev, "Error enabling ibmvtpm rc=%d\n", rc);
  347. return rc;
  348. }
  349. rc = vio_enable_interrupts(ibmvtpm->vdev);
  350. if (rc) {
  351. dev_err(dev, "Error vio_enable_interrupts rc=%d\n", rc);
  352. return rc;
  353. }
  354. rc = ibmvtpm_crq_send_init(ibmvtpm);
  355. if (rc)
  356. dev_err(dev, "Error send_init rc=%d\n", rc);
  357. return rc;
  358. }
  359. static bool tpm_ibmvtpm_req_canceled(struct tpm_chip *chip, u8 status)
  360. {
  361. return (status == 0);
  362. }
  363. static const struct tpm_class_ops tpm_ibmvtpm = {
  364. .recv = tpm_ibmvtpm_recv,
  365. .send = tpm_ibmvtpm_send,
  366. .cancel = tpm_ibmvtpm_cancel,
  367. .status = tpm_ibmvtpm_status,
  368. .req_complete_mask = 0,
  369. .req_complete_val = 0,
  370. .req_canceled = tpm_ibmvtpm_req_canceled,
  371. };
  372. static const struct dev_pm_ops tpm_ibmvtpm_pm_ops = {
  373. .suspend = tpm_ibmvtpm_suspend,
  374. .resume = tpm_ibmvtpm_resume,
  375. };
  376. /**
  377. * ibmvtpm_crq_get_next - Get next responded crq
  378. *
  379. * @ibmvtpm: vtpm device struct
  380. *
  381. * Return: vtpm crq pointer or NULL.
  382. */
  383. static struct ibmvtpm_crq *ibmvtpm_crq_get_next(struct ibmvtpm_dev *ibmvtpm)
  384. {
  385. struct ibmvtpm_crq_queue *crq_q = &ibmvtpm->crq_queue;
  386. struct ibmvtpm_crq *crq = &crq_q->crq_addr[crq_q->index];
  387. if (crq->valid & VTPM_MSG_RES) {
  388. if (++crq_q->index == crq_q->num_entry)
  389. crq_q->index = 0;
  390. smp_rmb();
  391. } else
  392. crq = NULL;
  393. return crq;
  394. }
  395. /**
  396. * ibmvtpm_crq_process - Process responded crq
  397. *
  398. * @crq: crq to be processed
  399. * @ibmvtpm: vtpm device struct
  400. *
  401. */
  402. static void ibmvtpm_crq_process(struct ibmvtpm_crq *crq,
  403. struct ibmvtpm_dev *ibmvtpm)
  404. {
  405. int rc = 0;
  406. switch (crq->valid) {
  407. case VALID_INIT_CRQ:
  408. switch (crq->msg) {
  409. case INIT_CRQ_RES:
  410. dev_info(ibmvtpm->dev, "CRQ initialized\n");
  411. rc = ibmvtpm_crq_send_init_complete(ibmvtpm);
  412. if (rc)
  413. dev_err(ibmvtpm->dev, "Unable to send CRQ init complete rc=%d\n", rc);
  414. return;
  415. case INIT_CRQ_COMP_RES:
  416. dev_info(ibmvtpm->dev,
  417. "CRQ initialization completed\n");
  418. return;
  419. default:
  420. dev_err(ibmvtpm->dev, "Unknown crq message type: %d\n", crq->msg);
  421. return;
  422. }
  423. case IBMVTPM_VALID_CMD:
  424. switch (crq->msg) {
  425. case VTPM_GET_RTCE_BUFFER_SIZE_RES:
  426. if (be16_to_cpu(crq->len) <= 0) {
  427. dev_err(ibmvtpm->dev, "Invalid rtce size\n");
  428. return;
  429. }
  430. ibmvtpm->rtce_size = be16_to_cpu(crq->len);
  431. ibmvtpm->rtce_buf = kmalloc(ibmvtpm->rtce_size,
  432. GFP_ATOMIC);
  433. if (!ibmvtpm->rtce_buf) {
  434. dev_err(ibmvtpm->dev, "Failed to allocate memory for rtce buffer\n");
  435. return;
  436. }
  437. ibmvtpm->rtce_dma_handle = dma_map_single(ibmvtpm->dev,
  438. ibmvtpm->rtce_buf, ibmvtpm->rtce_size,
  439. DMA_BIDIRECTIONAL);
  440. if (dma_mapping_error(ibmvtpm->dev,
  441. ibmvtpm->rtce_dma_handle)) {
  442. kfree(ibmvtpm->rtce_buf);
  443. ibmvtpm->rtce_buf = NULL;
  444. dev_err(ibmvtpm->dev, "Failed to dma map rtce buffer\n");
  445. }
  446. return;
  447. case VTPM_GET_VERSION_RES:
  448. ibmvtpm->vtpm_version = be32_to_cpu(crq->data);
  449. return;
  450. case VTPM_TPM_COMMAND_RES:
  451. /* len of the data in rtce buffer */
  452. ibmvtpm->res_len = be16_to_cpu(crq->len);
  453. ibmvtpm->tpm_processing_cmd = false;
  454. wake_up_interruptible(&ibmvtpm->wq);
  455. return;
  456. default:
  457. return;
  458. }
  459. }
  460. return;
  461. }
  462. /**
  463. * ibmvtpm_interrupt - Interrupt handler
  464. *
  465. * @irq: irq number to handle
  466. * @vtpm_instance: vtpm that received interrupt
  467. *
  468. * Returns:
  469. * IRQ_HANDLED
  470. **/
  471. static irqreturn_t ibmvtpm_interrupt(int irq, void *vtpm_instance)
  472. {
  473. struct ibmvtpm_dev *ibmvtpm = (struct ibmvtpm_dev *) vtpm_instance;
  474. struct ibmvtpm_crq *crq;
  475. /* while loop is needed for initial setup (get version and
  476. * get rtce_size). There should be only one tpm request at any
  477. * given time.
  478. */
  479. while ((crq = ibmvtpm_crq_get_next(ibmvtpm)) != NULL) {
  480. ibmvtpm_crq_process(crq, ibmvtpm);
  481. crq->valid = 0;
  482. smp_wmb();
  483. }
  484. return IRQ_HANDLED;
  485. }
  486. /**
  487. * tpm_ibmvtpm_probe - ibm vtpm initialize entry point
  488. *
  489. * @vio_dev: vio device struct
  490. * @id: vio device id struct
  491. *
  492. * Return:
  493. * 0 on success.
  494. * Non-zero on failure.
  495. */
  496. static int tpm_ibmvtpm_probe(struct vio_dev *vio_dev,
  497. const struct vio_device_id *id)
  498. {
  499. struct ibmvtpm_dev *ibmvtpm;
  500. struct device *dev = &vio_dev->dev;
  501. struct ibmvtpm_crq_queue *crq_q;
  502. struct tpm_chip *chip;
  503. int rc = -ENOMEM, rc1;
  504. chip = tpmm_chip_alloc(dev, &tpm_ibmvtpm);
  505. if (IS_ERR(chip))
  506. return PTR_ERR(chip);
  507. ibmvtpm = kzalloc(sizeof(struct ibmvtpm_dev), GFP_KERNEL);
  508. if (!ibmvtpm) {
  509. dev_err(dev, "kzalloc for ibmvtpm failed\n");
  510. goto cleanup;
  511. }
  512. ibmvtpm->dev = dev;
  513. ibmvtpm->vdev = vio_dev;
  514. crq_q = &ibmvtpm->crq_queue;
  515. crq_q->crq_addr = (struct ibmvtpm_crq *)get_zeroed_page(GFP_KERNEL);
  516. if (!crq_q->crq_addr) {
  517. dev_err(dev, "Unable to allocate memory for crq_addr\n");
  518. goto cleanup;
  519. }
  520. crq_q->num_entry = CRQ_RES_BUF_SIZE / sizeof(*crq_q->crq_addr);
  521. ibmvtpm->crq_dma_handle = dma_map_single(dev, crq_q->crq_addr,
  522. CRQ_RES_BUF_SIZE,
  523. DMA_BIDIRECTIONAL);
  524. if (dma_mapping_error(dev, ibmvtpm->crq_dma_handle)) {
  525. dev_err(dev, "dma mapping failed\n");
  526. goto cleanup;
  527. }
  528. rc = plpar_hcall_norets(H_REG_CRQ, vio_dev->unit_address,
  529. ibmvtpm->crq_dma_handle, CRQ_RES_BUF_SIZE);
  530. if (rc == H_RESOURCE)
  531. rc = ibmvtpm_reset_crq(ibmvtpm);
  532. if (rc) {
  533. dev_err(dev, "Unable to register CRQ rc=%d\n", rc);
  534. goto reg_crq_cleanup;
  535. }
  536. rc = request_irq(vio_dev->irq, ibmvtpm_interrupt, 0,
  537. tpm_ibmvtpm_driver_name, ibmvtpm);
  538. if (rc) {
  539. dev_err(dev, "Error %d register irq 0x%x\n", rc, vio_dev->irq);
  540. goto init_irq_cleanup;
  541. }
  542. rc = vio_enable_interrupts(vio_dev);
  543. if (rc) {
  544. dev_err(dev, "Error %d enabling interrupts\n", rc);
  545. goto init_irq_cleanup;
  546. }
  547. init_waitqueue_head(&ibmvtpm->wq);
  548. crq_q->index = 0;
  549. dev_set_drvdata(&chip->dev, ibmvtpm);
  550. spin_lock_init(&ibmvtpm->rtce_lock);
  551. rc = ibmvtpm_crq_send_init(ibmvtpm);
  552. if (rc)
  553. goto init_irq_cleanup;
  554. rc = ibmvtpm_crq_get_version(ibmvtpm);
  555. if (rc)
  556. goto init_irq_cleanup;
  557. rc = ibmvtpm_crq_get_rtce_size(ibmvtpm);
  558. if (rc)
  559. goto init_irq_cleanup;
  560. return tpm_chip_register(chip);
  561. init_irq_cleanup:
  562. do {
  563. rc1 = plpar_hcall_norets(H_FREE_CRQ, vio_dev->unit_address);
  564. } while (rc1 == H_BUSY || H_IS_LONG_BUSY(rc1));
  565. reg_crq_cleanup:
  566. dma_unmap_single(dev, ibmvtpm->crq_dma_handle, CRQ_RES_BUF_SIZE,
  567. DMA_BIDIRECTIONAL);
  568. cleanup:
  569. if (ibmvtpm) {
  570. if (crq_q->crq_addr)
  571. free_page((unsigned long)crq_q->crq_addr);
  572. kfree(ibmvtpm);
  573. }
  574. return rc;
  575. }
  576. static struct vio_driver ibmvtpm_driver = {
  577. .id_table = tpm_ibmvtpm_device_table,
  578. .probe = tpm_ibmvtpm_probe,
  579. .remove = tpm_ibmvtpm_remove,
  580. .get_desired_dma = tpm_ibmvtpm_get_desired_dma,
  581. .name = tpm_ibmvtpm_driver_name,
  582. .pm = &tpm_ibmvtpm_pm_ops,
  583. };
  584. /**
  585. * ibmvtpm_module_init - Initialize ibm vtpm module.
  586. *
  587. *
  588. * Return:
  589. * 0 on success.
  590. * Non-zero on failure.
  591. */
  592. static int __init ibmvtpm_module_init(void)
  593. {
  594. return vio_register_driver(&ibmvtpm_driver);
  595. }
  596. /**
  597. * ibmvtpm_module_exit - Tear down ibm vtpm module.
  598. */
  599. static void __exit ibmvtpm_module_exit(void)
  600. {
  601. vio_unregister_driver(&ibmvtpm_driver);
  602. }
  603. module_init(ibmvtpm_module_init);
  604. module_exit(ibmvtpm_module_exit);
  605. MODULE_AUTHOR("adlai@us.ibm.com");
  606. MODULE_DESCRIPTION("IBM vTPM Driver");
  607. MODULE_VERSION("1.0");
  608. MODULE_LICENSE("GPL");