From patchwork Wed Mar 9 17:39:28 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Berger X-Patchwork-Id: 8547911 Return-Path: X-Original-To: patchwork-tpmdd-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 3BFE09F38C for ; Wed, 9 Mar 2016 17:39:51 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id EBA94202DD for ; Wed, 9 Mar 2016 17:39:49 +0000 (UTC) Received: from lists.sourceforge.net (lists.sourceforge.net [216.34.181.88]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id B5659202E5 for ; Wed, 9 Mar 2016 17:39:48 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=sfs-ml-4.v29.ch3.sourceforge.com) by sfs-ml-4.v29.ch3.sourceforge.com with esmtp (Exim 4.76) (envelope-from ) id 1adi5J-0007NS-76; Wed, 09 Mar 2016 17:39:45 +0000 Received: from sog-mx-3.v43.ch3.sourceforge.com ([172.29.43.193] helo=mx.sourceforge.net) by sfs-ml-4.v29.ch3.sourceforge.com with esmtp (Exim 4.76) (envelope-from ) id 1adi5I-0007NK-8g for tpmdd-devel@lists.sourceforge.net; Wed, 09 Mar 2016 17:39:44 +0000 X-ACL-Warn: Received: from e19.ny.us.ibm.com ([129.33.205.209]) by sog-mx-3.v43.ch3.sourceforge.com with esmtps (TLSv1:AES256-SHA:256) (Exim 4.76) id 1adi5G-0000hj-F5 for tpmdd-devel@lists.sourceforge.net; Wed, 09 Mar 2016 17:39:44 +0000 Received: from localhost by e19.ny.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Wed, 9 Mar 2016 12:39:37 -0500 Received: from d01dlp02.pok.ibm.com (9.56.250.167) by e19.ny.us.ibm.com (146.89.104.206) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Wed, 9 Mar 2016 12:39:36 -0500 X-IBM-Helo: d01dlp02.pok.ibm.com X-IBM-MailFrom: stefanb@linux.vnet.ibm.com X-IBM-RcptTo: tpmdd-devel@lists.sourceforge.net Received: from b01cxnp23034.gho.pok.ibm.com (b01cxnp23034.gho.pok.ibm.com [9.57.198.29]) by d01dlp02.pok.ibm.com (Postfix) with ESMTP id 788526E8040 for ; Wed, 9 Mar 2016 12:26:26 -0500 (EST) Received: from d01av01.pok.ibm.com (d01av01.pok.ibm.com [9.56.224.215]) by b01cxnp23034.gho.pok.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id u29HdZD033161430 for ; Wed, 9 Mar 2016 17:39:35 GMT Received: from d01av01.pok.ibm.com (localhost [127.0.0.1]) by d01av01.pok.ibm.com (8.14.4/8.14.4/NCO v10.0 AVout) with ESMTP id u29HdZ14021249 for ; Wed, 9 Mar 2016 12:39:35 -0500 Received: from dhcp-9-2-140-43.watson.ibm.com (dhcp-9-2-140-28.watson.ibm.com [9.2.140.28]) by d01av01.pok.ibm.com (8.14.4/8.14.4/NCO v10.0 AVin) with ESMTP id u29HdW4L020852; Wed, 9 Mar 2016 12:39:35 -0500 From: Stefan Berger To: tpmdd-devel@lists.sourceforge.net Date: Wed, 9 Mar 2016 12:39:28 -0500 Message-Id: <1457545170-30120-10-git-send-email-stefanb@linux.vnet.ibm.com> X-Mailer: git-send-email 2.4.3 In-Reply-To: <1457545170-30120-1-git-send-email-stefanb@linux.vnet.ibm.com> References: <1457545170-30120-1-git-send-email-stefanb@linux.vnet.ibm.com> X-TM-AS-MML: disable X-Content-Scanned: Fidelis XPS MAILER x-cbid: 16030917-0057-0000-0000-000003A9051B X-Spam-Score: -0.0 (/) X-Headers-End: 1adi5G-0000hj-F5 Cc: linux-doc@vger.kernel.org, linux-api@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [tpmdd-devel] [PATCH v6 09/11] tpm: Initialize TPM and get durations and timeouts X-BeenThere: tpmdd-devel@lists.sourceforge.net X-Mailman-Version: 2.1.9 Precedence: list List-Id: Tpm Device Driver maintainance List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: tpmdd-devel-bounces@lists.sourceforge.net X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, HK_RANDOM_ENVFROM, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Add the retrieval of TPM 1.2 durations and timeouts. Since this requires the startup of the TPM, do this for TPM 1.2 and TPM 2. Signed-off-by: Stefan Berger CC: linux-kernel@vger.kernel.org CC: linux-doc@vger.kernel.org CC: linux-api@vger.kernel.org --- drivers/char/tpm/tpm_vtpm.c | 94 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 85 insertions(+), 9 deletions(-) diff --git a/drivers/char/tpm/tpm_vtpm.c b/drivers/char/tpm/tpm_vtpm.c index 0da311b..4f75248 100644 --- a/drivers/char/tpm/tpm_vtpm.c +++ b/drivers/char/tpm/tpm_vtpm.c @@ -45,8 +45,11 @@ struct vtpm_dev { size_t req_len; /* length of queued TPM request */ size_t resp_len; /* length of queued TPM response */ u8 buffer[TPM_BUFSIZE]; /* request/response buffer */ + + struct work_struct work; /* task that retrieves TPM timeouts */ }; +static struct workqueue_struct *workqueue; static void vtpm_delete_device(struct vtpm_dev *vtpm_dev); @@ -67,6 +70,15 @@ static ssize_t vtpm_fops_read(struct file *filp, char __user *buf, size_t len; int sig, rc; + mutex_lock(&vtpm_dev->buf_lock); + + if (!(vtpm_dev->state & STATE_OPENED_FLAG)) { + mutex_unlock(&vtpm_dev->buf_lock); + return -EPIPE; + } + + mutex_unlock(&vtpm_dev->buf_lock); + sig = wait_event_interruptible(vtpm_dev->wq, vtpm_dev->req_len != 0); if (sig) return -EINTR; @@ -110,6 +122,11 @@ static ssize_t vtpm_fops_write(struct file *filp, const char __user *buf, mutex_lock(&vtpm_dev->buf_lock); + if (!(vtpm_dev->state & STATE_OPENED_FLAG)) { + mutex_unlock(&vtpm_dev->buf_lock); + return -EPIPE; + } + if (count > sizeof(vtpm_dev->buffer) || !(vtpm_dev->state & STATE_WAIT_RESPONSE_FLAG)) { mutex_unlock(&vtpm_dev->buf_lock); @@ -154,6 +171,9 @@ static unsigned int vtpm_fops_poll(struct file *filp, poll_table *wait) if (vtpm_dev->req_len) ret |= POLLIN | POLLRDNORM; + if (!(vtpm_dev->state & STATE_OPENED_FLAG)) + ret |= POLLHUP; + mutex_unlock(&vtpm_dev->buf_lock); return ret; @@ -341,6 +361,54 @@ static const struct tpm_class_ops vtpm_tpm_ops = { }; /* + * Code related to the startup of the TPM 2 and startup of TPM 1.2 + + * retrieval of timeouts and durations. + */ + +static void vtpm_dev_work(struct work_struct *work) +{ + struct vtpm_dev *vtpm_dev = container_of(work, struct vtpm_dev, work); + int rc; + + if (vtpm_dev->flags & VTPM_FLAG_TPM2) + rc = tpm2_startup(vtpm_dev->chip, TPM2_SU_CLEAR); + else + rc = tpm_get_timeouts(vtpm_dev->chip); + + if (rc) + goto err; + + rc = tpm_chip_register(vtpm_dev->chip); + if (rc) + goto err; + + return; + +err: + vtpm_fops_undo_open(vtpm_dev); +} + +/* + * vtpm_dev_work_stop: make sure the work has finished + * + * This function is useful when user space closed the fd + * while the driver still determines timeouts. + */ +static void vtpm_dev_work_stop(struct vtpm_dev *vtpm_dev) +{ + vtpm_fops_undo_open(vtpm_dev); + flush_work(&vtpm_dev->work); +} + +/* + * vtpm_dev_work_start: Schedule the work for TPM 1.2 & 2 initialization + */ +static inline void vtpm_dev_work_start(struct vtpm_dev *vtpm_dev) +{ + queue_work(workqueue, &vtpm_dev->work); +} + +/* * Code related to creation and deletion of device pairs */ static struct vtpm_dev *vtpm_create_vtpm_dev(void) @@ -355,6 +423,7 @@ static struct vtpm_dev *vtpm_create_vtpm_dev(void) init_waitqueue_head(&vtpm_dev->wq); mutex_init(&vtpm_dev->buf_lock); + INIT_WORK(&vtpm_dev->work, vtpm_dev_work); chip = tpm_chip_alloc(NULL, &vtpm_tpm_ops); if (IS_ERR(chip)) { @@ -424,9 +493,7 @@ static struct file *vtpm_create_device( if (vtpm_dev->flags & VTPM_FLAG_TPM2) vtpm_dev->chip->flags |= TPM_CHIP_FLAG_TPM2; - rc = tpm_chip_register(vtpm_dev->chip); - if (rc) - goto err_vtpm_fput; + vtpm_dev_work_start(vtpm_dev); vtpm_new_dev->fd = fd; vtpm_new_dev->major = MAJOR(vtpm_dev->chip->dev.devt); @@ -435,12 +502,6 @@ static struct file *vtpm_create_device( return file; -err_vtpm_fput: - put_unused_fd(fd); - fput(file); - - return ERR_PTR(rc); - err_put_unused_fd: put_unused_fd(fd); @@ -455,6 +516,8 @@ err_delete_vtpm_dev: */ static void vtpm_delete_device(struct vtpm_dev *vtpm_dev) { + vtpm_dev_work_stop(vtpm_dev); + tpm_chip_unregister(vtpm_dev->chip); vtpm_fops_undo_open(vtpm_dev); @@ -549,11 +612,24 @@ static int __init vtpm_module_init(void) return rc; } + workqueue = create_workqueue("tpm-vtpm"); + if (!workqueue) { + pr_err("couldn't create workqueue\n"); + rc = -ENOMEM; + goto err_vtpmx_cleanup; + } + return 0; + +err_vtpmx_cleanup: + vtpmx_cleanup(); + + return rc; } static void __exit vtpm_module_exit(void) { + destroy_workqueue(workqueue); vtpmx_cleanup(); }