tpm_ibmvtpm.c 16 KB

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