xref: /openbmc/linux/drivers/char/tpm/tpm_ibmvtpm.c (revision 3381df09)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2012 IBM Corporation
4  *
5  * Author: Ashley Lai <ashleydlai@gmail.com>
6  *
7  * Maintained by: <tpmdd-devel@lists.sourceforge.net>
8  *
9  * Device driver for TCG/TCPA TPM (trusted platform module).
10  * Specifications at www.trustedcomputinggroup.org
11  */
12 
13 #include <linux/dma-mapping.h>
14 #include <linux/dmapool.h>
15 #include <linux/slab.h>
16 #include <asm/vio.h>
17 #include <asm/irq.h>
18 #include <linux/types.h>
19 #include <linux/list.h>
20 #include <linux/spinlock.h>
21 #include <linux/interrupt.h>
22 #include <linux/wait.h>
23 #include <asm/prom.h>
24 
25 #include "tpm.h"
26 #include "tpm_ibmvtpm.h"
27 
28 static const char tpm_ibmvtpm_driver_name[] = "tpm_ibmvtpm";
29 
30 static const struct vio_device_id tpm_ibmvtpm_device_table[] = {
31 	{ "IBM,vtpm", "IBM,vtpm"},
32 	{ "IBM,vtpm", "IBM,vtpm20"},
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 /**
96  * tpm_ibmvtpm_recv - Receive data after send
97  *
98  * @chip:	tpm chip struct
99  * @buf:	buffer to read
100  * @count:	size of buffer
101  *
102  * Return:
103  *	Number of bytes read
104  */
105 static int tpm_ibmvtpm_recv(struct tpm_chip *chip, u8 *buf, size_t count)
106 {
107 	struct ibmvtpm_dev *ibmvtpm = dev_get_drvdata(&chip->dev);
108 	u16 len;
109 	int sig;
110 
111 	if (!ibmvtpm->rtce_buf) {
112 		dev_err(ibmvtpm->dev, "ibmvtpm device is not ready\n");
113 		return 0;
114 	}
115 
116 	sig = wait_event_interruptible(ibmvtpm->wq, !ibmvtpm->tpm_processing_cmd);
117 	if (sig)
118 		return -EINTR;
119 
120 	len = ibmvtpm->res_len;
121 
122 	if (count < len) {
123 		dev_err(ibmvtpm->dev,
124 			"Invalid size in recv: count=%zd, crq_size=%d\n",
125 			count, len);
126 		return -EIO;
127 	}
128 
129 	spin_lock(&ibmvtpm->rtce_lock);
130 	memcpy((void *)buf, (void *)ibmvtpm->rtce_buf, len);
131 	memset(ibmvtpm->rtce_buf, 0, len);
132 	ibmvtpm->res_len = 0;
133 	spin_unlock(&ibmvtpm->rtce_lock);
134 	return len;
135 }
136 
137 /**
138  * tpm_ibmvtpm_send() - Send a TPM command
139  * @chip:	tpm chip struct
140  * @buf:	buffer contains data to send
141  * @count:	size of buffer
142  *
143  * Return:
144  *   0 on success,
145  *   -errno on error
146  */
147 static int tpm_ibmvtpm_send(struct tpm_chip *chip, u8 *buf, size_t count)
148 {
149 	struct ibmvtpm_dev *ibmvtpm = dev_get_drvdata(&chip->dev);
150 	int rc, sig;
151 
152 	if (!ibmvtpm->rtce_buf) {
153 		dev_err(ibmvtpm->dev, "ibmvtpm device is not ready\n");
154 		return 0;
155 	}
156 
157 	if (count > ibmvtpm->rtce_size) {
158 		dev_err(ibmvtpm->dev,
159 			"Invalid size in send: count=%zd, rtce_size=%d\n",
160 			count, ibmvtpm->rtce_size);
161 		return -EIO;
162 	}
163 
164 	if (ibmvtpm->tpm_processing_cmd) {
165 		dev_info(ibmvtpm->dev,
166 		         "Need to wait for TPM to finish\n");
167 		/* wait for previous command to finish */
168 		sig = wait_event_interruptible(ibmvtpm->wq, !ibmvtpm->tpm_processing_cmd);
169 		if (sig)
170 			return -EINTR;
171 	}
172 
173 	spin_lock(&ibmvtpm->rtce_lock);
174 	ibmvtpm->res_len = 0;
175 	memcpy((void *)ibmvtpm->rtce_buf, (void *)buf, count);
176 
177 	/*
178 	 * set the processing flag before the Hcall, since we may get the
179 	 * result (interrupt) before even being able to check rc.
180 	 */
181 	ibmvtpm->tpm_processing_cmd = true;
182 
183 	rc = ibmvtpm_send_crq(ibmvtpm->vdev,
184 			IBMVTPM_VALID_CMD, VTPM_TPM_COMMAND,
185 			count, ibmvtpm->rtce_dma_handle);
186 	if (rc != H_SUCCESS) {
187 		dev_err(ibmvtpm->dev, "tpm_ibmvtpm_send failed rc=%d\n", rc);
188 		rc = 0;
189 		ibmvtpm->tpm_processing_cmd = false;
190 	} else
191 		rc = 0;
192 
193 	spin_unlock(&ibmvtpm->rtce_lock);
194 	return rc;
195 }
196 
197 static void tpm_ibmvtpm_cancel(struct tpm_chip *chip)
198 {
199 	return;
200 }
201 
202 static u8 tpm_ibmvtpm_status(struct tpm_chip *chip)
203 {
204 	return 0;
205 }
206 
207 /**
208  * ibmvtpm_crq_get_rtce_size - Send a CRQ request to get rtce size
209  *
210  * @ibmvtpm:	vtpm device struct
211  *
212  * Return:
213  *	0 on success.
214  *	Non-zero on failure.
215  */
216 static int ibmvtpm_crq_get_rtce_size(struct ibmvtpm_dev *ibmvtpm)
217 {
218 	int rc;
219 
220 	rc = ibmvtpm_send_crq(ibmvtpm->vdev,
221 			IBMVTPM_VALID_CMD, VTPM_GET_RTCE_BUFFER_SIZE, 0, 0);
222 	if (rc != H_SUCCESS)
223 		dev_err(ibmvtpm->dev,
224 			"ibmvtpm_crq_get_rtce_size failed rc=%d\n", rc);
225 
226 	return rc;
227 }
228 
229 /**
230  * ibmvtpm_crq_get_version - Send a CRQ request to get vtpm version
231  *			   - Note that this is vtpm version and not tpm version
232  *
233  * @ibmvtpm:	vtpm device struct
234  *
235  * Return:
236  *	0 on success.
237  *	Non-zero on failure.
238  */
239 static int ibmvtpm_crq_get_version(struct ibmvtpm_dev *ibmvtpm)
240 {
241 	int rc;
242 
243 	rc = ibmvtpm_send_crq(ibmvtpm->vdev,
244 			IBMVTPM_VALID_CMD, VTPM_GET_VERSION, 0, 0);
245 	if (rc != H_SUCCESS)
246 		dev_err(ibmvtpm->dev,
247 			"ibmvtpm_crq_get_version failed rc=%d\n", rc);
248 
249 	return rc;
250 }
251 
252 /**
253  * ibmvtpm_crq_send_init_complete - Send a CRQ initialize complete message
254  * @ibmvtpm:	vtpm device struct
255  *
256  * Return:
257  *	0 on success.
258  *	Non-zero on failure.
259  */
260 static int ibmvtpm_crq_send_init_complete(struct ibmvtpm_dev *ibmvtpm)
261 {
262 	int rc;
263 
264 	rc = ibmvtpm_send_crq_word(ibmvtpm->vdev, INIT_CRQ_COMP_CMD);
265 	if (rc != H_SUCCESS)
266 		dev_err(ibmvtpm->dev,
267 			"ibmvtpm_crq_send_init_complete failed rc=%d\n", rc);
268 
269 	return rc;
270 }
271 
272 /**
273  * ibmvtpm_crq_send_init - Send a CRQ initialize message
274  * @ibmvtpm:	vtpm device struct
275  *
276  * Return:
277  *	0 on success.
278  *	Non-zero on failure.
279  */
280 static int ibmvtpm_crq_send_init(struct ibmvtpm_dev *ibmvtpm)
281 {
282 	int rc;
283 
284 	rc = ibmvtpm_send_crq_word(ibmvtpm->vdev, INIT_CRQ_CMD);
285 	if (rc != H_SUCCESS)
286 		dev_err(ibmvtpm->dev,
287 			"ibmvtpm_crq_send_init failed rc=%d\n", rc);
288 
289 	return rc;
290 }
291 
292 /**
293  * tpm_ibmvtpm_remove - ibm vtpm remove entry point
294  * @vdev:	vio device struct
295  *
296  * Return: Always 0.
297  */
298 static int tpm_ibmvtpm_remove(struct vio_dev *vdev)
299 {
300 	struct tpm_chip *chip = dev_get_drvdata(&vdev->dev);
301 	struct ibmvtpm_dev *ibmvtpm = dev_get_drvdata(&chip->dev);
302 	int rc = 0;
303 
304 	tpm_chip_unregister(chip);
305 
306 	free_irq(vdev->irq, ibmvtpm);
307 
308 	do {
309 		if (rc)
310 			msleep(100);
311 		rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
312 	} while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
313 
314 	dma_unmap_single(ibmvtpm->dev, ibmvtpm->crq_dma_handle,
315 			 CRQ_RES_BUF_SIZE, DMA_BIDIRECTIONAL);
316 	free_page((unsigned long)ibmvtpm->crq_queue.crq_addr);
317 
318 	if (ibmvtpm->rtce_buf) {
319 		dma_unmap_single(ibmvtpm->dev, ibmvtpm->rtce_dma_handle,
320 				 ibmvtpm->rtce_size, DMA_BIDIRECTIONAL);
321 		kfree(ibmvtpm->rtce_buf);
322 	}
323 
324 	kfree(ibmvtpm);
325 	/* For tpm_ibmvtpm_get_desired_dma */
326 	dev_set_drvdata(&vdev->dev, NULL);
327 
328 	return 0;
329 }
330 
331 /**
332  * tpm_ibmvtpm_get_desired_dma - Get DMA size needed by this driver
333  * @vdev:	vio device struct
334  *
335  * Return:
336  *	Number of bytes the driver needs to DMA map.
337  */
338 static unsigned long tpm_ibmvtpm_get_desired_dma(struct vio_dev *vdev)
339 {
340 	struct tpm_chip *chip = dev_get_drvdata(&vdev->dev);
341 	struct ibmvtpm_dev *ibmvtpm;
342 
343 	/*
344 	 * ibmvtpm initializes at probe time, so the data we are
345 	 * asking for may not be set yet. Estimate that 4K required
346 	 * for TCE-mapped buffer in addition to CRQ.
347 	 */
348 	if (chip)
349 		ibmvtpm = dev_get_drvdata(&chip->dev);
350 	else
351 		return CRQ_RES_BUF_SIZE + PAGE_SIZE;
352 
353 	return CRQ_RES_BUF_SIZE + ibmvtpm->rtce_size;
354 }
355 
356 /**
357  * tpm_ibmvtpm_suspend - Suspend
358  * @dev:	device struct
359  *
360  * Return: Always 0.
361  */
362 static int tpm_ibmvtpm_suspend(struct device *dev)
363 {
364 	struct tpm_chip *chip = dev_get_drvdata(dev);
365 	struct ibmvtpm_dev *ibmvtpm = dev_get_drvdata(&chip->dev);
366 	int rc = 0;
367 
368 	rc = ibmvtpm_send_crq(ibmvtpm->vdev,
369 			IBMVTPM_VALID_CMD, VTPM_PREPARE_TO_SUSPEND, 0, 0);
370 	if (rc != H_SUCCESS)
371 		dev_err(ibmvtpm->dev,
372 			"tpm_ibmvtpm_suspend failed rc=%d\n", rc);
373 
374 	return rc;
375 }
376 
377 /**
378  * ibmvtpm_reset_crq - Reset CRQ
379  *
380  * @ibmvtpm:	ibm vtpm struct
381  *
382  * Return:
383  *	0 on success.
384  *	Non-zero on failure.
385  */
386 static int ibmvtpm_reset_crq(struct ibmvtpm_dev *ibmvtpm)
387 {
388 	int rc = 0;
389 
390 	do {
391 		if (rc)
392 			msleep(100);
393 		rc = plpar_hcall_norets(H_FREE_CRQ,
394 					ibmvtpm->vdev->unit_address);
395 	} while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
396 
397 	memset(ibmvtpm->crq_queue.crq_addr, 0, CRQ_RES_BUF_SIZE);
398 	ibmvtpm->crq_queue.index = 0;
399 
400 	return plpar_hcall_norets(H_REG_CRQ, ibmvtpm->vdev->unit_address,
401 				  ibmvtpm->crq_dma_handle, CRQ_RES_BUF_SIZE);
402 }
403 
404 /**
405  * tpm_ibmvtpm_resume - Resume from suspend
406  *
407  * @dev:	device struct
408  *
409  * Return: Always 0.
410  */
411 static int tpm_ibmvtpm_resume(struct device *dev)
412 {
413 	struct tpm_chip *chip = dev_get_drvdata(dev);
414 	struct ibmvtpm_dev *ibmvtpm = dev_get_drvdata(&chip->dev);
415 	int rc = 0;
416 
417 	do {
418 		if (rc)
419 			msleep(100);
420 		rc = plpar_hcall_norets(H_ENABLE_CRQ,
421 					ibmvtpm->vdev->unit_address);
422 	} while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
423 
424 	if (rc) {
425 		dev_err(dev, "Error enabling ibmvtpm rc=%d\n", rc);
426 		return rc;
427 	}
428 
429 	rc = vio_enable_interrupts(ibmvtpm->vdev);
430 	if (rc) {
431 		dev_err(dev, "Error vio_enable_interrupts rc=%d\n", rc);
432 		return rc;
433 	}
434 
435 	rc = ibmvtpm_crq_send_init(ibmvtpm);
436 	if (rc)
437 		dev_err(dev, "Error send_init rc=%d\n", rc);
438 
439 	return rc;
440 }
441 
442 static bool tpm_ibmvtpm_req_canceled(struct tpm_chip *chip, u8 status)
443 {
444 	return (status == 0);
445 }
446 
447 static const struct tpm_class_ops tpm_ibmvtpm = {
448 	.recv = tpm_ibmvtpm_recv,
449 	.send = tpm_ibmvtpm_send,
450 	.cancel = tpm_ibmvtpm_cancel,
451 	.status = tpm_ibmvtpm_status,
452 	.req_complete_mask = 0,
453 	.req_complete_val = 0,
454 	.req_canceled = tpm_ibmvtpm_req_canceled,
455 };
456 
457 static const struct dev_pm_ops tpm_ibmvtpm_pm_ops = {
458 	.suspend = tpm_ibmvtpm_suspend,
459 	.resume = tpm_ibmvtpm_resume,
460 };
461 
462 /**
463  * ibmvtpm_crq_get_next - Get next responded crq
464  *
465  * @ibmvtpm:	vtpm device struct
466  *
467  * Return: vtpm crq pointer or NULL.
468  */
469 static struct ibmvtpm_crq *ibmvtpm_crq_get_next(struct ibmvtpm_dev *ibmvtpm)
470 {
471 	struct ibmvtpm_crq_queue *crq_q = &ibmvtpm->crq_queue;
472 	struct ibmvtpm_crq *crq = &crq_q->crq_addr[crq_q->index];
473 
474 	if (crq->valid & VTPM_MSG_RES) {
475 		if (++crq_q->index == crq_q->num_entry)
476 			crq_q->index = 0;
477 		smp_rmb();
478 	} else
479 		crq = NULL;
480 	return crq;
481 }
482 
483 /**
484  * ibmvtpm_crq_process - Process responded crq
485  *
486  * @crq:	crq to be processed
487  * @ibmvtpm:	vtpm device struct
488  *
489  */
490 static void ibmvtpm_crq_process(struct ibmvtpm_crq *crq,
491 				struct ibmvtpm_dev *ibmvtpm)
492 {
493 	int rc = 0;
494 
495 	switch (crq->valid) {
496 	case VALID_INIT_CRQ:
497 		switch (crq->msg) {
498 		case INIT_CRQ_RES:
499 			dev_info(ibmvtpm->dev, "CRQ initialized\n");
500 			rc = ibmvtpm_crq_send_init_complete(ibmvtpm);
501 			if (rc)
502 				dev_err(ibmvtpm->dev, "Unable to send CRQ init complete rc=%d\n", rc);
503 			return;
504 		case INIT_CRQ_COMP_RES:
505 			dev_info(ibmvtpm->dev,
506 				 "CRQ initialization completed\n");
507 			return;
508 		default:
509 			dev_err(ibmvtpm->dev, "Unknown crq message type: %d\n", crq->msg);
510 			return;
511 		}
512 	case IBMVTPM_VALID_CMD:
513 		switch (crq->msg) {
514 		case VTPM_GET_RTCE_BUFFER_SIZE_RES:
515 			if (be16_to_cpu(crq->len) <= 0) {
516 				dev_err(ibmvtpm->dev, "Invalid rtce size\n");
517 				return;
518 			}
519 			ibmvtpm->rtce_size = be16_to_cpu(crq->len);
520 			ibmvtpm->rtce_buf = kmalloc(ibmvtpm->rtce_size,
521 						    GFP_ATOMIC);
522 			if (!ibmvtpm->rtce_buf) {
523 				dev_err(ibmvtpm->dev, "Failed to allocate memory for rtce buffer\n");
524 				return;
525 			}
526 
527 			ibmvtpm->rtce_dma_handle = dma_map_single(ibmvtpm->dev,
528 				ibmvtpm->rtce_buf, ibmvtpm->rtce_size,
529 				DMA_BIDIRECTIONAL);
530 
531 			if (dma_mapping_error(ibmvtpm->dev,
532 					      ibmvtpm->rtce_dma_handle)) {
533 				kfree(ibmvtpm->rtce_buf);
534 				ibmvtpm->rtce_buf = NULL;
535 				dev_err(ibmvtpm->dev, "Failed to dma map rtce buffer\n");
536 			}
537 
538 			return;
539 		case VTPM_GET_VERSION_RES:
540 			ibmvtpm->vtpm_version = be32_to_cpu(crq->data);
541 			return;
542 		case VTPM_TPM_COMMAND_RES:
543 			/* len of the data in rtce buffer */
544 			ibmvtpm->res_len = be16_to_cpu(crq->len);
545 			ibmvtpm->tpm_processing_cmd = false;
546 			wake_up_interruptible(&ibmvtpm->wq);
547 			return;
548 		default:
549 			return;
550 		}
551 	}
552 	return;
553 }
554 
555 /**
556  * ibmvtpm_interrupt -	Interrupt handler
557  *
558  * @irq:		irq number to handle
559  * @vtpm_instance:	vtpm that received interrupt
560  *
561  * Returns:
562  *	IRQ_HANDLED
563  **/
564 static irqreturn_t ibmvtpm_interrupt(int irq, void *vtpm_instance)
565 {
566 	struct ibmvtpm_dev *ibmvtpm = (struct ibmvtpm_dev *) vtpm_instance;
567 	struct ibmvtpm_crq *crq;
568 
569 	/* while loop is needed for initial setup (get version and
570 	 * get rtce_size). There should be only one tpm request at any
571 	 * given time.
572 	 */
573 	while ((crq = ibmvtpm_crq_get_next(ibmvtpm)) != NULL) {
574 		ibmvtpm_crq_process(crq, ibmvtpm);
575 		wake_up_interruptible(&ibmvtpm->crq_queue.wq);
576 		crq->valid = 0;
577 		smp_wmb();
578 	}
579 
580 	return IRQ_HANDLED;
581 }
582 
583 /**
584  * tpm_ibmvtpm_probe - ibm vtpm initialize entry point
585  *
586  * @vio_dev:	vio device struct
587  * @id:		vio device id struct
588  *
589  * Return:
590  *	0 on success.
591  *	Non-zero on failure.
592  */
593 static int tpm_ibmvtpm_probe(struct vio_dev *vio_dev,
594 				   const struct vio_device_id *id)
595 {
596 	struct ibmvtpm_dev *ibmvtpm;
597 	struct device *dev = &vio_dev->dev;
598 	struct ibmvtpm_crq_queue *crq_q;
599 	struct tpm_chip *chip;
600 	int rc = -ENOMEM, rc1;
601 
602 	chip = tpmm_chip_alloc(dev, &tpm_ibmvtpm);
603 	if (IS_ERR(chip))
604 		return PTR_ERR(chip);
605 
606 	ibmvtpm = kzalloc(sizeof(struct ibmvtpm_dev), GFP_KERNEL);
607 	if (!ibmvtpm) {
608 		dev_err(dev, "kzalloc for ibmvtpm failed\n");
609 		goto cleanup;
610 	}
611 
612 	ibmvtpm->dev = dev;
613 	ibmvtpm->vdev = vio_dev;
614 
615 	crq_q = &ibmvtpm->crq_queue;
616 	crq_q->crq_addr = (struct ibmvtpm_crq *)get_zeroed_page(GFP_KERNEL);
617 	if (!crq_q->crq_addr) {
618 		dev_err(dev, "Unable to allocate memory for crq_addr\n");
619 		goto cleanup;
620 	}
621 
622 	crq_q->num_entry = CRQ_RES_BUF_SIZE / sizeof(*crq_q->crq_addr);
623 	init_waitqueue_head(&crq_q->wq);
624 	ibmvtpm->crq_dma_handle = dma_map_single(dev, crq_q->crq_addr,
625 						 CRQ_RES_BUF_SIZE,
626 						 DMA_BIDIRECTIONAL);
627 
628 	if (dma_mapping_error(dev, ibmvtpm->crq_dma_handle)) {
629 		dev_err(dev, "dma mapping failed\n");
630 		goto cleanup;
631 	}
632 
633 	rc = plpar_hcall_norets(H_REG_CRQ, vio_dev->unit_address,
634 				ibmvtpm->crq_dma_handle, CRQ_RES_BUF_SIZE);
635 	if (rc == H_RESOURCE)
636 		rc = ibmvtpm_reset_crq(ibmvtpm);
637 
638 	if (rc) {
639 		dev_err(dev, "Unable to register CRQ rc=%d\n", rc);
640 		goto reg_crq_cleanup;
641 	}
642 
643 	rc = request_irq(vio_dev->irq, ibmvtpm_interrupt, 0,
644 			 tpm_ibmvtpm_driver_name, ibmvtpm);
645 	if (rc) {
646 		dev_err(dev, "Error %d register irq 0x%x\n", rc, vio_dev->irq);
647 		goto init_irq_cleanup;
648 	}
649 
650 	rc = vio_enable_interrupts(vio_dev);
651 	if (rc) {
652 		dev_err(dev, "Error %d enabling interrupts\n", rc);
653 		goto init_irq_cleanup;
654 	}
655 
656 	init_waitqueue_head(&ibmvtpm->wq);
657 
658 	crq_q->index = 0;
659 
660 	dev_set_drvdata(&chip->dev, ibmvtpm);
661 
662 	spin_lock_init(&ibmvtpm->rtce_lock);
663 
664 	rc = ibmvtpm_crq_send_init(ibmvtpm);
665 	if (rc)
666 		goto init_irq_cleanup;
667 
668 	rc = ibmvtpm_crq_get_version(ibmvtpm);
669 	if (rc)
670 		goto init_irq_cleanup;
671 
672 	rc = ibmvtpm_crq_get_rtce_size(ibmvtpm);
673 	if (rc)
674 		goto init_irq_cleanup;
675 
676 	if (!strcmp(id->compat, "IBM,vtpm20")) {
677 		chip->flags |= TPM_CHIP_FLAG_TPM2;
678 		rc = tpm2_get_cc_attrs_tbl(chip);
679 		if (rc)
680 			goto init_irq_cleanup;
681 	}
682 
683 	if (!wait_event_timeout(ibmvtpm->crq_queue.wq,
684 				ibmvtpm->rtce_buf != NULL,
685 				HZ)) {
686 		dev_err(dev, "CRQ response timed out\n");
687 		goto init_irq_cleanup;
688 	}
689 
690 	return tpm_chip_register(chip);
691 init_irq_cleanup:
692 	do {
693 		rc1 = plpar_hcall_norets(H_FREE_CRQ, vio_dev->unit_address);
694 	} while (rc1 == H_BUSY || H_IS_LONG_BUSY(rc1));
695 reg_crq_cleanup:
696 	dma_unmap_single(dev, ibmvtpm->crq_dma_handle, CRQ_RES_BUF_SIZE,
697 			 DMA_BIDIRECTIONAL);
698 cleanup:
699 	if (ibmvtpm) {
700 		if (crq_q->crq_addr)
701 			free_page((unsigned long)crq_q->crq_addr);
702 		kfree(ibmvtpm);
703 	}
704 
705 	return rc;
706 }
707 
708 static struct vio_driver ibmvtpm_driver = {
709 	.id_table	 = tpm_ibmvtpm_device_table,
710 	.probe		 = tpm_ibmvtpm_probe,
711 	.remove		 = tpm_ibmvtpm_remove,
712 	.get_desired_dma = tpm_ibmvtpm_get_desired_dma,
713 	.name		 = tpm_ibmvtpm_driver_name,
714 	.pm		 = &tpm_ibmvtpm_pm_ops,
715 };
716 
717 /**
718  * ibmvtpm_module_init - Initialize ibm vtpm module.
719  *
720  *
721  * Return:
722  *	0 on success.
723  *	Non-zero on failure.
724  */
725 static int __init ibmvtpm_module_init(void)
726 {
727 	return vio_register_driver(&ibmvtpm_driver);
728 }
729 
730 /**
731  * ibmvtpm_module_exit - Tear down ibm vtpm module.
732  */
733 static void __exit ibmvtpm_module_exit(void)
734 {
735 	vio_unregister_driver(&ibmvtpm_driver);
736 }
737 
738 module_init(ibmvtpm_module_init);
739 module_exit(ibmvtpm_module_exit);
740 
741 MODULE_AUTHOR("adlai@us.ibm.com");
742 MODULE_DESCRIPTION("IBM vTPM Driver");
743 MODULE_VERSION("1.0");
744 MODULE_LICENSE("GPL");
745