2 * Copyright (C) 2012 IBM Corporation
4 * Author: Ashley Lai <adlai@us.ibm.com>
6 * Maintained by: <tpmdd-devel@lists.sourceforge.net>
8 * Device driver for TCG/TCPA TPM (trusted platform module).
9 * Specifications at www.trustedcomputinggroup.org
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
18 #include <linux/dma-mapping.h>
19 #include <linux/dmapool.h>
20 #include <linux/slab.h>
23 #include <linux/types.h>
24 #include <linux/list.h>
25 #include <linux/spinlock.h>
26 #include <linux/interrupt.h>
27 #include <linux/wait.h>
31 #include "tpm_ibmvtpm.h"
33 static const char tpm_ibmvtpm_driver_name
[] = "tpm_ibmvtpm";
35 static struct vio_device_id tpm_ibmvtpm_device_table
[] = {
36 { "IBM,vtpm", "IBM,vtpm"},
39 MODULE_DEVICE_TABLE(vio
, tpm_ibmvtpm_device_table
);
42 * ibmvtpm_send_crq - Send a CRQ request
43 * @vdev: vio device struct
51 static int ibmvtpm_send_crq(struct vio_dev
*vdev
, u64 w1
, u64 w2
)
53 return plpar_hcall_norets(H_SEND_CRQ
, vdev
->unit_address
, w1
, w2
);
57 * ibmvtpm_get_data - Retrieve ibm vtpm data
63 static struct ibmvtpm_dev
*ibmvtpm_get_data(const struct device
*dev
)
65 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
67 return (struct ibmvtpm_dev
*)TPM_VPRIV(chip
);
72 * tpm_ibmvtpm_recv - Receive data after send
73 * @chip: tpm chip struct
74 * @buf: buffer to read
75 * count: size of buffer
78 * Number of bytes read
80 static int tpm_ibmvtpm_recv(struct tpm_chip
*chip
, u8
*buf
, size_t count
)
82 struct ibmvtpm_dev
*ibmvtpm
;
86 ibmvtpm
= (struct ibmvtpm_dev
*)TPM_VPRIV(chip
);
88 if (!ibmvtpm
->rtce_buf
) {
89 dev_err(ibmvtpm
->dev
, "ibmvtpm device is not ready\n");
93 sig
= wait_event_interruptible(ibmvtpm
->wq
, ibmvtpm
->res_len
!= 0);
97 len
= ibmvtpm
->res_len
;
100 dev_err(ibmvtpm
->dev
,
101 "Invalid size in recv: count=%ld, crq_size=%d\n",
106 spin_lock(&ibmvtpm
->rtce_lock
);
107 memcpy((void *)buf
, (void *)ibmvtpm
->rtce_buf
, len
);
108 memset(ibmvtpm
->rtce_buf
, 0, len
);
109 ibmvtpm
->res_len
= 0;
110 spin_unlock(&ibmvtpm
->rtce_lock
);
115 * tpm_ibmvtpm_send - Send tpm request
116 * @chip: tpm chip struct
117 * @buf: buffer contains data to send
118 * count: size of buffer
121 * Number of bytes sent
123 static int tpm_ibmvtpm_send(struct tpm_chip
*chip
, u8
*buf
, size_t count
)
125 struct ibmvtpm_dev
*ibmvtpm
;
126 struct ibmvtpm_crq crq
;
127 u64
*word
= (u64
*) &crq
;
130 ibmvtpm
= (struct ibmvtpm_dev
*)TPM_VPRIV(chip
);
132 if (!ibmvtpm
->rtce_buf
) {
133 dev_err(ibmvtpm
->dev
, "ibmvtpm device is not ready\n");
137 if (count
> ibmvtpm
->rtce_size
) {
138 dev_err(ibmvtpm
->dev
,
139 "Invalid size in send: count=%ld, rtce_size=%d\n",
140 count
, ibmvtpm
->rtce_size
);
144 spin_lock(&ibmvtpm
->rtce_lock
);
145 memcpy((void *)ibmvtpm
->rtce_buf
, (void *)buf
, count
);
146 crq
.valid
= (u8
)IBMVTPM_VALID_CMD
;
147 crq
.msg
= (u8
)VTPM_TPM_COMMAND
;
148 crq
.len
= (u16
)count
;
149 crq
.data
= ibmvtpm
->rtce_dma_handle
;
151 rc
= ibmvtpm_send_crq(ibmvtpm
->vdev
, word
[0], word
[1]);
152 if (rc
!= H_SUCCESS
) {
153 dev_err(ibmvtpm
->dev
, "tpm_ibmvtpm_send failed rc=%d\n", rc
);
158 spin_unlock(&ibmvtpm
->rtce_lock
);
162 static void tpm_ibmvtpm_cancel(struct tpm_chip
*chip
)
167 static u8
tpm_ibmvtpm_status(struct tpm_chip
*chip
)
173 * ibmvtpm_crq_get_rtce_size - Send a CRQ request to get rtce size
174 * @ibmvtpm: vtpm device struct
180 static int ibmvtpm_crq_get_rtce_size(struct ibmvtpm_dev
*ibmvtpm
)
182 struct ibmvtpm_crq crq
;
183 u64
*buf
= (u64
*) &crq
;
186 crq
.valid
= (u8
)IBMVTPM_VALID_CMD
;
187 crq
.msg
= (u8
)VTPM_GET_RTCE_BUFFER_SIZE
;
189 rc
= ibmvtpm_send_crq(ibmvtpm
->vdev
, buf
[0], buf
[1]);
191 dev_err(ibmvtpm
->dev
,
192 "ibmvtpm_crq_get_rtce_size failed rc=%d\n", rc
);
198 * ibmvtpm_crq_get_version - Send a CRQ request to get vtpm version
199 * - Note that this is vtpm version and not tpm version
200 * @ibmvtpm: vtpm device struct
206 static int ibmvtpm_crq_get_version(struct ibmvtpm_dev
*ibmvtpm
)
208 struct ibmvtpm_crq crq
;
209 u64
*buf
= (u64
*) &crq
;
212 crq
.valid
= (u8
)IBMVTPM_VALID_CMD
;
213 crq
.msg
= (u8
)VTPM_GET_VERSION
;
215 rc
= ibmvtpm_send_crq(ibmvtpm
->vdev
, buf
[0], buf
[1]);
217 dev_err(ibmvtpm
->dev
,
218 "ibmvtpm_crq_get_version failed rc=%d\n", rc
);
224 * ibmvtpm_crq_send_init_complete - Send a CRQ initialize complete message
225 * @ibmvtpm: vtpm device struct
231 static int ibmvtpm_crq_send_init_complete(struct ibmvtpm_dev
*ibmvtpm
)
235 rc
= ibmvtpm_send_crq(ibmvtpm
->vdev
, INIT_CRQ_COMP_CMD
, 0);
237 dev_err(ibmvtpm
->dev
,
238 "ibmvtpm_crq_send_init_complete failed rc=%d\n", rc
);
244 * ibmvtpm_crq_send_init - Send a CRQ initialize message
245 * @ibmvtpm: vtpm device struct
251 static int ibmvtpm_crq_send_init(struct ibmvtpm_dev
*ibmvtpm
)
255 rc
= ibmvtpm_send_crq(ibmvtpm
->vdev
, INIT_CRQ_CMD
, 0);
257 dev_err(ibmvtpm
->dev
,
258 "ibmvtpm_crq_send_init failed rc=%d\n", rc
);
264 * tpm_ibmvtpm_remove - ibm vtpm remove entry point
265 * @vdev: vio device struct
270 static int tpm_ibmvtpm_remove(struct vio_dev
*vdev
)
272 struct ibmvtpm_dev
*ibmvtpm
= ibmvtpm_get_data(&vdev
->dev
);
275 free_irq(vdev
->irq
, ibmvtpm
);
280 rc
= plpar_hcall_norets(H_FREE_CRQ
, vdev
->unit_address
);
281 } while (rc
== H_BUSY
|| H_IS_LONG_BUSY(rc
));
283 dma_unmap_single(ibmvtpm
->dev
, ibmvtpm
->crq_dma_handle
,
284 CRQ_RES_BUF_SIZE
, DMA_BIDIRECTIONAL
);
285 free_page((unsigned long)ibmvtpm
->crq_queue
.crq_addr
);
287 if (ibmvtpm
->rtce_buf
) {
288 dma_unmap_single(ibmvtpm
->dev
, ibmvtpm
->rtce_dma_handle
,
289 ibmvtpm
->rtce_size
, DMA_BIDIRECTIONAL
);
290 kfree(ibmvtpm
->rtce_buf
);
293 tpm_remove_hardware(ibmvtpm
->dev
);
301 * tpm_ibmvtpm_get_desired_dma - Get DMA size needed by this driver
302 * @vdev: vio device struct
305 * Number of bytes the driver needs to DMA map
307 static unsigned long tpm_ibmvtpm_get_desired_dma(struct vio_dev
*vdev
)
309 struct ibmvtpm_dev
*ibmvtpm
= ibmvtpm_get_data(&vdev
->dev
);
310 return CRQ_RES_BUF_SIZE
+ ibmvtpm
->rtce_size
;
314 * tpm_ibmvtpm_suspend - Suspend
315 * @dev: device struct
320 static int tpm_ibmvtpm_suspend(struct device
*dev
)
322 struct ibmvtpm_dev
*ibmvtpm
= ibmvtpm_get_data(dev
);
323 struct ibmvtpm_crq crq
;
324 u64
*buf
= (u64
*) &crq
;
327 crq
.valid
= (u8
)IBMVTPM_VALID_CMD
;
328 crq
.msg
= (u8
)VTPM_PREPARE_TO_SUSPEND
;
330 rc
= ibmvtpm_send_crq(ibmvtpm
->vdev
, buf
[0], buf
[1]);
332 dev_err(ibmvtpm
->dev
,
333 "tpm_ibmvtpm_suspend failed rc=%d\n", rc
);
339 * ibmvtpm_reset_crq - Reset CRQ
340 * @ibmvtpm: ibm vtpm struct
346 static int ibmvtpm_reset_crq(struct ibmvtpm_dev
*ibmvtpm
)
353 rc
= plpar_hcall_norets(H_FREE_CRQ
,
354 ibmvtpm
->vdev
->unit_address
);
355 } while (rc
== H_BUSY
|| H_IS_LONG_BUSY(rc
));
357 memset(ibmvtpm
->crq_queue
.crq_addr
, 0, CRQ_RES_BUF_SIZE
);
358 ibmvtpm
->crq_queue
.index
= 0;
360 return plpar_hcall_norets(H_REG_CRQ
, ibmvtpm
->vdev
->unit_address
,
361 ibmvtpm
->crq_dma_handle
, CRQ_RES_BUF_SIZE
);
365 * tpm_ibmvtpm_resume - Resume from suspend
366 * @dev: device struct
371 static int tpm_ibmvtpm_resume(struct device
*dev
)
373 struct ibmvtpm_dev
*ibmvtpm
= ibmvtpm_get_data(dev
);
379 rc
= plpar_hcall_norets(H_ENABLE_CRQ
,
380 ibmvtpm
->vdev
->unit_address
);
381 } while (rc
== H_IN_PROGRESS
|| rc
== H_BUSY
|| H_IS_LONG_BUSY(rc
));
384 dev_err(dev
, "Error enabling ibmvtpm rc=%d\n", rc
);
388 rc
= vio_enable_interrupts(ibmvtpm
->vdev
);
390 dev_err(dev
, "Error vio_enable_interrupts rc=%d\n", rc
);
394 rc
= ibmvtpm_crq_send_init(ibmvtpm
);
396 dev_err(dev
, "Error send_init rc=%d\n", rc
);
401 static bool tpm_ibmvtpm_req_canceled(struct tpm_chip
*chip
, u8 status
)
403 return (status
== 0);
406 static const struct file_operations ibmvtpm_ops
= {
407 .owner
= THIS_MODULE
,
412 .release
= tpm_release
,
415 static DEVICE_ATTR(pubek
, S_IRUGO
, tpm_show_pubek
, NULL
);
416 static DEVICE_ATTR(pcrs
, S_IRUGO
, tpm_show_pcrs
, NULL
);
417 static DEVICE_ATTR(enabled
, S_IRUGO
, tpm_show_enabled
, NULL
);
418 static DEVICE_ATTR(active
, S_IRUGO
, tpm_show_active
, NULL
);
419 static DEVICE_ATTR(owned
, S_IRUGO
, tpm_show_owned
, NULL
);
420 static DEVICE_ATTR(temp_deactivated
, S_IRUGO
, tpm_show_temp_deactivated
,
422 static DEVICE_ATTR(caps
, S_IRUGO
, tpm_show_caps_1_2
, NULL
);
423 static DEVICE_ATTR(cancel
, S_IWUSR
| S_IWGRP
, NULL
, tpm_store_cancel
);
424 static DEVICE_ATTR(durations
, S_IRUGO
, tpm_show_durations
, NULL
);
425 static DEVICE_ATTR(timeouts
, S_IRUGO
, tpm_show_timeouts
, NULL
);
427 static struct attribute
*ibmvtpm_attrs
[] = {
428 &dev_attr_pubek
.attr
,
430 &dev_attr_enabled
.attr
,
431 &dev_attr_active
.attr
,
432 &dev_attr_owned
.attr
,
433 &dev_attr_temp_deactivated
.attr
,
435 &dev_attr_cancel
.attr
,
436 &dev_attr_durations
.attr
,
437 &dev_attr_timeouts
.attr
, NULL
,
440 static struct attribute_group ibmvtpm_attr_grp
= { .attrs
= ibmvtpm_attrs
};
442 static const struct tpm_vendor_specific tpm_ibmvtpm
= {
443 .recv
= tpm_ibmvtpm_recv
,
444 .send
= tpm_ibmvtpm_send
,
445 .cancel
= tpm_ibmvtpm_cancel
,
446 .status
= tpm_ibmvtpm_status
,
447 .req_complete_mask
= 0,
448 .req_complete_val
= 0,
449 .req_canceled
= tpm_ibmvtpm_req_canceled
,
450 .attr_group
= &ibmvtpm_attr_grp
,
451 .miscdev
= { .fops
= &ibmvtpm_ops
, },
454 static const struct dev_pm_ops tpm_ibmvtpm_pm_ops
= {
455 .suspend
= tpm_ibmvtpm_suspend
,
456 .resume
= tpm_ibmvtpm_resume
,
460 * ibmvtpm_crq_get_next - Get next responded crq
461 * @ibmvtpm vtpm device struct
466 static struct ibmvtpm_crq
*ibmvtpm_crq_get_next(struct ibmvtpm_dev
*ibmvtpm
)
468 struct ibmvtpm_crq_queue
*crq_q
= &ibmvtpm
->crq_queue
;
469 struct ibmvtpm_crq
*crq
= &crq_q
->crq_addr
[crq_q
->index
];
471 if (crq
->valid
& VTPM_MSG_RES
) {
472 if (++crq_q
->index
== crq_q
->num_entry
)
481 * ibmvtpm_crq_process - Process responded crq
482 * @crq crq to be processed
483 * @ibmvtpm vtpm device struct
488 static void ibmvtpm_crq_process(struct ibmvtpm_crq
*crq
,
489 struct ibmvtpm_dev
*ibmvtpm
)
493 switch (crq
->valid
) {
497 dev_info(ibmvtpm
->dev
, "CRQ initialized\n");
498 rc
= ibmvtpm_crq_send_init_complete(ibmvtpm
);
500 dev_err(ibmvtpm
->dev
, "Unable to send CRQ init complete rc=%d\n", rc
);
502 case INIT_CRQ_COMP_RES
:
503 dev_info(ibmvtpm
->dev
,
504 "CRQ initialization completed\n");
507 dev_err(ibmvtpm
->dev
, "Unknown crq message type: %d\n", crq
->msg
);
511 case IBMVTPM_VALID_CMD
:
513 case VTPM_GET_RTCE_BUFFER_SIZE_RES
:
515 dev_err(ibmvtpm
->dev
, "Invalid rtce size\n");
518 ibmvtpm
->rtce_size
= crq
->len
;
519 ibmvtpm
->rtce_buf
= kmalloc(ibmvtpm
->rtce_size
,
521 if (!ibmvtpm
->rtce_buf
) {
522 dev_err(ibmvtpm
->dev
, "Failed to allocate memory for rtce buffer\n");
526 ibmvtpm
->rtce_dma_handle
= dma_map_single(ibmvtpm
->dev
,
527 ibmvtpm
->rtce_buf
, ibmvtpm
->rtce_size
,
530 if (dma_mapping_error(ibmvtpm
->dev
,
531 ibmvtpm
->rtce_dma_handle
)) {
532 kfree(ibmvtpm
->rtce_buf
);
533 ibmvtpm
->rtce_buf
= NULL
;
534 dev_err(ibmvtpm
->dev
, "Failed to dma map rtce buffer\n");
538 case VTPM_GET_VERSION_RES
:
539 ibmvtpm
->vtpm_version
= crq
->data
;
541 case VTPM_TPM_COMMAND_RES
:
542 /* len of the data in rtce buffer */
543 ibmvtpm
->res_len
= crq
->len
;
544 wake_up_interruptible(&ibmvtpm
->wq
);
554 * ibmvtpm_interrupt - Interrupt handler
555 * @irq: irq number to handle
556 * @vtpm_instance: vtpm that received interrupt
561 static irqreturn_t
ibmvtpm_interrupt(int irq
, void *vtpm_instance
)
563 struct ibmvtpm_dev
*ibmvtpm
= (struct ibmvtpm_dev
*) vtpm_instance
;
564 struct ibmvtpm_crq
*crq
;
566 /* while loop is needed for initial setup (get version and
567 * get rtce_size). There should be only one tpm request at any
570 while ((crq
= ibmvtpm_crq_get_next(ibmvtpm
)) != NULL
) {
571 ibmvtpm_crq_process(crq
, ibmvtpm
);
580 * tpm_ibmvtpm_probe - ibm vtpm initialize entry point
581 * @vio_dev: vio device struct
582 * @id: vio device id struct
588 static int tpm_ibmvtpm_probe(struct vio_dev
*vio_dev
,
589 const struct vio_device_id
*id
)
591 struct ibmvtpm_dev
*ibmvtpm
;
592 struct device
*dev
= &vio_dev
->dev
;
593 struct ibmvtpm_crq_queue
*crq_q
;
594 struct tpm_chip
*chip
;
595 int rc
= -ENOMEM
, rc1
;
597 chip
= tpm_register_hardware(dev
, &tpm_ibmvtpm
);
599 dev_err(dev
, "tpm_register_hardware failed\n");
603 ibmvtpm
= kzalloc(sizeof(struct ibmvtpm_dev
), GFP_KERNEL
);
605 dev_err(dev
, "kzalloc for ibmvtpm failed\n");
609 crq_q
= &ibmvtpm
->crq_queue
;
610 crq_q
->crq_addr
= (struct ibmvtpm_crq
*)get_zeroed_page(GFP_KERNEL
);
611 if (!crq_q
->crq_addr
) {
612 dev_err(dev
, "Unable to allocate memory for crq_addr\n");
616 crq_q
->num_entry
= CRQ_RES_BUF_SIZE
/ sizeof(*crq_q
->crq_addr
);
617 ibmvtpm
->crq_dma_handle
= dma_map_single(dev
, crq_q
->crq_addr
,
621 if (dma_mapping_error(dev
, ibmvtpm
->crq_dma_handle
)) {
622 dev_err(dev
, "dma mapping failed\n");
626 rc
= plpar_hcall_norets(H_REG_CRQ
, vio_dev
->unit_address
,
627 ibmvtpm
->crq_dma_handle
, CRQ_RES_BUF_SIZE
);
628 if (rc
== H_RESOURCE
)
629 rc
= ibmvtpm_reset_crq(ibmvtpm
);
632 dev_err(dev
, "Unable to register CRQ rc=%d\n", rc
);
633 goto reg_crq_cleanup
;
636 rc
= request_irq(vio_dev
->irq
, ibmvtpm_interrupt
, 0,
637 tpm_ibmvtpm_driver_name
, ibmvtpm
);
639 dev_err(dev
, "Error %d register irq 0x%x\n", rc
, vio_dev
->irq
);
640 goto init_irq_cleanup
;
643 rc
= vio_enable_interrupts(vio_dev
);
645 dev_err(dev
, "Error %d enabling interrupts\n", rc
);
646 goto init_irq_cleanup
;
649 init_waitqueue_head(&ibmvtpm
->wq
);
654 ibmvtpm
->vdev
= vio_dev
;
655 TPM_VPRIV(chip
) = (void *)ibmvtpm
;
657 spin_lock_init(&ibmvtpm
->rtce_lock
);
659 rc
= ibmvtpm_crq_send_init(ibmvtpm
);
661 goto init_irq_cleanup
;
663 rc
= ibmvtpm_crq_get_version(ibmvtpm
);
665 goto init_irq_cleanup
;
667 rc
= ibmvtpm_crq_get_rtce_size(ibmvtpm
);
669 goto init_irq_cleanup
;
674 rc1
= plpar_hcall_norets(H_FREE_CRQ
, vio_dev
->unit_address
);
675 } while (rc1
== H_BUSY
|| H_IS_LONG_BUSY(rc1
));
677 dma_unmap_single(dev
, ibmvtpm
->crq_dma_handle
, CRQ_RES_BUF_SIZE
,
682 free_page((unsigned long)crq_q
->crq_addr
);
686 tpm_remove_hardware(dev
);
691 static struct vio_driver ibmvtpm_driver
= {
692 .id_table
= tpm_ibmvtpm_device_table
,
693 .probe
= tpm_ibmvtpm_probe
,
694 .remove
= tpm_ibmvtpm_remove
,
695 .get_desired_dma
= tpm_ibmvtpm_get_desired_dma
,
696 .name
= tpm_ibmvtpm_driver_name
,
697 .pm
= &tpm_ibmvtpm_pm_ops
,
701 * ibmvtpm_module_init - Initialize ibm vtpm module
707 static int __init
ibmvtpm_module_init(void)
709 return vio_register_driver(&ibmvtpm_driver
);
713 * ibmvtpm_module_exit - Teardown ibm vtpm module
718 static void __exit
ibmvtpm_module_exit(void)
720 vio_unregister_driver(&ibmvtpm_driver
);
723 module_init(ibmvtpm_module_init
);
724 module_exit(ibmvtpm_module_exit
);
726 MODULE_AUTHOR("adlai@us.ibm.com");
727 MODULE_DESCRIPTION("IBM vTPM Driver");
728 MODULE_VERSION("1.0");
729 MODULE_LICENSE("GPL");