tpm_ibmvtpm.c 17 KB

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