xref: /linux/drivers/char/tpm/tpm_ibmvtpm.c (revision c2a9c4bf034ab7415ec272c4c86f1c0b796f80e6)
1132f7629SAshley Lai /*
2132f7629SAshley Lai  * Copyright (C) 2012 IBM Corporation
3132f7629SAshley Lai  *
41a0f1b27SAshley Lai  * Author: Ashley Lai <ashleydlai@gmail.com>
5132f7629SAshley Lai  *
6132f7629SAshley Lai  * Maintained by: <tpmdd-devel@lists.sourceforge.net>
7132f7629SAshley Lai  *
8132f7629SAshley Lai  * Device driver for TCG/TCPA TPM (trusted platform module).
9132f7629SAshley Lai  * Specifications at www.trustedcomputinggroup.org
10132f7629SAshley Lai  *
11132f7629SAshley Lai  * This program is free software; you can redistribute it and/or
12132f7629SAshley Lai  * modify it under the terms of the GNU General Public License as
13132f7629SAshley Lai  * published by the Free Software Foundation, version 2 of the
14132f7629SAshley Lai  * License.
15132f7629SAshley Lai  *
16132f7629SAshley Lai  */
17132f7629SAshley Lai 
18132f7629SAshley Lai #include <linux/dma-mapping.h>
19132f7629SAshley Lai #include <linux/dmapool.h>
20132f7629SAshley Lai #include <linux/slab.h>
21132f7629SAshley Lai #include <asm/vio.h>
22132f7629SAshley Lai #include <asm/irq.h>
23132f7629SAshley Lai #include <linux/types.h>
24132f7629SAshley Lai #include <linux/list.h>
25132f7629SAshley Lai #include <linux/spinlock.h>
26132f7629SAshley Lai #include <linux/interrupt.h>
27132f7629SAshley Lai #include <linux/wait.h>
28132f7629SAshley Lai #include <asm/prom.h>
29132f7629SAshley Lai 
30132f7629SAshley Lai #include "tpm.h"
31132f7629SAshley Lai #include "tpm_ibmvtpm.h"
32132f7629SAshley Lai 
33132f7629SAshley Lai static const char tpm_ibmvtpm_driver_name[] = "tpm_ibmvtpm";
34132f7629SAshley Lai 
35*c2a9c4bfSArvind Yadav static const struct vio_device_id tpm_ibmvtpm_device_table[] = {
36132f7629SAshley Lai 	{ "IBM,vtpm", "IBM,vtpm"},
37132f7629SAshley Lai 	{ "", "" }
38132f7629SAshley Lai };
39132f7629SAshley Lai MODULE_DEVICE_TABLE(vio, tpm_ibmvtpm_device_table);
40132f7629SAshley Lai 
41132f7629SAshley Lai /**
42132f7629SAshley Lai  * ibmvtpm_send_crq - Send a CRQ request
4393c12f29SWinkler, Tomas  *
44132f7629SAshley Lai  * @vdev:	vio device struct
45132f7629SAshley Lai  * @w1:		first word
46132f7629SAshley Lai  * @w2:		second word
47132f7629SAshley Lai  *
4893c12f29SWinkler, Tomas  * Return:
49132f7629SAshley Lai  *	0 -Sucess
50132f7629SAshley Lai  *	Non-zero - Failure
51132f7629SAshley Lai  */
52132f7629SAshley Lai static int ibmvtpm_send_crq(struct vio_dev *vdev, u64 w1, u64 w2)
53132f7629SAshley Lai {
54132f7629SAshley Lai 	return plpar_hcall_norets(H_SEND_CRQ, vdev->unit_address, w1, w2);
55132f7629SAshley Lai }
56132f7629SAshley Lai 
57132f7629SAshley Lai /**
58132f7629SAshley Lai  * tpm_ibmvtpm_recv - Receive data after send
5993c12f29SWinkler, Tomas  *
60132f7629SAshley Lai  * @chip:	tpm chip struct
61132f7629SAshley Lai  * @buf:	buffer to read
6293c12f29SWinkler, Tomas  * @count:	size of buffer
63132f7629SAshley Lai  *
6493c12f29SWinkler, Tomas  * Return:
65132f7629SAshley Lai  *	Number of bytes read
66132f7629SAshley Lai  */
67132f7629SAshley Lai static int tpm_ibmvtpm_recv(struct tpm_chip *chip, u8 *buf, size_t count)
68132f7629SAshley Lai {
699e0d39d8SChristophe Ricard 	struct ibmvtpm_dev *ibmvtpm = dev_get_drvdata(&chip->dev);
70132f7629SAshley Lai 	u16 len;
71b5666502SAshley Lai 	int sig;
72132f7629SAshley Lai 
73132f7629SAshley Lai 	if (!ibmvtpm->rtce_buf) {
74132f7629SAshley Lai 		dev_err(ibmvtpm->dev, "ibmvtpm device is not ready\n");
75132f7629SAshley Lai 		return 0;
76132f7629SAshley Lai 	}
77132f7629SAshley Lai 
786674ff14SStefan Berger 	sig = wait_event_interruptible(ibmvtpm->wq, !ibmvtpm->tpm_processing_cmd);
79b5666502SAshley Lai 	if (sig)
80b5666502SAshley Lai 		return -EINTR;
81132f7629SAshley Lai 
82b5666502SAshley Lai 	len = ibmvtpm->res_len;
83b5666502SAshley Lai 
84b5666502SAshley Lai 	if (count < len) {
85132f7629SAshley Lai 		dev_err(ibmvtpm->dev,
8637ab0341SJason Gunthorpe 			"Invalid size in recv: count=%zd, crq_size=%d\n",
87b5666502SAshley Lai 			count, len);
88132f7629SAshley Lai 		return -EIO;
89132f7629SAshley Lai 	}
90132f7629SAshley Lai 
91132f7629SAshley Lai 	spin_lock(&ibmvtpm->rtce_lock);
92b5666502SAshley Lai 	memcpy((void *)buf, (void *)ibmvtpm->rtce_buf, len);
93b5666502SAshley Lai 	memset(ibmvtpm->rtce_buf, 0, len);
94b5666502SAshley Lai 	ibmvtpm->res_len = 0;
95132f7629SAshley Lai 	spin_unlock(&ibmvtpm->rtce_lock);
96132f7629SAshley Lai 	return len;
97132f7629SAshley Lai }
98132f7629SAshley Lai 
99132f7629SAshley Lai /**
100132f7629SAshley Lai  * tpm_ibmvtpm_send - Send tpm request
10193c12f29SWinkler, Tomas  *
102132f7629SAshley Lai  * @chip:	tpm chip struct
103132f7629SAshley Lai  * @buf:	buffer contains data to send
10493c12f29SWinkler, Tomas  * @count:	size of buffer
105132f7629SAshley Lai  *
10693c12f29SWinkler, Tomas  * Return:
10793c12f29SWinkler, Tomas  *	Number of bytes sent or < 0 on error.
108132f7629SAshley Lai  */
109132f7629SAshley Lai static int tpm_ibmvtpm_send(struct tpm_chip *chip, u8 *buf, size_t count)
110132f7629SAshley Lai {
1119e0d39d8SChristophe Ricard 	struct ibmvtpm_dev *ibmvtpm = dev_get_drvdata(&chip->dev);
112132f7629SAshley Lai 	struct ibmvtpm_crq crq;
11362dfd912Sjmlatten@linux.vnet.ibm.com 	__be64 *word = (__be64 *)&crq;
1146674ff14SStefan Berger 	int rc, sig;
115132f7629SAshley Lai 
116132f7629SAshley Lai 	if (!ibmvtpm->rtce_buf) {
117132f7629SAshley Lai 		dev_err(ibmvtpm->dev, "ibmvtpm device is not ready\n");
118132f7629SAshley Lai 		return 0;
119132f7629SAshley Lai 	}
120132f7629SAshley Lai 
121132f7629SAshley Lai 	if (count > ibmvtpm->rtce_size) {
122132f7629SAshley Lai 		dev_err(ibmvtpm->dev,
12337ab0341SJason Gunthorpe 			"Invalid size in send: count=%zd, rtce_size=%d\n",
124132f7629SAshley Lai 			count, ibmvtpm->rtce_size);
125132f7629SAshley Lai 		return -EIO;
126132f7629SAshley Lai 	}
127132f7629SAshley Lai 
1286674ff14SStefan Berger 	if (ibmvtpm->tpm_processing_cmd) {
1296674ff14SStefan Berger 		dev_info(ibmvtpm->dev,
1306674ff14SStefan Berger 		         "Need to wait for TPM to finish\n");
1316674ff14SStefan Berger 		/* wait for previous command to finish */
1326674ff14SStefan Berger 		sig = wait_event_interruptible(ibmvtpm->wq, !ibmvtpm->tpm_processing_cmd);
1336674ff14SStefan Berger 		if (sig)
1346674ff14SStefan Berger 			return -EINTR;
1356674ff14SStefan Berger 	}
1366674ff14SStefan Berger 
137132f7629SAshley Lai 	spin_lock(&ibmvtpm->rtce_lock);
1386674ff14SStefan Berger 	ibmvtpm->res_len = 0;
139132f7629SAshley Lai 	memcpy((void *)ibmvtpm->rtce_buf, (void *)buf, count);
140132f7629SAshley Lai 	crq.valid = (u8)IBMVTPM_VALID_CMD;
141132f7629SAshley Lai 	crq.msg = (u8)VTPM_TPM_COMMAND;
14262dfd912Sjmlatten@linux.vnet.ibm.com 	crq.len = cpu_to_be16(count);
14362dfd912Sjmlatten@linux.vnet.ibm.com 	crq.data = cpu_to_be32(ibmvtpm->rtce_dma_handle);
144132f7629SAshley Lai 
1456674ff14SStefan Berger 	/*
1466674ff14SStefan Berger 	 * set the processing flag before the Hcall, since we may get the
1476674ff14SStefan Berger 	 * result (interrupt) before even being able to check rc.
1486674ff14SStefan Berger 	 */
1496674ff14SStefan Berger 	ibmvtpm->tpm_processing_cmd = true;
1506674ff14SStefan Berger 
15162dfd912Sjmlatten@linux.vnet.ibm.com 	rc = ibmvtpm_send_crq(ibmvtpm->vdev, be64_to_cpu(word[0]),
15262dfd912Sjmlatten@linux.vnet.ibm.com 			      be64_to_cpu(word[1]));
153132f7629SAshley Lai 	if (rc != H_SUCCESS) {
154132f7629SAshley Lai 		dev_err(ibmvtpm->dev, "tpm_ibmvtpm_send failed rc=%d\n", rc);
155132f7629SAshley Lai 		rc = 0;
1566674ff14SStefan Berger 		ibmvtpm->tpm_processing_cmd = false;
157132f7629SAshley Lai 	} else
158132f7629SAshley Lai 		rc = count;
159132f7629SAshley Lai 
160132f7629SAshley Lai 	spin_unlock(&ibmvtpm->rtce_lock);
161132f7629SAshley Lai 	return rc;
162132f7629SAshley Lai }
163132f7629SAshley Lai 
164132f7629SAshley Lai static void tpm_ibmvtpm_cancel(struct tpm_chip *chip)
165132f7629SAshley Lai {
166132f7629SAshley Lai 	return;
167132f7629SAshley Lai }
168132f7629SAshley Lai 
169132f7629SAshley Lai static u8 tpm_ibmvtpm_status(struct tpm_chip *chip)
170132f7629SAshley Lai {
171132f7629SAshley Lai 	return 0;
172132f7629SAshley Lai }
173132f7629SAshley Lai 
174132f7629SAshley Lai /**
175132f7629SAshley Lai  * ibmvtpm_crq_get_rtce_size - Send a CRQ request to get rtce size
17693c12f29SWinkler, Tomas  *
177132f7629SAshley Lai  * @ibmvtpm:	vtpm device struct
178132f7629SAshley Lai  *
17993c12f29SWinkler, Tomas  * Return:
18093c12f29SWinkler, Tomas  *	0 on success.
18193c12f29SWinkler, Tomas  *	Non-zero on failure.
182132f7629SAshley Lai  */
183132f7629SAshley Lai static int ibmvtpm_crq_get_rtce_size(struct ibmvtpm_dev *ibmvtpm)
184132f7629SAshley Lai {
185132f7629SAshley Lai 	struct ibmvtpm_crq crq;
186132f7629SAshley Lai 	u64 *buf = (u64 *) &crq;
187132f7629SAshley Lai 	int rc;
188132f7629SAshley Lai 
189132f7629SAshley Lai 	crq.valid = (u8)IBMVTPM_VALID_CMD;
190132f7629SAshley Lai 	crq.msg = (u8)VTPM_GET_RTCE_BUFFER_SIZE;
191132f7629SAshley Lai 
192eb71f8a5Shonclo 	rc = ibmvtpm_send_crq(ibmvtpm->vdev, cpu_to_be64(buf[0]),
193eb71f8a5Shonclo 			      cpu_to_be64(buf[1]));
194132f7629SAshley Lai 	if (rc != H_SUCCESS)
195132f7629SAshley Lai 		dev_err(ibmvtpm->dev,
196132f7629SAshley Lai 			"ibmvtpm_crq_get_rtce_size failed rc=%d\n", rc);
197132f7629SAshley Lai 
198132f7629SAshley Lai 	return rc;
199132f7629SAshley Lai }
200132f7629SAshley Lai 
201132f7629SAshley Lai /**
202132f7629SAshley Lai  * ibmvtpm_crq_get_version - Send a CRQ request to get vtpm version
203132f7629SAshley Lai  *			   - Note that this is vtpm version and not tpm version
20493c12f29SWinkler, Tomas  *
205132f7629SAshley Lai  * @ibmvtpm:	vtpm device struct
206132f7629SAshley Lai  *
20793c12f29SWinkler, Tomas  * Return:
20893c12f29SWinkler, Tomas  *	0 on success.
20993c12f29SWinkler, Tomas  *	Non-zero on failure.
210132f7629SAshley Lai  */
211132f7629SAshley Lai static int ibmvtpm_crq_get_version(struct ibmvtpm_dev *ibmvtpm)
212132f7629SAshley Lai {
213132f7629SAshley Lai 	struct ibmvtpm_crq crq;
214132f7629SAshley Lai 	u64 *buf = (u64 *) &crq;
215132f7629SAshley Lai 	int rc;
216132f7629SAshley Lai 
217132f7629SAshley Lai 	crq.valid = (u8)IBMVTPM_VALID_CMD;
218132f7629SAshley Lai 	crq.msg = (u8)VTPM_GET_VERSION;
219132f7629SAshley Lai 
220eb71f8a5Shonclo 	rc = ibmvtpm_send_crq(ibmvtpm->vdev, cpu_to_be64(buf[0]),
221eb71f8a5Shonclo 			      cpu_to_be64(buf[1]));
222132f7629SAshley Lai 	if (rc != H_SUCCESS)
223132f7629SAshley Lai 		dev_err(ibmvtpm->dev,
224132f7629SAshley Lai 			"ibmvtpm_crq_get_version failed rc=%d\n", rc);
225132f7629SAshley Lai 
226132f7629SAshley Lai 	return rc;
227132f7629SAshley Lai }
228132f7629SAshley Lai 
229132f7629SAshley Lai /**
230132f7629SAshley Lai  * ibmvtpm_crq_send_init_complete - Send a CRQ initialize complete message
231132f7629SAshley Lai  * @ibmvtpm:	vtpm device struct
232132f7629SAshley Lai  *
23393c12f29SWinkler, Tomas  * Return:
23493c12f29SWinkler, Tomas  *	0 on success.
23593c12f29SWinkler, Tomas  *	Non-zero on failure.
236132f7629SAshley Lai  */
237132f7629SAshley Lai static int ibmvtpm_crq_send_init_complete(struct ibmvtpm_dev *ibmvtpm)
238132f7629SAshley Lai {
239132f7629SAshley Lai 	int rc;
240132f7629SAshley Lai 
241132f7629SAshley Lai 	rc = ibmvtpm_send_crq(ibmvtpm->vdev, INIT_CRQ_COMP_CMD, 0);
242132f7629SAshley Lai 	if (rc != H_SUCCESS)
243132f7629SAshley Lai 		dev_err(ibmvtpm->dev,
244132f7629SAshley Lai 			"ibmvtpm_crq_send_init_complete failed rc=%d\n", rc);
245132f7629SAshley Lai 
246132f7629SAshley Lai 	return rc;
247132f7629SAshley Lai }
248132f7629SAshley Lai 
249132f7629SAshley Lai /**
250132f7629SAshley Lai  * ibmvtpm_crq_send_init - Send a CRQ initialize message
251132f7629SAshley Lai  * @ibmvtpm:	vtpm device struct
252132f7629SAshley Lai  *
25393c12f29SWinkler, Tomas  * Return:
25493c12f29SWinkler, Tomas  *	0 on success.
25593c12f29SWinkler, Tomas  *	Non-zero on failure.
256132f7629SAshley Lai  */
257132f7629SAshley Lai static int ibmvtpm_crq_send_init(struct ibmvtpm_dev *ibmvtpm)
258132f7629SAshley Lai {
259132f7629SAshley Lai 	int rc;
260132f7629SAshley Lai 
261132f7629SAshley Lai 	rc = ibmvtpm_send_crq(ibmvtpm->vdev, INIT_CRQ_CMD, 0);
262132f7629SAshley Lai 	if (rc != H_SUCCESS)
263132f7629SAshley Lai 		dev_err(ibmvtpm->dev,
264132f7629SAshley Lai 			"ibmvtpm_crq_send_init failed rc=%d\n", rc);
265132f7629SAshley Lai 
266132f7629SAshley Lai 	return rc;
267132f7629SAshley Lai }
268132f7629SAshley Lai 
269132f7629SAshley Lai /**
270132f7629SAshley Lai  * tpm_ibmvtpm_remove - ibm vtpm remove entry point
271132f7629SAshley Lai  * @vdev:	vio device struct
272132f7629SAshley Lai  *
27393c12f29SWinkler, Tomas  * Return: Always 0.
274132f7629SAshley Lai  */
27539af33fcSBill Pemberton static int tpm_ibmvtpm_remove(struct vio_dev *vdev)
276132f7629SAshley Lai {
2779e0d39d8SChristophe Ricard 	struct tpm_chip *chip = dev_get_drvdata(&vdev->dev);
2789e0d39d8SChristophe Ricard 	struct ibmvtpm_dev *ibmvtpm = dev_get_drvdata(&chip->dev);
279132f7629SAshley Lai 	int rc = 0;
280132f7629SAshley Lai 
281afb5abc2SJarkko Sakkinen 	tpm_chip_unregister(chip);
282afb5abc2SJarkko Sakkinen 
283132f7629SAshley Lai 	free_irq(vdev->irq, ibmvtpm);
284132f7629SAshley Lai 
285132f7629SAshley Lai 	do {
286132f7629SAshley Lai 		if (rc)
287132f7629SAshley Lai 			msleep(100);
288132f7629SAshley Lai 		rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
289132f7629SAshley Lai 	} while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
290132f7629SAshley Lai 
291132f7629SAshley Lai 	dma_unmap_single(ibmvtpm->dev, ibmvtpm->crq_dma_handle,
292132f7629SAshley Lai 			 CRQ_RES_BUF_SIZE, DMA_BIDIRECTIONAL);
293132f7629SAshley Lai 	free_page((unsigned long)ibmvtpm->crq_queue.crq_addr);
294132f7629SAshley Lai 
295132f7629SAshley Lai 	if (ibmvtpm->rtce_buf) {
296132f7629SAshley Lai 		dma_unmap_single(ibmvtpm->dev, ibmvtpm->rtce_dma_handle,
297132f7629SAshley Lai 				 ibmvtpm->rtce_size, DMA_BIDIRECTIONAL);
298132f7629SAshley Lai 		kfree(ibmvtpm->rtce_buf);
299132f7629SAshley Lai 	}
300132f7629SAshley Lai 
301132f7629SAshley Lai 	kfree(ibmvtpm);
30231574d32SHon Ching \(Vicky\) Lo 	/* For tpm_ibmvtpm_get_desired_dma */
30331574d32SHon Ching \(Vicky\) Lo 	dev_set_drvdata(&vdev->dev, NULL);
304132f7629SAshley Lai 
305132f7629SAshley Lai 	return 0;
306132f7629SAshley Lai }
307132f7629SAshley Lai 
308132f7629SAshley Lai /**
309132f7629SAshley Lai  * tpm_ibmvtpm_get_desired_dma - Get DMA size needed by this driver
310132f7629SAshley Lai  * @vdev:	vio device struct
311132f7629SAshley Lai  *
31293c12f29SWinkler, Tomas  * Return:
31393c12f29SWinkler, Tomas  *	Number of bytes the driver needs to DMA map.
314132f7629SAshley Lai  */
315132f7629SAshley Lai static unsigned long tpm_ibmvtpm_get_desired_dma(struct vio_dev *vdev)
316132f7629SAshley Lai {
3179e0d39d8SChristophe Ricard 	struct tpm_chip *chip = dev_get_drvdata(&vdev->dev);
31831574d32SHon Ching \(Vicky\) Lo 	struct ibmvtpm_dev *ibmvtpm;
31984eb186bSHon Ching (Vicky) Lo 
32093c12f29SWinkler, Tomas 	/*
32193c12f29SWinkler, Tomas 	 * ibmvtpm initializes at probe time, so the data we are
32284eb186bSHon Ching (Vicky) Lo 	 * asking for may not be set yet. Estimate that 4K required
32384eb186bSHon Ching (Vicky) Lo 	 * for TCE-mapped buffer in addition to CRQ.
32484eb186bSHon Ching (Vicky) Lo 	 */
32531574d32SHon Ching \(Vicky\) Lo 	if (chip)
32631574d32SHon Ching \(Vicky\) Lo 		ibmvtpm = dev_get_drvdata(&chip->dev);
32731574d32SHon Ching \(Vicky\) Lo 	else
32884eb186bSHon Ching (Vicky) Lo 		return CRQ_RES_BUF_SIZE + PAGE_SIZE;
32984eb186bSHon Ching (Vicky) Lo 
330132f7629SAshley Lai 	return CRQ_RES_BUF_SIZE + ibmvtpm->rtce_size;
331132f7629SAshley Lai }
332132f7629SAshley Lai 
333132f7629SAshley Lai /**
334132f7629SAshley Lai  * tpm_ibmvtpm_suspend - Suspend
335132f7629SAshley Lai  * @dev:	device struct
336132f7629SAshley Lai  *
33793c12f29SWinkler, Tomas  * Return: Always 0.
338132f7629SAshley Lai  */
339132f7629SAshley Lai static int tpm_ibmvtpm_suspend(struct device *dev)
340132f7629SAshley Lai {
3419e0d39d8SChristophe Ricard 	struct tpm_chip *chip = dev_get_drvdata(dev);
3429e0d39d8SChristophe Ricard 	struct ibmvtpm_dev *ibmvtpm = dev_get_drvdata(&chip->dev);
343132f7629SAshley Lai 	struct ibmvtpm_crq crq;
344132f7629SAshley Lai 	u64 *buf = (u64 *) &crq;
345132f7629SAshley Lai 	int rc = 0;
346132f7629SAshley Lai 
347132f7629SAshley Lai 	crq.valid = (u8)IBMVTPM_VALID_CMD;
348132f7629SAshley Lai 	crq.msg = (u8)VTPM_PREPARE_TO_SUSPEND;
349132f7629SAshley Lai 
350eb71f8a5Shonclo 	rc = ibmvtpm_send_crq(ibmvtpm->vdev, cpu_to_be64(buf[0]),
351eb71f8a5Shonclo 			      cpu_to_be64(buf[1]));
352132f7629SAshley Lai 	if (rc != H_SUCCESS)
353132f7629SAshley Lai 		dev_err(ibmvtpm->dev,
354132f7629SAshley Lai 			"tpm_ibmvtpm_suspend failed rc=%d\n", rc);
355132f7629SAshley Lai 
356132f7629SAshley Lai 	return rc;
357132f7629SAshley Lai }
358132f7629SAshley Lai 
359132f7629SAshley Lai /**
360132f7629SAshley Lai  * ibmvtpm_reset_crq - Reset CRQ
36193c12f29SWinkler, Tomas  *
362132f7629SAshley Lai  * @ibmvtpm:	ibm vtpm struct
363132f7629SAshley Lai  *
36493c12f29SWinkler, Tomas  * Return:
36593c12f29SWinkler, Tomas  *	0 on success.
36693c12f29SWinkler, Tomas  *	Non-zero on failure.
367132f7629SAshley Lai  */
368132f7629SAshley Lai static int ibmvtpm_reset_crq(struct ibmvtpm_dev *ibmvtpm)
369132f7629SAshley Lai {
370132f7629SAshley Lai 	int rc = 0;
371132f7629SAshley Lai 
372132f7629SAshley Lai 	do {
373132f7629SAshley Lai 		if (rc)
374132f7629SAshley Lai 			msleep(100);
375132f7629SAshley Lai 		rc = plpar_hcall_norets(H_FREE_CRQ,
376132f7629SAshley Lai 					ibmvtpm->vdev->unit_address);
377132f7629SAshley Lai 	} while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
378132f7629SAshley Lai 
379132f7629SAshley Lai 	memset(ibmvtpm->crq_queue.crq_addr, 0, CRQ_RES_BUF_SIZE);
380132f7629SAshley Lai 	ibmvtpm->crq_queue.index = 0;
381132f7629SAshley Lai 
382132f7629SAshley Lai 	return plpar_hcall_norets(H_REG_CRQ, ibmvtpm->vdev->unit_address,
383132f7629SAshley Lai 				  ibmvtpm->crq_dma_handle, CRQ_RES_BUF_SIZE);
384132f7629SAshley Lai }
385132f7629SAshley Lai 
386132f7629SAshley Lai /**
387132f7629SAshley Lai  * tpm_ibmvtpm_resume - Resume from suspend
38893c12f29SWinkler, Tomas  *
389132f7629SAshley Lai  * @dev:	device struct
390132f7629SAshley Lai  *
39193c12f29SWinkler, Tomas  * Return: Always 0.
392132f7629SAshley Lai  */
393132f7629SAshley Lai static int tpm_ibmvtpm_resume(struct device *dev)
394132f7629SAshley Lai {
3959e0d39d8SChristophe Ricard 	struct tpm_chip *chip = dev_get_drvdata(dev);
3969e0d39d8SChristophe Ricard 	struct ibmvtpm_dev *ibmvtpm = dev_get_drvdata(&chip->dev);
397132f7629SAshley Lai 	int rc = 0;
398132f7629SAshley Lai 
399132f7629SAshley Lai 	do {
400132f7629SAshley Lai 		if (rc)
401132f7629SAshley Lai 			msleep(100);
402132f7629SAshley Lai 		rc = plpar_hcall_norets(H_ENABLE_CRQ,
403132f7629SAshley Lai 					ibmvtpm->vdev->unit_address);
404132f7629SAshley Lai 	} while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
405132f7629SAshley Lai 
406132f7629SAshley Lai 	if (rc) {
407132f7629SAshley Lai 		dev_err(dev, "Error enabling ibmvtpm rc=%d\n", rc);
408132f7629SAshley Lai 		return rc;
409132f7629SAshley Lai 	}
410132f7629SAshley Lai 
411b5666502SAshley Lai 	rc = vio_enable_interrupts(ibmvtpm->vdev);
412b5666502SAshley Lai 	if (rc) {
413b5666502SAshley Lai 		dev_err(dev, "Error vio_enable_interrupts rc=%d\n", rc);
414b5666502SAshley Lai 		return rc;
415b5666502SAshley Lai 	}
416132f7629SAshley Lai 
417132f7629SAshley Lai 	rc = ibmvtpm_crq_send_init(ibmvtpm);
418132f7629SAshley Lai 	if (rc)
419132f7629SAshley Lai 		dev_err(dev, "Error send_init rc=%d\n", rc);
420132f7629SAshley Lai 
421132f7629SAshley Lai 	return rc;
422132f7629SAshley Lai }
423132f7629SAshley Lai 
4241f866057SStefan Berger static bool tpm_ibmvtpm_req_canceled(struct tpm_chip *chip, u8 status)
4251f866057SStefan Berger {
4261f866057SStefan Berger 	return (status == 0);
4271f866057SStefan Berger }
4281f866057SStefan Berger 
42901ad1fa7SJason Gunthorpe static const struct tpm_class_ops tpm_ibmvtpm = {
430132f7629SAshley Lai 	.recv = tpm_ibmvtpm_recv,
431132f7629SAshley Lai 	.send = tpm_ibmvtpm_send,
432132f7629SAshley Lai 	.cancel = tpm_ibmvtpm_cancel,
433132f7629SAshley Lai 	.status = tpm_ibmvtpm_status,
434132f7629SAshley Lai 	.req_complete_mask = 0,
435132f7629SAshley Lai 	.req_complete_val = 0,
4361f866057SStefan Berger 	.req_canceled = tpm_ibmvtpm_req_canceled,
437132f7629SAshley Lai };
438132f7629SAshley Lai 
439132f7629SAshley Lai static const struct dev_pm_ops tpm_ibmvtpm_pm_ops = {
440132f7629SAshley Lai 	.suspend = tpm_ibmvtpm_suspend,
441132f7629SAshley Lai 	.resume = tpm_ibmvtpm_resume,
442132f7629SAshley Lai };
443132f7629SAshley Lai 
444132f7629SAshley Lai /**
445132f7629SAshley Lai  * ibmvtpm_crq_get_next - Get next responded crq
446132f7629SAshley Lai  *
44793c12f29SWinkler, Tomas  * @ibmvtpm:	vtpm device struct
44893c12f29SWinkler, Tomas  *
44993c12f29SWinkler, Tomas  * Return: vtpm crq pointer or NULL.
450132f7629SAshley Lai  */
451132f7629SAshley Lai static struct ibmvtpm_crq *ibmvtpm_crq_get_next(struct ibmvtpm_dev *ibmvtpm)
452132f7629SAshley Lai {
453132f7629SAshley Lai 	struct ibmvtpm_crq_queue *crq_q = &ibmvtpm->crq_queue;
454132f7629SAshley Lai 	struct ibmvtpm_crq *crq = &crq_q->crq_addr[crq_q->index];
455132f7629SAshley Lai 
456132f7629SAshley Lai 	if (crq->valid & VTPM_MSG_RES) {
457132f7629SAshley Lai 		if (++crq_q->index == crq_q->num_entry)
458132f7629SAshley Lai 			crq_q->index = 0;
459b5666502SAshley Lai 		smp_rmb();
460132f7629SAshley Lai 	} else
461132f7629SAshley Lai 		crq = NULL;
462132f7629SAshley Lai 	return crq;
463132f7629SAshley Lai }
464132f7629SAshley Lai 
465132f7629SAshley Lai /**
466132f7629SAshley Lai  * ibmvtpm_crq_process - Process responded crq
467132f7629SAshley Lai  *
46893c12f29SWinkler, Tomas  * @crq:	crq to be processed
46993c12f29SWinkler, Tomas  * @ibmvtpm:	vtpm device struct
47093c12f29SWinkler, Tomas  *
471132f7629SAshley Lai  */
472132f7629SAshley Lai static void ibmvtpm_crq_process(struct ibmvtpm_crq *crq,
473132f7629SAshley Lai 				struct ibmvtpm_dev *ibmvtpm)
474132f7629SAshley Lai {
475132f7629SAshley Lai 	int rc = 0;
476132f7629SAshley Lai 
477132f7629SAshley Lai 	switch (crq->valid) {
478132f7629SAshley Lai 	case VALID_INIT_CRQ:
479132f7629SAshley Lai 		switch (crq->msg) {
480132f7629SAshley Lai 		case INIT_CRQ_RES:
481132f7629SAshley Lai 			dev_info(ibmvtpm->dev, "CRQ initialized\n");
482132f7629SAshley Lai 			rc = ibmvtpm_crq_send_init_complete(ibmvtpm);
483132f7629SAshley Lai 			if (rc)
484132f7629SAshley Lai 				dev_err(ibmvtpm->dev, "Unable to send CRQ init complete rc=%d\n", rc);
485132f7629SAshley Lai 			return;
486132f7629SAshley Lai 		case INIT_CRQ_COMP_RES:
487132f7629SAshley Lai 			dev_info(ibmvtpm->dev,
488132f7629SAshley Lai 				 "CRQ initialization completed\n");
489132f7629SAshley Lai 			return;
490132f7629SAshley Lai 		default:
491132f7629SAshley Lai 			dev_err(ibmvtpm->dev, "Unknown crq message type: %d\n", crq->msg);
492132f7629SAshley Lai 			return;
493132f7629SAshley Lai 		}
494132f7629SAshley Lai 	case IBMVTPM_VALID_CMD:
495132f7629SAshley Lai 		switch (crq->msg) {
496132f7629SAshley Lai 		case VTPM_GET_RTCE_BUFFER_SIZE_RES:
497eb71f8a5Shonclo 			if (be16_to_cpu(crq->len) <= 0) {
498132f7629SAshley Lai 				dev_err(ibmvtpm->dev, "Invalid rtce size\n");
499132f7629SAshley Lai 				return;
500132f7629SAshley Lai 			}
501eb71f8a5Shonclo 			ibmvtpm->rtce_size = be16_to_cpu(crq->len);
502132f7629SAshley Lai 			ibmvtpm->rtce_buf = kmalloc(ibmvtpm->rtce_size,
50360ecd86cSHon Ching \(Vicky\) Lo 						    GFP_ATOMIC);
504132f7629SAshley Lai 			if (!ibmvtpm->rtce_buf) {
505132f7629SAshley Lai 				dev_err(ibmvtpm->dev, "Failed to allocate memory for rtce buffer\n");
506132f7629SAshley Lai 				return;
507132f7629SAshley Lai 			}
508132f7629SAshley Lai 
509132f7629SAshley Lai 			ibmvtpm->rtce_dma_handle = dma_map_single(ibmvtpm->dev,
510132f7629SAshley Lai 				ibmvtpm->rtce_buf, ibmvtpm->rtce_size,
511132f7629SAshley Lai 				DMA_BIDIRECTIONAL);
512132f7629SAshley Lai 
513132f7629SAshley Lai 			if (dma_mapping_error(ibmvtpm->dev,
514132f7629SAshley Lai 					      ibmvtpm->rtce_dma_handle)) {
515132f7629SAshley Lai 				kfree(ibmvtpm->rtce_buf);
516132f7629SAshley Lai 				ibmvtpm->rtce_buf = NULL;
517132f7629SAshley Lai 				dev_err(ibmvtpm->dev, "Failed to dma map rtce buffer\n");
518132f7629SAshley Lai 			}
519132f7629SAshley Lai 
520132f7629SAshley Lai 			return;
521132f7629SAshley Lai 		case VTPM_GET_VERSION_RES:
522eb71f8a5Shonclo 			ibmvtpm->vtpm_version = be32_to_cpu(crq->data);
523132f7629SAshley Lai 			return;
524132f7629SAshley Lai 		case VTPM_TPM_COMMAND_RES:
525b5666502SAshley Lai 			/* len of the data in rtce buffer */
526eb71f8a5Shonclo 			ibmvtpm->res_len = be16_to_cpu(crq->len);
5276674ff14SStefan Berger 			ibmvtpm->tpm_processing_cmd = false;
528b5666502SAshley Lai 			wake_up_interruptible(&ibmvtpm->wq);
529132f7629SAshley Lai 			return;
530132f7629SAshley Lai 		default:
531132f7629SAshley Lai 			return;
532132f7629SAshley Lai 		}
533132f7629SAshley Lai 	}
534132f7629SAshley Lai 	return;
535132f7629SAshley Lai }
536132f7629SAshley Lai 
537132f7629SAshley Lai /**
538132f7629SAshley Lai  * ibmvtpm_interrupt -	Interrupt handler
53993c12f29SWinkler, Tomas  *
540132f7629SAshley Lai  * @irq:		irq number to handle
541132f7629SAshley Lai  * @vtpm_instance:	vtpm that received interrupt
542132f7629SAshley Lai  *
543132f7629SAshley Lai  * Returns:
544132f7629SAshley Lai  *	IRQ_HANDLED
545132f7629SAshley Lai  **/
546132f7629SAshley Lai static irqreturn_t ibmvtpm_interrupt(int irq, void *vtpm_instance)
547132f7629SAshley Lai {
548132f7629SAshley Lai 	struct ibmvtpm_dev *ibmvtpm = (struct ibmvtpm_dev *) vtpm_instance;
549132f7629SAshley Lai 	struct ibmvtpm_crq *crq;
550132f7629SAshley Lai 
551b5666502SAshley Lai 	/* while loop is needed for initial setup (get version and
552b5666502SAshley Lai 	 * get rtce_size). There should be only one tpm request at any
553b5666502SAshley Lai 	 * given time.
554b5666502SAshley Lai 	 */
555132f7629SAshley Lai 	while ((crq = ibmvtpm_crq_get_next(ibmvtpm)) != NULL) {
556132f7629SAshley Lai 		ibmvtpm_crq_process(crq, ibmvtpm);
557132f7629SAshley Lai 		crq->valid = 0;
558b5666502SAshley Lai 		smp_wmb();
559132f7629SAshley Lai 	}
560132f7629SAshley Lai 
561b5666502SAshley Lai 	return IRQ_HANDLED;
562132f7629SAshley Lai }
563132f7629SAshley Lai 
564132f7629SAshley Lai /**
565132f7629SAshley Lai  * tpm_ibmvtpm_probe - ibm vtpm initialize entry point
56693c12f29SWinkler, Tomas  *
567132f7629SAshley Lai  * @vio_dev:	vio device struct
568132f7629SAshley Lai  * @id:		vio device id struct
569132f7629SAshley Lai  *
57093c12f29SWinkler, Tomas  * Return:
57193c12f29SWinkler, Tomas  *	0 on success.
57293c12f29SWinkler, Tomas  *	Non-zero on failure.
573132f7629SAshley Lai  */
574afc6d369SBill Pemberton static int tpm_ibmvtpm_probe(struct vio_dev *vio_dev,
575132f7629SAshley Lai 				   const struct vio_device_id *id)
576132f7629SAshley Lai {
577132f7629SAshley Lai 	struct ibmvtpm_dev *ibmvtpm;
578132f7629SAshley Lai 	struct device *dev = &vio_dev->dev;
579132f7629SAshley Lai 	struct ibmvtpm_crq_queue *crq_q;
580132f7629SAshley Lai 	struct tpm_chip *chip;
581132f7629SAshley Lai 	int rc = -ENOMEM, rc1;
582132f7629SAshley Lai 
583afb5abc2SJarkko Sakkinen 	chip = tpmm_chip_alloc(dev, &tpm_ibmvtpm);
584afb5abc2SJarkko Sakkinen 	if (IS_ERR(chip))
585afb5abc2SJarkko Sakkinen 		return PTR_ERR(chip);
586132f7629SAshley Lai 
587132f7629SAshley Lai 	ibmvtpm = kzalloc(sizeof(struct ibmvtpm_dev), GFP_KERNEL);
588132f7629SAshley Lai 	if (!ibmvtpm) {
589132f7629SAshley Lai 		dev_err(dev, "kzalloc for ibmvtpm failed\n");
590132f7629SAshley Lai 		goto cleanup;
591132f7629SAshley Lai 	}
592132f7629SAshley Lai 
5939d75f089SHon Ching \(Vicky\) Lo 	ibmvtpm->dev = dev;
5949d75f089SHon Ching \(Vicky\) Lo 	ibmvtpm->vdev = vio_dev;
5959d75f089SHon Ching \(Vicky\) Lo 
596132f7629SAshley Lai 	crq_q = &ibmvtpm->crq_queue;
597132f7629SAshley Lai 	crq_q->crq_addr = (struct ibmvtpm_crq *)get_zeroed_page(GFP_KERNEL);
598132f7629SAshley Lai 	if (!crq_q->crq_addr) {
599132f7629SAshley Lai 		dev_err(dev, "Unable to allocate memory for crq_addr\n");
600132f7629SAshley Lai 		goto cleanup;
601132f7629SAshley Lai 	}
602132f7629SAshley Lai 
603132f7629SAshley Lai 	crq_q->num_entry = CRQ_RES_BUF_SIZE / sizeof(*crq_q->crq_addr);
604132f7629SAshley Lai 	ibmvtpm->crq_dma_handle = dma_map_single(dev, crq_q->crq_addr,
605132f7629SAshley Lai 						 CRQ_RES_BUF_SIZE,
606132f7629SAshley Lai 						 DMA_BIDIRECTIONAL);
607132f7629SAshley Lai 
608132f7629SAshley Lai 	if (dma_mapping_error(dev, ibmvtpm->crq_dma_handle)) {
609132f7629SAshley Lai 		dev_err(dev, "dma mapping failed\n");
610132f7629SAshley Lai 		goto cleanup;
611132f7629SAshley Lai 	}
612132f7629SAshley Lai 
613132f7629SAshley Lai 	rc = plpar_hcall_norets(H_REG_CRQ, vio_dev->unit_address,
614132f7629SAshley Lai 				ibmvtpm->crq_dma_handle, CRQ_RES_BUF_SIZE);
615132f7629SAshley Lai 	if (rc == H_RESOURCE)
616132f7629SAshley Lai 		rc = ibmvtpm_reset_crq(ibmvtpm);
617132f7629SAshley Lai 
618132f7629SAshley Lai 	if (rc) {
619132f7629SAshley Lai 		dev_err(dev, "Unable to register CRQ rc=%d\n", rc);
620132f7629SAshley Lai 		goto reg_crq_cleanup;
621132f7629SAshley Lai 	}
622132f7629SAshley Lai 
623132f7629SAshley Lai 	rc = request_irq(vio_dev->irq, ibmvtpm_interrupt, 0,
624132f7629SAshley Lai 			 tpm_ibmvtpm_driver_name, ibmvtpm);
625132f7629SAshley Lai 	if (rc) {
626132f7629SAshley Lai 		dev_err(dev, "Error %d register irq 0x%x\n", rc, vio_dev->irq);
627132f7629SAshley Lai 		goto init_irq_cleanup;
628132f7629SAshley Lai 	}
629132f7629SAshley Lai 
630132f7629SAshley Lai 	rc = vio_enable_interrupts(vio_dev);
631132f7629SAshley Lai 	if (rc) {
632132f7629SAshley Lai 		dev_err(dev, "Error %d enabling interrupts\n", rc);
633132f7629SAshley Lai 		goto init_irq_cleanup;
634132f7629SAshley Lai 	}
635132f7629SAshley Lai 
636b5666502SAshley Lai 	init_waitqueue_head(&ibmvtpm->wq);
637b5666502SAshley Lai 
638132f7629SAshley Lai 	crq_q->index = 0;
639132f7629SAshley Lai 
64075254557SStephen Rothwell 	dev_set_drvdata(&chip->dev, ibmvtpm);
641132f7629SAshley Lai 
642132f7629SAshley Lai 	spin_lock_init(&ibmvtpm->rtce_lock);
643132f7629SAshley Lai 
644132f7629SAshley Lai 	rc = ibmvtpm_crq_send_init(ibmvtpm);
645132f7629SAshley Lai 	if (rc)
646132f7629SAshley Lai 		goto init_irq_cleanup;
647132f7629SAshley Lai 
648132f7629SAshley Lai 	rc = ibmvtpm_crq_get_version(ibmvtpm);
649132f7629SAshley Lai 	if (rc)
650132f7629SAshley Lai 		goto init_irq_cleanup;
651132f7629SAshley Lai 
652132f7629SAshley Lai 	rc = ibmvtpm_crq_get_rtce_size(ibmvtpm);
653132f7629SAshley Lai 	if (rc)
654132f7629SAshley Lai 		goto init_irq_cleanup;
655132f7629SAshley Lai 
656afb5abc2SJarkko Sakkinen 	return tpm_chip_register(chip);
657132f7629SAshley Lai init_irq_cleanup:
658132f7629SAshley Lai 	do {
659132f7629SAshley Lai 		rc1 = plpar_hcall_norets(H_FREE_CRQ, vio_dev->unit_address);
660132f7629SAshley Lai 	} while (rc1 == H_BUSY || H_IS_LONG_BUSY(rc1));
661132f7629SAshley Lai reg_crq_cleanup:
662132f7629SAshley Lai 	dma_unmap_single(dev, ibmvtpm->crq_dma_handle, CRQ_RES_BUF_SIZE,
663132f7629SAshley Lai 			 DMA_BIDIRECTIONAL);
664132f7629SAshley Lai cleanup:
665132f7629SAshley Lai 	if (ibmvtpm) {
666132f7629SAshley Lai 		if (crq_q->crq_addr)
667132f7629SAshley Lai 			free_page((unsigned long)crq_q->crq_addr);
668132f7629SAshley Lai 		kfree(ibmvtpm);
669132f7629SAshley Lai 	}
670132f7629SAshley Lai 
671132f7629SAshley Lai 	return rc;
672132f7629SAshley Lai }
673132f7629SAshley Lai 
674132f7629SAshley Lai static struct vio_driver ibmvtpm_driver = {
675132f7629SAshley Lai 	.id_table	 = tpm_ibmvtpm_device_table,
676132f7629SAshley Lai 	.probe		 = tpm_ibmvtpm_probe,
677132f7629SAshley Lai 	.remove		 = tpm_ibmvtpm_remove,
678132f7629SAshley Lai 	.get_desired_dma = tpm_ibmvtpm_get_desired_dma,
679132f7629SAshley Lai 	.name		 = tpm_ibmvtpm_driver_name,
680132f7629SAshley Lai 	.pm		 = &tpm_ibmvtpm_pm_ops,
681132f7629SAshley Lai };
682132f7629SAshley Lai 
683132f7629SAshley Lai /**
68493c12f29SWinkler, Tomas  * ibmvtpm_module_init - Initialize ibm vtpm module.
685132f7629SAshley Lai  *
68693c12f29SWinkler, Tomas  *
68793c12f29SWinkler, Tomas  * Return:
68893c12f29SWinkler, Tomas  *	0 on success.
68993c12f29SWinkler, Tomas  *	Non-zero on failure.
690132f7629SAshley Lai  */
691132f7629SAshley Lai static int __init ibmvtpm_module_init(void)
692132f7629SAshley Lai {
693132f7629SAshley Lai 	return vio_register_driver(&ibmvtpm_driver);
694132f7629SAshley Lai }
695132f7629SAshley Lai 
696132f7629SAshley Lai /**
69793c12f29SWinkler, Tomas  * ibmvtpm_module_exit - Tear down ibm vtpm module.
698132f7629SAshley Lai  */
699132f7629SAshley Lai static void __exit ibmvtpm_module_exit(void)
700132f7629SAshley Lai {
701132f7629SAshley Lai 	vio_unregister_driver(&ibmvtpm_driver);
702132f7629SAshley Lai }
703132f7629SAshley Lai 
704132f7629SAshley Lai module_init(ibmvtpm_module_init);
705132f7629SAshley Lai module_exit(ibmvtpm_module_exit);
706132f7629SAshley Lai 
707132f7629SAshley Lai MODULE_AUTHOR("adlai@us.ibm.com");
708132f7629SAshley Lai MODULE_DESCRIPTION("IBM vTPM Driver");
709132f7629SAshley Lai MODULE_VERSION("1.0");
710132f7629SAshley Lai MODULE_LICENSE("GPL");
711