From patchwork Wed May 10 23:54:21 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Berger X-Patchwork-Id: 760884 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org 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 ozlabs.org (Postfix) with ESMTPS id 3wNY4h3Br7z9s8F for ; Thu, 11 May 2017 09:54:44 +1000 (AEST) Received: from localhost ([127.0.0.1] helo=sfs-ml-1.v29.ch3.sourceforge.com) by sfs-ml-1.v29.ch3.sourceforge.com with esmtp (Exim 4.76) (envelope-from ) id 1d8bRH-0005GW-1Q; Wed, 10 May 2017 23:54:39 +0000 Received: from sog-mx-1.v43.ch3.sourceforge.com ([172.29.43.191] helo=mx.sourceforge.net) by sfs-ml-1.v29.ch3.sourceforge.com with esmtp (Exim 4.76) (envelope-from ) id 1d8bRF-0005GL-Cv for tpmdd-devel@lists.sourceforge.net; Wed, 10 May 2017 23:54:37 +0000 X-ACL-Warn: Received: from mx0b-001b2d01.pphosted.com ([148.163.158.5] helo=mx0a-001b2d01.pphosted.com) by sog-mx-1.v43.ch3.sourceforge.com with esmtps (TLSv1:AES256-SHA:256) (Exim 4.76) id 1d8bRE-0002ZV-G4 for tpmdd-devel@lists.sourceforge.net; Wed, 10 May 2017 23:54:37 +0000 Received: from pps.filterd (m0098414.ppops.net [127.0.0.1]) by mx0b-001b2d01.pphosted.com (8.16.0.20/8.16.0.20) with SMTP id v4ANn1mP008267 for ; Wed, 10 May 2017 19:54:30 -0400 Received: from e34.co.us.ibm.com (e34.co.us.ibm.com [32.97.110.152]) by mx0b-001b2d01.pphosted.com with ESMTP id 2abqg56kmp-1 (version=TLSv1.2 cipher=AES256-SHA bits=256 verify=NOT) for ; Wed, 10 May 2017 19:54:30 -0400 Received: from localhost by e34.co.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Wed, 10 May 2017 17:54:30 -0600 Received: from b03cxnp08025.gho.boulder.ibm.com (9.17.130.17) by e34.co.us.ibm.com (192.168.1.134) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Wed, 10 May 2017 17:54:27 -0600 Received: from b03ledav001.gho.boulder.ibm.com (b03ledav001.gho.boulder.ibm.com [9.17.130.232]) by b03cxnp08025.gho.boulder.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id v4ANsRsX9634086; Wed, 10 May 2017 16:54:27 -0700 Received: from b03ledav001.gho.boulder.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 35ECC6E038; Wed, 10 May 2017 17:54:27 -0600 (MDT) Received: from sbct-3.watson.ibm.com (unknown [9.2.141.158]) by b03ledav001.gho.boulder.ibm.com (Postfix) with ESMTP id A498D6E035; Wed, 10 May 2017 17:54:26 -0600 (MDT) From: Stefan Berger To: tpmdd-devel@lists.sourceforge.net, linux-security-module@vger.kernel.org, jarkko.sakkinen@linux.intel.com Date: Wed, 10 May 2017 19:54:21 -0400 X-Mailer: git-send-email 2.5.5 In-Reply-To: <1494460462-29022-1-git-send-email-stefanb@linux.vnet.ibm.com> References: <1494460462-29022-1-git-send-email-stefanb@linux.vnet.ibm.com> X-TM-AS-GCONF: 00 x-cbid: 17051023-0016-0000-0000-000006B75D1E X-IBM-SpamModules-Scores: X-IBM-SpamModules-Versions: BY=3.00007044; HX=3.00000240; KW=3.00000007; PH=3.00000004; SC=3.00000210; SDB=6.00858879; UDB=6.00425616; IPR=6.00638355; BA=6.00005343; NDR=6.00000001; ZLA=6.00000005; ZF=6.00000009; ZB=6.00000000; ZP=6.00000000; ZH=6.00000000; ZU=6.00000002; MB=3.00015403; XFM=3.00000015; UTC=2017-05-10 23:54:29 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 17051023-0017-0000-0000-0000399618D4 Message-Id: <1494460462-29022-2-git-send-email-stefanb@linux.vnet.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:, , definitions=2017-05-10_20:, , signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 spamscore=0 suspectscore=0 malwarescore=0 phishscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1703280000 definitions=main-1705100161 X-Spam-Score: 0.0 (/) X-Spam-Report: Spam Filtering performed by mx.sourceforge.net. See http://spamassassin.org/tag/ for more details. X-Headers-End: 1d8bRE-0002ZV-G4 Cc: linux-kernel@vger.kernel.org Subject: [tpmdd-devel] [PATCH v4 1/2] tpm: Refactor tpm_transmit pulling out tpm_transfer function 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 Refactor tpm_transmit and pull out code sending the command and receiving the response and put this into tpm_transfer. Signed-off-by: Stefan Berger --- drivers/char/tpm/tpm-interface.c | 121 +++++++++++++++++++++++---------------- 1 file changed, 73 insertions(+), 48 deletions(-) diff --git a/drivers/char/tpm/tpm-interface.c b/drivers/char/tpm/tpm-interface.c index 158c1db..263b6d1 100644 --- a/drivers/char/tpm/tpm-interface.c +++ b/drivers/char/tpm/tpm-interface.c @@ -370,67 +370,29 @@ static bool tpm_validate_command(struct tpm_chip *chip, } /** - * tmp_transmit - Internal kernel interface to transmit TPM commands. + * tmp_transfer - Send a TPM command to the TPM and receive response * * @chip: TPM chip to use * @buf: TPM command buffer + * @count: size of the TPM command * @bufsiz: length of the TPM command buffer - * @flags: tpm transmit flags - bitmap * * Return: - * 0 when the operation is successful. + * >0 when the operation is successful; returns response length * A negative number for system errors (errno). */ -ssize_t tpm_transmit(struct tpm_chip *chip, struct tpm_space *space, - u8 *buf, size_t bufsiz, unsigned int flags) +ssize_t tpm_transfer(struct tpm_chip *chip, u8 *buf, u32 count, size_t bufsiz) { - struct tpm_output_header *header = (void *)buf; int rc; + struct tpm_output_header *header = (void *)buf; + u32 ordinal = be32_to_cpu(*((__be32 *) (buf + 6))); ssize_t len = 0; - u32 count, ordinal; unsigned long stop; - bool need_locality; - - if (!tpm_validate_command(chip, space, buf, bufsiz)) - return -EINVAL; - - if (bufsiz > TPM_BUFSIZE) - bufsiz = TPM_BUFSIZE; - - count = be32_to_cpu(*((__be32 *) (buf + 2))); - ordinal = be32_to_cpu(*((__be32 *) (buf + 6))); - if (count == 0) - return -ENODATA; - if (count > bufsiz) { - dev_err(&chip->dev, - "invalid count value %x %zx\n", count, bufsiz); - return -E2BIG; - } - - if (!(flags & TPM_TRANSMIT_UNLOCKED)) - mutex_lock(&chip->tpm_mutex); - - if (chip->dev.parent) - pm_runtime_get_sync(chip->dev.parent); - - /* Store the decision as chip->locality will be changed. */ - need_locality = chip->locality == -1; - - if (need_locality && chip->ops->request_locality) { - rc = chip->ops->request_locality(chip, 0); - if (rc < 0) - goto out_no_locality; - chip->locality = rc; - } - - rc = tpm2_prepare_space(chip, space, ordinal, buf); - if (rc) - goto out; rc = chip->ops->send(chip, (u8 *) buf, count); if (rc < 0) { dev_err(&chip->dev, - "tpm_transmit: tpm_send: error %d\n", rc); + "tpm_transfer: tpm_send: error %d\n", rc); goto out; } @@ -467,18 +429,81 @@ ssize_t tpm_transmit(struct tpm_chip *chip, struct tpm_space *space, if (len < 0) { rc = len; dev_err(&chip->dev, - "tpm_transmit: tpm_recv: error %d\n", rc); + "tpm_transfer: tpm_recv: error %d\n", rc); goto out; } else if (len < TPM_HEADER_SIZE) { rc = -EFAULT; goto out; } - if (len != be32_to_cpu(header->length)) { + if (len != be32_to_cpu(header->length)) rc = -EFAULT; - goto out; + +out: + return rc ? rc : len; +} +EXPORT_SYMBOL_GPL(tpm_transfer); + +/** + * tmp_transmit - Internal kernel interface to transmit TPM commands. + * + * @chip: TPM chip to use + * @buf: TPM command buffer + * @bufsiz: length of the TPM command buffer + * @flags: tpm transmit flags - bitmap + * + * Return: + * 0 when the operation is successful. + * A negative number for system errors (errno). + */ +ssize_t tpm_transmit(struct tpm_chip *chip, struct tpm_space *space, + u8 *buf, size_t bufsiz, unsigned int flags) +{ + int rc; + ssize_t len = 0; + u32 count, ordinal; + bool need_locality; + + if (!tpm_validate_command(chip, space, buf, bufsiz)) + return -EINVAL; + + if (bufsiz > TPM_BUFSIZE) + bufsiz = TPM_BUFSIZE; + + count = be32_to_cpu(*((__be32 *) (buf + 2))); + ordinal = be32_to_cpu(*((__be32 *) (buf + 6))); + if (count == 0) + return -ENODATA; + if (count > bufsiz) { + dev_err(&chip->dev, + "invalid count value %x %zx\n", count, bufsiz); + return -E2BIG; + } + + if (!(flags & TPM_TRANSMIT_UNLOCKED)) + mutex_lock(&chip->tpm_mutex); + + if (chip->dev.parent) + pm_runtime_get_sync(chip->dev.parent); + + /* Store the decision as chip->locality will be changed. */ + need_locality = chip->locality == -1; + + if (need_locality && chip->ops->request_locality) { + rc = chip->ops->request_locality(chip, 0); + if (rc < 0) + goto out_no_locality; + chip->locality = rc; } + rc = tpm2_prepare_space(chip, space, ordinal, buf); + if (rc) + goto out; + + len = tpm_transfer(chip, buf, count, bufsiz); + if (len < 0) + goto out; + rc = tpm2_commit_space(chip, space, ordinal, buf, &len); out: